]> git.corax.cc Git - ccc/commitdiff
grammar: Add syntax_node_debug() method parser-rewrite
authorMatthias Kruk <m@m10k.eu>
Wed, 22 Jul 2020 16:01:29 +0000 (01:01 +0900)
committerMatthias Kruk <m@m10k.eu>
Wed, 22 Jul 2020 16:01:29 +0000 (01:01 +0900)
src/grammar.c
src/grammar.h

index dc6a8a08888c2f98658bdedd920d63ea3bf0086c..9df0bf8e476390eb302dff46bd1a4714b728c9f8 100644 (file)
@@ -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;
index 4cd6eadae856864c3857681092a024ac15b85956..6da2dd5de531714b9e7d4b9d1dfc45aaf2f971df 100644 (file)
@@ -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 */