diff options
| author | hachem <im@hachem.wtf> | 2025-12-14 16:44:22 +0100 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-12-14 16:44:22 +0100 |
| commit | 0e8a995bc2cb5416228c12add93d10a631b7f952 (patch) | |
| tree | 0c440a2a7c537df5f6bad73d901638d59fdd0470 /src/ui/macro_expander.h | |
| parent | 643287696ea9baccc90a26643b6adb9fabcdf433 (diff) | |
feat: macro expansion
Diffstat (limited to 'src/ui/macro_expander.h')
| -rw-r--r-- | src/ui/macro_expander.h | 76 |
1 files changed, 76 insertions, 0 deletions
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 <cstdint> +#include <filesystem> +#include <string> +#include <string_view> +#include <unordered_map> +#include <vector> + +namespace Hsdbg +{ + enum class PpKind : uint8_t + { + Identifier, + Number, + String, + Char, + Punct, + }; + + struct PpToken + { + PpKind kind = PpKind::Identifier; + std::string text; + std::vector<std::string> hide; + }; + + struct MacroDef + { + std::string name; + bool function_like = false; + bool variadic = false; + std::vector<std::string> params; + std::vector<PpToken> body; + }; + + class MacroTable + { + public: + auto clear() -> void; + + auto build(const std::vector<std::string>& 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<std::string>& lines, + const std::filesystem::path& base_dir, int depth, + std::vector<std::filesystem::path>& seen) -> void; + auto scan_path(const std::filesystem::path& path, int depth, + std::vector<std::filesystem::path>& seen) -> void; + + std::unordered_map<std::string, MacroDef> m_macros; + uint64_t m_revision = 0; + }; + + struct MacroExpansion + { + std::vector<std::string> levels; + + std::vector<std::vector<std::string>> 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; +} |
