py/parse: De-duplicate and simplify code for parser "or" rule.

This commit is contained in:
Damien George 2015-07-24 14:35:57 +00:00
parent d241c2a592
commit fa7c61dfab
1 changed files with 13 additions and 25 deletions

View File

@ -448,36 +448,24 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
} else {
backtrack = false;
}
for (; i < n - 1; ++i) {
switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
case RULE_ARG_TOK:
if (lex->tok_kind == (rule->arg[i] & RULE_ARG_ARG_MASK)) {
push_result_token(&parser);
mp_lexer_to_next(lex);
goto next_rule;
}
break;
case RULE_ARG_RULE:
rule_or_no_other_choice:
push_rule(&parser, rule_src_line, rule, i + 1); // save this or-rule
push_rule_from_arg(&parser, rule->arg[i]); // push child of or-rule
for (; i < n; ++i) {
uint16_t kind = rule->arg[i] & RULE_ARG_KIND_MASK;
if (kind == RULE_ARG_TOK) {
if (lex->tok_kind == (rule->arg[i] & RULE_ARG_ARG_MASK)) {
push_result_token(&parser);
mp_lexer_to_next(lex);
goto next_rule;
default:
assert(0);
goto rule_or_no_other_choice; // to help flow control analysis
}
}
if ((rule->arg[i] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
if (lex->tok_kind == (rule->arg[i] & RULE_ARG_ARG_MASK)) {
push_result_token(&parser);
mp_lexer_to_next(lex);
}
} else {
backtrack = true;
assert(kind == RULE_ARG_RULE);
if (i + 1 < n) {
push_rule(&parser, rule_src_line, rule, i + 1); // save this or-rule
}
push_rule_from_arg(&parser, rule->arg[i]); // push child of or-rule
goto next_rule;
}
} else {
push_rule_from_arg(&parser, rule->arg[i]);
}
backtrack = true;
break;
case RULE_ACT_AND: {