rename test. tweak errs
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9089]
This commit is contained in:
parent
6b4e9905fb
commit
9894221249
|
@ -17,6 +17,8 @@ package org.antlr.v4.runtime;
|
|||
* can be shared between multiple parsers running at the same time.
|
||||
* This is just for flexibility, not that we need it for the default system.
|
||||
*
|
||||
* TODO: To bail out upon first error, simply rethrow e?
|
||||
*
|
||||
* TODO: what to do about lexers
|
||||
*/
|
||||
public interface ANTLRErrorStrategy {
|
||||
|
@ -40,8 +42,6 @@ public interface ANTLRErrorStrategy {
|
|||
* Because we can recover from a single token deletions by
|
||||
* "inserting" tokens, we need to specify what that implicitly created
|
||||
* token is. We use object, because it could be a tree node.
|
||||
*
|
||||
* To bail out upon first error, simply rethrow e.
|
||||
*/
|
||||
Object recoverInline(BaseRecognizer recognizer)
|
||||
throws RecognitionException;
|
||||
|
@ -49,13 +49,36 @@ public interface ANTLRErrorStrategy {
|
|||
/** Resynchronize the parser by consuming tokens until we find one
|
||||
* in the resynchronization set--loosely the set of tokens that can follow
|
||||
* the current rule.
|
||||
*
|
||||
* To bail out upon first error, simply rethrow e.
|
||||
*/
|
||||
void recover(BaseRecognizer recognizer);
|
||||
|
||||
/** Implement Jim Idle's magic sync mechanism in closures and optional
|
||||
* subrules. E.g.,
|
||||
*
|
||||
* a : sync ( stuff sync )* ;
|
||||
* sync : {consume to what can follow sync} ;
|
||||
*
|
||||
* Previous versions of ANTLR did a poor job of their recovery within
|
||||
* loops. A single mismatch token or missing token would force the parser
|
||||
* to bail out of the entire rules surrounding the loop. So, for rule
|
||||
*
|
||||
* classDef : 'class' ID '{' member* '}'
|
||||
*
|
||||
* input with an extra token between members would force the parser to
|
||||
* consume until it found the next class definition rather than the
|
||||
* next member definition of the current class.
|
||||
*
|
||||
* This functionality cost a little bit of effort because the parser
|
||||
* has to compare token set at the start of the loop and add each
|
||||
* iteration. If for some reason speed is suffering for you, you can
|
||||
* turn off this functionality by simply overriding this method as
|
||||
* a blank { }.
|
||||
*/
|
||||
void sync(BaseRecognizer recognizer);
|
||||
|
||||
/** Reset the error handler. The parser invokes this
|
||||
* from its own reset method.
|
||||
* when it matches a valid token (indicating no longer in recovery mode)
|
||||
* and from its own reset method.
|
||||
*/
|
||||
void reset();
|
||||
}
|
||||
|
|
|
@ -29,7 +29,13 @@
|
|||
|
||||
package org.antlr.v4.runtime;
|
||||
|
||||
import com.sun.istack.internal.Nullable;
|
||||
|
||||
/** */
|
||||
public interface ANTLRParserListener {
|
||||
public void error(int line, int charPositionInLine, String msg);
|
||||
public void error(BaseRecognizer recognizer,
|
||||
int line,
|
||||
int charPositionInLine,
|
||||
String msg,
|
||||
@Nullable RecognitionException e);
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
*/
|
||||
package org.antlr.v4.runtime;
|
||||
|
||||
import com.sun.istack.internal.Nullable;
|
||||
import org.antlr.v4.runtime.atn.*;
|
||||
import org.antlr.v4.runtime.misc.*;
|
||||
|
||||
|
@ -69,6 +70,7 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
|
|||
/** reset the parser's state */
|
||||
public void reset() {
|
||||
if ( getInputStream()!=null ) getInputStream().seek(0);
|
||||
_errHandler.reset();
|
||||
errorRecovery = false;
|
||||
_ctx = null;
|
||||
}
|
||||
|
@ -91,6 +93,7 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
|
|||
if ( getInputStream().LA(1)==ttype ) {
|
||||
getInputStream().consume();
|
||||
errorRecovery = false;
|
||||
_errHandler.reset();
|
||||
if ( buildParseTrees ) _ctx.addChild((Token)matchedSymbol);
|
||||
return matchedSymbol;
|
||||
}
|
||||
|
@ -219,7 +222,7 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
|
|||
syntaxErrors++; // don't count spurious
|
||||
errorRecovery = true;
|
||||
|
||||
notifyListeners(e.line, e.charPositionInLine, e.getMessage());
|
||||
notifyListeners(e.line, e.charPositionInLine, e.getMessage(), e);
|
||||
}
|
||||
|
||||
|
||||
|
@ -516,13 +519,15 @@ public abstract class BaseRecognizer extends Recognizer<ParserATNSimulator> {
|
|||
*/
|
||||
protected Object getCurrentInputSymbol() { return null; }
|
||||
|
||||
public void notifyListeners(int line, int charPositionInLine, String msg) {
|
||||
public void notifyListeners(int line, int charPositionInLine, String msg,
|
||||
@Nullable RecognitionException e)
|
||||
{
|
||||
if ( _listeners==null || _listeners.size()==0 ) {
|
||||
emitErrorMessage("line "+line+":"+charPositionInLine+" "+msg);
|
||||
return;
|
||||
}
|
||||
for (ANTLRParserListener pl : _listeners) {
|
||||
pl.error(line, charPositionInLine, msg);
|
||||
pl.error(this, line, charPositionInLine, msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
|
|||
else {
|
||||
System.err.println("unknown recognition error type: "+e.getClass().getName());
|
||||
if ( recognizer!=null ) {
|
||||
recognizer.notifyListeners(e.line, e.charPositionInLine, e.getMessage());
|
||||
recognizer.notifyListeners(e.line, e.charPositionInLine, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -73,6 +73,10 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
|
|||
consumeUntil(recognizer, followSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sync(BaseRecognizer recognizer) {
|
||||
}
|
||||
|
||||
public void reportNoViableAlternative(BaseRecognizer recognizer,
|
||||
NoViableAltException e)
|
||||
throws RecognitionException
|
||||
|
@ -81,7 +85,7 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
|
|||
trackError(recognizer);
|
||||
|
||||
String msg = "no viable alternative at input "+getTokenErrorDisplay(e.token);
|
||||
recognizer.notifyListeners(e.line, e.charPositionInLine, msg);
|
||||
recognizer.notifyListeners(e.line, e.charPositionInLine, msg, e);
|
||||
}
|
||||
|
||||
public void reportInputMismatch(BaseRecognizer recognizer,
|
||||
|
@ -93,7 +97,7 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
|
|||
|
||||
String msg = "mismatched input "+getTokenErrorDisplay(e.token)+
|
||||
" expecting "+e.expecting.toString(recognizer.getTokenNames());
|
||||
recognizer.notifyListeners(e.line, e.charPositionInLine, msg);
|
||||
recognizer.notifyListeners(e.line, e.charPositionInLine, msg, e);
|
||||
}
|
||||
|
||||
public void reportFailedPredicate(BaseRecognizer recognizer,
|
||||
|
@ -106,7 +110,7 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
|
|||
String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()];
|
||||
String msg = "rule "+ruleName+" failed predicate: {"+
|
||||
e.predicateText+"}?";
|
||||
recognizer.notifyListeners(e.line, e.charPositionInLine, msg);
|
||||
recognizer.notifyListeners(e.line, e.charPositionInLine, msg, e);
|
||||
}
|
||||
|
||||
public void reportUnwantedToken(BaseRecognizer recognizer) {
|
||||
|
@ -119,7 +123,7 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
|
|||
IntervalSet expecting = getExpectedTokens(recognizer);
|
||||
String msg = "extraneous input "+tokenName+" expecting "+
|
||||
expecting.toString(recognizer.getTokenNames());
|
||||
recognizer.notifyListeners(t.getLine(), t.getCharPositionInLine(), msg);
|
||||
recognizer.notifyListeners(t.getLine(), t.getCharPositionInLine(), msg, null);
|
||||
}
|
||||
|
||||
public void reportMissingToken(BaseRecognizer recognizer) {
|
||||
|
@ -131,7 +135,7 @@ public class DefaultANTLRErrorStrategy implements ANTLRErrorStrategy {
|
|||
String msg = "missing "+expecting.toString(recognizer.getTokenNames())+
|
||||
" at "+getTokenErrorDisplay(t);
|
||||
|
||||
recognizer.notifyListeners(t.getLine(), t.getCharPositionInLine(), msg);
|
||||
recognizer.notifyListeners(t.getLine(), t.getCharPositionInLine(), msg, null);
|
||||
}
|
||||
|
||||
/** Attempt to recover from a single missing or extra token.
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
|
||||
package org.antlr.v4.runtime.atn;
|
||||
|
||||
import com.sun.istack.internal.Nullable;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
||||
|
||||
|
@ -59,13 +60,13 @@ public class LL1Analyzer {
|
|||
return look;
|
||||
}
|
||||
|
||||
public IntervalSet LOOK(ATNState s, RuleContext ctx) {
|
||||
public IntervalSet LOOK(ATNState s, @Nullable RuleContext ctx) {
|
||||
IntervalSet r = new IntervalSet();
|
||||
_LOOK(s, ctx, r, new HashSet<ATNConfig>());
|
||||
return r;
|
||||
}
|
||||
|
||||
protected void _LOOK(ATNState s, RuleContext ctx, IntervalSet look,
|
||||
protected void _LOOK(ATNState s, @Nullable RuleContext ctx, IntervalSet look,
|
||||
Set<ATNConfig> lookBusy) {
|
||||
// System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx);
|
||||
ATNConfig c = new ATNConfig(s, 0, ctx);
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.antlr.v4.test;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestSyntaxErrors extends BaseTest {
|
||||
public class TestToolSyntaxErrors extends BaseTest {
|
||||
static String[] A = {
|
||||
// INPUT
|
||||
"grammar A;\n" +
|
Loading…
Reference in New Issue