Skip to content

Commit

Permalink
Make empty strings section invalid (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
msm-code committed Dec 2, 2021
1 parent ef3a54a commit 217a60f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/parser/parser_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,13 @@ void ParserDriver::defineGrammar()
}

_parser.rule("strings") // shared_ptr<StringsTrie>
.production("STRINGS", "COLON", "strings_body", [](auto&& args) -> Value {
.production("STRINGS", "COLON", "strings_body_nonempty", [](auto&& args) -> Value {
args[1].getTokenIt()->setType(TokenType::COLON_BEFORE_NEWLINE);
return std::move(args[2]);
})
;

_parser.rule("strings_body") // shared_ptr<StringsTrie>
_parser.rule("strings_body_nonempty") // shared_ptr<StringsTrie>
.production(
"strings_body", "STRING_ID", "ASSIGN", [](auto&& args) -> Value {
args[1].getTokenIt()->setType(TokenType::STRING_ID_AFTER_NEWLINE);
Expand All @@ -676,13 +676,17 @@ void ParserDriver::defineGrammar()
}
return strings;
}
)
);

_parser.rule("strings_body") // shared_ptr<StringsTrie>
.production("strings_body_nonempty", [&](auto&& args) -> Value {
return std::move(args[0].getStringsTrie());
})
.production([&](auto&&) -> Value {
auto strings = std::make_shared<Rule::StringsTrie>();
setCurrentStrings(strings);
return strings;
})
;
});

_parser.rule("string")
.production("STRING_LITERAL", "plain_string_mods", [&](auto&& args) -> Value {
Expand Down
23 changes: 23 additions & 0 deletions tests/cpp/parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,29 @@ rule variable_in_condition
EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
RuleWithNoStringsDoesntWork) {
prepareInput(
R"(
rule rule_with_no_strings
{
strings:
condition:
true
}
)");
try
{
driver.parse(input);
FAIL() << "Parser did not throw an exception.";
}
catch (const ParserError& err)
{
EXPECT_EQ(0u, driver.getParsedFile().getRules().size());
EXPECT_EQ("Error at 5.2-10: Syntax error: Unexpected condition, expected one of string identifier", err.getErrorMessage());
}
}

TEST_F(ParserTests,
RuleWithPlainTextStringsWorks) {
prepareInput(
Expand Down

0 comments on commit 217a60f

Please sign in to comment.