summaryrefslogtreecommitdiff
path: root/ext/json/ext/parser/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/ext/parser/parser.c')
-rw-r--r--ext/json/ext/parser/parser.c185
1 files changed, 106 insertions, 79 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index d2e4eb6..bc4ac46 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -27,7 +27,7 @@ enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
/* unicode */
-static const char digit_values[256] = {
+static const signed char digit_values[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,
@@ -46,7 +46,7 @@ static const char digit_values[256] = {
static UTF32 unescape_unicode(const unsigned char *p)
{
- char b;
+ signed char b;
UTF32 result = 0;
b = digit_values[p[0]];
if (b < 0) return UNI_REPLACEMENT_CHAR;
@@ -91,19 +91,20 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
static VALUE CNaN, CInfinity, CMinusInfinity;
+static VALUE cBigDecimal = Qundef;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
i_object_class, i_array_class, i_decimal_class, i_key_p,
i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
- i_leftshift, i_new;
+ i_leftshift, i_new, i_BigDecimal;
-#line 125 "parser.rl"
+#line 126 "parser.rl"
-#line 107 "parser.c"
+#line 108 "parser.c"
enum {JSON_object_start = 1};
enum {JSON_object_first_final = 27};
enum {JSON_object_error = 0};
@@ -111,7 +112,7 @@ enum {JSON_object_error = 0};
enum {JSON_object_en_main = 1};
-#line 166 "parser.rl"
+#line 167 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -127,14 +128,14 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
-#line 131 "parser.c"
+#line 132 "parser.c"
{
cs = JSON_object_start;
}
-#line 181 "parser.rl"
+#line 182 "parser.rl"
-#line 138 "parser.c"
+#line 139 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -162,7 +163,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 148 "parser.rl"
+#line 149 "parser.rl"
{
char *np;
json->parsing_name = 1;
@@ -175,7 +176,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 179 "parser.c"
+#line 180 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -242,7 +243,7 @@ case 8:
goto st8;
goto st0;
tr11:
-#line 133 "parser.rl"
+#line 134 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@@ -262,7 +263,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 266 "parser.c"
+#line 267 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -351,14 +352,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 156 "parser.rl"
+#line 157 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 362 "parser.c"
+#line 363 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -456,7 +457,7 @@ case 26:
_out: {}
}
-#line 182 "parser.rl"
+#line 183 "parser.rl"
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
@@ -481,7 +482,7 @@ case 26:
-#line 485 "parser.c"
+#line 486 "parser.c"
enum {JSON_value_start = 1};
enum {JSON_value_first_final = 29};
enum {JSON_value_error = 0};
@@ -489,7 +490,7 @@ enum {JSON_value_error = 0};
enum {JSON_value_en_main = 1};
-#line 282 "parser.rl"
+#line 283 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -497,14 +498,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 501 "parser.c"
+#line 502 "parser.c"
{
cs = JSON_value_start;
}
-#line 289 "parser.rl"
+#line 290 "parser.rl"
-#line 508 "parser.c"
+#line 509 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -538,14 +539,14 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 234 "parser.rl"
+#line 235 "parser.rl"
{
char *np = JSON_parse_string(json, p, pe, result);
if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
}
goto st29;
tr3:
-#line 239 "parser.rl"
+#line 240 "parser.rl"
{
char *np;
if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
@@ -565,7 +566,7 @@ tr3:
}
goto st29;
tr7:
-#line 257 "parser.rl"
+#line 258 "parser.rl"
{
char *np;
np = JSON_parse_array(json, p, pe, result, current_nesting + 1);
@@ -573,7 +574,7 @@ tr7:
}
goto st29;
tr11:
-#line 263 "parser.rl"
+#line 264 "parser.rl"
{
char *np;
np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
@@ -581,7 +582,7 @@ tr11:
}
goto st29;
tr25:
-#line 227 "parser.rl"
+#line 228 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
@@ -591,7 +592,7 @@ tr25:
}
goto st29;
tr27:
-#line 220 "parser.rl"
+#line 221 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
@@ -601,19 +602,19 @@ tr27:
}
goto st29;
tr31:
-#line 214 "parser.rl"
+#line 215 "parser.rl"
{
*result = Qfalse;
}
goto st29;
tr34:
-#line 211 "parser.rl"
+#line 212 "parser.rl"
{
*result = Qnil;
}
goto st29;
tr37:
-#line 217 "parser.rl"
+#line 218 "parser.rl"
{
*result = Qtrue;
}
@@ -622,9 +623,9 @@ st29:
if ( ++p == pe )
goto _test_eof29;
case 29:
-#line 269 "parser.rl"
+#line 270 "parser.rl"
{ p--; {p++; cs = 29; goto _out;} }
-#line 628 "parser.c"
+#line 629 "parser.c"
switch( (*p) ) {
case 13: goto st29;
case 32: goto st29;
@@ -865,7 +866,7 @@ case 28:
_out: {}
}
-#line 290 "parser.rl"
+#line 291 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -875,7 +876,7 @@ case 28:
}
-#line 879 "parser.c"
+#line 880 "parser.c"
enum {JSON_integer_start = 1};
enum {JSON_integer_first_final = 3};
enum {JSON_integer_error = 0};
@@ -883,7 +884,7 @@ enum {JSON_integer_error = 0};
enum {JSON_integer_en_main = 1};
-#line 306 "parser.rl"
+#line 307 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -891,15 +892,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 895 "parser.c"
+#line 896 "parser.c"
{
cs = JSON_integer_start;
}
-#line 313 "parser.rl"
+#line 314 "parser.rl"
json->memo = p;
-#line 903 "parser.c"
+#line 904 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -933,14 +934,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 303 "parser.rl"
+#line 304 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 944 "parser.c"
+#line 945 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -959,7 +960,7 @@ case 5:
_out: {}
}
-#line 315 "parser.rl"
+#line 316 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -974,7 +975,7 @@ case 5:
}
-#line 978 "parser.c"
+#line 979 "parser.c"
enum {JSON_float_start = 1};
enum {JSON_float_first_final = 8};
enum {JSON_float_error = 0};
@@ -982,23 +983,36 @@ enum {JSON_float_error = 0};
enum {JSON_float_en_main = 1};
-#line 340 "parser.rl"
+#line 341 "parser.rl"
+static int is_bigdecimal_class(VALUE obj)
+{
+ if (cBigDecimal == Qundef) {
+ if (rb_const_defined(rb_cObject, i_BigDecimal)) {
+ cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal);
+ }
+ else {
+ return 0;
+ }
+ }
+ return obj == cBigDecimal;
+}
+
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
-#line 994 "parser.c"
+#line 1008 "parser.c"
{
cs = JSON_float_start;
}
-#line 347 "parser.rl"
+#line 361 "parser.rl"
json->memo = p;
-#line 1002 "parser.c"
+#line 1016 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1056,14 +1070,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 334 "parser.rl"
+#line 335 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 1067 "parser.c"
+#line 1081 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1124,7 +1138,7 @@ case 7:
_out: {}
}
-#line 349 "parser.rl"
+#line 363 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -1136,7 +1150,11 @@ case 7:
} else {
VALUE text;
text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
- *result = rb_funcall(json->decimal_class, i_new, 1, text);
+ if (is_bigdecimal_class(json->decimal_class)) {
+ *result = rb_funcall(Qnil, i_BigDecimal, 1, text);
+ } else {
+ *result = rb_funcall(json->decimal_class, i_new, 1, text);
+ }
}
return p + 1;
} else {
@@ -1146,7 +1164,7 @@ case 7:
-#line 1150 "parser.c"
+#line 1168 "parser.c"
enum {JSON_array_start = 1};
enum {JSON_array_first_final = 17};
enum {JSON_array_error = 0};
@@ -1154,7 +1172,7 @@ enum {JSON_array_error = 0};
enum {JSON_array_en_main = 1};
-#line 398 "parser.rl"
+#line 416 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -1168,14 +1186,14 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
*result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
-#line 1172 "parser.c"
+#line 1190 "parser.c"
{
cs = JSON_array_start;
}
-#line 411 "parser.rl"
+#line 429 "parser.rl"
-#line 1179 "parser.c"
+#line 1197 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1214,7 +1232,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 375 "parser.rl"
+#line 393 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@@ -1234,7 +1252,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1238 "parser.c"
+#line 1256 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1334,14 +1352,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 390 "parser.rl"
+#line 408 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1345 "parser.c"
+#line 1363 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1397,7 +1415,7 @@ case 16:
_out: {}
}
-#line 412 "parser.rl"
+#line 430 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1486,7 +1504,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1490 "parser.c"
+#line 1508 "parser.c"
enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0};
@@ -1494,7 +1512,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1};
-#line 519 "parser.rl"
+#line 537 "parser.rl"
static int
@@ -1516,15 +1534,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1520 "parser.c"
+#line 1538 "parser.c"
{
cs = JSON_string_start;
}
-#line 540 "parser.rl"
+#line 558 "parser.rl"
json->memo = p;
-#line 1528 "parser.c"
+#line 1546 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1549,7 +1567,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 505 "parser.rl"
+#line 523 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1560,14 +1578,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 516 "parser.rl"
+#line 534 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1571 "parser.c"
+#line 1589 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1643,7 +1661,7 @@ case 7:
_out: {}
}
-#line 542 "parser.rl"
+#line 560 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1658,7 +1676,7 @@ case 7:
if (json->symbolize_names && json->parsing_name) {
*result = rb_str_intern(*result);
- } else {
+ } else if (RB_TYPE_P(*result, T_STRING)) {
rb_str_resize(*result, RSTRING_LEN(*result));
}
if (cs >= JSON_string_first_final) {
@@ -1815,7 +1833,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->max_nesting = 100;
json->allow_nan = 0;
- json->create_additions = 1;
+ json->create_additions = 0;
json->create_id = rb_funcall(mJSON, i_create_id, 0);
json->object_class = Qnil;
json->array_class = Qnil;
@@ -1830,7 +1848,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1834 "parser.c"
+#line 1852 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@@ -1838,7 +1856,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
-#line 742 "parser.rl"
+#line 760 "parser.rl"
/*
@@ -1855,16 +1873,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
-#line 1859 "parser.c"
+#line 1877 "parser.c"
{
cs = JSON_start;
}
-#line 758 "parser.rl"
+#line 776 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1868 "parser.c"
+#line 1886 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1898,7 +1916,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 734 "parser.rl"
+#line 752 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result, 0);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1908,7 +1926,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1912 "parser.c"
+#line 1930 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1997,7 +2015,7 @@ case 9:
_out: {}
}
-#line 761 "parser.rl"
+#line 779 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -2064,20 +2082,28 @@ static VALUE cParser_source(VALUE self)
void Init_parser(void)
{
+#undef rb_intern
rb_require("json/common");
mJSON = rb_define_module("JSON");
mExt = rb_define_module_under(mJSON, "Ext");
cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
eParserError = rb_path2class("JSON::ParserError");
eNestingError = rb_path2class("JSON::NestingError");
+ rb_gc_register_mark_object(eParserError);
+ rb_gc_register_mark_object(eNestingError);
rb_define_alloc_func(cParser, cJSON_parser_s_allocate);
rb_define_method(cParser, "initialize", cParser_initialize, -1);
rb_define_method(cParser, "parse", cParser_parse, 0);
rb_define_method(cParser, "source", cParser_source, 0);
CNaN = rb_const_get(mJSON, rb_intern("NaN"));
+ rb_gc_register_mark_object(CNaN);
+
CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
+ rb_gc_register_mark_object(CInfinity);
+
CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
+ rb_gc_register_mark_object(CMinusInfinity);
i_json_creatable_p = rb_intern("json_creatable?");
i_json_create = rb_intern("json_create");
@@ -2098,6 +2124,7 @@ void Init_parser(void)
i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
i_new = rb_intern("new");
+ i_BigDecimal = rb_intern("BigDecimal");
}
/*