001: package antlr.debug;
002:
003: import antlr.collections.impl.BitSet;
004: import java.io.IOException;
005: import antlr.TokenStreamException;
006: import antlr.*;
007:
008: import antlr.debug.ParserEventSupport;
009:
010: import java.lang.reflect.Constructor;
011:
012: public class LLkDebuggingParser extends LLkParser implements
013: DebuggingParser {
014: protected ParserEventSupport parserEventSupport = new ParserEventSupport(
015: this );
016:
017: private boolean _notDebugMode = false;
018: protected String ruleNames[];
019: protected String semPredNames[];
020:
021: public LLkDebuggingParser(int k_) {
022: super (k_);
023: }
024:
025: public LLkDebuggingParser(ParserSharedInputState state, int k_) {
026: super (state, k_);
027: }
028:
029: public LLkDebuggingParser(TokenBuffer tokenBuf, int k_) {
030: super (tokenBuf, k_);
031: }
032:
033: public LLkDebuggingParser(TokenStream lexer, int k_) {
034: super (lexer, k_);
035: }
036:
037: public void addMessageListener(MessageListener l) {
038: parserEventSupport.addMessageListener(l);
039: }
040:
041: public void addParserListener(ParserListener l) {
042: parserEventSupport.addParserListener(l);
043: }
044:
045: public void addParserMatchListener(ParserMatchListener l) {
046: parserEventSupport.addParserMatchListener(l);
047: }
048:
049: public void addParserTokenListener(ParserTokenListener l) {
050: parserEventSupport.addParserTokenListener(l);
051: }
052:
053: public void addSemanticPredicateListener(SemanticPredicateListener l) {
054: parserEventSupport.addSemanticPredicateListener(l);
055: }
056:
057: public void addSyntacticPredicateListener(
058: SyntacticPredicateListener l) {
059: parserEventSupport.addSyntacticPredicateListener(l);
060: }
061:
062: public void addTraceListener(TraceListener l) {
063: parserEventSupport.addTraceListener(l);
064: }
065:
066: /**Get another token object from the token stream */
067: /*public void consume() {
068: int la_1 = -99;
069: la_1 = LA(1);
070: super.consume();
071: parserEventSupport.fireConsume(la_1);
072: }*/
073: protected void fireEnterRule(int num, int data) {
074: if (isDebugMode())
075: parserEventSupport.fireEnterRule(num, inputState.guessing,
076: data);
077: }
078:
079: protected void fireExitRule(int num, int data) {
080: if (isDebugMode())
081: parserEventSupport.fireExitRule(num, inputState.guessing,
082: data);
083: }
084:
085: protected boolean fireSemanticPredicateEvaluated(int type, int num,
086: boolean condition) {
087: if (isDebugMode())
088: return parserEventSupport.fireSemanticPredicateEvaluated(
089: type, num, condition, inputState.guessing);
090: else
091: return condition;
092: }
093:
094: protected void fireSyntacticPredicateFailed() {
095: if (isDebugMode())
096: parserEventSupport
097: .fireSyntacticPredicateFailed(inputState.guessing);
098: }
099:
100: protected void fireSyntacticPredicateStarted() {
101: if (isDebugMode())
102: parserEventSupport
103: .fireSyntacticPredicateStarted(inputState.guessing);
104: }
105:
106: protected void fireSyntacticPredicateSucceeded() {
107: if (isDebugMode())
108: parserEventSupport
109: .fireSyntacticPredicateSucceeded(inputState.guessing);
110: }
111:
112: public String getRuleName(int num) {
113: return ruleNames[num];
114: }
115:
116: public String getSemPredName(int num) {
117: return semPredNames[num];
118: }
119:
120: public synchronized void goToSleep() {
121: try {
122: wait();
123: } catch (InterruptedException e) {
124: }
125: }
126:
127: public boolean isDebugMode() {
128: return !_notDebugMode;
129: }
130:
131: public boolean isGuessing() {
132: return inputState.guessing > 0;
133: }
134:
135: /** Return the token type of the ith token of lookahead where i=1
136: * is the current token being examined by the parser (i.e., it
137: * has not been matched yet).
138: */
139: /*public int LA(int i) {
140: int la = super.LA(i);
141: parserEventSupport.fireLA(i, la);
142: return la;
143: }*/
144: /**Make sure current lookahead symbol matches token type <tt>t</tt>.
145: * Throw an exception upon mismatch, which is catch by either the
146: * error handler or by the syntactic predicate.
147: */
148: public void match(int t) throws MismatchedTokenException {
149: String text = LT(1).getText();
150: int la_1 = LA(1);
151: try {
152: super .match(t);
153: parserEventSupport.fireMatch(t, text, inputState.guessing);
154: } catch (MismatchedTokenException e) {
155: if (inputState.guessing == 0)
156: parserEventSupport.fireMismatch(la_1, t, text,
157: inputState.guessing);
158: throw e;
159: }
160: }
161:
162: /**Make sure current lookahead symbol matches the given set
163: * Throw an exception upon mismatch, which is catch by either the
164: * error handler or by the syntactic predicate.
165: */
166: public void match(BitSet b) throws MismatchedTokenException {
167: String text = LT(1).getText();
168: int la_1 = LA(1);
169: try {
170: super .match(b);
171: parserEventSupport.fireMatch(la_1, b, text,
172: inputState.guessing);
173: } catch (MismatchedTokenException e) {
174: if (inputState.guessing == 0)
175: parserEventSupport.fireMismatch(la_1, b, text,
176: inputState.guessing);
177: throw e;
178: }
179: }
180:
181: public void matchNot(int t) throws MismatchedTokenException {
182: String text = LT(1).getText();
183: int la_1 = LA(1);
184: try {
185: super .matchNot(t);
186: parserEventSupport.fireMatchNot(la_1, t, text,
187: inputState.guessing);
188: } catch (MismatchedTokenException e) {
189: if (inputState.guessing == 0)
190: parserEventSupport.fireMismatchNot(la_1, t, text,
191: inputState.guessing);
192: throw e;
193: }
194: }
195:
196: public void removeMessageListener(MessageListener l) {
197: parserEventSupport.removeMessageListener(l);
198: }
199:
200: public void removeParserListener(ParserListener l) {
201: parserEventSupport.removeParserListener(l);
202: }
203:
204: public void removeParserMatchListener(ParserMatchListener l) {
205: parserEventSupport.removeParserMatchListener(l);
206: }
207:
208: public void removeParserTokenListener(ParserTokenListener l) {
209: parserEventSupport.removeParserTokenListener(l);
210: }
211:
212: public void removeSemanticPredicateListener(
213: SemanticPredicateListener l) {
214: parserEventSupport.removeSemanticPredicateListener(l);
215: }
216:
217: public void removeSyntacticPredicateListener(
218: SyntacticPredicateListener l) {
219: parserEventSupport.removeSyntacticPredicateListener(l);
220: }
221:
222: public void removeTraceListener(TraceListener l) {
223: parserEventSupport.removeTraceListener(l);
224: }
225:
226: /** Parser error-reporting function can be overridden in subclass */
227: public void reportError(RecognitionException ex) {
228: parserEventSupport.fireReportError(ex);
229: super .reportError(ex);
230: }
231:
232: /** Parser error-reporting function can be overridden in subclass */
233: public void reportError(String s) {
234: parserEventSupport.fireReportError(s);
235: super .reportError(s);
236: }
237:
238: /** Parser warning-reporting function can be overridden in subclass */
239: public void reportWarning(String s) {
240: parserEventSupport.fireReportWarning(s);
241: super .reportWarning(s);
242: }
243:
244: public void setDebugMode(boolean value) {
245: _notDebugMode = !value;
246: }
247:
248: public void setupDebugging(TokenBuffer tokenBuf) {
249: setupDebugging(null, tokenBuf);
250: }
251:
252: public void setupDebugging(TokenStream lexer) {
253: setupDebugging(lexer, null);
254: }
255:
256: /** User can override to do their own debugging */
257: protected void setupDebugging(TokenStream lexer,
258: TokenBuffer tokenBuf) {
259: setDebugMode(true);
260: // default parser debug setup is ParseView
261: try {
262: try {
263: Utils.loadClass("javax.swing.JButton");
264: } catch (ClassNotFoundException e) {
265: System.err
266: .println("Swing is required to use ParseView, but is not present in your CLASSPATH");
267: System.exit(1);
268: }
269: Class c = Utils.loadClass("antlr.parseview.ParseView");
270: Constructor constructor = c.getConstructor(new Class[] {
271: LLkDebuggingParser.class, TokenStream.class,
272: TokenBuffer.class });
273: constructor.newInstance(new Object[] { this , lexer,
274: tokenBuf });
275: } catch (Exception e) {
276: System.err.println("Error initializing ParseView: " + e);
277: System.err
278: .println("Please report this to Scott Stanchfield, thetick@magelang.com");
279: System.exit(1);
280: }
281: }
282:
283: public synchronized void wakeUp() {
284: notify();
285: }
286: }
|