Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Rook / Castle.Rook.Compiler / Parser / antlr / debug / LLkDebuggingParser.cs
blobe054c55ac283b3bc34366f52f3321bec32aed5a7
1 namespace antlr.debug
3 using System;
4 using System.Threading;
5 using antlr.collections.impl;
7 public class LLkDebuggingParser : LLkParser, DebuggingParser
9 private void InitBlock()
11 parserEventSupport = new ParserEventSupport(this);
13 public override void setDebugMode(bool mode)
15 _notDebugMode = !mode;
17 protected internal ParserEventSupport parserEventSupport;
19 private bool _notDebugMode = false;
20 protected internal string[] ruleNames;
21 protected internal string[] semPredNames;
24 public LLkDebuggingParser(int k_):base(k_)
26 InitBlock();
28 public LLkDebuggingParser(ParserSharedInputState state, int k_):base(state, k_)
30 InitBlock();
32 public LLkDebuggingParser(TokenBuffer tokenBuf, int k_):base(tokenBuf, k_)
34 InitBlock();
36 public LLkDebuggingParser(TokenStream lexer, int k_):base(lexer, k_)
38 InitBlock();
40 public override void addMessageListener(MessageListener l)
42 parserEventSupport.addMessageListener(l);
44 public override void addParserListener(ParserListener l)
46 parserEventSupport.addParserListener(l);
48 public override void addParserMatchListener(ParserMatchListener l)
50 parserEventSupport.addParserMatchListener(l);
52 public override void addParserTokenListener(ParserTokenListener l)
54 parserEventSupport.addParserTokenListener(l);
56 public override void addSemanticPredicateListener(SemanticPredicateListener l)
58 parserEventSupport.addSemanticPredicateListener(l);
60 public override void addSyntacticPredicateListener(SyntacticPredicateListener l)
62 parserEventSupport.addSyntacticPredicateListener(l);
64 public override void addTraceListener(TraceListener l)
66 parserEventSupport.addTraceListener(l);
68 /// <summary>Get another token object from the token stream
69 /// </summary>
70 public override void consume()
72 int la_1 = - 99;
73 la_1 = LA(1);
74 base.consume();
75 parserEventSupport.fireConsume(la_1);
77 protected internal virtual void fireEnterRule(int num, int data)
79 if (isDebugMode())
80 parserEventSupport.fireEnterRule(num, inputState.guessing, data);
82 protected internal virtual void fireExitRule(int num, int data)
84 if (isDebugMode())
85 parserEventSupport.fireExitRule(num, inputState.guessing, data);
87 protected internal virtual bool fireSemanticPredicateEvaluated(int type, int num, bool condition)
89 if (isDebugMode())
90 return parserEventSupport.fireSemanticPredicateEvaluated(type, num, condition, inputState.guessing);
91 else
92 return condition;
94 protected internal virtual void fireSyntacticPredicateFailed()
96 if (isDebugMode())
97 parserEventSupport.fireSyntacticPredicateFailed(inputState.guessing);
99 protected internal virtual void fireSyntacticPredicateStarted()
101 if (isDebugMode())
102 parserEventSupport.fireSyntacticPredicateStarted(inputState.guessing);
104 protected internal virtual void fireSyntacticPredicateSucceeded()
106 if (isDebugMode())
107 parserEventSupport.fireSyntacticPredicateSucceeded(inputState.guessing);
109 public virtual string getRuleName(int num)
111 return ruleNames[num];
113 public virtual string getSemPredName(int num)
115 return semPredNames[num];
118 public virtual void goToSleep()
120 lock(this)
124 Monitor.Wait(this);
126 catch (System.Threading.ThreadInterruptedException)
131 public override bool isDebugMode()
133 return !_notDebugMode;
135 public virtual bool isGuessing()
137 return inputState.guessing > 0;
139 /// <summary>Return the token type of the ith token of lookahead where i=1
140 /// is the current token being examined by the parser (i.e., it
141 /// has not been matched yet).
142 /// </summary>
143 public override int LA(int i)
145 int la = base.LA(i);
146 parserEventSupport.fireLA(i, la);
147 return la;
149 /// <summary>Make sure current lookahead symbol matches token type <tt>t</tt>.
150 /// Throw an exception upon mismatch, which is catch by either the
151 /// error handler or by the syntactic predicate.
152 /// </summary>
153 public override void match(int t)
155 string text = LT(1).getText();
156 int la_1 = LA(1);
159 base.match(t);
160 parserEventSupport.fireMatch(t, text, inputState.guessing);
162 catch (MismatchedTokenException e)
164 if (inputState.guessing == 0)
165 parserEventSupport.fireMismatch(la_1, t, text, inputState.guessing);
166 throw e;
169 /// <summary>Make sure current lookahead symbol matches the given set
170 /// Throw an exception upon mismatch, which is catch by either the
171 /// error handler or by the syntactic predicate.
172 /// </summary>
173 public override void match(BitSet b)
175 string text = LT(1).getText();
176 int la_1 = LA(1);
179 base.match(b);
180 parserEventSupport.fireMatch(la_1, b, text, inputState.guessing);
182 catch (MismatchedTokenException e)
184 if (inputState.guessing == 0)
185 parserEventSupport.fireMismatch(la_1, b, text, inputState.guessing);
186 throw e;
189 public override void matchNot(int t)
191 string text = LT(1).getText();
192 int la_1 = LA(1);
195 base.matchNot(t);
196 parserEventSupport.fireMatchNot(la_1, t, text, inputState.guessing);
198 catch (MismatchedTokenException e)
200 if (inputState.guessing == 0)
201 parserEventSupport.fireMismatchNot(la_1, t, text, inputState.guessing);
202 throw e;
205 public override void removeMessageListener(MessageListener l)
207 parserEventSupport.removeMessageListener(l);
209 public override void removeParserListener(ParserListener l)
211 parserEventSupport.removeParserListener(l);
213 public override void removeParserMatchListener(ParserMatchListener l)
215 parserEventSupport.removeParserMatchListener(l);
217 public override void removeParserTokenListener(ParserTokenListener l)
219 parserEventSupport.removeParserTokenListener(l);
221 public override void removeSemanticPredicateListener(SemanticPredicateListener l)
223 parserEventSupport.removeSemanticPredicateListener(l);
225 public override void removeSyntacticPredicateListener(SyntacticPredicateListener l)
227 parserEventSupport.removeSyntacticPredicateListener(l);
229 public override void removeTraceListener(TraceListener l)
231 parserEventSupport.removeTraceListener(l);
233 /// <summary>Parser error-reporting function can be overridden in subclass
234 /// </summary>
235 public override void reportError(RecognitionException ex)
237 parserEventSupport.fireReportError(ex);
238 base.reportError(ex);
240 /// <summary>Parser error-reporting function can be overridden in subclass
241 /// </summary>
242 public override void reportError(string s)
244 parserEventSupport.fireReportError(s);
245 base.reportError(s);
247 /// <summary>Parser warning-reporting function can be overridden in subclass
248 /// </summary>
249 public override void reportWarning(string s)
251 parserEventSupport.fireReportWarning(s);
252 base.reportWarning(s);
254 public virtual void setupDebugging(TokenBuffer tokenBuf)
256 setupDebugging(null, tokenBuf);
258 public virtual void setupDebugging(TokenStream lexer)
260 setupDebugging(lexer, null);
262 /// <summary>User can override to do their own debugging
263 /// </summary>
264 protected internal virtual void setupDebugging(TokenStream lexer, TokenBuffer tokenBuf)
266 setDebugMode(true);
267 // default parser debug setup is ParseView
270 // try
271 // {
272 // System.Type.GetType("javax.swing.JButton");
273 // }
274 // catch (System.Exception)
275 // {
276 // System.Console.Error.WriteLine("Swing is required to use ParseView, but is not present in your CLASSPATH");
277 // System.Environment.Exit(1);
278 // }
279 System.Type c = System.Type.GetType("antlr.parseview.ParseView");
280 System.Reflection.ConstructorInfo constructor = c.GetConstructor(new System.Type[]{typeof(LLkDebuggingParser), typeof(TokenStream), typeof(TokenBuffer)});
281 constructor.Invoke(new object[]{this, lexer, tokenBuf});
283 catch (System.Exception e)
285 System.Console.Error.WriteLine("Error initializing ParseView: " + e);
286 System.Console.Error.WriteLine("Please report this to Scott Stanchfield, thetick@magelang.com");
287 System.Environment.Exit(1);
291 public virtual void wakeUp()
293 lock(this)
295 Monitor.Pulse(this);