Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(401)

Side by Side Diff: lib/compiler/implementation/tree/prettyprint.dart

Issue 10449021: New Prettyprinter class that converts Node-tree to string. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Change class name from PrettyPrint to PrettyPrinter Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | lib/compiler/implementation/tree/tree.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * Pretty-prints Node tree in XML-like format.
7 *
8 * TODO(smok): Add main() to run from command-line to print out tree for given
9 * .dart file.
10 */
11 class PrettyPrinter implements Visitor {
12 StringBuffer sb;
13 int depth;
14
15 PrettyPrinter() : sb = new StringBuffer(), depth = 0;
16
17 void add(SourceString string) {
18 string.printOn(sb);
19 }
20
21 void addInNode(String type, [Map params]) {
ahe 2012/06/01 12:52:57 What does the method name mean?
Roman 2012/06/05 09:28:54 Changed to "openNode"
22 addCurrentIndent();
23 sb.add("<");
24 addTypeWithParams(type, params);
25 sb.add(">\n");
26 depth++;
27 }
28
29 void addInOutNode(String type, [Map params]) {
ahe 2012/06/01 12:52:57 Ditto.
Roman 2012/06/05 09:28:54 Changed to openAndCloseNode(), not sure if it's th
30 addCurrentIndent();
31 sb.add("<");
32 addTypeWithParams(type, params);
33 sb.add("/>\n");
34 }
35
36 void addOutNode(String type, [Map params]) {
ahe 2012/06/01 12:52:57 Ditto.
Roman 2012/06/05 09:28:54 Changed to "closeNode"
37 depth--;
38 addCurrentIndent();
39 sb.add("</");
40 addTypeWithParams(type, params);
41 sb.add(">\n");
42 }
43
44 void addTypeWithParams(String type, [Map params]) {
45 sb.add("${type}");
46 if (params != null) {
47 // TODO(smok): Escape doublequotes in values.
ahe 2012/06/01 12:52:57 When you get to address this TODO, we have code fo
48 params.forEach((k, v) => sb.add(' $k="$v"'));
49 }
50 }
51
52 void addCurrentIndent() {
53 for (int i = 0; i < depth; i++) {
54 if (i % 4 == 0) {
55 sb.add(". ");
ahe 2012/06/01 12:52:57 I'd prefer if you didn't add the periods. If you m
Roman 2012/06/05 09:28:54 Done.
56 } else {
57 sb.add(" ");
ahe 2012/06/01 12:52:57 Perhaps make this a static constant in the beginni
Roman 2012/06/05 09:28:54 Created static final String _INDENT = " "; Or did
58 }
59 }
60 }
61
62 /**
63 * Pretty-prints given node tree into string.
64 */
65 String prettyPrint(Node node) {
66 depth = 0;
67 sb = new StringBuffer();
ahe 2012/06/01 12:52:57 How do you expect to use this method and class? Ar
Roman 2012/06/05 09:28:54 What do you mean by two StringBuffers? I was think
ahe 2012/06/08 07:37:36 The constructor always creates a string buffer. So
Roman 2012/06/08 09:10:15 Ah, I see now. Yeah, that's dumb. Changed to just
68 node.accept(this);
69 return sb.toString();
70 }
71
72 visitNodeWithChildren(Node node, String type) {
73 addInNode(type);
74 node.visitChildren(this);
75 addOutNode(type);
76 }
77
78 visitBlock(Block node) {
79 visitNodeWithChildren(node, "Block");
80 }
81
82 visitBreakStatement(BreakStatement node) {
83 visitNodeWithChildren(node, "BreakStatement");
84 }
85
86 visitCascade(Cascade node) {
87 visitNodeWithChildren(node, "Cascade");
88 }
89
90 visitCascadeReceiver(CascadeReceiver node) {
91 visitNodeWithChildren(node, "CascadeReceiver");
92 }
93
94 visitCaseMatch(CaseMatch node) {
95 visitNodeWithChildren(node, "CaseMatch");
96 }
97
98 visitCatchBlock(CatchBlock node) {
99 visitNodeWithChildren(node, "CatchBlock");
100 }
101
102 visitClassNode(ClassNode node) {
103 visitNodeWithChildren(node, "ClassNode");
104 }
105
106 visitConditional(Conditional node) {
107 visitNodeWithChildren(node, "Conditional");
108 }
109
110 visitContinueStatement(ContinueStatement node) {
111 visitNodeWithChildren(node, "ContinueStatement");
112 }
113
114 visitDoWhile(DoWhile node) {
115 visitNodeWithChildren(node, "DoWhile");
116 }
117
118 visitEmptyStatement(EmptyStatement node) {
119 visitNodeWithChildren(node, "EmptyStatement");
120 }
121
122 visitExpressionStatement(ExpressionStatement node) {
123 visitNodeWithChildren(node, "ExpressionStatement");
124 }
125
126 visitFor(For node) {
127 visitNodeWithChildren(node, "For");
128 }
129
130 visitForIn(ForIn node) {
131 visitNodeWithChildren(node, "ForIn");
132 }
133
134 visitFunctionDeclaration(FunctionDeclaration node) {
135 visitNodeWithChildren(node, "FunctionDeclaration");
136 }
137
138 visitFunctionExpression(FunctionExpression node) {
139 visitNodeWithChildren(node, "FunctionExpression");
140 }
141
142 visitIdentifier(Identifier node) {
143 addInOutNode("Identifier", {"token" : node.token.slowToString()});
144 }
145
146 visitIf(If node) {
147 visitNodeWithChildren(node, "If");
148 }
149
150 visitLabel(Label node) {
151 visitNodeWithChildren(node, "Label");
152 }
153
154 visitLabeledStatement(LabeledStatement node) {
155 visitNodeWithChildren(node, "LabeledStatement");
156 }
157
158 // Custom.
159 visitLiteral(Literal node, String type) {
160 addInOutNode(type, {"value" : node.value.toString()});
161 }
162
163 visitLiteralBool(LiteralBool node) {
164 visitLiteral(node, "LiteralBool");
165 }
166
167 visitLiteralDouble(LiteralDouble node) {
168 visitLiteral(node, "LiteralDouble");
169 }
170
171 visitLiteralInt(LiteralInt node) {
172 visitLiteral(node, "LiteralInt");
173 }
174
175 visitLiteralList(LiteralList node) {
176 visitNodeWithChildren(node, "LiteralList");
177 }
178
179 visitLiteralMap(LiteralMap node) {
180 visitNodeWithChildren(node, "LiteralMap");
181 }
182
183 visitLiteralMapEntry(LiteralMapEntry node) {
184 visitNodeWithChildren(node, "LiteralMapEntry");
185 }
186
187 visitLiteralNull(LiteralNull node) {
188 visitLiteral(node, "LiteralNull");
189 }
190
191 visitLiteralString(LiteralString node) {
192 addInOutNode("LiteralString", {"value" : node.token.slowToString()});
193 }
194
195 visitModifiers(Modifiers node) {
196 visitNodeWithChildren(node, "Modifiers");
197 }
198
199 visitNamedArgument(NamedArgument node) {
200 visitNodeWithChildren(node, "NamedArgument");
201 }
202
203 visitNewExpression(NewExpression node) {
204 visitNodeWithChildren(node, "NewExpression");
205 }
206
207 visitNodeList(NodeList node) {
208 if (node.nodes.toList().length == 0) {
209 addInOutNode("NodeList");
210 } else {
211 visitNodeWithChildren(node, "NodeList");
212 }
213 }
214
215 visitOperator(Operator node) {
216 addInOutNode("Operator", {"value" : node.token.slowToString()});
217 }
218
219 visitParenthesizedExpression(ParenthesizedExpression node) {
220 visitNodeWithChildren(node, "ParenthesizedExpression");
221 }
222
223 visitReturn(Return node) {
224 visitNodeWithChildren(node, "Return");
225 }
226
227 visitScriptTag(ScriptTag node) {
228 visitNodeWithChildren(node, "ScriptTag");
229 }
230
231 visitSend(Send node) {
232 visitNodeWithChildren(node, "Send");
233 }
234
235 visitSendSet(SendSet node) {
236 visitNodeWithChildren(node, "SendSet");
237 }
238
239 visitStringInterpolation(StringInterpolation node) {
240 visitNodeWithChildren(node, "StringInterpolation");
241 }
242
243 visitStringInterpolationPart(StringInterpolationPart node) {
244 visitNodeWithChildren(node, "StringInterpolationPart");
245 }
246
247 visitStringJuxtaposition(StringJuxtaposition node) {
248 visitNodeWithChildren(node, "StringJuxtaposition");
249 }
250
251 visitSwitchCase(SwitchCase node) {
252 visitNodeWithChildren(node, "SwitchCase");
253 }
254
255 visitSwitchStatement(SwitchStatement node) {
256 visitNodeWithChildren(node, "SwitchStatement");
257 }
258
259 visitThrow(Throw node) {
260 visitNodeWithChildren(node, "Throw");
261 }
262
263 visitTryStatement(TryStatement node) {
264 visitNodeWithChildren(node, "TryStatement");
265 }
266
267 visitTypeAnnotation(TypeAnnotation node) {
268 visitNodeWithChildren(node, "TypeAnnotation");
269 }
270
271 visitTypedef(Typedef node) {
272 visitNodeWithChildren(node, "Typedef");
273 }
274
275 visitTypeVariable(TypeVariable node) {
276 visitNodeWithChildren(node, "TypeVariable");
277 }
278
279 visitVariableDefinitions(VariableDefinitions node) {
280 visitNodeWithChildren(node, "VariableDefinitions");
281 }
282
283 visitWhile(While node) {
284 visitNodeWithChildren(node, "While");
285 }
286 }
OLDNEW
« no previous file with comments | « no previous file | lib/compiler/implementation/tree/tree.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698