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();
String input;
if (tokens instanceof TokenStream) {
if ( e.startToken.getType()==Token.EOF ) input = "<EOF>";
else input = ((TokenStream)tokens).getText(e.startToken, e.offendingToken);
if ( e.getStartToken().getType()==Token.EOF ) input = "<EOF>";
else input = ((TokenStream)tokens).getText(e.getStartToken(), e.offendingToken);
}
else {
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.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);
}
}

View File

@ -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;
}
}

View File

@ -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);