header_utils
Loading...
Searching...
No Matches
sexps.h
1
4
5#pragma once
6
7#include "string_ops.h"
8#include "parsing.h"
9#include "ranges.h"
10#include <nlohmann/json.hpp>
11
13{
15 auto consume_value(std::string_view& sexp_str, std::array<char, 2> braces = { '[', ']' }) -> nlohmann::json;
16
18 auto consume_atom(std::string_view& sexp_str, char end_brace = ']') -> nlohmann::json;
19
22 auto consume_list(std::string_view& sexp_str, std::array<char, 2> braces = { '[', ']' }) -> nlohmann::json;
23
24 inline auto parse_value(std::string_view sexp_str, std::array<char, 2> braces = { '[', ']' }) -> nlohmann::json { return consume_value(sexp_str, braces); }
25 inline auto parse_atom(std::string_view sexp_str, char end_brace = ']') -> nlohmann::json { return consume_atom(sexp_str, end_brace); }
26 inline auto parse_list(std::string_view sexp_str, std::array<char, 2> braces = { '[', ']' }) -> nlohmann::json { return consume_list(sexp_str, braces); }
27}
28
30
32{
33 inline auto consume_atom(std::string_view& sexp_str, char end_brace) -> nlohmann::json
34 {
35 string_ops::trim_whitespace_left(sexp_str);
36
38 if (sexp_str.starts_with('\''))
39 return parsing::consume_c_string<'\''>(sexp_str).second;
40
41 if (sexp_str.starts_with('"'))
42 return parsing::consume_c_string<'"'>(sexp_str).second;
43
46 return nlohmann::json(",");
47
51
52 if (result == "true") return true;
53 if (result == "false") return false;
54 if (result == "null") return nullptr;
55
57 {
58 nlohmann::json::number_integer_t num_result;
59 const auto fcres = string_ops::from_chars(result, num_result);
60 if (fcres.ec == std::errc{} && fcres.ptr == sexp_str.data())
61 {
62 string_ops::trim_whitespace_left(sexp_str);
63 return num_result;
64 }
65 }
66
67 {
68 nlohmann::json::number_unsigned_t num_result;
69 const auto fcres = string_ops::from_chars(result, num_result);
70 if (fcres.ec == std::errc{} && fcres.ptr == sexp_str.data())
71 {
72 string_ops::trim_whitespace_left(sexp_str);
73 return num_result;
74 }
75 }
76
77 {
78 nlohmann::json::number_float_t num_result;
79 const auto fcres = string_ops::from_chars(result, num_result);
80 if (fcres.ec == std::errc{} && fcres.ptr == sexp_str.data())
81 {
82 string_ops::trim_whitespace_left(sexp_str);
83 return num_result;
84 }
85 }
86
87 string_ops::trim_whitespace_left(sexp_str);
88 return result;
89 }
90
91 inline auto consume_list(std::string_view& sexp_str, std::array<char, 2> braces) -> nlohmann::json
92 {
93 nlohmann::json result = nlohmann::json::array();
94 string_ops::trim_whitespace_left(sexp_str);
95 while (!sexp_str.empty() && !sexp_str.starts_with(braces[1]))
96 {
97 result.push_back(consume_value(sexp_str, braces));
98 string_ops::trim_whitespace_left(sexp_str);
99 }
100 std::ignore = string_ops::consume(sexp_str, braces[1]);
101 return result;
102 }
103
104 inline auto consume_value(std::string_view& sexp_str, std::array<char, 2> braces) -> nlohmann::json
105 {
106 string_ops::trim_whitespace_left(sexp_str);
109 return consume_atom(sexp_str, braces[1]);
110 }
111
112}
constexpr auto whitespace_chars
All characters that match ascii::isspace.
Definition string_ops.h:377
constexpr auto bit_count
Equal to the number of bits in the type.
Definition bits.h:33
auto from_chars(std::string_view str, T &value, const int base=10) noexcept
A version of std::from_chars that takes a std::string_view as the first argument.
char consume(std::string_view &str)
Consumes and returns the first character in the str, or \0 if no more characters.
Definition string_ops.h:652
std::string_view consume_until_any(std::string_view &str, ARGS &&... args)
Consumes characters from the beginning of str until one is equal to any in the parameter pack,...
Definition string_ops.h:834
auto consume_list(std::string_view &sexp_str, std::array< char, 2 > braces={ '[', ']' }) -> nlohmann::json
Consumes space-delimited elements until close-brace or end-of-string.
Definition sexps.h:91
auto consume_value(std::string_view &sexp_str, std::array< char, 2 > braces={ '[', ']' }) -> nlohmann::json
Consumes [list] or atom; a ',' (comma character) is considered its own atom.
Definition sexps.h:104
auto consume_atom(std::string_view &sexp_str, char end_brace=']') -> nlohmann::json
Consumes a space-delimited word, ‘'string’ "literal", or14.46` number.
Definition sexps.h:33