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