Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | struct _py_lexer_t; |
| 2 | |
| 3 | // a py_parse_node_t is: |
| 4 | // - 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 |
| 11 | // - xxxx...1101: a token; bits 4 and above are py_token_kind_t |
| 12 | // - xxxx...xxx0: pointer to py_parse_node_struct_t |
| 13 | |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 14 | // 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 |
| 16 | #define PY_UINT_HIGH_5_BITS (~((~((machine_uint_t)0)) >> 5)) |
| 17 | #define PY_FIT_SMALL_INT(x) (((((machine_uint_t)(x)) & PY_UINT_HIGH_5_BITS) == 0) || ((((machine_uint_t)(x)) & PY_UINT_HIGH_5_BITS) == PY_UINT_HIGH_5_BITS)) |
| 18 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 19 | #define PY_PARSE_NODE_NULL (0) |
| 20 | #define PY_PARSE_NODE_ID (0x1) |
| 21 | #define PY_PARSE_NODE_SMALL_INT (0x3) |
| 22 | #define PY_PARSE_NODE_INTEGER (0x5) |
| 23 | #define PY_PARSE_NODE_DECIMAL (0x7) |
| 24 | #define PY_PARSE_NODE_STRING (0x9) |
| 25 | #define PY_PARSE_NODE_BYTES (0xb) |
| 26 | #define PY_PARSE_NODE_TOKEN (0xd) |
| 27 | |
| 28 | typedef machine_uint_t py_parse_node_t; // must be pointer size |
| 29 | |
| 30 | typedef struct _py_parse_node_struct_t { |
| 31 | uint32_t source; // file identifier, and line number |
| 32 | uint32_t kind_num_nodes; // parse node kind, and number of nodes |
| 33 | py_parse_node_t nodes[]; // nodes |
| 34 | } py_parse_node_struct_t; |
| 35 | |
| 36 | // macros for py_parse_node_t usage |
| 37 | // some of these evaluate their argument more than once |
| 38 | |
| 39 | #define PY_PARSE_NODE_IS_NULL(pn) ((pn) == PY_PARSE_NODE_NULL) |
| 40 | #define PY_PARSE_NODE_IS_LEAF(pn) ((pn) & 1) |
| 41 | #define PY_PARSE_NODE_IS_STRUCT(pn) ((pn) != PY_PARSE_NODE_NULL && ((pn) & 1) == 0) |
| 42 | #define PY_PARSE_NODE_IS_STRUCT_KIND(pn, k) ((pn) != PY_PARSE_NODE_NULL && ((pn) & 1) == 0 && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)(pn)) == (k)) |
| 43 | |
| 44 | #define PY_PARSE_NODE_IS_ID(pn) (((pn) & 0xf) == PY_PARSE_NODE_ID) |
| 45 | #define PY_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0xf) == PY_PARSE_NODE_SMALL_INT) |
| 46 | #define PY_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0xf) == PY_PARSE_NODE_TOKEN) |
| 47 | #define PY_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (PY_PARSE_NODE_TOKEN | (k << 4))) |
| 48 | |
| 49 | #define PY_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0xf) |
| 50 | // TODO should probably have int and uint versions of this macro |
| 51 | #define PY_PARSE_NODE_LEAF_ARG(pn) (((machine_int_t)(pn)) >> 4) |
| 52 | #define PY_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff) |
| 53 | #define PY_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8) |
| 54 | |
| 55 | py_parse_node_t py_parse_node_new_leaf(machine_int_t kind, machine_int_t arg); |
| 56 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 57 | void py_parse_node_show(py_parse_node_t pn, int indent); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 58 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 59 | typedef enum { |
| 60 | PY_PARSE_SINGLE_INPUT, |
| 61 | PY_PARSE_FILE_INPUT, |
| 62 | PY_PARSE_EVAL_INPUT, |
| 63 | } py_parse_input_kind_t; |
| 64 | |
| 65 | py_parse_node_t py_parse(struct _py_lexer_t *lex, py_parse_input_kind_t input_kind); |