blob: 6c8ac9aeedab6edcbe9fe3bbf6b30a4f7206e03a [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>
28#include <assert.h>
29
Damien Georgeb4b10fd2015-01-01 23:30:53 +000030#include "py/mpstate.h"
Damien George511c0832016-11-16 16:22:08 +110031#include "py/reader.h"
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/lexer.h"
Damien George081f9322015-09-07 17:08:49 +010033#include "py/runtime.h"
Damien429d7192013-10-04 19:53:11 +010034
Damien Georgedd5353a2015-12-18 12:35:44 +000035#if MICROPY_ENABLE_COMPILER
36
Damien429d7192013-10-04 19:53:11 +010037#define TAB_SIZE (8)
Damien429d7192013-10-04 19:53:11 +010038
Damien92c06562013-10-22 22:32:27 +010039// 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 George9528cd62014-01-15 21:23:31 +000042// TODO replace with a call to a standard function
Damien Georgea4c52c52014-12-05 19:35:18 +000043STATIC bool str_strn_equal(const char *str, const char *strn, mp_uint_t len) {
Damien George54eb4e72014-07-03 13:47:47 +010044 mp_uint_t i = 0;
Damien429d7192013-10-04 19:53:11 +010045
Damiena5185f42013-10-20 14:41:27 +010046 while (i < len && *str == *strn) {
Damien429d7192013-10-04 19:53:11 +010047 ++i;
Damien429d7192013-10-04 19:53:11 +010048 ++str;
Damiena5185f42013-10-20 14:41:27 +010049 ++strn;
Damien429d7192013-10-04 19:53:11 +010050 }
51
Damiena5185f42013-10-20 14:41:27 +010052 return i == len && *str == 0;
Damien429d7192013-10-04 19:53:11 +010053}
54
Damien George5bdf1652016-11-16 18:27:20 +110055#define MP_LEXER_EOF ((unichar)MP_READER_EOF)
Damiena5185f42013-10-20 14:41:27 +010056#define CUR_CHAR(lex) ((lex)->chr0)
57
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020058STATIC bool is_end(mp_lexer_t *lex) {
Damien George94fbe972014-07-30 11:46:05 +010059 return lex->chr0 == MP_LEXER_EOF;
Damien429d7192013-10-04 19:53:11 +010060}
61
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020062STATIC bool is_physical_newline(mp_lexer_t *lex) {
Damien George32bade12015-01-30 00:27:46 +000063 return lex->chr0 == '\n';
Damien429d7192013-10-04 19:53:11 +010064}
65
Damien George2e2e4042015-03-19 00:21:29 +000066STATIC bool is_char(mp_lexer_t *lex, byte c) {
Damien429d7192013-10-04 19:53:11 +010067 return lex->chr0 == c;
68}
69
Damien George2e2e4042015-03-19 00:21:29 +000070STATIC bool is_char_or(mp_lexer_t *lex, byte c1, byte c2) {
Damien429d7192013-10-04 19:53:11 +010071 return lex->chr0 == c1 || lex->chr0 == c2;
72}
73
Damien George2e2e4042015-03-19 00:21:29 +000074STATIC bool is_char_or3(mp_lexer_t *lex, byte c1, byte c2, byte c3) {
Damien429d7192013-10-04 19:53:11 +010075 return lex->chr0 == c1 || lex->chr0 == c2 || lex->chr0 == c3;
76}
77
78/*
Damien George2e2e4042015-03-19 00:21:29 +000079STATIC bool is_char_following(mp_lexer_t *lex, byte c) {
Damien429d7192013-10-04 19:53:11 +010080 return lex->chr1 == c;
81}
82*/
83
Damien George2e2e4042015-03-19 00:21:29 +000084STATIC bool is_char_following_or(mp_lexer_t *lex, byte c1, byte c2) {
Damien429d7192013-10-04 19:53:11 +010085 return lex->chr1 == c1 || lex->chr1 == c2;
86}
87
Damien George2e2e4042015-03-19 00:21:29 +000088STATIC bool is_char_following_following_or(mp_lexer_t *lex, byte c1, byte c2) {
Damien429d7192013-10-04 19:53:11 +010089 return lex->chr2 == c1 || lex->chr2 == c2;
90}
91
Damien George2e2e4042015-03-19 00:21:29 +000092STATIC bool is_char_and(mp_lexer_t *lex, byte c1, byte c2) {
Damien429d7192013-10-04 19:53:11 +010093 return lex->chr0 == c1 && lex->chr1 == c2;
94}
95
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020096STATIC bool is_whitespace(mp_lexer_t *lex) {
Damien George8cc96a32013-12-30 18:23:50 +000097 return unichar_isspace(lex->chr0);
Damien429d7192013-10-04 19:53:11 +010098}
99
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200100STATIC bool is_letter(mp_lexer_t *lex) {
Damien George8cc96a32013-12-30 18:23:50 +0000101 return unichar_isalpha(lex->chr0);
Damien429d7192013-10-04 19:53:11 +0100102}
103
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200104STATIC bool is_digit(mp_lexer_t *lex) {
Damien George8cc96a32013-12-30 18:23:50 +0000105 return unichar_isdigit(lex->chr0);
Damien429d7192013-10-04 19:53:11 +0100106}
107
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200108STATIC bool is_following_digit(mp_lexer_t *lex) {
Damien George8cc96a32013-12-30 18:23:50 +0000109 return unichar_isdigit(lex->chr1);
Damien429d7192013-10-04 19:53:11 +0100110}
111
Damien George2b000472015-09-07 17:33:44 +0100112STATIC 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 George7d414a12015-02-08 01:57:40 +0000115}
116
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200117STATIC bool is_following_odigit(mp_lexer_t *lex) {
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200118 return lex->chr1 >= '0' && lex->chr1 <= '7';
119}
120
Damien George7ed58cb2015-06-09 10:58:07 +0000121// to easily parse utf-8 identifiers we allow any raw byte with high bit set
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200122STATIC bool is_head_of_identifier(mp_lexer_t *lex) {
Damien George7ed58cb2015-06-09 10:58:07 +0000123 return is_letter(lex) || lex->chr0 == '_' || lex->chr0 >= 0x80;
Damien429d7192013-10-04 19:53:11 +0100124}
125
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200126STATIC bool is_tail_of_identifier(mp_lexer_t *lex) {
Damien429d7192013-10-04 19:53:11 +0100127 return is_head_of_identifier(lex) || is_digit(lex);
128}
129
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200130STATIC void next_char(mp_lexer_t *lex) {
Damien429d7192013-10-04 19:53:11 +0100131 if (lex->chr0 == '\n') {
Damien George32bade12015-01-30 00:27:46 +0000132 // a new line
Damien429d7192013-10-04 19:53:11 +0100133 ++lex->line;
134 lex->column = 1;
Damien429d7192013-10-04 19:53:11 +0100135 } 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 George32bade12015-01-30 00:27:46 +0000143 lex->chr0 = lex->chr1;
144 lex->chr1 = lex->chr2;
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 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 George5bdf1652016-11-16 18:27:20 +1100153 lex->chr2 = lex->reader.readbyte(lex->reader.data);
Damien George32bade12015-01-30 00:27:46 +0000154 }
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';
Damien429d7192013-10-04 19:53:11 +0100163 }
164 }
165}
166
Damien Georgea4c52c52014-12-05 19:35:18 +0000167STATIC void indent_push(mp_lexer_t *lex, mp_uint_t indent) {
Damien429d7192013-10-04 19:53:11 +0100168 if (lex->num_indent_level >= lex->alloc_indent_level) {
Damien Georgee1199ec2014-05-10 17:48:01 +0100169 // TODO use m_renew_maybe and somehow indicate an error if it fails... probably by using MP_TOKEN_MEMORY_ERROR
Damien George58ebde42014-05-21 20:32:59 +0100170 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;
Damien429d7192013-10-04 19:53:11 +0100172 }
173 lex->indent_level[lex->num_indent_level++] = indent;
174}
175
Damien Georgea4c52c52014-12-05 19:35:18 +0000176STATIC mp_uint_t indent_top(mp_lexer_t *lex) {
Damien429d7192013-10-04 19:53:11 +0100177 return lex->indent_level[lex->num_indent_level - 1];
178}
179
Damien Georgea4c52c52014-12-05 19:35:18 +0000180STATIC void indent_pop(mp_lexer_t *lex) {
Damien429d7192013-10-04 19:53:11 +0100181 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 George3ff16ff2016-05-20 12:38:15 +0100191STATIC const char *const tok_enc =
Damien429d7192013-10-04 19:53:11 +0100192 "()[]{},:;@~" // 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 George2e9eb2d2014-04-10 12:19:33 +0100204 "!E="; // !=
Damien429d7192013-10-04 19:53:11 +0100205
206// 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 +0200207STATIC const uint8_t tok_enc_kind[] = {
Damiend99b0522013-12-21 18:17:45 +0000208 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,
Damien429d7192013-10-04 19:53:11 +0100212
Damiend99b0522013-12-21 18:17:45 +0000213 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,
Damien429d7192013-10-04 19:53:11 +0100225};
226
227// must have the same order as enum in lexer.h
Damien George3ff16ff2016-05-20 12:38:15 +0100228STATIC const char *const tok_kw[] = {
Damien429d7192013-10-04 19:53:11 +0100229 "False",
230 "None",
231 "True",
232 "and",
233 "as",
234 "assert",
pohmelie81ebba72016-01-27 23:23:11 +0300235 #if MICROPY_PY_ASYNC_AWAIT
236 "async",
237 "await",
238 #endif
Damien429d7192013-10-04 19:53:11 +0100239 "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 George97f9a282014-05-12 23:07:34 +0100266 "__debug__",
Damien429d7192013-10-04 19:53:11 +0100267};
268
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200269// This is called with CUR_CHAR() before first hex digit, and should return with
270// it pointing to last hex digit
Damien George54eb4e72014-07-03 13:47:47 +0100271// num_digits must be greater than zero
272STATIC bool get_hex(mp_lexer_t *lex, mp_uint_t num_digits, mp_uint_t *result) {
273 mp_uint_t num = 0;
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200274 while (num_digits-- != 0) {
275 next_char(lex);
276 unichar c = CUR_CHAR(lex);
277 if (!unichar_isxdigit(c)) {
278 return false;
279 }
Dave Hylands3ad94d62015-05-18 14:41:25 -0700280 num = (num << 4) + unichar_xdigit_value(c);
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200281 }
282 *result = num;
283 return true;
284}
285
Damien Georgea4c52c52014-12-05 19:35:18 +0000286STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) {
287 // start new token text
288 vstr_reset(&lex->vstr);
289
Damiena5185f42013-10-20 14:41:27 +0100290 // skip white space and comments
Damien429d7192013-10-04 19:53:11 +0100291 bool had_physical_newline = false;
Damien429d7192013-10-04 19:53:11 +0100292 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 George69a818d2014-01-12 13:55:24 +0000308 // SyntaxError: unexpected character after line continuation character
Damien Georgea4c52c52014-12-05 19:35:18 +0000309 lex->tok_line = lex->line;
310 lex->tok_column = lex->column;
311 lex->tok_kind = MP_TOKEN_BAD_LINE_CONTINUATION;
Damien George69a818d2014-01-12 13:55:24 +0000312 return;
Damien429d7192013-10-04 19:53:11 +0100313 } else {
314 next_char(lex);
315 }
316 } else {
317 break;
318 }
319 }
320
Damiena5185f42013-10-20 14:41:27 +0100321 // set token source information
Damien Georgea4c52c52014-12-05 19:35:18 +0000322 lex->tok_line = lex->line;
323 lex->tok_column = lex->column;
Damiena5185f42013-10-20 14:41:27 +0100324
325 if (first_token && lex->line == 1 && lex->column != 1) {
326 // check that the first token is in the first column
327 // if first token is not on first line, we get a physical newline and
328 // this check is done as part of normal indent/dedent checking below
329 // (done to get equivalence with CPython)
Damien Georgea4c52c52014-12-05 19:35:18 +0000330 lex->tok_kind = MP_TOKEN_INDENT;
Damiena5185f42013-10-20 14:41:27 +0100331
332 } else if (lex->emit_dent < 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000333 lex->tok_kind = MP_TOKEN_DEDENT;
Damien429d7192013-10-04 19:53:11 +0100334 lex->emit_dent += 1;
335
336 } else if (lex->emit_dent > 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000337 lex->tok_kind = MP_TOKEN_INDENT;
Damien429d7192013-10-04 19:53:11 +0100338 lex->emit_dent -= 1;
339
Damien91d387d2013-10-09 15:09:52 +0100340 } else if (had_physical_newline && lex->nested_bracket_level == 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000341 lex->tok_kind = MP_TOKEN_NEWLINE;
Damien429d7192013-10-04 19:53:11 +0100342
Damien George54eb4e72014-07-03 13:47:47 +0100343 mp_uint_t num_spaces = lex->column - 1;
Damien429d7192013-10-04 19:53:11 +0100344 if (num_spaces == indent_top(lex)) {
345 } else if (num_spaces > indent_top(lex)) {
346 indent_push(lex, num_spaces);
347 lex->emit_dent += 1;
348 } else {
349 while (num_spaces < indent_top(lex)) {
350 indent_pop(lex);
351 lex->emit_dent -= 1;
352 }
353 if (num_spaces != indent_top(lex)) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000354 lex->tok_kind = MP_TOKEN_DEDENT_MISMATCH;
Damien429d7192013-10-04 19:53:11 +0100355 }
356 }
357
358 } else if (is_end(lex)) {
Damien George31101d92016-10-12 11:00:17 +1100359 lex->tok_kind = MP_TOKEN_END;
Damien429d7192013-10-04 19:53:11 +0100360
361 } else if (is_char_or(lex, '\'', '\"')
362 || (is_char_or3(lex, 'r', 'u', 'b') && is_char_following_or(lex, '\'', '\"'))
363 || ((is_char_and(lex, 'r', 'b') || is_char_and(lex, 'b', 'r')) && is_char_following_following_or(lex, '\'', '\"'))) {
364 // a string or bytes literal
365
366 // parse type codes
367 bool is_raw = false;
368 bool is_bytes = false;
369 if (is_char(lex, 'u')) {
370 next_char(lex);
371 } else if (is_char(lex, 'b')) {
372 is_bytes = true;
373 next_char(lex);
374 if (is_char(lex, 'r')) {
375 is_raw = true;
376 next_char(lex);
377 }
378 } else if (is_char(lex, 'r')) {
379 is_raw = true;
380 next_char(lex);
381 if (is_char(lex, 'b')) {
382 is_bytes = true;
383 next_char(lex);
384 }
385 }
386
387 // set token kind
388 if (is_bytes) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000389 lex->tok_kind = MP_TOKEN_BYTES;
Damien429d7192013-10-04 19:53:11 +0100390 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000391 lex->tok_kind = MP_TOKEN_STRING;
Damien429d7192013-10-04 19:53:11 +0100392 }
393
394 // get first quoting character
395 char quote_char = '\'';
396 if (is_char(lex, '\"')) {
397 quote_char = '\"';
398 }
399 next_char(lex);
400
401 // work out if it's a single or triple quoted literal
Damien George54eb4e72014-07-03 13:47:47 +0100402 mp_uint_t num_quotes;
Damien429d7192013-10-04 19:53:11 +0100403 if (is_char_and(lex, quote_char, quote_char)) {
404 // triple quotes
405 next_char(lex);
406 next_char(lex);
407 num_quotes = 3;
408 } else {
409 // single quotes
410 num_quotes = 1;
411 }
412
Damien429d7192013-10-04 19:53:11 +0100413 // parse the literal
Damien George54eb4e72014-07-03 13:47:47 +0100414 mp_uint_t n_closing = 0;
Damien429d7192013-10-04 19:53:11 +0100415 while (!is_end(lex) && (num_quotes > 1 || !is_char(lex, '\n')) && n_closing < num_quotes) {
416 if (is_char(lex, quote_char)) {
417 n_closing += 1;
Damiena5185f42013-10-20 14:41:27 +0100418 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100419 } else {
420 n_closing = 0;
Damien Georgea91f4142014-04-10 11:30:55 +0100421 if (is_char(lex, '\\')) {
Damien429d7192013-10-04 19:53:11 +0100422 next_char(lex);
Damiena5185f42013-10-20 14:41:27 +0100423 unichar c = CUR_CHAR(lex);
Damien Georgea91f4142014-04-10 11:30:55 +0100424 if (is_raw) {
425 // raw strings allow escaping of quotes, but the backslash is also emitted
426 vstr_add_char(&lex->vstr, '\\');
427 } else {
428 switch (c) {
Damien Georgeb9c47832016-12-22 10:37:13 +1100429 // note: "c" can never be MP_LEXER_EOF because next_char
430 // always inserts a newline at the end of the input stream
Damien Georgeadccafb2016-12-22 10:32:06 +1100431 case '\n': c = MP_LEXER_EOF; break; // backslash escape the newline, just ignore it
Damien Georgea91f4142014-04-10 11:30:55 +0100432 case '\\': break;
433 case '\'': break;
434 case '"': break;
435 case 'a': c = 0x07; break;
436 case 'b': c = 0x08; break;
437 case 't': c = 0x09; break;
438 case 'n': c = 0x0a; break;
439 case 'v': c = 0x0b; break;
440 case 'f': c = 0x0c; break;
441 case 'r': c = 0x0d; break;
Chris Angelico2ba22992014-06-04 05:28:12 +1000442 case 'u':
443 case 'U':
444 if (is_bytes) {
445 // b'\u1234' == b'\\u1234'
446 vstr_add_char(&lex->vstr, '\\');
447 break;
448 }
449 // Otherwise fall through.
Damien Georgea91f4142014-04-10 11:30:55 +0100450 case 'x':
451 {
Damien George54eb4e72014-07-03 13:47:47 +0100452 mp_uint_t num = 0;
Chris Angelico2ba22992014-06-04 05:28:12 +1000453 if (!get_hex(lex, (c == 'x' ? 2 : c == 'u' ? 4 : 8), &num)) {
Damien Georged241c2a2015-07-23 23:20:37 +0100454 // not enough hex chars for escape sequence
455 lex->tok_kind = MP_TOKEN_INVALID;
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200456 }
457 c = num;
Damien Georgea91f4142014-04-10 11:30:55 +0100458 break;
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200459 }
Chris Angelico2ba22992014-06-04 05:28:12 +1000460 case 'N':
461 // Supporting '\N{LATIN SMALL LETTER A}' == 'a' would require keeping the
462 // entire Unicode name table in the core. As of Unicode 6.3.0, that's nearly
463 // 3MB of text; even gzip-compressed and with minimal structure, it'll take
464 // roughly half a meg of storage. This form of Unicode escape may be added
465 // later on, but it's definitely not a priority right now. -- CJA 20140607
Damien George081f9322015-09-07 17:08:49 +0100466 mp_not_implemented("unicode name escapes");
Chris Angelico2ba22992014-06-04 05:28:12 +1000467 break;
Damien Georgea91f4142014-04-10 11:30:55 +0100468 default:
469 if (c >= '0' && c <= '7') {
470 // Octal sequence, 1-3 chars
Damien George54eb4e72014-07-03 13:47:47 +0100471 mp_uint_t digits = 3;
472 mp_uint_t num = c - '0';
Damien Georgea91f4142014-04-10 11:30:55 +0100473 while (is_following_odigit(lex) && --digits != 0) {
474 next_char(lex);
475 num = num * 8 + (CUR_CHAR(lex) - '0');
476 }
477 c = num;
478 } else {
479 // unrecognised escape character; CPython lets this through verbatim as '\' and then the character
480 vstr_add_char(&lex->vstr, '\\');
481 }
482 break;
483 }
Damiena5185f42013-10-20 14:41:27 +0100484 }
Damien George94fbe972014-07-30 11:46:05 +0100485 if (c != MP_LEXER_EOF) {
Damien Georgeea235202016-02-11 22:30:53 +0000486 if (MICROPY_PY_BUILTINS_STR_UNICODE_DYNAMIC) {
487 if (c < 0x110000 && !is_bytes) {
488 vstr_add_char(&lex->vstr, c);
489 } else if (c < 0x100 && is_bytes) {
490 vstr_add_byte(&lex->vstr, c);
491 } else {
492 // unicode character out of range
493 // this raises a generic SyntaxError; could provide more info
494 lex->tok_kind = MP_TOKEN_INVALID;
495 }
496 } else {
497 // without unicode everything is just added as an 8-bit byte
498 if (c < 0x100) {
499 vstr_add_byte(&lex->vstr, c);
500 } else {
501 // 8-bit character out of range
502 // this raises a generic SyntaxError; could provide more info
503 lex->tok_kind = MP_TOKEN_INVALID;
504 }
Chris Angelico2ba22992014-06-04 05:28:12 +1000505 }
Damiena5185f42013-10-20 14:41:27 +0100506 }
507 } else {
Damien George94fbe972014-07-30 11:46:05 +0100508 // Add the "character" as a byte so that we remain 8-bit clean.
509 // This way, strings are parsed correctly whether or not they contain utf-8 chars.
510 vstr_add_byte(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100511 }
512 }
513 next_char(lex);
514 }
515
516 // check we got the required end quotes
517 if (n_closing < num_quotes) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000518 lex->tok_kind = MP_TOKEN_LONELY_STRING_OPEN;
Damien429d7192013-10-04 19:53:11 +0100519 }
520
Damiena5185f42013-10-20 14:41:27 +0100521 // cut off the end quotes from the token text
Damien George280e7202014-03-15 14:33:09 +0000522 vstr_cut_tail_bytes(&lex->vstr, n_closing);
Damien429d7192013-10-04 19:53:11 +0100523
524 } else if (is_head_of_identifier(lex)) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000525 lex->tok_kind = MP_TOKEN_NAME;
Damien429d7192013-10-04 19:53:11 +0100526
Damien George7ed58cb2015-06-09 10:58:07 +0000527 // get first char (add as byte to remain 8-bit clean and support utf-8)
528 vstr_add_byte(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100529 next_char(lex);
530
Damiena5185f42013-10-20 14:41:27 +0100531 // get tail chars
Damien429d7192013-10-04 19:53:11 +0100532 while (!is_end(lex) && is_tail_of_identifier(lex)) {
Damien George7ed58cb2015-06-09 10:58:07 +0000533 vstr_add_byte(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100534 next_char(lex);
535 }
536
537 } else if (is_digit(lex) || (is_char(lex, '.') && is_following_digit(lex))) {
Damien George7d414a12015-02-08 01:57:40 +0000538 bool forced_integer = false;
539 if (is_char(lex, '.')) {
540 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
541 } else {
542 lex->tok_kind = MP_TOKEN_INTEGER;
Damien George2b000472015-09-07 17:33:44 +0100543 if (is_char(lex, '0') && is_following_base_char(lex)) {
Damien George7d414a12015-02-08 01:57:40 +0000544 forced_integer = true;
545 }
546 }
Damien429d7192013-10-04 19:53:11 +0100547
Damiena5185f42013-10-20 14:41:27 +0100548 // get first char
549 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100550 next_char(lex);
551
Damiena5185f42013-10-20 14:41:27 +0100552 // get tail chars
Damien429d7192013-10-04 19:53:11 +0100553 while (!is_end(lex)) {
Damien George7d414a12015-02-08 01:57:40 +0000554 if (!forced_integer && is_char_or(lex, 'e', 'E')) {
555 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
Damiena5185f42013-10-20 14:41:27 +0100556 vstr_add_char(&lex->vstr, 'e');
Damien429d7192013-10-04 19:53:11 +0100557 next_char(lex);
558 if (is_char(lex, '+') || is_char(lex, '-')) {
Damiena5185f42013-10-20 14:41:27 +0100559 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100560 next_char(lex);
561 }
Damien George7d414a12015-02-08 01:57:40 +0000562 } else if (is_letter(lex) || is_digit(lex) || is_char(lex, '.')) {
563 if (is_char_or3(lex, '.', 'j', 'J')) {
564 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
565 }
Damiena5185f42013-10-20 14:41:27 +0100566 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100567 next_char(lex);
568 } else {
569 break;
570 }
571 }
572
Damien George2e9eb2d2014-04-10 12:19:33 +0100573 } else if (is_char(lex, '.')) {
574 // special handling for . and ... operators, because .. is not a valid operator
575
576 // get first char
577 vstr_add_char(&lex->vstr, '.');
578 next_char(lex);
579
580 if (is_char_and(lex, '.', '.')) {
581 vstr_add_char(&lex->vstr, '.');
582 vstr_add_char(&lex->vstr, '.');
583 next_char(lex);
584 next_char(lex);
Damien Georgea4c52c52014-12-05 19:35:18 +0000585 lex->tok_kind = MP_TOKEN_ELLIPSIS;
Damien George2e9eb2d2014-04-10 12:19:33 +0100586 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000587 lex->tok_kind = MP_TOKEN_DEL_PERIOD;
Damien George2e9eb2d2014-04-10 12:19:33 +0100588 }
589
Damien429d7192013-10-04 19:53:11 +0100590 } else {
591 // search for encoded delimiter or operator
592
593 const char *t = tok_enc;
Damien George54eb4e72014-07-03 13:47:47 +0100594 mp_uint_t tok_enc_index = 0;
Damien429d7192013-10-04 19:53:11 +0100595 for (; *t != 0 && !is_char(lex, *t); t += 1) {
596 if (*t == 'e' || *t == 'c') {
597 t += 1;
598 } else if (*t == 'E') {
599 tok_enc_index -= 1;
600 t += 1;
601 }
602 tok_enc_index += 1;
603 }
604
605 next_char(lex);
606
607 if (*t == 0) {
608 // didn't match any delimiter or operator characters
Damien Georgea4c52c52014-12-05 19:35:18 +0000609 lex->tok_kind = MP_TOKEN_INVALID;
Damien429d7192013-10-04 19:53:11 +0100610
611 } else {
612 // matched a delimiter or operator character
613
614 // get the maximum characters for a valid token
615 t += 1;
Damien George54eb4e72014-07-03 13:47:47 +0100616 mp_uint_t t_index = tok_enc_index;
Damien429d7192013-10-04 19:53:11 +0100617 for (;;) {
618 for (; *t == 'e'; t += 1) {
619 t += 1;
620 t_index += 1;
621 if (is_char(lex, *t)) {
622 next_char(lex);
623 tok_enc_index = t_index;
624 break;
625 }
626 }
627
628 if (*t == 'E') {
629 t += 1;
630 if (is_char(lex, *t)) {
631 next_char(lex);
632 tok_enc_index = t_index;
633 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000634 lex->tok_kind = MP_TOKEN_INVALID;
Damien George2e9eb2d2014-04-10 12:19:33 +0100635 goto tok_enc_no_match;
Damien429d7192013-10-04 19:53:11 +0100636 }
637 break;
638 }
639
640 if (*t == 'c') {
641 t += 1;
642 t_index += 1;
643 if (is_char(lex, *t)) {
644 next_char(lex);
645 tok_enc_index = t_index;
646 t += 1;
647 } else {
648 break;
649 }
650 } else {
651 break;
652 }
653 }
654
655 // set token kind
Damien Georgea4c52c52014-12-05 19:35:18 +0000656 lex->tok_kind = tok_enc_kind[tok_enc_index];
Damien429d7192013-10-04 19:53:11 +0100657
Damien George2e9eb2d2014-04-10 12:19:33 +0100658 tok_enc_no_match:
659
Damien429d7192013-10-04 19:53:11 +0100660 // compute bracket level for implicit line joining
Damien Georgea4c52c52014-12-05 19:35:18 +0000661 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 +0100662 lex->nested_bracket_level += 1;
Damien Georgea4c52c52014-12-05 19:35:18 +0000663 } 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 +0100664 lex->nested_bracket_level -= 1;
665 }
666 }
667 }
668
Damiena5185f42013-10-20 14:41:27 +0100669 // check for keywords
Damien Georgea4c52c52014-12-05 19:35:18 +0000670 if (lex->tok_kind == MP_TOKEN_NAME) {
Damien George97f9a282014-05-12 23:07:34 +0100671 // We check for __debug__ here and convert it to its value. This is so
672 // the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
673 // need to check for this special token in many places in the compiler.
674 // TODO improve speed of these string comparisons
Damien George54eb4e72014-07-03 13:47:47 +0100675 //for (mp_int_t i = 0; tok_kw[i] != NULL; i++) {
Damien George963a5a32015-01-16 17:47:07 +0000676 for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000677 if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) {
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200678 if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
679 // tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000680 lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
Damien George97f9a282014-05-12 23:07:34 +0100681 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000682 lex->tok_kind = MP_TOKEN_KW_FALSE + i;
Damien George97f9a282014-05-12 23:07:34 +0100683 }
Damien429d7192013-10-04 19:53:11 +0100684 break;
685 }
686 }
687 }
688}
689
Damien George5bdf1652016-11-16 18:27:20 +1100690mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
Damien George9bf5f282014-10-09 16:53:37 +0100691 mp_lexer_t *lex = m_new_obj_maybe(mp_lexer_t);
Damien Georgee1199ec2014-05-10 17:48:01 +0100692
693 // check for memory allocation error
694 if (lex == NULL) {
Damien George5bdf1652016-11-16 18:27:20 +1100695 reader.close(reader.data);
Damien Georgee1199ec2014-05-10 17:48:01 +0100696 return NULL;
697 }
Damien429d7192013-10-04 19:53:11 +0100698
Damien Georgeb829b5c2014-01-25 13:51:19 +0000699 lex->source_name = src_name;
Damien George5bdf1652016-11-16 18:27:20 +1100700 lex->reader = reader;
Damien429d7192013-10-04 19:53:11 +0100701 lex->line = 1;
702 lex->column = 1;
Damien429d7192013-10-04 19:53:11 +0100703 lex->emit_dent = 0;
704 lex->nested_bracket_level = 0;
Damien George58ebde42014-05-21 20:32:59 +0100705 lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT;
Damien429d7192013-10-04 19:53:11 +0100706 lex->num_indent_level = 1;
Damien Georgee1199ec2014-05-10 17:48:01 +0100707 lex->indent_level = m_new_maybe(uint16_t, lex->alloc_indent_level);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200708 vstr_init(&lex->vstr, 32);
Damien429d7192013-10-04 19:53:11 +0100709
Damien Georgee1199ec2014-05-10 17:48:01 +0100710 // check for memory allocation error
Damien George5da0d292016-09-19 11:17:02 +1000711 // note: vstr_init above may fail on malloc, but so may mp_lexer_next_token_into below
712 if (lex->indent_level == NULL) {
Damien Georgee1199ec2014-05-10 17:48:01 +0100713 mp_lexer_free(lex);
714 return NULL;
715 }
716
717 // store sentinel for first indentation level
718 lex->indent_level[0] = 0;
719
Damien429d7192013-10-04 19:53:11 +0100720 // preload characters
Damien George5bdf1652016-11-16 18:27:20 +1100721 lex->chr0 = reader.readbyte(reader.data);
722 lex->chr1 = reader.readbyte(reader.data);
723 lex->chr2 = reader.readbyte(reader.data);
Damiena5185f42013-10-20 14:41:27 +0100724
725 // 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 George94fbe972014-07-30 11:46:05 +0100726 if (lex->chr0 == MP_LEXER_EOF) {
Damiena5185f42013-10-20 14:41:27 +0100727 lex->chr0 = '\n';
Damien George94fbe972014-07-30 11:46:05 +0100728 } else if (lex->chr1 == MP_LEXER_EOF) {
Damien George32bade12015-01-30 00:27:46 +0000729 if (lex->chr0 == '\r') {
730 lex->chr0 = '\n';
731 } else if (lex->chr0 != '\n') {
Damiena5185f42013-10-20 14:41:27 +0100732 lex->chr1 = '\n';
Damien429d7192013-10-04 19:53:11 +0100733 }
Damien George94fbe972014-07-30 11:46:05 +0100734 } else if (lex->chr2 == MP_LEXER_EOF) {
Damien George32bade12015-01-30 00:27:46 +0000735 if (lex->chr1 == '\r') {
736 lex->chr1 = '\n';
737 } else if (lex->chr1 != '\n') {
Damiena5185f42013-10-20 14:41:27 +0100738 lex->chr2 = '\n';
Damien429d7192013-10-04 19:53:11 +0100739 }
Damien429d7192013-10-04 19:53:11 +0100740 }
741
Damiena5185f42013-10-20 14:41:27 +0100742 // preload first token
Damien Georgea4c52c52014-12-05 19:35:18 +0000743 mp_lexer_next_token_into(lex, true);
Damien429d7192013-10-04 19:53:11 +0100744
745 return lex;
746}
747
Damien George511c0832016-11-16 16:22:08 +1100748mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) {
749 mp_reader_t reader;
750 if (!mp_reader_new_mem(&reader, (const byte*)str, len, free_len)) {
751 return NULL;
752 }
Damien George5bdf1652016-11-16 18:27:20 +1100753 return mp_lexer_new(src_name, reader);
Damien George511c0832016-11-16 16:22:08 +1100754}
755
Damien Georgee5ef15a2016-11-16 16:25:06 +1100756#if MICROPY_READER_POSIX || MICROPY_READER_FATFS
757
758mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
759 mp_reader_t reader;
760 int ret = mp_reader_new_file(&reader, filename);
761 if (ret != 0) {
762 return NULL;
763 }
Damien George5bdf1652016-11-16 18:27:20 +1100764 return mp_lexer_new(qstr_from_str(filename), reader);
Damien Georgee5ef15a2016-11-16 16:25:06 +1100765}
766
Damien George66d955c2016-11-16 18:12:55 +1100767#if MICROPY_HELPER_LEXER_UNIX
768
769mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd) {
770 mp_reader_t reader;
771 int ret = mp_reader_new_file_from_fd(&reader, fd, close_fd);
772 if (ret != 0) {
773 return NULL;
774 }
Damien George5bdf1652016-11-16 18:27:20 +1100775 return mp_lexer_new(filename, reader);
Damien George66d955c2016-11-16 18:12:55 +1100776}
777
778#endif
779
Damien Georgee5ef15a2016-11-16 16:25:06 +1100780#endif
781
Damiend99b0522013-12-21 18:17:45 +0000782void mp_lexer_free(mp_lexer_t *lex) {
Damiena5185f42013-10-20 14:41:27 +0100783 if (lex) {
Damien George5bdf1652016-11-16 18:27:20 +1100784 lex->reader.close(lex->reader.data);
Damienbb5316b2013-10-22 21:12:29 +0100785 vstr_clear(&lex->vstr);
Paul Sokolovsky624ed5d2014-01-23 22:25:57 +0200786 m_del(uint16_t, lex->indent_level, lex->alloc_indent_level);
Damien732407f2013-12-29 19:33:23 +0000787 m_del_obj(mp_lexer_t, lex);
Damien429d7192013-10-04 19:53:11 +0100788 }
Damien429d7192013-10-04 19:53:11 +0100789}
790
Damiend99b0522013-12-21 18:17:45 +0000791void mp_lexer_to_next(mp_lexer_t *lex) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000792 mp_lexer_next_token_into(lex, false);
Damien429d7192013-10-04 19:53:11 +0100793}
794
Damien Georgea4c52c52014-12-05 19:35:18 +0000795#if MICROPY_DEBUG_PRINTERS
796void mp_lexer_show_token(const mp_lexer_t *lex) {
Damien George451a0872014-12-05 22:50:16 +0000797 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 +0000798 if (lex->vstr.len > 0) {
799 const byte *i = (const byte *)lex->vstr.buf;
800 const byte *j = (const byte *)i + lex->vstr.len;
801 printf(" ");
802 while (i < j) {
803 unichar c = utf8_get_char(i);
804 i = utf8_next_char(i);
805 if (unichar_isprint(c)) {
Damien George7f19a392015-06-22 17:40:12 +0100806 printf("%c", (int)c);
Damien Georgea4c52c52014-12-05 19:35:18 +0000807 } else {
808 printf("?");
809 }
810 }
811 }
812 printf("\n");
Damien429d7192013-10-04 19:53:11 +0100813}
Damien Georgea4c52c52014-12-05 19:35:18 +0000814#endif
Damien Georgedd5353a2015-12-18 12:35:44 +0000815
816#endif // MICROPY_ENABLE_COMPILER