json 2.9.1 → 2.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,36 +1,22 @@
1
- /* This file is automatically generated from parser.rl by using ragel */
2
- #line 1 "parser.rl"
3
- #include "ruby.h"
4
- #include "../fbuffer/fbuffer.h"
1
+ #include "../json.h"
2
+ #include "../vendor/ryu.h"
3
+ #include "../simd/simd.h"
5
4
 
6
- static VALUE mJSON, mExt, cParser, eNestingError, Encoding_UTF_8;
5
+ static VALUE mJSON, eNestingError, Encoding_UTF_8;
7
6
  static VALUE CNaN, CInfinity, CMinusInfinity;
8
7
 
9
- static ID i_json_creatable_p, i_json_create, i_create_id,
10
- i_chr, i_deep_const_get, i_match, i_aset, i_aref,
11
- i_leftshift, i_new, i_try_convert, i_uminus, i_encode;
8
+ static ID i_new, i_try_convert, i_uminus, i_encode;
12
9
 
13
- static VALUE sym_max_nesting, sym_allow_nan, sym_allow_trailing_comma, sym_symbolize_names, sym_freeze,
14
- sym_create_additions, sym_create_id, sym_object_class, sym_array_class,
15
- sym_decimal_class, sym_match_string;
10
+ static VALUE sym_max_nesting, sym_allow_nan, sym_allow_trailing_comma, sym_allow_control_characters, sym_symbolize_names, sym_freeze,
11
+ sym_decimal_class, sym_on_load, sym_allow_duplicate_key;
16
12
 
17
13
  static int binary_encindex;
18
14
  static int utf8_encindex;
19
15
 
20
- #ifdef HAVE_RB_CATEGORY_WARN
21
- # define json_deprecated(message) rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, message)
22
- #else
23
- # define json_deprecated(message) rb_warn(message)
24
- #endif
25
-
26
- static const char deprecated_create_additions_warning[] =
27
- "JSON.load implicit support for `create_additions: true` is deprecated "
28
- "and will be removed in 3.0, use JSON.unsafe_load or explicitly "
29
- "pass `create_additions: true`";
30
-
31
16
  #ifndef HAVE_RB_HASH_BULK_INSERT
32
17
  // For TruffleRuby
33
- void rb_hash_bulk_insert(long count, const VALUE *pairs, VALUE hash)
18
+ static void
19
+ rb_hash_bulk_insert(long count, const VALUE *pairs, VALUE hash)
34
20
  {
35
21
  long index = 0;
36
22
  while (index < count) {
@@ -42,6 +28,17 @@ void rb_hash_bulk_insert(long count, const VALUE *pairs, VALUE hash)
42
28
  }
43
29
  #endif
44
30
 
31
+ #ifndef HAVE_RB_HASH_NEW_CAPA
32
+ #define rb_hash_new_capa(n) rb_hash_new()
33
+ #endif
34
+
35
+ #ifndef HAVE_RB_STR_TO_INTERNED_STR
36
+ static VALUE rb_str_to_interned_str(VALUE str)
37
+ {
38
+ return rb_funcall(rb_str_freeze(str), i_uminus, 0);
39
+ }
40
+ #endif
41
+
45
42
  /* name cache */
46
43
 
47
44
  #include <string.h>
@@ -86,116 +83,104 @@ static void rvalue_cache_insert_at(rvalue_cache *cache, int index, VALUE rstring
86
83
  cache->entries[index] = rstring;
87
84
  }
88
85
 
89
- static inline int rstring_cache_cmp(const char *str, const long length, VALUE rstring)
86
+ #define rstring_cache_memcmp memcmp
87
+
88
+ #if JSON_CPU_LITTLE_ENDIAN_64BITS
89
+ #if __has_builtin(__builtin_bswap64)
90
+ #undef rstring_cache_memcmp
91
+ ALWAYS_INLINE(static) int rstring_cache_memcmp(const char *str, const char *rptr, const long length)
90
92
  {
91
- long rstring_length = RSTRING_LEN(rstring);
92
- if (length == rstring_length) {
93
- return memcmp(str, RSTRING_PTR(rstring), length);
94
- } else {
95
- return (int)(length - rstring_length);
93
+ // The libc memcmp has numerous complex optimizations, but in this particular case,
94
+ // we know the string is small (JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH), so being able to
95
+ // inline a simpler memcmp outperforms calling the libc version.
96
+ long i = 0;
97
+
98
+ for (; i + 8 <= length; i += 8) {
99
+ uint64_t a, b;
100
+ memcpy(&a, str + i, 8);
101
+ memcpy(&b, rptr + i, 8);
102
+ if (a != b) {
103
+ a = __builtin_bswap64(a);
104
+ b = __builtin_bswap64(b);
105
+ return (a < b) ? -1 : 1;
106
+ }
107
+ }
108
+
109
+ for (; i < length; i++) {
110
+ if (str[i] != rptr[i]) {
111
+ return (str[i] < rptr[i]) ? -1 : 1;
112
+ }
96
113
  }
114
+
115
+ return 0;
97
116
  }
117
+ #endif
118
+ #endif
98
119
 
99
- static VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const long length)
120
+ ALWAYS_INLINE(static) int rstring_cache_cmp(const char *str, const long length, VALUE rstring)
100
121
  {
101
- if (RB_UNLIKELY(length > JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH)) {
102
- // Common names aren't likely to be very long. So we just don't
103
- // cache names above an arbitrary threshold.
104
- return Qfalse;
105
- }
122
+ const char *rstring_ptr;
123
+ long rstring_length;
124
+
125
+ RSTRING_GETMEM(rstring, rstring_ptr, rstring_length);
106
126
 
107
- if (RB_UNLIKELY(!isalpha(str[0]))) {
108
- // Simple heuristic, if the first character isn't a letter,
109
- // we're much less likely to see this string again.
110
- // We mostly want to cache strings that are likely to be repeated.
111
- return Qfalse;
127
+ if (length == rstring_length) {
128
+ return rstring_cache_memcmp(str, rstring_ptr, length);
129
+ } else {
130
+ return (int)(length - rstring_length);
112
131
  }
132
+ }
113
133
 
134
+ ALWAYS_INLINE(static) VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const long length)
135
+ {
114
136
  int low = 0;
115
137
  int high = cache->length - 1;
116
- int mid = 0;
117
- int last_cmp = 0;
118
138
 
119
139
  while (low <= high) {
120
- mid = (high + low) >> 1;
140
+ int mid = (high + low) >> 1;
121
141
  VALUE entry = cache->entries[mid];
122
- last_cmp = rstring_cache_cmp(str, length, entry);
142
+ int cmp = rstring_cache_cmp(str, length, entry);
123
143
 
124
- if (last_cmp == 0) {
144
+ if (cmp == 0) {
125
145
  return entry;
126
- } else if (last_cmp > 0) {
146
+ } else if (cmp > 0) {
127
147
  low = mid + 1;
128
148
  } else {
129
149
  high = mid - 1;
130
150
  }
131
151
  }
132
152
 
133
- if (RB_UNLIKELY(memchr(str, '\\', length))) {
134
- // We assume the overwhelming majority of names don't need to be escaped.
135
- // But if they do, we have to fallback to the slow path.
136
- return Qfalse;
137
- }
138
-
139
153
  VALUE rstring = build_interned_string(str, length);
140
154
 
141
155
  if (cache->length < JSON_RVALUE_CACHE_CAPA) {
142
- if (last_cmp > 0) {
143
- mid += 1;
144
- }
145
-
146
- rvalue_cache_insert_at(cache, mid, rstring);
156
+ rvalue_cache_insert_at(cache, low, rstring);
147
157
  }
148
158
  return rstring;
149
159
  }
150
160
 
151
161
  static VALUE rsymbol_cache_fetch(rvalue_cache *cache, const char *str, const long length)
152
162
  {
153
- if (RB_UNLIKELY(length > JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH)) {
154
- // Common names aren't likely to be very long. So we just don't
155
- // cache names above an arbitrary threshold.
156
- return Qfalse;
157
- }
158
-
159
- if (RB_UNLIKELY(!isalpha(str[0]))) {
160
- // Simple heuristic, if the first character isn't a letter,
161
- // we're much less likely to see this string again.
162
- // We mostly want to cache strings that are likely to be repeated.
163
- return Qfalse;
164
- }
165
-
166
163
  int low = 0;
167
164
  int high = cache->length - 1;
168
- int mid = 0;
169
- int last_cmp = 0;
170
165
 
171
166
  while (low <= high) {
172
- mid = (high + low) >> 1;
167
+ int mid = (high + low) >> 1;
173
168
  VALUE entry = cache->entries[mid];
174
- last_cmp = rstring_cache_cmp(str, length, rb_sym2str(entry));
169
+ int cmp = rstring_cache_cmp(str, length, rb_sym2str(entry));
175
170
 
176
- if (last_cmp == 0) {
171
+ if (cmp == 0) {
177
172
  return entry;
178
- } else if (last_cmp > 0) {
173
+ } else if (cmp > 0) {
179
174
  low = mid + 1;
180
175
  } else {
181
176
  high = mid - 1;
182
177
  }
183
178
  }
184
179
 
185
- if (RB_UNLIKELY(memchr(str, '\\', length))) {
186
- // We assume the overwhelming majority of names don't need to be escaped.
187
- // But if they do, we have to fallback to the slow path.
188
- return Qfalse;
189
- }
190
-
191
180
  VALUE rsymbol = build_symbol(str, length);
192
181
 
193
182
  if (cache->length < JSON_RVALUE_CACHE_CAPA) {
194
- if (last_cmp > 0) {
195
- mid += 1;
196
- }
197
-
198
- rvalue_cache_insert_at(cache, mid, rsymbol);
183
+ rvalue_cache_insert_at(cache, low, rsymbol);
199
184
  }
200
185
  return rsymbol;
201
186
  }
@@ -231,13 +216,14 @@ static rvalue_stack *rvalue_stack_grow(rvalue_stack *stack, VALUE *handle, rvalu
231
216
  return stack;
232
217
  }
233
218
 
234
- static void rvalue_stack_push(rvalue_stack *stack, VALUE value, VALUE *handle, rvalue_stack **stack_ref)
219
+ static VALUE rvalue_stack_push(rvalue_stack *stack, VALUE value, VALUE *handle, rvalue_stack **stack_ref)
235
220
  {
236
221
  if (RB_UNLIKELY(stack->head >= stack->capa)) {
237
222
  stack = rvalue_stack_grow(stack, handle, stack_ref);
238
223
  }
239
224
  stack->ptr[stack->head] = value;
240
225
  stack->head++;
226
+ return value;
241
227
  }
242
228
 
243
229
  static inline VALUE *rvalue_stack_peek(rvalue_stack *stack, long count)
@@ -301,50 +287,12 @@ static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle,
301
287
 
302
288
  static void rvalue_stack_eagerly_release(VALUE handle)
303
289
  {
304
- rvalue_stack *stack;
305
- TypedData_Get_Struct(handle, rvalue_stack, &JSON_Parser_rvalue_stack_type, stack);
306
- RTYPEDDATA_DATA(handle) = NULL;
307
- rvalue_stack_free(stack);
308
- }
309
-
310
- /* unicode */
311
-
312
- static const signed char digit_values[256] = {
313
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
314
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
315
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,
316
- -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
317
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
318
- 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
319
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
320
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
321
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
322
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
323
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
324
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
325
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
326
- -1, -1, -1, -1, -1, -1, -1
327
- };
328
-
329
- static uint32_t unescape_unicode(const unsigned char *p)
330
- {
331
- const uint32_t replacement_char = 0xFFFD;
332
-
333
- signed char b;
334
- uint32_t result = 0;
335
- b = digit_values[p[0]];
336
- if (b < 0) return replacement_char;
337
- result = (result << 4) | (unsigned char)b;
338
- b = digit_values[p[1]];
339
- if (b < 0) return replacement_char;
340
- result = (result << 4) | (unsigned char)b;
341
- b = digit_values[p[2]];
342
- if (b < 0) return replacement_char;
343
- result = (result << 4) | (unsigned char)b;
344
- b = digit_values[p[3]];
345
- if (b < 0) return replacement_char;
346
- result = (result << 4) | (unsigned char)b;
347
- return result;
290
+ if (handle) {
291
+ rvalue_stack *stack;
292
+ TypedData_Get_Struct(handle, rvalue_stack, &JSON_Parser_rvalue_stack_type, stack);
293
+ RTYPEDDATA_DATA(handle) = NULL;
294
+ rvalue_stack_free(stack);
295
+ }
348
296
  }
349
297
 
350
298
  static int convert_UTF32_to_UTF8(char *buf, uint32_t ch)
@@ -373,1869 +321,265 @@ static int convert_UTF32_to_UTF8(char *buf, uint32_t ch)
373
321
  return len;
374
322
  }
375
323
 
324
+ enum duplicate_key_action {
325
+ JSON_DEPRECATED = 0,
326
+ JSON_IGNORE,
327
+ JSON_RAISE,
328
+ };
329
+
376
330
  typedef struct JSON_ParserStruct {
377
- VALUE Vsource;
378
- char *source;
379
- long len;
380
- char *memo;
381
- VALUE create_id;
382
- VALUE object_class;
383
- VALUE array_class;
331
+ VALUE on_load_proc;
384
332
  VALUE decimal_class;
385
- VALUE match_string;
386
- FBuffer fbuffer;
387
- int in_array;
333
+ ID decimal_method_id;
334
+ enum duplicate_key_action on_duplicate_key;
388
335
  int max_nesting;
389
336
  bool allow_nan;
390
337
  bool allow_trailing_comma;
391
- bool parsing_name;
338
+ bool allow_control_characters;
392
339
  bool symbolize_names;
393
340
  bool freeze;
394
- bool create_additions;
395
- bool deprecated_create_additions;
396
- rvalue_cache name_cache;
397
- rvalue_stack *stack;
398
- VALUE stack_handle;
399
- } JSON_Parser;
400
-
401
- #define GET_PARSER \
402
- GET_PARSER_INIT; \
403
- if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance")
341
+ } JSON_ParserConfig;
404
342
 
405
- #define GET_PARSER_INIT \
406
- JSON_Parser *json; \
407
- TypedData_Get_Struct(self, JSON_Parser, &JSON_Parser_type, json)
408
-
409
- #define MinusInfinity "-Infinity"
410
- #define EVIL 0x666
411
-
412
- static const rb_data_type_t JSON_Parser_type;
413
- static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result);
414
- static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
415
- static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
416
- static char *JSON_parse_number(JSON_Parser *json, char *p, char *pe, VALUE *result);
417
- static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
343
+ typedef struct JSON_ParserStateStruct {
344
+ VALUE stack_handle;
345
+ const char *start;
346
+ const char *cursor;
347
+ const char *end;
348
+ rvalue_stack *stack;
349
+ rvalue_cache name_cache;
350
+ int in_array;
351
+ int current_nesting;
352
+ } JSON_ParserState;
418
353
 
354
+ static inline size_t rest(JSON_ParserState *state) {
355
+ return state->end - state->cursor;
356
+ }
419
357
 
420
- #ifndef HAVE_STRNLEN
421
- static size_t strnlen(const char *s, size_t maxlen)
422
- {
423
- char *p;
424
- return ((p = memchr(s, '\0', maxlen)) ? p - s : maxlen);
358
+ static inline bool eos(JSON_ParserState *state) {
359
+ return state->cursor >= state->end;
425
360
  }
426
- #endif
427
361
 
428
- #define PARSE_ERROR_FRAGMENT_LEN 32
429
- #ifdef RBIMPL_ATTR_NORETURN
430
- RBIMPL_ATTR_NORETURN()
431
- #endif
432
- static void raise_parse_error(const char *format, const char *start)
362
+ static inline char peek(JSON_ParserState *state)
433
363
  {
434
- char buffer[PARSE_ERROR_FRAGMENT_LEN + 1];
435
-
436
- size_t len = strnlen(start, PARSE_ERROR_FRAGMENT_LEN);
437
- const char *ptr = start;
438
-
439
- if (len == PARSE_ERROR_FRAGMENT_LEN) {
440
- MEMCPY(buffer, start, char, PARSE_ERROR_FRAGMENT_LEN);
441
- buffer[PARSE_ERROR_FRAGMENT_LEN] = '\0';
442
- ptr = buffer;
364
+ if (RB_UNLIKELY(eos(state))) {
365
+ return 0;
443
366
  }
444
-
445
- rb_enc_raise(enc_utf8, rb_path2class("JSON::ParserError"), format, ptr);
367
+ return *state->cursor;
446
368
  }
447
369
 
448
-
449
-
450
- #line 473 "parser.rl"
451
-
452
-
453
-
454
- #line 455 "parser.c"
455
- enum {JSON_object_start = 1};
456
- enum {JSON_object_first_final = 32};
457
- enum {JSON_object_error = 0};
458
-
459
- enum {JSON_object_en_main = 1};
460
-
461
-
462
- #line 513 "parser.rl"
463
-
464
-
465
- #define PUSH(result) rvalue_stack_push(json->stack, result, &json->stack_handle, &json->stack)
466
-
467
- static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
370
+ static void cursor_position(JSON_ParserState *state, long *line_out, long *column_out)
468
371
  {
469
- int cs = EVIL;
470
-
471
- if (json->max_nesting && current_nesting > json->max_nesting) {
472
- rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
473
- }
372
+ const char *cursor = state->cursor;
373
+ long column = 0;
374
+ long line = 1;
474
375
 
475
- long stack_head = json->stack->head;
476
-
477
-
478
- #line 479 "parser.c"
479
- {
480
- cs = JSON_object_start;
481
- }
482
-
483
- #line 528 "parser.rl"
484
-
485
- #line 486 "parser.c"
486
- {
487
- short _widec;
488
- if ( p == pe )
489
- goto _test_eof;
490
- switch ( cs )
491
- {
492
- case 1:
493
- if ( (*p) == 123 )
494
- goto st2;
495
- goto st0;
496
- st0:
497
- cs = 0;
498
- goto _out;
499
- st2:
500
- if ( ++p == pe )
501
- goto _test_eof2;
502
- case 2:
503
- switch( (*p) ) {
504
- case 13: goto st2;
505
- case 32: goto st2;
506
- case 34: goto tr2;
507
- case 47: goto st28;
508
- case 125: goto tr4;
509
- }
510
- if ( 9 <= (*p) && (*p) <= 10 )
511
- goto st2;
512
- goto st0;
513
- tr2:
514
- #line 492 "parser.rl"
515
- {
516
- char *np;
517
- json->parsing_name = true;
518
- np = JSON_parse_string(json, p, pe, result);
519
- json->parsing_name = false;
520
- if (np == NULL) { p--; {p++; cs = 3; goto _out;} } else {
521
- PUSH(*result);
522
- {p = (( np))-1;}
523
- }
524
- }
525
- goto st3;
526
- st3:
527
- if ( ++p == pe )
528
- goto _test_eof3;
529
- case 3:
530
- #line 531 "parser.c"
531
- switch( (*p) ) {
532
- case 13: goto st3;
533
- case 32: goto st3;
534
- case 47: goto st4;
535
- case 58: goto st8;
536
- }
537
- if ( 9 <= (*p) && (*p) <= 10 )
538
- goto st3;
539
- goto st0;
540
- st4:
541
- if ( ++p == pe )
542
- goto _test_eof4;
543
- case 4:
544
- switch( (*p) ) {
545
- case 42: goto st5;
546
- case 47: goto st7;
547
- }
548
- goto st0;
549
- st5:
550
- if ( ++p == pe )
551
- goto _test_eof5;
552
- case 5:
553
- if ( (*p) == 42 )
554
- goto st6;
555
- goto st5;
556
- st6:
557
- if ( ++p == pe )
558
- goto _test_eof6;
559
- case 6:
560
- switch( (*p) ) {
561
- case 42: goto st6;
562
- case 47: goto st3;
563
- }
564
- goto st5;
565
- st7:
566
- if ( ++p == pe )
567
- goto _test_eof7;
568
- case 7:
569
- if ( (*p) == 10 )
570
- goto st3;
571
- goto st7;
572
- st8:
573
- if ( ++p == pe )
574
- goto _test_eof8;
575
- case 8:
576
- switch( (*p) ) {
577
- case 13: goto st8;
578
- case 32: goto st8;
579
- case 34: goto tr11;
580
- case 45: goto tr11;
581
- case 47: goto st24;
582
- case 73: goto tr11;
583
- case 78: goto tr11;
584
- case 91: goto tr11;
585
- case 102: goto tr11;
586
- case 110: goto tr11;
587
- case 116: goto tr11;
588
- case 123: goto tr11;
589
- }
590
- if ( (*p) > 10 ) {
591
- if ( 48 <= (*p) && (*p) <= 57 )
592
- goto tr11;
593
- } else if ( (*p) >= 9 )
594
- goto st8;
595
- goto st0;
596
- tr11:
597
- #line 481 "parser.rl"
598
- {
599
- char *np = JSON_parse_value(json, p, pe, result, current_nesting);
600
- if (np == NULL) {
601
- p--; {p++; cs = 9; goto _out;}
602
- } else {
603
- {p = (( np))-1;}
376
+ while (cursor >= state->start) {
377
+ if (*cursor-- == '\n') {
378
+ break;
604
379
  }
380
+ column++;
605
381
  }
606
- goto st9;
607
- st9:
608
- if ( ++p == pe )
609
- goto _test_eof9;
610
- case 9:
611
- #line 612 "parser.c"
612
- _widec = (*p);
613
- if ( (*p) < 13 ) {
614
- if ( (*p) > 9 ) {
615
- if ( 10 <= (*p) && (*p) <= 10 ) {
616
- _widec = (short)(128 + ((*p) - -128));
617
- if (
618
- #line 490 "parser.rl"
619
- json->allow_trailing_comma ) _widec += 256;
620
- }
621
- } else if ( (*p) >= 9 ) {
622
- _widec = (short)(128 + ((*p) - -128));
623
- if (
624
- #line 490 "parser.rl"
625
- json->allow_trailing_comma ) _widec += 256;
626
- }
627
- } else if ( (*p) > 13 ) {
628
- if ( (*p) < 44 ) {
629
- if ( 32 <= (*p) && (*p) <= 32 ) {
630
- _widec = (short)(128 + ((*p) - -128));
631
- if (
632
- #line 490 "parser.rl"
633
- json->allow_trailing_comma ) _widec += 256;
634
- }
635
- } else if ( (*p) > 44 ) {
636
- if ( 47 <= (*p) && (*p) <= 47 ) {
637
- _widec = (short)(128 + ((*p) - -128));
638
- if (
639
- #line 490 "parser.rl"
640
- json->allow_trailing_comma ) _widec += 256;
641
- }
642
- } else {
643
- _widec = (short)(128 + ((*p) - -128));
644
- if (
645
- #line 490 "parser.rl"
646
- json->allow_trailing_comma ) _widec += 256;
647
- }
648
- } else {
649
- _widec = (short)(128 + ((*p) - -128));
650
- if (
651
- #line 490 "parser.rl"
652
- json->allow_trailing_comma ) _widec += 256;
653
- }
654
- switch( _widec ) {
655
- case 125: goto tr4;
656
- case 269: goto st10;
657
- case 288: goto st10;
658
- case 300: goto st11;
659
- case 303: goto st16;
660
- case 525: goto st9;
661
- case 544: goto st9;
662
- case 556: goto st2;
663
- case 559: goto st20;
664
- }
665
- if ( _widec > 266 ) {
666
- if ( 521 <= _widec && _widec <= 522 )
667
- goto st9;
668
- } else if ( _widec >= 265 )
669
- goto st10;
670
- goto st0;
671
- tr4:
672
- #line 503 "parser.rl"
673
- { p--; {p++; cs = 32; goto _out;} }
674
- goto st32;
675
- st32:
676
- if ( ++p == pe )
677
- goto _test_eof32;
678
- case 32:
679
- #line 680 "parser.c"
680
- goto st0;
681
- st10:
682
- if ( ++p == pe )
683
- goto _test_eof10;
684
- case 10:
685
- switch( (*p) ) {
686
- case 13: goto st10;
687
- case 32: goto st10;
688
- case 44: goto st11;
689
- case 47: goto st16;
690
- case 125: goto tr4;
691
- }
692
- if ( 9 <= (*p) && (*p) <= 10 )
693
- goto st10;
694
- goto st0;
695
- st11:
696
- if ( ++p == pe )
697
- goto _test_eof11;
698
- case 11:
699
- switch( (*p) ) {
700
- case 13: goto st11;
701
- case 32: goto st11;
702
- case 34: goto tr2;
703
- case 47: goto st12;
704
- }
705
- if ( 9 <= (*p) && (*p) <= 10 )
706
- goto st11;
707
- goto st0;
708
- st12:
709
- if ( ++p == pe )
710
- goto _test_eof12;
711
- case 12:
712
- switch( (*p) ) {
713
- case 42: goto st13;
714
- case 47: goto st15;
715
- }
716
- goto st0;
717
- st13:
718
- if ( ++p == pe )
719
- goto _test_eof13;
720
- case 13:
721
- if ( (*p) == 42 )
722
- goto st14;
723
- goto st13;
724
- st14:
725
- if ( ++p == pe )
726
- goto _test_eof14;
727
- case 14:
728
- switch( (*p) ) {
729
- case 42: goto st14;
730
- case 47: goto st11;
731
- }
732
- goto st13;
733
- st15:
734
- if ( ++p == pe )
735
- goto _test_eof15;
736
- case 15:
737
- if ( (*p) == 10 )
738
- goto st11;
739
- goto st15;
740
- st16:
741
- if ( ++p == pe )
742
- goto _test_eof16;
743
- case 16:
744
- switch( (*p) ) {
745
- case 42: goto st17;
746
- case 47: goto st19;
747
- }
748
- goto st0;
749
- st17:
750
- if ( ++p == pe )
751
- goto _test_eof17;
752
- case 17:
753
- if ( (*p) == 42 )
754
- goto st18;
755
- goto st17;
756
- st18:
757
- if ( ++p == pe )
758
- goto _test_eof18;
759
- case 18:
760
- switch( (*p) ) {
761
- case 42: goto st18;
762
- case 47: goto st10;
763
- }
764
- goto st17;
765
- st19:
766
- if ( ++p == pe )
767
- goto _test_eof19;
768
- case 19:
769
- if ( (*p) == 10 )
770
- goto st10;
771
- goto st19;
772
- st20:
773
- if ( ++p == pe )
774
- goto _test_eof20;
775
- case 20:
776
- _widec = (*p);
777
- if ( (*p) > 42 ) {
778
- if ( 47 <= (*p) && (*p) <= 47 ) {
779
- _widec = (short)(128 + ((*p) - -128));
780
- if (
781
- #line 490 "parser.rl"
782
- json->allow_trailing_comma ) _widec += 256;
783
- }
784
- } else if ( (*p) >= 42 ) {
785
- _widec = (short)(128 + ((*p) - -128));
786
- if (
787
- #line 490 "parser.rl"
788
- json->allow_trailing_comma ) _widec += 256;
789
- }
790
- switch( _widec ) {
791
- case 298: goto st17;
792
- case 303: goto st19;
793
- case 554: goto st21;
794
- case 559: goto st23;
795
- }
796
- goto st0;
797
- st21:
798
- if ( ++p == pe )
799
- goto _test_eof21;
800
- case 21:
801
- _widec = (*p);
802
- if ( (*p) < 42 ) {
803
- if ( (*p) <= 41 ) {
804
- _widec = (short)(128 + ((*p) - -128));
805
- if (
806
- #line 490 "parser.rl"
807
- json->allow_trailing_comma ) _widec += 256;
808
- }
809
- } else if ( (*p) > 42 ) {
810
- if ( 43 <= (*p) )
811
- { _widec = (short)(128 + ((*p) - -128));
812
- if (
813
- #line 490 "parser.rl"
814
- json->allow_trailing_comma ) _widec += 256;
815
- }
816
- } else {
817
- _widec = (short)(128 + ((*p) - -128));
818
- if (
819
- #line 490 "parser.rl"
820
- json->allow_trailing_comma ) _widec += 256;
821
- }
822
- switch( _widec ) {
823
- case 298: goto st18;
824
- case 554: goto st22;
825
- }
826
- if ( _widec > 383 ) {
827
- if ( 384 <= _widec && _widec <= 639 )
828
- goto st21;
829
- } else if ( _widec >= 128 )
830
- goto st17;
831
- goto st0;
832
- st22:
833
- if ( ++p == pe )
834
- goto _test_eof22;
835
- case 22:
836
- _widec = (*p);
837
- if ( (*p) < 43 ) {
838
- if ( (*p) > 41 ) {
839
- if ( 42 <= (*p) && (*p) <= 42 ) {
840
- _widec = (short)(128 + ((*p) - -128));
841
- if (
842
- #line 490 "parser.rl"
843
- json->allow_trailing_comma ) _widec += 256;
844
- }
845
- } else {
846
- _widec = (short)(128 + ((*p) - -128));
847
- if (
848
- #line 490 "parser.rl"
849
- json->allow_trailing_comma ) _widec += 256;
850
- }
851
- } else if ( (*p) > 46 ) {
852
- if ( (*p) > 47 ) {
853
- if ( 48 <= (*p) )
854
- { _widec = (short)(128 + ((*p) - -128));
855
- if (
856
- #line 490 "parser.rl"
857
- json->allow_trailing_comma ) _widec += 256;
858
- }
859
- } else if ( (*p) >= 47 ) {
860
- _widec = (short)(128 + ((*p) - -128));
861
- if (
862
- #line 490 "parser.rl"
863
- json->allow_trailing_comma ) _widec += 256;
864
- }
865
- } else {
866
- _widec = (short)(128 + ((*p) - -128));
867
- if (
868
- #line 490 "parser.rl"
869
- json->allow_trailing_comma ) _widec += 256;
870
- }
871
- switch( _widec ) {
872
- case 298: goto st18;
873
- case 303: goto st10;
874
- case 554: goto st22;
875
- case 559: goto st9;
876
- }
877
- if ( _widec > 383 ) {
878
- if ( 384 <= _widec && _widec <= 639 )
879
- goto st21;
880
- } else if ( _widec >= 128 )
881
- goto st17;
882
- goto st0;
883
- st23:
884
- if ( ++p == pe )
885
- goto _test_eof23;
886
- case 23:
887
- _widec = (*p);
888
- if ( (*p) < 10 ) {
889
- if ( (*p) <= 9 ) {
890
- _widec = (short)(128 + ((*p) - -128));
891
- if (
892
- #line 490 "parser.rl"
893
- json->allow_trailing_comma ) _widec += 256;
894
- }
895
- } else if ( (*p) > 10 ) {
896
- if ( 11 <= (*p) )
897
- { _widec = (short)(128 + ((*p) - -128));
898
- if (
899
- #line 490 "parser.rl"
900
- json->allow_trailing_comma ) _widec += 256;
901
- }
902
- } else {
903
- _widec = (short)(128 + ((*p) - -128));
904
- if (
905
- #line 490 "parser.rl"
906
- json->allow_trailing_comma ) _widec += 256;
907
- }
908
- switch( _widec ) {
909
- case 266: goto st10;
910
- case 522: goto st9;
911
- }
912
- if ( _widec > 383 ) {
913
- if ( 384 <= _widec && _widec <= 639 )
914
- goto st23;
915
- } else if ( _widec >= 128 )
916
- goto st19;
917
- goto st0;
918
- st24:
919
- if ( ++p == pe )
920
- goto _test_eof24;
921
- case 24:
922
- switch( (*p) ) {
923
- case 42: goto st25;
924
- case 47: goto st27;
925
- }
926
- goto st0;
927
- st25:
928
- if ( ++p == pe )
929
- goto _test_eof25;
930
- case 25:
931
- if ( (*p) == 42 )
932
- goto st26;
933
- goto st25;
934
- st26:
935
- if ( ++p == pe )
936
- goto _test_eof26;
937
- case 26:
938
- switch( (*p) ) {
939
- case 42: goto st26;
940
- case 47: goto st8;
941
- }
942
- goto st25;
943
- st27:
944
- if ( ++p == pe )
945
- goto _test_eof27;
946
- case 27:
947
- if ( (*p) == 10 )
948
- goto st8;
949
- goto st27;
950
- st28:
951
- if ( ++p == pe )
952
- goto _test_eof28;
953
- case 28:
954
- switch( (*p) ) {
955
- case 42: goto st29;
956
- case 47: goto st31;
957
- }
958
- goto st0;
959
- st29:
960
- if ( ++p == pe )
961
- goto _test_eof29;
962
- case 29:
963
- if ( (*p) == 42 )
964
- goto st30;
965
- goto st29;
966
- st30:
967
- if ( ++p == pe )
968
- goto _test_eof30;
969
- case 30:
970
- switch( (*p) ) {
971
- case 42: goto st30;
972
- case 47: goto st2;
973
- }
974
- goto st29;
975
- st31:
976
- if ( ++p == pe )
977
- goto _test_eof31;
978
- case 31:
979
- if ( (*p) == 10 )
980
- goto st2;
981
- goto st31;
982
- }
983
- _test_eof2: cs = 2; goto _test_eof;
984
- _test_eof3: cs = 3; goto _test_eof;
985
- _test_eof4: cs = 4; goto _test_eof;
986
- _test_eof5: cs = 5; goto _test_eof;
987
- _test_eof6: cs = 6; goto _test_eof;
988
- _test_eof7: cs = 7; goto _test_eof;
989
- _test_eof8: cs = 8; goto _test_eof;
990
- _test_eof9: cs = 9; goto _test_eof;
991
- _test_eof32: cs = 32; goto _test_eof;
992
- _test_eof10: cs = 10; goto _test_eof;
993
- _test_eof11: cs = 11; goto _test_eof;
994
- _test_eof12: cs = 12; goto _test_eof;
995
- _test_eof13: cs = 13; goto _test_eof;
996
- _test_eof14: cs = 14; goto _test_eof;
997
- _test_eof15: cs = 15; goto _test_eof;
998
- _test_eof16: cs = 16; goto _test_eof;
999
- _test_eof17: cs = 17; goto _test_eof;
1000
- _test_eof18: cs = 18; goto _test_eof;
1001
- _test_eof19: cs = 19; goto _test_eof;
1002
- _test_eof20: cs = 20; goto _test_eof;
1003
- _test_eof21: cs = 21; goto _test_eof;
1004
- _test_eof22: cs = 22; goto _test_eof;
1005
- _test_eof23: cs = 23; goto _test_eof;
1006
- _test_eof24: cs = 24; goto _test_eof;
1007
- _test_eof25: cs = 25; goto _test_eof;
1008
- _test_eof26: cs = 26; goto _test_eof;
1009
- _test_eof27: cs = 27; goto _test_eof;
1010
- _test_eof28: cs = 28; goto _test_eof;
1011
- _test_eof29: cs = 29; goto _test_eof;
1012
- _test_eof30: cs = 30; goto _test_eof;
1013
- _test_eof31: cs = 31; goto _test_eof;
1014
-
1015
- _test_eof: {}
1016
- _out: {}
1017
- }
1018
-
1019
- #line 529 "parser.rl"
1020
-
1021
- if (cs >= JSON_object_first_final) {
1022
- long count = json->stack->head - stack_head;
1023
-
1024
- if (RB_UNLIKELY(json->object_class)) {
1025
- VALUE object = rb_class_new_instance(0, 0, json->object_class);
1026
- long index = 0;
1027
- VALUE *items = rvalue_stack_peek(json->stack, count);
1028
- while (index < count) {
1029
- VALUE name = items[index++];
1030
- VALUE value = items[index++];
1031
- rb_funcall(object, i_aset, 2, name, value);
1032
- }
1033
- *result = object;
1034
- } else {
1035
- VALUE hash;
1036
- #ifdef HAVE_RB_HASH_NEW_CAPA
1037
- hash = rb_hash_new_capa(count >> 1);
1038
- #else
1039
- hash = rb_hash_new();
1040
- #endif
1041
- rb_hash_bulk_insert(count, rvalue_stack_peek(json->stack, count), hash);
1042
- *result = hash;
1043
- }
1044
- rvalue_stack_pop(json->stack, count);
1045
382
 
1046
- if (RB_UNLIKELY(json->create_additions)) {
1047
- VALUE klassname;
1048
- if (json->object_class) {
1049
- klassname = rb_funcall(*result, i_aref, 1, json->create_id);
1050
- } else {
1051
- klassname = rb_hash_aref(*result, json->create_id);
1052
- }
1053
- if (!NIL_P(klassname)) {
1054
- VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
1055
- if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
1056
- if (json->deprecated_create_additions) {
1057
- json_deprecated(deprecated_create_additions_warning);
1058
- }
1059
- *result = rb_funcall(klass, i_json_create, 1, *result);
1060
- }
1061
- }
383
+ while (cursor >= state->start) {
384
+ if (*cursor-- == '\n') {
385
+ line++;
1062
386
  }
1063
- return p + 1;
1064
- } else {
1065
- return NULL;
1066
387
  }
388
+ *line_out = line;
389
+ *column_out = column;
1067
390
  }
1068
391
 
392
+ static void emit_parse_warning(const char *message, JSON_ParserState *state)
393
+ {
394
+ long line, column;
395
+ cursor_position(state, &line, &column);
1069
396
 
1070
- #line 1071 "parser.c"
1071
- enum {JSON_value_start = 1};
1072
- enum {JSON_value_first_final = 29};
1073
- enum {JSON_value_error = 0};
1074
-
1075
- enum {JSON_value_en_main = 1};
1076
-
1077
-
1078
- #line 662 "parser.rl"
397
+ VALUE warning = rb_sprintf("%s at line %ld column %ld", message, line, column);
398
+ rb_funcall(mJSON, rb_intern("deprecation_warning"), 1, warning);
399
+ }
1079
400
 
401
+ #define PARSE_ERROR_FRAGMENT_LEN 32
1080
402
 
1081
- static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
403
+ #ifdef RBIMPL_ATTR_NORETURN
404
+ RBIMPL_ATTR_NORETURN()
405
+ #endif
406
+ static void raise_parse_error(const char *format, JSON_ParserState *state)
1082
407
  {
1083
- int cs = EVIL;
1084
-
1085
-
1086
- #line 1087 "parser.c"
1087
- {
1088
- cs = JSON_value_start;
1089
- }
1090
-
1091
- #line 669 "parser.rl"
1092
-
1093
- #line 1094 "parser.c"
1094
- {
1095
- if ( p == pe )
1096
- goto _test_eof;
1097
- switch ( cs )
1098
- {
1099
- st1:
1100
- if ( ++p == pe )
1101
- goto _test_eof1;
1102
- case 1:
1103
- switch( (*p) ) {
1104
- case 13: goto st1;
1105
- case 32: goto st1;
1106
- case 34: goto tr2;
1107
- case 45: goto tr3;
1108
- case 47: goto st6;
1109
- case 73: goto st10;
1110
- case 78: goto st17;
1111
- case 91: goto tr7;
1112
- case 102: goto st19;
1113
- case 110: goto st23;
1114
- case 116: goto st26;
1115
- case 123: goto tr11;
1116
- }
1117
- if ( (*p) > 10 ) {
1118
- if ( 48 <= (*p) && (*p) <= 57 )
1119
- goto tr3;
1120
- } else if ( (*p) >= 9 )
1121
- goto st1;
1122
- goto st0;
1123
- st0:
1124
- cs = 0;
1125
- goto _out;
1126
- tr2:
1127
- #line 607 "parser.rl"
1128
- {
1129
- char *np = JSON_parse_string(json, p, pe, result);
1130
- if (np == NULL) {
1131
- p--;
1132
- {p++; cs = 29; goto _out;}
1133
- } else {
1134
- {p = (( np))-1;}
1135
- }
1136
- }
1137
- goto st29;
1138
- tr3:
1139
- #line 617 "parser.rl"
1140
- {
1141
- char *np;
1142
- if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
1143
- if (json->allow_nan) {
1144
- *result = CMinusInfinity;
1145
- {p = (( p + 10))-1;}
1146
- p--; {p++; cs = 29; goto _out;}
1147
- } else {
1148
- raise_parse_error("unexpected token at '%s'", p);
408
+ unsigned char buffer[PARSE_ERROR_FRAGMENT_LEN + 3];
409
+ long line, column;
410
+ cursor_position(state, &line, &column);
411
+
412
+ const char *ptr = "EOF";
413
+ if (state->cursor && state->cursor < state->end) {
414
+ ptr = state->cursor;
415
+ size_t len = 0;
416
+ while (len < PARSE_ERROR_FRAGMENT_LEN) {
417
+ char ch = ptr[len];
418
+ if (!ch || ch == '\n' || ch == ' ' || ch == '\t' || ch == '\r') {
419
+ break;
1149
420
  }
421
+ len++;
1150
422
  }
1151
- np = JSON_parse_number(json, p, pe, result);
1152
- if (np != NULL) {
1153
- {p = (( np))-1;}
1154
- }
1155
- p--; {p++; cs = 29; goto _out;}
1156
- }
1157
- goto st29;
1158
- tr7:
1159
- #line 635 "parser.rl"
1160
- {
1161
- char *np;
1162
- json->in_array++;
1163
- np = JSON_parse_array(json, p, pe, result, current_nesting + 1);
1164
- json->in_array--;
1165
- if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
1166
- }
1167
- goto st29;
1168
- tr11:
1169
- #line 643 "parser.rl"
1170
- {
1171
- char *np;
1172
- np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
1173
- if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
1174
- }
1175
- goto st29;
1176
- tr25:
1177
- #line 600 "parser.rl"
1178
- {
1179
- if (json->allow_nan) {
1180
- *result = CInfinity;
1181
- } else {
1182
- raise_parse_error("unexpected token at '%s'", p - 7);
1183
- }
1184
- }
1185
- goto st29;
1186
- tr27:
1187
- #line 593 "parser.rl"
1188
- {
1189
- if (json->allow_nan) {
1190
- *result = CNaN;
1191
- } else {
1192
- raise_parse_error("unexpected token at '%s'", p - 2);
1193
- }
1194
- }
1195
- goto st29;
1196
- tr31:
1197
- #line 587 "parser.rl"
1198
- {
1199
- *result = Qfalse;
1200
- }
1201
- goto st29;
1202
- tr34:
1203
- #line 584 "parser.rl"
1204
- {
1205
- *result = Qnil;
1206
- }
1207
- goto st29;
1208
- tr37:
1209
- #line 590 "parser.rl"
1210
- {
1211
- *result = Qtrue;
1212
- }
1213
- goto st29;
1214
- st29:
1215
- if ( ++p == pe )
1216
- goto _test_eof29;
1217
- case 29:
1218
- #line 649 "parser.rl"
1219
- { p--; {p++; cs = 29; goto _out;} }
1220
- #line 1221 "parser.c"
1221
- switch( (*p) ) {
1222
- case 13: goto st29;
1223
- case 32: goto st29;
1224
- case 47: goto st2;
1225
- }
1226
- if ( 9 <= (*p) && (*p) <= 10 )
1227
- goto st29;
1228
- goto st0;
1229
- st2:
1230
- if ( ++p == pe )
1231
- goto _test_eof2;
1232
- case 2:
1233
- switch( (*p) ) {
1234
- case 42: goto st3;
1235
- case 47: goto st5;
1236
- }
1237
- goto st0;
1238
- st3:
1239
- if ( ++p == pe )
1240
- goto _test_eof3;
1241
- case 3:
1242
- if ( (*p) == 42 )
1243
- goto st4;
1244
- goto st3;
1245
- st4:
1246
- if ( ++p == pe )
1247
- goto _test_eof4;
1248
- case 4:
1249
- switch( (*p) ) {
1250
- case 42: goto st4;
1251
- case 47: goto st29;
1252
- }
1253
- goto st3;
1254
- st5:
1255
- if ( ++p == pe )
1256
- goto _test_eof5;
1257
- case 5:
1258
- if ( (*p) == 10 )
1259
- goto st29;
1260
- goto st5;
1261
- st6:
1262
- if ( ++p == pe )
1263
- goto _test_eof6;
1264
- case 6:
1265
- switch( (*p) ) {
1266
- case 42: goto st7;
1267
- case 47: goto st9;
1268
- }
1269
- goto st0;
1270
- st7:
1271
- if ( ++p == pe )
1272
- goto _test_eof7;
1273
- case 7:
1274
- if ( (*p) == 42 )
1275
- goto st8;
1276
- goto st7;
1277
- st8:
1278
- if ( ++p == pe )
1279
- goto _test_eof8;
1280
- case 8:
1281
- switch( (*p) ) {
1282
- case 42: goto st8;
1283
- case 47: goto st1;
1284
- }
1285
- goto st7;
1286
- st9:
1287
- if ( ++p == pe )
1288
- goto _test_eof9;
1289
- case 9:
1290
- if ( (*p) == 10 )
1291
- goto st1;
1292
- goto st9;
1293
- st10:
1294
- if ( ++p == pe )
1295
- goto _test_eof10;
1296
- case 10:
1297
- if ( (*p) == 110 )
1298
- goto st11;
1299
- goto st0;
1300
- st11:
1301
- if ( ++p == pe )
1302
- goto _test_eof11;
1303
- case 11:
1304
- if ( (*p) == 102 )
1305
- goto st12;
1306
- goto st0;
1307
- st12:
1308
- if ( ++p == pe )
1309
- goto _test_eof12;
1310
- case 12:
1311
- if ( (*p) == 105 )
1312
- goto st13;
1313
- goto st0;
1314
- st13:
1315
- if ( ++p == pe )
1316
- goto _test_eof13;
1317
- case 13:
1318
- if ( (*p) == 110 )
1319
- goto st14;
1320
- goto st0;
1321
- st14:
1322
- if ( ++p == pe )
1323
- goto _test_eof14;
1324
- case 14:
1325
- if ( (*p) == 105 )
1326
- goto st15;
1327
- goto st0;
1328
- st15:
1329
- if ( ++p == pe )
1330
- goto _test_eof15;
1331
- case 15:
1332
- if ( (*p) == 116 )
1333
- goto st16;
1334
- goto st0;
1335
- st16:
1336
- if ( ++p == pe )
1337
- goto _test_eof16;
1338
- case 16:
1339
- if ( (*p) == 121 )
1340
- goto tr25;
1341
- goto st0;
1342
- st17:
1343
- if ( ++p == pe )
1344
- goto _test_eof17;
1345
- case 17:
1346
- if ( (*p) == 97 )
1347
- goto st18;
1348
- goto st0;
1349
- st18:
1350
- if ( ++p == pe )
1351
- goto _test_eof18;
1352
- case 18:
1353
- if ( (*p) == 78 )
1354
- goto tr27;
1355
- goto st0;
1356
- st19:
1357
- if ( ++p == pe )
1358
- goto _test_eof19;
1359
- case 19:
1360
- if ( (*p) == 97 )
1361
- goto st20;
1362
- goto st0;
1363
- st20:
1364
- if ( ++p == pe )
1365
- goto _test_eof20;
1366
- case 20:
1367
- if ( (*p) == 108 )
1368
- goto st21;
1369
- goto st0;
1370
- st21:
1371
- if ( ++p == pe )
1372
- goto _test_eof21;
1373
- case 21:
1374
- if ( (*p) == 115 )
1375
- goto st22;
1376
- goto st0;
1377
- st22:
1378
- if ( ++p == pe )
1379
- goto _test_eof22;
1380
- case 22:
1381
- if ( (*p) == 101 )
1382
- goto tr31;
1383
- goto st0;
1384
- st23:
1385
- if ( ++p == pe )
1386
- goto _test_eof23;
1387
- case 23:
1388
- if ( (*p) == 117 )
1389
- goto st24;
1390
- goto st0;
1391
- st24:
1392
- if ( ++p == pe )
1393
- goto _test_eof24;
1394
- case 24:
1395
- if ( (*p) == 108 )
1396
- goto st25;
1397
- goto st0;
1398
- st25:
1399
- if ( ++p == pe )
1400
- goto _test_eof25;
1401
- case 25:
1402
- if ( (*p) == 108 )
1403
- goto tr34;
1404
- goto st0;
1405
- st26:
1406
- if ( ++p == pe )
1407
- goto _test_eof26;
1408
- case 26:
1409
- if ( (*p) == 114 )
1410
- goto st27;
1411
- goto st0;
1412
- st27:
1413
- if ( ++p == pe )
1414
- goto _test_eof27;
1415
- case 27:
1416
- if ( (*p) == 117 )
1417
- goto st28;
1418
- goto st0;
1419
- st28:
1420
- if ( ++p == pe )
1421
- goto _test_eof28;
1422
- case 28:
1423
- if ( (*p) == 101 )
1424
- goto tr37;
1425
- goto st0;
1426
- }
1427
- _test_eof1: cs = 1; goto _test_eof;
1428
- _test_eof29: cs = 29; goto _test_eof;
1429
- _test_eof2: cs = 2; goto _test_eof;
1430
- _test_eof3: cs = 3; goto _test_eof;
1431
- _test_eof4: cs = 4; goto _test_eof;
1432
- _test_eof5: cs = 5; goto _test_eof;
1433
- _test_eof6: cs = 6; goto _test_eof;
1434
- _test_eof7: cs = 7; goto _test_eof;
1435
- _test_eof8: cs = 8; goto _test_eof;
1436
- _test_eof9: cs = 9; goto _test_eof;
1437
- _test_eof10: cs = 10; goto _test_eof;
1438
- _test_eof11: cs = 11; goto _test_eof;
1439
- _test_eof12: cs = 12; goto _test_eof;
1440
- _test_eof13: cs = 13; goto _test_eof;
1441
- _test_eof14: cs = 14; goto _test_eof;
1442
- _test_eof15: cs = 15; goto _test_eof;
1443
- _test_eof16: cs = 16; goto _test_eof;
1444
- _test_eof17: cs = 17; goto _test_eof;
1445
- _test_eof18: cs = 18; goto _test_eof;
1446
- _test_eof19: cs = 19; goto _test_eof;
1447
- _test_eof20: cs = 20; goto _test_eof;
1448
- _test_eof21: cs = 21; goto _test_eof;
1449
- _test_eof22: cs = 22; goto _test_eof;
1450
- _test_eof23: cs = 23; goto _test_eof;
1451
- _test_eof24: cs = 24; goto _test_eof;
1452
- _test_eof25: cs = 25; goto _test_eof;
1453
- _test_eof26: cs = 26; goto _test_eof;
1454
- _test_eof27: cs = 27; goto _test_eof;
1455
- _test_eof28: cs = 28; goto _test_eof;
1456
-
1457
- _test_eof: {}
1458
- _out: {}
1459
- }
1460
-
1461
- #line 670 "parser.rl"
1462
-
1463
- if (json->freeze) {
1464
- OBJ_FREEZE(*result);
1465
- }
1466
-
1467
- if (cs >= JSON_value_first_final) {
1468
- PUSH(*result);
1469
- return p;
1470
- } else {
1471
- return NULL;
1472
- }
1473
- }
1474
423
 
424
+ if (len) {
425
+ buffer[0] = '\'';
426
+ MEMCPY(buffer + 1, ptr, char, len);
1475
427
 
1476
- #line 1477 "parser.c"
1477
- enum {JSON_integer_start = 1};
1478
- enum {JSON_integer_first_final = 3};
1479
- enum {JSON_integer_error = 0};
428
+ while (buffer[len] >= 0x80 && buffer[len] < 0xC0) { // Is continuation byte
429
+ len--;
430
+ }
1480
431
 
1481
- enum {JSON_integer_en_main = 1};
432
+ if (buffer[len] >= 0xC0) { // multibyte character start
433
+ len--;
434
+ }
1482
435
 
436
+ buffer[len + 1] = '\'';
437
+ buffer[len + 2] = '\0';
438
+ ptr = (const char *)buffer;
439
+ }
440
+ }
1483
441
 
1484
- #line 691 "parser.rl"
442
+ VALUE msg = rb_sprintf(format, ptr);
443
+ VALUE message = rb_enc_sprintf(enc_utf8, "%s at line %ld column %ld", RSTRING_PTR(msg), line, column);
444
+ RB_GC_GUARD(msg);
1485
445
 
446
+ VALUE exc = rb_exc_new_str(rb_path2class("JSON::ParserError"), message);
447
+ rb_ivar_set(exc, rb_intern("@line"), LONG2NUM(line));
448
+ rb_ivar_set(exc, rb_intern("@column"), LONG2NUM(column));
449
+ rb_exc_raise(exc);
450
+ }
1486
451
 
1487
- #define MAX_FAST_INTEGER_SIZE 18
1488
- static inline VALUE fast_parse_integer(char *p, char *pe)
452
+ #ifdef RBIMPL_ATTR_NORETURN
453
+ RBIMPL_ATTR_NORETURN()
454
+ #endif
455
+ static void raise_parse_error_at(const char *format, JSON_ParserState *state, const char *at)
1489
456
  {
1490
- bool negative = false;
1491
- if (*p == '-') {
1492
- negative = true;
1493
- p++;
1494
- }
457
+ state->cursor = at;
458
+ raise_parse_error(format, state);
459
+ }
1495
460
 
1496
- long long memo = 0;
1497
- while (p < pe) {
1498
- memo *= 10;
1499
- memo += *p - '0';
1500
- p++;
1501
- }
461
+ /* unicode */
1502
462
 
1503
- if (negative) {
1504
- memo = -memo;
1505
- }
1506
- return LL2NUM(memo);
1507
- }
463
+ static const signed char digit_values[256] = {
464
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
465
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
466
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,
467
+ -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
468
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
469
+ 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
470
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
471
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
472
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
473
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
474
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
475
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
476
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
477
+ -1, -1, -1, -1, -1, -1, -1
478
+ };
1508
479
 
1509
- static char *JSON_decode_integer(JSON_Parser *json, char *p, VALUE *result)
480
+ static uint32_t unescape_unicode(JSON_ParserState *state, const char *sp, const char *spe)
1510
481
  {
1511
- long len = p - json->memo;
1512
- if (RB_LIKELY(len < MAX_FAST_INTEGER_SIZE)) {
1513
- *result = fast_parse_integer(json->memo, p);
1514
- } else {
1515
- fbuffer_clear(&json->fbuffer);
1516
- fbuffer_append(&json->fbuffer, json->memo, len);
1517
- fbuffer_append_char(&json->fbuffer, '\0');
1518
- *result = rb_cstr2inum(FBUFFER_PTR(&json->fbuffer), 10);
1519
- }
1520
- return p + 1;
1521
- }
482
+ if (RB_UNLIKELY(sp > spe - 4)) {
483
+ raise_parse_error_at("incomplete unicode character escape sequence at %s", state, sp - 2);
484
+ }
1522
485
 
486
+ const unsigned char *p = (const unsigned char *)sp;
1523
487
 
1524
- #line 1525 "parser.c"
1525
- enum {JSON_float_start = 1};
1526
- enum {JSON_float_first_final = 6};
1527
- enum {JSON_float_error = 0};
488
+ const signed char b0 = digit_values[p[0]];
489
+ const signed char b1 = digit_values[p[1]];
490
+ const signed char b2 = digit_values[p[2]];
491
+ const signed char b3 = digit_values[p[3]];
1528
492
 
1529
- enum {JSON_float_en_main = 1};
493
+ if (RB_UNLIKELY((signed char)(b0 | b1 | b2 | b3) < 0)) {
494
+ raise_parse_error_at("incomplete unicode character escape sequence at %s", state, sp - 2);
495
+ }
1530
496
 
497
+ return ((uint32_t)b0 << 12) | ((uint32_t)b1 << 8) | ((uint32_t)b2 << 4) | (uint32_t)b3;
498
+ }
1531
499
 
1532
- #line 743 "parser.rl"
500
+ #define GET_PARSER_CONFIG \
501
+ JSON_ParserConfig *config; \
502
+ TypedData_Get_Struct(self, JSON_ParserConfig, &JSON_ParserConfig_type, config)
1533
503
 
504
+ static const rb_data_type_t JSON_ParserConfig_type;
1534
505
 
1535
- static char *JSON_parse_number(JSON_Parser *json, char *p, char *pe, VALUE *result)
506
+ static void
507
+ json_eat_comments(JSON_ParserState *state)
1536
508
  {
1537
- int cs = EVIL;
1538
- bool is_float = false;
1539
-
1540
-
1541
- #line 1542 "parser.c"
1542
- {
1543
- cs = JSON_float_start;
1544
- }
1545
-
1546
- #line 751 "parser.rl"
1547
- json->memo = p;
1548
-
1549
- #line 1550 "parser.c"
1550
- {
1551
- if ( p == pe )
1552
- goto _test_eof;
1553
- switch ( cs )
1554
- {
1555
- case 1:
1556
- switch( (*p) ) {
1557
- case 45: goto st2;
1558
- case 48: goto st6;
1559
- }
1560
- if ( 49 <= (*p) && (*p) <= 57 )
1561
- goto st10;
1562
- goto st0;
1563
- st0:
1564
- cs = 0;
1565
- goto _out;
1566
- st2:
1567
- if ( ++p == pe )
1568
- goto _test_eof2;
1569
- case 2:
1570
- if ( (*p) == 48 )
1571
- goto st6;
1572
- if ( 49 <= (*p) && (*p) <= 57 )
1573
- goto st10;
1574
- goto st0;
1575
- st6:
1576
- if ( ++p == pe )
1577
- goto _test_eof6;
1578
- case 6:
1579
- switch( (*p) ) {
1580
- case 45: goto st0;
1581
- case 46: goto tr8;
1582
- case 69: goto tr9;
1583
- case 101: goto tr9;
1584
- }
1585
- if ( 48 <= (*p) && (*p) <= 57 )
1586
- goto st0;
1587
- goto tr7;
1588
- tr7:
1589
- #line 735 "parser.rl"
1590
- { p--; {p++; cs = 7; goto _out;} }
1591
- goto st7;
1592
- st7:
1593
- if ( ++p == pe )
1594
- goto _test_eof7;
1595
- case 7:
1596
- #line 1597 "parser.c"
1597
- goto st0;
1598
- tr8:
1599
- #line 736 "parser.rl"
1600
- { is_float = true; }
1601
- goto st3;
1602
- st3:
1603
- if ( ++p == pe )
1604
- goto _test_eof3;
1605
- case 3:
1606
- #line 1607 "parser.c"
1607
- if ( 48 <= (*p) && (*p) <= 57 )
1608
- goto st8;
1609
- goto st0;
1610
- st8:
1611
- if ( ++p == pe )
1612
- goto _test_eof8;
1613
- case 8:
1614
- switch( (*p) ) {
1615
- case 69: goto st4;
1616
- case 101: goto st4;
1617
- }
1618
- if ( (*p) > 46 ) {
1619
- if ( 48 <= (*p) && (*p) <= 57 )
1620
- goto st8;
1621
- } else if ( (*p) >= 45 )
1622
- goto st0;
1623
- goto tr7;
1624
- tr9:
1625
- #line 736 "parser.rl"
1626
- { is_float = true; }
1627
- goto st4;
1628
- st4:
1629
- if ( ++p == pe )
1630
- goto _test_eof4;
1631
- case 4:
1632
- #line 1633 "parser.c"
1633
- switch( (*p) ) {
1634
- case 43: goto st5;
1635
- case 45: goto st5;
1636
- }
1637
- if ( 48 <= (*p) && (*p) <= 57 )
1638
- goto st9;
1639
- goto st0;
1640
- st5:
1641
- if ( ++p == pe )
1642
- goto _test_eof5;
1643
- case 5:
1644
- if ( 48 <= (*p) && (*p) <= 57 )
1645
- goto st9;
1646
- goto st0;
1647
- st9:
1648
- if ( ++p == pe )
1649
- goto _test_eof9;
1650
- case 9:
1651
- switch( (*p) ) {
1652
- case 69: goto st0;
1653
- case 101: goto st0;
1654
- }
1655
- if ( (*p) > 46 ) {
1656
- if ( 48 <= (*p) && (*p) <= 57 )
1657
- goto st9;
1658
- } else if ( (*p) >= 45 )
1659
- goto st0;
1660
- goto tr7;
1661
- st10:
1662
- if ( ++p == pe )
1663
- goto _test_eof10;
1664
- case 10:
1665
- switch( (*p) ) {
1666
- case 45: goto st0;
1667
- case 46: goto tr8;
1668
- case 69: goto tr9;
1669
- case 101: goto tr9;
1670
- }
1671
- if ( 48 <= (*p) && (*p) <= 57 )
1672
- goto st10;
1673
- goto tr7;
1674
- }
1675
- _test_eof2: cs = 2; goto _test_eof;
1676
- _test_eof6: cs = 6; goto _test_eof;
1677
- _test_eof7: cs = 7; goto _test_eof;
1678
- _test_eof3: cs = 3; goto _test_eof;
1679
- _test_eof8: cs = 8; goto _test_eof;
1680
- _test_eof4: cs = 4; goto _test_eof;
1681
- _test_eof5: cs = 5; goto _test_eof;
1682
- _test_eof9: cs = 9; goto _test_eof;
1683
- _test_eof10: cs = 10; goto _test_eof;
1684
-
1685
- _test_eof: {}
1686
- _out: {}
1687
- }
1688
-
1689
- #line 753 "parser.rl"
1690
-
1691
- if (cs >= JSON_float_first_final) {
1692
- if (!is_float) {
1693
- return JSON_decode_integer(json, p, result);
1694
- }
1695
- VALUE mod = Qnil;
1696
- ID method_id = 0;
1697
- if (json->decimal_class) {
1698
- if (rb_respond_to(json->decimal_class, i_try_convert)) {
1699
- mod = json->decimal_class;
1700
- method_id = i_try_convert;
1701
- } else if (rb_respond_to(json->decimal_class, i_new)) {
1702
- mod = json->decimal_class;
1703
- method_id = i_new;
1704
- } else if (RB_TYPE_P(json->decimal_class, T_CLASS)) {
1705
- VALUE name = rb_class_name(json->decimal_class);
1706
- const char *name_cstr = RSTRING_PTR(name);
1707
- const char *last_colon = strrchr(name_cstr, ':');
1708
- if (last_colon) {
1709
- const char *mod_path_end = last_colon - 1;
1710
- VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
1711
- mod = rb_path_to_class(mod_path);
1712
-
1713
- const char *method_name_beg = last_colon + 1;
1714
- long before_len = method_name_beg - name_cstr;
1715
- long len = RSTRING_LEN(name) - before_len;
1716
- VALUE method_name = rb_str_substr(name, before_len, len);
1717
- method_id = SYM2ID(rb_str_intern(method_name));
1718
- } else {
1719
- mod = rb_mKernel;
1720
- method_id = SYM2ID(rb_str_intern(name));
1721
- }
509
+ const char *start = state->cursor;
510
+ state->cursor++;
511
+
512
+ switch (peek(state)) {
513
+ case '/': {
514
+ state->cursor = memchr(state->cursor, '\n', state->end - state->cursor);
515
+ if (!state->cursor) {
516
+ state->cursor = state->end;
517
+ } else {
518
+ state->cursor++;
1722
519
  }
520
+ break;
1723
521
  }
522
+ case '*': {
523
+ state->cursor++;
1724
524
 
1725
- long len = p - json->memo;
1726
- fbuffer_clear(&json->fbuffer);
1727
- fbuffer_append(&json->fbuffer, json->memo, len);
1728
- fbuffer_append_char(&json->fbuffer, '\0');
525
+ while (true) {
526
+ const char *next_match = memchr(state->cursor, '*', state->end - state->cursor);
527
+ if (!next_match) {
528
+ raise_parse_error_at("unterminated comment, expected closing '*/'", state, start);
529
+ }
1729
530
 
1730
- if (method_id) {
1731
- VALUE text = rb_str_new2(FBUFFER_PTR(&json->fbuffer));
1732
- *result = rb_funcallv(mod, method_id, 1, &text);
1733
- } else {
1734
- *result = DBL2NUM(rb_cstr_to_dbl(FBUFFER_PTR(&json->fbuffer), 1));
531
+ state->cursor = next_match + 1;
532
+ if (peek(state) == '/') {
533
+ state->cursor++;
534
+ break;
535
+ }
536
+ }
537
+ break;
1735
538
  }
1736
-
1737
- return p + 1;
1738
- } else {
1739
- return NULL;
539
+ default:
540
+ raise_parse_error_at("unexpected token %s", state, start);
541
+ break;
1740
542
  }
1741
543
  }
1742
544
 
1743
-
1744
-
1745
- #line 1746 "parser.c"
1746
- enum {JSON_array_start = 1};
1747
- enum {JSON_array_first_final = 22};
1748
- enum {JSON_array_error = 0};
1749
-
1750
- enum {JSON_array_en_main = 1};
1751
-
1752
-
1753
- #line 833 "parser.rl"
1754
-
1755
-
1756
- static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
545
+ ALWAYS_INLINE(static) void
546
+ json_eat_whitespace(JSON_ParserState *state)
1757
547
  {
1758
- int cs = EVIL;
548
+ while (true) {
549
+ switch (peek(state)) {
550
+ case ' ':
551
+ state->cursor++;
552
+ break;
553
+ case '\n':
554
+ state->cursor++;
555
+
556
+ // Heuristic: if we see a newline, there is likely consecutive spaces after it.
557
+ #if JSON_CPU_LITTLE_ENDIAN_64BITS
558
+ while (rest(state) > 8) {
559
+ uint64_t chunk;
560
+ memcpy(&chunk, state->cursor, sizeof(uint64_t));
561
+ if (chunk == 0x2020202020202020) {
562
+ state->cursor += 8;
563
+ continue;
564
+ }
1759
565
 
1760
- if (json->max_nesting && current_nesting > json->max_nesting) {
1761
- rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
1762
- }
1763
- long stack_head = json->stack->head;
1764
-
1765
-
1766
- #line 1767 "parser.c"
1767
- {
1768
- cs = JSON_array_start;
1769
- }
1770
-
1771
- #line 845 "parser.rl"
1772
-
1773
- #line 1774 "parser.c"
1774
- {
1775
- short _widec;
1776
- if ( p == pe )
1777
- goto _test_eof;
1778
- switch ( cs )
1779
- {
1780
- case 1:
1781
- if ( (*p) == 91 )
1782
- goto st2;
1783
- goto st0;
1784
- st0:
1785
- cs = 0;
1786
- goto _out;
1787
- st2:
1788
- if ( ++p == pe )
1789
- goto _test_eof2;
1790
- case 2:
1791
- switch( (*p) ) {
1792
- case 13: goto st2;
1793
- case 32: goto st2;
1794
- case 34: goto tr2;
1795
- case 45: goto tr2;
1796
- case 47: goto st18;
1797
- case 73: goto tr2;
1798
- case 78: goto tr2;
1799
- case 91: goto tr2;
1800
- case 93: goto tr4;
1801
- case 102: goto tr2;
1802
- case 110: goto tr2;
1803
- case 116: goto tr2;
1804
- case 123: goto tr2;
1805
- }
1806
- if ( (*p) > 10 ) {
1807
- if ( 48 <= (*p) && (*p) <= 57 )
1808
- goto tr2;
1809
- } else if ( (*p) >= 9 )
1810
- goto st2;
1811
- goto st0;
1812
- tr2:
1813
- #line 813 "parser.rl"
1814
- {
1815
- VALUE v = Qnil;
1816
- char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
1817
- if (np == NULL) {
1818
- p--; {p++; cs = 3; goto _out;}
1819
- } else {
1820
- {p = (( np))-1;}
1821
- }
1822
- }
1823
- goto st3;
1824
- st3:
1825
- if ( ++p == pe )
1826
- goto _test_eof3;
1827
- case 3:
1828
- #line 1829 "parser.c"
1829
- _widec = (*p);
1830
- if ( 44 <= (*p) && (*p) <= 44 ) {
1831
- _widec = (short)(128 + ((*p) - -128));
1832
- if (
1833
- #line 823 "parser.rl"
1834
- json->allow_trailing_comma ) _widec += 256;
1835
- }
1836
- switch( _widec ) {
1837
- case 13: goto st3;
1838
- case 32: goto st3;
1839
- case 47: goto st4;
1840
- case 93: goto tr4;
1841
- case 300: goto st8;
1842
- case 556: goto st13;
1843
- }
1844
- if ( 9 <= _widec && _widec <= 10 )
1845
- goto st3;
1846
- goto st0;
1847
- st4:
1848
- if ( ++p == pe )
1849
- goto _test_eof4;
1850
- case 4:
1851
- switch( (*p) ) {
1852
- case 42: goto st5;
1853
- case 47: goto st7;
1854
- }
1855
- goto st0;
1856
- st5:
1857
- if ( ++p == pe )
1858
- goto _test_eof5;
1859
- case 5:
1860
- if ( (*p) == 42 )
1861
- goto st6;
1862
- goto st5;
1863
- st6:
1864
- if ( ++p == pe )
1865
- goto _test_eof6;
1866
- case 6:
1867
- switch( (*p) ) {
1868
- case 42: goto st6;
1869
- case 47: goto st3;
1870
- }
1871
- goto st5;
1872
- st7:
1873
- if ( ++p == pe )
1874
- goto _test_eof7;
1875
- case 7:
1876
- if ( (*p) == 10 )
1877
- goto st3;
1878
- goto st7;
1879
- tr4:
1880
- #line 825 "parser.rl"
1881
- { p--; {p++; cs = 22; goto _out;} }
1882
- goto st22;
1883
- st22:
1884
- if ( ++p == pe )
1885
- goto _test_eof22;
1886
- case 22:
1887
- #line 1888 "parser.c"
1888
- goto st0;
1889
- st8:
1890
- if ( ++p == pe )
1891
- goto _test_eof8;
1892
- case 8:
1893
- switch( (*p) ) {
1894
- case 13: goto st8;
1895
- case 32: goto st8;
1896
- case 34: goto tr2;
1897
- case 45: goto tr2;
1898
- case 47: goto st9;
1899
- case 73: goto tr2;
1900
- case 78: goto tr2;
1901
- case 91: goto tr2;
1902
- case 102: goto tr2;
1903
- case 110: goto tr2;
1904
- case 116: goto tr2;
1905
- case 123: goto tr2;
1906
- }
1907
- if ( (*p) > 10 ) {
1908
- if ( 48 <= (*p) && (*p) <= 57 )
1909
- goto tr2;
1910
- } else if ( (*p) >= 9 )
1911
- goto st8;
1912
- goto st0;
1913
- st9:
1914
- if ( ++p == pe )
1915
- goto _test_eof9;
1916
- case 9:
1917
- switch( (*p) ) {
1918
- case 42: goto st10;
1919
- case 47: goto st12;
1920
- }
1921
- goto st0;
1922
- st10:
1923
- if ( ++p == pe )
1924
- goto _test_eof10;
1925
- case 10:
1926
- if ( (*p) == 42 )
1927
- goto st11;
1928
- goto st10;
1929
- st11:
1930
- if ( ++p == pe )
1931
- goto _test_eof11;
1932
- case 11:
1933
- switch( (*p) ) {
1934
- case 42: goto st11;
1935
- case 47: goto st8;
1936
- }
1937
- goto st10;
1938
- st12:
1939
- if ( ++p == pe )
1940
- goto _test_eof12;
1941
- case 12:
1942
- if ( (*p) == 10 )
1943
- goto st8;
1944
- goto st12;
1945
- st13:
1946
- if ( ++p == pe )
1947
- goto _test_eof13;
1948
- case 13:
1949
- _widec = (*p);
1950
- if ( (*p) < 13 ) {
1951
- if ( (*p) > 9 ) {
1952
- if ( 10 <= (*p) && (*p) <= 10 ) {
1953
- _widec = (short)(128 + ((*p) - -128));
1954
- if (
1955
- #line 823 "parser.rl"
1956
- json->allow_trailing_comma ) _widec += 256;
1957
- }
1958
- } else if ( (*p) >= 9 ) {
1959
- _widec = (short)(128 + ((*p) - -128));
1960
- if (
1961
- #line 823 "parser.rl"
1962
- json->allow_trailing_comma ) _widec += 256;
1963
- }
1964
- } else if ( (*p) > 13 ) {
1965
- if ( (*p) > 32 ) {
1966
- if ( 47 <= (*p) && (*p) <= 47 ) {
1967
- _widec = (short)(128 + ((*p) - -128));
1968
- if (
1969
- #line 823 "parser.rl"
1970
- json->allow_trailing_comma ) _widec += 256;
1971
- }
1972
- } else if ( (*p) >= 32 ) {
1973
- _widec = (short)(128 + ((*p) - -128));
1974
- if (
1975
- #line 823 "parser.rl"
1976
- json->allow_trailing_comma ) _widec += 256;
1977
- }
1978
- } else {
1979
- _widec = (short)(128 + ((*p) - -128));
1980
- if (
1981
- #line 823 "parser.rl"
1982
- json->allow_trailing_comma ) _widec += 256;
1983
- }
1984
- switch( _widec ) {
1985
- case 34: goto tr2;
1986
- case 45: goto tr2;
1987
- case 73: goto tr2;
1988
- case 78: goto tr2;
1989
- case 91: goto tr2;
1990
- case 93: goto tr4;
1991
- case 102: goto tr2;
1992
- case 110: goto tr2;
1993
- case 116: goto tr2;
1994
- case 123: goto tr2;
1995
- case 269: goto st8;
1996
- case 288: goto st8;
1997
- case 303: goto st9;
1998
- case 525: goto st13;
1999
- case 544: goto st13;
2000
- case 559: goto st14;
2001
- }
2002
- if ( _widec < 265 ) {
2003
- if ( 48 <= _widec && _widec <= 57 )
2004
- goto tr2;
2005
- } else if ( _widec > 266 ) {
2006
- if ( 521 <= _widec && _widec <= 522 )
2007
- goto st13;
2008
- } else
2009
- goto st8;
2010
- goto st0;
2011
- st14:
2012
- if ( ++p == pe )
2013
- goto _test_eof14;
2014
- case 14:
2015
- _widec = (*p);
2016
- if ( (*p) > 42 ) {
2017
- if ( 47 <= (*p) && (*p) <= 47 ) {
2018
- _widec = (short)(128 + ((*p) - -128));
2019
- if (
2020
- #line 823 "parser.rl"
2021
- json->allow_trailing_comma ) _widec += 256;
2022
- }
2023
- } else if ( (*p) >= 42 ) {
2024
- _widec = (short)(128 + ((*p) - -128));
2025
- if (
2026
- #line 823 "parser.rl"
2027
- json->allow_trailing_comma ) _widec += 256;
2028
- }
2029
- switch( _widec ) {
2030
- case 298: goto st10;
2031
- case 303: goto st12;
2032
- case 554: goto st15;
2033
- case 559: goto st17;
2034
- }
2035
- goto st0;
2036
- st15:
2037
- if ( ++p == pe )
2038
- goto _test_eof15;
2039
- case 15:
2040
- _widec = (*p);
2041
- if ( (*p) < 42 ) {
2042
- if ( (*p) <= 41 ) {
2043
- _widec = (short)(128 + ((*p) - -128));
2044
- if (
2045
- #line 823 "parser.rl"
2046
- json->allow_trailing_comma ) _widec += 256;
2047
- }
2048
- } else if ( (*p) > 42 ) {
2049
- if ( 43 <= (*p) )
2050
- { _widec = (short)(128 + ((*p) - -128));
2051
- if (
2052
- #line 823 "parser.rl"
2053
- json->allow_trailing_comma ) _widec += 256;
2054
- }
2055
- } else {
2056
- _widec = (short)(128 + ((*p) - -128));
2057
- if (
2058
- #line 823 "parser.rl"
2059
- json->allow_trailing_comma ) _widec += 256;
2060
- }
2061
- switch( _widec ) {
2062
- case 298: goto st11;
2063
- case 554: goto st16;
2064
- }
2065
- if ( _widec > 383 ) {
2066
- if ( 384 <= _widec && _widec <= 639 )
2067
- goto st15;
2068
- } else if ( _widec >= 128 )
2069
- goto st10;
2070
- goto st0;
2071
- st16:
2072
- if ( ++p == pe )
2073
- goto _test_eof16;
2074
- case 16:
2075
- _widec = (*p);
2076
- if ( (*p) < 43 ) {
2077
- if ( (*p) > 41 ) {
2078
- if ( 42 <= (*p) && (*p) <= 42 ) {
2079
- _widec = (short)(128 + ((*p) - -128));
2080
- if (
2081
- #line 823 "parser.rl"
2082
- json->allow_trailing_comma ) _widec += 256;
2083
- }
2084
- } else {
2085
- _widec = (short)(128 + ((*p) - -128));
2086
- if (
2087
- #line 823 "parser.rl"
2088
- json->allow_trailing_comma ) _widec += 256;
2089
- }
2090
- } else if ( (*p) > 46 ) {
2091
- if ( (*p) > 47 ) {
2092
- if ( 48 <= (*p) )
2093
- { _widec = (short)(128 + ((*p) - -128));
2094
- if (
2095
- #line 823 "parser.rl"
2096
- json->allow_trailing_comma ) _widec += 256;
2097
- }
2098
- } else if ( (*p) >= 47 ) {
2099
- _widec = (short)(128 + ((*p) - -128));
2100
- if (
2101
- #line 823 "parser.rl"
2102
- json->allow_trailing_comma ) _widec += 256;
2103
- }
2104
- } else {
2105
- _widec = (short)(128 + ((*p) - -128));
2106
- if (
2107
- #line 823 "parser.rl"
2108
- json->allow_trailing_comma ) _widec += 256;
2109
- }
2110
- switch( _widec ) {
2111
- case 298: goto st11;
2112
- case 303: goto st8;
2113
- case 554: goto st16;
2114
- case 559: goto st13;
2115
- }
2116
- if ( _widec > 383 ) {
2117
- if ( 384 <= _widec && _widec <= 639 )
2118
- goto st15;
2119
- } else if ( _widec >= 128 )
2120
- goto st10;
2121
- goto st0;
2122
- st17:
2123
- if ( ++p == pe )
2124
- goto _test_eof17;
2125
- case 17:
2126
- _widec = (*p);
2127
- if ( (*p) < 10 ) {
2128
- if ( (*p) <= 9 ) {
2129
- _widec = (short)(128 + ((*p) - -128));
2130
- if (
2131
- #line 823 "parser.rl"
2132
- json->allow_trailing_comma ) _widec += 256;
2133
- }
2134
- } else if ( (*p) > 10 ) {
2135
- if ( 11 <= (*p) )
2136
- { _widec = (short)(128 + ((*p) - -128));
2137
- if (
2138
- #line 823 "parser.rl"
2139
- json->allow_trailing_comma ) _widec += 256;
2140
- }
2141
- } else {
2142
- _widec = (short)(128 + ((*p) - -128));
2143
- if (
2144
- #line 823 "parser.rl"
2145
- json->allow_trailing_comma ) _widec += 256;
2146
- }
2147
- switch( _widec ) {
2148
- case 266: goto st8;
2149
- case 522: goto st13;
2150
- }
2151
- if ( _widec > 383 ) {
2152
- if ( 384 <= _widec && _widec <= 639 )
2153
- goto st17;
2154
- } else if ( _widec >= 128 )
2155
- goto st12;
2156
- goto st0;
2157
- st18:
2158
- if ( ++p == pe )
2159
- goto _test_eof18;
2160
- case 18:
2161
- switch( (*p) ) {
2162
- case 42: goto st19;
2163
- case 47: goto st21;
2164
- }
2165
- goto st0;
2166
- st19:
2167
- if ( ++p == pe )
2168
- goto _test_eof19;
2169
- case 19:
2170
- if ( (*p) == 42 )
2171
- goto st20;
2172
- goto st19;
2173
- st20:
2174
- if ( ++p == pe )
2175
- goto _test_eof20;
2176
- case 20:
2177
- switch( (*p) ) {
2178
- case 42: goto st20;
2179
- case 47: goto st2;
2180
- }
2181
- goto st19;
2182
- st21:
2183
- if ( ++p == pe )
2184
- goto _test_eof21;
2185
- case 21:
2186
- if ( (*p) == 10 )
2187
- goto st2;
2188
- goto st21;
2189
- }
2190
- _test_eof2: cs = 2; goto _test_eof;
2191
- _test_eof3: cs = 3; goto _test_eof;
2192
- _test_eof4: cs = 4; goto _test_eof;
2193
- _test_eof5: cs = 5; goto _test_eof;
2194
- _test_eof6: cs = 6; goto _test_eof;
2195
- _test_eof7: cs = 7; goto _test_eof;
2196
- _test_eof22: cs = 22; goto _test_eof;
2197
- _test_eof8: cs = 8; goto _test_eof;
2198
- _test_eof9: cs = 9; goto _test_eof;
2199
- _test_eof10: cs = 10; goto _test_eof;
2200
- _test_eof11: cs = 11; goto _test_eof;
2201
- _test_eof12: cs = 12; goto _test_eof;
2202
- _test_eof13: cs = 13; goto _test_eof;
2203
- _test_eof14: cs = 14; goto _test_eof;
2204
- _test_eof15: cs = 15; goto _test_eof;
2205
- _test_eof16: cs = 16; goto _test_eof;
2206
- _test_eof17: cs = 17; goto _test_eof;
2207
- _test_eof18: cs = 18; goto _test_eof;
2208
- _test_eof19: cs = 19; goto _test_eof;
2209
- _test_eof20: cs = 20; goto _test_eof;
2210
- _test_eof21: cs = 21; goto _test_eof;
2211
-
2212
- _test_eof: {}
2213
- _out: {}
2214
- }
2215
-
2216
- #line 846 "parser.rl"
2217
-
2218
- if(cs >= JSON_array_first_final) {
2219
- long count = json->stack->head - stack_head;
2220
-
2221
- if (RB_UNLIKELY(json->array_class)) {
2222
- VALUE array = rb_class_new_instance(0, 0, json->array_class);
2223
- VALUE *items = rvalue_stack_peek(json->stack, count);
2224
- long index;
2225
- for (index = 0; index < count; index++) {
2226
- rb_funcall(array, i_leftshift, 1, items[index]);
2227
- }
2228
- *result = array;
2229
- } else {
2230
- VALUE array = rb_ary_new_from_values(count, rvalue_stack_peek(json->stack, count));
2231
- *result = array;
566
+ uint32_t consecutive_spaces = trailing_zeros64(chunk ^ 0x2020202020202020) / CHAR_BIT;
567
+ state->cursor += consecutive_spaces;
568
+ break;
569
+ }
570
+ #endif
571
+ break;
572
+ case '\t':
573
+ case '\r':
574
+ state->cursor++;
575
+ break;
576
+ case '/':
577
+ json_eat_comments(state);
578
+ break;
579
+
580
+ default:
581
+ return;
2232
582
  }
2233
- rvalue_stack_pop(json->stack, count);
2234
-
2235
- return p + 1;
2236
- } else {
2237
- raise_parse_error("unexpected token at '%s'", p);
2238
- return NULL;
2239
583
  }
2240
584
  }
2241
585
 
@@ -2265,16 +609,27 @@ static inline VALUE build_string(const char *start, const char *end, bool intern
2265
609
  return result;
2266
610
  }
2267
611
 
2268
- static VALUE json_string_fastpath(JSON_Parser *json, char *string, char *stringEnd, bool is_name, bool intern, bool symbolize)
612
+ static inline bool json_string_cacheable_p(const char *string, size_t length)
613
+ {
614
+ // We mostly want to cache strings that are likely to be repeated.
615
+ // Simple heuristics:
616
+ // - Common names aren't likely to be very long. So we just don't cache names above an arbitrary threshold.
617
+ // - If the first character isn't a letter, we're much less likely to see this string again.
618
+ return length <= JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH && rb_isalpha(string[0]);
619
+ }
620
+
621
+ static inline VALUE json_string_fastpath(JSON_ParserState *state, JSON_ParserConfig *config, const char *string, const char *stringEnd, bool is_name)
2269
622
  {
623
+ bool intern = is_name || config->freeze;
624
+ bool symbolize = is_name && config->symbolize_names;
2270
625
  size_t bufferSize = stringEnd - string;
2271
626
 
2272
- if (is_name && json->in_array) {
627
+ if (is_name && state->in_array && RB_LIKELY(json_string_cacheable_p(string, bufferSize))) {
2273
628
  VALUE cached_key;
2274
629
  if (RB_UNLIKELY(symbolize)) {
2275
- cached_key = rsymbol_cache_fetch(&json->name_cache, string, bufferSize);
630
+ cached_key = rsymbol_cache_fetch(&state->name_cache, string, bufferSize);
2276
631
  } else {
2277
- cached_key = rstring_cache_fetch(&json->name_cache, string, bufferSize);
632
+ cached_key = rstring_cache_fetch(&state->name_cache, string, bufferSize);
2278
633
  }
2279
634
 
2280
635
  if (RB_LIKELY(cached_key)) {
@@ -2285,323 +640,760 @@ static VALUE json_string_fastpath(JSON_Parser *json, char *string, char *stringE
2285
640
  return build_string(string, stringEnd, intern, symbolize);
2286
641
  }
2287
642
 
2288
- static VALUE json_string_unescape(JSON_Parser *json, char *string, char *stringEnd, bool is_name, bool intern, bool symbolize)
2289
- {
2290
- size_t bufferSize = stringEnd - string;
2291
- char *p = string, *pe = string, *unescape, *bufferStart, *buffer;
2292
- int unescape_len;
2293
- char buf[4];
2294
-
2295
- if (is_name && json->in_array) {
2296
- VALUE cached_key;
2297
- if (RB_UNLIKELY(symbolize)) {
2298
- cached_key = rsymbol_cache_fetch(&json->name_cache, string, bufferSize);
2299
- } else {
2300
- cached_key = rstring_cache_fetch(&json->name_cache, string, bufferSize);
2301
- }
643
+ #define JSON_MAX_UNESCAPE_POSITIONS 16
644
+ typedef struct _json_unescape_positions {
645
+ long size;
646
+ const char **positions;
647
+ unsigned long additional_backslashes;
648
+ } JSON_UnescapePositions;
2302
649
 
2303
- if (RB_LIKELY(cached_key)) {
2304
- return cached_key;
650
+ static inline const char *json_next_backslash(const char *pe, const char *stringEnd, JSON_UnescapePositions *positions)
651
+ {
652
+ while (positions->size) {
653
+ positions->size--;
654
+ const char *next_position = positions->positions[0];
655
+ positions->positions++;
656
+ if (next_position >= pe) {
657
+ return next_position;
2305
658
  }
2306
659
  }
2307
660
 
2308
- pe = memchr(p, '\\', bufferSize);
2309
- if (RB_UNLIKELY(pe == NULL)) {
2310
- return build_string(string, stringEnd, intern, symbolize);
661
+ if (positions->additional_backslashes) {
662
+ positions->additional_backslashes--;
663
+ return memchr(pe, '\\', stringEnd - pe);
2311
664
  }
2312
665
 
666
+ return NULL;
667
+ }
668
+
669
+ NOINLINE(static) VALUE json_string_unescape(JSON_ParserState *state, JSON_ParserConfig *config, const char *string, const char *stringEnd, bool is_name, JSON_UnescapePositions *positions)
670
+ {
671
+ bool intern = is_name || config->freeze;
672
+ bool symbolize = is_name && config->symbolize_names;
673
+ size_t bufferSize = stringEnd - string;
674
+ const char *p = string, *pe = string, *bufferStart;
675
+ char *buffer;
676
+
2313
677
  VALUE result = rb_str_buf_new(bufferSize);
2314
678
  rb_enc_associate_index(result, utf8_encindex);
2315
- buffer = bufferStart = RSTRING_PTR(result);
2316
-
2317
- while (pe < stringEnd) {
2318
- if (*pe == '\\') {
2319
- unescape = (char *) "?";
2320
- unescape_len = 1;
2321
- if (pe > p) {
2322
- MEMCPY(buffer, p, char, pe - p);
2323
- buffer += pe - p;
2324
- }
2325
- switch (*++pe) {
2326
- case 'n':
2327
- unescape = (char *) "\n";
2328
- break;
2329
- case 'r':
2330
- unescape = (char *) "\r";
2331
- break;
2332
- case 't':
2333
- unescape = (char *) "\t";
2334
- break;
2335
- case '"':
2336
- unescape = (char *) "\"";
2337
- break;
2338
- case '\\':
2339
- unescape = (char *) "\\";
2340
- break;
2341
- case 'b':
2342
- unescape = (char *) "\b";
2343
- break;
2344
- case 'f':
2345
- unescape = (char *) "\f";
2346
- break;
2347
- case 'u':
2348
- if (pe > stringEnd - 4) {
2349
- raise_parse_error("incomplete unicode character escape sequence at '%s'", p);
679
+ buffer = RSTRING_PTR(result);
680
+ bufferStart = buffer;
681
+
682
+ #define APPEND_CHAR(chr) *buffer++ = chr; p = ++pe;
683
+
684
+ while (pe < stringEnd && (pe = json_next_backslash(pe, stringEnd, positions))) {
685
+ if (pe > p) {
686
+ MEMCPY(buffer, p, char, pe - p);
687
+ buffer += pe - p;
688
+ }
689
+ switch (*++pe) {
690
+ case '"':
691
+ case '/':
692
+ p = pe; // nothing to unescape just need to skip the backslash
693
+ break;
694
+ case '\\':
695
+ APPEND_CHAR('\\');
696
+ break;
697
+ case 'n':
698
+ APPEND_CHAR('\n');
699
+ break;
700
+ case 'r':
701
+ APPEND_CHAR('\r');
702
+ break;
703
+ case 't':
704
+ APPEND_CHAR('\t');
705
+ break;
706
+ case 'b':
707
+ APPEND_CHAR('\b');
708
+ break;
709
+ case 'f':
710
+ APPEND_CHAR('\f');
711
+ break;
712
+ case 'u': {
713
+ uint32_t ch = unescape_unicode(state, ++pe, stringEnd);
714
+ pe += 3;
715
+ /* To handle values above U+FFFF, we take a sequence of
716
+ * \uXXXX escapes in the U+D800..U+DBFF then
717
+ * U+DC00..U+DFFF ranges, take the low 10 bits from each
718
+ * to make a 20-bit number, then add 0x10000 to get the
719
+ * final codepoint.
720
+ *
721
+ * See Unicode 15: 3.8 "Surrogates", 5.3 "Handling
722
+ * Surrogate Pairs in UTF-16", and 23.6 "Surrogates
723
+ * Area".
724
+ */
725
+ if ((ch & 0xFC00) == 0xD800) {
726
+ pe++;
727
+ if (RB_LIKELY((pe <= stringEnd - 6) && memcmp(pe, "\\u", 2) == 0)) {
728
+ uint32_t sur = unescape_unicode(state, pe + 2, stringEnd);
729
+
730
+ if (RB_UNLIKELY((sur & 0xFC00) != 0xDC00)) {
731
+ raise_parse_error_at("invalid surrogate pair at %s", state, p);
732
+ }
733
+
734
+ ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16) | (sur & 0x3FF));
735
+ pe += 5;
2350
736
  } else {
2351
- uint32_t ch = unescape_unicode((unsigned char *) ++pe);
2352
- pe += 3;
2353
- /* To handle values above U+FFFF, we take a sequence of
2354
- * \uXXXX escapes in the U+D800..U+DBFF then
2355
- * U+DC00..U+DFFF ranges, take the low 10 bits from each
2356
- * to make a 20-bit number, then add 0x10000 to get the
2357
- * final codepoint.
2358
- *
2359
- * See Unicode 15: 3.8 "Surrogates", 5.3 "Handling
2360
- * Surrogate Pairs in UTF-16", and 23.6 "Surrogates
2361
- * Area".
2362
- */
2363
- if ((ch & 0xFC00) == 0xD800) {
2364
- pe++;
2365
- if (pe > stringEnd - 6) {
2366
- raise_parse_error("incomplete surrogate pair at '%s'", p);
2367
- }
2368
- if (pe[0] == '\\' && pe[1] == 'u') {
2369
- uint32_t sur = unescape_unicode((unsigned char *) pe + 2);
2370
- ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
2371
- | (sur & 0x3FF));
2372
- pe += 5;
2373
- } else {
2374
- unescape = (char *) "?";
2375
- break;
2376
- }
737
+ raise_parse_error_at("incomplete surrogate pair at %s", state, p);
738
+ break;
739
+ }
740
+ }
741
+
742
+ int unescape_len = convert_UTF32_to_UTF8(buffer, ch);
743
+ buffer += unescape_len;
744
+ p = ++pe;
745
+ break;
746
+ }
747
+ default:
748
+ if ((unsigned char)*pe < 0x20) {
749
+ if (!config->allow_control_characters) {
750
+ if (*pe == '\n') {
751
+ raise_parse_error_at("Invalid unescaped newline character (\\n) in string: %s", state, pe - 1);
2377
752
  }
2378
- unescape_len = convert_UTF32_to_UTF8(buf, ch);
2379
- unescape = buf;
753
+ raise_parse_error_at("invalid ASCII control character in string: %s", state, pe - 1);
2380
754
  }
2381
- break;
2382
- default:
2383
- p = pe;
2384
- continue;
2385
- }
2386
- MEMCPY(buffer, unescape, char, unescape_len);
2387
- buffer += unescape_len;
2388
- p = ++pe;
2389
- } else {
2390
- pe++;
755
+ } else {
756
+ raise_parse_error_at("invalid escape character in string: %s", state, pe - 1);
757
+ }
758
+ break;
2391
759
  }
2392
760
  }
761
+ #undef APPEND_CHAR
2393
762
 
2394
- if (pe > p) {
2395
- MEMCPY(buffer, p, char, pe - p);
2396
- buffer += pe - p;
763
+ if (stringEnd > p) {
764
+ MEMCPY(buffer, p, char, stringEnd - p);
765
+ buffer += stringEnd - p;
2397
766
  }
2398
767
  rb_str_set_len(result, buffer - bufferStart);
2399
768
 
2400
769
  if (symbolize) {
2401
770
  result = rb_str_intern(result);
2402
771
  } else if (intern) {
2403
- result = rb_funcall(rb_str_freeze(result), i_uminus, 0);
772
+ result = rb_str_to_interned_str(result);
2404
773
  }
2405
774
 
2406
775
  return result;
2407
776
  }
2408
777
 
778
+ #define MAX_FAST_INTEGER_SIZE 18
779
+
780
+ static VALUE json_decode_large_integer(const char *start, long len)
781
+ {
782
+ VALUE buffer_v;
783
+ char *buffer = RB_ALLOCV_N(char, buffer_v, len + 1);
784
+ MEMCPY(buffer, start, char, len);
785
+ buffer[len] = '\0';
786
+ VALUE number = rb_cstr2inum(buffer, 10);
787
+ RB_ALLOCV_END(buffer_v);
788
+ return number;
789
+ }
790
+
791
+ static inline VALUE
792
+ json_decode_integer(uint64_t mantissa, int mantissa_digits, bool negative, const char *start, const char *end)
793
+ {
794
+ if (RB_LIKELY(mantissa_digits < MAX_FAST_INTEGER_SIZE)) {
795
+ if (negative) {
796
+ return INT64T2NUM(-((int64_t)mantissa));
797
+ }
798
+ return UINT64T2NUM(mantissa);
799
+ }
2409
800
 
2410
- #line 2411 "parser.c"
2411
- enum {JSON_string_start = 1};
2412
- enum {JSON_string_first_final = 9};
2413
- enum {JSON_string_error = 0};
801
+ return json_decode_large_integer(start, end - start);
802
+ }
2414
803
 
2415
- enum {JSON_string_en_main = 1};
804
+ static VALUE json_decode_large_float(const char *start, long len)
805
+ {
806
+ if (RB_LIKELY(len < 64)) {
807
+ char buffer[64];
808
+ MEMCPY(buffer, start, char, len);
809
+ buffer[len] = '\0';
810
+ return DBL2NUM(rb_cstr_to_dbl(buffer, 1));
811
+ }
812
+
813
+ VALUE buffer_v;
814
+ char *buffer = RB_ALLOCV_N(char, buffer_v, len + 1);
815
+ MEMCPY(buffer, start, char, len);
816
+ buffer[len] = '\0';
817
+ VALUE number = DBL2NUM(rb_cstr_to_dbl(buffer, 1));
818
+ RB_ALLOCV_END(buffer_v);
819
+ return number;
820
+ }
2416
821
 
822
+ /* Ruby JSON optimized float decoder using vendored Ryu algorithm
823
+ * Accepts pre-extracted mantissa and exponent from first-pass validation
824
+ */
825
+ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantissa, int mantissa_digits, int32_t exponent, bool negative,
826
+ const char *start, const char *end)
827
+ {
828
+ if (RB_UNLIKELY(config->decimal_class)) {
829
+ VALUE text = rb_str_new(start, end - start);
830
+ return rb_funcallv(config->decimal_class, config->decimal_method_id, 1, &text);
831
+ }
2417
832
 
2418
- #line 1069 "parser.rl"
833
+ // Fall back to rb_cstr_to_dbl for potential subnormals (rare edge case)
834
+ // Ryu has rounding issues with subnormals around 1e-310 (< 2.225e-308)
835
+ if (RB_UNLIKELY(mantissa_digits > 17 || mantissa_digits + exponent < -307)) {
836
+ return json_decode_large_float(start, end - start);
837
+ }
2419
838
 
839
+ return DBL2NUM(ryu_s2d_from_parts(mantissa, mantissa_digits, exponent, negative));
840
+ }
2420
841
 
2421
- static int
2422
- match_i(VALUE regexp, VALUE klass, VALUE memo)
842
+ static inline VALUE json_decode_array(JSON_ParserState *state, JSON_ParserConfig *config, long count)
2423
843
  {
2424
- if (regexp == Qundef) return ST_STOP;
2425
- if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
2426
- RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
2427
- rb_ary_push(memo, klass);
2428
- return ST_STOP;
844
+ VALUE array = rb_ary_new_from_values(count, rvalue_stack_peek(state->stack, count));
845
+ rvalue_stack_pop(state->stack, count);
846
+
847
+ if (config->freeze) {
848
+ RB_OBJ_FREEZE(array);
2429
849
  }
2430
- return ST_CONTINUE;
850
+
851
+ return array;
2431
852
  }
2432
853
 
2433
- static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
854
+ static VALUE json_find_duplicated_key(size_t count, const VALUE *pairs)
2434
855
  {
2435
- int cs = EVIL;
2436
- VALUE match_string;
2437
-
2438
-
2439
- #line 2440 "parser.c"
2440
- {
2441
- cs = JSON_string_start;
2442
- }
2443
-
2444
- #line 1089 "parser.rl"
2445
- json->memo = p;
2446
-
2447
- #line 2448 "parser.c"
2448
- {
2449
- if ( p == pe )
2450
- goto _test_eof;
2451
- switch ( cs )
2452
- {
2453
- case 1:
2454
- if ( (*p) == 34 )
2455
- goto st2;
2456
- goto st0;
2457
- st0:
2458
- cs = 0;
2459
- goto _out;
2460
- st2:
2461
- if ( ++p == pe )
2462
- goto _test_eof2;
2463
- case 2:
2464
- switch( (*p) ) {
2465
- case 34: goto tr2;
2466
- case 92: goto st3;
2467
- }
2468
- if ( 0 <= (signed char)(*(p)) && (*(p)) <= 31 )
2469
- goto st0;
2470
- goto st2;
2471
- tr2:
2472
- #line 1051 "parser.rl"
2473
- {
2474
- *result = json_string_fastpath(json, json->memo + 1, p, json->parsing_name, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names);
2475
- {p = (( p + 1))-1;}
2476
- p--;
2477
- {p++; cs = 9; goto _out;}
856
+ VALUE set = rb_hash_new_capa(count / 2);
857
+ for (size_t index = 0; index < count; index += 2) {
858
+ size_t before = RHASH_SIZE(set);
859
+ VALUE key = pairs[index];
860
+ rb_hash_aset(set, key, Qtrue);
861
+ if (RHASH_SIZE(set) == before) {
862
+ if (RB_SYMBOL_P(key)) {
863
+ return rb_sym2str(key);
864
+ }
865
+ return key;
866
+ }
2478
867
  }
2479
- #line 1044 "parser.rl"
2480
- {
2481
- *result = json_string_unescape(json, json->memo + 1, p, json->parsing_name, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names);
2482
- {p = (( p + 1))-1;}
2483
- p--;
2484
- {p++; cs = 9; goto _out;}
868
+ return Qfalse;
869
+ }
870
+
871
+ static void emit_duplicate_key_warning(JSON_ParserState *state, VALUE duplicate_key)
872
+ {
873
+ VALUE message = rb_sprintf(
874
+ "detected duplicate key %"PRIsVALUE" in JSON object. This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`",
875
+ rb_inspect(duplicate_key)
876
+ );
877
+
878
+ emit_parse_warning(RSTRING_PTR(message), state);
879
+ RB_GC_GUARD(message);
880
+ }
881
+
882
+ #ifdef RBIMPL_ATTR_NORETURN
883
+ RBIMPL_ATTR_NORETURN()
884
+ #endif
885
+ static void raise_duplicate_key_error(JSON_ParserState *state, VALUE duplicate_key)
886
+ {
887
+ VALUE message = rb_sprintf(
888
+ "duplicate key %"PRIsVALUE,
889
+ rb_inspect(duplicate_key)
890
+ );
891
+
892
+ raise_parse_error(RSTRING_PTR(message), state);
893
+ RB_GC_GUARD(message);
894
+ }
895
+
896
+ static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfig *config, size_t count)
897
+ {
898
+ size_t entries_count = count / 2;
899
+ VALUE object = rb_hash_new_capa(entries_count);
900
+ const VALUE *pairs = rvalue_stack_peek(state->stack, count);
901
+ rb_hash_bulk_insert(count, pairs, object);
902
+
903
+ if (RB_UNLIKELY(RHASH_SIZE(object) < entries_count)) {
904
+ switch (config->on_duplicate_key) {
905
+ case JSON_IGNORE:
906
+ break;
907
+ case JSON_DEPRECATED:
908
+ emit_duplicate_key_warning(state, json_find_duplicated_key(count, pairs));
909
+ break;
910
+ case JSON_RAISE:
911
+ raise_duplicate_key_error(state, json_find_duplicated_key(count, pairs));
912
+ break;
913
+ }
2485
914
  }
2486
- goto st9;
2487
- tr6:
2488
- #line 1044 "parser.rl"
2489
- {
2490
- *result = json_string_unescape(json, json->memo + 1, p, json->parsing_name, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names);
2491
- {p = (( p + 1))-1;}
2492
- p--;
2493
- {p++; cs = 9; goto _out;}
915
+
916
+ rvalue_stack_pop(state->stack, count);
917
+
918
+ if (config->freeze) {
919
+ RB_OBJ_FREEZE(object);
2494
920
  }
2495
- goto st9;
2496
- st9:
2497
- if ( ++p == pe )
2498
- goto _test_eof9;
2499
- case 9:
2500
- #line 2501 "parser.c"
2501
- goto st0;
2502
- st3:
2503
- if ( ++p == pe )
2504
- goto _test_eof3;
2505
- case 3:
2506
- if ( (*p) == 117 )
2507
- goto st5;
2508
- if ( 0 <= (signed char)(*(p)) && (*(p)) <= 31 )
2509
- goto st0;
2510
- goto st4;
2511
- st4:
2512
- if ( ++p == pe )
2513
- goto _test_eof4;
2514
- case 4:
2515
- switch( (*p) ) {
2516
- case 34: goto tr6;
2517
- case 92: goto st3;
2518
- }
2519
- if ( 0 <= (signed char)(*(p)) && (*(p)) <= 31 )
2520
- goto st0;
2521
- goto st4;
2522
- st5:
2523
- if ( ++p == pe )
2524
- goto _test_eof5;
2525
- case 5:
2526
- if ( (*p) < 65 ) {
2527
- if ( 48 <= (*p) && (*p) <= 57 )
2528
- goto st6;
2529
- } else if ( (*p) > 70 ) {
2530
- if ( 97 <= (*p) && (*p) <= 102 )
2531
- goto st6;
2532
- } else
2533
- goto st6;
2534
- goto st0;
2535
- st6:
2536
- if ( ++p == pe )
2537
- goto _test_eof6;
2538
- case 6:
2539
- if ( (*p) < 65 ) {
2540
- if ( 48 <= (*p) && (*p) <= 57 )
2541
- goto st7;
2542
- } else if ( (*p) > 70 ) {
2543
- if ( 97 <= (*p) && (*p) <= 102 )
2544
- goto st7;
2545
- } else
2546
- goto st7;
2547
- goto st0;
2548
- st7:
2549
- if ( ++p == pe )
2550
- goto _test_eof7;
2551
- case 7:
2552
- if ( (*p) < 65 ) {
2553
- if ( 48 <= (*p) && (*p) <= 57 )
2554
- goto st8;
2555
- } else if ( (*p) > 70 ) {
2556
- if ( 97 <= (*p) && (*p) <= 102 )
2557
- goto st8;
2558
- } else
2559
- goto st8;
2560
- goto st0;
2561
- st8:
2562
- if ( ++p == pe )
2563
- goto _test_eof8;
2564
- case 8:
2565
- if ( (*p) < 65 ) {
2566
- if ( 48 <= (*p) && (*p) <= 57 )
2567
- goto st4;
2568
- } else if ( (*p) > 70 ) {
2569
- if ( 97 <= (*p) && (*p) <= 102 )
2570
- goto st4;
2571
- } else
2572
- goto st4;
2573
- goto st0;
2574
- }
2575
- _test_eof2: cs = 2; goto _test_eof;
2576
- _test_eof9: cs = 9; goto _test_eof;
2577
- _test_eof3: cs = 3; goto _test_eof;
2578
- _test_eof4: cs = 4; goto _test_eof;
2579
- _test_eof5: cs = 5; goto _test_eof;
2580
- _test_eof6: cs = 6; goto _test_eof;
2581
- _test_eof7: cs = 7; goto _test_eof;
2582
- _test_eof8: cs = 8; goto _test_eof;
2583
-
2584
- _test_eof: {}
2585
- _out: {}
2586
- }
2587
-
2588
- #line 1091 "parser.rl"
2589
-
2590
- if (json->create_additions && RTEST(match_string = json->match_string)) {
2591
- VALUE klass;
2592
- VALUE memo = rb_ary_new2(2);
2593
- rb_ary_push(memo, *result);
2594
- rb_hash_foreach(match_string, match_i, memo);
2595
- klass = rb_ary_entry(memo, 1);
2596
- if (RTEST(klass)) {
2597
- *result = rb_funcall(klass, i_json_create, 1, *result);
2598
- }
921
+
922
+ return object;
923
+ }
924
+
925
+ static inline VALUE json_push_value(JSON_ParserState *state, JSON_ParserConfig *config, VALUE value)
926
+ {
927
+ if (RB_UNLIKELY(config->on_load_proc)) {
928
+ value = rb_proc_call_with_block(config->on_load_proc, 1, &value, Qnil);
2599
929
  }
930
+ rvalue_stack_push(state->stack, value, &state->stack_handle, &state->stack);
931
+ return value;
932
+ }
2600
933
 
2601
- if (cs >= JSON_string_first_final) {
2602
- return p + 1;
2603
- } else {
2604
- return NULL;
934
+ static const bool string_scan_table[256] = {
935
+ // ASCII Control Characters
936
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
937
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
938
+ // ASCII Characters
939
+ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
940
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
941
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
942
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // '\\'
943
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
944
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
945
+ };
946
+
947
+ #ifdef HAVE_SIMD
948
+ static SIMD_Implementation simd_impl = SIMD_NONE;
949
+ #endif /* HAVE_SIMD */
950
+
951
+ ALWAYS_INLINE(static) bool string_scan(JSON_ParserState *state)
952
+ {
953
+ #ifdef HAVE_SIMD
954
+ #if defined(HAVE_SIMD_NEON)
955
+
956
+ uint64_t mask = 0;
957
+ if (string_scan_simd_neon(&state->cursor, state->end, &mask)) {
958
+ state->cursor += trailing_zeros64(mask) >> 2;
959
+ return true;
960
+ }
961
+
962
+ #elif defined(HAVE_SIMD_SSE2)
963
+ if (simd_impl == SIMD_SSE2) {
964
+ int mask = 0;
965
+ if (string_scan_simd_sse2(&state->cursor, state->end, &mask)) {
966
+ state->cursor += trailing_zeros(mask);
967
+ return true;
968
+ }
969
+ }
970
+ #endif /* HAVE_SIMD_NEON or HAVE_SIMD_SSE2 */
971
+ #endif /* HAVE_SIMD */
972
+
973
+ while (!eos(state)) {
974
+ if (RB_UNLIKELY(string_scan_table[(unsigned char)*state->cursor])) {
975
+ return true;
976
+ }
977
+ state->cursor++;
978
+ }
979
+ return false;
980
+ }
981
+
982
+ static VALUE json_parse_escaped_string(JSON_ParserState *state, JSON_ParserConfig *config, bool is_name, const char *start)
983
+ {
984
+ const char *backslashes[JSON_MAX_UNESCAPE_POSITIONS];
985
+ JSON_UnescapePositions positions = {
986
+ .size = 0,
987
+ .positions = backslashes,
988
+ .additional_backslashes = 0,
989
+ };
990
+
991
+ do {
992
+ switch (*state->cursor) {
993
+ case '"': {
994
+ VALUE string = json_string_unescape(state, config, start, state->cursor, is_name, &positions);
995
+ state->cursor++;
996
+ return json_push_value(state, config, string);
997
+ }
998
+ case '\\': {
999
+ if (RB_LIKELY(positions.size < JSON_MAX_UNESCAPE_POSITIONS)) {
1000
+ backslashes[positions.size] = state->cursor;
1001
+ positions.size++;
1002
+ } else {
1003
+ positions.additional_backslashes++;
1004
+ }
1005
+ state->cursor++;
1006
+ break;
1007
+ }
1008
+ default:
1009
+ if (!config->allow_control_characters) {
1010
+ raise_parse_error("invalid ASCII control character in string: %s", state);
1011
+ }
1012
+ break;
1013
+ }
1014
+
1015
+ state->cursor++;
1016
+ } while (string_scan(state));
1017
+
1018
+ raise_parse_error("unexpected end of input, expected closing \"", state);
1019
+ return Qfalse;
1020
+ }
1021
+
1022
+ ALWAYS_INLINE(static) VALUE json_parse_string(JSON_ParserState *state, JSON_ParserConfig *config, bool is_name)
1023
+ {
1024
+ state->cursor++;
1025
+ const char *start = state->cursor;
1026
+
1027
+ if (RB_UNLIKELY(!string_scan(state))) {
1028
+ raise_parse_error("unexpected end of input, expected closing \"", state);
1029
+ }
1030
+
1031
+ if (RB_LIKELY(*state->cursor == '"')) {
1032
+ VALUE string = json_string_fastpath(state, config, start, state->cursor, is_name);
1033
+ state->cursor++;
1034
+ return json_push_value(state, config, string);
1035
+ }
1036
+ return json_parse_escaped_string(state, config, is_name, start);
1037
+ }
1038
+
1039
+ #if JSON_CPU_LITTLE_ENDIAN_64BITS
1040
+ // From: https://lemire.me/blog/2022/01/21/swar-explained-parsing-eight-digits/
1041
+ // Additional References:
1042
+ // https://johnnylee-sde.github.io/Fast-numeric-string-to-int/
1043
+ // http://0x80.pl/notesen/2014-10-12-parsing-decimal-numbers-part-1-swar.html
1044
+ static inline uint64_t decode_8digits_unrolled(uint64_t val) {
1045
+ const uint64_t mask = 0x000000FF000000FF;
1046
+ const uint64_t mul1 = 0x000F424000000064; // 100 + (1000000ULL << 32)
1047
+ const uint64_t mul2 = 0x0000271000000001; // 1 + (10000ULL << 32)
1048
+ val -= 0x3030303030303030;
1049
+ val = (val * 10) + (val >> 8); // val = (val * 2561) >> 8;
1050
+ val = (((val & mask) * mul1) + (((val >> 16) & mask) * mul2)) >> 32;
1051
+ return val;
1052
+ }
1053
+
1054
+ static inline uint64_t decode_4digits_unrolled(uint32_t val) {
1055
+ const uint32_t mask = 0x000000FF;
1056
+ const uint32_t mul1 = 100;
1057
+ val -= 0x30303030;
1058
+ val = (val * 10) + (val >> 8); // val = (val * 2561) >> 8;
1059
+ val = ((val & mask) * mul1) + (((val >> 16) & mask));
1060
+ return val;
1061
+ }
1062
+ #endif
1063
+
1064
+ static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulator)
1065
+ {
1066
+ const char *start = state->cursor;
1067
+
1068
+ #if JSON_CPU_LITTLE_ENDIAN_64BITS
1069
+ while (rest(state) >= sizeof(uint64_t)) {
1070
+ uint64_t next_8bytes;
1071
+ memcpy(&next_8bytes, state->cursor, sizeof(uint64_t));
1072
+
1073
+ // From: https://github.com/simdjson/simdjson/blob/32b301893c13d058095a07d9868edaaa42ee07aa/include/simdjson/generic/numberparsing.h#L333
1074
+ // Branchless version of: http://0x80.pl/articles/swar-digits-validate.html
1075
+ uint64_t match = (next_8bytes & 0xF0F0F0F0F0F0F0F0) | (((next_8bytes + 0x0606060606060606) & 0xF0F0F0F0F0F0F0F0) >> 4);
1076
+
1077
+ if (match == 0x3333333333333333) { // 8 consecutive digits
1078
+ *accumulator = (*accumulator * 100000000) + decode_8digits_unrolled(next_8bytes);
1079
+ state->cursor += 8;
1080
+ continue;
1081
+ }
1082
+
1083
+ uint32_t consecutive_digits = trailing_zeros64(match ^ 0x3333333333333333) / CHAR_BIT;
1084
+
1085
+ if (consecutive_digits >= 4) {
1086
+ *accumulator = (*accumulator * 10000) + decode_4digits_unrolled((uint32_t)next_8bytes);
1087
+ state->cursor += 4;
1088
+ consecutive_digits -= 4;
1089
+ }
1090
+
1091
+ while (consecutive_digits) {
1092
+ *accumulator = *accumulator * 10 + (*state->cursor - '0');
1093
+ consecutive_digits--;
1094
+ state->cursor++;
1095
+ }
1096
+
1097
+ return (int)(state->cursor - start);
1098
+ }
1099
+ #endif
1100
+
1101
+ char next_char;
1102
+ while (rb_isdigit(next_char = peek(state))) {
1103
+ *accumulator = *accumulator * 10 + (next_char - '0');
1104
+ state->cursor++;
1105
+ }
1106
+ return (int)(state->cursor - start);
1107
+ }
1108
+
1109
+ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig *config, bool negative, const char *start)
1110
+ {
1111
+ bool integer = true;
1112
+ const char first_digit = *state->cursor;
1113
+
1114
+ // Variables for Ryu optimization - extract digits during parsing
1115
+ int32_t exponent = 0;
1116
+ int decimal_point_pos = -1;
1117
+ uint64_t mantissa = 0;
1118
+
1119
+ // Parse integer part and extract mantissa digits
1120
+ int mantissa_digits = json_parse_digits(state, &mantissa);
1121
+
1122
+ if (RB_UNLIKELY((first_digit == '0' && mantissa_digits > 1) || (negative && mantissa_digits == 0))) {
1123
+ raise_parse_error_at("invalid number: %s", state, start);
1124
+ }
1125
+
1126
+ // Parse fractional part
1127
+ if (peek(state) == '.') {
1128
+ integer = false;
1129
+ decimal_point_pos = mantissa_digits; // Remember position of decimal point
1130
+ state->cursor++;
1131
+
1132
+ int fractional_digits = json_parse_digits(state, &mantissa);
1133
+ mantissa_digits += fractional_digits;
1134
+
1135
+ if (RB_UNLIKELY(!fractional_digits)) {
1136
+ raise_parse_error_at("invalid number: %s", state, start);
1137
+ }
1138
+ }
1139
+
1140
+ // Parse exponent
1141
+ if (rb_tolower(peek(state)) == 'e') {
1142
+ integer = false;
1143
+ state->cursor++;
1144
+
1145
+ bool negative_exponent = false;
1146
+ const char next_char = peek(state);
1147
+ if (next_char == '-' || next_char == '+') {
1148
+ negative_exponent = next_char == '-';
1149
+ state->cursor++;
1150
+ }
1151
+
1152
+ uint64_t abs_exponent = 0;
1153
+ int exponent_digits = json_parse_digits(state, &abs_exponent);
1154
+
1155
+ if (RB_UNLIKELY(!exponent_digits)) {
1156
+ raise_parse_error_at("invalid number: %s", state, start);
1157
+ }
1158
+
1159
+ exponent = negative_exponent ? -((int32_t)abs_exponent) : ((int32_t)abs_exponent);
1160
+ }
1161
+
1162
+ if (integer) {
1163
+ return json_decode_integer(mantissa, mantissa_digits, negative, start, state->cursor);
1164
+ }
1165
+
1166
+ // Adjust exponent based on decimal point position
1167
+ if (decimal_point_pos >= 0) {
1168
+ exponent -= (mantissa_digits - decimal_point_pos);
1169
+ }
1170
+
1171
+ return json_decode_float(config, mantissa, mantissa_digits, exponent, negative, start, state->cursor);
1172
+ }
1173
+
1174
+ static inline VALUE json_parse_positive_number(JSON_ParserState *state, JSON_ParserConfig *config)
1175
+ {
1176
+ return json_parse_number(state, config, false, state->cursor);
1177
+ }
1178
+
1179
+ static inline VALUE json_parse_negative_number(JSON_ParserState *state, JSON_ParserConfig *config)
1180
+ {
1181
+ const char *start = state->cursor;
1182
+ state->cursor++;
1183
+ return json_parse_number(state, config, true, start);
1184
+ }
1185
+
1186
+ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
1187
+ {
1188
+ json_eat_whitespace(state);
1189
+
1190
+ switch (peek(state)) {
1191
+ case 'n':
1192
+ if (rest(state) >= 4 && (memcmp(state->cursor, "null", 4) == 0)) {
1193
+ state->cursor += 4;
1194
+ return json_push_value(state, config, Qnil);
1195
+ }
1196
+
1197
+ raise_parse_error("unexpected token %s", state);
1198
+ break;
1199
+ case 't':
1200
+ if (rest(state) >= 4 && (memcmp(state->cursor, "true", 4) == 0)) {
1201
+ state->cursor += 4;
1202
+ return json_push_value(state, config, Qtrue);
1203
+ }
1204
+
1205
+ raise_parse_error("unexpected token %s", state);
1206
+ break;
1207
+ case 'f':
1208
+ // Note: memcmp with a small power of two compile to an integer comparison
1209
+ if (rest(state) >= 5 && (memcmp(state->cursor + 1, "alse", 4) == 0)) {
1210
+ state->cursor += 5;
1211
+ return json_push_value(state, config, Qfalse);
1212
+ }
1213
+
1214
+ raise_parse_error("unexpected token %s", state);
1215
+ break;
1216
+ case 'N':
1217
+ // Note: memcmp with a small power of two compile to an integer comparison
1218
+ if (config->allow_nan && rest(state) >= 3 && (memcmp(state->cursor + 1, "aN", 2) == 0)) {
1219
+ state->cursor += 3;
1220
+ return json_push_value(state, config, CNaN);
1221
+ }
1222
+
1223
+ raise_parse_error("unexpected token %s", state);
1224
+ break;
1225
+ case 'I':
1226
+ if (config->allow_nan && rest(state) >= 8 && (memcmp(state->cursor, "Infinity", 8) == 0)) {
1227
+ state->cursor += 8;
1228
+ return json_push_value(state, config, CInfinity);
1229
+ }
1230
+
1231
+ raise_parse_error("unexpected token %s", state);
1232
+ break;
1233
+ case '-': {
1234
+ // Note: memcmp with a small power of two compile to an integer comparison
1235
+ if (rest(state) >= 9 && (memcmp(state->cursor + 1, "Infinity", 8) == 0)) {
1236
+ if (config->allow_nan) {
1237
+ state->cursor += 9;
1238
+ return json_push_value(state, config, CMinusInfinity);
1239
+ } else {
1240
+ raise_parse_error("unexpected token %s", state);
1241
+ }
1242
+ }
1243
+ return json_push_value(state, config, json_parse_negative_number(state, config));
1244
+ break;
1245
+ }
1246
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1247
+ return json_push_value(state, config, json_parse_positive_number(state, config));
1248
+ break;
1249
+ case '"': {
1250
+ // %r{\A"[^"\\\t\n\x00]*(?:\\[bfnrtu\\/"][^"\\]*)*"}
1251
+ return json_parse_string(state, config, false);
1252
+ break;
1253
+ }
1254
+ case '[': {
1255
+ state->cursor++;
1256
+ json_eat_whitespace(state);
1257
+ long stack_head = state->stack->head;
1258
+
1259
+ if (peek(state) == ']') {
1260
+ state->cursor++;
1261
+ return json_push_value(state, config, json_decode_array(state, config, 0));
1262
+ } else {
1263
+ state->current_nesting++;
1264
+ if (RB_UNLIKELY(config->max_nesting && (config->max_nesting < state->current_nesting))) {
1265
+ rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
1266
+ }
1267
+ state->in_array++;
1268
+ json_parse_any(state, config);
1269
+ }
1270
+
1271
+ while (true) {
1272
+ json_eat_whitespace(state);
1273
+
1274
+ const char next_char = peek(state);
1275
+
1276
+ if (RB_LIKELY(next_char == ',')) {
1277
+ state->cursor++;
1278
+ if (config->allow_trailing_comma) {
1279
+ json_eat_whitespace(state);
1280
+ if (peek(state) == ']') {
1281
+ continue;
1282
+ }
1283
+ }
1284
+ json_parse_any(state, config);
1285
+ continue;
1286
+ }
1287
+
1288
+ if (next_char == ']') {
1289
+ state->cursor++;
1290
+ long count = state->stack->head - stack_head;
1291
+ state->current_nesting--;
1292
+ state->in_array--;
1293
+ return json_push_value(state, config, json_decode_array(state, config, count));
1294
+ }
1295
+
1296
+ raise_parse_error("expected ',' or ']' after array value", state);
1297
+ }
1298
+ break;
1299
+ }
1300
+ case '{': {
1301
+ const char *object_start_cursor = state->cursor;
1302
+
1303
+ state->cursor++;
1304
+ json_eat_whitespace(state);
1305
+ long stack_head = state->stack->head;
1306
+
1307
+ if (peek(state) == '}') {
1308
+ state->cursor++;
1309
+ return json_push_value(state, config, json_decode_object(state, config, 0));
1310
+ } else {
1311
+ state->current_nesting++;
1312
+ if (RB_UNLIKELY(config->max_nesting && (config->max_nesting < state->current_nesting))) {
1313
+ rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
1314
+ }
1315
+
1316
+ if (peek(state) != '"') {
1317
+ raise_parse_error("expected object key, got %s", state);
1318
+ }
1319
+ json_parse_string(state, config, true);
1320
+
1321
+ json_eat_whitespace(state);
1322
+ if (peek(state) != ':') {
1323
+ raise_parse_error("expected ':' after object key", state);
1324
+ }
1325
+ state->cursor++;
1326
+
1327
+ json_parse_any(state, config);
1328
+ }
1329
+
1330
+ while (true) {
1331
+ json_eat_whitespace(state);
1332
+
1333
+ const char next_char = peek(state);
1334
+ if (next_char == '}') {
1335
+ state->cursor++;
1336
+ state->current_nesting--;
1337
+ size_t count = state->stack->head - stack_head;
1338
+
1339
+ // Temporary rewind cursor in case an error is raised
1340
+ const char *final_cursor = state->cursor;
1341
+ state->cursor = object_start_cursor;
1342
+ VALUE object = json_decode_object(state, config, count);
1343
+ state->cursor = final_cursor;
1344
+
1345
+ return json_push_value(state, config, object);
1346
+ }
1347
+
1348
+ if (next_char == ',') {
1349
+ state->cursor++;
1350
+ json_eat_whitespace(state);
1351
+
1352
+ if (config->allow_trailing_comma) {
1353
+ if (peek(state) == '}') {
1354
+ continue;
1355
+ }
1356
+ }
1357
+
1358
+ if (RB_UNLIKELY(peek(state) != '"')) {
1359
+ raise_parse_error("expected object key, got: %s", state);
1360
+ }
1361
+ json_parse_string(state, config, true);
1362
+
1363
+ json_eat_whitespace(state);
1364
+ if (RB_UNLIKELY(peek(state) != ':')) {
1365
+ raise_parse_error("expected ':' after object key, got: %s", state);
1366
+ }
1367
+ state->cursor++;
1368
+
1369
+ json_parse_any(state, config);
1370
+
1371
+ continue;
1372
+ }
1373
+
1374
+ raise_parse_error("expected ',' or '}' after object value, got: %s", state);
1375
+ }
1376
+ break;
1377
+ }
1378
+
1379
+ case 0:
1380
+ raise_parse_error("unexpected end of input", state);
1381
+ break;
1382
+
1383
+ default:
1384
+ raise_parse_error("unexpected character: %s", state);
1385
+ break;
1386
+ }
1387
+
1388
+ raise_parse_error("unreachable: %s", state);
1389
+ return Qundef;
1390
+ }
1391
+
1392
+ static void json_ensure_eof(JSON_ParserState *state)
1393
+ {
1394
+ json_eat_whitespace(state);
1395
+ if (!eos(state)) {
1396
+ raise_parse_error("unexpected token at end of stream %s", state);
2605
1397
  }
2606
1398
  }
2607
1399
 
@@ -2633,72 +1425,70 @@ static VALUE convert_encoding(VALUE source)
2633
1425
  return rb_funcall(source, i_encode, 1, Encoding_UTF_8);
2634
1426
  }
2635
1427
 
2636
- static int configure_parser_i(VALUE key, VALUE val, VALUE data)
1428
+ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
2637
1429
  {
2638
- JSON_Parser *json = (JSON_Parser *)data;
2639
-
2640
- if (key == sym_max_nesting) { json->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
2641
- else if (key == sym_allow_nan) { json->allow_nan = RTEST(val); }
2642
- else if (key == sym_allow_trailing_comma) { json->allow_trailing_comma = RTEST(val); }
2643
- else if (key == sym_symbolize_names) { json->symbolize_names = RTEST(val); }
2644
- else if (key == sym_freeze) { json->freeze = RTEST(val); }
2645
- else if (key == sym_create_id) { json->create_id = RTEST(val) ? val : Qfalse; }
2646
- else if (key == sym_object_class) { json->object_class = RTEST(val) ? val : Qfalse; }
2647
- else if (key == sym_array_class) { json->array_class = RTEST(val) ? val : Qfalse; }
2648
- else if (key == sym_decimal_class) { json->decimal_class = RTEST(val) ? val : Qfalse; }
2649
- else if (key == sym_match_string) { json->match_string = RTEST(val) ? val : Qfalse; }
2650
- else if (key == sym_create_additions) {
2651
- if (NIL_P(val)) {
2652
- json->create_additions = true;
2653
- json->deprecated_create_additions = true;
2654
- } else {
2655
- json->create_additions = RTEST(val);
2656
- json->deprecated_create_additions = false;
1430
+ JSON_ParserConfig *config = (JSON_ParserConfig *)data;
1431
+
1432
+ if (key == sym_max_nesting) { config->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
1433
+ else if (key == sym_allow_nan) { config->allow_nan = RTEST(val); }
1434
+ else if (key == sym_allow_trailing_comma) { config->allow_trailing_comma = RTEST(val); }
1435
+ else if (key == sym_allow_control_characters) { config->allow_control_characters = RTEST(val); }
1436
+ else if (key == sym_symbolize_names) { config->symbolize_names = RTEST(val); }
1437
+ else if (key == sym_freeze) { config->freeze = RTEST(val); }
1438
+ else if (key == sym_on_load) { config->on_load_proc = RTEST(val) ? val : Qfalse; }
1439
+ else if (key == sym_allow_duplicate_key) { config->on_duplicate_key = RTEST(val) ? JSON_IGNORE : JSON_RAISE; }
1440
+ else if (key == sym_decimal_class) {
1441
+ if (RTEST(val)) {
1442
+ if (rb_respond_to(val, i_try_convert)) {
1443
+ config->decimal_class = val;
1444
+ config->decimal_method_id = i_try_convert;
1445
+ } else if (rb_respond_to(val, i_new)) {
1446
+ config->decimal_class = val;
1447
+ config->decimal_method_id = i_new;
1448
+ } else if (RB_TYPE_P(val, T_CLASS)) {
1449
+ VALUE name = rb_class_name(val);
1450
+ const char *name_cstr = RSTRING_PTR(name);
1451
+ const char *last_colon = strrchr(name_cstr, ':');
1452
+ if (last_colon) {
1453
+ const char *mod_path_end = last_colon - 1;
1454
+ VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
1455
+ config->decimal_class = rb_path_to_class(mod_path);
1456
+
1457
+ const char *method_name_beg = last_colon + 1;
1458
+ long before_len = method_name_beg - name_cstr;
1459
+ long len = RSTRING_LEN(name) - before_len;
1460
+ VALUE method_name = rb_str_substr(name, before_len, len);
1461
+ config->decimal_method_id = SYM2ID(rb_str_intern(method_name));
1462
+ } else {
1463
+ config->decimal_class = rb_mKernel;
1464
+ config->decimal_method_id = SYM2ID(rb_str_intern(name));
1465
+ }
1466
+ }
2657
1467
  }
2658
1468
  }
2659
1469
 
2660
1470
  return ST_CONTINUE;
2661
1471
  }
2662
1472
 
2663
- static void parser_init(JSON_Parser *json, VALUE source, VALUE opts)
1473
+ static void parser_config_init(JSON_ParserConfig *config, VALUE opts)
2664
1474
  {
2665
- if (json->Vsource) {
2666
- rb_raise(rb_eTypeError, "already initialized instance");
2667
- }
2668
-
2669
- json->fbuffer.initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
2670
- json->max_nesting = 100;
1475
+ config->max_nesting = 100;
2671
1476
 
2672
1477
  if (!NIL_P(opts)) {
2673
1478
  Check_Type(opts, T_HASH);
2674
1479
  if (RHASH_SIZE(opts) > 0) {
2675
1480
  // We assume in most cases few keys are set so it's faster to go over
2676
1481
  // the provided keys than to check all possible keys.
2677
- rb_hash_foreach(opts, configure_parser_i, (VALUE)json);
2678
-
2679
- if (json->symbolize_names && json->create_additions) {
2680
- rb_raise(rb_eArgError,
2681
- "options :symbolize_names and :create_additions cannot be "
2682
- " used in conjunction");
2683
- }
2684
-
2685
- if (json->create_additions && !json->create_id) {
2686
- json->create_id = rb_funcall(mJSON, i_create_id, 0);
2687
- }
1482
+ rb_hash_foreach(opts, parser_config_init_i, (VALUE)config);
2688
1483
  }
2689
1484
 
2690
1485
  }
2691
- source = convert_encoding(StringValue(source));
2692
- StringValue(source);
2693
- json->len = RSTRING_LEN(source);
2694
- json->source = RSTRING_PTR(source);
2695
- json->Vsource = source;
2696
1486
  }
2697
1487
 
2698
1488
  /*
2699
- * call-seq: new(source, opts => {})
1489
+ * call-seq: new(opts => {})
2700
1490
  *
2701
- * Creates a new JSON::Ext::Parser instance for the string _source_.
1491
+ * Creates a new JSON::Ext::ParserConfig instance.
2702
1492
  *
2703
1493
  * It will be configured by the _opts_ hash. _opts_ can have the following
2704
1494
  * keys:
@@ -2714,456 +1504,115 @@ static void parser_init(JSON_Parser *json, VALUE source, VALUE opts)
2714
1504
  * (keys) in a JSON object. Otherwise strings are returned, which is
2715
1505
  * also the default. It's not possible to use this option in
2716
1506
  * conjunction with the *create_additions* option.
2717
- * * *create_additions*: If set to false, the Parser doesn't create
2718
- * additions even if a matching class and create_id was found. This option
2719
- * defaults to false.
2720
- * * *object_class*: Defaults to Hash. If another type is provided, it will be used
2721
- * instead of Hash to represent JSON objects. The type must respond to
2722
- * +new+ without arguments, and return an object that respond to +[]=+.
2723
- * * *array_class*: Defaults to Array If another type is provided, it will be used
2724
- * instead of Hash to represent JSON arrays. The type must respond to
2725
- * +new+ without arguments, and return an object that respond to +<<+.
2726
1507
  * * *decimal_class*: Specifies which class to use instead of the default
2727
1508
  * (Float) when parsing decimal numbers. This class must accept a single
2728
1509
  * string argument in its constructor.
2729
1510
  */
2730
- static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1511
+ static VALUE cParserConfig_initialize(VALUE self, VALUE opts)
2731
1512
  {
2732
- GET_PARSER_INIT;
1513
+ rb_check_frozen(self);
1514
+ GET_PARSER_CONFIG;
2733
1515
 
2734
- rb_check_arity(argc, 1, 2);
1516
+ parser_config_init(config, opts);
1517
+
1518
+ RB_OBJ_WRITTEN(self, Qundef, config->decimal_class);
2735
1519
 
2736
- parser_init(json, argv[0], argc == 2 ? argv[1] : Qnil);
2737
1520
  return self;
2738
1521
  }
2739
1522
 
1523
+ static VALUE cParser_parse(JSON_ParserConfig *config, VALUE Vsource)
1524
+ {
1525
+ Vsource = convert_encoding(StringValue(Vsource));
1526
+ StringValue(Vsource);
2740
1527
 
2741
- #line 2742 "parser.c"
2742
- enum {JSON_start = 1};
2743
- enum {JSON_first_final = 10};
2744
- enum {JSON_error = 0};
1528
+ VALUE rvalue_stack_buffer[RVALUE_STACK_INITIAL_CAPA];
1529
+ rvalue_stack stack = {
1530
+ .type = RVALUE_STACK_STACK_ALLOCATED,
1531
+ .ptr = rvalue_stack_buffer,
1532
+ .capa = RVALUE_STACK_INITIAL_CAPA,
1533
+ };
2745
1534
 
2746
- enum {JSON_en_main = 1};
1535
+ long len;
1536
+ const char *start;
1537
+ RSTRING_GETMEM(Vsource, start, len);
1538
+
1539
+ JSON_ParserState _state = {
1540
+ .start = start,
1541
+ .cursor = start,
1542
+ .end = start + len,
1543
+ .stack = &stack,
1544
+ };
1545
+ JSON_ParserState *state = &_state;
1546
+
1547
+ VALUE result = json_parse_any(state, config);
2747
1548
 
1549
+ // This may be skipped in case of exception, but
1550
+ // it won't cause a leak.
1551
+ rvalue_stack_eagerly_release(state->stack_handle);
2748
1552
 
2749
- #line 1257 "parser.rl"
1553
+ json_ensure_eof(state);
2750
1554
 
1555
+ return result;
1556
+ }
2751
1557
 
2752
1558
  /*
2753
- * call-seq: parse()
1559
+ * call-seq: parse(source)
2754
1560
  *
2755
1561
  * Parses the current JSON text _source_ and returns the complete data
2756
1562
  * structure as a result.
2757
1563
  * It raises JSON::ParserError if fail to parse.
2758
1564
  */
2759
- static VALUE cParser_parse(VALUE self)
1565
+ static VALUE cParserConfig_parse(VALUE self, VALUE Vsource)
2760
1566
  {
2761
- char *p, *pe;
2762
- int cs = EVIL;
2763
- VALUE result = Qnil;
2764
- GET_PARSER;
2765
-
2766
- char stack_buffer[FBUFFER_STACK_SIZE];
2767
- fbuffer_stack_init(&json->fbuffer, FBUFFER_INITIAL_LENGTH_DEFAULT, stack_buffer, FBUFFER_STACK_SIZE);
2768
-
2769
- VALUE rvalue_stack_buffer[RVALUE_STACK_INITIAL_CAPA];
2770
- rvalue_stack stack = {
2771
- .type = RVALUE_STACK_STACK_ALLOCATED,
2772
- .ptr = rvalue_stack_buffer,
2773
- .capa = RVALUE_STACK_INITIAL_CAPA,
2774
- };
2775
- json->stack = &stack;
2776
-
2777
-
2778
- #line 2779 "parser.c"
2779
- {
2780
- cs = JSON_start;
2781
- }
2782
-
2783
- #line 1285 "parser.rl"
2784
- p = json->source;
2785
- pe = p + json->len;
2786
-
2787
- #line 2788 "parser.c"
2788
- {
2789
- if ( p == pe )
2790
- goto _test_eof;
2791
- switch ( cs )
2792
- {
2793
- st1:
2794
- if ( ++p == pe )
2795
- goto _test_eof1;
2796
- case 1:
2797
- switch( (*p) ) {
2798
- case 13: goto st1;
2799
- case 32: goto st1;
2800
- case 34: goto tr2;
2801
- case 45: goto tr2;
2802
- case 47: goto st6;
2803
- case 73: goto tr2;
2804
- case 78: goto tr2;
2805
- case 91: goto tr2;
2806
- case 102: goto tr2;
2807
- case 110: goto tr2;
2808
- case 116: goto tr2;
2809
- case 123: goto tr2;
2810
- }
2811
- if ( (*p) > 10 ) {
2812
- if ( 48 <= (*p) && (*p) <= 57 )
2813
- goto tr2;
2814
- } else if ( (*p) >= 9 )
2815
- goto st1;
2816
- goto st0;
2817
- st0:
2818
- cs = 0;
2819
- goto _out;
2820
- tr2:
2821
- #line 1249 "parser.rl"
2822
- {
2823
- char *np = JSON_parse_value(json, p, pe, &result, 0);
2824
- if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
2825
- }
2826
- goto st10;
2827
- st10:
2828
- if ( ++p == pe )
2829
- goto _test_eof10;
2830
- case 10:
2831
- #line 2832 "parser.c"
2832
- switch( (*p) ) {
2833
- case 13: goto st10;
2834
- case 32: goto st10;
2835
- case 47: goto st2;
2836
- }
2837
- if ( 9 <= (*p) && (*p) <= 10 )
2838
- goto st10;
2839
- goto st0;
2840
- st2:
2841
- if ( ++p == pe )
2842
- goto _test_eof2;
2843
- case 2:
2844
- switch( (*p) ) {
2845
- case 42: goto st3;
2846
- case 47: goto st5;
2847
- }
2848
- goto st0;
2849
- st3:
2850
- if ( ++p == pe )
2851
- goto _test_eof3;
2852
- case 3:
2853
- if ( (*p) == 42 )
2854
- goto st4;
2855
- goto st3;
2856
- st4:
2857
- if ( ++p == pe )
2858
- goto _test_eof4;
2859
- case 4:
2860
- switch( (*p) ) {
2861
- case 42: goto st4;
2862
- case 47: goto st10;
2863
- }
2864
- goto st3;
2865
- st5:
2866
- if ( ++p == pe )
2867
- goto _test_eof5;
2868
- case 5:
2869
- if ( (*p) == 10 )
2870
- goto st10;
2871
- goto st5;
2872
- st6:
2873
- if ( ++p == pe )
2874
- goto _test_eof6;
2875
- case 6:
2876
- switch( (*p) ) {
2877
- case 42: goto st7;
2878
- case 47: goto st9;
2879
- }
2880
- goto st0;
2881
- st7:
2882
- if ( ++p == pe )
2883
- goto _test_eof7;
2884
- case 7:
2885
- if ( (*p) == 42 )
2886
- goto st8;
2887
- goto st7;
2888
- st8:
2889
- if ( ++p == pe )
2890
- goto _test_eof8;
2891
- case 8:
2892
- switch( (*p) ) {
2893
- case 42: goto st8;
2894
- case 47: goto st1;
2895
- }
2896
- goto st7;
2897
- st9:
2898
- if ( ++p == pe )
2899
- goto _test_eof9;
2900
- case 9:
2901
- if ( (*p) == 10 )
2902
- goto st1;
2903
- goto st9;
2904
- }
2905
- _test_eof1: cs = 1; goto _test_eof;
2906
- _test_eof10: cs = 10; goto _test_eof;
2907
- _test_eof2: cs = 2; goto _test_eof;
2908
- _test_eof3: cs = 3; goto _test_eof;
2909
- _test_eof4: cs = 4; goto _test_eof;
2910
- _test_eof5: cs = 5; goto _test_eof;
2911
- _test_eof6: cs = 6; goto _test_eof;
2912
- _test_eof7: cs = 7; goto _test_eof;
2913
- _test_eof8: cs = 8; goto _test_eof;
2914
- _test_eof9: cs = 9; goto _test_eof;
2915
-
2916
- _test_eof: {}
2917
- _out: {}
2918
- }
2919
-
2920
- #line 1288 "parser.rl"
2921
-
2922
- if (json->stack_handle) {
2923
- rvalue_stack_eagerly_release(json->stack_handle);
2924
- }
2925
-
2926
- if (cs >= JSON_first_final && p == pe) {
2927
- return result;
2928
- } else {
2929
- raise_parse_error("unexpected token at '%s'", p);
2930
- return Qnil;
2931
- }
1567
+ GET_PARSER_CONFIG;
1568
+ return cParser_parse(config, Vsource);
2932
1569
  }
2933
1570
 
2934
- static VALUE cParser_m_parse(VALUE klass, VALUE source, VALUE opts)
1571
+ static VALUE cParser_m_parse(VALUE klass, VALUE Vsource, VALUE opts)
2935
1572
  {
2936
- char *p, *pe;
2937
- int cs = EVIL;
2938
- VALUE result = Qnil;
1573
+ Vsource = convert_encoding(StringValue(Vsource));
1574
+ StringValue(Vsource);
2939
1575
 
2940
- JSON_Parser _parser = {0};
2941
- JSON_Parser *json = &_parser;
2942
- parser_init(json, source, opts);
2943
-
2944
- char stack_buffer[FBUFFER_STACK_SIZE];
2945
- fbuffer_stack_init(&json->fbuffer, FBUFFER_INITIAL_LENGTH_DEFAULT, stack_buffer, FBUFFER_STACK_SIZE);
2946
-
2947
- VALUE rvalue_stack_buffer[RVALUE_STACK_INITIAL_CAPA];
2948
- rvalue_stack stack = {
2949
- .type = RVALUE_STACK_STACK_ALLOCATED,
2950
- .ptr = rvalue_stack_buffer,
2951
- .capa = RVALUE_STACK_INITIAL_CAPA,
2952
- };
2953
- json->stack = &stack;
2954
-
2955
-
2956
- #line 2957 "parser.c"
2957
- {
2958
- cs = JSON_start;
2959
- }
2960
-
2961
- #line 1323 "parser.rl"
2962
- p = json->source;
2963
- pe = p + json->len;
2964
-
2965
- #line 2966 "parser.c"
2966
- {
2967
- if ( p == pe )
2968
- goto _test_eof;
2969
- switch ( cs )
2970
- {
2971
- st1:
2972
- if ( ++p == pe )
2973
- goto _test_eof1;
2974
- case 1:
2975
- switch( (*p) ) {
2976
- case 13: goto st1;
2977
- case 32: goto st1;
2978
- case 34: goto tr2;
2979
- case 45: goto tr2;
2980
- case 47: goto st6;
2981
- case 73: goto tr2;
2982
- case 78: goto tr2;
2983
- case 91: goto tr2;
2984
- case 102: goto tr2;
2985
- case 110: goto tr2;
2986
- case 116: goto tr2;
2987
- case 123: goto tr2;
2988
- }
2989
- if ( (*p) > 10 ) {
2990
- if ( 48 <= (*p) && (*p) <= 57 )
2991
- goto tr2;
2992
- } else if ( (*p) >= 9 )
2993
- goto st1;
2994
- goto st0;
2995
- st0:
2996
- cs = 0;
2997
- goto _out;
2998
- tr2:
2999
- #line 1249 "parser.rl"
3000
- {
3001
- char *np = JSON_parse_value(json, p, pe, &result, 0);
3002
- if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
3003
- }
3004
- goto st10;
3005
- st10:
3006
- if ( ++p == pe )
3007
- goto _test_eof10;
3008
- case 10:
3009
- #line 3010 "parser.c"
3010
- switch( (*p) ) {
3011
- case 13: goto st10;
3012
- case 32: goto st10;
3013
- case 47: goto st2;
3014
- }
3015
- if ( 9 <= (*p) && (*p) <= 10 )
3016
- goto st10;
3017
- goto st0;
3018
- st2:
3019
- if ( ++p == pe )
3020
- goto _test_eof2;
3021
- case 2:
3022
- switch( (*p) ) {
3023
- case 42: goto st3;
3024
- case 47: goto st5;
3025
- }
3026
- goto st0;
3027
- st3:
3028
- if ( ++p == pe )
3029
- goto _test_eof3;
3030
- case 3:
3031
- if ( (*p) == 42 )
3032
- goto st4;
3033
- goto st3;
3034
- st4:
3035
- if ( ++p == pe )
3036
- goto _test_eof4;
3037
- case 4:
3038
- switch( (*p) ) {
3039
- case 42: goto st4;
3040
- case 47: goto st10;
3041
- }
3042
- goto st3;
3043
- st5:
3044
- if ( ++p == pe )
3045
- goto _test_eof5;
3046
- case 5:
3047
- if ( (*p) == 10 )
3048
- goto st10;
3049
- goto st5;
3050
- st6:
3051
- if ( ++p == pe )
3052
- goto _test_eof6;
3053
- case 6:
3054
- switch( (*p) ) {
3055
- case 42: goto st7;
3056
- case 47: goto st9;
3057
- }
3058
- goto st0;
3059
- st7:
3060
- if ( ++p == pe )
3061
- goto _test_eof7;
3062
- case 7:
3063
- if ( (*p) == 42 )
3064
- goto st8;
3065
- goto st7;
3066
- st8:
3067
- if ( ++p == pe )
3068
- goto _test_eof8;
3069
- case 8:
3070
- switch( (*p) ) {
3071
- case 42: goto st8;
3072
- case 47: goto st1;
3073
- }
3074
- goto st7;
3075
- st9:
3076
- if ( ++p == pe )
3077
- goto _test_eof9;
3078
- case 9:
3079
- if ( (*p) == 10 )
3080
- goto st1;
3081
- goto st9;
3082
- }
3083
- _test_eof1: cs = 1; goto _test_eof;
3084
- _test_eof10: cs = 10; goto _test_eof;
3085
- _test_eof2: cs = 2; goto _test_eof;
3086
- _test_eof3: cs = 3; goto _test_eof;
3087
- _test_eof4: cs = 4; goto _test_eof;
3088
- _test_eof5: cs = 5; goto _test_eof;
3089
- _test_eof6: cs = 6; goto _test_eof;
3090
- _test_eof7: cs = 7; goto _test_eof;
3091
- _test_eof8: cs = 8; goto _test_eof;
3092
- _test_eof9: cs = 9; goto _test_eof;
3093
-
3094
- _test_eof: {}
3095
- _out: {}
3096
- }
3097
-
3098
- #line 1326 "parser.rl"
3099
-
3100
- if (json->stack_handle) {
3101
- rvalue_stack_eagerly_release(json->stack_handle);
3102
- }
1576
+ JSON_ParserConfig _config = {0};
1577
+ JSON_ParserConfig *config = &_config;
1578
+ parser_config_init(config, opts);
3103
1579
 
3104
- if (cs >= JSON_first_final && p == pe) {
3105
- return result;
3106
- } else {
3107
- raise_parse_error("unexpected token at '%s'", p);
3108
- return Qnil;
3109
- }
1580
+ return cParser_parse(config, Vsource);
3110
1581
  }
3111
1582
 
3112
- static void JSON_mark(void *ptr)
1583
+ static void JSON_ParserConfig_mark(void *ptr)
3113
1584
  {
3114
- JSON_Parser *json = ptr;
3115
- rb_gc_mark(json->Vsource);
3116
- rb_gc_mark(json->create_id);
3117
- rb_gc_mark(json->object_class);
3118
- rb_gc_mark(json->array_class);
3119
- rb_gc_mark(json->decimal_class);
3120
- rb_gc_mark(json->match_string);
3121
- rb_gc_mark(json->stack_handle);
3122
-
3123
- long index;
3124
- for (index = 0; index < json->name_cache.length; index++) {
3125
- rb_gc_mark(json->name_cache.entries[index]);
3126
- }
1585
+ JSON_ParserConfig *config = ptr;
1586
+ rb_gc_mark(config->on_load_proc);
1587
+ rb_gc_mark(config->decimal_class);
3127
1588
  }
3128
1589
 
3129
- static void JSON_free(void *ptr)
1590
+ static void JSON_ParserConfig_free(void *ptr)
3130
1591
  {
3131
- JSON_Parser *json = ptr;
3132
- fbuffer_free(&json->fbuffer);
3133
- ruby_xfree(json);
1592
+ JSON_ParserConfig *config = ptr;
1593
+ ruby_xfree(config);
3134
1594
  }
3135
1595
 
3136
- static size_t JSON_memsize(const void *ptr)
1596
+ static size_t JSON_ParserConfig_memsize(const void *ptr)
3137
1597
  {
3138
- const JSON_Parser *json = ptr;
3139
- return sizeof(*json) + FBUFFER_CAPA(&json->fbuffer);
1598
+ return sizeof(JSON_ParserConfig);
3140
1599
  }
3141
1600
 
3142
- static const rb_data_type_t JSON_Parser_type = {
3143
- "JSON/Parser",
3144
- {JSON_mark, JSON_free, JSON_memsize,},
1601
+ static const rb_data_type_t JSON_ParserConfig_type = {
1602
+ "JSON::Ext::Parser/ParserConfig",
1603
+ {
1604
+ JSON_ParserConfig_mark,
1605
+ JSON_ParserConfig_free,
1606
+ JSON_ParserConfig_memsize,
1607
+ },
3145
1608
  0, 0,
3146
- RUBY_TYPED_FREE_IMMEDIATELY,
1609
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
3147
1610
  };
3148
1611
 
3149
1612
  static VALUE cJSON_parser_s_allocate(VALUE klass)
3150
1613
  {
3151
- JSON_Parser *json;
3152
- VALUE obj = TypedData_Make_Struct(klass, JSON_Parser, &JSON_Parser_type, json);
3153
- fbuffer_stack_init(&json->fbuffer, 0, NULL, 0);
3154
- return obj;
3155
- }
3156
-
3157
- /*
3158
- * call-seq: source()
3159
- *
3160
- * Returns a copy of the current _source_ string, that was used to construct
3161
- * this Parser.
3162
- */
3163
- static VALUE cParser_source(VALUE self)
3164
- {
3165
- GET_PARSER;
3166
- return rb_str_dup(json->Vsource);
1614
+ JSON_ParserConfig *config;
1615
+ return TypedData_Make_Struct(klass, JSON_ParserConfig, &JSON_ParserConfig_type, config);
3167
1616
  }
3168
1617
 
3169
1618
  void Init_parser(void)
@@ -3175,15 +1624,15 @@ void Init_parser(void)
3175
1624
  #undef rb_intern
3176
1625
  rb_require("json/common");
3177
1626
  mJSON = rb_define_module("JSON");
3178
- mExt = rb_define_module_under(mJSON, "Ext");
3179
- cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
1627
+ VALUE mExt = rb_define_module_under(mJSON, "Ext");
1628
+ VALUE cParserConfig = rb_define_class_under(mExt, "ParserConfig", rb_cObject);
3180
1629
  eNestingError = rb_path2class("JSON::NestingError");
3181
1630
  rb_gc_register_mark_object(eNestingError);
3182
- rb_define_alloc_func(cParser, cJSON_parser_s_allocate);
3183
- rb_define_method(cParser, "initialize", cParser_initialize, -1);
3184
- rb_define_method(cParser, "parse", cParser_parse, 0);
3185
- rb_define_method(cParser, "source", cParser_source, 0);
1631
+ rb_define_alloc_func(cParserConfig, cJSON_parser_s_allocate);
1632
+ rb_define_method(cParserConfig, "initialize", cParserConfig_initialize, 1);
1633
+ rb_define_method(cParserConfig, "parse", cParserConfig_parse, 1);
3186
1634
 
1635
+ VALUE cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
3187
1636
  rb_define_singleton_method(cParser, "parse", cParser_m_parse, 2);
3188
1637
 
3189
1638
  CNaN = rb_const_get(mJSON, rb_intern("NaN"));
@@ -3201,24 +1650,13 @@ void Init_parser(void)
3201
1650
  sym_max_nesting = ID2SYM(rb_intern("max_nesting"));
3202
1651
  sym_allow_nan = ID2SYM(rb_intern("allow_nan"));
3203
1652
  sym_allow_trailing_comma = ID2SYM(rb_intern("allow_trailing_comma"));
1653
+ sym_allow_control_characters = ID2SYM(rb_intern("allow_control_characters"));
3204
1654
  sym_symbolize_names = ID2SYM(rb_intern("symbolize_names"));
3205
1655
  sym_freeze = ID2SYM(rb_intern("freeze"));
3206
- sym_create_additions = ID2SYM(rb_intern("create_additions"));
3207
- sym_create_id = ID2SYM(rb_intern("create_id"));
3208
- sym_object_class = ID2SYM(rb_intern("object_class"));
3209
- sym_array_class = ID2SYM(rb_intern("array_class"));
1656
+ sym_on_load = ID2SYM(rb_intern("on_load"));
3210
1657
  sym_decimal_class = ID2SYM(rb_intern("decimal_class"));
3211
- sym_match_string = ID2SYM(rb_intern("match_string"));
3212
-
3213
- i_create_id = rb_intern("create_id");
3214
- i_json_creatable_p = rb_intern("json_creatable?");
3215
- i_json_create = rb_intern("json_create");
3216
- i_chr = rb_intern("chr");
3217
- i_match = rb_intern("match");
3218
- i_deep_const_get = rb_intern("deep_const_get");
3219
- i_aset = rb_intern("[]=");
3220
- i_aref = rb_intern("[]");
3221
- i_leftshift = rb_intern("<<");
1658
+ sym_allow_duplicate_key = ID2SYM(rb_intern("allow_duplicate_key"));
1659
+
3222
1660
  i_new = rb_intern("new");
3223
1661
  i_try_convert = rb_intern("try_convert");
3224
1662
  i_uminus = rb_intern("-@");
@@ -3227,12 +1665,8 @@ void Init_parser(void)
3227
1665
  binary_encindex = rb_ascii8bit_encindex();
3228
1666
  utf8_encindex = rb_utf8_encindex();
3229
1667
  enc_utf8 = rb_utf8_encoding();
3230
- }
3231
1668
 
3232
- /*
3233
- * Local variables:
3234
- * mode: c
3235
- * c-file-style: ruby
3236
- * indent-tabs-mode: nil
3237
- * End:
3238
- */
1669
+ #ifdef HAVE_SIMD
1670
+ simd_impl = find_simd_implementation();
1671
+ #endif
1672
+ }