Numworks Epsilon  1.4.1
Graphing Calculator Operating System
parse.h
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 #ifndef MICROPY_INCLUDED_PY_PARSE_H
27 #define MICROPY_INCLUDED_PY_PARSE_H
28 
29 #include <stddef.h>
30 #include <stdint.h>
31 
32 #include "py/obj.h"
33 
34 struct _mp_lexer_t;
35 
36 // a mp_parse_node_t is:
37 // - 0000...0000: no node
38 // - 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
40 // - 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
44 
45 #define MP_PARSE_NODE_NULL (0)
46 #define MP_PARSE_NODE_SMALL_INT (0x1)
47 #define MP_PARSE_NODE_ID (0x02)
48 #define MP_PARSE_NODE_STRING (0x06)
49 #define MP_PARSE_NODE_BYTES (0x0a)
50 #define MP_PARSE_NODE_TOKEN (0x0e)
51 
52 typedef uintptr_t mp_parse_node_t; // must be pointer size
53 
54 typedef struct _mp_parse_node_struct_t {
55  uint32_t source_line; // line number in source file
56  uint32_t kind_num_nodes; // parse node kind, and number of nodes
57  mp_parse_node_t nodes[]; // nodes
59 
60 // macros for mp_parse_node_t usage
61 // some of these evaluate their argument more than once
62 
63 #define MP_PARSE_NODE_IS_NULL(pn) ((pn) == MP_PARSE_NODE_NULL)
64 #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))
67 
68 #define MP_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0x1) == MP_PARSE_NODE_SMALL_INT)
69 #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)))
72 
73 #define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x0f)
74 #define MP_PARSE_NODE_LEAF_ARG(pn) (((uintptr_t)(pn)) >> 4)
75 #define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((mp_int_t)(intptr_t)(pn)) >> 1)
76 #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)
78 
79 static 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 }
82 static 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 }
88 int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
89 void mp_parse_node_print(mp_parse_node_t pn, size_t indent);
90 
91 typedef enum {
96 
97 typedef struct _mp_parse_t {
99  struct _mp_parse_chunk_t *chunk;
101 
102 // the parser will raise an exception if an error occurred
103 // the parser will free the lexer before it returns
106 
107 #endif // MICROPY_INCLUDED_PY_PARSE_H
bool mp_parse_node_is_const_true(mp_parse_node_t pn)
intptr_t mp_int_t
Definition: mpconfigport.h:73
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
bool mp_parse_node_is_const_false(mp_parse_node_t pn)
mp_parse_node_t root
Definition: parse.h:98
mp_parse_node_t nodes[]
Definition: parse.h:57
unsigned int uintptr_t
Definition: stdint.h:14
#define MP_PARSE_NODE_SMALL_INT
Definition: parse.h:46
uintptr_t mp_parse_node_t
Definition: parse.h:52
uint32_t source_line
Definition: parse.h:55
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o)
mp_parse_input_kind_t
Definition: parse.h:91
struct _mp_parse_node_struct_t mp_parse_node_struct_t
void mp_parse_node_print(mp_parse_node_t pn, size_t indent)
int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes)
unsigned int uint32_t
Definition: stdint.h:6
struct _mp_parse_chunk_t * chunk
Definition: parse.h:99
struct _mp_parse_t mp_parse_tree_t
uint32_t kind_num_nodes
Definition: parse.h:56
mp_parse_tree_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind)
uint64_t mp_obj_t
Definition: obj.h:39
void mp_parse_tree_clear(mp_parse_tree_t *tree)