forked from jasder/antlr
Fixes #896. EOF was not counted in source interval.
This commit is contained in:
parent
2da28ee5b2
commit
9e5cda85ed
|
@ -177,6 +177,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
|
|||
*/
|
||||
protected int _syntaxErrors;
|
||||
|
||||
/** Indicates parser has match()ed EOF token */
|
||||
protected boolean matchedEOF;
|
||||
|
||||
public Parser(TokenStream input) {
|
||||
setInputStream(input);
|
||||
}
|
||||
|
@ -217,6 +220,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
|
|||
public Token match(int ttype) throws RecognitionException {
|
||||
Token t = getCurrentToken();
|
||||
if ( t.getType()==ttype ) {
|
||||
if ( t.getType()==Token.EOF ) {
|
||||
matchedEOF = true;
|
||||
}
|
||||
_errHandler.reportMatch(this);
|
||||
consume();
|
||||
}
|
||||
|
@ -630,7 +636,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
|
|||
}
|
||||
|
||||
public void exitRule() {
|
||||
_ctx.stop = _input.LT(-1);
|
||||
if ( matchedEOF ) _ctx.stop = _input.LT(1);
|
||||
else _ctx.stop = _input.LT(-1);
|
||||
// _ctx.stop = _input.LT(-1);
|
||||
// trigger event on _ctx, before it reverts to parent
|
||||
if ( _parseListeners != null) triggerExitRuleEvent();
|
||||
setState(_ctx.invokingState);
|
||||
|
@ -909,6 +917,10 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean isMatchedEOF() {
|
||||
return matchedEOF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the set of input symbols which could follow the current parser
|
||||
* state and context, as given by {@link #getState} and {@link #getContext},
|
||||
|
|
|
@ -82,6 +82,21 @@ public class TestParserInterpreter extends BaseTest {
|
|||
assertEquals("0..1", t.getSourceInterval().toString());
|
||||
}
|
||||
|
||||
@Test public void testEOFInChild() throws Exception {
|
||||
LexerGrammar lg = new LexerGrammar(
|
||||
"lexer grammar L;\n" +
|
||||
"A : 'a' ;\n");
|
||||
Grammar g = new Grammar(
|
||||
"parser grammar T;\n" +
|
||||
"s : x ;\n" +
|
||||
"x : A EOF ;",
|
||||
lg);
|
||||
|
||||
ParseTree t = testInterp(lg, g, "s", "a", "(s (x a <EOF>))");
|
||||
assertEquals("0..1", t.getSourceInterval().toString());
|
||||
assertEquals("0..1", t.getChild(0).getSourceInterval().toString());
|
||||
}
|
||||
|
||||
@Test public void testAorB() throws Exception {
|
||||
LexerGrammar lg = new LexerGrammar(
|
||||
"lexer grammar L;\n" +
|
||||
|
|
Loading…
Reference in New Issue