|
61 | 61 | typedef struct _rule_t { |
62 | 62 | byte rule_id; |
63 | 63 | byte act; |
64 | | -#ifdef USE_RULE_NAME |
65 | | - const char *rule_name; |
66 | | -#endif |
67 | 64 | uint16_t arg[]; |
68 | 65 | } rule_t; |
69 | 66 |
|
@@ -94,13 +91,8 @@ enum { |
94 | 91 | #define tok(t) (RULE_ARG_TOK | MP_TOKEN_##t) |
95 | 92 | #define rule(r) (RULE_ARG_RULE | RULE_##r) |
96 | 93 | #define opt_rule(r) (RULE_ARG_OPT_RULE | RULE_##r) |
97 | | -#ifdef USE_RULE_NAME |
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__ } }; |
100 | | -#else |
101 | 94 | #define DEF_RULE(rule, comp, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } }; |
102 | 95 | #define DEF_RULE_NC(rule, kind, ...) static const rule_t rule_##rule = { RULE_##rule, kind, { __VA_ARGS__ } }; |
103 | | -#endif |
104 | 96 | #include "py/grammar.h" |
105 | 97 | #undef or |
106 | 98 | #undef and |
@@ -130,6 +122,23 @@ STATIC const rule_t *const rules[] = { |
130 | 122 | #undef DEF_RULE_NC |
131 | 123 | }; |
132 | 124 |
|
| 125 | +#if USE_RULE_NAME |
| 126 | +// Define an array of rule names corresponding to each rule |
| 127 | +STATIC const char *const rule_name_table[] = { |
| 128 | +#define DEF_RULE(rule, comp, kind, ...) #rule, |
| 129 | +#define DEF_RULE_NC(rule, kind, ...) |
| 130 | +#include "py/grammar.h" |
| 131 | +#undef DEF_RULE |
| 132 | +#undef DEF_RULE_NC |
| 133 | + "", // RULE_const_object |
| 134 | +#define DEF_RULE(rule, comp, kind, ...) |
| 135 | +#define DEF_RULE_NC(rule, kind, ...) #rule, |
| 136 | +#include "py/grammar.h" |
| 137 | +#undef DEF_RULE |
| 138 | +#undef DEF_RULE_NC |
| 139 | +}; |
| 140 | +#endif |
| 141 | + |
133 | 142 | typedef struct _rule_stack_t { |
134 | 143 | size_t src_line : 8 * sizeof(size_t) - 8; // maximum bits storing source line number |
135 | 144 | size_t rule_id : 8; // this must be large enough to fit largest rule number |
@@ -313,11 +322,11 @@ void mp_parse_node_print(mp_parse_node_t pn, size_t indent) { |
313 | 322 | #endif |
314 | 323 | } else { |
315 | 324 | size_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
316 | | -#ifdef USE_RULE_NAME |
317 | | - printf("%s(%u) (n=%u)\n", rules[MP_PARSE_NODE_STRUCT_KIND(pns)]->rule_name, (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); |
318 | | -#else |
| 325 | + #if USE_RULE_NAME |
| 326 | + printf("%s(%u) (n=%u)\n", rule_name_table[MP_PARSE_NODE_STRUCT_KIND(pns)], (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); |
| 327 | + #else |
319 | 328 | printf("rule(%u) (n=%u)\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns), (uint)n); |
320 | | -#endif |
| 329 | + #endif |
321 | 330 | for (size_t i = 0; i < n; i++) { |
322 | 331 | mp_parse_node_print(pns->nodes[i], indent + 2); |
323 | 332 | } |
@@ -797,7 +806,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) { |
797 | 806 | for (int j = 0; j < parser.rule_stack_top; ++j) { |
798 | 807 | printf(" "); |
799 | 808 | } |
800 | | - printf("%s n=%d i=%d bt=%d\n", rule->rule_name, n, i, backtrack); |
| 809 | + printf("%s n=%d i=%d bt=%d\n", rule_name_table[rule->rule_id], n, i, backtrack); |
801 | 810 | */ |
802 | 811 |
|
803 | 812 | switch (rule->act & RULE_ACT_KIND_MASK) { |
|
0 commit comments