| OLD | NEW |
| (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 |
| 13 /** String used to represent one level of indent. */ |
| 14 static final String INDENT = " "; |
| 15 |
| 16 StringBuffer sb; |
| 17 int depth; |
| 18 |
| 19 PrettyPrinter() : sb = new StringBuffer(), depth = 0; |
| 20 |
| 21 /** |
| 22 * Adds given string to result string. |
| 23 */ |
| 24 void add(SourceString string) { |
| 25 string.printOn(sb); |
| 26 } |
| 27 |
| 28 /** |
| 29 * Adds given node type to result string, increasing current depth by 1. |
| 30 * The method "opens" the node, meaning that all output after calling |
| 31 * this method and before calling closeNode() will represent contents |
| 32 * of given node. |
| 33 */ |
| 34 void openNode(String type, [Map params]) { |
| 35 addCurrentIndent(); |
| 36 sb.add("<"); |
| 37 addTypeWithParams(type, params); |
| 38 sb.add(">\n"); |
| 39 depth++; |
| 40 } |
| 41 |
| 42 /** |
| 43 * Adds given node to result string, depth is not affected. |
| 44 */ |
| 45 void openAndCloseNode(String type, [Map params]) { |
| 46 addCurrentIndent(); |
| 47 sb.add("<"); |
| 48 addTypeWithParams(type, params); |
| 49 sb.add("/>\n"); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Closes given node type, decreasing current depth by 1. |
| 54 */ |
| 55 void closeNode(String type, [Map params]) { |
| 56 depth--; |
| 57 addCurrentIndent(); |
| 58 sb.add("</"); |
| 59 addTypeWithParams(type, params); |
| 60 sb.add(">\n"); |
| 61 } |
| 62 |
| 63 void addTypeWithParams(String type, [Map params]) { |
| 64 sb.add("${type}"); |
| 65 if (params != null) { |
| 66 // TODO(smok): Escape doublequotes in values. |
| 67 params.forEach((k, v) => sb.add(' $k="$v"')); |
| 68 } |
| 69 } |
| 70 |
| 71 void addCurrentIndent() { |
| 72 for (int i = 0; i < depth; i++) { |
| 73 sb.add(INDENT); |
| 74 } |
| 75 } |
| 76 |
| 77 /** |
| 78 * Pretty-prints given node tree into string. |
| 79 */ |
| 80 static String prettyPrint(Node node) { |
| 81 var p = new PrettyPrinter(); |
| 82 node.accept(p); |
| 83 return p.sb.toString(); |
| 84 } |
| 85 |
| 86 visitNodeWithChildren(Node node, String type) { |
| 87 openNode(type); |
| 88 node.visitChildren(this); |
| 89 closeNode(type); |
| 90 } |
| 91 |
| 92 visitBlock(Block node) { |
| 93 visitNodeWithChildren(node, "Block"); |
| 94 } |
| 95 |
| 96 visitBreakStatement(BreakStatement node) { |
| 97 visitNodeWithChildren(node, "BreakStatement"); |
| 98 } |
| 99 |
| 100 visitCascade(Cascade node) { |
| 101 visitNodeWithChildren(node, "Cascade"); |
| 102 } |
| 103 |
| 104 visitCascadeReceiver(CascadeReceiver node) { |
| 105 visitNodeWithChildren(node, "CascadeReceiver"); |
| 106 } |
| 107 |
| 108 visitCaseMatch(CaseMatch node) { |
| 109 visitNodeWithChildren(node, "CaseMatch"); |
| 110 } |
| 111 |
| 112 visitCatchBlock(CatchBlock node) { |
| 113 visitNodeWithChildren(node, "CatchBlock"); |
| 114 } |
| 115 |
| 116 visitClassNode(ClassNode node) { |
| 117 visitNodeWithChildren(node, "ClassNode"); |
| 118 } |
| 119 |
| 120 visitConditional(Conditional node) { |
| 121 visitNodeWithChildren(node, "Conditional"); |
| 122 } |
| 123 |
| 124 visitContinueStatement(ContinueStatement node) { |
| 125 visitNodeWithChildren(node, "ContinueStatement"); |
| 126 } |
| 127 |
| 128 visitDoWhile(DoWhile node) { |
| 129 visitNodeWithChildren(node, "DoWhile"); |
| 130 } |
| 131 |
| 132 visitEmptyStatement(EmptyStatement node) { |
| 133 visitNodeWithChildren(node, "EmptyStatement"); |
| 134 } |
| 135 |
| 136 visitExpressionStatement(ExpressionStatement node) { |
| 137 visitNodeWithChildren(node, "ExpressionStatement"); |
| 138 } |
| 139 |
| 140 visitFor(For node) { |
| 141 visitNodeWithChildren(node, "For"); |
| 142 } |
| 143 |
| 144 visitForIn(ForIn node) { |
| 145 visitNodeWithChildren(node, "ForIn"); |
| 146 } |
| 147 |
| 148 visitFunctionDeclaration(FunctionDeclaration node) { |
| 149 visitNodeWithChildren(node, "FunctionDeclaration"); |
| 150 } |
| 151 |
| 152 visitFunctionExpression(FunctionExpression node) { |
| 153 visitNodeWithChildren(node, "FunctionExpression"); |
| 154 } |
| 155 |
| 156 visitIdentifier(Identifier node) { |
| 157 openAndCloseNode("Identifier", {"token" : node.token.slowToString()}); |
| 158 } |
| 159 |
| 160 visitIf(If node) { |
| 161 visitNodeWithChildren(node, "If"); |
| 162 } |
| 163 |
| 164 visitLabel(Label node) { |
| 165 visitNodeWithChildren(node, "Label"); |
| 166 } |
| 167 |
| 168 visitLabeledStatement(LabeledStatement node) { |
| 169 visitNodeWithChildren(node, "LabeledStatement"); |
| 170 } |
| 171 |
| 172 // Custom. |
| 173 visitLiteral(Literal node, String type) { |
| 174 openAndCloseNode(type, {"value" : node.value.toString()}); |
| 175 } |
| 176 |
| 177 visitLiteralBool(LiteralBool node) { |
| 178 visitLiteral(node, "LiteralBool"); |
| 179 } |
| 180 |
| 181 visitLiteralDouble(LiteralDouble node) { |
| 182 visitLiteral(node, "LiteralDouble"); |
| 183 } |
| 184 |
| 185 visitLiteralInt(LiteralInt node) { |
| 186 visitLiteral(node, "LiteralInt"); |
| 187 } |
| 188 |
| 189 visitLiteralList(LiteralList node) { |
| 190 visitNodeWithChildren(node, "LiteralList"); |
| 191 } |
| 192 |
| 193 visitLiteralMap(LiteralMap node) { |
| 194 visitNodeWithChildren(node, "LiteralMap"); |
| 195 } |
| 196 |
| 197 visitLiteralMapEntry(LiteralMapEntry node) { |
| 198 visitNodeWithChildren(node, "LiteralMapEntry"); |
| 199 } |
| 200 |
| 201 visitLiteralNull(LiteralNull node) { |
| 202 visitLiteral(node, "LiteralNull"); |
| 203 } |
| 204 |
| 205 visitLiteralString(LiteralString node) { |
| 206 openAndCloseNode("LiteralString", {"value" : node.token.slowToString()}); |
| 207 } |
| 208 |
| 209 visitModifiers(Modifiers node) { |
| 210 visitNodeWithChildren(node, "Modifiers"); |
| 211 } |
| 212 |
| 213 visitNamedArgument(NamedArgument node) { |
| 214 visitNodeWithChildren(node, "NamedArgument"); |
| 215 } |
| 216 |
| 217 visitNewExpression(NewExpression node) { |
| 218 visitNodeWithChildren(node, "NewExpression"); |
| 219 } |
| 220 |
| 221 visitNodeList(NodeList node) { |
| 222 if (node.nodes.toList().length == 0) { |
| 223 openAndCloseNode("NodeList"); |
| 224 } else { |
| 225 visitNodeWithChildren(node, "NodeList"); |
| 226 } |
| 227 } |
| 228 |
| 229 visitOperator(Operator node) { |
| 230 openAndCloseNode("Operator", {"value" : node.token.slowToString()}); |
| 231 } |
| 232 |
| 233 visitParenthesizedExpression(ParenthesizedExpression node) { |
| 234 visitNodeWithChildren(node, "ParenthesizedExpression"); |
| 235 } |
| 236 |
| 237 visitReturn(Return node) { |
| 238 visitNodeWithChildren(node, "Return"); |
| 239 } |
| 240 |
| 241 visitScriptTag(ScriptTag node) { |
| 242 visitNodeWithChildren(node, "ScriptTag"); |
| 243 } |
| 244 |
| 245 visitSend(Send node) { |
| 246 visitNodeWithChildren(node, "Send"); |
| 247 } |
| 248 |
| 249 visitSendSet(SendSet node) { |
| 250 visitNodeWithChildren(node, "SendSet"); |
| 251 } |
| 252 |
| 253 visitStringInterpolation(StringInterpolation node) { |
| 254 visitNodeWithChildren(node, "StringInterpolation"); |
| 255 } |
| 256 |
| 257 visitStringInterpolationPart(StringInterpolationPart node) { |
| 258 visitNodeWithChildren(node, "StringInterpolationPart"); |
| 259 } |
| 260 |
| 261 visitStringJuxtaposition(StringJuxtaposition node) { |
| 262 visitNodeWithChildren(node, "StringJuxtaposition"); |
| 263 } |
| 264 |
| 265 visitSwitchCase(SwitchCase node) { |
| 266 visitNodeWithChildren(node, "SwitchCase"); |
| 267 } |
| 268 |
| 269 visitSwitchStatement(SwitchStatement node) { |
| 270 visitNodeWithChildren(node, "SwitchStatement"); |
| 271 } |
| 272 |
| 273 visitThrow(Throw node) { |
| 274 visitNodeWithChildren(node, "Throw"); |
| 275 } |
| 276 |
| 277 visitTryStatement(TryStatement node) { |
| 278 visitNodeWithChildren(node, "TryStatement"); |
| 279 } |
| 280 |
| 281 visitTypeAnnotation(TypeAnnotation node) { |
| 282 visitNodeWithChildren(node, "TypeAnnotation"); |
| 283 } |
| 284 |
| 285 visitTypedef(Typedef node) { |
| 286 visitNodeWithChildren(node, "Typedef"); |
| 287 } |
| 288 |
| 289 visitTypeVariable(TypeVariable node) { |
| 290 visitNodeWithChildren(node, "TypeVariable"); |
| 291 } |
| 292 |
| 293 visitVariableDefinitions(VariableDefinitions node) { |
| 294 visitNodeWithChildren(node, "VariableDefinitions"); |
| 295 } |
| 296 |
| 297 visitWhile(While node) { |
| 298 visitNodeWithChildren(node, "While"); |
| 299 } |
| 300 } |
| OLD | NEW |