2020-11-10 15:26:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
2022-01-31 14:37:12 +01:00
|
|
|
#include <hex/pattern_language/error.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/pattern_language/token.hpp>
|
2022-02-27 23:25:39 +01:00
|
|
|
|
|
|
|
#include <hex/pattern_language/ast/ast_node.hpp>
|
|
|
|
#include <hex/pattern_language/ast/ast_node_rvalue.hpp>
|
|
|
|
#include <hex/pattern_language/ast/ast_node_attribute.hpp>
|
|
|
|
#include <hex/pattern_language/ast/ast_node_type_decl.hpp>
|
2020-11-10 15:26:38 +01:00
|
|
|
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
2020-11-10 15:26:38 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
namespace hex::pl {
|
2020-11-10 15:26:38 +01:00
|
|
|
|
|
|
|
class Parser {
|
|
|
|
public:
|
2021-01-10 13:40:07 +01:00
|
|
|
using TokenIter = std::vector<Token>::const_iterator;
|
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
Parser() = default;
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
~Parser() = default;
|
2020-11-10 15:26:38 +01:00
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
std::optional<std::vector<std::shared_ptr<ASTNode>>> parse(const std::vector<Token> &tokens);
|
2022-01-31 14:37:12 +01:00
|
|
|
const std::optional<PatternLanguageError> &getError() { return this->m_error; }
|
2020-11-27 21:20:23 +01:00
|
|
|
|
|
|
|
private:
|
2022-01-31 14:37:12 +01:00
|
|
|
std::optional<PatternLanguageError> m_error;
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
TokenIter m_curr;
|
2022-01-29 04:16:32 +01:00
|
|
|
TokenIter m_originalPosition, m_partOriginalPosition;
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
std::unordered_map<std::string, std::shared_ptr<ASTNode>> m_types;
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
std::vector<TokenIter> m_matchedOptionals;
|
2021-08-25 17:07:01 +02:00
|
|
|
std::vector<std::vector<std::string>> m_currNamespace;
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
u32 getLineNumber(i32 index) const {
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
return this->m_curr[index].lineNumber;
|
|
|
|
}
|
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
template<typename T>
|
|
|
|
std::unique_ptr<T> create(T *node) {
|
2021-09-21 21:29:18 +02:00
|
|
|
node->setLineNumber(this->getLineNumber(-1));
|
2022-02-27 23:25:39 +01:00
|
|
|
return std::unique_ptr<T>(node);
|
2021-09-21 21:29:18 +02:00
|
|
|
}
|
|
|
|
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
template<typename T>
|
2022-01-24 20:53:17 +01:00
|
|
|
const T &getValue(i32 index) const {
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
auto value = std::get_if<T>(&this->m_curr[index].value);
|
|
|
|
|
|
|
|
if (value == nullptr)
|
2022-01-31 14:37:12 +01:00
|
|
|
throwParserError("failed to decode token. Invalid type.", getLineNumber(index));
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
Token::Type getType(i32 index) const {
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
return this->m_curr[index].type;
|
|
|
|
}
|
|
|
|
|
2021-08-25 17:07:01 +02:00
|
|
|
std::string getNamespacePrefixedName(const std::string &name) {
|
|
|
|
std::string result;
|
|
|
|
for (const auto &part : this->m_currNamespace.back()) {
|
|
|
|
result += part + "::";
|
|
|
|
}
|
|
|
|
|
|
|
|
result += name;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
std::unique_ptr<ASTNode> parseFunctionCall();
|
|
|
|
std::unique_ptr<ASTNode> parseStringLiteral();
|
2021-09-03 02:33:45 +02:00
|
|
|
std::string parseNamespaceResolution();
|
2022-02-27 23:25:39 +01:00
|
|
|
std::unique_ptr<ASTNode> parseScopeResolution();
|
|
|
|
std::unique_ptr<ASTNode> parseRValue();
|
|
|
|
std::unique_ptr<ASTNode> parseRValue(ASTNodeRValue::Path &path);
|
|
|
|
std::unique_ptr<ASTNode> parseFactor();
|
|
|
|
std::unique_ptr<ASTNode> parseCastExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseUnaryExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseMultiplicativeExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseAdditiveExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseShiftExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseBinaryAndExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseBinaryXorExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseBinaryOrExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseBooleanAnd();
|
|
|
|
std::unique_ptr<ASTNode> parseBooleanXor();
|
|
|
|
std::unique_ptr<ASTNode> parseBooleanOr();
|
|
|
|
std::unique_ptr<ASTNode> parseRelationExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseEqualityExpression();
|
|
|
|
std::unique_ptr<ASTNode> parseTernaryConditional();
|
|
|
|
std::unique_ptr<ASTNode> parseMathematicalExpression();
|
|
|
|
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionDefinition();
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionVariableDecl();
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionStatement();
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionVariableAssignment(const std::string &lvalue);
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionVariableCompoundAssignment(const std::string &lvalue);
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionControlFlowStatement();
|
|
|
|
std::vector<std::unique_ptr<ASTNode>> parseStatementBody();
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionConditional();
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionWhileLoop();
|
|
|
|
std::unique_ptr<ASTNode> parseFunctionForLoop();
|
2021-06-21 00:21:38 +02:00
|
|
|
|
|
|
|
void parseAttribute(Attributable *currNode);
|
2022-02-27 23:25:39 +01:00
|
|
|
std::unique_ptr<ASTNode> parseConditional();
|
|
|
|
std::unique_ptr<ASTNode> parseWhileStatement();
|
|
|
|
std::unique_ptr<ASTNodeTypeDecl> parseType(bool allowFunctionTypes = false);
|
|
|
|
std::shared_ptr<ASTNodeTypeDecl> parseUsingDeclaration();
|
|
|
|
std::unique_ptr<ASTNode> parsePadding();
|
|
|
|
std::unique_ptr<ASTNode> parseMemberVariable(std::unique_ptr<ASTNodeTypeDecl> &&type);
|
|
|
|
std::unique_ptr<ASTNode> parseMemberArrayVariable(std::unique_ptr<ASTNodeTypeDecl> &&type);
|
|
|
|
std::unique_ptr<ASTNode> parseMemberPointerVariable(std::unique_ptr<ASTNodeTypeDecl> &&type);
|
|
|
|
std::unique_ptr<ASTNode> parseMember();
|
|
|
|
std::shared_ptr<ASTNodeTypeDecl> parseStruct();
|
|
|
|
std::shared_ptr<ASTNodeTypeDecl> parseUnion();
|
|
|
|
std::shared_ptr<ASTNodeTypeDecl> parseEnum();
|
|
|
|
std::shared_ptr<ASTNodeTypeDecl> parseBitfield();
|
|
|
|
std::unique_ptr<ASTNode> parseVariablePlacement(std::unique_ptr<ASTNodeTypeDecl> &&type);
|
|
|
|
std::unique_ptr<ASTNode> parseArrayVariablePlacement(std::unique_ptr<ASTNodeTypeDecl> &&type);
|
|
|
|
std::unique_ptr<ASTNode> parsePointerVariablePlacement(std::unique_ptr<ASTNodeTypeDecl> &&type);
|
|
|
|
std::unique_ptr<ASTNode> parsePlacement();
|
|
|
|
std::vector<std::shared_ptr<ASTNode>> parseNamespace();
|
|
|
|
std::vector<std::shared_ptr<ASTNode>> parseStatements();
|
|
|
|
|
|
|
|
std::shared_ptr<ASTNodeTypeDecl> addType(const std::string &name, std::unique_ptr<ASTNode> &&node, std::optional<std::endian> endian = std::nullopt);
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<ASTNode>> parseTillToken(Token::Type endTokenType, const auto value) {
|
|
|
|
std::vector<std::shared_ptr<ASTNode>> program;
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
|
|
|
while (this->m_curr->type != endTokenType || (*this->m_curr) != value) {
|
2022-02-27 23:25:39 +01:00
|
|
|
for (auto &statement : parseStatements())
|
|
|
|
program.push_back(std::move(statement));
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this->m_curr++;
|
|
|
|
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:37:12 +01:00
|
|
|
[[noreturn]] void throwParserError(const std::string &error, i32 token = -1) const {
|
|
|
|
throw PatternLanguageError(this->m_curr[token].lineNumber, "Parser: " + error);
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Token consuming */
|
|
|
|
|
2022-02-07 21:37:10 +01:00
|
|
|
enum class Setting
|
|
|
|
{
|
|
|
|
};
|
2021-01-22 18:01:39 +01:00
|
|
|
constexpr static auto Normal = static_cast<Setting>(0);
|
2022-02-01 22:09:44 +01:00
|
|
|
constexpr static auto Not = static_cast<Setting>(1);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
bool begin() {
|
|
|
|
this->m_originalPosition = this->m_curr;
|
|
|
|
this->m_matchedOptionals.clear();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-29 04:16:32 +01:00
|
|
|
bool partBegin() {
|
|
|
|
this->m_partOriginalPosition = this->m_curr;
|
|
|
|
this->m_matchedOptionals.clear();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-25 17:07:01 +02:00
|
|
|
void reset() {
|
|
|
|
this->m_curr = this->m_originalPosition;
|
|
|
|
}
|
|
|
|
|
2022-01-29 04:16:32 +01:00
|
|
|
void partReset() {
|
|
|
|
this->m_curr = this->m_partOriginalPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool resetIfFailed(bool value) {
|
|
|
|
if (!value) reset();
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
template<Setting S = Normal>
|
2022-01-29 04:16:32 +01:00
|
|
|
bool sequenceImpl() {
|
2021-01-22 18:01:39 +01:00
|
|
|
if constexpr (S == Normal)
|
|
|
|
return true;
|
|
|
|
else if constexpr (S == Not)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
__builtin_unreachable();
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
template<Setting S = Normal>
|
2022-01-29 04:16:32 +01:00
|
|
|
bool sequenceImpl(Token::Type type, auto value, auto... args) {
|
2021-01-22 18:01:39 +01:00
|
|
|
if constexpr (S == Normal) {
|
|
|
|
if (!peek(type, value)) {
|
2022-01-29 04:16:32 +01:00
|
|
|
partReset();
|
2021-01-22 18:01:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
this->m_curr++;
|
|
|
|
|
2022-01-29 04:16:32 +01:00
|
|
|
if (!sequenceImpl<Normal>(args...)) {
|
|
|
|
partReset();
|
2021-01-22 18:01:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else if constexpr (S == Not) {
|
|
|
|
if (!peek(type, value))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
this->m_curr++;
|
|
|
|
|
2022-01-29 04:16:32 +01:00
|
|
|
if (!sequenceImpl<Normal>(args...))
|
2021-01-22 18:01:39 +01:00
|
|
|
return true;
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
|
2022-01-29 04:16:32 +01:00
|
|
|
partReset();
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
return false;
|
2021-01-22 18:01:39 +01:00
|
|
|
} else
|
|
|
|
__builtin_unreachable();
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
template<Setting S = Normal>
|
2022-01-29 04:16:32 +01:00
|
|
|
bool sequence(Token::Type type, auto value, auto... args) {
|
|
|
|
return partBegin() && sequenceImpl<S>(type, value, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Setting S = Normal>
|
|
|
|
bool oneOfImpl() {
|
2021-01-22 18:01:39 +01:00
|
|
|
if constexpr (S == Normal)
|
|
|
|
return false;
|
|
|
|
else if constexpr (S == Not)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
__builtin_unreachable();
|
2021-01-20 22:55:57 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
template<Setting S = Normal>
|
2022-01-29 04:16:32 +01:00
|
|
|
bool oneOfImpl(Token::Type type, auto value, auto... args) {
|
2021-01-22 18:01:39 +01:00
|
|
|
if constexpr (S == Normal)
|
2022-01-29 04:16:32 +01:00
|
|
|
return sequenceImpl<Normal>(type, value) || oneOfImpl(args...);
|
2021-01-22 18:01:39 +01:00
|
|
|
else if constexpr (S == Not)
|
2022-01-29 04:16:32 +01:00
|
|
|
return sequenceImpl<Not>(type, value) && oneOfImpl(args...);
|
2021-01-22 18:01:39 +01:00
|
|
|
else
|
|
|
|
__builtin_unreachable();
|
2021-01-20 22:55:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 04:16:32 +01:00
|
|
|
template<Setting S = Normal>
|
|
|
|
bool oneOf(Token::Type type, auto value, auto... args) {
|
|
|
|
return partBegin() && oneOfImpl<S>(type, value, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool variantImpl(Token::Type type1, auto value1, Token::Type type2, auto value2) {
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
if (!peek(type1, value1)) {
|
|
|
|
if (!peek(type2, value2)) {
|
2022-01-29 04:16:32 +01:00
|
|
|
partReset();
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_curr++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-29 04:16:32 +01:00
|
|
|
bool variant(Token::Type type1, auto value1, Token::Type type2, auto value2) {
|
|
|
|
return partBegin() && variantImpl(type1, value1, type2, value2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool optionalImpl(Token::Type type, auto value) {
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
if (peek(type, value)) {
|
|
|
|
this->m_matchedOptionals.push_back(this->m_curr);
|
|
|
|
this->m_curr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-29 04:16:32 +01:00
|
|
|
bool optional(Token::Type type, auto value) {
|
|
|
|
return partBegin() && optionalImpl(type, value);
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
bool peek(Token::Type type, auto value, i32 index = 0) {
|
Pattern Language rewrite (#111)
* Initial parser rewrite effort
Lexer and Token cleanup, Parser started over
* Greatly improved parser syntax
* Reimplemented using declarations and variable placement parsing
* Added back unions and structs
* Added enums as well as mathematical expressions (+, -, *, /, <<, >>, &, |, ^)
* Code style improvement
* Implemented arrays and fixed memory issues
* Fixed more memory issues in parser, reimplemented validator, evaluator and patterns
* Fixed builtin types, arrays and reimplemented strings
* Improved error messages
* Made character a distinct type, used for chars and strings
* Implemented padding, fixed arrays
* Added bitfields
* Added rvalue parsing, no evaluating yet
* Added .idea folder to gitignore
* Fixed build on MacOS
* Added custom implementation of integral concept if not available
* Rebased onto master
* Fixed array variable decl crash
* Added rvalues and dot syntax
* Lower case all pattern language error messages
* Fixed typo in variable name
* Fixed bug where preprocessor would not ignore commented out directives
* Reimplemented pointers
* Fixed rebase issues
2021-01-02 20:27:11 +01:00
|
|
|
return this->m_curr[index].type == type && this->m_curr[index] == value;
|
|
|
|
}
|
2020-11-10 15:26:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|