1 package antlr.debug;
2 
3 import antlr.*;
4 import antlr.collections.*;
5 import antlr.collections.impl.*;
6 import java.io.*;
7 
8 public abstract class DebuggingCharScanner extends CharScanner implements DebuggingParser {
9 	private ParserEventSupport parserEventSupport = new ParserEventSupport(this);
10 	private boolean _notDebugMode = false;
11 	protected String ruleNames[];
12 	protected String semPredNames[];
13 
14 
DebuggingCharScanner(InputBuffer cb)15 	public DebuggingCharScanner(InputBuffer cb) {
16 		super(cb);
17 	}
DebuggingCharScanner(LexerSharedInputState state)18 	public DebuggingCharScanner(LexerSharedInputState state) {
19 		super(state);
20 	}
addMessageListener(MessageListener l)21 	public void addMessageListener(MessageListener l) {
22 		parserEventSupport.addMessageListener(l);
23 	}
addNewLineListener(NewLineListener l)24 	public void addNewLineListener(NewLineListener l) {
25 		parserEventSupport.addNewLineListener(l);
26 	}
addParserListener(ParserListener l)27 	public void addParserListener(ParserListener l) {
28 		parserEventSupport.addParserListener(l);
29 	}
addParserMatchListener(ParserMatchListener l)30 	public void addParserMatchListener(ParserMatchListener l) {
31 		parserEventSupport.addParserMatchListener(l);
32 	}
addParserTokenListener(ParserTokenListener l)33 	public void addParserTokenListener(ParserTokenListener l) {
34 		parserEventSupport.addParserTokenListener(l);
35 	}
addSemanticPredicateListener(SemanticPredicateListener l)36 	public void addSemanticPredicateListener(SemanticPredicateListener l) {
37 		parserEventSupport.addSemanticPredicateListener(l);
38 	}
addSyntacticPredicateListener(SyntacticPredicateListener l)39 	public void addSyntacticPredicateListener(SyntacticPredicateListener l) {
40 		parserEventSupport.addSyntacticPredicateListener(l);
41 	}
addTraceListener(TraceListener l)42 	public void addTraceListener(TraceListener l) {
43 		parserEventSupport.addTraceListener(l);
44 	}
consume()45 	public void consume() throws CharStreamException {
46 		int la_1 = -99;
47 		try {la_1 = LA(1);}
48 		catch (CharStreamException ignoreAnIOException) {}
49 		super.consume();
50 		parserEventSupport.fireConsume(la_1);
51 	}
fireEnterRule(int num, int data)52 	protected void fireEnterRule(int num, int data) {
53 		if (isDebugMode())
54 			parserEventSupport.fireEnterRule(num,inputState.guessing,data);
55 	}
fireExitRule(int num, int ttype)56 	protected void fireExitRule(int num, int ttype) {
57 		if (isDebugMode())
58 			parserEventSupport.fireExitRule(num,inputState.guessing, ttype);
59 	}
fireSemanticPredicateEvaluated(int type, int num, boolean condition)60 	protected boolean fireSemanticPredicateEvaluated(int type, int num, boolean condition) {
61 		if (isDebugMode())
62 			return parserEventSupport.fireSemanticPredicateEvaluated(type,num,condition,inputState.guessing);
63 		else
64 			return condition;
65 	}
fireSyntacticPredicateFailed()66 	protected void fireSyntacticPredicateFailed() {
67 		if (isDebugMode())
68 			parserEventSupport.fireSyntacticPredicateFailed(inputState.guessing);
69 	}
fireSyntacticPredicateStarted()70 	protected void fireSyntacticPredicateStarted() {
71 		if (isDebugMode())
72 			parserEventSupport.fireSyntacticPredicateStarted(inputState.guessing);
73 	}
fireSyntacticPredicateSucceeded()74 	protected void fireSyntacticPredicateSucceeded() {
75 		if (isDebugMode())
76 			parserEventSupport.fireSyntacticPredicateSucceeded(inputState.guessing);
77 	}
getRuleName(int num)78 	public String getRuleName(int num) {
79 		return ruleNames[num];
80 	}
getSemPredName(int num)81 	public String getSemPredName(int num) {
82 		return semPredNames[num];
83 	}
goToSleep()84 	public synchronized void goToSleep() {
85 		try {wait();}
86 		catch (InterruptedException e) {	}
87 	}
isDebugMode()88 	public boolean isDebugMode() {
89 		return !_notDebugMode;
90 	}
LA(int i)91 	public char LA(int i) throws CharStreamException {
92 		char la = super.LA(i);
93 		parserEventSupport.fireLA(i, la);
94 		return la;
95 	}
makeToken(int t)96 	protected Token makeToken(int t) {
97 		// do something with char buffer???
98 //		try {
99 //			Token tok = (Token)tokenObjectClass.newInstance();
100 //			tok.setType(t);
101 //			// tok.setText(getText()); done in generated lexer now
102 //			tok.setLine(line);
103 //			return tok;
104 //		}
105 //		catch (InstantiationException ie) {
106 //			panic("can't instantiate a Token");
107 //		}
108 //		catch (IllegalAccessException iae) {
109 //			panic("Token class is not accessible");
110 //		}
111 		return super.makeToken(t);
112 	}
match(char c)113 	public void match(char c) throws MismatchedCharException, CharStreamException {
114 		char la_1 = LA(1);
115 		try {
116 			super.match(c);
117 			parserEventSupport.fireMatch(c, inputState.guessing);
118 		}
119 		catch (MismatchedCharException e) {
120 			if (inputState.guessing == 0)
121 				parserEventSupport.fireMismatch(la_1, c, inputState.guessing);
122 			throw e;
123 		}
124 	}
match(BitSet b)125 	public void match(BitSet b) throws MismatchedCharException, CharStreamException {
126 		String text = this.text.toString();
127 		char la_1 = LA(1);
128 		try {
129 			super.match(b);
130 			parserEventSupport.fireMatch(la_1, b, text, inputState.guessing);
131 		}
132 		catch (MismatchedCharException e) {
133 			if (inputState.guessing == 0)
134 				parserEventSupport.fireMismatch(la_1, b, text, inputState.guessing);
135 			throw e;
136 		}
137 	}
match(String s)138 	public void match(String s) throws MismatchedCharException, CharStreamException {
139 		StringBuffer la_s = new StringBuffer("");
140 		int len = s.length();
141 		// peek at the next len worth of characters
142 		try {
143 			for(int i = 1; i <= len; i++) {
144 				la_s.append(super.LA(i));
145 			}
146 		}
147 		catch(Exception ignoreMe) {}
148 
149 		try {
150 			super.match(s);
151 			parserEventSupport.fireMatch(s, inputState.guessing);
152 		}
153 		catch (MismatchedCharException e) {
154 			if (inputState.guessing == 0)
155 				parserEventSupport.fireMismatch(la_s.toString(), s, inputState.guessing);
156 			throw e;
157 		}
158 
159 	}
matchNot(char c)160 	public void matchNot(char c) throws MismatchedCharException, CharStreamException {
161 		char la_1 = LA(1);
162 		try {
163 			super.matchNot(c);
164 			parserEventSupport.fireMatchNot(la_1, c, inputState.guessing);
165 		}
166 		catch (MismatchedCharException e) {
167 			if (inputState.guessing == 0)
168 				parserEventSupport.fireMismatchNot(la_1, c, inputState.guessing);
169 			throw e;
170 		}
171 
172 	}
matchRange(char c1, char c2)173 	public void matchRange(char c1, char c2) throws MismatchedCharException, CharStreamException {
174 		char la_1 = LA(1);
175 		try {
176 			super.matchRange(c1,c2);
177 			parserEventSupport.fireMatch(la_1, ""+c1+c2, inputState.guessing);
178 		}
179 		catch (MismatchedCharException e) {
180 			if (inputState.guessing == 0)
181 				parserEventSupport.fireMismatch(la_1, ""+c1+c2, inputState.guessing);
182 			throw e;
183 		}
184 
185 	}
newline()186 	public void newline() {
187 		super.newline();
188 		parserEventSupport.fireNewLine(getLine());
189 	}
removeMessageListener(MessageListener l)190 	public void removeMessageListener(MessageListener l) {
191 		parserEventSupport.removeMessageListener(l);
192 	}
removeNewLineListener(NewLineListener l)193 	public void removeNewLineListener(NewLineListener l) {
194 		parserEventSupport.removeNewLineListener(l);
195 	}
removeParserListener(ParserListener l)196 	public void removeParserListener(ParserListener l) {
197 		parserEventSupport.removeParserListener(l);
198 	}
removeParserMatchListener(ParserMatchListener l)199 	public void removeParserMatchListener(ParserMatchListener l) {
200 		parserEventSupport.removeParserMatchListener(l);
201 	}
removeParserTokenListener(ParserTokenListener l)202 	public void removeParserTokenListener(ParserTokenListener l) {
203 		parserEventSupport.removeParserTokenListener(l);
204 	}
removeSemanticPredicateListener(SemanticPredicateListener l)205 	public void removeSemanticPredicateListener(SemanticPredicateListener l) {
206 		parserEventSupport.removeSemanticPredicateListener(l);
207 	}
removeSyntacticPredicateListener(SyntacticPredicateListener l)208 	public void removeSyntacticPredicateListener(SyntacticPredicateListener l) {
209 		parserEventSupport.removeSyntacticPredicateListener(l);
210 	}
removeTraceListener(TraceListener l)211 	public void removeTraceListener(TraceListener l) {
212 		parserEventSupport.removeTraceListener(l);
213 	}
214 	/** Report exception errors caught in nextToken() */
reportError(MismatchedCharException e)215 	public void reportError(MismatchedCharException e) {
216 		parserEventSupport.fireReportError(e);
217 		super.reportError(e);
218 	}
219 	/** Parser error-reporting function can be overridden in subclass */
reportError(String s)220 	public void reportError(String s) {
221 		parserEventSupport.fireReportError(s);
222 		super.reportError(s);
223 	}
224 	/** Parser warning-reporting function can be overridden in subclass */
reportWarning(String s)225 	public void reportWarning(String s) {
226 		parserEventSupport.fireReportWarning(s);
227 		super.reportWarning(s);
228 	}
setDebugMode(boolean value)229 	public void setDebugMode(boolean value) {
230 		_notDebugMode = !value;
231 	}
setupDebugging()232 	public void setupDebugging() {
233 	}
wakeUp()234 	public synchronized void wakeUp() {
235 		notify();
236 	}
237 }
238