Skip to content

Commit a68c754

Browse files
committed
py/lexer: Move check for keyword to name-tokenising block.
Keywords only needs to be searched for if the token is a MP_TOKEN_NAME, so we can move the seach to the part of the code that does the tokenising for MP_TOKEN_NAME.
1 parent 98b3072 commit a68c754

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

py/lexer.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,23 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
527527
next_char(lex);
528528
}
529529

530+
// Check if the name is a keyword.
531+
// We also check for __debug__ here and convert it to its value. This is
532+
// so the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
533+
// need to check for this special token in many places in the compiler.
534+
// TODO improve speed of these string comparisons
535+
for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
536+
if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) {
537+
if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
538+
// tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"
539+
lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
540+
} else {
541+
lex->tok_kind = MP_TOKEN_KW_FALSE + i;
542+
}
543+
break;
544+
}
545+
}
546+
530547
} else if (is_digit(lex) || (is_char(lex, '.') && is_following_digit(lex))) {
531548
bool forced_integer = false;
532549
if (is_char(lex, '.')) {
@@ -655,26 +672,6 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
655672
}
656673
}
657674
}
658-
659-
// check for keywords
660-
if (lex->tok_kind == MP_TOKEN_NAME) {
661-
// We check for __debug__ here and convert it to its value. This is so
662-
// the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
663-
// need to check for this special token in many places in the compiler.
664-
// TODO improve speed of these string comparisons
665-
//for (mp_int_t i = 0; tok_kw[i] != NULL; i++) {
666-
for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
667-
if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) {
668-
if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
669-
// tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"
670-
lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
671-
} else {
672-
lex->tok_kind = MP_TOKEN_KW_FALSE + i;
673-
}
674-
break;
675-
}
676-
}
677-
}
678675
}
679676

680677
mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {

0 commit comments

Comments
 (0)