summaryrefslogtreecommitdiffstats
path: root/src/data_parser.hh
blob: ca54a586372df7ba008d4b6c9c19ce44dd9d0322 (plain)
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
 * Copyright (c) 2007-2012, Timothy Stack
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither the name of Timothy Stack nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef data_parser_hh
#define data_parser_hh

#include <iterator>
#include <list>
#include <stack>
#include <vector>

#include <stdio.h>

#include "base/lnav_log.hh"
#include "byte_array.hh"
#include "data_scanner.hh"

#define ELEMENT_LIST_T(var) var("" #var, __FILE__, __LINE__, group_depth)
#define PUSH_FRONT(elem)    push_front(elem, __FILE__, __LINE__)
#define PUSH_BACK(elem)     push_back(elem, __FILE__, __LINE__)
#define POP_FRONT(elem)     pop_front(__FILE__, __LINE__)
#define POP_BACK(elem)      pop_back(__FILE__, __LINE__)
#define CLEAR(elem)         clear2(__FILE__, __LINE__)
#define SWAP(other)         swap(other, __FILE__, __LINE__)
#define SPLICE(pos, other, first, last) \
    splice(pos, other, first, last, __FILE__, __LINE__)

template<class Container, class UnaryPredicate>
void
strip(Container& container, UnaryPredicate p)
{
    while (!container.empty() && p(container.front())) {
        container.POP_FRONT();
    }
    while (!container.empty() && p(container.back())) {
        container.POP_BACK();
    }
}

enum data_format_state_t {
    DFS_ERROR = -1,
    DFS_INIT,
    DFS_KEY,
    DFS_EXPECTING_SEP,
    DFS_VALUE,
};

struct data_format {
    data_format(const char* name = nullptr,
                data_token_t appender = DT_INVALID,
                data_token_t terminator = DT_INVALID) noexcept
        : df_name(name), df_appender(appender), df_terminator(terminator),
          df_qualifier(DT_INVALID), df_separator(DT_COLON),
          df_prefix_terminator(DT_INVALID)
    {
    }

    const char* df_name;
    data_token_t df_appender;
    data_token_t df_terminator;
    data_token_t df_qualifier;
    data_token_t df_separator;
    data_token_t df_prefix_terminator;
};

data_format_state_t dfs_prefix_next(data_format_state_t state,
                                    data_token_t next_token);
data_format_state_t dfs_semi_next(data_format_state_t state,
                                  data_token_t next_token);
data_format_state_t dfs_comma_next(data_format_state_t state,
                                   data_token_t next_token);

#define LIST_INIT_TRACE \
    do { \
        if (TRACE_FILE != NULL) { \
            fprintf(TRACE_FILE, \
                    "%p %s:%d %s %s %d\n", \
                    this, \
                    fn, \
                    line, \
                    __func__, \
                    varname, \
                    group_depth); \
        } \
    } while (false)

#define LIST_DEINIT_TRACE \
    do { \
        if (TRACE_FILE != NULL) { \
            fprintf(TRACE_FILE, "%p %s:%d %s\n", this, fn, line, __func__); \
        } \
    } while (false)

#define ELEMENT_TRACE \
    do { \
        if (TRACE_FILE != NULL) { \
            fprintf(TRACE_FILE, \
                    "%p %s:%d %s %s %d:%d\n", \
                    this, \
                    fn, \
                    line, \
                    __func__, \
                    data_scanner::token2name(elem.e_token), \
                    elem.e_capture.c_begin, \
                    elem.e_capture.c_end); \
        } \
    } while (false)

#define LIST_TRACE \
    do { \
        if (TRACE_FILE != NULL) { \
            fprintf(TRACE_FILE, "%p %s:%d %s\n", this, fn, line, __func__); \
        } \
    } while (false)

#define SPLICE_TRACE \
    do { \
        if (TRACE_FILE != NULL) { \
            fprintf(TRACE_FILE, \
                    "%p %s:%d %s %d %p %d:%d\n", \
                    this, \
                    fn, \
                    line, \
                    __func__, \
                    (int) std::distance(this->begin(), pos), \
                    &other, \
                    (int) std::distance(other.begin(), first), \
                    (int) std::distance(last, other.end())); \
        } \
    } while (false);

#define SWAP_TRACE(other) \
    do { \
        if (TRACE_FILE != NULL) { \
            fprintf(TRACE_FILE, \
                    "%p %s:%d %s %p\n", \
                    this, \
                    fn, \
                    line, \
                    __func__, \
                    &other); \
        } \
    } while (false);

#define POINT_TRACE(name) \
    do { \
        if (TRACE_FILE) { \
            fprintf( \
                TRACE_FILE, "0x0 %s:%d point %s\n", __FILE__, __LINE__, name); \
        } \
    } while (false);

#define FORMAT_TRACE(elist) \
    do { \
        if (TRACE_FILE) { \
            const data_format& df = elist.el_format; \
            fprintf(TRACE_FILE, \
                    "%p %s:%d format %d %s %s %s %s %s\n", \
                    &elist, \
                    __FILE__, \
                    __LINE__, \
                    group_depth, \
                    data_scanner::token2name(df.df_appender), \
                    data_scanner::token2name(df.df_terminator), \
                    data_scanner::token2name(df.df_qualifier), \
                    data_scanner::token2name(df.df_separator), \
                    data_scanner::token2name(df.df_prefix_terminator)); \
        } \
    } while (false);

#define CONSUMED_TRACE(elist) \
    do { \
        if (TRACE_FILE) { \
            fprintf(TRACE_FILE, \
                    "%p %s:%d consumed\n", \
                    &elist, \
                    __FILE__, \
                    __LINE__); \
        } \
    } while (false);

class data_parser {
public:
    static data_format FORMAT_SEMI;
    static data_format FORMAT_COMMA;
    static data_format FORMAT_PLAIN;

    static FILE* TRACE_FILE;

    typedef byte_array<2, uint64_t> schema_id_t;

    struct element;
    /* typedef std::list<element> element_list_t; */

    class element_list_t : public std::list<element> {
    public:
        element_list_t(const char* varname,
                       const char* fn,
                       int line,
                       int group_depth = -1)
        {
            LIST_INIT_TRACE;
        }

        element_list_t()
        {
            const char* varname = "_anon2_";
            const char* fn = __FILE__;
            int line = __LINE__;
            int group_depth = -1;

            LIST_INIT_TRACE;
        }

        element_list_t(const element_list_t& other) : std::list<element>(other)
        {
            this->el_format = other.el_format;
        }

        ~element_list_t()
        {
            const char* fn = __FILE__;
            int line = __LINE__;

            LIST_DEINIT_TRACE;
        }

        void push_front(const element& elem, const char* fn, int line)
        {
            ELEMENT_TRACE;

            require(elem.e_capture.c_end >= -1);
            this->std::list<element>::push_front(elem);
        }

        void push_back(const element& elem, const char* fn, int line)
        {
            ELEMENT_TRACE;

            require(elem.e_capture.c_end >= -1);
            this->std::list<element>::push_back(elem);
        }

        void pop_front(const char* fn, int line)
        {
            LIST_TRACE;

            this->std::list<element>::pop_front();
        }

        void pop_back(const char* fn, int line)
        {
            LIST_TRACE;

            this->std::list<element>::pop_back();
        }

        void clear2(const char* fn, int line)
        {
            LIST_TRACE;

            this->std::list<element>::clear();
        }

        void swap(element_list_t& other, const char* fn, int line)
        {
            SWAP_TRACE(other);

            this->std::list<element>::swap(other);
        }

        void splice(iterator pos,
                    element_list_t& other,
                    iterator first,
                    iterator last,
                    const char* fn,
                    int line)
        {
            SPLICE_TRACE;

            this->std::list<element>::splice(pos, other, first, last);
        }

        data_format el_format;
    };

    struct element {
        element();

        element(element_list_t& subs,
                data_token_t token,
                bool assign_subs_elements = true);

        element(const element& other);

        ~element();

        element& operator=(const element& other);

        void assign_elements(element_list_t& subs);

        void update_capture();

        const element& get_pair_value() const;

        data_token_t value_token() const;

        const element& get_value_elem() const;

        const element& get_pair_elem() const;

        void print(FILE* out, data_scanner&, int offset = 0) const;

        data_scanner::capture_t e_capture;
        data_token_t e_token;

        element_list_t* e_sub_elements;
    };

    struct element_cmp {
        bool operator()(data_token_t token, const element& elem) const
        {
            return token == elem.e_token || token == DT_ANY;
        }

        bool operator()(const element& elem, data_token_t token) const
        {
            return (*this)(token, elem);
        }
    };

    struct element_if {
        element_if(data_token_t token) : ei_token(token) {}

        bool operator()(const element& a) const
        {
            return a.e_token == this->ei_token;
        }

    private:
        data_token_t ei_token;
    };

    struct element_is_space {
        bool operator()(const element& el) const
        {
            return el.e_token == DT_WHITE || el.e_token == DT_CSI;
        }
    };

    struct discover_format_state {
        discover_format_state();

        void update_for_element(const element& elem);

        void finalize();

        data_format_state_t dfs_prefix_state;
        data_format_state_t dfs_semi_state;
        data_format_state_t dfs_comma_state;
        int dfs_hist[DT_TERMINAL_MAX];

        data_format dfs_format;
    };

    data_parser(data_scanner* ds);

    void pairup(schema_id_t* schema,
                element_list_t& pairs_out,
                element_list_t& in_list,
                int group_depth = 0);

    void discover_format();

    void end_of_value(element_list_t& el_stack,
                      element_list_t& key_comps,
                      element_list_t& value,
                      const element_list_t& in_list,
                      int group_depth);

    void parse();

    std::string get_element_string(const element& elem) const;

    std::string get_string_up_to_value(const element& elem);

    const char* get_element_string(const element& elem, size_t& len_out);

    void print(FILE* out, element_list_t& el);

    std::vector<data_token_t> dp_group_token;
    std::list<element_list_t> dp_group_stack;

    element_list_t dp_errors;

    element_list_t dp_pairs;
    schema_id_t dp_schema_id;
    std::string* dp_msg_format;
    int dp_msg_format_begin;

private:
    data_scanner* dp_scanner;
};

#endif