aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/parser_detail.h
blob: fb72affed559ab4d7347bd01b1940ff3426650f5 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#pragma once

#include "detail.h"

namespace NYson {
    namespace NDetail {
        ////////////////////////////////////////////////////////////////////////////////

        template <class TConsumer, class TBlockStream, bool EnableLinePositionInfo>
        class TParser
           : public TLexerBase<TBlockStream, EnableLinePositionInfo> {
        private:
            using TBase = TLexerBase<TBlockStream, EnableLinePositionInfo>;
            TConsumer* Consumer;
            bool ConsumeUntilEof_;

        public:
            TParser(
                const TBlockStream& blockStream,
                TConsumer* consumer,
                bool consumeUntilEof,
                TMaybe<ui64> memoryLimit)
                    : TBase(blockStream, memoryLimit)
                    , Consumer(consumer)
                    , ConsumeUntilEof_(consumeUntilEof)
            {
            }

            void DoParse(EYsonType ysonType) {
                switch (ysonType) {
                    case ::NYson::EYsonType::Node:
                        ParseNode<true>();
                        break;

                    case ::NYson::EYsonType::ListFragment:
                        ParseListFragment<true>(EndSymbol);
                        break;

                    case ::NYson::EYsonType::MapFragment:
                        ParseMapFragment<true>(EndSymbol);
                        break;

                    default:
                        Y_ABORT("unreachable");
                }

                if (ConsumeUntilEof_) {
                    while (!(TBase::IsFinished() && TBase::IsEmpty())) {
                        if (TBase::template SkipSpaceAndGetChar<true>() != EndSymbol) {
                            ythrow TYsonException() << "Stray '" << (*TBase::Begin()) << "' found";
                        } else if (!TBase::IsEmpty()) {
                            TBase::Advance(1);
                        }
                    }
                }
            }

            bool DoParseListFragment(bool first) {
                bool ret = first ? first : ParseListSeparator<true>(EndSymbol);
                return ret && ParseListItem<true>(EndSymbol);
            }

            void ParseAttributes() {
                Consumer->OnBeginAttributes();
                ParseMapFragment(EndAttributesSymbol);
                TBase::SkipCharToken(EndAttributesSymbol);
                Consumer->OnEndAttributes();
            }

            void ParseMap() {
                Consumer->OnBeginMap();
                ParseMapFragment(EndMapSymbol);
                TBase::SkipCharToken(EndMapSymbol);
                Consumer->OnEndMap();
            }

            void ParseList() {
                Consumer->OnBeginList();
                ParseListFragment(EndListSymbol);
                TBase::SkipCharToken(EndListSymbol);
                Consumer->OnEndList();
            }

            template <bool AllowFinish>
            void ParseNode() {
                return ParseNode<AllowFinish>(TBase::SkipSpaceAndGetChar());
            }

            template <bool AllowFinish>
            void ParseNode(char ch) {
                if (ch == BeginAttributesSymbol) {
                    TBase::Advance(1);
                    ParseAttributes();
                    ch = TBase::SkipSpaceAndGetChar();
                }

                switch (ch) {
                    case BeginMapSymbol:
                        TBase::Advance(1);
                        ParseMap();
                        break;

                    case BeginListSymbol:
                        TBase::Advance(1);
                        ParseList();
                        break;

                    case '"': {
                        TBase::Advance(1);
                        TStringBuf value;
                        TBase::ReadQuotedString(&value);
                        Consumer->OnStringScalar(value);
                        break;
                    }
                    case StringMarker: {
                        TBase::Advance(1);
                        TStringBuf value;
                        TBase::ReadBinaryString(&value);
                        Consumer->OnStringScalar(value);
                        break;
                    }
                    case Int64Marker: {
                        TBase::Advance(1);
                        i64 value;
                        TBase::ReadBinaryInt64(&value);
                        Consumer->OnInt64Scalar(value);
                        break;
                    }
                    case Uint64Marker: {
                        TBase::Advance(1);
                        ui64 value;
                        TBase::ReadBinaryUint64(&value);
                        Consumer->OnUint64Scalar(value);
                        break;
                    }
                    case DoubleMarker: {
                        TBase::Advance(1);
                        double value;
                        TBase::ReadBinaryDouble(&value);
                        Consumer->OnDoubleScalar(value);
                        break;
                    }
                    case FalseMarker: {
                        TBase::Advance(1);
                        Consumer->OnBooleanScalar(false);
                        break;
                    }
                    case TrueMarker: {
                        TBase::Advance(1);
                        Consumer->OnBooleanScalar(true);
                        break;
                    }
                    case EntitySymbol:
                        TBase::Advance(1);
                        Consumer->OnEntity();
                        break;

                    default: {
                        if (isdigit((unsigned char)ch) || ch == '-' || ch == '+') { // case of '+' is handled in AfterPlus state
                            ReadNumeric<AllowFinish>();
                        } else if (isalpha((unsigned char)ch) || ch == '_') {
                            TStringBuf value;
                            TBase::template ReadUnquotedString<AllowFinish>(&value);
                            Consumer->OnStringScalar(value);
                        } else if (ch == '%') {
                            TBase::Advance(1);
                            ch = TBase::template GetChar<AllowFinish>();
                            if (ch == 't' || ch == 'f') {
                                Consumer->OnBooleanScalar(TBase::template ReadBoolean<AllowFinish>());
                            } else {
                                Consumer->OnDoubleScalar(TBase::template ReadNanOrInf<AllowFinish>());
                            }
                        } else {
                            ythrow TYsonException() << "Unexpected '" << ch << "' while parsing node";
                        }
                    }
                }
            }

            void ParseKey() {
                return ParseKey(TBase::SkipSpaceAndGetChar());
            }

            void ParseKey(char ch) {
                switch (ch) {
                    case '"': {
                        TBase::Advance(1);
                        TStringBuf value;
                        TBase::ReadQuotedString(&value);
                        Consumer->OnKeyedItem(value);
                        break;
                    }
                    case StringMarker: {
                        TBase::Advance(1);
                        TStringBuf value;
                        TBase::ReadBinaryString(&value);
                        Consumer->OnKeyedItem(value);
                        break;
                    }
                    default: {
                        if (isalpha(ch) || ch == '_') {
                            TStringBuf value;
                            TBase::ReadUnquotedString(&value);
                            Consumer->OnKeyedItem(value);
                        } else {
                            ythrow TYsonException() << "Unexpected '" << ch << "' while parsing key";
                        }
                    }
                }
            }

            template <bool AllowFinish>
            void ParseMapFragment(char endSymbol) {
                char ch = TBase::template SkipSpaceAndGetChar<AllowFinish>();
                while (ch != endSymbol) {
                    ParseKey(ch);
                    ch = TBase::template SkipSpaceAndGetChar<AllowFinish>();
                    if (ch == KeyValueSeparatorSymbol) {
                        TBase::Advance(1);
                    } else {
                        ythrow TYsonException() << "Expected '" << KeyValueSeparatorSymbol << "' but '" << ch << "' found";
                    }
                    ParseNode<AllowFinish>();
                    ch = TBase::template SkipSpaceAndGetChar<AllowFinish>();
                    if (ch == KeyedItemSeparatorSymbol) {
                        TBase::Advance(1);
                        ch = TBase::template SkipSpaceAndGetChar<AllowFinish>();
                    } else if (ch != endSymbol) {
                        ythrow TYsonException() << "Expected '" << KeyedItemSeparatorSymbol
                                                << "' or '\\0' ' but '" << ch << "' found";
                    }
                }
            }

            void ParseMapFragment(char endSymbol) {
                ParseMapFragment<false>(endSymbol);
            }

            template <bool AllowFinish>
            bool ParseListItem(char endSymbol) {
                char ch = TBase::template SkipSpaceAndGetChar<AllowFinish>();
                if (ch != endSymbol) {
                    Consumer->OnListItem();
                    ParseNode<AllowFinish>(ch);
                    return true;
                }
                return false;
            }

            template <bool AllowFinish>
            bool ParseListSeparator(char endSymbol) {
                char ch = TBase::template SkipSpaceAndGetChar<AllowFinish>();
                if (ch == ListItemSeparatorSymbol) {
                    TBase::Advance(1);
                    return true;
                } else if (ch != endSymbol) {
                    ythrow TYsonException() << "Expected '" << ListItemSeparatorSymbol
                                            << "' or '\\0' but '" << ch << "' found";
                }
                return false;
            }

            template <bool AllowFinish>
            void ParseListFragment(char endSymbol) {
                while (ParseListItem<AllowFinish>(endSymbol) && ParseListSeparator<AllowFinish>(endSymbol)) {
                }
            }

            void ParseListFragment(char endSymbol) {
                ParseListFragment<false>(endSymbol);
            }

            template <bool AllowFinish>
            void ReadNumeric() {
                TStringBuf valueBuffer;
                ENumericResult numericResult = TBase::template ReadNumeric<AllowFinish>(&valueBuffer);

                if (numericResult == ENumericResult::Double) {
                    double value;
                    try {
                        value = FromString<double>(valueBuffer);
                    } catch (yexception& e) {
                        // This exception is wrapped in parser.
                        ythrow TYsonException() << "Failed to parse double literal '" << valueBuffer << "'" << e;
                    }
                    Consumer->OnDoubleScalar(value);
                } else if (numericResult == ENumericResult::Int64) {
                    i64 value;
                    try {
                        value = FromString<i64>(valueBuffer);
                    } catch (yexception& e) {
                        // This exception is wrapped in parser.
                        ythrow TYsonException() << "Failed to parse int64 literal '" << valueBuffer << "'" << e;
                    }
                    Consumer->OnInt64Scalar(value);
                } else if (numericResult == ENumericResult::Uint64) {
                    ui64 value;
                    try {
                        value = FromString<ui64>(valueBuffer.SubStr(0, valueBuffer.size() - 1));
                    } catch (yexception& e) {
                        // This exception is wrapped in parser.
                        ythrow TYsonException() << "Failed to parse uint64 literal '" << valueBuffer << "'" << e;
                    }
                    Consumer->OnUint64Scalar(value);
                }
            }
        };

        ////////////////////////////////////////////////////////////////////////////////

    }

    template <class TConsumer, class TBlockStream>
    void ParseYsonStreamImpl(
        const TBlockStream& blockStream,
        NYT::NYson::IYsonConsumer* consumer,
        EYsonType parsingMode,
        bool enableLinePositionInfo,
        bool consumeUntilEof,
        TMaybe<ui64> memoryLimit) {
        if (enableLinePositionInfo) {
            using TImpl = NDetail::TParser<TConsumer, TBlockStream, true>;
            TImpl impl(blockStream, consumer, consumeUntilEof, memoryLimit);
            impl.DoParse(parsingMode);
        } else {
            using TImpl = NDetail::TParser<TConsumer, TBlockStream, false>;
            TImpl impl(blockStream, consumer, consumeUntilEof, memoryLimit);
            impl.DoParse(parsingMode);
        }
    }

    class TStatelessYsonParserImplBase {
    public:
        virtual void Parse(const TStringBuf& data, EYsonType type = ::NYson::EYsonType::Node) = 0;

        virtual ~TStatelessYsonParserImplBase() {
        }
    };

    template <class TConsumer, bool EnableLinePositionInfo>
    class TStatelessYsonParserImpl
       : public TStatelessYsonParserImplBase {
    private:
        using TParser = NDetail::TParser<TConsumer, TStringReader, EnableLinePositionInfo>;
        TParser Parser;

    public:
        TStatelessYsonParserImpl(TConsumer* consumer, TMaybe<ui64> memoryLimit)
            : Parser(TStringReader(), consumer, true, memoryLimit)
        {
        }

        void Parse(const TStringBuf& data, EYsonType type = ::NYson::EYsonType::Node) override {
            Parser.SetBuffer(data.begin(), data.end());
            Parser.DoParse(type);
        }
    };

    class TYsonListParserImplBase {
    public:
        virtual bool Parse() = 0;

        virtual ~TYsonListParserImplBase() {
        }
    };

    template <class TConsumer, class TBlockStream, bool EnableLinePositionInfo>
    class TYsonListParserImpl
       : public TYsonListParserImplBase {
    private:
        using TParser = NDetail::TParser<TConsumer, TBlockStream, EnableLinePositionInfo>;
        TParser Parser;
        bool First = true;

    public:
        TYsonListParserImpl(const TBlockStream& blockStream, TConsumer* consumer, TMaybe<ui64> memoryLimit)
            : Parser(blockStream, consumer, true, memoryLimit)
        {
        }

        bool Parse() override {
            bool ret = Parser.DoParseListFragment(First);
            First = false;
            return ret;
        }
    };

    ////////////////////////////////////////////////////////////////////////////////

} // namespace NYson