1 /*
2 (C) Copyright 2009 Jonathan Schmidt-Dominé <devel@the-user.org>
3 Derived from the KDevelop-Java-Parser
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "dumptree.h"
21 #include "ccast.h"
22 #include "kdev-pg-token-stream.h"
23
24 #include <QString>
25
26 #include <iostream>
27
28 static char const * const names[] = {
29 "AND_expression",
30 "Abstract_declarator",
31 "Additive_expression",
32 "Argument_expression_list",
33 "Asm_against_mangling",
34 "Asm_specifier",
35 "Assignment_expression",
36 "Assignment_operator",
37 "Cast_expression",
38 "Compound_statement",
39 "Conditional_expression",
40 "Constant",
41 "Constant_expression",
42 "Ddeclaration",
43 "Declaration",
44 "Declaration_header",
45 "Declaration_parameter",
46 "Declaration_specifier",
47 "Declarator",
48 "Direct_abstract_declarator",
49 "Direct_declarator",
50 "Direct_declarator_rest",
51 "Document",
52 "Enum_specifier",
53 "Enumerator",
54 "Equality_expression",
55 "Exclusive_OR_expression",
56 "Execution_block",
57 "Expression",
58 "Expression_statement",
59 "Ext_expression",
60 "External_Block",
61 "External_declaration",
62 "Function_declaration",
63 "Function_definition",
64 "Inclusive_OR_expression",
65 "Init_declarator",
66 "Initializer",
67 "Inline_asm",
68 "Iteration_statement",
69 "Jump_statement",
70 "Labeled_statement",
71 "Logical_AND_expression",
72 "Logical_OR_expression",
73 "Multiplicative_expression",
74 "Named_parameter",
75 "Parameter",
76 "Parameter_declaration",
77 "Parameter_type_list",
78 "Pointer",
79 "Postfix_expression",
80 "Postfix_expression_rest",
81 "Primary_expression",
82 "Relational_expression",
83 "Selection_statement",
84 "Shift_expression",
85 "Specifier_qualifier",
86 "Statement",
87 "Storage_class_specifier",
88 "Struct_declaration",
89 "Struct_declarator",
90 "Struct_or_union_specifier",
91 "Translation_unit",
92 "Type_name",
93 "Type_name_d",
94 "Type_qualifier",
95 "Type_specifier",
96 "Typed_identifier",
97 "Typedef_d",
98 "Unary_expression",
99 "Unary_operator",
100 "Value_Declaration",
101 "Variable_declaration",
102 "Variable_or_function"
103 };
104
105
106 using namespace cc;
107
DumpTree()108 DumpTree::DumpTree()
109 : indent(0)
110 {
111 }
112
dump(AstNode * node)113 void DumpTree::dump( AstNode * node )
114 {
115 visitNode(node);
116 }
117
visitNode(AstNode * node)118 void DumpTree::visitNode(AstNode *node)
119 {
120 QString nodeText;
121 if (node)
122 {
123 std::cout << QString(indent * 2, ' ').toLatin1().constData() << names[node->kind - 1000]
124 << "[" << node->startToken << "," << node->endToken << "]" << nodeText.toLatin1().constData() << std::endl;
125 }
126
127 ++indent;
128 DefaultVisitor::visitNode(node);
129 --indent;
130
131 if (node)
132 {
133 std::cout << QString(indent * 2, ' ').toLatin1().constData() << names[node->kind - 1000] << std::endl;
134 }
135 }
136
~DumpTree()137 DumpTree::~DumpTree( )
138 {
139 }
140
141