aboutsummaryrefslogtreecommitdiff
path: root/libbutl/json/parser.cxx
blob: 8ef7422f821c7cb87f3bd59fc3e15a71c2239ea2 (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#define PDJSON_SYMEXPORT static // See below.

#include <libbutl/json/parser.hxx>

#include <istream>

// There is an issue (segfault) with using std::current_exception() and
// std::rethrow_exception() with older versions of libc++ on Linux. While the
// exact root cause hasn't been determined, the suspicion is that something
// gets messed up if we "smuggle" std::exception_ptr through extern "C" call
// frames (we cannot even destroy such an exception without a segfault). We
// also could not determine in which version exactly this has been fixed but
// we know that libc++ 6.0.0 doesn't appear to have this issue (though we are
// not entirely sure the issue is (only) in libc++; libgcc_s could also be
// involved).
//
// The workaround is to just catch (and note) the exception and then throw a
// new instance of generic std::istream::failure. In order not to drag the
// below test into the header, we wrap exception_ptr with optional<> and use
// NULL to indicate the presence of the exception when the workaround is
// required.
//
// Note that if/when we drop this workaround, we should also get rid of
// optional<> in stream::exception member.
//
#undef LIBBUTL_JSON_NO_EXCEPTION_PTR

#if defined (__linux__) && defined(__clang__)
#  if __has_include(<__config>)
#    include <__config> // _LIBCPP_VERSION
#    if _LIBCPP_VERSION < 6000
#      define LIBBUTL_JSON_NO_EXCEPTION_PTR 1
#    endif
#  endif
#endif

namespace butl
{
  namespace json
  {
    using namespace std;

    parser::
    ~parser ()
    {
      json_close (impl_);
    }

    static int
    stream_get (void* x)
    {
      auto& s (*static_cast<parser::stream*> (x));

      // In the multi-value mode reading of whitespaces/separators is split
      // between our code and pdjson's. As a result, these functions may end
      // up being called more than once after EOF is reached. Which is
      // something iostream does not handle gracefully.
      //
      if (!s.is->eof ())
      {
        try
        {
          // We first peek not to trip failbit on EOF.
          //
          if (s.is->peek () != istream::traits_type::eof ())
            return static_cast<char> (s.is->get ());
        }
        catch (...)
        {
#ifndef LIBBUTL_JSON_NO_EXCEPTION_PTR
          s.exception = current_exception ();
#else
          s.exception = nullptr;
#endif
        }
      }

      return EOF;
    }

    static int
    stream_peek (void* x)
    {
      auto& s (*static_cast<parser::stream*> (x));

      if (!s.is->eof ())
      {
        try
        {
          auto c (s.is->peek ());
          if (c != istream::traits_type::eof ())
            return static_cast<char> (c);
        }
        catch (...)
        {
#ifndef LIBBUTL_JSON_NO_EXCEPTION_PTR
          s.exception = current_exception ();
#else
          s.exception = nullptr;
#endif
        }
      }

      return EOF;
    }

    // NOTE: watch out for exception safety (specifically, doing anything that
    // might throw after opening the stream).
    //
    parser::
    parser (istream& is, const char* n, bool mv, const char* sep) noexcept
        : input_name (n),
          stream_ {&is, nullopt},
          multi_value_ (mv),
          separators_ (sep),
          raw_s_ (nullptr),
          raw_n_ (0)
    {
      json_open_user (impl_, &stream_get, &stream_peek, &stream_);
      json_set_streaming (impl_, multi_value_);
    }

    parser::
    parser (const void* t,
            size_t s,
            const char* n,
            bool mv,
            const char* sep) noexcept
        : input_name (n),
          stream_ {nullptr, nullopt},
          multi_value_ (mv),
          separators_ (sep),
          raw_s_ (nullptr),
          raw_n_ (0)
    {
      json_open_buffer (impl_, t, s);
      json_set_streaming (impl_, multi_value_);
    }

    optional<event> parser::
    next ()
    {
      name_p_ = value_p_ = location_p_ = false;

      // Note that for now we don't worry about the state of the parser if
      // next_impl() throws assuming it is not going to be reused.
      //
      if (peeked_)
      {
        parsed_ = peeked_;
        peeked_ = nullopt;
      }
      else
        parsed_ = next_impl ();

      return translate (*parsed_);
    }

    optional<event> parser::
    peek ()
    {
      if (!peeked_)
      {
        if (parsed_)
        {
          cache_parsed_data ();
          cache_parsed_location ();
        }
        peeked_ = next_impl ();
      }
      return translate (*peeked_);
    }

    static inline const char*
    event_name (event e)
    {
      switch (e)
      {
      case event::begin_object: return "beginning of object";
      case event::end_object:   return "end of object";
      case event::begin_array:  return "beginning of array";
      case event::end_array:    return "end of array";
      case event::name:         return "member name";
      case event::string:       return "string value";
      case event::number:       return "numeric value";
      case event::boolean:      return "boolean value";
      case event::null:         return "null value";
      }

      return "";
    }

    bool parser::
    next_expect (event p, optional<event> s)
    {
      optional<event> e (next ());
      bool r;
      if (e && ((r = *e == p) || (s && *e == *s)))
        return r;

      string d ("expected ");
      d += event_name (p);

      if (s)
      {
        d += " or ";
        d += event_name (*s);
      }

      if (e)
      {
        d += " instead of ";
        d += event_name (*e);
      }

      throw invalid_json_input (input_name != nullptr ? input_name : "",
                                line (),
                                column (),
                                position (),
                                move (d));
    }

    void parser::
    next_expect_name (const char* n, bool su)
    {
      for (;;)
      {
        next_expect (event::name);

        if (name () == n)
          return;

        if (!su)
          break;

        next_expect_value_skip ();
      }

      string d ("expected object member name '");
      d += n;
      d += "' instead of '";
      d += name ();
      d += '\'';

      throw invalid_json_input (input_name != nullptr ? input_name : "",
                                line (),
                                column (),
                                position (),
                                move (d));
    }

    void parser::
    next_expect_value_skip ()
    {
      optional<event> e (next ());

      if (e)
      {
        switch (*e)
        {
        case event::begin_object:
        case event::begin_array:
          {
            // Skip until matching end_object/array keeping track of nesting.
            // We are going to rely on the fact that we should either get such
            // an event or next() should throw.
            //
            event be (*e);
            event ee (be == event::begin_object
                      ? event::end_object
                      : event::end_array);

            for (size_t n (0);; )
            {
              event e (*next ());

              if (e == ee)
              {
                if (n == 0)
                  break;

                --n;
              }
              else if (e == be)
                ++n;
            }

            return;
          }
        case event::string:
        case event::number:
        case event::boolean:
        case event::null:
          return;
        case event::name:
        case event::end_object:
        case event::end_array:
          break;
        }
      }

      string d ("expected value");

      if (e)
      {
        d += " instead of ";
        d += event_name (*e);
      }

      throw invalid_json_input (input_name != nullptr ? input_name : "",
                                line (),
                                column (),
                                position (),
                                move (d));
    }

    std::uint64_t parser::
    line () const noexcept
    {
      if (!location_p_)
      {
        if (!parsed_)
          return 0;

        assert (!peeked_);

        return static_cast<uint64_t> (
            json_get_lineno (const_cast<json_stream*> (impl_)));
      }

      return line_;
    }

    std::uint64_t parser::
    column () const noexcept
    {
      if (!location_p_)
      {
        if (!parsed_)
          return 0;

        assert (!peeked_);

        return static_cast<uint64_t> (
            json_get_column (const_cast<json_stream*> (impl_)));
      }

      return column_;
    }

    std::uint64_t parser::
    position () const noexcept
    {
      if (!location_p_)
      {
        if (!parsed_)
          return 0;

        assert (!peeked_);

        return static_cast<uint64_t> (
            json_get_position (const_cast<json_stream*> (impl_)));
      }

      return position_;
    }

    json_type parser::
    next_impl ()
    {
      raw_s_ = nullptr;
      raw_n_ = 0;
      json_type e;

      // Read characters between values skipping required separators and JSON
      // whitespaces. Return whether a required separator was encountered as
      // well as the first non-separator/whitespace character (which, if EOF,
      // should trigger a check for input/output errors).
      //
      // Note that the returned non-separator will not have been extracted
      // from the input (so position, column, etc. will still refer to its
      // predecessor).
      //
      auto skip_separators = [this] () -> pair<bool, int>
      {
        bool r (separators_ == nullptr);

        int c;
        for (; (c = json_source_peek (impl_)) != EOF; json_source_get (impl_))
        {
          // User separator.
          //
          if (separators_ != nullptr && *separators_ != '\0')
          {
            if (strchr (separators_, c) != nullptr)
            {
              r = true;
              continue;
            }
          }

          // JSON separator.
          //
          if (json_isspace (c))
          {
            if (separators_ != nullptr && *separators_ == '\0')
              r = true;

            continue;
          }

          break;
        }

        return make_pair (r, c);
      };

      // In the multi-value mode skip any instances of required separators
      // (and any other JSON whitespace) preceding the first JSON value.
      //
      if (multi_value_ && !parsed_ && !peeked_)
      {
        if (skip_separators ().second == EOF && stream_.is != nullptr)
        {
          if (stream_.exception)   goto fail_rethrow;
          if (stream_.is->fail ()) goto fail_stream;
        }
      }

      e = json_next (impl_);

      // First check for a pending input/output error.
      //
      if (stream_.is != nullptr)
      {
        if (stream_.exception)   goto fail_rethrow;
        if (stream_.is->fail ()) goto fail_stream;
      }

      // There are two ways to view separation between two values: as following
      // the first value or as preceding the second value. And one aspect that
      // is determined by this is whether a separation violation is a problem
      // with the first value or with the second, which becomes important if
      // the user bails out before parsing the second value.
      //
      // Consider these two unseparated value (yes, in JSON they are two
      // values, leading zeros are not allowed in JSON numbers):
      //
      // 01
      //
      // If the user bails out after parsing 0 in a stream that should have
      // been newline-delimited, they most likely would want to get an error
      // since this is most definitely an invalid value rather than two
      // values that are not properly separated. So in this light we handle
      // separators at the end of the first value.
      //
      switch (e)
      {
      case JSON_DONE:
        {
          // Deal with the following value separators.
          //
          // Note that we must not do this for the second JSON_DONE (or the
          // first one in case there are no values) that signals the end of
          // input.
          //
          if (multi_value_         &&
              (parsed_ || peeked_) &&
              (peeked_ ? *peeked_ : *parsed_) != JSON_DONE)
          {
            auto p (skip_separators ());

            if (p.second == EOF && stream_.is != nullptr)
            {
              if (stream_.exception)   goto fail_rethrow;
              if (stream_.is->fail ()) goto fail_stream;
            }

            // Note that we don't require separators after the last value.
            //
            if (!p.first && p.second != EOF)
            {
              json_source_get (impl_); // Consume to update column number.
              goto fail_separation;
            }

            json_reset (impl_);
          }
          break;
        }
      case JSON_ERROR: goto fail_json;
      case JSON_STRING:
      case JSON_NUMBER:
        raw_s_ = json_get_string (impl_, &raw_n_);
        raw_n_--; // Includes terminating `\0`.
        break;
      case JSON_TRUE:  raw_s_ = "true";  raw_n_ = 4; break;
      case JSON_FALSE: raw_s_ = "false"; raw_n_ = 5; break;
      case JSON_NULL:  raw_s_ = "null";  raw_n_ = 4; break;
      default: break;
      }

      return e;

    fail_json:
      throw invalid_json_input (
          input_name != nullptr ? input_name : "",
          static_cast<uint64_t> (json_get_lineno (impl_)),
          static_cast<uint64_t> (json_get_column (impl_)),
          static_cast<uint64_t> (json_get_position (impl_)),
          json_get_error (impl_));

    fail_separation:
      throw invalid_json_input (
          input_name != nullptr ? input_name : "",
          static_cast<uint64_t> (json_get_lineno (impl_)),
          static_cast<uint64_t> (json_get_column (impl_)),
          static_cast<uint64_t> (json_get_position (impl_)),
          "missing separator between JSON values");

    fail_stream:
      throw invalid_json_input (
          input_name != nullptr ? input_name : "",
          static_cast<uint64_t> (json_get_lineno (impl_)),
          static_cast<uint64_t> (json_get_column (impl_)),
          static_cast<uint64_t> (json_get_position (impl_)),
          "unable to read JSON input text");

    fail_rethrow:
#ifndef LIBBUTL_JSON_NO_EXCEPTION_PTR
      rethrow_exception (move (*stream_.exception));
#else
      throw istream::failure ("unable to read");
#endif
    }

    optional<event> parser::
    translate (json_type e) const noexcept
    {
      switch (e)
      {
      case JSON_DONE: return nullopt;
      case JSON_OBJECT: return event::begin_object;
      case JSON_OBJECT_END: return event::end_object;
      case JSON_ARRAY: return event::begin_array;
      case JSON_ARRAY_END: return event::end_array;
      case JSON_STRING:
        {
          // This can be a value or, inside an object, a name from the
          // name/value pair.
          //
          size_t n;
          return json_get_context (const_cast<json_stream*> (impl_), &n) ==
                             JSON_OBJECT &&
                         n % 2 == 1
                     ? event::name
                     : event::string;
        }
      case JSON_NUMBER: return event::number;
      case JSON_TRUE: return event::boolean;
      case JSON_FALSE: return event::boolean;
      case JSON_NULL: return event::null;
      case JSON_ERROR: assert (false); // Should've been handled by caller.
      }

      return nullopt; // Should never reach.
    }

    void parser::
    cache_parsed_data ()
    {
      name_p_ = value_p_ = false;
      if (const optional<event> e = translate (*parsed_))
      {
        if (e == event::name)
        {
          name_.assign (raw_s_, raw_n_);
          name_p_ = true;
        }
        else if (value_event (e))
        {
          value_.assign (raw_s_, raw_n_);
          value_p_ = true;
        }
      }
    }

    void parser::
    cache_parsed_location () noexcept
    {
      line_ = static_cast<uint64_t> (json_get_lineno (impl_));
      column_ = static_cast<uint64_t> (json_get_column (impl_));
      position_ = static_cast<uint64_t> (json_get_position (impl_));
      location_p_ = true;
    }

    bool parser::
    value_event (optional<event> e) noexcept
    {
      if (!e)
        return false;

      switch (*e)
      {
      case event::string:
      case event::number:
      case event::boolean:
      case event::null:
        return true;
      default:
        return false;
      }
    }

    [[noreturn]] void parser::
    throw_invalid_value (const char* type, const char* v, size_t n) const
    {
      string d (string ("invalid ") + type + " value: '");
      d.append (v, n);
      d += '\'';

      throw invalid_json_input (input_name != nullptr ? input_name : "",
                                line (),
                                column (),
                                position (),
                                move (d));
    }
  } // namespace json
} // namespace butl

// Include the implementation into our translation unit (instead of compiling
// it separately) to (hopefully) get function inlining without LTO.
//
// Let's keep it last since the implementation defines a couple of macros.
//
#if defined(__clang__) || defined(__GNUC__)
#  pragma GCC diagnostic ignored "-Wunused-function"
#endif

extern "C"
{
#define PDJSON_STACK_INC 16
#define PDJSON_STACK_MAX 2048
#include "pdjson.c"
}