Method: Kleene::DSL#with_err!
- Defined in:
- lib/kleene/dsl.rb
#with_err!(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 transitions to itself on any token.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kleene/dsl.rb', line 62 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 |