2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_PARSE_H
|
|
|
|
#define MICROPY_INCLUDED_PY_PARSE_H
|
2014-05-03 18:27:38 -04:00
|
|
|
|
2015-12-17 17:17:26 -05:00
|
|
|
#include <stddef.h>
|
2015-01-01 15:27:54 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-01-07 09:40:35 -05:00
|
|
|
#include "py/obj.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
|
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
|
2015-02-07 20:57:40 -05:00
|
|
|
// - xx...xx0010: an identifier; bits 4 and above are the qstr
|
|
|
|
// - xx...xx0110: a string; bits 4 and above are the qstr holding the value
|
2022-03-15 09:22:58 -04:00
|
|
|
// - xx...xx1010: a token; bits 4 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)
|
2015-02-07 20:57:40 -05:00
|
|
|
#define MP_PARSE_NODE_STRING (0x06)
|
2022-03-15 09:22:58 -04:00
|
|
|
#define MP_PARSE_NODE_TOKEN (0x0a)
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
typedef uintptr_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)
|
2020-02-26 23:36:53 -05:00
|
|
|
#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)
|
2015-02-07 20:57:40 -05:00
|
|
|
#define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0x0f) == MP_PARSE_NODE_ID)
|
|
|
|
#define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0x0f) == MP_PARSE_NODE_TOKEN)
|
|
|
|
#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 4)))
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2015-02-07 20:57:40 -05:00
|
|
|
#define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x0f)
|
2015-12-17 08:06:05 -05:00
|
|
|
#define MP_PARSE_NODE_LEAF_ARG(pn) (((uintptr_t)(pn)) >> 4)
|
2015-11-27 12:01:44 -05:00
|
|
|
#define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((mp_int_t)(intptr_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
|
|
|
|
2016-11-12 23:35:11 -05:00
|
|
|
static inline mp_parse_node_t mp_parse_node_new_small_int(mp_int_t val) {
|
|
|
|
return (mp_parse_node_t)(MP_PARSE_NODE_SMALL_INT | ((mp_uint_t)val << 1));
|
|
|
|
}
|
2022-03-30 23:26:07 -04:00
|
|
|
|
2016-11-12 23:35:11 -05:00
|
|
|
static inline mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
|
|
|
|
return (mp_parse_node_t)(kind | ((mp_uint_t)arg << 4));
|
|
|
|
}
|
2022-03-30 23:26:07 -04:00
|
|
|
|
|
|
|
static inline mp_obj_t mp_parse_node_extract_const_object(mp_parse_node_struct_t *pns) {
|
|
|
|
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
|
|
|
|
// nodes are 32-bit pointers, but need to extract 64-bit object
|
|
|
|
return (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
|
|
|
|
#else
|
|
|
|
return (mp_obj_t)pns->nodes[0];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-12 23:32:05 -05:00
|
|
|
bool mp_parse_node_is_const_false(mp_parse_node_t pn);
|
|
|
|
bool mp_parse_node_is_const_true(mp_parse_node_t pn);
|
2016-01-07 09:40:35 -05:00
|
|
|
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);
|
2020-05-04 08:11:44 -04:00
|
|
|
size_t mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
|
2020-09-11 09:00:03 -04:00
|
|
|
void mp_parse_node_print(const mp_print_t *print, mp_parse_node_t pn, size_t 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
|
|
|
|
2015-09-23 05:50:43 -04:00
|
|
|
typedef struct _mp_parse_t {
|
|
|
|
mp_parse_node_t root;
|
|
|
|
struct _mp_parse_chunk_t *chunk;
|
|
|
|
} mp_parse_tree_t;
|
|
|
|
|
2015-02-07 13:33:58 -05:00
|
|
|
// the parser will raise an exception if an error occurred
|
|
|
|
// the parser will free the lexer before it returns
|
2015-09-23 05:50:43 -04:00
|
|
|
mp_parse_tree_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind);
|
|
|
|
void mp_parse_tree_clear(mp_parse_tree_t *tree);
|
2014-12-25 16:29:19 -05:00
|
|
|
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#endif // MICROPY_INCLUDED_PY_PARSE_H
|