1 | using System; |
---|
2 | using EventHandlerList = System.ComponentModel.EventHandlerList; |
---|
3 | |
---|
4 | using BitSet = antlr.collections.impl.BitSet; |
---|
5 | using AST = antlr.collections.AST; |
---|
6 | using ASTArray = antlr.collections.impl.ASTArray; |
---|
7 | using antlr.debug; |
---|
8 | |
---|
9 | using MessageListener = antlr.debug.MessageListener; |
---|
10 | using ParserListener = antlr.debug.ParserListener; |
---|
11 | using ParserMatchListener = antlr.debug.ParserMatchListener; |
---|
12 | using ParserTokenListener = antlr.debug.ParserTokenListener; |
---|
13 | using SemanticPredicateListener = antlr.debug.SemanticPredicateListener; |
---|
14 | using SyntacticPredicateListener = antlr.debug.SyntacticPredicateListener; |
---|
15 | using TraceListener = antlr.debug.TraceListener; |
---|
16 | |
---|
17 | /* |
---|
18 | private Vector messageListeners; |
---|
19 | private Vector newLineListeners; |
---|
20 | private Vector matchListeners; |
---|
21 | private Vector tokenListeners; |
---|
22 | private Vector semPredListeners; |
---|
23 | private Vector synPredListeners; |
---|
24 | private Vector traceListeners; |
---|
25 | */ |
---|
26 | |
---|
27 | namespace antlr |
---|
28 | { |
---|
29 | /*ANTLR Translator Generator |
---|
30 | * Project led by Terence Parr at http://www.jGuru.com |
---|
31 | * Software rights: http://www.antlr.org/license.html |
---|
32 | * |
---|
33 | * $Id:$ |
---|
34 | */ |
---|
35 | |
---|
36 | // |
---|
37 | // ANTLR C# Code Generator by Micheal Jordan |
---|
38 | // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com |
---|
39 | // Anthony Oguntimehin |
---|
40 | // |
---|
41 | // With many thanks to Eric V. Smith from the ANTLR list. |
---|
42 | // |
---|
43 | |
---|
44 | public abstract class Parser : IParserDebugSubject |
---|
45 | { |
---|
46 | // Used to store event delegates |
---|
47 | private EventHandlerList events_ = new EventHandlerList(); |
---|
48 | |
---|
49 | protected internal EventHandlerList Events |
---|
50 | { |
---|
51 | get { return events_; } |
---|
52 | } |
---|
53 | |
---|
54 | // The unique keys for each event that Parser [objects] can generate |
---|
55 | internal static readonly object EnterRuleEventKey = new object(); |
---|
56 | internal static readonly object ExitRuleEventKey = new object(); |
---|
57 | internal static readonly object DoneEventKey = new object(); |
---|
58 | internal static readonly object ReportErrorEventKey = new object(); |
---|
59 | internal static readonly object ReportWarningEventKey = new object(); |
---|
60 | internal static readonly object NewLineEventKey = new object(); |
---|
61 | internal static readonly object MatchEventKey = new object(); |
---|
62 | internal static readonly object MatchNotEventKey = new object(); |
---|
63 | internal static readonly object MisMatchEventKey = new object(); |
---|
64 | internal static readonly object MisMatchNotEventKey = new object(); |
---|
65 | internal static readonly object ConsumeEventKey = new object(); |
---|
66 | internal static readonly object LAEventKey = new object(); |
---|
67 | internal static readonly object SemPredEvaluatedEventKey = new object(); |
---|
68 | internal static readonly object SynPredStartedEventKey = new object(); |
---|
69 | internal static readonly object SynPredFailedEventKey = new object(); |
---|
70 | internal static readonly object SynPredSucceededEventKey = new object(); |
---|
71 | |
---|
72 | protected internal ParserSharedInputState inputState; |
---|
73 | |
---|
74 | /*Nesting level of registered handlers */ |
---|
75 | // protected int exceptionLevel = 0; |
---|
76 | |
---|
77 | /*Table of token type to token names */ |
---|
78 | protected internal string[] tokenNames; |
---|
79 | |
---|
80 | /*AST return value for a rule is squirreled away here */ |
---|
81 | protected internal AST returnAST; |
---|
82 | |
---|
83 | /*AST support code; parser and treeparser delegate to this object */ |
---|
84 | protected internal ASTFactory astFactory = new ASTFactory(); |
---|
85 | |
---|
86 | private bool ignoreInvalidDebugCalls = false; |
---|
87 | |
---|
88 | /*Used to keep track of indentdepth for traceIn/Out */ |
---|
89 | protected internal int traceDepth = 0; |
---|
90 | |
---|
91 | public Parser() |
---|
92 | { |
---|
93 | inputState = new ParserSharedInputState(); |
---|
94 | } |
---|
95 | |
---|
96 | public Parser(ParserSharedInputState state) |
---|
97 | { |
---|
98 | inputState = state; |
---|
99 | } |
---|
100 | |
---|
101 | /// <summary> |
---|
102 | /// |
---|
103 | /// </summary> |
---|
104 | |
---|
105 | public event TraceEventHandler EnterRule |
---|
106 | { |
---|
107 | add { Events.AddHandler(EnterRuleEventKey, value); } |
---|
108 | remove { Events.RemoveHandler(EnterRuleEventKey, value); } |
---|
109 | } |
---|
110 | |
---|
111 | public event TraceEventHandler ExitRule |
---|
112 | { |
---|
113 | add { Events.AddHandler(ExitRuleEventKey, value); } |
---|
114 | remove { Events.RemoveHandler(ExitRuleEventKey, value); } |
---|
115 | } |
---|
116 | |
---|
117 | public event TraceEventHandler Done |
---|
118 | { |
---|
119 | add { Events.AddHandler(DoneEventKey, value); } |
---|
120 | remove { Events.RemoveHandler(DoneEventKey, value); } |
---|
121 | } |
---|
122 | |
---|
123 | public event MessageEventHandler ErrorReported |
---|
124 | { |
---|
125 | add { Events.AddHandler(ReportErrorEventKey, value); } |
---|
126 | remove { Events.RemoveHandler(ReportErrorEventKey, value); } |
---|
127 | } |
---|
128 | |
---|
129 | public event MessageEventHandler WarningReported |
---|
130 | { |
---|
131 | add { Events.AddHandler(ReportWarningEventKey, value); } |
---|
132 | remove { Events.RemoveHandler(ReportWarningEventKey, value); } |
---|
133 | } |
---|
134 | |
---|
135 | public event MatchEventHandler MatchedToken |
---|
136 | { |
---|
137 | add { Events.AddHandler(MatchEventKey, value); } |
---|
138 | remove { Events.RemoveHandler(MatchEventKey, value); } |
---|
139 | } |
---|
140 | |
---|
141 | public event MatchEventHandler MatchedNotToken |
---|
142 | { |
---|
143 | add { Events.AddHandler(MatchNotEventKey, value); } |
---|
144 | remove { Events.RemoveHandler(MatchNotEventKey, value); } |
---|
145 | } |
---|
146 | |
---|
147 | public event MatchEventHandler MisMatchedToken |
---|
148 | { |
---|
149 | add { Events.AddHandler(MisMatchEventKey, value); } |
---|
150 | remove { Events.RemoveHandler(MisMatchEventKey, value); } |
---|
151 | } |
---|
152 | |
---|
153 | public event MatchEventHandler MisMatchedNotToken |
---|
154 | { |
---|
155 | add { Events.AddHandler(MisMatchNotEventKey, value); } |
---|
156 | remove { Events.RemoveHandler(MisMatchNotEventKey, value); } |
---|
157 | } |
---|
158 | |
---|
159 | public event TokenEventHandler ConsumedToken |
---|
160 | { |
---|
161 | add { Events.AddHandler(ConsumeEventKey, value); } |
---|
162 | remove { Events.RemoveHandler(ConsumeEventKey, value); } |
---|
163 | } |
---|
164 | |
---|
165 | public event TokenEventHandler TokenLA |
---|
166 | { |
---|
167 | add { Events.AddHandler(LAEventKey, value); } |
---|
168 | remove { Events.RemoveHandler(LAEventKey, value); } |
---|
169 | } |
---|
170 | |
---|
171 | public event SemanticPredicateEventHandler SemPredEvaluated |
---|
172 | { |
---|
173 | add { Events.AddHandler(SemPredEvaluatedEventKey, value); } |
---|
174 | remove { Events.RemoveHandler(SemPredEvaluatedEventKey, value); } |
---|
175 | } |
---|
176 | |
---|
177 | public event SyntacticPredicateEventHandler SynPredStarted |
---|
178 | { |
---|
179 | add { Events.AddHandler(SynPredStartedEventKey, value); } |
---|
180 | remove { Events.RemoveHandler(SynPredStartedEventKey, value); } |
---|
181 | } |
---|
182 | |
---|
183 | public event SyntacticPredicateEventHandler SynPredFailed |
---|
184 | { |
---|
185 | add { Events.AddHandler(SynPredFailedEventKey, value); } |
---|
186 | remove { Events.RemoveHandler(SynPredFailedEventKey, value); } |
---|
187 | } |
---|
188 | |
---|
189 | public event SyntacticPredicateEventHandler SynPredSucceeded |
---|
190 | { |
---|
191 | add { Events.AddHandler(SynPredSucceededEventKey, value); } |
---|
192 | remove { Events.RemoveHandler(SynPredSucceededEventKey, value); } |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | public virtual void addMessageListener(MessageListener l) |
---|
197 | { |
---|
198 | if (!ignoreInvalidDebugCalls) |
---|
199 | throw new System.ArgumentException("addMessageListener() is only valid if parser built for debugging"); |
---|
200 | } |
---|
201 | |
---|
202 | public virtual void addParserListener(ParserListener l) |
---|
203 | { |
---|
204 | if (!ignoreInvalidDebugCalls) |
---|
205 | throw new System.ArgumentException("addParserListener() is only valid if parser built for debugging"); |
---|
206 | } |
---|
207 | |
---|
208 | public virtual void addParserMatchListener(ParserMatchListener l) |
---|
209 | { |
---|
210 | if (!ignoreInvalidDebugCalls) |
---|
211 | throw new System.ArgumentException("addParserMatchListener() is only valid if parser built for debugging"); |
---|
212 | } |
---|
213 | |
---|
214 | public virtual void addParserTokenListener(ParserTokenListener l) |
---|
215 | { |
---|
216 | if (!ignoreInvalidDebugCalls) |
---|
217 | throw new System.ArgumentException("addParserTokenListener() is only valid if parser built for debugging"); |
---|
218 | } |
---|
219 | |
---|
220 | public virtual void addSemanticPredicateListener(SemanticPredicateListener l) |
---|
221 | { |
---|
222 | if (!ignoreInvalidDebugCalls) |
---|
223 | throw new System.ArgumentException("addSemanticPredicateListener() is only valid if parser built for debugging"); |
---|
224 | } |
---|
225 | |
---|
226 | public virtual void addSyntacticPredicateListener(SyntacticPredicateListener l) |
---|
227 | { |
---|
228 | if (!ignoreInvalidDebugCalls) |
---|
229 | throw new System.ArgumentException("addSyntacticPredicateListener() is only valid if parser built for debugging"); |
---|
230 | } |
---|
231 | |
---|
232 | public virtual void addTraceListener(TraceListener l) |
---|
233 | { |
---|
234 | if (!ignoreInvalidDebugCalls) |
---|
235 | throw new System.ArgumentException("addTraceListener() is only valid if parser built for debugging"); |
---|
236 | } |
---|
237 | |
---|
238 | /*Get another token object from the token stream */ |
---|
239 | public abstract void consume(); |
---|
240 | /*Consume tokens until one matches the given token */ |
---|
241 | public virtual void consumeUntil(int tokenType) |
---|
242 | { |
---|
243 | while (LA(1) != Token.EOF_TYPE && LA(1) != tokenType) |
---|
244 | { |
---|
245 | consume(); |
---|
246 | } |
---|
247 | } |
---|
248 | /*Consume tokens until one matches the given token set */ |
---|
249 | public virtual void consumeUntil(BitSet bset) |
---|
250 | { |
---|
251 | while (LA(1) != Token.EOF_TYPE && !bset.member(LA(1))) |
---|
252 | { |
---|
253 | consume(); |
---|
254 | } |
---|
255 | } |
---|
256 | protected internal virtual void defaultDebuggingSetup(TokenStream lexer, TokenBuffer tokBuf) |
---|
257 | { |
---|
258 | // by default, do nothing -- we're not debugging |
---|
259 | } |
---|
260 | /*Get the AST return value squirreled away in the parser */ |
---|
261 | public virtual AST getAST() |
---|
262 | { |
---|
263 | return returnAST; |
---|
264 | } |
---|
265 | public virtual ASTFactory getASTFactory() |
---|
266 | { |
---|
267 | return astFactory; |
---|
268 | } |
---|
269 | public virtual string getFilename() |
---|
270 | { |
---|
271 | return inputState.filename; |
---|
272 | } |
---|
273 | |
---|
274 | public virtual ParserSharedInputState getInputState() |
---|
275 | { |
---|
276 | return inputState; |
---|
277 | } |
---|
278 | |
---|
279 | public virtual void setInputState(ParserSharedInputState state) |
---|
280 | { |
---|
281 | inputState = state; |
---|
282 | } |
---|
283 | |
---|
284 | public virtual void resetState() |
---|
285 | { |
---|
286 | traceDepth = 0; |
---|
287 | inputState.reset(); |
---|
288 | } |
---|
289 | |
---|
290 | public virtual string getTokenName(int num) |
---|
291 | { |
---|
292 | return tokenNames[num]; |
---|
293 | } |
---|
294 | public virtual string[] getTokenNames() |
---|
295 | { |
---|
296 | return tokenNames; |
---|
297 | } |
---|
298 | public virtual bool isDebugMode() |
---|
299 | { |
---|
300 | return false; |
---|
301 | } |
---|
302 | /*Return the token type of the ith token of lookahead where i=1 |
---|
303 | * is the current token being examined by the parser (i.e., it |
---|
304 | * has not been matched yet). |
---|
305 | */ |
---|
306 | public abstract int LA(int i); |
---|
307 | /*Return the ith token of lookahead */ |
---|
308 | public abstract IToken LT(int i); |
---|
309 | // Forwarded to TokenBuffer |
---|
310 | public virtual int mark() |
---|
311 | { |
---|
312 | return inputState.input.mark(); |
---|
313 | } |
---|
314 | /*Make sure current lookahead symbol matches token type <tt>t</tt>. |
---|
315 | * Throw an exception upon mismatch, which is catch by either the |
---|
316 | * error handler or by the syntactic predicate. |
---|
317 | */ |
---|
318 | public virtual void match(int t) |
---|
319 | { |
---|
320 | if (LA(1) != t) |
---|
321 | throw new MismatchedTokenException(tokenNames, LT(1), t, false, getFilename()); |
---|
322 | else |
---|
323 | consume(); |
---|
324 | } |
---|
325 | /*Make sure current lookahead symbol matches the given set |
---|
326 | * Throw an exception upon mismatch, which is catch by either the |
---|
327 | * error handler or by the syntactic predicate. |
---|
328 | */ |
---|
329 | public virtual void match(BitSet b) |
---|
330 | { |
---|
331 | if (!b.member(LA(1))) |
---|
332 | throw new MismatchedTokenException(tokenNames, LT(1), b, false, getFilename()); |
---|
333 | else |
---|
334 | consume(); |
---|
335 | } |
---|
336 | public virtual void matchNot(int t) |
---|
337 | { |
---|
338 | if (LA(1) == t) |
---|
339 | throw new MismatchedTokenException(tokenNames, LT(1), t, true, getFilename()); |
---|
340 | else |
---|
341 | consume(); |
---|
342 | } |
---|
343 | |
---|
344 | /// <summary> |
---|
345 | /// @deprecated as of 2.7.2. This method calls System.exit() and writes |
---|
346 | /// directly to stderr, which is usually not appropriate when |
---|
347 | /// a parser is embedded into a larger application. Since the method is |
---|
348 | /// <code>static</code>, it cannot be overridden to avoid these problems. |
---|
349 | /// ANTLR no longer uses this method internally or in generated code. |
---|
350 | /// </summary> |
---|
351 | /// |
---|
352 | [Obsolete("De-activated since version 2.7.2.6 as it cannot be overidden.", true)] |
---|
353 | public static void panic() |
---|
354 | { |
---|
355 | System.Console.Error.WriteLine("Parser: panic"); |
---|
356 | System.Environment.Exit(1); |
---|
357 | } |
---|
358 | |
---|
359 | public virtual void removeMessageListener(MessageListener l) |
---|
360 | { |
---|
361 | if (!ignoreInvalidDebugCalls) |
---|
362 | throw new System.SystemException("removeMessageListener() is only valid if parser built for debugging"); |
---|
363 | } |
---|
364 | public virtual void removeParserListener(ParserListener l) |
---|
365 | { |
---|
366 | if (!ignoreInvalidDebugCalls) |
---|
367 | throw new System.SystemException("removeParserListener() is only valid if parser built for debugging"); |
---|
368 | } |
---|
369 | public virtual void removeParserMatchListener(ParserMatchListener l) |
---|
370 | { |
---|
371 | if (!ignoreInvalidDebugCalls) |
---|
372 | throw new System.SystemException("removeParserMatchListener() is only valid if parser built for debugging"); |
---|
373 | } |
---|
374 | public virtual void removeParserTokenListener(ParserTokenListener l) |
---|
375 | { |
---|
376 | if (!ignoreInvalidDebugCalls) |
---|
377 | throw new System.SystemException("removeParserTokenListener() is only valid if parser built for debugging"); |
---|
378 | } |
---|
379 | public virtual void removeSemanticPredicateListener(SemanticPredicateListener l) |
---|
380 | { |
---|
381 | if (!ignoreInvalidDebugCalls) |
---|
382 | throw new System.ArgumentException("removeSemanticPredicateListener() is only valid if parser built for debugging"); |
---|
383 | } |
---|
384 | public virtual void removeSyntacticPredicateListener(SyntacticPredicateListener l) |
---|
385 | { |
---|
386 | if (!ignoreInvalidDebugCalls) |
---|
387 | throw new System.ArgumentException("removeSyntacticPredicateListener() is only valid if parser built for debugging"); |
---|
388 | } |
---|
389 | public virtual void removeTraceListener(TraceListener l) |
---|
390 | { |
---|
391 | if (!ignoreInvalidDebugCalls) |
---|
392 | throw new System.SystemException("removeTraceListener() is only valid if parser built for debugging"); |
---|
393 | } |
---|
394 | |
---|
395 | /*Parser error-reporting function can be overridden in subclass */ |
---|
396 | public virtual void reportError(RecognitionException ex) |
---|
397 | { |
---|
398 | Console.Error.WriteLine(ex); |
---|
399 | } |
---|
400 | |
---|
401 | /*Parser error-reporting function can be overridden in subclass */ |
---|
402 | public virtual void reportError(string s) |
---|
403 | { |
---|
404 | if (getFilename() == null) |
---|
405 | { |
---|
406 | Console.Error.WriteLine("error: " + s); |
---|
407 | } |
---|
408 | else |
---|
409 | { |
---|
410 | Console.Error.WriteLine(getFilename() + ": error: " + s); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | /*Parser warning-reporting function can be overridden in subclass */ |
---|
415 | public virtual void reportWarning(string s) |
---|
416 | { |
---|
417 | if (getFilename() == null) |
---|
418 | { |
---|
419 | Console.Error.WriteLine("warning: " + s); |
---|
420 | } |
---|
421 | else |
---|
422 | { |
---|
423 | Console.Error.WriteLine(getFilename() + ": warning: " + s); |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | public virtual void recover(RecognitionException ex, BitSet tokenSet) |
---|
428 | { |
---|
429 | consume(); |
---|
430 | consumeUntil(tokenSet); |
---|
431 | } |
---|
432 | |
---|
433 | public virtual void rewind(int pos) |
---|
434 | { |
---|
435 | inputState.input.rewind(pos); |
---|
436 | } |
---|
437 | |
---|
438 | /// <summary> |
---|
439 | /// Specify an object with support code (shared by Parser and TreeParser. |
---|
440 | /// Normally, the programmer does not play with this, using |
---|
441 | /// <see cref="setASTNodeClass"/> instead. |
---|
442 | /// </summary> |
---|
443 | /// <param name="f"></param> |
---|
444 | public virtual void setASTFactory(ASTFactory f) |
---|
445 | { |
---|
446 | astFactory = f; |
---|
447 | } |
---|
448 | |
---|
449 | /// <summary> |
---|
450 | /// Specify the type of node to create during tree building. |
---|
451 | /// </summary> |
---|
452 | /// <param name="cl">Fully qualified AST Node type name.</param> |
---|
453 | public virtual void setASTNodeClass(string cl) |
---|
454 | { |
---|
455 | astFactory.setASTNodeType(cl); |
---|
456 | } |
---|
457 | |
---|
458 | /// <summary> |
---|
459 | /// Specify the type of node to create during tree building. |
---|
460 | /// use <see cref="setASTNodeClass"/> now to be consistent with |
---|
461 | /// Token Object Type accessor. |
---|
462 | /// </summary> |
---|
463 | /// <param name="nodeType">Fully qualified AST Node type name.</param> |
---|
464 | [Obsolete("Replaced by setASTNodeClass(string) since version 2.7.1", true)] |
---|
465 | public virtual void setASTNodeType(string nodeType) |
---|
466 | { |
---|
467 | setASTNodeClass(nodeType); |
---|
468 | } |
---|
469 | |
---|
470 | public virtual void setDebugMode(bool debugMode) |
---|
471 | { |
---|
472 | if (!ignoreInvalidDebugCalls) |
---|
473 | throw new System.SystemException("setDebugMode() only valid if parser built for debugging"); |
---|
474 | } |
---|
475 | public virtual void setFilename(string f) |
---|
476 | { |
---|
477 | inputState.filename = f; |
---|
478 | } |
---|
479 | public virtual void setIgnoreInvalidDebugCalls(bool Value) |
---|
480 | { |
---|
481 | ignoreInvalidDebugCalls = Value; |
---|
482 | } |
---|
483 | /*Set or change the input token buffer */ |
---|
484 | public virtual void setTokenBuffer(TokenBuffer t) |
---|
485 | { |
---|
486 | inputState.input = t; |
---|
487 | } |
---|
488 | |
---|
489 | public virtual void traceIndent() |
---|
490 | { |
---|
491 | for (int i = 0; i < traceDepth; i++) |
---|
492 | Console.Out.Write(" "); |
---|
493 | } |
---|
494 | public virtual void traceIn(string rname) |
---|
495 | { |
---|
496 | traceDepth += 1; |
---|
497 | traceIndent(); |
---|
498 | Console.Out.WriteLine("> " + rname + "; LA(1)==" + LT(1).getText() + ((inputState.guessing > 0)?" [guessing]":"")); |
---|
499 | } |
---|
500 | public virtual void traceOut(string rname) |
---|
501 | { |
---|
502 | traceIndent(); |
---|
503 | Console.Out.WriteLine("< " + rname + "; LA(1)==" + LT(1).getText() + ((inputState.guessing > 0)?" [guessing]":"")); |
---|
504 | traceDepth -= 1; |
---|
505 | } |
---|
506 | } |
---|
507 | } |
---|