module Kleene::DSL
def with_err!(nfa, alphabet = nfa.alphabet)
adds and error state to the NFA, create error transitions from all non-error states to the error state on any unhandled token.
def with_err!(nfa, alphabet = nfa.alphabet) error_state = nfa.states.find(&:error?) return nfa if error_state error_state = State.new_error_state nfa.add_state(error_state) nfa.states.each do |state| tokens_on_outbound_transitions = nfa.transitions_from(state).map(&:token) missing_tokens = alphabet - tokens_on_outbound_transitions missing_tokens.each do |token| nfa.add_transition(token, state, error_state) end end nfa.remove_state(error_state) if nfa.all_transitions.none? {|transition| transition.from == error_state || transition.to == error_state } nfa.set_regex_pattern("/#{nfa.regex_pattern}/E") end