From 9399cf873f50ee6de921a90ac74492975e0e744b Mon Sep 17 00:00:00 2001 From: Polshakov Dmitry Date: Sat, 12 Feb 2022 17:40:42 +0300 Subject: [PATCH] pattern: Add attribute to format array entries (#435) Co-authored-by: Dmitry Polshakov --- .../include/hex/pattern_language/ast_node.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/libimhex/include/hex/pattern_language/ast_node.hpp b/lib/libimhex/include/hex/pattern_language/ast_node.hpp index bbf96b4a7..71d8c06b0 100644 --- a/lib/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/lib/libimhex/include/hex/pattern_language/ast_node.hpp @@ -803,6 +803,24 @@ namespace hex::pl { else inlinable->setInlined(true); } + + if (auto value = attributable->getAttributeValue("format_entries"); value) { + auto functions = evaluator->getCustomFunctions(); + if (!functions.contains(*value)) + LogConsole::abortEvaluation(hex::format("cannot find formatter function '{}'", *value), node); + + const auto &function = functions[*value]; + if (function.parameterCount != 1) + LogConsole::abortEvaluation("formatter function needs exactly one parameter", node); + + auto array = dynamic_cast(pattern); + if (array == nullptr) + LogConsole::abortEvaluation("inline_array attribute can only be applied to array types", node); + + for (const auto& entry : array->getEntries()) { + entry->setFormatterFunction(function); + } + } } class ASTNodeVariableDecl : public ASTNode,