KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > debug > LLkDebuggingParser


1 package persistence.antlr.debug;
2
3 import persistence.antlr.ParserSharedInputState;
4 import persistence.antlr.TokenStreamException;
5 import persistence.antlr.LLkParser;
6 import persistence.antlr.TokenBuffer;
7 import persistence.antlr.TokenStream;
8 import persistence.antlr.MismatchedTokenException;
9 import persistence.antlr.RecognitionException;
10 import persistence.antlr.collections.impl.BitSet;
11 import java.io.IOException JavaDoc;
12 import persistence.antlr.TokenStreamException;
13
14 import persistence.antlr.debug.ParserEventSupport;
15
16 import java.lang.reflect.Constructor JavaDoc;
17
18 public class LLkDebuggingParser extends LLkParser implements DebuggingParser {
19     protected ParserEventSupport parserEventSupport = new ParserEventSupport(this);
20
21     private boolean _notDebugMode = false;
22     protected String JavaDoc ruleNames[];
23     protected String JavaDoc semPredNames[];
24
25
26     public LLkDebuggingParser(int k_) {
27         super(k_);
28     }
29     public LLkDebuggingParser(ParserSharedInputState state, int k_) {
30         super(state, k_);
31     }
32     public LLkDebuggingParser(TokenBuffer tokenBuf, int k_) {
33         super(tokenBuf, k_);
34     }
35     public LLkDebuggingParser(TokenStream lexer, int k_) {
36         super(lexer, k_);
37     }
38     public void addMessageListener(MessageListener l) {
39         parserEventSupport.addMessageListener(l);
40     }
41     public void addParserListener(ParserListener l) {
42         parserEventSupport.addParserListener(l);
43     }
44     public void addParserMatchListener(ParserMatchListener l) {
45         parserEventSupport.addParserMatchListener(l);
46     }
47     public void addParserTokenListener(ParserTokenListener l) {
48         parserEventSupport.addParserTokenListener(l);
49     }
50     public void addSemanticPredicateListener(SemanticPredicateListener l) {
51         parserEventSupport.addSemanticPredicateListener(l);
52     }
53     public void addSyntacticPredicateListener(SyntacticPredicateListener l) {
54         parserEventSupport.addSyntacticPredicateListener(l);
55     }
56     public void addTraceListener(TraceListener l) {
57         parserEventSupport.addTraceListener(l);
58     }
59     /**Get another token object from the token stream */
60     public void consume() {
61         int la_1 = -99;
62         try {la_1 = LA(1);}
63         catch (TokenStreamException ignoreAnException) {}
64         super.consume();
65         parserEventSupport.fireConsume(la_1);
66     }
67     protected void fireEnterRule(int num,int data) {
68         if (isDebugMode())
69             parserEventSupport.fireEnterRule(num,inputState.guessing,data);
70     }
71     protected void fireExitRule(int num,int data) {
72         if (isDebugMode())
73             parserEventSupport.fireExitRule(num,inputState.guessing,data);
74     }
75     protected boolean fireSemanticPredicateEvaluated(int type, int num, boolean condition) {
76         if (isDebugMode())
77             return parserEventSupport.fireSemanticPredicateEvaluated(type,num,condition,inputState.guessing);
78         else
79             return condition;
80     }
81     protected void fireSyntacticPredicateFailed() {
82         if (isDebugMode())
83             parserEventSupport.fireSyntacticPredicateFailed(inputState.guessing);
84     }
85     protected void fireSyntacticPredicateStarted() {
86         if (isDebugMode())
87             parserEventSupport.fireSyntacticPredicateStarted(inputState.guessing);
88     }
89     protected void fireSyntacticPredicateSucceeded() {
90         if (isDebugMode())
91             parserEventSupport.fireSyntacticPredicateSucceeded(inputState.guessing);
92     }
93     public String JavaDoc getRuleName(int num) {
94         return ruleNames[num];
95     }
96     public String JavaDoc getSemPredName(int num) {
97         return semPredNames[num];
98     }
99     public synchronized void goToSleep() {
100         try {wait();}
101         catch (InterruptedException JavaDoc e) { }
102     }
103     public boolean isDebugMode() {
104         return !_notDebugMode;
105     }
106     public boolean isGuessing() {
107         return inputState.guessing > 0;
108     }
109     /** Return the token type of the ith token of lookahead where i=1
110      * is the current token being examined by the parser (i.e., it
111      * has not been matched yet).
112      */

113     public int LA(int i) throws TokenStreamException {
114         int la = super.LA(i);
115         parserEventSupport.fireLA(i, la);
116         return la;
117     }
118     /**Make sure current lookahead symbol matches token type <tt>t</tt>.
119      * Throw an exception upon mismatch, which is catch by either the
120      * error handler or by the syntactic predicate.
121      */

122     public void match(int t) throws MismatchedTokenException, TokenStreamException {
123         String JavaDoc text = LT(1).getText();
124         int la_1 = LA(1);
125         try {
126             super.match(t);
127             parserEventSupport.fireMatch(t, text, inputState.guessing);
128         }
129         catch (MismatchedTokenException e) {
130             if (inputState.guessing == 0)
131                 parserEventSupport.fireMismatch(la_1, t, text, inputState.guessing);
132             throw e;
133         }
134     }
135     /**Make sure current lookahead symbol matches the given set
136      * Throw an exception upon mismatch, which is catch by either the
137      * error handler or by the syntactic predicate.
138      */

139     public void match(BitSet b) throws MismatchedTokenException, TokenStreamException {
140         String JavaDoc text = LT(1).getText();
141         int la_1 = LA(1);
142         try {
143             super.match(b);
144             parserEventSupport.fireMatch(la_1,b, text, inputState.guessing);
145         }
146         catch (MismatchedTokenException e) {
147             if (inputState.guessing == 0)
148                 parserEventSupport.fireMismatch(la_1, b, text, inputState.guessing);
149             throw e;
150         }
151     }
152     public void matchNot(int t) throws MismatchedTokenException, TokenStreamException {
153         String JavaDoc text = LT(1).getText();
154         int la_1 = LA(1);
155         try {
156             super.matchNot(t);
157             parserEventSupport.fireMatchNot(la_1, t, text, inputState.guessing);
158         }
159         catch (MismatchedTokenException e) {
160             if (inputState.guessing == 0)
161                 parserEventSupport.fireMismatchNot(la_1, t, text, inputState.guessing);
162             throw e;
163         }
164     }
165     public void removeMessageListener(MessageListener l) {
166         parserEventSupport.removeMessageListener(l);
167     }
168     public void removeParserListener(ParserListener l) {
169         parserEventSupport.removeParserListener(l);
170     }
171     public void removeParserMatchListener(ParserMatchListener l) {
172         parserEventSupport.removeParserMatchListener(l);
173     }
174     public void removeParserTokenListener(ParserTokenListener l) {
175         parserEventSupport.removeParserTokenListener(l);
176     }
177     public void removeSemanticPredicateListener(SemanticPredicateListener l) {
178         parserEventSupport.removeSemanticPredicateListener(l);
179     }
180     public void removeSyntacticPredicateListener(SyntacticPredicateListener l) {
181         parserEventSupport.removeSyntacticPredicateListener(l);
182     }
183     public void removeTraceListener(TraceListener l) {
184         parserEventSupport.removeTraceListener(l);
185     }
186     /** Parser error-reporting function can be overridden in subclass */
187     public void reportError(RecognitionException ex) {
188         parserEventSupport.fireReportError(ex);
189         super.reportError(ex);
190     }
191     /** Parser error-reporting function can be overridden in subclass */
192     public void reportError(String JavaDoc s) {
193         parserEventSupport.fireReportError(s);
194         super.reportError(s);
195     }
196     /** Parser warning-reporting function can be overridden in subclass */
197     public void reportWarning(String JavaDoc s) {
198         parserEventSupport.fireReportWarning(s);
199         super.reportWarning(s);
200     }
201     public void setDebugMode(boolean value) {
202         _notDebugMode = !value;
203     }
204     public void setupDebugging(TokenBuffer tokenBuf) {
205         setupDebugging(null, tokenBuf);
206     }
207     public void setupDebugging(TokenStream lexer) {
208         setupDebugging(lexer, null);
209     }
210     /** User can override to do their own debugging */
211     protected void setupDebugging(TokenStream lexer, TokenBuffer tokenBuf) {
212         setDebugMode(true);
213         // default parser debug setup is ParseView
214
try {
215             try {
216                 Class.forName("javax.swing.JButton");
217             }
218             catch (ClassNotFoundException JavaDoc e) {
219                 System.err.println("Swing is required to use ParseView, but is not present in your CLASSPATH");
220                 System.exit(1);
221             }
222             Class JavaDoc c = Class.forName("antlr.parseview.ParseView");
223             Constructor JavaDoc constructor = c.getConstructor(new Class JavaDoc[] {LLkDebuggingParser.class, TokenStream.class, TokenBuffer.class});
224             constructor.newInstance(new Object JavaDoc[] {this, lexer, tokenBuf});
225         }
226         catch(Exception JavaDoc e) {
227             System.err.println("Error initializing ParseView: "+e);
228             System.err.println("Please report this to Scott Stanchfield, thetick@magelang.com");
229             System.exit(1);
230         }
231     }
232     public synchronized void wakeUp() {
233         notify();
234     }
235 }
236
Popular Tags