Class: RegularExpression::AST::Expression

Inherits:
Object
  • Object
show all
Defined in:
lib/regular_expression/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Expression

Returns a new instance of Expression.



51
52
53
# File 'lib/regular_expression/ast.rb', line 51

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Group | Match | Anchor



49
50
51
# File 'lib/regular_expression/ast.rb', line 49

def items
  @items
end

Instance Method Details

#to_dot(parent) ⇒ Object



55
56
57
58
59
# File 'lib/regular_expression/ast.rb', line 55

def to_dot(parent)
  node = parent.add_node(object_id, label: "Expression")

  items.each { |item| item.to_dot(node) }
end

#to_nfa(start, finish) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/regular_expression/ast.rb', line 61

def to_nfa(start, finish)
  inner = Array.new(items.length - 1) { NFA::State.new }
  states = [start, *inner, finish]

  items.each_with_index do |item, index|
    item.to_nfa(states[index], states[index + 1])
  end
end