Skip to content

Commit 65904e8

Browse files
committed
parser.c: refactor json_push_value / json_value_completed
Makes each case simpler, but also more consistent.
1 parent b07f74b commit 65904e8

1 file changed

Lines changed: 25 additions & 29 deletions

File tree

ext/json/ext/parser/parser.c

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ static VALUE json_parse_escaped_string(JSON_ParserState *state, JSON_ParserConfi
12361236
case '"': {
12371237
VALUE string = json_string_unescape(state, config, start, state->cursor, is_name, &positions);
12381238
state->cursor++;
1239-
return json_push_value(state, config, string);
1239+
return string;
12401240
}
12411241
case '\\': {
12421242
if (RB_LIKELY(positions.size < JSON_MAX_UNESCAPE_POSITIONS)) {
@@ -1271,12 +1271,16 @@ ALWAYS_INLINE(static) VALUE json_parse_string(JSON_ParserState *state, JSON_Pars
12711271
raise_parse_error("unexpected end of input, expected closing \"", state);
12721272
}
12731273

1274+
VALUE string;
12741275
if (RB_LIKELY(*state->cursor == '"')) {
1275-
VALUE string = json_string_fastpath(state, config, start, state->cursor, is_name);
1276+
string = json_string_fastpath(state, config, start, state->cursor, is_name);
12761277
state->cursor++;
1277-
return json_push_value(state, config, string);
12781278
}
1279-
return json_parse_escaped_string(state, config, is_name, start);
1279+
else {
1280+
string = json_parse_escaped_string(state, config, is_name, start);
1281+
}
1282+
1283+
return string;
12801284
}
12811285

12821286
#if JSON_CPU_LITTLE_ENDIAN_64BITS
@@ -1487,77 +1491,67 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
14871491
JSON_PHASE_VALUE:
14881492
json_eat_whitespace(state);
14891493

1494+
VALUE value;
14901495
switch (peek(state)) {
14911496
case 'n':
14921497
if (json_match_keyword(state, "null", 0)) {
1493-
json_push_value(state, config, Qnil);
1494-
json_value_completed(frame);
1498+
value = Qnil;
14951499
break;
14961500
}
14971501

14981502
raise_parse_error("unexpected token %s", state);
14991503
case 't':
15001504
if (json_match_keyword(state, "true", 0)) {
1501-
json_push_value(state, config, Qtrue);
1502-
json_value_completed(frame);
1505+
value = Qtrue;
15031506
break;
15041507
}
15051508

15061509
raise_parse_error("unexpected token %s", state);
15071510
case 'f':
15081511
if (json_match_keyword(state, "false", 1)) {
1509-
json_push_value(state, config, Qfalse);
1510-
json_value_completed(frame);
1512+
value = Qfalse;
15111513
break;
15121514
}
15131515

15141516
raise_parse_error("unexpected token %s", state);
15151517
case 'N':
15161518
// Note: memcmp with a small power of two compile to an integer comparison
15171519
if (config->allow_nan && json_match_keyword(state, "NaN", 1)) {
1518-
json_push_value(state, config, CNaN);
1519-
json_value_completed(frame);
1520+
value = CNaN;
15201521
break;
15211522
}
15221523

15231524
raise_parse_error("unexpected token %s", state);
15241525
case 'I':
15251526
if (config->allow_nan && json_match_keyword(state, "Infinity", 0)) {
1526-
json_push_value(state, config, CInfinity);
1527-
json_value_completed(frame);
1527+
value = CInfinity;
15281528
break;
15291529
}
15301530

15311531
raise_parse_error("unexpected token %s", state);
15321532
case '-': {
15331533
state->cursor++;
15341534
if (config->allow_nan && json_match_keyword(state, "Infinity", 0)) {
1535-
json_push_value(state, config, CMinusInfinity);
1536-
json_value_completed(frame);
1537-
break;
1535+
value = CMinusInfinity;
1536+
} else {
1537+
value = json_parse_negative_number(state, config);
15381538
}
1539-
1540-
json_push_value(state, config, json_parse_negative_number(state, config));
1541-
json_value_completed(frame);
15421539
break;
15431540
}
15441541
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1545-
json_push_value(state, config, json_parse_positive_number(state, config));
1546-
json_value_completed(frame);
1542+
value = json_parse_positive_number(state, config);
15471543
break;
15481544
case '"':
15491545
// %r{\A"[^"\\\t\n\x00]*(?:\\[bfnrtu\\/"][^"\\]*)*"}
1550-
json_parse_string(state, config, false);
1551-
json_value_completed(frame);
1546+
value = json_parse_string(state, config, false);
15521547
break;
15531548
case '[': {
15541549
state->cursor++;
15551550
json_eat_whitespace(state);
15561551

15571552
if (peek(state) == ']') {
15581553
state->cursor++;
1559-
json_push_value(state, config, json_decode_array(state, config, 0));
1560-
json_value_completed(frame);
1554+
value = json_decode_array(state, config, 0);
15611555
break;
15621556
}
15631557

@@ -1584,8 +1578,7 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
15841578

15851579
if (peek(state) == '}') {
15861580
state->cursor++;
1587-
json_push_value(state, config, json_decode_object(state, config, 0));
1588-
json_value_completed(frame);
1581+
value = json_decode_object(state, config, 0);
15891582
break;
15901583
}
15911584

@@ -1611,6 +1604,9 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
16111604
default:
16121605
raise_parse_error("unexpected character: %s", state);
16131606
}
1607+
1608+
json_push_value(state, config, value);
1609+
json_value_completed(frame);
16141610
break;
16151611
}
16161612

@@ -1621,7 +1617,7 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
16211617
json_eat_whitespace(state);
16221618

16231619
if (RB_LIKELY(peek(state) == '"')) {
1624-
json_parse_string(state, config, true);
1620+
json_push_value(state, config, json_parse_string(state, config, true));
16251621
frame->phase = JSON_PHASE_OBJECT_COLON;
16261622
goto JSON_PHASE_OBJECT_COLON;
16271623
} else {

0 commit comments

Comments
 (0)