Method: Kleene::DSL#kleene

Defined in:
lib/kleene/dsl.rb

#kleene(machine) ⇒ Object

Implements Kleene Star, as defined in the Ragel manual in section 2.5.6 of www.colm.net/files/ragel/ragel-guide-6.10.pdf: The machine resulting from the Kleene Star operator will match zero or more repetitions of the machine it is applied to. It creates a new start state and an additional final state. Epsilon transitions are drawn between the new start state and the old start state, between the new start state and the new final state, and between the final states of the machine and the new start state.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/kleene/dsl.rb', line 201

def kleene(machine)
  machine = machine.deep_clone
  start = State.new
  final = State.new(true)

  nfa = NFA.new(start, machine.alphabet)
  nfa.add_states(machine.states)
  nfa.add_transition(NFATransition::Epsilon, start, final)
  nfa.add_transition(NFATransition::Epsilon, start, machine.start_state)
  machine.final_states.each do |final_state|
    nfa.add_transition(NFATransition::Epsilon, final_state, start)
    final_state.final = false
  end

  # add all of machine's transitions to the new machine
  (machine.all_transitions).each {|t| nfa.add_transition(t.token, t.from, t.to) }
  nfa.update_final_states

  nfa.set_regex_pattern("#{machine.regex_pattern}*")
end