Merge branch 'encapsulation' of git://github.com/sharwell/antlr4

This commit is contained in:
Terence Parr 2012-10-24 12:59:46 -07:00
commit d5341b55af
8 changed files with 120 additions and 72 deletions

View File

@ -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);
}
}
}
@ -205,23 +205,23 @@ 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 = tokens.getText(e.getStartToken(), e.getOffendingToken());
}
else {
input = "<unknown 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,
@ -229,8 +229,8 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
throws RecognitionException
{
String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()];
String msg = "rule "+ruleName+" "+e.msg;
recognizer.notifyErrorListeners((Token) e.offendingToken, msg, e);
String msg = "rule "+ruleName+" "+e.getMessage();
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) {

View File

@ -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.setOffendingToken(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);
}
}

View File

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

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();
@ -57,11 +69,11 @@ 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);
}
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;
this.setOffendingToken(offendingToken);
}
@NotNull
public Token getStartToken() {
return startToken;
}
@Nullable
public ATNConfigSet getDeadEndConfigs() {
return deadEndConfigs;
}
}

View File

@ -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)
@ -66,13 +66,29 @@ 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.
* 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?
@ -94,6 +110,10 @@ public class RecognitionException extends RuntimeException {
return offendingToken;
}
protected final void setOffendingToken(Token offendingToken) {
this.offendingToken = offendingToken;
}
public Recognizer<?, ?> getRecognizer() {
return recognizer;
}

View File

@ -67,8 +67,8 @@ public abstract class Recognizer<Symbol, ATNInterpreter extends ATNSimulator> {
/** 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;
}

View File

@ -1335,7 +1335,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);