aboutsummaryrefslogtreecommitdiffstats
path: root/tools/enum_parser/parse_enum/parse_enum.h
blob: ef8b512ae407fcdbc7739b164fa5ec61ef082683 (plain) (blame)
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
77
78
#pragma once

#include <util/stream/output.h>
#include <util/stream/input.h>
#include <util/stream/mem.h>
#include <util/string/strip.h>
#include <util/generic/maybe.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>

class TEnumParser {
public:

    struct TItem {
        TMaybe<TString> Value;
        TString CppName;
        TVector<TString> Aliases;
        TString CommentText;

        void Clear() {
            *this = TItem();
        }

        void NormalizeValue() {
            if (!Value)
                return;
            StripInPlace(*Value);
        }

    };

    // vector is to preserve declaration order
    typedef TVector<TItem> TItems;

    typedef TVector<TString> TScope;

    struct TEnum {
        TItems Items;
        TString CppName;
        TScope Scope;
        // enum or enum class
        bool EnumClass = false;
        bool BodyDetected = false;
        bool ForwardDeclaration = false;

        void Clear() {
            *this = TEnum();
        }
    };

    typedef TVector<TEnum> TEnums;

    /// Parse results stored here
    TEnums Enums;

    /// Parse enums from file containing C++ code
    TEnumParser(const TString& fileName);

    /// Parse enums from memory buffer containing C++ code
    TEnumParser(const char* data, size_t length);

    /// Parse enums from input stream
    TEnumParser(IInputStream& in);

    static TString ScopeStr(const TScope& scope) {
        TString result;
        for (const TString& name : scope) {
            result += name;
            result += "::";
        }
        return result;
    }

private:
    void Parse(const char* data, size_t length);
protected:
    TString SourceFileName;
};