🚧 extend API

This commit is contained in:
Niels Lohmann 2020-06-17 22:03:14 +02:00
parent e9bfcf7255
commit 74520d8bb0
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
5 changed files with 148 additions and 66 deletions

View file

@ -37,11 +37,11 @@ using nlohmann::json;
namespace
{
// shortcut to scan a string literal
json::lexer::token_type scan_string(const char* s);
json::lexer::token_type scan_string(const char* s)
json::lexer::token_type scan_string(const char* s, const bool ignore_comments = false);
json::lexer::token_type scan_string(const char* s, const bool ignore_comments)
{
auto ia = nlohmann::detail::input_adapter(s);
return nlohmann::detail::lexer<json, decltype(ia)>(std::move(ia)).scan();
return nlohmann::detail::lexer<json, decltype(ia)>(std::move(ia), ignore_comments).scan();
}
}
@ -163,9 +163,6 @@ TEST_CASE("lexer class")
break;
}
// case ('/'):
// break;
// anything else is not expected
default:
{
@ -185,18 +182,39 @@ TEST_CASE("lexer class")
CHECK((scan_string(s.c_str()) == json::lexer::token_type::value_string));
}
// SECTION("ignore comments")
// {
// CHECK((scan_string("/") == json::lexer::token_type::parse_error));
//
// CHECK((scan_string("/!") == json::lexer::token_type::parse_error));
// CHECK((scan_string("/*") == json::lexer::token_type::parse_error));
// CHECK((scan_string("/**") == json::lexer::token_type::parse_error));
//
// CHECK((scan_string("//") == json::lexer::token_type::end_of_input));
// CHECK((scan_string("/**/") == json::lexer::token_type::end_of_input));
// CHECK((scan_string("/** /") == json::lexer::token_type::parse_error));
//
// CHECK((scan_string("/***/") == json::lexer::token_type::end_of_input));
// }
SECTION("fail on comments")
{
CHECK((scan_string("/", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/!", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/*", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/**", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("//", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/**/", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/** /", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/***/", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/* true */", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/*/**/", false) == json::lexer::token_type::parse_error));
CHECK((scan_string("/*/* */", false) == json::lexer::token_type::parse_error));
}
SECTION("ignore comments")
{
CHECK((scan_string("/", true) == json::lexer::token_type::parse_error));
CHECK((scan_string("/!", true) == json::lexer::token_type::parse_error));
CHECK((scan_string("/*", true) == json::lexer::token_type::parse_error));
CHECK((scan_string("/**", true) == json::lexer::token_type::parse_error));
CHECK((scan_string("//", true) == json::lexer::token_type::end_of_input));
CHECK((scan_string("/**/", true) == json::lexer::token_type::end_of_input));
CHECK((scan_string("/** /", true) == json::lexer::token_type::parse_error));
CHECK((scan_string("/***/", true) == json::lexer::token_type::end_of_input));
CHECK((scan_string("/* true */", true) == json::lexer::token_type::end_of_input));
CHECK((scan_string("/*/**/", true) == json::lexer::token_type::end_of_input));
CHECK((scan_string("/*/* */", true) == json::lexer::token_type::end_of_input));
}
}