| 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 */ |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 } | 63 } |
| 64 | 64 |
| 65 void addTypeWithParams(String type, [Map params]) { | 65 void addTypeWithParams(String type, [Map params]) { |
| 66 if (nextTypePrefix !== null) { | 66 if (nextTypePrefix !== null) { |
| 67 sb.add(nextTypePrefix); | 67 sb.add(nextTypePrefix); |
| 68 nextTypePrefix = null; | 68 nextTypePrefix = null; |
| 69 } | 69 } |
| 70 sb.add("${type}"); | 70 sb.add("${type}"); |
| 71 if (params != null) { | 71 if (params != null) { |
| 72 // TODO(smok): Escape doublequotes in values. | 72 // TODO(smok): Escape doublequotes in values. |
| 73 params.forEach((k, v) => sb.add(' $k="$v"')); | 73 params.forEach((k, v) { |
| 74 sb.add(' $k='); |
| 75 if (v !== null) { |
| 76 sb.add('"$v"'); |
| 77 } else { |
| 78 sb.add('null'); |
| 79 } |
| 80 }); |
| 74 } | 81 } |
| 75 } | 82 } |
| 76 | 83 |
| 77 void addCurrentIndent() { | 84 void addCurrentIndent() { |
| 78 for (int i = 0; i < depth; i++) { | 85 for (int i = 0; i < depth; i++) { |
| 79 sb.add(INDENT); | 86 sb.add(INDENT); |
| 80 } | 87 } |
| 81 } | 88 } |
| 82 | 89 |
| 83 /** | 90 /** |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 } | 192 } |
| 186 | 193 |
| 187 visitLiteralDouble(LiteralDouble node) { | 194 visitLiteralDouble(LiteralDouble node) { |
| 188 visitLiteral(node, "LiteralDouble"); | 195 visitLiteral(node, "LiteralDouble"); |
| 189 } | 196 } |
| 190 | 197 |
| 191 visitLiteralInt(LiteralInt node) { | 198 visitLiteralInt(LiteralInt node) { |
| 192 visitLiteral(node, "LiteralInt"); | 199 visitLiteral(node, "LiteralInt"); |
| 193 } | 200 } |
| 194 | 201 |
| 202 /** Returns token string value or [null] if token is [null]. */ |
| 203 tokenToStringOrNull(Token token) => token === null ? null : token.stringValue; |
| 204 |
| 195 visitLiteralList(LiteralList node) { | 205 visitLiteralList(LiteralList node) { |
| 196 visitNodeWithChildren(node, "LiteralList"); | 206 openNode("LiteralList", { |
| 207 "constKeyword" : tokenToStringOrNull(node.constKeyword) |
| 208 }); |
| 209 visitWithPrefix(node.type, "type:"); |
| 210 visitWithPrefix(node.elements, "elements:"); |
| 211 closeNode("LiteralList"); |
| 197 } | 212 } |
| 198 | 213 |
| 199 visitLiteralMap(LiteralMap node) { | 214 visitLiteralMap(LiteralMap node) { |
| 200 visitNodeWithChildren(node, "LiteralMap"); | 215 visitNodeWithChildren(node, "LiteralMap"); |
| 201 } | 216 } |
| 202 | 217 |
| 203 visitLiteralMapEntry(LiteralMapEntry node) { | 218 visitLiteralMapEntry(LiteralMapEntry node) { |
| 204 visitNodeWithChildren(node, "LiteralMapEntry"); | 219 visitNodeWithChildren(node, "LiteralMapEntry"); |
| 205 } | 220 } |
| 206 | 221 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 218 | 233 |
| 219 visitNamedArgument(NamedArgument node) { | 234 visitNamedArgument(NamedArgument node) { |
| 220 visitNodeWithChildren(node, "NamedArgument"); | 235 visitNodeWithChildren(node, "NamedArgument"); |
| 221 } | 236 } |
| 222 | 237 |
| 223 visitNewExpression(NewExpression node) { | 238 visitNewExpression(NewExpression node) { |
| 224 visitNodeWithChildren(node, "NewExpression"); | 239 visitNodeWithChildren(node, "NewExpression"); |
| 225 } | 240 } |
| 226 | 241 |
| 227 visitNodeList(NodeList node) { | 242 visitNodeList(NodeList node) { |
| 243 var params = { |
| 244 "delimiter" : |
| 245 node.delimiter !== null ? node.delimiter.stringValue : null, |
| 246 "beginToken" : tokenToStringOrNull(node.getBeginToken()), |
| 247 "endToken" : tokenToStringOrNull(node.getEndToken())}; |
| 228 if (node.nodes.toList().length == 0) { | 248 if (node.nodes.toList().length == 0) { |
| 229 openAndCloseNode("NodeList"); | 249 openAndCloseNode("NodeList", params); |
| 230 } else { | 250 } else { |
| 231 visitNodeWithChildren(node, "NodeList"); | 251 openNode("NodeList", params); |
| 252 node.visitChildren(this); |
| 253 closeNode("NodeList"); |
| 232 } | 254 } |
| 233 } | 255 } |
| 234 | 256 |
| 235 visitOperator(Operator node) { | 257 visitOperator(Operator node) { |
| 236 openAndCloseNode("Operator", {"value" : node.token.slowToString()}); | 258 openAndCloseNode("Operator", {"value" : node.token.slowToString()}); |
| 237 } | 259 } |
| 238 | 260 |
| 239 visitParenthesizedExpression(ParenthesizedExpression node) { | 261 visitParenthesizedExpression(ParenthesizedExpression node) { |
| 240 visitNodeWithChildren(node, "ParenthesizedExpression"); | 262 visitNodeWithChildren(node, "ParenthesizedExpression"); |
| 241 } | 263 } |
| 242 | 264 |
| 243 visitReturn(Return node) { | 265 visitReturn(Return node) { |
| 244 var beginToken = | 266 var beginToken = node.getBeginToken() !== null |
| 245 node.beginToken !== null ? node.beginToken.stringValue : "null"; | 267 ? node.getBeginToken().stringValue : "null"; |
| 246 var endToken = node.endToken !== null ? node.endToken.stringValue : "null"; | 268 var endToken = node.getEndToken() !== null |
| 269 ? node.getEndToken().stringValue : "null"; |
| 247 openNode("Return", {"beginToken" : beginToken, "endToken" : endToken}); | 270 openNode("Return", {"beginToken" : beginToken, "endToken" : endToken}); |
| 248 if (node.hasExpression) visitWithPrefix(node.expression, "expression:"); | 271 visitWithPrefix(node.expression, "expression:"); |
| 249 closeNode("Return"); | 272 closeNode("Return"); |
| 250 } | 273 } |
| 251 | 274 |
| 252 visitScriptTag(ScriptTag node) { | 275 visitScriptTag(ScriptTag node) { |
| 253 visitNodeWithChildren(node, "ScriptTag"); | 276 visitNodeWithChildren(node, "ScriptTag"); |
| 254 } | 277 } |
| 255 | 278 |
| 256 /** Custom helper to visit given node and print its type with prefix. */ | 279 /** Custom helper to visit given node and print its type with prefix. */ |
| 257 visitWithPrefix(Node node, String prefix) { | 280 visitWithPrefix(Node node, String prefix) { |
| 281 if (node === null) return; |
| 258 nextTypePrefix = prefix; | 282 nextTypePrefix = prefix; |
| 259 node.accept(this); | 283 node.accept(this); |
| 260 } | 284 } |
| 261 | 285 |
| 262 openSendNodeWithFields(Send node, String type) { | 286 openSendNodeWithFields(Send node, String type) { |
| 263 openNode(type, { | 287 openNode(type, { |
| 264 "isPrefix" : "${node.isPrefix}", | 288 "isPrefix" : "${node.isPrefix}", |
| 265 "isPostfix" : "${node.isPostfix}", | 289 "isPostfix" : "${node.isPostfix}", |
| 266 "isIndex" : "${node.isIndex}" | 290 "isIndex" : "${node.isIndex}" |
| 267 }); | 291 }); |
| 268 if (node.receiver !== null) visitWithPrefix(node.receiver, "receiver:"); | 292 visitWithPrefix(node.receiver, "receiver:"); |
| 269 if (node.selector !== null) visitWithPrefix(node.selector, "selector:"); | 293 visitWithPrefix(node.selector, "selector:"); |
| 270 if (node.argumentsNode !== null) | 294 visitWithPrefix(node.argumentsNode, "argumentsNode:"); |
| 271 visitWithPrefix(node.argumentsNode, "argumentsNode:"); | |
| 272 } | 295 } |
| 273 | 296 |
| 274 visitSend(Send node) { | 297 visitSend(Send node) { |
| 275 openSendNodeWithFields(node, "Send"); | 298 openSendNodeWithFields(node, "Send"); |
| 276 closeNode("Send"); | 299 closeNode("Send"); |
| 277 } | 300 } |
| 278 | 301 |
| 279 visitSendSet(SendSet node) { | 302 visitSendSet(SendSet node) { |
| 280 openSendNodeWithFields(node, "SendSet"); | 303 openSendNodeWithFields(node, "SendSet"); |
| 281 if (node.assignmentOperator !== null) | 304 visitWithPrefix(node.assignmentOperator, "assignmentOperator:"); |
| 282 visitWithPrefix(node.assignmentOperator, "assignmentOperator:"); | |
| 283 closeNode("SendSet"); | 305 closeNode("SendSet"); |
| 284 } | 306 } |
| 285 | 307 |
| 286 visitStringInterpolation(StringInterpolation node) { | 308 visitStringInterpolation(StringInterpolation node) { |
| 287 visitNodeWithChildren(node, "StringInterpolation"); | 309 visitNodeWithChildren(node, "StringInterpolation"); |
| 288 } | 310 } |
| 289 | 311 |
| 290 visitStringInterpolationPart(StringInterpolationPart node) { | 312 visitStringInterpolationPart(StringInterpolationPart node) { |
| 291 visitNodeWithChildren(node, "StringInterpolationPart"); | 313 visitNodeWithChildren(node, "StringInterpolationPart"); |
| 292 } | 314 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 317 | 339 |
| 318 visitTypedef(Typedef node) { | 340 visitTypedef(Typedef node) { |
| 319 visitNodeWithChildren(node, "Typedef"); | 341 visitNodeWithChildren(node, "Typedef"); |
| 320 } | 342 } |
| 321 | 343 |
| 322 visitTypeVariable(TypeVariable node) { | 344 visitTypeVariable(TypeVariable node) { |
| 323 visitNodeWithChildren(node, "TypeVariable"); | 345 visitNodeWithChildren(node, "TypeVariable"); |
| 324 } | 346 } |
| 325 | 347 |
| 326 visitVariableDefinitions(VariableDefinitions node) { | 348 visitVariableDefinitions(VariableDefinitions node) { |
| 327 visitNodeWithChildren(node, "VariableDefinitions"); | 349 openNode("VariableDefinitions", { |
| 350 "beginToken" : "${node.getBeginToken().stringValue}", |
| 351 "endToken" : "${node.getEndToken().stringValue}" |
| 352 }); |
| 353 visitWithPrefix(node.type, "type:"); |
| 354 visitWithPrefix(node.modifiers, "modifiers:"); |
| 355 visitWithPrefix(node.definitions, "definitions:"); |
| 356 closeNode("VariableDefinitions"); |
| 328 } | 357 } |
| 329 | 358 |
| 330 visitWhile(While node) { | 359 visitWhile(While node) { |
| 331 visitNodeWithChildren(node, "While"); | 360 visitNodeWithChildren(node, "While"); |
| 332 } | 361 } |
| 333 } | 362 } |
| OLD | NEW |