diff --git a/src/json.hpp b/src/json.hpp index ebf7dc7542..517558c291 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -11132,7 +11132,8 @@ class basic_json name_separator, ///< the name separator `:` value_separator, ///< the value separator `,` parse_error, ///< indicating a parse error - end_of_input ///< indicating the end of the input buffer + end_of_input, ///< indicating the end of the input buffer + literal_or_value ///< a literal or the begin of a value (only for diagnostics) }; /// return name of values of type token_type (only used for errors) @@ -11170,6 +11171,8 @@ class basic_json return ""; case token_type::end_of_input: return "end of input"; + case token_type::literal_or_value: + return "'[', '{', or a literal"; default: { // catch non-enum values @@ -12903,10 +12906,16 @@ class basic_json break; } + case lexer::token_type::parse_error: + { + // using "uninitialized" to avoid "expected" message + expect(lexer::token_type::uninitialized); + } + default: { - // the last token was unexpected - unexpect(); + // we expected a value + expect(lexer::token_type::literal_or_value); } } @@ -13060,15 +13069,6 @@ class basic_json } } - /*! - @throw parse_error.101 if unexpected token occurred - */ - void unexpect() - { - errored = true; - throw_exception(); - } - [[noreturn]] void throw_exception() const { std::string error_msg = "syntax error - "; diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index a95d164202..1cca2adf15 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -694,15 +694,15 @@ TEST_CASE("parser class") CHECK_THROWS_AS(json::parser(json::input_adapter::create(std::string("[1,]"))).parse(), json::parse_error); CHECK_THROWS_AS(json::parser(json::input_adapter::create(std::string("]"))).parse(), json::parse_error); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("["))).parse(), - "[json.exception.parse_error.101] parse error at 2: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 2: syntax error - unexpected end of input; expected '[', '{', or a literal"); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("[1"))).parse(), "[json.exception.parse_error.101] parse error at 3: syntax error - unexpected end of input; expected ']'"); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("[1,"))).parse(), - "[json.exception.parse_error.101] parse error at 4: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 4: syntax error - unexpected end of input; expected '[', '{', or a literal"); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("[1,]"))).parse(), - "[json.exception.parse_error.101] parse error at 4: syntax error - unexpected ']'"); + "[json.exception.parse_error.101] parse error at 4: syntax error - unexpected ']'; expected '[', '{', or a literal"); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("]"))).parse(), - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected ']'"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected ']'; expected '[', '{', or a literal"); // missing/unexpected end of object CHECK_THROWS_AS(json::parser(json::input_adapter::create(std::string("{"))).parse(), json::parse_error); @@ -716,13 +716,13 @@ TEST_CASE("parser class") CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("{\"foo\""))).parse(), "[json.exception.parse_error.101] parse error at 7: syntax error - unexpected end of input; expected ':'"); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("{\"foo\":"))).parse(), - "[json.exception.parse_error.101] parse error at 8: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 8: syntax error - unexpected end of input; expected '[', '{', or a literal"); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("{\"foo\":}"))).parse(), - "[json.exception.parse_error.101] parse error at 8: syntax error - unexpected '}'"); + "[json.exception.parse_error.101] parse error at 8: syntax error - unexpected '}'; expected '[', '{', or a literal"); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("{\"foo\":1,}"))).parse(), "[json.exception.parse_error.101] parse error at 10: syntax error - unexpected '}'; expected string literal"); CHECK_THROWS_WITH(json::parser(json::input_adapter::create(std::string("}"))).parse(), - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected '}'"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected '}'; expected '[', '{', or a literal"); // missing/unexpected end of string CHECK_THROWS_AS(json::parser(json::input_adapter::create(std::string("\""))).parse(), json::parse_error); diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 4d1231c220..d4017c7afc 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -597,7 +597,7 @@ TEST_CASE("regression tests") // a parse error because of the EOF. CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("issue #367 - behavior of operator>> should more closely resemble that of built-in overloads") @@ -608,7 +608,7 @@ TEST_CASE("regression tests") json j; CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("(whitespace)") @@ -618,7 +618,7 @@ TEST_CASE("regression tests") json j; CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("one value") @@ -631,7 +631,7 @@ TEST_CASE("regression tests") CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("one value + whitespace") @@ -644,7 +644,7 @@ TEST_CASE("regression tests") CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("whitespace + one value") @@ -657,7 +657,7 @@ TEST_CASE("regression tests") CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("three values") @@ -674,7 +674,7 @@ TEST_CASE("regression tests") CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("literals without whitespace") @@ -693,7 +693,7 @@ TEST_CASE("regression tests") CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("example from #529") @@ -708,7 +708,7 @@ TEST_CASE("regression tests") CHECK_THROWS_AS(ss >> j, json::parse_error); CHECK_THROWS_WITH(ss >> j, - "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input"); + "[json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal"); } SECTION("second example from #529")