Class: Hoozuki::Node::Choice

Inherits:
Object
  • Object
show all
Defined in:
lib/hoozuki/node/choice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children) ⇒ Choice

Returns a new instance of Choice.



8
9
10
# File 'lib/hoozuki/node/choice.rb', line 8

def initialize(children)
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/hoozuki/node/choice.rb', line 6

def children
  @children
end

Instance Method Details

#to_nfa(state) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hoozuki/node/choice.rb', line 12

def to_nfa(state)
  child_nfas = @children.map { |child| child.to_nfa(state) }
  start_state = state.new_state
  accepts = child_nfas.flat_map(&:accept).to_set
  nfa = Automaton::NFA.new(start_state, accepts)
  child_nfas.each do |child_nfa|
    nfa.merge_transitions(child_nfa)
    nfa.add_epsilon_transition(start_state, child_nfa.start)
  end
  nfa
end