Add boolean RuleStartState.isPrecedenceRule

This commit is contained in:
Sam Harwell 2012-12-05 15:31:31 -06:00
parent c44ae39ae5
commit c556b821f6
4 changed files with 26 additions and 1 deletions

View File

@ -43,7 +43,7 @@ import java.util.List;
public abstract class ATNSimulator {
public static final int SERIALIZED_VERSION;
static {
SERIALIZED_VERSION = 1;
SERIALIZED_VERSION = 2;
}
/** Must distinguish between missing edge and edge we know leads nowhere */
@ -153,6 +153,12 @@ public abstract class ATNSimulator {
((DecisionState)atn.states.get(stateNumber)).nonGreedy = true;
}
int numPrecedenceStates = toInt(data[p++]);
for (int i = 0; i < numPrecedenceStates; i++) {
int stateNumber = toInt(data[p++]);
((RuleStartState)atn.states.get(stateNumber)).isPrecedenceRule = true;
}
//
// RULES
//

View File

@ -32,4 +32,5 @@ package org.antlr.v4.runtime.atn;
public class RuleStartState extends ATNState {
public RuleStopState stopState;
public boolean isPrecedenceRule;
}

View File

@ -43,6 +43,7 @@ import org.antlr.v4.runtime.atn.LoopEndState;
import org.antlr.v4.runtime.atn.PrecedencePredicateTransition;
import org.antlr.v4.runtime.atn.PredicateTransition;
import org.antlr.v4.runtime.atn.RangeTransition;
import org.antlr.v4.runtime.atn.RuleStartState;
import org.antlr.v4.runtime.atn.RuleTransition;
import org.antlr.v4.runtime.atn.SetTransition;
import org.antlr.v4.runtime.atn.Transition;
@ -99,6 +100,7 @@ public class ATNSerializer {
// dump states, count edges and collect sets while doing so
IntegerList nonGreedyStates = new IntegerList();
IntegerList precedenceStates = new IntegerList();
data.add(atn.states.size());
for (ATNState s : atn.states) {
if ( s==null ) { // might be optimized away
@ -111,6 +113,10 @@ public class ATNSerializer {
nonGreedyStates.add(s.stateNumber);
}
if (s instanceof RuleStartState && ((RuleStartState)s).isPrecedenceRule) {
precedenceStates.add(s.stateNumber);
}
data.add(stateType);
data.add(s.ruleIndex);
if ( s.getStateType() == ATNState.LOOP_END ) {
@ -141,6 +147,12 @@ public class ATNSerializer {
data.add(nonGreedyStates.get(i));
}
// precedence states
data.add(precedenceStates.size());
for (int i = 0; i < precedenceStates.size(); i++) {
data.add(precedenceStates.get(i));
}
int nrules = atn.ruleToStartState.length;
data.add(nrules);
for (int r=0; r<nrules; r++) {
@ -285,6 +297,10 @@ public class ATNSerializer {
for (int i = 0; i < numNonGreedyStates; i++) {
int stateNumber = ATNSimulator.toInt(data[p++]);
}
int numPrecedenceStates = ATNSimulator.toInt(data[p++]);
for (int i = 0; i < numPrecedenceStates; i++) {
int stateNumber = ATNSimulator.toInt(data[p++]);
}
int nrules = ATNSimulator.toInt(data[p++]);
for (int i=0; i<nrules; i++) {
int s = ATNSimulator.toInt(data[p++]);

View File

@ -70,6 +70,7 @@ import org.antlr.v4.semantics.UseDefAnalyzer;
import org.antlr.v4.tool.ErrorManager;
import org.antlr.v4.tool.ErrorType;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LeftRecursiveRule;
import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.ActionAST;
import org.antlr.v4.tool.ast.AltAST;
@ -572,6 +573,7 @@ public class ParserATNFactory implements ATNFactory {
RuleStartState start = newState(RuleStartState.class, r.ast);
RuleStopState stop = newState(RuleStopState.class, r.ast);
start.stopState = stop;
start.isPrecedenceRule = r instanceof LeftRecursiveRule;
start.setRuleIndex(r.index);
stop.setRuleIndex(r.index);
atn.ruleToStartState[r.index] = start;