Skip to content

Commit

Permalink
🔨 removed unexpect function
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jun 21, 2017
1 parent 2fd214c commit 6f3bebf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
24 changes: 12 additions & 12 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -11170,6 +11171,8 @@ class basic_json
return "<parse error>";
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
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 - ";
Expand Down
14 changes: 7 additions & 7 deletions test/src/unit-class_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 6f3bebf

Please sign in to comment.