From 9e5cda85edba2cd2acbbf352c428c6a84c8c8524 Mon Sep 17 00:00:00 2001 From: parrt Date: Thu, 11 Jun 2015 10:56:59 -0700 Subject: [PATCH] Fixes #896. EOF was not counted in source interval. --- runtime/Java/src/org/antlr/v4/runtime/Parser.java | 14 +++++++++++++- .../antlr/v4/test/tool/TestParserInterpreter.java | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/Parser.java b/runtime/Java/src/org/antlr/v4/runtime/Parser.java index 41ad686b7..5843ad430 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Parser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Parser.java @@ -177,6 +177,9 @@ public abstract class Parser extends Recognizer { */ 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 { 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 { } 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 { 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}, diff --git a/tool/test/org/antlr/v4/test/tool/TestParserInterpreter.java b/tool/test/org/antlr/v4/test/tool/TestParserInterpreter.java index f83fe7ece..420825fb5 100644 --- a/tool/test/org/antlr/v4/test/tool/TestParserInterpreter.java +++ b/tool/test/org/antlr/v4/test/tool/TestParserInterpreter.java @@ -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 ))"); + 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" +