Skip to content

Commit 887274e

Browse files
samyronbyroot
authored andcommitted
Reorder the json_frame_type and json_frame_phase enum to simplify the transition from a JSON_PHASE_VALUE to the next phase.
1 parent 84fbc08 commit 887274e

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

ext/json/ext/parser/parser.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,25 @@ static void rvalue_stack_eagerly_release(VALUE handle)
345345
#define JSON_FRAME_STACK_INITIAL_CAPA 32
346346

347347
enum json_frame_type {
348-
JSON_FRAME_ROOT,
349-
JSON_FRAME_ARRAY,
350-
JSON_FRAME_OBJECT,
348+
JSON_FRAME_ROOT, // == JSON_PHASE_DONE
349+
JSON_FRAME_ARRAY, // == JSON_PHASE_ARRAY_COMMA
350+
JSON_FRAME_OBJECT, // = JSON_PHASE_OBJECT_COMMA
351351
};
352352

353353
// Where a frame is within its container's grammar. This is the entirety of the
354354
// parser's "what to do next" state: json_parse_any dispatches on the top
355355
// frame's phase and holds no resume state in C locals, so a parse can stop at
356356
// any value boundary and be resumed purely from the (persistable) frame stack.
357+
//
358+
// The first three phases are deliberately equal to the corresponding json_frame_type
359+
// to simplify the transition of phase in json_value_completed.
357360
enum json_frame_phase {
361+
JSON_PHASE_DONE = JSON_FRAME_ROOT, // root only: the document value has been parsed
362+
JSON_PHASE_ARRAY_COMMA = JSON_FRAME_ARRAY, // after a value: expecting ',' or the closing ']'
363+
JSON_PHASE_OBJECT_COMMA = JSON_FRAME_OBJECT, // after a value: expecting ',' or the closing '}'
358364
JSON_PHASE_VALUE, // expecting a value (document root, array element, or object value after ':')
359-
JSON_PHASE_ARRAY_COMMA, // after a value: expecting ',' or the closing ']'
360365
JSON_PHASE_OBJECT_KEY, // expecting a '"' key (after '{' or ',')
361-
JSON_PHASE_OBJECT_COMMA, // after a value: expecting ',' or the closing '}'
362366
JSON_PHASE_OBJECT_COLON, // object only: after a key, expecting ':'
363-
JSON_PHASE_DONE, // root only: the document value has been parsed
364367
};
365368

366369
typedef struct json_frame_struct {
@@ -1442,18 +1445,11 @@ static inline long json_frame_entry_count(const json_frame *frame, const rvalue_
14421445
// after a container close is the freshly re-exposed parent.
14431446
static inline void json_value_completed(json_frame *frame)
14441447
{
1445-
switch (frame->type) {
1446-
case JSON_FRAME_ROOT:
1447-
frame->phase = JSON_PHASE_DONE;
1448-
return;
1449-
case JSON_FRAME_ARRAY:
1450-
frame->phase = JSON_PHASE_ARRAY_COMMA;
1451-
return;
1452-
case JSON_FRAME_OBJECT:
1453-
frame->phase = JSON_PHASE_OBJECT_COMMA;
1454-
return;
1455-
}
1456-
UNREACHABLE;
1448+
JSON_ASSERT((int)JSON_PHASE_DONE == (int)JSON_FRAME_ROOT);
1449+
JSON_ASSERT((int)JSON_PHASE_ARRAY_COMMA == (int)JSON_FRAME_ARRAY);
1450+
JSON_ASSERT((int)JSON_PHASE_OBJECT_COMMA == (int)JSON_FRAME_OBJECT);
1451+
1452+
frame->phase = (enum json_frame_phase) frame->type;
14571453
}
14581454

14591455
// Parse an arbitrary JSON value iteratively. This is a state machine driven

0 commit comments

Comments
 (0)