54 using number_integer_t =
typename BasicJsonType::number_integer_t;
55 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
56 using number_float_t =
typename BasicJsonType::number_float_t;
57 using string_t =
typename BasicJsonType::string_t;
59 using token_type =
typename lexer_t::token_type;
63 explicit parser(InputAdapterType&& adapter,
64 const parser_callback_t<BasicJsonType> cb =
nullptr,
65 const bool allow_exceptions_ =
true,
66 const bool skip_comments =
false)
68 , m_lexer(std::move(adapter), skip_comments)
69 , allow_exceptions(allow_exceptions_)
90 sax_parse_internal(&sdp);
93 if (
strict && (get_token() != token_type::end_of_input))
98 exception_message(token_type::end_of_input,
"value"), BasicJsonType()));
102 if (sdp.is_errored())
110 if (result.is_discarded())
118 sax_parse_internal(&sdp);
121 if (
strict && (get_token() != token_type::end_of_input))
129 if (sdp.is_errored())
136 result.assert_invariant();
148 return sax_parse(&sax_acceptor,
strict);
151 template<
typename SAX>
152 JSON_HEDLEY_NON_NULL(2)
153 bool sax_parse(SAX* sax, const
bool strict = true)
156 const bool result = sax_parse_internal(sax);
159 if (result &&
strict && (get_token() != token_type::end_of_input))
170 template<
typename SAX>
171 JSON_HEDLEY_NON_NULL(2)
172 bool sax_parse_internal(SAX* sax)
176 std::vector<bool> states;
178 bool skip_to_state_evaluation =
false;
182 if (!skip_to_state_evaluation)
187 case token_type::begin_object:
189 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1))))
195 if (get_token() == token_type::end_object)
197 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
205 if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
211 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.
get_string())))
217 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
225 states.push_back(
false);
232 case token_type::begin_array:
234 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1))))
240 if (get_token() == token_type::end_array)
242 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
250 states.push_back(
true);
256 case token_type::value_float:
260 if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
264 out_of_range::create(406,
"number overflow parsing '" + m_lexer.
get_token_string() +
"'", BasicJsonType()));
267 if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.
get_string())))
275 case token_type::literal_false:
277 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(
false)))
284 case token_type::literal_null:
286 if (JSON_HEDLEY_UNLIKELY(!sax->null()))
293 case token_type::literal_true:
295 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(
true)))
302 case token_type::value_integer:
311 case token_type::value_string:
313 if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.
get_string())))
320 case token_type::value_unsigned:
329 case token_type::parse_error:
337 case token_type::uninitialized:
338 case token_type::end_array:
339 case token_type::end_object:
340 case token_type::name_separator:
341 case token_type::value_separator:
342 case token_type::end_of_input:
343 case token_type::literal_or_value:
354 skip_to_state_evaluation =
false;
367 if (get_token() == token_type::value_separator)
375 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
377 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
386 JSON_ASSERT(!states.empty());
388 skip_to_state_evaluation =
true;
400 if (get_token() == token_type::value_separator)
403 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
410 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.
get_string())))
416 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
429 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
431 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
440 JSON_ASSERT(!states.empty());
442 skip_to_state_evaluation =
true;
453 token_type get_token()
455 return last_token = m_lexer.scan();
458 std::string exception_message(
const token_type expected,
const std::string& context)
460 std::string error_msg =
"syntax error ";
462 if (!context.empty())
464 error_msg +=
"while parsing " + context +
" ";
469 if (last_token == token_type::parse_error)
479 if (expected != token_type::uninitialized)
489 const parser_callback_t<BasicJsonType> callback =
nullptr;
491 token_type last_token = token_type::uninitialized;
495 const bool allow_exceptions =
true;