summaryrefslogtreecommitdiffstats
path: root/src/formats/logfmt/logfmt.parser.cc
blob: 20c725234f3fd8e3ba4e39055de0072cb140a6d2 (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
/**
 * Copyright (c) 2021, 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.
 *
 * @file logfmt.parser.cc
 */

#include "logfmt.parser.hh"

#include "base/intern_string.hh"
#include "config.h"
#include "scn/scn.h"

logfmt::parser::parser(string_fragment sf) : p_next_input(sf) {}

static bool
is_not_eq(char ch)
{
    return ch != '=';
}

struct bare_value_predicate {
    enum class int_state_t {
        INIT,
        NEED_DIGIT,
        DIGITS,
        INVALID,
    };

    enum class float_state_t {
        INIT,
        NEED_DIGIT,
        DIGITS,
        FRACTION_DIGIT,
        EXPONENT_INIT,
        EXPONENT_NEED_DIGIT,
        EXPONENT_DIGIT,
        INVALID,
    };

    int_state_t bvp_int_state{int_state_t::INIT};
    float_state_t bvp_float_state{float_state_t::INIT};
    size_t bvp_index{0};

    bool is_integer() const
    {
        return this->bvp_int_state == int_state_t::DIGITS;
    }

    bool is_float() const
    {
        switch (this->bvp_float_state) {
            case float_state_t::DIGITS:
            case float_state_t::FRACTION_DIGIT:
            case float_state_t::EXPONENT_DIGIT:
                return true;
            default:
                return false;
        }
    }

    bool operator()(char ch)
    {
        if (ch == ' ') {
            return false;
        }

        bool got_digit = isdigit(ch);
        switch (this->bvp_int_state) {
            case int_state_t::INIT:
                if (got_digit) {
                    this->bvp_int_state = int_state_t::DIGITS;
                } else if (ch == '-') {
                    this->bvp_int_state = int_state_t::NEED_DIGIT;
                } else {
                    this->bvp_int_state = int_state_t::INVALID;
                }
                break;
            case int_state_t::DIGITS:
            case int_state_t::NEED_DIGIT:
                if (got_digit) {
                    this->bvp_int_state = int_state_t::DIGITS;
                } else {
                    this->bvp_int_state = int_state_t::INVALID;
                }
                break;
            case int_state_t::INVALID:
                break;
        }

        switch (this->bvp_float_state) {
            case float_state_t::INIT:
                if (got_digit) {
                    this->bvp_float_state = float_state_t::DIGITS;
                } else if (ch == '-') {
                    this->bvp_float_state = float_state_t::NEED_DIGIT;
                } else {
                    this->bvp_float_state = float_state_t::INVALID;
                }
                break;
            case float_state_t::DIGITS:
            case float_state_t::NEED_DIGIT:
                if (got_digit) {
                    this->bvp_float_state = float_state_t::DIGITS;
                } else if (ch == '.') {
                    this->bvp_float_state = float_state_t::FRACTION_DIGIT;
                } else if (ch == 'e' || ch == 'E') {
                    this->bvp_float_state = float_state_t::EXPONENT_INIT;
                } else {
                    this->bvp_float_state = float_state_t::INVALID;
                }
                break;
            case float_state_t::FRACTION_DIGIT:
                if (got_digit) {
                    this->bvp_float_state = float_state_t::FRACTION_DIGIT;
                } else if (ch == 'e' || ch == 'E') {
                    this->bvp_float_state = float_state_t::EXPONENT_INIT;
                } else {
                    this->bvp_float_state = float_state_t::INVALID;
                }
                break;
            case float_state_t::EXPONENT_INIT:
                if (got_digit) {
                    this->bvp_float_state = float_state_t::EXPONENT_DIGIT;
                } else if (ch == '-' || ch == '+') {
                    this->bvp_float_state = float_state_t::EXPONENT_NEED_DIGIT;
                } else {
                    this->bvp_float_state = float_state_t::INVALID;
                }
                break;
            case float_state_t::EXPONENT_NEED_DIGIT:
            case float_state_t::EXPONENT_DIGIT:
                if (got_digit) {
                    this->bvp_float_state = float_state_t::EXPONENT_DIGIT;
                } else {
                    this->bvp_float_state = float_state_t::INVALID;
                }
                break;
            case float_state_t::INVALID:
                break;
        }

        this->bvp_index += 1;

        return true;
    }
};

logfmt::parser::step_result
logfmt::parser::step()
{
    const static auto IS_DQ = string_fragment::tag1{'"'};

    auto remaining = this->p_next_input.skip(isspace);

    if (remaining.empty()) {
        return end_of_input{};
    }

    auto pair_opt = remaining.split_while(is_not_eq);

    if (!pair_opt) {
        return error{remaining.sf_begin, "expecting key followed by '='"};
    }

    auto key_frag = pair_opt->first;
    auto after_eq = pair_opt->second.consume(string_fragment::tag1{'='});

    if (!after_eq) {
        return error{pair_opt->second.sf_begin, "expecting '='"};
    }

    auto value_start = after_eq.value();

    if (value_start.startswith("\"")) {
        string_fragment::quoted_string_body qsb;
        auto quoted_pair = value_start.consume_n(1)->split_while(qsb);

        if (!quoted_pair) {
            return error{value_start.sf_begin + 1, "string body missing"};
        }

        auto after_quote = quoted_pair->second.consume(IS_DQ);

        if (!after_quote) {
            return error{quoted_pair->second.sf_begin, "non-terminated string"};
        }

        this->p_next_input = after_quote.value();
        return std::make_pair(
            key_frag,
            quoted_value{string_fragment{quoted_pair->first.sf_string,
                                         quoted_pair->first.sf_begin - 1,
                                         quoted_pair->first.sf_end + 1}});
    }

    bare_value_predicate bvp;
    auto value_pair = value_start.split_while(bvp);

    if (value_pair) {
        static const auto TRUE_FRAG = string_fragment::from_const("true");
        static const auto FALSE_FRAG = string_fragment::from_const("false");

        this->p_next_input = value_pair->second;
        if (bvp.is_integer()) {
            int_value retval;

            auto int_scan_res
                = scn::scan_value<int64_t>(value_pair->first.to_string_view());
            if (int_scan_res) {
                retval.iv_value = int_scan_res.value();
            }
            retval.iv_str_value = value_pair->first;

            return std::make_pair(key_frag, retval);
        }
        if (bvp.is_float()) {
            float_value retval;

            auto float_scan_res
                = scn::scan_value<double>(value_pair->first.to_string_view());
            if (float_scan_res) {
                retval.fv_value = float_scan_res.value();
            }
            retval.fv_str_value = value_pair->first;

            return std::make_pair(key_frag, retval);
        }
        if (value_pair->first.iequal(TRUE_FRAG)) {
            return std::make_pair(key_frag,
                                  bool_value{true, value_pair->first});
        }
        if (value_pair->first.iequal(FALSE_FRAG)) {
            return std::make_pair(key_frag,
                                  bool_value{false, value_pair->first});
        }
        return std::make_pair(key_frag, unquoted_value{value_pair->first});
    }

    this->p_next_input = value_start;
    return std::make_pair(key_frag, unquoted_value{string_fragment{}});
}