Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <assert.h> |
| 29 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 30 | #include "py/mpstate.h" |
Damien George | 511c083 | 2016-11-16 16:22:08 +1100 | [diff] [blame] | 31 | #include "py/reader.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include "py/lexer.h" |
Damien George | 081f932 | 2015-09-07 17:08:49 +0100 | [diff] [blame] | 33 | #include "py/runtime.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 34 | |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 35 | #if MICROPY_ENABLE_COMPILER |
| 36 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 37 | #define TAB_SIZE (8) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 38 | |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 39 | // TODO seems that CPython allows NULL byte in the input stream |
| 40 | // don't know if that's intentional or not, but we don't allow it |
| 41 | |
Damien George | 9528cd6 | 2014-01-15 21:23:31 +0000 | [diff] [blame] | 42 | // TODO replace with a call to a standard function |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 43 | STATIC bool str_strn_equal(const char *str, const char *strn, mp_uint_t len) { |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 44 | mp_uint_t i = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 45 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 46 | while (i < len && *str == *strn) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 47 | ++i; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 48 | ++str; |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 49 | ++strn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 52 | return i == len && *str == 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 55 | #define MP_LEXER_EOF ((unichar)MP_READER_EOF) |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 56 | #define CUR_CHAR(lex) ((lex)->chr0) |
| 57 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 58 | STATIC bool is_end(mp_lexer_t *lex) { |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 59 | return lex->chr0 == MP_LEXER_EOF; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 62 | STATIC bool is_physical_newline(mp_lexer_t *lex) { |
Damien George | 32bade1 | 2015-01-30 00:27:46 +0000 | [diff] [blame] | 63 | return lex->chr0 == '\n'; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Damien George | 2e2e404 | 2015-03-19 00:21:29 +0000 | [diff] [blame] | 66 | STATIC bool is_char(mp_lexer_t *lex, byte c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 67 | return lex->chr0 == c; |
| 68 | } |
| 69 | |
Damien George | 2e2e404 | 2015-03-19 00:21:29 +0000 | [diff] [blame] | 70 | STATIC bool is_char_or(mp_lexer_t *lex, byte c1, byte c2) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 71 | return lex->chr0 == c1 || lex->chr0 == c2; |
| 72 | } |
| 73 | |
Damien George | 2e2e404 | 2015-03-19 00:21:29 +0000 | [diff] [blame] | 74 | STATIC bool is_char_or3(mp_lexer_t *lex, byte c1, byte c2, byte c3) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 75 | return lex->chr0 == c1 || lex->chr0 == c2 || lex->chr0 == c3; |
| 76 | } |
| 77 | |
| 78 | /* |
Damien George | 2e2e404 | 2015-03-19 00:21:29 +0000 | [diff] [blame] | 79 | STATIC bool is_char_following(mp_lexer_t *lex, byte c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 80 | return lex->chr1 == c; |
| 81 | } |
| 82 | */ |
| 83 | |
Damien George | 2e2e404 | 2015-03-19 00:21:29 +0000 | [diff] [blame] | 84 | STATIC bool is_char_following_or(mp_lexer_t *lex, byte c1, byte c2) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 85 | return lex->chr1 == c1 || lex->chr1 == c2; |
| 86 | } |
| 87 | |
Damien George | 2e2e404 | 2015-03-19 00:21:29 +0000 | [diff] [blame] | 88 | STATIC bool is_char_following_following_or(mp_lexer_t *lex, byte c1, byte c2) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 89 | return lex->chr2 == c1 || lex->chr2 == c2; |
| 90 | } |
| 91 | |
Damien George | 2e2e404 | 2015-03-19 00:21:29 +0000 | [diff] [blame] | 92 | STATIC bool is_char_and(mp_lexer_t *lex, byte c1, byte c2) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 93 | return lex->chr0 == c1 && lex->chr1 == c2; |
| 94 | } |
| 95 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 96 | STATIC bool is_whitespace(mp_lexer_t *lex) { |
Damien George | 8cc96a3 | 2013-12-30 18:23:50 +0000 | [diff] [blame] | 97 | return unichar_isspace(lex->chr0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 100 | STATIC bool is_letter(mp_lexer_t *lex) { |
Damien George | 8cc96a3 | 2013-12-30 18:23:50 +0000 | [diff] [blame] | 101 | return unichar_isalpha(lex->chr0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 104 | STATIC bool is_digit(mp_lexer_t *lex) { |
Damien George | 8cc96a3 | 2013-12-30 18:23:50 +0000 | [diff] [blame] | 105 | return unichar_isdigit(lex->chr0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 108 | STATIC bool is_following_digit(mp_lexer_t *lex) { |
Damien George | 8cc96a3 | 2013-12-30 18:23:50 +0000 | [diff] [blame] | 109 | return unichar_isdigit(lex->chr1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Damien George | 2b00047 | 2015-09-07 17:33:44 +0100 | [diff] [blame] | 112 | STATIC bool is_following_base_char(mp_lexer_t *lex) { |
| 113 | const unichar chr1 = lex->chr1 | 0x20; |
| 114 | return chr1 == 'b' || chr1 == 'o' || chr1 == 'x'; |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 117 | STATIC bool is_following_odigit(mp_lexer_t *lex) { |
Paul Sokolovsky | 0b7184d | 2014-01-22 22:40:02 +0200 | [diff] [blame] | 118 | return lex->chr1 >= '0' && lex->chr1 <= '7'; |
| 119 | } |
| 120 | |
Damien George | 7ed58cb | 2015-06-09 10:58:07 +0000 | [diff] [blame] | 121 | // to easily parse utf-8 identifiers we allow any raw byte with high bit set |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 122 | STATIC bool is_head_of_identifier(mp_lexer_t *lex) { |
Damien George | 7ed58cb | 2015-06-09 10:58:07 +0000 | [diff] [blame] | 123 | return is_letter(lex) || lex->chr0 == '_' || lex->chr0 >= 0x80; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 126 | STATIC bool is_tail_of_identifier(mp_lexer_t *lex) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 127 | return is_head_of_identifier(lex) || is_digit(lex); |
| 128 | } |
| 129 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 130 | STATIC void next_char(mp_lexer_t *lex) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 131 | if (lex->chr0 == '\n') { |
Damien George | 32bade1 | 2015-01-30 00:27:46 +0000 | [diff] [blame] | 132 | // a new line |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 133 | ++lex->line; |
| 134 | lex->column = 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 135 | } else if (lex->chr0 == '\t') { |
| 136 | // a tab |
| 137 | lex->column = (((lex->column - 1 + TAB_SIZE) / TAB_SIZE) * TAB_SIZE) + 1; |
| 138 | } else { |
| 139 | // a character worth one column |
| 140 | ++lex->column; |
| 141 | } |
| 142 | |
Damien George | 32bade1 | 2015-01-30 00:27:46 +0000 | [diff] [blame] | 143 | lex->chr0 = lex->chr1; |
| 144 | lex->chr1 = lex->chr2; |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 145 | lex->chr2 = lex->reader.readbyte(lex->reader.data); |
Damien George | 32bade1 | 2015-01-30 00:27:46 +0000 | [diff] [blame] | 146 | |
| 147 | if (lex->chr0 == '\r') { |
| 148 | // CR is a new line, converted to LF |
| 149 | lex->chr0 = '\n'; |
| 150 | if (lex->chr1 == '\n') { |
| 151 | // CR LF is a single new line |
| 152 | lex->chr1 = lex->chr2; |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 153 | lex->chr2 = lex->reader.readbyte(lex->reader.data); |
Damien George | 32bade1 | 2015-01-30 00:27:46 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | if (lex->chr2 == MP_LEXER_EOF) { |
| 158 | // EOF, check if we need to insert a newline at end of file |
| 159 | if (lex->chr1 != MP_LEXER_EOF && lex->chr1 != '\n') { |
| 160 | // if lex->chr1 == '\r' then this makes a CR LF which will be converted to LF above |
| 161 | // otherwise it just inserts a LF |
| 162 | lex->chr2 = '\n'; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 167 | STATIC void indent_push(mp_lexer_t *lex, mp_uint_t indent) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 168 | if (lex->num_indent_level >= lex->alloc_indent_level) { |
Damien George | e1199ec | 2014-05-10 17:48:01 +0100 | [diff] [blame] | 169 | // TODO use m_renew_maybe and somehow indicate an error if it fails... probably by using MP_TOKEN_MEMORY_ERROR |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 170 | lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level, lex->alloc_indent_level + MICROPY_ALLOC_LEXEL_INDENT_INC); |
| 171 | lex->alloc_indent_level += MICROPY_ALLOC_LEXEL_INDENT_INC; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 172 | } |
| 173 | lex->indent_level[lex->num_indent_level++] = indent; |
| 174 | } |
| 175 | |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 176 | STATIC mp_uint_t indent_top(mp_lexer_t *lex) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 177 | return lex->indent_level[lex->num_indent_level - 1]; |
| 178 | } |
| 179 | |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 180 | STATIC void indent_pop(mp_lexer_t *lex) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 181 | lex->num_indent_level -= 1; |
| 182 | } |
| 183 | |
| 184 | // some tricky operator encoding: |
| 185 | // <op> = begin with <op>, if this opchar matches then begin here |
| 186 | // e<op> = end with <op>, if this opchar matches then end |
| 187 | // E<op> = mandatory end with <op>, this opchar must match, then end |
| 188 | // c<op> = continue with <op>, if this opchar matches then continue matching |
| 189 | // this means if the start of two ops are the same then they are equal til the last char |
| 190 | |
Damien George | 3ff16ff | 2016-05-20 12:38:15 +0100 | [diff] [blame] | 191 | STATIC const char *const tok_enc = |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 192 | "()[]{},:;@~" // singles |
| 193 | "<e=c<e=" // < <= << <<= |
| 194 | ">e=c>e=" // > >= >> >>= |
| 195 | "*e=c*e=" // * *= ** **= |
| 196 | "+e=" // + += |
| 197 | "-e=e>" // - -= -> |
| 198 | "&e=" // & &= |
| 199 | "|e=" // | |= |
| 200 | "/e=c/e=" // / /= // //= |
| 201 | "%e=" // % %= |
| 202 | "^e=" // ^ ^= |
| 203 | "=e=" // = == |
Damien George | 2e9eb2d | 2014-04-10 12:19:33 +0100 | [diff] [blame] | 204 | "!E="; // != |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 205 | |
| 206 | // TODO static assert that number of tokens is less than 256 so we can safely make this table with byte sized entries |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 207 | STATIC const uint8_t tok_enc_kind[] = { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 208 | MP_TOKEN_DEL_PAREN_OPEN, MP_TOKEN_DEL_PAREN_CLOSE, |
| 209 | MP_TOKEN_DEL_BRACKET_OPEN, MP_TOKEN_DEL_BRACKET_CLOSE, |
| 210 | MP_TOKEN_DEL_BRACE_OPEN, MP_TOKEN_DEL_BRACE_CLOSE, |
| 211 | MP_TOKEN_DEL_COMMA, MP_TOKEN_DEL_COLON, MP_TOKEN_DEL_SEMICOLON, MP_TOKEN_DEL_AT, MP_TOKEN_OP_TILDE, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 212 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 213 | MP_TOKEN_OP_LESS, MP_TOKEN_OP_LESS_EQUAL, MP_TOKEN_OP_DBL_LESS, MP_TOKEN_DEL_DBL_LESS_EQUAL, |
| 214 | MP_TOKEN_OP_MORE, MP_TOKEN_OP_MORE_EQUAL, MP_TOKEN_OP_DBL_MORE, MP_TOKEN_DEL_DBL_MORE_EQUAL, |
| 215 | MP_TOKEN_OP_STAR, MP_TOKEN_DEL_STAR_EQUAL, MP_TOKEN_OP_DBL_STAR, MP_TOKEN_DEL_DBL_STAR_EQUAL, |
| 216 | MP_TOKEN_OP_PLUS, MP_TOKEN_DEL_PLUS_EQUAL, |
| 217 | MP_TOKEN_OP_MINUS, MP_TOKEN_DEL_MINUS_EQUAL, MP_TOKEN_DEL_MINUS_MORE, |
| 218 | MP_TOKEN_OP_AMPERSAND, MP_TOKEN_DEL_AMPERSAND_EQUAL, |
| 219 | MP_TOKEN_OP_PIPE, MP_TOKEN_DEL_PIPE_EQUAL, |
| 220 | MP_TOKEN_OP_SLASH, MP_TOKEN_DEL_SLASH_EQUAL, MP_TOKEN_OP_DBL_SLASH, MP_TOKEN_DEL_DBL_SLASH_EQUAL, |
| 221 | MP_TOKEN_OP_PERCENT, MP_TOKEN_DEL_PERCENT_EQUAL, |
| 222 | MP_TOKEN_OP_CARET, MP_TOKEN_DEL_CARET_EQUAL, |
| 223 | MP_TOKEN_DEL_EQUAL, MP_TOKEN_OP_DBL_EQUAL, |
| 224 | MP_TOKEN_OP_NOT_EQUAL, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | // must have the same order as enum in lexer.h |
Damien George | 3ff16ff | 2016-05-20 12:38:15 +0100 | [diff] [blame] | 228 | STATIC const char *const tok_kw[] = { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 229 | "False", |
| 230 | "None", |
| 231 | "True", |
| 232 | "and", |
| 233 | "as", |
| 234 | "assert", |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 235 | #if MICROPY_PY_ASYNC_AWAIT |
| 236 | "async", |
| 237 | "await", |
| 238 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 239 | "break", |
| 240 | "class", |
| 241 | "continue", |
| 242 | "def", |
| 243 | "del", |
| 244 | "elif", |
| 245 | "else", |
| 246 | "except", |
| 247 | "finally", |
| 248 | "for", |
| 249 | "from", |
| 250 | "global", |
| 251 | "if", |
| 252 | "import", |
| 253 | "in", |
| 254 | "is", |
| 255 | "lambda", |
| 256 | "nonlocal", |
| 257 | "not", |
| 258 | "or", |
| 259 | "pass", |
| 260 | "raise", |
| 261 | "return", |
| 262 | "try", |
| 263 | "while", |
| 264 | "with", |
| 265 | "yield", |
Damien George | 97f9a28 | 2014-05-12 23:07:34 +0100 | [diff] [blame] | 266 | "__debug__", |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 267 | }; |
| 268 | |
Paul Sokolovsky | 0b7184d | 2014-01-22 22:40:02 +0200 | [diff] [blame] | 269 | // This is called with CUR_CHAR() before first hex digit, and should return with |
| 270 | // it pointing to last hex digit |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 271 | // num_digits must be greater than zero |
| 272 | STATIC bool get_hex(mp_lexer_t *lex, mp_uint_t num_digits, mp_uint_t *result) { |
| 273 | mp_uint_t num = 0; |
Paul Sokolovsky | 0b7184d | 2014-01-22 22:40:02 +0200 | [diff] [blame] | 274 | while (num_digits-- != 0) { |
| 275 | next_char(lex); |
| 276 | unichar c = CUR_CHAR(lex); |
| 277 | if (!unichar_isxdigit(c)) { |
| 278 | return false; |
| 279 | } |
Dave Hylands | 3ad94d6 | 2015-05-18 14:41:25 -0700 | [diff] [blame] | 280 | num = (num << 4) + unichar_xdigit_value(c); |
Paul Sokolovsky | 0b7184d | 2014-01-22 22:40:02 +0200 | [diff] [blame] | 281 | } |
| 282 | *result = num; |
| 283 | return true; |
| 284 | } |
| 285 | |
Damien George | 98b3072 | 2017-02-17 10:56:06 +1100 | [diff] [blame^] | 286 | void mp_lexer_to_next(mp_lexer_t *lex) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 287 | // start new token text |
| 288 | vstr_reset(&lex->vstr); |
| 289 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 290 | // skip white space and comments |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 291 | bool had_physical_newline = false; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 292 | while (!is_end(lex)) { |
| 293 | if (is_physical_newline(lex)) { |
| 294 | had_physical_newline = true; |
| 295 | next_char(lex); |
| 296 | } else if (is_whitespace(lex)) { |
| 297 | next_char(lex); |
| 298 | } else if (is_char(lex, '#')) { |
| 299 | next_char(lex); |
| 300 | while (!is_end(lex) && !is_physical_newline(lex)) { |
| 301 | next_char(lex); |
| 302 | } |
| 303 | // had_physical_newline will be set on next loop |
| 304 | } else if (is_char(lex, '\\')) { |
| 305 | // backslash (outside string literals) must appear just before a physical newline |
| 306 | next_char(lex); |
| 307 | if (!is_physical_newline(lex)) { |
Damien George | 69a818d | 2014-01-12 13:55:24 +0000 | [diff] [blame] | 308 | // SyntaxError: unexpected character after line continuation character |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 309 | lex->tok_line = lex->line; |
| 310 | lex->tok_column = lex->column; |
| 311 | lex->tok_kind = MP_TOKEN_BAD_LINE_CONTINUATION; |
Damien George | 69a818d | 2014-01-12 13:55:24 +0000 | [diff] [blame] | 312 | return; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 313 | } else { |
| 314 | next_char(lex); |
| 315 | } |
| 316 | } else { |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 321 | // set token source information |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 322 | lex->tok_line = lex->line; |
| 323 | lex->tok_column = lex->column; |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 324 | |
Damien George | 98b3072 | 2017-02-17 10:56:06 +1100 | [diff] [blame^] | 325 | if (lex->emit_dent < 0) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 326 | lex->tok_kind = MP_TOKEN_DEDENT; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 327 | lex->emit_dent += 1; |
| 328 | |
| 329 | } else if (lex->emit_dent > 0) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 330 | lex->tok_kind = MP_TOKEN_INDENT; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 331 | lex->emit_dent -= 1; |
| 332 | |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 333 | } else if (had_physical_newline && lex->nested_bracket_level == 0) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 334 | lex->tok_kind = MP_TOKEN_NEWLINE; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 335 | |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 336 | mp_uint_t num_spaces = lex->column - 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 337 | if (num_spaces == indent_top(lex)) { |
| 338 | } else if (num_spaces > indent_top(lex)) { |
| 339 | indent_push(lex, num_spaces); |
| 340 | lex->emit_dent += 1; |
| 341 | } else { |
| 342 | while (num_spaces < indent_top(lex)) { |
| 343 | indent_pop(lex); |
| 344 | lex->emit_dent -= 1; |
| 345 | } |
| 346 | if (num_spaces != indent_top(lex)) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 347 | lex->tok_kind = MP_TOKEN_DEDENT_MISMATCH; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | } else if (is_end(lex)) { |
Damien George | 31101d9 | 2016-10-12 11:00:17 +1100 | [diff] [blame] | 352 | lex->tok_kind = MP_TOKEN_END; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 353 | |
| 354 | } else if (is_char_or(lex, '\'', '\"') |
| 355 | || (is_char_or3(lex, 'r', 'u', 'b') && is_char_following_or(lex, '\'', '\"')) |
| 356 | || ((is_char_and(lex, 'r', 'b') || is_char_and(lex, 'b', 'r')) && is_char_following_following_or(lex, '\'', '\"'))) { |
| 357 | // a string or bytes literal |
| 358 | |
| 359 | // parse type codes |
| 360 | bool is_raw = false; |
| 361 | bool is_bytes = false; |
| 362 | if (is_char(lex, 'u')) { |
| 363 | next_char(lex); |
| 364 | } else if (is_char(lex, 'b')) { |
| 365 | is_bytes = true; |
| 366 | next_char(lex); |
| 367 | if (is_char(lex, 'r')) { |
| 368 | is_raw = true; |
| 369 | next_char(lex); |
| 370 | } |
| 371 | } else if (is_char(lex, 'r')) { |
| 372 | is_raw = true; |
| 373 | next_char(lex); |
| 374 | if (is_char(lex, 'b')) { |
| 375 | is_bytes = true; |
| 376 | next_char(lex); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // set token kind |
| 381 | if (is_bytes) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 382 | lex->tok_kind = MP_TOKEN_BYTES; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 383 | } else { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 384 | lex->tok_kind = MP_TOKEN_STRING; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // get first quoting character |
| 388 | char quote_char = '\''; |
| 389 | if (is_char(lex, '\"')) { |
| 390 | quote_char = '\"'; |
| 391 | } |
| 392 | next_char(lex); |
| 393 | |
| 394 | // work out if it's a single or triple quoted literal |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 395 | mp_uint_t num_quotes; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 396 | if (is_char_and(lex, quote_char, quote_char)) { |
| 397 | // triple quotes |
| 398 | next_char(lex); |
| 399 | next_char(lex); |
| 400 | num_quotes = 3; |
| 401 | } else { |
| 402 | // single quotes |
| 403 | num_quotes = 1; |
| 404 | } |
| 405 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 406 | // parse the literal |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 407 | mp_uint_t n_closing = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 408 | while (!is_end(lex) && (num_quotes > 1 || !is_char(lex, '\n')) && n_closing < num_quotes) { |
| 409 | if (is_char(lex, quote_char)) { |
| 410 | n_closing += 1; |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 411 | vstr_add_char(&lex->vstr, CUR_CHAR(lex)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 412 | } else { |
| 413 | n_closing = 0; |
Damien George | a91f414 | 2014-04-10 11:30:55 +0100 | [diff] [blame] | 414 | if (is_char(lex, '\\')) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 415 | next_char(lex); |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 416 | unichar c = CUR_CHAR(lex); |
Damien George | a91f414 | 2014-04-10 11:30:55 +0100 | [diff] [blame] | 417 | if (is_raw) { |
| 418 | // raw strings allow escaping of quotes, but the backslash is also emitted |
| 419 | vstr_add_char(&lex->vstr, '\\'); |
| 420 | } else { |
| 421 | switch (c) { |
Damien George | b9c4783 | 2016-12-22 10:37:13 +1100 | [diff] [blame] | 422 | // note: "c" can never be MP_LEXER_EOF because next_char |
| 423 | // always inserts a newline at the end of the input stream |
Damien George | adccafb | 2016-12-22 10:32:06 +1100 | [diff] [blame] | 424 | case '\n': c = MP_LEXER_EOF; break; // backslash escape the newline, just ignore it |
Damien George | a91f414 | 2014-04-10 11:30:55 +0100 | [diff] [blame] | 425 | case '\\': break; |
| 426 | case '\'': break; |
| 427 | case '"': break; |
| 428 | case 'a': c = 0x07; break; |
| 429 | case 'b': c = 0x08; break; |
| 430 | case 't': c = 0x09; break; |
| 431 | case 'n': c = 0x0a; break; |
| 432 | case 'v': c = 0x0b; break; |
| 433 | case 'f': c = 0x0c; break; |
| 434 | case 'r': c = 0x0d; break; |
Chris Angelico | 2ba2299 | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 435 | case 'u': |
| 436 | case 'U': |
| 437 | if (is_bytes) { |
| 438 | // b'\u1234' == b'\\u1234' |
| 439 | vstr_add_char(&lex->vstr, '\\'); |
| 440 | break; |
| 441 | } |
| 442 | // Otherwise fall through. |
Damien George | a91f414 | 2014-04-10 11:30:55 +0100 | [diff] [blame] | 443 | case 'x': |
| 444 | { |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 445 | mp_uint_t num = 0; |
Chris Angelico | 2ba2299 | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 446 | if (!get_hex(lex, (c == 'x' ? 2 : c == 'u' ? 4 : 8), &num)) { |
Damien George | d241c2a | 2015-07-23 23:20:37 +0100 | [diff] [blame] | 447 | // not enough hex chars for escape sequence |
| 448 | lex->tok_kind = MP_TOKEN_INVALID; |
Paul Sokolovsky | 0b7184d | 2014-01-22 22:40:02 +0200 | [diff] [blame] | 449 | } |
| 450 | c = num; |
Damien George | a91f414 | 2014-04-10 11:30:55 +0100 | [diff] [blame] | 451 | break; |
Paul Sokolovsky | 0b7184d | 2014-01-22 22:40:02 +0200 | [diff] [blame] | 452 | } |
Chris Angelico | 2ba2299 | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 453 | case 'N': |
| 454 | // Supporting '\N{LATIN SMALL LETTER A}' == 'a' would require keeping the |
| 455 | // entire Unicode name table in the core. As of Unicode 6.3.0, that's nearly |
| 456 | // 3MB of text; even gzip-compressed and with minimal structure, it'll take |
| 457 | // roughly half a meg of storage. This form of Unicode escape may be added |
| 458 | // later on, but it's definitely not a priority right now. -- CJA 20140607 |
Damien George | 081f932 | 2015-09-07 17:08:49 +0100 | [diff] [blame] | 459 | mp_not_implemented("unicode name escapes"); |
Chris Angelico | 2ba2299 | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 460 | break; |
Damien George | a91f414 | 2014-04-10 11:30:55 +0100 | [diff] [blame] | 461 | default: |
| 462 | if (c >= '0' && c <= '7') { |
| 463 | // Octal sequence, 1-3 chars |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 464 | mp_uint_t digits = 3; |
| 465 | mp_uint_t num = c - '0'; |
Damien George | a91f414 | 2014-04-10 11:30:55 +0100 | [diff] [blame] | 466 | while (is_following_odigit(lex) && --digits != 0) { |
| 467 | next_char(lex); |
| 468 | num = num * 8 + (CUR_CHAR(lex) - '0'); |
| 469 | } |
| 470 | c = num; |
| 471 | } else { |
| 472 | // unrecognised escape character; CPython lets this through verbatim as '\' and then the character |
| 473 | vstr_add_char(&lex->vstr, '\\'); |
| 474 | } |
| 475 | break; |
| 476 | } |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 477 | } |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 478 | if (c != MP_LEXER_EOF) { |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame] | 479 | if (MICROPY_PY_BUILTINS_STR_UNICODE_DYNAMIC) { |
| 480 | if (c < 0x110000 && !is_bytes) { |
| 481 | vstr_add_char(&lex->vstr, c); |
| 482 | } else if (c < 0x100 && is_bytes) { |
| 483 | vstr_add_byte(&lex->vstr, c); |
| 484 | } else { |
| 485 | // unicode character out of range |
| 486 | // this raises a generic SyntaxError; could provide more info |
| 487 | lex->tok_kind = MP_TOKEN_INVALID; |
| 488 | } |
| 489 | } else { |
| 490 | // without unicode everything is just added as an 8-bit byte |
| 491 | if (c < 0x100) { |
| 492 | vstr_add_byte(&lex->vstr, c); |
| 493 | } else { |
| 494 | // 8-bit character out of range |
| 495 | // this raises a generic SyntaxError; could provide more info |
| 496 | lex->tok_kind = MP_TOKEN_INVALID; |
| 497 | } |
Chris Angelico | 2ba2299 | 2014-06-04 05:28:12 +1000 | [diff] [blame] | 498 | } |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 499 | } |
| 500 | } else { |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 501 | // Add the "character" as a byte so that we remain 8-bit clean. |
| 502 | // This way, strings are parsed correctly whether or not they contain utf-8 chars. |
| 503 | vstr_add_byte(&lex->vstr, CUR_CHAR(lex)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | next_char(lex); |
| 507 | } |
| 508 | |
| 509 | // check we got the required end quotes |
| 510 | if (n_closing < num_quotes) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 511 | lex->tok_kind = MP_TOKEN_LONELY_STRING_OPEN; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 514 | // cut off the end quotes from the token text |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 515 | vstr_cut_tail_bytes(&lex->vstr, n_closing); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 516 | |
| 517 | } else if (is_head_of_identifier(lex)) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 518 | lex->tok_kind = MP_TOKEN_NAME; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 519 | |
Damien George | 7ed58cb | 2015-06-09 10:58:07 +0000 | [diff] [blame] | 520 | // get first char (add as byte to remain 8-bit clean and support utf-8) |
| 521 | vstr_add_byte(&lex->vstr, CUR_CHAR(lex)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 522 | next_char(lex); |
| 523 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 524 | // get tail chars |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 525 | while (!is_end(lex) && is_tail_of_identifier(lex)) { |
Damien George | 7ed58cb | 2015-06-09 10:58:07 +0000 | [diff] [blame] | 526 | vstr_add_byte(&lex->vstr, CUR_CHAR(lex)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 527 | next_char(lex); |
| 528 | } |
| 529 | |
| 530 | } else if (is_digit(lex) || (is_char(lex, '.') && is_following_digit(lex))) { |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 531 | bool forced_integer = false; |
| 532 | if (is_char(lex, '.')) { |
| 533 | lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG; |
| 534 | } else { |
| 535 | lex->tok_kind = MP_TOKEN_INTEGER; |
Damien George | 2b00047 | 2015-09-07 17:33:44 +0100 | [diff] [blame] | 536 | if (is_char(lex, '0') && is_following_base_char(lex)) { |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 537 | forced_integer = true; |
| 538 | } |
| 539 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 540 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 541 | // get first char |
| 542 | vstr_add_char(&lex->vstr, CUR_CHAR(lex)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 543 | next_char(lex); |
| 544 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 545 | // get tail chars |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 546 | while (!is_end(lex)) { |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 547 | if (!forced_integer && is_char_or(lex, 'e', 'E')) { |
| 548 | lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG; |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 549 | vstr_add_char(&lex->vstr, 'e'); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 550 | next_char(lex); |
| 551 | if (is_char(lex, '+') || is_char(lex, '-')) { |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 552 | vstr_add_char(&lex->vstr, CUR_CHAR(lex)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 553 | next_char(lex); |
| 554 | } |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 555 | } else if (is_letter(lex) || is_digit(lex) || is_char(lex, '.')) { |
| 556 | if (is_char_or3(lex, '.', 'j', 'J')) { |
| 557 | lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG; |
| 558 | } |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 559 | vstr_add_char(&lex->vstr, CUR_CHAR(lex)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 560 | next_char(lex); |
| 561 | } else { |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | |
Damien George | 2e9eb2d | 2014-04-10 12:19:33 +0100 | [diff] [blame] | 566 | } else if (is_char(lex, '.')) { |
| 567 | // special handling for . and ... operators, because .. is not a valid operator |
| 568 | |
| 569 | // get first char |
Damien George | 2e9eb2d | 2014-04-10 12:19:33 +0100 | [diff] [blame] | 570 | next_char(lex); |
| 571 | |
| 572 | if (is_char_and(lex, '.', '.')) { |
Damien George | 2e9eb2d | 2014-04-10 12:19:33 +0100 | [diff] [blame] | 573 | next_char(lex); |
| 574 | next_char(lex); |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 575 | lex->tok_kind = MP_TOKEN_ELLIPSIS; |
Damien George | 2e9eb2d | 2014-04-10 12:19:33 +0100 | [diff] [blame] | 576 | } else { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 577 | lex->tok_kind = MP_TOKEN_DEL_PERIOD; |
Damien George | 2e9eb2d | 2014-04-10 12:19:33 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 580 | } else { |
| 581 | // search for encoded delimiter or operator |
| 582 | |
| 583 | const char *t = tok_enc; |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 584 | mp_uint_t tok_enc_index = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 585 | for (; *t != 0 && !is_char(lex, *t); t += 1) { |
| 586 | if (*t == 'e' || *t == 'c') { |
| 587 | t += 1; |
| 588 | } else if (*t == 'E') { |
| 589 | tok_enc_index -= 1; |
| 590 | t += 1; |
| 591 | } |
| 592 | tok_enc_index += 1; |
| 593 | } |
| 594 | |
| 595 | next_char(lex); |
| 596 | |
| 597 | if (*t == 0) { |
| 598 | // didn't match any delimiter or operator characters |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 599 | lex->tok_kind = MP_TOKEN_INVALID; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 600 | |
| 601 | } else { |
| 602 | // matched a delimiter or operator character |
| 603 | |
| 604 | // get the maximum characters for a valid token |
| 605 | t += 1; |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 606 | mp_uint_t t_index = tok_enc_index; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 607 | for (;;) { |
| 608 | for (; *t == 'e'; t += 1) { |
| 609 | t += 1; |
| 610 | t_index += 1; |
| 611 | if (is_char(lex, *t)) { |
| 612 | next_char(lex); |
| 613 | tok_enc_index = t_index; |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (*t == 'E') { |
| 619 | t += 1; |
| 620 | if (is_char(lex, *t)) { |
| 621 | next_char(lex); |
| 622 | tok_enc_index = t_index; |
| 623 | } else { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 624 | lex->tok_kind = MP_TOKEN_INVALID; |
Damien George | 2e9eb2d | 2014-04-10 12:19:33 +0100 | [diff] [blame] | 625 | goto tok_enc_no_match; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 626 | } |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | if (*t == 'c') { |
| 631 | t += 1; |
| 632 | t_index += 1; |
| 633 | if (is_char(lex, *t)) { |
| 634 | next_char(lex); |
| 635 | tok_enc_index = t_index; |
| 636 | t += 1; |
| 637 | } else { |
| 638 | break; |
| 639 | } |
| 640 | } else { |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // set token kind |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 646 | lex->tok_kind = tok_enc_kind[tok_enc_index]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 647 | |
Damien George | 2e9eb2d | 2014-04-10 12:19:33 +0100 | [diff] [blame] | 648 | tok_enc_no_match: |
| 649 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 650 | // compute bracket level for implicit line joining |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 651 | if (lex->tok_kind == MP_TOKEN_DEL_PAREN_OPEN || lex->tok_kind == MP_TOKEN_DEL_BRACKET_OPEN || lex->tok_kind == MP_TOKEN_DEL_BRACE_OPEN) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 652 | lex->nested_bracket_level += 1; |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 653 | } else if (lex->tok_kind == MP_TOKEN_DEL_PAREN_CLOSE || lex->tok_kind == MP_TOKEN_DEL_BRACKET_CLOSE || lex->tok_kind == MP_TOKEN_DEL_BRACE_CLOSE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 654 | lex->nested_bracket_level -= 1; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 659 | // check for keywords |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 660 | if (lex->tok_kind == MP_TOKEN_NAME) { |
Damien George | 97f9a28 | 2014-05-12 23:07:34 +0100 | [diff] [blame] | 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 |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 665 | //for (mp_int_t i = 0; tok_kw[i] != NULL; i++) { |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 666 | for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 667 | if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) { |
Emmanuel Blot | f6932d6 | 2014-06-19 18:54:34 +0200 | [diff] [blame] | 668 | if (i == MP_ARRAY_SIZE(tok_kw) - 1) { |
| 669 | // tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 670 | lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE); |
Damien George | 97f9a28 | 2014-05-12 23:07:34 +0100 | [diff] [blame] | 671 | } else { |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 672 | lex->tok_kind = MP_TOKEN_KW_FALSE + i; |
Damien George | 97f9a28 | 2014-05-12 23:07:34 +0100 | [diff] [blame] | 673 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 674 | break; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 680 | mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) { |
Damien George | 9bf5f28 | 2014-10-09 16:53:37 +0100 | [diff] [blame] | 681 | mp_lexer_t *lex = m_new_obj_maybe(mp_lexer_t); |
Damien George | e1199ec | 2014-05-10 17:48:01 +0100 | [diff] [blame] | 682 | |
| 683 | // check for memory allocation error |
| 684 | if (lex == NULL) { |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 685 | reader.close(reader.data); |
Damien George | e1199ec | 2014-05-10 17:48:01 +0100 | [diff] [blame] | 686 | return NULL; |
| 687 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 688 | |
Damien George | b829b5c | 2014-01-25 13:51:19 +0000 | [diff] [blame] | 689 | lex->source_name = src_name; |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 690 | lex->reader = reader; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 691 | lex->line = 1; |
| 692 | lex->column = 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 693 | lex->emit_dent = 0; |
| 694 | lex->nested_bracket_level = 0; |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 695 | lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 696 | lex->num_indent_level = 1; |
Damien George | e1199ec | 2014-05-10 17:48:01 +0100 | [diff] [blame] | 697 | lex->indent_level = m_new_maybe(uint16_t, lex->alloc_indent_level); |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 698 | vstr_init(&lex->vstr, 32); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 699 | |
Damien George | e1199ec | 2014-05-10 17:48:01 +0100 | [diff] [blame] | 700 | // check for memory allocation error |
Damien George | 98b3072 | 2017-02-17 10:56:06 +1100 | [diff] [blame^] | 701 | // note: vstr_init above may fail on malloc, but so may mp_lexer_to_next below |
Damien George | 5da0d29 | 2016-09-19 11:17:02 +1000 | [diff] [blame] | 702 | if (lex->indent_level == NULL) { |
Damien George | e1199ec | 2014-05-10 17:48:01 +0100 | [diff] [blame] | 703 | mp_lexer_free(lex); |
| 704 | return NULL; |
| 705 | } |
| 706 | |
| 707 | // store sentinel for first indentation level |
| 708 | lex->indent_level[0] = 0; |
| 709 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 710 | // preload characters |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 711 | lex->chr0 = reader.readbyte(reader.data); |
| 712 | lex->chr1 = reader.readbyte(reader.data); |
| 713 | lex->chr2 = reader.readbyte(reader.data); |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 714 | |
| 715 | // if input stream is 0, 1 or 2 characters long and doesn't end in a newline, then insert a newline at the end |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 716 | if (lex->chr0 == MP_LEXER_EOF) { |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 717 | lex->chr0 = '\n'; |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 718 | } else if (lex->chr1 == MP_LEXER_EOF) { |
Damien George | 32bade1 | 2015-01-30 00:27:46 +0000 | [diff] [blame] | 719 | if (lex->chr0 == '\r') { |
| 720 | lex->chr0 = '\n'; |
| 721 | } else if (lex->chr0 != '\n') { |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 722 | lex->chr1 = '\n'; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 723 | } |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 724 | } else if (lex->chr2 == MP_LEXER_EOF) { |
Damien George | 32bade1 | 2015-01-30 00:27:46 +0000 | [diff] [blame] | 725 | if (lex->chr1 == '\r') { |
| 726 | lex->chr1 = '\n'; |
| 727 | } else if (lex->chr1 != '\n') { |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 728 | lex->chr2 = '\n'; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 729 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 732 | // preload first token |
Damien George | 98b3072 | 2017-02-17 10:56:06 +1100 | [diff] [blame^] | 733 | mp_lexer_to_next(lex); |
| 734 | |
| 735 | // Check that the first token is in the first column. If it's not then we |
| 736 | // convert the token kind to INDENT so that the parser gives a syntax error. |
| 737 | if (lex->tok_column != 1) { |
| 738 | lex->tok_kind = MP_TOKEN_INDENT; |
| 739 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 740 | |
| 741 | return lex; |
| 742 | } |
| 743 | |
Damien George | 511c083 | 2016-11-16 16:22:08 +1100 | [diff] [blame] | 744 | mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) { |
| 745 | mp_reader_t reader; |
| 746 | if (!mp_reader_new_mem(&reader, (const byte*)str, len, free_len)) { |
| 747 | return NULL; |
| 748 | } |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 749 | return mp_lexer_new(src_name, reader); |
Damien George | 511c083 | 2016-11-16 16:22:08 +1100 | [diff] [blame] | 750 | } |
| 751 | |
Damien George | 8beba73 | 2017-01-29 15:16:51 +1100 | [diff] [blame] | 752 | #if MICROPY_READER_POSIX || MICROPY_READER_VFS |
Damien George | e5ef15a | 2016-11-16 16:25:06 +1100 | [diff] [blame] | 753 | |
| 754 | mp_lexer_t *mp_lexer_new_from_file(const char *filename) { |
| 755 | mp_reader_t reader; |
| 756 | int ret = mp_reader_new_file(&reader, filename); |
| 757 | if (ret != 0) { |
| 758 | return NULL; |
| 759 | } |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 760 | return mp_lexer_new(qstr_from_str(filename), reader); |
Damien George | e5ef15a | 2016-11-16 16:25:06 +1100 | [diff] [blame] | 761 | } |
| 762 | |
Damien George | 66d955c | 2016-11-16 18:12:55 +1100 | [diff] [blame] | 763 | #if MICROPY_HELPER_LEXER_UNIX |
| 764 | |
| 765 | mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd) { |
| 766 | mp_reader_t reader; |
| 767 | int ret = mp_reader_new_file_from_fd(&reader, fd, close_fd); |
| 768 | if (ret != 0) { |
| 769 | return NULL; |
| 770 | } |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 771 | return mp_lexer_new(filename, reader); |
Damien George | 66d955c | 2016-11-16 18:12:55 +1100 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | #endif |
| 775 | |
Damien George | e5ef15a | 2016-11-16 16:25:06 +1100 | [diff] [blame] | 776 | #endif |
| 777 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 778 | void mp_lexer_free(mp_lexer_t *lex) { |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 779 | if (lex) { |
Damien George | 5bdf165 | 2016-11-16 18:27:20 +1100 | [diff] [blame] | 780 | lex->reader.close(lex->reader.data); |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 781 | vstr_clear(&lex->vstr); |
Paul Sokolovsky | 624ed5d | 2014-01-23 22:25:57 +0200 | [diff] [blame] | 782 | m_del(uint16_t, lex->indent_level, lex->alloc_indent_level); |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 783 | m_del_obj(mp_lexer_t, lex); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 784 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 785 | } |
| 786 | |
Damien George | c305ae3 | 2016-12-22 10:49:54 +1100 | [diff] [blame] | 787 | #if 0 |
| 788 | // This function is used to print the current token and should only be |
| 789 | // needed to debug the lexer, so it's not available via a config option. |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 790 | void mp_lexer_show_token(const mp_lexer_t *lex) { |
Damien George | 451a087 | 2014-12-05 22:50:16 +0000 | [diff] [blame] | 791 | printf("(" UINT_FMT ":" UINT_FMT ") kind:%u str:%p len:%zu", lex->tok_line, lex->tok_column, lex->tok_kind, lex->vstr.buf, lex->vstr.len); |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 792 | if (lex->vstr.len > 0) { |
| 793 | const byte *i = (const byte *)lex->vstr.buf; |
| 794 | const byte *j = (const byte *)i + lex->vstr.len; |
| 795 | printf(" "); |
| 796 | while (i < j) { |
| 797 | unichar c = utf8_get_char(i); |
| 798 | i = utf8_next_char(i); |
| 799 | if (unichar_isprint(c)) { |
Damien George | 7f19a39 | 2015-06-22 17:40:12 +0100 | [diff] [blame] | 800 | printf("%c", (int)c); |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 801 | } else { |
| 802 | printf("?"); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | printf("\n"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 807 | } |
Damien George | a4c52c5 | 2014-12-05 19:35:18 +0000 | [diff] [blame] | 808 | #endif |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 809 | |
| 810 | #endif // MICROPY_ENABLE_COMPILER |