summaryrefslogtreecommitdiff
path: root/ext/json/parser/parser.c
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2024-10-18 13:45:29 +0200
committerHiroshi SHIBATA <[email protected]>2024-10-26 18:44:15 +0900
commit7dfc1f3d66d6041631da4a94767dfa33ff290945 (patch)
tree93b55ff4675e6888f94181b312417b39fdb0a1e0 /ext/json/parser/parser.c
parent618085f48d953197ed536386011c09baf21d22bc (diff)
[ruby/json] parser.c: refactor raise_parse_error
https://github.com/ruby/json/commit/09e1df2643
Diffstat (limited to 'ext/json/parser/parser.c')
-rw-r--r--ext/json/parser/parser.c200
1 files changed, 100 insertions, 100 deletions
diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c
index ed86817f26..5348d6cc0b 100644
--- a/ext/json/parser/parser.c
+++ b/ext/json/parser/parser.c
@@ -69,7 +69,15 @@ static int convert_UTF32_to_UTF8(char *buf, uint32_t ch)
return len;
}
-static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
+#ifdef RBIMPL_ATTR_NORETURN
+RBIMPL_ATTR_NORETURN()
+#endif
+static void raise_parse_error(const char *format, const char *start)
+{
+ rb_enc_raise(rb_utf8_encoding(), rb_path2class("JSON::ParserError"), format, start);
+}
+
+static VALUE mJSON, mExt, cParser, eNestingError;
static VALUE CNaN, CInfinity, CMinusInfinity;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
@@ -83,11 +91,11 @@ static int utf8_encindex;
-#line 109 "parser.rl"
+#line 117 "parser.rl"
-#line 91 "parser.c"
+#line 99 "parser.c"
enum {JSON_object_start = 1};
enum {JSON_object_first_final = 27};
enum {JSON_object_error = 0};
@@ -95,7 +103,7 @@ enum {JSON_object_error = 0};
enum {JSON_object_en_main = 1};
-#line 151 "parser.rl"
+#line 159 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -111,14 +119,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 123 "parser.c"
{
cs = JSON_object_start;
}
-#line 166 "parser.rl"
+#line 174 "parser.rl"
-#line 122 "parser.c"
+#line 130 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -146,7 +154,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 133 "parser.rl"
+#line 141 "parser.rl"
{
char *np;
json->parsing_name = 1;
@@ -159,7 +167,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 163 "parser.c"
+#line 171 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -226,7 +234,7 @@ case 8:
goto st8;
goto st0;
tr11:
-#line 117 "parser.rl"
+#line 125 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@@ -247,7 +255,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 251 "parser.c"
+#line 259 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -336,14 +344,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 141 "parser.rl"
+#line 149 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 347 "parser.c"
+#line 355 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -441,7 +449,7 @@ case 26:
_out: {}
}
-#line 167 "parser.rl"
+#line 175 "parser.rl"
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
@@ -466,7 +474,7 @@ case 26:
-#line 470 "parser.c"
+#line 478 "parser.c"
enum {JSON_value_start = 1};
enum {JSON_value_first_final = 29};
enum {JSON_value_error = 0};
@@ -474,7 +482,7 @@ enum {JSON_value_error = 0};
enum {JSON_value_en_main = 1};
-#line 267 "parser.rl"
+#line 275 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -482,14 +490,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 486 "parser.c"
+#line 494 "parser.c"
{
cs = JSON_value_start;
}
-#line 274 "parser.rl"
+#line 282 "parser.rl"
-#line 493 "parser.c"
+#line 501 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -523,14 +531,14 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 219 "parser.rl"
+#line 227 "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 224 "parser.rl"
+#line 232 "parser.rl"
{
char *np;
if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
@@ -539,7 +547,7 @@ tr3:
{p = (( p + 10))-1;}
p--; {p++; cs = 29; goto _out;}
} else {
- rb_enc_raise(rb_utf8_encoding(), eParserError, "unexpected token at '%s'", p);
+ raise_parse_error("unexpected token at '%s'", p);
}
}
np = JSON_parse_float(json, p, pe, result);
@@ -550,7 +558,7 @@ tr3:
}
goto st29;
tr7:
-#line 242 "parser.rl"
+#line 250 "parser.rl"
{
char *np;
np = JSON_parse_array(json, p, pe, result, current_nesting + 1);
@@ -558,7 +566,7 @@ tr7:
}
goto st29;
tr11:
-#line 248 "parser.rl"
+#line 256 "parser.rl"
{
char *np;
np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
@@ -566,39 +574,39 @@ tr11:
}
goto st29;
tr25:
-#line 212 "parser.rl"
+#line 220 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
} else {
- rb_enc_raise(rb_utf8_encoding(), eParserError, "unexpected token at '%s'", p - 7);
+ raise_parse_error("unexpected token at '%s'", p - 7);
}
}
goto st29;
tr27:
-#line 205 "parser.rl"
+#line 213 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
} else {
- rb_enc_raise(rb_utf8_encoding(), eParserError, "unexpected token at '%s'", p - 2);
+ raise_parse_error("unexpected token at '%s'", p - 2);
}
}
goto st29;
tr31:
-#line 199 "parser.rl"
+#line 207 "parser.rl"
{
*result = Qfalse;
}
goto st29;
tr34:
-#line 196 "parser.rl"
+#line 204 "parser.rl"
{
*result = Qnil;
}
goto st29;
tr37:
-#line 202 "parser.rl"
+#line 210 "parser.rl"
{
*result = Qtrue;
}
@@ -607,9 +615,9 @@ st29:
if ( ++p == pe )
goto _test_eof29;
case 29:
-#line 254 "parser.rl"
+#line 262 "parser.rl"
{ p--; {p++; cs = 29; goto _out;} }
-#line 613 "parser.c"
+#line 621 "parser.c"
switch( (*p) ) {
case 13: goto st29;
case 32: goto st29;
@@ -850,7 +858,7 @@ case 28:
_out: {}
}
-#line 275 "parser.rl"
+#line 283 "parser.rl"
if (json->freeze) {
OBJ_FREEZE(*result);
@@ -864,7 +872,7 @@ case 28:
}
-#line 868 "parser.c"
+#line 876 "parser.c"
enum {JSON_integer_start = 1};
enum {JSON_integer_first_final = 3};
enum {JSON_integer_error = 0};
@@ -872,7 +880,7 @@ enum {JSON_integer_error = 0};
enum {JSON_integer_en_main = 1};
-#line 295 "parser.rl"
+#line 303 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -880,15 +888,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 884 "parser.c"
+#line 892 "parser.c"
{
cs = JSON_integer_start;
}
-#line 302 "parser.rl"
+#line 310 "parser.rl"
json->memo = p;
-#line 892 "parser.c"
+#line 900 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -922,14 +930,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 292 "parser.rl"
+#line 300 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 933 "parser.c"
+#line 941 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -948,7 +956,7 @@ case 5:
_out: {}
}
-#line 304 "parser.rl"
+#line 312 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -963,7 +971,7 @@ case 5:
}
-#line 967 "parser.c"
+#line 975 "parser.c"
enum {JSON_float_start = 1};
enum {JSON_float_first_final = 8};
enum {JSON_float_error = 0};
@@ -971,7 +979,7 @@ enum {JSON_float_error = 0};
enum {JSON_float_en_main = 1};
-#line 329 "parser.rl"
+#line 337 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -979,15 +987,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 983 "parser.c"
+#line 991 "parser.c"
{
cs = JSON_float_start;
}
-#line 336 "parser.rl"
+#line 344 "parser.rl"
json->memo = p;
-#line 991 "parser.c"
+#line 999 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1045,14 +1053,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 323 "parser.rl"
+#line 331 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 1056 "parser.c"
+#line 1064 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1113,7 +1121,7 @@ case 7:
_out: {}
}
-#line 338 "parser.rl"
+#line 346 "parser.rl"
if (cs >= JSON_float_first_final) {
VALUE mod = Qnil;
@@ -1164,7 +1172,7 @@ case 7:
-#line 1168 "parser.c"
+#line 1176 "parser.c"
enum {JSON_array_start = 1};
enum {JSON_array_first_final = 17};
enum {JSON_array_error = 0};
@@ -1172,7 +1180,7 @@ enum {JSON_array_error = 0};
enum {JSON_array_en_main = 1};
-#line 416 "parser.rl"
+#line 424 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -1186,14 +1194,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 1190 "parser.c"
+#line 1198 "parser.c"
{
cs = JSON_array_start;
}
-#line 429 "parser.rl"
+#line 437 "parser.rl"
-#line 1197 "parser.c"
+#line 1205 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1232,7 +1240,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 393 "parser.rl"
+#line 401 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@@ -1252,7 +1260,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1256 "parser.c"
+#line 1264 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1352,14 +1360,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 408 "parser.rl"
+#line 416 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1363 "parser.c"
+#line 1371 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1415,12 +1423,12 @@ case 16:
_out: {}
}
-#line 430 "parser.rl"
+#line 438 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
} else {
- rb_enc_raise(rb_utf8_encoding(), eParserError, "unexpected token at '%s'", p);
+ raise_parse_error("unexpected token at '%s'", p);
return NULL;
}
}
@@ -1483,10 +1491,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
ruby_xfree(bufferStart);
}
- rb_enc_raise(
- rb_utf8_encoding(), eParserError,
- "incomplete unicode character escape sequence at '%s'", p
- );
+ raise_parse_error("incomplete unicode character escape sequence at '%s'", p);
} else {
uint32_t ch = unescape_unicode((unsigned char *) ++pe);
pe += 3;
@@ -1506,10 +1511,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
ruby_xfree(bufferStart);
}
- rb_enc_raise(
- rb_utf8_encoding(), eParserError,
- "incomplete surrogate pair at '%s'", p
- );
+ raise_parse_error("incomplete surrogate pair at '%s'", p);
}
if (pe[0] == '\\' && pe[1] == 'u') {
uint32_t sur = unescape_unicode((unsigned char *) pe + 2);
@@ -1582,7 +1584,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
}
-#line 1586 "parser.c"
+#line 1588 "parser.c"
enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0};
@@ -1590,7 +1592,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1};
-#line 614 "parser.rl"
+#line 616 "parser.rl"
static int
@@ -1611,15 +1613,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
VALUE match_string;
-#line 1615 "parser.c"
+#line 1617 "parser.c"
{
cs = JSON_string_start;
}
-#line 634 "parser.rl"
+#line 636 "parser.rl"
json->memo = p;
-#line 1623 "parser.c"
+#line 1625 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1644,7 +1646,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 601 "parser.rl"
+#line 603 "parser.rl"
{
*result = json_string_unescape(json->memo + 1, p, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names);
if (NIL_P(*result)) {
@@ -1654,14 +1656,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 611 "parser.rl"
+#line 613 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1665 "parser.c"
+#line 1667 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1737,7 +1739,7 @@ case 7:
_out: {}
}
-#line 636 "parser.rl"
+#line 638 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1924,7 +1926,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1928 "parser.c"
+#line 1930 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@@ -1932,7 +1934,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
-#line 836 "parser.rl"
+#line 838 "parser.rl"
/*
@@ -1944,22 +1946,22 @@ enum {JSON_en_main = 1};
*/
static VALUE cParser_parse(VALUE self)
{
- char *p, *pe;
- int cs = EVIL;
- VALUE result = Qnil;
- GET_PARSER;
+ char *p, *pe;
+ int cs = EVIL;
+ VALUE result = Qnil;
+ GET_PARSER;
-#line 1954 "parser.c"
+#line 1956 "parser.c"
{
cs = JSON_start;
}
-#line 853 "parser.rl"
- p = json->source;
- pe = p + json->len;
+#line 855 "parser.rl"
+ p = json->source;
+ pe = p + json->len;
-#line 1963 "parser.c"
+#line 1965 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1993,7 +1995,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 828 "parser.rl"
+#line 830 "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;}
@@ -2003,7 +2005,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 2007 "parser.c"
+#line 2009 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -2092,14 +2094,14 @@ case 9:
_out: {}
}
-#line 856 "parser.rl"
+#line 858 "parser.rl"
- if (cs >= JSON_first_final && p == pe) {
- return result;
- } else {
- rb_enc_raise(rb_utf8_encoding(), eParserError, "unexpected token at '%s'", p);
- return Qnil;
- }
+ if (cs >= JSON_first_final && p == pe) {
+ return result;
+ } else {
+ raise_parse_error("unexpected token at '%s'", p);
+ return Qnil;
+ }
}
static void JSON_mark(void *ptr)
@@ -2164,9 +2166,7 @@ void Init_parser(void)
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);