json 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of json might be problematic. Click here for more details.

@@ -2,8 +2,8 @@ require 'mkmf'
2
2
  require 'rbconfig'
3
3
 
4
4
  if CONFIG['CC'] =~ /gcc/
5
- #CONFIG['CC'] += ' -Wall -ggdb'
6
- CONFIG['CC'] += ' -Wall'
5
+ #$CFLAGS += ' -Wall -ggdb'
6
+ $CFLAGS += ' -Wall'
7
7
  end
8
8
 
9
9
  create_makefile 'parser'
@@ -9,8 +9,12 @@
9
9
  #define EVIL 0x666
10
10
 
11
11
  static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
12
+ static VALUE CNaN, CInfinity, CMinusInfinity;
12
13
 
13
- static ID i_json_creatable_p, i_json_create, i_create_id, i_chr, i_max_nesting;
14
+ static ID i_json_creatable_p, i_json_create, i_create_id, i_chr, i_max_nesting,
15
+ i_allow_nan;
16
+
17
+ #define MinusInfinity "-Infinity"
14
18
 
15
19
  typedef struct JSON_ParserStruct {
16
20
  VALUE Vsource;
@@ -20,6 +24,7 @@ typedef struct JSON_ParserStruct {
20
24
  VALUE create_id;
21
25
  int max_nesting;
22
26
  int current_nesting;
27
+ int allow_nan;
23
28
  } JSON_Parser;
24
29
 
25
30
  static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result);
@@ -33,18 +38,18 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
33
38
  JSON_Parser *json; \
34
39
  Data_Get_Struct(self, JSON_Parser, json);
35
40
 
36
- #line 58 "parser.rl"
41
+ #line 66 "parser.rl"
37
42
 
38
43
 
39
44
 
40
- #line 41 "parser.c"
45
+ #line 46 "parser.c"
41
46
  static const int JSON_object_start = 1;
42
47
  static const int JSON_object_first_final = 27;
43
48
  static const int JSON_object_error = 0;
44
49
 
45
50
  static const int JSON_object_en_main = 1;
46
51
 
47
- #line 91 "parser.rl"
52
+ #line 99 "parser.rl"
48
53
 
49
54
 
50
55
  static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -59,13 +64,13 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
59
64
  *result = rb_hash_new();
60
65
 
61
66
 
62
- #line 63 "parser.c"
67
+ #line 68 "parser.c"
63
68
  {
64
69
  cs = JSON_object_start;
65
70
  }
66
- #line 105 "parser.rl"
71
+ #line 113 "parser.rl"
67
72
 
68
- #line 69 "parser.c"
73
+ #line 74 "parser.c"
69
74
  {
70
75
  if ( p == pe )
71
76
  goto _out;
@@ -92,7 +97,7 @@ case 2:
92
97
  goto st2;
93
98
  goto st0;
94
99
  tr2:
95
- #line 77 "parser.rl"
100
+ #line 85 "parser.rl"
96
101
  {
97
102
  char *np = JSON_parse_string(json, p, pe, &last_name);
98
103
  if (np == NULL) goto _out3; else {p = (( np))-1;}
@@ -102,7 +107,7 @@ st3:
102
107
  if ( ++p == pe )
103
108
  goto _out3;
104
109
  case 3:
105
- #line 106 "parser.c"
110
+ #line 111 "parser.c"
106
111
  switch( (*p) ) {
107
112
  case 13: goto st3;
108
113
  case 32: goto st3;
@@ -154,6 +159,8 @@ case 8:
154
159
  case 34: goto tr11;
155
160
  case 45: goto tr11;
156
161
  case 47: goto st19;
162
+ case 73: goto tr11;
163
+ case 78: goto tr11;
157
164
  case 91: goto tr11;
158
165
  case 102: goto tr11;
159
166
  case 110: goto tr11;
@@ -167,7 +174,7 @@ case 8:
167
174
  goto st8;
168
175
  goto st0;
169
176
  tr11:
170
- #line 66 "parser.rl"
177
+ #line 74 "parser.rl"
171
178
  {
172
179
  VALUE v = Qnil;
173
180
  char *np = JSON_parse_value(json, p, pe, &v);
@@ -183,7 +190,7 @@ st9:
183
190
  if ( ++p == pe )
184
191
  goto _out9;
185
192
  case 9:
186
- #line 187 "parser.c"
193
+ #line 194 "parser.c"
187
194
  switch( (*p) ) {
188
195
  case 13: goto st9;
189
196
  case 32: goto st9;
@@ -272,14 +279,14 @@ case 18:
272
279
  goto st9;
273
280
  goto st18;
274
281
  tr4:
275
- #line 82 "parser.rl"
282
+ #line 90 "parser.rl"
276
283
  { goto _out27; }
277
284
  goto st27;
278
285
  st27:
279
286
  if ( ++p == pe )
280
287
  goto _out27;
281
288
  case 27:
282
- #line 283 "parser.c"
289
+ #line 290 "parser.c"
283
290
  goto st0;
284
291
  st19:
285
292
  if ( ++p == pe )
@@ -376,7 +383,7 @@ case 26:
376
383
 
377
384
  _out: {}
378
385
  }
379
- #line 106 "parser.rl"
386
+ #line 114 "parser.rl"
380
387
 
381
388
  if (cs >= JSON_object_first_final) {
382
389
  VALUE klassname = rb_hash_aref(*result, json->create_id);
@@ -393,14 +400,14 @@ case 26:
393
400
  }
394
401
 
395
402
 
396
- #line 397 "parser.c"
403
+ #line 404 "parser.c"
397
404
  static const int JSON_value_start = 1;
398
- static const int JSON_value_first_final = 12;
405
+ static const int JSON_value_first_final = 21;
399
406
  static const int JSON_value_error = 0;
400
407
 
401
408
  static const int JSON_value_en_main = 1;
402
409
 
403
- #line 177 "parser.rl"
410
+ #line 210 "parser.rl"
404
411
 
405
412
 
406
413
  static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -408,13 +415,13 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
408
415
  int cs = EVIL;
409
416
 
410
417
 
411
- #line 412 "parser.c"
418
+ #line 419 "parser.c"
412
419
  {
413
420
  cs = JSON_value_start;
414
421
  }
415
- #line 184 "parser.rl"
422
+ #line 217 "parser.rl"
416
423
 
417
- #line 418 "parser.c"
424
+ #line 425 "parser.c"
418
425
  {
419
426
  if ( p == pe )
420
427
  goto _out;
@@ -424,11 +431,13 @@ case 1:
424
431
  switch( (*p) ) {
425
432
  case 34: goto tr0;
426
433
  case 45: goto tr2;
427
- case 91: goto tr3;
428
- case 102: goto st2;
429
- case 110: goto st6;
430
- case 116: goto st9;
431
- case 123: goto tr7;
434
+ case 73: goto st2;
435
+ case 78: goto st9;
436
+ case 91: goto tr5;
437
+ case 102: goto st11;
438
+ case 110: goto st15;
439
+ case 116: goto st18;
440
+ case 123: goto tr9;
432
441
  }
433
442
  if ( 48 <= (*p) && (*p) <= 57 )
434
443
  goto tr2;
@@ -436,142 +445,234 @@ case 1:
436
445
  st0:
437
446
  goto _out0;
438
447
  tr0:
439
- #line 136 "parser.rl"
448
+ #line 158 "parser.rl"
440
449
  {
441
450
  char *np = JSON_parse_string(json, p, pe, result);
442
- if (np == NULL) goto _out12; else {p = (( np))-1;}
451
+ if (np == NULL) goto _out21; else {p = (( np))-1;}
443
452
  }
444
- goto st12;
453
+ goto st21;
445
454
  tr2:
446
- #line 141 "parser.rl"
455
+ #line 163 "parser.rl"
447
456
  {
448
457
  char *np;
458
+ if(pe > p + 9 && !strncmp(MinusInfinity, p, 9)) {
459
+ if (json->allow_nan) {
460
+ *result = CMinusInfinity;
461
+ {p = (( p + 10))-1;}
462
+ goto _out21;
463
+ } else {
464
+ rb_raise(eParserError, "unexpected token at '%s'", p);
465
+ }
466
+ }
449
467
  np = JSON_parse_float(json, p, pe, result);
450
468
  if (np != NULL) {p = (( np))-1;}
451
469
  np = JSON_parse_integer(json, p, pe, result);
452
470
  if (np != NULL) {p = (( np))-1;}
453
- goto _out12;
471
+ goto _out21;
454
472
  }
455
- goto st12;
456
- tr3:
457
- #line 150 "parser.rl"
473
+ goto st21;
474
+ tr5:
475
+ #line 181 "parser.rl"
458
476
  {
459
477
  char *np;
460
478
  json->current_nesting += 1;
461
479
  np = JSON_parse_array(json, p, pe, result);
462
480
  json->current_nesting -= 1;
463
- if (np == NULL) goto _out12; else {p = (( np))-1;}
481
+ if (np == NULL) goto _out21; else {p = (( np))-1;}
464
482
  }
465
- goto st12;
466
- tr7:
467
- #line 158 "parser.rl"
483
+ goto st21;
484
+ tr9:
485
+ #line 189 "parser.rl"
468
486
  {
469
487
  char *np;
470
488
  json->current_nesting += 1;
471
489
  np = JSON_parse_object(json, p, pe, result);
472
490
  json->current_nesting -= 1;
473
- if (np == NULL) goto _out12; else {p = (( np))-1;}
491
+ if (np == NULL) goto _out21; else {p = (( np))-1;}
474
492
  }
475
- goto st12;
476
- tr11:
477
- #line 130 "parser.rl"
493
+ goto st21;
494
+ tr16:
495
+ #line 151 "parser.rl"
496
+ {
497
+ if (json->allow_nan) {
498
+ *result = CInfinity;
499
+ } else {
500
+ rb_raise(eParserError, "unexpected token at '%s'", p - 8);
501
+ }
502
+ }
503
+ goto st21;
504
+ tr18:
505
+ #line 144 "parser.rl"
506
+ {
507
+ if (json->allow_nan) {
508
+ *result = CNaN;
509
+ } else {
510
+ rb_raise(eParserError, "unexpected token at '%s'", p - 2);
511
+ }
512
+ }
513
+ goto st21;
514
+ tr22:
515
+ #line 138 "parser.rl"
478
516
  {
479
517
  *result = Qfalse;
480
518
  }
481
- goto st12;
482
- tr14:
483
- #line 127 "parser.rl"
519
+ goto st21;
520
+ tr25:
521
+ #line 135 "parser.rl"
484
522
  {
485
523
  *result = Qnil;
486
524
  }
487
- goto st12;
488
- tr17:
489
- #line 133 "parser.rl"
525
+ goto st21;
526
+ tr28:
527
+ #line 141 "parser.rl"
490
528
  {
491
529
  *result = Qtrue;
492
530
  }
493
- goto st12;
494
- st12:
531
+ goto st21;
532
+ st21:
495
533
  if ( ++p == pe )
496
- goto _out12;
497
- case 12:
498
- #line 166 "parser.rl"
499
- { goto _out12; }
500
- #line 501 "parser.c"
534
+ goto _out21;
535
+ case 21:
536
+ #line 197 "parser.rl"
537
+ { goto _out21; }
538
+ #line 539 "parser.c"
501
539
  goto st0;
502
540
  st2:
503
541
  if ( ++p == pe )
504
542
  goto _out2;
505
543
  case 2:
506
- if ( (*p) == 97 )
544
+ if ( (*p) == 110 )
507
545
  goto st3;
508
546
  goto st0;
509
547
  st3:
510
548
  if ( ++p == pe )
511
549
  goto _out3;
512
550
  case 3:
513
- if ( (*p) == 108 )
551
+ if ( (*p) == 102 )
514
552
  goto st4;
515
553
  goto st0;
516
554
  st4:
517
555
  if ( ++p == pe )
518
556
  goto _out4;
519
557
  case 4:
520
- if ( (*p) == 115 )
558
+ if ( (*p) == 105 )
521
559
  goto st5;
522
560
  goto st0;
523
561
  st5:
524
562
  if ( ++p == pe )
525
563
  goto _out5;
526
564
  case 5:
527
- if ( (*p) == 101 )
528
- goto tr11;
565
+ if ( (*p) == 110 )
566
+ goto st6;
529
567
  goto st0;
530
568
  st6:
531
569
  if ( ++p == pe )
532
570
  goto _out6;
533
571
  case 6:
534
- if ( (*p) == 117 )
572
+ if ( (*p) == 105 )
535
573
  goto st7;
536
574
  goto st0;
537
575
  st7:
538
576
  if ( ++p == pe )
539
577
  goto _out7;
540
578
  case 7:
541
- if ( (*p) == 108 )
579
+ if ( (*p) == 116 )
542
580
  goto st8;
543
581
  goto st0;
544
582
  st8:
545
583
  if ( ++p == pe )
546
584
  goto _out8;
547
585
  case 8:
548
- if ( (*p) == 108 )
549
- goto tr14;
586
+ if ( (*p) == 121 )
587
+ goto tr16;
550
588
  goto st0;
551
589
  st9:
552
590
  if ( ++p == pe )
553
591
  goto _out9;
554
592
  case 9:
555
- if ( (*p) == 114 )
593
+ if ( (*p) == 97 )
556
594
  goto st10;
557
595
  goto st0;
558
596
  st10:
559
597
  if ( ++p == pe )
560
598
  goto _out10;
561
599
  case 10:
562
- if ( (*p) == 117 )
563
- goto st11;
600
+ if ( (*p) == 78 )
601
+ goto tr18;
564
602
  goto st0;
565
603
  st11:
566
604
  if ( ++p == pe )
567
605
  goto _out11;
568
606
  case 11:
607
+ if ( (*p) == 97 )
608
+ goto st12;
609
+ goto st0;
610
+ st12:
611
+ if ( ++p == pe )
612
+ goto _out12;
613
+ case 12:
614
+ if ( (*p) == 108 )
615
+ goto st13;
616
+ goto st0;
617
+ st13:
618
+ if ( ++p == pe )
619
+ goto _out13;
620
+ case 13:
621
+ if ( (*p) == 115 )
622
+ goto st14;
623
+ goto st0;
624
+ st14:
625
+ if ( ++p == pe )
626
+ goto _out14;
627
+ case 14:
628
+ if ( (*p) == 101 )
629
+ goto tr22;
630
+ goto st0;
631
+ st15:
632
+ if ( ++p == pe )
633
+ goto _out15;
634
+ case 15:
635
+ if ( (*p) == 117 )
636
+ goto st16;
637
+ goto st0;
638
+ st16:
639
+ if ( ++p == pe )
640
+ goto _out16;
641
+ case 16:
642
+ if ( (*p) == 108 )
643
+ goto st17;
644
+ goto st0;
645
+ st17:
646
+ if ( ++p == pe )
647
+ goto _out17;
648
+ case 17:
649
+ if ( (*p) == 108 )
650
+ goto tr25;
651
+ goto st0;
652
+ st18:
653
+ if ( ++p == pe )
654
+ goto _out18;
655
+ case 18:
656
+ if ( (*p) == 114 )
657
+ goto st19;
658
+ goto st0;
659
+ st19:
660
+ if ( ++p == pe )
661
+ goto _out19;
662
+ case 19:
663
+ if ( (*p) == 117 )
664
+ goto st20;
665
+ goto st0;
666
+ st20:
667
+ if ( ++p == pe )
668
+ goto _out20;
669
+ case 20:
569
670
  if ( (*p) == 101 )
570
- goto tr17;
671
+ goto tr28;
571
672
  goto st0;
572
673
  }
573
674
  _out0: cs = 0; goto _out;
574
- _out12: cs = 12; goto _out;
675
+ _out21: cs = 21; goto _out;
575
676
  _out2: cs = 2; goto _out;
576
677
  _out3: cs = 3; goto _out;
577
678
  _out4: cs = 4; goto _out;
@@ -582,10 +683,19 @@ case 11:
582
683
  _out9: cs = 9; goto _out;
583
684
  _out10: cs = 10; goto _out;
584
685
  _out11: cs = 11; goto _out;
686
+ _out12: cs = 12; goto _out;
687
+ _out13: cs = 13; goto _out;
688
+ _out14: cs = 14; goto _out;
689
+ _out15: cs = 15; goto _out;
690
+ _out16: cs = 16; goto _out;
691
+ _out17: cs = 17; goto _out;
692
+ _out18: cs = 18; goto _out;
693
+ _out19: cs = 19; goto _out;
694
+ _out20: cs = 20; goto _out;
585
695
 
586
696
  _out: {}
587
697
  }
588
- #line 185 "parser.rl"
698
+ #line 218 "parser.rl"
589
699
 
590
700
  if (cs >= JSON_value_first_final) {
591
701
  return p;
@@ -595,14 +705,14 @@ case 11:
595
705
  }
596
706
 
597
707
 
598
- #line 599 "parser.c"
708
+ #line 709 "parser.c"
599
709
  static const int JSON_integer_start = 1;
600
710
  static const int JSON_integer_first_final = 5;
601
711
  static const int JSON_integer_error = 0;
602
712
 
603
713
  static const int JSON_integer_en_main = 1;
604
714
 
605
- #line 201 "parser.rl"
715
+ #line 234 "parser.rl"
606
716
 
607
717
 
608
718
  static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -610,14 +720,14 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
610
720
  int cs = EVIL;
611
721
 
612
722
 
613
- #line 614 "parser.c"
723
+ #line 724 "parser.c"
614
724
  {
615
725
  cs = JSON_integer_start;
616
726
  }
617
- #line 208 "parser.rl"
727
+ #line 241 "parser.rl"
618
728
  json->memo = p;
619
729
 
620
- #line 621 "parser.c"
730
+ #line 731 "parser.c"
621
731
  {
622
732
  if ( p == pe )
623
733
  goto _out;
@@ -650,14 +760,14 @@ case 3:
650
760
  goto st0;
651
761
  goto tr4;
652
762
  tr4:
653
- #line 198 "parser.rl"
763
+ #line 231 "parser.rl"
654
764
  { goto _out5; }
655
765
  goto st5;
656
766
  st5:
657
767
  if ( ++p == pe )
658
768
  goto _out5;
659
769
  case 5:
660
- #line 661 "parser.c"
770
+ #line 771 "parser.c"
661
771
  goto st0;
662
772
  st4:
663
773
  if ( ++p == pe )
@@ -675,7 +785,7 @@ case 4:
675
785
 
676
786
  _out: {}
677
787
  }
678
- #line 210 "parser.rl"
788
+ #line 243 "parser.rl"
679
789
 
680
790
  if (cs >= JSON_integer_first_final) {
681
791
  long len = p - json->memo;
@@ -687,14 +797,14 @@ case 4:
687
797
  }
688
798
 
689
799
 
690
- #line 691 "parser.c"
800
+ #line 801 "parser.c"
691
801
  static const int JSON_float_start = 1;
692
802
  static const int JSON_float_first_final = 10;
693
803
  static const int JSON_float_error = 0;
694
804
 
695
805
  static const int JSON_float_en_main = 1;
696
806
 
697
- #line 232 "parser.rl"
807
+ #line 265 "parser.rl"
698
808
 
699
809
 
700
810
  static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -702,14 +812,14 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
702
812
  int cs = EVIL;
703
813
 
704
814
 
705
- #line 706 "parser.c"
815
+ #line 816 "parser.c"
706
816
  {
707
817
  cs = JSON_float_start;
708
818
  }
709
- #line 239 "parser.rl"
819
+ #line 272 "parser.rl"
710
820
  json->memo = p;
711
821
 
712
- #line 713 "parser.c"
822
+ #line 823 "parser.c"
713
823
  {
714
824
  if ( p == pe )
715
825
  goto _out;
@@ -766,14 +876,14 @@ case 5:
766
876
  goto st0;
767
877
  goto tr7;
768
878
  tr7:
769
- #line 226 "parser.rl"
879
+ #line 259 "parser.rl"
770
880
  { goto _out10; }
771
881
  goto st10;
772
882
  st10:
773
883
  if ( ++p == pe )
774
884
  goto _out10;
775
885
  case 10:
776
- #line 777 "parser.c"
886
+ #line 887 "parser.c"
777
887
  goto st0;
778
888
  st6:
779
889
  if ( ++p == pe )
@@ -833,7 +943,7 @@ case 9:
833
943
 
834
944
  _out: {}
835
945
  }
836
- #line 241 "parser.rl"
946
+ #line 274 "parser.rl"
837
947
 
838
948
  if (cs >= JSON_float_first_final) {
839
949
  long len = p - json->memo;
@@ -846,14 +956,14 @@ case 9:
846
956
 
847
957
 
848
958
 
849
- #line 850 "parser.c"
959
+ #line 960 "parser.c"
850
960
  static const int JSON_array_start = 1;
851
961
  static const int JSON_array_first_final = 17;
852
962
  static const int JSON_array_error = 0;
853
963
 
854
964
  static const int JSON_array_en_main = 1;
855
965
 
856
- #line 277 "parser.rl"
966
+ #line 310 "parser.rl"
857
967
 
858
968
 
859
969
  static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -866,13 +976,13 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
866
976
  *result = rb_ary_new();
867
977
 
868
978
 
869
- #line 870 "parser.c"
979
+ #line 980 "parser.c"
870
980
  {
871
981
  cs = JSON_array_start;
872
982
  }
873
- #line 289 "parser.rl"
983
+ #line 322 "parser.rl"
874
984
 
875
- #line 876 "parser.c"
985
+ #line 986 "parser.c"
876
986
  {
877
987
  if ( p == pe )
878
988
  goto _out;
@@ -894,6 +1004,8 @@ case 2:
894
1004
  case 34: goto tr2;
895
1005
  case 45: goto tr2;
896
1006
  case 47: goto st13;
1007
+ case 73: goto tr2;
1008
+ case 78: goto tr2;
897
1009
  case 91: goto tr2;
898
1010
  case 93: goto tr4;
899
1011
  case 102: goto tr2;
@@ -908,7 +1020,7 @@ case 2:
908
1020
  goto st2;
909
1021
  goto st0;
910
1022
  tr2:
911
- #line 258 "parser.rl"
1023
+ #line 291 "parser.rl"
912
1024
  {
913
1025
  VALUE v = Qnil;
914
1026
  char *np = JSON_parse_value(json, p, pe, &v);
@@ -924,7 +1036,7 @@ st3:
924
1036
  if ( ++p == pe )
925
1037
  goto _out3;
926
1038
  case 3:
927
- #line 928 "parser.c"
1039
+ #line 1040 "parser.c"
928
1040
  switch( (*p) ) {
929
1041
  case 13: goto st3;
930
1042
  case 32: goto st3;
@@ -945,6 +1057,8 @@ case 4:
945
1057
  case 34: goto tr2;
946
1058
  case 45: goto tr2;
947
1059
  case 47: goto st5;
1060
+ case 73: goto tr2;
1061
+ case 78: goto tr2;
948
1062
  case 91: goto tr2;
949
1063
  case 102: goto tr2;
950
1064
  case 110: goto tr2;
@@ -1022,14 +1136,14 @@ case 12:
1022
1136
  goto st3;
1023
1137
  goto st12;
1024
1138
  tr4:
1025
- #line 269 "parser.rl"
1139
+ #line 302 "parser.rl"
1026
1140
  { goto _out17; }
1027
1141
  goto st17;
1028
1142
  st17:
1029
1143
  if ( ++p == pe )
1030
1144
  goto _out17;
1031
1145
  case 17:
1032
- #line 1033 "parser.c"
1146
+ #line 1147 "parser.c"
1033
1147
  goto st0;
1034
1148
  st13:
1035
1149
  if ( ++p == pe )
@@ -1084,7 +1198,7 @@ case 16:
1084
1198
 
1085
1199
  _out: {}
1086
1200
  }
1087
- #line 290 "parser.rl"
1201
+ #line 323 "parser.rl"
1088
1202
 
1089
1203
  if(cs >= JSON_array_first_final) {
1090
1204
  return p + 1;
@@ -1150,14 +1264,14 @@ static VALUE json_string_unescape(char *p, char *pe)
1150
1264
  }
1151
1265
 
1152
1266
 
1153
- #line 1154 "parser.c"
1267
+ #line 1268 "parser.c"
1154
1268
  static const int JSON_string_start = 1;
1155
1269
  static const int JSON_string_first_final = 8;
1156
1270
  static const int JSON_string_error = 0;
1157
1271
 
1158
1272
  static const int JSON_string_en_main = 1;
1159
1273
 
1160
- #line 368 "parser.rl"
1274
+ #line 401 "parser.rl"
1161
1275
 
1162
1276
 
1163
1277
  static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -1166,14 +1280,14 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
1166
1280
 
1167
1281
  *result = rb_str_new("", 0);
1168
1282
 
1169
- #line 1170 "parser.c"
1283
+ #line 1284 "parser.c"
1170
1284
  {
1171
1285
  cs = JSON_string_start;
1172
1286
  }
1173
- #line 376 "parser.rl"
1287
+ #line 409 "parser.rl"
1174
1288
  json->memo = p;
1175
1289
 
1176
- #line 1177 "parser.c"
1290
+ #line 1291 "parser.c"
1177
1291
  {
1178
1292
  if ( p == pe )
1179
1293
  goto _out;
@@ -1197,19 +1311,19 @@ case 2:
1197
1311
  goto st0;
1198
1312
  goto st2;
1199
1313
  tr2:
1200
- #line 360 "parser.rl"
1314
+ #line 393 "parser.rl"
1201
1315
  {
1202
1316
  *result = json_string_unescape(json->memo + 1, p);
1203
1317
  if (NIL_P(*result)) goto _out8; else {p = (( p + 1))-1;}
1204
1318
  }
1205
- #line 365 "parser.rl"
1319
+ #line 398 "parser.rl"
1206
1320
  { goto _out8; }
1207
1321
  goto st8;
1208
1322
  st8:
1209
1323
  if ( ++p == pe )
1210
1324
  goto _out8;
1211
1325
  case 8:
1212
- #line 1213 "parser.c"
1326
+ #line 1327 "parser.c"
1213
1327
  goto st0;
1214
1328
  st3:
1215
1329
  if ( ++p == pe )
@@ -1284,7 +1398,7 @@ case 7:
1284
1398
 
1285
1399
  _out: {}
1286
1400
  }
1287
- #line 378 "parser.rl"
1401
+ #line 411 "parser.rl"
1288
1402
 
1289
1403
  if (cs >= JSON_string_first_final) {
1290
1404
  return p + 1;
@@ -1295,14 +1409,14 @@ case 7:
1295
1409
 
1296
1410
 
1297
1411
 
1298
- #line 1299 "parser.c"
1412
+ #line 1413 "parser.c"
1299
1413
  static const int JSON_start = 1;
1300
1414
  static const int JSON_first_final = 10;
1301
1415
  static const int JSON_error = 0;
1302
1416
 
1303
1417
  static const int JSON_en_main = 1;
1304
1418
 
1305
- #line 412 "parser.rl"
1419
+ #line 445 "parser.rl"
1306
1420
 
1307
1421
 
1308
1422
  /*
@@ -1329,7 +1443,11 @@ static const int JSON_en_main = 1;
1329
1443
  *
1330
1444
  * _opts_ can have the following keys:
1331
1445
  * * *max_nesting*: The maximum depth of nesting allowed in the parsed data
1332
- * structures. Disable depth checking with :max_nesting => false.
1446
+ * structures. Disable depth checking with :max_nesting => false|nil|0, it
1447
+ * defaults to 19.
1448
+ * * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
1449
+ * defiance of RFC 4627 to be parsed by the Parser. This option defaults to
1450
+ * false.
1333
1451
  */
1334
1452
  static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1335
1453
  {
@@ -1345,14 +1463,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1345
1463
  rb_raise(eParserError, "A JSON text must at least contain two octets!");
1346
1464
  }
1347
1465
  json->max_nesting = 19;
1466
+ json->allow_nan = 0;
1348
1467
  if (!NIL_P(opts)) {
1349
1468
  opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
1350
1469
  if (NIL_P(opts)) {
1351
1470
  rb_raise(rb_eArgError, "opts needs to be like a hash");
1352
1471
  } else {
1353
- VALUE s_max_nesting = ID2SYM(i_max_nesting);
1354
- if (st_lookup(RHASH(opts)->tbl, s_max_nesting, 0)) {
1355
- VALUE max_nesting = rb_hash_aref(opts, s_max_nesting);
1472
+ VALUE tmp = ID2SYM(i_max_nesting);
1473
+ if (st_lookup(RHASH(opts)->tbl, tmp, 0)) {
1474
+ VALUE max_nesting = rb_hash_aref(opts, tmp);
1356
1475
  if (RTEST(max_nesting)) {
1357
1476
  Check_Type(max_nesting, T_FIXNUM);
1358
1477
  json->max_nesting = FIX2INT(max_nesting);
@@ -1360,6 +1479,11 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1360
1479
  json->max_nesting = 0;
1361
1480
  }
1362
1481
  }
1482
+ tmp = ID2SYM(i_allow_nan);
1483
+ if (st_lookup(RHASH(opts)->tbl, tmp, 0)) {
1484
+ VALUE allow_nan = rb_hash_aref(opts, tmp);
1485
+ if (RTEST(allow_nan)) json->allow_nan = 1;
1486
+ }
1363
1487
  }
1364
1488
  }
1365
1489
  json->current_nesting = 0;
@@ -1396,15 +1520,15 @@ static VALUE cParser_parse(VALUE self)
1396
1520
  GET_STRUCT;
1397
1521
 
1398
1522
 
1399
- #line 1400 "parser.c"
1523
+ #line 1524 "parser.c"
1400
1524
  {
1401
1525
  cs = JSON_start;
1402
1526
  }
1403
- #line 505 "parser.rl"
1527
+ #line 548 "parser.rl"
1404
1528
  p = json->source;
1405
1529
  pe = p + json->len;
1406
1530
 
1407
- #line 1408 "parser.c"
1531
+ #line 1532 "parser.c"
1408
1532
  {
1409
1533
  if ( p == pe )
1410
1534
  goto _out;
@@ -1459,7 +1583,7 @@ case 5:
1459
1583
  goto st1;
1460
1584
  goto st5;
1461
1585
  tr3:
1462
- #line 401 "parser.rl"
1586
+ #line 434 "parser.rl"
1463
1587
  {
1464
1588
  char *np;
1465
1589
  json->current_nesting = 1;
@@ -1468,7 +1592,7 @@ tr3:
1468
1592
  }
1469
1593
  goto st10;
1470
1594
  tr4:
1471
- #line 394 "parser.rl"
1595
+ #line 427 "parser.rl"
1472
1596
  {
1473
1597
  char *np;
1474
1598
  json->current_nesting = 1;
@@ -1480,7 +1604,7 @@ st10:
1480
1604
  if ( ++p == pe )
1481
1605
  goto _out10;
1482
1606
  case 10:
1483
- #line 1484 "parser.c"
1607
+ #line 1608 "parser.c"
1484
1608
  switch( (*p) ) {
1485
1609
  case 13: goto st10;
1486
1610
  case 32: goto st10;
@@ -1536,7 +1660,7 @@ case 9:
1536
1660
 
1537
1661
  _out: {}
1538
1662
  }
1539
- #line 508 "parser.rl"
1663
+ #line 551 "parser.rl"
1540
1664
 
1541
1665
  if (cs >= JSON_first_final && p == pe) {
1542
1666
  return result;
@@ -1593,9 +1717,14 @@ void Init_parser()
1593
1717
  rb_define_method(cParser, "parse", cParser_parse, 0);
1594
1718
  rb_define_method(cParser, "source", cParser_source, 0);
1595
1719
 
1720
+ CNaN = rb_const_get(mJSON, rb_intern("NaN"));
1721
+ CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
1722
+ CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
1723
+
1596
1724
  i_json_creatable_p = rb_intern("json_creatable?");
1597
1725
  i_json_create = rb_intern("json_create");
1598
1726
  i_create_id = rb_intern("create_id");
1599
1727
  i_chr = rb_intern("chr");
1600
1728
  i_max_nesting = rb_intern("max_nesting");
1729
+ i_allow_nan = rb_intern("allow_nan");
1601
1730
  }