err clean up

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9091]
This commit is contained in:
parrt 2011-10-02 12:51:33 -08:00
parent 298a15e017
commit 49571e386f
21 changed files with 93 additions and 547 deletions

View File

@ -109,14 +109,14 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
}
// like matchSet but w/o consume; error checking routine.
public void sync(IntervalSet expecting) {
if ( expecting.contains(getInputStream().LA(1)) ) return;
// System.out.println("failed sync to "+expecting);
IntervalSet followSet = computeErrorRecoverySet();
followSet.addAll(expecting);
NoViableAltException e = new NoViableAltException(this);
recoverFromMismatchedSet(e, followSet);
}
// public void sync(IntervalSet expecting) {
// if ( expecting.contains(getInputStream().LA(1)) ) return;
//// System.out.println("failed sync to "+expecting);
// IntervalSet followSet = computeErrorRecoverySet();
// followSet.addAll(expecting);
// NoViableAltException e = new NoViableAltException(this);
// recoverFromMismatchedSet(e, followSet);
// }
/** Match the wildcard: in a symbol */
public void matchAny() {
@ -461,7 +461,6 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
* mismatched token error. To recover, it sees that LA(1)==';'
* is in the set of tokens that can follow the ')' token
* reference in rule atom. It can assume that you forgot the ')'.
*/
protected Object recoverFromMismatchedToken(int ttype, IntervalSet follow)
throws RecognitionException
{
@ -469,11 +468,9 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
// if next token is what we are looking for then "delete" this token
if ( mismatchIsUnwantedToken(ttype) ) {
e = new UnwantedTokenException(this, getInputStream(), ttype);
/*
System.err.println("recoverFromMismatchedToken deleting "+
((TokenStream)input).LT(1)+
" since "+((TokenStream)input).LT(2)+" is what we want");
*/
getInputStream().consume(); // simply delete extra token
reportError(e); // report after consuming so AW sees the token in the exception
// we want to return the token we're actually matching
@ -492,7 +489,9 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
e = new MismatchedTokenException(this, getInputStream(), ttype);
throw e;
}
*/
/*
public Object recoverFromMismatchedSet(RecognitionException e,
IntervalSet follow)
throws RecognitionException
@ -506,7 +505,7 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
// TODO do single token deletion like above for Token mismatch
throw e;
}
*/
public abstract IntStream getInputStream();
public abstract void setInputStream(IntStream input);
@ -524,11 +523,10 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
{
int line = offendingToken.getLine();
int charPositionInLine = offendingToken.getCharPositionInLine();
int start = getInputStream().index();
int start = offendingToken.getTokenIndex();
int stop = start;
if ( e instanceof NoViableAltException ) {
start = ((NoViableAltException) e).startIndex;
stop = e.offendingTokenIndex;
start = ((NoViableAltException)e).startToken.getTokenIndex();
}
if ( _listeners==null || _listeners.size()==0 ) {
emitErrorMessage("line "+line+":"+charPositionInLine+" "+msg);

View File

@ -96,7 +96,7 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
trackError(recognizer);
String msg = "mismatched input "+getTokenErrorDisplay(e.offendingToken)+
" expecting "+e.expecting.toString(recognizer.getTokenNames());
" expecting "+e.getExpectedTokens().toString(recognizer.getTokenNames());
recognizer.notifyListeners(e.offendingToken, msg, e);
}

View File

@ -1,39 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.runtime;
/** The recognizer did not match anything for a (..)+ loop. */
public class EarlyExitException extends RecognitionException {
/** Used for remote debugger deserialization */
public EarlyExitException() {;}
public EarlyExitException(BaseRecognizer recognizer, IntStream input) {
super(recognizer, input, recognizer._ctx);
}
}

View File

@ -37,23 +37,20 @@ public class FailedPredicateException extends RecognitionException {
public String ruleName;
public String predicateText;
/** Used for remote debugger deserialization */
public FailedPredicateException() {;}
public FailedPredicateException(BaseRecognizer recognizer, String predText) {
super(recognizer);
this.predicateText = predText;
}
public FailedPredicateException(BaseRecognizer recognizer,
IntStream input,
String ruleName,
String predicateText)
{
super(recognizer, input, recognizer._ctx);
this.ruleName = ruleName;
this.predicateText = predicateText;
}
// public FailedPredicateException(BaseRecognizer recognizer,
// IntStream input,
// String ruleName,
// String predicateText)
// {
// super(recognizer, input, recognizer._ctx);
// this.ruleName = ruleName;
// this.predicateText = predicateText;
// }
public String toString() {
return "FailedPredicateException("+ruleName+",{"+predicateText+"}?)";

View File

@ -273,6 +273,7 @@ public abstract class Lexer extends Recognizer<LexerATNSimulator>
return null;
}
/*
public String getErrorMessage(RecognitionException e) {
String msg = null;
if ( e instanceof MismatchedTokenException ) {
@ -310,6 +311,7 @@ public abstract class Lexer extends Recognizer<LexerATNSimulator>
}
return msg;
}
*/
public String getCharErrorDisplay(int c) {
String s = String.valueOf((char)c);

View File

@ -1,46 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.runtime;
/**
*/
public class MismatchedASTNodeException extends RecognitionException {
public MismatchedASTNodeException() {
}
public MismatchedASTNodeException(BaseRecognizer recognizer,
IntStream input, int firstSet)
{
super(recognizer, input, recognizer._ctx);
}
public String toString() {
return "MismatchedTreeNodeException("+getUnexpectedType()+"!="+expecting+")";
}
}

View File

@ -1,42 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.runtime;
public class MismatchedNotSetException extends MismatchedSetException {
/** Used for remote debugger deserialization */
public MismatchedNotSetException() {;}
public MismatchedNotSetException(BaseRecognizer recognizer, IntStream input) {
super(recognizer, input);
}
public String toString() {
return "MismatchedNotSetException("+getUnexpectedType()+"!="+expecting+")";
}
}

View File

@ -1,46 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.runtime;
public class MismatchedRangeException extends RecognitionException {
public int a,b;
/** Used for remote debugger deserialization */
public MismatchedRangeException() {;}
public MismatchedRangeException(BaseRecognizer recognizer, IntStream input, int a, int b) {
super(recognizer, input, recognizer._ctx);
this.a = a;
this.b = b;
}
public String toString() {
return "MismatchedNotSetException("+getUnexpectedType()+" not in ["+a+","+b+"])";
}
}

View File

@ -1,42 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.runtime;
public class MismatchedSetException extends RecognitionException {
/** Used for remote debugger deserialization */
public MismatchedSetException() {;}
public MismatchedSetException(BaseRecognizer recognizer, IntStream input) {
super(recognizer, input, recognizer._ctx);
}
public String toString() {
return "MismatchedSetException("+getUnexpectedType()+"!="+expecting+")";
}
}

View File

@ -1,43 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.runtime;
/** A mismatched char or Token or tree node */
public class MismatchedTokenException extends RecognitionException {
/** Used for remote debugger deserialization */
public MismatchedTokenException() {;}
public MismatchedTokenException(BaseRecognizer recognizer, IntStream input, int expecting) {
super(recognizer, input, recognizer._ctx);
}
public String toString() {
return "MismatchedTokenException("+getUnexpectedType()+"!="+expecting+")";
}
}

View File

@ -1,57 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.runtime;
/** We were expecting a token but it's not found. The current token
* is actually what we wanted next. Used for tree node errors too.
*/
public class MissingTokenException extends MismatchedTokenException {
public Object inserted;
/** Used for remote debugger deserialization */
public MissingTokenException() {;}
public MissingTokenException(BaseRecognizer recognizer, IntStream input, int expecting, Object inserted) {
super(recognizer, input, expecting);
this.inserted = inserted;
}
public int getMissingType() {
return expecting.getSingleElement();
}
public String toString() {
if ( inserted!=null && offendingToken !=null ) {
return "MissingTokenException(inserted "+inserted+" at "+ offendingToken.getText()+")";
}
if ( offendingToken !=null ) {
return "MissingTokenException(at "+ offendingToken.getText()+")";
}
return "MissingTokenException";
}
}

View File

@ -31,9 +31,13 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.atn.ATNConfig;
import org.antlr.v4.runtime.misc.OrderedHashSet;
/** The parser could not decide which path in the decision to take based
* upon the remaining input.
*/
public class NoViableAltException extends RecognitionException {
/** Prediction began at what input index? */
public int startIndex;
/** Which configurations did we try at input.index() that couldn't match input.LT(1)? */
public OrderedHashSet<ATNConfig> 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
@ -41,32 +45,34 @@ public class NoViableAltException extends RecognitionException {
*/
public Token startToken;
/** Which configurations did we try at input.index() that couldn't match input.LT(1)? */
public OrderedHashSet<ATNConfig> deadEndConfigs;
/** Used for remote debugger deserialization */
public NoViableAltException() {;}
public NoViableAltException(BaseRecognizer recognizer) { // LL(1) error
super(recognizer, recognizer.getInputStream(), recognizer._ctx);
this(recognizer,recognizer.getInputStream(),
((TokenStream)recognizer.getInputStream()).LT(1),
((TokenStream)recognizer.getInputStream()).LT(1),
null,
recognizer._ctx);
}
public NoViableAltException(BaseRecognizer recognizer, IntStream input,
public NoViableAltException(BaseRecognizer recognizer,
IntStream input,
Token startToken,
Token offendingToken,
OrderedHashSet<ATNConfig> deadEndConfigs,
RuleContext ctx)
{
super(recognizer, input, ctx);
this.deadEndConfigs = deadEndConfigs;
this.startToken = startToken;
this.startIndex = startToken.getTokenIndex();
this.offendingToken = offendingToken;
}
public String toString() {
if ( recognizer!=null ) {
TokenStream tokens = ((Parser)recognizer).getTokenStream();
String bad = tokens.toString(startIndex, offendingTokenIndex);
return "NoViableAltException(input=\""+bad+"\" last token type is "+getUnexpectedType()+")";
String bad = tokens.toString(startToken.getTokenIndex(),
offendingToken.getTokenIndex());
return "NoViableAltException(input=\""+bad+"\" last token type is "+
getUnexpectedType()+")";
}
return "NoViableAltException(last token type is "+getUnexpectedType()+")";
}

View File

@ -31,49 +31,26 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.runtime.tree.*;
/** The root of the ANTLR exception hierarchy.
*
* To avoid English-only error messages and to generally make things
* as flexible as possible, these exceptions are not created with strings,
* but rather the information necessary to generate an error. Then
* the various reporting methods in Parser and Lexer can be overridden
* to generate a localized error message. For example, MismatchedToken
* exceptions are built with the expected token type.
* So, don't expect getMessage() to return anything.
*
* Note that as of Java 1.4, you can access the stack trace, which means
* that you can compute the complete trace of rules from the start symbol.
* This gives you considerable context information with which to generate
* useful error messages.
*
* ANTLR generates code that throws exceptions upon recognition error and
* also generates code to catch these exceptions in each rule. If you
* want to quit upon first error, you can turn off the automatic error
* handling mechanism using rulecatch action, but you still need to
* override methods mismatch and recoverFromMismatchSet.
*
* In general, the recognition exceptions can track where in a grammar a
* problem occurred and/or what was the expected input. While the parser
* knows its state (such as current input symbol and line info) that
* state can change before the exception is reported so current token index
* is computed and stored at exception time. From this info, you can
* perhaps print an entire line of input not just a single token, for example.
* Better to just say the recognizer had a problem and then let the parser
* figure out a fancy report.
/** The root of the ANTLR exception hierarchy. In general, ANTLR tracks just
* 3 kinds of errors: prediction errors, failed predicate errors, and
* mismatched input errors. In each case, the parser knows where it is
* in the input, where it is in the ATN, the rule invocation stack,
* and what kind of problem occurred.
*/
// TODO: split out lexer one
public class RecognitionException extends RuntimeException {
/** Who threw the exception? */
public BaseRecognizer recognizer;
public RuleContext ctx; // should be what is in recognizer, but won't work when interpreting
// 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
public IntervalSet expecting;
public RuleContext ctx;
public IntStream input;
/** What is index of token/char were we looking at when the error occurred? */
public int offendingTokenIndex;
// public int offendingTokenIndex;
/** The current Token when an error occurred. Since not all streams
* can retrieve the ith Token, we have to track the Token object.
@ -86,19 +63,12 @@ public class RecognitionException extends RuntimeException {
*/
public Object offendingNode;
/** The current char when an error occurred. For lexers. */
public int c;
/** If you are parsing a tree node stream, you will encounter som
* imaginary nodes w/o line/col info. We now search backwards looking
* for most recent token with line/col info, but notify getErrorHeader()
* that info is approximate.
*/
public boolean approximateLineInfo;
/** Used for remote debugger deserialization */
public RecognitionException() {
}
*/
public RecognitionException(BaseRecognizer recognizer) {
this(recognizer, recognizer.getInputStream(), recognizer._ctx);
@ -110,68 +80,21 @@ public class RecognitionException extends RuntimeException {
this.recognizer = recognizer;
this.input = input;
this.ctx = ctx;
// firstSet is what can we're expecting within rule that calls this ctor.
// must combine with context-sensitive FOLLOW of that rule.
// LABitSet viableTokensFollowingThisRule = recognizer.computeNextViableTokenSet();
// this.expecting = viableTokensFollowingThisRule.or(firstSet);
if ( recognizer!=null ) expecting = recognizer._interp.atn.nextTokens(ctx);
this.offendingTokenIndex = input.index();
if ( input instanceof TokenStream ) {
this.offendingToken = ((TokenStream)input).LT(1);
}
else if ( input instanceof ASTNodeStream) {
//extractInformationFromTreeNodeStream(input);
}
else {
this.c = input.LA(1);
}
//this.offendingTokenIndex = input.index();
// if ( input instanceof TokenStream ) {
// this.offendingToken = ((TokenStream)input).LT(1);
// }
// else if ( input instanceof ASTNodeStream) {
// //extractInformationFromTreeNodeStream(input);
// }
}
/*
protected void extractInformationFromTreeNodeStream(IntStream input) {
TreeNodeStream nodes = (TreeNodeStream)input;
this.node = nodes.LT(1);
TreeAdaptor adaptor = nodes.getTreeAdaptor();
Token payload = adaptor.getToken(node);
if ( payload!=null ) {
this.token = payload;
if ( payload.getLine()<= 0 ) {
// imaginary node; no line/pos info; scan backwards
int i = -1;
Object priorNode = nodes.LT(i);
while ( priorNode!=null ) {
Token priorPayload = adaptor.getToken(priorNode);
if ( priorPayload!=null && priorPayload.getLine()>0 ) {
// we found the most recent real line / pos info
this.line = priorPayload.getLine();
this.charPositionInLine = priorPayload.getCharPositionInLine();
this.approximateLineInfo = true;
break;
}
--i;
priorNode = nodes.LT(i);
}
}
else { // node created from real token
this.line = payload.getLine();
this.charPositionInLine = payload.getCharPositionInLine();
}
}
else if ( this.node instanceof Tree) {
this.line = ((Tree)this.node).getLine();
this.charPositionInLine = ((Tree)this.node).getCharPositionInLine();
if ( this.node instanceof CommonTree) {
this.token = ((CommonTree)this.node).token;
}
}
else {
int type = adaptor.getType(this.node);
String text = adaptor.getText(this.node);
this.token = new CommonToken(type, text);
}
public IntervalSet getExpectedTokens() {
if ( recognizer!=null ) return recognizer._interp.atn.nextTokens(ctx);
return null;
}
*/
/** Return the token type or char of the unexpected input element */
public int getUnexpectedType() {
@ -184,8 +107,6 @@ public class RecognitionException extends RuntimeException {
ASTAdaptor adaptor = nodes.getTreeAdaptor();
return adaptor.getType(offendingNode);
}
else {
return c;
}
return Token.INVALID_TYPE;
}
}

View File

@ -57,11 +57,13 @@ public class Recognizer<ATNInterpreter> {
public ATNInterpreter getInterpreter() { return _interp; }
/*
public void displayRecognitionError(RecognitionException e) {
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e);
emitErrorMessage(hdr+" "+msg);
}
*/
/** What error message should be generated for the various
* exception types?
@ -84,7 +86,6 @@ public class Recognizer<ATNInterpreter> {
*
* Override this to change the message generated for one or more
* exception types.
*/
public String getErrorMessage(RecognitionException e) {
String[] tokenNames = getTokenNames();
String msg = e.getMessage();
@ -159,6 +160,7 @@ public class Recognizer<ATNInterpreter> {
}
return msg;
}
*/
/** What is the error header, normally line/character position information? */
public String getErrorHeader(RecognitionException e) {

View File

@ -1,51 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.runtime;
/** An extra token while parsing a TokenStream */
public class UnwantedTokenException extends MismatchedTokenException {
/** Used for remote debugger deserialization */
public UnwantedTokenException() {;}
public UnwantedTokenException(BaseRecognizer recognizer, IntStream input, int expecting) {
super(recognizer, input, expecting);
}
public Token getUnexpectedToken() {
return offendingToken;
}
public String toString() {
String exp = ", expected "+expecting;
if ( offendingToken ==null ) {
return "UnwantedTokenException(found="+null+exp+")";
}
return "UnwantedTokenException(found="+ offendingToken.getText()+exp+")";
}
}

View File

@ -366,7 +366,9 @@ public class ParserATNSimulator extends ATNSimulator {
if ( prevAccept==null ) {
System.out.println("no viable token at input "+ getLookaheadName(input) +", index "+input.index());
NoViableAltException nvae =
new NoViableAltException(parser, input, input.get(startIndex),
new NoViableAltException(parser, input,
input.get(startIndex),
input.LT(1),
closure, outerContext);
throw nvae;
}
@ -801,8 +803,8 @@ public class ParserATNSimulator extends ATNSimulator {
public String getTokenName(int t) {
if ( t==-1 ) return "EOF";
String[] tokensNames = parser.getTokenNames();
if ( parser!=null && tokensNames !=null ) {
if ( parser!=null && parser.getTokenNames()!=null ) {
String[] tokensNames = parser.getTokenNames();
if ( t>=tokensNames.length ) {
System.err.println(t+" ttype out of range: "+Arrays.toString(tokensNames));
System.err.println(((CommonTokenStream)parser.getInputStream()).getTokens());

View File

@ -88,6 +88,7 @@ public class CommonErrorNode extends CommonAST {
}
public String toString() {
/*
if ( trappedException instanceof MissingTokenException ) {
return "<missing type: "+
((MissingTokenException)trappedException).getMissingType()+
@ -105,6 +106,7 @@ public class CommonErrorNode extends CommonAST {
return "<unexpected: "+trappedException.offendingToken +
", resync="+getText()+">";
}
*/
return "<error: "+getText()+">";
}
}

View File

@ -146,7 +146,6 @@ public class TreeParser extends BaseRecognizer {
/** Prefix error message with the grammar name because message is
* always intended for the programmer because the parser built
* the input tree not the user.
*/
public String getErrorHeader(RecognitionException e) {
// todo: might not have token; use node?
int line = e.offendingToken.getLine();
@ -154,10 +153,10 @@ public class TreeParser extends BaseRecognizer {
return getGrammarFileName()+": node from "+
(e.approximateLineInfo?"after ":"")+"line "+line+":"+charPositionInLine;
}
*/
/** Tree parsers parse nodes they usually have a token object as
* payload. Set the exception token and do the default behavior.
*/
public String getErrorMessage(RecognitionException e, String[] tokenNames) {
if ( this instanceof TreeParser ) {
ASTAdaptor adaptor = ((ASTNodeStream)e.input).getTreeAdaptor();
@ -169,6 +168,7 @@ public class TreeParser extends BaseRecognizer {
}
return super.getErrorMessage(e);
}
*/
/** Check if current node in input has a context. Context means sequence
* of nodes towards root of tree. For example, you might say context

View File

@ -15,14 +15,6 @@ javaTypeInitMap ::= [
ParserFile(file, parser, namedActions) ::= <<
// $ANTLR ANTLRVersion> <file.fileName> generatedTimestamp>
<namedActions.header>
import org.antlr.v4.runtime.NoViableAltException;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.EarlyExitException;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.FailedPredicateException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.*;

View File

@ -33,24 +33,15 @@ import org.antlr.v4.automata.*;
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.misc.Utils;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.atn.DecisionState;
import org.antlr.v4.runtime.atn.LexerATNSimulator;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.semantics.SemanticPipeline;
import org.antlr.v4.tool.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupString;
import org.antlr.v4.tool.Rule;
import org.junit.*;
import org.stringtemplate.v4.*;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.*;
import java.io.*;
import java.util.*;
@ -1316,12 +1307,11 @@ public abstract class BaseTest {
}
public Token LT(int i) {
if ( (p+i-1)>=types.size() ) return new CommonToken(-1);
return new CommonToken(types.get(p+i-1));
}
public int range() {
return 0;
CommonToken t;
if ( (p+i-1)>=types.size() ) t = new CommonToken(-1);
t = new CommonToken(types.get(p+i-1));
t.setTokenIndex(p+i-1);
return t;
}
public Token get(int i) {

View File

@ -68,7 +68,7 @@ public class TestATNInterpreter extends BaseTest {
checkMatchedAlt(lg, g, "ac", 1);
}
catch (NoViableAltException re) {
errorIndex = re.offendingTokenIndex;
errorIndex = re.offendingToken.getTokenIndex();
errorTokenType = re.offendingToken.getType();
}
assertEquals(1, errorIndex);
@ -94,7 +94,7 @@ public class TestATNInterpreter extends BaseTest {
checkMatchedAlt(lg, g, "abd", 1);
}
catch (NoViableAltException re) {
errorIndex = re.offendingTokenIndex;
errorIndex = re.offendingToken.getTokenIndex();
errorTokenType = re.offendingToken.getType();
}
assertEquals(2, errorIndex);
@ -117,7 +117,7 @@ public class TestATNInterpreter extends BaseTest {
checkMatchedAlt(lg, g, "abd", 1);
}
catch (NoViableAltException re) {
errorIndex = re.offendingTokenIndex;
errorIndex = re.offendingToken.getTokenIndex();
errorTokenType = re.offendingToken.getType();
}
assertEquals(2, errorIndex);
@ -186,7 +186,7 @@ public class TestATNInterpreter extends BaseTest {
checkMatchedAlt(lg, g, "abd", 1);
}
catch (NoViableAltException re) {
errorIndex = re.offendingTokenIndex;
errorIndex = re.offendingToken.getTokenIndex();
errorTokenType = re.offendingToken.getType();
}
assertEquals(2, errorIndex);