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.c254
1 files changed, 137 insertions, 117 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index 5b2e61c..71292e4 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -3,6 +3,28 @@
#include "../fbuffer/fbuffer.h"
#include "parser.h"
+#if defined HAVE_RUBY_ENCODING_H
+# define EXC_ENCODING rb_utf8_encoding(),
+# ifndef HAVE_RB_ENC_RAISE
+static void
+enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
+{
+ va_list args;
+ VALUE mesg;
+
+ va_start(args, fmt);
+ mesg = rb_enc_vsprintf(enc, fmt, args);
+ va_end(args);
+
+ rb_exc_raise(rb_exc_new3(exc, mesg));
+}
+# define rb_enc_raise enc_raise
+# endif
+#else
+# define EXC_ENCODING /* nothing */
+# define rb_enc_raise rb_raise
+#endif
+
/* unicode */
static const char digit_values[256] = {
@@ -70,8 +92,6 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
#ifdef HAVE_RUBY_ENCODING_H
static VALUE CEncoding_ASCII_8BIT, CEncoding_UTF_8;
static ID i_encoding, i_encode;
-#else
-static ID i_iconv;
#endif
static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
@@ -83,19 +103,19 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_match_string, i_aset, i_aref, i_leftshift;
-#line 109 "parser.rl"
+#line 129 "parser.rl"
-#line 91 "parser.c"
-static const int JSON_object_start = 1;
-static const int JSON_object_first_final = 27;
-static const int JSON_object_error = 0;
+#line 111 "parser.c"
+enum {JSON_object_start = 1};
+enum {JSON_object_first_final = 27};
+enum {JSON_object_error = 0};
-static const int JSON_object_en_main = 1;
+enum {JSON_object_en_main = 1};
-#line 150 "parser.rl"
+#line 170 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -111,14 +131,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 115 "parser.c"
+#line 135 "parser.c"
{
cs = JSON_object_start;
}
-#line 165 "parser.rl"
+#line 185 "parser.rl"
-#line 122 "parser.c"
+#line 142 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -146,7 +166,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 132 "parser.rl"
+#line 152 "parser.rl"
{
char *np;
json->parsing_name = 1;
@@ -159,7 +179,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 163 "parser.c"
+#line 183 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -226,7 +246,7 @@ case 8:
goto st8;
goto st0;
tr11:
-#line 117 "parser.rl"
+#line 137 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -246,7 +266,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 250 "parser.c"
+#line 270 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -335,14 +355,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 140 "parser.rl"
+#line 160 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 346 "parser.c"
+#line 366 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -440,7 +460,7 @@ case 26:
_out: {}
}
-#line 166 "parser.rl"
+#line 186 "parser.rl"
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
@@ -465,15 +485,15 @@ case 26:
-#line 469 "parser.c"
-static const int JSON_value_start = 1;
-static const int JSON_value_first_final = 29;
-static const int JSON_value_error = 0;
+#line 489 "parser.c"
+enum {JSON_value_start = 1};
+enum {JSON_value_first_final = 29};
+enum {JSON_value_error = 0};
-static const int JSON_value_en_main = 1;
+enum {JSON_value_en_main = 1};
-#line 270 "parser.rl"
+#line 290 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -481,14 +501,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 485 "parser.c"
+#line 505 "parser.c"
{
cs = JSON_value_start;
}
-#line 277 "parser.rl"
+#line 297 "parser.rl"
-#line 492 "parser.c"
+#line 512 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -522,14 +542,14 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 218 "parser.rl"
+#line 238 "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 223 "parser.rl"
+#line 243 "parser.rl"
{
char *np;
if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
@@ -538,7 +558,7 @@ tr3:
{p = (( p + 10))-1;}
p--; {p++; cs = 29; goto _out;}
} else {
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
}
}
np = JSON_parse_float(json, p, pe, result);
@@ -549,7 +569,7 @@ tr3:
}
goto st29;
tr7:
-#line 241 "parser.rl"
+#line 261 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -559,7 +579,7 @@ tr7:
}
goto st29;
tr11:
-#line 249 "parser.rl"
+#line 269 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -569,39 +589,39 @@ tr11:
}
goto st29;
tr25:
-#line 211 "parser.rl"
+#line 231 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
} else {
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
}
}
goto st29;
tr27:
-#line 204 "parser.rl"
+#line 224 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
} else {
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
}
}
goto st29;
tr31:
-#line 198 "parser.rl"
+#line 218 "parser.rl"
{
*result = Qfalse;
}
goto st29;
tr34:
-#line 195 "parser.rl"
+#line 215 "parser.rl"
{
*result = Qnil;
}
goto st29;
tr37:
-#line 201 "parser.rl"
+#line 221 "parser.rl"
{
*result = Qtrue;
}
@@ -610,9 +630,9 @@ st29:
if ( ++p == pe )
goto _test_eof29;
case 29:
-#line 257 "parser.rl"
+#line 277 "parser.rl"
{ p--; {p++; cs = 29; goto _out;} }
-#line 616 "parser.c"
+#line 636 "parser.c"
switch( (*p) ) {
case 13: goto st29;
case 32: goto st29;
@@ -853,7 +873,7 @@ case 28:
_out: {}
}
-#line 278 "parser.rl"
+#line 298 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -863,15 +883,15 @@ case 28:
}
-#line 867 "parser.c"
-static const int JSON_integer_start = 1;
-static const int JSON_integer_first_final = 3;
-static const int JSON_integer_error = 0;
+#line 887 "parser.c"
+enum {JSON_integer_start = 1};
+enum {JSON_integer_first_final = 3};
+enum {JSON_integer_error = 0};
-static const int JSON_integer_en_main = 1;
+enum {JSON_integer_en_main = 1};
-#line 294 "parser.rl"
+#line 314 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -879,15 +899,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 883 "parser.c"
+#line 903 "parser.c"
{
cs = JSON_integer_start;
}
-#line 301 "parser.rl"
+#line 321 "parser.rl"
json->memo = p;
-#line 891 "parser.c"
+#line 911 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -921,14 +941,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 291 "parser.rl"
+#line 311 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 932 "parser.c"
+#line 952 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -947,7 +967,7 @@ case 5:
_out: {}
}
-#line 303 "parser.rl"
+#line 323 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -962,15 +982,15 @@ case 5:
}
-#line 966 "parser.c"
-static const int JSON_float_start = 1;
-static const int JSON_float_first_final = 8;
-static const int JSON_float_error = 0;
+#line 986 "parser.c"
+enum {JSON_float_start = 1};
+enum {JSON_float_first_final = 8};
+enum {JSON_float_error = 0};
-static const int JSON_float_en_main = 1;
+enum {JSON_float_en_main = 1};
-#line 328 "parser.rl"
+#line 348 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -978,15 +998,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 982 "parser.c"
+#line 1002 "parser.c"
{
cs = JSON_float_start;
}
-#line 335 "parser.rl"
+#line 355 "parser.rl"
json->memo = p;
-#line 990 "parser.c"
+#line 1010 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1044,14 +1064,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 322 "parser.rl"
+#line 342 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 1055 "parser.c"
+#line 1075 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1112,7 +1132,7 @@ case 7:
_out: {}
}
-#line 337 "parser.rl"
+#line 357 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -1128,15 +1148,15 @@ case 7:
-#line 1132 "parser.c"
-static const int JSON_array_start = 1;
-static const int JSON_array_first_final = 17;
-static const int JSON_array_error = 0;
+#line 1152 "parser.c"
+enum {JSON_array_start = 1};
+enum {JSON_array_first_final = 17};
+enum {JSON_array_error = 0};
-static const int JSON_array_en_main = 1;
+enum {JSON_array_en_main = 1};
-#line 380 "parser.rl"
+#line 400 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -1150,14 +1170,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 1154 "parser.c"
+#line 1174 "parser.c"
{
cs = JSON_array_start;
}
-#line 393 "parser.rl"
+#line 413 "parser.rl"
-#line 1161 "parser.c"
+#line 1181 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1196,7 +1216,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 357 "parser.rl"
+#line 377 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -1216,7 +1236,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1220 "parser.c"
+#line 1240 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1316,14 +1336,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 372 "parser.rl"
+#line 392 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1327 "parser.c"
+#line 1347 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1379,12 +1399,12 @@ case 16:
_out: {}
}
-#line 394 "parser.rl"
+#line 414 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
} else {
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
return NULL;
}
}
@@ -1460,15 +1480,15 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1464 "parser.c"
-static const int JSON_string_start = 1;
-static const int JSON_string_first_final = 8;
-static const int JSON_string_error = 0;
+#line 1484 "parser.c"
+enum {JSON_string_start = 1};
+enum {JSON_string_first_final = 8};
+enum {JSON_string_error = 0};
-static const int JSON_string_en_main = 1;
+enum {JSON_string_en_main = 1};
-#line 493 "parser.rl"
+#line 513 "parser.rl"
static int
@@ -1490,15 +1510,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1494 "parser.c"
+#line 1514 "parser.c"
{
cs = JSON_string_start;
}
-#line 514 "parser.rl"
+#line 534 "parser.rl"
json->memo = p;
-#line 1502 "parser.c"
+#line 1522 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1523,7 +1543,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 479 "parser.rl"
+#line 499 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1534,14 +1554,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 490 "parser.rl"
+#line 510 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1545 "parser.c"
+#line 1565 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1617,7 +1637,7 @@ case 7:
_out: {}
}
-#line 516 "parser.rl"
+#line 536 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1655,12 +1675,7 @@ case 7:
static VALUE convert_encoding(VALUE source)
{
#ifdef HAVE_RUBY_ENCODING_H
- VALUE encoding = rb_funcall(source, i_encoding, 0);
- if (encoding == CEncoding_ASCII_8BIT) {
- FORCE_UTF8(source);
- } else {
- source = rb_funcall(source, i_encode, 1, CEncoding_UTF_8);
- }
+ source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
#endif
return source;
}
@@ -1700,12 +1715,18 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
if (json->Vsource) {
rb_raise(rb_eTypeError, "already initialized instance");
}
+#ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
+ rb_scan_args(argc, argv, "1:", &source, &opts);
+#else
rb_scan_args(argc, argv, "11", &source, &opts);
+#endif
if (!NIL_P(opts)) {
+#ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
if (NIL_P(opts)) {
rb_raise(rb_eArgError, "opts needs to be like a hash");
} else {
+#endif
VALUE tmp = ID2SYM(i_max_nesting);
if (option_given_p(opts, tmp)) {
VALUE max_nesting = rb_hash_aref(opts, tmp);
@@ -1766,7 +1787,9 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->match_string = Qnil;
}
+#ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
}
+#endif
} else {
json->max_nesting = 100;
json->allow_nan = 0;
@@ -1775,7 +1798,6 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
json->object_class = Qnil;
json->array_class = Qnil;
}
- source = rb_convert_type(source, T_STRING, "String", "to_str");
source = convert_encoding(StringValue(source));
json->current_nesting = 0;
StringValue(source);
@@ -1786,15 +1808,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1790 "parser.c"
-static const int JSON_start = 1;
-static const int JSON_first_final = 10;
-static const int JSON_error = 0;
+#line 1812 "parser.c"
+enum {JSON_start = 1};
+enum {JSON_first_final = 10};
+enum {JSON_error = 0};
-static const int JSON_en_main = 1;
+enum {JSON_en_main = 1};
-#line 698 "parser.rl"
+#line 720 "parser.rl"
/*
@@ -1811,16 +1833,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
-#line 1815 "parser.c"
+#line 1837 "parser.c"
{
cs = JSON_start;
}
-#line 714 "parser.rl"
+#line 736 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1824 "parser.c"
+#line 1846 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1854,7 +1876,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 690 "parser.rl"
+#line 712 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1864,7 +1886,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1868 "parser.c"
+#line 1890 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1953,12 +1975,12 @@ case 9:
_out: {}
}
-#line 717 "parser.rl"
+#line 739 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
} else {
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
return Qnil;
}
}
@@ -2056,8 +2078,6 @@ void Init_parser(void)
CEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit"));
i_encoding = rb_intern("encoding");
i_encode = rb_intern("encode");
-#else
- i_iconv = rb_intern("iconv");
#endif
}