Skip to content

Commit 2870d85

Browse files
committed
py: Save a few code bytes in parser; make vars local where possible.
1 parent 978f4ca commit 2870d85

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

py/parse.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,12 @@ STATIC void push_result_token(parser_t *parser) {
314314
bool overflow = false;
315315
for (; i < len; i++) {
316316
mp_uint_t dig;
317+
int clower = str[i] | 0x20;
317318
if (unichar_isdigit(str[i]) && str[i] - '0' < base) {
318319
dig = str[i] - '0';
319-
} else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') {
320-
dig = str[i] - 'a' + 10;
321-
} else if (base == 16 && 'A' <= str[i] && str[i] <= 'F') {
322-
dig = str[i] - 'A' + 10;
323-
} else if (str[i] == '.' || str[i] == 'e' || str[i] == 'E' || str[i] == 'j' || str[i] == 'J') {
320+
} else if (base == 16 && 'a' <= clower && clower <= 'f') {
321+
dig = clower - 'a' + 10;
322+
} else if (str[i] == '.' || clower == 'e' || clower == 'j') {
324323
dec = true;
325324
break;
326325
} else {
@@ -422,9 +421,6 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
422421
mp_uint_t rule_src_line; // source line for the first token matched by the current rule
423422
bool backtrack = false;
424423
const rule_t *rule = NULL;
425-
mp_token_kind_t tok_kind;
426-
bool emit_rule;
427-
bool had_trailing_sep;
428424

429425
for (;;) {
430426
next_rule:
@@ -481,7 +477,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
481477
}
482478
break;
483479

484-
case RULE_ACT_AND:
480+
case RULE_ACT_AND: {
485481

486482
// failed, backtrack if we can, else syntax error
487483
if (backtrack) {
@@ -504,9 +500,9 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
504500
// progress through the rule
505501
for (; i < n; ++i) {
506502
switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
507-
case RULE_ARG_TOK:
503+
case RULE_ARG_TOK: {
508504
// need to match a token
509-
tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
505+
mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
510506
if (lex->tok_kind == tok_kind) {
511507
// matched token
512508
if (tok_kind == MP_TOKEN_NAME) {
@@ -525,6 +521,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
525521
}
526522
}
527523
break;
524+
}
528525
case RULE_ARG_RULE:
529526
case RULE_ARG_OPT_RULE:
530527
push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
@@ -541,10 +538,10 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
541538

542539
// count number of arguments for the parse_node
543540
i = 0;
544-
emit_rule = false;
541+
bool emit_rule = false;
545542
for (mp_uint_t x = 0; x < n; ++x) {
546543
if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
547-
tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
544+
mp_token_kind_t tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
548545
if (tok_kind >= MP_TOKEN_NAME) {
549546
emit_rule = true;
550547
}
@@ -617,11 +614,13 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
617614
push_result_rule(&parser, rule_src_line, rule, i);
618615
}
619616
break;
617+
}
620618

621-
case RULE_ACT_LIST:
619+
case RULE_ACT_LIST: {
622620
// n=2 is: item item*
623621
// n=1 is: item (sep item)*
624622
// n=3 is: item (sep item)* [sep]
623+
bool had_trailing_sep;
625624
if (backtrack) {
626625
list_backtrack:
627626
had_trailing_sep = false;
@@ -704,6 +703,7 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p
704703
push_result_rule(&parser, rule_src_line, rule, i);
705704
}
706705
break;
706+
}
707707

708708
default:
709709
assert(0);

0 commit comments

Comments
 (0)