001: package persistence.antlr.debug;
002:
003: import persistence.antlr.*;
004: import persistence.antlr.collections.*;
005: import persistence.antlr.collections.impl.*;
006: import java.io.*;
007:
008: public abstract class DebuggingCharScanner extends CharScanner
009: implements DebuggingParser {
010: private ParserEventSupport parserEventSupport = new ParserEventSupport(
011: this );
012: private boolean _notDebugMode = false;
013: protected String ruleNames[];
014: protected String semPredNames[];
015:
016: public DebuggingCharScanner(InputBuffer cb) {
017: super (cb);
018: }
019:
020: public DebuggingCharScanner(LexerSharedInputState state) {
021: super (state);
022: }
023:
024: public void addMessageListener(MessageListener l) {
025: parserEventSupport.addMessageListener(l);
026: }
027:
028: public void addNewLineListener(NewLineListener l) {
029: parserEventSupport.addNewLineListener(l);
030: }
031:
032: public void addParserListener(ParserListener l) {
033: parserEventSupport.addParserListener(l);
034: }
035:
036: public void addParserMatchListener(ParserMatchListener l) {
037: parserEventSupport.addParserMatchListener(l);
038: }
039:
040: public void addParserTokenListener(ParserTokenListener l) {
041: parserEventSupport.addParserTokenListener(l);
042: }
043:
044: public void addSemanticPredicateListener(SemanticPredicateListener l) {
045: parserEventSupport.addSemanticPredicateListener(l);
046: }
047:
048: public void addSyntacticPredicateListener(
049: SyntacticPredicateListener l) {
050: parserEventSupport.addSyntacticPredicateListener(l);
051: }
052:
053: public void addTraceListener(TraceListener l) {
054: parserEventSupport.addTraceListener(l);
055: }
056:
057: public void consume() throws CharStreamException {
058: int la_1 = -99;
059: try {
060: la_1 = LA(1);
061: } catch (CharStreamException ignoreAnIOException) {
062: }
063: super .consume();
064: parserEventSupport.fireConsume(la_1);
065: }
066:
067: protected void fireEnterRule(int num, int data) {
068: if (isDebugMode())
069: parserEventSupport.fireEnterRule(num, inputState.guessing,
070: data);
071: }
072:
073: protected void fireExitRule(int num, int ttype) {
074: if (isDebugMode())
075: parserEventSupport.fireExitRule(num, inputState.guessing,
076: ttype);
077: }
078:
079: protected boolean fireSemanticPredicateEvaluated(int type, int num,
080: boolean condition) {
081: if (isDebugMode())
082: return parserEventSupport.fireSemanticPredicateEvaluated(
083: type, num, condition, inputState.guessing);
084: else
085: return condition;
086: }
087:
088: protected void fireSyntacticPredicateFailed() {
089: if (isDebugMode())
090: parserEventSupport
091: .fireSyntacticPredicateFailed(inputState.guessing);
092: }
093:
094: protected void fireSyntacticPredicateStarted() {
095: if (isDebugMode())
096: parserEventSupport
097: .fireSyntacticPredicateStarted(inputState.guessing);
098: }
099:
100: protected void fireSyntacticPredicateSucceeded() {
101: if (isDebugMode())
102: parserEventSupport
103: .fireSyntacticPredicateSucceeded(inputState.guessing);
104: }
105:
106: public String getRuleName(int num) {
107: return ruleNames[num];
108: }
109:
110: public String getSemPredName(int num) {
111: return semPredNames[num];
112: }
113:
114: public synchronized void goToSleep() {
115: try {
116: wait();
117: } catch (InterruptedException e) {
118: }
119: }
120:
121: public boolean isDebugMode() {
122: return !_notDebugMode;
123: }
124:
125: public char LA(int i) throws CharStreamException {
126: char la = super .LA(i);
127: parserEventSupport.fireLA(i, la);
128: return la;
129: }
130:
131: protected Token makeToken(int t) {
132: // do something with char buffer???
133: // try {
134: // Token tok = (Token)tokenObjectClass.newInstance();
135: // tok.setType(t);
136: // // tok.setText(getText()); done in generated lexer now
137: // tok.setLine(line);
138: // return tok;
139: // }
140: // catch (InstantiationException ie) {
141: // panic("can't instantiate a Token");
142: // }
143: // catch (IllegalAccessException iae) {
144: // panic("Token class is not accessible");
145: // }
146: return super .makeToken(t);
147: }
148:
149: public void match(char c) throws MismatchedCharException,
150: CharStreamException {
151: char la_1 = LA(1);
152: try {
153: super .match(c);
154: parserEventSupport.fireMatch(c, inputState.guessing);
155: } catch (MismatchedCharException e) {
156: if (inputState.guessing == 0)
157: parserEventSupport.fireMismatch(la_1, c,
158: inputState.guessing);
159: throw e;
160: }
161: }
162:
163: public void match(BitSet b) throws MismatchedCharException,
164: CharStreamException {
165: String text = this .text.toString();
166: char la_1 = LA(1);
167: try {
168: super .match(b);
169: parserEventSupport.fireMatch(la_1, b, text,
170: inputState.guessing);
171: } catch (MismatchedCharException e) {
172: if (inputState.guessing == 0)
173: parserEventSupport.fireMismatch(la_1, b, text,
174: inputState.guessing);
175: throw e;
176: }
177: }
178:
179: public void match(String s) throws MismatchedCharException,
180: CharStreamException {
181: StringBuffer la_s = new StringBuffer("");
182: int len = s.length();
183: // peek at the next len worth of characters
184: try {
185: for (int i = 1; i <= len; i++) {
186: la_s.append(super .LA(i));
187: }
188: } catch (Exception ignoreMe) {
189: }
190:
191: try {
192: super .match(s);
193: parserEventSupport.fireMatch(s, inputState.guessing);
194: } catch (MismatchedCharException e) {
195: if (inputState.guessing == 0)
196: parserEventSupport.fireMismatch(la_s.toString(), s,
197: inputState.guessing);
198: throw e;
199: }
200:
201: }
202:
203: public void matchNot(char c) throws MismatchedCharException,
204: CharStreamException {
205: char la_1 = LA(1);
206: try {
207: super .matchNot(c);
208: parserEventSupport.fireMatchNot(la_1, c,
209: inputState.guessing);
210: } catch (MismatchedCharException e) {
211: if (inputState.guessing == 0)
212: parserEventSupport.fireMismatchNot(la_1, c,
213: inputState.guessing);
214: throw e;
215: }
216:
217: }
218:
219: public void matchRange(char c1, char c2)
220: throws MismatchedCharException, CharStreamException {
221: char la_1 = LA(1);
222: try {
223: super .matchRange(c1, c2);
224: parserEventSupport.fireMatch(la_1, "" + c1 + c2,
225: inputState.guessing);
226: } catch (MismatchedCharException e) {
227: if (inputState.guessing == 0)
228: parserEventSupport.fireMismatch(la_1, "" + c1 + c2,
229: inputState.guessing);
230: throw e;
231: }
232:
233: }
234:
235: public void newline() {
236: super .newline();
237: parserEventSupport.fireNewLine(getLine());
238: }
239:
240: public void removeMessageListener(MessageListener l) {
241: parserEventSupport.removeMessageListener(l);
242: }
243:
244: public void removeNewLineListener(NewLineListener l) {
245: parserEventSupport.removeNewLineListener(l);
246: }
247:
248: public void removeParserListener(ParserListener l) {
249: parserEventSupport.removeParserListener(l);
250: }
251:
252: public void removeParserMatchListener(ParserMatchListener l) {
253: parserEventSupport.removeParserMatchListener(l);
254: }
255:
256: public void removeParserTokenListener(ParserTokenListener l) {
257: parserEventSupport.removeParserTokenListener(l);
258: }
259:
260: public void removeSemanticPredicateListener(
261: SemanticPredicateListener l) {
262: parserEventSupport.removeSemanticPredicateListener(l);
263: }
264:
265: public void removeSyntacticPredicateListener(
266: SyntacticPredicateListener l) {
267: parserEventSupport.removeSyntacticPredicateListener(l);
268: }
269:
270: public void removeTraceListener(TraceListener l) {
271: parserEventSupport.removeTraceListener(l);
272: }
273:
274: /** Report exception errors caught in nextToken() */
275: public void reportError(MismatchedCharException e) {
276: parserEventSupport.fireReportError(e);
277: super .reportError(e);
278: }
279:
280: /** Parser error-reporting function can be overridden in subclass */
281: public void reportError(String s) {
282: parserEventSupport.fireReportError(s);
283: super .reportError(s);
284: }
285:
286: /** Parser warning-reporting function can be overridden in subclass */
287: public void reportWarning(String s) {
288: parserEventSupport.fireReportWarning(s);
289: super .reportWarning(s);
290: }
291:
292: public void setDebugMode(boolean value) {
293: _notDebugMode = !value;
294: }
295:
296: public void setupDebugging() {
297: }
298:
299: public synchronized void wakeUp() {
300: notify();
301: }
302: }
|