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 |
11 * .dart file. | 11 * .dart file. |
12 */ | 12 */ |
13 class PrettyPrinter extends Indentation implements Visitor { | 13 class PrettyPrinter extends Indentation with Tagging<Node> implements Visitor { |
14 | 14 @override |
15 StringBuffer sb; | 15 void addDefaultParameters(Node node, Map params) { |
16 Link<String> tagStack; | |
17 | |
18 PrettyPrinter() : | |
19 sb = new StringBuffer(), | |
20 tagStack = const Link<String>(); | |
21 | |
22 void pushTag(String tag) { | |
23 tagStack = tagStack.prepend(tag); | |
24 indentMore(); | |
25 } | |
26 | |
27 String popTag() { | |
28 assert(!tagStack.isEmpty); | |
29 String tag = tagStack.head; | |
30 tagStack = tagStack.tail; | |
31 indentLess(); | |
32 return tag; | |
33 } | |
34 | |
35 /** | |
36 * Adds given string to result string. | |
37 */ | |
38 void add(String string) { | |
39 sb.write(string); | |
40 } | |
41 | |
42 void addBeginAndEndTokensToParams(Node node, Map params) { | |
43 params['getBeginToken'] = tokenToStringOrNull(node.getBeginToken()); | 16 params['getBeginToken'] = tokenToStringOrNull(node.getBeginToken()); |
44 params['getEndToken'] = tokenToStringOrNull(node.getEndToken()); | 17 params['getEndToken'] = tokenToStringOrNull(node.getEndToken()); |
45 } | 18 } |
46 | 19 |
47 /** | 20 String valueToString(var value) { |
48 * Adds given node type to result string. | 21 if (value is Token) { |
49 * The method "opens" the node, meaning that all output after calling | 22 return value.value; |
50 * this method and before calling closeNode() will represent contents | 23 } |
51 * of given node. | 24 return value; |
52 */ | |
53 void openNode(Node node, String type, [Map params]) { | |
54 if (params == null) params = new Map(); | |
55 addCurrentIndent(); | |
56 sb.write("<"); | |
57 addBeginAndEndTokensToParams(node, params); | |
58 addTypeWithParams(type, params); | |
59 sb.write(">\n"); | |
60 pushTag(type); | |
61 } | 25 } |
62 | 26 |
63 /** | 27 /** |
64 * Adds given node to result string. | |
65 */ | |
66 void openAndCloseNode(Node node, String type, [Map params]) { | |
67 if (params == null) params = new Map(); | |
68 addCurrentIndent(); | |
69 sb.write("<"); | |
70 addBeginAndEndTokensToParams(node, params); | |
71 addTypeWithParams(type, params); | |
72 sb.write("/>\n"); | |
73 } | |
74 | |
75 /** | |
76 * Closes current node type. | |
77 */ | |
78 void closeNode() { | |
79 String tag = popTag(); | |
80 addCurrentIndent(); | |
81 sb.write("</"); | |
82 addTypeWithParams(tag); | |
83 sb.write(">\n"); | |
84 } | |
85 | |
86 void addTypeWithParams(String type, [Map params]) { | |
87 if (params == null) params = new Map(); | |
88 sb.write("${type}"); | |
89 params.forEach((k, v) { | |
90 String value; | |
91 if (v != null) { | |
92 var str = v; | |
93 if (v is Token) str = v.value; | |
94 value = str | |
95 .replaceAll("<", "<") | |
96 .replaceAll(">", ">") | |
97 .replaceAll('"', "'"); | |
98 } else { | |
99 value = "[null]"; | |
100 } | |
101 sb.write(' $k="$value"'); | |
102 }); | |
103 } | |
104 | |
105 void addCurrentIndent() { | |
106 sb.write(indentation); | |
107 } | |
108 | |
109 /** | |
110 * Pretty-prints given node tree into string. | 28 * Pretty-prints given node tree into string. |
111 */ | 29 */ |
112 static String prettyPrint(Node node) { | 30 static String prettyPrint(Node node) { |
113 var p = new PrettyPrinter(); | 31 var p = new PrettyPrinter(); |
114 node.accept(p); | 32 node.accept(p); |
115 return p.sb.toString(); | 33 return p.sb.toString(); |
116 } | 34 } |
117 | 35 |
118 visitNodeWithChildren(Node node, String type) { | 36 visitNodeWithChildren(Node node, String type) { |
119 openNode(node, type); | 37 openNode(node, type); |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 } | 450 } |
533 | 451 |
534 visitGotoStatement(GotoStatement node) { | 452 visitGotoStatement(GotoStatement node) { |
535 unimplemented('visitNode', node: node); | 453 unimplemented('visitNode', node: node); |
536 } | 454 } |
537 | 455 |
538 unimplemented(String message, {Node node}) { | 456 unimplemented(String message, {Node node}) { |
539 throw message; | 457 throw message; |
540 } | 458 } |
541 } | 459 } |
OLD | NEW |