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

Auto add newlines #53

Merged
merged 5 commits into from
Jan 17, 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
3 changes: 3 additions & 0 deletions include/yaramod/types/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ enum TokenType
RCB, // '}'
ASSIGN,
COLON,
COLON_BEFORE_NEWLINE,
COMMA,
PRIVATE,
GLOBAL,
Expand All @@ -82,6 +83,7 @@ enum TokenType
NOCASE,
WIDE,
FULLWORD,
PRIVATE_STRING_MODIFIER,
XOR,
IMPORT_MODULE,
IMPORT_KEYWORD,
Expand All @@ -104,6 +106,7 @@ enum TokenType
INTEGER,
DOUBLE,
STRING_ID,
STRING_ID_BEFORE_NEWLINE,
STRING_ID_WILDCARD,
STRING_LENGTH,
STRING_OFFSET,
Expand Down
2 changes: 1 addition & 1 deletion include/yaramod/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ template <typename T>
std::string numToStr(const T num, std::ios_base &(*format)(std::ios_base&) = std::dec, bool showbase = false, bool toUpper = false)
{
std::ostringstream os;
if(toUpper)
if (toUpper)
os << std::uppercase;
if (showbase)
os << format << std::showbase << num;
Expand Down
16 changes: 8 additions & 8 deletions src/builder/yara_rule_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ YaraRuleBuilder& YaraRuleBuilder::withStringMeta(const std::string& key, const s
if (_metas.empty())
{
_tokenStream->emplace(insert_before, META, "meta");
_tokenStream->emplace(insert_before, COLON, ":");
_tokenStream->emplace(insert_before, COLON_BEFORE_NEWLINE, ":");
_tokenStream->emplace(insert_before, TokenType::NEW_LINE, "\n");
}

Expand Down Expand Up @@ -217,7 +217,7 @@ YaraRuleBuilder& YaraRuleBuilder::withIntMeta(const std::string& key, std::int64
if (_metas.empty())
{
_tokenStream->emplace(insert_before, META, "meta");
_tokenStream->emplace(insert_before, COLON, ":");
_tokenStream->emplace(insert_before, COLON_BEFORE_NEWLINE, ":");
_tokenStream->emplace(insert_before, TokenType::NEW_LINE, "\n");
}

Expand Down Expand Up @@ -248,7 +248,7 @@ YaraRuleBuilder& YaraRuleBuilder::withUIntMeta(const std::string& key, std::uint
if (_metas.empty())
{
_tokenStream->emplace(insert_before, META, "meta");
_tokenStream->emplace(insert_before, COLON, ":");
_tokenStream->emplace(insert_before, COLON_BEFORE_NEWLINE, ":");
_tokenStream->emplace(insert_before, TokenType::NEW_LINE, "\n");
}

Expand Down Expand Up @@ -279,7 +279,7 @@ YaraRuleBuilder& YaraRuleBuilder::withHexIntMeta(const std::string& key, std::ui
if (_metas.empty())
{
_tokenStream->emplace(insert_before, META, "meta");
_tokenStream->emplace(insert_before, COLON, ":");
_tokenStream->emplace(insert_before, COLON_BEFORE_NEWLINE, ":");
_tokenStream->emplace(insert_before, TokenType::NEW_LINE, "\n");
}

Expand Down Expand Up @@ -310,7 +310,7 @@ YaraRuleBuilder& YaraRuleBuilder::withBoolMeta(const std::string& key, bool valu
if (_metas.empty())
{
_tokenStream->emplace(insert_before, META, "meta");
_tokenStream->emplace(insert_before, COLON, ":");
_tokenStream->emplace(insert_before, COLON_BEFORE_NEWLINE, ":");
_tokenStream->emplace(insert_before, TokenType::NEW_LINE, "\n");
}

Expand Down Expand Up @@ -473,7 +473,7 @@ YaraRuleBuilder& YaraRuleBuilder::fullword()

YaraRuleBuilder& YaraRuleBuilder::private_()
{
auto token = _stringModsTokens->emplace_back(PRIVATE, "private");
auto token = _stringModsTokens->emplace_back(PRIVATE_STRING_MODIFIER, "private");
_stringMods.push_back(std::make_shared<PrivateStringModifier>(token));
return *this;
}
Expand Down Expand Up @@ -510,7 +510,7 @@ YaraRuleBuilder& YaraRuleBuilder::xor_(std::uint64_t low, std::uint64_t high)
void YaraRuleBuilder::initializeStrings()
{
_strings_it = _tokenStream->emplace(_condition_it, STRINGS, "strings");
_tokenStream->emplace(_condition_it, COLON, ":");
_tokenStream->emplace(_condition_it, COLON_BEFORE_NEWLINE, ":");
_tokenStream->emplace(_condition_it, NEW_LINE, "\n");
}

Expand All @@ -522,7 +522,7 @@ void YaraRuleBuilder::resetTokens()
_tokenStream->emplace_back(NEW_LINE, "\n");
_strings_it = std::nullopt;
_condition_it = _tokenStream->emplace_back(CONDITION, "condition");
_colon_it = _tokenStream->emplace_back(COLON, ":");
_colon_it = _tokenStream->emplace_back(COLON_BEFORE_NEWLINE, ":");
_tokenStream->emplace_back(NEW_LINE, "\n");
_rcb = _tokenStream->emplace_back(RULE_END, "}");

Expand Down
133 changes: 71 additions & 62 deletions src/parser/parser_driver.cpp

Large diffs are not rendered by default.

54 changes: 49 additions & 5 deletions src/types/token_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ TokenIt TokenStream::find(TokenType type, TokenIt from)

TokenIt TokenStream::find(TokenType type, TokenIt from, TokenIt to)
{
return std::find_if( from, to, [&type](const Token& t){ return t.getType() == type; });
return std::find_if(from, to, [&type](const Token& t){ return t.getType() == type; });
}

TokenIt TokenStream::findBackwards(TokenType type)
Expand Down Expand Up @@ -303,27 +303,71 @@ void TokenStream::addMissingNewLines()
auto current = it->getType();
auto nextIt = std::next(it);
if (nextIt == end())
break;
{
if (current != NEW_LINE)
emplace(nextIt, NEW_LINE, _new_line_style);
break;
}
auto next = nextIt->getType();
if (current == LP || current == LP_ENUMERATION || current == HEX_JUMP_LEFT_BRACKET || current == REGEXP_START_SLASH || current == HEX_START_BRACKET || current == LP_WITH_SPACE_AFTER || current == LP_WITH_SPACES)
{
brackets.addLeftBracket(lineCounter, it->getFlag());
if (brackets.putNewlineInCurrentSector() && next != NEW_LINE && next != ONELINE_COMMENT && next != COMMENT)
{
nextIt = emplace(nextIt, TokenType::NEW_LINE, _new_line_style);
nextIt = emplace(nextIt, NEW_LINE, _new_line_style);
next = nextIt->getType();
}
}
if (next == RP || next == RP_ENUMERATION || next == HEX_JUMP_RIGHT_BRACKET || next == REGEXP_END_SLASH || next == HEX_END_BRACKET || next == RP_WITH_SPACE_BEFORE || next == RP_WITH_SPACES)
{
if (brackets.putNewlineInCurrentSector() && current != NEW_LINE)
{
nextIt = emplace(nextIt, TokenType::NEW_LINE, _new_line_style);
nextIt = emplace(nextIt, NEW_LINE, _new_line_style);
next = nextIt->getType();
}
else
brackets.addRightBracket();
}
if (current != NEW_LINE && (next == CONDITION || next == STRINGS || next == STRING_ID_BEFORE_NEWLINE || next == META || next == META_KEY || next == RULE_END || next == RULE_BEGIN))
{
nextIt = emplace(nextIt, NEW_LINE, _new_line_style);
next = nextIt->getType();
}
if ((next == RULE || next == GLOBAL || next == PRIVATE) && it != begin())
{
if (current != GLOBAL && current != PRIVATE)
{
if (current != NEW_LINE)
{
nextIt = emplace(nextIt, NEW_LINE, _new_line_style);
nextIt = emplace(nextIt, NEW_LINE, _new_line_style);
next = nextIt->getType();
}
else
{
auto prev = std::prev(it)->getType();
if (prev != NEW_LINE && prev != COMMENT && prev != ONELINE_COMMENT)
{
nextIt = emplace(nextIt, NEW_LINE, _new_line_style);
next = nextIt->getType();
}
}
}
}
if (current == COLON_BEFORE_NEWLINE && next != NEW_LINE)
{
if (next == COMMENT)
{
auto nextNextIt = std::next(nextIt);
if ((nextNextIt != end()) && (nextNextIt->getType() != NEW_LINE))
emplace(nextNextIt, NEW_LINE, _new_line_style);
}
else if (next != ONELINE_COMMENT)
{
nextIt = emplace(nextIt, NEW_LINE, _new_line_style);
next = nextIt->getType();
}
}
if (current == NEW_LINE)
++lineCounter;
}
Expand Down Expand Up @@ -402,7 +446,7 @@ std::size_t TokenStream::PrintHelper::printComment(std::stringstream* ss, TokenS
{
*ss << indent;
}
else if(alignComment && columnCounter < indentation && (!prevIt || (*prevIt)->getType() != COLON))
else if (alignComment && columnCounter < indentation && (!prevIt || (*prevIt)->getType() != COLON))
*ss << std::string(indentation - columnCounter, ' ');
*ss << it->getPureText();
}
Expand Down
Loading