aboutsummaryrefslogtreecommitdiff
path: root/py/parse.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-01-17 17:00:55 +1100
committerDamien George <damien.p.george@gmail.com>2017-01-17 17:00:55 +1100
commit86e942309ad0c61c7f0cc9abca509bfbca404ed5 (patch)
tree3aeb2991d48614ccc35b239a9cc59c4cb81a13c1 /py/parse.c
parent5314219f182e8a303e3fa68e2bacca978cb08002 (diff)
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.
Diffstat (limited to 'py/parse.c')
-rw-r--r--py/parse.c105
1 files changed, 46 insertions, 59 deletions
diff --git a/py/parse.c b/py/parse.c
index dc360e40c..b1c2c19ea 100644
--- a/py/parse.c
+++ b/py/parse.c
@@ -295,8 +295,9 @@ void mp_parse_node_print(mp_parse_node_t pn, size_t indent) {
case MP_PARSE_NODE_ID: printf("id(%s)\n", qstr_str(arg)); break;
case MP_PARSE_NODE_STRING: printf("str(%s)\n", qstr_str(arg)); break;
case MP_PARSE_NODE_BYTES: printf("bytes(%s)\n", qstr_str(arg)); break;
- case MP_PARSE_NODE_TOKEN: printf("tok(%u)\n", (uint)arg); break;
- default: assert(0);
+ default:
+ assert(MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN);
+ printf("tok(%u)\n", (uint)arg); break;
}
} else {
// 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) {
// progress through the rule
for (; i < n; ++i) {
- switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
- case RULE_ARG_TOK: {
- // need to match a token
- mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
- if (lex->tok_kind == tok_kind) {
- // matched token
- if (tok_kind == MP_TOKEN_NAME) {
- push_result_token(&parser, rule);
- }
- mp_lexer_to_next(lex);
+ if ((rule->arg[i] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
+ // need to match a token
+ mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
+ if (lex->tok_kind == tok_kind) {
+ // matched token
+ if (tok_kind == MP_TOKEN_NAME) {
+ push_result_token(&parser, rule);
+ }
+ mp_lexer_to_next(lex);
+ } else {
+ // failed to match token
+ if (i > 0) {
+ // already eaten tokens so can't backtrack
+ goto syntax_error;
} else {
- // failed to match token
- if (i > 0) {
- // already eaten tokens so can't backtrack
- goto syntax_error;
- } else {
- // this rule failed, so backtrack
- backtrack = true;
- goto next_rule;
- }
+ // this rule failed, so backtrack
+ backtrack = true;
+ goto next_rule;
}
- break;
}
- case RULE_ARG_RULE:
- case RULE_ARG_OPT_RULE:
- rule_and_no_other_choice:
- push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
- push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
- goto next_rule;
- default:
- assert(0);
- goto rule_and_no_other_choice; // to help flow control analysis
+ } else {
+ push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
+ push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
+ goto next_rule;
}
}
@@ -973,7 +966,9 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
break;
}
- case RULE_ACT_LIST: {
+ default: {
+ assert((rule->act & RULE_ACT_KIND_MASK) == RULE_ACT_LIST);
+
// n=2 is: item item*
// n=1 is: item (sep item)*
// 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) {
} else {
for (;;) {
size_t arg = rule->arg[i & 1 & n];
- switch (arg & RULE_ARG_KIND_MASK) {
- case RULE_ARG_TOK:
- if (lex->tok_kind == (arg & RULE_ARG_ARG_MASK)) {
- if (i & 1 & n) {
- // separators which are tokens are not pushed to result stack
- } else {
- push_result_token(&parser, rule);
- }
- mp_lexer_to_next(lex);
- // got element of list, so continue parsing list
- i += 1;
+ if ((arg & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
+ if (lex->tok_kind == (arg & RULE_ARG_ARG_MASK)) {
+ if (i & 1 & n) {
+ // separators which are tokens are not pushed to result stack
} else {
- // couldn't get element of list
- i += 1;
- backtrack = true;
- goto list_backtrack;
+ push_result_token(&parser, rule);
}
- break;
- case RULE_ARG_RULE:
- rule_list_no_other_choice:
- push_rule(&parser, rule_src_line, rule, i + 1); // save this list-rule
- push_rule_from_arg(&parser, arg); // push child of list-rule
- goto next_rule;
- default:
- assert(0);
- goto rule_list_no_other_choice; // to help flow control analysis
+ mp_lexer_to_next(lex);
+ // got element of list, so continue parsing list
+ i += 1;
+ } else {
+ // couldn't get element of list
+ i += 1;
+ backtrack = true;
+ goto list_backtrack;
+ }
+ } else {
+ assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE);
+ push_rule(&parser, rule_src_line, rule, i + 1); // save this list-rule
+ push_rule_from_arg(&parser, arg); // push child of list-rule
+ goto next_rule;
}
}
}
@@ -1062,9 +1052,6 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
}
break;
}
-
- default:
- assert(0);
}
}