blob: bbe5b038a63160eab48fd1d2b36df920d64c58dc [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
Damien Georgeb47ea4e2014-12-20 18:37:50 +000059#define ADD_BLANK_NODE(rule) ((rule->act & RULE_ACT_ADD_BLANK) != 0)
Damien Georgeb829b5c2014-01-25 13:51:19 +000060
Damien429d7192013-10-04 19:53:11 +010061// (un)comment to use rule names; for debugging
62//#define USE_RULE_NAME (1)
63
64typedef struct _rule_t {
65 byte rule_id;
66 byte act;
67#ifdef USE_RULE_NAME
68 const char *rule_name;
69#endif
70 uint16_t arg[];
71} rule_t;
72
73enum {
Damien George00208ce2014-01-23 00:00:53 +000074#define DEF_RULE(rule, comp, kind, ...) RULE_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000075#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010076#undef DEF_RULE
77 RULE_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010078 RULE_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000079 RULE_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000080 RULE_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010081};
82
Damien Georgeb47ea4e2014-12-20 18:37:50 +000083#define ident (RULE_ACT_ALLOW_IDENT)
84#define blank (RULE_ACT_ADD_BLANK)
Damien429d7192013-10-04 19:53:11 +010085#define or(n) (RULE_ACT_OR | n)
86#define and(n) (RULE_ACT_AND | n)
87#define one_or_more (RULE_ACT_LIST | 2)
88#define list (RULE_ACT_LIST | 1)
89#define list_with_end (RULE_ACT_LIST | 3)
Damiend99b0522013-12-21 18:17:45 +000090#define tok(t) (RULE_ARG_TOK | MP_TOKEN_##t)
Damien429d7192013-10-04 19:53:11 +010091#define rule(r) (RULE_ARG_RULE | RULE_##r)
Damien429d7192013-10-04 19:53:11 +010092#define opt_rule(r) (RULE_ARG_OPT_RULE | RULE_##r)
93#ifdef USE_RULE_NAME
Damien George00208ce2014-01-23 00:00:53 +000094#define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, #rule, { __VA_ARGS__ } };
Damien429d7192013-10-04 19:53:11 +010095#else
Damien George00208ce2014-01-23 00:00:53 +000096#define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } };
Damien429d7192013-10-04 19:53:11 +010097#endif
Damien George51dfcb42015-01-01 20:27:54 +000098#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010099#undef or
100#undef and
101#undef list
102#undef list_with_end
103#undef tok
104#undef rule
Damien429d7192013-10-04 19:53:11 +0100105#undef opt_rule
106#undef one_or_more
107#undef DEF_RULE
108
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200109STATIC const rule_t *rules[] = {
Damien George00208ce2014-01-23 00:00:53 +0000110#define DEF_RULE(rule, comp, kind, ...) &rule_##rule,
Damien George51dfcb42015-01-01 20:27:54 +0000111#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +0100112#undef DEF_RULE
113};
114
115typedef struct _rule_stack_t {
Damien George16a6a472015-12-17 13:06:05 +0000116 size_t src_line : 8 * sizeof(size_t) - 8; // maximum bits storing source line number
117 size_t rule_id : 8; // this must be large enough to fit largest rule number
118 size_t arg_i; // this dictates the maximum nodes in a "list" of things
Damien429d7192013-10-04 19:53:11 +0100119} rule_stack_t;
120
Damien George58e0f4a2015-09-23 10:50:43 +0100121typedef struct _mp_parse_chunk_t {
Damien George16a6a472015-12-17 13:06:05 +0000122 size_t alloc;
Damien George58e0f4a2015-09-23 10:50:43 +0100123 union {
Damien George16a6a472015-12-17 13:06:05 +0000124 size_t used;
Damien George58e0f4a2015-09-23 10:50:43 +0100125 struct _mp_parse_chunk_t *next;
126 } union_;
127 byte data[];
128} mp_parse_chunk_t;
129
Damien George64f2b212015-10-08 14:58:15 +0100130typedef enum {
131 PARSE_ERROR_NONE = 0,
132 PARSE_ERROR_MEMORY,
133 PARSE_ERROR_CONST,
134} parse_error_t;
135
Damien429d7192013-10-04 19:53:11 +0100136typedef struct _parser_t {
Damien George64f2b212015-10-08 14:58:15 +0100137 parse_error_t parse_error;
Damien George58ba4c32014-04-10 14:27:31 +0000138
Damien George16a6a472015-12-17 13:06:05 +0000139 size_t rule_stack_alloc;
140 size_t rule_stack_top;
Damien429d7192013-10-04 19:53:11 +0100141 rule_stack_t *rule_stack;
142
Damien George16a6a472015-12-17 13:06:05 +0000143 size_t result_stack_alloc;
144 size_t result_stack_top;
Damiend99b0522013-12-21 18:17:45 +0000145 mp_parse_node_t *result_stack;
Damien George08335002014-01-18 23:24:36 +0000146
147 mp_lexer_t *lexer;
Damien George58e0f4a2015-09-23 10:50:43 +0100148
149 mp_parse_tree_t tree;
150 mp_parse_chunk_t *cur_chunk;
Damien429d7192013-10-04 19:53:11 +0100151
Damien George64f2b212015-10-08 14:58:15 +0100152 #if MICROPY_COMP_CONST
153 mp_map_t consts;
154 #endif
155} parser_t;
Damien George58ba4c32014-04-10 14:27:31 +0000156
Damien George58e0f4a2015-09-23 10:50:43 +0100157STATIC void *parser_alloc(parser_t *parser, size_t num_bytes) {
158 // use a custom memory allocator to store parse nodes sequentially in large chunks
159
160 mp_parse_chunk_t *chunk = parser->cur_chunk;
161
162 if (chunk != NULL && chunk->union_.used + num_bytes > chunk->alloc) {
163 // not enough room at end of previously allocated chunk so try to grow
164 mp_parse_chunk_t *new_data = (mp_parse_chunk_t*)m_renew_maybe(byte, chunk,
165 sizeof(mp_parse_chunk_t) + chunk->alloc,
166 sizeof(mp_parse_chunk_t) + chunk->alloc + num_bytes, false);
167 if (new_data == NULL) {
168 // could not grow existing memory; shrink it to fit previous
169 (void)m_renew(byte, chunk, sizeof(mp_parse_chunk_t) + chunk->alloc,
170 sizeof(mp_parse_chunk_t) + chunk->union_.used);
171 chunk->alloc = chunk->union_.used;
172 chunk->union_.next = parser->tree.chunk;
173 parser->tree.chunk = chunk;
174 chunk = NULL;
175 } else {
176 // could grow existing memory
177 chunk->alloc += num_bytes;
178 }
179 }
180
181 if (chunk == NULL) {
182 // no previous chunk, allocate a new chunk
183 size_t alloc = MICROPY_ALLOC_PARSE_CHUNK_INIT;
184 if (alloc < num_bytes) {
185 alloc = num_bytes;
186 }
187 chunk = (mp_parse_chunk_t*)m_new(byte, sizeof(mp_parse_chunk_t) + alloc);
188 chunk->alloc = alloc;
189 chunk->union_.used = 0;
190 parser->cur_chunk = chunk;
191 }
192
193 byte *ret = chunk->data + chunk->union_.used;
194 chunk->union_.used += num_bytes;
195 return ret;
196}
197
Damien George16a6a472015-12-17 13:06:05 +0000198STATIC 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 +0100199 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000200 return;
201 }
Damien429d7192013-10-04 19:53:11 +0100202 if (parser->rule_stack_top >= parser->rule_stack_alloc) {
Damien Georgeade9a052015-06-13 21:53:22 +0100203 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 +0000204 if (rs == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100205 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000206 return;
207 }
208 parser->rule_stack = rs;
Damien George58ebde42014-05-21 20:32:59 +0100209 parser->rule_stack_alloc += MICROPY_ALLOC_PARSE_RULE_INC;
Damien429d7192013-10-04 19:53:11 +0100210 }
Damien George08335002014-01-18 23:24:36 +0000211 rule_stack_t *rs = &parser->rule_stack[parser->rule_stack_top++];
212 rs->src_line = src_line;
213 rs->rule_id = rule->rule_id;
214 rs->arg_i = arg_i;
Damien429d7192013-10-04 19:53:11 +0100215}
216
Damien George16a6a472015-12-17 13:06:05 +0000217STATIC void push_rule_from_arg(parser_t *parser, size_t arg) {
Damien429d7192013-10-04 19:53:11 +0100218 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 +0000219 size_t rule_id = arg & RULE_ARG_ARG_MASK;
Damien429d7192013-10-04 19:53:11 +0100220 assert(rule_id < RULE_maximum_number_of);
Damien Georgea4c52c52014-12-05 19:35:18 +0000221 push_rule(parser, parser->lexer->tok_line, rules[rule_id], 0);
Damien429d7192013-10-04 19:53:11 +0100222}
223
Damien George16a6a472015-12-17 13:06:05 +0000224STATIC 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 +0100225 assert(!parser->parse_error);
Damien429d7192013-10-04 19:53:11 +0100226 parser->rule_stack_top -= 1;
227 *rule = rules[parser->rule_stack[parser->rule_stack_top].rule_id];
228 *arg_i = parser->rule_stack[parser->rule_stack_top].arg_i;
Damien George08335002014-01-18 23:24:36 +0000229 *src_line = parser->rule_stack[parser->rule_stack_top].src_line;
Damien429d7192013-10-04 19:53:11 +0100230}
231
Damien George16a6a472015-12-17 13:06:05 +0000232mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200233 if (kind == MP_PARSE_NODE_SMALL_INT) {
234 return (mp_parse_node_t)(kind | (arg << 1));
235 }
Damien George7d414a12015-02-08 01:57:40 +0000236 return (mp_parse_node_t)(kind | (arg << 4));
Damien429d7192013-10-04 19:53:11 +0100237}
238
Damien George22b22652016-01-07 14:40:35 +0000239bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
240 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
241 *o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn));
242 return true;
243 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, RULE_const_object)) {
244 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
245 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
246 // nodes are 32-bit pointers, but need to extract 64-bit object
247 *o = (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
248 #else
249 *o = (mp_obj_t)pns->nodes[0];
250 #endif
251 return MP_OBJ_IS_INT(*o);
252 } else {
253 return false;
254 }
255}
256
Damien George16a6a472015-12-17 13:06:05 +0000257int 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 +0000258 if (MP_PARSE_NODE_IS_NULL(*pn)) {
259 *nodes = NULL;
260 return 0;
261 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
262 *nodes = pn;
263 return 1;
264 } else {
265 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
266 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
267 *nodes = pn;
268 return 1;
269 } else {
270 *nodes = pns->nodes;
271 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
272 }
273 }
274}
275
Damien Georgecbd2f742014-01-19 11:48:48 +0000276#if MICROPY_DEBUG_PRINTERS
Damien George16a6a472015-12-17 13:06:05 +0000277void mp_parse_node_print(mp_parse_node_t pn, size_t indent) {
Damien George08335002014-01-18 23:24:36 +0000278 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
279 printf("[% 4d] ", (int)((mp_parse_node_struct_t*)pn)->source_line);
280 } else {
281 printf(" ");
282 }
Damien George16a6a472015-12-17 13:06:05 +0000283 for (size_t i = 0; i < indent; i++) {
Damien429d7192013-10-04 19:53:11 +0100284 printf(" ");
285 }
Damiend99b0522013-12-21 18:17:45 +0000286 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100287 printf("NULL\n");
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200288 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +0100289 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200290 printf("int(" INT_FMT ")\n", arg);
Damiend99b0522013-12-21 18:17:45 +0000291 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George16a6a472015-12-17 13:06:05 +0000292 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000293 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
294 case MP_PARSE_NODE_ID: printf("id(%s)\n", qstr_str(arg)); break;
Damiend99b0522013-12-21 18:17:45 +0000295 case MP_PARSE_NODE_STRING: printf("str(%s)\n", qstr_str(arg)); break;
296 case MP_PARSE_NODE_BYTES: printf("bytes(%s)\n", qstr_str(arg)); break;
Damien George16a6a472015-12-17 13:06:05 +0000297 case MP_PARSE_NODE_TOKEN: printf("tok(%u)\n", (uint)arg); break;
Damien429d7192013-10-04 19:53:11 +0100298 default: assert(0);
299 }
300 } else {
Damien George5042bce2014-05-25 22:06:06 +0100301 // node must be a mp_parse_node_struct_t
Damien Georgeb829b5c2014-01-25 13:51:19 +0000302 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +0100303 if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
304 printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
Damien George4c81ba82015-01-13 16:21:23 +0000305 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_bytes) {
306 printf("literal bytes(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
Damien George7d414a12015-02-08 01:57:40 +0000307 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000308 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
309 printf("literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
310 #else
Damien George7d414a12015-02-08 01:57:40 +0000311 printf("literal const(%p)\n", (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000312 #endif
Damien George5042bce2014-05-25 22:06:06 +0100313 } else {
Damien George16a6a472015-12-17 13:06:05 +0000314 size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100315#ifdef USE_RULE_NAME
Damien George16a6a472015-12-17 13:06:05 +0000316 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 +0100317#else
Damien George16a6a472015-12-17 13:06:05 +0000318 printf("rule(%u) (n=%u)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n);
Damien429d7192013-10-04 19:53:11 +0100319#endif
Damien George16a6a472015-12-17 13:06:05 +0000320 for (size_t i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +0100321 mp_parse_node_print(pns->nodes[i], indent + 2);
322 }
Damien429d7192013-10-04 19:53:11 +0100323 }
324 }
325}
Damien Georgecbd2f742014-01-19 11:48:48 +0000326#endif // MICROPY_DEBUG_PRINTERS
Damien429d7192013-10-04 19:53:11 +0100327
328/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200329STATIC void result_stack_show(parser_t *parser) {
Damien429d7192013-10-04 19:53:11 +0100330 printf("result stack, most recent first\n");
Damien George16a6a472015-12-17 13:06:05 +0000331 for (ssize_t i = parser->result_stack_top - 1; i >= 0; i--) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000332 mp_parse_node_print(parser->result_stack[i], 0);
Damien429d7192013-10-04 19:53:11 +0100333 }
334}
335*/
336
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200337STATIC mp_parse_node_t pop_result(parser_t *parser) {
Damien George64f2b212015-10-08 14:58:15 +0100338 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000339 return MP_PARSE_NODE_NULL;
340 }
Damien429d7192013-10-04 19:53:11 +0100341 assert(parser->result_stack_top > 0);
342 return parser->result_stack[--parser->result_stack_top];
343}
344
Damien George16a6a472015-12-17 13:06:05 +0000345STATIC mp_parse_node_t peek_result(parser_t *parser, size_t pos) {
Damien George64f2b212015-10-08 14:58:15 +0100346 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000347 return MP_PARSE_NODE_NULL;
348 }
Damien429d7192013-10-04 19:53:11 +0100349 assert(parser->result_stack_top > pos);
350 return parser->result_stack[parser->result_stack_top - 1 - pos];
351}
352
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200353STATIC void push_result_node(parser_t *parser, mp_parse_node_t pn) {
Damien George64f2b212015-10-08 14:58:15 +0100354 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000355 return;
356 }
Damien George69a818d2014-01-12 13:55:24 +0000357 if (parser->result_stack_top >= parser->result_stack_alloc) {
Damien Georgeade9a052015-06-13 21:53:22 +0100358 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 +0000359 if (stack == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100360 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000361 return;
362 }
Damien George50912e72015-01-20 11:55:10 +0000363 parser->result_stack = stack;
Damien George58ebde42014-05-21 20:32:59 +0100364 parser->result_stack_alloc += MICROPY_ALLOC_PARSE_RESULT_INC;
Damien George69a818d2014-01-12 13:55:24 +0000365 }
Damien429d7192013-10-04 19:53:11 +0100366 parser->result_stack[parser->result_stack_top++] = pn;
367}
368
Damien George16a6a472015-12-17 13:06:05 +0000369STATIC 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 +0100370 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 +0100371 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100372 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George7d414a12015-02-08 01:57:40 +0000373 return MP_PARSE_NODE_NULL;
Damien George5042bce2014-05-25 22:06:06 +0100374 }
375 pn->source_line = src_line;
Damien George4c81ba82015-01-13 16:21:23 +0000376 pn->kind_num_nodes = rule_kind | (2 << 8);
Damien George5042bce2014-05-25 22:06:06 +0100377 char *p = m_new(char, len);
378 memcpy(p, str, len);
Damien George999cedb2015-11-27 17:01:44 +0000379 pn->nodes[0] = (uintptr_t)p;
Damien George5042bce2014-05-25 22:06:06 +0100380 pn->nodes[1] = len;
Damien George7d414a12015-02-08 01:57:40 +0000381 return (mp_parse_node_t)pn;
382}
383
Damien George16a6a472015-12-17 13:06:05 +0000384STATIC 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 +0000385 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 +0000386 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100387 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George7d414a12015-02-08 01:57:40 +0000388 return MP_PARSE_NODE_NULL;
389 }
390 pn->source_line = src_line;
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000391 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
392 // nodes are 32-bit pointers, but need to store 64-bit object
393 pn->kind_num_nodes = RULE_const_object | (2 << 8);
394 pn->nodes[0] = (uint64_t)obj;
395 pn->nodes[1] = (uint64_t)obj >> 32;
396 #else
Damien George7d414a12015-02-08 01:57:40 +0000397 pn->kind_num_nodes = RULE_const_object | (1 << 8);
Damien George16a6a472015-12-17 13:06:05 +0000398 pn->nodes[0] = (uintptr_t)obj;
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000399 #endif
Damien George7d414a12015-02-08 01:57:40 +0000400 return (mp_parse_node_t)pn;
Damien George5042bce2014-05-25 22:06:06 +0100401}
Paul Sokolovsky9e76b112014-05-08 22:43:46 +0300402
Damien Georgea4c52c52014-12-05 19:35:18 +0000403STATIC void push_result_token(parser_t *parser) {
Damiend99b0522013-12-21 18:17:45 +0000404 mp_parse_node_t pn;
Damien Georgea4c52c52014-12-05 19:35:18 +0000405 mp_lexer_t *lex = parser->lexer;
406 if (lex->tok_kind == MP_TOKEN_NAME) {
Damien George64f2b212015-10-08 14:58:15 +0100407 qstr id = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
408 #if MICROPY_COMP_CONST
409 // lookup identifier in table of dynamic constants
410 mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP);
411 if (elem != NULL) {
412 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
413 } else
414 #endif
415 {
416 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, id);
417 }
Damien George7d414a12015-02-08 01:57:40 +0000418 } else if (lex->tok_kind == MP_TOKEN_INTEGER) {
419 mp_obj_t o = mp_parse_num_integer(lex->vstr.buf, lex->vstr.len, 0, lex);
420 if (MP_OBJ_IS_SMALL_INT(o)) {
421 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(o));
Damien429d7192013-10-04 19:53:11 +0100422 } else {
Damien George7d414a12015-02-08 01:57:40 +0000423 pn = make_node_const_object(parser, lex->tok_line, o);
Damien429d7192013-10-04 19:53:11 +0100424 }
Damien George7d414a12015-02-08 01:57:40 +0000425 } else if (lex->tok_kind == MP_TOKEN_FLOAT_OR_IMAG) {
426 mp_obj_t o = mp_parse_num_decimal(lex->vstr.buf, lex->vstr.len, true, false, lex);
427 pn = make_node_const_object(parser, lex->tok_line, o);
Damien George4c81ba82015-01-13 16:21:23 +0000428 } else if (lex->tok_kind == MP_TOKEN_STRING || lex->tok_kind == MP_TOKEN_BYTES) {
429 // Don't automatically intern all strings/bytes. doc strings (which are usually large)
Damien George5042bce2014-05-25 22:06:06 +0100430 // will be discarded by the compiler, and so we shouldn't intern them.
431 qstr qst = MP_QSTR_NULL;
Damien Georgea4c52c52014-12-05 19:35:18 +0000432 if (lex->vstr.len <= MICROPY_ALLOC_PARSE_INTERN_STRING_LEN) {
Damien George5042bce2014-05-25 22:06:06 +0100433 // intern short strings
Damien Georgea4c52c52014-12-05 19:35:18 +0000434 qst = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
Damien George5042bce2014-05-25 22:06:06 +0100435 } else {
436 // check if this string is already interned
Damien Georgea4c52c52014-12-05 19:35:18 +0000437 qst = qstr_find_strn(lex->vstr.buf, lex->vstr.len);
Damien George5042bce2014-05-25 22:06:06 +0100438 }
439 if (qst != MP_QSTR_NULL) {
440 // qstr exists, make a leaf node
Damien George4c81ba82015-01-13 16:21:23 +0000441 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 +0100442 } else {
Damien George4c81ba82015-01-13 16:21:23 +0000443 // not interned, make a node holding a pointer to the string/bytes data
Damien George7d414a12015-02-08 01:57:40 +0000444 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 +0100445 }
Damien429d7192013-10-04 19:53:11 +0100446 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000447 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_TOKEN, lex->tok_kind);
Damien429d7192013-10-04 19:53:11 +0100448 }
449 push_result_node(parser, pn);
450}
451
Damien George64f2b212015-10-08 14:58:15 +0100452#if MICROPY_COMP_MODULE_CONST
Damien Georgecbf76742015-11-27 13:38:15 +0000453STATIC const mp_rom_map_elem_t mp_constants_table[] = {
Damien George64f2b212015-10-08 14:58:15 +0100454 #if MICROPY_PY_UCTYPES
Damien Georgecbf76742015-11-27 13:38:15 +0000455 { MP_ROM_QSTR(MP_QSTR_uctypes), MP_ROM_PTR(&mp_module_uctypes) },
Damien George64f2b212015-10-08 14:58:15 +0100456 #endif
457 // Extra constants as defined by a port
458 MICROPY_PORT_CONSTANTS
459};
460STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
461#endif
462
463#if MICROPY_COMP_CONST_FOLDING
Damien George16a6a472015-12-17 13:06:05 +0000464STATIC bool fold_constants(parser_t *parser, const rule_t *rule, size_t num_args) {
Damien George64f2b212015-10-08 14:58:15 +0100465 // this code does folding of arbitrary integer expressions, eg 1 + 2 * 3 + 4
466 // it does not do partial folding, eg 1 + 2 + x -> 3 + x
467
Damien George22b22652016-01-07 14:40:35 +0000468 mp_obj_t arg0;
Damien George64f2b212015-10-08 14:58:15 +0100469 if (rule->rule_id == RULE_expr
470 || rule->rule_id == RULE_xor_expr
471 || rule->rule_id == RULE_and_expr) {
472 // folding for binary ops: | ^ &
473 mp_parse_node_t pn = peek_result(parser, num_args - 1);
Damien George22b22652016-01-07 14:40:35 +0000474 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100475 return false;
476 }
Damien George22b22652016-01-07 14:40:35 +0000477 mp_binary_op_t op;
478 if (rule->rule_id == RULE_expr) {
479 op = MP_BINARY_OP_OR;
480 } else if (rule->rule_id == RULE_xor_expr) {
481 op = MP_BINARY_OP_XOR;
482 } else {
483 op = MP_BINARY_OP_AND;
484 }
Damien George16a6a472015-12-17 13:06:05 +0000485 for (ssize_t i = num_args - 2; i >= 0; --i) {
Damien George64f2b212015-10-08 14:58:15 +0100486 pn = peek_result(parser, i);
Damien George22b22652016-01-07 14:40:35 +0000487 mp_obj_t arg1;
488 if (!mp_parse_node_get_int_maybe(pn, &arg1)) {
Damien George64f2b212015-10-08 14:58:15 +0100489 return false;
490 }
Damien George22b22652016-01-07 14:40:35 +0000491 arg0 = mp_binary_op(op, arg0, arg1);
Damien George64f2b212015-10-08 14:58:15 +0100492 }
493 } else if (rule->rule_id == RULE_shift_expr
494 || rule->rule_id == RULE_arith_expr
495 || rule->rule_id == RULE_term) {
496 // folding for binary ops: << >> + - * / % //
497 mp_parse_node_t pn = peek_result(parser, num_args - 1);
Damien George22b22652016-01-07 14:40:35 +0000498 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100499 return false;
500 }
Damien George16a6a472015-12-17 13:06:05 +0000501 for (ssize_t i = num_args - 2; i >= 1; i -= 2) {
Damien George64f2b212015-10-08 14:58:15 +0100502 pn = peek_result(parser, i - 1);
Damien George22b22652016-01-07 14:40:35 +0000503 mp_obj_t arg1;
504 if (!mp_parse_node_get_int_maybe(pn, &arg1)) {
Damien George64f2b212015-10-08 14:58:15 +0100505 return false;
506 }
Damien George64f2b212015-10-08 14:58:15 +0100507 mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, i));
Damien George22b22652016-01-07 14:40:35 +0000508 static const uint8_t token_to_op[] = {
509 MP_BINARY_OP_ADD,
510 MP_BINARY_OP_SUBTRACT,
511 MP_BINARY_OP_MULTIPLY,
512 255,//MP_BINARY_OP_POWER,
513 255,//MP_BINARY_OP_TRUE_DIVIDE,
514 MP_BINARY_OP_FLOOR_DIVIDE,
515 MP_BINARY_OP_MODULO,
516 255,//MP_BINARY_OP_LESS
517 MP_BINARY_OP_LSHIFT,
518 255,//MP_BINARY_OP_MORE
519 MP_BINARY_OP_RSHIFT,
520 };
521 mp_binary_op_t op = token_to_op[tok - MP_TOKEN_OP_PLUS];
Antonin ENFRUNefc971e2016-01-12 22:06:39 +0100522 if (op == (mp_binary_op_t)255) {
Damien George64f2b212015-10-08 14:58:15 +0100523 return false;
524 }
Damien George22b22652016-01-07 14:40:35 +0000525 int rhs_sign = mp_obj_int_sign(arg1);
526 if (op <= MP_BINARY_OP_RSHIFT) {
527 // << and >> can't have negative rhs
528 if (rhs_sign < 0) {
529 return false;
530 }
531 } else if (op >= MP_BINARY_OP_FLOOR_DIVIDE) {
532 // % and // can't have zero rhs
533 if (rhs_sign == 0) {
534 return false;
535 }
536 }
537 arg0 = mp_binary_op(op, arg0, arg1);
Damien George64f2b212015-10-08 14:58:15 +0100538 }
539 } else if (rule->rule_id == RULE_factor_2) {
540 // folding for unary ops: + - ~
541 mp_parse_node_t pn = peek_result(parser, 0);
Damien George22b22652016-01-07 14:40:35 +0000542 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100543 return false;
544 }
Damien George64f2b212015-10-08 14:58:15 +0100545 mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, 1));
Antonin ENFRUNefc971e2016-01-12 22:06:39 +0100546 mp_unary_op_t op;
Damien George64f2b212015-10-08 14:58:15 +0100547 if (tok == MP_TOKEN_OP_PLUS) {
Damien George22b22652016-01-07 14:40:35 +0000548 op = MP_UNARY_OP_POSITIVE;
Damien George64f2b212015-10-08 14:58:15 +0100549 } else if (tok == MP_TOKEN_OP_MINUS) {
Damien George22b22652016-01-07 14:40:35 +0000550 op = MP_UNARY_OP_NEGATIVE;
Damien George64f2b212015-10-08 14:58:15 +0100551 } else {
552 assert(tok == MP_TOKEN_OP_TILDE); // should be
Damien George22b22652016-01-07 14:40:35 +0000553 op = MP_UNARY_OP_INVERT;
Damien George64f2b212015-10-08 14:58:15 +0100554 }
Damien George22b22652016-01-07 14:40:35 +0000555 arg0 = mp_unary_op(op, arg0);
Damien George64f2b212015-10-08 14:58:15 +0100556
557 #if MICROPY_COMP_CONST
558 } else if (rule->rule_id == RULE_expr_stmt) {
559 mp_parse_node_t pn1 = peek_result(parser, 0);
560 if (!MP_PARSE_NODE_IS_NULL(pn1)
561 && !(MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_augassign)
562 || MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_assign_list))) {
563 // this node is of the form <x> = <y>
564 mp_parse_node_t pn0 = peek_result(parser, 1);
565 if (MP_PARSE_NODE_IS_ID(pn0)
566 && MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_power)
567 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pn1)->nodes[0])
568 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn1)->nodes[0]) == MP_QSTR_const
569 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pn1)->nodes[1], RULE_trailer_paren)
570 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pn1)->nodes[2])
571 ) {
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
602 } else if (rule->rule_id == RULE_power) {
603 mp_parse_node_t pn0 = peek_result(parser, 2);
604 mp_parse_node_t pn1 = peek_result(parser, 1);
605 mp_parse_node_t pn2 = peek_result(parser, 0);
606 if (!(MP_PARSE_NODE_IS_ID(pn0)
607 && MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_trailer_period)
608 && MP_PARSE_NODE_IS_NULL(pn2))) {
609 return false;
610 }
611 // id1.id2
612 // look it up in constant table, see if it can be replaced with an integer
613 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pn1;
614 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
615 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pn0);
616 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
617 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
618 if (elem == NULL) {
619 return false;
620 }
621 mp_obj_t dest[2];
622 mp_load_method_maybe(elem->value, q_attr, dest);
Damien George22b22652016-01-07 14:40:35 +0000623 if (!(MP_OBJ_IS_INT(dest[0]) && dest[1] == MP_OBJ_NULL)) {
Damien George64f2b212015-10-08 14:58:15 +0100624 return false;
625 }
Damien George22b22652016-01-07 14:40:35 +0000626 arg0 = dest[0];
Damien George64f2b212015-10-08 14:58:15 +0100627 #endif
628
629 } else {
630 return false;
631 }
632
633 // success folding this rule
634
Damien George16a6a472015-12-17 13:06:05 +0000635 for (size_t i = num_args; i > 0; i--) {
Damien George64f2b212015-10-08 14:58:15 +0100636 pop_result(parser);
637 }
Damien George22b22652016-01-07 14:40:35 +0000638 if (MP_OBJ_IS_SMALL_INT(arg0)) {
639 push_result_node(parser, mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(arg0)));
640 } else {
641 // TODO reuse memory for parse node struct?
642 push_result_node(parser, make_node_const_object(parser, 0, arg0));
643 }
Damien George64f2b212015-10-08 14:58:15 +0100644
645 return true;
646}
647#endif
648
Damien George16a6a472015-12-17 13:06:05 +0000649STATIC 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 +0000650 // optimise away parenthesis around an expression if possible
651 if (rule->rule_id == RULE_atom_paren) {
652 // there should be just 1 arg for this rule
653 mp_parse_node_t pn = peek_result(parser, 0);
654 if (MP_PARSE_NODE_IS_NULL(pn)) {
655 // need to keep parenthesis for ()
656 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, RULE_testlist_comp)) {
657 // need to keep parenthesis for (a, b, ...)
658 } else {
659 // parenthesis around a single expression, so it's just the expression
660 return;
661 }
662 }
663
Damien George64f2b212015-10-08 14:58:15 +0100664 #if MICROPY_COMP_CONST_FOLDING
665 if (fold_constants(parser, rule, num_args)) {
666 // we folded this rule so return straight away
667 return;
668 }
669 #endif
670
Damien George58e0f4a2015-09-23 10:50:43 +0100671 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 +0000672 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100673 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000674 return;
675 }
676 pn->source_line = src_line;
677 pn->kind_num_nodes = (rule->rule_id & 0xff) | (num_args << 8);
Damien George16a6a472015-12-17 13:06:05 +0000678 for (size_t i = num_args; i > 0; i--) {
Damien429d7192013-10-04 19:53:11 +0100679 pn->nodes[i - 1] = pop_result(parser);
680 }
Damiend99b0522013-12-21 18:17:45 +0000681 push_result_node(parser, (mp_parse_node_t)pn);
Damien429d7192013-10-04 19:53:11 +0100682}
683
Damien George58e0f4a2015-09-23 10:50:43 +0100684mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
Damien George69a818d2014-01-12 13:55:24 +0000685
Damien George1b82e9a2014-05-10 17:36:41 +0100686 // initialise parser and allocate memory for its stacks
Damien George69a818d2014-01-12 13:55:24 +0000687
Damien George1b82e9a2014-05-10 17:36:41 +0100688 parser_t parser;
Damien George69a818d2014-01-12 13:55:24 +0000689
Damien George64f2b212015-10-08 14:58:15 +0100690 parser.parse_error = PARSE_ERROR_NONE;
Damien George58ba4c32014-04-10 14:27:31 +0000691
Damien George58ebde42014-05-21 20:32:59 +0100692 parser.rule_stack_alloc = MICROPY_ALLOC_PARSE_RULE_INIT;
Damien George1b82e9a2014-05-10 17:36:41 +0100693 parser.rule_stack_top = 0;
694 parser.rule_stack = m_new_maybe(rule_stack_t, parser.rule_stack_alloc);
Damien429d7192013-10-04 19:53:11 +0100695
Damien George58ebde42014-05-21 20:32:59 +0100696 parser.result_stack_alloc = MICROPY_ALLOC_PARSE_RESULT_INIT;
Damien George1b82e9a2014-05-10 17:36:41 +0100697 parser.result_stack_top = 0;
698 parser.result_stack = m_new_maybe(mp_parse_node_t, parser.result_stack_alloc);
Damien429d7192013-10-04 19:53:11 +0100699
Damien George1b82e9a2014-05-10 17:36:41 +0100700 parser.lexer = lex;
701
Damien George58e0f4a2015-09-23 10:50:43 +0100702 parser.tree.chunk = NULL;
703 parser.cur_chunk = NULL;
704
Damien George64f2b212015-10-08 14:58:15 +0100705 #if MICROPY_COMP_CONST
706 mp_map_init(&parser.consts, 0);
707 #endif
708
Damien George1b82e9a2014-05-10 17:36:41 +0100709 // check if we could allocate the stacks
710 if (parser.rule_stack == NULL || parser.result_stack == NULL) {
711 goto memory_error;
712 }
Damien George08335002014-01-18 23:24:36 +0000713
Damien George69a818d2014-01-12 13:55:24 +0000714 // work out the top-level rule to use, and push it on the stack
Damien George16a6a472015-12-17 13:06:05 +0000715 size_t top_level_rule;
Damien5ac1b2e2013-10-18 19:58:12 +0100716 switch (input_kind) {
Damiend99b0522013-12-21 18:17:45 +0000717 case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
Damien Georged02c6d82014-01-15 22:14:03 +0000718 case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
Damien5ac1b2e2013-10-18 19:58:12 +0100719 default: top_level_rule = RULE_file_input;
720 }
Damien Georgea4c52c52014-12-05 19:35:18 +0000721 push_rule(&parser, lex->tok_line, rules[top_level_rule], 0);
Damien429d7192013-10-04 19:53:11 +0100722
Damien George69a818d2014-01-12 13:55:24 +0000723 // parse!
724
Damien George16a6a472015-12-17 13:06:05 +0000725 size_t n, i; // state for the current rule
726 size_t rule_src_line; // source line for the first token matched by the current rule
Damien429d7192013-10-04 19:53:11 +0100727 bool backtrack = false;
Damien George08335002014-01-18 23:24:36 +0000728 const rule_t *rule = NULL;
Damien429d7192013-10-04 19:53:11 +0100729
730 for (;;) {
731 next_rule:
Damien George64f2b212015-10-08 14:58:15 +0100732 if (parser.rule_stack_top == 0 || parser.parse_error) {
Damien429d7192013-10-04 19:53:11 +0100733 break;
734 }
735
Damien George1b82e9a2014-05-10 17:36:41 +0100736 pop_rule(&parser, &rule, &i, &rule_src_line);
Damien429d7192013-10-04 19:53:11 +0100737 n = rule->act & RULE_ACT_ARG_MASK;
738
739 /*
740 // debugging
Damien George1b82e9a2014-05-10 17:36:41 +0100741 printf("depth=%d ", parser.rule_stack_top);
742 for (int j = 0; j < parser.rule_stack_top; ++j) {
Damien429d7192013-10-04 19:53:11 +0100743 printf(" ");
744 }
745 printf("%s n=%d i=%d bt=%d\n", rule->rule_name, n, i, backtrack);
746 */
747
748 switch (rule->act & RULE_ACT_KIND_MASK) {
749 case RULE_ACT_OR:
750 if (i > 0 && !backtrack) {
751 goto next_rule;
752 } else {
753 backtrack = false;
754 }
Damien Georgefa7c61d2015-07-24 14:35:57 +0000755 for (; i < n; ++i) {
756 uint16_t kind = rule->arg[i] & RULE_ARG_KIND_MASK;
757 if (kind == RULE_ARG_TOK) {
758 if (lex->tok_kind == (rule->arg[i] & RULE_ARG_ARG_MASK)) {
759 push_result_token(&parser);
760 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100761 goto next_rule;
Damien Georgefa7c61d2015-07-24 14:35:57 +0000762 }
Damien429d7192013-10-04 19:53:11 +0100763 } else {
Damien Georgefa7c61d2015-07-24 14:35:57 +0000764 assert(kind == RULE_ARG_RULE);
765 if (i + 1 < n) {
766 push_rule(&parser, rule_src_line, rule, i + 1); // save this or-rule
767 }
768 push_rule_from_arg(&parser, rule->arg[i]); // push child of or-rule
Damien429d7192013-10-04 19:53:11 +0100769 goto next_rule;
770 }
Damien429d7192013-10-04 19:53:11 +0100771 }
Damien Georgefa7c61d2015-07-24 14:35:57 +0000772 backtrack = true;
Damien429d7192013-10-04 19:53:11 +0100773 break;
774
Damien George2870d852014-12-20 18:06:08 +0000775 case RULE_ACT_AND: {
Damien429d7192013-10-04 19:53:11 +0100776
777 // failed, backtrack if we can, else syntax error
778 if (backtrack) {
779 assert(i > 0);
780 if ((rule->arg[i - 1] & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE) {
781 // an optional rule that failed, so continue with next arg
Damien George1b82e9a2014-05-10 17:36:41 +0100782 push_result_node(&parser, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +0100783 backtrack = false;
784 } else {
785 // a mandatory rule that failed, so propagate backtrack
786 if (i > 1) {
787 // already eaten tokens so can't backtrack
788 goto syntax_error;
789 } else {
790 goto next_rule;
791 }
792 }
793 }
794
795 // progress through the rule
796 for (; i < n; ++i) {
797 switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
Damien George2870d852014-12-20 18:06:08 +0000798 case RULE_ARG_TOK: {
Damien429d7192013-10-04 19:53:11 +0100799 // need to match a token
Damien George2870d852014-12-20 18:06:08 +0000800 mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
Damien Georgea4c52c52014-12-05 19:35:18 +0000801 if (lex->tok_kind == tok_kind) {
Damien429d7192013-10-04 19:53:11 +0100802 // matched token
Damiend99b0522013-12-21 18:17:45 +0000803 if (tok_kind == MP_TOKEN_NAME) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000804 push_result_token(&parser);
Damien429d7192013-10-04 19:53:11 +0100805 }
Damiend99b0522013-12-21 18:17:45 +0000806 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100807 } else {
808 // failed to match token
809 if (i > 0) {
810 // already eaten tokens so can't backtrack
811 goto syntax_error;
812 } else {
813 // this rule failed, so backtrack
814 backtrack = true;
815 goto next_rule;
816 }
817 }
818 break;
Damien Georged2d64f02015-01-14 21:32:42 +0000819 }
Damien429d7192013-10-04 19:53:11 +0100820 case RULE_ARG_RULE:
Damien429d7192013-10-04 19:53:11 +0100821 case RULE_ARG_OPT_RULE:
Damien Georged2d64f02015-01-14 21:32:42 +0000822 rule_and_no_other_choice:
Damien George1b82e9a2014-05-10 17:36:41 +0100823 push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
824 push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
Damien429d7192013-10-04 19:53:11 +0100825 goto next_rule;
826 default:
827 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000828 goto rule_and_no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100829 }
830 }
831
832 assert(i == n);
833
834 // matched the rule, so now build the corresponding parse_node
835
836 // count number of arguments for the parse_node
837 i = 0;
Damien George2870d852014-12-20 18:06:08 +0000838 bool emit_rule = false;
Damien George16a6a472015-12-17 13:06:05 +0000839 for (size_t x = 0; x < n; ++x) {
Damien429d7192013-10-04 19:53:11 +0100840 if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
Damien George2870d852014-12-20 18:06:08 +0000841 mp_token_kind_t tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
Damiend99b0522013-12-21 18:17:45 +0000842 if (tok_kind >= MP_TOKEN_NAME) {
Damien429d7192013-10-04 19:53:11 +0100843 emit_rule = true;
844 }
Damiend99b0522013-12-21 18:17:45 +0000845 if (tok_kind == MP_TOKEN_NAME) {
Damien429d7192013-10-04 19:53:11 +0100846 // only tokens which were names are pushed to stack
847 i += 1;
848 }
849 } else {
850 // rules are always pushed
851 i += 1;
852 }
853 }
854
Damien George65dc9602015-08-14 12:24:11 +0100855 #if !MICROPY_ENABLE_DOC_STRING
Damien George5042bce2014-05-25 22:06:06 +0100856 // this code discards lonely statements, such as doc strings
Damien George1b82e9a2014-05-10 17:36:41 +0100857 if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) {
858 mp_parse_node_t p = peek_result(&parser, 1);
Damien George5042bce2014-05-25 22:06:06 +0100859 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 +0000860 pop_result(&parser); // MP_PARSE_NODE_NULL
Damien George58e0f4a2015-09-23 10:50:43 +0100861 mp_parse_node_t pn = pop_result(&parser); // possibly RULE_string
862 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
863 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
864 if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
Damien George16a6a472015-12-17 13:06:05 +0000865 m_del(char, (char*)pns->nodes[0], (size_t)pns->nodes[1]);
Damien George58e0f4a2015-09-23 10:50:43 +0100866 }
867 }
Damien George1b82e9a2014-05-10 17:36:41 +0100868 push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0);
Damien George93afa232014-05-06 21:44:11 +0100869 break;
870 }
871 }
Damien George65dc9602015-08-14 12:24:11 +0100872 #endif
Damien George93afa232014-05-06 21:44:11 +0100873
Damien429d7192013-10-04 19:53:11 +0100874 // always emit these rules, even if they have only 1 argument
875 if (rule->rule_id == RULE_expr_stmt || rule->rule_id == RULE_yield_stmt) {
876 emit_rule = true;
877 }
878
Damien Georgeb47ea4e2014-12-20 18:37:50 +0000879 // if a rule has the RULE_ACT_ALLOW_IDENT bit set then this
880 // rule should not be emitted if it has only 1 argument
Damien Georgeb47ea4e2014-12-20 18:37:50 +0000881 if (rule->act & RULE_ACT_ALLOW_IDENT) {
Damien429d7192013-10-04 19:53:11 +0100882 emit_rule = false;
883 }
884
885 // always emit these rules, and add an extra blank node at the end (to be used by the compiler to store data)
Damien Georgeb47ea4e2014-12-20 18:37:50 +0000886 if (ADD_BLANK_NODE(rule)) {
Damien429d7192013-10-04 19:53:11 +0100887 emit_rule = true;
Damien George1b82e9a2014-05-10 17:36:41 +0100888 push_result_node(&parser, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +0100889 i += 1;
890 }
891
Damien George16a6a472015-12-17 13:06:05 +0000892 size_t num_not_nil = 0;
893 for (size_t x = 0; x < i; ++x) {
Damien George1b82e9a2014-05-10 17:36:41 +0100894 if (peek_result(&parser, x) != MP_PARSE_NODE_NULL) {
Damien429d7192013-10-04 19:53:11 +0100895 num_not_nil += 1;
896 }
897 }
Damien George366239b2015-10-08 23:13:18 +0100898 if (emit_rule || num_not_nil != 1) {
899 // need to add rule when num_not_nil==0 for, eg, atom_paren, testlist_comp_3b
Damien George1b82e9a2014-05-10 17:36:41 +0100900 push_result_rule(&parser, rule_src_line, rule, i);
Damien George366239b2015-10-08 23:13:18 +0100901 } else {
Damien429d7192013-10-04 19:53:11 +0100902 // single result, leave it on stack
Damiend99b0522013-12-21 18:17:45 +0000903 mp_parse_node_t pn = MP_PARSE_NODE_NULL;
Damien George16a6a472015-12-17 13:06:05 +0000904 for (size_t x = 0; x < i; ++x) {
Damien George1b82e9a2014-05-10 17:36:41 +0100905 mp_parse_node_t pn2 = pop_result(&parser);
Damiend99b0522013-12-21 18:17:45 +0000906 if (pn2 != MP_PARSE_NODE_NULL) {
Damien429d7192013-10-04 19:53:11 +0100907 pn = pn2;
908 }
909 }
Damien George1b82e9a2014-05-10 17:36:41 +0100910 push_result_node(&parser, pn);
Damien429d7192013-10-04 19:53:11 +0100911 }
912 break;
Damien George2870d852014-12-20 18:06:08 +0000913 }
Damien429d7192013-10-04 19:53:11 +0100914
Damien George2870d852014-12-20 18:06:08 +0000915 case RULE_ACT_LIST: {
Damien429d7192013-10-04 19:53:11 +0100916 // n=2 is: item item*
917 // n=1 is: item (sep item)*
918 // n=3 is: item (sep item)* [sep]
Damien George2870d852014-12-20 18:06:08 +0000919 bool had_trailing_sep;
Damien429d7192013-10-04 19:53:11 +0100920 if (backtrack) {
921 list_backtrack:
922 had_trailing_sep = false;
923 if (n == 2) {
924 if (i == 1) {
925 // fail on item, first time round; propagate backtrack
926 goto next_rule;
927 } else {
928 // fail on item, in later rounds; finish with this rule
929 backtrack = false;
930 }
931 } else {
932 if (i == 1) {
933 // fail on item, first time round; propagate backtrack
934 goto next_rule;
935 } else if ((i & 1) == 1) {
936 // fail on item, in later rounds; have eaten tokens so can't backtrack
937 if (n == 3) {
938 // list allows trailing separator; finish parsing list
939 had_trailing_sep = true;
940 backtrack = false;
941 } else {
942 // list doesn't allowing trailing separator; fail
943 goto syntax_error;
944 }
945 } else {
946 // fail on separator; finish parsing list
947 backtrack = false;
948 }
949 }
950 } else {
951 for (;;) {
Damien George16a6a472015-12-17 13:06:05 +0000952 size_t arg = rule->arg[i & 1 & n];
Damien429d7192013-10-04 19:53:11 +0100953 switch (arg & RULE_ARG_KIND_MASK) {
954 case RULE_ARG_TOK:
Damien Georgea4c52c52014-12-05 19:35:18 +0000955 if (lex->tok_kind == (arg & RULE_ARG_ARG_MASK)) {
Damien429d7192013-10-04 19:53:11 +0100956 if (i & 1 & n) {
957 // separators which are tokens are not pushed to result stack
958 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000959 push_result_token(&parser);
Damien429d7192013-10-04 19:53:11 +0100960 }
Damiend99b0522013-12-21 18:17:45 +0000961 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100962 // got element of list, so continue parsing list
963 i += 1;
964 } else {
965 // couldn't get element of list
966 i += 1;
967 backtrack = true;
968 goto list_backtrack;
969 }
970 break;
971 case RULE_ARG_RULE:
Damien Georged2d64f02015-01-14 21:32:42 +0000972 rule_list_no_other_choice:
Damien George1b82e9a2014-05-10 17:36:41 +0100973 push_rule(&parser, rule_src_line, rule, i + 1); // save this list-rule
974 push_rule_from_arg(&parser, arg); // push child of list-rule
Damien429d7192013-10-04 19:53:11 +0100975 goto next_rule;
976 default:
977 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000978 goto rule_list_no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100979 }
980 }
981 }
982 assert(i >= 1);
983
984 // compute number of elements in list, result in i
985 i -= 1;
986 if ((n & 1) && (rule->arg[1] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
987 // don't count separators when they are tokens
988 i = (i + 1) / 2;
989 }
990
991 if (i == 1) {
992 // list matched single item
993 if (had_trailing_sep) {
994 // if there was a trailing separator, make a list of a single item
Damien George1b82e9a2014-05-10 17:36:41 +0100995 push_result_rule(&parser, rule_src_line, rule, i);
Damien429d7192013-10-04 19:53:11 +0100996 } else {
997 // just leave single item on stack (ie don't wrap in a list)
998 }
999 } else {
Damien George1b82e9a2014-05-10 17:36:41 +01001000 push_result_rule(&parser, rule_src_line, rule, i);
Damien429d7192013-10-04 19:53:11 +01001001 }
1002 break;
Damien George2870d852014-12-20 18:06:08 +00001003 }
Damien429d7192013-10-04 19:53:11 +01001004
1005 default:
1006 assert(0);
1007 }
1008 }
Damien91d387d2013-10-09 15:09:52 +01001009
Damien George64f2b212015-10-08 14:58:15 +01001010 #if MICROPY_COMP_CONST
1011 mp_map_deinit(&parser.consts);
1012 #endif
1013
Damien George58e0f4a2015-09-23 10:50:43 +01001014 // truncate final chunk and link into chain of chunks
1015 if (parser.cur_chunk != NULL) {
1016 (void)m_renew(byte, parser.cur_chunk,
1017 sizeof(mp_parse_chunk_t) + parser.cur_chunk->alloc,
1018 sizeof(mp_parse_chunk_t) + parser.cur_chunk->union_.used);
1019 parser.cur_chunk->alloc = parser.cur_chunk->union_.used;
1020 parser.cur_chunk->union_.next = parser.tree.chunk;
1021 parser.tree.chunk = parser.cur_chunk;
1022 }
1023
Damien Georgef8048332015-02-08 13:40:20 +00001024 mp_obj_t exc;
Damien George58ba4c32014-04-10 14:27:31 +00001025
Damien George64f2b212015-10-08 14:58:15 +01001026 if (parser.parse_error) {
1027 #if MICROPY_COMP_CONST
1028 if (parser.parse_error == PARSE_ERROR_CONST) {
1029 exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
1030 "constant must be an integer");
1031 } else
1032 #endif
1033 {
1034 assert(parser.parse_error == PARSE_ERROR_MEMORY);
1035 memory_error:
1036 exc = mp_obj_new_exception_msg(&mp_type_MemoryError,
1037 "parser could not allocate enough memory");
1038 }
Damien George58e0f4a2015-09-23 10:50:43 +01001039 parser.tree.root = MP_PARSE_NODE_NULL;
Damien Georgefdfcee72015-10-12 12:59:18 +01001040 } else if (
1041 lex->tok_kind != MP_TOKEN_END // check we are at the end of the token stream
1042 || parser.result_stack_top == 0 // check that we got a node (can fail on empty input)
1043 ) {
1044 syntax_error:
1045 if (lex->tok_kind == MP_TOKEN_INDENT) {
1046 exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
1047 "unexpected indent");
1048 } else if (lex->tok_kind == MP_TOKEN_DEDENT_MISMATCH) {
1049 exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
1050 "unindent does not match any outer indentation level");
1051 } else {
1052 exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
1053 "invalid syntax");
1054 }
1055 parser.tree.root = MP_PARSE_NODE_NULL;
1056 } else {
1057 // no errors
1058
1059 //result_stack_show(parser);
1060 //printf("rule stack alloc: %d\n", parser.rule_stack_alloc);
1061 //printf("result stack alloc: %d\n", parser.result_stack_alloc);
1062 //printf("number of parse nodes allocated: %d\n", num_parse_nodes_allocated);
1063
1064 // get the root parse node that we created
1065 assert(parser.result_stack_top == 1);
1066 exc = MP_OBJ_NULL;
1067 parser.tree.root = parser.result_stack[0];
Damien George58ba4c32014-04-10 14:27:31 +00001068 }
1069
Damien George69a818d2014-01-12 13:55:24 +00001070 // free the memory that we don't need anymore
Damien George1b82e9a2014-05-10 17:36:41 +01001071 m_del(rule_stack_t, parser.rule_stack, parser.rule_stack_alloc);
1072 m_del(mp_parse_node_t, parser.result_stack, parser.result_stack_alloc);
Damien George0bfc7632015-02-07 18:33:58 +00001073 // we also free the lexer on behalf of the caller (see below)
Damien George69a818d2014-01-12 13:55:24 +00001074
Damien George0bfc7632015-02-07 18:33:58 +00001075 if (exc != MP_OBJ_NULL) {
1076 // had an error so raise the exception
1077 // add traceback to give info about file name and location
1078 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
1079 mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
1080 mp_lexer_free(lex);
1081 nlr_raise(exc);
1082 } else {
1083 mp_lexer_free(lex);
Damien George58e0f4a2015-09-23 10:50:43 +01001084 return parser.tree;
Damien George0bfc7632015-02-07 18:33:58 +00001085 }
Damien429d7192013-10-04 19:53:11 +01001086}
Damien George58e0f4a2015-09-23 10:50:43 +01001087
1088void mp_parse_tree_clear(mp_parse_tree_t *tree) {
1089 mp_parse_chunk_t *chunk = tree->chunk;
1090 while (chunk != NULL) {
1091 mp_parse_chunk_t *next = chunk->union_.next;
1092 m_del(byte, chunk, sizeof(mp_parse_chunk_t) + chunk->alloc);
1093 chunk = next;
1094 }
1095}
Damien Georgedd5353a2015-12-18 12:35:44 +00001096
1097#endif // MICROPY_ENABLE_COMPILER