42 #if MICROPY_ENABLE_COMPILER 44 #define RULE_ACT_ARG_MASK (0x0f) 45 #define RULE_ACT_KIND_MASK (0x30) 46 #define RULE_ACT_ALLOW_IDENT (0x40) 47 #define RULE_ACT_ADD_BLANK (0x80) 48 #define RULE_ACT_OR (0x10) 49 #define RULE_ACT_AND (0x20) 50 #define RULE_ACT_LIST (0x30) 52 #define RULE_ARG_KIND_MASK (0xf000) 53 #define RULE_ARG_ARG_MASK (0x0fff) 54 #define RULE_ARG_TOK (0x1000) 55 #define RULE_ARG_RULE (0x2000) 56 #define RULE_ARG_OPT_RULE (0x3000) 61 typedef struct _rule_t {
65 const char *rule_name;
72 #define DEF_RULE(rule, comp, kind, ...) RULE_##rule, 73 #define DEF_RULE_NC(rule, kind, ...) 80 #define DEF_RULE(rule, comp, kind, ...) 81 #define DEF_RULE_NC(rule, kind, ...) RULE_##rule, 87 #define or(n) (RULE_ACT_OR | n) 88 #define and(n) (RULE_ACT_AND | n) 89 #define and_ident(n) (RULE_ACT_AND | n | RULE_ACT_ALLOW_IDENT) 90 #define and_blank(n) (RULE_ACT_AND | n | RULE_ACT_ADD_BLANK) 91 #define one_or_more (RULE_ACT_LIST | 2) 92 #define list (RULE_ACT_LIST | 1) 93 #define list_with_end (RULE_ACT_LIST | 3) 94 #define tok(t) (RULE_ARG_TOK | MP_TOKEN_##t) 95 #define rule(r) (RULE_ARG_RULE | RULE_##r) 96 #define opt_rule(r) (RULE_ARG_OPT_RULE | RULE_##r) 98 #define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, #rule, { __VA_ARGS__ } }; 99 #define DEF_RULE_NC(rule, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, #rule, { __VA_ARGS__ } }; 101 #define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } }; 102 #define DEF_RULE_NC(rule, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } }; 116 STATIC const rule_t *
const rules[] = {
118 #define DEF_RULE(rule, comp, kind, ...) &rule_##rule, 119 #define DEF_RULE_NC(rule, kind, ...) 126 #define DEF_RULE(rule, comp, kind, ...) 127 #define DEF_RULE_NC(rule, kind, ...) &rule_##rule, 133 typedef struct _rule_stack_t {
134 size_t src_line : 8 *
sizeof(
size_t) - 8;
139 typedef struct _mp_parse_chunk_t {
143 struct _mp_parse_chunk_t *next;
148 typedef struct _parser_t {
149 size_t rule_stack_alloc;
150 size_t rule_stack_top;
151 rule_stack_t *rule_stack;
153 size_t result_stack_alloc;
154 size_t result_stack_top;
160 mp_parse_chunk_t *cur_chunk;
162 #if MICROPY_COMP_CONST 167 STATIC void *parser_alloc(parser_t *
parser,
size_t num_bytes) {
170 mp_parse_chunk_t *chunk =
parser->cur_chunk;
172 if (chunk !=
NULL && chunk->union_.used + num_bytes > chunk->alloc) {
175 sizeof(mp_parse_chunk_t) + chunk->alloc,
176 sizeof(mp_parse_chunk_t) + chunk->alloc + num_bytes,
false);
177 if (new_data ==
NULL) {
180 sizeof(mp_parse_chunk_t) + chunk->union_.used,
false);
181 chunk->alloc = chunk->union_.used;
182 chunk->union_.next =
parser->tree.chunk;
183 parser->tree.chunk = chunk;
187 chunk->alloc += num_bytes;
194 if (alloc < num_bytes) {
197 chunk = (mp_parse_chunk_t*)
m_new(
byte,
sizeof(mp_parse_chunk_t) + alloc);
198 chunk->alloc = alloc;
199 chunk->union_.used = 0;
200 parser->cur_chunk = chunk;
203 byte *ret = chunk->data + chunk->union_.used;
204 chunk->union_.used += num_bytes;
208 STATIC void push_rule(parser_t *
parser,
size_t src_line,
const rule_t *
rule,
size_t arg_i) {
209 if (
parser->rule_stack_top >=
parser->rule_stack_alloc) {
214 rule_stack_t *rs = &
parser->rule_stack[
parser->rule_stack_top++];
215 rs->src_line = src_line;
216 rs->rule_id =
rule->rule_id;
220 STATIC void push_rule_from_arg(parser_t *
parser,
size_t arg) {
221 assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE || (arg & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE);
222 size_t rule_id = arg & RULE_ARG_ARG_MASK;
223 push_rule(
parser,
parser->lexer->tok_line, rules[rule_id], 0);
226 STATIC void pop_rule(parser_t *
parser,
const rule_t **
rule,
size_t *arg_i,
size_t *src_line) {
227 parser->rule_stack_top -= 1;
229 *arg_i =
parser->rule_stack[
parser->rule_stack_top].arg_i;
230 *src_line =
parser->rule_stack[
parser->rule_stack_top].src_line;
249 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D 280 #if MICROPY_DEBUG_PRINTERS 287 for (
size_t i = 0; i < indent; i++) {
294 printf(
"int(" INT_FMT ")\n", arg);
303 printf(
"tok(%u)\n", (
uint)arg);
break;
309 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D 321 for (
size_t i = 0; i < n; i++) {
327 #endif // MICROPY_DEBUG_PRINTERS 340 return parser->result_stack[--
parser->result_stack_top];
345 return parser->result_stack[
parser->result_stack_top - 1 - pos];
349 if (
parser->result_stack_top >=
parser->result_stack_alloc) {
351 parser->result_stack = stack;
360 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D 377 #if MICROPY_COMP_CONST 380 if (
rule->rule_id == RULE_atom
382 if (MP_OBJ_IS_SMALL_INT(elem->
value)) {
396 if (MP_OBJ_IS_SMALL_INT(o)) {
407 qstr qst = MP_QSTR_NULL;
415 if (qst != MP_QSTR_NULL) {
428 push_result_node(
parser, pn);
431 #if MICROPY_COMP_MODULE_CONST 433 #if MICROPY_PY_UERRNO 436 #if MICROPY_PY_UCTYPES 445 STATIC void push_result_rule(parser_t *
parser,
size_t src_line,
const rule_t *
rule,
size_t num_args);
447 #if MICROPY_COMP_CONST_FOLDING 448 STATIC bool fold_logical_constants(parser_t *
parser,
const rule_t *
rule,
size_t *num_args) {
449 if (
rule->rule_id == RULE_or_test
450 ||
rule->rule_id == RULE_and_test) {
452 size_t copy_to = *num_args;
453 for (
size_t i = copy_to; i > 0;) {
455 parser->result_stack[
parser->result_stack_top - copy_to] = pn;
460 if (
rule->rule_id == RULE_or_test) {
479 for (
size_t i = 0; i < copy_to; ++i) {
482 *num_args -= copy_to;
485 return *num_args == 1;
487 }
else if (
rule->rule_id == RULE_not_test_2) {
498 push_result_node(
parser, pn);
505 STATIC bool fold_constants(parser_t *
parser,
const rule_t *
rule,
size_t num_args) {
510 if (
rule->rule_id == RULE_expr
511 ||
rule->rule_id == RULE_xor_expr
512 ||
rule->rule_id == RULE_and_expr) {
519 if (
rule->rule_id == RULE_expr) {
521 }
else if (
rule->rule_id == RULE_xor_expr) {
526 for (
ssize_t i = num_args - 2; i >= 0; --i) {
527 pn = peek_result(
parser, i);
534 }
else if (
rule->rule_id == RULE_shift_expr
535 ||
rule->rule_id == RULE_arith_expr
536 ||
rule->rule_id == RULE_term) {
542 for (
ssize_t i = num_args - 2; i >= 1; i -= 2) {
543 pn = peek_result(
parser, i - 1);
549 static const uint8_t token_to_op[] = {
580 }
else if (
rule->rule_id == RULE_factor_2) {
598 #if MICROPY_COMP_CONST 599 }
else if (
rule->rule_id == RULE_expr_stmt) {
622 "constant must be an integer");
638 push_result_rule(
parser, 0, rules[RULE_pass_stmt], 0);
644 push_result_node(
parser, pn_value);
653 #if MICROPY_COMP_MODULE_CONST 654 }
else if (
rule->rule_id == RULE_atom_expr_normal) {
685 for (
size_t i = num_args; i > 0; i--) {
688 if (MP_OBJ_IS_SMALL_INT(arg0)) {
692 push_result_node(
parser, make_node_const_object(
parser, 0, arg0));
699 STATIC void push_result_rule(parser_t *
parser,
size_t src_line,
const rule_t *
rule,
size_t num_args) {
701 if (
rule->rule_id == RULE_atom_paren) {
714 #if MICROPY_COMP_CONST_FOLDING 715 if (fold_logical_constants(
parser,
rule, &num_args)) {
728 for (
size_t i = num_args; i > 0; i--) {
741 parser.rule_stack_top = 0;
745 parser.result_stack_top = 0;
753 #if MICROPY_COMP_CONST 758 size_t top_level_rule;
759 switch (input_kind) {
762 default: top_level_rule = RULE_file_input;
769 size_t rule_src_line;
770 bool backtrack =
false;
775 if (
parser.rule_stack_top == 0) {
780 n =
rule->act & RULE_ACT_ARG_MASK;
791 switch (
rule->act & RULE_ACT_KIND_MASK) {
793 if (i > 0 && !backtrack) {
800 if (kind == RULE_ARG_TOK) {
801 if (lex->
tok_kind == (
rule->arg[i] & RULE_ARG_ARG_MASK)) {
807 assert(kind == RULE_ARG_RULE);
809 push_rule(&
parser, rule_src_line,
rule, i + 1);
823 if ((
rule->arg[i - 1] & RULE_ARG_KIND_MASK) == RULE_ARG_OPT_RULE) {
840 if ((
rule->arg[i] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
861 push_rule(&
parser, rule_src_line,
rule, i + 1);
871 #if !MICROPY_ENABLE_DOC_STRING 882 push_result_rule(&
parser, rule_src_line, rules[RULE_pass_stmt], 0);
890 size_t num_not_nil = 0;
891 for (
size_t x = n; x > 0;) {
893 if ((
rule->arg[x] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
909 if (num_not_nil == 1 && (
rule->act & RULE_ACT_ALLOW_IDENT)) {
912 for (
size_t x = 0; x < i; ++x) {
918 push_result_node(&
parser, pn);
922 if (
rule->act & RULE_ACT_ADD_BLANK) {
928 push_result_rule(&
parser, rule_src_line,
rule, i);
934 assert((
rule->act & RULE_ACT_KIND_MASK) == RULE_ACT_LIST);
939 bool had_trailing_sep;
942 had_trailing_sep =
false;
955 }
else if ((i & 1) == 1) {
959 had_trailing_sep =
true;
972 size_t arg =
rule->arg[i & 1 & n];
973 if ((arg & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
974 if (lex->
tok_kind == (arg & RULE_ARG_ARG_MASK)) {
990 assert((arg & RULE_ARG_KIND_MASK) == RULE_ARG_RULE);
991 push_rule(&
parser, rule_src_line,
rule, i + 1);
992 push_rule_from_arg(&
parser, arg);
1001 if ((n & 1) && (
rule->arg[1] & RULE_ARG_KIND_MASK) == RULE_ARG_TOK) {
1008 if (had_trailing_sep) {
1010 push_result_rule(&
parser, rule_src_line,
rule, i);
1015 push_result_rule(&
parser, rule_src_line,
rule, i);
1022 #if MICROPY_COMP_CONST 1029 sizeof(mp_parse_chunk_t) +
parser.cur_chunk->alloc,
1030 sizeof(mp_parse_chunk_t) +
parser.cur_chunk->union_.used,
1032 parser.cur_chunk->alloc =
parser.cur_chunk->union_.used;
1039 ||
parser.result_stack_top == 0
1045 "unexpected indent");
1048 "unindent does not match any outer indentation level");
1074 mp_parse_chunk_t *chunk = tree->
chunk;
1075 while (chunk !=
NULL) {
1076 mp_parse_chunk_t *next = chunk->union_.next;
1077 m_del(
byte, chunk,
sizeof(mp_parse_chunk_t) + chunk->alloc);
1082 #endif // MICROPY_ENABLE_COMPILER
#define MICROPY_ALLOC_PARSE_RULE_INIT
#define m_renew_maybe(type, ptr, old_num, new_num, allow_move)
bool mp_parse_node_is_const_true(mp_parse_node_t pn)
mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg)
qstr qstr_from_strn(const char *str, size_t len)
bool mp_parse_node_is_const_false(mp_parse_node_t pn)
#define MICROPY_ALLOC_PARSE_RULE_INC
void mp_lexer_to_next(mp_lexer_t *lex)
void mp_lexer_free(mp_lexer_t *lex)
const char * qstr_str(qstr q)
void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest)
rule(file_input_3)) DEF_RULE_NC(file_input_3
#define MICROPY_PORT_CONSTANTS
#define MICROPY_ALLOC_PARSE_INTERN_STRING_LEN
#define MP_PARSE_NODE_STRUCT_KIND(pns)
#define m_del(type, ptr, num)
#define MP_PARSE_NODE_LEAF_KIND(pn)
void mp_map_init(mp_map_t *map, size_t n)
#define MP_PARSE_NODE_LEAF_SMALL_INT(pn)
#define MP_PARSE_NODE_IS_SMALL_INT(pn)
#define MP_OBJ_NEW_QSTR(qst)
const mp_obj_type_t mp_type_bytes
#define MICROPY_ALLOC_PARSE_RESULT_INC
uintptr_t mp_parse_node_t
#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns)
qstr qstr_find_strn(const char *str, size_t str_len)
#define MP_OBJ_SMALL_INT_VALUE(o)
enum _mp_token_kind_t mp_token_kind_t
const mp_obj_type_t mp_type_SyntaxError
MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table)
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o)
mp_map_elem_t * mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind)
mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg)
void mp_map_deinit(mp_map_t *map)
void mp_parse_node_print(mp_parse_node_t pn, size_t indent)
#define MP_OBJ_NEW_SMALL_INT(small_int)
#define MP_PARSE_NODE_IS_ID(pn)
#define MP_PARSE_NODE_BYTES
#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k)
#define MP_PARSE_NODE_IS_STRUCT_KIND(pn, k)
int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes)
struct _mp_parse_chunk_t * chunk
#define MP_PARSE_NODE_STRING
#define MICROPY_ALLOC_PARSE_RESULT_INIT
#define MP_PARSE_NODE_TOKEN
unsigned long long uint64_t
const mp_obj_type_t mp_type_str
mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs)
int mp_obj_int_sign(mp_obj_t self_in)
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block)
mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex)
#define m_renew(type, ptr, old_num, new_num)
mp_parse_tree_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind)
const mp_obj_type_t mp_type_IndentationError
#define MP_PARSE_NODE_LEAF_ARG(pn)
const mp_obj_module_t mp_module_uerrno
#define MP_PARSE_NODE_IS_STRUCT(pn)
mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte *data, size_t len)
mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex)
#define MP_PARSE_NODE_IS_LEAF(pn)
#define MP_PARSE_NODE_IS_NULL(pn)
const mp_obj_module_t mp_module_uctypes
void mp_parse_tree_clear(mp_parse_tree_t *tree)
#define MICROPY_ALLOC_PARSE_CHUNK_INIT
#define MP_PARSE_NODE_NULL