OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino 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 library servicec.listener; | |
6 | |
7 import 'package:compiler/src/tokens/token.dart' show | |
8 ErrorToken, | |
9 Token; | |
10 | |
11 import 'errors.dart' show | |
12 ErrorTag; | |
13 | |
14 /// Identity listener: methods just propagate the argument. | |
15 abstract class Listener { | |
16 Token beginCompilationUnit(Token tokens) { | |
17 return tokens; | |
18 } | |
19 | |
20 Token endCompilationUnit(Token tokens, int count) { | |
21 return tokens; | |
22 } | |
23 | |
24 Token beginTopLevel(Token tokens) { | |
25 return tokens; | |
26 } | |
27 | |
28 Token endTopLevel(Token tokens) { | |
29 return tokens; | |
30 } | |
31 | |
32 Token beginService(Token tokens) { | |
33 return tokens; | |
34 } | |
35 | |
36 Token endService(Token tokens, int count) { | |
37 return tokens; | |
38 } | |
39 | |
40 Token beginStruct(Token tokens) { | |
41 return tokens; | |
42 } | |
43 | |
44 Token endStruct(Token tokens, int count) { | |
45 return tokens; | |
46 } | |
47 | |
48 Token handleIdentifier(Token tokens) { | |
49 return tokens; | |
50 } | |
51 | |
52 Token beginFunction(Token tokens) { | |
53 return tokens; | |
54 } | |
55 | |
56 Token endFunction(Token tokens, int count) { | |
57 return tokens; | |
58 } | |
59 | |
60 Token beginUnion(Token tokens) { | |
61 return tokens; | |
62 } | |
63 | |
64 Token endUnion(Token tokens, int count) { | |
65 return tokens; | |
66 } | |
67 | |
68 Token beginField(Token tokens) { | |
69 return tokens; | |
70 } | |
71 | |
72 Token endField(Token tokens) { | |
73 return tokens; | |
74 } | |
75 | |
76 Token beginType(Token tokens) { | |
77 return tokens; | |
78 } | |
79 | |
80 Token endType(Token tokens) { | |
81 return tokens; | |
82 } | |
83 | |
84 Token handleSimpleType(Token tokens) { | |
85 return tokens; | |
86 } | |
87 | |
88 Token handlePointerType(Token tokens) { | |
89 return tokens; | |
90 } | |
91 | |
92 Token handleListType(Token tokens) { | |
93 return tokens; | |
94 } | |
95 | |
96 Token beginFormal(Token tokens) { | |
97 return tokens; | |
98 } | |
99 | |
100 Token endFormal(Token tokens) { | |
101 return tokens; | |
102 } | |
103 | |
104 Token expectedTopLevel(Token tokens); | |
105 | |
106 Token expectedIdentifier(Token tokens); | |
107 | |
108 Token expectedType(Token tokens); | |
109 | |
110 Token expected(String string, Token tokens); | |
111 } | |
112 | |
113 enum LogLevel { DEBUG, INFO } | |
114 | |
115 /// Used for debugging other listeners. | |
116 class DebugListener implements Listener { | |
117 Listener debugSubject; | |
118 LogLevel logLevel; | |
119 int scope; | |
120 | |
121 DebugListener(this.debugSubject, [this.logLevel = LogLevel.DEBUG]) | |
122 : scope = 0; | |
123 | |
124 void beginScope() { ++scope; } | |
125 void endScope() { --scope; } | |
126 | |
127 void log(String string) { | |
128 print("${" " * scope}$string"); | |
129 } | |
130 | |
131 void logBeginScope(String nodeName) { | |
132 String prefix = logLevel == LogLevel.INFO ? "begin " : ""; | |
133 log("$prefix$nodeName"); | |
134 beginScope(); | |
135 } | |
136 | |
137 void logEndScope(String nodeName, [String summary = ""]) { | |
138 if (logLevel == LogLevel.DEBUG) { | |
139 if (summary != "") log("$summary"); | |
140 endScope(); | |
141 } else { | |
142 endScope(); | |
143 log("end $nodeName; $summary"); | |
144 } | |
145 } | |
146 | |
147 Token beginCompilationUnit(Token tokens) { | |
148 logBeginScope("unit"); | |
149 return debugSubject.beginCompilationUnit(tokens); | |
150 } | |
151 | |
152 Token endCompilationUnit(Token tokens, int count) { | |
153 logEndScope("unit", "top-level declarations count = $count"); | |
154 return debugSubject.endCompilationUnit(tokens, count); | |
155 } | |
156 | |
157 Token beginTopLevel(Token tokens) { | |
158 logBeginScope("top-level declaration"); | |
159 return debugSubject.beginTopLevel(tokens); | |
160 } | |
161 | |
162 Token endTopLevel(Token tokens) { | |
163 logEndScope("top-level declaration"); | |
164 return debugSubject.endTopLevel(tokens); | |
165 } | |
166 | |
167 Token beginService(Token tokens) { | |
168 logBeginScope("service"); | |
169 return debugSubject.beginService(tokens); | |
170 } | |
171 | |
172 Token endService(Token tokens, int count) { | |
173 logEndScope("service", "functions count = $count"); | |
174 return debugSubject.endService(tokens, count); | |
175 } | |
176 | |
177 Token beginStruct(Token tokens) { | |
178 logBeginScope("struct"); | |
179 return debugSubject.beginStruct(tokens); | |
180 } | |
181 | |
182 Token endStruct(Token tokens, int count) { | |
183 logEndScope("struct", "fields count = $count"); | |
184 return debugSubject.endStruct(tokens, count); | |
185 } | |
186 | |
187 Token handleIdentifier(Token tokens) { | |
188 bool valid = tokens is! ErrorToken && tokens != null; | |
189 String identifierValue = valid ? " [${tokens.value}]" : ""; | |
190 log("indentifier$identifierValue"); | |
191 return debugSubject.handleIdentifier(tokens); | |
192 } | |
193 | |
194 Token beginFunction(Token tokens) { | |
195 logBeginScope("function"); | |
196 return debugSubject.beginFunction(tokens); | |
197 } | |
198 | |
199 Token endFunction(Token tokens, int count) { | |
200 logEndScope("function", "formal parameters count = $count"); | |
201 return debugSubject.endFunction(tokens, count); | |
202 } | |
203 | |
204 Token beginUnion(Token tokens) { | |
205 logBeginScope("union"); | |
206 return debugSubject.beginUnion(tokens); | |
207 } | |
208 | |
209 Token endUnion(Token tokens, int count) { | |
210 logEndScope("union", "fields count = $count"); | |
211 return debugSubject.endUnion(tokens, count); | |
212 } | |
213 | |
214 Token beginField(Token tokens) { | |
215 logBeginScope("field"); | |
216 return debugSubject.beginField(tokens); | |
217 } | |
218 | |
219 Token endField(Token tokens) { | |
220 logEndScope("field"); | |
221 return debugSubject.endField(tokens); | |
222 } | |
223 | |
224 Token beginType(Token tokens) { | |
225 logBeginScope("type"); | |
226 return debugSubject.beginType(tokens); | |
227 } | |
228 | |
229 Token endType(Token tokens) { | |
230 logEndScope("type"); | |
231 return debugSubject.endType(tokens); | |
232 } | |
233 | |
234 Token handleSimpleType(Token tokens) { | |
235 log("Simple type"); | |
236 return debugSubject.handleSimpleType(tokens); | |
237 } | |
238 | |
239 Token handlePointerType(Token tokens) { | |
240 log("Pointer type"); | |
241 return debugSubject.handlePointerType(tokens); | |
242 } | |
243 | |
244 Token handleListType(Token tokens) { | |
245 log("List type"); | |
246 return debugSubject.handleListType(tokens); | |
247 } | |
248 | |
249 Token beginFormal(Token tokens) { | |
250 logBeginScope("formal parameter"); | |
251 return debugSubject.beginFormal(tokens); | |
252 } | |
253 | |
254 Token endFormal(Token tokens) { | |
255 logEndScope("formal parameter"); | |
256 return debugSubject.endFormal(tokens); | |
257 } | |
258 | |
259 Token expectedTopLevel(Token tokens) { | |
260 log("error: $tokens is not a top-level declaration"); | |
261 return debugSubject.expectedTopLevel(tokens); | |
262 } | |
263 | |
264 Token expectedIdentifier(Token tokens) { | |
265 log("error: $tokens is not an identifier"); | |
266 return debugSubject.expectedIdentifier(tokens); | |
267 } | |
268 | |
269 Token expectedType(Token tokens) { | |
270 log("error: $tokens is not a type"); | |
271 return debugSubject.expectedType(tokens); | |
272 } | |
273 | |
274 Token expected(String string, Token tokens) { | |
275 log("error: $tokens is not the symbol $string"); | |
276 return debugSubject.expected(string, tokens); | |
277 } | |
278 } | |
OLD | NEW |