Encapsulate fields in NoViableAltException and LexerNoViableAltException

This commit is contained in:
Sam Harwell 2012-10-22 09:40:44 -05:00
parent 1ae3c0104c
commit 4790ab76e1
4 changed files with 47 additions and 19 deletions

View File

@ -205,8 +205,8 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
TokenStream tokens = recognizer.getInputStream(); TokenStream tokens = recognizer.getInputStream();
String input; String input;
if (tokens instanceof TokenStream) { if (tokens instanceof TokenStream) {
if ( e.startToken.getType()==Token.EOF ) input = "<EOF>"; if ( e.getStartToken().getType()==Token.EOF ) input = "<EOF>";
else input = ((TokenStream)tokens).getText(e.startToken, e.offendingToken); else input = ((TokenStream)tokens).getText(e.getStartToken(), e.offendingToken);
} }
else { else {
input = "<unknown input>"; input = "<unknown input>";

View File

@ -31,24 +31,36 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.atn.ATNConfigSet; import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.misc.Interval; 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; import org.antlr.v4.runtime.misc.Utils;
public class LexerNoViableAltException extends RecognitionException { public class LexerNoViableAltException extends RecognitionException {
/** Matching attempted at what input index? */ /** 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)? */ /** 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, public LexerNoViableAltException(@Nullable Lexer lexer,
CharStream input, @NotNull CharStream input,
int startIndex, int startIndex,
ATNConfigSet deadEndConfigs) { @Nullable ATNConfigSet deadEndConfigs) {
super(lexer, input, null); super(lexer, input, null);
this.startIndex = startIndex; this.startIndex = startIndex;
this.deadEndConfigs = deadEndConfigs; this.deadEndConfigs = deadEndConfigs;
} }
public int getStartIndex() {
return startIndex;
}
@Nullable
public ATNConfigSet getDeadEndConfigs() {
return deadEndConfigs;
}
@Override @Override
public CharStream getInputStream() { public CharStream getInputStream() {
return (CharStream)super.getInputStream(); return (CharStream)super.getInputStream();
@ -62,6 +74,6 @@ public class LexerNoViableAltException extends RecognitionException {
symbol = Utils.escapeWhitespace(symbol, false); symbol = Utils.escapeWhitespace(symbol, false);
} }
return "NoViableAltException('" + symbol + "')"; return String.format("%s('%s')", LexerNoViableAltException.class.getSimpleName(), symbol);
} }
} }

View File

@ -29,6 +29,8 @@
package org.antlr.v4.runtime; package org.antlr.v4.runtime;
import org.antlr.v4.runtime.atn.ATNConfigSet; 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 /** 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 * 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 { public class NoViableAltException extends RecognitionException {
/** Which configurations did we try at input.index() that couldn't match input.LT(1)? */ /** 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 /** The token object at the start index; the input stream might
* not be buffering tokens so get a reference to it. (At the * not be buffering tokens so get a reference to it. (At the
* time the error occurred, of course the stream needs to keep a * 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.) * 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 public NoViableAltException(@NotNull Parser recognizer) { // LL(1) error
this(recognizer,recognizer.getInputStream(), this(recognizer,
recognizer.getInputStream(),
recognizer.getCurrentToken(), recognizer.getCurrentToken(),
recognizer.getCurrentToken(), recognizer.getCurrentToken(),
null, null,
recognizer._ctx); recognizer._ctx);
} }
public NoViableAltException(Parser recognizer, public NoViableAltException(@NotNull Parser recognizer,
TokenStream input, @NotNull TokenStream input,
Token startToken, @NotNull Token startToken,
Token offendingToken, @NotNull Token offendingToken,
ATNConfigSet deadEndConfigs, @Nullable ATNConfigSet deadEndConfigs,
ParserRuleContext<?> ctx) @NotNull ParserRuleContext<?> ctx)
{ {
super(recognizer, input, ctx); super(recognizer, input, ctx);
this.deadEndConfigs = deadEndConfigs; this.deadEndConfigs = deadEndConfigs;
this.startToken = startToken; this.startToken = startToken;
this.offendingToken = offendingToken; this.offendingToken = offendingToken;
} }
@NotNull
public Token getStartToken() {
return startToken;
}
@Nullable
public ATNConfigSet getDeadEndConfigs() {
return deadEndConfigs;
}
} }

View File

@ -1368,7 +1368,7 @@ public class ParserATNSimulator extends ATNSimulator {
public void dumpDeadEndConfigs(@NotNull NoViableAltException nvae) { public void dumpDeadEndConfigs(@NotNull NoViableAltException nvae) {
System.err.println("dead end configs: "); System.err.println("dead end configs: ");
for (ATNConfig c : nvae.deadEndConfigs) { for (ATNConfig c : nvae.getDeadEndConfigs()) {
String trans = "no edges"; String trans = "no edges";
if ( c.state.getNumberOfTransitions()>0 ) { if ( c.state.getNumberOfTransitions()>0 ) {
Transition t = c.state.transition(0); Transition t = c.state.transition(0);