From 0e8a995bc2cb5416228c12add93d10a631b7f952 Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 14 Dec 2025 16:44:22 +0100 Subject: feat: macro expansion --- src/ui/macro_expander.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/ui/macro_expander.h (limited to 'src/ui/macro_expander.h') diff --git a/src/ui/macro_expander.h b/src/ui/macro_expander.h new file mode 100644 index 0000000..3352a53 --- /dev/null +++ b/src/ui/macro_expander.h @@ -0,0 +1,76 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Hsdbg +{ + enum class PpKind : uint8_t + { + Identifier, + Number, + String, + Char, + Punct, + }; + + struct PpToken + { + PpKind kind = PpKind::Identifier; + std::string text; + std::vector hide; + }; + + struct MacroDef + { + std::string name; + bool function_like = false; + bool variadic = false; + std::vector params; + std::vector body; + }; + + class MacroTable + { + public: + auto clear() -> void; + + auto build(const std::vector& lines, + const std::filesystem::path& base_dir) -> void; + + auto find(std::string_view name) const -> const MacroDef*; + + auto empty() const -> bool { return m_macros.empty(); } + auto size() const -> size_t { return m_macros.size(); } + + auto revision() const -> uint64_t { return m_revision; } + + private: + auto scan_lines(const std::vector& lines, + const std::filesystem::path& base_dir, int depth, + std::vector& seen) -> void; + auto scan_path(const std::filesystem::path& path, int depth, + std::vector& seen) -> void; + + std::unordered_map m_macros; + uint64_t m_revision = 0; + }; + + struct MacroExpansion + { + std::vector levels; + + std::vector> expanded; + + auto fully_expanded() const -> bool { return m_reached_fixpoint; } + + bool m_reached_fixpoint = true; + }; + + auto expand_stages(const MacroTable& table, std::string_view input, + int max_levels = 64) -> MacroExpansion; +} -- cgit v1.3