1
0
mirror of synced 2024-11-28 17:40:51 +01:00

patterns: Update all pattern language code to use new API

This commit is contained in:
WerWolv 2023-12-20 10:08:40 +01:00
parent 2b5789631f
commit 5b3ae56912
5 changed files with 24 additions and 33 deletions

@ -1 +1 @@
Subproject commit b5de2d2f8aa073c8de3a0bc419b43a4a73070ae3 Subproject commit 0bacaf8a286c739c4a4f4c5e3e1dc94039f33d52

View File

@ -22,7 +22,7 @@ namespace hex::plugin::builtin::ui {
virtual ~PatternDrawer() = default; virtual ~PatternDrawer() = default;
void draw(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, const pl::PatternLanguage *runtime = nullptr, float height = 0.0F); void draw(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, pl::PatternLanguage *runtime = nullptr, float height = 0.0F);
enum class TreeStyle { enum class TreeStyle {
Default = 0, Default = 0,
@ -87,7 +87,7 @@ namespace hex::plugin::builtin::ui {
std::optional<pl::core::Token::Literal> value; std::optional<pl::core::Token::Literal> value;
}; };
std::optional<Filter> parseRValueFilter(const std::string &filter) const; std::optional<Filter> parseRValueFilter(pl::PatternLanguage *runtime, const std::string &filter) const;
private: private:
std::map<const pl::ptrn::Pattern*, u64> m_displayEnd; std::map<const pl::ptrn::Pattern*, u64> m_displayEnd;

View File

@ -133,7 +133,7 @@ namespace hex::plugin::builtin {
// Execute the inspector file // Execute the inspector file
if (!inspectorCode.empty()) { 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 // Loop over patterns produced by the runtime
const auto &patterns = m_runtime.getPatterns(); const auto &patterns = m_runtime.getPatterns();

View File

@ -857,14 +857,13 @@ namespace hex::plugin::builtin {
if (!file.isValid()) if (!file.isValid())
continue; continue;
try {
auto &preprocessor = runtime.getInternals().preprocessor; auto &preprocessor = runtime.getInternals().preprocessor;
auto ret = preprocessor->preprocess(runtime, file.readString());
if (!ret.has_value()) { auto source = runtime.addVirtualSource(file.readString(), wolv::util::toUTF8String(file.getPath()));
log::warn("Failed to preprocess file {} during MIME analysis: {}", entry.path().string(), preprocessor->getError()->what());
} auto result = preprocessor->preprocess(&runtime, source);
} catch (pl::core::err::PreprocessorError::Exception &e) { if (result.hasErrs()) {
log::warn("Failed to preprocess file {} during MIME analysis: {}", entry.path().string(), e.what()); log::warn("Failed to preprocess file {} during MIME analysis", entry.path().string());
} }
if (foundCorrectType) if (foundCorrectType)
@ -960,7 +959,7 @@ namespace hex::plugin::builtin {
m_runningParsers += 1; m_runningParsers += 1;
ContentRegistry::PatternLanguage::configureRuntime(*m_parserRuntime, nullptr); 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); 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) { if (!m_lastEvaluationResult) {
*m_lastEvaluationError = runtime.getError(); *m_lastEvaluationError = runtime.getError();
} }

View File

@ -157,7 +157,7 @@ namespace hex::plugin::builtin::ui {
} }
std::optional<PatternDrawer::Filter> PatternDrawer::parseRValueFilter(const std::string &filter) const { std::optional<PatternDrawer::Filter> PatternDrawer::parseRValueFilter(pl::PatternLanguage *runtime, const std::string &filter) const {
Filter result; Filter result;
if (filter.empty()) { if (filter.empty()) {
@ -169,13 +169,8 @@ namespace hex::plugin::builtin::ui {
char c = filter[i]; char c = filter[i];
if (i < filter.size() - 1 && c == '=' && filter[i + 1] == '=') { if (i < filter.size() - 1 && c == '=' && filter[i + 1] == '=') {
try { auto tokens = runtime->lexString(filter.substr(i + 2), pl::api::Source::DefaultSource);
pl::core::Lexer lexer; if (!tokens.has_value())
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; return std::nullopt;
auto literal = std::get_if<pl::core::Token::Literal>(&tokens->front().value); auto literal = std::get_if<pl::core::Token::Literal>(&tokens->front().value);
@ -183,9 +178,6 @@ namespace hex::plugin::builtin::ui {
return std::nullopt; return std::nullopt;
result.value = *literal; result.value = *literal;
} catch (pl::core::err::LexerError &) {
return std::nullopt;
}
break; break;
} else if (c == '.') } else if (c == '.')
@ -1083,7 +1075,7 @@ namespace hex::plugin::builtin::ui {
} }
} }
void PatternDrawer::draw(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, const pl::PatternLanguage *runtime, float height) { void PatternDrawer::draw(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, pl::PatternLanguage *runtime, float height) {
std::scoped_lock lock(s_resetDrawMutex); std::scoped_lock lock(s_resetDrawMutex);
const auto treeStyleButton = [this](auto icon, TreeStyle style, const char *tooltip) { 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); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 9.5);
if (ImGuiExt::InputTextIcon("##Search", ICON_VS_FILTER, m_filterText)) { 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(); ImGui::PopItemWidth();