1
0
mirror of synced 2024-11-24 15:50:16 +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;
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 {
Default = 0,
@ -87,7 +87,7 @@ namespace hex::plugin::builtin::ui {
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:
std::map<const pl::ptrn::Pattern*, u64> m_displayEnd;

View File

@ -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();

View File

@ -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();
}

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;
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<pl::core::Token::Literal>(&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<pl::core::Token::Literal>(&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<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);
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();