Merge branch 'master' into main

This commit is contained in:
Terence Parr 2012-08-04 21:31:37 -07:00
commit 492980de71
5 changed files with 15 additions and 6 deletions

View File

@ -32,6 +32,7 @@ package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.dfa.DFAState;
import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Pair;
import java.util.ArrayList;
import java.util.IdentityHashMap;
@ -100,6 +101,7 @@ public abstract class ATNSimulator {
//
// STATES
//
List<Pair<LoopEndState, Integer>> loopBackStateNumbers = new ArrayList<Pair<LoopEndState, Integer>>();
int nstates = toInt(data[p++]);
for (int i=1; i<=nstates; i++) {
int stype = toInt(data[p++]);
@ -111,11 +113,17 @@ public abstract class ATNSimulator {
ATNState s = stateFactory(stype, i);
s.ruleIndex = toInt(data[p++]);
if ( stype == ATNState.LOOP_END ) { // special case
((LoopEndState)s).loopBackStateNumber = toInt(data[p++]);
int loopBackStateNumber = toInt(data[p++]);
loopBackStateNumbers.add(new Pair<LoopEndState, Integer>((LoopEndState)s, loopBackStateNumber));
}
atn.addState(s);
}
// delay the assignment of loop back states until we know all the state instances have been initialized
for (Pair<LoopEndState, Integer> pair : loopBackStateNumbers) {
pair.a.loopBackState = atn.states.get(pair.b);
}
//
// RULES
//

View File

@ -31,5 +31,5 @@ package org.antlr.v4.runtime.atn;
/** Mark the end of a * or + loop */
public class LoopEndState extends ATNState {
public int loopBackStateNumber;
public ATNState loopBackState;
}

View File

@ -1164,7 +1164,8 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
if ( debug ) System.out.print("Loop end; pop, stack=" + config.context);
LoopEndState end = (LoopEndState)config.state;
// pop all the way back until we don't see the loopback state anymore
config.context = config.context.popAll(end.loopBackStateNumber,
int loopBackStateNumber = end.loopBackState.stateNumber;
config.context = config.context.popAll(loopBackStateNumber,
configs.fullCtx,
mergeCache);
if ( debug ) System.out.println(" becomes "+config.context);

View File

@ -100,7 +100,7 @@ public class ATNSerializer {
}
data.add(s.getStateType());
data.add(s.ruleIndex);
if ( s.getStateType() == ATNState.LOOP_END ) data.add(((LoopEndState)s).loopBackStateNumber);
if ( s.getStateType() == ATNState.LOOP_END ) data.add(((LoopEndState)s).loopBackState.stateNumber);
nedges += s.getNumberOfTransitions();
for (int i=0; i<s.getNumberOfTransitions(); i++) {
Transition t = s.transition(i);

View File

@ -448,7 +448,7 @@ public class ParserATNFactory implements ATNFactory {
atn.defineDecisionState(loop);
LoopEndState end = newState(LoopEndState.class, plusAST);
blkStart.loopBackState = loop;
end.loopBackStateNumber = loop.stateNumber;
end.loopBackState = loop;
plusAST.atnState = blkStart;
epsilon(blkEnd, loop); // blk can see loop back
@ -491,7 +491,7 @@ public class ParserATNFactory implements ATNFactory {
LoopEndState end = newState(LoopEndState.class, starAST);
StarLoopbackState loop = newState(StarLoopbackState.class, starAST);
entry.loopBackState = loop;
end.loopBackStateNumber = loop.stateNumber;
end.loopBackState = loop;
BlockAST blkAST = (BlockAST)starAST.getChild(0);
entry.isGreedy = isGreedy(blkAST);