From 1ae3c0104c91235ed59d578665836a2cdc0fb765 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 22 Oct 2012 09:18:29 -0500 Subject: [PATCH 1/3] Simplify FailedPredicateException constructors, encapsulate fields --- .../v4/runtime/DefaultErrorStrategy.java | 2 +- .../v4/runtime/FailedPredicateException.java | 57 +++++++++++-------- .../v4/runtime/RecognitionException.java | 10 ++++ 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java index dad26c7e5..464f10769 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java @@ -229,7 +229,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy { throws RecognitionException { String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()]; - String msg = "rule "+ruleName+" "+e.msg; + String msg = "rule "+ruleName+" "+e.getMessage(); recognizer.notifyErrorListeners((Token) e.offendingToken, msg, e); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java index 6a8d4f9eb..6de56adb5 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java @@ -30,6 +30,7 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.atn.PredicateTransition; +import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.Nullable; /** A semantic predicate failed during validation. Validation of predicates @@ -38,40 +39,50 @@ import org.antlr.v4.runtime.misc.Nullable; * prediction. */ public class FailedPredicateException extends RecognitionException { - public int ruleIndex; - public int predIndex; - public String predicate; - public String msg; + private final int ruleIndex; + private final int predicateIndex; + private final String predicate; - public FailedPredicateException(Parser recognizer) { + public FailedPredicateException(@NotNull Parser recognizer) { this(recognizer, null); } - public FailedPredicateException(Parser recognizer, @Nullable String predicate) { - super(recognizer, recognizer.getInputStream(), recognizer._ctx); - ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s); - PredicateTransition trans = (PredicateTransition)s.transition(0); - ruleIndex = trans.ruleIndex; - predIndex = trans.predIndex; - this.predicate = predicate; - this.msg = String.format("failed predicate: {%s}?", predicate); - Token la = recognizer.getCurrentToken(); - this.offendingToken = la; + public FailedPredicateException(@NotNull Parser recognizer, @Nullable String predicate) { + this(recognizer, predicate, null); } - public FailedPredicateException(Parser recognizer, + public FailedPredicateException(@NotNull Parser recognizer, @Nullable String predicate, - @Nullable String msg) + @Nullable String message) { - super(recognizer, recognizer.getInputStream(), recognizer._ctx); + super(formatMessage(predicate, message), recognizer, recognizer.getInputStream(), recognizer._ctx); ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s); PredicateTransition trans = (PredicateTransition)s.transition(0); - ruleIndex = trans.ruleIndex; - predIndex = trans.predIndex; + this.ruleIndex = trans.ruleIndex; + this.predicateIndex = trans.predIndex; this.predicate = predicate; - this.msg = msg; - Token la = recognizer.getCurrentToken(); - this.offendingToken = la; + this.offendingToken = recognizer.getCurrentToken(); } + public int getRuleIndex() { + return ruleIndex; + } + + public int getPredIndex() { + return predicateIndex; + } + + @Nullable + public String getPredicate() { + return predicate; + } + + @NotNull + private static String formatMessage(@Nullable String predicate, @Nullable String message) { + if (message != null) { + return message; + } + + return String.format("failed predicate: {%s}?", predicate); + } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java index 5a1062eec..c168a9671 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RecognitionException.java @@ -66,6 +66,16 @@ public class RecognitionException extends RuntimeException { if ( ctx!=null ) this.offendingState = ctx.s; } + public RecognitionException(String message, @Nullable Recognizer recognizer, IntStream input, + @Nullable ParserRuleContext ctx) + { + super(message); + this.recognizer = recognizer; + this.input = input; + this.ctx = ctx; + if ( ctx!=null ) this.offendingState = ctx.s; + } + /** Where was the parser in the ATN when the error occurred? * For No viable alternative exceptions, this is the decision state number. * For others, it is the state whose emanating edge we couldn't match. From 4790ab76e141442eda404f4083a71c34f45bd6f3 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 22 Oct 2012 09:40:44 -0500 Subject: [PATCH 2/3] Encapsulate fields in NoViableAltException and LexerNoViableAltException --- .../v4/runtime/DefaultErrorStrategy.java | 4 +-- .../v4/runtime/LexerNoViableAltException.java | 24 +++++++++---- .../v4/runtime/NoViableAltException.java | 36 +++++++++++++------ .../v4/runtime/atn/ParserATNSimulator.java | 2 +- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java index 464f10769..d3eed2a0a 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java +++ b/runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java @@ -205,8 +205,8 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy { TokenStream tokens = recognizer.getInputStream(); String input; if (tokens instanceof TokenStream) { - if ( e.startToken.getType()==Token.EOF ) input = ""; - else input = ((TokenStream)tokens).getText(e.startToken, e.offendingToken); + if ( e.getStartToken().getType()==Token.EOF ) input = ""; + else input = ((TokenStream)tokens).getText(e.getStartToken(), e.offendingToken); } else { input = ""; diff --git a/runtime/Java/src/org/antlr/v4/runtime/LexerNoViableAltException.java b/runtime/Java/src/org/antlr/v4/runtime/LexerNoViableAltException.java index b3a971ce9..6c01c18ee 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/LexerNoViableAltException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/LexerNoViableAltException.java @@ -31,24 +31,36 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.atn.ATNConfigSet; import org.antlr.v4.runtime.misc.Interval; +import org.antlr.v4.runtime.misc.NotNull; +import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.runtime.misc.Utils; public class LexerNoViableAltException extends RecognitionException { /** Matching attempted at what input index? */ - public int startIndex; + private final int startIndex; /** Which configurations did we try at input.index() that couldn't match input.LA(1)? */ - public ATNConfigSet deadEndConfigs; + @Nullable + private final ATNConfigSet deadEndConfigs; - public LexerNoViableAltException(Lexer lexer, - CharStream input, + public LexerNoViableAltException(@Nullable Lexer lexer, + @NotNull CharStream input, int startIndex, - ATNConfigSet deadEndConfigs) { + @Nullable ATNConfigSet deadEndConfigs) { super(lexer, input, null); this.startIndex = startIndex; this.deadEndConfigs = deadEndConfigs; } + public int getStartIndex() { + return startIndex; + } + + @Nullable + public ATNConfigSet getDeadEndConfigs() { + return deadEndConfigs; + } + @Override public CharStream getInputStream() { return (CharStream)super.getInputStream(); @@ -62,6 +74,6 @@ public class LexerNoViableAltException extends RecognitionException { symbol = Utils.escapeWhitespace(symbol, false); } - return "NoViableAltException('" + symbol + "')"; + return String.format("%s('%s')", LexerNoViableAltException.class.getSimpleName(), symbol); } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java index a43784ef8..2403fe7f9 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/NoViableAltException.java @@ -29,6 +29,8 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.atn.ATNConfigSet; +import org.antlr.v4.runtime.misc.NotNull; +import org.antlr.v4.runtime.misc.Nullable; /** Indicates that the parser could not decide which of two or more paths * to take based upon the remaining input. It tracks the starting token @@ -37,33 +39,47 @@ import org.antlr.v4.runtime.atn.ATNConfigSet; */ public class NoViableAltException extends RecognitionException { /** Which configurations did we try at input.index() that couldn't match input.LT(1)? */ - public ATNConfigSet deadEndConfigs; + @Nullable + private final ATNConfigSet deadEndConfigs; /** The token object at the start index; the input stream might * not be buffering tokens so get a reference to it. (At the * time the error occurred, of course the stream needs to keep a * buffer all of the tokens but later we might not have access to those.) */ - public Token startToken; + @NotNull + private final Token startToken; - public NoViableAltException(Parser recognizer) { // LL(1) error - this(recognizer,recognizer.getInputStream(), + public NoViableAltException(@NotNull Parser recognizer) { // LL(1) error + this(recognizer, + recognizer.getInputStream(), recognizer.getCurrentToken(), recognizer.getCurrentToken(), null, recognizer._ctx); } - public NoViableAltException(Parser recognizer, - TokenStream input, - Token startToken, - Token offendingToken, - ATNConfigSet deadEndConfigs, - ParserRuleContext ctx) + public NoViableAltException(@NotNull Parser recognizer, + @NotNull TokenStream input, + @NotNull Token startToken, + @NotNull Token offendingToken, + @Nullable ATNConfigSet deadEndConfigs, + @NotNull ParserRuleContext ctx) { super(recognizer, input, ctx); this.deadEndConfigs = deadEndConfigs; this.startToken = startToken; this.offendingToken = offendingToken; } + + @NotNull + public Token getStartToken() { + return startToken; + } + + @Nullable + public ATNConfigSet getDeadEndConfigs() { + return deadEndConfigs; + } + } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index ca470e926..a5b51e2d3 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -1368,7 +1368,7 @@ public class ParserATNSimulator extends ATNSimulator { public void dumpDeadEndConfigs(@NotNull NoViableAltException nvae) { System.err.println("dead end configs: "); - for (ATNConfig c : nvae.deadEndConfigs) { + for (ATNConfig c : nvae.getDeadEndConfigs()) { String trans = "no edges"; if ( c.state.getNumberOfTransitions()>0 ) { Transition t = c.state.transition(0); From 36c63db2997453a31a6e2f0cd1272251b50d62f1 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 22 Oct 2012 09:52:55 -0500 Subject: [PATCH 3/3] 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; }