From 36c63db2997453a31a6e2f0cd1272251b50d62f1 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 22 Oct 2012 09:52:55 -0500 Subject: [PATCH] Remove redundant casts, encapsulate fields in RecognitionException --- .../v4/runtime/DefaultErrorStrategy.java | 26 ++++++------------- .../v4/runtime/FailedPredicateException.java | 2 +- .../v4/runtime/InputMismatchException.java | 3 +-- .../v4/runtime/LexerNoViableAltException.java | 2 +- .../v4/runtime/NoViableAltException.java | 2 +- .../v4/runtime/RecognitionException.java | 22 +++++++++++----- .../src/org/antlr/v4/runtime/Recognizer.java | 4 +-- 7 files changed, 30 insertions(+), 31 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java index d3eed2a0a..71006b39b 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java @@ -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 = ""; - else input = ((TokenStream)tokens).getText(e.getStartToken(), e.offendingToken); + else input = tokens.getText(e.getStartToken(), e.getOffendingToken()); } else { 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) { diff --git a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java index 6de56adb5..ece08cc97 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java @@ -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() { diff --git a/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java b/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java index 34ce4766f..ab15e83eb 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/InputMismatchException.java @@ -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()); } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/LexerNoViableAltException.java b/runtime/Java/src/org/antlr/v4/runtime/LexerNoViableAltException.java index 6c01c18ee..d9a8fbbf2 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/LexerNoViableAltException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/LexerNoViableAltException.java @@ -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); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java index 2403fe7f9..1ec07dec7 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java @@ -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 diff --git a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java index c168a9671..3b3bedc3a 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java @@ -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; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java index 9f314dd96..719434f8f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java @@ -67,8 +67,8 @@ public abstract class Recognizer { /** 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; }