Added support for more literal types and scope resolution operator parsing

This commit is contained in:
WerWolv
2021-01-05 14:42:08 +01:00
parent f137d759c8
commit ceee311efa
7 changed files with 331 additions and 109 deletions

View File

@@ -26,10 +26,8 @@ namespace hex::lang {
class ASTNodeIntegerLiteral : public ASTNode {
public:
ASTNodeIntegerLiteral(std::variant<u128, s128> value, Token::ValueType type)
: ASTNode(), m_value(value), m_type(type) {
ASTNodeIntegerLiteral(Token::IntegerLiteral literal) : ASTNode(), m_literal(literal) { }
}
ASTNodeIntegerLiteral(const ASTNodeIntegerLiteral&) = default;
ASTNode* clone() override {
@@ -37,16 +35,15 @@ namespace hex::lang {
}
[[nodiscard]] const auto& getValue() const {
return this->m_value;
return this->m_literal.second;
}
[[nodiscard]] Token::ValueType getType() const {
return this->m_type;
return this->m_literal.first;
}
private:
std::variant<u128, s128> m_value;
Token::ValueType m_type;
Token::IntegerLiteral m_literal;
};
class ASTNodeNumericExpression : public ASTNode {
@@ -351,4 +348,22 @@ namespace hex::lang {
std::vector<std::string> m_path;
};
class ASTNodeScopeResolution : public ASTNode {
public:
explicit ASTNodeScopeResolution(std::vector<std::string> path) : ASTNode(), m_path(std::move(path)) { }
ASTNodeScopeResolution(const ASTNodeScopeResolution&) = default;
ASTNode* clone() override {
return new ASTNodeScopeResolution(*this);
}
const std::vector<std::string>& getPath() {
return this->m_path;
}
private:
std::vector<std::string> m_path;
};
}

View File

@@ -53,6 +53,7 @@ namespace hex::lang {
return this->m_curr[index].type;
}
ASTNode* parseScopeResolution(std::vector<std::string> &path);
ASTNode* parseRValue(std::vector<std::string> &path);
ASTNode* parseFactor();
ASTNode* parseMultiplicativeExpression();

View File

@@ -7,6 +7,7 @@
#include "providers/provider.hpp"
#include "helpers/utils.hpp"
#include "lang/token.hpp"
#include <cstring>
#include <random>
@@ -638,7 +639,7 @@ namespace hex::lang {
class PatternDataEnum : public PatternData {
public:
PatternDataEnum(u64 offset, size_t size, std::vector<std::pair<u64, std::string>> enumValues, u32 color = 0)
PatternDataEnum(u64 offset, size_t size, std::vector<std::pair<Token::IntegerLiteral, std::string>> enumValues, u32 color = 0)
: PatternData(offset, size, color), m_enumValues(std::move(enumValues)) { }
PatternData* clone() override {
@@ -653,12 +654,18 @@ namespace hex::lang {
std::string valueString = PatternData::getTypeName() + "::";
bool foundValue = false;
for (auto &[entryValue, entryName] : this->m_enumValues) {
if (value == entryValue) {
valueString += entryName;
foundValue = true;
for (auto &[entryValueLiteral, entryName] : this->m_enumValues) {
bool matches = std::visit([&, name = entryName](auto &&entryValue) {
if (value == entryValue) {
valueString += name;
foundValue = true;
return true;
}
return false;
}, entryValueLiteral.second);
if (matches)
break;
}
}
if (!foundValue)
@@ -694,7 +701,7 @@ namespace hex::lang {
}
private:
std::vector<std::pair<u64, std::string>> m_enumValues;
std::vector<std::pair<Token::IntegerLiteral, std::string>> m_enumValues;
};
class PatternDataBitfield : public PatternData {

View File

@@ -78,11 +78,13 @@ namespace hex::lang {
SquareBracketClose,
Comma,
Dot,
ScopeResolution,
EndOfExpression,
EndOfProgram
};
using ValueTypes = std::variant<Keyword, std::string, Operator, s128, ValueType, Separator>;
using IntegerLiteral = std::pair<ValueType, std::variant<u8, s8, u16, s16, u32, s32, u64, s64, u128, s128, float, double>>;
using ValueTypes = std::variant<Keyword, std::string, Operator, IntegerLiteral, ValueType, Separator>;
Token(Type type, auto value, u32 lineNumber) : type(type), value(value), lineNumber(lineNumber) {
@@ -174,7 +176,7 @@ namespace hex::lang {
#define KEYWORD_LE COMPONENT(Keyword, LittleEndian)
#define KEYWORD_BE COMPONENT(Keyword, BigEndian)
#define INTEGER hex::lang::Token::Type::Integer, 0xFFFF'FFFF'FFFF'FFFF
#define INTEGER hex::lang::Token::Type::Integer, hex::lang::Token::IntegerLiteral({ hex::lang::Token::ValueType::Any, 0xFFFF'FFFF'FFFF'FFFF })
#define IDENTIFIER hex::lang::Token::Type::Identifier, ""
#define OPERATOR_AT COMPONENT(Operator, AtDeclaration)
@@ -206,5 +208,6 @@ namespace hex::lang {
#define SEPARATOR_SQUAREBRACKETCLOSE COMPONENT(Separator, SquareBracketClose)
#define SEPARATOR_COMMA COMPONENT(Separator, Comma)
#define SEPARATOR_DOT COMPONENT(Separator, Dot)
#define SEPARATOR_SCOPE_RESOLUTION COMPONENT(Separator, ScopeResolution)
#define SEPARATOR_ENDOFEXPRESSION COMPONENT(Separator, EndOfExpression)
#define SEPARATOR_ENDOFPROGRAM COMPONENT(Separator, EndOfProgram)