Skip to content

Commit 6d310a5

Browse files
committed
py/parse: Only replace constants that are standalone identifiers.
This fixes constant substitution so that only standalone identifiers are replaced with their constant value (if they have one). I.e. don't replace NAME in expressions like obj.NAME or NAME = expr.
1 parent eeb9d99 commit 6d310a5

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

py/parse.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,21 +398,24 @@ STATIC mp_parse_node_t make_node_const_object(parser_t *parser, size_t src_line,
398398
return (mp_parse_node_t)pn;
399399
}
400400

401-
STATIC void push_result_token(parser_t *parser) {
401+
STATIC void push_result_token(parser_t *parser, const rule_t *rule) {
402402
mp_parse_node_t pn;
403403
mp_lexer_t *lex = parser->lexer;
404404
if (lex->tok_kind == MP_TOKEN_NAME) {
405405
qstr id = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
406406
#if MICROPY_COMP_CONST
407-
// lookup identifier in table of dynamic constants
408-
mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP);
409-
if (elem != NULL) {
407+
// if name is a standalone identifier, look it up in the table of dynamic constants
408+
mp_map_elem_t *elem;
409+
if (rule->rule_id == RULE_atom
410+
&& (elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP)) != NULL) {
410411
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
411-
} else
412-
#endif
413-
{
412+
} else {
414413
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, id);
415414
}
415+
#else
416+
(void)rule;
417+
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, id);
418+
#endif
416419
} else if (lex->tok_kind == MP_TOKEN_INTEGER) {
417420
mp_obj_t o = mp_parse_num_integer(lex->vstr.buf, lex->vstr.len, 0, lex);
418421
if (MP_OBJ_IS_SMALL_INT(o)) {
@@ -765,7 +768,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
765768
uint16_t kind = rule->arg[i] & RULE_ARG_KIND_MASK;
766769
if (kind == RULE_ARG_TOK) {
767770
if (lex->tok_kind == (rule->arg[i] & RULE_ARG_ARG_MASK)) {
768-
push_result_token(&parser);
771+
push_result_token(&parser, rule);
769772
mp_lexer_to_next(lex);
770773
goto next_rule;
771774
}
@@ -810,7 +813,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
810813
if (lex->tok_kind == tok_kind) {
811814
// matched token
812815
if (tok_kind == MP_TOKEN_NAME) {
813-
push_result_token(&parser);
816+
push_result_token(&parser, rule);
814817
}
815818
mp_lexer_to_next(lex);
816819
} else {
@@ -950,7 +953,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
950953
if (i & 1 & n) {
951954
// separators which are tokens are not pushed to result stack
952955
} else {
953-
push_result_token(&parser);
956+
push_result_token(&parser, rule);
954957
}
955958
mp_lexer_to_next(lex);
956959
// got element of list, so continue parsing list

0 commit comments

Comments
 (0)