hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/ui/macro_expander.h
blob: 3352a536415adadde0e53e41577f6405396a4124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
}