Skip to content

Commit

Permalink
TokenType: convert to cpp11 enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejKastak committed Aug 9, 2020
1 parent 282c662 commit 5e6dc9c
Show file tree
Hide file tree
Showing 18 changed files with 454 additions and 452 deletions.
4 changes: 2 additions & 2 deletions include/yaramod/builder/yara_hex_string_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ template <typename... Args>
YaraHexStringBuilder _alt(const std::shared_ptr<TokenStream>& ts, std::vector<std::shared_ptr<HexString>>& hexStrings, const YaraHexStringBuilder& unit, const Args&... args)
{
if (hexStrings.size() == 0)
ts->emplace_back(HEX_ALT_LEFT_BRACKET, "(");
ts->emplace_back(TokenType::HEX_ALT_LEFT_BRACKET, "(");
const auto& hexString = unit.get(ts, false);
hexStrings.push_back(hexString);
ts->emplace_back(HEX_ALT, "|");
ts->emplace_back(TokenType::HEX_ALT, "|");
return _alt(ts, hexStrings, args...);
}
/// @}
Expand Down
32 changes: 16 additions & 16 deletions include/yaramod/types/expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace yaramod {
class StringExpression : public Expression
{
public:
StringExpression(const std::string& id) { _id = _tokenStream->emplace_back(STRING_ID, id); }
StringExpression(std::string&& id) { _id = _tokenStream->emplace_back(STRING_ID, std::move(id)); }
StringExpression(const std::string& id) { _id = _tokenStream->emplace_back(TokenType::STRING_ID, id); }
StringExpression(std::string&& id) { _id = _tokenStream->emplace_back(TokenType::STRING_ID, std::move(id)); }
StringExpression(TokenIt id) : _id(id) {}

virtual VisitResult accept(Visitor* v) override
Expand Down Expand Up @@ -70,7 +70,7 @@ class StringWildcardExpression : public Expression
template <typename Str>
StringWildcardExpression(Str&& id)
{
_id = _tokenStream->emplace_back(STRING_ID, std::forward<Str>(id));
_id = _tokenStream->emplace_back(TokenType::STRING_ID, std::forward<Str>(id));
}
StringWildcardExpression(TokenIt it) : _id(it) {}

Expand Down Expand Up @@ -111,8 +111,8 @@ class StringAtExpression : public Expression
StringAtExpression(const std::string& id, ExpPtr&& at)
: _at(std::forward<ExpPtr>(at))
{
_id = _tokenStream->emplace_back(STRING_ID, id);
_at_symbol = _tokenStream->emplace_back(OP_AT, "at");
_id = _tokenStream->emplace_back(TokenType::STRING_ID, id);
_at_symbol = _tokenStream->emplace_back(TokenType::OP_AT, "at");
_tokenStream->moveAppend(_at->getTokenStream());
}

Expand Down Expand Up @@ -165,8 +165,8 @@ class StringInRangeExpression : public Expression
template <typename ExpPtr>
StringInRangeExpression(const std::string& id, ExpPtr&& range)
{
_id = _tokenStream->emplace_back(STRING_ID, id);
_in_symbol = _tokenStream->emplace_back(OP_IN, "in");
_id = _tokenStream->emplace_back(TokenType::STRING_ID, id);
_in_symbol = _tokenStream->emplace_back(TokenType::OP_IN, "in");
_range = std::forward<ExpPtr>(range);
_tokenStream->moveAppend(_range->getTokenStream());
}
Expand Down Expand Up @@ -223,7 +223,7 @@ class StringCountExpression : public Expression
template <typename Str>
StringCountExpression(Str&& id)
{
_id = _tokenStream->emplace_back(STRING_COUNT, std::forward<Str>(id));
_id = _tokenStream->emplace_back(TokenType::STRING_COUNT, std::forward<Str>(id));
}

virtual VisitResult accept(Visitor* v) override
Expand Down Expand Up @@ -277,13 +277,13 @@ class StringOffsetExpression : public Expression
template <typename Str>
StringOffsetExpression(Str&& id)
{
_id = _tokenStream->emplace_back(STRING_OFFSET, std::forward<Str>(id));
_id = _tokenStream->emplace_back(TokenType::STRING_OFFSET, std::forward<Str>(id));
}
template <typename Str, typename ExpPtr>
StringOffsetExpression(Str&& id, ExpPtr&& expr)
: _expr(std::forward<ExpPtr>(expr))
{
_id = _tokenStream->emplace_back(STRING_OFFSET, std::forward<Str>(id));
_id = _tokenStream->emplace_back(TokenType::STRING_OFFSET, std::forward<Str>(id));
}

virtual VisitResult accept(Visitor* v) override
Expand Down Expand Up @@ -341,13 +341,13 @@ class StringLengthExpression : public Expression
template <typename Str>
StringLengthExpression(Str&& id)
{
_id = _tokenStream->emplace_back(STRING_LENGTH, std::forward<Str>(id));
_id = _tokenStream->emplace_back(TokenType::STRING_LENGTH, std::forward<Str>(id));
}
template <typename Str, typename ExpPtr>
StringLengthExpression(Str&& id, ExpPtr&& expr)
: _expr(std::forward<ExpPtr>(expr))
{
_id = _tokenStream->emplace_back(STRING_LENGTH, std::forward<Str>(id));
_id = _tokenStream->emplace_back(TokenType::STRING_LENGTH, std::forward<Str>(id));
}

virtual VisitResult accept(Visitor* v) override
Expand Down Expand Up @@ -387,7 +387,7 @@ class UnaryOpExpression : public Expression
public:
virtual std::string getText(const std::string& indent = std::string{}) const override
{
if (_op->getType() == NOT)
if (_op->getType() == TokenType::NOT)
return _op->getString() + " " + _expr->getText(indent);
else
return _op->getString() + _expr->getText(indent);
Expand Down Expand Up @@ -1527,9 +1527,9 @@ class BoolLiteralExpression : public LiteralExpression<bool>
: LiteralExpression<bool>()
{
if (value)
_value = _tokenStream->emplace_back(BOOL_TRUE, value, "true");
_value = _tokenStream->emplace_back(TokenType::BOOL_TRUE, value, "true");
else
_value = _tokenStream->emplace_back(BOOL_FALSE, value, "false");
_value = _tokenStream->emplace_back(TokenType::BOOL_FALSE, value, "false");
_valid = true;
}

Expand Down Expand Up @@ -1980,4 +1980,4 @@ class RegexpExpression : public Expression
std::shared_ptr<String> _regexp; ///< Regular expression string
};

}
}
2 changes: 1 addition & 1 deletion include/yaramod/types/rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Rule
std::shared_ptr<StringsTrie> _strings; ///< Strings
Expression::Ptr _condition; ///< Condition expression
std::vector<TokenIt> _tags; ///< Tags
Location _location; ///< Which file was this rule included from
Location _location; ///< Which file was this rule included from and its textual position
};

}
12 changes: 6 additions & 6 deletions include/yaramod/types/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class String
, _mods()
{
assert(_tokenStream);
_id = _tokenStream->emplace_back(STRING_KEY, id);
_assignToken = _tokenStream->emplace_back(ASSIGN, "=");
_id = _tokenStream->emplace_back(TokenType::STRING_KEY, id);
_assignToken = _tokenStream->emplace_back(TokenType::ASSIGN, "=");
}

explicit String(const std::shared_ptr<TokenStream>& ts, Type type, TokenIt id, TokenIt assignToken)
Expand Down Expand Up @@ -163,8 +163,8 @@ class String
else
{
auto first = getFirstTokenIt();
_id = _tokenStream->emplace(first, STRING_KEY, std::move(id));
_assignToken = _tokenStream->emplace(first, ASSIGN, "=");
_id = _tokenStream->emplace(first, TokenType::STRING_KEY, std::move(id));
_assignToken = _tokenStream->emplace(first, TokenType::ASSIGN, "=");
}
}

Expand All @@ -175,8 +175,8 @@ class String
else
{
auto first = getFirstTokenIt();
_id = _tokenStream->emplace(first, STRING_KEY, id);
_assignToken = _tokenStream->emplace(first, ASSIGN, "=");
_id = _tokenStream->emplace(first, TokenType::STRING_KEY, id);
_assignToken = _tokenStream->emplace(first, TokenType::ASSIGN, "=");
}
}

Expand Down
12 changes: 6 additions & 6 deletions include/yaramod/types/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ class Symbol
{
switch(_type)
{
case Type::Value : return VALUE_SYMBOL;
case Type::Array : return ARRAY_SYMBOL;
case Type::Dictionary : return DICTIONARY_SYMBOL;
case Type::Function : return FUNCTION_SYMBOL;
case Type::Structure : return STRUCTURE_SYMBOL;
default: return INVALID;
case Type::Value : return TokenType::VALUE_SYMBOL;
case Type::Array : return TokenType::ARRAY_SYMBOL;
case Type::Dictionary : return TokenType::DICTIONARY_SYMBOL;
case Type::Function : return TokenType::FUNCTION_SYMBOL;
case Type::Structure : return TokenType::STRUCTURE_SYMBOL;
default: return TokenType::INVALID;
}
}
/// @}
Expand Down
12 changes: 6 additions & 6 deletions include/yaramod/types/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ class Token

bool isStringModifier() const
{
return _type == ASCII
|| _type == WIDE
|| _type == FULLWORD
|| _type == NOCASE
|| _type == XOR
|| _type == PRIVATE;
return _type == TokenType::ASCII
|| _type == TokenType::WIDE
|| _type == TokenType::FULLWORD
|| _type == TokenType::NOCASE
|| _type == TokenType::XOR
|| _type == TokenType::PRIVATE;
}
/// @}

Expand Down
2 changes: 1 addition & 1 deletion include/yaramod/types/token_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace yaramod {
/**
* Represents type of parsed tokens.
*/
enum TokenType
enum class TokenType
{
RULE_NAME,
TAG,
Expand Down
2 changes: 1 addition & 1 deletion include/yaramod/types/yara_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class YaraFile
{
_ruleTable.erase(_ruleTable.find((*rem_itr)->getName()));
auto behind = _tokenStream->erase((*rem_itr)->getFirstTokenIt(), std::next((*rem_itr)->getLastTokenIt()));
while (behind != _tokenStream->end() && behind->getType() == NEW_LINE)
while (behind != _tokenStream->end() && behind->getType() == TokenType::NEW_LINE)
behind = _tokenStream->erase(behind);
}
_rules.erase(itr, _rules.end());
Expand Down
Loading

0 comments on commit 5e6dc9c

Please sign in to comment.