don't look backwards for err msg if EOF is entire input. make sure we don't use -1 rule index for ruleNames[]

This commit is contained in:
Terence Parr 2012-04-29 12:12:42 -07:00
parent 5fbc994342
commit c590ba8fd8
3 changed files with 9 additions and 22 deletions

View File

@ -373,8 +373,9 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
if ( expectedTokenType== Token.EOF ) tokenText = "<missing EOF>";
else tokenText = "<missing "+recognizer.getTokenNames()[expectedTokenType]+">";
Token current = currentSymbol;
if ( current.getType() == Token.EOF ) {
current = recognizer.getInputStream().LT(-1);
Token lookback = recognizer.getInputStream().LT(-1);
if ( current.getType() == Token.EOF && lookback!=null ) {
current = lookback;
}
return
_factory.create(current.getTokenSource(), expectedTokenType, tokenText,

View File

@ -525,7 +525,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
List<String> stack = new ArrayList<String>();
while ( p!=null ) {
// compute what follows who invoked us
stack.add(ruleNames[p.getRuleIndex()]);
int ruleIndex = p.getRuleIndex();
if ( ruleIndex<0 ) stack.add("n/a");
else stack.add(ruleNames[ruleIndex]);
p = p.parent;
}
return stack;

View File

@ -1,24 +1,8 @@
grammar T;
@members {
public static class LeafListener extends TBaseListener {
public void exitCall(TParser.CallContext ctx) {
System.out.printf("%s %s",ctx.e().start.getText(),
ctx.eList());
}
public void exitInt(TParser.IntContext ctx) {
System.out.println(ctx.INT().getSymbol().getText());
}
}
}
s
@init {setBuildParseTree(true);}
@after { System.out.println($r.ctx.toStringTree(this)); ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(new LeafListener(), $r.ctx);}
: r=e ;
e : e '(' eList ')' # Call
| INT # Int
s : r=e ;
e : e '(' INT ')'
| INT
;
eList : e (',' e)* ;
MULT: '*' ;
ADD : '+' ;
INT : [0-9]+ ;