| 8682 | */ |
| 8683 | template<typename BasicJsonType> |
| 8684 | class parser |
| 8685 | { |
| 8686 | using number_integer_t = typename BasicJsonType::number_integer_t; |
| 8687 | using number_unsigned_t = typename BasicJsonType::number_unsigned_t; |
| 8688 | using number_float_t = typename BasicJsonType::number_float_t; |
| 8689 | using string_t = typename BasicJsonType::string_t; |
| 8690 | using lexer_t = lexer<BasicJsonType>; |
| 8691 | using token_type = typename lexer_t::token_type; |
| 8692 | |
| 8693 | public: |
| 8694 | enum class parse_event_t : uint8_t |
| 8695 | { |
| 8696 | /// the parser read `{` and started to process a JSON object |
| 8697 | object_start, |
| 8698 | /// the parser read `}` and finished processing a JSON object |
| 8699 | object_end, |
| 8700 | /// the parser read `[` and started to process a JSON array |
| 8701 | array_start, |
| 8702 | /// the parser read `]` and finished processing a JSON array |
| 8703 | array_end, |
| 8704 | /// the parser read a key of a value in an object |
| 8705 | key, |
| 8706 | /// the parser finished reading a JSON value |
| 8707 | value |
| 8708 | }; |
| 8709 | |
| 8710 | using parser_callback_t = |
| 8711 | std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>; |
| 8712 | |
| 8713 | /// a parser reading from an input adapter |
| 8714 | explicit parser(detail::input_adapter_t&& adapter, |
| 8715 | const parser_callback_t cb = nullptr, |
| 8716 | const bool allow_exceptions_ = true) |
| 8717 | : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) |
| 8718 | { |
| 8719 | // read first token |
| 8720 | get_token(); |
| 8721 | } |
| 8722 | |
| 8723 | /*! |
| 8724 | @brief public parser interface |
| 8725 | |
| 8726 | @param[in] strict whether to expect the last token to be EOF |
| 8727 | @param[in,out] result parsed JSON value |
| 8728 | |
| 8729 | @throw parse_error.101 in case of an unexpected token |
| 8730 | @throw parse_error.102 if to_unicode fails or surrogate error |
| 8731 | @throw parse_error.103 if to_unicode fails |
| 8732 | */ |
| 8733 | void parse(const bool strict, BasicJsonType& result) |
| 8734 | { |
| 8735 | if (callback) |
| 8736 | { |
| 8737 | json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions); |
| 8738 | sax_parse_internal(&sdp); |
| 8739 | result.assert_invariant(); |
| 8740 | |
| 8741 | // in strict mode, input must be completely read |