summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/json/extconf.rb1
-rw-r--r--ext/json/parser/extconf.rb25
-rw-r--r--ext/json/parser/parser.c115
-rw-r--r--ext/json/parser/parser.h1
-rw-r--r--ext/json/parser/parser.rl29
-rw-r--r--test/json/json_parser_test.rb21
6 files changed, 147 insertions, 45 deletions
@@ -1,2 +1,3 @@
require 'mkmf'
create_makefile('json')
@@ -3,4 +3,29 @@ require 'mkmf'
have_func("rb_enc_raise", "ruby.h")
create_makefile 'json/ext/parser'
@@ -97,7 +97,7 @@ 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_BigDecimal;
#line 126 "parser.rl"
@@ -869,6 +869,10 @@ case 28:
#line 292 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
} else {
@@ -877,7 +881,7 @@ case 28:
}
-#line 881 "parser.c"
enum {JSON_integer_start = 1};
enum {JSON_integer_first_final = 3};
enum {JSON_integer_error = 0};
@@ -885,7 +889,7 @@ enum {JSON_integer_error = 0};
enum {JSON_integer_en_main = 1};
-#line 308 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -893,15 +897,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 897 "parser.c"
{
cs = JSON_integer_start;
}
-#line 315 "parser.rl"
json->memo = p;
-#line 905 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -935,14 +939,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 305 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 946 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -961,7 +965,7 @@ case 5:
_out: {}
}
-#line 317 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -976,7 +980,7 @@ case 5:
}
-#line 980 "parser.c"
enum {JSON_float_start = 1};
enum {JSON_float_first_final = 8};
enum {JSON_float_error = 0};
@@ -984,7 +988,7 @@ enum {JSON_float_error = 0};
enum {JSON_float_en_main = 1};
-#line 342 "parser.rl"
static int is_bigdecimal_class(VALUE obj)
@@ -1005,15 +1009,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 1009 "parser.c"
{
cs = JSON_float_start;
}
-#line 362 "parser.rl"
json->memo = p;
-#line 1017 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1071,14 +1075,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 336 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 1082 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1139,7 +1143,7 @@ case 7:
_out: {}
}
-#line 364 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -1165,7 +1169,7 @@ case 7:
-#line 1169 "parser.c"
enum {JSON_array_start = 1};
enum {JSON_array_first_final = 17};
enum {JSON_array_error = 0};
@@ -1173,7 +1177,7 @@ enum {JSON_array_error = 0};
enum {JSON_array_en_main = 1};
-#line 417 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -1187,14 +1191,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 1191 "parser.c"
{
cs = JSON_array_start;
}
-#line 430 "parser.rl"
-#line 1198 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1233,7 +1237,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 394 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@@ -1253,7 +1257,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1257 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1353,14 +1357,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 409 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1364 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1416,7 +1420,7 @@ case 16:
_out: {}
}
-#line 431 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1505,7 +1509,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1509 "parser.c"
enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0};
@@ -1513,7 +1517,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1};
-#line 538 "parser.rl"
static int
@@ -1535,15 +1539,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1539 "parser.c"
{
cs = JSON_string_start;
}
-#line 559 "parser.rl"
json->memo = p;
-#line 1547 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1568,7 +1572,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 524 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1579,14 +1583,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 535 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1590 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1662,7 +1666,7 @@ case 7:
_out: {}
}
-#line 561 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1678,7 +1682,22 @@ case 7:
if (json->symbolize_names && json->parsing_name) {
*result = rb_str_intern(*result);
} else if (RB_TYPE_P(*result, T_STRING)) {
rb_str_resize(*result, RSTRING_LEN(*result));
}
if (cs >= JSON_string_first_final) {
return p + 1;
@@ -1786,6 +1805,12 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->symbolize_names = 0;
}
tmp = ID2SYM(i_create_additions);
if (option_given_p(opts, tmp)) {
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
@@ -1849,7 +1874,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1853 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@@ -1857,7 +1882,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
-#line 761 "parser.rl"
/*
@@ -1874,16 +1899,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
-#line 1878 "parser.c"
{
cs = JSON_start;
}
-#line 777 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1887 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1917,7 +1942,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 753 "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;}
@@ -1927,7 +1952,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1931 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -2016,7 +2041,7 @@ case 9:
_out: {}
}
-#line 780 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -2126,6 +2151,8 @@ void Init_parser(void)
i_leftshift = rb_intern("<<");
i_new = rb_intern("new");
i_BigDecimal = rb_intern("BigDecimal");
}
/*
@@ -37,6 +37,7 @@ typedef struct JSON_ParserStruct {
int allow_nan;
int parsing_name;
int symbolize_names;
VALUE object_class;
VALUE array_class;
VALUE decimal_class;
@@ -95,7 +95,7 @@ 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_BigDecimal;
%%{
machine JSON_common;
@@ -290,6 +290,10 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
%% write init;
%% write exec;
if (cs >= JSON_value_first_final) {
return p;
} else {
@@ -573,7 +577,22 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
if (json->symbolize_names && json->parsing_name) {
*result = rb_str_intern(*result);
} else if (RB_TYPE_P(*result, T_STRING)) {
rb_str_resize(*result, RSTRING_LEN(*result));
}
if (cs >= JSON_string_first_final) {
return p + 1;
@@ -681,6 +700,12 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->symbolize_names = 0;
}
tmp = ID2SYM(i_create_additions);
if (option_given_p(opts, tmp)) {
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
@@ -886,6 +911,8 @@ void Init_parser(void)
i_leftshift = rb_intern("<<");
i_new = rb_intern("new");
i_BigDecimal = rb_intern("BigDecimal");
}
/*
@@ -218,6 +218,17 @@ class JSONParserTest < Test::Unit::TestCase
end
end
def test_parse_comments
json = <<EOT
{
@@ -468,6 +479,16 @@ EOT
private
def assert_equal_float(expected, actual, delta = 1e-2)
Array === expected and expected = expected.first
Array === actual and actual = actual.first