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