forked from jasder/antlr
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:
parent
5fbc994342
commit
c590ba8fd8
|
@ -373,8 +373,9 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
|
||||||
if ( expectedTokenType== Token.EOF ) tokenText = "<missing EOF>";
|
if ( expectedTokenType== Token.EOF ) tokenText = "<missing EOF>";
|
||||||
else tokenText = "<missing "+recognizer.getTokenNames()[expectedTokenType]+">";
|
else tokenText = "<missing "+recognizer.getTokenNames()[expectedTokenType]+">";
|
||||||
Token current = currentSymbol;
|
Token current = currentSymbol;
|
||||||
if ( current.getType() == Token.EOF ) {
|
Token lookback = recognizer.getInputStream().LT(-1);
|
||||||
current = recognizer.getInputStream().LT(-1);
|
if ( current.getType() == Token.EOF && lookback!=null ) {
|
||||||
|
current = lookback;
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
_factory.create(current.getTokenSource(), expectedTokenType, tokenText,
|
_factory.create(current.getTokenSource(), expectedTokenType, tokenText,
|
||||||
|
|
|
@ -525,7 +525,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
|
||||||
List<String> stack = new ArrayList<String>();
|
List<String> stack = new ArrayList<String>();
|
||||||
while ( p!=null ) {
|
while ( p!=null ) {
|
||||||
// compute what follows who invoked us
|
// 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;
|
p = p.parent;
|
||||||
}
|
}
|
||||||
return stack;
|
return stack;
|
||||||
|
|
|
@ -1,24 +1,8 @@
|
||||||
grammar T;
|
grammar T;
|
||||||
@members {
|
s : r=e ;
|
||||||
public static class LeafListener extends TBaseListener {
|
e : e '(' INT ')'
|
||||||
public void exitCall(TParser.CallContext ctx) {
|
| INT
|
||||||
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
|
|
||||||
;
|
;
|
||||||
eList : e (',' e)* ;
|
|
||||||
MULT: '*' ;
|
MULT: '*' ;
|
||||||
ADD : '+' ;
|
ADD : '+' ;
|
||||||
INT : [0-9]+ ;
|
INT : [0-9]+ ;
|
||||||
|
|
Loading…
Reference in New Issue