1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-27 00:41:05 +03:00

started working on #458

a simple acceptor function
This commit is contained in:
Niels Lohmann
2017-04-24 17:46:21 +02:00
parent cfc2e8391c
commit 8b9f51179e
2 changed files with 589 additions and 0 deletions

View File

@ -12501,6 +12501,7 @@ scan_number_done:
@brief public parser interface
@param[in] strict whether to expect the last token to be EOF
@return parsed JSON value
@throw parse_error.101 in case of an unexpected token
@throw parse_error.102 if to_unicode fails or surrogate error
@ -12524,6 +12525,30 @@ scan_number_done:
return result.is_discarded() ? basic_json() : std::move(result);
}
/*!
@brief public accept interface
@param[in] strict whether to expect the last token to be EOF
@return whether the input is a proper JSON text
*/
bool accept(const bool strict = true)
{
// read first token
get_token();
if (not accept_internal())
{
return false;
}
if (strict and last_token != lexer::token_type::end_of_input)
{
return false;
}
return true;
}
private:
/*!
@brief the actual parser
@ -12745,6 +12770,125 @@ scan_number_done:
return result;
}
/*!
@brief the acutal acceptor
*/
bool accept_internal()
{
switch (last_token)
{
case lexer::token_type::begin_object:
{
// read next token
get_token();
// closing } -> we are done
if (last_token == lexer::token_type::end_object)
{
get_token();
return true;
}
// parse values
while (true)
{
// parse key
if (last_token != lexer::token_type::value_string)
{
return false;
}
// parse separator (:)
get_token();
if (last_token != lexer::token_type::name_separator)
{
return false;
}
// parse value
get_token();
if (not accept_internal())
{
return false;
}
// comma -> next value
if (last_token == lexer::token_type::value_separator)
{
get_token();
continue;
}
// closing }
if (last_token != lexer::token_type::end_object)
{
return false;
}
get_token();
return true;
}
}
case lexer::token_type::begin_array:
{
// read next token
get_token();
// closing ] -> we are done
if (last_token == lexer::token_type::end_array)
{
get_token();
return true;
}
// parse values
while (true)
{
// parse value
if (not accept_internal())
{
return false;
}
// comma -> next value
if (last_token == lexer::token_type::value_separator)
{
get_token();
continue;
}
// closing ]
if (last_token != lexer::token_type::end_array)
{
return false;
}
get_token();
return true;
}
}
case lexer::token_type::literal_null:
case lexer::token_type::value_string:
case lexer::token_type::literal_true:
case lexer::token_type::literal_false:
case lexer::token_type::value_unsigned:
case lexer::token_type::value_integer:
case lexer::token_type::value_float:
{
get_token();
return true;
}
default:
{
// the last token was unexpected
return false;
}
}
}
/// get next token from lexer
typename lexer::token_type get_token()
{