summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-02-17 10:59:57 +1100
committerDamien George <damien.p.george@gmail.com>2017-02-17 10:59:57 +1100
commita68c75468812859633814e90207faec1a361778e (patch)
tree044594c9f228e3ca4c5eafa36e2ee7e11872dbbb
parent98b3072da58b9d8f6c054ab40703abea7bbe7018 (diff)
downloadmicropython-a68c75468812859633814e90207faec1a361778e.tar.gz
micropython-a68c75468812859633814e90207faec1a361778e.zip
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.
-rw-r--r--py/lexer.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/py/lexer.c b/py/lexer.c
index 91019e7e43..6a3fa656b1 100644
--- a/py/lexer.c
+++ b/py/lexer.c
@@ -527,6 +527,23 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
next_char(lex);
}
+ // Check if the name is a keyword.
+ // We also check for __debug__ here and convert it to its value. This is
+ // so the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
+ // need to check for this special token in many places in the compiler.
+ // TODO improve speed of these string comparisons
+ for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
+ if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) {
+ if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
+ // tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"
+ lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
+ } else {
+ lex->tok_kind = MP_TOKEN_KW_FALSE + i;
+ }
+ break;
+ }
+ }
+
} else if (is_digit(lex) || (is_char(lex, '.') && is_following_digit(lex))) {
bool forced_integer = false;
if (is_char(lex, '.')) {
@@ -655,26 +672,6 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
}
}
}
-
- // check for keywords
- if (lex->tok_kind == MP_TOKEN_NAME) {
- // We check for __debug__ here and convert it to its value. This is so
- // the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
- // need to check for this special token in many places in the compiler.
- // TODO improve speed of these string comparisons
- //for (mp_int_t i = 0; tok_kw[i] != NULL; i++) {
- for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
- if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) {
- if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
- // tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"
- lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
- } else {
- lex->tok_kind = MP_TOKEN_KW_FALSE + i;
- }
- break;
- }
- }
- }
}
mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {