added state to be consistent with enterRule(); most importantly, we need to set the state when we enter a recursive rule, which we are not doing at the moment. This was not a problem before because we never could directly call a recursive rule and the outer rule set the current state. Now that we are using recursive rules as start rules, we need to set the state.

This commit is contained in:
Terence Parr 2013-11-20 17:53:51 -08:00
parent 2791dd5619
commit 2394b38995
3 changed files with 8 additions and 13 deletions

View File

@ -577,10 +577,11 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
*/
@Deprecated
public void enterRecursionRule(ParserRuleContext localctx, int ruleIndex) {
enterRecursionRule(localctx, ruleIndex, 0);
enterRecursionRule(localctx, getATN().ruleToStartState[ruleIndex].stateNumber, ruleIndex, 0);
}
public void enterRecursionRule(ParserRuleContext localctx, int ruleIndex, int precedence) {
public void enterRecursionRule(ParserRuleContext localctx, int state, int ruleIndex, int precedence) {
setState(state);
_precedenceStack.push(precedence);
_ctx = localctx;
_ctx.start = _input.LT(1);

View File

@ -30,12 +30,6 @@
package org.antlr.v4.runtime;
import org.antlr.v4.runtime.FailedPredicateException;
import org.antlr.v4.runtime.InterpreterRuleContext;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.atn.ActionTransition;
@ -153,7 +147,7 @@ public class ParserInterpreter extends Parser {
InterpreterRuleContext rootContext = new InterpreterRuleContext(null, ATNState.INVALID_STATE_NUMBER, startRuleIndex);
if (startRuleStartState.isPrecedenceRule) {
enterRecursionRule(rootContext, startRuleIndex, 0);
enterRecursionRule(rootContext, startRuleStartState.stateNumber, startRuleIndex, 0);
}
else {
enterRule(rootContext, startRuleStartState.stateNumber, startRuleIndex);
@ -180,9 +174,9 @@ public class ParserInterpreter extends Parser {
}
@Override
public void enterRecursionRule(ParserRuleContext localctx, int ruleIndex, int precedence) {
public void enterRecursionRule(ParserRuleContext localctx, int state, int ruleIndex, int precedence) {
_parentContextStack.push(new Pair<ParserRuleContext, Integer>(_ctx, localctx.invokingState));
super.enterRecursionRule(localctx, ruleIndex, precedence);
super.enterRecursionRule(localctx, state, ruleIndex, precedence);
}
protected ATNState getATNState() {
@ -229,7 +223,7 @@ public class ParserInterpreter extends Parser {
int ruleIndex = ruleStartState.ruleIndex;
InterpreterRuleContext ctx = new InterpreterRuleContext(_ctx, p.stateNumber, ruleIndex);
if (ruleStartState.isPrecedenceRule) {
enterRecursionRule(ctx, ruleIndex, ((RuleTransition)transition).precedence);
enterRecursionRule(ctx, ruleStartState.stateNumber, ruleIndex, ((RuleTransition)transition).precedence);
}
else {
enterRule(ctx, transition.target.stateNumber, ruleIndex);

View File

@ -372,7 +372,7 @@ private <currentRule.ctxType> <currentRule.name>(int _p<currentRule.args:{a | ,
<currentRule.ctxType> _localctx = new <currentRule.ctxType>(_ctx, _parentState<currentRule.args:{a | , <a.name>}>);
<currentRule.ctxType> _prevctx = _localctx;
int _startState = <currentRule.startState>;
enterRecursionRule(_localctx, RULE_<currentRule.name>, _p);
enterRecursionRule(_localctx, <currentRule.startState>, RULE_<currentRule.name>, _p);
<namedActions.init>
<locals; separator="\n">
try {