From cd89b55f5b9ad6fdea94ce154085ea4d86a99497 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 31 Oct 2021 15:06:48 +0100 Subject: [PATCH] patterns: ASTNode and LogConsole cleanup --- plugins/libimhex/CMakeLists.txt | 1 + .../include/hex/pattern_language/ast_node.hpp | 55 +++++++++++++++- .../hex/pattern_language/ast_node_base.hpp | 65 ------------------- .../hex/pattern_language/log_console.hpp | 26 ++------ .../source/pattern_language/log_console.cpp | 32 +++++++++ 5 files changed, 91 insertions(+), 88 deletions(-) delete mode 100644 plugins/libimhex/include/hex/pattern_language/ast_node_base.hpp create mode 100644 plugins/libimhex/source/pattern_language/log_console.cpp diff --git a/plugins/libimhex/CMakeLists.txt b/plugins/libimhex/CMakeLists.txt index 9bc207900..771c5cee9 100644 --- a/plugins/libimhex/CMakeLists.txt +++ b/plugins/libimhex/CMakeLists.txt @@ -84,6 +84,7 @@ set(LIBIMHEX_SOURCES source/pattern_language/parser.cpp source/pattern_language/validator.cpp source/pattern_language/evaluator.cpp + source/pattern_language/log_console.cpp source/providers/provider.cpp diff --git a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp index e65cf54c6..e00f10a1e 100644 --- a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp @@ -11,11 +11,62 @@ #include #include -#include - namespace hex::pl { + class ASTNode; + class ASTNodeAttribute; + class PatternData; + class Evaluator; + + class Attributable { + protected: + Attributable() = default; + + Attributable(const Attributable &) = default; + + public: + + void addAttribute(ASTNodeAttribute *attribute) { + this->m_attributes.push_back(attribute); + } + + [[nodiscard]] const auto &getAttributes() const { + return this->m_attributes; + } + + private: + std::vector m_attributes; + }; + + class Clonable { + public: + [[nodiscard]] + virtual ASTNode* clone() const = 0; + }; + + class ASTNode : public Clonable { + public: + constexpr ASTNode() = default; + + constexpr virtual ~ASTNode() = default; + + constexpr ASTNode(const ASTNode &) = default; + + [[nodiscard]] constexpr u32 getLineNumber() const { return this->m_lineNumber; } + + [[maybe_unused]] constexpr void setLineNumber(u32 lineNumber) { this->m_lineNumber = lineNumber; } + + [[nodiscard]] virtual ASTNode *evaluate(Evaluator *evaluator) const { return this->clone(); } + + [[nodiscard]] virtual std::vector createPatterns(Evaluator *evaluator) const { return {}; } + + using FunctionResult = std::pair>; + virtual FunctionResult execute(Evaluator *evaluator) { throw std::pair(this->getLineNumber(), "cannot execute non-function statement"); } + + private: + u32 m_lineNumber = 1; + }; class ASTNodeAttribute : public ASTNode { public: diff --git a/plugins/libimhex/include/hex/pattern_language/ast_node_base.hpp b/plugins/libimhex/include/hex/pattern_language/ast_node_base.hpp deleted file mode 100644 index 624488f04..000000000 --- a/plugins/libimhex/include/hex/pattern_language/ast_node_base.hpp +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -#include -#include - -#include - -namespace hex::pl { - - class ASTNode; - class ASTNodeAttribute; - - class PatternData; - class Evaluator; - - class Attributable { - protected: - Attributable() = default; - - Attributable(const Attributable &) = default; - - public: - - void addAttribute(ASTNodeAttribute *attribute) { - this->m_attributes.push_back(attribute); - } - - [[nodiscard]] const auto &getAttributes() const { - return this->m_attributes; - } - - private: - std::vector m_attributes; - }; - - class Clonable { - public: - [[nodiscard]] - virtual ASTNode* clone() const = 0; - }; - - class ASTNode : public Clonable { - public: - constexpr ASTNode() = default; - - constexpr virtual ~ASTNode() = default; - - constexpr ASTNode(const ASTNode &) = default; - - [[nodiscard]] constexpr u32 getLineNumber() const { return this->m_lineNumber; } - - [[maybe_unused]] constexpr void setLineNumber(u32 lineNumber) { this->m_lineNumber = lineNumber; } - - [[nodiscard]] virtual ASTNode *evaluate(Evaluator *evaluator) const { return this->clone(); } - - [[nodiscard]] virtual std::vector createPatterns(Evaluator *evaluator) const { return {}; } - - using FunctionResult = std::pair>; - virtual FunctionResult execute(Evaluator *evaluator) { throw std::pair(this->getLineNumber(), "cannot execute non-function statement"); } - - private: - u32 m_lineNumber = 1; - }; - -} \ No newline at end of file diff --git a/plugins/libimhex/include/hex/pattern_language/log_console.hpp b/plugins/libimhex/include/hex/pattern_language/log_console.hpp index fbc6da852..78a49d4a0 100644 --- a/plugins/libimhex/include/hex/pattern_language/log_console.hpp +++ b/plugins/libimhex/include/hex/pattern_language/log_console.hpp @@ -2,13 +2,12 @@ #include +#include #include #include #include #include -#include - namespace hex::pl { class ASTNode; @@ -27,30 +26,15 @@ namespace hex::pl { using EvaluateError = std::pair; - void log(Level level, const std::string &message) { - switch (level) { - default: - case Level::Debug: this->m_consoleLog.emplace_back(level, "[-] " + message); break; - case Level::Info: this->m_consoleLog.emplace_back(level, "[i] " + message); break; - case Level::Warning: this->m_consoleLog.emplace_back(level, "[*] " + message); break; - case Level::Error: this->m_consoleLog.emplace_back(level, "[!] " + message); break; - } - } + void log(Level level, const std::string &message); [[noreturn]] - static void abortEvaluation(const std::string &message) { - throw EvaluateError(0, message); - } + static void abortEvaluation(const std::string &message); [[noreturn]] - static void abortEvaluation(const std::string &message, const auto *node) { - throw EvaluateError(static_cast(node)->getLineNumber(), message); - } + static void abortEvaluation(const std::string &message, const ASTNode *node); - void clear() { - this->m_consoleLog.clear(); - this->m_lastHardError = { }; - } + void clear(); void setHardError(const EvaluateError &error) { this->m_lastHardError = error; } diff --git a/plugins/libimhex/source/pattern_language/log_console.cpp b/plugins/libimhex/source/pattern_language/log_console.cpp new file mode 100644 index 000000000..3074918ee --- /dev/null +++ b/plugins/libimhex/source/pattern_language/log_console.cpp @@ -0,0 +1,32 @@ +#include + +#include + +namespace hex::pl { + + void LogConsole::log(Level level, const std::string &message) { + switch (level) { + default: + case Level::Debug: this->m_consoleLog.emplace_back(level, "[-] " + message); break; + case Level::Info: this->m_consoleLog.emplace_back(level, "[i] " + message); break; + case Level::Warning: this->m_consoleLog.emplace_back(level, "[*] " + message); break; + case Level::Error: this->m_consoleLog.emplace_back(level, "[!] " + message); break; + } + } + + [[noreturn]] + void LogConsole::abortEvaluation(const std::string &message) { + throw EvaluateError(0, message); + } + + [[noreturn]] + void LogConsole::abortEvaluation(const std::string &message, const ASTNode *node) { + throw EvaluateError(static_cast(node)->getLineNumber(), message); + } + + void LogConsole::clear() { + this->m_consoleLog.clear(); + this->m_lastHardError = { }; + } + +} \ No newline at end of file