aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/detail/lexer_base.h
blob: 8cf4d45f2a7e02e06f413f8d4589324ab38f09fa (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
#pragma once

#include "byte_reader.h"
#include "cescape.h"
#include "macros.h"
#include "number.h"
#include "percent_scalar.h"
#include "stream_counter.h"
#include "varint.h"

#include <util/generic/maybe.h>
#include <util/generic/vector.h>
#include <util/string/cast.h>

namespace NYsonPull {
    namespace NDetail {
        template <bool EnableLinePositionInfo>
        class lexer_base: public byte_reader<stream_counter<EnableLinePositionInfo>> {
            using Base = byte_reader<
                stream_counter<EnableLinePositionInfo>>;

            TVector<ui8> token_buffer_;
            TMaybe<size_t> memory_limit_;

        public:
            lexer_base(
                NYsonPull::NInput::IStream& buffer,
                TMaybe<size_t> memory_limit)
                : Base(buffer)
                , memory_limit_{memory_limit} {
            }

            ATTRIBUTE(noinline, hot)
            ui8 skip_space_and_get_byte() {
                auto& buf = Base::stream().buffer();
                if (Y_LIKELY(!buf.is_empty())) {
                    auto ch = *buf.pos();
                    if (Y_LIKELY(!is_space(ch))) {
                        return ch;
                    }
                }
                return skip_space_and_get_byte_fallback();
            }

            ATTRIBUTE(hot)
            ui8 get_byte() {
                auto& buf = Base::stream().buffer();
                if (Y_LIKELY(!buf.is_empty())) {
                    return *buf.pos();
                }
                return Base::get_byte();
            }

            number read_numeric() {
                token_buffer_.clear();
                auto type = number_type::int64;
                while (true) {
                    auto ch = this->Base::template get_byte<true>(); 
                    if (isdigit(ch) || ch == '+' || ch == '-') {
                        token_buffer_.push_back(ch);
                    } else if (ch == '.' || ch == 'e' || ch == 'E') {
                        token_buffer_.push_back(ch);
                        type = number_type::float64;
                    } else if (ch == 'u') {
                        token_buffer_.push_back(ch);
                        type = number_type::uint64;
                    } else if (Y_UNLIKELY(isalpha(ch))) {
                        COLD_BLOCK_BYVALUE
                        Base::fail("Unexpected ", NCEscape::quote(ch), " in numeric literal");
                        COLD_BLOCK_END
                    } else {
                        break;
                    }
                    check_memory_limit();
                    Base::advance(1);
                }

                auto str = token_buffer();
                try {
                    switch (type) {
                        case number_type::float64:
                            return FromString<double>(str);
                        case number_type::int64:
                            return FromString<i64>(str);
                        case number_type::uint64:
                            str.Chop(1); // 'u' suffix
                            return FromString<ui64>(str);
                    }
                    Y_UNREACHABLE();
                } catch (const std::exception& err) {
                    Base::fail(err.what());
                }
            }

            TStringBuf read_quoted_string() {
                auto count_trailing_slashes = [](ui8* begin, ui8* end) {
                    auto count = size_t{0};
                    if (begin < end) {
                        for (auto p = end - 1; p >= begin && *p == '\\'; --p) {
                            ++count;
                        }
                    }
                    return count;
                };

                token_buffer_.clear();
                auto& buf = Base::stream().buffer();
                while (true) {
                    this->Base::template fill_buffer<false>(); 
                    auto* quote = reinterpret_cast<const ui8*>(
                        ::memchr(buf.pos(), '"', buf.available()));
                    if (quote == nullptr) {
                        token_buffer_.insert(
                            token_buffer_.end(),
                            buf.pos(),
                            buf.end());
                        Base::advance(buf.available());
                        continue;
                    }

                    token_buffer_.insert(
                        token_buffer_.end(),
                        buf.pos(),
                        quote);
                    Base::advance(quote - buf.pos() + 1); // +1 for the quote itself

                    // We must count the number of '\' at the end of StringValue
                    // to check if it's not \"
                    int slash_count = count_trailing_slashes(
                        token_buffer_.data(),
                        token_buffer_.data() + token_buffer_.size());
                    if (slash_count % 2 == 0) {
                        break;
                    } else {
                        token_buffer_.push_back('"');
                    }
                    check_memory_limit();
                }

                NCEscape::decode_inplace(token_buffer_);
                return token_buffer();
            }

            TStringBuf read_unquoted_string() {
                token_buffer_.clear();
                while (true) {
                    auto ch = this->Base::template get_byte<true>(); 
                    if (isalpha(ch) || isdigit(ch) ||
                        ch == '_' || ch == '-' || ch == '%' || ch == '.') {
                        token_buffer_.push_back(ch);
                    } else {
                        break;
                    }
                    check_memory_limit();
                    Base::advance(1);
                }
                return token_buffer();
            }

            ATTRIBUTE(noinline, hot)
            TStringBuf read_binary_string() {
                auto slength = NVarInt::read<i32>(*this);
                if (Y_UNLIKELY(slength < 0)) {
                    COLD_BLOCK_BYVALUE
                    Base::fail("Negative binary string literal length ", slength);
                    COLD_BLOCK_END
                }
                auto length = static_cast<ui32>(slength);

                auto& buf = Base::stream().buffer();
                if (Y_LIKELY(buf.available() >= length)) {
                    auto result = TStringBuf{
                        reinterpret_cast<const char*>(buf.pos()),
                        length};
                    Base::advance(length);
                    return result;
                } else { // reading in Buffer
                    return read_binary_string_fallback(length);
                }
            }

            ATTRIBUTE(noinline)
            TStringBuf read_binary_string_fallback(size_t length) {
                auto& buf = Base::stream().buffer();
                auto needToRead = length;
                token_buffer_.clear();
                while (needToRead) {
                    this->Base::template fill_buffer<false>(); 
                    auto chunk_size = std::min(needToRead, buf.available());

                    token_buffer_.insert(
                        token_buffer_.end(),
                        buf.pos(),
                        buf.pos() + chunk_size);
                    check_memory_limit();
                    needToRead -= chunk_size;
                    Base::advance(chunk_size);
                }
                return token_buffer();
            }

            percent_scalar read_percent_scalar() {
                auto throw_incorrect_percent_scalar = [&]() {
                    Base::fail("Incorrect %-literal prefix ", NCEscape::quote(token_buffer()));
                };

                auto assert_literal = [&](TStringBuf literal) -> void {
                    for (size_t i = 2; i < literal.size(); ++i) {
                        token_buffer_.push_back(this->Base::template get_byte<false>()); 
                        Base::advance(1);
                        if (Y_UNLIKELY(token_buffer_.back() != literal[i])) {
                            throw_incorrect_percent_scalar();
                        }
                    }
                };

                token_buffer_.clear();
                token_buffer_.push_back(this->Base::template get_byte<false>()); 
                Base::advance(1);

                switch (token_buffer_[0]) {
                    case 't':
                        assert_literal(percent_scalar::true_literal);
                        return percent_scalar(true);
                    case 'f':
                        assert_literal(percent_scalar::false_literal);
                        return percent_scalar(false);
                    case 'n':
                        assert_literal(percent_scalar::nan_literal);
                        return percent_scalar(std::numeric_limits<double>::quiet_NaN());
                    case 'i':
                        assert_literal(percent_scalar::positive_inf_literal);
                        return percent_scalar(std::numeric_limits<double>::infinity());
                    case '-':
                        assert_literal(percent_scalar::negative_inf_literal);
                        return percent_scalar(-std::numeric_limits<double>::infinity());
                    default:
                        throw_incorrect_percent_scalar();
                }

                Y_UNREACHABLE();
            }

            i64 read_binary_int64() {
                return NVarInt::read<i64>(*this);
            }

            ui64 read_binary_uint64() {
                return NVarInt::read<ui64>(*this);
            }

            double read_binary_double() {
                union {
                    double as_double;
                    ui8 as_bytes[sizeof(double)];
                } data;
                static_assert(sizeof(data) == sizeof(double), "bad union size");

                auto needToRead = sizeof(double);

                auto& buf = Base::stream().buffer();
                while (needToRead != 0) {
                    Base::fill_buffer();

                    auto chunk_size = std::min(needToRead, buf.available());
                    if (chunk_size == 0) {
                        Base::fail("Error parsing binary double literal");
                    }
                    std::copy(
                        buf.pos(),
                        buf.pos() + chunk_size,
                        data.as_bytes + (sizeof(double) - needToRead));
                    needToRead -= chunk_size;
                    Base::advance(chunk_size);
                }
                return data.as_double;
            }

        private:
            static bool is_space(ui8 ch) {
                static const ui8 lookupTable[] =
                    {
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                return lookupTable[ch];
            }

            ATTRIBUTE(noinline, cold)
            ui8 skip_space_and_get_byte_fallback() {
                auto& buf = Base::stream().buffer();
                while (true) {
                    // FIXME
                    if (buf.is_empty()) {
                        if (Base::stream().at_end()) {
                            return '\0';
                        }
                        Base::fill_buffer();
                    } else {
                        if (!is_space(*buf.pos())) {
                            break;
                        }
                        Base::advance(1);
                    }
                }
                return Base::get_byte();
            }

            void check_memory_limit() {
                if (Y_UNLIKELY(memory_limit_ && token_buffer_.capacity() > *memory_limit_)) {
                    COLD_BLOCK_BYVALUE
                    Base::fail(
                        "Memory limit exceeded while parsing YSON stream: "
                        "allocated ",
                        token_buffer_.capacity(),
                        ", limit ", *memory_limit_);
                    COLD_BLOCK_END
                }
            }

            TStringBuf token_buffer() const {
                auto* begin = reinterpret_cast<const char*>(token_buffer_.data());
                return {begin, token_buffer_.size()};
            }
        };
    }
}