blob: abc1f3ebbb0e6f231ddc354a8111fb344c277618 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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
Damien429d7192013-10-04 19:53:11 +010027#include <stdio.h>
Damien Georgeae436792017-02-17 11:10:35 +110028#include <string.h>
Damien429d7192013-10-04 19:53:11 +010029#include <assert.h>
30
Damien Georgeb4b10fd2015-01-01 23:30:53 +000031#include "py/mpstate.h"
Damien George511c0832016-11-16 16:22:08 +110032#include "py/reader.h"
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/lexer.h"
Damien George081f9322015-09-07 17:08:49 +010034#include "py/runtime.h"
Damien429d7192013-10-04 19:53:11 +010035
Damien Georgedd5353a2015-12-18 12:35:44 +000036#if MICROPY_ENABLE_COMPILER
37
Damien429d7192013-10-04 19:53:11 +010038#define TAB_SIZE (8)
Damien429d7192013-10-04 19:53:11 +010039
Damien92c06562013-10-22 22:32:27 +010040// TODO seems that CPython allows NULL byte in the input stream
41// don't know if that's intentional or not, but we don't allow it
42
Damien George5bdf1652016-11-16 18:27:20 +110043#define MP_LEXER_EOF ((unichar)MP_READER_EOF)
Damiena5185f42013-10-20 14:41:27 +010044#define CUR_CHAR(lex) ((lex)->chr0)
45
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020046STATIC bool is_end(mp_lexer_t *lex) {
Damien George94fbe972014-07-30 11:46:05 +010047 return lex->chr0 == MP_LEXER_EOF;
Damien429d7192013-10-04 19:53:11 +010048}
49
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020050STATIC bool is_physical_newline(mp_lexer_t *lex) {
Damien George32bade12015-01-30 00:27:46 +000051 return lex->chr0 == '\n';
Damien429d7192013-10-04 19:53:11 +010052}
53
Damien George2e2e4042015-03-19 00:21:29 +000054STATIC bool is_char(mp_lexer_t *lex, byte c) {
Damien429d7192013-10-04 19:53:11 +010055 return lex->chr0 == c;
56}
57
Damien George2e2e4042015-03-19 00:21:29 +000058STATIC bool is_char_or(mp_lexer_t *lex, byte c1, byte c2) {
Damien429d7192013-10-04 19:53:11 +010059 return lex->chr0 == c1 || lex->chr0 == c2;
60}
61
Damien George2e2e4042015-03-19 00:21:29 +000062STATIC bool is_char_or3(mp_lexer_t *lex, byte c1, byte c2, byte c3) {
Damien429d7192013-10-04 19:53:11 +010063 return lex->chr0 == c1 || lex->chr0 == c2 || lex->chr0 == c3;
64}
65
Damien George2e2e4042015-03-19 00:21:29 +000066STATIC bool is_char_following(mp_lexer_t *lex, byte c) {
Damien429d7192013-10-04 19:53:11 +010067 return lex->chr1 == c;
68}
Damien429d7192013-10-04 19:53:11 +010069
Damien George2e2e4042015-03-19 00:21:29 +000070STATIC bool is_char_following_or(mp_lexer_t *lex, byte c1, byte c2) {
Damien429d7192013-10-04 19:53:11 +010071 return lex->chr1 == c1 || lex->chr1 == c2;
72}
73
Damien George2e2e4042015-03-19 00:21:29 +000074STATIC bool is_char_following_following_or(mp_lexer_t *lex, byte c1, byte c2) {
Damien429d7192013-10-04 19:53:11 +010075 return lex->chr2 == c1 || lex->chr2 == c2;
76}
77
Damien George2e2e4042015-03-19 00:21:29 +000078STATIC bool is_char_and(mp_lexer_t *lex, byte c1, byte c2) {
Damien429d7192013-10-04 19:53:11 +010079 return lex->chr0 == c1 && lex->chr1 == c2;
80}
81
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020082STATIC bool is_whitespace(mp_lexer_t *lex) {
Damien George8cc96a32013-12-30 18:23:50 +000083 return unichar_isspace(lex->chr0);
Damien429d7192013-10-04 19:53:11 +010084}
85
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020086STATIC bool is_letter(mp_lexer_t *lex) {
Damien George8cc96a32013-12-30 18:23:50 +000087 return unichar_isalpha(lex->chr0);
Damien429d7192013-10-04 19:53:11 +010088}
89
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020090STATIC bool is_digit(mp_lexer_t *lex) {
Damien George8cc96a32013-12-30 18:23:50 +000091 return unichar_isdigit(lex->chr0);
Damien429d7192013-10-04 19:53:11 +010092}
93
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020094STATIC bool is_following_digit(mp_lexer_t *lex) {
Damien George8cc96a32013-12-30 18:23:50 +000095 return unichar_isdigit(lex->chr1);
Damien429d7192013-10-04 19:53:11 +010096}
97
Damien George2b000472015-09-07 17:33:44 +010098STATIC bool is_following_base_char(mp_lexer_t *lex) {
99 const unichar chr1 = lex->chr1 | 0x20;
100 return chr1 == 'b' || chr1 == 'o' || chr1 == 'x';
Damien George7d414a12015-02-08 01:57:40 +0000101}
102
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200103STATIC bool is_following_odigit(mp_lexer_t *lex) {
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200104 return lex->chr1 >= '0' && lex->chr1 <= '7';
105}
106
Damien George534b7c32017-02-17 12:12:40 +1100107STATIC bool is_string_or_bytes(mp_lexer_t *lex) {
108 return is_char_or(lex, '\'', '\"')
109 || (is_char_or3(lex, 'r', 'u', 'b') && is_char_following_or(lex, '\'', '\"'))
110 || ((is_char_and(lex, 'r', 'b') || is_char_and(lex, 'b', 'r'))
111 && is_char_following_following_or(lex, '\'', '\"'));
112}
113
Damien George7ed58cb2015-06-09 10:58:07 +0000114// to easily parse utf-8 identifiers we allow any raw byte with high bit set
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200115STATIC bool is_head_of_identifier(mp_lexer_t *lex) {
Damien George7ed58cb2015-06-09 10:58:07 +0000116 return is_letter(lex) || lex->chr0 == '_' || lex->chr0 >= 0x80;
Damien429d7192013-10-04 19:53:11 +0100117}
118
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200119STATIC bool is_tail_of_identifier(mp_lexer_t *lex) {
Damien429d7192013-10-04 19:53:11 +0100120 return is_head_of_identifier(lex) || is_digit(lex);
121}
122
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200123STATIC void next_char(mp_lexer_t *lex) {
Damien429d7192013-10-04 19:53:11 +0100124 if (lex->chr0 == '\n') {
Damien George32bade12015-01-30 00:27:46 +0000125 // a new line
Damien429d7192013-10-04 19:53:11 +0100126 ++lex->line;
127 lex->column = 1;
Damien429d7192013-10-04 19:53:11 +0100128 } else if (lex->chr0 == '\t') {
129 // a tab
130 lex->column = (((lex->column - 1 + TAB_SIZE) / TAB_SIZE) * TAB_SIZE) + 1;
131 } else {
132 // a character worth one column
133 ++lex->column;
134 }
135
Damien George32bade12015-01-30 00:27:46 +0000136 lex->chr0 = lex->chr1;
137 lex->chr1 = lex->chr2;
Damien George5bdf1652016-11-16 18:27:20 +1100138 lex->chr2 = lex->reader.readbyte(lex->reader.data);
Damien George32bade12015-01-30 00:27:46 +0000139
Tom Collins6f564122017-05-09 13:19:46 -0700140 if (lex->chr1 == '\r') {
Damien George32bade12015-01-30 00:27:46 +0000141 // CR is a new line, converted to LF
Tom Collins6f564122017-05-09 13:19:46 -0700142 lex->chr1 = '\n';
143 if (lex->chr2 == '\n') {
144 // CR LF is a single new line, throw out the extra LF
Damien George5bdf1652016-11-16 18:27:20 +1100145 lex->chr2 = lex->reader.readbyte(lex->reader.data);
Damien George32bade12015-01-30 00:27:46 +0000146 }
147 }
148
Tom Collins6f564122017-05-09 13:19:46 -0700149 // check if we need to insert a newline at end of file
150 if (lex->chr2 == MP_LEXER_EOF && lex->chr1 != MP_LEXER_EOF && lex->chr1 != '\n') {
151 lex->chr2 = '\n';
Damien429d7192013-10-04 19:53:11 +0100152 }
153}
154
Damien George5124a942017-02-17 12:44:24 +1100155STATIC void indent_push(mp_lexer_t *lex, size_t indent) {
Damien429d7192013-10-04 19:53:11 +0100156 if (lex->num_indent_level >= lex->alloc_indent_level) {
Damien George58ebde42014-05-21 20:32:59 +0100157 lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level, lex->alloc_indent_level + MICROPY_ALLOC_LEXEL_INDENT_INC);
158 lex->alloc_indent_level += MICROPY_ALLOC_LEXEL_INDENT_INC;
Damien429d7192013-10-04 19:53:11 +0100159 }
160 lex->indent_level[lex->num_indent_level++] = indent;
161}
162
Damien George5124a942017-02-17 12:44:24 +1100163STATIC size_t indent_top(mp_lexer_t *lex) {
Damien429d7192013-10-04 19:53:11 +0100164 return lex->indent_level[lex->num_indent_level - 1];
165}
166
Damien Georgea4c52c52014-12-05 19:35:18 +0000167STATIC void indent_pop(mp_lexer_t *lex) {
Damien429d7192013-10-04 19:53:11 +0100168 lex->num_indent_level -= 1;
169}
170
171// some tricky operator encoding:
172// <op> = begin with <op>, if this opchar matches then begin here
173// e<op> = end with <op>, if this opchar matches then end
Damien429d7192013-10-04 19:53:11 +0100174// c<op> = continue with <op>, if this opchar matches then continue matching
175// this means if the start of two ops are the same then they are equal til the last char
176
Damien George3ff16ff2016-05-20 12:38:15 +0100177STATIC const char *const tok_enc =
Damien429d7192013-10-04 19:53:11 +0100178 "()[]{},:;@~" // singles
179 "<e=c<e=" // < <= << <<=
180 ">e=c>e=" // > >= >> >>=
181 "*e=c*e=" // * *= ** **=
182 "+e=" // + +=
183 "-e=e>" // - -= ->
184 "&e=" // & &=
185 "|e=" // | |=
186 "/e=c/e=" // / /= // //=
187 "%e=" // % %=
188 "^e=" // ^ ^=
189 "=e=" // = ==
Damien George5010d192017-03-29 10:55:36 +1100190 "!."; // start of special cases: != . ...
Damien429d7192013-10-04 19:53:11 +0100191
192// TODO static assert that number of tokens is less than 256 so we can safely make this table with byte sized entries
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200193STATIC const uint8_t tok_enc_kind[] = {
Damiend99b0522013-12-21 18:17:45 +0000194 MP_TOKEN_DEL_PAREN_OPEN, MP_TOKEN_DEL_PAREN_CLOSE,
195 MP_TOKEN_DEL_BRACKET_OPEN, MP_TOKEN_DEL_BRACKET_CLOSE,
196 MP_TOKEN_DEL_BRACE_OPEN, MP_TOKEN_DEL_BRACE_CLOSE,
197 MP_TOKEN_DEL_COMMA, MP_TOKEN_DEL_COLON, MP_TOKEN_DEL_SEMICOLON, MP_TOKEN_DEL_AT, MP_TOKEN_OP_TILDE,
Damien429d7192013-10-04 19:53:11 +0100198
Damiend99b0522013-12-21 18:17:45 +0000199 MP_TOKEN_OP_LESS, MP_TOKEN_OP_LESS_EQUAL, MP_TOKEN_OP_DBL_LESS, MP_TOKEN_DEL_DBL_LESS_EQUAL,
200 MP_TOKEN_OP_MORE, MP_TOKEN_OP_MORE_EQUAL, MP_TOKEN_OP_DBL_MORE, MP_TOKEN_DEL_DBL_MORE_EQUAL,
201 MP_TOKEN_OP_STAR, MP_TOKEN_DEL_STAR_EQUAL, MP_TOKEN_OP_DBL_STAR, MP_TOKEN_DEL_DBL_STAR_EQUAL,
202 MP_TOKEN_OP_PLUS, MP_TOKEN_DEL_PLUS_EQUAL,
203 MP_TOKEN_OP_MINUS, MP_TOKEN_DEL_MINUS_EQUAL, MP_TOKEN_DEL_MINUS_MORE,
204 MP_TOKEN_OP_AMPERSAND, MP_TOKEN_DEL_AMPERSAND_EQUAL,
205 MP_TOKEN_OP_PIPE, MP_TOKEN_DEL_PIPE_EQUAL,
206 MP_TOKEN_OP_SLASH, MP_TOKEN_DEL_SLASH_EQUAL, MP_TOKEN_OP_DBL_SLASH, MP_TOKEN_DEL_DBL_SLASH_EQUAL,
207 MP_TOKEN_OP_PERCENT, MP_TOKEN_DEL_PERCENT_EQUAL,
208 MP_TOKEN_OP_CARET, MP_TOKEN_DEL_CARET_EQUAL,
209 MP_TOKEN_DEL_EQUAL, MP_TOKEN_OP_DBL_EQUAL,
Damien429d7192013-10-04 19:53:11 +0100210};
211
212// must have the same order as enum in lexer.h
Damien Georgeae436792017-02-17 11:10:35 +1100213// must be sorted according to strcmp
Damien George3ff16ff2016-05-20 12:38:15 +0100214STATIC const char *const tok_kw[] = {
Damien429d7192013-10-04 19:53:11 +0100215 "False",
216 "None",
217 "True",
Damien Georgeae436792017-02-17 11:10:35 +1100218 "__debug__",
Damien429d7192013-10-04 19:53:11 +0100219 "and",
220 "as",
221 "assert",
pohmelie81ebba72016-01-27 23:23:11 +0300222 #if MICROPY_PY_ASYNC_AWAIT
223 "async",
224 "await",
225 #endif
Damien429d7192013-10-04 19:53:11 +0100226 "break",
227 "class",
228 "continue",
229 "def",
230 "del",
231 "elif",
232 "else",
233 "except",
234 "finally",
235 "for",
236 "from",
237 "global",
238 "if",
239 "import",
240 "in",
241 "is",
242 "lambda",
243 "nonlocal",
244 "not",
245 "or",
246 "pass",
247 "raise",
248 "return",
249 "try",
250 "while",
251 "with",
252 "yield",
Damien429d7192013-10-04 19:53:11 +0100253};
254
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200255// This is called with CUR_CHAR() before first hex digit, and should return with
256// it pointing to last hex digit
Damien George54eb4e72014-07-03 13:47:47 +0100257// num_digits must be greater than zero
Damien George5124a942017-02-17 12:44:24 +1100258STATIC bool get_hex(mp_lexer_t *lex, size_t num_digits, mp_uint_t *result) {
Damien George54eb4e72014-07-03 13:47:47 +0100259 mp_uint_t num = 0;
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200260 while (num_digits-- != 0) {
261 next_char(lex);
262 unichar c = CUR_CHAR(lex);
263 if (!unichar_isxdigit(c)) {
264 return false;
265 }
Dave Hylands3ad94d62015-05-18 14:41:25 -0700266 num = (num << 4) + unichar_xdigit_value(c);
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200267 }
268 *result = num;
269 return true;
270}
271
Damien George534b7c32017-02-17 12:12:40 +1100272STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw) {
273 // get first quoting character
274 char quote_char = '\'';
275 if (is_char(lex, '\"')) {
276 quote_char = '\"';
277 }
278 next_char(lex);
Damien Georgea4c52c52014-12-05 19:35:18 +0000279
Damien George534b7c32017-02-17 12:12:40 +1100280 // work out if it's a single or triple quoted literal
281 size_t num_quotes;
282 if (is_char_and(lex, quote_char, quote_char)) {
283 // triple quotes
284 next_char(lex);
285 next_char(lex);
286 num_quotes = 3;
287 } else {
288 // single quotes
289 num_quotes = 1;
290 }
291
292 size_t n_closing = 0;
293 while (!is_end(lex) && (num_quotes > 1 || !is_char(lex, '\n')) && n_closing < num_quotes) {
294 if (is_char(lex, quote_char)) {
295 n_closing += 1;
296 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
297 } else {
298 n_closing = 0;
299 if (is_char(lex, '\\')) {
300 next_char(lex);
301 unichar c = CUR_CHAR(lex);
302 if (is_raw) {
303 // raw strings allow escaping of quotes, but the backslash is also emitted
304 vstr_add_char(&lex->vstr, '\\');
305 } else {
306 switch (c) {
307 // note: "c" can never be MP_LEXER_EOF because next_char
308 // always inserts a newline at the end of the input stream
309 case '\n': c = MP_LEXER_EOF; break; // backslash escape the newline, just ignore it
310 case '\\': break;
311 case '\'': break;
312 case '"': break;
313 case 'a': c = 0x07; break;
314 case 'b': c = 0x08; break;
315 case 't': c = 0x09; break;
316 case 'n': c = 0x0a; break;
317 case 'v': c = 0x0b; break;
318 case 'f': c = 0x0c; break;
319 case 'r': c = 0x0d; break;
320 case 'u':
321 case 'U':
322 if (lex->tok_kind == MP_TOKEN_BYTES) {
323 // b'\u1234' == b'\\u1234'
324 vstr_add_char(&lex->vstr, '\\');
325 break;
326 }
327 // Otherwise fall through.
328 case 'x':
329 {
330 mp_uint_t num = 0;
331 if (!get_hex(lex, (c == 'x' ? 2 : c == 'u' ? 4 : 8), &num)) {
332 // not enough hex chars for escape sequence
333 lex->tok_kind = MP_TOKEN_INVALID;
334 }
335 c = num;
336 break;
337 }
338 case 'N':
339 // Supporting '\N{LATIN SMALL LETTER A}' == 'a' would require keeping the
340 // entire Unicode name table in the core. As of Unicode 6.3.0, that's nearly
341 // 3MB of text; even gzip-compressed and with minimal structure, it'll take
342 // roughly half a meg of storage. This form of Unicode escape may be added
343 // later on, but it's definitely not a priority right now. -- CJA 20140607
344 mp_not_implemented("unicode name escapes");
345 break;
346 default:
347 if (c >= '0' && c <= '7') {
348 // Octal sequence, 1-3 chars
Damien George5124a942017-02-17 12:44:24 +1100349 size_t digits = 3;
Damien George534b7c32017-02-17 12:12:40 +1100350 mp_uint_t num = c - '0';
351 while (is_following_odigit(lex) && --digits != 0) {
352 next_char(lex);
353 num = num * 8 + (CUR_CHAR(lex) - '0');
354 }
355 c = num;
356 } else {
357 // unrecognised escape character; CPython lets this through verbatim as '\' and then the character
358 vstr_add_char(&lex->vstr, '\\');
359 }
360 break;
361 }
362 }
363 if (c != MP_LEXER_EOF) {
364 if (MICROPY_PY_BUILTINS_STR_UNICODE_DYNAMIC) {
365 if (c < 0x110000 && lex->tok_kind == MP_TOKEN_STRING) {
366 vstr_add_char(&lex->vstr, c);
367 } else if (c < 0x100 && lex->tok_kind == MP_TOKEN_BYTES) {
368 vstr_add_byte(&lex->vstr, c);
369 } else {
370 // unicode character out of range
371 // this raises a generic SyntaxError; could provide more info
372 lex->tok_kind = MP_TOKEN_INVALID;
373 }
374 } else {
375 // without unicode everything is just added as an 8-bit byte
376 if (c < 0x100) {
377 vstr_add_byte(&lex->vstr, c);
378 } else {
379 // 8-bit character out of range
380 // this raises a generic SyntaxError; could provide more info
381 lex->tok_kind = MP_TOKEN_INVALID;
382 }
383 }
384 }
385 } else {
386 // Add the "character" as a byte so that we remain 8-bit clean.
387 // This way, strings are parsed correctly whether or not they contain utf-8 chars.
388 vstr_add_byte(&lex->vstr, CUR_CHAR(lex));
389 }
390 }
391 next_char(lex);
392 }
393
394 // check we got the required end quotes
395 if (n_closing < num_quotes) {
396 lex->tok_kind = MP_TOKEN_LONELY_STRING_OPEN;
397 }
398
399 // cut off the end quotes from the token text
400 vstr_cut_tail_bytes(&lex->vstr, n_closing);
401}
402
403STATIC bool skip_whitespace(mp_lexer_t *lex, bool stop_at_newline) {
Damien429d7192013-10-04 19:53:11 +0100404 bool had_physical_newline = false;
Damien429d7192013-10-04 19:53:11 +0100405 while (!is_end(lex)) {
406 if (is_physical_newline(lex)) {
Damien George534b7c32017-02-17 12:12:40 +1100407 if (stop_at_newline && lex->nested_bracket_level == 0) {
408 break;
409 }
Damien429d7192013-10-04 19:53:11 +0100410 had_physical_newline = true;
411 next_char(lex);
412 } else if (is_whitespace(lex)) {
413 next_char(lex);
414 } else if (is_char(lex, '#')) {
415 next_char(lex);
416 while (!is_end(lex) && !is_physical_newline(lex)) {
417 next_char(lex);
418 }
419 // had_physical_newline will be set on next loop
Damien George773278e2017-02-17 11:30:14 +1100420 } else if (is_char_and(lex, '\\', '\n')) {
421 // line-continuation, so don't set had_physical_newline
Damien429d7192013-10-04 19:53:11 +0100422 next_char(lex);
Damien George773278e2017-02-17 11:30:14 +1100423 next_char(lex);
Damien429d7192013-10-04 19:53:11 +0100424 } else {
425 break;
426 }
427 }
Damien George534b7c32017-02-17 12:12:40 +1100428 return had_physical_newline;
429}
430
431void mp_lexer_to_next(mp_lexer_t *lex) {
432 // start new token text
433 vstr_reset(&lex->vstr);
434
435 // skip white space and comments
436 bool had_physical_newline = skip_whitespace(lex, false);
Damien429d7192013-10-04 19:53:11 +0100437
Damiena5185f42013-10-20 14:41:27 +0100438 // set token source information
Damien Georgea4c52c52014-12-05 19:35:18 +0000439 lex->tok_line = lex->line;
440 lex->tok_column = lex->column;
Damiena5185f42013-10-20 14:41:27 +0100441
Damien George98b30722017-02-17 10:56:06 +1100442 if (lex->emit_dent < 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000443 lex->tok_kind = MP_TOKEN_DEDENT;
Damien429d7192013-10-04 19:53:11 +0100444 lex->emit_dent += 1;
445
446 } else if (lex->emit_dent > 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000447 lex->tok_kind = MP_TOKEN_INDENT;
Damien429d7192013-10-04 19:53:11 +0100448 lex->emit_dent -= 1;
449
Damien91d387d2013-10-09 15:09:52 +0100450 } else if (had_physical_newline && lex->nested_bracket_level == 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000451 lex->tok_kind = MP_TOKEN_NEWLINE;
Damien429d7192013-10-04 19:53:11 +0100452
Damien George5124a942017-02-17 12:44:24 +1100453 size_t num_spaces = lex->column - 1;
Damien429d7192013-10-04 19:53:11 +0100454 if (num_spaces == indent_top(lex)) {
455 } else if (num_spaces > indent_top(lex)) {
456 indent_push(lex, num_spaces);
457 lex->emit_dent += 1;
458 } else {
459 while (num_spaces < indent_top(lex)) {
460 indent_pop(lex);
461 lex->emit_dent -= 1;
462 }
463 if (num_spaces != indent_top(lex)) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000464 lex->tok_kind = MP_TOKEN_DEDENT_MISMATCH;
Damien429d7192013-10-04 19:53:11 +0100465 }
466 }
467
468 } else if (is_end(lex)) {
Damien George31101d92016-10-12 11:00:17 +1100469 lex->tok_kind = MP_TOKEN_END;
Damien429d7192013-10-04 19:53:11 +0100470
Damien George534b7c32017-02-17 12:12:40 +1100471 } else if (is_string_or_bytes(lex)) {
Damien429d7192013-10-04 19:53:11 +0100472 // a string or bytes literal
473
Damien George534b7c32017-02-17 12:12:40 +1100474 // Python requires adjacent string/bytes literals to be automatically
475 // concatenated. We do it here in the tokeniser to make efficient use of RAM,
476 // because then the lexer's vstr can be used to accumulate the string literal,
477 // in contrast to creating a parse tree of strings and then joining them later
478 // in the compiler. It's also more compact in code size to do it here.
479
480 // MP_TOKEN_END is used to indicate that this is the first string token
481 lex->tok_kind = MP_TOKEN_END;
482
483 // Loop to accumulate string/bytes literals
484 do {
485 // parse type codes
486 bool is_raw = false;
487 mp_token_kind_t kind = MP_TOKEN_STRING;
488 int n_char = 0;
489 if (is_char(lex, 'u')) {
490 n_char = 1;
491 } else if (is_char(lex, 'b')) {
492 kind = MP_TOKEN_BYTES;
493 n_char = 1;
494 if (is_char_following(lex, 'r')) {
495 is_raw = true;
496 n_char = 2;
497 }
498 } else if (is_char(lex, 'r')) {
Damien429d7192013-10-04 19:53:11 +0100499 is_raw = true;
Damien George534b7c32017-02-17 12:12:40 +1100500 n_char = 1;
501 if (is_char_following(lex, 'b')) {
502 kind = MP_TOKEN_BYTES;
503 n_char = 2;
Damien429d7192013-10-04 19:53:11 +0100504 }
505 }
Damien429d7192013-10-04 19:53:11 +0100506
Damien George534b7c32017-02-17 12:12:40 +1100507 // Set or check token kind
508 if (lex->tok_kind == MP_TOKEN_END) {
509 lex->tok_kind = kind;
510 } else if (lex->tok_kind != kind) {
511 // Can't concatenate string with bytes
512 break;
513 }
Damien429d7192013-10-04 19:53:11 +0100514
Damien George534b7c32017-02-17 12:12:40 +1100515 // Skip any type code characters
516 if (n_char != 0) {
517 next_char(lex);
518 if (n_char == 2) {
519 next_char(lex);
520 }
521 }
522
523 // Parse the literal
524 parse_string_literal(lex, is_raw);
525
526 // Skip whitespace so we can check if there's another string following
527 skip_whitespace(lex, true);
528
529 } while (is_string_or_bytes(lex));
Damien429d7192013-10-04 19:53:11 +0100530
531 } else if (is_head_of_identifier(lex)) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000532 lex->tok_kind = MP_TOKEN_NAME;
Damien429d7192013-10-04 19:53:11 +0100533
Damien George7ed58cb2015-06-09 10:58:07 +0000534 // get first char (add as byte to remain 8-bit clean and support utf-8)
535 vstr_add_byte(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100536 next_char(lex);
537
Damiena5185f42013-10-20 14:41:27 +0100538 // get tail chars
Damien429d7192013-10-04 19:53:11 +0100539 while (!is_end(lex) && is_tail_of_identifier(lex)) {
Damien George7ed58cb2015-06-09 10:58:07 +0000540 vstr_add_byte(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100541 next_char(lex);
542 }
543
Damien Georgea68c7542017-02-17 10:59:57 +1100544 // Check if the name is a keyword.
545 // We also check for __debug__ here and convert it to its value. This is
546 // so the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
547 // need to check for this special token in many places in the compiler.
Damien Georgeae436792017-02-17 11:10:35 +1100548 const char *s = vstr_null_terminated_str(&lex->vstr);
Damien Georgea68c7542017-02-17 10:59:57 +1100549 for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
Damien Georgeae436792017-02-17 11:10:35 +1100550 int cmp = strcmp(s, tok_kw[i]);
551 if (cmp == 0) {
552 lex->tok_kind = MP_TOKEN_KW_FALSE + i;
553 if (lex->tok_kind == MP_TOKEN_KW___DEBUG__) {
Damien Georgea68c7542017-02-17 10:59:57 +1100554 lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
Damien Georgea68c7542017-02-17 10:59:57 +1100555 }
556 break;
Damien Georgeae436792017-02-17 11:10:35 +1100557 } else if (cmp < 0) {
558 // Table is sorted and comparison was less-than, so stop searching
559 break;
Damien Georgea68c7542017-02-17 10:59:57 +1100560 }
561 }
562
Damien429d7192013-10-04 19:53:11 +0100563 } else if (is_digit(lex) || (is_char(lex, '.') && is_following_digit(lex))) {
Damien George7d414a12015-02-08 01:57:40 +0000564 bool forced_integer = false;
565 if (is_char(lex, '.')) {
566 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
567 } else {
568 lex->tok_kind = MP_TOKEN_INTEGER;
Damien George2b000472015-09-07 17:33:44 +0100569 if (is_char(lex, '0') && is_following_base_char(lex)) {
Damien George7d414a12015-02-08 01:57:40 +0000570 forced_integer = true;
571 }
572 }
Damien429d7192013-10-04 19:53:11 +0100573
Damiena5185f42013-10-20 14:41:27 +0100574 // get first char
575 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100576 next_char(lex);
577
Damiena5185f42013-10-20 14:41:27 +0100578 // get tail chars
Damien429d7192013-10-04 19:53:11 +0100579 while (!is_end(lex)) {
Damien George7d414a12015-02-08 01:57:40 +0000580 if (!forced_integer && is_char_or(lex, 'e', 'E')) {
581 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
Damiena5185f42013-10-20 14:41:27 +0100582 vstr_add_char(&lex->vstr, 'e');
Damien429d7192013-10-04 19:53:11 +0100583 next_char(lex);
584 if (is_char(lex, '+') || is_char(lex, '-')) {
Damiena5185f42013-10-20 14:41:27 +0100585 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100586 next_char(lex);
587 }
Damien George7d414a12015-02-08 01:57:40 +0000588 } else if (is_letter(lex) || is_digit(lex) || is_char(lex, '.')) {
589 if (is_char_or3(lex, '.', 'j', 'J')) {
590 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
591 }
Damiena5185f42013-10-20 14:41:27 +0100592 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100593 next_char(lex);
594 } else {
595 break;
596 }
597 }
598
599 } else {
600 // search for encoded delimiter or operator
601
602 const char *t = tok_enc;
Damien George5124a942017-02-17 12:44:24 +1100603 size_t tok_enc_index = 0;
Damien429d7192013-10-04 19:53:11 +0100604 for (; *t != 0 && !is_char(lex, *t); t += 1) {
605 if (*t == 'e' || *t == 'c') {
606 t += 1;
Damien429d7192013-10-04 19:53:11 +0100607 }
608 tok_enc_index += 1;
609 }
610
611 next_char(lex);
612
613 if (*t == 0) {
614 // didn't match any delimiter or operator characters
Damien Georgea4c52c52014-12-05 19:35:18 +0000615 lex->tok_kind = MP_TOKEN_INVALID;
Damien429d7192013-10-04 19:53:11 +0100616
Damien George5010d192017-03-29 10:55:36 +1100617 } else if (*t == '!') {
618 // "!=" is a special case because "!" is not a valid operator
619 if (is_char(lex, '=')) {
620 next_char(lex);
621 lex->tok_kind = MP_TOKEN_OP_NOT_EQUAL;
622 } else {
623 lex->tok_kind = MP_TOKEN_INVALID;
624 }
625
626 } else if (*t == '.') {
627 // "." and "..." are special cases because ".." is not a valid operator
628 if (is_char_and(lex, '.', '.')) {
629 next_char(lex);
630 next_char(lex);
631 lex->tok_kind = MP_TOKEN_ELLIPSIS;
632 } else {
633 lex->tok_kind = MP_TOKEN_DEL_PERIOD;
634 }
635
Damien429d7192013-10-04 19:53:11 +0100636 } else {
637 // matched a delimiter or operator character
638
639 // get the maximum characters for a valid token
640 t += 1;
Damien George5124a942017-02-17 12:44:24 +1100641 size_t t_index = tok_enc_index;
Damien George5010d192017-03-29 10:55:36 +1100642 while (*t == 'c' || *t == 'e') {
643 t_index += 1;
644 if (is_char(lex, t[1])) {
645 next_char(lex);
646 tok_enc_index = t_index;
647 if (*t == 'e') {
Damien429d7192013-10-04 19:53:11 +0100648 break;
649 }
Damien George5010d192017-03-29 10:55:36 +1100650 } else if (*t == 'c') {
Damien429d7192013-10-04 19:53:11 +0100651 break;
652 }
Damien George5010d192017-03-29 10:55:36 +1100653 t += 2;
Damien429d7192013-10-04 19:53:11 +0100654 }
655
656 // set token kind
Damien Georgea4c52c52014-12-05 19:35:18 +0000657 lex->tok_kind = tok_enc_kind[tok_enc_index];
Damien429d7192013-10-04 19:53:11 +0100658
659 // compute bracket level for implicit line joining
Damien Georgea4c52c52014-12-05 19:35:18 +0000660 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) {
Damien429d7192013-10-04 19:53:11 +0100661 lex->nested_bracket_level += 1;
Damien Georgea4c52c52014-12-05 19:35:18 +0000662 } 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) {
Damien429d7192013-10-04 19:53:11 +0100663 lex->nested_bracket_level -= 1;
664 }
665 }
666 }
Damien429d7192013-10-04 19:53:11 +0100667}
668
Damien George5bdf1652016-11-16 18:27:20 +1100669mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
Damien George18310342017-03-14 11:16:31 +1100670 mp_lexer_t *lex = m_new_obj(mp_lexer_t);
Damien429d7192013-10-04 19:53:11 +0100671
Damien Georgeb829b5c2014-01-25 13:51:19 +0000672 lex->source_name = src_name;
Damien George5bdf1652016-11-16 18:27:20 +1100673 lex->reader = reader;
Damien429d7192013-10-04 19:53:11 +0100674 lex->line = 1;
Tom Collins29986472017-05-04 16:31:08 -0700675 lex->column = -2; // account for 3 dummy bytes
Damien429d7192013-10-04 19:53:11 +0100676 lex->emit_dent = 0;
677 lex->nested_bracket_level = 0;
Damien George58ebde42014-05-21 20:32:59 +0100678 lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT;
Damien429d7192013-10-04 19:53:11 +0100679 lex->num_indent_level = 1;
Damien George18310342017-03-14 11:16:31 +1100680 lex->indent_level = m_new(uint16_t, lex->alloc_indent_level);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200681 vstr_init(&lex->vstr, 32);
Damien429d7192013-10-04 19:53:11 +0100682
Damien Georgee1199ec2014-05-10 17:48:01 +0100683 // store sentinel for first indentation level
684 lex->indent_level[0] = 0;
685
Tom Collins29986472017-05-04 16:31:08 -0700686 // load lexer with start of file, advancing lex->column to 1
687 // start with dummy bytes and use next_char() for proper EOL/EOF handling
688 lex->chr0 = lex->chr1 = lex->chr2 = 0;
689 next_char(lex);
690 next_char(lex);
691 next_char(lex);
Damien429d7192013-10-04 19:53:11 +0100692
Damiena5185f42013-10-20 14:41:27 +0100693 // preload first token
Damien George98b30722017-02-17 10:56:06 +1100694 mp_lexer_to_next(lex);
695
696 // Check that the first token is in the first column. If it's not then we
697 // convert the token kind to INDENT so that the parser gives a syntax error.
698 if (lex->tok_column != 1) {
699 lex->tok_kind = MP_TOKEN_INDENT;
700 }
Damien429d7192013-10-04 19:53:11 +0100701
702 return lex;
703}
704
Damien George5124a942017-02-17 12:44:24 +1100705mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len, size_t free_len) {
Damien George511c0832016-11-16 16:22:08 +1100706 mp_reader_t reader;
Damien George18310342017-03-14 11:16:31 +1100707 mp_reader_new_mem(&reader, (const byte*)str, len, free_len);
Damien George5bdf1652016-11-16 18:27:20 +1100708 return mp_lexer_new(src_name, reader);
Damien George511c0832016-11-16 16:22:08 +1100709}
710
Damien George8beba732017-01-29 15:16:51 +1100711#if MICROPY_READER_POSIX || MICROPY_READER_VFS
Damien Georgee5ef15a2016-11-16 16:25:06 +1100712
713mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
714 mp_reader_t reader;
Damien George18310342017-03-14 11:16:31 +1100715 mp_reader_new_file(&reader, filename);
Damien George5bdf1652016-11-16 18:27:20 +1100716 return mp_lexer_new(qstr_from_str(filename), reader);
Damien Georgee5ef15a2016-11-16 16:25:06 +1100717}
718
Damien George66d955c2016-11-16 18:12:55 +1100719#if MICROPY_HELPER_LEXER_UNIX
720
721mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd) {
722 mp_reader_t reader;
Damien George18310342017-03-14 11:16:31 +1100723 mp_reader_new_file_from_fd(&reader, fd, close_fd);
Damien George5bdf1652016-11-16 18:27:20 +1100724 return mp_lexer_new(filename, reader);
Damien George66d955c2016-11-16 18:12:55 +1100725}
726
727#endif
728
Damien Georgee5ef15a2016-11-16 16:25:06 +1100729#endif
730
Damiend99b0522013-12-21 18:17:45 +0000731void mp_lexer_free(mp_lexer_t *lex) {
Damiena5185f42013-10-20 14:41:27 +0100732 if (lex) {
Damien George5bdf1652016-11-16 18:27:20 +1100733 lex->reader.close(lex->reader.data);
Damienbb5316b2013-10-22 21:12:29 +0100734 vstr_clear(&lex->vstr);
Paul Sokolovsky624ed5d2014-01-23 22:25:57 +0200735 m_del(uint16_t, lex->indent_level, lex->alloc_indent_level);
Damien732407f2013-12-29 19:33:23 +0000736 m_del_obj(mp_lexer_t, lex);
Damien429d7192013-10-04 19:53:11 +0100737 }
Damien429d7192013-10-04 19:53:11 +0100738}
739
Damien Georgec305ae32016-12-22 10:49:54 +1100740#if 0
741// This function is used to print the current token and should only be
742// needed to debug the lexer, so it's not available via a config option.
Damien Georgea4c52c52014-12-05 19:35:18 +0000743void mp_lexer_show_token(const mp_lexer_t *lex) {
Damien George451a0872014-12-05 22:50:16 +0000744 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 Georgea4c52c52014-12-05 19:35:18 +0000745 if (lex->vstr.len > 0) {
746 const byte *i = (const byte *)lex->vstr.buf;
747 const byte *j = (const byte *)i + lex->vstr.len;
748 printf(" ");
749 while (i < j) {
750 unichar c = utf8_get_char(i);
751 i = utf8_next_char(i);
752 if (unichar_isprint(c)) {
Damien George7f19a392015-06-22 17:40:12 +0100753 printf("%c", (int)c);
Damien Georgea4c52c52014-12-05 19:35:18 +0000754 } else {
755 printf("?");
756 }
757 }
758 }
759 printf("\n");
Damien429d7192013-10-04 19:53:11 +0100760}
Damien Georgea4c52c52014-12-05 19:35:18 +0000761#endif
Damien Georgedd5353a2015-12-18 12:35:44 +0000762
763#endif // MICROPY_ENABLE_COMPILER