1
0
mirror of synced 2024-11-28 09:30:51 +01:00

patterns: Fixed recursive types

This commit is contained in:
WerWolv 2022-03-17 00:10:16 +01:00
parent 844845223f
commit 2d87d29fa0
7 changed files with 46 additions and 43 deletions

View File

@ -19,12 +19,12 @@ namespace hex::pl {
class ASTNodeArrayVariableDecl : public ASTNode,
public Attributable {
public:
ASTNodeArrayVariableDecl(std::string name, std::unique_ptr<ASTNode> &&type, std::unique_ptr<ASTNode> &&size, std::unique_ptr<ASTNode> &&placementOffset = {})
ASTNodeArrayVariableDecl(std::string name, std::shared_ptr<ASTNodeTypeDecl> type, std::unique_ptr<ASTNode> &&size, std::unique_ptr<ASTNode> &&placementOffset = {})
: ASTNode(), m_name(std::move(name)), m_type(std::move(type)), m_size(std::move(size)), m_placementOffset(std::move(placementOffset)) { }
ASTNodeArrayVariableDecl(const ASTNodeArrayVariableDecl &other) : ASTNode(other), Attributable(other) {
this->m_name = other.m_name;
this->m_type = other.m_type->clone();
this->m_type = other.m_type;
if (other.m_size != nullptr)
this->m_size = other.m_size->clone();
else
@ -81,7 +81,7 @@ namespace hex::pl {
private:
std::string m_name;
std::unique_ptr<ASTNode> m_type;
std::shared_ptr<ASTNodeTypeDecl> m_type;
std::unique_ptr<ASTNode> m_size;
std::unique_ptr<ASTNode> m_placementOffset;

View File

@ -10,13 +10,13 @@ namespace hex::pl {
class ASTNodePointerVariableDecl : public ASTNode,
public Attributable {
public:
ASTNodePointerVariableDecl(std::string name, std::shared_ptr<ASTNode> &&type, std::shared_ptr<ASTNode> &&sizeType, std::unique_ptr<ASTNode> &&placementOffset = nullptr)
ASTNodePointerVariableDecl(std::string name, std::shared_ptr<ASTNodeTypeDecl> type, std::shared_ptr<ASTNodeTypeDecl> sizeType, std::unique_ptr<ASTNode> &&placementOffset = nullptr)
: ASTNode(), m_name(std::move(name)), m_type(std::move(type)), m_sizeType(std::move(sizeType)), m_placementOffset(std::move(placementOffset)) { }
ASTNodePointerVariableDecl(const ASTNodePointerVariableDecl &other) : ASTNode(other), Attributable(other) {
this->m_name = other.m_name;
this->m_type = other.m_type->clone();
this->m_sizeType = other.m_sizeType->clone();
this->m_type = other.m_type;
this->m_sizeType = other.m_sizeType;
if (other.m_placementOffset != nullptr)
this->m_placementOffset = other.m_placementOffset->clone();
@ -29,8 +29,8 @@ namespace hex::pl {
}
[[nodiscard]] const std::string &getName() const { return this->m_name; }
[[nodiscard]] constexpr const std::shared_ptr<ASTNode> &getType() const { return this->m_type; }
[[nodiscard]] constexpr const std::shared_ptr<ASTNode> &getSizeType() const { return this->m_sizeType; }
[[nodiscard]] constexpr const std::shared_ptr<ASTNodeTypeDecl> &getType() const { return this->m_type; }
[[nodiscard]] constexpr const std::shared_ptr<ASTNodeTypeDecl> &getSizeType() const { return this->m_sizeType; }
[[nodiscard]] constexpr const std::unique_ptr<ASTNode> &getPlacementOffset() const { return this->m_placementOffset; }
[[nodiscard]] std::vector<std::unique_ptr<Pattern>> createPatterns(Evaluator *evaluator) const override {
@ -87,8 +87,8 @@ namespace hex::pl {
private:
std::string m_name;
std::shared_ptr<ASTNode> m_type;
std::shared_ptr<ASTNode> m_sizeType;
std::shared_ptr<ASTNodeTypeDecl> m_type;
std::shared_ptr<ASTNodeTypeDecl> m_sizeType;
std::unique_ptr<ASTNode> m_placementOffset;
};

View File

@ -8,7 +8,7 @@ namespace hex::pl {
class ASTNodeTypeDecl : public ASTNode,
public Attributable {
public:
ASTNodeTypeDecl(std::string name, std::shared_ptr<ASTNode> &&type, std::optional<std::endian> endian = std::nullopt)
ASTNodeTypeDecl(std::string name, std::shared_ptr<ASTNode> type, std::optional<std::endian> endian = std::nullopt)
: ASTNode(), m_name(std::move(name)), m_type(std::move(type)), m_endian(endian) { }
ASTNodeTypeDecl(const ASTNodeTypeDecl &other) : ASTNode(other), Attributable(other) {

View File

@ -3,18 +3,19 @@
#include <hex/pattern_language/ast/ast_node.hpp>
#include <hex/pattern_language/ast/ast_node_attribute.hpp>
#include <hex/pattern_language/ast/ast_node_literal.hpp>
#include <hex/pattern_language/ast/ast_node_type_decl.hpp>
namespace hex::pl {
class ASTNodeVariableDecl : public ASTNode,
public Attributable {
public:
ASTNodeVariableDecl(std::string name, std::unique_ptr<ASTNode> &&type, std::unique_ptr<ASTNode> &&placementOffset = nullptr, bool inVariable = false, bool outVariable = false)
ASTNodeVariableDecl(std::string name, std::shared_ptr<ASTNodeTypeDecl> type, std::unique_ptr<ASTNode> &&placementOffset = nullptr, bool inVariable = false, bool outVariable = false)
: ASTNode(), m_name(std::move(name)), m_type(std::move(type)), m_placementOffset(std::move(placementOffset)), m_inVariable(inVariable), m_outVariable(outVariable) { }
ASTNodeVariableDecl(const ASTNodeVariableDecl &other) : ASTNode(other), Attributable(other) {
this->m_name = other.m_name;
this->m_type = other.m_type->clone();
this->m_type = other.m_type;
if (other.m_placementOffset != nullptr)
this->m_placementOffset = other.m_placementOffset->clone();
@ -30,7 +31,7 @@ namespace hex::pl {
}
[[nodiscard]] const std::string &getName() const { return this->m_name; }
[[nodiscard]] constexpr const std::unique_ptr<ASTNode> &getType() const { return this->m_type; }
[[nodiscard]] constexpr const std::shared_ptr<ASTNodeTypeDecl> &getType() const { return this->m_type; }
[[nodiscard]] constexpr const std::unique_ptr<ASTNode> &getPlacementOffset() const { return this->m_placementOffset; }
[[nodiscard]] constexpr bool isInVariable() const { return this->m_inVariable; }
@ -71,7 +72,7 @@ namespace hex::pl {
private:
std::string m_name;
std::unique_ptr<ASTNode> m_type;
std::shared_ptr<ASTNodeTypeDecl> m_type;
std::unique_ptr<ASTNode> m_placementOffset;
bool m_inVariable = false, m_outVariable = false;

View File

@ -112,17 +112,17 @@ namespace hex::pl {
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> parseMemberVariable(const std::shared_ptr<ASTNodeTypeDecl> &type);
std::unique_ptr<ASTNode> parseMemberArrayVariable(const std::shared_ptr<ASTNodeTypeDecl> &type);
std::unique_ptr<ASTNode> parseMemberPointerVariable(const std::shared_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> parseVariablePlacement(const std::shared_ptr<ASTNodeTypeDecl> &type);
std::unique_ptr<ASTNode> parseArrayVariablePlacement(const std::shared_ptr<ASTNodeTypeDecl> &type);
std::unique_ptr<ASTNode> parsePointerVariablePlacement(const std::shared_ptr<ASTNodeTypeDecl> &type);
std::unique_ptr<ASTNode> parsePlacement();
std::vector<std::shared_ptr<ASTNode>> parseNamespace();
std::vector<std::shared_ptr<ASTNode>> parseStatements();

View File

@ -686,9 +686,9 @@ namespace hex::pl {
std::string typeName = parseNamespaceResolution();
if (this->m_types.contains(typeName))
return create(new ASTNodeTypeDecl({}, this->m_types[typeName]->clone(), endian));
return create(new ASTNodeTypeDecl({}, this->m_types[typeName], endian));
else if (this->m_types.contains(getNamespacePrefixedName(typeName)))
return create(new ASTNodeTypeDecl({}, this->m_types[getNamespacePrefixedName(typeName)]->clone(), endian));
return create(new ASTNodeTypeDecl({}, this->m_types[getNamespacePrefixedName(typeName)], endian));
else
throwParserError(hex::format("unknown type '{}'", typeName));
} else if (MATCHES(sequence(VALUETYPE_ANY))) { // Builtin type
@ -728,24 +728,24 @@ namespace hex::pl {
}
// (parseType) Identifier
std::unique_ptr<ASTNode> Parser::parseMemberVariable(std::unique_ptr<ASTNodeTypeDecl> &&type) {
std::unique_ptr<ASTNode> Parser::parseMemberVariable(const std::shared_ptr<ASTNodeTypeDecl> &type) {
if (peek(SEPARATOR_COMMA)) {
std::vector<std::unique_ptr<ASTNode>> variables;
do {
variables.push_back(create(new ASTNodeVariableDecl(getValue<Token::Identifier>(-1).get(), type->clone())));
variables.push_back(create(new ASTNodeVariableDecl(getValue<Token::Identifier>(-1).get(), type)));
} while (MATCHES(sequence(SEPARATOR_COMMA, IDENTIFIER)));
return create(new ASTNodeMultiVariableDecl(std::move(variables)));
} else if (MATCHES(sequence(OPERATOR_AT)))
return create(new ASTNodeVariableDecl(getValue<Token::Identifier>(-2).get(), std::move(type), parseMathematicalExpression()));
return create(new ASTNodeVariableDecl(getValue<Token::Identifier>(-2).get(), type, parseMathematicalExpression()));
else
return create(new ASTNodeVariableDecl(getValue<Token::Identifier>(-1).get(), std::move(type)));
return create(new ASTNodeVariableDecl(getValue<Token::Identifier>(-1).get(), type));
}
// (parseType) Identifier[(parseMathematicalExpression)]
std::unique_ptr<ASTNode> Parser::parseMemberArrayVariable(std::unique_ptr<ASTNodeTypeDecl> &&type) {
std::unique_ptr<ASTNode> Parser::parseMemberArrayVariable(const std::shared_ptr<ASTNodeTypeDecl> &type) {
auto name = getValue<Token::Identifier>(-2).get();
std::unique_ptr<ASTNode> size;
@ -761,13 +761,13 @@ namespace hex::pl {
}
if (MATCHES(sequence(OPERATOR_AT)))
return create(new ASTNodeArrayVariableDecl(name, std::move(type), std::move(size), parseMathematicalExpression()));
return create(new ASTNodeArrayVariableDecl(name, type, std::move(size), parseMathematicalExpression()));
else
return create(new ASTNodeArrayVariableDecl(name, std::move(type), std::move(size)));
return create(new ASTNodeArrayVariableDecl(name, type, std::move(size)));
}
// (parseType) *Identifier : (parseType)
std::unique_ptr<ASTNode> Parser::parseMemberPointerVariable(std::unique_ptr<ASTNodeTypeDecl> &&type) {
std::unique_ptr<ASTNode> Parser::parseMemberPointerVariable(const std::shared_ptr<ASTNodeTypeDecl> &type) {
auto name = getValue<Token::Identifier>(-2).get();
auto sizeType = parseType();
@ -780,9 +780,9 @@ namespace hex::pl {
}
if (MATCHES(sequence(OPERATOR_AT)))
return create(new ASTNodePointerVariableDecl(name, std::move(type), std::move(sizeType), parseMathematicalExpression()));
return create(new ASTNodePointerVariableDecl(name, type, std::move(sizeType), parseMathematicalExpression()));
else
return create(new ASTNodePointerVariableDecl(name, std::move(type), std::move(sizeType)));
return create(new ASTNodePointerVariableDecl(name, type, std::move(sizeType)));
}
// [(parsePadding)|(parseMemberVariable)|(parseMemberArrayVariable)|(parseMemberPointerVariable)]
@ -977,7 +977,7 @@ namespace hex::pl {
}
// (parseType) Identifier @ Integer
std::unique_ptr<ASTNode> Parser::parseVariablePlacement(std::unique_ptr<ASTNodeTypeDecl> &&type) {
std::unique_ptr<ASTNode> Parser::parseVariablePlacement(const std::shared_ptr<ASTNodeTypeDecl> &type) {
bool inVariable = false;
bool outVariable = false;
@ -992,11 +992,11 @@ namespace hex::pl {
outVariable = true;
}
return create(new ASTNodeVariableDecl(name, type->clone(), std::move(placementOffset), inVariable, outVariable));
return create(new ASTNodeVariableDecl(name, type, std::move(placementOffset), inVariable, outVariable));
}
// (parseType) Identifier[[(parseMathematicalExpression)]] @ Integer
std::unique_ptr<ASTNode> Parser::parseArrayVariablePlacement(std::unique_ptr<ASTNodeTypeDecl> &&type) {
std::unique_ptr<ASTNode> Parser::parseArrayVariablePlacement(const std::shared_ptr<ASTNodeTypeDecl> &type) {
auto name = getValue<Token::Identifier>(-2).get();
std::unique_ptr<ASTNode> size;
@ -1016,11 +1016,11 @@ namespace hex::pl {
auto placementOffset = parseMathematicalExpression();
return create(new ASTNodeArrayVariableDecl(name, std::move(type), std::move(size), std::move(placementOffset)));
return create(new ASTNodeArrayVariableDecl(name, type, std::move(size), std::move(placementOffset)));
}
// (parseType) *Identifier : (parseType) @ Integer
std::unique_ptr<ASTNode> Parser::parsePointerVariablePlacement(std::unique_ptr<ASTNodeTypeDecl> &&type) {
std::unique_ptr<ASTNode> Parser::parsePointerVariablePlacement(const std::shared_ptr<ASTNodeTypeDecl> &type) {
auto name = getValue<Token::Identifier>(-2).get();
auto sizeType = parseType();
@ -1037,7 +1037,7 @@ namespace hex::pl {
auto placementOffset = parseMathematicalExpression();
return create(new ASTNodePointerVariableDecl(name, type->clone(), std::move(sizeType), std::move(placementOffset)));
return create(new ASTNodePointerVariableDecl(name, type, std::move(sizeType), std::move(placementOffset)));
}
std::vector<std::shared_ptr<ASTNode>> Parser::parseNamespace() {

View File

@ -357,10 +357,12 @@ namespace hex::plugin::builtin {
this->m_console = this->m_lastEvaluationLog;
if (!this->m_lastEvaluationResult) {
TextEditor::ErrorMarkers errorMarkers = {
{this->m_lastEvaluationError->getLineNumber(), this->m_lastEvaluationError->what()}
};
this->m_textEditor.SetErrorMarkers(errorMarkers);
if (this->m_lastEvaluationError) {
TextEditor::ErrorMarkers errorMarkers = {
{this->m_lastEvaluationError->getLineNumber(), this->m_lastEvaluationError->what()}
};
this->m_textEditor.SetErrorMarkers(errorMarkers);
}
} else {
for (auto &[name, variable] : this->m_patternVariables) {
if (variable.outVariable && this->m_lastEvaluationOutVars.contains(name))