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 type passed to next openNode() call. */ | |
|
Anton Muhin
2012/06/14 20:52:07
nit: I think you'd better put openNode() into [: :
Roman
2012/06/14 21:14:31
Simple grep shows that in "lib" [::] is never used
| |
| 19 String nextPrefix; | |
|
Anton Muhin
2012/06/14 20:52:07
maybe something like nextTypePrefix would be a bet
Roman
2012/06/14 21:14:31
Done.
| |
| 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) { | |
| 67 sb.add(nextPrefix); | |
| 68 nextPrefix = null; | |
| 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 // Custom helper to visit given node and print its type with prefix. | |
| 252 visitWithPrefix(Node node, String prefix) { | |
| 253 nextPrefix = prefix; | |
| 254 node.accept(this); | |
| 255 } | |
| 256 | |
| 257 openSendNodeWithFields(Send node, String type) { | |
| 258 openNode(type, { | |
| 259 "isPrefix" : "${node.isPrefix}", | |
| 260 "isPostfix" : "${node.isPostfix}", | |
| 261 "isIndex" : "${node.isIndex}" | |
| 262 }); | |
| 263 if (node.receiver !== null) { | |
|
Anton Muhin
2012/06/14 20:52:07
nit: single line? here and below
Roman
2012/06/14 21:14:31
Done (where possible).
| |
| 264 visitWithPrefix(node.receiver, "receiver:"); | |
| 265 } | |
| 266 if (node.selector !== null) { | |
| 267 visitWithPrefix(node.selector, "selector:"); | |
| 268 } | |
| 269 if (node.argumentsNode !== null) { | |
| 270 visitWithPrefix(node.argumentsNode, "argumentsNode:"); | |
| 271 } | |
| 272 } | |
| 273 | |
| 245 visitSend(Send node) { | 274 visitSend(Send node) { |
| 246 visitNodeWithChildren(node, "Send"); | 275 openSendNodeWithFields(node, "Send"); |
| 276 closeNode("Send"); | |
| 247 } | 277 } |
| 248 | 278 |
| 249 visitSendSet(SendSet node) { | 279 visitSendSet(SendSet node) { |
| 250 visitNodeWithChildren(node, "SendSet"); | 280 openSendNodeWithFields(node, "SendSet"); |
| 281 if (node.assignmentOperator !== null) { | |
| 282 visitWithPrefix(node.assignmentOperator, "assignmentOperator:"); | |
| 283 } | |
| 284 closeNode("SendSet"); | |
| 251 } | 285 } |
| 252 | 286 |
| 253 visitStringInterpolation(StringInterpolation node) { | 287 visitStringInterpolation(StringInterpolation node) { |
| 254 visitNodeWithChildren(node, "StringInterpolation"); | 288 visitNodeWithChildren(node, "StringInterpolation"); |
| 255 } | 289 } |
| 256 | 290 |
| 257 visitStringInterpolationPart(StringInterpolationPart node) { | 291 visitStringInterpolationPart(StringInterpolationPart node) { |
| 258 visitNodeWithChildren(node, "StringInterpolationPart"); | 292 visitNodeWithChildren(node, "StringInterpolationPart"); |
| 259 } | 293 } |
| 260 | 294 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 291 } | 325 } |
| 292 | 326 |
| 293 visitVariableDefinitions(VariableDefinitions node) { | 327 visitVariableDefinitions(VariableDefinitions node) { |
| 294 visitNodeWithChildren(node, "VariableDefinitions"); | 328 visitNodeWithChildren(node, "VariableDefinitions"); |
| 295 } | 329 } |
| 296 | 330 |
| 297 visitWhile(While node) { | 331 visitWhile(While node) { |
| 298 visitNodeWithChildren(node, "While"); | 332 visitNodeWithChildren(node, "While"); |
| 299 } | 333 } |
| 300 } | 334 } |
| OLD | NEW |