1 | namespace antlr.debug |
---|
2 | { |
---|
3 | using System; |
---|
4 | using System.Threading; |
---|
5 | using antlr.collections.impl; |
---|
6 | |
---|
7 | public class LLkDebuggingParser : LLkParser, DebuggingParser |
---|
8 | { |
---|
9 | private void InitBlock() |
---|
10 | { |
---|
11 | parserEventSupport = new ParserEventSupport(this); |
---|
12 | } |
---|
13 | public override void setDebugMode(bool mode) |
---|
14 | { |
---|
15 | _notDebugMode = !mode; |
---|
16 | } |
---|
17 | protected internal ParserEventSupport parserEventSupport; |
---|
18 | |
---|
19 | private bool _notDebugMode = false; |
---|
20 | protected internal string[] ruleNames; |
---|
21 | protected internal string[] semPredNames; |
---|
22 | |
---|
23 | |
---|
24 | public LLkDebuggingParser(int k_):base(k_) |
---|
25 | { |
---|
26 | InitBlock(); |
---|
27 | } |
---|
28 | public LLkDebuggingParser(ParserSharedInputState state, int k_):base(state, k_) |
---|
29 | { |
---|
30 | InitBlock(); |
---|
31 | } |
---|
32 | public LLkDebuggingParser(TokenBuffer tokenBuf, int k_):base(tokenBuf, k_) |
---|
33 | { |
---|
34 | InitBlock(); |
---|
35 | } |
---|
36 | public LLkDebuggingParser(TokenStream lexer, int k_):base(lexer, k_) |
---|
37 | { |
---|
38 | InitBlock(); |
---|
39 | } |
---|
40 | public override void addMessageListener(MessageListener l) |
---|
41 | { |
---|
42 | parserEventSupport.addMessageListener(l); |
---|
43 | } |
---|
44 | public override void addParserListener(ParserListener l) |
---|
45 | { |
---|
46 | parserEventSupport.addParserListener(l); |
---|
47 | } |
---|
48 | public override void addParserMatchListener(ParserMatchListener l) |
---|
49 | { |
---|
50 | parserEventSupport.addParserMatchListener(l); |
---|
51 | } |
---|
52 | public override void addParserTokenListener(ParserTokenListener l) |
---|
53 | { |
---|
54 | parserEventSupport.addParserTokenListener(l); |
---|
55 | } |
---|
56 | public override void addSemanticPredicateListener(SemanticPredicateListener l) |
---|
57 | { |
---|
58 | parserEventSupport.addSemanticPredicateListener(l); |
---|
59 | } |
---|
60 | public override void addSyntacticPredicateListener(SyntacticPredicateListener l) |
---|
61 | { |
---|
62 | parserEventSupport.addSyntacticPredicateListener(l); |
---|
63 | } |
---|
64 | public override void addTraceListener(TraceListener l) |
---|
65 | { |
---|
66 | parserEventSupport.addTraceListener(l); |
---|
67 | } |
---|
68 | /// <summary>Get another token object from the token stream |
---|
69 | /// </summary> |
---|
70 | public override void consume() |
---|
71 | { |
---|
72 | int la_1 = - 99; |
---|
73 | la_1 = LA(1); |
---|
74 | base.consume(); |
---|
75 | parserEventSupport.fireConsume(la_1); |
---|
76 | } |
---|
77 | protected internal virtual void fireEnterRule(int num, int data) |
---|
78 | { |
---|
79 | if (isDebugMode()) |
---|
80 | parserEventSupport.fireEnterRule(num, inputState.guessing, data); |
---|
81 | } |
---|
82 | protected internal virtual void fireExitRule(int num, int data) |
---|
83 | { |
---|
84 | if (isDebugMode()) |
---|
85 | parserEventSupport.fireExitRule(num, inputState.guessing, data); |
---|
86 | } |
---|
87 | protected internal virtual bool fireSemanticPredicateEvaluated(int type, int num, bool condition) |
---|
88 | { |
---|
89 | if (isDebugMode()) |
---|
90 | return parserEventSupport.fireSemanticPredicateEvaluated(type, num, condition, inputState.guessing); |
---|
91 | else |
---|
92 | return condition; |
---|
93 | } |
---|
94 | protected internal virtual void fireSyntacticPredicateFailed() |
---|
95 | { |
---|
96 | if (isDebugMode()) |
---|
97 | parserEventSupport.fireSyntacticPredicateFailed(inputState.guessing); |
---|
98 | } |
---|
99 | protected internal virtual void fireSyntacticPredicateStarted() |
---|
100 | { |
---|
101 | if (isDebugMode()) |
---|
102 | parserEventSupport.fireSyntacticPredicateStarted(inputState.guessing); |
---|
103 | } |
---|
104 | protected internal virtual void fireSyntacticPredicateSucceeded() |
---|
105 | { |
---|
106 | if (isDebugMode()) |
---|
107 | parserEventSupport.fireSyntacticPredicateSucceeded(inputState.guessing); |
---|
108 | } |
---|
109 | public virtual string getRuleName(int num) |
---|
110 | { |
---|
111 | return ruleNames[num]; |
---|
112 | } |
---|
113 | public virtual string getSemPredName(int num) |
---|
114 | { |
---|
115 | return semPredNames[num]; |
---|
116 | } |
---|
117 | |
---|
118 | public virtual void goToSleep() |
---|
119 | { |
---|
120 | lock(this) |
---|
121 | { |
---|
122 | try |
---|
123 | { |
---|
124 | Monitor.Wait(this); |
---|
125 | } |
---|
126 | catch (System.Threading.ThreadInterruptedException) |
---|
127 | { |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | public override bool isDebugMode() |
---|
132 | { |
---|
133 | return !_notDebugMode; |
---|
134 | } |
---|
135 | public virtual bool isGuessing() |
---|
136 | { |
---|
137 | return inputState.guessing > 0; |
---|
138 | } |
---|
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) |
---|
144 | { |
---|
145 | int la = base.LA(i); |
---|
146 | parserEventSupport.fireLA(i, la); |
---|
147 | return la; |
---|
148 | } |
---|
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) |
---|
154 | { |
---|
155 | string text = LT(1).getText(); |
---|
156 | int la_1 = LA(1); |
---|
157 | try |
---|
158 | { |
---|
159 | base.match(t); |
---|
160 | parserEventSupport.fireMatch(t, text, inputState.guessing); |
---|
161 | } |
---|
162 | catch (MismatchedTokenException e) |
---|
163 | { |
---|
164 | if (inputState.guessing == 0) |
---|
165 | parserEventSupport.fireMismatch(la_1, t, text, inputState.guessing); |
---|
166 | throw e; |
---|
167 | } |
---|
168 | } |
---|
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) |
---|
174 | { |
---|
175 | string text = LT(1).getText(); |
---|
176 | int la_1 = LA(1); |
---|
177 | try |
---|
178 | { |
---|
179 | base.match(b); |
---|
180 | parserEventSupport.fireMatch(la_1, b, text, inputState.guessing); |
---|
181 | } |
---|
182 | catch (MismatchedTokenException e) |
---|
183 | { |
---|
184 | if (inputState.guessing == 0) |
---|
185 | parserEventSupport.fireMismatch(la_1, b, text, inputState.guessing); |
---|
186 | throw e; |
---|
187 | } |
---|
188 | } |
---|
189 | public override void matchNot(int t) |
---|
190 | { |
---|
191 | string text = LT(1).getText(); |
---|
192 | int la_1 = LA(1); |
---|
193 | try |
---|
194 | { |
---|
195 | base.matchNot(t); |
---|
196 | parserEventSupport.fireMatchNot(la_1, t, text, inputState.guessing); |
---|
197 | } |
---|
198 | catch (MismatchedTokenException e) |
---|
199 | { |
---|
200 | if (inputState.guessing == 0) |
---|
201 | parserEventSupport.fireMismatchNot(la_1, t, text, inputState.guessing); |
---|
202 | throw e; |
---|
203 | } |
---|
204 | } |
---|
205 | public override void removeMessageListener(MessageListener l) |
---|
206 | { |
---|
207 | parserEventSupport.removeMessageListener(l); |
---|
208 | } |
---|
209 | public override void removeParserListener(ParserListener l) |
---|
210 | { |
---|
211 | parserEventSupport.removeParserListener(l); |
---|
212 | } |
---|
213 | public override void removeParserMatchListener(ParserMatchListener l) |
---|
214 | { |
---|
215 | parserEventSupport.removeParserMatchListener(l); |
---|
216 | } |
---|
217 | public override void removeParserTokenListener(ParserTokenListener l) |
---|
218 | { |
---|
219 | parserEventSupport.removeParserTokenListener(l); |
---|
220 | } |
---|
221 | public override void removeSemanticPredicateListener(SemanticPredicateListener l) |
---|
222 | { |
---|
223 | parserEventSupport.removeSemanticPredicateListener(l); |
---|
224 | } |
---|
225 | public override void removeSyntacticPredicateListener(SyntacticPredicateListener l) |
---|
226 | { |
---|
227 | parserEventSupport.removeSyntacticPredicateListener(l); |
---|
228 | } |
---|
229 | public override void removeTraceListener(TraceListener l) |
---|
230 | { |
---|
231 | parserEventSupport.removeTraceListener(l); |
---|
232 | } |
---|
233 | /// <summary>Parser error-reporting function can be overridden in subclass |
---|
234 | /// </summary> |
---|
235 | public override void reportError(RecognitionException ex) |
---|
236 | { |
---|
237 | parserEventSupport.fireReportError(ex); |
---|
238 | base.reportError(ex); |
---|
239 | } |
---|
240 | /// <summary>Parser error-reporting function can be overridden in subclass |
---|
241 | /// </summary> |
---|
242 | public override void reportError(string s) |
---|
243 | { |
---|
244 | parserEventSupport.fireReportError(s); |
---|
245 | base.reportError(s); |
---|
246 | } |
---|
247 | /// <summary>Parser warning-reporting function can be overridden in subclass |
---|
248 | /// </summary> |
---|
249 | public override void reportWarning(string s) |
---|
250 | { |
---|
251 | parserEventSupport.fireReportWarning(s); |
---|
252 | base.reportWarning(s); |
---|
253 | } |
---|
254 | public virtual void setupDebugging(TokenBuffer tokenBuf) |
---|
255 | { |
---|
256 | setupDebugging(null, tokenBuf); |
---|
257 | } |
---|
258 | public virtual void setupDebugging(TokenStream lexer) |
---|
259 | { |
---|
260 | setupDebugging(lexer, null); |
---|
261 | } |
---|
262 | /// <summary>User can override to do their own debugging |
---|
263 | /// </summary> |
---|
264 | protected internal virtual void setupDebugging(TokenStream lexer, TokenBuffer tokenBuf) |
---|
265 | { |
---|
266 | setDebugMode(true); |
---|
267 | // default parser debug setup is ParseView |
---|
268 | try |
---|
269 | { |
---|
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}); |
---|
282 | } |
---|
283 | catch (System.Exception e) |
---|
284 | { |
---|
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); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | public virtual void wakeUp() |
---|
292 | { |
---|
293 | lock(this) |
---|
294 | { |
---|
295 | Monitor.Pulse(this); |
---|
296 | } |
---|
297 | } |
---|
298 | } |
---|
299 | } |
---|