Always update ATNConfigSet.hasSemanticContext and ATNConfigSet.dipsIntoOuterContext when adding configurations to the set; remove unnecessary manual updates

This commit is contained in:
Sam Harwell 2013-01-03 02:41:02 -06:00
parent 9b5417c54a
commit 6c23a96046
2 changed files with 11 additions and 10 deletions

View File

@ -33,6 +33,8 @@ package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.misc.AbstractEqualityComparator; import org.antlr.v4.runtime.misc.AbstractEqualityComparator;
import org.antlr.v4.runtime.misc.Array2DHashSet; import org.antlr.v4.runtime.misc.Array2DHashSet;
import org.antlr.v4.runtime.misc.DoubleKeyMap; import org.antlr.v4.runtime.misc.DoubleKeyMap;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
@ -317,22 +319,28 @@ public class ATNConfigSet implements Set<ATNConfig> {
} }
@Override @Override
public boolean add(ATNConfig config) { public boolean add(@NotNull ATNConfig config) {
return add(config, null); return add(config, null);
} }
/** Adding a new config means merging contexts with existing configs for /** Adding a new config means merging contexts with existing configs for
* (s, i, pi, _) * (s, i, pi, _)
* We use (s,i,pi) as key * We use (s,i,pi) as key
* <p/>
* This method updates {@link #dipsIntoOuterContext} and
* {@link #hasSemanticContext} when necessary.
*/ */
public boolean add( public boolean add(
ATNConfig config, @NotNull ATNConfig config,
DoubleKeyMap<PredictionContext,PredictionContext,PredictionContext> mergeCache) @Nullable DoubleKeyMap<PredictionContext,PredictionContext,PredictionContext> mergeCache)
{ {
if ( readonly ) throw new IllegalStateException("This set is readonly"); if ( readonly ) throw new IllegalStateException("This set is readonly");
if ( config.semanticContext!=SemanticContext.NONE ) { if ( config.semanticContext!=SemanticContext.NONE ) {
hasSemanticContext = true; hasSemanticContext = true;
} }
if (config.reachesIntoOuterContext > 0) {
dipsIntoOuterContext = true;
}
ATNConfig existing = configLookup.getOrAdd(config); ATNConfig existing = configLookup.getOrAdd(config);
if ( existing==config ) { // we added this new one if ( existing==config ) { // we added this new one
configs.add(config); // track order here configs.add(config); // track order here

View File

@ -968,7 +968,6 @@ public class ParserATNSimulator extends ATNSimulator {
if (skippedStopStates != null && !PredictionMode.hasConfigAtRuleStopState(reach)) { if (skippedStopStates != null && !PredictionMode.hasConfigAtRuleStopState(reach)) {
for (ATNConfig c : skippedStopStates) { for (ATNConfig c : skippedStopStates) {
assert c.reachesIntoOuterContext == 0 && c.semanticContext == SemanticContext.NONE;
reach.add(c, mergeCache); reach.add(c, mergeCache);
} }
} }
@ -1232,12 +1231,6 @@ public class ParserATNSimulator extends ATNSimulator {
// optimization // optimization
if ( !p.onlyHasEpsilonTransitions() ) { if ( !p.onlyHasEpsilonTransitions() ) {
configs.add(config, mergeCache); configs.add(config, mergeCache);
if ( config.semanticContext!= SemanticContext.NONE ) {
configs.hasSemanticContext = true;
}
if ( config.reachesIntoOuterContext>0 ) {
configs.dipsIntoOuterContext = true;
}
// if ( debug ) System.out.println("added config "+configs); // if ( debug ) System.out.println("added config "+configs);
} }