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 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;
95 static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
96 i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
97 i_object_class, i_array_class, i_decimal_class, i_key_p,
98 i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
102 #line 125 "parser.rl" 114 #line 166 "parser.rl" 117 static char *JSON_parse_object(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result,
int current_nesting)
124 rb_raise(eNestingError,
"nesting of %d is too deep", current_nesting);
135 #line 181 "parser.rl" 161 if ( 9 <= (*p) && (*p) <= 10 )
165 #line 148 "parser.rl" 169 np = JSON_parse_string(json, p, pe, &last_name);
171 if (np ==
NULL) { p--; {p++; cs = 3;
goto _out;} }
else {p = (( np))-1;}
185 if ( 9 <= (*p) && (*p) <= 10 )
239 if ( 48 <= (*p) && (*p) <= 57 )
241 }
else if ( (*p) >= 9 )
245 #line 133 "parser.rl" 248 char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
250 p--; {p++; cs = 9;
goto _out;}
273 if ( 9 <= (*p) && (*p) <= 10 )
286 if ( 9 <= (*p) && (*p) <= 10 )
354 #line 156 "parser.rl" 355 { p--; {p++; cs = 27;
goto _out;} }
428 _test_eof2: cs = 2;
goto _test_eof;
429 _test_eof3: cs = 3;
goto _test_eof;
430 _test_eof4: cs = 4;
goto _test_eof;
431 _test_eof5: cs = 5;
goto _test_eof;
432 _test_eof6: cs = 6;
goto _test_eof;
433 _test_eof7: cs = 7;
goto _test_eof;
434 _test_eof8: cs = 8;
goto _test_eof;
435 _test_eof9: cs = 9;
goto _test_eof;
436 _test_eof10: cs = 10;
goto _test_eof;
437 _test_eof11: cs = 11;
goto _test_eof;
438 _test_eof12: cs = 12;
goto _test_eof;
439 _test_eof13: cs = 13;
goto _test_eof;
440 _test_eof14: cs = 14;
goto _test_eof;
441 _test_eof15: cs = 15;
goto _test_eof;
442 _test_eof16: cs = 16;
goto _test_eof;
443 _test_eof17: cs = 17;
goto _test_eof;
444 _test_eof18: cs = 18;
goto _test_eof;
445 _test_eof27: cs = 27;
goto _test_eof;
446 _test_eof19: cs = 19;
goto _test_eof;
447 _test_eof20: cs = 20;
goto _test_eof;
448 _test_eof21: cs = 21;
goto _test_eof;
449 _test_eof22: cs = 22;
goto _test_eof;
450 _test_eof23: cs = 23;
goto _test_eof;
451 _test_eof24: cs = 24;
goto _test_eof;
452 _test_eof25: cs = 25;
goto _test_eof;
453 _test_eof26: cs = 26;
goto _test_eof;
459 #line 182 "parser.rl" 469 if (!
NIL_P(klassname)) {
472 *result =
rb_funcall(klass, i_json_create, 1, *result);
492 #line 282 "parser.rl" 495 static char *JSON_parse_value(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result,
int current_nesting)
505 #line 289 "parser.rl" 532 if ( 48 <= (*p) && (*p) <= 57 )
534 }
else if ( (*p) >= 9 )
541 #line 234 "parser.rl" 543 char *np = JSON_parse_string(json, p, pe, result);
544 if (np ==
NULL) { p--; {p++; cs = 29;
goto _out;} }
else {p = (( np))-1;}
548 #line 239 "parser.rl" 553 *result = CMinusInfinity;
555 p--; {p++; cs = 29;
goto _out;}
560 np = JSON_parse_float(json, p, pe, result);
561 if (np !=
NULL) {p = (( np))-1;}
562 np = JSON_parse_integer(json, p, pe, result);
563 if (np !=
NULL) {p = (( np))-1;}
564 p--; {p++; cs = 29;
goto _out;}
568 #line 257 "parser.rl" 571 np = JSON_parse_array(json, p, pe, result, current_nesting + 1);
572 if (np ==
NULL) { p--; {p++; cs = 29;
goto _out;} }
else {p = (( np))-1;}
576 #line 263 "parser.rl" 579 np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
580 if (np ==
NULL) { p--; {p++; cs = 29;
goto _out;} }
else {p = (( np))-1;}
584 #line 227 "parser.rl" 594 #line 220 "parser.rl" 604 #line 214 "parser.rl" 610 #line 211 "parser.rl" 616 #line 217 "parser.rl" 625 #line 269 "parser.rl" 626 { p--; {p++; cs = 29;
goto _out;} }
633 if ( 9 <= (*p) && (*p) <= 10 )
834 _test_eof1: cs = 1;
goto _test_eof;
835 _test_eof29: cs = 29;
goto _test_eof;
836 _test_eof2: cs = 2;
goto _test_eof;
837 _test_eof3: cs = 3;
goto _test_eof;
838 _test_eof4: cs = 4;
goto _test_eof;
839 _test_eof5: cs = 5;
goto _test_eof;
840 _test_eof6: cs = 6;
goto _test_eof;
841 _test_eof7: cs = 7;
goto _test_eof;
842 _test_eof8: cs = 8;
goto _test_eof;
843 _test_eof9: cs = 9;
goto _test_eof;
844 _test_eof10: cs = 10;
goto _test_eof;
845 _test_eof11: cs = 11;
goto _test_eof;
846 _test_eof12: cs = 12;
goto _test_eof;
847 _test_eof13: cs = 13;
goto _test_eof;
848 _test_eof14: cs = 14;
goto _test_eof;
849 _test_eof15: cs = 15;
goto _test_eof;
850 _test_eof16: cs = 16;
goto _test_eof;
851 _test_eof17: cs = 17;
goto _test_eof;
852 _test_eof18: cs = 18;
goto _test_eof;
853 _test_eof19: cs = 19;
goto _test_eof;
854 _test_eof20: cs = 20;
goto _test_eof;
855 _test_eof21: cs = 21;
goto _test_eof;
856 _test_eof22: cs = 22;
goto _test_eof;
857 _test_eof23: cs = 23;
goto _test_eof;
858 _test_eof24: cs = 24;
goto _test_eof;
859 _test_eof25: cs = 25;
goto _test_eof;
860 _test_eof26: cs = 26;
goto _test_eof;
861 _test_eof27: cs = 27;
goto _test_eof;
862 _test_eof28: cs = 28;
goto _test_eof;
868 #line 290 "parser.rl" 886 #line 306 "parser.rl" 889 static char *JSON_parse_integer(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result)
899 #line 313 "parser.rl" 913 if ( 49 <= (*p) && (*p) <= 57 )
925 if ( 49 <= (*p) && (*p) <= 57 )
932 if ( 48 <= (*p) && (*p) <= 57 )
936 #line 303 "parser.rl" 937 { p--; {p++; cs = 4;
goto _out;} }
949 if ( 48 <= (*p) && (*p) <= 57 )
953 _test_eof2: cs = 2;
goto _test_eof;
954 _test_eof3: cs = 3;
goto _test_eof;
955 _test_eof4: cs = 4;
goto _test_eof;
956 _test_eof5: cs = 5;
goto _test_eof;
962 #line 315 "parser.rl" 968 fbuffer_append_char(json->
fbuffer,
'\0');
985 #line 340 "parser.rl" 988 static char *JSON_parse_float(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result)
998 #line 347 "parser.rl" 1001 #line 1002 "parser.c" 1012 if ( 49 <= (*p) && (*p) <= 57 )
1024 if ( 49 <= (*p) && (*p) <= 57 )
1041 if ( 48 <= (*p) && (*p) <= 57 )
1053 if ( 48 <= (*p) && (*p) <= 57 )
1055 }
else if ( (*p) >= 45 )
1059 #line 334 "parser.rl" 1060 { p--; {p++; cs = 9;
goto _out;} }
1066 #line 1067 "parser.c" 1076 if ( 48 <= (*p) && (*p) <= 57 )
1083 if ( 48 <= (*p) && (*p) <= 57 )
1095 if ( 48 <= (*p) && (*p) <= 57 )
1097 }
else if ( (*p) >= 45 )
1109 if ( 48 <= (*p) && (*p) <= 57 )
1113 _test_eof2: cs = 2;
goto _test_eof;
1114 _test_eof3: cs = 3;
goto _test_eof;
1115 _test_eof4: cs = 4;
goto _test_eof;
1116 _test_eof8: cs = 8;
goto _test_eof;
1117 _test_eof9: cs = 9;
goto _test_eof;
1118 _test_eof5: cs = 5;
goto _test_eof;
1119 _test_eof6: cs = 6;
goto _test_eof;
1120 _test_eof10: cs = 10;
goto _test_eof;
1121 _test_eof7: cs = 7;
goto _test_eof;
1127 #line 349 "parser.rl" 1133 fbuffer_append_char(json->
fbuffer,
'\0');
1149 #line 1150 "parser.c" 1157 #line 398 "parser.rl" 1160 static char *JSON_parse_array(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result,
int current_nesting)
1166 rb_raise(eNestingError,
"nesting of %d is too deep", current_nesting);
1171 #line 1172 "parser.c" 1176 #line 411 "parser.rl" 1178 #line 1179 "parser.c" 1211 if ( 48 <= (*p) && (*p) <= 57 )
1213 }
else if ( (*p) >= 9 )
1217 #line 375 "parser.rl" 1220 char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
1222 p--; {p++; cs = 3;
goto _out;}
1237 #line 1238 "parser.c" 1245 if ( 9 <= (*p) && (*p) <= 10 )
1267 if ( 48 <= (*p) && (*p) <= 57 )
1269 }
else if ( (*p) >= 9 )
1337 #line 390 "parser.rl" 1338 { p--; {p++; cs = 17;
goto _out;} }
1344 #line 1345 "parser.c" 1379 _test_eof2: cs = 2;
goto _test_eof;
1380 _test_eof3: cs = 3;
goto _test_eof;
1381 _test_eof4: cs = 4;
goto _test_eof;
1382 _test_eof5: cs = 5;
goto _test_eof;
1383 _test_eof6: cs = 6;
goto _test_eof;
1384 _test_eof7: cs = 7;
goto _test_eof;
1385 _test_eof8: cs = 8;
goto _test_eof;
1386 _test_eof9: cs = 9;
goto _test_eof;
1387 _test_eof10: cs = 10;
goto _test_eof;
1388 _test_eof11: cs = 11;
goto _test_eof;
1389 _test_eof12: cs = 12;
goto _test_eof;
1390 _test_eof17: cs = 17;
goto _test_eof;
1391 _test_eof13: cs = 13;
goto _test_eof;
1392 _test_eof14: cs = 14;
goto _test_eof;
1393 _test_eof15: cs = 15;
goto _test_eof;
1394 _test_eof16: cs = 16;
goto _test_eof;
1400 #line 412 "parser.rl" 1410 static VALUE json_string_unescape(
VALUE result,
char *
string,
char *stringEnd)
1412 char *p = string, *pe = string, *unescape;
1416 while (pe < stringEnd) {
1418 unescape = (
char *)
"?";
1423 unescape = (
char *)
"\n";
1426 unescape = (
char *)
"\r";
1429 unescape = (
char *)
"\t";
1432 unescape = (
char *)
"\"";
1435 unescape = (
char *)
"\\";
1438 unescape = (
char *)
"\b";
1441 unescape = (
char *)
"\f";
1444 if (pe > stringEnd - 4) {
1447 "%u: incomplete unicode character escape sequence at '%s'", __LINE__, p
1450 UTF32 ch = unescape_unicode((
unsigned char *) ++pe);
1454 if (pe > stringEnd - 6) {
1457 "%u: incomplete surrogate pair at '%s'", __LINE__, p
1460 if (pe[0] ==
'\\' && pe[1] ==
'u') {
1461 UTF32 sur = unescape_unicode((
unsigned char *) pe + 2);
1462 ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
1466 unescape = (
char *)
"?";
1470 unescape_len = convert_UTF32_to_UTF8(buf, ch);
1489 #line 1490 "parser.c" 1497 #line 519 "parser.rl" 1512 static char *JSON_parse_string(
JSON_Parser *json,
char *p,
char *pe,
VALUE *result)
1519 #line 1520 "parser.c" 1524 #line 540 "parser.rl" 1527 #line 1528 "parser.c" 1548 if ( 0 <= (*p) && (*p) <= 31 )
1552 #line 505 "parser.rl" 1554 *result = json_string_unescape(*result, json->
memo + 1, p);
1555 if (
NIL_P(*result)) {
1557 {p++; cs = 8;
goto _out;}
1563 #line 516 "parser.rl" 1564 { p--; {p++; cs = 8;
goto _out;} }
1570 #line 1571 "parser.c" 1578 if ( 0 <= (*p) && (*p) <= 31 )
1586 if ( 48 <= (*p) && (*p) <= 57 )
1588 }
else if ( (*p) > 70 ) {
1589 if ( 97 <= (*p) && (*p) <= 102 )
1599 if ( 48 <= (*p) && (*p) <= 57 )
1601 }
else if ( (*p) > 70 ) {
1602 if ( 97 <= (*p) && (*p) <= 102 )
1612 if ( 48 <= (*p) && (*p) <= 57 )
1614 }
else if ( (*p) > 70 ) {
1615 if ( 97 <= (*p) && (*p) <= 102 )
1625 if ( 48 <= (*p) && (*p) <= 57 )
1627 }
else if ( (*p) > 70 ) {
1628 if ( 97 <= (*p) && (*p) <= 102 )
1634 _test_eof2: cs = 2;
goto _test_eof;
1635 _test_eof8: cs = 8;
goto _test_eof;
1636 _test_eof3: cs = 3;
goto _test_eof;
1637 _test_eof4: cs = 4;
goto _test_eof;
1638 _test_eof5: cs = 5;
goto _test_eof;
1639 _test_eof6: cs = 6;
goto _test_eof;
1640 _test_eof7: cs = 7;
goto _test_eof;
1646 #line 542 "parser.rl" 1655 *result =
rb_funcall(klass, i_json_create, 1, *result);
1685 #ifdef HAVE_RUBY_ENCODING_H 1734 #ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH 1740 #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH 1749 if (
RTEST(max_nesting)) {
1758 tmp =
ID2SYM(i_allow_nan);
1764 tmp =
ID2SYM(i_symbolize_names);
1770 tmp =
ID2SYM(i_create_additions);
1778 "options :symbolize_names and :create_additions cannot be " 1779 " used in conjunction");
1781 tmp =
ID2SYM(i_create_id);
1787 tmp =
ID2SYM(i_object_class);
1793 tmp =
ID2SYM(i_array_class);
1799 tmp =
ID2SYM(i_decimal_class);
1805 tmp =
ID2SYM(i_match_string);
1812 #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH 1833 #line 1834 "parser.c" 1841 #line 742 "parser.rl" 1858 #line 1859 "parser.c" 1863 #line 758 "parser.rl" 1867 #line 1868 "parser.c" 1892 if ( 48 <= (*p) && (*p) <= 57 )
1894 }
else if ( (*p) >= 9 )
1901 #line 734 "parser.rl" 1903 char *np = JSON_parse_value(json, p, pe, &result, 0);
1904 if (np ==
NULL) { p--; {p++; cs = 10;
goto _out;} }
else {p = (( np))-1;}
1911 #line 1912 "parser.c" 1917 if ( 9 <= (*p) && (*p) <= 10 )
1985 _test_eof1: cs = 1;
goto _test_eof;
1986 _test_eof10: cs = 10;
goto _test_eof;
1987 _test_eof2: cs = 2;
goto _test_eof;
1988 _test_eof3: cs = 3;
goto _test_eof;
1989 _test_eof4: cs = 4;
goto _test_eof;
1990 _test_eof5: cs = 5;
goto _test_eof;
1991 _test_eof6: cs = 6;
goto _test_eof;
1992 _test_eof7: cs = 7;
goto _test_eof;
1993 _test_eof8: cs = 8;
goto _test_eof;
1994 _test_eof9: cs = 9;
goto _test_eof;
2000 #line 761 "parser.rl" 2010 static void JSON_mark(
void *ptr)
2021 static void JSON_free(
void *ptr)
2028 static size_t JSON_memsize(
const void *ptr)
2034 #ifdef NEW_TYPEDDATA_WRAPPER 2037 {JSON_mark, JSON_free, JSON_memsize,},
2038 #ifdef RUBY_TYPED_FREE_IMMEDIATELY 2045 static VALUE cJSON_parser_s_allocate(
VALUE klass)
2049 json->
fbuffer = fbuffer_alloc(0);
2082 i_json_creatable_p =
rb_intern(
"json_creatable?");
2083 i_json_create =
rb_intern(
"json_create");
2085 i_create_additions =
rb_intern(
"create_additions");
2087 i_max_nesting =
rb_intern(
"max_nesting");
2089 i_symbolize_names =
rb_intern(
"symbolize_names");
2090 i_object_class =
rb_intern(
"object_class");
2091 i_array_class =
rb_intern(
"array_class");
2092 i_decimal_class =
rb_intern(
"decimal_class");
2094 i_match_string =
rb_intern(
"match_string");
2096 i_deep_const_get =
rb_intern(
"deep_const_get");
VALUE rb_ary_entry(VALUE ary, long offset)
#define RUBY_TYPED_FREE_IMMEDIATELY
void rb_raise(VALUE exc, const char *fmt,...)
#define option_given_p(opts, key)
VALUE rb_ary_push(VALUE ary, VALUE item)
double rb_cstr_to_dbl(const char *, int)
Parses a string representation of a floating point number.
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_enc_vsprintf(rb_encoding *, const char *, va_list)
VALUE rb_path2class(const char *)
rb_encoding * rb_utf8_encoding(void)
VALUE rb_str_buf_cat(VALUE, const char *, long)
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
VALUE rb_require(const char *)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
RUBY_EXTERN VALUE rb_cObject
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
VALUE rb_str_resize(VALUE, long)
VALUE rb_const_get(VALUE, ID)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
unsigned char buf[MIME_BUF_SIZE]
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
#define UNI_SUR_HIGH_START
register unsigned int len
VALUE rb_define_module_under(VALUE outer, const char *name)
rb_encoding * rb_enc_get(VALUE obj)
VALUE rb_hash_aref(VALUE hash, VALUE key)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
#define TypedData_Make_Struct(klass, type, data_type, sval)
#define UNI_REPLACEMENT_CHAR
void rb_gc_mark_maybe(VALUE obj)
rb_encoding * rb_ascii8bit_encoding(void)
VALUE rb_str_intern(VALUE)
VALUE rb_define_module(const char *name)
VALUE rb_str_buf_new(long)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_cstr2inum(const char *str, int base)