addDFAState method

DFAState addDFAState(
  1. DFA dfa,
  2. DFAState D
)

Add state D to the DFA if it is not already present, and return the actual instance stored in the DFA. If a state equivalent to D is already in the DFA, the existing state is returned. Otherwise this method returns D after adding it to the DFA.

If [D] is {@link #ERROR}, this method returns {@link #ERROR} and does not change the DFA.

@param dfa The dfa @param D The DFA state to add @return The state stored in the DFA. This will be either the existing state if D is already in the DFA, or D itself if the state was not already present.

Implementation

DFAState addDFAState(DFA dfa, DFAState D) {
  if (D == ATNSimulator.ERROR) {
    return D;
  }

  final existing = dfa.states[D];
  if (existing != null) return existing;

  D.stateNumber = dfa.states.length;
  if (!D.configs.readOnly) {
    D.configs.optimizeConfigs(this);
    D.configs.readOnly = true;
  }
  dfa.states[D] = D;
  if (debug) log('adding new DFA state: $D');
  return D;
}