Fixes #896. EOF was not counted in source interval.

This commit is contained in:
parrt 2015-06-11 10:56:59 -07:00
parent 2da28ee5b2
commit 9e5cda85ed
2 changed files with 28 additions and 1 deletions

View File

@ -177,6 +177,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
*/ */
protected int _syntaxErrors; protected int _syntaxErrors;
/** Indicates parser has match()ed EOF token */
protected boolean matchedEOF;
public Parser(TokenStream input) { public Parser(TokenStream input) {
setInputStream(input); setInputStream(input);
} }
@ -217,6 +220,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
public Token match(int ttype) throws RecognitionException { public Token match(int ttype) throws RecognitionException {
Token t = getCurrentToken(); Token t = getCurrentToken();
if ( t.getType()==ttype ) { if ( t.getType()==ttype ) {
if ( t.getType()==Token.EOF ) {
matchedEOF = true;
}
_errHandler.reportMatch(this); _errHandler.reportMatch(this);
consume(); consume();
} }
@ -630,7 +636,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
} }
public void exitRule() { 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 // trigger event on _ctx, before it reverts to parent
if ( _parseListeners != null) triggerExitRuleEvent(); if ( _parseListeners != null) triggerExitRuleEvent();
setState(_ctx.invokingState); setState(_ctx.invokingState);
@ -909,6 +917,10 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
return false; return false;
} }
public boolean isMatchedEOF() {
return matchedEOF;
}
/** /**
* Computes the set of input symbols which could follow the current parser * Computes the set of input symbols which could follow the current parser
* state and context, as given by {@link #getState} and {@link #getContext}, * state and context, as given by {@link #getState} and {@link #getContext},

View File

@ -82,6 +82,21 @@ public class TestParserInterpreter extends BaseTest {
assertEquals("0..1", t.getSourceInterval().toString()); 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 { @Test public void testAorB() throws Exception {
LexerGrammar lg = new LexerGrammar( LexerGrammar lg = new LexerGrammar(
"lexer grammar L;\n" + "lexer grammar L;\n" +