🔨 using the SAX-DOM parser
This commit is contained in:
parent
faf2546a15
commit
5beab80553
6 changed files with 173 additions and 166 deletions
|
@ -132,6 +132,10 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
|
|||
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
|
||||
using number_float_t = typename BasicJsonType::number_float_t;
|
||||
|
||||
json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
|
||||
: root(r), allow_exceptions(allow_exceptions_)
|
||||
{}
|
||||
|
||||
bool null() override
|
||||
{
|
||||
handle_value(nullptr);
|
||||
|
@ -204,25 +208,30 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
|
|||
return true;
|
||||
}
|
||||
|
||||
bool parse_error(std::size_t position, const std::string&, const std::string& error_msg) override
|
||||
bool parse_error(std::size_t position, const std::string& token,
|
||||
const std::string& error_msg) override
|
||||
{
|
||||
JSON_THROW(BasicJsonType::parse_error::create(101, position, error_msg));
|
||||
errored = true;
|
||||
if (allow_exceptions)
|
||||
{
|
||||
if (error_msg == "number overflow")
|
||||
{
|
||||
JSON_THROW(BasicJsonType::out_of_range::create(406, "number overflow parsing '" + token + "'"));
|
||||
}
|
||||
else
|
||||
{
|
||||
JSON_THROW(BasicJsonType::parse_error::create(101, position, error_msg));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
BasicJsonType& get_value()
|
||||
bool is_errored() const
|
||||
{
|
||||
return root;
|
||||
return errored;
|
||||
}
|
||||
|
||||
private:
|
||||
/// the parsed JSON value
|
||||
BasicJsonType root;
|
||||
/// stack to model hierarchy of values
|
||||
std::vector<BasicJsonType*> ref_stack;
|
||||
/// helper to hold the reference for the next object element
|
||||
BasicJsonType* object_element = nullptr;
|
||||
|
||||
/*!
|
||||
@invariant If the ref stack is empty, then the passed value will be the new
|
||||
root.
|
||||
|
@ -234,7 +243,6 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
|
|||
{
|
||||
if (ref_stack.empty())
|
||||
{
|
||||
assert(root.is_null());
|
||||
root = BasicJsonType(std::forward<Value>(v));
|
||||
return &root;
|
||||
}
|
||||
|
@ -254,6 +262,17 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// the parsed JSON value
|
||||
BasicJsonType& root;
|
||||
/// stack to model hierarchy of values
|
||||
std::vector<BasicJsonType*> ref_stack;
|
||||
/// helper to hold the reference for the next object element
|
||||
BasicJsonType* object_element = nullptr;
|
||||
/// whether a syntax error occurred
|
||||
bool errored = false;
|
||||
/// whether to throw exceptions in case of errors
|
||||
const bool allow_exceptions = true;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -63,11 +63,10 @@ class parser
|
|||
const parser_callback_t cb = nullptr,
|
||||
const bool allow_exceptions_ = true)
|
||||
: callback(cb), m_lexer(adapter), allow_exceptions(allow_exceptions_)
|
||||
{}
|
||||
|
||||
parser(detail::input_adapter_t adapter, json_sax_t* s)
|
||||
: m_lexer(adapter), sax(s)
|
||||
{}
|
||||
{
|
||||
// read first token
|
||||
get_token();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief public parser interface
|
||||
|
@ -81,31 +80,52 @@ class parser
|
|||
*/
|
||||
void parse(const bool strict, BasicJsonType& result)
|
||||
{
|
||||
// read first token
|
||||
get_token();
|
||||
|
||||
parse_internal(true, result);
|
||||
result.assert_invariant();
|
||||
|
||||
// in strict mode, input must be completely read
|
||||
if (strict)
|
||||
if (callback)
|
||||
{
|
||||
get_token();
|
||||
expect(token_type::end_of_input);
|
||||
parse_internal(true, result);
|
||||
result.assert_invariant();
|
||||
|
||||
// in strict mode, input must be completely read
|
||||
if (strict)
|
||||
{
|
||||
get_token();
|
||||
expect(token_type::end_of_input);
|
||||
}
|
||||
|
||||
// in case of an error, return discarded value
|
||||
if (errored)
|
||||
{
|
||||
result = value_t::discarded;
|
||||
return;
|
||||
}
|
||||
|
||||
// set top-level value to null if it was discarded by the callback
|
||||
// function
|
||||
if (result.is_discarded())
|
||||
{
|
||||
result = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// in case of an error, return discarded value
|
||||
if (errored)
|
||||
else
|
||||
{
|
||||
result = value_t::discarded;
|
||||
return;
|
||||
}
|
||||
json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
|
||||
sax_parse_internal(&sdp);
|
||||
result.assert_invariant();
|
||||
|
||||
// set top-level value to null if it was discarded by the callback
|
||||
// function
|
||||
if (result.is_discarded())
|
||||
{
|
||||
result = nullptr;
|
||||
// in strict mode, input must be completely read
|
||||
if (strict and (get_token() != token_type::end_of_input))
|
||||
{
|
||||
sdp.parse_error(m_lexer.get_position(),
|
||||
m_lexer.get_token_string(),
|
||||
exception_message(token_type::end_of_input));
|
||||
}
|
||||
|
||||
// in case of an error, return discarded value
|
||||
if (sdp.is_errored())
|
||||
{
|
||||
result = value_t::discarded;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,9 +137,6 @@ class parser
|
|||
*/
|
||||
bool accept(const bool strict = true)
|
||||
{
|
||||
// read first token
|
||||
get_token();
|
||||
|
||||
if (not accept_internal())
|
||||
{
|
||||
return false;
|
||||
|
@ -129,12 +146,9 @@ class parser
|
|||
return not strict or (get_token() == token_type::end_of_input);
|
||||
}
|
||||
|
||||
bool sax_parse()
|
||||
bool sax_parse(json_sax_t* sax)
|
||||
{
|
||||
// read first token
|
||||
get_token();
|
||||
|
||||
return sax_parse_internal();
|
||||
return sax_parse_internal(sax);
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -535,7 +549,7 @@ class parser
|
|||
}
|
||||
}
|
||||
|
||||
bool sax_parse_internal()
|
||||
bool sax_parse_internal(json_sax_t* sax)
|
||||
{
|
||||
switch (last_token)
|
||||
{
|
||||
|
@ -584,7 +598,7 @@ class parser
|
|||
|
||||
// parse value
|
||||
get_token();
|
||||
if (not sax_parse_internal())
|
||||
if (not sax_parse_internal(sax))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -631,7 +645,7 @@ class parser
|
|||
while (true)
|
||||
{
|
||||
// parse value
|
||||
if (not sax_parse_internal())
|
||||
if (not sax_parse_internal(sax))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -782,8 +796,6 @@ class parser
|
|||
bool errored = false;
|
||||
/// whether to throw exceptions in case of errors
|
||||
const bool allow_exceptions = true;
|
||||
/// associated SAX parse event receiver
|
||||
json_sax_t* sax = nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5930,12 +5930,12 @@ class basic_json
|
|||
|
||||
static bool sax_parse(detail::input_adapter i, json_sax_t* sax)
|
||||
{
|
||||
return parser(i, sax).sax_parse();
|
||||
return parser(i).sax_parse(sax);
|
||||
}
|
||||
|
||||
static bool sax_parse(detail::input_adapter& i, json_sax_t* sax)
|
||||
{
|
||||
return parser(i, sax).sax_parse();
|
||||
return parser(i).sax_parse(sax);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -6013,7 +6013,7 @@ class basic_json
|
|||
typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
|
||||
static bool sax_parse(IteratorType first, IteratorType last, json_sax_t* sax)
|
||||
{
|
||||
return parser(detail::input_adapter(first, last), sax).sax_parse();
|
||||
return parser(detail::input_adapter(first, last)).sax_parse(sax);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue