Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 26 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 27 | struct _mp_lexer_t; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 28 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 29 | // a mp_parse_node_t is: |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 30 | // - 0000...0000: no node |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 31 | // - xxxx...xxx1: a small integer; bits 1 and above are the signed value, 2's complement |
| 32 | // - xxxx...xx00: pointer to mp_parse_node_struct_t |
| 33 | // - xx...x00010: an identifier; bits 5 and above are the qstr |
| 34 | // - xx...x00110: an integer; bits 5 and above are the qstr holding the value |
| 35 | // - xx...x01010: a decimal; bits 5 and above are the qstr holding the value |
| 36 | // - xx...x01110: a string; bits 5 and above are the qstr holding the value |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 37 | // - xx...x10010: a string of bytes; bits 5 and above are the qstr holding the value |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 38 | // - xx...x10110: a token; bits 5 and above are mp_token_kind_t |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 39 | |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 40 | // TODO: these can now be unified with MP_OBJ_FITS_SMALL_INT(x) |
| 41 | // makes sure the top 2 bits of x are all cleared (positive number) or all set (negavite number) |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 42 | // these macros can probably go somewhere else because they are used more than just in the parser |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 43 | #define MP_UINT_HIGH_2_BITS (~((~((machine_uint_t)0)) >> 2)) |
Paul Sokolovsky | bbf0e2f | 2014-02-21 02:04:32 +0200 | [diff] [blame] | 44 | // parser's small ints are different from VM small int |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 45 | #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)) |
Damien | 0efb3a1 | 2013-10-12 16:16:56 +0100 | [diff] [blame] | 46 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 47 | #define MP_PARSE_NODE_NULL (0) |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 48 | #define MP_PARSE_NODE_SMALL_INT (0x1) |
| 49 | #define MP_PARSE_NODE_ID (0x02) |
| 50 | #define MP_PARSE_NODE_INTEGER (0x06) |
| 51 | #define MP_PARSE_NODE_DECIMAL (0x0a) |
| 52 | #define MP_PARSE_NODE_STRING (0x0e) |
| 53 | #define MP_PARSE_NODE_BYTES (0x12) |
| 54 | #define MP_PARSE_NODE_TOKEN (0x16) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 55 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 56 | typedef machine_uint_t mp_parse_node_t; // must be pointer size |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 57 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 58 | typedef struct _mp_parse_node_struct_t { |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 59 | uint32_t source_line; // line number in source file |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 60 | uint32_t kind_num_nodes; // parse node kind, and number of nodes |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 61 | mp_parse_node_t nodes[]; // nodes |
| 62 | } mp_parse_node_struct_t; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 63 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 64 | // macros for mp_parse_node_t usage |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 65 | // some of these evaluate their argument more than once |
| 66 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 67 | #define MP_PARSE_NODE_IS_NULL(pn) ((pn) == MP_PARSE_NODE_NULL) |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 68 | #define MP_PARSE_NODE_IS_LEAF(pn) ((pn) & 3) |
| 69 | #define MP_PARSE_NODE_IS_STRUCT(pn) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 3) == 0) |
| 70 | #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)) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 71 | |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 72 | #define MP_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0x1) == MP_PARSE_NODE_SMALL_INT) |
| 73 | #define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0x1f) == MP_PARSE_NODE_ID) |
| 74 | #define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0x1f) == MP_PARSE_NODE_TOKEN) |
| 75 | #define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 5))) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 76 | |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 77 | #define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x1f) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 78 | // TODO should probably have int and uint versions of this macro |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 79 | #define MP_PARSE_NODE_LEAF_ARG(pn) (((machine_uint_t)(pn)) >> 5) |
| 80 | #define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((machine_int_t)(pn)) >> 1) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 81 | #define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff) |
| 82 | #define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 83 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 84 | mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg); |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame^] | 85 | void mp_parse_node_free(mp_parse_node_t pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 86 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 87 | void mp_parse_node_print(mp_parse_node_t pn, int indent); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 88 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 89 | typedef enum { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 90 | MP_PARSE_SINGLE_INPUT, |
| 91 | MP_PARSE_FILE_INPUT, |
| 92 | MP_PARSE_EVAL_INPUT, |
| 93 | } mp_parse_input_kind_t; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 94 | |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 95 | typedef enum { |
Damien George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 96 | MP_PARSE_ERROR_MEMORY, |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 97 | MP_PARSE_ERROR_UNEXPECTED_INDENT, |
| 98 | MP_PARSE_ERROR_UNMATCHED_UNINDENT, |
| 99 | MP_PARSE_ERROR_INVALID_SYNTAX, |
| 100 | } mp_parse_error_kind_t; |
| 101 | |
| 102 | // returns MP_PARSE_NODE_NULL on error, and then parse_error_kind_out is valid |
| 103 | mp_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); |