Chromium Code Reviews| 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 String prettyPrint(Node node) { | |
|
ahe
2012/06/08 09:45:31
I'm not really sure I understand the purpose of th
Roman
2012/06/08 09:56:33
Yes, this is great. Done.
| |
| 81 depth = 0; | |
| 82 sb.clear(); | |
| 83 node.accept(this); | |
| 84 return sb.toString(); | |
| 85 } | |
| 86 | |
| 87 visitNodeWithChildren(Node node, String type) { | |
| 88 openNode(type); | |
| 89 node.visitChildren(this); | |
| 90 closeNode(type); | |
| 91 } | |
| 92 | |
| 93 visitBlock(Block node) { | |
| 94 visitNodeWithChildren(node, "Block"); | |
| 95 } | |
| 96 | |
| 97 visitBreakStatement(BreakStatement node) { | |
| 98 visitNodeWithChildren(node, "BreakStatement"); | |
| 99 } | |
| 100 | |
| 101 visitCascade(Cascade node) { | |
| 102 visitNodeWithChildren(node, "Cascade"); | |
| 103 } | |
| 104 | |
| 105 visitCascadeReceiver(CascadeReceiver node) { | |
| 106 visitNodeWithChildren(node, "CascadeReceiver"); | |
| 107 } | |
| 108 | |
| 109 visitCaseMatch(CaseMatch node) { | |
| 110 visitNodeWithChildren(node, "CaseMatch"); | |
| 111 } | |
| 112 | |
| 113 visitCatchBlock(CatchBlock node) { | |
| 114 visitNodeWithChildren(node, "CatchBlock"); | |
| 115 } | |
| 116 | |
| 117 visitClassNode(ClassNode node) { | |
| 118 visitNodeWithChildren(node, "ClassNode"); | |
| 119 } | |
| 120 | |
| 121 visitConditional(Conditional node) { | |
| 122 visitNodeWithChildren(node, "Conditional"); | |
| 123 } | |
| 124 | |
| 125 visitContinueStatement(ContinueStatement node) { | |
| 126 visitNodeWithChildren(node, "ContinueStatement"); | |
| 127 } | |
| 128 | |
| 129 visitDoWhile(DoWhile node) { | |
| 130 visitNodeWithChildren(node, "DoWhile"); | |
| 131 } | |
| 132 | |
| 133 visitEmptyStatement(EmptyStatement node) { | |
| 134 visitNodeWithChildren(node, "EmptyStatement"); | |
| 135 } | |
| 136 | |
| 137 visitExpressionStatement(ExpressionStatement node) { | |
| 138 visitNodeWithChildren(node, "ExpressionStatement"); | |
| 139 } | |
| 140 | |
| 141 visitFor(For node) { | |
| 142 visitNodeWithChildren(node, "For"); | |
| 143 } | |
| 144 | |
| 145 visitForIn(ForIn node) { | |
| 146 visitNodeWithChildren(node, "ForIn"); | |
| 147 } | |
| 148 | |
| 149 visitFunctionDeclaration(FunctionDeclaration node) { | |
| 150 visitNodeWithChildren(node, "FunctionDeclaration"); | |
| 151 } | |
| 152 | |
| 153 visitFunctionExpression(FunctionExpression node) { | |
| 154 visitNodeWithChildren(node, "FunctionExpression"); | |
| 155 } | |
| 156 | |
| 157 visitIdentifier(Identifier node) { | |
| 158 openAndCloseNode("Identifier", {"token" : node.token.slowToString()}); | |
| 159 } | |
| 160 | |
| 161 visitIf(If node) { | |
| 162 visitNodeWithChildren(node, "If"); | |
| 163 } | |
| 164 | |
| 165 visitLabel(Label node) { | |
| 166 visitNodeWithChildren(node, "Label"); | |
| 167 } | |
| 168 | |
| 169 visitLabeledStatement(LabeledStatement node) { | |
| 170 visitNodeWithChildren(node, "LabeledStatement"); | |
| 171 } | |
| 172 | |
| 173 // Custom. | |
| 174 visitLiteral(Literal node, String type) { | |
| 175 openAndCloseNode(type, {"value" : node.value.toString()}); | |
| 176 } | |
| 177 | |
| 178 visitLiteralBool(LiteralBool node) { | |
| 179 visitLiteral(node, "LiteralBool"); | |
| 180 } | |
| 181 | |
| 182 visitLiteralDouble(LiteralDouble node) { | |
| 183 visitLiteral(node, "LiteralDouble"); | |
| 184 } | |
| 185 | |
| 186 visitLiteralInt(LiteralInt node) { | |
| 187 visitLiteral(node, "LiteralInt"); | |
| 188 } | |
| 189 | |
| 190 visitLiteralList(LiteralList node) { | |
| 191 visitNodeWithChildren(node, "LiteralList"); | |
| 192 } | |
| 193 | |
| 194 visitLiteralMap(LiteralMap node) { | |
| 195 visitNodeWithChildren(node, "LiteralMap"); | |
| 196 } | |
| 197 | |
| 198 visitLiteralMapEntry(LiteralMapEntry node) { | |
| 199 visitNodeWithChildren(node, "LiteralMapEntry"); | |
| 200 } | |
| 201 | |
| 202 visitLiteralNull(LiteralNull node) { | |
| 203 visitLiteral(node, "LiteralNull"); | |
| 204 } | |
| 205 | |
| 206 visitLiteralString(LiteralString node) { | |
| 207 openAndCloseNode("LiteralString", {"value" : node.token.slowToString()}); | |
| 208 } | |
| 209 | |
| 210 visitModifiers(Modifiers node) { | |
| 211 visitNodeWithChildren(node, "Modifiers"); | |
| 212 } | |
| 213 | |
| 214 visitNamedArgument(NamedArgument node) { | |
| 215 visitNodeWithChildren(node, "NamedArgument"); | |
| 216 } | |
| 217 | |
| 218 visitNewExpression(NewExpression node) { | |
| 219 visitNodeWithChildren(node, "NewExpression"); | |
| 220 } | |
| 221 | |
| 222 visitNodeList(NodeList node) { | |
| 223 if (node.nodes.toList().length == 0) { | |
| 224 openAndCloseNode("NodeList"); | |
| 225 } else { | |
| 226 visitNodeWithChildren(node, "NodeList"); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 visitOperator(Operator node) { | |
| 231 openAndCloseNode("Operator", {"value" : node.token.slowToString()}); | |
| 232 } | |
| 233 | |
| 234 visitParenthesizedExpression(ParenthesizedExpression node) { | |
| 235 visitNodeWithChildren(node, "ParenthesizedExpression"); | |
| 236 } | |
| 237 | |
| 238 visitReturn(Return node) { | |
| 239 visitNodeWithChildren(node, "Return"); | |
| 240 } | |
| 241 | |
| 242 visitScriptTag(ScriptTag node) { | |
| 243 visitNodeWithChildren(node, "ScriptTag"); | |
| 244 } | |
| 245 | |
| 246 visitSend(Send node) { | |
| 247 visitNodeWithChildren(node, "Send"); | |
| 248 } | |
| 249 | |
| 250 visitSendSet(SendSet node) { | |
| 251 visitNodeWithChildren(node, "SendSet"); | |
| 252 } | |
| 253 | |
| 254 visitStringInterpolation(StringInterpolation node) { | |
| 255 visitNodeWithChildren(node, "StringInterpolation"); | |
| 256 } | |
| 257 | |
| 258 visitStringInterpolationPart(StringInterpolationPart node) { | |
| 259 visitNodeWithChildren(node, "StringInterpolationPart"); | |
| 260 } | |
| 261 | |
| 262 visitStringJuxtaposition(StringJuxtaposition node) { | |
| 263 visitNodeWithChildren(node, "StringJuxtaposition"); | |
| 264 } | |
| 265 | |
| 266 visitSwitchCase(SwitchCase node) { | |
| 267 visitNodeWithChildren(node, "SwitchCase"); | |
| 268 } | |
| 269 | |
| 270 visitSwitchStatement(SwitchStatement node) { | |
| 271 visitNodeWithChildren(node, "SwitchStatement"); | |
| 272 } | |
| 273 | |
| 274 visitThrow(Throw node) { | |
| 275 visitNodeWithChildren(node, "Throw"); | |
| 276 } | |
| 277 | |
| 278 visitTryStatement(TryStatement node) { | |
| 279 visitNodeWithChildren(node, "TryStatement"); | |
| 280 } | |
| 281 | |
| 282 visitTypeAnnotation(TypeAnnotation node) { | |
| 283 visitNodeWithChildren(node, "TypeAnnotation"); | |
| 284 } | |
| 285 | |
| 286 visitTypedef(Typedef node) { | |
| 287 visitNodeWithChildren(node, "Typedef"); | |
| 288 } | |
| 289 | |
| 290 visitTypeVariable(TypeVariable node) { | |
| 291 visitNodeWithChildren(node, "TypeVariable"); | |
| 292 } | |
| 293 | |
| 294 visitVariableDefinitions(VariableDefinitions node) { | |
| 295 visitNodeWithChildren(node, "VariableDefinitions"); | |
| 296 } | |
| 297 | |
| 298 visitWhile(While node) { | |
| 299 visitNodeWithChildren(node, "While"); | |
| 300 } | |
| 301 } | |
| OLD | NEW |