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_
)
28 public LLkDebuggingParser(ParserSharedInputState state
, int k_
):base(state
, k_
)
32 public LLkDebuggingParser(TokenBuffer tokenBuf
, int k_
):base(tokenBuf
, k_
)
36 public LLkDebuggingParser(TokenStream lexer
, int k_
):base(lexer
, k_
)
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
70 public override void consume()
75 parserEventSupport
.fireConsume(la_1
);
77 protected internal virtual void fireEnterRule(int num
, int data
)
80 parserEventSupport
.fireEnterRule(num
, inputState
.guessing
, data
);
82 protected internal virtual void fireExitRule(int num
, int data
)
85 parserEventSupport
.fireExitRule(num
, inputState
.guessing
, data
);
87 protected internal virtual bool fireSemanticPredicateEvaluated(int type
, int num
, bool condition
)
90 return parserEventSupport
.fireSemanticPredicateEvaluated(type
, num
, condition
, inputState
.guessing
);
94 protected internal virtual void fireSyntacticPredicateFailed()
97 parserEventSupport
.fireSyntacticPredicateFailed(inputState
.guessing
);
99 protected internal virtual void fireSyntacticPredicateStarted()
102 parserEventSupport
.fireSyntacticPredicateStarted(inputState
.guessing
);
104 protected internal virtual void fireSyntacticPredicateSucceeded()
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()
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).
143 public override int LA(int i
)
146 parserEventSupport
.fireLA(i
, 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.
153 public override void match(int t
)
155 string text
= LT(1).getText();
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
);
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.
173 public override void match(BitSet b
)
175 string text
= LT(1).getText();
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
);
189 public override void matchNot(int t
)
191 string text
= LT(1).getText();
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
);
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
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
242 public override void reportError(string s
)
244 parserEventSupport
.fireReportError(s
);
247 /// <summary>Parser warning-reporting function can be overridden in subclass
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
264 protected internal virtual void setupDebugging(TokenStream lexer
, TokenBuffer tokenBuf
)
267 // default parser debug setup is ParseView
272 // System.Type.GetType("javax.swing.JButton");
274 // catch (System.Exception)
276 // System.Console.Error.WriteLine("Swing is required to use ParseView, but is not present in your CLASSPATH");
277 // System.Environment.Exit(1);
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()