aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/config/markup.cpp
blob: df072c65290d99a11ee4e0c78434286e160fb81b (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "markup.h"

#include <util/stream/output.h>
#include <util/stream/mem.h>
#include <util/string/strip.h>
#include <util/string/cast.h>

using namespace NConfig;

#define DBG(x)

namespace {
#define MACHINE_DATA
#include "markupfsm.h"
#undef MACHINE_DATA

    class IXmlCB {
    public:
        inline void DoTagOpen(const TStringBuf& key) {
            DBG(Cerr << "topen" << key << Endl);

            S_.push_back(key);
            OnTagOpen(key);
        }

        inline void DoTagClose(const TStringBuf& key) {
            DBG(Cerr << "tclose" << key << Endl);

            if (S_.empty()) {
                ythrow yexception() << "unbalanced tag";
            }

            if (S_.back() != key) {
                ythrow yexception() << "unbalanced tag";
            }

            S_.pop_back();
            OnTagClose();
        }

        inline void DoText(const TStringBuf& text) {
            DBG(Cerr << "ttext" << text << Endl);

            if (!!text) {
                OnText(text);
            }
        }

        inline void DoAttrKey(const TStringBuf& key) {
            DBG(Cerr << "tattrkey" << key << Endl);

            A_ = key;
        }

        inline void DoAttrValue(const TStringBuf& key) {
            DBG(Cerr << "tattrvalue" << key << Endl);

            if (!A_) {
                ythrow yexception() << "dangling attribute";
            }

            OnAttr(A_, key);
            A_ = TStringBuf();
        }

        virtual void OnTagOpen(const TStringBuf& key) = 0;
        virtual void OnTagClose() = 0;
        virtual void OnText(const TStringBuf& text) = 0;
        virtual void OnAttr(const TStringBuf& key, const TStringBuf& value) = 0;
        virtual ~IXmlCB() = default;

    private:
        TVector<TStringBuf> S_;
        TStringBuf A_;
    };

    inline void Parse(TStringBuf s, IXmlCB* cb) {
        const char* p = s.data();
        const char* pe = s.end();
        const char* eof = pe;
        const char* l = p;

        int cs;
        TString cur;

#define MACHINE_INIT
#include "markupfsm.h"
#undef MACHINE_INIT

#define MACHINE_EXEC
#include "markupfsm.h"
#undef MACHINE_EXEC

        if (cs < ParseXml_first_final) {
            ythrow TConfigParseError() << "can not parse markup data at offset " << (p - s.data());
        }
    }

    inline IValue* SmartValue(const TStringBuf& v) {
        try {
            return ConstructValue(FromString<ui64>(v));
        } catch (...) {
        }

        try {
            return ConstructValue(FromString<i64>(v));
        } catch (...) {
        }

        try {
            return ConstructValue(FromString<double>(v));
        } catch (...) {
        }

        try {
            return ConstructValue(FromString<bool>(v));
        } catch (...) {
        }

        return ConstructValue(ToString(v));
    }

    inline TConfig Parse(TStringBuf s0) {
        struct TXmlParser: public IXmlCB {
            inline TXmlParser()
                : Root(ConstructValue(TDict()))
            {
                S.push_back(&Root);
            }

            void OnTagOpen(const TStringBuf& key) override {
                *Push(key) = ConstructValue(TDict());
            }

            void OnTagClose() override {
                S.pop_back();
            }

            static inline bool IsWS(char ch) {
                switch (ch) {
                    case ' ':
                    case '\t':
                    case '\r':
                    case '\n':
                    case ':':
                        return true;
                }

                return false;
            }

            void OnText(const TStringBuf& text) override {
                TMemoryInput mi(text.data(), text.size());
                TString line;

                while (mi.ReadLine(line)) {
                    DBG(Cerr << line << Endl);

                    TStringBuf s = StripString(TStringBuf(line));

                    DBG(Cerr << s << Endl);

                    if (!s) {
                        continue;
                    }

                    const char* b = s.data();
                    const char* c = b;
                    const char* e = s.end();

                    while (c < e && !IsWS(*c)) {
                        ++c;
                    }

                    const TStringBuf key(b, c);

                    while (c < e && IsWS(*c)) {
                        ++c;
                    }

                    const TStringBuf value(c, e);

                    if (!key) {
                        continue;
                    }

                    DBG(Cerr << key << " " << value << Endl);

                    SetAttr(key, value);
                }
            }

            void OnAttr(const TStringBuf& key, const TStringBuf& value) override {
                SetAttr(key, value);
            }

            inline void SetAttr(const TStringBuf& key, const TStringBuf& value) {
                Dict()[ToString(key)] = SmartValue(value);
            }

            inline TConfig* Top() {
                return S.back();
            }

            inline TConfig* Push(const TStringBuf& key) {
                TDict& d = Dict();
                const TString k = ToString(key);

                if (d.find(k) == d.end()) {
                    S.push_back(&d[k]);
                } else {
                    TConfig tmp = d[k];

                    if (tmp.IsA<TArray>()) {
                        TArray& arr = d[k].GetNonConstant<TArray>();

                        arr.push_back(TConfig());

                        S.push_back(&arr.back());
                    } else {
                        d[k] = ConstructValue(TArray());

                        TArray& arr = d[k].GetNonConstant<TArray>();

                        arr.push_back(tmp);
                        arr.push_back(TConfig());

                        S.push_back(&arr.back());
                    }
                }

                return Top();
            }

            inline TDict& Dict() {
                try {
                    return Top()->GetNonConstant<TDict>();
                } catch (...) {
                }

                return Top()->Get<TArray>().back().GetNonConstant<TDict>();
            }

            TConfig Root;
            TVector<TConfig*> S;
        };

        TXmlParser cb;

        Parse(s0, &cb);

        return cb.Root;
    }
}

TConfig NConfig::ParseRawMarkup(IInputStream& in) {
    return Parse(in.ReadAll());
}