OLD | NEW |
1 // Copyright (c) 2011, 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 |
| 5 part of csslib.visitor; |
| 6 |
4 /** | 7 /** |
5 * The base type for all nodes in a dart abstract syntax tree. | 8 * The base type for all nodes in a CSS abstract syntax tree. |
6 */ | 9 */ |
7 class ASTNode { | 10 abstract class TreeNode { |
8 /** The source code this [ASTNode] represents. */ | 11 /** The source code this [TreeNode] represents. */ |
9 SourceSpan span; | 12 Span span; |
10 | 13 |
11 ASTNode(this.span) {} | 14 TreeNode(this.span) {} |
12 | 15 |
13 /** Classic double-dispatch visitor for implementing passes. */ | 16 /** Classic double-dispatch visitor for implementing passes. */ |
14 abstract visit(TreeVisitor visitor); | 17 visit(VisitorBase visitor); |
15 | 18 |
16 /** A multiline string showing the node and its children. */ | 19 /** A multiline string showing the node and its children. */ |
17 String toDebugString() { | 20 String toDebugString() { |
18 var to = new TreeOutput(); | 21 var to = new TreeOutput(); |
19 var tp = new TreePrinter(to); | 22 var tp = new _TreePrinter(to, true); |
20 this.visit(tp); | 23 this.visit(tp); |
21 return to.buf.toString(); | 24 return to.buf.toString(); |
22 } | 25 } |
23 } | 26 } |
24 | 27 |
25 // TODO(jimhug): Clean-up and factor out of core. | 28 /** The base type for expressions. */ |
| 29 abstract class Expression extends TreeNode { |
| 30 Expression(Span span): super(span); |
| 31 } |
| 32 |
26 /** Simple class to provide a textual dump of trees for debugging. */ | 33 /** Simple class to provide a textual dump of trees for debugging. */ |
27 class TreeOutput { | 34 class TreeOutput { |
28 int depth; | 35 int depth = 0; |
29 StringBuffer buf; | 36 final StringBuffer buf = new StringBuffer(); |
30 | |
31 var printer; | 37 var printer; |
32 | 38 |
33 static void dump(ASTNode node) { | |
34 var o = new TreeOutput(); | |
35 node.visit(new TreePrinter(o)); | |
36 print(o.buf); | |
37 } | |
38 | |
39 TreeOutput(): this.depth = 0, this.buf = new StringBuffer() { | |
40 } | |
41 | |
42 void write(String s) { | 39 void write(String s) { |
43 for (int i=0; i < depth; i++) { | 40 for (int i=0; i < depth; i++) { |
44 buf.write(' '); | 41 buf.write(' '); |
45 } | 42 } |
46 buf.write(s); | 43 buf.write(s); |
47 } | 44 } |
48 | 45 |
49 void writeln(String s) { | 46 void writeln(String s) { |
50 buf.writeln(s); | 47 write(s); |
| 48 buf.write('\n'); |
51 } | 49 } |
52 | 50 |
53 void heading(String name, span) { | 51 void heading(String name, [span]) { |
54 write(name); | 52 write(name); |
55 buf.writeln(' (${span.locationText})'); | 53 if (span != null) { |
| 54 buf.write(' (${span.getLocationMessage('')})'); |
| 55 } |
| 56 buf.write('\n'); |
56 } | 57 } |
57 | 58 |
58 String toValue(value) { | 59 String toValue(value) { |
59 if (value == null) return 'null'; | 60 if (value == null) return 'null'; |
60 else if (value is Identifier) return value.name; | 61 else if (value is Identifier) return value.name; |
61 else return value.toString(); | 62 else return value.toString(); |
62 } | 63 } |
63 | 64 |
64 void writeNode(String label, ASTNode node) { | 65 void writeNode(String label, TreeNode node) { |
65 write(label + ': '); | 66 write('${label}: '); |
66 depth += 1; | 67 depth += 1; |
67 if (node != null) node.visit(printer); | 68 if (node != null) node.visit(printer); |
68 else writeln('null'); | 69 else writeln('null'); |
69 depth -= 1; | 70 depth -= 1; |
70 } | 71 } |
71 | 72 |
72 void writeValue(String label, value) { | 73 void writeValue(String label, value) { |
73 var v = toValue(value); | 74 var v = toValue(value); |
74 writeln('${label}: ${v}'); | 75 writeln('${label}: ${v}'); |
75 } | 76 } |
76 | 77 |
77 void writeList(String label, List list) { | 78 void writeList(String label, List list) { |
78 write(label + ': '); | 79 write('${label}: '); |
79 if (list == null) { | 80 if (list == null) { |
80 buf.writeln('null'); | 81 buf.write('null'); |
| 82 buf.write('\n'); |
81 } else { | 83 } else { |
82 for (var item in list) { | 84 for (var item in list) { |
83 buf.write(item.toString()); | 85 buf.write(item.toString()); |
84 buf.write(', '); | 86 buf.write(', '); |
85 } | 87 } |
86 buf.write('\n'); | 88 buf.write('\n'); |
87 } | 89 } |
88 } | 90 } |
89 | 91 |
90 void writeNodeList(String label, List list) { | 92 void writeNodeList(String label, List list) { |
91 writeln('${label} ['); | 93 writeln('${label} ['); |
92 if (list != null) { | 94 if (list != null) { |
93 depth += 1; | 95 depth += 1; |
94 for (var node in list) { | 96 for (var node in list) { |
95 if (node != null) { | 97 if (node != null) { |
96 node.visit(printer); | 98 node.visit(printer); |
97 } else { | 99 } else { |
98 writeln('null'); | 100 writeln('null'); |
99 } | 101 } |
100 } | 102 } |
101 depth -= 1; | 103 depth -= 1; |
102 writeln(']'); | 104 writeln(']'); |
103 } | 105 } |
104 } | 106 } |
| 107 |
| 108 String toString() => buf.toString(); |
105 } | 109 } |
OLD | NEW |