blob: 6a3fa656b1d10acd5551b0702e78e30d836fb0f9 [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 George98b30722017-02-17 10:56:06 +1100286void mp_lexer_to_next(mp_lexer_t *lex) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000287 // 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
Damien George98b30722017-02-17 10:56:06 +1100325 if (lex->emit_dent < 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000326 lex->tok_kind = MP_TOKEN_DEDENT;
Damien429d7192013-10-04 19:53:11 +0100327 lex->emit_dent += 1;
328
329 } else if (lex->emit_dent > 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000330 lex->tok_kind = MP_TOKEN_INDENT;
Damien429d7192013-10-04 19:53:11 +0100331 lex->emit_dent -= 1;
332
Damien91d387d2013-10-09 15:09:52 +0100333 } else if (had_physical_newline && lex->nested_bracket_level == 0) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000334 lex->tok_kind = MP_TOKEN_NEWLINE;
Damien429d7192013-10-04 19:53:11 +0100335
Damien George54eb4e72014-07-03 13:47:47 +0100336 mp_uint_t num_spaces = lex->column - 1;
Damien429d7192013-10-04 19:53:11 +0100337 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 Georgea4c52c52014-12-05 19:35:18 +0000347 lex->tok_kind = MP_TOKEN_DEDENT_MISMATCH;
Damien429d7192013-10-04 19:53:11 +0100348 }
349 }
350
351 } else if (is_end(lex)) {
Damien George31101d92016-10-12 11:00:17 +1100352 lex->tok_kind = MP_TOKEN_END;
Damien429d7192013-10-04 19:53:11 +0100353
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 Georgea4c52c52014-12-05 19:35:18 +0000382 lex->tok_kind = MP_TOKEN_BYTES;
Damien429d7192013-10-04 19:53:11 +0100383 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000384 lex->tok_kind = MP_TOKEN_STRING;
Damien429d7192013-10-04 19:53:11 +0100385 }
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 George54eb4e72014-07-03 13:47:47 +0100395 mp_uint_t num_quotes;
Damien429d7192013-10-04 19:53:11 +0100396 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
Damien429d7192013-10-04 19:53:11 +0100406 // parse the literal
Damien George54eb4e72014-07-03 13:47:47 +0100407 mp_uint_t n_closing = 0;
Damien429d7192013-10-04 19:53:11 +0100408 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;
Damiena5185f42013-10-20 14:41:27 +0100411 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100412 } else {
413 n_closing = 0;
Damien Georgea91f4142014-04-10 11:30:55 +0100414 if (is_char(lex, '\\')) {
Damien429d7192013-10-04 19:53:11 +0100415 next_char(lex);
Damiena5185f42013-10-20 14:41:27 +0100416 unichar c = CUR_CHAR(lex);
Damien Georgea91f4142014-04-10 11:30:55 +0100417 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 Georgeb9c47832016-12-22 10:37:13 +1100422 // note: "c" can never be MP_LEXER_EOF because next_char
423 // always inserts a newline at the end of the input stream
Damien Georgeadccafb2016-12-22 10:32:06 +1100424 case '\n': c = MP_LEXER_EOF; break; // backslash escape the newline, just ignore it
Damien Georgea91f4142014-04-10 11:30:55 +0100425 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 Angelico2ba22992014-06-04 05:28:12 +1000435 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 Georgea91f4142014-04-10 11:30:55 +0100443 case 'x':
444 {
Damien George54eb4e72014-07-03 13:47:47 +0100445 mp_uint_t num = 0;
Chris Angelico2ba22992014-06-04 05:28:12 +1000446 if (!get_hex(lex, (c == 'x' ? 2 : c == 'u' ? 4 : 8), &num)) {
Damien Georged241c2a2015-07-23 23:20:37 +0100447 // not enough hex chars for escape sequence
448 lex->tok_kind = MP_TOKEN_INVALID;
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200449 }
450 c = num;
Damien Georgea91f4142014-04-10 11:30:55 +0100451 break;
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200452 }
Chris Angelico2ba22992014-06-04 05:28:12 +1000453 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 George081f9322015-09-07 17:08:49 +0100459 mp_not_implemented("unicode name escapes");
Chris Angelico2ba22992014-06-04 05:28:12 +1000460 break;
Damien Georgea91f4142014-04-10 11:30:55 +0100461 default:
462 if (c >= '0' && c <= '7') {
463 // Octal sequence, 1-3 chars
Damien George54eb4e72014-07-03 13:47:47 +0100464 mp_uint_t digits = 3;
465 mp_uint_t num = c - '0';
Damien Georgea91f4142014-04-10 11:30:55 +0100466 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 }
Damiena5185f42013-10-20 14:41:27 +0100477 }
Damien George94fbe972014-07-30 11:46:05 +0100478 if (c != MP_LEXER_EOF) {
Damien Georgeea235202016-02-11 22:30:53 +0000479 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 Angelico2ba22992014-06-04 05:28:12 +1000498 }
Damiena5185f42013-10-20 14:41:27 +0100499 }
500 } else {
Damien George94fbe972014-07-30 11:46:05 +0100501 // 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));
Damien429d7192013-10-04 19:53:11 +0100504 }
505 }
506 next_char(lex);
507 }
508
509 // check we got the required end quotes
510 if (n_closing < num_quotes) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000511 lex->tok_kind = MP_TOKEN_LONELY_STRING_OPEN;
Damien429d7192013-10-04 19:53:11 +0100512 }
513
Damiena5185f42013-10-20 14:41:27 +0100514 // cut off the end quotes from the token text
Damien George280e7202014-03-15 14:33:09 +0000515 vstr_cut_tail_bytes(&lex->vstr, n_closing);
Damien429d7192013-10-04 19:53:11 +0100516
517 } else if (is_head_of_identifier(lex)) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000518 lex->tok_kind = MP_TOKEN_NAME;
Damien429d7192013-10-04 19:53:11 +0100519
Damien George7ed58cb2015-06-09 10:58:07 +0000520 // get first char (add as byte to remain 8-bit clean and support utf-8)
521 vstr_add_byte(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100522 next_char(lex);
523
Damiena5185f42013-10-20 14:41:27 +0100524 // get tail chars
Damien429d7192013-10-04 19:53:11 +0100525 while (!is_end(lex) && is_tail_of_identifier(lex)) {
Damien George7ed58cb2015-06-09 10:58:07 +0000526 vstr_add_byte(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100527 next_char(lex);
528 }
529
Damien Georgea68c7542017-02-17 10:59:57 +1100530 // Check if the name is a keyword.
531 // We also check for __debug__ here and convert it to its value. This is
532 // so the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
533 // need to check for this special token in many places in the compiler.
534 // TODO improve speed of these string comparisons
535 for (size_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
536 if (str_strn_equal(tok_kw[i], lex->vstr.buf, lex->vstr.len)) {
537 if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
538 // tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"
539 lex->tok_kind = (MP_STATE_VM(mp_optimise_value) == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
540 } else {
541 lex->tok_kind = MP_TOKEN_KW_FALSE + i;
542 }
543 break;
544 }
545 }
546
Damien429d7192013-10-04 19:53:11 +0100547 } else if (is_digit(lex) || (is_char(lex, '.') && is_following_digit(lex))) {
Damien George7d414a12015-02-08 01:57:40 +0000548 bool forced_integer = false;
549 if (is_char(lex, '.')) {
550 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
551 } else {
552 lex->tok_kind = MP_TOKEN_INTEGER;
Damien George2b000472015-09-07 17:33:44 +0100553 if (is_char(lex, '0') && is_following_base_char(lex)) {
Damien George7d414a12015-02-08 01:57:40 +0000554 forced_integer = true;
555 }
556 }
Damien429d7192013-10-04 19:53:11 +0100557
Damiena5185f42013-10-20 14:41:27 +0100558 // get first char
559 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100560 next_char(lex);
561
Damiena5185f42013-10-20 14:41:27 +0100562 // get tail chars
Damien429d7192013-10-04 19:53:11 +0100563 while (!is_end(lex)) {
Damien George7d414a12015-02-08 01:57:40 +0000564 if (!forced_integer && is_char_or(lex, 'e', 'E')) {
565 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
Damiena5185f42013-10-20 14:41:27 +0100566 vstr_add_char(&lex->vstr, 'e');
Damien429d7192013-10-04 19:53:11 +0100567 next_char(lex);
568 if (is_char(lex, '+') || is_char(lex, '-')) {
Damiena5185f42013-10-20 14:41:27 +0100569 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100570 next_char(lex);
571 }
Damien George7d414a12015-02-08 01:57:40 +0000572 } else if (is_letter(lex) || is_digit(lex) || is_char(lex, '.')) {
573 if (is_char_or3(lex, '.', 'j', 'J')) {
574 lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
575 }
Damiena5185f42013-10-20 14:41:27 +0100576 vstr_add_char(&lex->vstr, CUR_CHAR(lex));
Damien429d7192013-10-04 19:53:11 +0100577 next_char(lex);
578 } else {
579 break;
580 }
581 }
582
Damien George2e9eb2d2014-04-10 12:19:33 +0100583 } else if (is_char(lex, '.')) {
584 // special handling for . and ... operators, because .. is not a valid operator
585
586 // get first char
Damien George2e9eb2d2014-04-10 12:19:33 +0100587 next_char(lex);
588
589 if (is_char_and(lex, '.', '.')) {
Damien George2e9eb2d2014-04-10 12:19:33 +0100590 next_char(lex);
591 next_char(lex);
Damien Georgea4c52c52014-12-05 19:35:18 +0000592 lex->tok_kind = MP_TOKEN_ELLIPSIS;
Damien George2e9eb2d2014-04-10 12:19:33 +0100593 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000594 lex->tok_kind = MP_TOKEN_DEL_PERIOD;
Damien George2e9eb2d2014-04-10 12:19:33 +0100595 }
596
Damien429d7192013-10-04 19:53:11 +0100597 } else {
598 // search for encoded delimiter or operator
599
600 const char *t = tok_enc;
Damien George54eb4e72014-07-03 13:47:47 +0100601 mp_uint_t tok_enc_index = 0;
Damien429d7192013-10-04 19:53:11 +0100602 for (; *t != 0 && !is_char(lex, *t); t += 1) {
603 if (*t == 'e' || *t == 'c') {
604 t += 1;
605 } else if (*t == 'E') {
606 tok_enc_index -= 1;
607 t += 1;
608 }
609 tok_enc_index += 1;
610 }
611
612 next_char(lex);
613
614 if (*t == 0) {
615 // didn't match any delimiter or operator characters
Damien Georgea4c52c52014-12-05 19:35:18 +0000616 lex->tok_kind = MP_TOKEN_INVALID;
Damien429d7192013-10-04 19:53:11 +0100617
618 } else {
619 // matched a delimiter or operator character
620
621 // get the maximum characters for a valid token
622 t += 1;
Damien George54eb4e72014-07-03 13:47:47 +0100623 mp_uint_t t_index = tok_enc_index;
Damien429d7192013-10-04 19:53:11 +0100624 for (;;) {
625 for (; *t == 'e'; t += 1) {
626 t += 1;
627 t_index += 1;
628 if (is_char(lex, *t)) {
629 next_char(lex);
630 tok_enc_index = t_index;
631 break;
632 }
633 }
634
635 if (*t == 'E') {
636 t += 1;
637 if (is_char(lex, *t)) {
638 next_char(lex);
639 tok_enc_index = t_index;
640 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000641 lex->tok_kind = MP_TOKEN_INVALID;
Damien George2e9eb2d2014-04-10 12:19:33 +0100642 goto tok_enc_no_match;
Damien429d7192013-10-04 19:53:11 +0100643 }
644 break;
645 }
646
647 if (*t == 'c') {
648 t += 1;
649 t_index += 1;
650 if (is_char(lex, *t)) {
651 next_char(lex);
652 tok_enc_index = t_index;
653 t += 1;
654 } else {
655 break;
656 }
657 } else {
658 break;
659 }
660 }
661
662 // set token kind
Damien Georgea4c52c52014-12-05 19:35:18 +0000663 lex->tok_kind = tok_enc_kind[tok_enc_index];
Damien429d7192013-10-04 19:53:11 +0100664
Damien George2e9eb2d2014-04-10 12:19:33 +0100665 tok_enc_no_match:
666
Damien429d7192013-10-04 19:53:11 +0100667 // compute bracket level for implicit line joining
Damien Georgea4c52c52014-12-05 19:35:18 +0000668 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 +0100669 lex->nested_bracket_level += 1;
Damien Georgea4c52c52014-12-05 19:35:18 +0000670 } 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 +0100671 lex->nested_bracket_level -= 1;
672 }
673 }
674 }
Damien429d7192013-10-04 19:53:11 +0100675}
676
Damien George5bdf1652016-11-16 18:27:20 +1100677mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
Damien George9bf5f282014-10-09 16:53:37 +0100678 mp_lexer_t *lex = m_new_obj_maybe(mp_lexer_t);
Damien Georgee1199ec2014-05-10 17:48:01 +0100679
680 // check for memory allocation error
681 if (lex == NULL) {
Damien George5bdf1652016-11-16 18:27:20 +1100682 reader.close(reader.data);
Damien Georgee1199ec2014-05-10 17:48:01 +0100683 return NULL;
684 }
Damien429d7192013-10-04 19:53:11 +0100685
Damien Georgeb829b5c2014-01-25 13:51:19 +0000686 lex->source_name = src_name;
Damien George5bdf1652016-11-16 18:27:20 +1100687 lex->reader = reader;
Damien429d7192013-10-04 19:53:11 +0100688 lex->line = 1;
689 lex->column = 1;
Damien429d7192013-10-04 19:53:11 +0100690 lex->emit_dent = 0;
691 lex->nested_bracket_level = 0;
Damien George58ebde42014-05-21 20:32:59 +0100692 lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT;
Damien429d7192013-10-04 19:53:11 +0100693 lex->num_indent_level = 1;
Damien Georgee1199ec2014-05-10 17:48:01 +0100694 lex->indent_level = m_new_maybe(uint16_t, lex->alloc_indent_level);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200695 vstr_init(&lex->vstr, 32);
Damien429d7192013-10-04 19:53:11 +0100696
Damien Georgee1199ec2014-05-10 17:48:01 +0100697 // check for memory allocation error
Damien George98b30722017-02-17 10:56:06 +1100698 // note: vstr_init above may fail on malloc, but so may mp_lexer_to_next below
Damien George5da0d292016-09-19 11:17:02 +1000699 if (lex->indent_level == NULL) {
Damien Georgee1199ec2014-05-10 17:48:01 +0100700 mp_lexer_free(lex);
701 return NULL;
702 }
703
704 // store sentinel for first indentation level
705 lex->indent_level[0] = 0;
706
Damien429d7192013-10-04 19:53:11 +0100707 // preload characters
Damien George5bdf1652016-11-16 18:27:20 +1100708 lex->chr0 = reader.readbyte(reader.data);
709 lex->chr1 = reader.readbyte(reader.data);
710 lex->chr2 = reader.readbyte(reader.data);
Damiena5185f42013-10-20 14:41:27 +0100711
712 // 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 +0100713 if (lex->chr0 == MP_LEXER_EOF) {
Damiena5185f42013-10-20 14:41:27 +0100714 lex->chr0 = '\n';
Damien George94fbe972014-07-30 11:46:05 +0100715 } else if (lex->chr1 == MP_LEXER_EOF) {
Damien George32bade12015-01-30 00:27:46 +0000716 if (lex->chr0 == '\r') {
717 lex->chr0 = '\n';
718 } else if (lex->chr0 != '\n') {
Damiena5185f42013-10-20 14:41:27 +0100719 lex->chr1 = '\n';
Damien429d7192013-10-04 19:53:11 +0100720 }
Damien George94fbe972014-07-30 11:46:05 +0100721 } else if (lex->chr2 == MP_LEXER_EOF) {
Damien George32bade12015-01-30 00:27:46 +0000722 if (lex->chr1 == '\r') {
723 lex->chr1 = '\n';
724 } else if (lex->chr1 != '\n') {
Damiena5185f42013-10-20 14:41:27 +0100725 lex->chr2 = '\n';
Damien429d7192013-10-04 19:53:11 +0100726 }
Damien429d7192013-10-04 19:53:11 +0100727 }
728
Damiena5185f42013-10-20 14:41:27 +0100729 // preload first token
Damien George98b30722017-02-17 10:56:06 +1100730 mp_lexer_to_next(lex);
731
732 // Check that the first token is in the first column. If it's not then we
733 // convert the token kind to INDENT so that the parser gives a syntax error.
734 if (lex->tok_column != 1) {
735 lex->tok_kind = MP_TOKEN_INDENT;
736 }
Damien429d7192013-10-04 19:53:11 +0100737
738 return lex;
739}
740
Damien George511c0832016-11-16 16:22:08 +1100741mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) {
742 mp_reader_t reader;
743 if (!mp_reader_new_mem(&reader, (const byte*)str, len, free_len)) {
744 return NULL;
745 }
Damien George5bdf1652016-11-16 18:27:20 +1100746 return mp_lexer_new(src_name, reader);
Damien George511c0832016-11-16 16:22:08 +1100747}
748
Damien George8beba732017-01-29 15:16:51 +1100749#if MICROPY_READER_POSIX || MICROPY_READER_VFS
Damien Georgee5ef15a2016-11-16 16:25:06 +1100750
751mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
752 mp_reader_t reader;
753 int ret = mp_reader_new_file(&reader, filename);
754 if (ret != 0) {
755 return NULL;
756 }
Damien George5bdf1652016-11-16 18:27:20 +1100757 return mp_lexer_new(qstr_from_str(filename), reader);
Damien Georgee5ef15a2016-11-16 16:25:06 +1100758}
759
Damien George66d955c2016-11-16 18:12:55 +1100760#if MICROPY_HELPER_LEXER_UNIX
761
762mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd) {
763 mp_reader_t reader;
764 int ret = mp_reader_new_file_from_fd(&reader, fd, close_fd);
765 if (ret != 0) {
766 return NULL;
767 }
Damien George5bdf1652016-11-16 18:27:20 +1100768 return mp_lexer_new(filename, reader);
Damien George66d955c2016-11-16 18:12:55 +1100769}
770
771#endif
772
Damien Georgee5ef15a2016-11-16 16:25:06 +1100773#endif
774
Damiend99b0522013-12-21 18:17:45 +0000775void mp_lexer_free(mp_lexer_t *lex) {
Damiena5185f42013-10-20 14:41:27 +0100776 if (lex) {
Damien George5bdf1652016-11-16 18:27:20 +1100777 lex->reader.close(lex->reader.data);
Damienbb5316b2013-10-22 21:12:29 +0100778 vstr_clear(&lex->vstr);
Paul Sokolovsky624ed5d2014-01-23 22:25:57 +0200779 m_del(uint16_t, lex->indent_level, lex->alloc_indent_level);
Damien732407f2013-12-29 19:33:23 +0000780 m_del_obj(mp_lexer_t, lex);
Damien429d7192013-10-04 19:53:11 +0100781 }
Damien429d7192013-10-04 19:53:11 +0100782}
783
Damien Georgec305ae32016-12-22 10:49:54 +1100784#if 0
785// This function is used to print the current token and should only be
786// needed to debug the lexer, so it's not available via a config option.
Damien Georgea4c52c52014-12-05 19:35:18 +0000787void mp_lexer_show_token(const mp_lexer_t *lex) {
Damien George451a0872014-12-05 22:50:16 +0000788 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 +0000789 if (lex->vstr.len > 0) {
790 const byte *i = (const byte *)lex->vstr.buf;
791 const byte *j = (const byte *)i + lex->vstr.len;
792 printf(" ");
793 while (i < j) {
794 unichar c = utf8_get_char(i);
795 i = utf8_next_char(i);
796 if (unichar_isprint(c)) {
Damien George7f19a392015-06-22 17:40:12 +0100797 printf("%c", (int)c);
Damien Georgea4c52c52014-12-05 19:35:18 +0000798 } else {
799 printf("?");
800 }
801 }
802 }
803 printf("\n");
Damien429d7192013-10-04 19:53:11 +0100804}
Damien Georgea4c52c52014-12-05 19:35:18 +0000805#endif
Damien Georgedd5353a2015-12-18 12:35:44 +0000806
807#endif // MICROPY_ENABLE_COMPILER