patterns: Initial namespace support

This commit is contained in:
WerWolv
2021-08-25 17:07:01 +02:00
parent 15665b03a7
commit 9289ebf4c9
6 changed files with 240 additions and 124 deletions

View File

@@ -161,6 +161,7 @@ namespace hex::lang {
return new ASTNodeTypeDecl(*this);
}
void setName(const std::string &name) { this->m_name = name; }
[[nodiscard]] std::string_view getName() const { return this->m_name; }
[[nodiscard]] ASTNode* getType() { return this->m_type; }
[[nodiscard]] std::optional<std::endian> getEndian() const { return this->m_endian; }

View File

@@ -32,6 +32,7 @@ namespace hex::lang {
std::unordered_map<std::string, ASTNode*> m_types;
std::vector<TokenIter> m_matchedOptionals;
std::vector<std::vector<std::string>> m_currNamespace;
u32 getLineNumber(s32 index) const {
return this->m_curr[index].lineNumber;
@@ -51,9 +52,20 @@ namespace hex::lang {
return this->m_curr[index].type;
}
std::string getNamespacePrefixedName(const std::string &name) {
std::string result;
for (const auto &part : this->m_currNamespace.back()) {
result += part + "::";
}
result += name;
return result;
}
ASTNode* parseFunctionCall();
ASTNode* parseStringLiteral();
ASTNode* parseScopeResolution(std::vector<std::string> &path);
std::string parseScopeResolution();
ASTNode* parseRValue(ASTNodeRValue::Path &path);
ASTNode* parseFactor();
ASTNode* parseUnaryExpression();
@@ -81,21 +93,23 @@ namespace hex::lang {
void parseAttribute(Attributable *currNode);
ASTNode* parseConditional();
ASTNode* parseWhileStatement();
ASTNode* parseType(s32 startIndex);
ASTNodeTypeDecl* parseType();
ASTNode* parseUsingDeclaration();
ASTNode* parsePadding();
ASTNode* parseMemberVariable();
ASTNode* parseMemberArrayVariable();
ASTNode* parseMemberPointerVariable();
ASTNode* parseMemberVariable(ASTNodeTypeDecl *type);
ASTNode* parseMemberArrayVariable(ASTNodeTypeDecl *type);
ASTNode* parseMemberPointerVariable(ASTNodeTypeDecl *type);
ASTNode* parseMember();
ASTNode* parseStruct();
ASTNode* parseUnion();
ASTNode* parseEnum();
ASTNode* parseBitfield();
ASTNode* parseVariablePlacement();
ASTNode* parseArrayVariablePlacement();
ASTNode* parsePointerVariablePlacement();
ASTNode* parseStatement();
ASTNode* parseVariablePlacement(ASTNodeTypeDecl *type);
ASTNode* parseArrayVariablePlacement(ASTNodeTypeDecl *type);
ASTNode* parsePointerVariablePlacement(ASTNodeTypeDecl *type);
ASTNode* parsePlacement();
std::vector<ASTNode*> parseNamespace();
std::vector<ASTNode*> parseStatements();
std::vector<ASTNode*> parseTillToken(Token::Type endTokenType, const auto value) {
std::vector<ASTNode*> program;
@@ -105,7 +119,8 @@ namespace hex::lang {
};
while (this->m_curr->type != endTokenType || (*this->m_curr) != value) {
program.push_back(parseStatement());
for (auto statement : parseStatements())
program.push_back(statement);
}
this->m_curr++;
@@ -132,6 +147,10 @@ namespace hex::lang {
return true;
}
void reset() {
this->m_curr = this->m_originalPosition;
}
template<Setting S = Normal>
bool sequence() {
if constexpr (S == Normal)
@@ -146,14 +165,14 @@ namespace hex::lang {
bool sequence(Token::Type type, auto value, auto ... args) {
if constexpr (S == Normal) {
if (!peek(type, value)) {
this->m_curr = this->m_originalPosition;
reset();
return false;
}
this->m_curr++;
if (!sequence<Normal>(args...)) {
this->m_curr = this->m_originalPosition;
reset();
return false;
}
@@ -167,7 +186,7 @@ namespace hex::lang {
if (!sequence<Normal>(args...))
return true;
this->m_curr = this->m_originalPosition;
reset();
return false;
} else
__builtin_unreachable();
@@ -196,7 +215,7 @@ namespace hex::lang {
bool variant(Token::Type type1, auto value1, Token::Type type2, auto value2) {
if (!peek(type1, value1)) {
if (!peek(type2, value2)) {
this->m_curr = this->m_originalPosition;
reset();
return false;
}
}

View File

@@ -35,7 +35,8 @@ namespace hex::lang {
Parent,
While,
Function,
Return
Return,
Namespace
};
enum class Operator {
@@ -66,7 +67,8 @@ namespace hex::lang {
TernaryConditional,
Dollar,
AddressOf,
SizeOf
SizeOf,
ScopeResolution
};
enum class ValueType {
@@ -208,6 +210,7 @@ namespace hex::lang {
#define KEYWORD_WHILE COMPONENT(Keyword, While)
#define KEYWORD_FUNCTION COMPONENT(Keyword, Function)
#define KEYWORD_RETURN COMPONENT(Keyword, Return)
#define KEYWORD_NAMESPACE COMPONENT(Keyword, Namespace)
#define INTEGER hex::lang::Token::Type::Integer, hex::lang::Token::IntegerLiteral(u64(0))
#define IDENTIFIER hex::lang::Token::Type::Identifier, ""
@@ -241,6 +244,7 @@ namespace hex::lang {
#define OPERATOR_DOLLAR COMPONENT(Operator, Dollar)
#define OPERATOR_ADDRESSOF COMPONENT(Operator, AddressOf)
#define OPERATOR_SIZEOF COMPONENT(Operator, SizeOf)
#define OPERATOR_SCOPERESOLUTION COMPONENT(Operator, ScopeResolution)
#define VALUETYPE_CUSTOMTYPE COMPONENT(ValueType, CustomType)
#define VALUETYPE_PADDING COMPONENT(ValueType, Padding)