Skip to content

Commit 86e9423

Browse files
committed
py/parse: Refactor code to remove assert(0)'s.
This helps to improve code coverage. Note that most of the changes in this patch are just de-denting the cases of the switch statements.
1 parent 5314219 commit 86e9423

1 file changed

Lines changed: 46 additions & 59 deletions

File tree

py/parse.c

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,9 @@ void mp_parse_node_print(mp_parse_node_t pn, size_t indent) {
295295
case MP_PARSE_NODE_ID: printf("id(%s)\n", qstr_str(arg)); break;
296296
case MP_PARSE_NODE_STRING: printf("str(%s)\n", qstr_str(arg)); break;
297297
case MP_PARSE_NODE_BYTES: printf("bytes(%s)\n", qstr_str(arg)); break;
298-
case MP_PARSE_NODE_TOKEN: printf("tok(%u)\n", (uint)arg); break;
299-
default: assert(0);
298+
default:
299+
assert(MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN);
300+
printf("tok(%u)\n", (uint)arg); break;
300301
}
301302
} else {
302303
// node must be a mp_parse_node_struct_t
@@ -870,38 +871,30 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
870871

871872
// progress through the rule
872873
for (; i < n; ++i) {
873-
switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
874-
case RULE_ARG_TOK: {
875-
// need to match a token
876-
mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
877-
if (lex->tok_kind == tok_kind) {
878-
// matched token
879-
if (tok_kind == MP_TOKEN_NAME) {
880-
push_result_token(&parser, rule);
881-
}
882-
mp_lexer_to_next(lex);
874+
if ((rule->arg[i] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
875+
// need to match a token
876+
mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
877+
if (lex->tok_kind == tok_kind) {
878+
// matched token
879+
if (tok_kind == MP_TOKEN_NAME) {
880+
push_result_token(&parser, rule);
881+
}
882+
mp_lexer_to_next(lex);
883+
} else {
884+
// failed to match token
885+
if (i > 0) {
886+
// already eaten tokens so can't backtrack
887+
goto syntax_error;
883888
} else {
884-
// failed to match token
885-
if (i > 0) {
886-
// already eaten tokens so can't backtrack
887-
goto syntax_error;
888-
} else {
889-
// this rule failed, so backtrack
890-
backtrack = true;
891-
goto next_rule;
892-
}
889+
// this rule failed, so backtrack
890+
backtrack = true;
891+
goto next_rule;
893892
}
894-
break;
895893
}
896-
case RULE_ARG_RULE:
897-
case RULE_ARG_OPT_RULE:
898-
rule_and_no_other_choice:
899-
push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
900-
push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
901-
goto next_rule;
902-
default:
903-
assert(0);
904-
goto rule_and_no_other_choice; // to help flow control analysis
894+
} else {
895+
push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
896+
push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
897+
goto next_rule;
905898
}
906899
}
907900

@@ -973,7 +966,9 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
973966
break;
974967
}
975968

976-
case RULE_ACT_LIST: {
969+
default: {
970+
assert((rule->act & RULE_ACT_KIND_MASK) == RULE_ACT_LIST);
971+
977972
// n=2 is: item item*
978973
// n=1 is: item (sep item)*
979974
// n=3 is: item (sep item)* [sep]
@@ -1011,32 +1006,27 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
10111006
} else {
10121007
for (;;) {
10131008
size_t arg = rule->arg[i & 1 & n];
1014-
switch (arg & RULE_ARG_KIND_MASK) {
1015-
case RULE_ARG_TOK:
1016-
if (lex->tok_kind == (arg & RULE_ARG_ARG_MASK)) {
1017-
if (i & 1 & n) {
1018-
// separators which are tokens are not pushed to result stack
1019-
} else {
1020-
push_result_token(&parser, rule);
1021-
}
1022-
mp_lexer_to_next(lex);
1023-
// got element of list, so continue parsing list
1024-
i += 1;
1009+
if ((arg & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
1010+
if (lex->tok_kind == (arg & RULE_ARG_ARG_MASK)) {
1011+
if (i & 1 & n) {
1012+
// separators which are tokens are not pushed to result stack
10251013
} else {
1026-
// couldn't get element of list
1027-
i += 1;
1028-
backtrack = true;
1029-
goto list_backtrack;
1014+
push_result_token(&parser, rule);
10301015
}
1031-
break;
1032-
case RULE_ARG_RULE:
1033-
rule_list_no_other_choice:
1034-
push_rule(&parser, rule_src_line, rule, i + 1); // save this list-rule
1035-
push_rule_from_arg(&parser, arg); // push child of list-rule
1036-
goto next_rule;
1037-
default:
1038-
assert(0);
1039-
goto rule_list_no_other_choice; // to help flow control analysis
1016+
mp_lexer_to_next(lex);
1017+
// got element of list, so continue parsing list
1018+
i += 1;
1019+
} else {
1020+
// couldn't get element of list
1021+
i += 1;
1022+
backtrack = true;
1023+
goto list_backtrack;
1024+
}
1025+
} else {
1026+
assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE);
1027+
push_rule(&parser, rule_src_line, rule, i + 1); // save this list-rule
1028+
push_rule_from_arg(&parser, arg); // push child of list-rule
1029+
goto next_rule;
10401030
}
10411031
}
10421032
}
@@ -1062,9 +1052,6 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
10621052
}
10631053
break;
10641054
}
1065-
1066-
default:
1067-
assert(0);
10681055
}
10691056
}
10701057

0 commit comments

Comments
 (0)