blob: 0b60569d0fb4c4f75abe997e6d6b034a8d4fa16d [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 *
Damien George4735c452015-04-21 16:43:18 +00006 * Copyright (c) 2013-2015 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
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
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
29#include <stdio.h>
Damien George7dbf74c2016-01-08 13:42:00 +000030#include <unistd.h> // for ssize_t
Damien429d7192013-10-04 19:53:11 +010031#include <assert.h>
Damien George5042bce2014-05-25 22:06:06 +010032#include <string.h>
Damien429d7192013-10-04 19:53:11 +010033
Damien George0bfc7632015-02-07 18:33:58 +000034#include "py/nlr.h"
Damien George51dfcb42015-01-01 20:27:54 +000035#include "py/lexer.h"
36#include "py/parse.h"
37#include "py/parsenum.h"
Damien George22b22652016-01-07 14:40:35 +000038#include "py/runtime0.h"
Damien George64f2b212015-10-08 14:58:15 +010039#include "py/runtime.h"
Damien George22b22652016-01-07 14:40:35 +000040#include "py/objint.h"
Damien George64f2b212015-10-08 14:58:15 +010041#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010042
Damien Georgedd5353a2015-12-18 12:35:44 +000043#if MICROPY_ENABLE_COMPILER
44
Damien429d7192013-10-04 19:53:11 +010045#define RULE_ACT_ARG_MASK (0x0f)
Damien Georgeb47ea4e2014-12-20 18:37:50 +000046#define RULE_ACT_KIND_MASK (0x30)
47#define RULE_ACT_ALLOW_IDENT (0x40)
48#define RULE_ACT_ADD_BLANK (0x80)
Damien429d7192013-10-04 19:53:11 +010049#define RULE_ACT_OR (0x10)
50#define RULE_ACT_AND (0x20)
51#define RULE_ACT_LIST (0x30)
52
Damien429d7192013-10-04 19:53:11 +010053#define RULE_ARG_KIND_MASK (0xf000)
54#define RULE_ARG_ARG_MASK (0x0fff)
55#define RULE_ARG_TOK (0x1000)
56#define RULE_ARG_RULE (0x2000)
Damien George4735c452015-04-21 16:43:18 +000057#define RULE_ARG_OPT_RULE (0x3000)
Damien429d7192013-10-04 19:53:11 +010058
59// (un)comment to use rule names; for debugging
60//#define USE_RULE_NAME (1)
61
62typedef struct _rule_t {
63 byte rule_id;
64 byte act;
65#ifdef USE_RULE_NAME
66 const char *rule_name;
67#endif
68 uint16_t arg[];
69} rule_t;
70
71enum {
Damien George00208ce2014-01-23 00:00:53 +000072#define DEF_RULE(rule, comp, kind, ...) RULE_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000073#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010074#undef DEF_RULE
75 RULE_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010076 RULE_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000077 RULE_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000078 RULE_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010079};
80
81#define or(n) (RULE_ACT_OR | n)
82#define and(n) (RULE_ACT_AND | n)
Damien George0c1de1c2016-04-14 13:23:50 +010083#define and_ident(n) (RULE_ACT_AND | n | RULE_ACT_ALLOW_IDENT)
84#define and_blank(n) (RULE_ACT_AND | n | RULE_ACT_ADD_BLANK)
Damien429d7192013-10-04 19:53:11 +010085#define one_or_more (RULE_ACT_LIST | 2)
86#define list (RULE_ACT_LIST | 1)
87#define list_with_end (RULE_ACT_LIST | 3)
Damiend99b0522013-12-21 18:17:45 +000088#define tok(t) (RULE_ARG_TOK | MP_TOKEN_##t)
Damien429d7192013-10-04 19:53:11 +010089#define rule(r) (RULE_ARG_RULE | RULE_##r)
Damien429d7192013-10-04 19:53:11 +010090#define opt_rule(r) (RULE_ARG_OPT_RULE | RULE_##r)
91#ifdef USE_RULE_NAME
Damien George00208ce2014-01-23 00:00:53 +000092#define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, #rule, { __VA_ARGS__ } };
Damien429d7192013-10-04 19:53:11 +010093#else
Damien George00208ce2014-01-23 00:00:53 +000094#define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } };
Damien429d7192013-10-04 19:53:11 +010095#endif
Damien George51dfcb42015-01-01 20:27:54 +000096#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010097#undef or
98#undef and
99#undef list
100#undef list_with_end
101#undef tok
102#undef rule
Damien429d7192013-10-04 19:53:11 +0100103#undef opt_rule
104#undef one_or_more
105#undef DEF_RULE
106
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200107STATIC const rule_t *rules[] = {
Damien George00208ce2014-01-23 00:00:53 +0000108#define DEF_RULE(rule, comp, kind, ...) &rule_##rule,
Damien George51dfcb42015-01-01 20:27:54 +0000109#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +0100110#undef DEF_RULE
111};
112
113typedef struct _rule_stack_t {
Damien George16a6a472015-12-17 13:06:05 +0000114 size_t src_line : 8 * sizeof(size_t) - 8; // maximum bits storing source line number
115 size_t rule_id : 8; // this must be large enough to fit largest rule number
116 size_t arg_i; // this dictates the maximum nodes in a "list" of things
Damien429d7192013-10-04 19:53:11 +0100117} rule_stack_t;
118
Damien George58e0f4a2015-09-23 10:50:43 +0100119typedef struct _mp_parse_chunk_t {
Damien George16a6a472015-12-17 13:06:05 +0000120 size_t alloc;
Damien George58e0f4a2015-09-23 10:50:43 +0100121 union {
Damien George16a6a472015-12-17 13:06:05 +0000122 size_t used;
Damien George58e0f4a2015-09-23 10:50:43 +0100123 struct _mp_parse_chunk_t *next;
124 } union_;
125 byte data[];
126} mp_parse_chunk_t;
127
Damien George64f2b212015-10-08 14:58:15 +0100128typedef enum {
129 PARSE_ERROR_NONE = 0,
130 PARSE_ERROR_MEMORY,
131 PARSE_ERROR_CONST,
132} parse_error_t;
133
Damien429d7192013-10-04 19:53:11 +0100134typedef struct _parser_t {
Damien George64f2b212015-10-08 14:58:15 +0100135 parse_error_t parse_error;
Damien George58ba4c32014-04-10 14:27:31 +0000136
Damien George16a6a472015-12-17 13:06:05 +0000137 size_t rule_stack_alloc;
138 size_t rule_stack_top;
Damien429d7192013-10-04 19:53:11 +0100139 rule_stack_t *rule_stack;
140
Damien George16a6a472015-12-17 13:06:05 +0000141 size_t result_stack_alloc;
142 size_t result_stack_top;
Damiend99b0522013-12-21 18:17:45 +0000143 mp_parse_node_t *result_stack;
Damien George08335002014-01-18 23:24:36 +0000144
145 mp_lexer_t *lexer;
Damien George58e0f4a2015-09-23 10:50:43 +0100146
147 mp_parse_tree_t tree;
148 mp_parse_chunk_t *cur_chunk;
Damien429d7192013-10-04 19:53:11 +0100149
Damien George64f2b212015-10-08 14:58:15 +0100150 #if MICROPY_COMP_CONST
151 mp_map_t consts;
152 #endif
153} parser_t;
Damien George58ba4c32014-04-10 14:27:31 +0000154
Damien George58e0f4a2015-09-23 10:50:43 +0100155STATIC void *parser_alloc(parser_t *parser, size_t num_bytes) {
156 // use a custom memory allocator to store parse nodes sequentially in large chunks
157
158 mp_parse_chunk_t *chunk = parser->cur_chunk;
159
160 if (chunk != NULL && chunk->union_.used + num_bytes > chunk->alloc) {
161 // not enough room at end of previously allocated chunk so try to grow
162 mp_parse_chunk_t *new_data = (mp_parse_chunk_t*)m_renew_maybe(byte, chunk,
163 sizeof(mp_parse_chunk_t) + chunk->alloc,
164 sizeof(mp_parse_chunk_t) + chunk->alloc + num_bytes, false);
165 if (new_data == NULL) {
166 // could not grow existing memory; shrink it to fit previous
Damien Georged6c558c2016-02-23 13:44:29 +0000167 (void)m_renew_maybe(byte, chunk, sizeof(mp_parse_chunk_t) + chunk->alloc,
168 sizeof(mp_parse_chunk_t) + chunk->union_.used, false);
Damien George58e0f4a2015-09-23 10:50:43 +0100169 chunk->alloc = chunk->union_.used;
170 chunk->union_.next = parser->tree.chunk;
171 parser->tree.chunk = chunk;
172 chunk = NULL;
173 } else {
174 // could grow existing memory
175 chunk->alloc += num_bytes;
176 }
177 }
178
179 if (chunk == NULL) {
180 // no previous chunk, allocate a new chunk
181 size_t alloc = MICROPY_ALLOC_PARSE_CHUNK_INIT;
182 if (alloc < num_bytes) {
183 alloc = num_bytes;
184 }
185 chunk = (mp_parse_chunk_t*)m_new(byte, sizeof(mp_parse_chunk_t) + alloc);
186 chunk->alloc = alloc;
187 chunk->union_.used = 0;
188 parser->cur_chunk = chunk;
189 }
190
191 byte *ret = chunk->data + chunk->union_.used;
192 chunk->union_.used += num_bytes;
193 return ret;
194}
195
Damien George16a6a472015-12-17 13:06:05 +0000196STATIC void push_rule(parser_t *parser, size_t src_line, const rule_t *rule, size_t arg_i) {
Damien George64f2b212015-10-08 14:58:15 +0100197 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000198 return;
199 }
Damien429d7192013-10-04 19:53:11 +0100200 if (parser->rule_stack_top >= parser->rule_stack_alloc) {
Damien Georgeade9a052015-06-13 21:53:22 +0100201 rule_stack_t *rs = m_renew_maybe(rule_stack_t, parser->rule_stack, parser->rule_stack_alloc, parser->rule_stack_alloc + MICROPY_ALLOC_PARSE_RULE_INC, true);
Damien George58ba4c32014-04-10 14:27:31 +0000202 if (rs == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100203 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000204 return;
205 }
206 parser->rule_stack = rs;
Damien George58ebde42014-05-21 20:32:59 +0100207 parser->rule_stack_alloc += MICROPY_ALLOC_PARSE_RULE_INC;
Damien429d7192013-10-04 19:53:11 +0100208 }
Damien George08335002014-01-18 23:24:36 +0000209 rule_stack_t *rs = &parser->rule_stack[parser->rule_stack_top++];
210 rs->src_line = src_line;
211 rs->rule_id = rule->rule_id;
212 rs->arg_i = arg_i;
Damien429d7192013-10-04 19:53:11 +0100213}
214
Damien George16a6a472015-12-17 13:06:05 +0000215STATIC void push_rule_from_arg(parser_t *parser, size_t arg) {
Damien429d7192013-10-04 19:53:11 +0100216 assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE || (arg & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE);
Damien George16a6a472015-12-17 13:06:05 +0000217 size_t rule_id = arg & RULE_ARG_ARG_MASK;
Damien429d7192013-10-04 19:53:11 +0100218 assert(rule_id < RULE_maximum_number_of);
Damien Georgea4c52c52014-12-05 19:35:18 +0000219 push_rule(parser, parser->lexer->tok_line, rules[rule_id], 0);
Damien429d7192013-10-04 19:53:11 +0100220}
221
Damien George16a6a472015-12-17 13:06:05 +0000222STATIC void pop_rule(parser_t *parser, const rule_t **rule, size_t *arg_i, size_t *src_line) {
Damien George64f2b212015-10-08 14:58:15 +0100223 assert(!parser->parse_error);
Damien429d7192013-10-04 19:53:11 +0100224 parser->rule_stack_top -= 1;
225 *rule = rules[parser->rule_stack[parser->rule_stack_top].rule_id];
226 *arg_i = parser->rule_stack[parser->rule_stack_top].arg_i;
Damien George08335002014-01-18 23:24:36 +0000227 *src_line = parser->rule_stack[parser->rule_stack_top].src_line;
Damien429d7192013-10-04 19:53:11 +0100228}
229
Damien George16a6a472015-12-17 13:06:05 +0000230mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200231 if (kind == MP_PARSE_NODE_SMALL_INT) {
232 return (mp_parse_node_t)(kind | (arg << 1));
233 }
Damien George7d414a12015-02-08 01:57:40 +0000234 return (mp_parse_node_t)(kind | (arg << 4));
Damien429d7192013-10-04 19:53:11 +0100235}
236
Damien George22b22652016-01-07 14:40:35 +0000237bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
238 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
239 *o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn));
240 return true;
241 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, RULE_const_object)) {
242 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
243 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
244 // nodes are 32-bit pointers, but need to extract 64-bit object
245 *o = (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
246 #else
247 *o = (mp_obj_t)pns->nodes[0];
248 #endif
249 return MP_OBJ_IS_INT(*o);
250 } else {
251 return false;
252 }
253}
254
Damien George16a6a472015-12-17 13:06:05 +0000255int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes) {
Damien Georgedfe944c2015-02-13 02:29:46 +0000256 if (MP_PARSE_NODE_IS_NULL(*pn)) {
257 *nodes = NULL;
258 return 0;
259 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
260 *nodes = pn;
261 return 1;
262 } else {
263 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
264 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
265 *nodes = pn;
266 return 1;
267 } else {
268 *nodes = pns->nodes;
269 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
270 }
271 }
272}
273
Damien Georgecbd2f742014-01-19 11:48:48 +0000274#if MICROPY_DEBUG_PRINTERS
Damien George16a6a472015-12-17 13:06:05 +0000275void mp_parse_node_print(mp_parse_node_t pn, size_t indent) {
Damien George08335002014-01-18 23:24:36 +0000276 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
277 printf("[% 4d] ", (int)((mp_parse_node_struct_t*)pn)->source_line);
278 } else {
279 printf(" ");
280 }
Damien George16a6a472015-12-17 13:06:05 +0000281 for (size_t i = 0; i < indent; i++) {
Damien429d7192013-10-04 19:53:11 +0100282 printf(" ");
283 }
Damiend99b0522013-12-21 18:17:45 +0000284 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100285 printf("NULL\n");
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200286 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +0100287 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200288 printf("int(" INT_FMT ")\n", arg);
Damiend99b0522013-12-21 18:17:45 +0000289 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George16a6a472015-12-17 13:06:05 +0000290 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000291 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
292 case MP_PARSE_NODE_ID: printf("id(%s)\n", qstr_str(arg)); break;
Damiend99b0522013-12-21 18:17:45 +0000293 case MP_PARSE_NODE_STRING: printf("str(%s)\n", qstr_str(arg)); break;
294 case MP_PARSE_NODE_BYTES: printf("bytes(%s)\n", qstr_str(arg)); break;
Damien George16a6a472015-12-17 13:06:05 +0000295 case MP_PARSE_NODE_TOKEN: printf("tok(%u)\n", (uint)arg); break;
Damien429d7192013-10-04 19:53:11 +0100296 default: assert(0);
297 }
298 } else {
Damien George5042bce2014-05-25 22:06:06 +0100299 // node must be a mp_parse_node_struct_t
Damien Georgeb829b5c2014-01-25 13:51:19 +0000300 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +0100301 if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
302 printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
Damien George4c81ba82015-01-13 16:21:23 +0000303 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_bytes) {
304 printf("literal bytes(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
Damien George7d414a12015-02-08 01:57:40 +0000305 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000306 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
307 printf("literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
308 #else
Damien George7d414a12015-02-08 01:57:40 +0000309 printf("literal const(%p)\n", (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000310 #endif
Damien George5042bce2014-05-25 22:06:06 +0100311 } else {
Damien George16a6a472015-12-17 13:06:05 +0000312 size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100313#ifdef USE_RULE_NAME
Damien George16a6a472015-12-17 13:06:05 +0000314 printf("%s(%u) (n=%u)\n", rules[MP_PARSE_NODE_STRUCT_KIND(pns)]->rule_name, (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n);
Damien429d7192013-10-04 19:53:11 +0100315#else
Damien George16a6a472015-12-17 13:06:05 +0000316 printf("rule(%u) (n=%u)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n);
Damien429d7192013-10-04 19:53:11 +0100317#endif
Damien George16a6a472015-12-17 13:06:05 +0000318 for (size_t i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +0100319 mp_parse_node_print(pns->nodes[i], indent + 2);
320 }
Damien429d7192013-10-04 19:53:11 +0100321 }
322 }
323}
Damien Georgecbd2f742014-01-19 11:48:48 +0000324#endif // MICROPY_DEBUG_PRINTERS
Damien429d7192013-10-04 19:53:11 +0100325
326/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200327STATIC void result_stack_show(parser_t *parser) {
Damien429d7192013-10-04 19:53:11 +0100328 printf("result stack, most recent first\n");
Damien George16a6a472015-12-17 13:06:05 +0000329 for (ssize_t i = parser->result_stack_top - 1; i >= 0; i--) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000330 mp_parse_node_print(parser->result_stack[i], 0);
Damien429d7192013-10-04 19:53:11 +0100331 }
332}
333*/
334
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200335STATIC mp_parse_node_t pop_result(parser_t *parser) {
Damien George64f2b212015-10-08 14:58:15 +0100336 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000337 return MP_PARSE_NODE_NULL;
338 }
Damien429d7192013-10-04 19:53:11 +0100339 assert(parser->result_stack_top > 0);
340 return parser->result_stack[--parser->result_stack_top];
341}
342
Damien George16a6a472015-12-17 13:06:05 +0000343STATIC mp_parse_node_t peek_result(parser_t *parser, size_t pos) {
Damien George64f2b212015-10-08 14:58:15 +0100344 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000345 return MP_PARSE_NODE_NULL;
346 }
Damien429d7192013-10-04 19:53:11 +0100347 assert(parser->result_stack_top > pos);
348 return parser->result_stack[parser->result_stack_top - 1 - pos];
349}
350
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200351STATIC void push_result_node(parser_t *parser, mp_parse_node_t pn) {
Damien George64f2b212015-10-08 14:58:15 +0100352 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000353 return;
354 }
Damien George69a818d2014-01-12 13:55:24 +0000355 if (parser->result_stack_top >= parser->result_stack_alloc) {
Damien Georgeade9a052015-06-13 21:53:22 +0100356 mp_parse_node_t *stack = m_renew_maybe(mp_parse_node_t, parser->result_stack, parser->result_stack_alloc, parser->result_stack_alloc + MICROPY_ALLOC_PARSE_RESULT_INC, true);
Damien George50912e72015-01-20 11:55:10 +0000357 if (stack == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100358 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000359 return;
360 }
Damien George50912e72015-01-20 11:55:10 +0000361 parser->result_stack = stack;
Damien George58ebde42014-05-21 20:32:59 +0100362 parser->result_stack_alloc += MICROPY_ALLOC_PARSE_RESULT_INC;
Damien George69a818d2014-01-12 13:55:24 +0000363 }
Damien429d7192013-10-04 19:53:11 +0100364 parser->result_stack[parser->result_stack_top++] = pn;
365}
366
Damien George16a6a472015-12-17 13:06:05 +0000367STATIC mp_parse_node_t make_node_string_bytes(parser_t *parser, size_t src_line, size_t rule_kind, const char *str, size_t len) {
Damien George58e0f4a2015-09-23 10:50:43 +0100368 mp_parse_node_struct_t *pn = parser_alloc(parser, sizeof(mp_parse_node_struct_t) + sizeof(mp_parse_node_t) * 2);
Damien George5042bce2014-05-25 22:06:06 +0100369 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100370 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George7d414a12015-02-08 01:57:40 +0000371 return MP_PARSE_NODE_NULL;
Damien George5042bce2014-05-25 22:06:06 +0100372 }
373 pn->source_line = src_line;
Damien George4c81ba82015-01-13 16:21:23 +0000374 pn->kind_num_nodes = rule_kind | (2 << 8);
Damien George5042bce2014-05-25 22:06:06 +0100375 char *p = m_new(char, len);
376 memcpy(p, str, len);
Damien George999cedb2015-11-27 17:01:44 +0000377 pn->nodes[0] = (uintptr_t)p;
Damien George5042bce2014-05-25 22:06:06 +0100378 pn->nodes[1] = len;
Damien George7d414a12015-02-08 01:57:40 +0000379 return (mp_parse_node_t)pn;
380}
381
Damien George16a6a472015-12-17 13:06:05 +0000382STATIC mp_parse_node_t make_node_const_object(parser_t *parser, size_t src_line, mp_obj_t obj) {
Damien George999cedb2015-11-27 17:01:44 +0000383 mp_parse_node_struct_t *pn = parser_alloc(parser, sizeof(mp_parse_node_struct_t) + sizeof(mp_obj_t));
Damien George7d414a12015-02-08 01:57:40 +0000384 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100385 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George7d414a12015-02-08 01:57:40 +0000386 return MP_PARSE_NODE_NULL;
387 }
388 pn->source_line = src_line;
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000389 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
390 // nodes are 32-bit pointers, but need to store 64-bit object
391 pn->kind_num_nodes = RULE_const_object | (2 << 8);
392 pn->nodes[0] = (uint64_t)obj;
393 pn->nodes[1] = (uint64_t)obj >> 32;
394 #else
Damien George7d414a12015-02-08 01:57:40 +0000395 pn->kind_num_nodes = RULE_const_object | (1 << 8);
Damien George16a6a472015-12-17 13:06:05 +0000396 pn->nodes[0] = (uintptr_t)obj;
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000397 #endif
Damien George7d414a12015-02-08 01:57:40 +0000398 return (mp_parse_node_t)pn;
Damien George5042bce2014-05-25 22:06:06 +0100399}
Paul Sokolovsky9e76b112014-05-08 22:43:46 +0300400
Damien Georgea4c52c52014-12-05 19:35:18 +0000401STATIC void push_result_token(parser_t *parser) {
Damiend99b0522013-12-21 18:17:45 +0000402 mp_parse_node_t pn;
Damien Georgea4c52c52014-12-05 19:35:18 +0000403 mp_lexer_t *lex = parser->lexer;
404 if (lex->tok_kind == MP_TOKEN_NAME) {
Damien George64f2b212015-10-08 14:58:15 +0100405 qstr id = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
406 #if MICROPY_COMP_CONST
407 // lookup identifier in table of dynamic constants
408 mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP);
409 if (elem != NULL) {
410 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
411 } else
412 #endif
413 {
414 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, id);
415 }
Damien George7d414a12015-02-08 01:57:40 +0000416 } else if (lex->tok_kind == MP_TOKEN_INTEGER) {
417 mp_obj_t o = mp_parse_num_integer(lex->vstr.buf, lex->vstr.len, 0, lex);
418 if (MP_OBJ_IS_SMALL_INT(o)) {
419 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(o));
Damien429d7192013-10-04 19:53:11 +0100420 } else {
Damien George7d414a12015-02-08 01:57:40 +0000421 pn = make_node_const_object(parser, lex->tok_line, o);
Damien429d7192013-10-04 19:53:11 +0100422 }
Damien George7d414a12015-02-08 01:57:40 +0000423 } else if (lex->tok_kind == MP_TOKEN_FLOAT_OR_IMAG) {
424 mp_obj_t o = mp_parse_num_decimal(lex->vstr.buf, lex->vstr.len, true, false, lex);
425 pn = make_node_const_object(parser, lex->tok_line, o);
Damien George4c81ba82015-01-13 16:21:23 +0000426 } else if (lex->tok_kind == MP_TOKEN_STRING || lex->tok_kind == MP_TOKEN_BYTES) {
427 // Don't automatically intern all strings/bytes. doc strings (which are usually large)
Damien George5042bce2014-05-25 22:06:06 +0100428 // will be discarded by the compiler, and so we shouldn't intern them.
429 qstr qst = MP_QSTR_NULL;
Damien Georgea4c52c52014-12-05 19:35:18 +0000430 if (lex->vstr.len <= MICROPY_ALLOC_PARSE_INTERN_STRING_LEN) {
Damien George5042bce2014-05-25 22:06:06 +0100431 // intern short strings
Damien Georgea4c52c52014-12-05 19:35:18 +0000432 qst = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
Damien George5042bce2014-05-25 22:06:06 +0100433 } else {
434 // check if this string is already interned
Damien Georgea4c52c52014-12-05 19:35:18 +0000435 qst = qstr_find_strn(lex->vstr.buf, lex->vstr.len);
Damien George5042bce2014-05-25 22:06:06 +0100436 }
437 if (qst != MP_QSTR_NULL) {
438 // qstr exists, make a leaf node
Damien George4c81ba82015-01-13 16:21:23 +0000439 pn = mp_parse_node_new_leaf(lex->tok_kind == MP_TOKEN_STRING ? MP_PARSE_NODE_STRING : MP_PARSE_NODE_BYTES, qst);
Damien George5042bce2014-05-25 22:06:06 +0100440 } else {
Damien George4c81ba82015-01-13 16:21:23 +0000441 // not interned, make a node holding a pointer to the string/bytes data
Damien George7d414a12015-02-08 01:57:40 +0000442 pn = make_node_string_bytes(parser, lex->tok_line, lex->tok_kind == MP_TOKEN_STRING ? RULE_string : RULE_bytes, lex->vstr.buf, lex->vstr.len);
Damien George5042bce2014-05-25 22:06:06 +0100443 }
Damien429d7192013-10-04 19:53:11 +0100444 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000445 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_TOKEN, lex->tok_kind);
Damien429d7192013-10-04 19:53:11 +0100446 }
447 push_result_node(parser, pn);
448}
449
Damien George64f2b212015-10-08 14:58:15 +0100450#if MICROPY_COMP_MODULE_CONST
Damien Georgecbf76742015-11-27 13:38:15 +0000451STATIC const mp_rom_map_elem_t mp_constants_table[] = {
Damien Georgee36ff982016-05-10 23:23:50 +0100452 #if MICROPY_PY_UERRNO
453 { MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_module_uerrno) },
454 #endif
Damien George64f2b212015-10-08 14:58:15 +0100455 #if MICROPY_PY_UCTYPES
Damien Georgecbf76742015-11-27 13:38:15 +0000456 { MP_ROM_QSTR(MP_QSTR_uctypes), MP_ROM_PTR(&mp_module_uctypes) },
Damien George64f2b212015-10-08 14:58:15 +0100457 #endif
458 // Extra constants as defined by a port
459 MICROPY_PORT_CONSTANTS
460};
461STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
462#endif
463
464#if MICROPY_COMP_CONST_FOLDING
Damien George16a6a472015-12-17 13:06:05 +0000465STATIC bool fold_constants(parser_t *parser, const rule_t *rule, size_t num_args) {
Damien George64f2b212015-10-08 14:58:15 +0100466 // this code does folding of arbitrary integer expressions, eg 1 + 2 * 3 + 4
467 // it does not do partial folding, eg 1 + 2 + x -> 3 + x
468
Damien George22b22652016-01-07 14:40:35 +0000469 mp_obj_t arg0;
Damien George64f2b212015-10-08 14:58:15 +0100470 if (rule->rule_id == RULE_expr
471 || rule->rule_id == RULE_xor_expr
472 || rule->rule_id == RULE_and_expr) {
473 // folding for binary ops: | ^ &
474 mp_parse_node_t pn = peek_result(parser, num_args - 1);
Damien George22b22652016-01-07 14:40:35 +0000475 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100476 return false;
477 }
Damien George22b22652016-01-07 14:40:35 +0000478 mp_binary_op_t op;
479 if (rule->rule_id == RULE_expr) {
480 op = MP_BINARY_OP_OR;
481 } else if (rule->rule_id == RULE_xor_expr) {
482 op = MP_BINARY_OP_XOR;
483 } else {
484 op = MP_BINARY_OP_AND;
485 }
Damien George16a6a472015-12-17 13:06:05 +0000486 for (ssize_t i = num_args - 2; i >= 0; --i) {
Damien George64f2b212015-10-08 14:58:15 +0100487 pn = peek_result(parser, i);
Damien George22b22652016-01-07 14:40:35 +0000488 mp_obj_t arg1;
489 if (!mp_parse_node_get_int_maybe(pn, &arg1)) {
Damien George64f2b212015-10-08 14:58:15 +0100490 return false;
491 }
Damien George22b22652016-01-07 14:40:35 +0000492 arg0 = mp_binary_op(op, arg0, arg1);
Damien George64f2b212015-10-08 14:58:15 +0100493 }
494 } else if (rule->rule_id == RULE_shift_expr
495 || rule->rule_id == RULE_arith_expr
496 || rule->rule_id == RULE_term) {
497 // folding for binary ops: << >> + - * / % //
498 mp_parse_node_t pn = peek_result(parser, num_args - 1);
Damien George22b22652016-01-07 14:40:35 +0000499 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100500 return false;
501 }
Damien George16a6a472015-12-17 13:06:05 +0000502 for (ssize_t i = num_args - 2; i >= 1; i -= 2) {
Damien George64f2b212015-10-08 14:58:15 +0100503 pn = peek_result(parser, i - 1);
Damien George22b22652016-01-07 14:40:35 +0000504 mp_obj_t arg1;
505 if (!mp_parse_node_get_int_maybe(pn, &arg1)) {
Damien George64f2b212015-10-08 14:58:15 +0100506 return false;
507 }
Damien George64f2b212015-10-08 14:58:15 +0100508 mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, i));
Damien George22b22652016-01-07 14:40:35 +0000509 static const uint8_t token_to_op[] = {
510 MP_BINARY_OP_ADD,
511 MP_BINARY_OP_SUBTRACT,
512 MP_BINARY_OP_MULTIPLY,
513 255,//MP_BINARY_OP_POWER,
514 255,//MP_BINARY_OP_TRUE_DIVIDE,
515 MP_BINARY_OP_FLOOR_DIVIDE,
516 MP_BINARY_OP_MODULO,
517 255,//MP_BINARY_OP_LESS
518 MP_BINARY_OP_LSHIFT,
519 255,//MP_BINARY_OP_MORE
520 MP_BINARY_OP_RSHIFT,
521 };
522 mp_binary_op_t op = token_to_op[tok - MP_TOKEN_OP_PLUS];
Antonin ENFRUNefc971e2016-01-12 22:06:39 +0100523 if (op == (mp_binary_op_t)255) {
Damien George64f2b212015-10-08 14:58:15 +0100524 return false;
525 }
Damien George22b22652016-01-07 14:40:35 +0000526 int rhs_sign = mp_obj_int_sign(arg1);
527 if (op <= MP_BINARY_OP_RSHIFT) {
528 // << and >> can't have negative rhs
529 if (rhs_sign < 0) {
530 return false;
531 }
532 } else if (op >= MP_BINARY_OP_FLOOR_DIVIDE) {
533 // % and // can't have zero rhs
534 if (rhs_sign == 0) {
535 return false;
536 }
537 }
538 arg0 = mp_binary_op(op, arg0, arg1);
Damien George64f2b212015-10-08 14:58:15 +0100539 }
540 } else if (rule->rule_id == RULE_factor_2) {
541 // folding for unary ops: + - ~
542 mp_parse_node_t pn = peek_result(parser, 0);
Damien George22b22652016-01-07 14:40:35 +0000543 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100544 return false;
545 }
Damien George64f2b212015-10-08 14:58:15 +0100546 mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, 1));
Antonin ENFRUNefc971e2016-01-12 22:06:39 +0100547 mp_unary_op_t op;
Damien George64f2b212015-10-08 14:58:15 +0100548 if (tok == MP_TOKEN_OP_PLUS) {
Damien George22b22652016-01-07 14:40:35 +0000549 op = MP_UNARY_OP_POSITIVE;
Damien George64f2b212015-10-08 14:58:15 +0100550 } else if (tok == MP_TOKEN_OP_MINUS) {
Damien George22b22652016-01-07 14:40:35 +0000551 op = MP_UNARY_OP_NEGATIVE;
Damien George64f2b212015-10-08 14:58:15 +0100552 } else {
553 assert(tok == MP_TOKEN_OP_TILDE); // should be
Damien George22b22652016-01-07 14:40:35 +0000554 op = MP_UNARY_OP_INVERT;
Damien George64f2b212015-10-08 14:58:15 +0100555 }
Damien George22b22652016-01-07 14:40:35 +0000556 arg0 = mp_unary_op(op, arg0);
Damien George64f2b212015-10-08 14:58:15 +0100557
558 #if MICROPY_COMP_CONST
559 } else if (rule->rule_id == RULE_expr_stmt) {
560 mp_parse_node_t pn1 = peek_result(parser, 0);
561 if (!MP_PARSE_NODE_IS_NULL(pn1)
562 && !(MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_augassign)
563 || MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_assign_list))) {
564 // this node is of the form <x> = <y>
565 mp_parse_node_t pn0 = peek_result(parser, 1);
566 if (MP_PARSE_NODE_IS_ID(pn0)
Damien Georgeeacbd7a2016-04-13 15:21:47 +0100567 && MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_atom_expr_normal)
Damien George64f2b212015-10-08 14:58:15 +0100568 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pn1)->nodes[0])
569 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn1)->nodes[0]) == MP_QSTR_const
570 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pn1)->nodes[1], RULE_trailer_paren)
Damien George64f2b212015-10-08 14:58:15 +0100571 ) {
572 // code to assign dynamic constants: id = const(value)
573
574 // get the id
575 qstr id = MP_PARSE_NODE_LEAF_ARG(pn0);
576
577 // get the value
578 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pn1)->nodes[1])->nodes[0];
579 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
580 parser->parse_error = PARSE_ERROR_CONST;
581 return false;
582 }
583 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
584
585 // store the value in the table of dynamic constants
586 mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
587 assert(elem->value == MP_OBJ_NULL);
588 elem->value = MP_OBJ_NEW_SMALL_INT(value);
589
590 // replace const(value) with value
591 pop_result(parser);
592 push_result_node(parser, pn_value);
593
594 // finished folding this assignment, but we still want it to be part of the tree
595 return false;
596 }
597 }
598 return false;
599 #endif
600
601 #if MICROPY_COMP_MODULE_CONST
Damien Georgeeacbd7a2016-04-13 15:21:47 +0100602 } else if (rule->rule_id == RULE_atom_expr_normal) {
603 mp_parse_node_t pn0 = peek_result(parser, 1);
604 mp_parse_node_t pn1 = peek_result(parser, 0);
Damien George64f2b212015-10-08 14:58:15 +0100605 if (!(MP_PARSE_NODE_IS_ID(pn0)
Damien Georgeeacbd7a2016-04-13 15:21:47 +0100606 && MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_trailer_period))) {
Damien George64f2b212015-10-08 14:58:15 +0100607 return false;
608 }
609 // id1.id2
610 // look it up in constant table, see if it can be replaced with an integer
611 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pn1;
612 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
613 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pn0);
614 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
615 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
616 if (elem == NULL) {
617 return false;
618 }
619 mp_obj_t dest[2];
620 mp_load_method_maybe(elem->value, q_attr, dest);
Damien George8d4d6732016-03-19 21:36:32 +0000621 if (!(dest[0] != MP_OBJ_NULL && MP_OBJ_IS_INT(dest[0]) && dest[1] == MP_OBJ_NULL)) {
Damien George64f2b212015-10-08 14:58:15 +0100622 return false;
623 }
Damien George22b22652016-01-07 14:40:35 +0000624 arg0 = dest[0];
Damien George64f2b212015-10-08 14:58:15 +0100625 #endif
626
627 } else {
628 return false;
629 }
630
631 // success folding this rule
632
Damien George16a6a472015-12-17 13:06:05 +0000633 for (size_t i = num_args; i > 0; i--) {
Damien George64f2b212015-10-08 14:58:15 +0100634 pop_result(parser);
635 }
Damien George22b22652016-01-07 14:40:35 +0000636 if (MP_OBJ_IS_SMALL_INT(arg0)) {
637 push_result_node(parser, mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(arg0)));
638 } else {
639 // TODO reuse memory for parse node struct?
640 push_result_node(parser, make_node_const_object(parser, 0, arg0));
641 }
Damien George64f2b212015-10-08 14:58:15 +0100642
643 return true;
644}
645#endif
646
Damien George16a6a472015-12-17 13:06:05 +0000647STATIC void push_result_rule(parser_t *parser, size_t src_line, const rule_t *rule, size_t num_args) {
Damien George93b37262016-01-07 13:07:52 +0000648 // optimise away parenthesis around an expression if possible
649 if (rule->rule_id == RULE_atom_paren) {
650 // there should be just 1 arg for this rule
651 mp_parse_node_t pn = peek_result(parser, 0);
652 if (MP_PARSE_NODE_IS_NULL(pn)) {
653 // need to keep parenthesis for ()
654 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, RULE_testlist_comp)) {
655 // need to keep parenthesis for (a, b, ...)
656 } else {
657 // parenthesis around a single expression, so it's just the expression
658 return;
659 }
660 }
661
Damien George64f2b212015-10-08 14:58:15 +0100662 #if MICROPY_COMP_CONST_FOLDING
663 if (fold_constants(parser, rule, num_args)) {
664 // we folded this rule so return straight away
665 return;
666 }
667 #endif
668
Damien George58e0f4a2015-09-23 10:50:43 +0100669 mp_parse_node_struct_t *pn = parser_alloc(parser, sizeof(mp_parse_node_struct_t) + sizeof(mp_parse_node_t) * num_args);
Damien George58ba4c32014-04-10 14:27:31 +0000670 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100671 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000672 return;
673 }
674 pn->source_line = src_line;
675 pn->kind_num_nodes = (rule->rule_id & 0xff) | (num_args << 8);
Damien George16a6a472015-12-17 13:06:05 +0000676 for (size_t i = num_args; i > 0; i--) {
Damien429d7192013-10-04 19:53:11 +0100677 pn->nodes[i - 1] = pop_result(parser);
678 }
Damiend99b0522013-12-21 18:17:45 +0000679 push_result_node(parser, (mp_parse_node_t)pn);
Damien429d7192013-10-04 19:53:11 +0100680}
681
Damien George58e0f4a2015-09-23 10:50:43 +0100682mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
Damien George69a818d2014-01-12 13:55:24 +0000683
Damien George1b82e9a2014-05-10 17:36:41 +0100684 // initialise parser and allocate memory for its stacks
Damien George69a818d2014-01-12 13:55:24 +0000685
Damien George1b82e9a2014-05-10 17:36:41 +0100686 parser_t parser;
Damien George69a818d2014-01-12 13:55:24 +0000687
Damien George64f2b212015-10-08 14:58:15 +0100688 parser.parse_error = PARSE_ERROR_NONE;
Damien George58ba4c32014-04-10 14:27:31 +0000689
Damien George58ebde42014-05-21 20:32:59 +0100690 parser.rule_stack_alloc = MICROPY_ALLOC_PARSE_RULE_INIT;
Damien George1b82e9a2014-05-10 17:36:41 +0100691 parser.rule_stack_top = 0;
692 parser.rule_stack = m_new_maybe(rule_stack_t, parser.rule_stack_alloc);
Damien429d7192013-10-04 19:53:11 +0100693
Damien George58ebde42014-05-21 20:32:59 +0100694 parser.result_stack_alloc = MICROPY_ALLOC_PARSE_RESULT_INIT;
Damien George1b82e9a2014-05-10 17:36:41 +0100695 parser.result_stack_top = 0;
696 parser.result_stack = m_new_maybe(mp_parse_node_t, parser.result_stack_alloc);
Damien429d7192013-10-04 19:53:11 +0100697
Damien George1b82e9a2014-05-10 17:36:41 +0100698 parser.lexer = lex;
699
Damien George58e0f4a2015-09-23 10:50:43 +0100700 parser.tree.chunk = NULL;
701 parser.cur_chunk = NULL;
702
Damien George64f2b212015-10-08 14:58:15 +0100703 #if MICROPY_COMP_CONST
704 mp_map_init(&parser.consts, 0);
705 #endif
706
Damien George1b82e9a2014-05-10 17:36:41 +0100707 // check if we could allocate the stacks
708 if (parser.rule_stack == NULL || parser.result_stack == NULL) {
709 goto memory_error;
710 }
Damien George08335002014-01-18 23:24:36 +0000711
Damien George69a818d2014-01-12 13:55:24 +0000712 // work out the top-level rule to use, and push it on the stack
Damien George16a6a472015-12-17 13:06:05 +0000713 size_t top_level_rule;
Damien5ac1b2e2013-10-18 19:58:12 +0100714 switch (input_kind) {
Damiend99b0522013-12-21 18:17:45 +0000715 case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
Damien Georged02c6d82014-01-15 22:14:03 +0000716 case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
Damien5ac1b2e2013-10-18 19:58:12 +0100717 default: top_level_rule = RULE_file_input;
718 }
Damien Georgea4c52c52014-12-05 19:35:18 +0000719 push_rule(&parser, lex->tok_line, rules[top_level_rule], 0);
Damien429d7192013-10-04 19:53:11 +0100720
Damien George69a818d2014-01-12 13:55:24 +0000721 // parse!
722
Damien George16a6a472015-12-17 13:06:05 +0000723 size_t n, i; // state for the current rule
724 size_t rule_src_line; // source line for the first token matched by the current rule
Damien429d7192013-10-04 19:53:11 +0100725 bool backtrack = false;
Damien George08335002014-01-18 23:24:36 +0000726 const rule_t *rule = NULL;
Damien429d7192013-10-04 19:53:11 +0100727
728 for (;;) {
729 next_rule:
Damien George64f2b212015-10-08 14:58:15 +0100730 if (parser.rule_stack_top == 0 || parser.parse_error) {
Damien429d7192013-10-04 19:53:11 +0100731 break;
732 }
733
Damien George1b82e9a2014-05-10 17:36:41 +0100734 pop_rule(&parser, &rule, &i, &rule_src_line);
Damien429d7192013-10-04 19:53:11 +0100735 n = rule->act & RULE_ACT_ARG_MASK;
736
737 /*
738 // debugging
Damien George1b82e9a2014-05-10 17:36:41 +0100739 printf("depth=%d ", parser.rule_stack_top);
740 for (int j = 0; j < parser.rule_stack_top; ++j) {
Damien429d7192013-10-04 19:53:11 +0100741 printf(" ");
742 }
743 printf("%s n=%d i=%d bt=%d\n", rule->rule_name, n, i, backtrack);
744 */
745
746 switch (rule->act & RULE_ACT_KIND_MASK) {
747 case RULE_ACT_OR:
748 if (i > 0 && !backtrack) {
749 goto next_rule;
750 } else {
751 backtrack = false;
752 }
Damien Georgefa7c61d2015-07-24 14:35:57 +0000753 for (; i < n; ++i) {
754 uint16_t kind = rule->arg[i] & RULE_ARG_KIND_MASK;
755 if (kind == RULE_ARG_TOK) {
756 if (lex->tok_kind == (rule->arg[i] & RULE_ARG_ARG_MASK)) {
757 push_result_token(&parser);
758 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100759 goto next_rule;
Damien Georgefa7c61d2015-07-24 14:35:57 +0000760 }
Damien429d7192013-10-04 19:53:11 +0100761 } else {
Damien Georgefa7c61d2015-07-24 14:35:57 +0000762 assert(kind == RULE_ARG_RULE);
763 if (i + 1 < n) {
764 push_rule(&parser, rule_src_line, rule, i + 1); // save this or-rule
765 }
766 push_rule_from_arg(&parser, rule->arg[i]); // push child of or-rule
Damien429d7192013-10-04 19:53:11 +0100767 goto next_rule;
768 }
Damien429d7192013-10-04 19:53:11 +0100769 }
Damien Georgefa7c61d2015-07-24 14:35:57 +0000770 backtrack = true;
Damien429d7192013-10-04 19:53:11 +0100771 break;
772
Damien George2870d852014-12-20 18:06:08 +0000773 case RULE_ACT_AND: {
Damien429d7192013-10-04 19:53:11 +0100774
775 // failed, backtrack if we can, else syntax error
776 if (backtrack) {
777 assert(i > 0);
778 if ((rule->arg[i - 1] & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE) {
779 // an optional rule that failed, so continue with next arg
Damien George1b82e9a2014-05-10 17:36:41 +0100780 push_result_node(&parser, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +0100781 backtrack = false;
782 } else {
783 // a mandatory rule that failed, so propagate backtrack
784 if (i > 1) {
785 // already eaten tokens so can't backtrack
786 goto syntax_error;
787 } else {
788 goto next_rule;
789 }
790 }
791 }
792
793 // progress through the rule
794 for (; i < n; ++i) {
795 switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
Damien George2870d852014-12-20 18:06:08 +0000796 case RULE_ARG_TOK: {
Damien429d7192013-10-04 19:53:11 +0100797 // need to match a token
Damien George2870d852014-12-20 18:06:08 +0000798 mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
Damien Georgea4c52c52014-12-05 19:35:18 +0000799 if (lex->tok_kind == tok_kind) {
Damien429d7192013-10-04 19:53:11 +0100800 // matched token
Damiend99b0522013-12-21 18:17:45 +0000801 if (tok_kind == MP_TOKEN_NAME) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000802 push_result_token(&parser);
Damien429d7192013-10-04 19:53:11 +0100803 }
Damiend99b0522013-12-21 18:17:45 +0000804 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100805 } else {
806 // failed to match token
807 if (i > 0) {
808 // already eaten tokens so can't backtrack
809 goto syntax_error;
810 } else {
811 // this rule failed, so backtrack
812 backtrack = true;
813 goto next_rule;
814 }
815 }
816 break;
Damien Georged2d64f02015-01-14 21:32:42 +0000817 }
Damien429d7192013-10-04 19:53:11 +0100818 case RULE_ARG_RULE:
Damien429d7192013-10-04 19:53:11 +0100819 case RULE_ARG_OPT_RULE:
Damien Georged2d64f02015-01-14 21:32:42 +0000820 rule_and_no_other_choice:
Damien George1b82e9a2014-05-10 17:36:41 +0100821 push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
822 push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
Damien429d7192013-10-04 19:53:11 +0100823 goto next_rule;
824 default:
825 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000826 goto rule_and_no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100827 }
828 }
829
830 assert(i == n);
831
832 // matched the rule, so now build the corresponding parse_node
833
Damien George65dc9602015-08-14 12:24:11 +0100834 #if !MICROPY_ENABLE_DOC_STRING
Damien George5042bce2014-05-25 22:06:06 +0100835 // this code discards lonely statements, such as doc strings
Damien George1b82e9a2014-05-10 17:36:41 +0100836 if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) {
837 mp_parse_node_t p = peek_result(&parser, 1);
Damien George5042bce2014-05-25 22:06:06 +0100838 if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) || MP_PARSE_NODE_IS_STRUCT_KIND(p, RULE_string)) {
Damien George52b5d762014-09-23 15:31:56 +0000839 pop_result(&parser); // MP_PARSE_NODE_NULL
Damien George58e0f4a2015-09-23 10:50:43 +0100840 mp_parse_node_t pn = pop_result(&parser); // possibly RULE_string
841 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
842 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
843 if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
Damien George16a6a472015-12-17 13:06:05 +0000844 m_del(char, (char*)pns->nodes[0], (size_t)pns->nodes[1]);
Damien George58e0f4a2015-09-23 10:50:43 +0100845 }
846 }
Damien George1b82e9a2014-05-10 17:36:41 +0100847 push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0);
Damien George93afa232014-05-06 21:44:11 +0100848 break;
849 }
850 }
Damien George65dc9602015-08-14 12:24:11 +0100851 #endif
Damien George93afa232014-05-06 21:44:11 +0100852
Damien George0c1de1c2016-04-14 13:23:50 +0100853 // count number of arguments for the parse node
854 i = 0;
Damien George16a6a472015-12-17 13:06:05 +0000855 size_t num_not_nil = 0;
Damien George0c1de1c2016-04-14 13:23:50 +0100856 for (size_t x = n; x > 0;) {
857 --x;
858 if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
859 mp_token_kind_t tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
860 if (tok_kind == MP_TOKEN_NAME) {
861 // only tokens which were names are pushed to stack
862 i += 1;
863 num_not_nil += 1;
864 }
865 } else {
866 // rules are always pushed
867 if (peek_result(&parser, i) != MP_PARSE_NODE_NULL) {
868 num_not_nil += 1;
869 }
870 i += 1;
Damien429d7192013-10-04 19:53:11 +0100871 }
872 }
Damien George0c1de1c2016-04-14 13:23:50 +0100873
874 if (num_not_nil == 1 && (rule->act & RULE_ACT_ALLOW_IDENT)) {
875 // this rule has only 1 argument and should not be emitted
Damiend99b0522013-12-21 18:17:45 +0000876 mp_parse_node_t pn = MP_PARSE_NODE_NULL;
Damien George16a6a472015-12-17 13:06:05 +0000877 for (size_t x = 0; x < i; ++x) {
Damien George1b82e9a2014-05-10 17:36:41 +0100878 mp_parse_node_t pn2 = pop_result(&parser);
Damiend99b0522013-12-21 18:17:45 +0000879 if (pn2 != MP_PARSE_NODE_NULL) {
Damien429d7192013-10-04 19:53:11 +0100880 pn = pn2;
881 }
882 }
Damien George1b82e9a2014-05-10 17:36:41 +0100883 push_result_node(&parser, pn);
Damien George0c1de1c2016-04-14 13:23:50 +0100884 } else {
885 // this rule must be emitted
886
887 if (rule->act & RULE_ACT_ADD_BLANK) {
888 // and add an extra blank node at the end (used by the compiler to store data)
889 push_result_node(&parser, MP_PARSE_NODE_NULL);
890 i += 1;
891 }
892
893 push_result_rule(&parser, rule_src_line, rule, i);
Damien429d7192013-10-04 19:53:11 +0100894 }
895 break;
Damien George2870d852014-12-20 18:06:08 +0000896 }
Damien429d7192013-10-04 19:53:11 +0100897
Damien George2870d852014-12-20 18:06:08 +0000898 case RULE_ACT_LIST: {
Damien429d7192013-10-04 19:53:11 +0100899 // n=2 is: item item*
900 // n=1 is: item (sep item)*
901 // n=3 is: item (sep item)* [sep]
Damien George2870d852014-12-20 18:06:08 +0000902 bool had_trailing_sep;
Damien429d7192013-10-04 19:53:11 +0100903 if (backtrack) {
904 list_backtrack:
905 had_trailing_sep = false;
906 if (n == 2) {
907 if (i == 1) {
908 // fail on item, first time round; propagate backtrack
909 goto next_rule;
910 } else {
911 // fail on item, in later rounds; finish with this rule
912 backtrack = false;
913 }
914 } else {
915 if (i == 1) {
916 // fail on item, first time round; propagate backtrack
917 goto next_rule;
918 } else if ((i & 1) == 1) {
919 // fail on item, in later rounds; have eaten tokens so can't backtrack
920 if (n == 3) {
921 // list allows trailing separator; finish parsing list
922 had_trailing_sep = true;
923 backtrack = false;
924 } else {
925 // list doesn't allowing trailing separator; fail
926 goto syntax_error;
927 }
928 } else {
929 // fail on separator; finish parsing list
930 backtrack = false;
931 }
932 }
933 } else {
934 for (;;) {
Damien George16a6a472015-12-17 13:06:05 +0000935 size_t arg = rule->arg[i & 1 & n];
Damien429d7192013-10-04 19:53:11 +0100936 switch (arg & RULE_ARG_KIND_MASK) {
937 case RULE_ARG_TOK:
Damien Georgea4c52c52014-12-05 19:35:18 +0000938 if (lex->tok_kind == (arg & RULE_ARG_ARG_MASK)) {
Damien429d7192013-10-04 19:53:11 +0100939 if (i & 1 & n) {
940 // separators which are tokens are not pushed to result stack
941 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000942 push_result_token(&parser);
Damien429d7192013-10-04 19:53:11 +0100943 }
Damiend99b0522013-12-21 18:17:45 +0000944 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100945 // got element of list, so continue parsing list
946 i += 1;
947 } else {
948 // couldn't get element of list
949 i += 1;
950 backtrack = true;
951 goto list_backtrack;
952 }
953 break;
954 case RULE_ARG_RULE:
Damien Georged2d64f02015-01-14 21:32:42 +0000955 rule_list_no_other_choice:
Damien George1b82e9a2014-05-10 17:36:41 +0100956 push_rule(&parser, rule_src_line, rule, i + 1); // save this list-rule
957 push_rule_from_arg(&parser, arg); // push child of list-rule
Damien429d7192013-10-04 19:53:11 +0100958 goto next_rule;
959 default:
960 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000961 goto rule_list_no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100962 }
963 }
964 }
965 assert(i >= 1);
966
967 // compute number of elements in list, result in i
968 i -= 1;
969 if ((n & 1) && (rule->arg[1] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
970 // don't count separators when they are tokens
971 i = (i + 1) / 2;
972 }
973
974 if (i == 1) {
975 // list matched single item
976 if (had_trailing_sep) {
977 // if there was a trailing separator, make a list of a single item
Damien George1b82e9a2014-05-10 17:36:41 +0100978 push_result_rule(&parser, rule_src_line, rule, i);
Damien429d7192013-10-04 19:53:11 +0100979 } else {
980 // just leave single item on stack (ie don't wrap in a list)
981 }
982 } else {
Damien George1b82e9a2014-05-10 17:36:41 +0100983 push_result_rule(&parser, rule_src_line, rule, i);
Damien429d7192013-10-04 19:53:11 +0100984 }
985 break;
Damien George2870d852014-12-20 18:06:08 +0000986 }
Damien429d7192013-10-04 19:53:11 +0100987
988 default:
989 assert(0);
990 }
991 }
Damien91d387d2013-10-09 15:09:52 +0100992
Damien George64f2b212015-10-08 14:58:15 +0100993 #if MICROPY_COMP_CONST
994 mp_map_deinit(&parser.consts);
995 #endif
996
Damien George58e0f4a2015-09-23 10:50:43 +0100997 // truncate final chunk and link into chain of chunks
998 if (parser.cur_chunk != NULL) {
999 (void)m_renew(byte, parser.cur_chunk,
1000 sizeof(mp_parse_chunk_t) + parser.cur_chunk->alloc,
1001 sizeof(mp_parse_chunk_t) + parser.cur_chunk->union_.used);
1002 parser.cur_chunk->alloc = parser.cur_chunk->union_.used;
1003 parser.cur_chunk->union_.next = parser.tree.chunk;
1004 parser.tree.chunk = parser.cur_chunk;
1005 }
1006
Damien Georgef8048332015-02-08 13:40:20 +00001007 mp_obj_t exc;
Damien George58ba4c32014-04-10 14:27:31 +00001008
Damien George64f2b212015-10-08 14:58:15 +01001009 if (parser.parse_error) {
1010 #if MICROPY_COMP_CONST
1011 if (parser.parse_error == PARSE_ERROR_CONST) {
1012 exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
1013 "constant must be an integer");
1014 } else
1015 #endif
1016 {
1017 assert(parser.parse_error == PARSE_ERROR_MEMORY);
1018 memory_error:
1019 exc = mp_obj_new_exception_msg(&mp_type_MemoryError,
1020 "parser could not allocate enough memory");
1021 }
Damien George58e0f4a2015-09-23 10:50:43 +01001022 parser.tree.root = MP_PARSE_NODE_NULL;
Damien Georgefdfcee72015-10-12 12:59:18 +01001023 } else if (
1024 lex->tok_kind != MP_TOKEN_END // check we are at the end of the token stream
1025 || parser.result_stack_top == 0 // check that we got a node (can fail on empty input)
1026 ) {
1027 syntax_error:
1028 if (lex->tok_kind == MP_TOKEN_INDENT) {
1029 exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
1030 "unexpected indent");
1031 } else if (lex->tok_kind == MP_TOKEN_DEDENT_MISMATCH) {
1032 exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
1033 "unindent does not match any outer indentation level");
1034 } else {
1035 exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
1036 "invalid syntax");
1037 }
1038 parser.tree.root = MP_PARSE_NODE_NULL;
1039 } else {
1040 // no errors
1041
1042 //result_stack_show(parser);
1043 //printf("rule stack alloc: %d\n", parser.rule_stack_alloc);
1044 //printf("result stack alloc: %d\n", parser.result_stack_alloc);
1045 //printf("number of parse nodes allocated: %d\n", num_parse_nodes_allocated);
1046
1047 // get the root parse node that we created
1048 assert(parser.result_stack_top == 1);
1049 exc = MP_OBJ_NULL;
1050 parser.tree.root = parser.result_stack[0];
Damien George58ba4c32014-04-10 14:27:31 +00001051 }
1052
Damien George69a818d2014-01-12 13:55:24 +00001053 // free the memory that we don't need anymore
Damien George1b82e9a2014-05-10 17:36:41 +01001054 m_del(rule_stack_t, parser.rule_stack, parser.rule_stack_alloc);
1055 m_del(mp_parse_node_t, parser.result_stack, parser.result_stack_alloc);
Damien George0bfc7632015-02-07 18:33:58 +00001056 // we also free the lexer on behalf of the caller (see below)
Damien George69a818d2014-01-12 13:55:24 +00001057
Damien George0bfc7632015-02-07 18:33:58 +00001058 if (exc != MP_OBJ_NULL) {
1059 // had an error so raise the exception
1060 // add traceback to give info about file name and location
1061 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
1062 mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
1063 mp_lexer_free(lex);
1064 nlr_raise(exc);
1065 } else {
1066 mp_lexer_free(lex);
Damien George58e0f4a2015-09-23 10:50:43 +01001067 return parser.tree;
Damien George0bfc7632015-02-07 18:33:58 +00001068 }
Damien429d7192013-10-04 19:53:11 +01001069}
Damien George58e0f4a2015-09-23 10:50:43 +01001070
1071void mp_parse_tree_clear(mp_parse_tree_t *tree) {
1072 mp_parse_chunk_t *chunk = tree->chunk;
1073 while (chunk != NULL) {
1074 mp_parse_chunk_t *next = chunk->union_.next;
1075 m_del(byte, chunk, sizeof(mp_parse_chunk_t) + chunk->alloc);
1076 chunk = next;
1077 }
1078}
Damien Georgedd5353a2015-12-18 12:35:44 +00001079
1080#endif // MICROPY_ENABLE_COMPILER