Method: Kleene::DSL#with_err_dead_end!
- Defined in:
- lib/kleene/dsl.rb
#with_err_dead_end!(nfa, alphabet = nfa.alphabet) ⇒ Object
adds and error state to the NFA, create error transitions from all non-error states to the error state on any unhandled token. the error state doesn’t have any outbound transitions.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/kleene/dsl.rb', line 89 def with_err_dead_end!(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| unless state.error? tokens_on_outbound_transitions = nfa.transitions_from(state).map(&:token).to_set only_outbound_transition_is_epsilon_transition = tokens_on_outbound_transitions.size == 1 && tokens_on_outbound_transitions.first == NFATransition::Epsilon unless only_outbound_transition_is_epsilon_transition missing_tokens = (alphabet - Set[NFATransition::Epsilon]) - tokens_on_outbound_transitions missing_tokens.each do |token| nfa.add_transition(token, state, error_state) end end end end # remove the error state if it has no inbound or outbound transitions 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}/DE") end |