10#include <nlohmann/json.hpp>
21 auto consume_value(std::string_view& sexp_str, std::array<char, 2> braces = {
'[',
']' }) -> nlohmann::json;
24 auto consume_atom(std::string_view& sexp_str,
char end_brace =
']') -> nlohmann::json;
28 auto consume_list(std::string_view& sexp_str, std::array<char, 2> braces = {
'[',
']' }) -> nlohmann::json;
30 inline auto parse_value(std::string_view sexp_str, std::array<char, 2> braces = {
'[',
']' }) -> nlohmann::json {
return consume_value(sexp_str, braces); }
31 inline auto parse_atom(std::string_view sexp_str,
char end_brace =
']') -> nlohmann::json {
return consume_atom(sexp_str, end_brace); }
32 inline auto parse_list(std::string_view sexp_str, std::array<char, 2> braces = {
'[',
']' }) -> nlohmann::json {
return consume_list(sexp_str, braces); }
41 inline auto consume_atom(std::string_view& sexp_str,
char end_brace) -> nlohmann::json
43 string_ops::trim_whitespace_left(sexp_str);
46 if (sexp_str.starts_with(
'\''))
47 return parsing::consume_c_string<'\''>(sexp_str).second;
49 if (sexp_str.starts_with(
'"'))
50 return parsing::consume_c_string<
'"'>(sexp_str).second;
54 return nlohmann::json(
",");
60 if (result ==
"true")
return true;
61 if (result ==
"false")
return false;
62 if (result ==
"null")
return nullptr;
66 nlohmann::json::number_integer_t num_result;
68 if (fcres.ec == std::errc{} && fcres.ptr == sexp_str.data())
70 string_ops::trim_whitespace_left(sexp_str);
76 nlohmann::json::number_unsigned_t num_result;
78 if (fcres.ec == std::errc{} && fcres.ptr == sexp_str.data())
80 string_ops::trim_whitespace_left(sexp_str);
86 nlohmann::json::number_float_t num_result;
88 if (fcres.ec == std::errc{} && fcres.ptr == sexp_str.data())
90 string_ops::trim_whitespace_left(sexp_str);
95 string_ops::trim_whitespace_left(sexp_str);
100 inline auto consume_list(std::string_view& sexp_str, std::array<char, 2> braces) -> nlohmann::json
102 nlohmann::json result = nlohmann::json::array();
103 string_ops::trim_whitespace_left(sexp_str);
104 while (!sexp_str.empty() && !sexp_str.starts_with(braces[1]))
107 string_ops::trim_whitespace_left(sexp_str);
113 inline auto consume_value(std::string_view& sexp_str, std::array<char, 2> braces) -> nlohmann::json
115 string_ops::trim_whitespace_left(sexp_str);
constexpr auto whitespace_chars
All characters that match ascii::isspace.
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.
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.
auto consume_atom(std::string_view &sexp_str, char end_brace=']') -> nlohmann::json
Consumes a space-delimited word, 'string' "literal", or 14.46 number.
char consume(std::string_view &str)
Consumes and returns the first character in the str, or \0 if no more characters.
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,...
constexpr 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.