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