Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Pretty-prints Node tree in XML-like format. | 6 * Pretty-prints Node tree in XML-like format. |
| 7 * | 7 * |
| 8 * TODO(smok): Add main() to run from command-line to print out tree for given | 8 * TODO(smok): Add main() to run from command-line to print out tree for given |
| 9 * .dart file. | 9 * .dart file. |
| 10 */ | 10 */ |
| 11 class PrettyPrinter implements Visitor { | 11 class PrettyPrinter implements Visitor { |
| 12 | 12 |
| 13 /** String used to represent one level of indent. */ | 13 /** String used to represent one level of indent. */ |
| 14 static final String INDENT = " "; | 14 static final String INDENT = " "; |
| 15 | 15 |
| 16 StringBuffer sb; | 16 StringBuffer sb; |
| 17 int depth; | 17 int depth; |
| 18 // Prefix for the next openNode() invocation for that type. | |
|
Anton Muhin
2012/06/14 20:32:37
nit: /** Prefix for the next [:openNode():] invoca
Roman
2012/06/14 20:47:32
Done. Reformulated. It's the type of the node to p
| |
| 19 String nextPrefix; | |
| 18 | 20 |
| 19 PrettyPrinter() : sb = new StringBuffer(), depth = 0; | 21 PrettyPrinter() : sb = new StringBuffer(), depth = 0; |
| 20 | 22 |
| 21 /** | 23 /** |
| 22 * Adds given string to result string. | 24 * Adds given string to result string. |
| 23 */ | 25 */ |
| 24 void add(SourceString string) { | 26 void add(SourceString string) { |
| 25 string.printOn(sb); | 27 string.printOn(sb); |
| 26 } | 28 } |
| 27 | 29 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 54 */ | 56 */ |
| 55 void closeNode(String type, [Map params]) { | 57 void closeNode(String type, [Map params]) { |
| 56 depth--; | 58 depth--; |
| 57 addCurrentIndent(); | 59 addCurrentIndent(); |
| 58 sb.add("</"); | 60 sb.add("</"); |
| 59 addTypeWithParams(type, params); | 61 addTypeWithParams(type, params); |
| 60 sb.add(">\n"); | 62 sb.add(">\n"); |
| 61 } | 63 } |
| 62 | 64 |
| 63 void addTypeWithParams(String type, [Map params]) { | 65 void addTypeWithParams(String type, [Map params]) { |
| 66 if (nextPrefix != null) { | |
|
Anton Muhin
2012/06/14 20:32:37
nit: !==
Roman
2012/06/14 20:47:32
Done.
| |
| 67 sb.add("$nextPrefix"); | |
|
Anton Muhin
2012/06/14 20:32:37
I think simple sb.add(nextPrefix) should work as w
Roman
2012/06/14 20:47:32
Done.
| |
| 68 nextPrefix = null; | |
|
Anton Muhin
2012/06/14 20:32:37
is it okay to reset it that early, do we want to h
Roman
2012/06/14 20:47:32
It may be nice, but not so necessary. And we would
| |
| 69 } | |
| 64 sb.add("${type}"); | 70 sb.add("${type}"); |
| 65 if (params != null) { | 71 if (params != null) { |
| 66 // TODO(smok): Escape doublequotes in values. | 72 // TODO(smok): Escape doublequotes in values. |
| 67 params.forEach((k, v) => sb.add(' $k="$v"')); | 73 params.forEach((k, v) => sb.add(' $k="$v"')); |
| 68 } | 74 } |
| 69 } | 75 } |
| 70 | 76 |
| 71 void addCurrentIndent() { | 77 void addCurrentIndent() { |
| 72 for (int i = 0; i < depth; i++) { | 78 for (int i = 0; i < depth; i++) { |
| 73 sb.add(INDENT); | 79 sb.add(INDENT); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 } | 241 } |
| 236 | 242 |
| 237 visitReturn(Return node) { | 243 visitReturn(Return node) { |
| 238 visitNodeWithChildren(node, "Return"); | 244 visitNodeWithChildren(node, "Return"); |
| 239 } | 245 } |
| 240 | 246 |
| 241 visitScriptTag(ScriptTag node) { | 247 visitScriptTag(ScriptTag node) { |
| 242 visitNodeWithChildren(node, "ScriptTag"); | 248 visitNodeWithChildren(node, "ScriptTag"); |
| 243 } | 249 } |
| 244 | 250 |
| 251 openSendNodeWithFields(Send node, String type) { | |
| 252 openNode(type, | |
|
Anton Muhin
2012/06/14 20:32:37
nit: somewhat weird formatting, I'd rather do it l
Roman
2012/06/14 20:47:32
Done.
| |
| 253 {"isPrefix" : "${node.isPrefix}", "isPostfix" : "${node.isPostfix}", | |
| 254 "isIndex" : "${node.isIndex}"}); | |
| 255 if (node.receiver !== null) { | |
| 256 nextPrefix = "receiver:"; | |
|
Anton Muhin
2012/06/14 20:32:37
you may want to introduce withPrefix helper, somet
Roman
2012/06/14 20:47:32
Done.
| |
| 257 node.receiver.accept(this); | |
| 258 } | |
| 259 if (node.selector !== null) { | |
| 260 nextPrefix = "selector:"; | |
| 261 node.selector.accept(this); | |
| 262 } | |
| 263 if (node.argumentsNode !== null) { | |
| 264 nextPrefix = "argumentsNode:"; | |
| 265 node.argumentsNode.accept(this); | |
| 266 } | |
| 267 } | |
| 268 | |
| 245 visitSend(Send node) { | 269 visitSend(Send node) { |
| 246 visitNodeWithChildren(node, "Send"); | 270 openSendNodeWithFields(node, "Send"); |
| 271 closeNode("Send"); | |
| 247 } | 272 } |
| 248 | 273 |
| 249 visitSendSet(SendSet node) { | 274 visitSendSet(SendSet node) { |
| 250 visitNodeWithChildren(node, "SendSet"); | 275 openSendNodeWithFields(node, "SendSet"); |
| 276 if (node.assignmentOperator !== null) { | |
| 277 nextPrefix = "assignmentOperator:"; | |
| 278 node.assignmentOperator.accept(this); | |
| 279 } | |
| 280 closeNode("SendSet"); | |
| 251 } | 281 } |
| 252 | 282 |
| 253 visitStringInterpolation(StringInterpolation node) { | 283 visitStringInterpolation(StringInterpolation node) { |
| 254 visitNodeWithChildren(node, "StringInterpolation"); | 284 visitNodeWithChildren(node, "StringInterpolation"); |
| 255 } | 285 } |
| 256 | 286 |
| 257 visitStringInterpolationPart(StringInterpolationPart node) { | 287 visitStringInterpolationPart(StringInterpolationPart node) { |
| 258 visitNodeWithChildren(node, "StringInterpolationPart"); | 288 visitNodeWithChildren(node, "StringInterpolationPart"); |
| 259 } | 289 } |
| 260 | 290 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 291 } | 321 } |
| 292 | 322 |
| 293 visitVariableDefinitions(VariableDefinitions node) { | 323 visitVariableDefinitions(VariableDefinitions node) { |
| 294 visitNodeWithChildren(node, "VariableDefinitions"); | 324 visitNodeWithChildren(node, "VariableDefinitions"); |
| 295 } | 325 } |
| 296 | 326 |
| 297 visitWhile(While node) { | 327 visitWhile(While node) { |
| 298 visitNodeWithChildren(node, "While"); | 328 visitNodeWithChildren(node, "While"); |
| 299 } | 329 } |
| 300 } | 330 } |
| OLD | NEW |