blob: 6492f4d9e053359ff8e9ca4901d45ccf44e38314 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001struct _mp_lexer_t;
Damien429d7192013-10-04 19:53:11 +01002
Damiend99b0522013-12-21 18:17:45 +00003// a mp_parse_node_t is:
Damien429d7192013-10-04 19:53:11 +01004// - 0000...0000: no node
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02005// - xxxx...xxx1: a small integer; bits 1 and above are the signed value, 2's complement
6// - xxxx...xx00: pointer to mp_parse_node_struct_t
7// - xx...x00010: an identifier; bits 5 and above are the qstr
8// - xx...x00110: an integer; bits 5 and above are the qstr holding the value
9// - xx...x01010: a decimal; bits 5 and above are the qstr holding the value
10// - xx...x01110: a string; bits 5 and above are the qstr holding the value
11// - xx...x10010: a string with triple quotes; bits 5 and above are the qstr holding the value
12// - xx...x10110: a token; bits 5 and above are mp_token_kind_t
Damien429d7192013-10-04 19:53:11 +010013
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020014// TODO: these can now be unified with MP_OBJ_FITS_SMALL_INT(x)
15// makes sure the top 2 bits of x are all cleared (positive number) or all set (negavite number)
Damien0efb3a12013-10-12 16:16:56 +010016// these macros can probably go somewhere else because they are used more than just in the parser
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020017#define MP_UINT_HIGH_2_BITS (~((~((machine_uint_t)0)) >> 2))
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +020018// parser's small ints are different from VM small int
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020019#define MP_PARSE_FITS_SMALL_INT(x) (((((machine_uint_t)(x)) & MP_UINT_HIGH_2_BITS) == 0) || ((((machine_uint_t)(x)) & MP_UINT_HIGH_2_BITS) == MP_UINT_HIGH_2_BITS))
Damien0efb3a12013-10-12 16:16:56 +010020
Damiend99b0522013-12-21 18:17:45 +000021#define MP_PARSE_NODE_NULL (0)
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020022#define MP_PARSE_NODE_SMALL_INT (0x1)
23#define MP_PARSE_NODE_ID (0x02)
24#define MP_PARSE_NODE_INTEGER (0x06)
25#define MP_PARSE_NODE_DECIMAL (0x0a)
26#define MP_PARSE_NODE_STRING (0x0e)
27#define MP_PARSE_NODE_BYTES (0x12)
28#define MP_PARSE_NODE_TOKEN (0x16)
Damien429d7192013-10-04 19:53:11 +010029
Damiend99b0522013-12-21 18:17:45 +000030typedef machine_uint_t mp_parse_node_t; // must be pointer size
Damien429d7192013-10-04 19:53:11 +010031
Damiend99b0522013-12-21 18:17:45 +000032typedef struct _mp_parse_node_struct_t {
Damien George08335002014-01-18 23:24:36 +000033 uint32_t source_line; // line number in source file
Damien429d7192013-10-04 19:53:11 +010034 uint32_t kind_num_nodes; // parse node kind, and number of nodes
Damiend99b0522013-12-21 18:17:45 +000035 mp_parse_node_t nodes[]; // nodes
36} mp_parse_node_struct_t;
Damien429d7192013-10-04 19:53:11 +010037
Damiend99b0522013-12-21 18:17:45 +000038// macros for mp_parse_node_t usage
Damien429d7192013-10-04 19:53:11 +010039// some of these evaluate their argument more than once
40
Damiend99b0522013-12-21 18:17:45 +000041#define MP_PARSE_NODE_IS_NULL(pn) ((pn) == MP_PARSE_NODE_NULL)
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020042#define MP_PARSE_NODE_IS_LEAF(pn) ((pn) & 3)
43#define MP_PARSE_NODE_IS_STRUCT(pn) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 3) == 0)
44#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 +010045
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020046#define MP_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0x1) == MP_PARSE_NODE_SMALL_INT)
47#define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0x1f) == MP_PARSE_NODE_ID)
48#define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0x1f) == MP_PARSE_NODE_TOKEN)
49#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 5)))
Damien429d7192013-10-04 19:53:11 +010050
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020051#define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x1f)
Damien429d7192013-10-04 19:53:11 +010052// TODO should probably have int and uint versions of this macro
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020053#define MP_PARSE_NODE_LEAF_ARG(pn) (((machine_uint_t)(pn)) >> 5)
54#define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((machine_int_t)(pn)) >> 1)
Damiend99b0522013-12-21 18:17:45 +000055#define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
56#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
Damien429d7192013-10-04 19:53:11 +010057
Damiend99b0522013-12-21 18:17:45 +000058mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg);
Damien Georgeb829b5c2014-01-25 13:51:19 +000059uint mp_parse_node_free(mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +010060
Damien Georgecbd2f742014-01-19 11:48:48 +000061void mp_parse_node_print(mp_parse_node_t pn, int indent);
Damien429d7192013-10-04 19:53:11 +010062
Damien5ac1b2e2013-10-18 19:58:12 +010063typedef enum {
Damiend99b0522013-12-21 18:17:45 +000064 MP_PARSE_SINGLE_INPUT,
65 MP_PARSE_FILE_INPUT,
66 MP_PARSE_EVAL_INPUT,
67} mp_parse_input_kind_t;
Damien5ac1b2e2013-10-18 19:58:12 +010068
Damien Georgec5966122014-02-15 16:10:44 +000069typedef enum {
70 MP_PARSE_ERROR_UNEXPECTED_INDENT,
71 MP_PARSE_ERROR_UNMATCHED_UNINDENT,
72 MP_PARSE_ERROR_INVALID_SYNTAX,
73} mp_parse_error_kind_t;
74
75// returns MP_PARSE_NODE_NULL on error, and then parse_error_kind_out is valid
76mp_parse_node_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_parse_error_kind_t *parse_error_kind_out);