Skip to content

Commit

Permalink
Merge remote-tracking branch 'antlr/master' into optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Apr 7, 2014
2 parents f1e98fb + 9c6023b commit 2ef1828
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,9 @@ protected Tuple2<DFAState, ParserRuleContext> computeTargetState(@NotNull DFA df
* The conditions assume that intermediate
* contains all configurations relevant to the reach set, but this
* condition is not true when one or more configurations have been
* withheld in skippedStopStates.
* withheld in skippedStopStates, or when the current symbol is EOF.
*/
if (optimize_unique_closure && skippedStopStates == null && reachIntermediate.getUniqueAlt() != ATN.INVALID_ALT_NUMBER) {
if (optimize_unique_closure && skippedStopStates == null && t != Token.EOF && reachIntermediate.getUniqueAlt() != ATN.INVALID_ALT_NUMBER) {
reachIntermediate.setOutermostConfigSet(reach.isOutermostConfigSet());
reach = reachIntermediate;
break;
Expand Down Expand Up @@ -1712,6 +1712,11 @@ else if (!hasMoreContexts) {
}
}

if (!t.isEpsilon() && !closureBusy.add(c)) {
// avoid infinite recursion for EOF* and EOF+
continue;
}

int newDepth = depth;
if ( config.getState() instanceof RuleStopState ) {
// target fell off end of rule; mark resulting c as having dipped into outer context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public static BitSet getAlts(@NotNull Collection<BitSet> altsets) {
* </pre>
*/
@NotNull
public static Collection<BitSet> getConflictingAltSubsets(ATNConfigSet configs) {
public static Collection<BitSet> getConflictingAltSubsets(@NotNull ATNConfigSet configs) {
AltAndContextMap configToAlts = new AltAndContextMap();
for (ATNConfig c : configs) {
BitSet alts = configToAlts.get(c);
Expand Down
14 changes: 12 additions & 2 deletions runtime/Java/src/org/antlr/v4/runtime/atn/Transition.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,18 @@ protected Transition(@NotNull ATNState target) {

public abstract int getSerializationType();

/** Are we epsilon, action, sempred? */
public boolean isEpsilon() { return false; }
/**
* Determines if the transition is an "epsilon" transition.
*
* <p>The default implementation returns {@code false}.</p>
*
* @return {@code true} if traversing this transition in the ATN does not
* consume an input symbol; otherwise, {@code false} if traversing this
* transition consumes (matches) an input symbol.
*/
public boolean isEpsilon() {
return false;
}

@Nullable
public IntervalSet label() { return null; }
Expand Down
33 changes: 33 additions & 0 deletions tool/test/org/antlr/v4/test/TestParserExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,37 @@ public void testPredicatedIfIfElse() throws Exception {
assertEquals("", found);
assertNull(stderrDuringParse);
}

/**
* This test ensures that {@link ParserATNSimulator} produces a correct
* result when the grammar contains multiple explicit references to
* {@code EOF} inside of parser rules.
*/
@Test
public void testMultipleEOFHandling() throws Exception {
String grammar =
"grammar T;\n" +
"prog : ('x' | 'x' 'y') EOF EOF;\n";
String input = "x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "prog", input, false);
assertEquals("", found);
assertNull(stderrDuringParse);
}

/**
* This test ensures that {@link ParserATNSimulator} does not produce a
* {@link StackOverflowError} when it encounters an {@code EOF} transition
* inside a closure.
*/
@Test
public void testEOFInClosure() throws Exception {
String grammar =
"grammar T;\n" +
"prog : stat EOF;\n" +
"stat : 'x' ('y' | EOF)*?;\n";
String input = "x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "prog", input, false);
assertEquals("", found);
assertNull(stderrDuringParse);
}
}

0 comments on commit 2ef1828

Please sign in to comment.