From f7a0edefe475f36123d44208fd747c5db213ec7d Mon Sep 17 00:00:00 2001 From: Tadeas Kucera Date: Wed, 25 Mar 2020 13:14:01 +0100 Subject: [PATCH] Remove new line before and/or (#79) (#86) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove new line before and/or * Erase ALL newlines that are directly before and/or conjunctions Co-authored-by: Tadeáš Kučera --- src/types/token_stream.cpp | 8 +++- tests/cpp/parser_tests.cpp | 93 +++++++++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/types/token_stream.cpp b/src/types/token_stream.cpp index ed38369a..046d2a3d 100644 --- a/src/types/token_stream.cpp +++ b/src/types/token_stream.cpp @@ -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; + } } } diff --git a/tests/cpp/parser_tests.cpp b/tests/cpp/parser_tests.cpp index 1be3a2b5..9407a232 100644 --- a/tests/cpp/parser_tests.cpp +++ b/tests/cpp/parser_tests.cpp @@ -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()); } @@ -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 ) } @@ -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( @@ -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 @@ -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) } )";