🔨 simplified error handling in parser

parent f2cdb3d5
...@@ -12902,7 +12902,7 @@ scan_number_done: ...@@ -12902,7 +12902,7 @@ scan_number_done:
default: default:
{ {
// the last token was unexpected // the last token was unexpected
unexpect(last_token); unexpect();
} }
} }
...@@ -13040,51 +13040,49 @@ scan_number_done: ...@@ -13040,51 +13040,49 @@ scan_number_done:
/// get next token from lexer /// get next token from lexer
typename lexer::token_type get_token() typename lexer::token_type get_token()
{ {
last_token = m_lexer.scan(); return (last_token = m_lexer.scan());
return last_token;
} }
/*! /*!
@throw parse_error.101 if expected token did not occur @throw parse_error.101 if expected token did not occur
*/ */
void expect(typename lexer::token_type t) const void expect(typename lexer::token_type t)
{ {
if (JSON_UNLIKELY(t != last_token)) if (JSON_UNLIKELY(t != last_token))
{ {
std::string error_msg = "syntax error - "; errored = true;
if (last_token == lexer::token_type::parse_error) expected = t;
{ throw_exception();
error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" + m_lexer.get_token_string() + "'";
}
else
{
error_msg += "unexpected " + std::string(lexer::token_type_name(last_token));
}
error_msg += "; expected " + std::string(lexer::token_type_name(t));
JSON_THROW(parse_error::create(101, m_lexer.get_position(), error_msg));
} }
} }
/*! /*!
@throw parse_error.101 if unexpected token occurred @throw parse_error.101 if unexpected token occurred
*/ */
void unexpect(typename lexer::token_type t) const void unexpect()
{ {
if (JSON_UNLIKELY(t == last_token)) errored = true;
throw_exception();
}
[[noreturn]] void throw_exception() const
{ {
std::string error_msg = "syntax error - "; std::string error_msg = "syntax error - ";
if (last_token == lexer::token_type::parse_error) if (last_token == lexer::token_type::parse_error)
{ {
error_msg += std::string(m_lexer.get_error_message()) + "; last read '" + m_lexer.get_token_string() + "'"; error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" + m_lexer.get_token_string() + "'";
} }
else else
{ {
error_msg += "unexpected " + std::string(lexer::token_type_name(last_token)); error_msg += "unexpected " + std::string(lexer::token_type_name(last_token));
} }
JSON_THROW(parse_error::create(101, m_lexer.get_position(), error_msg)); if (expected != lexer::token_type::uninitialized)
{
error_msg += "; expected " + std::string(lexer::token_type_name(expected));
} }
JSON_THROW(parse_error::create(101, m_lexer.get_position(), error_msg));
} }
private: private:
...@@ -13096,6 +13094,10 @@ scan_number_done: ...@@ -13096,6 +13094,10 @@ scan_number_done:
typename lexer::token_type last_token = lexer::token_type::uninitialized; typename lexer::token_type last_token = lexer::token_type::uninitialized;
/// the lexer /// the lexer
lexer m_lexer; lexer m_lexer;
/// whether a syntax error occurred
bool errored = false;
/// possible reason for the syntax error
typename lexer::token_type expected = lexer::token_type::uninitialized;
}; };
public: public:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment