Remove redundant casts, encapsulate fields in RecognitionException

This commit is contained in:
Sam Harwell 2012-10-22 09:52:55 -05:00
parent 4790ab76e1
commit 36c63db299
7 changed files with 30 additions and 31 deletions

View File

@ -110,7 +110,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
else {
System.err.println("unknown recognition error type: "+e.getClass().getName());
if ( recognizer!=null ) {
recognizer.notifyErrorListeners((Token) e.offendingToken, e.getMessage(), e);
recognizer.notifyErrorListeners(e.getOffendingToken(), e.getMessage(), e);
}
}
}
@ -206,22 +206,22 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
String input;
if (tokens instanceof TokenStream) {
if ( e.getStartToken().getType()==Token.EOF ) input = "<EOF>";
else input = ((TokenStream)tokens).getText(e.getStartToken(), e.offendingToken);
else input = tokens.getText(e.getStartToken(), e.getOffendingToken());
}
else {
input = "<unknown input>";
}
String msg = "no viable alternative at input "+escapeWSAndQuote(input);
recognizer.notifyErrorListeners((Token) e.offendingToken, msg, e);
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
public void reportInputMismatch(Parser recognizer,
InputMismatchException e)
throws RecognitionException
{
String msg = "mismatched input "+getTokenErrorDisplay((Token)e.offendingToken)+
String msg = "mismatched input "+getTokenErrorDisplay(e.getOffendingToken())+
" expecting "+e.getExpectedTokens().toString(recognizer.getTokenNames());
recognizer.notifyErrorListeners((Token) e.offendingToken, msg, e);
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
public void reportFailedPredicate(Parser recognizer,
@ -230,7 +230,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
{
String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()];
String msg = "rule "+ruleName+" "+e.getMessage();
recognizer.notifyErrorListeners((Token) e.offendingToken, msg, e);
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}
public void reportUnwantedToken(Parser recognizer) {
@ -412,21 +412,11 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
}
protected String getSymbolText(@NotNull Token symbol) {
if (symbol instanceof Token) {
return ((Token)symbol).getText();
}
else {
return symbol.toString();
}
return symbol.getText();
}
protected int getSymbolType(@NotNull Token symbol) {
if (symbol instanceof Token) {
return ((Token)symbol).getType();
}
else {
return Token.INVALID_TYPE;
}
return symbol.getType();
}
protected String escapeWSAndQuote(String s) {

View File

@ -61,7 +61,7 @@ public class FailedPredicateException extends RecognitionException {
this.ruleIndex = trans.ruleIndex;
this.predicateIndex = trans.predIndex;
this.predicate = predicate;
this.offendingToken = recognizer.getCurrentToken();
this.setOffendingToken(recognizer.getCurrentToken());
}
public int getRuleIndex() {

View File

@ -34,7 +34,6 @@ package org.antlr.v4.runtime;
public class InputMismatchException extends RecognitionException {
public InputMismatchException(Parser recognizer) {
super(recognizer, recognizer.getInputStream(), recognizer._ctx);
Token la = recognizer.getCurrentToken();
this.offendingToken = la;
this.setOffendingToken(recognizer.getCurrentToken());
}
}

View File

@ -69,7 +69,7 @@ public class LexerNoViableAltException extends RecognitionException {
@Override
public String toString() {
String symbol = "";
if (startIndex >= 0 && startIndex < input.size()) {
if (startIndex >= 0 && startIndex < getInputStream().size()) {
symbol = getInputStream().getText(Interval.of(startIndex,startIndex));
symbol = Utils.escapeWhitespace(symbol, false);
}

View File

@ -69,7 +69,7 @@ public class NoViableAltException extends RecognitionException {
super(recognizer, input, ctx);
this.deadEndConfigs = deadEndConfigs;
this.startToken = startToken;
this.offendingToken = offendingToken;
this.setOffendingToken(offendingToken);
}
@NotNull

View File

@ -39,23 +39,23 @@ import org.antlr.v4.runtime.misc.Nullable;
*/
public class RecognitionException extends RuntimeException {
/** Who threw the exception? */
protected Recognizer<?, ?> recognizer;
private Recognizer<?, ?> recognizer;
// TODO: make a dummy recognizer for the interpreter to use?
// Next two (ctx,input) should be what is in recognizer, but
// won't work when interpreting
protected RuleContext ctx;
private RuleContext ctx;
protected IntStream input;
private IntStream input;
/** The current Token when an error occurred. Since not all streams
* can retrieve the ith Token, we have to track the Token object.
* For parsers. Even when it's a tree parser, token might be set.
*/
protected Token offendingToken;
private Token offendingToken;
protected int offendingState;
private int offendingState;
public RecognitionException(@Nullable Recognizer<?, ?> recognizer, IntStream input,
@Nullable ParserRuleContext ctx)
@ -82,7 +82,13 @@ public class RecognitionException extends RuntimeException {
* This will help us tie into the grammar and syntax diagrams in
* ANTLRWorks v2.
*/
public int getOffendingState() { return offendingState; }
public int getOffendingState() {
return offendingState;
}
protected final void setOffendingState(int offendingState) {
this.offendingState = offendingState;
}
public IntervalSet getExpectedTokens() {
// TODO: do we really need this type check?
@ -104,6 +110,10 @@ public class RecognitionException extends RuntimeException {
return offendingToken;
}
protected final void setOffendingToken(Token offendingToken) {
this.offendingToken = offendingToken;
}
public Recognizer<?, ?> getRecognizer() {
return recognizer;
}

View File

@ -67,8 +67,8 @@ public abstract class Recognizer<Symbol, ATNInterpreter extends ATNSimulator> {
/** What is the error header, normally line/character position information? */
public String getErrorHeader(RecognitionException e) {
int line = e.offendingToken.getLine();
int charPositionInLine = e.offendingToken.getCharPositionInLine();
int line = e.getOffendingToken().getLine();
int charPositionInLine = e.getOffendingToken().getCharPositionInLine();
return "line "+line+":"+charPositionInLine;
}