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.dfa.DFAState;
import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Pair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
@ -100,6 +101,7 @@ public abstract class ATNSimulator {
// //
// STATES // STATES
// //
List<Pair<LoopEndState, Integer>> loopBackStateNumbers = new ArrayList<Pair<LoopEndState, Integer>>();
int nstates = toInt(data[p++]); int nstates = toInt(data[p++]);
for (int i=1; i<=nstates; i++) { for (int i=1; i<=nstates; i++) {
int stype = toInt(data[p++]); int stype = toInt(data[p++]);
@ -111,11 +113,17 @@ public abstract class ATNSimulator {
ATNState s = stateFactory(stype, i); ATNState s = stateFactory(stype, i);
s.ruleIndex = toInt(data[p++]); s.ruleIndex = toInt(data[p++]);
if ( stype == ATNState.LOOP_END ) { // special case 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); 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 // RULES
// //

View File

@ -31,5 +31,5 @@ package org.antlr.v4.runtime.atn;
/** Mark the end of a * or + loop */ /** Mark the end of a * or + loop */
public class LoopEndState extends ATNState { 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); if ( debug ) System.out.print("Loop end; pop, stack=" + config.context);
LoopEndState end = (LoopEndState)config.state; LoopEndState end = (LoopEndState)config.state;
// pop all the way back until we don't see the loopback state anymore // 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, configs.fullCtx,
mergeCache); mergeCache);
if ( debug ) System.out.println(" becomes "+config.context); if ( debug ) System.out.println(" becomes "+config.context);

View File

@ -100,7 +100,7 @@ public class ATNSerializer {
} }
data.add(s.getStateType()); data.add(s.getStateType());
data.add(s.ruleIndex); 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(); nedges += s.getNumberOfTransitions();
for (int i=0; i<s.getNumberOfTransitions(); i++) { for (int i=0; i<s.getNumberOfTransitions(); i++) {
Transition t = s.transition(i); Transition t = s.transition(i);

View File

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