aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/lexer_detail.h
blob: be61289cb1bd37ba0512f3166c77347d54b85ffa (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
#pragma once

#include "detail.h"
#include "token.h"

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

    namespace NDetail { 
        /*! \internal */ 
        //////////////////////////////////////////////////////////////////////////////// 

        // EReadStartCase tree representation: 
        // Root                                =     xb 
        //     BinaryStringOrOtherSpecialToken =    x0b 
        //         BinaryString                =    00b 
        //         OtherSpecialToken           =    10b 
        //     Other                           =    x1b 
        //         BinaryScalar                =  xx01b 
        //             BinaryInt64             =  0001b 
        //             BinaryDouble            =  0101b 
        //             BinaryFalse             =  1001b 
        //             BinaryTrue              =  1101b 
        //         Other                       = xxx11b 
        //             Quote                   = 00011b 
        //             DigitOrMinus            = 00111b 
        //             String                  = 01011b 
        //             Space                   = 01111b 
        //             Plus                    = 10011b 
        //             None                    = 10111b 
        //             Percent                 = 11011b 

        enum EReadStartCase : unsigned {
            BinaryString = 0,      // =    00b 
            OtherSpecialToken = 2, // =    10b 

            BinaryInt64 = 1,   // =   001b 
            BinaryDouble = 5,  // =   101b 
            BinaryFalse = 9,   // =  1001b 
            BinaryTrue = 13,   // =  1101b 
            BinaryUint64 = 17, // = 10001b 

            Quote = 3,        // = 00011b 
            DigitOrMinus = 7, // = 00111b 
            String = 11,      // = 01011b 
            Space = 15,       // = 01111b 
            Plus = 19,        // = 10011b 
            None = 23,        // = 10111b 
            Percent = 27      // = 11011b 
        }; 

        template <class TBlockStream, bool EnableLinePositionInfo> 
        class TLexer 
           : public TLexerBase<TBlockStream, EnableLinePositionInfo> { 
        private: 
            using TBase = TLexerBase<TBlockStream, EnableLinePositionInfo>; 

            static EReadStartCase GetStartState(char ch) { 
#define NN EReadStartCase::None
#define BS EReadStartCase::BinaryString
#define BI EReadStartCase::BinaryInt64
#define BD EReadStartCase::BinaryDouble
#define BF EReadStartCase::BinaryFalse
#define BT EReadStartCase::BinaryTrue
#define BU EReadStartCase::BinaryUint64
#define SP NN // EReadStartCase::Space
#define DM EReadStartCase::DigitOrMinus
#define ST EReadStartCase::String
#define PL EReadStartCase::Plus
#define QU EReadStartCase::Quote
#define PC EReadStartCase::Percent
#define TT(name) (EReadStartCase(static_cast<ui8>(ETokenType::name) << 2) | EReadStartCase::OtherSpecialToken)

                static const ui8 lookupTable[] = 
                    { 
                        NN, BS, BI, BD, BF, BT, BU, NN, NN, SP, SP, SP, SP, SP, NN, NN, 
                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, 

                        // 32 
                        SP,                   // ' ' 
                        NN,                   // '!' 
                        QU,                   // '"' 
                        TT(Hash),             // '#' 
                        NN,                   // '$' 
                        PC,                   // '%' 
                        NN,                   // '&' 
                        NN,                   // "'" 
                        TT(LeftParenthesis),  // '(' 
                        TT(RightParenthesis), // ')' 
                        NN,                   // '*' 
                        PL,                   // '+' 
                        TT(Comma),            // ',' 
                        DM,                   // '-' 
                        NN,                   // '.' 
                        NN,                   // '/' 

                        // 48 
                        DM, DM, DM, DM, DM, DM, DM, DM, DM, DM, // '0' - '9' 
                        TT(Colon),                              // ':' 
                        TT(Semicolon),                          // ';' 
                        TT(LeftAngle),                          // '<' 
                        TT(Equals),                             // '=' 
                        TT(RightAngle),                         // '>' 
                        NN,                                     // '?' 

                        // 64 
                        NN,                                                 // '@' 
                        ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, // 'A' - 'M' 
                        ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, // 'N' - 'Z' 
                        TT(LeftBracket),                                    // '[' 
                        NN,                                                 // '\' 
                        TT(RightBracket),                                   // ']' 
                        NN,                                                 // '^' 
                        ST,                                                 // '_' 

                        // 96 
                        NN, // '`' 

                        ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, // 'a' - 'm' 
                        ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, ST, // 'n' - 'z' 
                        TT(LeftBrace),                                      // '{' 
                        NN,                                                 // '|' 
                        TT(RightBrace),                                     // '}' 
                        NN,                                                 // '~' 
                        NN,                                                 // '^?' non-printable 
                        // 128 
                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, 
                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, 
                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, 
                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, 

                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, 
                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, 
                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, 
                        NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN, NN}; 

#undef NN
#undef BS
#undef BI
#undef BD
#undef SP
#undef DM
#undef ST
#undef PL
#undef QU
#undef TT
                return static_cast<EReadStartCase>(lookupTable[static_cast<ui8>(ch)]); 
            } 

        public: 
            TLexer(const TBlockStream& blockStream, TMaybe<ui64> memoryLimit) 
                : TBase(blockStream, memoryLimit) 
            { 
            } 

            void GetToken(TToken* token) { 
                char ch1 = TBase::SkipSpaceAndGetChar(); 
                auto state = GetStartState(ch1); 
                auto stateBits = static_cast<unsigned>(state); 

                if (ch1 == '\0') { 
                    *token = TToken::EndOfStream; 
                    return; 
                } 

                if (stateBits & 1) {          // Other = x1b 
                    if (stateBits & 1 << 1) { // Other = xxx11b 
                        if (state == EReadStartCase::Quote) { 
                            TStringBuf value; 
                            TBase::Advance(1); 
                            TBase::ReadQuotedString(&value); 
                            *token = TToken(value); 
                        } else if (state == EReadStartCase::DigitOrMinus) { 
                            ReadNumeric<true>(token); 
                        } else if (state == EReadStartCase::Plus) { 
                            TBase::Advance(1); 

                            char ch2 = TBase::template GetChar<true>(); 

                            if (!isdigit(ch2)) { 
                                *token = TToken(ETokenType::Plus); 
                            } else { 
                                ReadNumeric<true>(token); 
                            } 
                        } else if (state == EReadStartCase::String) { 
                            TStringBuf value; 
                            TBase::template ReadUnquotedString<true>(&value); 
                            *token = TToken(value); 
                        } else if (state == EReadStartCase::Percent) { 
                            TBase::Advance(1); 
                            char ch3 = TBase::template GetChar<true>(); 
                            if (ch3 == 't' || ch3 == 'f') { 
                                *token = TToken(TBase::template ReadBoolean<true>()); 
                            } else { 
                                *token = TToken(TBase::template ReadNanOrInf<true>()); 
                            } 
                        } else { // None 
                            Y_ASSERT(state == EReadStartCase::None); 
                            ythrow TYsonException() << "Unexpected " << ch1;
                        } 
                    } else { // BinaryScalar = x01b 
                        TBase::Advance(1); 
                        if (state == EReadStartCase::BinaryDouble) { 
                            double value; 
                            TBase::ReadBinaryDouble(&value); 
                            *token = TToken(value); 
                        } else if (state == EReadStartCase::BinaryInt64) { 
                            i64 value; 
                            TBase::ReadBinaryInt64(&value); 
                            *token = TToken(value); 
                        } else if (state == EReadStartCase::BinaryUint64) { 
                            ui64 value; 
                            TBase::ReadBinaryUint64(&value); 
                            *token = TToken(value); 
                        } else if (state == EReadStartCase::BinaryFalse) { 
                            *token = TToken(false); 
                        } else if (state == EReadStartCase::BinaryTrue) { 
                            *token = TToken(true); 
                        } else { 
                            Y_FAIL("unreachable"); 
                        } 
                    }
                } else { // BinaryStringOrOtherSpecialToken = x0b 
                    TBase::Advance(1);
                    if (stateBits & 1 << 1) { // OtherSpecialToken = 10b 
                        Y_ASSERT((stateBits & 3) == static_cast<unsigned>(EReadStartCase::OtherSpecialToken)); 
                        *token = TToken(ETokenType(stateBits >> 2)); 
                    } else { // BinaryString = 00b 
                        Y_ASSERT((stateBits & 3) == static_cast<unsigned>(EReadStartCase::BinaryString)); 
                        TStringBuf value; 
                        TBase::ReadBinaryString(&value); 
                        *token = TToken(value); 
                    }
                }
            }

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

                if (numericResult == ENumericResult::Double) { 
                    try { 
                        *token = TToken(FromString<double>(valueBuffer)); 
                    } catch (yexception&) { 
                        ythrow TYsonException() << "Error parsing double literal " << valueBuffer;
                    } 
                } else if (numericResult == ENumericResult::Int64) { 
                    try { 
                        *token = TToken(FromString<i64>(valueBuffer)); 
                    } catch (yexception&) { 
                        ythrow TYsonException() << "Error parsing int64 literal " << valueBuffer;
                    } 
                } else if (numericResult == ENumericResult::Uint64) { 
                    try { 
                        *token = TToken(FromString<ui64>(valueBuffer.SubStr(0, valueBuffer.size() - 1))); 
                    } catch (yexception&) { 
                        ythrow TYsonException() << "Error parsing uint64 literal " << valueBuffer;
                    } 
                } 
            }
        }; 
        //////////////////////////////////////////////////////////////////////////////// 
        /*! \endinternal */ 
    }

    class TStatelessYsonLexerImplBase { 
    public: 
        virtual size_t GetToken(const TStringBuf& data, TToken* token) = 0; 

        virtual ~TStatelessYsonLexerImplBase() { 
        } 
    }; 

    template <bool EnableLinePositionInfo> 
    class TStatelesYsonLexerImpl: public TStatelessYsonLexerImplBase { 
    private: 
        using TLexer = NDetail::TLexer<TStringReader, EnableLinePositionInfo>; 
        TLexer Lexer; 

    public: 
        TStatelesYsonLexerImpl() 
            : Lexer(TStringReader(), Nothing()) 
        { 
        } 

        size_t GetToken(const TStringBuf& data, TToken* token) override { 
            Lexer.SetBuffer(data.begin(), data.end()); 
            Lexer.GetToken(token); 
            return Lexer.Begin() - data.begin(); 
        } 
    }; 

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

} // namespace NYson