2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
struct _mp_lexer_t;
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
// a mp_parse_node_t is:
|
2013-10-04 14:53:11 -04:00
|
|
|
// - 0000...0000: no node
|
2014-02-22 09:39:45 -05:00
|
|
|
// - xxxx...xxx1: a small integer; bits 1 and above are the signed value, 2's complement
|
|
|
|
// - xxxx...xx00: pointer to mp_parse_node_struct_t
|
|
|
|
// - xx...x00010: an identifier; bits 5 and above are the qstr
|
|
|
|
// - xx...x00110: an integer; bits 5 and above are the qstr holding the value
|
|
|
|
// - xx...x01010: a decimal; bits 5 and above are the qstr holding the value
|
|
|
|
// - xx...x01110: a string; bits 5 and above are the qstr holding the value
|
2014-02-22 13:12:43 -05:00
|
|
|
// - xx...x10010: a string of bytes; bits 5 and above are the qstr holding the value
|
2014-02-22 09:39:45 -05:00
|
|
|
// - xx...x10110: a token; bits 5 and above are mp_token_kind_t
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
#define MP_PARSE_NODE_NULL (0)
|
2014-02-22 09:39:45 -05:00
|
|
|
#define MP_PARSE_NODE_SMALL_INT (0x1)
|
|
|
|
#define MP_PARSE_NODE_ID (0x02)
|
|
|
|
#define MP_PARSE_NODE_INTEGER (0x06)
|
|
|
|
#define MP_PARSE_NODE_DECIMAL (0x0a)
|
|
|
|
#define MP_PARSE_NODE_STRING (0x0e)
|
|
|
|
#define MP_PARSE_NODE_BYTES (0x12)
|
|
|
|
#define MP_PARSE_NODE_TOKEN (0x16)
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
typedef machine_uint_t mp_parse_node_t; // must be pointer size
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
typedef struct _mp_parse_node_struct_t {
|
2014-01-18 18:24:36 -05:00
|
|
|
uint32_t source_line; // line number in source file
|
2013-10-04 14:53:11 -04:00
|
|
|
uint32_t kind_num_nodes; // parse node kind, and number of nodes
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_parse_node_t nodes[]; // nodes
|
|
|
|
} mp_parse_node_struct_t;
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
// macros for mp_parse_node_t usage
|
2013-10-04 14:53:11 -04:00
|
|
|
// some of these evaluate their argument more than once
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
#define MP_PARSE_NODE_IS_NULL(pn) ((pn) == MP_PARSE_NODE_NULL)
|
2014-02-22 09:39:45 -05:00
|
|
|
#define MP_PARSE_NODE_IS_LEAF(pn) ((pn) & 3)
|
|
|
|
#define MP_PARSE_NODE_IS_STRUCT(pn) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 3) == 0)
|
|
|
|
#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))
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-02-22 09:39:45 -05:00
|
|
|
#define MP_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0x1) == MP_PARSE_NODE_SMALL_INT)
|
|
|
|
#define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0x1f) == MP_PARSE_NODE_ID)
|
|
|
|
#define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0x1f) == MP_PARSE_NODE_TOKEN)
|
|
|
|
#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 5)))
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-02-22 09:39:45 -05:00
|
|
|
#define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x1f)
|
2013-10-04 14:53:11 -04:00
|
|
|
// TODO should probably have int and uint versions of this macro
|
2014-02-22 09:39:45 -05:00
|
|
|
#define MP_PARSE_NODE_LEAF_ARG(pn) (((machine_uint_t)(pn)) >> 5)
|
|
|
|
#define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((machine_int_t)(pn)) >> 1)
|
2013-12-21 13:17:45 -05:00
|
|
|
#define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
|
|
|
|
#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg);
|
2014-05-25 17:06:06 -04:00
|
|
|
void mp_parse_node_free(mp_parse_node_t pn);
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-01-19 06:48:48 -05:00
|
|
|
void mp_parse_node_print(mp_parse_node_t pn, int indent);
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-10-18 14:58:12 -04:00
|
|
|
typedef enum {
|
2013-12-21 13:17:45 -05:00
|
|
|
MP_PARSE_SINGLE_INPUT,
|
|
|
|
MP_PARSE_FILE_INPUT,
|
|
|
|
MP_PARSE_EVAL_INPUT,
|
|
|
|
} mp_parse_input_kind_t;
|
2013-10-18 14:58:12 -04:00
|
|
|
|
2014-02-15 11:10:44 -05:00
|
|
|
typedef enum {
|
2014-04-10 10:27:31 -04:00
|
|
|
MP_PARSE_ERROR_MEMORY,
|
2014-02-15 11:10:44 -05:00
|
|
|
MP_PARSE_ERROR_UNEXPECTED_INDENT,
|
|
|
|
MP_PARSE_ERROR_UNMATCHED_UNINDENT,
|
|
|
|
MP_PARSE_ERROR_INVALID_SYNTAX,
|
|
|
|
} mp_parse_error_kind_t;
|
|
|
|
|
|
|
|
// returns MP_PARSE_NODE_NULL on error, and then parse_error_kind_out is valid
|
|
|
|
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);
|