3 #include "../fbuffer/fbuffer.h"
6 #if defined HAVE_RUBY_ENCODING_H
7 # define EXC_ENCODING rb_utf8_encoding(),
8 # ifndef HAVE_RB_ENC_RAISE
21 # define rb_enc_raise enc_raise
25 # define rb_enc_raise rb_raise
30 static const signed char digit_values[256] = {
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,
34 -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1
47 static UTF32 unescape_unicode(
const unsigned char *p)
51 b = digit_values[p[0]];
53 result = (result << 4) | (
unsigned char)b;
54 b = digit_values[p[1]];
56 result = (result << 4) | (
unsigned char)b;
57 b = digit_values[p[2]];
59 result = (result << 4) | (
unsigned char)b;
60 b = digit_values[p[3]];
62 result = (result << 4) | (
unsigned char)b;
66 static int convert_UTF32_to_UTF8(
char *
buf,
UTF32 ch)
71 }
else if (ch <= 0x07FF) {
72 buf[0] = (
char) ((ch >> 6) | 0xC0);
73 buf[1] = (
char) ((ch & 0x3F) | 0x80);
75 }
else if (ch <= 0xFFFF) {
76 buf[0] = (
char) ((ch >> 12) | 0xE0);
77 buf[1] = (
char) (((ch >> 6) & 0x3F) | 0x80);
78 buf[2] = (
char) ((ch & 0x3F) | 0x80);
80 }
else if (ch <= 0x1fffff) {
81 buf[0] =(
char) ((ch >> 18) | 0xF0);
82 buf[1] =(
char) (((ch >> 12) & 0x3F) | 0x80);
83 buf[2] =(
char) (((ch >> 6) & 0x3F) | 0x80);
84 buf[3] =(
char) ((ch & 0x3F) | 0x80);
92 static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
93 static VALUE CNaN, CInfinity, CMinusInfinity;
96 static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
97 i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
98 i_object_class, i_array_class, i_decimal_class, i_key_p,
99 i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
100 i_leftshift, i_new, i_BigDecimal;
103 #line 126 "parser.rl"
115 #line 167 "parser.rl"
118 static char *JSON_parse_object(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result,
int current_nesting)
125 rb_raise(eNestingError,
"nesting of %d is too deep", current_nesting);
136 #line 182 "parser.rl"
162 if ( 9 <= (*p) && (*p) <= 10 )
166 #line 149 "parser.rl"
170 np = JSON_parse_string(json, p, pe, &last_name);
172 if (np ==
NULL) { p--; {p++; cs = 3;
goto _out;} }
else {p = (( np))-1;}
186 if ( 9 <= (*p) && (*p) <= 10 )
240 if ( 48 <= (*p) && (*p) <= 57 )
242 }
else if ( (*p) >= 9 )
246 #line 134 "parser.rl"
249 char *np = JSON_parse_value(json, p, pe, &
v, current_nesting);
251 p--; {p++; cs = 9;
goto _out;}
274 if ( 9 <= (*p) && (*p) <= 10 )
287 if ( 9 <= (*p) && (*p) <= 10 )
355 #line 157 "parser.rl"
356 { p--; {p++; cs = 27;
goto _out;} }
429 _test_eof2: cs = 2;
goto _test_eof;
430 _test_eof3: cs = 3;
goto _test_eof;
431 _test_eof4: cs = 4;
goto _test_eof;
432 _test_eof5: cs = 5;
goto _test_eof;
433 _test_eof6: cs = 6;
goto _test_eof;
434 _test_eof7: cs = 7;
goto _test_eof;
435 _test_eof8: cs = 8;
goto _test_eof;
436 _test_eof9: cs = 9;
goto _test_eof;
437 _test_eof10: cs = 10;
goto _test_eof;
438 _test_eof11: cs = 11;
goto _test_eof;
439 _test_eof12: cs = 12;
goto _test_eof;
440 _test_eof13: cs = 13;
goto _test_eof;
441 _test_eof14: cs = 14;
goto _test_eof;
442 _test_eof15: cs = 15;
goto _test_eof;
443 _test_eof16: cs = 16;
goto _test_eof;
444 _test_eof17: cs = 17;
goto _test_eof;
445 _test_eof18: cs = 18;
goto _test_eof;
446 _test_eof27: cs = 27;
goto _test_eof;
447 _test_eof19: cs = 19;
goto _test_eof;
448 _test_eof20: cs = 20;
goto _test_eof;
449 _test_eof21: cs = 21;
goto _test_eof;
450 _test_eof22: cs = 22;
goto _test_eof;
451 _test_eof23: cs = 23;
goto _test_eof;
452 _test_eof24: cs = 24;
goto _test_eof;
453 _test_eof25: cs = 25;
goto _test_eof;
454 _test_eof26: cs = 26;
goto _test_eof;
460 #line 183 "parser.rl"
470 if (!
NIL_P(klassname)) {
493 #line 283 "parser.rl"
496 static char *JSON_parse_value(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result,
int current_nesting)
506 #line 290 "parser.rl"
533 if ( 48 <= (*p) && (*p) <= 57 )
535 }
else if ( (*p) >= 9 )
542 #line 235 "parser.rl"
544 char *np = JSON_parse_string(json, p, pe, result);
545 if (np ==
NULL) { p--; {p++; cs = 29;
goto _out;} }
else {p = (( np))-1;}
549 #line 240 "parser.rl"
554 *result = CMinusInfinity;
556 p--; {p++; cs = 29;
goto _out;}
561 np = JSON_parse_float(json, p, pe, result);
562 if (np !=
NULL) {p = (( np))-1;}
563 np = JSON_parse_integer(json, p, pe, result);
564 if (np !=
NULL) {p = (( np))-1;}
565 p--; {p++; cs = 29;
goto _out;}
569 #line 258 "parser.rl"
572 np = JSON_parse_array(json, p, pe, result, current_nesting + 1);
573 if (np ==
NULL) { p--; {p++; cs = 29;
goto _out;} }
else {p = (( np))-1;}
577 #line 264 "parser.rl"
580 np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
581 if (np ==
NULL) { p--; {p++; cs = 29;
goto _out;} }
else {p = (( np))-1;}
585 #line 228 "parser.rl"
595 #line 221 "parser.rl"
605 #line 215 "parser.rl"
611 #line 212 "parser.rl"
617 #line 218 "parser.rl"
626 #line 270 "parser.rl"
627 { p--; {p++; cs = 29;
goto _out;} }
634 if ( 9 <= (*p) && (*p) <= 10 )
835 _test_eof1: cs = 1;
goto _test_eof;
836 _test_eof29: cs = 29;
goto _test_eof;
837 _test_eof2: cs = 2;
goto _test_eof;
838 _test_eof3: cs = 3;
goto _test_eof;
839 _test_eof4: cs = 4;
goto _test_eof;
840 _test_eof5: cs = 5;
goto _test_eof;
841 _test_eof6: cs = 6;
goto _test_eof;
842 _test_eof7: cs = 7;
goto _test_eof;
843 _test_eof8: cs = 8;
goto _test_eof;
844 _test_eof9: cs = 9;
goto _test_eof;
845 _test_eof10: cs = 10;
goto _test_eof;
846 _test_eof11: cs = 11;
goto _test_eof;
847 _test_eof12: cs = 12;
goto _test_eof;
848 _test_eof13: cs = 13;
goto _test_eof;
849 _test_eof14: cs = 14;
goto _test_eof;
850 _test_eof15: cs = 15;
goto _test_eof;
851 _test_eof16: cs = 16;
goto _test_eof;
852 _test_eof17: cs = 17;
goto _test_eof;
853 _test_eof18: cs = 18;
goto _test_eof;
854 _test_eof19: cs = 19;
goto _test_eof;
855 _test_eof20: cs = 20;
goto _test_eof;
856 _test_eof21: cs = 21;
goto _test_eof;
857 _test_eof22: cs = 22;
goto _test_eof;
858 _test_eof23: cs = 23;
goto _test_eof;
859 _test_eof24: cs = 24;
goto _test_eof;
860 _test_eof25: cs = 25;
goto _test_eof;
861 _test_eof26: cs = 26;
goto _test_eof;
862 _test_eof27: cs = 27;
goto _test_eof;
863 _test_eof28: cs = 28;
goto _test_eof;
869 #line 291 "parser.rl"
887 #line 307 "parser.rl"
890 static char *JSON_parse_integer(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result)
900 #line 314 "parser.rl"
914 if ( 49 <= (*p) && (*p) <= 57 )
926 if ( 49 <= (*p) && (*p) <= 57 )
933 if ( 48 <= (*p) && (*p) <= 57 )
937 #line 304 "parser.rl"
938 { p--; {p++; cs = 4;
goto _out;} }
950 if ( 48 <= (*p) && (*p) <= 57 )
954 _test_eof2: cs = 2;
goto _test_eof;
955 _test_eof3: cs = 3;
goto _test_eof;
956 _test_eof4: cs = 4;
goto _test_eof;
957 _test_eof5: cs = 5;
goto _test_eof;
963 #line 316 "parser.rl"
969 fbuffer_append_char(json->
fbuffer,
'\0');
986 #line 341 "parser.rl"
989 static int is_bigdecimal_class(
VALUE obj)
991 if (cBigDecimal ==
Qundef) {
999 return obj == cBigDecimal;
1002 static char *JSON_parse_float(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result)
1007 #line 1008 "parser.c"
1012 #line 361 "parser.rl"
1015 #line 1016 "parser.c"
1026 if ( 49 <= (*p) && (*p) <= 57 )
1038 if ( 49 <= (*p) && (*p) <= 57 )
1055 if ( 48 <= (*p) && (*p) <= 57 )
1067 if ( 48 <= (*p) && (*p) <= 57 )
1069 }
else if ( (*p) >= 45 )
1073 #line 335 "parser.rl"
1074 { p--; {p++; cs = 9;
goto _out;} }
1080 #line 1081 "parser.c"
1090 if ( 48 <= (*p) && (*p) <= 57 )
1097 if ( 48 <= (*p) && (*p) <= 57 )
1109 if ( 48 <= (*p) && (*p) <= 57 )
1111 }
else if ( (*p) >= 45 )
1123 if ( 48 <= (*p) && (*p) <= 57 )
1127 _test_eof2: cs = 2;
goto _test_eof;
1128 _test_eof3: cs = 3;
goto _test_eof;
1129 _test_eof4: cs = 4;
goto _test_eof;
1130 _test_eof8: cs = 8;
goto _test_eof;
1131 _test_eof9: cs = 9;
goto _test_eof;
1132 _test_eof5: cs = 5;
goto _test_eof;
1133 _test_eof6: cs = 6;
goto _test_eof;
1134 _test_eof10: cs = 10;
goto _test_eof;
1135 _test_eof7: cs = 7;
goto _test_eof;
1141 #line 363 "parser.rl"
1147 fbuffer_append_char(json->
fbuffer,
'\0');
1167 #line 1168 "parser.c"
1175 #line 416 "parser.rl"
1178 static char *JSON_parse_array(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result,
int current_nesting)
1184 rb_raise(eNestingError,
"nesting of %d is too deep", current_nesting);
1189 #line 1190 "parser.c"
1194 #line 429 "parser.rl"
1196 #line 1197 "parser.c"
1229 if ( 48 <= (*p) && (*p) <= 57 )
1231 }
else if ( (*p) >= 9 )
1235 #line 393 "parser.rl"
1238 char *np = JSON_parse_value(json, p, pe, &
v, current_nesting);
1240 p--; {p++; cs = 3;
goto _out;}
1255 #line 1256 "parser.c"
1263 if ( 9 <= (*p) && (*p) <= 10 )
1285 if ( 48 <= (*p) && (*p) <= 57 )
1287 }
else if ( (*p) >= 9 )
1355 #line 408 "parser.rl"
1356 { p--; {p++; cs = 17;
goto _out;} }
1362 #line 1363 "parser.c"
1397 _test_eof2: cs = 2;
goto _test_eof;
1398 _test_eof3: cs = 3;
goto _test_eof;
1399 _test_eof4: cs = 4;
goto _test_eof;
1400 _test_eof5: cs = 5;
goto _test_eof;
1401 _test_eof6: cs = 6;
goto _test_eof;
1402 _test_eof7: cs = 7;
goto _test_eof;
1403 _test_eof8: cs = 8;
goto _test_eof;
1404 _test_eof9: cs = 9;
goto _test_eof;
1405 _test_eof10: cs = 10;
goto _test_eof;
1406 _test_eof11: cs = 11;
goto _test_eof;
1407 _test_eof12: cs = 12;
goto _test_eof;
1408 _test_eof17: cs = 17;
goto _test_eof;
1409 _test_eof13: cs = 13;
goto _test_eof;
1410 _test_eof14: cs = 14;
goto _test_eof;
1411 _test_eof15: cs = 15;
goto _test_eof;
1412 _test_eof16: cs = 16;
goto _test_eof;
1418 #line 430 "parser.rl"
1428 static VALUE json_string_unescape(
VALUE result,
char *
string,
char *stringEnd)
1430 char *p = string, *pe = string, *unescape;
1434 while (pe < stringEnd) {
1436 unescape = (
char *)
"?";
1441 unescape = (
char *)
"\n";
1444 unescape = (
char *)
"\r";
1447 unescape = (
char *)
"\t";
1450 unescape = (
char *)
"\"";
1453 unescape = (
char *)
"\\";
1456 unescape = (
char *)
"\b";
1459 unescape = (
char *)
"\f";
1462 if (pe > stringEnd - 4) {
1465 "%u: incomplete unicode character escape sequence at '%s'", __LINE__, p
1468 UTF32 ch = unescape_unicode((
unsigned char *) ++pe);
1472 if (pe > stringEnd - 6) {
1475 "%u: incomplete surrogate pair at '%s'", __LINE__, p
1478 if (pe[0] ==
'\\' && pe[1] ==
'u') {
1479 UTF32 sur = unescape_unicode((
unsigned char *) pe + 2);
1480 ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
1484 unescape = (
char *)
"?";
1488 unescape_len = convert_UTF32_to_UTF8(
buf, ch);
1507 #line 1508 "parser.c"
1515 #line 537 "parser.rl"
1530 static char *JSON_parse_string(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result)
1537 #line 1538 "parser.c"
1542 #line 558 "parser.rl"
1545 #line 1546 "parser.c"
1566 if ( 0 <= (*p) && (*p) <= 31 )
1570 #line 523 "parser.rl"
1572 *result = json_string_unescape(*result, json->
memo + 1, p);
1573 if (
NIL_P(*result)) {
1575 {p++; cs = 8;
goto _out;}
1581 #line 534 "parser.rl"
1582 { p--; {p++; cs = 8;
goto _out;} }
1588 #line 1589 "parser.c"
1596 if ( 0 <= (*p) && (*p) <= 31 )
1604 if ( 48 <= (*p) && (*p) <= 57 )
1606 }
else if ( (*p) > 70 ) {
1607 if ( 97 <= (*p) && (*p) <= 102 )
1617 if ( 48 <= (*p) && (*p) <= 57 )
1619 }
else if ( (*p) > 70 ) {
1620 if ( 97 <= (*p) && (*p) <= 102 )
1630 if ( 48 <= (*p) && (*p) <= 57 )
1632 }
else if ( (*p) > 70 ) {
1633 if ( 97 <= (*p) && (*p) <= 102 )
1643 if ( 48 <= (*p) && (*p) <= 57 )
1645 }
else if ( (*p) > 70 ) {
1646 if ( 97 <= (*p) && (*p) <= 102 )
1652 _test_eof2: cs = 2;
goto _test_eof;
1653 _test_eof8: cs = 8;
goto _test_eof;
1654 _test_eof3: cs = 3;
goto _test_eof;
1655 _test_eof4: cs = 4;
goto _test_eof;
1656 _test_eof5: cs = 5;
goto _test_eof;
1657 _test_eof6: cs = 6;
goto _test_eof;
1658 _test_eof7: cs = 7;
goto _test_eof;
1664 #line 560 "parser.rl"
1703 #ifdef HAVE_RUBY_ENCODING_H
1752 #ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
1758 #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
1767 if (
RTEST(max_nesting)) {
1776 tmp =
ID2SYM(i_allow_nan);
1782 tmp =
ID2SYM(i_symbolize_names);
1788 tmp =
ID2SYM(i_create_additions);
1796 "options :symbolize_names and :create_additions cannot be "
1797 " used in conjunction");
1799 tmp =
ID2SYM(i_create_id);
1805 tmp =
ID2SYM(i_object_class);
1811 tmp =
ID2SYM(i_array_class);
1817 tmp =
ID2SYM(i_decimal_class);
1823 tmp =
ID2SYM(i_match_string);
1830 #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
1851 #line 1852 "parser.c"
1859 #line 760 "parser.rl"
1876 #line 1877 "parser.c"
1881 #line 776 "parser.rl"
1885 #line 1886 "parser.c"
1910 if ( 48 <= (*p) && (*p) <= 57 )
1912 }
else if ( (*p) >= 9 )
1919 #line 752 "parser.rl"
1921 char *np = JSON_parse_value(json, p, pe, &result, 0);
1922 if (np ==
NULL) { p--; {p++; cs = 10;
goto _out;} }
else {p = (( np))-1;}
1929 #line 1930 "parser.c"
1935 if ( 9 <= (*p) && (*p) <= 10 )
2003 _test_eof1: cs = 1;
goto _test_eof;
2004 _test_eof10: cs = 10;
goto _test_eof;
2005 _test_eof2: cs = 2;
goto _test_eof;
2006 _test_eof3: cs = 3;
goto _test_eof;
2007 _test_eof4: cs = 4;
goto _test_eof;
2008 _test_eof5: cs = 5;
goto _test_eof;
2009 _test_eof6: cs = 6;
goto _test_eof;
2010 _test_eof7: cs = 7;
goto _test_eof;
2011 _test_eof8: cs = 8;
goto _test_eof;
2012 _test_eof9: cs = 9;
goto _test_eof;
2018 #line 779 "parser.rl"
2028 static void JSON_mark(
void *
ptr)
2039 static void JSON_free(
void *
ptr)
2046 static size_t JSON_memsize(
const void *
ptr)
2052 #ifdef NEW_TYPEDDATA_WRAPPER
2055 {JSON_mark, JSON_free, JSON_memsize,},
2056 #ifdef RUBY_TYPED_FREE_IMMEDIATELY
2067 json->
fbuffer = fbuffer_alloc(0);
2108 i_json_creatable_p =
rb_intern(
"json_creatable?");
2109 i_json_create =
rb_intern(
"json_create");
2111 i_create_additions =
rb_intern(
"create_additions");
2113 i_max_nesting =
rb_intern(
"max_nesting");
2115 i_symbolize_names =
rb_intern(
"symbolize_names");
2116 i_object_class =
rb_intern(
"object_class");
2117 i_array_class =
rb_intern(
"array_class");
2118 i_decimal_class =
rb_intern(
"decimal_class");
2120 i_match_string =
rb_intern(
"match_string");
2122 i_deep_const_get =
rb_intern(
"deep_const_get");