blob: 04629e03ff8482d1ecba31c38d09989da407bef9 [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>
Damien429d7192013-10-04 19:53:11 +010030#include <assert.h>
Damien George5042bce2014-05-25 22:06:06 +010031#include <string.h>
Damien429d7192013-10-04 19:53:11 +010032
Damien George0bfc7632015-02-07 18:33:58 +000033#include "py/nlr.h"
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/lexer.h"
35#include "py/parse.h"
36#include "py/parsenum.h"
Damien George22b22652016-01-07 14:40:35 +000037#include "py/runtime0.h"
Damien George64f2b212015-10-08 14:58:15 +010038#include "py/runtime.h"
Damien George22b22652016-01-07 14:40:35 +000039#include "py/objint.h"
Damien George64f2b212015-10-08 14:58:15 +010040#include "py/builtin.h"
Damien429d7192013-10-04 19:53:11 +010041
Damien Georgedd5353a2015-12-18 12:35:44 +000042#if MICROPY_ENABLE_COMPILER
43
Damien429d7192013-10-04 19:53:11 +010044#define RULE_ACT_ARG_MASK (0x0f)
Damien Georgeb47ea4e2014-12-20 18:37:50 +000045#define RULE_ACT_KIND_MASK (0x30)
46#define RULE_ACT_ALLOW_IDENT (0x40)
47#define RULE_ACT_ADD_BLANK (0x80)
Damien429d7192013-10-04 19:53:11 +010048#define RULE_ACT_OR (0x10)
49#define RULE_ACT_AND (0x20)
50#define RULE_ACT_LIST (0x30)
51
Damien429d7192013-10-04 19:53:11 +010052#define RULE_ARG_KIND_MASK (0xf000)
53#define RULE_ARG_ARG_MASK (0x0fff)
54#define RULE_ARG_TOK (0x1000)
55#define RULE_ARG_RULE (0x2000)
Damien George4735c452015-04-21 16:43:18 +000056#define RULE_ARG_OPT_RULE (0x3000)
Damien429d7192013-10-04 19:53:11 +010057
Damien Georgeb47ea4e2014-12-20 18:37:50 +000058#define ADD_BLANK_NODE(rule) ((rule->act & RULE_ACT_ADD_BLANK) != 0)
Damien Georgeb829b5c2014-01-25 13:51:19 +000059
Damien429d7192013-10-04 19:53:11 +010060// (un)comment to use rule names; for debugging
61//#define USE_RULE_NAME (1)
62
63typedef struct _rule_t {
64 byte rule_id;
65 byte act;
66#ifdef USE_RULE_NAME
67 const char *rule_name;
68#endif
69 uint16_t arg[];
70} rule_t;
71
72enum {
Damien George00208ce2014-01-23 00:00:53 +000073#define DEF_RULE(rule, comp, kind, ...) RULE_##rule,
Damien George51dfcb42015-01-01 20:27:54 +000074#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010075#undef DEF_RULE
76 RULE_maximum_number_of,
Damien George5042bce2014-05-25 22:06:06 +010077 RULE_string, // special node for non-interned string
Damien George4c81ba82015-01-13 16:21:23 +000078 RULE_bytes, // special node for non-interned bytes
Damien George7d414a12015-02-08 01:57:40 +000079 RULE_const_object, // special node for a constant, generic Python object
Damien429d7192013-10-04 19:53:11 +010080};
81
Damien Georgeb47ea4e2014-12-20 18:37:50 +000082#define ident (RULE_ACT_ALLOW_IDENT)
83#define blank (RULE_ACT_ADD_BLANK)
Damien429d7192013-10-04 19:53:11 +010084#define or(n) (RULE_ACT_OR | n)
85#define and(n) (RULE_ACT_AND | n)
86#define one_or_more (RULE_ACT_LIST | 2)
87#define list (RULE_ACT_LIST | 1)
88#define list_with_end (RULE_ACT_LIST | 3)
Damiend99b0522013-12-21 18:17:45 +000089#define tok(t) (RULE_ARG_TOK | MP_TOKEN_##t)
Damien429d7192013-10-04 19:53:11 +010090#define rule(r) (RULE_ARG_RULE | RULE_##r)
Damien429d7192013-10-04 19:53:11 +010091#define opt_rule(r) (RULE_ARG_OPT_RULE | RULE_##r)
92#ifdef USE_RULE_NAME
Damien George00208ce2014-01-23 00:00:53 +000093#define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, #rule, { __VA_ARGS__ } };
Damien429d7192013-10-04 19:53:11 +010094#else
Damien George00208ce2014-01-23 00:00:53 +000095#define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } };
Damien429d7192013-10-04 19:53:11 +010096#endif
Damien George51dfcb42015-01-01 20:27:54 +000097#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +010098#undef or
99#undef and
100#undef list
101#undef list_with_end
102#undef tok
103#undef rule
Damien429d7192013-10-04 19:53:11 +0100104#undef opt_rule
105#undef one_or_more
106#undef DEF_RULE
107
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200108STATIC const rule_t *rules[] = {
Damien George00208ce2014-01-23 00:00:53 +0000109#define DEF_RULE(rule, comp, kind, ...) &rule_##rule,
Damien George51dfcb42015-01-01 20:27:54 +0000110#include "py/grammar.h"
Damien429d7192013-10-04 19:53:11 +0100111#undef DEF_RULE
112};
113
114typedef struct _rule_stack_t {
Damien George16a6a472015-12-17 13:06:05 +0000115 size_t src_line : 8 * sizeof(size_t) - 8; // maximum bits storing source line number
116 size_t rule_id : 8; // this must be large enough to fit largest rule number
117 size_t arg_i; // this dictates the maximum nodes in a "list" of things
Damien429d7192013-10-04 19:53:11 +0100118} rule_stack_t;
119
Damien George58e0f4a2015-09-23 10:50:43 +0100120typedef struct _mp_parse_chunk_t {
Damien George16a6a472015-12-17 13:06:05 +0000121 size_t alloc;
Damien George58e0f4a2015-09-23 10:50:43 +0100122 union {
Damien George16a6a472015-12-17 13:06:05 +0000123 size_t used;
Damien George58e0f4a2015-09-23 10:50:43 +0100124 struct _mp_parse_chunk_t *next;
125 } union_;
126 byte data[];
127} mp_parse_chunk_t;
128
Damien George64f2b212015-10-08 14:58:15 +0100129typedef enum {
130 PARSE_ERROR_NONE = 0,
131 PARSE_ERROR_MEMORY,
132 PARSE_ERROR_CONST,
133} parse_error_t;
134
Damien429d7192013-10-04 19:53:11 +0100135typedef struct _parser_t {
Damien George64f2b212015-10-08 14:58:15 +0100136 parse_error_t parse_error;
Damien George58ba4c32014-04-10 14:27:31 +0000137
Damien George16a6a472015-12-17 13:06:05 +0000138 size_t rule_stack_alloc;
139 size_t rule_stack_top;
Damien429d7192013-10-04 19:53:11 +0100140 rule_stack_t *rule_stack;
141
Damien George16a6a472015-12-17 13:06:05 +0000142 size_t result_stack_alloc;
143 size_t result_stack_top;
Damiend99b0522013-12-21 18:17:45 +0000144 mp_parse_node_t *result_stack;
Damien George08335002014-01-18 23:24:36 +0000145
146 mp_lexer_t *lexer;
Damien George58e0f4a2015-09-23 10:50:43 +0100147
148 mp_parse_tree_t tree;
149 mp_parse_chunk_t *cur_chunk;
Damien429d7192013-10-04 19:53:11 +0100150
Damien George64f2b212015-10-08 14:58:15 +0100151 #if MICROPY_COMP_CONST
152 mp_map_t consts;
153 #endif
154} parser_t;
Damien George58ba4c32014-04-10 14:27:31 +0000155
Damien George58e0f4a2015-09-23 10:50:43 +0100156STATIC void *parser_alloc(parser_t *parser, size_t num_bytes) {
157 // use a custom memory allocator to store parse nodes sequentially in large chunks
158
159 mp_parse_chunk_t *chunk = parser->cur_chunk;
160
161 if (chunk != NULL && chunk->union_.used + num_bytes > chunk->alloc) {
162 // not enough room at end of previously allocated chunk so try to grow
163 mp_parse_chunk_t *new_data = (mp_parse_chunk_t*)m_renew_maybe(byte, chunk,
164 sizeof(mp_parse_chunk_t) + chunk->alloc,
165 sizeof(mp_parse_chunk_t) + chunk->alloc + num_bytes, false);
166 if (new_data == NULL) {
167 // could not grow existing memory; shrink it to fit previous
168 (void)m_renew(byte, chunk, sizeof(mp_parse_chunk_t) + chunk->alloc,
169 sizeof(mp_parse_chunk_t) + chunk->union_.used);
170 chunk->alloc = chunk->union_.used;
171 chunk->union_.next = parser->tree.chunk;
172 parser->tree.chunk = chunk;
173 chunk = NULL;
174 } else {
175 // could grow existing memory
176 chunk->alloc += num_bytes;
177 }
178 }
179
180 if (chunk == NULL) {
181 // no previous chunk, allocate a new chunk
182 size_t alloc = MICROPY_ALLOC_PARSE_CHUNK_INIT;
183 if (alloc < num_bytes) {
184 alloc = num_bytes;
185 }
186 chunk = (mp_parse_chunk_t*)m_new(byte, sizeof(mp_parse_chunk_t) + alloc);
187 chunk->alloc = alloc;
188 chunk->union_.used = 0;
189 parser->cur_chunk = chunk;
190 }
191
192 byte *ret = chunk->data + chunk->union_.used;
193 chunk->union_.used += num_bytes;
194 return ret;
195}
196
Damien George16a6a472015-12-17 13:06:05 +0000197STATIC 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 +0100198 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000199 return;
200 }
Damien429d7192013-10-04 19:53:11 +0100201 if (parser->rule_stack_top >= parser->rule_stack_alloc) {
Damien Georgeade9a052015-06-13 21:53:22 +0100202 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 +0000203 if (rs == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100204 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000205 return;
206 }
207 parser->rule_stack = rs;
Damien George58ebde42014-05-21 20:32:59 +0100208 parser->rule_stack_alloc += MICROPY_ALLOC_PARSE_RULE_INC;
Damien429d7192013-10-04 19:53:11 +0100209 }
Damien George08335002014-01-18 23:24:36 +0000210 rule_stack_t *rs = &parser->rule_stack[parser->rule_stack_top++];
211 rs->src_line = src_line;
212 rs->rule_id = rule->rule_id;
213 rs->arg_i = arg_i;
Damien429d7192013-10-04 19:53:11 +0100214}
215
Damien George16a6a472015-12-17 13:06:05 +0000216STATIC void push_rule_from_arg(parser_t *parser, size_t arg) {
Damien429d7192013-10-04 19:53:11 +0100217 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 +0000218 size_t rule_id = arg & RULE_ARG_ARG_MASK;
Damien429d7192013-10-04 19:53:11 +0100219 assert(rule_id < RULE_maximum_number_of);
Damien Georgea4c52c52014-12-05 19:35:18 +0000220 push_rule(parser, parser->lexer->tok_line, rules[rule_id], 0);
Damien429d7192013-10-04 19:53:11 +0100221}
222
Damien George16a6a472015-12-17 13:06:05 +0000223STATIC 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 +0100224 assert(!parser->parse_error);
Damien429d7192013-10-04 19:53:11 +0100225 parser->rule_stack_top -= 1;
226 *rule = rules[parser->rule_stack[parser->rule_stack_top].rule_id];
227 *arg_i = parser->rule_stack[parser->rule_stack_top].arg_i;
Damien George08335002014-01-18 23:24:36 +0000228 *src_line = parser->rule_stack[parser->rule_stack_top].src_line;
Damien429d7192013-10-04 19:53:11 +0100229}
230
Damien George16a6a472015-12-17 13:06:05 +0000231mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200232 if (kind == MP_PARSE_NODE_SMALL_INT) {
233 return (mp_parse_node_t)(kind | (arg << 1));
234 }
Damien George7d414a12015-02-08 01:57:40 +0000235 return (mp_parse_node_t)(kind | (arg << 4));
Damien429d7192013-10-04 19:53:11 +0100236}
237
Damien George22b22652016-01-07 14:40:35 +0000238bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
239 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
240 *o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn));
241 return true;
242 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, RULE_const_object)) {
243 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
244 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
245 // nodes are 32-bit pointers, but need to extract 64-bit object
246 *o = (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
247 #else
248 *o = (mp_obj_t)pns->nodes[0];
249 #endif
250 return MP_OBJ_IS_INT(*o);
251 } else {
252 return false;
253 }
254}
255
Damien George16a6a472015-12-17 13:06:05 +0000256int 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 +0000257 if (MP_PARSE_NODE_IS_NULL(*pn)) {
258 *nodes = NULL;
259 return 0;
260 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
261 *nodes = pn;
262 return 1;
263 } else {
264 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
265 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
266 *nodes = pn;
267 return 1;
268 } else {
269 *nodes = pns->nodes;
270 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
271 }
272 }
273}
274
Damien Georgecbd2f742014-01-19 11:48:48 +0000275#if MICROPY_DEBUG_PRINTERS
Damien George16a6a472015-12-17 13:06:05 +0000276void mp_parse_node_print(mp_parse_node_t pn, size_t indent) {
Damien George08335002014-01-18 23:24:36 +0000277 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
278 printf("[% 4d] ", (int)((mp_parse_node_struct_t*)pn)->source_line);
279 } else {
280 printf(" ");
281 }
Damien George16a6a472015-12-17 13:06:05 +0000282 for (size_t i = 0; i < indent; i++) {
Damien429d7192013-10-04 19:53:11 +0100283 printf(" ");
284 }
Damiend99b0522013-12-21 18:17:45 +0000285 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100286 printf("NULL\n");
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200287 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
Damien George40f3c022014-07-03 13:25:24 +0100288 mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200289 printf("int(" INT_FMT ")\n", arg);
Damiend99b0522013-12-21 18:17:45 +0000290 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George16a6a472015-12-17 13:06:05 +0000291 uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +0000292 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
293 case MP_PARSE_NODE_ID: printf("id(%s)\n", qstr_str(arg)); break;
Damiend99b0522013-12-21 18:17:45 +0000294 case MP_PARSE_NODE_STRING: printf("str(%s)\n", qstr_str(arg)); break;
295 case MP_PARSE_NODE_BYTES: printf("bytes(%s)\n", qstr_str(arg)); break;
Damien George16a6a472015-12-17 13:06:05 +0000296 case MP_PARSE_NODE_TOKEN: printf("tok(%u)\n", (uint)arg); break;
Damien429d7192013-10-04 19:53:11 +0100297 default: assert(0);
298 }
299 } else {
Damien George5042bce2014-05-25 22:06:06 +0100300 // node must be a mp_parse_node_struct_t
Damien Georgeb829b5c2014-01-25 13:51:19 +0000301 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien George5042bce2014-05-25 22:06:06 +0100302 if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
303 printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
Damien George4c81ba82015-01-13 16:21:23 +0000304 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_bytes) {
305 printf("literal bytes(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
Damien George7d414a12015-02-08 01:57:40 +0000306 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) {
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000307 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
308 printf("literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
309 #else
Damien George7d414a12015-02-08 01:57:40 +0000310 printf("literal const(%p)\n", (mp_obj_t)pns->nodes[0]);
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000311 #endif
Damien George5042bce2014-05-25 22:06:06 +0100312 } else {
Damien George16a6a472015-12-17 13:06:05 +0000313 size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100314#ifdef USE_RULE_NAME
Damien George16a6a472015-12-17 13:06:05 +0000315 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 +0100316#else
Damien George16a6a472015-12-17 13:06:05 +0000317 printf("rule(%u) (n=%u)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n);
Damien429d7192013-10-04 19:53:11 +0100318#endif
Damien George16a6a472015-12-17 13:06:05 +0000319 for (size_t i = 0; i < n; i++) {
Damien George5042bce2014-05-25 22:06:06 +0100320 mp_parse_node_print(pns->nodes[i], indent + 2);
321 }
Damien429d7192013-10-04 19:53:11 +0100322 }
323 }
324}
Damien Georgecbd2f742014-01-19 11:48:48 +0000325#endif // MICROPY_DEBUG_PRINTERS
Damien429d7192013-10-04 19:53:11 +0100326
327/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200328STATIC void result_stack_show(parser_t *parser) {
Damien429d7192013-10-04 19:53:11 +0100329 printf("result stack, most recent first\n");
Damien George16a6a472015-12-17 13:06:05 +0000330 for (ssize_t i = parser->result_stack_top - 1; i >= 0; i--) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000331 mp_parse_node_print(parser->result_stack[i], 0);
Damien429d7192013-10-04 19:53:11 +0100332 }
333}
334*/
335
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200336STATIC mp_parse_node_t pop_result(parser_t *parser) {
Damien George64f2b212015-10-08 14:58:15 +0100337 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000338 return MP_PARSE_NODE_NULL;
339 }
Damien429d7192013-10-04 19:53:11 +0100340 assert(parser->result_stack_top > 0);
341 return parser->result_stack[--parser->result_stack_top];
342}
343
Damien George16a6a472015-12-17 13:06:05 +0000344STATIC mp_parse_node_t peek_result(parser_t *parser, size_t pos) {
Damien George64f2b212015-10-08 14:58:15 +0100345 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000346 return MP_PARSE_NODE_NULL;
347 }
Damien429d7192013-10-04 19:53:11 +0100348 assert(parser->result_stack_top > pos);
349 return parser->result_stack[parser->result_stack_top - 1 - pos];
350}
351
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200352STATIC void push_result_node(parser_t *parser, mp_parse_node_t pn) {
Damien George64f2b212015-10-08 14:58:15 +0100353 if (parser->parse_error) {
Damien George58ba4c32014-04-10 14:27:31 +0000354 return;
355 }
Damien George69a818d2014-01-12 13:55:24 +0000356 if (parser->result_stack_top >= parser->result_stack_alloc) {
Damien Georgeade9a052015-06-13 21:53:22 +0100357 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 +0000358 if (stack == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100359 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000360 return;
361 }
Damien George50912e72015-01-20 11:55:10 +0000362 parser->result_stack = stack;
Damien George58ebde42014-05-21 20:32:59 +0100363 parser->result_stack_alloc += MICROPY_ALLOC_PARSE_RESULT_INC;
Damien George69a818d2014-01-12 13:55:24 +0000364 }
Damien429d7192013-10-04 19:53:11 +0100365 parser->result_stack[parser->result_stack_top++] = pn;
366}
367
Damien George16a6a472015-12-17 13:06:05 +0000368STATIC 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 +0100369 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 +0100370 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100371 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George7d414a12015-02-08 01:57:40 +0000372 return MP_PARSE_NODE_NULL;
Damien George5042bce2014-05-25 22:06:06 +0100373 }
374 pn->source_line = src_line;
Damien George4c81ba82015-01-13 16:21:23 +0000375 pn->kind_num_nodes = rule_kind | (2 << 8);
Damien George5042bce2014-05-25 22:06:06 +0100376 char *p = m_new(char, len);
377 memcpy(p, str, len);
Damien George999cedb2015-11-27 17:01:44 +0000378 pn->nodes[0] = (uintptr_t)p;
Damien George5042bce2014-05-25 22:06:06 +0100379 pn->nodes[1] = len;
Damien George7d414a12015-02-08 01:57:40 +0000380 return (mp_parse_node_t)pn;
381}
382
Damien George16a6a472015-12-17 13:06:05 +0000383STATIC 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 +0000384 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 +0000385 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100386 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George7d414a12015-02-08 01:57:40 +0000387 return MP_PARSE_NODE_NULL;
388 }
389 pn->source_line = src_line;
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000390 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
391 // nodes are 32-bit pointers, but need to store 64-bit object
392 pn->kind_num_nodes = RULE_const_object | (2 << 8);
393 pn->nodes[0] = (uint64_t)obj;
394 pn->nodes[1] = (uint64_t)obj >> 32;
395 #else
Damien George7d414a12015-02-08 01:57:40 +0000396 pn->kind_num_nodes = RULE_const_object | (1 << 8);
Damien George16a6a472015-12-17 13:06:05 +0000397 pn->nodes[0] = (uintptr_t)obj;
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000398 #endif
Damien George7d414a12015-02-08 01:57:40 +0000399 return (mp_parse_node_t)pn;
Damien George5042bce2014-05-25 22:06:06 +0100400}
Paul Sokolovsky9e76b112014-05-08 22:43:46 +0300401
Damien Georgea4c52c52014-12-05 19:35:18 +0000402STATIC void push_result_token(parser_t *parser) {
Damiend99b0522013-12-21 18:17:45 +0000403 mp_parse_node_t pn;
Damien Georgea4c52c52014-12-05 19:35:18 +0000404 mp_lexer_t *lex = parser->lexer;
405 if (lex->tok_kind == MP_TOKEN_NAME) {
Damien George64f2b212015-10-08 14:58:15 +0100406 qstr id = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
407 #if MICROPY_COMP_CONST
408 // lookup identifier in table of dynamic constants
409 mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP);
410 if (elem != NULL) {
411 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
412 } else
413 #endif
414 {
415 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, id);
416 }
Damien George7d414a12015-02-08 01:57:40 +0000417 } else if (lex->tok_kind == MP_TOKEN_INTEGER) {
418 mp_obj_t o = mp_parse_num_integer(lex->vstr.buf, lex->vstr.len, 0, lex);
419 if (MP_OBJ_IS_SMALL_INT(o)) {
420 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(o));
Damien429d7192013-10-04 19:53:11 +0100421 } else {
Damien George7d414a12015-02-08 01:57:40 +0000422 pn = make_node_const_object(parser, lex->tok_line, o);
Damien429d7192013-10-04 19:53:11 +0100423 }
Damien George7d414a12015-02-08 01:57:40 +0000424 } else if (lex->tok_kind == MP_TOKEN_FLOAT_OR_IMAG) {
425 mp_obj_t o = mp_parse_num_decimal(lex->vstr.buf, lex->vstr.len, true, false, lex);
426 pn = make_node_const_object(parser, lex->tok_line, o);
Damien George4c81ba82015-01-13 16:21:23 +0000427 } else if (lex->tok_kind == MP_TOKEN_STRING || lex->tok_kind == MP_TOKEN_BYTES) {
428 // Don't automatically intern all strings/bytes. doc strings (which are usually large)
Damien George5042bce2014-05-25 22:06:06 +0100429 // will be discarded by the compiler, and so we shouldn't intern them.
430 qstr qst = MP_QSTR_NULL;
Damien Georgea4c52c52014-12-05 19:35:18 +0000431 if (lex->vstr.len <= MICROPY_ALLOC_PARSE_INTERN_STRING_LEN) {
Damien George5042bce2014-05-25 22:06:06 +0100432 // intern short strings
Damien Georgea4c52c52014-12-05 19:35:18 +0000433 qst = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
Damien George5042bce2014-05-25 22:06:06 +0100434 } else {
435 // check if this string is already interned
Damien Georgea4c52c52014-12-05 19:35:18 +0000436 qst = qstr_find_strn(lex->vstr.buf, lex->vstr.len);
Damien George5042bce2014-05-25 22:06:06 +0100437 }
438 if (qst != MP_QSTR_NULL) {
439 // qstr exists, make a leaf node
Damien George4c81ba82015-01-13 16:21:23 +0000440 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 +0100441 } else {
Damien George4c81ba82015-01-13 16:21:23 +0000442 // not interned, make a node holding a pointer to the string/bytes data
Damien George7d414a12015-02-08 01:57:40 +0000443 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 +0100444 }
Damien429d7192013-10-04 19:53:11 +0100445 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000446 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_TOKEN, lex->tok_kind);
Damien429d7192013-10-04 19:53:11 +0100447 }
448 push_result_node(parser, pn);
449}
450
Damien George64f2b212015-10-08 14:58:15 +0100451#if MICROPY_COMP_MODULE_CONST
Damien Georgecbf76742015-11-27 13:38:15 +0000452STATIC const mp_rom_map_elem_t mp_constants_table[] = {
Damien George64f2b212015-10-08 14:58:15 +0100453 #if MICROPY_PY_UCTYPES
Damien Georgecbf76742015-11-27 13:38:15 +0000454 { MP_ROM_QSTR(MP_QSTR_uctypes), MP_ROM_PTR(&mp_module_uctypes) },
Damien George64f2b212015-10-08 14:58:15 +0100455 #endif
456 // Extra constants as defined by a port
457 MICROPY_PORT_CONSTANTS
458};
459STATIC MP_DEFINE_CONST_MAP(mp_constants_map, mp_constants_table);
460#endif
461
462#if MICROPY_COMP_CONST_FOLDING
Damien George16a6a472015-12-17 13:06:05 +0000463STATIC bool fold_constants(parser_t *parser, const rule_t *rule, size_t num_args) {
Damien George64f2b212015-10-08 14:58:15 +0100464 // this code does folding of arbitrary integer expressions, eg 1 + 2 * 3 + 4
465 // it does not do partial folding, eg 1 + 2 + x -> 3 + x
466
Damien George22b22652016-01-07 14:40:35 +0000467 mp_obj_t arg0;
Damien George64f2b212015-10-08 14:58:15 +0100468 if (rule->rule_id == RULE_expr
469 || rule->rule_id == RULE_xor_expr
470 || rule->rule_id == RULE_and_expr) {
471 // folding for binary ops: | ^ &
472 mp_parse_node_t pn = peek_result(parser, num_args - 1);
Damien George22b22652016-01-07 14:40:35 +0000473 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100474 return false;
475 }
Damien George22b22652016-01-07 14:40:35 +0000476 mp_binary_op_t op;
477 if (rule->rule_id == RULE_expr) {
478 op = MP_BINARY_OP_OR;
479 } else if (rule->rule_id == RULE_xor_expr) {
480 op = MP_BINARY_OP_XOR;
481 } else {
482 op = MP_BINARY_OP_AND;
483 }
Damien George16a6a472015-12-17 13:06:05 +0000484 for (ssize_t i = num_args - 2; i >= 0; --i) {
Damien George64f2b212015-10-08 14:58:15 +0100485 pn = peek_result(parser, i);
Damien George22b22652016-01-07 14:40:35 +0000486 mp_obj_t arg1;
487 if (!mp_parse_node_get_int_maybe(pn, &arg1)) {
Damien George64f2b212015-10-08 14:58:15 +0100488 return false;
489 }
Damien George22b22652016-01-07 14:40:35 +0000490 arg0 = mp_binary_op(op, arg0, arg1);
Damien George64f2b212015-10-08 14:58:15 +0100491 }
492 } else if (rule->rule_id == RULE_shift_expr
493 || rule->rule_id == RULE_arith_expr
494 || rule->rule_id == RULE_term) {
495 // folding for binary ops: << >> + - * / % //
496 mp_parse_node_t pn = peek_result(parser, num_args - 1);
Damien George22b22652016-01-07 14:40:35 +0000497 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100498 return false;
499 }
Damien George16a6a472015-12-17 13:06:05 +0000500 for (ssize_t i = num_args - 2; i >= 1; i -= 2) {
Damien George64f2b212015-10-08 14:58:15 +0100501 pn = peek_result(parser, i - 1);
Damien George22b22652016-01-07 14:40:35 +0000502 mp_obj_t arg1;
503 if (!mp_parse_node_get_int_maybe(pn, &arg1)) {
Damien George64f2b212015-10-08 14:58:15 +0100504 return false;
505 }
Damien George64f2b212015-10-08 14:58:15 +0100506 mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, i));
Damien George22b22652016-01-07 14:40:35 +0000507 static const uint8_t token_to_op[] = {
508 MP_BINARY_OP_ADD,
509 MP_BINARY_OP_SUBTRACT,
510 MP_BINARY_OP_MULTIPLY,
511 255,//MP_BINARY_OP_POWER,
512 255,//MP_BINARY_OP_TRUE_DIVIDE,
513 MP_BINARY_OP_FLOOR_DIVIDE,
514 MP_BINARY_OP_MODULO,
515 255,//MP_BINARY_OP_LESS
516 MP_BINARY_OP_LSHIFT,
517 255,//MP_BINARY_OP_MORE
518 MP_BINARY_OP_RSHIFT,
519 };
520 mp_binary_op_t op = token_to_op[tok - MP_TOKEN_OP_PLUS];
521 if (op == 255) {
Damien George64f2b212015-10-08 14:58:15 +0100522 return false;
523 }
Damien George22b22652016-01-07 14:40:35 +0000524 int rhs_sign = mp_obj_int_sign(arg1);
525 if (op <= MP_BINARY_OP_RSHIFT) {
526 // << and >> can't have negative rhs
527 if (rhs_sign < 0) {
528 return false;
529 }
530 } else if (op >= MP_BINARY_OP_FLOOR_DIVIDE) {
531 // % and // can't have zero rhs
532 if (rhs_sign == 0) {
533 return false;
534 }
535 }
536 arg0 = mp_binary_op(op, arg0, arg1);
Damien George64f2b212015-10-08 14:58:15 +0100537 }
538 } else if (rule->rule_id == RULE_factor_2) {
539 // folding for unary ops: + - ~
540 mp_parse_node_t pn = peek_result(parser, 0);
Damien George22b22652016-01-07 14:40:35 +0000541 if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
Damien George64f2b212015-10-08 14:58:15 +0100542 return false;
543 }
Damien George64f2b212015-10-08 14:58:15 +0100544 mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, 1));
Damien George22b22652016-01-07 14:40:35 +0000545 mp_binary_op_t op;
Damien George64f2b212015-10-08 14:58:15 +0100546 if (tok == MP_TOKEN_OP_PLUS) {
Damien George22b22652016-01-07 14:40:35 +0000547 op = MP_UNARY_OP_POSITIVE;
Damien George64f2b212015-10-08 14:58:15 +0100548 } else if (tok == MP_TOKEN_OP_MINUS) {
Damien George22b22652016-01-07 14:40:35 +0000549 op = MP_UNARY_OP_NEGATIVE;
Damien George64f2b212015-10-08 14:58:15 +0100550 } else {
551 assert(tok == MP_TOKEN_OP_TILDE); // should be
Damien George22b22652016-01-07 14:40:35 +0000552 op = MP_UNARY_OP_INVERT;
Damien George64f2b212015-10-08 14:58:15 +0100553 }
Damien George22b22652016-01-07 14:40:35 +0000554 arg0 = mp_unary_op(op, arg0);
Damien George64f2b212015-10-08 14:58:15 +0100555
556 #if MICROPY_COMP_CONST
557 } else if (rule->rule_id == RULE_expr_stmt) {
558 mp_parse_node_t pn1 = peek_result(parser, 0);
559 if (!MP_PARSE_NODE_IS_NULL(pn1)
560 && !(MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_augassign)
561 || MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_assign_list))) {
562 // this node is of the form <x> = <y>
563 mp_parse_node_t pn0 = peek_result(parser, 1);
564 if (MP_PARSE_NODE_IS_ID(pn0)
565 && MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_power)
566 && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pn1)->nodes[0])
567 && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn1)->nodes[0]) == MP_QSTR_const
568 && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pn1)->nodes[1], RULE_trailer_paren)
569 && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pn1)->nodes[2])
570 ) {
571 // code to assign dynamic constants: id = const(value)
572
573 // get the id
574 qstr id = MP_PARSE_NODE_LEAF_ARG(pn0);
575
576 // get the value
577 mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pn1)->nodes[1])->nodes[0];
578 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
579 parser->parse_error = PARSE_ERROR_CONST;
580 return false;
581 }
582 mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
583
584 // store the value in the table of dynamic constants
585 mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
586 assert(elem->value == MP_OBJ_NULL);
587 elem->value = MP_OBJ_NEW_SMALL_INT(value);
588
589 // replace const(value) with value
590 pop_result(parser);
591 push_result_node(parser, pn_value);
592
593 // finished folding this assignment, but we still want it to be part of the tree
594 return false;
595 }
596 }
597 return false;
598 #endif
599
600 #if MICROPY_COMP_MODULE_CONST
601 } else if (rule->rule_id == RULE_power) {
602 mp_parse_node_t pn0 = peek_result(parser, 2);
603 mp_parse_node_t pn1 = peek_result(parser, 1);
604 mp_parse_node_t pn2 = peek_result(parser, 0);
605 if (!(MP_PARSE_NODE_IS_ID(pn0)
606 && MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_trailer_period)
607 && MP_PARSE_NODE_IS_NULL(pn2))) {
608 return false;
609 }
610 // id1.id2
611 // look it up in constant table, see if it can be replaced with an integer
612 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pn1;
613 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
614 qstr q_base = MP_PARSE_NODE_LEAF_ARG(pn0);
615 qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
616 mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
617 if (elem == NULL) {
618 return false;
619 }
620 mp_obj_t dest[2];
621 mp_load_method_maybe(elem->value, q_attr, dest);
Damien George22b22652016-01-07 14:40:35 +0000622 if (!(MP_OBJ_IS_INT(dest[0]) && dest[1] == MP_OBJ_NULL)) {
Damien George64f2b212015-10-08 14:58:15 +0100623 return false;
624 }
Damien George22b22652016-01-07 14:40:35 +0000625 arg0 = dest[0];
Damien George64f2b212015-10-08 14:58:15 +0100626 #endif
627
628 } else {
629 return false;
630 }
631
632 // success folding this rule
633
Damien George16a6a472015-12-17 13:06:05 +0000634 for (size_t i = num_args; i > 0; i--) {
Damien George64f2b212015-10-08 14:58:15 +0100635 pop_result(parser);
636 }
Damien George22b22652016-01-07 14:40:35 +0000637 if (MP_OBJ_IS_SMALL_INT(arg0)) {
638 push_result_node(parser, mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(arg0)));
639 } else {
640 // TODO reuse memory for parse node struct?
641 push_result_node(parser, make_node_const_object(parser, 0, arg0));
642 }
Damien George64f2b212015-10-08 14:58:15 +0100643
644 return true;
645}
646#endif
647
Damien George16a6a472015-12-17 13:06:05 +0000648STATIC 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 +0000649 // optimise away parenthesis around an expression if possible
650 if (rule->rule_id == RULE_atom_paren) {
651 // there should be just 1 arg for this rule
652 mp_parse_node_t pn = peek_result(parser, 0);
653 if (MP_PARSE_NODE_IS_NULL(pn)) {
654 // need to keep parenthesis for ()
655 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, RULE_testlist_comp)) {
656 // need to keep parenthesis for (a, b, ...)
657 } else {
658 // parenthesis around a single expression, so it's just the expression
659 return;
660 }
661 }
662
Damien George64f2b212015-10-08 14:58:15 +0100663 #if MICROPY_COMP_CONST_FOLDING
664 if (fold_constants(parser, rule, num_args)) {
665 // we folded this rule so return straight away
666 return;
667 }
668 #endif
669
Damien George58e0f4a2015-09-23 10:50:43 +0100670 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 +0000671 if (pn == NULL) {
Damien George64f2b212015-10-08 14:58:15 +0100672 parser->parse_error = PARSE_ERROR_MEMORY;
Damien George58ba4c32014-04-10 14:27:31 +0000673 return;
674 }
675 pn->source_line = src_line;
676 pn->kind_num_nodes = (rule->rule_id & 0xff) | (num_args << 8);
Damien George16a6a472015-12-17 13:06:05 +0000677 for (size_t i = num_args; i > 0; i--) {
Damien429d7192013-10-04 19:53:11 +0100678 pn->nodes[i - 1] = pop_result(parser);
679 }
Damiend99b0522013-12-21 18:17:45 +0000680 push_result_node(parser, (mp_parse_node_t)pn);
Damien429d7192013-10-04 19:53:11 +0100681}
682
Damien George58e0f4a2015-09-23 10:50:43 +0100683mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
Damien George69a818d2014-01-12 13:55:24 +0000684
Damien George1b82e9a2014-05-10 17:36:41 +0100685 // initialise parser and allocate memory for its stacks
Damien George69a818d2014-01-12 13:55:24 +0000686
Damien George1b82e9a2014-05-10 17:36:41 +0100687 parser_t parser;
Damien George69a818d2014-01-12 13:55:24 +0000688
Damien George64f2b212015-10-08 14:58:15 +0100689 parser.parse_error = PARSE_ERROR_NONE;
Damien George58ba4c32014-04-10 14:27:31 +0000690
Damien George58ebde42014-05-21 20:32:59 +0100691 parser.rule_stack_alloc = MICROPY_ALLOC_PARSE_RULE_INIT;
Damien George1b82e9a2014-05-10 17:36:41 +0100692 parser.rule_stack_top = 0;
693 parser.rule_stack = m_new_maybe(rule_stack_t, parser.rule_stack_alloc);
Damien429d7192013-10-04 19:53:11 +0100694
Damien George58ebde42014-05-21 20:32:59 +0100695 parser.result_stack_alloc = MICROPY_ALLOC_PARSE_RESULT_INIT;
Damien George1b82e9a2014-05-10 17:36:41 +0100696 parser.result_stack_top = 0;
697 parser.result_stack = m_new_maybe(mp_parse_node_t, parser.result_stack_alloc);
Damien429d7192013-10-04 19:53:11 +0100698
Damien George1b82e9a2014-05-10 17:36:41 +0100699 parser.lexer = lex;
700
Damien George58e0f4a2015-09-23 10:50:43 +0100701 parser.tree.chunk = NULL;
702 parser.cur_chunk = NULL;
703
Damien George64f2b212015-10-08 14:58:15 +0100704 #if MICROPY_COMP_CONST
705 mp_map_init(&parser.consts, 0);
706 #endif
707
Damien George1b82e9a2014-05-10 17:36:41 +0100708 // check if we could allocate the stacks
709 if (parser.rule_stack == NULL || parser.result_stack == NULL) {
710 goto memory_error;
711 }
Damien George08335002014-01-18 23:24:36 +0000712
Damien George69a818d2014-01-12 13:55:24 +0000713 // work out the top-level rule to use, and push it on the stack
Damien George16a6a472015-12-17 13:06:05 +0000714 size_t top_level_rule;
Damien5ac1b2e2013-10-18 19:58:12 +0100715 switch (input_kind) {
Damiend99b0522013-12-21 18:17:45 +0000716 case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
Damien Georged02c6d82014-01-15 22:14:03 +0000717 case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
Damien5ac1b2e2013-10-18 19:58:12 +0100718 default: top_level_rule = RULE_file_input;
719 }
Damien Georgea4c52c52014-12-05 19:35:18 +0000720 push_rule(&parser, lex->tok_line, rules[top_level_rule], 0);
Damien429d7192013-10-04 19:53:11 +0100721
Damien George69a818d2014-01-12 13:55:24 +0000722 // parse!
723
Damien George16a6a472015-12-17 13:06:05 +0000724 size_t n, i; // state for the current rule
725 size_t rule_src_line; // source line for the first token matched by the current rule
Damien429d7192013-10-04 19:53:11 +0100726 bool backtrack = false;
Damien George08335002014-01-18 23:24:36 +0000727 const rule_t *rule = NULL;
Damien429d7192013-10-04 19:53:11 +0100728
729 for (;;) {
730 next_rule:
Damien George64f2b212015-10-08 14:58:15 +0100731 if (parser.rule_stack_top == 0 || parser.parse_error) {
Damien429d7192013-10-04 19:53:11 +0100732 break;
733 }
734
Damien George1b82e9a2014-05-10 17:36:41 +0100735 pop_rule(&parser, &rule, &i, &rule_src_line);
Damien429d7192013-10-04 19:53:11 +0100736 n = rule->act & RULE_ACT_ARG_MASK;
737
738 /*
739 // debugging
Damien George1b82e9a2014-05-10 17:36:41 +0100740 printf("depth=%d ", parser.rule_stack_top);
741 for (int j = 0; j < parser.rule_stack_top; ++j) {
Damien429d7192013-10-04 19:53:11 +0100742 printf(" ");
743 }
744 printf("%s n=%d i=%d bt=%d\n", rule->rule_name, n, i, backtrack);
745 */
746
747 switch (rule->act & RULE_ACT_KIND_MASK) {
748 case RULE_ACT_OR:
749 if (i > 0 && !backtrack) {
750 goto next_rule;
751 } else {
752 backtrack = false;
753 }
Damien Georgefa7c61d2015-07-24 14:35:57 +0000754 for (; i < n; ++i) {
755 uint16_t kind = rule->arg[i] & RULE_ARG_KIND_MASK;
756 if (kind == RULE_ARG_TOK) {
757 if (lex->tok_kind == (rule->arg[i] & RULE_ARG_ARG_MASK)) {
758 push_result_token(&parser);
759 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100760 goto next_rule;
Damien Georgefa7c61d2015-07-24 14:35:57 +0000761 }
Damien429d7192013-10-04 19:53:11 +0100762 } else {
Damien Georgefa7c61d2015-07-24 14:35:57 +0000763 assert(kind == RULE_ARG_RULE);
764 if (i + 1 < n) {
765 push_rule(&parser, rule_src_line, rule, i + 1); // save this or-rule
766 }
767 push_rule_from_arg(&parser, rule->arg[i]); // push child of or-rule
Damien429d7192013-10-04 19:53:11 +0100768 goto next_rule;
769 }
Damien429d7192013-10-04 19:53:11 +0100770 }
Damien Georgefa7c61d2015-07-24 14:35:57 +0000771 backtrack = true;
Damien429d7192013-10-04 19:53:11 +0100772 break;
773
Damien George2870d852014-12-20 18:06:08 +0000774 case RULE_ACT_AND: {
Damien429d7192013-10-04 19:53:11 +0100775
776 // failed, backtrack if we can, else syntax error
777 if (backtrack) {
778 assert(i > 0);
779 if ((rule->arg[i - 1] & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE) {
780 // an optional rule that failed, so continue with next arg
Damien George1b82e9a2014-05-10 17:36:41 +0100781 push_result_node(&parser, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +0100782 backtrack = false;
783 } else {
784 // a mandatory rule that failed, so propagate backtrack
785 if (i > 1) {
786 // already eaten tokens so can't backtrack
787 goto syntax_error;
788 } else {
789 goto next_rule;
790 }
791 }
792 }
793
794 // progress through the rule
795 for (; i < n; ++i) {
796 switch (rule->arg[i] & RULE_ARG_KIND_MASK) {
Damien George2870d852014-12-20 18:06:08 +0000797 case RULE_ARG_TOK: {
Damien429d7192013-10-04 19:53:11 +0100798 // need to match a token
Damien George2870d852014-12-20 18:06:08 +0000799 mp_token_kind_t tok_kind = rule->arg[i] & RULE_ARG_ARG_MASK;
Damien Georgea4c52c52014-12-05 19:35:18 +0000800 if (lex->tok_kind == tok_kind) {
Damien429d7192013-10-04 19:53:11 +0100801 // matched token
Damiend99b0522013-12-21 18:17:45 +0000802 if (tok_kind == MP_TOKEN_NAME) {
Damien Georgea4c52c52014-12-05 19:35:18 +0000803 push_result_token(&parser);
Damien429d7192013-10-04 19:53:11 +0100804 }
Damiend99b0522013-12-21 18:17:45 +0000805 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100806 } else {
807 // failed to match token
808 if (i > 0) {
809 // already eaten tokens so can't backtrack
810 goto syntax_error;
811 } else {
812 // this rule failed, so backtrack
813 backtrack = true;
814 goto next_rule;
815 }
816 }
817 break;
Damien Georged2d64f02015-01-14 21:32:42 +0000818 }
Damien429d7192013-10-04 19:53:11 +0100819 case RULE_ARG_RULE:
Damien429d7192013-10-04 19:53:11 +0100820 case RULE_ARG_OPT_RULE:
Damien Georged2d64f02015-01-14 21:32:42 +0000821 rule_and_no_other_choice:
Damien George1b82e9a2014-05-10 17:36:41 +0100822 push_rule(&parser, rule_src_line, rule, i + 1); // save this and-rule
823 push_rule_from_arg(&parser, rule->arg[i]); // push child of and-rule
Damien429d7192013-10-04 19:53:11 +0100824 goto next_rule;
825 default:
826 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000827 goto rule_and_no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100828 }
829 }
830
831 assert(i == n);
832
833 // matched the rule, so now build the corresponding parse_node
834
835 // count number of arguments for the parse_node
836 i = 0;
Damien George2870d852014-12-20 18:06:08 +0000837 bool emit_rule = false;
Damien George16a6a472015-12-17 13:06:05 +0000838 for (size_t x = 0; x < n; ++x) {
Damien429d7192013-10-04 19:53:11 +0100839 if ((rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
Damien George2870d852014-12-20 18:06:08 +0000840 mp_token_kind_t tok_kind = rule->arg[x] & RULE_ARG_ARG_MASK;
Damiend99b0522013-12-21 18:17:45 +0000841 if (tok_kind >= MP_TOKEN_NAME) {
Damien429d7192013-10-04 19:53:11 +0100842 emit_rule = true;
843 }
Damiend99b0522013-12-21 18:17:45 +0000844 if (tok_kind == MP_TOKEN_NAME) {
Damien429d7192013-10-04 19:53:11 +0100845 // only tokens which were names are pushed to stack
846 i += 1;
847 }
848 } else {
849 // rules are always pushed
850 i += 1;
851 }
852 }
853
Damien George65dc9602015-08-14 12:24:11 +0100854 #if !MICROPY_ENABLE_DOC_STRING
Damien George5042bce2014-05-25 22:06:06 +0100855 // this code discards lonely statements, such as doc strings
Damien George1b82e9a2014-05-10 17:36:41 +0100856 if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) {
857 mp_parse_node_t p = peek_result(&parser, 1);
Damien George5042bce2014-05-25 22:06:06 +0100858 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 +0000859 pop_result(&parser); // MP_PARSE_NODE_NULL
Damien George58e0f4a2015-09-23 10:50:43 +0100860 mp_parse_node_t pn = pop_result(&parser); // possibly RULE_string
861 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
862 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
863 if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_string) {
Damien George16a6a472015-12-17 13:06:05 +0000864 m_del(char, (char*)pns->nodes[0], (size_t)pns->nodes[1]);
Damien George58e0f4a2015-09-23 10:50:43 +0100865 }
866 }
Damien George1b82e9a2014-05-10 17:36:41 +0100867 push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0);
Damien George93afa232014-05-06 21:44:11 +0100868 break;
869 }
870 }
Damien George65dc9602015-08-14 12:24:11 +0100871 #endif
Damien George93afa232014-05-06 21:44:11 +0100872
Damien429d7192013-10-04 19:53:11 +0100873 // always emit these rules, even if they have only 1 argument
874 if (rule->rule_id == RULE_expr_stmt || rule->rule_id == RULE_yield_stmt) {
875 emit_rule = true;
876 }
877
Damien Georgeb47ea4e2014-12-20 18:37:50 +0000878 // if a rule has the RULE_ACT_ALLOW_IDENT bit set then this
879 // rule should not be emitted if it has only 1 argument
Damien Georgeb47ea4e2014-12-20 18:37:50 +0000880 if (rule->act & RULE_ACT_ALLOW_IDENT) {
Damien429d7192013-10-04 19:53:11 +0100881 emit_rule = false;
882 }
883
884 // 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 +0000885 if (ADD_BLANK_NODE(rule)) {
Damien429d7192013-10-04 19:53:11 +0100886 emit_rule = true;
Damien George1b82e9a2014-05-10 17:36:41 +0100887 push_result_node(&parser, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +0100888 i += 1;
889 }
890
Damien George16a6a472015-12-17 13:06:05 +0000891 size_t num_not_nil = 0;
892 for (size_t x = 0; x < i; ++x) {
Damien George1b82e9a2014-05-10 17:36:41 +0100893 if (peek_result(&parser, x) != MP_PARSE_NODE_NULL) {
Damien429d7192013-10-04 19:53:11 +0100894 num_not_nil += 1;
895 }
896 }
Damien George366239b2015-10-08 23:13:18 +0100897 if (emit_rule || num_not_nil != 1) {
898 // need to add rule when num_not_nil==0 for, eg, atom_paren, testlist_comp_3b
Damien George1b82e9a2014-05-10 17:36:41 +0100899 push_result_rule(&parser, rule_src_line, rule, i);
Damien George366239b2015-10-08 23:13:18 +0100900 } else {
Damien429d7192013-10-04 19:53:11 +0100901 // single result, leave it on stack
Damiend99b0522013-12-21 18:17:45 +0000902 mp_parse_node_t pn = MP_PARSE_NODE_NULL;
Damien George16a6a472015-12-17 13:06:05 +0000903 for (size_t x = 0; x < i; ++x) {
Damien George1b82e9a2014-05-10 17:36:41 +0100904 mp_parse_node_t pn2 = pop_result(&parser);
Damiend99b0522013-12-21 18:17:45 +0000905 if (pn2 != MP_PARSE_NODE_NULL) {
Damien429d7192013-10-04 19:53:11 +0100906 pn = pn2;
907 }
908 }
Damien George1b82e9a2014-05-10 17:36:41 +0100909 push_result_node(&parser, pn);
Damien429d7192013-10-04 19:53:11 +0100910 }
911 break;
Damien George2870d852014-12-20 18:06:08 +0000912 }
Damien429d7192013-10-04 19:53:11 +0100913
Damien George2870d852014-12-20 18:06:08 +0000914 case RULE_ACT_LIST: {
Damien429d7192013-10-04 19:53:11 +0100915 // n=2 is: item item*
916 // n=1 is: item (sep item)*
917 // n=3 is: item (sep item)* [sep]
Damien George2870d852014-12-20 18:06:08 +0000918 bool had_trailing_sep;
Damien429d7192013-10-04 19:53:11 +0100919 if (backtrack) {
920 list_backtrack:
921 had_trailing_sep = false;
922 if (n == 2) {
923 if (i == 1) {
924 // fail on item, first time round; propagate backtrack
925 goto next_rule;
926 } else {
927 // fail on item, in later rounds; finish with this rule
928 backtrack = false;
929 }
930 } else {
931 if (i == 1) {
932 // fail on item, first time round; propagate backtrack
933 goto next_rule;
934 } else if ((i & 1) == 1) {
935 // fail on item, in later rounds; have eaten tokens so can't backtrack
936 if (n == 3) {
937 // list allows trailing separator; finish parsing list
938 had_trailing_sep = true;
939 backtrack = false;
940 } else {
941 // list doesn't allowing trailing separator; fail
942 goto syntax_error;
943 }
944 } else {
945 // fail on separator; finish parsing list
946 backtrack = false;
947 }
948 }
949 } else {
950 for (;;) {
Damien George16a6a472015-12-17 13:06:05 +0000951 size_t arg = rule->arg[i & 1 & n];
Damien429d7192013-10-04 19:53:11 +0100952 switch (arg & RULE_ARG_KIND_MASK) {
953 case RULE_ARG_TOK:
Damien Georgea4c52c52014-12-05 19:35:18 +0000954 if (lex->tok_kind == (arg & RULE_ARG_ARG_MASK)) {
Damien429d7192013-10-04 19:53:11 +0100955 if (i & 1 & n) {
956 // separators which are tokens are not pushed to result stack
957 } else {
Damien Georgea4c52c52014-12-05 19:35:18 +0000958 push_result_token(&parser);
Damien429d7192013-10-04 19:53:11 +0100959 }
Damiend99b0522013-12-21 18:17:45 +0000960 mp_lexer_to_next(lex);
Damien429d7192013-10-04 19:53:11 +0100961 // got element of list, so continue parsing list
962 i += 1;
963 } else {
964 // couldn't get element of list
965 i += 1;
966 backtrack = true;
967 goto list_backtrack;
968 }
969 break;
970 case RULE_ARG_RULE:
Damien Georged2d64f02015-01-14 21:32:42 +0000971 rule_list_no_other_choice:
Damien George1b82e9a2014-05-10 17:36:41 +0100972 push_rule(&parser, rule_src_line, rule, i + 1); // save this list-rule
973 push_rule_from_arg(&parser, arg); // push child of list-rule
Damien429d7192013-10-04 19:53:11 +0100974 goto next_rule;
975 default:
976 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000977 goto rule_list_no_other_choice; // to help flow control analysis
Damien429d7192013-10-04 19:53:11 +0100978 }
979 }
980 }
981 assert(i >= 1);
982
983 // compute number of elements in list, result in i
984 i -= 1;
985 if ((n & 1) && (rule->arg[1] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
986 // don't count separators when they are tokens
987 i = (i + 1) / 2;
988 }
989
990 if (i == 1) {
991 // list matched single item
992 if (had_trailing_sep) {
993 // if there was a trailing separator, make a list of a single item
Damien George1b82e9a2014-05-10 17:36:41 +0100994 push_result_rule(&parser, rule_src_line, rule, i);
Damien429d7192013-10-04 19:53:11 +0100995 } else {
996 // just leave single item on stack (ie don't wrap in a list)
997 }
998 } else {
Damien George1b82e9a2014-05-10 17:36:41 +0100999 push_result_rule(&parser, rule_src_line, rule, i);
Damien429d7192013-10-04 19:53:11 +01001000 }
1001 break;
Damien George2870d852014-12-20 18:06:08 +00001002 }
Damien429d7192013-10-04 19:53:11 +01001003
1004 default:
1005 assert(0);
1006 }
1007 }
Damien91d387d2013-10-09 15:09:52 +01001008
Damien George64f2b212015-10-08 14:58:15 +01001009 #if MICROPY_COMP_CONST
1010 mp_map_deinit(&parser.consts);
1011 #endif
1012
Damien George58e0f4a2015-09-23 10:50:43 +01001013 // truncate final chunk and link into chain of chunks
1014 if (parser.cur_chunk != NULL) {
1015 (void)m_renew(byte, parser.cur_chunk,
1016 sizeof(mp_parse_chunk_t) + parser.cur_chunk->alloc,
1017 sizeof(mp_parse_chunk_t) + parser.cur_chunk->union_.used);
1018 parser.cur_chunk->alloc = parser.cur_chunk->union_.used;
1019 parser.cur_chunk->union_.next = parser.tree.chunk;
1020 parser.tree.chunk = parser.cur_chunk;
1021 }
1022
Damien Georgef8048332015-02-08 13:40:20 +00001023 mp_obj_t exc;
Damien George58ba4c32014-04-10 14:27:31 +00001024
Damien George64f2b212015-10-08 14:58:15 +01001025 if (parser.parse_error) {
1026 #if MICROPY_COMP_CONST
1027 if (parser.parse_error == PARSE_ERROR_CONST) {
1028 exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
1029 "constant must be an integer");
1030 } else
1031 #endif
1032 {
1033 assert(parser.parse_error == PARSE_ERROR_MEMORY);
1034 memory_error:
1035 exc = mp_obj_new_exception_msg(&mp_type_MemoryError,
1036 "parser could not allocate enough memory");
1037 }
Damien George58e0f4a2015-09-23 10:50:43 +01001038 parser.tree.root = MP_PARSE_NODE_NULL;
Damien Georgefdfcee72015-10-12 12:59:18 +01001039 } else if (
1040 lex->tok_kind != MP_TOKEN_END // check we are at the end of the token stream
1041 || parser.result_stack_top == 0 // check that we got a node (can fail on empty input)
1042 ) {
1043 syntax_error:
1044 if (lex->tok_kind == MP_TOKEN_INDENT) {
1045 exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
1046 "unexpected indent");
1047 } else if (lex->tok_kind == MP_TOKEN_DEDENT_MISMATCH) {
1048 exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
1049 "unindent does not match any outer indentation level");
1050 } else {
1051 exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
1052 "invalid syntax");
1053 }
1054 parser.tree.root = MP_PARSE_NODE_NULL;
1055 } else {
1056 // no errors
1057
1058 //result_stack_show(parser);
1059 //printf("rule stack alloc: %d\n", parser.rule_stack_alloc);
1060 //printf("result stack alloc: %d\n", parser.result_stack_alloc);
1061 //printf("number of parse nodes allocated: %d\n", num_parse_nodes_allocated);
1062
1063 // get the root parse node that we created
1064 assert(parser.result_stack_top == 1);
1065 exc = MP_OBJ_NULL;
1066 parser.tree.root = parser.result_stack[0];
Damien George58ba4c32014-04-10 14:27:31 +00001067 }
1068
Damien George69a818d2014-01-12 13:55:24 +00001069 // free the memory that we don't need anymore
Damien George1b82e9a2014-05-10 17:36:41 +01001070 m_del(rule_stack_t, parser.rule_stack, parser.rule_stack_alloc);
1071 m_del(mp_parse_node_t, parser.result_stack, parser.result_stack_alloc);
Damien George0bfc7632015-02-07 18:33:58 +00001072 // we also free the lexer on behalf of the caller (see below)
Damien George69a818d2014-01-12 13:55:24 +00001073
Damien George0bfc7632015-02-07 18:33:58 +00001074 if (exc != MP_OBJ_NULL) {
1075 // had an error so raise the exception
1076 // add traceback to give info about file name and location
1077 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
1078 mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
1079 mp_lexer_free(lex);
1080 nlr_raise(exc);
1081 } else {
1082 mp_lexer_free(lex);
Damien George58e0f4a2015-09-23 10:50:43 +01001083 return parser.tree;
Damien George0bfc7632015-02-07 18:33:58 +00001084 }
Damien429d7192013-10-04 19:53:11 +01001085}
Damien George58e0f4a2015-09-23 10:50:43 +01001086
1087void mp_parse_tree_clear(mp_parse_tree_t *tree) {
1088 mp_parse_chunk_t *chunk = tree->chunk;
1089 while (chunk != NULL) {
1090 mp_parse_chunk_t *next = chunk->union_.next;
1091 m_del(byte, chunk, sizeof(mp_parse_chunk_t) + chunk->alloc);
1092 chunk = next;
1093 }
1094}
Damien Georgedd5353a2015-12-18 12:35:44 +00001095
1096#endif // MICROPY_ENABLE_COMPILER