| 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 part of tree; | 5 part of tree; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Pretty-prints Node tree in XML-like format. | 8 * Pretty-prints Node tree in XML-like format. |
| 9 * | 9 * |
| 10 * TODO(smok): Add main() to run from command-line to print out tree for given | 10 * TODO(smok): Add main() to run from command-line to print out tree for given |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 | 47 |
| 48 /** | 48 /** |
| 49 * Adds given node type to result string. | 49 * Adds given node type to result string. |
| 50 * The method "opens" the node, meaning that all output after calling | 50 * The method "opens" the node, meaning that all output after calling |
| 51 * this method and before calling closeNode() will represent contents | 51 * this method and before calling closeNode() will represent contents |
| 52 * of given node. | 52 * of given node. |
| 53 */ | 53 */ |
| 54 void openNode(Node node, String type, [Map params]) { | 54 void openNode(Node node, String type, [Map params]) { |
| 55 if (params == null) params = new Map(); | 55 if (params == null) params = new Map(); |
| 56 addCurrentIndent(); | 56 addCurrentIndent(); |
| 57 sb.add("<"); | 57 sb.write("<"); |
| 58 addBeginAndEndTokensToParams(node, params); | 58 addBeginAndEndTokensToParams(node, params); |
| 59 addTypeWithParams(type, params); | 59 addTypeWithParams(type, params); |
| 60 sb.add(">\n"); | 60 sb.write(">\n"); |
| 61 pushTag(type); | 61 pushTag(type); |
| 62 } | 62 } |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Adds given node to result string. | 65 * Adds given node to result string. |
| 66 */ | 66 */ |
| 67 void openAndCloseNode(Node node, String type, [Map params]) { | 67 void openAndCloseNode(Node node, String type, [Map params]) { |
| 68 if (params == null) params = new Map(); | 68 if (params == null) params = new Map(); |
| 69 addCurrentIndent(); | 69 addCurrentIndent(); |
| 70 sb.add("<"); | 70 sb.write("<"); |
| 71 addBeginAndEndTokensToParams(node, params); | 71 addBeginAndEndTokensToParams(node, params); |
| 72 addTypeWithParams(type, params); | 72 addTypeWithParams(type, params); |
| 73 sb.add("/>\n"); | 73 sb.write("/>\n"); |
| 74 } | 74 } |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * Closes current node type. | 77 * Closes current node type. |
| 78 */ | 78 */ |
| 79 void closeNode() { | 79 void closeNode() { |
| 80 String tag = popTag(); | 80 String tag = popTag(); |
| 81 addCurrentIndent(); | 81 addCurrentIndent(); |
| 82 sb.add("</"); | 82 sb.write("</"); |
| 83 addTypeWithParams(tag); | 83 addTypeWithParams(tag); |
| 84 sb.add(">\n"); | 84 sb.write(">\n"); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void addTypeWithParams(String type, [Map params]) { | 87 void addTypeWithParams(String type, [Map params]) { |
| 88 if (params == null) params = new Map(); | 88 if (params == null) params = new Map(); |
| 89 sb.add("${type}"); | 89 sb.write("${type}"); |
| 90 params.forEach((k, v) { | 90 params.forEach((k, v) { |
| 91 String value; | 91 String value; |
| 92 if (v != null) { | 92 if (v != null) { |
| 93 value = v | 93 value = v |
| 94 .replaceAll("<", "<") | 94 .replaceAll("<", "<") |
| 95 .replaceAll(">", ">") | 95 .replaceAll(">", ">") |
| 96 .replaceAll('"', "'"); | 96 .replaceAll('"', "'"); |
| 97 } else { | 97 } else { |
| 98 value = "[null]"; | 98 value = "[null]"; |
| 99 } | 99 } |
| 100 sb.add(' $k="$value"'); | 100 sb.write(' $k="$value"'); |
| 101 }); | 101 }); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void addCurrentIndent() { | 104 void addCurrentIndent() { |
| 105 tagStack.forEach((_) { sb.add(INDENT); }); | 105 tagStack.forEach((_) { sb.write(INDENT); }); |
| 106 } | 106 } |
| 107 | 107 |
| 108 /** | 108 /** |
| 109 * Pretty-prints given node tree into string. | 109 * Pretty-prints given node tree into string. |
| 110 */ | 110 */ |
| 111 static String prettyPrint(Node node) { | 111 static String prettyPrint(Node node) { |
| 112 var p = new PrettyPrinter(); | 112 var p = new PrettyPrinter(); |
| 113 node.accept(p); | 113 node.accept(p); |
| 114 return p.sb.toString(); | 114 return p.sb.toString(); |
| 115 } | 115 } |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 closeNode(); | 311 closeNode(); |
| 312 } | 312 } |
| 313 | 313 |
| 314 visitScriptTag(ScriptTag node) { | 314 visitScriptTag(ScriptTag node) { |
| 315 visitNodeWithChildren(node, "ScriptTag"); | 315 visitNodeWithChildren(node, "ScriptTag"); |
| 316 } | 316 } |
| 317 | 317 |
| 318 visitChildNode(Node node, String fieldName) { | 318 visitChildNode(Node node, String fieldName) { |
| 319 if (node == null) return; | 319 if (node == null) return; |
| 320 addCurrentIndent(); | 320 addCurrentIndent(); |
| 321 sb.add("<$fieldName>\n"); | 321 sb.write("<$fieldName>\n"); |
| 322 pushTag(fieldName); | 322 pushTag(fieldName); |
| 323 node.accept(this); | 323 node.accept(this); |
| 324 popTag(); | 324 popTag(); |
| 325 addCurrentIndent(); | 325 addCurrentIndent(); |
| 326 sb.add("</$fieldName>\n"); | 326 sb.write("</$fieldName>\n"); |
| 327 } | 327 } |
| 328 | 328 |
| 329 openSendNodeWithFields(Send node, String type) { | 329 openSendNodeWithFields(Send node, String type) { |
| 330 openNode(node, type, { | 330 openNode(node, type, { |
| 331 "isPrefix" : "${node.isPrefix}", | 331 "isPrefix" : "${node.isPrefix}", |
| 332 "isPostfix" : "${node.isPostfix}", | 332 "isPostfix" : "${node.isPostfix}", |
| 333 "isIndex" : "${node.isIndex}" | 333 "isIndex" : "${node.isIndex}" |
| 334 }); | 334 }); |
| 335 visitChildNode(node.receiver, "receiver"); | 335 visitChildNode(node.receiver, "receiver"); |
| 336 visitChildNode(node.selector, "selector"); | 336 visitChildNode(node.selector, "selector"); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 } | 471 } |
| 472 | 472 |
| 473 visitStringNode(StringNode node) { | 473 visitStringNode(StringNode node) { |
| 474 unimplemented('visitNode', node: node); | 474 unimplemented('visitNode', node: node); |
| 475 } | 475 } |
| 476 | 476 |
| 477 unimplemented(String message, {Node node}) { | 477 unimplemented(String message, {Node node}) { |
| 478 throw message; | 478 throw message; |
| 479 } | 479 } |
| 480 } | 480 } |
| OLD | NEW |