-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick-json
566 lines (478 loc) · 18.6 KB
/
brick-json
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
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
* Copyright (C) 2019 Petr Rockai <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include "brick-assert"
#include "brick-string"
#include "brick-except"
#include <stdexcept>
namespace brq
{
struct json_error : std::runtime_error
{
using std::runtime_error::runtime_error;
};
template< typename T > struct any { using type = T; };
template< typename T > struct is_any : std::false_type {};
template< typename T > struct is_any< any< T > > : std::true_type {};
template< typename T > constexpr bool is_any_v = is_any< T >::value;
struct json_listener
{
template< typename F >
void decode_escape( string_builder &b, F fetch )
{
auto unhex = [&]( int shift )
{
char32_t v = fetch();
if ( v >= '0' && v <= '9' ) v = v - '0';
else if ( v >= 'a' && v <= 'f' ) v = v - 'a' + 10;
else if ( v >= 'A' && v <= 'F' ) v = v - 'A' + 10;
else raise< json_error >() << "invalid character '" << rawchr32( v )
<< "' in a \\u-sequence";
return v << shift;
};
char c = fetch();
switch ( c )
{
case 'b': b << "\b"; break;
case 'n': b << "\n"; break;
case 'r': b << "\r"; break;
case 't': b << "\t"; break;
case 'f': b << "\f"; break;
case 'u':
{
char32_t ch = 0;
ch |= unhex( 12 );
ch |= unhex( 8 );
ch |= unhex( 4 );
ch |= unhex( 0 );
b << rawchr32( ch );
break;
}
default: b << rawchr( c );
}
}
std::string decode( std::vector< std::string_view > v )
{
int i = 0, j = 0;
brq::string_builder b;
auto fetch = [&]
{
if ( j < v[ i ].size() )
return v[ i ][ j++ ];
while ( ++i < v.size() )
if ( v[ i ].size() )
return j = 0, v[ i ][ j++ ];
return char( 0 );
};
while ( char c = fetch() )
if ( c == '\\' )
decode_escape( b, fetch );
else
b << rawchr( c );
return std::string( b.data() );
}
int64_t decode_int( std::vector< std::string_view > v ) { return std::stoi( decode( v ) ); }
double decode_float( std::vector< std::string_view > v ) { return std::stod( decode( v ) ); }
virtual void object_start() {}
virtual void object_item( std::vector< std::string_view > key ) { object_item( decode( key ) ); }
virtual void object_item( std::string ) {}
virtual void object_end() {}
virtual void array_start() {}
virtual void array_item() {}
virtual void array_end() {}
using value_var_t = std::variant< bool, int64_t, double, std::string >;
struct value_t : value_var_t
{
using value_var_t::value_var_t;
bool b() const { return std::get< bool >( *this ); }
int64_t i() const { return std::get< int64_t >( *this ); }
double f() const { return std::get< double >( *this ); }
std::string s() const { return std::get< std::string >( *this ); }
};
virtual void value( value_t ) {}
virtual void boolean( bool b ) { value( b ); }
virtual void null() {}
virtual void string( std::vector< std::string_view > v ) { string( decode( v ) ); }
virtual void string( std::string s ) { value( s ); }
virtual void number( int64_t i ) { value( i ); }
virtual void number( double f ) { value( f ); }
virtual void number( bool integer, std::vector< std::string_view > v )
{
if ( integer )
number( decode_int( v ) );
else
number( decode_float( v ) );
}
virtual ~json_listener() = default;
};
struct json_parser
{
json_listener &_l;
std::vector< bool > _stack;
std::string_view _chunk;
std::vector< std::string_view > _splice;
bool _backslash = false;
bool _integer = true;
enum state_t { read_value, read_comma_or_name, read_comma_or_value,
read_name, read_name_str, read_colon, read_comma,
read_bareword, read_string, read_number, read_digits, read_digits_tail,
read_decimal, read_exponent, read_exponent_digits } _state = read_value;
template< typename stream >
friend auto operator<<( stream &out, state_t s ) -> decltype( out << "" )
{
switch ( s )
{
case read_value: return out << "read_value";
case read_name: return out << "read_name";
case read_name_str: return out << "read_name_str";
case read_colon: return out << "read_colon";
case read_comma: return out << "read_comma";
case read_comma_or_name: return out << "read_comma_or_name";
case read_comma_or_value: return out << "read_comma_or_value";
case read_bareword: return out << "read_bareword";
case read_string: return out << "read_string";
case read_number: return out << "read_number";
case read_digits: return out << "read_digits";
case read_digits_tail: return out << "read_digits_tail";
case read_decimal: return out << "read_decimal";
case read_exponent: return out << "read_exponent";
case read_exponent_digits: return out << "read_exponent_digits";
}
}
void chunk( std::string_view c )
{
ASSERT( _chunk.empty() );
_chunk = c;
switch ( _state )
{
case read_digits:
case read_digits_tail:
case read_decimal:
case read_exponent:
case read_exponent_digits:
case read_number: number(); break;
case read_string: string(); break;
case read_colon:
case read_name_str:
case read_name: name( false ); break;
case read_comma: comma(); break;
case read_comma_or_name: comma_or_name(); break;
case read_comma_or_value: comma_or_value(); break;
case read_value: value(); break;
case read_bareword: bareword(); break;
}
if ( !_stack.empty() && !_chunk.empty() )
comma();
}
json_parser( json_listener &l ) : _l( l ) {}
bool next_state( state_t s, bool do_chomp ) /* return true if we have a character to work on */
{
TRACE( "→", s );
if ( do_chomp )
chomp( 0 );
_state = s;
return !_chunk.empty();
}
char next() { return _chunk[ 0 ]; }
void chomp( int count = 1 )
{
_chunk.remove_prefix( count );
while ( _chunk.size() && ( std::isblank( next() ) || next() == '\n' ) )
_chunk.remove_prefix( 1 );
}
std::vector< std::string_view > splice( int i, int cut = 0 )
{
auto rv = std::move( _splice );
rv.push_back( _chunk.substr( 0, i ) );
_chunk.remove_prefix( i + cut );
TRACE( "splice", rv );
return rv;
}
std::vector< std::string_view > scan_string()
{
for ( int i = 0; i < _chunk.size(); ++i )
{
if ( !_backslash && _chunk[ i ] == '"' ) /* end of string */
return splice( i, 1 );
if ( !_backslash && _chunk[ i ] == '\\' )
_backslash = true;
else
_backslash = false;
}
_splice.push_back( _chunk );
_chunk = "";
return {};
}
void string()
{
if ( !next_state( read_string, false ) )
return;
if ( auto v = scan_string(); !v.empty() )
{
_l.string( v );
if ( !_stack.empty() )
comma();
}
}
auto error()
{
auto chunk = _chunk.substr( 0, 60 );
auto error = brq::raise< json_error >();
error << "parsing JSON chunk '" << chunk <<"': ";
return std::move( error );
}
std::vector< std::string_view > scan_number()
{
for ( int i = 0; i < _chunk.size(); ++i )
{
switch( _state )
{
case read_number:
if ( _chunk[ i ] == '-' )
next_state( read_digits, false );
else if ( _chunk[ i ] == '0' )
next_state( read_digits_tail, false ); /* FIXME lenient */
else if ( std::isdigit( _chunk[ i ] ) )
next_state( read_digits_tail, false );
else
error() << "expected - or a digit at " << i;
_integer = true;
continue;
case read_digits:
TRACE( "read_digits", _chunk[ i ] );
if ( _chunk[ i ] == '0' )
next_state( read_decimal, false );
else if ( std::isdigit( _chunk[ i ] ) )
next_state( read_digits_tail, false );
else
error() << "expected a digit at " << i;
continue;
case read_digits_tail:
if ( std::isdigit( _chunk[ i ] ) )
; /* keep reading more digits */
else if ( _chunk[ i ] == '.' )
next_state( read_decimal, false );
else if ( _chunk[ i ] == 'e' || _chunk[ i ] == 'E' )
next_state( read_exponent, false );
else
return splice( i );
continue;
case read_decimal:
_integer = false;
if ( std::isdigit( _chunk[ i ] ) )
; /* keep reading digits */
else if ( _chunk[ i ] == 'e' || _chunk[ i ] == 'E' )
next_state( read_exponent, false );
else
return splice( i );
continue;
case read_exponent:
_integer = false;
if ( _chunk[ i ] == '+' || _chunk[ i ] == '-' )
{
next_state( read_exponent_digits, false );
continue;
}
case read_exponent_digits:
if ( std::isdigit( _chunk[ i ] ) )
; /* keep reading digits */
else
return splice( i ); /* end of the number */
continue;
default:
__builtin_unreachable();
}
}
_splice.push_back( _chunk );
_chunk = "";
return {}; /* incomplete number */
}
void number()
{
if ( auto v = scan_number(); !v.empty() )
{
_l.number( _integer, v );
if ( !_stack.empty() )
comma();
}
}
void name( bool set )
{
if ( set )
_state = read_name;
switch ( _state )
{
case read_name:
if ( !next_state( read_name, true ) )
return;
if ( next() != '"' )
error() << "expected \" (name)";
_chunk.remove_prefix( 1 );
case read_name_str:
if ( !next_state( read_name_str, false ) )
return;
if ( auto v = scan_string(); !v.empty() )
_l.object_item( v );
else
return;
case read_colon:
if ( !next_state( read_colon, true ) )
return;
if ( next() != ':' )
error() << "expected :";
chomp();
value();
break;
default:
__builtin_unreachable();
}
}
void pop( bool v )
{
ASSERT( !_stack.empty() );
if ( _stack.back() != v )
error() << "mismatched ] vs }";
_stack.pop_back();
chomp();
}
void comma()
{
if ( !next_state( read_comma, true ) )
return;
switch ( next() )
{
case '}': pop( true ); _l.object_end(); return comma();
case ']': pop( false ); _l.array_end(); return comma();
case ',': chomp(); break;
default: error() << "expected , or }";
}
if ( _stack.empty() )
return;
if ( _stack.back() )
return name( true );
else
return value();
}
void comma_or_name()
{
if ( !next_state( read_comma_or_name, false ) )
return;
if ( next() == '}' )
comma();
else
name( true );
}
void comma_or_value()
{
if ( !next_state( read_comma_or_value, false ) )
return;
if ( next() == ']' )
comma();
else
value();
}
void bareword()
{
next_state( read_bareword, false );
for ( int i = 0; i < _chunk.size(); ++i )
{
if ( !std::isalpha( _chunk[ i ] ) )
{
auto w = _l.decode( splice( i ) );
if ( w == "true" ) _l.boolean( true );
else if ( w == "false" ) _l.boolean( false );
else if ( w == "null" ) _l.null();
else error() << "unexpected bareword " << w;
if ( !_stack.empty() )
comma();
}
}
_splice.push_back( _chunk );
_chunk = "";
}
void value()
{
if ( !next_state( read_value, true ) )
return;
switch ( next() )
{
case '{':
chomp();
_stack.push_back( true );
_l.object_start();
comma_or_name();
break;
case '[':
chomp();
_stack.push_back( false );
_l.array_start();
comma_or_value();
break;
case '"':
_chunk.remove_prefix( 1 );
string();
break;
case 'f': case 't': case 'n':
bareword();
break;
default:
if ( std::isdigit( next() ) || next() == '-' )
{
next_state( read_number, false );
number();
}
else
error() << "unexpected character " << next();
}
if ( !_stack.empty() && !_chunk.empty() )
comma();
}
};
struct json_stack : json_listener
{
using any_index_t = any< int >;
using any_key_t = any< std::string >;
constexpr static const any_key_t any_key{};
constexpr static const any_index_t any_index{};
using item_t = std::variant< int, std::string >;
std::vector< item_t > _stack;
virtual void close() {}
void object_start() override { _stack.push_back( "" ); }
void object_end() override { _stack.pop_back(); close(); }
void array_start() override { _stack.push_back( -1 ); }
void array_end() override { _stack.pop_back(); close(); }
void object_item( std::string k ) override { _stack.back() = k; }
void array_item() override { std::get< int >( _stack.back() ) += 1; }
std::string top_key() const { return std::get< std::string >( _stack.back() ); }
int top_index() const { return std::get< int >( _stack.back() ); }
bool in_context() { return true; }
template< typename arg_t, typename... args_t >
bool in_context( arg_t arg, args_t... args )
{
if ( _stack.size() <= sizeof...( args ) )
return false;
bool match = true;
auto &item = _stack.rbegin()[ sizeof...( args ) ];
TRACE( "in_context", arg, item );
if constexpr ( is_any_v< arg_t > )
match = std::holds_alternative< typename arg_t::type >( item );
else
match = item == item_t( arg );
return match && in_context( args... );
}
};
}