Skip to content

Commit

Permalink
Revert "Simplify Parser.getExpectedTokens and DefaultErrorStrategy.ge…
Browse files Browse the repository at this point in the history
…tErrorRecoverySet"

This reverts commit d33172d.
  • Loading branch information
parrt committed Nov 5, 2012
1 parent e5c2dfa commit 61ed3bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
16 changes: 14 additions & 2 deletions runtime/Java/src/org/antlr/v4/runtime/DefaultErrorStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
import org.antlr.v4.runtime.atn.BlockStartState;
import org.antlr.v4.runtime.atn.PlusBlockStartState;
import org.antlr.v4.runtime.atn.PlusLoopbackState;
import org.antlr.v4.runtime.atn.RuleTransition;
import org.antlr.v4.runtime.atn.StarLoopEntryState;
import org.antlr.v4.runtime.atn.StarLoopbackState;
import org.antlr.v4.runtime.misc.IntervalSet;
Expand Down Expand Up @@ -521,8 +522,19 @@ protected String escapeWSAndQuote(String s) {
*/
protected IntervalSet getErrorRecoverySet(Parser recognizer) {
ATN atn = recognizer.getInterpreter().atn;
ParserRuleContext ctx = recognizer._ctx;
return atn.nextTokens(atn.states.get(ctx.s), ctx);
RuleContext ctx = recognizer._ctx;
IntervalSet recoverSet = new IntervalSet();
while ( ctx!=null && ctx.invokingState>=0 ) {
// compute what follows who invoked us
ATNState invokingState = atn.states.get(ctx.invokingState);
RuleTransition rt = (RuleTransition)invokingState.transition(0);
IntervalSet follow = atn.nextTokens(rt.followState);
recoverSet.addAll(follow);
ctx = ctx.parent;
}
recoverSet.remove(Token.EPSILON);
// System.out.println("recover set "+recoverSet.toString(recognizer.getTokenNames()));
return recoverSet;
}

/** Consume tokens until one matches the given token set */
Expand Down
19 changes: 18 additions & 1 deletion runtime/Java/src/org/antlr/v4/runtime/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,24 @@ public IntervalSet getExpectedTokens() {
ATN atn = getInterpreter().atn;
ParserRuleContext ctx = _ctx;
ATNState s = atn.states.get(ctx.s);
return atn.nextTokens(s, ctx);
IntervalSet following = atn.nextTokens(s);
// System.out.println("following "+s+"="+following);
if ( !following.contains(Token.EPSILON) ) return following;
IntervalSet expected = new IntervalSet();
expected.addAll(following);
expected.remove(Token.EPSILON);
while ( ctx!=null && ctx.invokingState>=0 && following.contains(Token.EPSILON) ) {
ATNState invokingState = atn.states.get(ctx.invokingState);
RuleTransition rt = (RuleTransition)invokingState.transition(0);
following = atn.nextTokens(rt.followState);
expected.addAll(following);
expected.remove(Token.EPSILON);
ctx = (ParserRuleContext)ctx.parent;
}
if ( following.contains(Token.EPSILON) ) {
expected.add(Token.EOF);
}
return expected;
}

public IntervalSet getExpectedTokensWithinCurrentRule() {
Expand Down

0 comments on commit 61ed3bc

Please sign in to comment.