Skip to content

Commit

Permalink
🐛 fix lexer to properly cope with repeated comments #2330
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jul 31, 2020
1 parent 1da9317 commit 3888b16
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/nlohmann/detail/input/lexer.hpp
Expand Up @@ -1511,7 +1511,7 @@ class lexer : public lexer_base<BasicJsonType>
skip_whitespace();

// ignore comments
if (ignore_comments && current == '/')
while (ignore_comments && current == '/')
{
if (!scan_comment())
{
Expand Down
2 changes: 1 addition & 1 deletion single_include/nlohmann/json.hpp
Expand Up @@ -7390,7 +7390,7 @@ class lexer : public lexer_base<BasicJsonType>
skip_whitespace();

// ignore comments
if (ignore_comments && current == '/')
while (ignore_comments && current == '/')
{
if (!scan_comment())
{
Expand Down
3 changes: 3 additions & 0 deletions test/src/unit-class_lexer.cpp
Expand Up @@ -241,5 +241,8 @@ TEST_CASE("lexer class")
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));

CHECK((scan_string("//\n//\n", true) == json::lexer::token_type::end_of_input));
CHECK((scan_string("/**//**//**/", true) == json::lexer::token_type::end_of_input));
}
}
7 changes: 7 additions & 0 deletions test/src/unit-regression2.cpp
Expand Up @@ -478,4 +478,11 @@ TEST_CASE("regression tests 2")
CHECK(jsonObj["aaaa"] == 11);
CHECK(jsonObj["bbb"] == 222);
}

SECTION("issue #2330 - ignore_comment=true fails on multiple consecutive lines starting with comments")
{
std::string ss = "//\n//\n{\n}\n";
json j = json::parse(ss, nullptr, true, true);
CHECK(j.dump() == "{}");
}
}

0 comments on commit 3888b16

Please sign in to comment.