Skip to content

Commit

Permalink
Remove new line before and/or (#79) (#86)
Browse files Browse the repository at this point in the history
* Remove new line before and/or

* Erase ALL newlines that are directly before and/or conjunctions

Co-authored-by: Tadeáš Kučera <tadeas.kucera@avast.com>
  • Loading branch information
TadeasKucera and TadeasKucera committed Mar 25, 2020
1 parent d7cfee7 commit f7a0ede
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/types/token_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,13 @@ void TokenStream::addMissingNewLines()
}
}
if (current == NEW_LINE)
++lineCounter;
{
if (next == OR || next == AND)
while (it->getType() == NEW_LINE)
it = std::prev(erase(it));
else
++lineCounter;
}
}
}

Expand Down
93 changes: 86 additions & 7 deletions tests/cpp/parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,19 @@ rule rule_with_complicated_regexp_class

EXPECT_EQ("cuckoo.process.executed_command(/[^\\\\]+/) and cuckoo.filesystem.file_write(/\\.bribe$/) and cuckoo.filesystem.file_write(/[\\]}]\\.(b[0-2]+|VC[0-9]*|DAQ)$/)", rule->getCondition()->getText());

EXPECT_EQ(input_text, driver.getParsedFile().getTextFormatted());
std::string expected = R"(
import "cuckoo"
import "pe"
rule rule_with_complicated_regexp_class
{
condition:
cuckoo.process.executed_command(/[^\\]+/) and
cuckoo.filesystem.file_write(/\.bribe$/) and
cuckoo.filesystem.file_write(/[\]}]\.(b[0-2]+|VC[0-9]*|DAQ)$/)
}
)";
EXPECT_EQ(expected, driver.getParsedFile().getTextFormatted());
}


Expand Down Expand Up @@ -3082,8 +3094,7 @@ rule rule_1 : Tag1 Tag2
$2 = { ab cd ef }
$3 = /ab*c/
condition:
pe.exports("ExitProcess")
and
pe.exports("ExitProcess") and
for any of them : ( $ at pe.entry_point )
}
Expand All @@ -3105,6 +3116,76 @@ rule rule_2
EXPECT_EQ(expected, driver.getParsedFile().getTextFormatted());
}

TEST_F(ParserTests,
RemoveLineBeforeAndWorks) {
prepareInput(
R"(rule rule_1 {
strings:
$1 = "plain string" wide
$2 = { ab cd ef }
$3 = /ab*c/
condition:
any of them
or (
true
and false)
}
rule rule_2
{
condition:
true
or
false
}
)");

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

EXPECT_EQ(
R"(rule rule_1 {
strings:
$1 = "plain string" wide
$2 = { AB CD EF }
$3 = /ab*c/
condition:
any of them or (true and false)
}
rule rule_2 {
condition:
true or false
})", driver.getParsedFile().getText());

std::string expected = R"(rule rule_1
{
strings:
$1 = "plain string" wide
$2 = { ab cd ef }
$3 = /ab*c/
condition:
any of them or
(
true and
false
)
}
rule rule_2
{
condition:
true or
false
}
)";

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

TEST_F(ParserTests,
MultipleRulesWorks2) {
prepareInput(
Expand Down Expand Up @@ -3794,8 +3875,7 @@ rule rule_2
condition comment
*/
condition:
elf.type == elf.ET_EXEC
and
elf.type == elf.ET_EXEC and
$abc at elf.entry_point
}
// Comment at the end of file
Expand Down Expand Up @@ -3944,8 +4024,7 @@ rule rule_2
$3 = /./
$5 = "String 5"
condition:
elf.type == elf.ET_EXEC
and
elf.type == elf.ET_EXEC and
2 of ($1, $2, $3 /*, $4*/, $5)
}
)";
Expand Down

0 comments on commit f7a0ede

Please sign in to comment.