Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Rook / Castle.Rook.Compiler / Parser / antlr / debug / ScannerEventSupport.cs
blob790ca4ea8a40a31416abaa5f6371cd3c2ef1ee0e
1 namespace antlr.debug
3 using System;
4 using System.Reflection;
5 using Hashtable = System.Collections.Hashtable;
6 using ArrayList = System.Collections.ArrayList;
8 using antlr.collections.impl;
11 /// <summary>A class to assist in firing parser events
12 /// NOTE: I intentionally _did_not_ synchronize the event firing and
13 /// add/remove listener methods. This is because the add/remove should
14 /// _only_ be called by the parser at its start/end, and the _same_thread_
15 /// should be performing the parsing. This should help performance a tad...
16 /// </summary>
17 public class ScannerEventSupport
19 private object source;
20 private Hashtable listeners;
21 private MatchEventArgs matchEvent;
22 private MessageEventArgs messageEvent;
23 private TokenEventArgs tokenEvent;
24 private SemanticPredicateEventArgs semPredEvent;
25 private SyntacticPredicateEventArgs synPredEvent;
26 private TraceEventArgs traceEvent;
27 private NewLineEventArgs newLineEvent;
28 //private ParserController controller;
30 private int ruleDepth = 0;
33 public ScannerEventSupport(object source)
35 matchEvent = new MatchEventArgs();
36 messageEvent = new MessageEventArgs();
37 tokenEvent = new TokenEventArgs();
38 traceEvent = new TraceEventArgs();
39 semPredEvent = new SemanticPredicateEventArgs();
40 synPredEvent = new SyntacticPredicateEventArgs();
41 newLineEvent = new NewLineEventArgs();
42 listeners = new Hashtable();
43 this.source = source;
46 public virtual void checkController()
48 //if (controller != null)
49 // controller.checkBreak();
52 public virtual void addDoneListener(Listener l)
54 ((CharScanner)source).Done += new TraceEventHandler(l.doneParsing);
55 listeners[l] = l;
57 public virtual void addMessageListener(MessageListener l)
59 ((CharScanner)source).ErrorReported += new MessageEventHandler(l.reportError);
60 ((CharScanner)source).WarningReported += new MessageEventHandler(l.reportWarning);
61 addDoneListener(l);
63 public virtual void addNewLineListener(NewLineListener l)
65 ((CharScanner)source).HitNewLine += new NewLineEventHandler(l.hitNewLine);
66 addDoneListener(l);
68 public virtual void addParserListener(ParserListener l)
70 if (l is ParserController)
72 //((ParserController) l).ParserEventSupport = this;
73 //controller = (ParserController) l;
75 addParserMatchListener(l);
76 addParserTokenListener(l);
78 addMessageListener(l);
79 addTraceListener(l);
80 addSemanticPredicateListener(l);
81 addSyntacticPredicateListener(l);
83 public virtual void addParserMatchListener(ParserMatchListener l)
85 ((CharScanner)source).MatchedChar += new MatchEventHandler(l.parserMatch);
86 ((CharScanner)source).MatchedNotChar += new MatchEventHandler(l.parserMatchNot);
87 ((CharScanner)source).MisMatchedChar += new MatchEventHandler(l.parserMismatch);
88 ((CharScanner)source).MisMatchedNotChar += new MatchEventHandler(l.parserMismatchNot);
89 addDoneListener(l);
91 public virtual void addParserTokenListener(ParserTokenListener l)
93 ((CharScanner)source).ConsumedChar += new TokenEventHandler(l.parserConsume);
94 ((CharScanner)source).CharLA += new TokenEventHandler(l.parserLA);
95 addDoneListener(l);
97 public virtual void addSemanticPredicateListener(SemanticPredicateListener l)
99 ((CharScanner)source).SemPredEvaluated += new SemanticPredicateEventHandler(l.semanticPredicateEvaluated);
100 addDoneListener(l);
102 public virtual void addSyntacticPredicateListener(SyntacticPredicateListener l)
104 ((CharScanner)source).SynPredStarted += new SyntacticPredicateEventHandler(l.syntacticPredicateStarted);
105 ((CharScanner)source).SynPredFailed += new SyntacticPredicateEventHandler(l.syntacticPredicateFailed);
106 ((CharScanner)source).SynPredSucceeded += new SyntacticPredicateEventHandler(l.syntacticPredicateSucceeded);
107 addDoneListener(l);
109 public virtual void addTraceListener(TraceListener l)
111 ((CharScanner)source).EnterRule += new TraceEventHandler(l.enterRule);
112 ((CharScanner)source).ExitRule += new TraceEventHandler(l.exitRule);
113 addDoneListener(l);
115 public virtual void fireConsume(int c)
117 TokenEventHandler eventDelegate = (TokenEventHandler)((CharScanner)source).Events[Parser.LAEventKey];
118 if (eventDelegate != null)
120 tokenEvent.setValues(TokenEventArgs.CONSUME, 1, c);
121 eventDelegate(source, tokenEvent);
123 checkController();
125 public virtual void fireDoneParsing()
127 TraceEventHandler eventDelegate = (TraceEventHandler)((CharScanner)source).Events[Parser.DoneEventKey];
128 if (eventDelegate != null)
130 traceEvent.setValues(TraceEventArgs.DONE_PARSING, 0, 0, 0);
131 eventDelegate(source, traceEvent);
133 checkController();
135 public virtual void fireEnterRule(int ruleNum, int guessing, int data)
137 ruleDepth++;
138 TraceEventHandler eventDelegate = (TraceEventHandler)((CharScanner)source).Events[Parser.EnterRuleEventKey];
139 if (eventDelegate != null)
141 traceEvent.setValues(TraceEventArgs.ENTER, ruleNum, guessing, data);
142 eventDelegate(source, traceEvent);
144 checkController();
146 public virtual void fireExitRule(int ruleNum, int guessing, int data)
148 TraceEventHandler eventDelegate = (TraceEventHandler)((CharScanner)source).Events[Parser.ExitRuleEventKey];
149 if (eventDelegate != null)
151 traceEvent.setValues(TraceEventArgs.EXIT, ruleNum, guessing, data);
152 eventDelegate(source, traceEvent);
154 checkController();
156 ruleDepth--;
157 if (ruleDepth == 0)
158 fireDoneParsing();
161 public virtual void fireLA(int k, int la)
163 TokenEventHandler eventDelegate = (TokenEventHandler)((CharScanner)source).Events[Parser.LAEventKey];
164 if (eventDelegate != null)
166 tokenEvent.setValues(TokenEventArgs.LA, k, la);
167 eventDelegate(source, tokenEvent);
169 checkController();
171 public virtual void fireMatch(char c, int guessing)
173 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MatchEventKey];
174 if (eventDelegate != null)
176 matchEvent.setValues(MatchEventArgs.CHAR, c, c, null, guessing, false, true);
177 eventDelegate(source, matchEvent);
179 checkController();
181 public virtual void fireMatch(char c, BitSet b, int guessing)
183 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MatchEventKey];
184 if (eventDelegate != null)
186 matchEvent.setValues(MatchEventArgs.CHAR_BITSET, c, b, null, guessing, false, true);
187 eventDelegate(source, matchEvent);
189 checkController();
191 public virtual void fireMatch(char c, string target, int guessing)
193 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MatchEventKey];
194 if (eventDelegate != null)
196 matchEvent.setValues(MatchEventArgs.CHAR_RANGE, c, target, null, guessing, false, true);
197 eventDelegate(source, matchEvent);
199 checkController();
201 public virtual void fireMatch(int c, BitSet b, string text, int guessing)
203 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MatchEventKey];
204 if (eventDelegate != null)
206 matchEvent.setValues(MatchEventArgs.BITSET, c, b, text, guessing, false, true);
207 eventDelegate(source, matchEvent);
209 checkController();
211 public virtual void fireMatch(int n, string text, int guessing)
213 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MatchEventKey];
214 if (eventDelegate != null)
216 matchEvent.setValues(MatchEventArgs.TOKEN, n, n, text, guessing, false, true);
217 eventDelegate(source, matchEvent);
219 checkController();
221 public virtual void fireMatch(string s, int guessing)
223 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MatchEventKey];
224 if (eventDelegate != null)
226 matchEvent.setValues(MatchEventArgs.STRING, 0, s, null, guessing, false, true);
227 eventDelegate(source, matchEvent);
229 checkController();
231 public virtual void fireMatchNot(char c, char n, int guessing)
233 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MatchNotEventKey];
234 if (eventDelegate != null)
236 matchEvent.setValues(MatchEventArgs.CHAR, c, n, null, guessing, true, true);
237 eventDelegate(source, matchEvent);
239 checkController();
241 public virtual void fireMatchNot(int c, int n, string text, int guessing)
243 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MatchNotEventKey];
244 if (eventDelegate != null)
246 matchEvent.setValues(MatchEventArgs.TOKEN, c, n, text, guessing, true, true);
247 eventDelegate(source, matchEvent);
249 checkController();
251 public virtual void fireMismatch(char c, char n, int guessing)
253 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MisMatchEventKey];
254 if (eventDelegate != null)
256 matchEvent.setValues(MatchEventArgs.CHAR, c, n, null, guessing, false, false);
257 eventDelegate(source, matchEvent);
259 checkController();
261 public virtual void fireMismatch(char c, BitSet b, int guessing)
263 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MisMatchEventKey];
264 if (eventDelegate != null)
266 matchEvent.setValues(MatchEventArgs.CHAR_BITSET, c, b, null, guessing, false, true);
267 eventDelegate(source, matchEvent);
269 checkController();
271 public virtual void fireMismatch(char c, string target, int guessing)
273 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MisMatchEventKey];
274 if (eventDelegate != null)
276 matchEvent.setValues(MatchEventArgs.CHAR_RANGE, c, target, null, guessing, false, true);
277 eventDelegate(source, matchEvent);
279 checkController();
281 public virtual void fireMismatch(int i, int n, string text, int guessing)
283 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MisMatchEventKey];
284 if (eventDelegate != null)
286 matchEvent.setValues(MatchEventArgs.TOKEN, i, n, text, guessing, false, false);
287 eventDelegate(source, matchEvent);
289 checkController();
291 public virtual void fireMismatch(int i, BitSet b, string text, int guessing)
293 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MisMatchEventKey];
294 if (eventDelegate != null)
296 matchEvent.setValues(MatchEventArgs.BITSET, i, b, text, guessing, false, true);
297 eventDelegate(source, matchEvent);
299 checkController();
301 public virtual void fireMismatch(string s, string text, int guessing)
303 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MisMatchEventKey];
304 if (eventDelegate != null)
306 matchEvent.setValues(MatchEventArgs.STRING, 0, text, s, guessing, false, true);
307 eventDelegate(source, matchEvent);
309 checkController();
311 public virtual void fireMismatchNot(char v, char c, int guessing)
313 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MisMatchNotEventKey];
314 if (eventDelegate != null)
316 matchEvent.setValues(MatchEventArgs.CHAR, v, c, null, guessing, true, true);
317 eventDelegate(source, matchEvent);
319 checkController();
321 public virtual void fireMismatchNot(int i, int n, string text, int guessing)
323 MatchEventHandler eventDelegate = (MatchEventHandler)((CharScanner)source).Events[Parser.MisMatchNotEventKey];
324 if (eventDelegate != null)
326 matchEvent.setValues(MatchEventArgs.TOKEN, i, n, text, guessing, true, true);
327 eventDelegate(source, matchEvent);
329 checkController();
331 public virtual void fireNewLine(int line)
333 NewLineEventHandler eventDelegate = (NewLineEventHandler)((CharScanner)source).Events[Parser.NewLineEventKey];
334 if (eventDelegate != null)
336 newLineEvent.Line = line;
337 eventDelegate(source, newLineEvent);
339 checkController();
341 public virtual void fireReportError(System.Exception e)
343 MessageEventHandler eventDelegate = (MessageEventHandler)((CharScanner)source).Events[Parser.ReportErrorEventKey];
344 if (eventDelegate != null)
346 messageEvent.setValues(MessageEventArgs.ERROR, e.ToString());
347 eventDelegate(source, messageEvent);
349 checkController();
351 public virtual void fireReportError(string s)
353 MessageEventHandler eventDelegate = (MessageEventHandler)((CharScanner)source).Events[Parser.ReportErrorEventKey];
354 if (eventDelegate != null)
356 messageEvent.setValues(MessageEventArgs.ERROR, s);
357 eventDelegate(source, messageEvent);
359 checkController();
361 public virtual void fireReportWarning(string s)
363 MessageEventHandler eventDelegate = (MessageEventHandler)((CharScanner)source).Events[Parser.ReportWarningEventKey];
364 if (eventDelegate != null)
366 messageEvent.setValues(MessageEventArgs.WARNING, s);
367 eventDelegate(source, messageEvent);
369 checkController();
371 public virtual bool fireSemanticPredicateEvaluated(int type, int condition, bool result, int guessing)
373 SemanticPredicateEventHandler eventDelegate = (SemanticPredicateEventHandler)((CharScanner)source).Events[Parser.SemPredEvaluatedEventKey];
374 if (eventDelegate != null)
376 semPredEvent.setValues(type, condition, result, guessing);
377 eventDelegate(source, semPredEvent);
379 checkController();
381 return result;
383 public virtual void fireSyntacticPredicateFailed(int guessing)
385 SyntacticPredicateEventHandler eventDelegate = (SyntacticPredicateEventHandler)((CharScanner)source).Events[Parser.SynPredFailedEventKey];
386 if (eventDelegate != null)
388 synPredEvent.setValues(0, guessing);
389 eventDelegate(source, synPredEvent);
391 checkController();
393 public virtual void fireSyntacticPredicateStarted(int guessing)
395 SyntacticPredicateEventHandler eventDelegate = (SyntacticPredicateEventHandler)((CharScanner)source).Events[Parser.SynPredStartedEventKey];
396 if (eventDelegate != null)
398 synPredEvent.setValues(0, guessing);
399 eventDelegate(source, synPredEvent);
401 checkController();
403 public virtual void fireSyntacticPredicateSucceeded(int guessing)
405 SyntacticPredicateEventHandler eventDelegate = (SyntacticPredicateEventHandler)((CharScanner)source).Events[Parser.SynPredSucceededEventKey];
406 if (eventDelegate != null)
408 synPredEvent.setValues(0, guessing);
409 eventDelegate(source, synPredEvent);
411 checkController();
413 public virtual void refreshListeners()
415 Hashtable clonedTable;
417 lock(listeners.SyncRoot)
419 clonedTable = (Hashtable)listeners.Clone();
421 foreach (Listener l in clonedTable)
423 l.refresh();
426 public virtual void removeDoneListener(Listener l)
428 ((CharScanner)source).Done -= new TraceEventHandler(l.doneParsing);
429 listeners.Remove(l);
431 public virtual void removeMessageListener(MessageListener l)
433 ((CharScanner)source).ErrorReported -= new MessageEventHandler(l.reportError);
434 ((CharScanner)source).WarningReported -= new MessageEventHandler(l.reportWarning);
435 removeDoneListener(l);
437 public virtual void removeNewLineListener(NewLineListener l)
439 ((CharScanner)source).HitNewLine -= new NewLineEventHandler(l.hitNewLine);
440 removeDoneListener(l);
442 public virtual void removeParserListener(ParserListener l)
444 removeParserMatchListener(l);
445 removeMessageListener(l);
446 removeParserTokenListener(l);
447 removeTraceListener(l);
448 removeSemanticPredicateListener(l);
449 removeSyntacticPredicateListener(l);
451 public virtual void removeParserMatchListener(ParserMatchListener l)
453 ((CharScanner)source).MatchedChar -= new MatchEventHandler(l.parserMatch);
454 ((CharScanner)source).MatchedNotChar -= new MatchEventHandler(l.parserMatchNot);
455 ((CharScanner)source).MisMatchedChar -= new MatchEventHandler(l.parserMismatch);
456 ((CharScanner)source).MisMatchedNotChar -= new MatchEventHandler(l.parserMismatchNot);
457 removeDoneListener(l);
459 public virtual void removeParserTokenListener(ParserTokenListener l)
461 ((CharScanner)source).ConsumedChar -= new TokenEventHandler(l.parserConsume);
462 ((CharScanner)source).CharLA -= new TokenEventHandler(l.parserLA);
463 removeDoneListener(l);
465 public virtual void removeSemanticPredicateListener(SemanticPredicateListener l)
467 ((CharScanner)source).SemPredEvaluated -= new SemanticPredicateEventHandler(l.semanticPredicateEvaluated);
468 removeDoneListener(l);
470 public virtual void removeSyntacticPredicateListener(SyntacticPredicateListener l)
472 ((CharScanner)source).SynPredStarted -= new SyntacticPredicateEventHandler(l.syntacticPredicateStarted);
473 ((CharScanner)source).SynPredFailed -= new SyntacticPredicateEventHandler(l.syntacticPredicateFailed);
474 ((CharScanner)source).SynPredSucceeded -= new SyntacticPredicateEventHandler(l.syntacticPredicateSucceeded);
475 removeDoneListener(l);
477 public virtual void removeTraceListener(TraceListener l)
479 ((CharScanner)source).EnterRule -= new TraceEventHandler(l.enterRule);
480 ((CharScanner)source).ExitRule -= new TraceEventHandler(l.exitRule);
481 removeDoneListener(l);