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_)
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:
344 case token_type::end_of_input:
351 "attempting to parse an empty input; check that your input string or stream contains the expected JSON",
nullptr));
358 case token_type::uninitialized:
359 case token_type::end_array:
360 case token_type::end_object:
361 case token_type::name_separator:
362 case token_type::value_separator:
363 case token_type::literal_or_value:
374 skip_to_state_evaluation =
false;
387 if (get_token() == token_type::value_separator)
408 skip_to_state_evaluation =
true;
420 if (get_token() == token_type::value_separator)
462 skip_to_state_evaluation =
true;
473 token_type get_token()
475 return last_token = m_lexer.
scan();
478 std::string exception_message(
const token_type expected,
const std::string&
context)
480 std::string error_msg =
"syntax error ";
489 if (last_token == token_type::parse_error)
499 if (expected != token_type::uninitialized)
511 token_type last_token = token_type::uninitialized;
515 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:652
Definition json_sax.h:345
bool parse_error(std::size_t, const std::string &, const Exception &ex)
Definition json_sax.h:533
constexpr bool is_errored() const
Definition json_sax.h:545
SAX implementation to create a JSON value from SAX events.
Definition json_sax.h:162
bool parse_error(std::size_t, const std::string &, const Exception &ex)
Definition json_sax.h:283
constexpr bool is_errored() const
Definition json_sax.h:295
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:63
lexical analysis
Definition lexer.h:113
JSON_HEDLEY_RETURNS_NON_NULL constexpr const char * get_error_message() const noexcept
return syntax error message
Definition lexer.h:1474
std::string get_token_string() const
return the last read token (for errors only).
Definition lexer.h:1449
constexpr number_integer_t get_number_integer() const noexcept
return integer value
Definition lexer.h:1413
constexpr position_t get_position() const noexcept
return position of last read token
Definition lexer.h:1441
token_type scan()
Definition lexer.h:1510
constexpr number_unsigned_t get_number_unsigned() const noexcept
return unsigned integer value
Definition lexer.h:1419
typename lexer_base< BasicJsonType >::token_type token_type
Definition lexer.h:122
string_t & get_string()
return current string value (implicitly resets the token; useful only once)
Definition lexer.h:1431
constexpr number_float_t get_number_float() const noexcept
return floating-point value
Definition lexer.h:1425
static out_of_range create(int id_, const std::string &what_arg, BasicJsonContext context)
Definition exceptions.h:228
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:149
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:200
detail namespace with internal helper functions
Definition input_adapters.h:32
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
@ strict
throw a type_error exception in case of invalid UTF-8
std::function< bool(int, parse_event_t, BasicJsonType &)> parser_callback_t
Definition parser.h:51
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
std::size_t chars_read_total
the total number of characters read
Definition position_t.h:23