Skip to content

Commit

Permalink
fix: regex parsing with \/
Browse files Browse the repository at this point in the history
  • Loading branch information
metthal committed Dec 14, 2024
1 parent dcb256b commit b8554dc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/parser/parser_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ void ParserDriver::defineTokens()
_parser.token(R"([^\\\[\(\)\|\$\.\^\+\+*\?])").states("$regexp").symbol("REGEXP_CHAR").description("regexp character").action([](std::string_view str) -> Value {
return std::string{str};
});
_parser.token(R"(\\\/)").states("$regexp").symbol("REGEXP_CHAR").description("regexp character").action([](std::string_view str) -> Value {
return std::string{str};
});
_parser.token(R"(\\w)").states("$regexp").symbol("REGEXP_WORD_CHAR").description("regexp \\w").action([](std::string_view) -> Value { return {};});
_parser.token(R"(\\W)").states("$regexp").symbol("REGEXP_NON_WORD_CHAR").description("regexp \\W").action([](std::string_view) -> Value { return {};});
_parser.token(R"(\\s)").states("$regexp").symbol("REGEXP_SPACE").description("regexp \\s").action([](std::string_view) -> Value { return {};});
Expand Down
35 changes: 33 additions & 2 deletions tests/cpp/parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,8 @@ rule regexp_with_just_chars
EXPECT_EQ("regexp_with_just_chars", rule->getName());
EXPECT_EQ(Rule::Modifier::None, rule->getModifier());

auto strings = rule->getStrings()
; ASSERT_EQ(1u, strings.size());
auto strings = rule->getStrings();
ASSERT_EQ(1u, strings.size());

auto regexp0 = strings[0];
EXPECT_TRUE(regexp0->isRegexp());
Expand All @@ -1150,6 +1150,37 @@ rule regexp_with_just_chars
EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
RegexpWithEscapeSequences) {
prepareInput(
R"(
rule regexp_with_escape_sequences
{
strings:
$1 = /http:\/\/someone\.doingevil\.com/
condition:
true
}
)");

EXPECT_TRUE(driver.parse(input));
ASSERT_EQ(1u, driver.getParsedFile().getRules().size());

const auto& rule = driver.getParsedFile().getRules()[0];
EXPECT_EQ("regexp_with_escape_sequences", rule->getName());
EXPECT_EQ(Rule::Modifier::None, rule->getModifier());

auto strings = rule->getStrings();
ASSERT_EQ(1u, strings.size());

auto regexp0 = strings[0];
EXPECT_TRUE(regexp0->isRegexp());
EXPECT_EQ("$1", regexp0->getIdentifier());
EXPECT_EQ("/http:\\/\\/someone\\.doingevil\\.com/", regexp0->getText());

EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
MultipleRegexpsWithJustCharsWorks) {
prepareInput(
Expand Down

0 comments on commit b8554dc

Please sign in to comment.