aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/fast_sax/parser.rl6
blob: 504f51d215c68c1c310d90e73278ae2ff23b2ba6 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <library/cpp/json/fast_sax/unescape.h>
#include <library/cpp/json/fast_sax/parser.h>

#include <util/string/cast.h>
#include <util/generic/buffer.h>
#include <util/generic/strbuf.h>
#include <util/generic/ymath.h>

namespace NJson {

enum EStoredStr {
    SS_NONE = 0, SS_NOCOPY, SS_MUSTCOPY
};

struct TParserCtx {
    TJsonCallbacks& Hndl;

    TBuffer Buffer;
    TStringBuf String;
    EStoredStr Stored = SS_NONE;
    bool ExpectValue = true;

    const char* p0 = nullptr;
    const char* p = nullptr;
    const char* pe = nullptr;
    const char* eof = nullptr;
    const char* ts = nullptr;
    const char* te = nullptr;
    int cs = 0;
    int act = 0;

    TParserCtx(TJsonCallbacks& h, TStringBuf data)
        : Hndl(h)
        , p0(data.data())
        , p(data.data())
        , pe(data.end())
        , eof(data.end())
    {}

    static inline bool GoodPtrs(const char* b, const char* e) {
        return b && e && b <= e;
    }

    bool OnError(TStringBuf reason = TStringBuf(""), bool end = false) const {
        size_t off = 0;
        TStringBuf token;

        if (GoodPtrs(p0, ts)) {
            off = ts - p0;
        } else if (end && GoodPtrs(p0, pe)) {
            off = pe - p0;
        }

        if (GoodPtrs(ts, te)) {
            token = TStringBuf(ts, te);
        }

        if (!token) {
            Hndl.OnError(off, reason);
        } else {
            Hndl.OnError(off, TString::Join(reason, " at token: '", token, "'"));
        }

        return false;
    }

    bool OnVal() {
        if (Y_UNLIKELY(!ExpectValue)) {
            return false;
        }
        ExpectValue = false;
        return true;
    }

    bool OnNull() {
        return Y_LIKELY(OnVal())
               && Hndl.OnNull();
    }

    bool OnTrue() {
        return Y_LIKELY(OnVal())
               && Hndl.OnBoolean(true);
    }

    bool OnFalse() {
        return Y_LIKELY(OnVal())
               && Hndl.OnBoolean(false);
    }

    bool OnPInt() {
        unsigned long long res = 0;
        return Y_LIKELY(OnVal())
               && TryFromString<unsigned long long>(TStringBuf(ts, te), res)
               && Hndl.OnUInteger(res);
    }

    bool OnNInt() {
        long long res = 0;
        return Y_LIKELY(OnVal())
               && TryFromString<long long>(TStringBuf(ts, te), res)
               && Hndl.OnInteger(res);
    }

    bool OnFlt() {
        double res = 0;
        return Y_LIKELY(OnVal())
               && TryFromString<double>(TStringBuf(ts, te), res)
               && IsFinite(res)
               && Hndl.OnDouble(res);
    }

    bool OnMapOpen() {
        bool res = Y_LIKELY(OnVal())
                   && Hndl.OnOpenMap();
        ExpectValue = true;
        return res;
    }

    bool OnArrOpen() {
        bool res = Y_LIKELY(OnVal())
                   && Hndl.OnOpenArray();
        ExpectValue = true;
        return res;
    }

    bool OnString(TStringBuf s, EStoredStr t) {
        if (Y_LIKELY(OnVal())) {
            String = s;
            Stored = t;
            return true;
        } else {
            return false;
        }
    }

    bool OnStrU() {
        return OnString(TStringBuf(ts, te), SS_NOCOPY);
    }

    bool OnStrQ() {
        return OnString(TStringBuf(ts + 1, te - 1), SS_NOCOPY);
    }

    bool OnStrE() {
        Buffer.Clear();
        Buffer.Reserve(2 * (te - ts));

        return OnString(UnescapeJsonUnicode(TStringBuf(ts + 1, te - ts - 2), Buffer.data()), SS_MUSTCOPY);
    }

    bool OnMapClose() {
        ExpectValue = false;
        return Y_LIKELY(OnAfterVal())
               && Hndl.OnCloseMap();
    }

    bool OnArrClose() {
        ExpectValue = false;
        return Y_LIKELY(OnAfterVal())
               && Hndl.OnCloseArray();
    }

    bool OnColon() {
        if (ExpectValue) {
            return false;
        }

        ExpectValue = true;
        const auto stored = Stored;
        Stored = SS_NONE;

        switch (stored) {
        default:
            return false;
        case SS_NOCOPY:
            return Hndl.OnMapKeyNoCopy(String);
        case SS_MUSTCOPY:
            return Hndl.OnMapKey(String);
        }
    }

    bool OnAfterVal() {
        const auto stored = Stored;
        Stored = SS_NONE;

        switch (stored) {
        default:
            return true;
        case SS_NOCOPY:
            return Hndl.OnStringNoCopy(String);
        case SS_MUSTCOPY:
            return Hndl.OnString(String);
        }
    }

    bool OnComma() {
        if (Y_UNLIKELY(ExpectValue)) {
            return false;
        }
        ExpectValue = true;
        return OnAfterVal();
    }

    bool Parse();
};

#if 0
%%{
machine fastjson;

alphtype char;

action OnNull  { if (Y_UNLIKELY(!OnNull()))  goto TOKEN_ERROR; }
action OnTrue  { if (Y_UNLIKELY(!OnTrue()))  goto TOKEN_ERROR; }
action OnFalse { if (Y_UNLIKELY(!OnFalse())) goto TOKEN_ERROR; }
action OnPInt  { if (Y_UNLIKELY(!OnPInt()))  goto TOKEN_ERROR; }
action OnNInt  { if (Y_UNLIKELY(!OnNInt()))  goto TOKEN_ERROR; }
action OnFlt   { if (Y_UNLIKELY(!OnFlt()))   goto TOKEN_ERROR; }
action OnStrU  { if (Y_UNLIKELY(!OnStrU()))  goto TOKEN_ERROR; }
action OnStrQ  { if (Y_UNLIKELY(!OnStrQ()))  goto TOKEN_ERROR; }
action OnStrE  { if (Y_UNLIKELY(!OnStrE()))  goto TOKEN_ERROR; }
action OnDictO { if (Y_UNLIKELY(!OnMapOpen()))  goto TOKEN_ERROR; }
action OnDictC { if (Y_UNLIKELY(!OnMapClose())) goto TOKEN_ERROR; }
action OnArrO  { if (Y_UNLIKELY(!OnArrOpen()))  goto TOKEN_ERROR; }
action OnArrC  { if (Y_UNLIKELY(!OnArrClose())) goto TOKEN_ERROR; }
action OnComma { if (Y_UNLIKELY(!OnComma())) goto TOKEN_ERROR; }
action OnColon { if (Y_UNLIKELY(!OnColon())) goto TOKEN_ERROR; }
action OnError { goto TOKEN_ERROR; }

comment1 = "/*" (any* -- "*/") "*/";

pint = [0-9]+;
nint = '-'[0-9]+;
flt  = '-'?[0-9.][0-9.eE+\-]+;

uchar0 = [a-zA-Z_@$] | (0x80 .. 0xFF);
uchar  = uchar0 | digit | [.\-];

qchar = [^'\\]; #';
dchar = [^"\\]; #";

echar = "\\" any;

qechar = qchar | echar;
dechar = dchar | echar;

strq = "'" qchar* "'";
strd = '"' dchar* '"';

strqe = "'" qechar* "'";
strde = '"' dechar* '"';

strU = uchar0 uchar*;
strQ = strq | strd;
strE = strqe | strde;

ws = (0x00 .. 0x20) | 0x7F;
sp = ws+;

main := |*
    'null'  => OnNull;
    'true'  => OnTrue;
    'false' => OnFalse;

    pint => OnPInt;
    nint => OnNInt;
    flt  => OnFlt;

    strU => OnStrU;
    strQ => OnStrQ;
    strE => OnStrE;

    ',' => OnComma;
    ':' => OnColon;

    '{' => OnDictO;
    '}' => OnDictC;
    '[' => OnArrO;
    ']' => OnArrC;

    sp;
    comment1;

    (flt | pint | nint) (any - (ws | ',' | ':' | '{' | '}' | '[' | ']')) => OnError;

    any => OnError;
         *|;
}%%
#endif

bool TParserCtx::Parse() {
    try {
        %%{
            write data noerror nofinal;
            write init;
            write exec;
        }%%
        Y_UNUSED(fastjson_en_main);
    } catch (const TFromStringException& e) {
        return OnError(e.what());
    }

    return OnAfterVal() && Hndl.OnEnd() || OnError("invalid or truncated", true);

    TOKEN_ERROR:
    return OnError("invalid syntax");
}

bool ReadJsonFast(TStringBuf data, TJsonCallbacks* h) {
    return TParserCtx(*h, data).Parse();
}

}