Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove new line before and/or (#79) #86

Merged
merged 2 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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