4 using System
.Reflection
;
5 using Hashtable
= System
.Collections
.Hashtable
;
6 using DictionaryEntry
= System
.Collections
.DictionaryEntry
;
7 using ArrayList
= System
.Collections
.ArrayList
;
9 using antlr
.collections
.impl
;
12 public delegate void MessageEventHandler(object sender
, MessageEventArgs e
);
13 public delegate void NewLineEventHandler(object sender
, NewLineEventArgs e
);
14 public delegate void MatchEventHandler(object sender
, MatchEventArgs e
);
15 public delegate void TokenEventHandler(object sender
, TokenEventArgs e
);
16 public delegate void SemanticPredicateEventHandler(object sender
, SemanticPredicateEventArgs e
);
17 public delegate void SyntacticPredicateEventHandler(object sender
, SyntacticPredicateEventArgs e
);
18 public delegate void TraceEventHandler(object sender
, TraceEventArgs e
);
20 /// <summary>A class to assist in firing parser events
21 /// NOTE: I intentionally _did_not_ synchronize the event firing and
22 /// add/remove listener methods. This is because the add/remove should
23 /// _only_ be called by the parser at its start/end, and the _same_thread_
24 /// should be performing the parsing. This should help performance a tad...
26 public class ParserEventSupport
28 private object source
;
29 private Hashtable listeners
;
31 private MatchEventArgs matchEvent
;
32 private MessageEventArgs messageEvent
;
33 private TokenEventArgs tokenEvent
;
34 private SemanticPredicateEventArgs semPredEvent
;
35 private SyntacticPredicateEventArgs synPredEvent
;
36 private TraceEventArgs traceEvent
;
37 private NewLineEventArgs newLineEvent
;
39 private ParserController controller
;
41 private int ruleDepth
= 0;
44 public ParserEventSupport(object source
)
46 matchEvent
= new MatchEventArgs();
47 messageEvent
= new MessageEventArgs();
48 tokenEvent
= new TokenEventArgs();
49 traceEvent
= new TraceEventArgs();
50 semPredEvent
= new SemanticPredicateEventArgs();
51 synPredEvent
= new SyntacticPredicateEventArgs();
52 newLineEvent
= new NewLineEventArgs();
53 listeners
= new Hashtable();
57 public virtual void checkController()
59 if (controller
!= null)
60 controller
.checkBreak();
63 public virtual void addDoneListener(Listener l
)
65 ((Parser
)source
).Done
+= new TraceEventHandler(l
.doneParsing
);
68 public virtual void addMessageListener(MessageListener l
)
70 ((Parser
)source
).ErrorReported
+= new MessageEventHandler(l
.reportError
);
71 ((Parser
)source
).WarningReported
+= new MessageEventHandler(l
.reportWarning
);
72 //messageListeners.Add(l);
75 public virtual void addParserListener(ParserListener l
)
77 if (l
is ParserController
)
79 ((ParserController
) l
).ParserEventSupport
= this;
80 controller
= (ParserController
) l
;
82 addParserMatchListener(l
);
83 addParserTokenListener(l
);
85 addMessageListener(l
);
87 addSemanticPredicateListener(l
);
88 addSyntacticPredicateListener(l
);
90 public virtual void addParserMatchListener(ParserMatchListener l
)
92 ((Parser
)source
).MatchedToken
+= new MatchEventHandler(l
.parserMatch
);
93 ((Parser
)source
).MatchedNotToken
+= new MatchEventHandler(l
.parserMatchNot
);
94 ((Parser
)source
).MisMatchedToken
+= new MatchEventHandler(l
.parserMismatch
);
95 ((Parser
)source
).MisMatchedNotToken
+= new MatchEventHandler(l
.parserMismatchNot
);
96 //matchListeners.Add(l);
99 public virtual void addParserTokenListener(ParserTokenListener l
)
101 ((Parser
)source
).ConsumedToken
+= new TokenEventHandler(l
.parserConsume
);
102 ((Parser
)source
).TokenLA
+= new TokenEventHandler(l
.parserLA
);
103 //tokenListeners.Add(l);
106 public virtual void addSemanticPredicateListener(SemanticPredicateListener l
)
108 ((Parser
)source
).SemPredEvaluated
+= new SemanticPredicateEventHandler(l
.semanticPredicateEvaluated
);
109 //semPredListeners.Add(l);
112 public virtual void addSyntacticPredicateListener(SyntacticPredicateListener l
)
114 ((Parser
)source
).SynPredStarted
+= new SyntacticPredicateEventHandler(l
.syntacticPredicateStarted
);
115 ((Parser
)source
).SynPredFailed
+= new SyntacticPredicateEventHandler(l
.syntacticPredicateFailed
);
116 ((Parser
)source
).SynPredSucceeded
+= new SyntacticPredicateEventHandler(l
.syntacticPredicateSucceeded
);
117 //synPredListeners.Add(l);
120 public virtual void addTraceListener(TraceListener l
)
122 ((Parser
)source
).EnterRule
+= new TraceEventHandler(l
.enterRule
);
123 ((Parser
)source
).ExitRule
+= new TraceEventHandler(l
.exitRule
);
124 //traceListeners.Add(l);
127 public virtual void fireConsume(int c
)
129 TokenEventHandler eventDelegate
= (TokenEventHandler
)((Parser
)source
).Events
[Parser
.LAEventKey
];
130 if (eventDelegate
!= null)
132 tokenEvent
.setValues(TokenEventArgs
.CONSUME
, 1, c
);
133 eventDelegate(source
, tokenEvent
);
137 public virtual void fireDoneParsing()
139 TraceEventHandler eventDelegate
= (TraceEventHandler
)((Parser
)source
).Events
[Parser
.DoneEventKey
];
140 if (eventDelegate
!= null)
142 traceEvent
.setValues(TraceEventArgs
.DONE_PARSING
, 0, 0, 0);
143 eventDelegate(source
, traceEvent
);
147 public virtual void fireEnterRule(int ruleNum
, int guessing
, int data
)
150 TraceEventHandler eventDelegate
= (TraceEventHandler
)((Parser
)source
).Events
[Parser
.EnterRuleEventKey
];
151 if (eventDelegate
!= null)
153 traceEvent
.setValues(TraceEventArgs
.ENTER
, ruleNum
, guessing
, data
);
154 eventDelegate(source
, traceEvent
);
158 public virtual void fireExitRule(int ruleNum
, int guessing
, int data
)
160 TraceEventHandler eventDelegate
= (TraceEventHandler
)((Parser
)source
).Events
[Parser
.ExitRuleEventKey
];
161 if (eventDelegate
!= null)
163 traceEvent
.setValues(TraceEventArgs
.EXIT
, ruleNum
, guessing
, data
);
164 eventDelegate(source
, traceEvent
);
173 public virtual void fireLA(int k
, int la
)
175 TokenEventHandler eventDelegate
= (TokenEventHandler
)((Parser
)source
).Events
[Parser
.LAEventKey
];
176 if (eventDelegate
!= null)
178 tokenEvent
.setValues(TokenEventArgs
.LA
, k
, la
);
179 eventDelegate(source
, tokenEvent
);
183 public virtual void fireMatch(char c
, int guessing
)
185 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MatchEventKey
];
186 if (eventDelegate
!= null)
188 matchEvent
.setValues(MatchEventArgs
.CHAR
, c
, c
, null, guessing
, false, true);
189 eventDelegate(source
, matchEvent
);
193 public virtual void fireMatch(char c
, BitSet b
, int guessing
)
195 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MatchEventKey
];
196 if (eventDelegate
!= null)
198 matchEvent
.setValues(MatchEventArgs
.CHAR_BITSET
, c
, b
, null, guessing
, false, true);
199 eventDelegate(source
, matchEvent
);
203 public virtual void fireMatch(char c
, string target
, int guessing
)
205 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MatchEventKey
];
206 if (eventDelegate
!= null)
208 matchEvent
.setValues(MatchEventArgs
.CHAR_RANGE
, c
, target
, null, guessing
, false, true);
209 eventDelegate(source
, matchEvent
);
213 public virtual void fireMatch(int c
, BitSet b
, string text
, int guessing
)
215 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MatchEventKey
];
216 if (eventDelegate
!= null)
218 matchEvent
.setValues(MatchEventArgs
.BITSET
, c
, b
, text
, guessing
, false, true);
219 eventDelegate(source
, matchEvent
);
223 public virtual void fireMatch(int n
, string text
, int guessing
)
225 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MatchEventKey
];
226 if (eventDelegate
!= null)
228 matchEvent
.setValues(MatchEventArgs
.TOKEN
, n
, n
, text
, guessing
, false, true);
229 eventDelegate(source
, matchEvent
);
233 public virtual void fireMatch(string s
, int guessing
)
235 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MatchEventKey
];
236 if (eventDelegate
!= null)
238 matchEvent
.setValues(MatchEventArgs
.STRING
, 0, s
, null, guessing
, false, true);
239 eventDelegate(source
, matchEvent
);
243 public virtual void fireMatchNot(char c
, char n
, int guessing
)
245 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MatchNotEventKey
];
246 if (eventDelegate
!= null)
248 matchEvent
.setValues(MatchEventArgs
.CHAR
, c
, n
, null, guessing
, true, true);
249 eventDelegate(source
, matchEvent
);
253 public virtual void fireMatchNot(int c
, int n
, string text
, int guessing
)
255 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MatchNotEventKey
];
256 if (eventDelegate
!= null)
258 matchEvent
.setValues(MatchEventArgs
.TOKEN
, c
, n
, text
, guessing
, true, true);
259 eventDelegate(source
, matchEvent
);
263 public virtual void fireMismatch(char c
, char n
, int guessing
)
265 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MisMatchEventKey
];
266 if (eventDelegate
!= null)
268 matchEvent
.setValues(MatchEventArgs
.CHAR
, c
, n
, null, guessing
, false, false);
269 eventDelegate(source
, matchEvent
);
273 public virtual void fireMismatch(char c
, BitSet b
, int guessing
)
275 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MisMatchEventKey
];
276 if (eventDelegate
!= null)
278 matchEvent
.setValues(MatchEventArgs
.CHAR_BITSET
, c
, b
, null, guessing
, false, true);
279 eventDelegate(source
, matchEvent
);
283 public virtual void fireMismatch(char c
, string target
, int guessing
)
285 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MisMatchEventKey
];
286 if (eventDelegate
!= null)
288 matchEvent
.setValues(MatchEventArgs
.CHAR_RANGE
, c
, target
, null, guessing
, false, true);
289 eventDelegate(source
, matchEvent
);
293 public virtual void fireMismatch(int i
, int n
, string text
, int guessing
)
295 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MisMatchEventKey
];
296 if (eventDelegate
!= null)
298 matchEvent
.setValues(MatchEventArgs
.TOKEN
, i
, n
, text
, guessing
, false, false);
299 eventDelegate(source
, matchEvent
);
303 public virtual void fireMismatch(int i
, BitSet b
, string text
, int guessing
)
305 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MisMatchEventKey
];
306 if (eventDelegate
!= null)
308 matchEvent
.setValues(MatchEventArgs
.BITSET
, i
, b
, text
, guessing
, false, true);
309 eventDelegate(source
, matchEvent
);
313 public virtual void fireMismatch(string s
, string text
, int guessing
)
315 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MisMatchEventKey
];
316 if (eventDelegate
!= null)
318 matchEvent
.setValues(MatchEventArgs
.STRING
, 0, text
, s
, guessing
, false, true);
319 eventDelegate(source
, matchEvent
);
323 public virtual void fireMismatchNot(char v
, char c
, int guessing
)
325 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MisMatchNotEventKey
];
326 if (eventDelegate
!= null)
328 matchEvent
.setValues(MatchEventArgs
.CHAR
, v
, c
, null, guessing
, true, true);
329 eventDelegate(source
, matchEvent
);
333 public virtual void fireMismatchNot(int i
, int n
, string text
, int guessing
)
335 MatchEventHandler eventDelegate
= (MatchEventHandler
)((Parser
)source
).Events
[Parser
.MisMatchNotEventKey
];
336 if (eventDelegate
!= null)
338 matchEvent
.setValues(MatchEventArgs
.TOKEN
, i
, n
, text
, guessing
, true, true);
339 eventDelegate(source
, matchEvent
);
343 public virtual void fireReportError(System
.Exception e
)
345 MessageEventHandler eventDelegate
= (MessageEventHandler
)((Parser
)source
).Events
[Parser
.ReportErrorEventKey
];
346 if (eventDelegate
!= null)
348 messageEvent
.setValues(MessageEventArgs
.ERROR
, e
.ToString());
349 eventDelegate(source
, messageEvent
);
353 public virtual void fireReportError(string s
)
355 MessageEventHandler eventDelegate
= (MessageEventHandler
)((Parser
)source
).Events
[Parser
.ReportErrorEventKey
];
356 if (eventDelegate
!= null)
358 messageEvent
.setValues(MessageEventArgs
.ERROR
, s
);
359 eventDelegate(source
, messageEvent
);
363 public virtual void fireReportWarning(string s
)
365 MessageEventHandler eventDelegate
= (MessageEventHandler
)((Parser
)source
).Events
[Parser
.ReportWarningEventKey
];
366 if (eventDelegate
!= null)
368 messageEvent
.setValues(MessageEventArgs
.WARNING
, s
);
369 eventDelegate(source
, messageEvent
);
373 public virtual bool fireSemanticPredicateEvaluated(int type
, int condition
, bool result
, int guessing
)
375 SemanticPredicateEventHandler eventDelegate
= (SemanticPredicateEventHandler
)((Parser
)source
).Events
[Parser
.SemPredEvaluatedEventKey
];
376 if (eventDelegate
!= null)
378 semPredEvent
.setValues(type
, condition
, result
, guessing
);
379 eventDelegate(source
, semPredEvent
);
385 public virtual void fireSyntacticPredicateFailed(int guessing
)
387 SyntacticPredicateEventHandler eventDelegate
= (SyntacticPredicateEventHandler
)((Parser
)source
).Events
[Parser
.SynPredFailedEventKey
];
388 if (eventDelegate
!= null)
390 synPredEvent
.setValues(0, guessing
);
391 eventDelegate(source
, synPredEvent
);
395 public virtual void fireSyntacticPredicateStarted(int guessing
)
397 SyntacticPredicateEventHandler eventDelegate
= (SyntacticPredicateEventHandler
)((Parser
)source
).Events
[Parser
.SynPredStartedEventKey
];
398 if (eventDelegate
!= null)
400 synPredEvent
.setValues(0, guessing
);
401 eventDelegate(source
, synPredEvent
);
405 public virtual void fireSyntacticPredicateSucceeded(int guessing
)
407 SyntacticPredicateEventHandler eventDelegate
= (SyntacticPredicateEventHandler
)((Parser
)source
).Events
[Parser
.SynPredSucceededEventKey
];
408 if (eventDelegate
!= null)
410 synPredEvent
.setValues(0, guessing
);
411 eventDelegate(source
, synPredEvent
);
415 public virtual void refreshListeners()
417 Hashtable clonedTable
;
419 lock(listeners
.SyncRoot
)
421 clonedTable
= (Hashtable
)listeners
.Clone();
423 foreach (DictionaryEntry entry
in clonedTable
)
425 if (entry
.Value
!= null)
427 ((Listener
) entry
.Value
).refresh();
431 public virtual void removeDoneListener(Listener l
)
433 ((Parser
)source
).Done
-= new TraceEventHandler(l
.doneParsing
);
436 public virtual void removeMessageListener(MessageListener l
)
438 ((Parser
)source
).ErrorReported
-= new MessageEventHandler(l
.reportError
);
439 ((Parser
)source
).WarningReported
-= new MessageEventHandler(l
.reportWarning
);
440 //messageListeners.Remove(l);
441 removeDoneListener(l
);
443 public virtual void removeParserListener(ParserListener l
)
445 removeParserMatchListener(l
);
446 removeMessageListener(l
);
447 removeParserTokenListener(l
);
448 removeTraceListener(l
);
449 removeSemanticPredicateListener(l
);
450 removeSyntacticPredicateListener(l
);
452 public virtual void removeParserMatchListener(ParserMatchListener l
)
454 ((Parser
)source
).MatchedToken
-= new MatchEventHandler(l
.parserMatch
);
455 ((Parser
)source
).MatchedNotToken
-= new MatchEventHandler(l
.parserMatchNot
);
456 ((Parser
)source
).MisMatchedToken
-= new MatchEventHandler(l
.parserMismatch
);
457 ((Parser
)source
).MisMatchedNotToken
-= new MatchEventHandler(l
.parserMismatchNot
);
458 //matchListeners.Remove(l);
459 removeDoneListener(l
);
461 public virtual void removeParserTokenListener(ParserTokenListener l
)
463 ((Parser
)source
).ConsumedToken
-= new TokenEventHandler(l
.parserConsume
);
464 ((Parser
)source
).TokenLA
-= new TokenEventHandler(l
.parserLA
);
465 //tokenListeners.Remove(l);
466 removeDoneListener(l
);
468 public virtual void removeSemanticPredicateListener(SemanticPredicateListener l
)
470 ((Parser
)source
).SemPredEvaluated
-= new SemanticPredicateEventHandler(l
.semanticPredicateEvaluated
);
471 //semPredListeners.Remove(l);
472 removeDoneListener(l
);
474 public virtual void removeSyntacticPredicateListener(SyntacticPredicateListener l
)
476 ((Parser
)source
).SynPredStarted
-= new SyntacticPredicateEventHandler(l
.syntacticPredicateStarted
);
477 ((Parser
)source
).SynPredFailed
-= new SyntacticPredicateEventHandler(l
.syntacticPredicateFailed
);
478 ((Parser
)source
).SynPredSucceeded
-= new SyntacticPredicateEventHandler(l
.syntacticPredicateSucceeded
);
479 //synPredListeners.Remove(l);
480 removeDoneListener(l
);
482 public virtual void removeTraceListener(TraceListener l
)
484 ((Parser
)source
).EnterRule
-= new TraceEventHandler(l
.enterRule
);
485 ((Parser
)source
).ExitRule
-= new TraceEventHandler(l
.exitRule
);
486 //traceListeners.Remove(l);
487 removeDoneListener(l
);