| 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 the next [openNode()] call. */ |
| 19 String nextTypePrefix; |
| 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 (nextTypePrefix !== null) { |
| 67 sb.add(nextTypePrefix); |
| 68 nextTypePrefix = 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 nextTypePrefix = 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) visitWithPrefix(node.receiver, "receiver:"); |
| 264 if (node.selector !== null) visitWithPrefix(node.selector, "selector:"); |
| 265 if (node.argumentsNode !== null) |
| 266 visitWithPrefix(node.argumentsNode, "argumentsNode:"); |
| 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 visitWithPrefix(node.assignmentOperator, "assignmentOperator:"); |
| 278 closeNode("SendSet"); |
| 251 } | 279 } |
| 252 | 280 |
| 253 visitStringInterpolation(StringInterpolation node) { | 281 visitStringInterpolation(StringInterpolation node) { |
| 254 visitNodeWithChildren(node, "StringInterpolation"); | 282 visitNodeWithChildren(node, "StringInterpolation"); |
| 255 } | 283 } |
| 256 | 284 |
| 257 visitStringInterpolationPart(StringInterpolationPart node) { | 285 visitStringInterpolationPart(StringInterpolationPart node) { |
| 258 visitNodeWithChildren(node, "StringInterpolationPart"); | 286 visitNodeWithChildren(node, "StringInterpolationPart"); |
| 259 } | 287 } |
| 260 | 288 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 291 } | 319 } |
| 292 | 320 |
| 293 visitVariableDefinitions(VariableDefinitions node) { | 321 visitVariableDefinitions(VariableDefinitions node) { |
| 294 visitNodeWithChildren(node, "VariableDefinitions"); | 322 visitNodeWithChildren(node, "VariableDefinitions"); |
| 295 } | 323 } |
| 296 | 324 |
| 297 visitWhile(While node) { | 325 visitWhile(While node) { |
| 298 visitNodeWithChildren(node, "While"); | 326 visitNodeWithChildren(node, "While"); |
| 299 } | 327 } |
| 300 } | 328 } |
| OLD | NEW |