blob: 769f5a8757e8f3aa7ba89097c546252e7e0b99d3 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
Paul Sokolovsky8ab6f902014-12-25 23:29:19 +020026#ifndef __MICROPY_INCLUDED_PY_PARSE_H__
27#define __MICROPY_INCLUDED_PY_PARSE_H__
Damien George04b91472014-05-03 23:27:38 +010028
Damien George2fe7e6b2015-12-17 22:17:26 +000029#include <stddef.h>
Damien George51dfcb42015-01-01 20:27:54 +000030#include <stdint.h>
31
Damien George22b22652016-01-07 14:40:35 +000032#include "py/obj.h"
Damien George51dfcb42015-01-01 20:27:54 +000033
Damiend99b0522013-12-21 18:17:45 +000034struct _mp_lexer_t;
Damien429d7192013-10-04 19:53:11 +010035
Damiend99b0522013-12-21 18:17:45 +000036// a mp_parse_node_t is:
Damien429d7192013-10-04 19:53:11 +010037// - 0000...0000: no node
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020038// - xxxx...xxx1: a small integer; bits 1 and above are the signed value, 2's complement
39// - xxxx...xx00: pointer to mp_parse_node_struct_t
Damien George7d414a12015-02-08 01:57:40 +000040// - xx...xx0010: an identifier; bits 4 and above are the qstr
41// - xx...xx0110: a string; bits 4 and above are the qstr holding the value
42// - xx...xx1010: a string of bytes; bits 4 and above are the qstr holding the value
43// - xx...xx1110: a token; bits 4 and above are mp_token_kind_t
Damien429d7192013-10-04 19:53:11 +010044
Damiend99b0522013-12-21 18:17:45 +000045#define MP_PARSE_NODE_NULL (0)
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020046#define MP_PARSE_NODE_SMALL_INT (0x1)
47#define MP_PARSE_NODE_ID (0x02)
Damien George7d414a12015-02-08 01:57:40 +000048#define MP_PARSE_NODE_STRING (0x06)
49#define MP_PARSE_NODE_BYTES (0x0a)
50#define MP_PARSE_NODE_TOKEN (0x0e)
Damien429d7192013-10-04 19:53:11 +010051
Damien George999cedb2015-11-27 17:01:44 +000052typedef uintptr_t mp_parse_node_t; // must be pointer size
Damien429d7192013-10-04 19:53:11 +010053
Damiend99b0522013-12-21 18:17:45 +000054typedef struct _mp_parse_node_struct_t {
Damien George08335002014-01-18 23:24:36 +000055 uint32_t source_line; // line number in source file
Damien429d7192013-10-04 19:53:11 +010056 uint32_t kind_num_nodes; // parse node kind, and number of nodes
Damiend99b0522013-12-21 18:17:45 +000057 mp_parse_node_t nodes[]; // nodes
58} mp_parse_node_struct_t;
Damien429d7192013-10-04 19:53:11 +010059
Damiend99b0522013-12-21 18:17:45 +000060// macros for mp_parse_node_t usage
Damien429d7192013-10-04 19:53:11 +010061// some of these evaluate their argument more than once
62
Damiend99b0522013-12-21 18:17:45 +000063#define MP_PARSE_NODE_IS_NULL(pn) ((pn) == MP_PARSE_NODE_NULL)
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020064#define MP_PARSE_NODE_IS_LEAF(pn) ((pn) & 3)
65#define MP_PARSE_NODE_IS_STRUCT(pn) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 3) == 0)
66#define MP_PARSE_NODE_IS_STRUCT_KIND(pn, k) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 3) == 0 && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)(pn)) == (k))
Damien429d7192013-10-04 19:53:11 +010067
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020068#define MP_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0x1) == MP_PARSE_NODE_SMALL_INT)
Damien George7d414a12015-02-08 01:57:40 +000069#define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0x0f) == MP_PARSE_NODE_ID)
70#define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0x0f) == MP_PARSE_NODE_TOKEN)
71#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 4)))
Damien429d7192013-10-04 19:53:11 +010072
Damien George7d414a12015-02-08 01:57:40 +000073#define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x0f)
Damien George16a6a472015-12-17 13:06:05 +000074#define MP_PARSE_NODE_LEAF_ARG(pn) (((uintptr_t)(pn)) >> 4)
Damien George999cedb2015-11-27 17:01:44 +000075#define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((mp_int_t)(intptr_t)(pn)) >> 1)
Damiend99b0522013-12-21 18:17:45 +000076#define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
77#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
Damien429d7192013-10-04 19:53:11 +010078
Damien Georgeed9c93f2016-11-13 15:35:11 +110079static inline mp_parse_node_t mp_parse_node_new_small_int(mp_int_t val) {
80 return (mp_parse_node_t)(MP_PARSE_NODE_SMALL_INT | ((mp_uint_t)val << 1));
81}
82static inline mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
83 return (mp_parse_node_t)(kind | ((mp_uint_t)arg << 4));
84}
Damien Georgeb0cbfb02016-11-13 15:32:05 +110085bool mp_parse_node_is_const_false(mp_parse_node_t pn);
86bool mp_parse_node_is_const_true(mp_parse_node_t pn);
Damien George22b22652016-01-07 14:40:35 +000087bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);
Damien George16a6a472015-12-17 13:06:05 +000088int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
89void mp_parse_node_print(mp_parse_node_t pn, size_t indent);
Damien429d7192013-10-04 19:53:11 +010090
Damien5ac1b2e2013-10-18 19:58:12 +010091typedef enum {
Damiend99b0522013-12-21 18:17:45 +000092 MP_PARSE_SINGLE_INPUT,
93 MP_PARSE_FILE_INPUT,
94 MP_PARSE_EVAL_INPUT,
95} mp_parse_input_kind_t;
Damien5ac1b2e2013-10-18 19:58:12 +010096
Damien George58e0f4a2015-09-23 10:50:43 +010097typedef struct _mp_parse_t {
98 mp_parse_node_t root;
99 struct _mp_parse_chunk_t *chunk;
100} mp_parse_tree_t;
101
Damien George0bfc7632015-02-07 18:33:58 +0000102// the parser will raise an exception if an error occurred
103// the parser will free the lexer before it returns
Damien George58e0f4a2015-09-23 10:50:43 +0100104mp_parse_tree_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind);
105void mp_parse_tree_clear(mp_parse_tree_t *tree);
Paul Sokolovsky8ab6f902014-12-25 23:29:19 +0200106
107#endif // __MICROPY_INCLUDED_PY_PARSE_H__