diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 173fed318..37b4bc7f2 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 173fed3182b2b2d82f952f41cc9d9ce2d19fa2e7 +Subproject commit 37b4bc7f2c0c342b6c70e6757d402aceb0d4a35b diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 96d90fcdd..bfb6e5fdf 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -450,6 +450,14 @@ namespace hex { bool dangerous; }; + struct TypeDefinition { + pl::api::Namespace ns; + std::string name; + + pl::api::FunctionParameterCount parameterCount; + pl::api::TypeCallback callback; + }; + struct Visualizer { pl::api::FunctionParameterCount parameterCount; VisualizerFunctionCallback callback; @@ -459,6 +467,7 @@ namespace hex { const std::map& getInlineVisualizers(); const std::map& getPragmas(); const std::vector& getFunctions(); + const std::vector& getTypes(); } @@ -517,6 +526,20 @@ namespace hex { const pl::api::FunctionCallback &func ); + /** + * @brief Adds a new type to the pattern language + * @param ns The namespace of the type + * @param name The name of the type + * @param parameterCount The amount of non-type template parameters the type takes + * @param func The type callback + */ + void addType( + const pl::api::Namespace &ns, + const std::string &name, + pl::api::FunctionParameterCount parameterCount, + const pl::api::TypeCallback &func + ); + /** * @brief Adds a new visualizer to the pattern language * @note Visualizers are extensions to the [[hex::visualize]] attribute, used to visualize data diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index caffba3a2..867c2cc95 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -552,6 +552,11 @@ namespace hex { return *s_functions; } + static AutoReset> s_types; + const std::vector& getTypes() { + return *s_types; + } + } static std::string getFunctionName(const pl::api::Namespace &ns, const std::string &name) { @@ -605,6 +610,10 @@ namespace hex { runtime.addFunction(ns, name, paramCount, callback); } + for (const auto &[ns, name, paramCount, callback] : impl::getTypes()) { + runtime.addType(ns, name, paramCount, callback); + } + for (const auto &[name, callback] : impl::getPragmas()) { runtime.addPragma(name, callback); } @@ -639,6 +648,15 @@ namespace hex { }); } + void addType(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::TypeCallback &func) { + log::debug("Registered new pattern language type: {}", getFunctionName(ns, name)); + + impl::s_types->push_back({ + ns, name, + parameterCount, func + }); + } + void addVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &function, pl::api::FunctionParameterCount parameterCount) { log::debug("Registered new pattern visualizer function: {}", name);