forked from jasder/antlr
Merge branch 'encapsulation' of git://github.com/sharwell/antlr4
This commit is contained in:
commit
d5341b55af
|
@ -110,7 +110,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
|
||||||
else {
|
else {
|
||||||
System.err.println("unknown recognition error type: "+e.getClass().getName());
|
System.err.println("unknown recognition error type: "+e.getClass().getName());
|
||||||
if ( recognizer!=null ) {
|
if ( recognizer!=null ) {
|
||||||
recognizer.notifyErrorListeners((Token) e.offendingToken, e.getMessage(), e);
|
recognizer.notifyErrorListeners(e.getOffendingToken(), e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,23 +205,23 @@ 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 = tokens.getText(e.getStartToken(), e.getOffendingToken());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
input = "<unknown input>";
|
input = "<unknown input>";
|
||||||
}
|
}
|
||||||
String msg = "no viable alternative at input "+escapeWSAndQuote(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,
|
public void reportInputMismatch(Parser recognizer,
|
||||||
InputMismatchException e)
|
InputMismatchException e)
|
||||||
throws RecognitionException
|
throws RecognitionException
|
||||||
{
|
{
|
||||||
String msg = "mismatched input "+getTokenErrorDisplay((Token)e.offendingToken)+
|
String msg = "mismatched input "+getTokenErrorDisplay(e.getOffendingToken())+
|
||||||
" expecting "+e.getExpectedTokens().toString(recognizer.getTokenNames());
|
" expecting "+e.getExpectedTokens().toString(recognizer.getTokenNames());
|
||||||
recognizer.notifyErrorListeners((Token) e.offendingToken, msg, e);
|
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reportFailedPredicate(Parser recognizer,
|
public void reportFailedPredicate(Parser recognizer,
|
||||||
|
@ -229,8 +229,8 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
|
||||||
throws RecognitionException
|
throws RecognitionException
|
||||||
{
|
{
|
||||||
String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()];
|
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);
|
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reportUnwantedToken(Parser recognizer) {
|
public void reportUnwantedToken(Parser recognizer) {
|
||||||
|
@ -412,21 +412,11 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getSymbolText(@NotNull Token symbol) {
|
protected String getSymbolText(@NotNull Token symbol) {
|
||||||
if (symbol instanceof Token) {
|
return symbol.getText();
|
||||||
return ((Token)symbol).getText();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return symbol.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int getSymbolType(@NotNull Token symbol) {
|
protected int getSymbolType(@NotNull Token symbol) {
|
||||||
if (symbol instanceof Token) {
|
return symbol.getType();
|
||||||
return ((Token)symbol).getType();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Token.INVALID_TYPE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String escapeWSAndQuote(String s) {
|
protected String escapeWSAndQuote(String s) {
|
||||||
|
|
|
@ -30,6 +30,7 @@ package org.antlr.v4.runtime;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.atn.ATNState;
|
import org.antlr.v4.runtime.atn.ATNState;
|
||||||
import org.antlr.v4.runtime.atn.PredicateTransition;
|
import org.antlr.v4.runtime.atn.PredicateTransition;
|
||||||
|
import org.antlr.v4.runtime.misc.NotNull;
|
||||||
import org.antlr.v4.runtime.misc.Nullable;
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
|
|
||||||
/** A semantic predicate failed during validation. Validation of predicates
|
/** A semantic predicate failed during validation. Validation of predicates
|
||||||
|
@ -38,40 +39,50 @@ import org.antlr.v4.runtime.misc.Nullable;
|
||||||
* prediction.
|
* prediction.
|
||||||
*/
|
*/
|
||||||
public class FailedPredicateException extends RecognitionException {
|
public class FailedPredicateException extends RecognitionException {
|
||||||
public int ruleIndex;
|
private final int ruleIndex;
|
||||||
public int predIndex;
|
private final int predicateIndex;
|
||||||
public String predicate;
|
private final String predicate;
|
||||||
public String msg;
|
|
||||||
|
|
||||||
public FailedPredicateException(Parser recognizer) {
|
public FailedPredicateException(@NotNull Parser recognizer) {
|
||||||
this(recognizer, null);
|
this(recognizer, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FailedPredicateException(Parser recognizer, @Nullable String predicate) {
|
public FailedPredicateException(@NotNull Parser recognizer, @Nullable String predicate) {
|
||||||
super(recognizer, recognizer.getInputStream(), recognizer._ctx);
|
this(recognizer, predicate, null);
|
||||||
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(Parser recognizer,
|
public FailedPredicateException(@NotNull Parser recognizer,
|
||||||
@Nullable String predicate,
|
@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);
|
ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s);
|
||||||
PredicateTransition trans = (PredicateTransition)s.transition(0);
|
PredicateTransition trans = (PredicateTransition)s.transition(0);
|
||||||
ruleIndex = trans.ruleIndex;
|
this.ruleIndex = trans.ruleIndex;
|
||||||
predIndex = trans.predIndex;
|
this.predicateIndex = trans.predIndex;
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
this.msg = msg;
|
this.setOffendingToken(recognizer.getCurrentToken());
|
||||||
Token la = recognizer.getCurrentToken();
|
|
||||||
this.offendingToken = la;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,6 @@ package org.antlr.v4.runtime;
|
||||||
public class InputMismatchException extends RecognitionException {
|
public class InputMismatchException extends RecognitionException {
|
||||||
public InputMismatchException(Parser recognizer) {
|
public InputMismatchException(Parser recognizer) {
|
||||||
super(recognizer, recognizer.getInputStream(), recognizer._ctx);
|
super(recognizer, recognizer.getInputStream(), recognizer._ctx);
|
||||||
Token la = recognizer.getCurrentToken();
|
this.setOffendingToken(recognizer.getCurrentToken());
|
||||||
this.offendingToken = la;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
@ -57,11 +69,11 @@ public class LexerNoViableAltException extends RecognitionException {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String symbol = "";
|
String symbol = "";
|
||||||
if (startIndex >= 0 && startIndex < input.size()) {
|
if (startIndex >= 0 && startIndex < getInputStream().size()) {
|
||||||
symbol = getInputStream().getText(Interval.of(startIndex,startIndex));
|
symbol = getInputStream().getText(Interval.of(startIndex,startIndex));
|
||||||
symbol = Utils.escapeWhitespace(symbol, false);
|
symbol = Utils.escapeWhitespace(symbol, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return "NoViableAltException('" + symbol + "')";
|
return String.format("%s('%s')", LexerNoViableAltException.class.getSimpleName(), symbol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.setOffendingToken(offendingToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Token getStartToken() {
|
||||||
|
return startToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public ATNConfigSet getDeadEndConfigs() {
|
||||||
|
return deadEndConfigs;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,23 +39,23 @@ import org.antlr.v4.runtime.misc.Nullable;
|
||||||
*/
|
*/
|
||||||
public class RecognitionException extends RuntimeException {
|
public class RecognitionException extends RuntimeException {
|
||||||
/** Who threw the exception? */
|
/** Who threw the exception? */
|
||||||
protected Recognizer<?, ?> recognizer;
|
private Recognizer<?, ?> recognizer;
|
||||||
|
|
||||||
// TODO: make a dummy recognizer for the interpreter to use?
|
// TODO: make a dummy recognizer for the interpreter to use?
|
||||||
// Next two (ctx,input) should be what is in recognizer, but
|
// Next two (ctx,input) should be what is in recognizer, but
|
||||||
// won't work when interpreting
|
// 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
|
/** The current Token when an error occurred. Since not all streams
|
||||||
* can retrieve the ith Token, we have to track the Token object.
|
* 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.
|
* 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,
|
public RecognitionException(@Nullable Recognizer<?, ?> recognizer, IntStream input,
|
||||||
@Nullable ParserRuleContext ctx)
|
@Nullable ParserRuleContext ctx)
|
||||||
|
@ -66,13 +66,29 @@ public class RecognitionException extends RuntimeException {
|
||||||
if ( ctx!=null ) this.offendingState = ctx.s;
|
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?
|
/** Where was the parser in the ATN when the error occurred?
|
||||||
* For No viable alternative exceptions, this is the decision state number.
|
* For No viable alternative exceptions, this is the decision state number.
|
||||||
* For others, it is the state whose emanating edge we couldn't match.
|
* For others, it is the state whose emanating edge we couldn't match.
|
||||||
* This will help us tie into the grammar and syntax diagrams in
|
* This will help us tie into the grammar and syntax diagrams in
|
||||||
* ANTLRWorks v2.
|
* ANTLRWorks v2.
|
||||||
*/
|
*/
|
||||||
public int getOffendingState() { return offendingState; }
|
public int getOffendingState() {
|
||||||
|
return offendingState;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final void setOffendingState(int offendingState) {
|
||||||
|
this.offendingState = offendingState;
|
||||||
|
}
|
||||||
|
|
||||||
public IntervalSet getExpectedTokens() {
|
public IntervalSet getExpectedTokens() {
|
||||||
// TODO: do we really need this type check?
|
// TODO: do we really need this type check?
|
||||||
|
@ -94,6 +110,10 @@ public class RecognitionException extends RuntimeException {
|
||||||
return offendingToken;
|
return offendingToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final void setOffendingToken(Token offendingToken) {
|
||||||
|
this.offendingToken = offendingToken;
|
||||||
|
}
|
||||||
|
|
||||||
public Recognizer<?, ?> getRecognizer() {
|
public Recognizer<?, ?> getRecognizer() {
|
||||||
return recognizer;
|
return recognizer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,8 +67,8 @@ public abstract class Recognizer<Symbol, ATNInterpreter extends ATNSimulator> {
|
||||||
|
|
||||||
/** What is the error header, normally line/character position information? */
|
/** What is the error header, normally line/character position information? */
|
||||||
public String getErrorHeader(RecognitionException e) {
|
public String getErrorHeader(RecognitionException e) {
|
||||||
int line = e.offendingToken.getLine();
|
int line = e.getOffendingToken().getLine();
|
||||||
int charPositionInLine = e.offendingToken.getCharPositionInLine();
|
int charPositionInLine = e.getOffendingToken().getCharPositionInLine();
|
||||||
return "line "+line+":"+charPositionInLine;
|
return "line "+line+":"+charPositionInLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1335,7 +1335,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);
|
||||||
|
|
Loading…
Reference in New Issue