patterns: Huge refactor of Pattern Language runtime to use smart pointers (#458)

* patterns: Initial work to refactor pattern language to use smart pointers

* patterns: Fixed remaining issues, moved patterns to unique files

* sys: Added missing includes for macOS
This commit is contained in:
WerWolv
2022-02-27 23:25:39 +01:00
committed by GitHub
parent b28eaf2dbf
commit 66d1b3fd2f
85 changed files with 4990 additions and 5146 deletions

View File

@@ -7,7 +7,7 @@
#include <hex/pattern_language/token.hpp>
#include <hex/pattern_language/log_console.hpp>
#include <hex/pattern_language/evaluator.hpp>
#include <hex/pattern_language/pattern_data.hpp>
#include <hex/pattern_language/patterns/pattern.hpp>
#include <vector>
@@ -15,7 +15,7 @@
namespace hex::plugin::builtin {
std::string format(pl::Evaluator *ctx, auto params) {
std::string format(pl::Evaluator *ctx, const auto &params) {
auto format = pl::Token::literalToString(params[0], true);
std::string message;
@@ -25,7 +25,7 @@ namespace hex::plugin::builtin {
auto &param = params[i];
std::visit(overloaded {
[&](pl::PatternData *value) {
[&](const std::shared_ptr<pl::Pattern> &value) {
formatArgs.push_back(value->toString(ctx->getProvider()));
},
[&](auto &&value) {

View File

@@ -1,8 +1,10 @@
#include "content/providers/disk_provider.hpp"
#include <hex/helpers/fmt.hpp>
#include <hex/api/localization.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/utils.hpp>
#include <bitset>
#include <filesystem>

View File

@@ -1,7 +1,9 @@
#include "content/views/view_pattern_data.hpp"
#include <hex/providers/provider.hpp>
#include <hex/pattern_language/pattern_data.hpp>
#include <hex/pattern_language/pattern_language.hpp>
#include <hex/pattern_language/patterns/pattern.hpp>
namespace hex::plugin::builtin {
@@ -18,8 +20,8 @@ namespace hex::plugin::builtin {
EventManager::unsubscribe<EventHighlightingChanged>(this);
}
static bool beginPatternDataTable(prv::Provider *&provider, const std::vector<pl::PatternData *> &patterns, std::vector<pl::PatternData *> &sortedPatterns) {
if (ImGui::BeginTable("##patterndatatable", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
static bool beginPatternTable(prv::Provider *&provider, const std::vector<std::shared_ptr<pl::Pattern>> &patterns, std::vector<std::shared_ptr<pl::Pattern>> &sortedPatterns) {
if (ImGui::BeginTable("##Patterntable", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("hex.builtin.view.pattern_data.var_name"_lang, 0, 0, ImGui::GetID("name"));
ImGui::TableSetupColumn("hex.builtin.view.pattern_data.color"_lang, 0, 0, ImGui::GetID("color"));
@@ -33,8 +35,8 @@ namespace hex::plugin::builtin {
if (sortSpecs->SpecsDirty || sortedPatterns.empty()) {
sortedPatterns = patterns;
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs, &provider](pl::PatternData *left, pl::PatternData *right) -> bool {
return pl::PatternData::sortPatternDataTable(sortSpecs, provider, left, right);
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs, &provider](const std::shared_ptr<pl::Pattern> &left, const std::shared_ptr<pl::Pattern> &right) -> bool {
return pl::Pattern::sortPatternTable(sortSpecs, provider, left.get(), right.get());
});
for (auto &pattern : sortedPatterns)
@@ -55,12 +57,12 @@ namespace hex::plugin::builtin {
if (ImHexApi::Provider::isValid() && provider->isReadable()) {
auto &sortedPatterns = this->m_sortedPatterns[ImHexApi::Provider::get()];
if (beginPatternDataTable(provider, provider->getPatternLanguageRuntime().getPatterns(), sortedPatterns)) {
if (beginPatternTable(provider, provider->getPatternLanguageRuntime().getPatterns(), sortedPatterns)) {
ImGui::TableHeadersRow();
if (!sortedPatterns.empty()) {
for (auto &patternData : sortedPatterns)
patternData->draw(provider);
for (auto &patterns : sortedPatterns)
patterns->draw(provider);
}
ImGui::EndTable();

View File

@@ -1,13 +1,17 @@
#include "content/views/view_pattern_editor.hpp"
#include <hex/helpers/project_file_handler.hpp>
#include <hex/pattern_language/preprocessor.hpp>
#include <hex/pattern_language/pattern_data.hpp>
#include <hex/pattern_language/ast_node.hpp>
#include <hex/pattern_language/patterns/pattern.hpp>
#include <hex/pattern_language/ast/ast_node.hpp>
#include <hex/pattern_language/ast/ast_node_variable_decl.hpp>
#include <hex/pattern_language/ast/ast_node_type_decl.hpp>
#include <hex/pattern_language/ast/ast_node_builtin_type.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/project_file_handler.hpp>
#include <hex/helpers/magic.hpp>
namespace hex::plugin::builtin {
@@ -225,7 +229,7 @@ namespace hex::plugin::builtin {
ImHexApi::HexEditor::addHighlightingProvider([](u64 address) -> std::optional<ImHexApi::HexEditor::Highlighting> {
auto patterns = ImHexApi::Provider::get()->getPatternLanguageRuntime().getPatterns();
const auto &patterns = ImHexApi::Provider::get()->getPatternLanguageRuntime().getPatterns();
for (const auto &pattern : patterns) {
auto child = pattern->getPattern(address);
if (child != nullptr) {
@@ -586,7 +590,7 @@ namespace hex::plugin::builtin {
}
}
void ViewPatternEditor::clearPatternData() {
void ViewPatternEditor::clearPatterns() {
if (!ImHexApi::Provider::isValid()) return;
ImHexApi::Provider::get()->getPatternLanguageRuntime().reset();
@@ -603,12 +607,12 @@ namespace hex::plugin::builtin {
this->m_patternTypes.clear();
if (ast) {
for (auto node : *ast) {
if (auto variableDecl = dynamic_cast<pl::ASTNodeVariableDecl *>(node)) {
auto type = dynamic_cast<pl::ASTNodeTypeDecl *>(variableDecl->getType());
for (auto &node : *ast) {
if (auto variableDecl = dynamic_cast<pl::ASTNodeVariableDecl *>(node.get())) {
auto type = dynamic_cast<pl::ASTNodeTypeDecl *>(variableDecl->getType().get());
if (type == nullptr) continue;
auto builtinType = dynamic_cast<pl::ASTNodeBuiltinType *>(type->getType());
auto builtinType = dynamic_cast<pl::ASTNodeBuiltinType *>(type->getType().get());
if (builtinType == nullptr) continue;
PatternVariable variable = {
@@ -634,7 +638,7 @@ namespace hex::plugin::builtin {
this->m_textEditor.SetErrorMarkers({});
this->m_console.clear();
this->clearPatternData();
this->clearPatterns();
EventManager::post<EventHighlightingChanged>();