Class: RegularExpression::AST::Root

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expressions, at_start: false) ⇒ Root

Returns a new instance of Root.



17
18
19
20
# File 'lib/regular_expression/ast.rb', line 17

def initialize(expressions, at_start: false)
  @expressions = expressions
  @at_start = at_start
end

Instance Attribute Details

#at_startObject (readonly)

bool



15
16
17
# File 'lib/regular_expression/ast.rb', line 15

def at_start
  @at_start
end

#expressionsObject (readonly)



14
15
16
# File 'lib/regular_expression/ast.rb', line 14

def expressions
  @expressions
end

Instance Method Details

#to_dot(graph) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/regular_expression/ast.rb', line 22

def to_dot(graph)
  label = "Root"
  label = "#{label} (at start)" if at_start

  node = graph.add_node(object_id, label: label)
  expressions.each { |expression| expression.to_dot(node) }
end

#to_nfaObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/regular_expression/ast.rb', line 30

def to_nfa
  start = NFA::StartState.new
  current = start

  if at_start
    current = NFA::State.new
    start.add_transition(NFA::Transition::BeginAnchor.new(current))
  end

  finish = NFA::FinishState.new
  expressions.each do |expression|
    expression.to_nfa(current, finish)
  end

  start
end