From e8eb95ec33334f40f3f3bbcbf16404aee07dfa8e Mon Sep 17 00:00:00 2001 From: naruse Date: Sat, 20 Sep 2008 17:41:14 +0000 Subject: * ext/json: import JSON v 1.1.3. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19436 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 + ext/json/ext/generator/extconf.rb | 5 +- ext/json/ext/generator/generator.c | 13 +- ext/json/ext/generator/unicode.c | 2 - ext/json/ext/parser/extconf.rb | 5 +- ext/json/ext/parser/parser.c | 633 +++++++++++++++++++------------------ ext/json/ext/parser/parser.rl | 64 ++-- ext/json/ext/parser/unicode.c | 10 +- ext/json/lib/json/add/core.rb | 41 ++- ext/json/lib/json/common.rb | 10 +- ext/json/lib/json/editor.rb | 1 - ext/json/lib/json/pure/parser.rb | 5 +- ext/json/lib/json/version.rb | 2 +- test/json/fixtures/fail18.json | 2 +- test/json/runner.rb | 1 - test/json/test_json.rb | 64 +++- test/json/test_json_addition.rb | 38 ++- test/json/test_json_generate.rb | 19 ++ test/json/test_json_rails.rb | 11 +- test/json/test_json_unicode.rb | 5 +- version.h | 6 +- 21 files changed, 506 insertions(+), 435 deletions(-) diff --git a/ChangeLog b/ChangeLog index d60772d124..af60235bcc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sun Sep 21 02:40:20 2008 NARUSE, Yui + + * ext/json: import JSON v 1.1.3. + Sat Sep 20 11:43:08 2008 Yusuke Endoh * lib/optparse.rb (summarize): separator "" should output new line. diff --git a/ext/json/ext/generator/extconf.rb b/ext/json/ext/generator/extconf.rb index 86ed7e6367..88aaf40d70 100644 --- a/ext/json/ext/generator/extconf.rb +++ b/ext/json/ext/generator/extconf.rb @@ -2,9 +2,8 @@ require 'mkmf' require 'rbconfig' if CONFIG['CC'] =~ /gcc/ - #$CFLAGS += ' -Wall -ggdb' $CFLAGS += ' -Wall' + #$CFLAGS += ' -O0 -ggdb' end -have_header 'st.h' -create_makefile 'json/ext/generator' +create_makefile 'generator' diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index b8a1ea16d1..1f48d3c780 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -1,15 +1,8 @@ -/* vim: set cin et sw=4 ts=4: */ - +#include #include "ruby.h" -#include "ruby/st.h" #include "unicode.h" -#include #include -#ifdef HAVE_IEEEFP_H -#include -#endif - #define check_max_nesting(state, depth) do { \ long current_nesting = 1 + depth; \ if (state->max_nesting != 0 && current_nesting > state->max_nesting) \ @@ -297,14 +290,14 @@ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self) return rb_funcall(self, i_to_s, 0); } else { tmp = rb_funcall(self, i_to_s, 0); - rb_raise(eGeneratorError, "%s not allowed in JSON", StringValueCStr(tmp)); + rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp)); } } else if (isnan(value)) { if (!state || state->allow_nan) { return rb_funcall(self, i_to_s, 0); } else { tmp = rb_funcall(self, i_to_s, 0); - rb_raise(eGeneratorError, "%s not allowed in JSON", StringValueCStr(tmp)); + rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp)); } } else { return rb_funcall(self, i_to_s, 0); diff --git a/ext/json/ext/generator/unicode.c b/ext/json/ext/generator/unicode.c index 44e1f41f98..76834eadc5 100644 --- a/ext/json/ext/generator/unicode.c +++ b/ext/json/ext/generator/unicode.c @@ -1,5 +1,3 @@ -/* vim: set cin et sw=4 ts=4: */ - #include "unicode.h" #define unicode_escape(buffer, character) \ diff --git a/ext/json/ext/parser/extconf.rb b/ext/json/ext/parser/extconf.rb index b2e7b051a4..f511bf07da 100644 --- a/ext/json/ext/parser/extconf.rb +++ b/ext/json/ext/parser/extconf.rb @@ -2,9 +2,8 @@ require 'mkmf' require 'rbconfig' if CONFIG['CC'] =~ /gcc/ - #$CFLAGS += ' -Wall -ggdb' $CFLAGS += ' -Wall' + #$CFLAGS += ' -O0 -ggdb' end -have_header 'st.h' -create_makefile 'json/ext/parser' +create_makefile 'parser' diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 8734de2f5b..f2bedebea9 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -1,10 +1,5 @@ #line 1 "parser.rl" -/* vim: set cin et sw=4 ts=4: */ - #include "ruby.h" -#include "ruby/re.h" -#include "ruby/st.h" -#include "ruby/encoding.h" #include "unicode.h" #define EVIL 0x666 @@ -39,18 +34,18 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul JSON_Parser *json; \ Data_Get_Struct(self, JSON_Parser, json); -#line 67 "parser.rl" +#line 64 "parser.rl" -#line 47 "parser.c" +#line 44 "parser.c" static const int JSON_object_start = 1; static const int JSON_object_first_final = 27; static const int JSON_object_error = 0; static const int JSON_object_en_main = 1; -#line 100 "parser.rl" +#line 97 "parser.rl" static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -65,16 +60,16 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = rb_hash_new(); -#line 69 "parser.c" +#line 66 "parser.c" { cs = JSON_object_start; } -#line 114 "parser.rl" +#line 111 "parser.rl" -#line 75 "parser.c" +#line 72 "parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { case 1: @@ -82,10 +77,11 @@ case 1: goto st2; goto st0; st0: - goto _out0; +cs = 0; + goto _out; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: switch( (*p) ) { case 13: goto st2; @@ -98,17 +94,17 @@ case 2: goto st2; goto st0; tr2: -#line 86 "parser.rl" +#line 83 "parser.rl" { char *np = JSON_parse_string(json, p, pe, &last_name); - if (np == NULL) goto _out3; else {p = (( np))-1;} + if (np == NULL) { p--; {p++; cs = 3; goto _out;} } else {p = (( np))-1;} } goto st3; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: -#line 112 "parser.c" +#line 110 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -120,7 +116,7 @@ case 3: goto st0; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: switch( (*p) ) { case 42: goto st5; @@ -129,14 +125,14 @@ case 4: goto st0; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: if ( (*p) == 42 ) goto st6; goto st5; st6: if ( ++p == pe ) - goto _out6; + goto _test_eof6; case 6: switch( (*p) ) { case 42: goto st6; @@ -145,14 +141,14 @@ case 6: goto st5; st7: if ( ++p == pe ) - goto _out7; + goto _test_eof7; case 7: if ( (*p) == 10 ) goto st3; goto st7; st8: if ( ++p == pe ) - goto _out8; + goto _test_eof8; case 8: switch( (*p) ) { case 13: goto st8; @@ -175,12 +171,12 @@ case 8: goto st8; goto st0; tr11: -#line 75 "parser.rl" +#line 72 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v); if (np == NULL) { - goto _out9; + p--; {p++; cs = 9; goto _out;} } else { rb_hash_aset(*result, last_name, v); {p = (( np))-1;} @@ -189,9 +185,9 @@ tr11: goto st9; st9: if ( ++p == pe ) - goto _out9; + goto _test_eof9; case 9: -#line 195 "parser.c" +#line 193 "parser.c" switch( (*p) ) { case 13: goto st9; case 32: goto st9; @@ -204,7 +200,7 @@ case 9: goto st0; st10: if ( ++p == pe ) - goto _out10; + goto _test_eof10; case 10: switch( (*p) ) { case 13: goto st10; @@ -217,7 +213,7 @@ case 10: goto st0; st11: if ( ++p == pe ) - goto _out11; + goto _test_eof11; case 11: switch( (*p) ) { case 42: goto st12; @@ -226,14 +222,14 @@ case 11: goto st0; st12: if ( ++p == pe ) - goto _out12; + goto _test_eof12; case 12: if ( (*p) == 42 ) goto st13; goto st12; st13: if ( ++p == pe ) - goto _out13; + goto _test_eof13; case 13: switch( (*p) ) { case 42: goto st13; @@ -242,14 +238,14 @@ case 13: goto st12; st14: if ( ++p == pe ) - goto _out14; + goto _test_eof14; case 14: if ( (*p) == 10 ) goto st10; goto st14; st15: if ( ++p == pe ) - goto _out15; + goto _test_eof15; case 15: switch( (*p) ) { case 42: goto st16; @@ -258,14 +254,14 @@ case 15: goto st0; st16: if ( ++p == pe ) - goto _out16; + goto _test_eof16; case 16: if ( (*p) == 42 ) goto st17; goto st16; st17: if ( ++p == pe ) - goto _out17; + goto _test_eof17; case 17: switch( (*p) ) { case 42: goto st17; @@ -274,24 +270,24 @@ case 17: goto st16; st18: if ( ++p == pe ) - goto _out18; + goto _test_eof18; case 18: if ( (*p) == 10 ) goto st9; goto st18; tr4: -#line 91 "parser.rl" - { goto _out27; } +#line 88 "parser.rl" + { p--; {p++; cs = 27; goto _out;} } goto st27; st27: if ( ++p == pe ) - goto _out27; + goto _test_eof27; case 27: -#line 291 "parser.c" +#line 289 "parser.c" goto st0; st19: if ( ++p == pe ) - goto _out19; + goto _test_eof19; case 19: switch( (*p) ) { case 42: goto st20; @@ -300,14 +296,14 @@ case 19: goto st0; st20: if ( ++p == pe ) - goto _out20; + goto _test_eof20; case 20: if ( (*p) == 42 ) goto st21; goto st20; st21: if ( ++p == pe ) - goto _out21; + goto _test_eof21; case 21: switch( (*p) ) { case 42: goto st21; @@ -316,14 +312,14 @@ case 21: goto st20; st22: if ( ++p == pe ) - goto _out22; + goto _test_eof22; case 22: if ( (*p) == 10 ) goto st8; goto st22; st23: if ( ++p == pe ) - goto _out23; + goto _test_eof23; case 23: switch( (*p) ) { case 42: goto st24; @@ -332,14 +328,14 @@ case 23: goto st0; st24: if ( ++p == pe ) - goto _out24; + goto _test_eof24; case 24: if ( (*p) == 42 ) goto st25; goto st24; st25: if ( ++p == pe ) - goto _out25; + goto _test_eof25; case 25: switch( (*p) ) { case 42: goto st25; @@ -348,43 +344,43 @@ case 25: goto st24; st26: if ( ++p == pe ) - goto _out26; + goto _test_eof26; case 26: if ( (*p) == 10 ) goto st2; goto st26; } - _out0: cs = 0; goto _out; - _out2: cs = 2; goto _out; - _out3: cs = 3; goto _out; - _out4: cs = 4; goto _out; - _out5: cs = 5; goto _out; - _out6: cs = 6; goto _out; - _out7: cs = 7; goto _out; - _out8: cs = 8; goto _out; - _out9: cs = 9; goto _out; - _out10: cs = 10; goto _out; - _out11: cs = 11; goto _out; - _out12: cs = 12; goto _out; - _out13: cs = 13; goto _out; - _out14: cs = 14; goto _out; - _out15: cs = 15; goto _out; - _out16: cs = 16; goto _out; - _out17: cs = 17; goto _out; - _out18: cs = 18; goto _out; - _out27: cs = 27; goto _out; - _out19: cs = 19; goto _out; - _out20: cs = 20; goto _out; - _out21: cs = 21; goto _out; - _out22: cs = 22; goto _out; - _out23: cs = 23; goto _out; - _out24: cs = 24; goto _out; - _out25: cs = 25; goto _out; - _out26: cs = 26; goto _out; - + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof11: cs = 11; goto _test_eof; + _test_eof12: cs = 12; goto _test_eof; + _test_eof13: cs = 13; goto _test_eof; + _test_eof14: cs = 14; goto _test_eof; + _test_eof15: cs = 15; goto _test_eof; + _test_eof16: cs = 16; goto _test_eof; + _test_eof17: cs = 17; goto _test_eof; + _test_eof18: cs = 18; goto _test_eof; + _test_eof27: cs = 27; goto _test_eof; + _test_eof19: cs = 19; goto _test_eof; + _test_eof20: cs = 20; goto _test_eof; + _test_eof21: cs = 21; goto _test_eof; + _test_eof22: cs = 22; goto _test_eof; + _test_eof23: cs = 23; goto _test_eof; + _test_eof24: cs = 24; goto _test_eof; + _test_eof25: cs = 25; goto _test_eof; + _test_eof26: cs = 26; goto _test_eof; + + _test_eof: {} _out: {} } -#line 115 "parser.rl" +#line 112 "parser.rl" if (cs >= JSON_object_first_final) { if (RTEST(json->create_id)) { @@ -403,14 +399,14 @@ case 26: } -#line 407 "parser.c" +#line 405 "parser.c" static const int JSON_value_start = 1; static const int JSON_value_first_final = 21; static const int JSON_value_error = 0; static const int JSON_value_en_main = 1; -#line 213 "parser.rl" +#line 210 "parser.rl" static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -418,16 +414,16 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 422 "parser.c" +#line 420 "parser.c" { cs = JSON_value_start; } -#line 220 "parser.rl" +#line 217 "parser.rl" -#line 428 "parser.c" +#line 426 "parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { case 1: @@ -446,259 +442,260 @@ case 1: goto tr2; goto st0; st0: - goto _out0; +cs = 0; + goto _out; tr0: -#line 161 "parser.rl" +#line 158 "parser.rl" { char *np = JSON_parse_string(json, p, pe, result); - if (np == NULL) goto _out21; else {p = (( np))-1;} + if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;} } goto st21; tr2: -#line 166 "parser.rl" +#line 163 "parser.rl" { char *np; if(pe > p + 9 && !strncmp(MinusInfinity, p, 9)) { if (json->allow_nan) { *result = CMinusInfinity; {p = (( p + 10))-1;} - goto _out21; + p--; {p++; cs = 21; goto _out;} } else { - rb_raise(eParserError, "unexpected token at '%s'", p); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); } } np = JSON_parse_float(json, p, pe, result); if (np != NULL) {p = (( np))-1;} np = JSON_parse_integer(json, p, pe, result); if (np != NULL) {p = (( np))-1;} - goto _out21; + p--; {p++; cs = 21; goto _out;} } goto st21; tr5: -#line 184 "parser.rl" +#line 181 "parser.rl" { char *np; - json->current_nesting += 1; + json->current_nesting++; np = JSON_parse_array(json, p, pe, result); - json->current_nesting -= 1; - if (np == NULL) goto _out21; else {p = (( np))-1;} + json->current_nesting--; + if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;} } goto st21; tr9: -#line 192 "parser.rl" +#line 189 "parser.rl" { char *np; - json->current_nesting += 1; + json->current_nesting++; np = JSON_parse_object(json, p, pe, result); - json->current_nesting -= 1; - if (np == NULL) goto _out21; else {p = (( np))-1;} + json->current_nesting--; + if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;} } goto st21; tr16: -#line 154 "parser.rl" +#line 151 "parser.rl" { if (json->allow_nan) { *result = CInfinity; } else { - rb_raise(eParserError, "unexpected token at '%s'", p - 8); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8); } } goto st21; tr18: -#line 147 "parser.rl" +#line 144 "parser.rl" { if (json->allow_nan) { *result = CNaN; } else { - rb_raise(eParserError, "unexpected token at '%s'", p - 2); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2); } } goto st21; tr22: -#line 141 "parser.rl" +#line 138 "parser.rl" { *result = Qfalse; } goto st21; tr25: -#line 138 "parser.rl" +#line 135 "parser.rl" { *result = Qnil; } goto st21; tr28: -#line 144 "parser.rl" +#line 141 "parser.rl" { *result = Qtrue; } goto st21; st21: if ( ++p == pe ) - goto _out21; + goto _test_eof21; case 21: -#line 200 "parser.rl" - { goto _out21; } -#line 542 "parser.c" +#line 197 "parser.rl" + { p--; {p++; cs = 21; goto _out;} } +#line 541 "parser.c" goto st0; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: if ( (*p) == 110 ) goto st3; goto st0; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: if ( (*p) == 102 ) goto st4; goto st0; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: if ( (*p) == 105 ) goto st5; goto st0; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: if ( (*p) == 110 ) goto st6; goto st0; st6: if ( ++p == pe ) - goto _out6; + goto _test_eof6; case 6: if ( (*p) == 105 ) goto st7; goto st0; st7: if ( ++p == pe ) - goto _out7; + goto _test_eof7; case 7: if ( (*p) == 116 ) goto st8; goto st0; st8: if ( ++p == pe ) - goto _out8; + goto _test_eof8; case 8: if ( (*p) == 121 ) goto tr16; goto st0; st9: if ( ++p == pe ) - goto _out9; + goto _test_eof9; case 9: if ( (*p) == 97 ) goto st10; goto st0; st10: if ( ++p == pe ) - goto _out10; + goto _test_eof10; case 10: if ( (*p) == 78 ) goto tr18; goto st0; st11: if ( ++p == pe ) - goto _out11; + goto _test_eof11; case 11: if ( (*p) == 97 ) goto st12; goto st0; st12: if ( ++p == pe ) - goto _out12; + goto _test_eof12; case 12: if ( (*p) == 108 ) goto st13; goto st0; st13: if ( ++p == pe ) - goto _out13; + goto _test_eof13; case 13: if ( (*p) == 115 ) goto st14; goto st0; st14: if ( ++p == pe ) - goto _out14; + goto _test_eof14; case 14: if ( (*p) == 101 ) goto tr22; goto st0; st15: if ( ++p == pe ) - goto _out15; + goto _test_eof15; case 15: if ( (*p) == 117 ) goto st16; goto st0; st16: if ( ++p == pe ) - goto _out16; + goto _test_eof16; case 16: if ( (*p) == 108 ) goto st17; goto st0; st17: if ( ++p == pe ) - goto _out17; + goto _test_eof17; case 17: if ( (*p) == 108 ) goto tr25; goto st0; st18: if ( ++p == pe ) - goto _out18; + goto _test_eof18; case 18: if ( (*p) == 114 ) goto st19; goto st0; st19: if ( ++p == pe ) - goto _out19; + goto _test_eof19; case 19: if ( (*p) == 117 ) goto st20; goto st0; st20: if ( ++p == pe ) - goto _out20; + goto _test_eof20; case 20: if ( (*p) == 101 ) goto tr28; goto st0; } - _out0: cs = 0; goto _out; - _out21: cs = 21; goto _out; - _out2: cs = 2; goto _out; - _out3: cs = 3; goto _out; - _out4: cs = 4; goto _out; - _out5: cs = 5; goto _out; - _out6: cs = 6; goto _out; - _out7: cs = 7; goto _out; - _out8: cs = 8; goto _out; - _out9: cs = 9; goto _out; - _out10: cs = 10; goto _out; - _out11: cs = 11; goto _out; - _out12: cs = 12; goto _out; - _out13: cs = 13; goto _out; - _out14: cs = 14; goto _out; - _out15: cs = 15; goto _out; - _out16: cs = 16; goto _out; - _out17: cs = 17; goto _out; - _out18: cs = 18; goto _out; - _out19: cs = 19; goto _out; - _out20: cs = 20; goto _out; - + _test_eof21: cs = 21; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof11: cs = 11; goto _test_eof; + _test_eof12: cs = 12; goto _test_eof; + _test_eof13: cs = 13; goto _test_eof; + _test_eof14: cs = 14; goto _test_eof; + _test_eof15: cs = 15; goto _test_eof; + _test_eof16: cs = 16; goto _test_eof; + _test_eof17: cs = 17; goto _test_eof; + _test_eof18: cs = 18; goto _test_eof; + _test_eof19: cs = 19; goto _test_eof; + _test_eof20: cs = 20; goto _test_eof; + + _test_eof: {} _out: {} } -#line 221 "parser.rl" +#line 218 "parser.rl" if (cs >= JSON_value_first_final) { return p; @@ -708,14 +705,14 @@ case 20: } -#line 712 "parser.c" +#line 711 "parser.c" static const int JSON_integer_start = 1; static const int JSON_integer_first_final = 5; static const int JSON_integer_error = 0; static const int JSON_integer_en_main = 1; -#line 237 "parser.rl" +#line 234 "parser.rl" static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -723,17 +720,17 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res int cs = EVIL; -#line 727 "parser.c" +#line 726 "parser.c" { cs = JSON_integer_start; } -#line 244 "parser.rl" +#line 241 "parser.rl" json->memo = p; -#line 734 "parser.c" +#line 733 "parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { case 1: @@ -745,10 +742,11 @@ case 1: goto st4; goto st0; st0: - goto _out0; +cs = 0; + goto _out; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: if ( (*p) == 48 ) goto st3; @@ -757,38 +755,38 @@ case 2: goto st0; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: if ( 48 <= (*p) && (*p) <= 57 ) goto st0; goto tr4; tr4: -#line 234 "parser.rl" - { goto _out5; } +#line 231 "parser.rl" + { p--; {p++; cs = 5; goto _out;} } goto st5; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: #line 774 "parser.c" goto st0; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: if ( 48 <= (*p) && (*p) <= 57 ) goto st4; goto tr4; } - _out0: cs = 0; goto _out; - _out2: cs = 2; goto _out; - _out3: cs = 3; goto _out; - _out5: cs = 5; goto _out; - _out4: cs = 4; goto _out; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof: {} _out: {} } -#line 246 "parser.rl" +#line 243 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; @@ -807,7 +805,7 @@ static const int JSON_float_error = 0; static const int JSON_float_en_main = 1; -#line 268 "parser.rl" +#line 265 "parser.rl" static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -819,13 +817,13 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul { cs = JSON_float_start; } -#line 275 "parser.rl" +#line 272 "parser.rl" json->memo = p; #line 826 "parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { case 1: @@ -837,10 +835,11 @@ case 1: goto st9; goto st0; st0: - goto _out0; +cs = 0; + goto _out; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: if ( (*p) == 48 ) goto st3; @@ -849,7 +848,7 @@ case 2: goto st0; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: switch( (*p) ) { case 46: goto st4; @@ -859,14 +858,14 @@ case 3: goto st0; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: if ( 48 <= (*p) && (*p) <= 57 ) goto st5; goto st0; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: switch( (*p) ) { case 69: goto st6; @@ -879,18 +878,18 @@ case 5: goto st0; goto tr7; tr7: -#line 262 "parser.rl" - { goto _out10; } +#line 259 "parser.rl" + { p--; {p++; cs = 10; goto _out;} } goto st10; st10: if ( ++p == pe ) - goto _out10; + goto _test_eof10; case 10: -#line 890 "parser.c" +#line 891 "parser.c" goto st0; st6: if ( ++p == pe ) - goto _out6; + goto _test_eof6; case 6: switch( (*p) ) { case 43: goto st7; @@ -901,14 +900,14 @@ case 6: goto st0; st7: if ( ++p == pe ) - goto _out7; + goto _test_eof7; case 7: if ( 48 <= (*p) && (*p) <= 57 ) goto st8; goto st0; st8: if ( ++p == pe ) - goto _out8; + goto _test_eof8; case 8: switch( (*p) ) { case 69: goto st0; @@ -922,7 +921,7 @@ case 8: goto tr7; st9: if ( ++p == pe ) - goto _out9; + goto _test_eof9; case 9: switch( (*p) ) { case 46: goto st4; @@ -933,20 +932,20 @@ case 9: goto st9; goto st0; } - _out0: cs = 0; goto _out; - _out2: cs = 2; goto _out; - _out3: cs = 3; goto _out; - _out4: cs = 4; goto _out; - _out5: cs = 5; goto _out; - _out10: cs = 10; goto _out; - _out6: cs = 6; goto _out; - _out7: cs = 7; goto _out; - _out8: cs = 8; goto _out; - _out9: cs = 9; goto _out; - + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + + _test_eof: {} _out: {} } -#line 277 "parser.rl" +#line 274 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; @@ -959,14 +958,14 @@ case 9: -#line 963 "parser.c" +#line 964 "parser.c" static const int JSON_array_start = 1; static const int JSON_array_first_final = 17; static const int JSON_array_error = 0; static const int JSON_array_en_main = 1; -#line 313 "parser.rl" +#line 310 "parser.rl" static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -979,16 +978,16 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul *result = rb_ary_new(); -#line 983 "parser.c" +#line 984 "parser.c" { cs = JSON_array_start; } -#line 325 "parser.rl" +#line 322 "parser.rl" -#line 989 "parser.c" +#line 990 "parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { case 1: @@ -996,10 +995,11 @@ case 1: goto st2; goto st0; st0: - goto _out0; +cs = 0; + goto _out; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: switch( (*p) ) { case 13: goto st2; @@ -1023,12 +1023,12 @@ case 2: goto st2; goto st0; tr2: -#line 294 "parser.rl" +#line 291 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v); if (np == NULL) { - goto _out3; + p--; {p++; cs = 3; goto _out;} } else { rb_ary_push(*result, v); {p = (( np))-1;} @@ -1037,9 +1037,9 @@ tr2: goto st3; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: -#line 1043 "parser.c" +#line 1045 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -1052,7 +1052,7 @@ case 3: goto st0; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: switch( (*p) ) { case 13: goto st4; @@ -1076,7 +1076,7 @@ case 4: goto st0; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: switch( (*p) ) { case 42: goto st6; @@ -1085,14 +1085,14 @@ case 5: goto st0; st6: if ( ++p == pe ) - goto _out6; + goto _test_eof6; case 6: if ( (*p) == 42 ) goto st7; goto st6; st7: if ( ++p == pe ) - goto _out7; + goto _test_eof7; case 7: switch( (*p) ) { case 42: goto st7; @@ -1101,14 +1101,14 @@ case 7: goto st6; st8: if ( ++p == pe ) - goto _out8; + goto _test_eof8; case 8: if ( (*p) == 10 ) goto st4; goto st8; st9: if ( ++p == pe ) - goto _out9; + goto _test_eof9; case 9: switch( (*p) ) { case 42: goto st10; @@ -1117,14 +1117,14 @@ case 9: goto st0; st10: if ( ++p == pe ) - goto _out10; + goto _test_eof10; case 10: if ( (*p) == 42 ) goto st11; goto st10; st11: if ( ++p == pe ) - goto _out11; + goto _test_eof11; case 11: switch( (*p) ) { case 42: goto st11; @@ -1133,24 +1133,24 @@ case 11: goto st10; st12: if ( ++p == pe ) - goto _out12; + goto _test_eof12; case 12: if ( (*p) == 10 ) goto st3; goto st12; tr4: -#line 305 "parser.rl" - { goto _out17; } +#line 302 "parser.rl" + { p--; {p++; cs = 17; goto _out;} } goto st17; st17: if ( ++p == pe ) - goto _out17; + goto _test_eof17; case 17: -#line 1150 "parser.c" +#line 1152 "parser.c" goto st0; st13: if ( ++p == pe ) - goto _out13; + goto _test_eof13; case 13: switch( (*p) ) { case 42: goto st14; @@ -1159,14 +1159,14 @@ case 13: goto st0; st14: if ( ++p == pe ) - goto _out14; + goto _test_eof14; case 14: if ( (*p) == 42 ) goto st15; goto st14; st15: if ( ++p == pe ) - goto _out15; + goto _test_eof15; case 15: switch( (*p) ) { case 42: goto st15; @@ -1175,44 +1175,44 @@ case 15: goto st14; st16: if ( ++p == pe ) - goto _out16; + goto _test_eof16; case 16: if ( (*p) == 10 ) goto st2; goto st16; } - _out0: cs = 0; goto _out; - _out2: cs = 2; goto _out; - _out3: cs = 3; goto _out; - _out4: cs = 4; goto _out; - _out5: cs = 5; goto _out; - _out6: cs = 6; goto _out; - _out7: cs = 7; goto _out; - _out8: cs = 8; goto _out; - _out9: cs = 9; goto _out; - _out10: cs = 10; goto _out; - _out11: cs = 11; goto _out; - _out12: cs = 12; goto _out; - _out17: cs = 17; goto _out; - _out13: cs = 13; goto _out; - _out14: cs = 14; goto _out; - _out15: cs = 15; goto _out; - _out16: cs = 16; goto _out; - + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof11: cs = 11; goto _test_eof; + _test_eof12: cs = 12; goto _test_eof; + _test_eof17: cs = 17; goto _test_eof; + _test_eof13: cs = 13; goto _test_eof; + _test_eof14: cs = 14; goto _test_eof; + _test_eof15: cs = 15; goto _test_eof; + _test_eof16: cs = 16; goto _test_eof; + + _test_eof: {} _out: {} } -#line 326 "parser.rl" +#line 323 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; } else { - rb_raise(eParserError, "unexpected token at '%s'", p); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); } } static VALUE json_string_unescape(char *p, char *pe) { - VALUE result = rb_enc_str_new("", 0, rb_utf8_encoding()); + VALUE result = rb_str_buf_new(pe - p + 1); while (p < pe) { if (*p == '\\') { @@ -1267,33 +1267,33 @@ static VALUE json_string_unescape(char *p, char *pe) } -#line 1271 "parser.c" +#line 1273 "parser.c" static const int JSON_string_start = 1; static const int JSON_string_first_final = 8; static const int JSON_string_error = 0; static const int JSON_string_en_main = 1; -#line 404 "parser.rl" +#line 401 "parser.rl" static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result) { int cs = EVIL; - *result = rb_enc_str_new("", 0, rb_utf8_encoding()); + *result = rb_str_new("", 0); -#line 1287 "parser.c" +#line 1289 "parser.c" { cs = JSON_string_start; } -#line 412 "parser.rl" +#line 409 "parser.rl" json->memo = p; -#line 1294 "parser.c" +#line 1296 "parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { case 1: @@ -1301,10 +1301,11 @@ case 1: goto st2; goto st0; st0: - goto _out0; +cs = 0; + goto _out; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: switch( (*p) ) { case 34: goto tr2; @@ -1314,23 +1315,23 @@ case 2: goto st0; goto st2; tr2: -#line 396 "parser.rl" +#line 393 "parser.rl" { *result = json_string_unescape(json->memo + 1, p); - if (NIL_P(*result)) goto _out8; else {p = (( p + 1))-1;} + if (NIL_P(*result)) { p--; {p++; cs = 8; goto _out;} } else {p = (( p + 1))-1;} } -#line 401 "parser.rl" - { goto _out8; } +#line 398 "parser.rl" + { p--; {p++; cs = 8; goto _out;} } goto st8; st8: if ( ++p == pe ) - goto _out8; + goto _test_eof8; case 8: -#line 1330 "parser.c" +#line 1333 "parser.c" goto st0; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: if ( (*p) == 117 ) goto st4; @@ -1339,7 +1340,7 @@ case 3: goto st2; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -1352,7 +1353,7 @@ case 4: goto st0; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -1365,7 +1366,7 @@ case 5: goto st0; st6: if ( ++p == pe ) - goto _out6; + goto _test_eof6; case 6: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -1378,7 +1379,7 @@ case 6: goto st0; st7: if ( ++p == pe ) - goto _out7; + goto _test_eof7; case 7: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -1390,18 +1391,18 @@ case 7: goto st2; goto st0; } - _out0: cs = 0; goto _out; - _out2: cs = 2; goto _out; - _out8: cs = 8; goto _out; - _out3: cs = 3; goto _out; - _out4: cs = 4; goto _out; - _out5: cs = 5; goto _out; - _out6: cs = 6; goto _out; - _out7: cs = 7; goto _out; - + _test_eof2: cs = 2; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + + _test_eof: {} _out: {} } -#line 414 "parser.rl" +#line 411 "parser.rl" if (cs >= JSON_string_first_final) { return p + 1; @@ -1412,14 +1413,14 @@ case 7: -#line 1416 "parser.c" +#line 1419 "parser.c" static const int JSON_start = 1; static const int JSON_first_final = 10; static const int JSON_error = 0; static const int JSON_en_main = 1; -#line 448 "parser.rl" +#line 445 "parser.rl" /* @@ -1542,23 +1543,23 @@ static VALUE cParser_parse(VALUE self) GET_STRUCT; -#line 1546 "parser.c" +#line 1549 "parser.c" { cs = JSON_start; } -#line 570 "parser.rl" +#line 567 "parser.rl" p = json->source; pe = p + json->len; -#line 1554 "parser.c" +#line 1557 "parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { st1: if ( ++p == pe ) - goto _out1; + goto _test_eof1; case 1: switch( (*p) ) { case 13: goto st1; @@ -1571,10 +1572,11 @@ case 1: goto st1; goto st0; st0: - goto _out0; +cs = 0; + goto _out; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: switch( (*p) ) { case 42: goto st3; @@ -1583,14 +1585,14 @@ case 2: goto st0; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: if ( (*p) == 42 ) goto st4; goto st3; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: switch( (*p) ) { case 42: goto st4; @@ -1599,34 +1601,34 @@ case 4: goto st3; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: if ( (*p) == 10 ) goto st1; goto st5; tr3: -#line 437 "parser.rl" +#line 434 "parser.rl" { char *np; json->current_nesting = 1; np = JSON_parse_array(json, p, pe, &result); - if (np == NULL) goto _out10; else {p = (( np))-1;} + if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} } goto st10; tr4: -#line 430 "parser.rl" +#line 427 "parser.rl" { char *np; json->current_nesting = 1; np = JSON_parse_object(json, p, pe, &result); - if (np == NULL) goto _out10; else {p = (( np))-1;} + if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} } goto st10; st10: if ( ++p == pe ) - goto _out10; + goto _test_eof10; case 10: -#line 1630 "parser.c" +#line 1634 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1637,7 +1639,7 @@ case 10: goto st0; st6: if ( ++p == pe ) - goto _out6; + goto _test_eof6; case 6: switch( (*p) ) { case 42: goto st7; @@ -1646,14 +1648,14 @@ case 6: goto st0; st7: if ( ++p == pe ) - goto _out7; + goto _test_eof7; case 7: if ( (*p) == 42 ) goto st8; goto st7; st8: if ( ++p == pe ) - goto _out8; + goto _test_eof8; case 8: switch( (*p) ) { case 42: goto st8; @@ -1662,32 +1664,32 @@ case 8: goto st7; st9: if ( ++p == pe ) - goto _out9; + goto _test_eof9; case 9: if ( (*p) == 10 ) goto st10; goto st9; } - _out1: cs = 1; goto _out; - _out0: cs = 0; goto _out; - _out2: cs = 2; goto _out; - _out3: cs = 3; goto _out; - _out4: cs = 4; goto _out; - _out5: cs = 5; goto _out; - _out10: cs = 10; goto _out; - _out6: cs = 6; goto _out; - _out7: cs = 7; goto _out; - _out8: cs = 8; goto _out; - _out9: cs = 9; goto _out; - + _test_eof1: cs = 1; goto _test_eof; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + + _test_eof: {} _out: {} } -#line 573 "parser.rl" +#line 570 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; } else { - rb_raise(eParserError, "unexpected token at '%s'", p); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); } } @@ -1706,7 +1708,7 @@ static void JSON_mark(JSON_Parser *json) static void JSON_free(JSON_Parser *json) { - ruby_xfree(json); + free(json); } static VALUE cJSON_parser_s_allocate(VALUE klass) @@ -1729,6 +1731,7 @@ static VALUE cParser_source(VALUE self) void Init_parser() { + 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); diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index c25b1278f7..c1f932d4b7 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -1,9 +1,4 @@ -/* vim: set cin et sw=4 ts=4: */ - #include "ruby.h" -#include "ruby/re.h" -#include "ruby/st.h" -#include "ruby/encoding.h" #include "unicode.h" #define EVIL 0x666 @@ -76,7 +71,7 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul VALUE v = Qnil; char *np = JSON_parse_value(json, fpc, pe, &v); if (np == NULL) { - fbreak; + fhold; fbreak; } else { rb_hash_aset(*result, last_name, v); fexec np; @@ -85,10 +80,10 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul action parse_name { char *np = JSON_parse_string(json, fpc, pe, &last_name); - if (np == NULL) fbreak; else fexec np; + if (np == NULL) { fhold; fbreak; } else fexec np; } - action exit { fbreak; } + action exit { fhold; fbreak; } a_pair = ignore* begin_name >parse_name ignore* name_separator ignore* @@ -148,19 +143,19 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu if (json->allow_nan) { *result = CNaN; } else { - rb_raise(eParserError, "unexpected token at '%s'", p - 2); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2); } } action parse_infinity { if (json->allow_nan) { *result = CInfinity; } else { - rb_raise(eParserError, "unexpected token at '%s'", p - 8); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8); } } action parse_string { char *np = JSON_parse_string(json, fpc, pe, result); - if (np == NULL) fbreak; else fexec np; + if (np == NULL) { fhold; fbreak; } else fexec np; } action parse_number { @@ -169,35 +164,35 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu if (json->allow_nan) { *result = CMinusInfinity; fexec p + 10; - fbreak; + fhold; fbreak; } else { - rb_raise(eParserError, "unexpected token at '%s'", p); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); } } np = JSON_parse_float(json, fpc, pe, result); if (np != NULL) fexec np; np = JSON_parse_integer(json, fpc, pe, result); if (np != NULL) fexec np; - fbreak; + fhold; fbreak; } action parse_array { char *np; - json->current_nesting += 1; + json->current_nesting++; np = JSON_parse_array(json, fpc, pe, result); - json->current_nesting -= 1; - if (np == NULL) fbreak; else fexec np; + json->current_nesting--; + if (np == NULL) { fhold; fbreak; } else fexec np; } action parse_object { char *np; - json->current_nesting += 1; + json->current_nesting++; np = JSON_parse_object(json, fpc, pe, result); - json->current_nesting -= 1; - if (np == NULL) fbreak; else fexec np; + json->current_nesting--; + if (np == NULL) { fhold; fbreak; } else fexec np; } - action exit { fbreak; } + action exit { fhold; fbreak; } main := ( Vnull @parse_null | @@ -231,7 +226,7 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul write data; - action exit { fbreak; } + action exit { fhold; fbreak; } main := '-'? ('0' | [1-9][0-9]*) (^[0-9] @exit); }%% @@ -259,7 +254,7 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res write data; - action exit { fbreak; } + action exit { fhold; fbreak; } main := '-'? ( (('0' | [1-9][0-9]*) '.' [0-9]+ ([Ee] [+\-]?[0-9]+)?) @@ -295,14 +290,14 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul VALUE v = Qnil; char *np = JSON_parse_value(json, fpc, pe, &v); if (np == NULL) { - fbreak; + fhold; fbreak; } else { rb_ary_push(*result, v); fexec np; } } - action exit { fbreak; } + action exit { fhold; fbreak; } next_element = value_separator ignore* begin_value >parse_value; @@ -327,13 +322,13 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul if(cs >= JSON_array_first_final) { return p + 1; } else { - rb_raise(eParserError, "unexpected token at '%s'", p); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); } } static VALUE json_string_unescape(char *p, char *pe) { - VALUE result = rb_enc_str_new(0, pe - p + 1, rb_utf8_encoding()); + VALUE result = rb_str_buf_new(pe - p + 1); while (p < pe) { if (*p == '\\') { @@ -395,10 +390,10 @@ static VALUE json_string_unescape(char *p, char *pe) action parse_string { *result = json_string_unescape(json->memo + 1, p); - if (NIL_P(*result)) fbreak; else fexec p + 1; + if (NIL_P(*result)) { fhold; fbreak; } else fexec p + 1; } - action exit { fbreak; } + action exit { fhold; fbreak; } main := '"' ((^(["\\] | 0..0x1f) | '\\'["\\/bfnrt] | '\\u'[0-9a-fA-F]{4} | '\\'^(["\\/bfnrtu]|0..0x1f))* %parse_string) '"' @exit; }%% @@ -407,7 +402,7 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu { int cs = EVIL; - *result = rb_enc_str_new("", 0, rb_utf8_encoding()); + *result = rb_str_new("", 0); %% write init; json->memo = p; %% write exec; @@ -431,14 +426,14 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu char *np; json->current_nesting = 1; np = JSON_parse_object(json, fpc, pe, &result); - if (np == NULL) fbreak; else fexec np; + if (np == NULL) { fhold; fbreak; } else fexec np; } action parse_array { char *np; json->current_nesting = 1; np = JSON_parse_array(json, fpc, pe, &result); - if (np == NULL) fbreak; else fexec np; + if (np == NULL) { fhold; fbreak; } else fexec np; } main := ignore* ( @@ -574,7 +569,7 @@ static VALUE cParser_parse(VALUE self) if (cs >= JSON_first_final && p == pe) { return result; } else { - rb_raise(eParserError, "unexpected token at '%s'", p); + rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); } } @@ -593,7 +588,7 @@ static void JSON_mark(JSON_Parser *json) static void JSON_free(JSON_Parser *json) { - ruby_xfree(json); + free(json); } static VALUE cJSON_parser_s_allocate(VALUE klass) @@ -616,6 +611,7 @@ static VALUE cParser_source(VALUE self) void Init_parser() { + 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); diff --git a/ext/json/ext/parser/unicode.c b/ext/json/ext/parser/unicode.c index f196727354..a16ff262d9 100644 --- a/ext/json/ext/parser/unicode.c +++ b/ext/json/ext/parser/unicode.c @@ -1,5 +1,3 @@ -/* vim: set cin et sw=4 ts=4: */ - #include "unicode.h" /* @@ -105,12 +103,12 @@ char *JSON_convert_UTF16_to_UTF8 ( + (ch2 - UNI_SUR_LOW_START) + halfBase; ++tmpPtr; } else if (flags == strictConversion) { /* it's an unpaired high surrogate */ - ruby_xfree(tmp); + free(tmp); rb_raise(rb_path2class("JSON::ParserError"), "source sequence is illegal/malformed near %s", source); } } else { /* We don't have the 16 bits following the high surrogate. */ - ruby_xfree(tmp); + free(tmp); rb_raise(rb_path2class("JSON::ParserError"), "partial character in source, but hit end near %s", source); break; @@ -118,7 +116,7 @@ char *JSON_convert_UTF16_to_UTF8 ( } else if (flags == strictConversion) { /* UTF-16 surrogate values are illegal in UTF-32 */ if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) { - ruby_xfree(tmp); + free(tmp); rb_raise(rb_path2class("JSON::ParserError"), "source sequence is illegal/malformed near %s", source); } @@ -150,7 +148,7 @@ char *JSON_convert_UTF16_to_UTF8 ( } rb_str_buf_cat(buffer, p, bytesToWrite); } - ruby_xfree(tmp); + free(tmp); source += 5 + (n - 1) * 6; return source; } diff --git a/ext/json/lib/json/add/core.rb b/ext/json/lib/json/add/core.rb index 630ed6e4b7..4423e7ad75 100644 --- a/ext/json/lib/json/add/core.rb +++ b/ext/json/lib/json/add/core.rb @@ -9,14 +9,21 @@ require 'date' class Time def self.json_create(object) - at(*object.values_at('s', 'u')) + if usec = object.delete('u') # used to be tv_usec -> tv_nsec + object['n'] = usec * 1000 + end + if respond_to?(:tv_nsec) + at(*object.values_at('s', 'n')) + else + at(object['s'], object['n'] / 1000) + end end def to_json(*args) { - 'json_class' => self.class.name.to_s, + 'json_class' => self.class.name, 's' => tv_sec, - 'u' => tv_usec, + 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000 }.to_json(*args) end end @@ -26,13 +33,15 @@ class Date civil(*object.values_at('y', 'm', 'd', 'sg')) end + alias start sg unless method_defined?(:start) + def to_json(*args) { - 'json_class' => self.class.name.to_s, + 'json_class' => self.class.name, 'y' => year, 'm' => month, 'd' => day, - 'sg' => @sg, + 'sg' => start, }.to_json(*args) end end @@ -41,14 +50,20 @@ class DateTime def self.json_create(object) args = object.values_at('y', 'm', 'd', 'H', 'M', 'S') of_a, of_b = object['of'].split('/') - args << Rational(of_a.to_i, of_b.to_i) + if of_b and of_b != '0' + args << Rational(of_a.to_i, of_b.to_i) + else + args << of_a + end args << object['sg'] civil(*args) end + alias start sg unless method_defined?(:start) + def to_json(*args) { - 'json_class' => self.class.name.to_s, + 'json_class' => self.class.name, 'y' => year, 'm' => month, 'd' => day, @@ -56,7 +71,7 @@ class DateTime 'M' => min, 'S' => sec, 'of' => offset.to_s, - 'sg' => @sg, + 'sg' => start, }.to_json(*args) end end @@ -68,7 +83,7 @@ class Range def to_json(*args) { - 'json_class' => self.class.name.to_s, + 'json_class' => self.class.name, 'a' => [ first, last, exclude_end? ] }.to_json(*args) end @@ -80,8 +95,8 @@ class Struct end def to_json(*args) - klass = self.class.name.to_s - klass.empty? and raise JSON::JSONError, "Only named structs are supported!" + klass = self.class.name + klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!" { 'json_class' => klass, 'v' => values, @@ -98,7 +113,7 @@ class Exception def to_json(*args) { - 'json_class' => self.class.name.to_s, + 'json_class' => self.class.name, 'm' => message, 'b' => backtrace, }.to_json(*args) @@ -112,7 +127,7 @@ class Regexp def to_json(*) { - 'json_class' => self.class.name.to_s, + 'json_class' => self.class.name, 'o' => options, 's' => source, }.to_json diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb index 7bc5ae0656..499fcc0dae 100644 --- a/ext/json/lib/json/common.rb +++ b/ext/json/lib/json/common.rb @@ -2,7 +2,7 @@ require 'json/version' module JSON class << self - # If _object_ is string like parse the string and return the parsed result + # If _object_ is string-like parse the string and return the parsed result # as a Ruby data structure. Otherwise generate a JSON text from the Ruby # data structure object and return it. # @@ -184,7 +184,8 @@ module JSON end # :stopdoc: - # I want to deprecate these later, so I'll first be silent about them, and later delete them. + # I want to deprecate these later, so I'll first be silent about them, and + # later delete them. alias unparse generate module_function :unparse # :startdoc: @@ -238,7 +239,7 @@ module JSON # :startdoc: # Load a ruby data structure from a JSON _source_ and return it. A source can - # either be a string like object, an IO like object, or an object responding + # either be a string-like object, an IO like object, or an object responding # to the read method. If _proc_ was given, it will be called with any nested # Ruby object as an argument recursively in depth first order. # @@ -327,7 +328,7 @@ module ::Kernel nil end - # If _object_ is string like parse the string and return the parsed result as + # If _object_ is string-like parse the string and return the parsed result as # a Ruby data structure. Otherwise generate a JSON text from the Ruby data # structure object and return it. # @@ -351,4 +352,3 @@ class ::Class respond_to?(:json_create) end end - # vim: set et sw=2 ts=2: diff --git a/ext/json/lib/json/editor.rb b/ext/json/lib/json/editor.rb index 9a65400622..12a7f94591 100644 --- a/ext/json/lib/json/editor.rb +++ b/ext/json/lib/json/editor.rb @@ -1360,4 +1360,3 @@ module JSON end end end - # vim: set et sw=2 ts=2: diff --git a/ext/json/lib/json/pure/parser.rb b/ext/json/lib/json/pure/parser.rb index 39bee54269..9b30f15c07 100644 --- a/ext/json/lib/json/pure/parser.rb +++ b/ext/json/lib/json/pure/parser.rb @@ -122,9 +122,8 @@ module JSON def parse_string if scan(STRING) return '' if self[1].empty? - self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do - c = $& - if u = UNESCAPE_MAP[c[1]] + self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c| + if u = UNESCAPE_MAP[$&[1]] u else # \uXXXX bytes = '' diff --git a/ext/json/lib/json/version.rb b/ext/json/lib/json/version.rb index 3d674ac44f..acf8217048 100644 --- a/ext/json/lib/json/version.rb +++ b/ext/json/lib/json/version.rb @@ -1,6 +1,6 @@ module JSON # JSON version - VERSION = '1.1.2' + VERSION = '1.1.3' VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc: VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc: VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: diff --git a/test/json/fixtures/fail18.json b/test/json/fixtures/fail18.json index d61a345465..e2d130c6eb 100644 --- a/test/json/fixtures/fail18.json +++ b/test/json/fixtures/fail18.json @@ -1 +1 @@ -[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] // No, we don't limit our depth: Moved to pass... +[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] diff --git a/test/json/runner.rb b/test/json/runner.rb index 91bc12a594..9ad04485bf 100755 --- a/test/json/runner.rb +++ b/test/json/runner.rb @@ -23,4 +23,3 @@ class TS_AllTests end end Test::Unit::UI::Console::TestRunner.run(TS_AllTests) - # vim: set et sw=2 ts=2: diff --git a/test/json/test_json.rb b/test/json/test_json.rb index 257b8e0f63..0ee993eb88 100755 --- a/test/json/test_json.rb +++ b/test/json/test_json.rb @@ -3,6 +3,7 @@ require 'test/unit' require 'json' +require 'stringio' class TC_JSON < Test::Unit::TestCase include JSON @@ -61,6 +62,12 @@ class TC_JSON < Test::Unit::TestCase assert_equal_float [3.141], parse('[3141.0E-3]') assert_equal_float [-3.141], parse('[-3141.0e-3]') assert_equal_float [-3.141], parse('[-3141e-3]') + assert_raises(ParserError) { parse('[NaN]') } + assert parse('[NaN]', :allow_nan => true).first.nan? + assert_raises(ParserError) { parse('[Infinity]') } + assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true) + assert_raises(ParserError) { parse('[-Infinity]') } + assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true) assert_equal([""], parse('[""]')) assert_equal(["foobar"], parse('["foobar"]')) assert_equal([{}], parse('[{}]')) @@ -238,18 +245,49 @@ EOT end def test_nesting - to_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]' - assert_raises(JSON::NestingError) { JSON.parse to_deep } - assert_raises(JSON::NestingError) { JSON.parser.new(to_deep).parse } - assert_raises(JSON::NestingError) { JSON.parse to_deep, :max_nesting => 19 } - ok = JSON.parse to_deep, :max_nesting => 20 - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => nil - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => false - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => 0 - assert_kind_of Array, ok + assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 } + assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse } + assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2) + too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]' + too_deep_ary = eval too_deep + assert_raises(JSON::NestingError) { JSON.parse too_deep } + assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse } + assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 } + ok = JSON.parse too_deep, :max_nesting => 20 + assert_equal too_deep_ary, ok + ok = JSON.parse too_deep, :max_nesting => nil + assert_equal too_deep_ary, ok + ok = JSON.parse too_deep, :max_nesting => false + assert_equal too_deep_ary, ok + ok = JSON.parse too_deep, :max_nesting => 0 + assert_equal too_deep_ary, ok + assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 } + assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2) + assert_raises(JSON::NestingError) { JSON.generate too_deep_ary } + assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 } + ok = JSON.generate too_deep_ary, :max_nesting => 20 + assert_equal too_deep, ok + ok = JSON.generate too_deep_ary, :max_nesting => nil + assert_equal too_deep, ok + ok = JSON.generate too_deep_ary, :max_nesting => false + assert_equal too_deep, ok + ok = JSON.generate too_deep_ary, :max_nesting => 0 + assert_equal too_deep, ok + end + + def test_load_dump + too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]' + assert_equal too_deep, JSON.dump(eval(too_deep)) + assert_kind_of String, Marshal.dump(eval(too_deep)) + assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 19) } + assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 19) } + assert_equal too_deep, JSON.dump(eval(too_deep), 20) + assert_kind_of String, Marshal.dump(eval(too_deep), 20) + output = StringIO.new + JSON.dump(eval(too_deep), output) + assert_equal too_deep, output.string + output = StringIO.new + JSON.dump(eval(too_deep), output, 20) + assert_equal too_deep, output.string end end - # vim: set et sw=2 ts=2: diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb index 04c690ce31..cf8a92ae10 100755 --- a/test/json/test_json_addition.rb +++ b/test/json/test_json_addition.rb @@ -31,6 +31,10 @@ class TC_JSONAddition < Test::Unit::TestCase end class B + def self.json_creatable? + false + end + def to_json(*args) { 'json_class' => self.class.name, @@ -50,9 +54,6 @@ class TC_JSONAddition < Test::Unit::TestCase end end - def setup - end - def test_extended_json a = A.new(666) assert A.json_creatable? @@ -77,14 +78,14 @@ class TC_JSONAddition < Test::Unit::TestCase ) end - def test_extended_json_fail + def test_extended_json_fail1 b = B.new assert !B.json_creatable? json = generate(b) - assert_equal({ 'json_class' => B.name }, JSON.parse(json)) + assert_equal({ "json_class"=>"TC_JSONAddition::B" }, JSON.parse(json)) end - def test_extended_json_fail + def test_extended_json_fail2 c = C.new assert !C.json_creatable? json = generate(c) @@ -111,9 +112,11 @@ EOT assert_equal raw, raw_again end + MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar + def test_core - t = Time.at(1198254128, 895990) - assert_equal t, JSON(JSON(t)) + t = Time.now + assert_equal t.inspect, JSON(JSON(t)).inspect d = Date.today assert_equal d, JSON(JSON(d)) d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161) @@ -122,8 +125,7 @@ EOT assert_equal 1...10, JSON(JSON(1...10)) assert_equal "a".."c", JSON(JSON("a".."c")) assert_equal "a"..."c", JSON(JSON("a"..."c")) - struct = Struct.new 'MyJsonStruct', :foo, :bar - s = struct.new 4711, 'foot' + s = MyJsonStruct.new 4711, 'foot' assert_equal s, JSON(JSON(s)) struct = Struct.new :foo, :bar s = struct.new 4711, 'foot' @@ -137,7 +139,19 @@ EOT assert_equal e.message, e_again.message assert_equal e.backtrace, e_again.backtrace end - assert_equal /foo/, JSON(JSON(/foo/)) - assert_equal /foo/i, JSON(JSON(/foo/i)) + assert_equal(/foo/, JSON(JSON(/foo/))) + assert_equal(/foo/i, JSON(JSON(/foo/i))) + end + + def test_utc_datetime + now = Time.now + d = DateTime.parse(now.to_s) # usual case + assert d, JSON.parse(d.to_json) + d = DateTime.parse(now.utc.to_s) # of = 0 + assert d, JSON.parse(d.to_json) + d = DateTime.civil(2008, 6, 17, 11, 48, 32, 1) # of = 1 / 12 => 1/12 + assert d, JSON.parse(d.to_json) + d = DateTime.civil(2008, 6, 17, 11, 48, 32, 12) # of = 12 / 12 => 12 + assert d, JSON.parse(d.to_json) end end diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb index e720b2d862..b7e0bc22c6 100644 --- a/test/json/test_json_generate.rb +++ b/test/json/test_json_generate.rb @@ -70,6 +70,7 @@ EOT #assert s.check_circular h = { 1=>2 } h[3] = h + assert_raises(JSON::CircularDatastructure) { generate(h) } assert_raises(JSON::CircularDatastructure) { generate(h, s) } s = JSON.state.new(:check_circular => true) #assert s.check_circular @@ -77,4 +78,22 @@ EOT a << a assert_raises(JSON::CircularDatastructure) { generate(a, s) } end + + def test_allow_nan + assert_raises(GeneratorError) { generate([JSON::NaN]) } + assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true) + assert_equal '[NaN]', fast_generate([JSON::NaN]) + assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) } + assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true) + assert_raises(GeneratorError) { generate([JSON::Infinity]) } + assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true) + assert_equal '[Infinity]', fast_generate([JSON::Infinity]) + assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) } + assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true) + assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) } + assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true) + assert_equal '[-Infinity]', fast_generate([JSON::MinusInfinity]) + assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) } + assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true) + end end diff --git a/test/json/test_json_rails.rb b/test/json/test_json_rails.rb index e44ea606e3..c0447ddaba 100644 --- a/test/json/test_json_rails.rb +++ b/test/json/test_json_rails.rb @@ -31,6 +31,10 @@ class TC_JSONRails < Test::Unit::TestCase end class B + def self.json_creatable? + false + end + def to_json(*args) { 'json_class' => self.class.name, @@ -46,9 +50,6 @@ class TC_JSONRails < Test::Unit::TestCase end end - def setup - end - def test_extended_json a = A.new(666) assert A.json_creatable? @@ -73,14 +74,14 @@ class TC_JSONRails < Test::Unit::TestCase ) end - def test_extended_json_fail + def test_extended_json_fail1 b = B.new assert !B.json_creatable? json = generate(b) assert_equal({ 'json_class' => B.name }, JSON.parse(json)) end - def test_extended_json_fail + def test_extended_json_fail2 c = C.new # with rails addition all objects are theoretically creatable assert C.json_creatable? json = generate(c) diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb index cad93846b5..862c6ea1e0 100755 --- a/test/json/test_json_unicode.rb +++ b/test/json/test_json_unicode.rb @@ -7,9 +7,6 @@ require 'json' class TC_JSONUnicode < Test::Unit::TestCase include JSON - def setup - end - def test_unicode assert_equal '""', ''.to_json assert_equal '"\\b"', "\b".to_json @@ -54,7 +51,7 @@ class TC_JSONUnicode < Test::Unit::TestCase end end assert_raises(JSON::GeneratorError) do - JSON.generate(["" << 0x80]) + JSON.generate(["\x80"]) end assert_equal "\302\200", JSON.parse('["\u0080"]').first end diff --git a/version.h b/version.h index 6b17e4338a..cc88cf8780 100644 --- a/version.h +++ b/version.h @@ -1,7 +1,7 @@ #define RUBY_VERSION "1.9.0" -#define RUBY_RELEASE_DATE "2008-09-20" +#define RUBY_RELEASE_DATE "2008-09-21" #define RUBY_VERSION_CODE 190 -#define RUBY_RELEASE_CODE 20080920 +#define RUBY_RELEASE_CODE 20080921 #define RUBY_PATCHLEVEL 0 #define RUBY_VERSION_MAJOR 1 @@ -9,7 +9,7 @@ #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_YEAR 2008 #define RUBY_RELEASE_MONTH 9 -#define RUBY_RELEASE_DAY 20 +#define RUBY_RELEASE_DAY 21 #ifdef RUBY_EXTERN RUBY_EXTERN const char ruby_version[]; -- cgit v1.2.3