50template<
typename BasicJsonType>
59template<
typename BasicJsonType,
typename InputAdapterType>
62 using number_integer_t =
typename BasicJsonType::number_integer_t;
63 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
64 using number_float_t =
typename BasicJsonType::number_float_t;
65 using string_t =
typename BasicJsonType::string_t;
71 explicit parser(InputAdapterType&& adapter,
73 const bool allow_exceptions_ =
true,
74 const bool skip_comments =
false)
76 , m_lexer(
std::move(adapter), skip_comments)
77 , allow_exceptions(allow_exceptions_)
93 void parse(
const bool strict, BasicJsonType& result)
98 sax_parse_internal(&sdp);
101 if (
strict && (get_token() != token_type::end_of_input))
106 exception_message(token_type::end_of_input,
"value"),
nullptr));
118 if (result.is_discarded())
126 sax_parse_internal(&sdp);
129 if (
strict && (get_token() != token_type::end_of_input))
144 result.assert_invariant();
159 template<
typename SAX>
164 const bool result = sax_parse_internal(sax);
167 if (result &&
strict && (get_token() != token_type::end_of_input))
178 template<
typename SAX>
180 bool sax_parse_internal(SAX* sax)
184 std::vector<bool> states;
186 bool skip_to_state_evaluation =
false;
190 if (!skip_to_state_evaluation)
195 case token_type::begin_object:
203 if (get_token() == token_type::end_object)
233 states.push_back(
false);
240 case token_type::begin_array:
248 if (get_token() == token_type::end_array)
258 states.push_back(
true);
264 case token_type::value_float:
283 case token_type::literal_false:
292 case token_type::literal_null:
301 case token_type::literal_true:
310 case token_type::value_integer:
319 case token_type::value_string:
328 case token_type::value_unsigned:
337 case token_type::parse_error:
345 case token_type::uninitialized:
346 case token_type::end_array:
347 case token_type::end_object:
348 case token_type::name_separator:
349 case token_type::value_separator:
350 case token_type::end_of_input:
351 case token_type::literal_or_value:
362 skip_to_state_evaluation =
false;
375 if (get_token() == token_type::value_separator)
396 skip_to_state_evaluation =
true;
408 if (get_token() == token_type::value_separator)
450 skip_to_state_evaluation =
true;
461 token_type get_token()
463 return last_token = m_lexer.
scan();
466 std::string exception_message(
const token_type expected,
const std::string& context)
468 std::string error_msg =
"syntax error ";
470 if (!context.empty())
472 error_msg +=
concat(
"while parsing ", context,
' ');
477 if (last_token == token_type::parse_error)
487 if (expected != token_type::uninitialized)
497 const parser_callback_t<BasicJsonType> callback =
nullptr;
499 token_type last_token = token_type::uninitialized;
503 const bool allow_exceptions =
true;
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
Definition: json_sax.h:653
Definition: json_sax.h:346
bool parse_error(std::size_t, const std::string &, const Exception &ex)
Definition: json_sax.h:534
constexpr bool is_errored() const
Definition: json_sax.h:546
SAX implementation to create a JSON value from SAX events.
Definition: json_sax.h:163
bool parse_error(std::size_t, const std::string &, const Exception &ex)
Definition: json_sax.h:284
constexpr bool is_errored() const
Definition: json_sax.h:296
JSON_HEDLEY_RETURNS_NON_NULL static JSON_HEDLEY_CONST const char * token_type_name(const token_type t) noexcept
return name of values of type token_type (only used for errors)
Definition: lexer.h:62
lexical analysis
Definition: lexer.h:112
JSON_HEDLEY_RETURNS_NON_NULL constexpr const char * get_error_message() const noexcept
return syntax error message
Definition: lexer.h:1473
std::string get_token_string() const
return the last read token (for errors only).
Definition: lexer.h:1448
constexpr number_integer_t get_number_integer() const noexcept
return integer value
Definition: lexer.h:1412
constexpr position_t get_position() const noexcept
return position of last read token
Definition: lexer.h:1440
token_type scan()
Definition: lexer.h:1509
constexpr number_unsigned_t get_number_unsigned() const noexcept
return unsigned integer value
Definition: lexer.h:1418
typename lexer_base< BasicJsonType >::token_type token_type
Definition: lexer.h:121
string_t & get_string()
return current string value (implicitly resets the token; useful only once)
Definition: lexer.h:1430
constexpr number_float_t get_number_float() const noexcept
return floating-point value
Definition: lexer.h:1424
static out_of_range create(int id_, const std::string &what_arg, BasicJsonContext context)
Definition: exceptions.h:226
static parse_error create(int id_, const position_t &pos, const std::string &what_arg, BasicJsonContext context)
create a parse error exception
Definition: exceptions.h:147
syntax analysis
Definition: parser.h:61
parser(InputAdapterType &&adapter, const parser_callback_t< BasicJsonType > cb=nullptr, const bool allow_exceptions_=true, const bool skip_comments=false)
a parser reading from an input adapter
Definition: parser.h:71
void parse(const bool strict, BasicJsonType &result)
public parser interface
Definition: parser.h:93
bool accept(const bool strict=true)
public accept interface
Definition: parser.h:153
bool sax_parse(SAX *sax, const bool strict=true)
Definition: parser.h:161
#define JSON_HEDLEY_LIKELY(expr)
Definition: hedley.h:1395
#define JSON_HEDLEY_NON_NULL(...)
Definition: hedley.h:1288
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: hedley.h:1396
#define JSON_ASSERT(x)
Definition: macro_scope.h:192
detail namespace with internal helper functions
Definition: xchar.h:20
std::function< bool(int, parse_event_t, BasicJsonType &)> parser_callback_t
Definition: parser.h:52
OutStringType concat(Args &&... args)
Definition: string_concat.h:137
parse_event_t
Definition: parser.h:35
@ key
the parser read a key of a value in an object
@ array_end
the parser read ] and finished processing a JSON array
@ array_start
the parser read [ and started to process a JSON array
@ object_start
the parser read { and started to process a JSON object
@ object_end
the parser read } and finished processing a JSON object
@ discarded
discarded by the parser callback function
bool isfinite(T)
Definition: chrono.h:1600
@ strict
throw a type_error exception in case of invalid UTF-8