addDFAState method

DFAState addDFAState(
  1. ATNConfigSet configs
)

Add a new DFA state if there isn't one with this set of configurations already. This method also detects the first configuration containing an ATN rule stop state. Later, when traversing the DFA, we will know which rule to accept.

Implementation

DFAState addDFAState(ATNConfigSet configs) {
  /* the lexer evaluates predicates on-the-fly; by this point configs
		 * should not contain any configurations with unevaluated predicates.
		 */
  assert(!configs.hasSemanticContext);

  final proposed = DFAState(configs: configs);
  ATNConfig? firstConfigWithRuleStopState;
  for (var c in configs) {
    if (c.state is RuleStopState) {
      firstConfigWithRuleStopState = c;
      break;
    }
  }

  if (firstConfigWithRuleStopState != null) {
    proposed.isAcceptState = true;
    proposed.lexerActionExecutor =
        (firstConfigWithRuleStopState as LexerATNConfig).lexerActionExecutor;
    proposed.prediction =
        atn.ruleToTokenType[firstConfigWithRuleStopState.state.ruleIndex];
  }

  final dfa = decisionToDFA[mode];
  final existing = dfa.states[proposed];
  if (existing != null) return existing;

  final newState = proposed;

  newState.stateNumber = dfa.states.length;
  configs.readOnly = true;
  newState.configs = configs;
  dfa.states[newState] = newState;
  return newState;
}