blob: 7326243ea442ae49c21f2d3afed8daeb7013f020 [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
5// - xxxx...0001: an identifier; bits 4 and above are the qstr
6// - xxxx...0011: a small integer; bits 4 and above are the signed value, 2's complement
7// - xxxx...0101: an integer; bits 4 and above are the qstr holding the value
8// - xxxx...0111: a decimal; bits 4 and above are the qstr holding the value
9// - xxxx...1001: a string; bits 4 and above are the qstr holding the value
10// - xxxx...1011: a string with triple quotes; bits 4 and above are the qstr holding the value
Damiend99b0522013-12-21 18:17:45 +000011// - xxxx...1101: a token; bits 4 and above are mp_token_kind_t
12// - xxxx...xxx0: pointer to mp_parse_node_struct_t
Damien429d7192013-10-04 19:53:11 +010013
Damien0efb3a12013-10-12 16:16:56 +010014// makes sure the top 5 bits of x are all cleared (positive number) or all set (negavite number)
15// these macros can probably go somewhere else because they are used more than just in the parser
Damiend99b0522013-12-21 18:17:45 +000016#define MP_UINT_HIGH_5_BITS (~((~((machine_uint_t)0)) >> 5))
17#define MP_FIT_SMALL_INT(x) (((((machine_uint_t)(x)) & MP_UINT_HIGH_5_BITS) == 0) || ((((machine_uint_t)(x)) & MP_UINT_HIGH_5_BITS) == MP_UINT_HIGH_5_BITS))
Damien0efb3a12013-10-12 16:16:56 +010018
Damiend99b0522013-12-21 18:17:45 +000019#define MP_PARSE_NODE_NULL (0)
20#define MP_PARSE_NODE_ID (0x1)
21#define MP_PARSE_NODE_SMALL_INT (0x3)
22#define MP_PARSE_NODE_INTEGER (0x5)
23#define MP_PARSE_NODE_DECIMAL (0x7)
24#define MP_PARSE_NODE_STRING (0x9)
25#define MP_PARSE_NODE_BYTES (0xb)
26#define MP_PARSE_NODE_TOKEN (0xd)
Damien429d7192013-10-04 19:53:11 +010027
Damiend99b0522013-12-21 18:17:45 +000028typedef machine_uint_t mp_parse_node_t; // must be pointer size
Damien429d7192013-10-04 19:53:11 +010029
Damiend99b0522013-12-21 18:17:45 +000030typedef struct _mp_parse_node_struct_t {
Damien429d7192013-10-04 19:53:11 +010031 uint32_t source; // file identifier, and line number
32 uint32_t kind_num_nodes; // parse node kind, and number of nodes
Damiend99b0522013-12-21 18:17:45 +000033 mp_parse_node_t nodes[]; // nodes
34} mp_parse_node_struct_t;
Damien429d7192013-10-04 19:53:11 +010035
Damiend99b0522013-12-21 18:17:45 +000036// macros for mp_parse_node_t usage
Damien429d7192013-10-04 19:53:11 +010037// some of these evaluate their argument more than once
38
Damiend99b0522013-12-21 18:17:45 +000039#define MP_PARSE_NODE_IS_NULL(pn) ((pn) == MP_PARSE_NODE_NULL)
40#define MP_PARSE_NODE_IS_LEAF(pn) ((pn) & 1)
41#define MP_PARSE_NODE_IS_STRUCT(pn) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 1) == 0)
42#define MP_PARSE_NODE_IS_STRUCT_KIND(pn, k) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 1) == 0 && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)(pn)) == (k))
Damien429d7192013-10-04 19:53:11 +010043
Damiend99b0522013-12-21 18:17:45 +000044#define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0xf) == MP_PARSE_NODE_ID)
45#define MP_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0xf) == MP_PARSE_NODE_SMALL_INT)
46#define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0xf) == MP_PARSE_NODE_TOKEN)
47#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | (k << 4)))
Damien429d7192013-10-04 19:53:11 +010048
Damiend99b0522013-12-21 18:17:45 +000049#define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0xf)
Damien429d7192013-10-04 19:53:11 +010050// TODO should probably have int and uint versions of this macro
Damiend99b0522013-12-21 18:17:45 +000051#define MP_PARSE_NODE_LEAF_ARG(pn) (((machine_int_t)(pn)) >> 4)
52#define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
53#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
Damien429d7192013-10-04 19:53:11 +010054
Damiend99b0522013-12-21 18:17:45 +000055mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg);
Damien429d7192013-10-04 19:53:11 +010056
Damiend99b0522013-12-21 18:17:45 +000057void mp_parse_node_show(mp_parse_node_t pn, int indent);
Damien429d7192013-10-04 19:53:11 +010058
Damien5ac1b2e2013-10-18 19:58:12 +010059typedef enum {
Damiend99b0522013-12-21 18:17:45 +000060 MP_PARSE_SINGLE_INPUT,
61 MP_PARSE_FILE_INPUT,
62 MP_PARSE_EVAL_INPUT,
63} mp_parse_input_kind_t;
Damien5ac1b2e2013-10-18 19:58:12 +010064
Damiend99b0522013-12-21 18:17:45 +000065mp_parse_node_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind);