From 5b3ae56912cbe12d958848c2502b7c3146974c84 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 20 Dec 2023 10:08:40 +0100 Subject: [PATCH] patterns: Update all pattern language code to use new API --- lib/external/pattern_language | 2 +- plugins/builtin/include/ui/pattern_drawer.hpp | 4 +-- .../content/views/view_data_inspector.cpp | 2 +- .../content/views/view_pattern_editor.cpp | 19 ++++++------ plugins/builtin/source/ui/pattern_drawer.cpp | 30 +++++++------------ 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index b5de2d2f8..0bacaf8a2 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit b5de2d2f8aa073c8de3a0bc419b43a4a73070ae3 +Subproject commit 0bacaf8a286c739c4a4f4c5e3e1dc94039f33d52 diff --git a/plugins/builtin/include/ui/pattern_drawer.hpp b/plugins/builtin/include/ui/pattern_drawer.hpp index 7591cbc48..e2dce71c0 100644 --- a/plugins/builtin/include/ui/pattern_drawer.hpp +++ b/plugins/builtin/include/ui/pattern_drawer.hpp @@ -22,7 +22,7 @@ namespace hex::plugin::builtin::ui { virtual ~PatternDrawer() = default; - void draw(const std::vector> &patterns, const pl::PatternLanguage *runtime = nullptr, float height = 0.0F); + void draw(const std::vector> &patterns, pl::PatternLanguage *runtime = nullptr, float height = 0.0F); enum class TreeStyle { Default = 0, @@ -87,7 +87,7 @@ namespace hex::plugin::builtin::ui { std::optional value; }; - std::optional parseRValueFilter(const std::string &filter) const; + std::optional parseRValueFilter(pl::PatternLanguage *runtime, const std::string &filter) const; private: std::map m_displayEnd; diff --git a/plugins/builtin/source/content/views/view_data_inspector.cpp b/plugins/builtin/source/content/views/view_data_inspector.cpp index 890825ef5..4fa24ea5d 100644 --- a/plugins/builtin/source/content/views/view_data_inspector.cpp +++ b/plugins/builtin/source/content/views/view_data_inspector.cpp @@ -133,7 +133,7 @@ namespace hex::plugin::builtin { // Execute the inspector file if (!inspectorCode.empty()) { - if (m_runtime.executeString(inspectorCode, {}, inVariables, true)) { + if (m_runtime.executeString(inspectorCode, pl::api::Source::DefaultSource, {}, inVariables, true)) { // Loop over patterns produced by the runtime const auto &patterns = m_runtime.getPatterns(); diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 0d69dc326..9fdafe347 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -857,14 +857,13 @@ namespace hex::plugin::builtin { if (!file.isValid()) continue; - try { - auto &preprocessor = runtime.getInternals().preprocessor; - auto ret = preprocessor->preprocess(runtime, file.readString()); - if (!ret.has_value()) { - log::warn("Failed to preprocess file {} during MIME analysis: {}", entry.path().string(), preprocessor->getError()->what()); - } - } catch (pl::core::err::PreprocessorError::Exception &e) { - log::warn("Failed to preprocess file {} during MIME analysis: {}", entry.path().string(), e.what()); + auto &preprocessor = runtime.getInternals().preprocessor; + + auto source = runtime.addVirtualSource(file.readString(), wolv::util::toUTF8String(file.getPath())); + + auto result = preprocessor->preprocess(&runtime, source); + if (result.hasErrs()) { + log::warn("Failed to preprocess file {} during MIME analysis", entry.path().string()); } if (foundCorrectType) @@ -960,7 +959,7 @@ namespace hex::plugin::builtin { m_runningParsers += 1; ContentRegistry::PatternLanguage::configureRuntime(*m_parserRuntime, nullptr); - auto ast = m_parserRuntime->parseString(code); + auto ast = m_parserRuntime->parseString(code, pl::api::Source::DefaultSource); auto &patternVariables = m_patternVariables.get(provider); @@ -1088,7 +1087,7 @@ namespace hex::plugin::builtin { }; - m_lastEvaluationResult = runtime.executeString(code, envVars, inVariables); + m_lastEvaluationResult = runtime.executeString(code, pl::api::Source::DefaultSource, envVars, inVariables); if (!m_lastEvaluationResult) { *m_lastEvaluationError = runtime.getError(); } diff --git a/plugins/builtin/source/ui/pattern_drawer.cpp b/plugins/builtin/source/ui/pattern_drawer.cpp index f11baa77f..2f7191ac2 100644 --- a/plugins/builtin/source/ui/pattern_drawer.cpp +++ b/plugins/builtin/source/ui/pattern_drawer.cpp @@ -157,7 +157,7 @@ namespace hex::plugin::builtin::ui { } - std::optional PatternDrawer::parseRValueFilter(const std::string &filter) const { + std::optional PatternDrawer::parseRValueFilter(pl::PatternLanguage *runtime, const std::string &filter) const { Filter result; if (filter.empty()) { @@ -169,23 +169,15 @@ namespace hex::plugin::builtin::ui { char c = filter[i]; if (i < filter.size() - 1 && c == '=' && filter[i + 1] == '=') { - try { - pl::core::Lexer lexer; - - auto source = filter.substr(i + 2); - auto tokens = lexer.lex(filter.substr(i + 2), filter.substr(i + 2)); - - if (!tokens.has_value() || tokens->size() != 2) - return std::nullopt; - - auto literal = std::get_if(&tokens->front().value); - if (literal == nullptr) - return std::nullopt; - - result.value = *literal; - } catch (pl::core::err::LexerError &) { + auto tokens = runtime->lexString(filter.substr(i + 2), pl::api::Source::DefaultSource); + if (!tokens.has_value()) return std::nullopt; - } + + auto literal = std::get_if(&tokens->front().value); + if (literal == nullptr) + return std::nullopt; + + result.value = *literal; break; } else if (c == '.') @@ -1083,7 +1075,7 @@ namespace hex::plugin::builtin::ui { } } - void PatternDrawer::draw(const std::vector> &patterns, const pl::PatternLanguage *runtime, float height) { + void PatternDrawer::draw(const std::vector> &patterns, pl::PatternLanguage *runtime, float height) { std::scoped_lock lock(s_resetDrawMutex); const auto treeStyleButton = [this](auto icon, TreeStyle style, const char *tooltip) { @@ -1109,7 +1101,7 @@ namespace hex::plugin::builtin::ui { ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 9.5); if (ImGuiExt::InputTextIcon("##Search", ICON_VS_FILTER, m_filterText)) { - m_filter = parseRValueFilter(m_filterText).value_or(Filter{ }); + m_filter = parseRValueFilter(runtime, m_filterText).value_or(Filter{ }); } ImGui::PopItemWidth();