From: Matthias Kruk Date: Wed, 22 Jul 2020 16:01:29 +0000 (+0900) Subject: grammar: Add syntax_node_debug() method X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=refs%2Fheads%2Fparser-rewrite;p=ccc grammar: Add syntax_node_debug() method --- diff --git a/src/grammar.c b/src/grammar.c index dc6a8a0..9df0bf8 100644 --- a/src/grammar.c +++ b/src/grammar.c @@ -8,6 +8,78 @@ #include "lex.h" #include "assoc_array.h" +static const char *_syntax_node_type_strs[] = { + "(invalid)", + "translation-unit", + "external-declaration", + "function-definition", + "declaration", + "declaration-list", + "declaration-specifiers", + "storage-class-specifiers", + "type-specifier", + "type-qualifier", + "struct-or-union-specifier", + "struct-or-union", + "struct-declaration-list", + "init-declarator-list", + "init-declarator", + "struct-declaration", + "specifier-qualifier-list", + "struct-declarator-list", + "struct-declarator", + "enum-specifier", + "enumerator-list", + "enumerator", + "declarator", + "direct-declarator", + "pointer", + "type-qualifier-list", + "parameter-type-list", + "parameter-list", + "parameter-declaration", + "identifier", + "identifier-list", + "initializer", + "initializer-list", + "type-name", + "abstract-declarator", + "direct-abstract-declarator", + "typedef-name", + "statement", + "labeled-statement", + "expression-statement", + "compound-statement", + "statement-list", + "selection-statement", + "iteration-statement", + "jump-statement", + "expression", + "assignment-expression", + "assignment-operator", + "conditional-expression", + "constant-expression", + "logical-or-expression", + "logical-and-expression", + "inclusive-or-expression", + "exclusive-or-expression", + "and-expression", + "equality-expression", + "relational-expression", + "shift-expression", + "additive-expression", + "multiplicative-expression", + "cast-expression", + "unary-expression", + "unary-operator", + "postfix-expression", + "primary-expression", + "argument-expression-list", + "constant", + "(max)", + NULL +}; + static struct rule _translation_unit_production_A[] = { { .type = RULE_NONTERMINAL, @@ -3320,6 +3392,7 @@ struct syntax_node *syntax_node_parse_production(syntax_node_type_t type, int pr } origin = lex_getpos(); + node->type = type; for(rule = _node_descriptors[type].productions[prod], match = 1; rule->type != RULE_INVALID && match; @@ -3394,6 +3467,30 @@ struct syntax_node *syntax_node_parse(syntax_node_type_t type) return(NULL); } +void _print_child(const char *name, struct syntax_node *node, int *level) +{ + syntax_node_debug(node, *level); + return; +} + +void syntax_node_debug(struct syntax_node *node, int level) +{ + int i; + + for(i = 0; i < level; i++) { + printf(" "); + } + + printf("%s\n", _syntax_node_type_strs[node->type]); + i = level + 1; + + assoc_array_foreach(node->children, + (void(*)(const char*, void*, void*))_print_child, + &i); + + return; +} + struct function_definition *function_definition_new(void) { struct function_definition *fd; diff --git a/src/grammar.h b/src/grammar.h index 4cd6ead..6da2dd5 100644 --- a/src/grammar.h +++ b/src/grammar.h @@ -1017,5 +1017,6 @@ int syntax_node_set_child(struct syntax_node*, const char*, struct syntax_node*) int syntax_node_get_child(struct syntax_node*, const char*, struct syntax_node**); syntax_node_type_t syntax_node_get_type(struct syntax_node*); void syntax_node_set_type(struct syntax_node*, const syntax_node_type_t); +void syntax_node_debug(struct syntax_node*, int); #endif /* GRAMMAR_H */