removeAllConfigsNotInRuleStopState method

ATNConfigSet removeAllConfigsNotInRuleStopState(
  1. ATNConfigSet configs,
  2. bool lookToEndOfRule
)

Return a configuration set containing only the configurations from configs which are in a RuleStopState. If all configurations in configs are already in a rule stop state, this method simply returns configs.

When [lookToEndOfRule] is true, this method uses {@link ATN#nextTokens} for each configuration in [configs] which is not already in a rule stop state to see if a rule stop state is reachable from the configuration via epsilon-only transitions.

@param configs the configuration set to update @param lookToEndOfRule when true, this method checks for rule stop states reachable by epsilon-only transitions from each configuration in configs.

@return configs if all configurations in configs are in a rule stop state, otherwise return a new configuration set containing only the configurations from configs which are in a rule stop state

Implementation

ATNConfigSet removeAllConfigsNotInRuleStopState(
    ATNConfigSet configs, bool lookToEndOfRule) {
  if (PredictionModeExtension.allConfigsInRuleStopStates(configs)) {
    return configs;
  }

  final result = ATNConfigSet(configs.fullCtx);
  for (var config in configs) {
    if (config.state is RuleStopState) {
      result.add(config, mergeCache);
      continue;
    }

    if (lookToEndOfRule && config.state.onlyHasEpsilonTransitions()) {
      final nextTokens = atn.nextTokens(config.state);
      if (nextTokens.contains(Token.EPSILON)) {
        ATNState endOfRuleState = atn.ruleToStopState[config.state.ruleIndex];
        result.add(ATNConfig.dup(config, state: endOfRuleState), mergeCache);
      }
    }
  }

  return result;
}