Class: ActionDispatch::Journey::NFA::Visitor

Inherits:
Visitors::Visitor show all
Defined in:
actionpack/lib/action_dispatch/journey/nfa/builder.rb

Overview

:nodoc:

Constant Summary

Constants inherited from Visitors::Visitor

Visitors::Visitor::DISPATCH_CACHE

Instance Method Summary collapse

Methods inherited from Visitors::Visitor

#accept

Constructor Details

#initialize(tt) ⇒ Visitor

Returns a new instance of Visitor.



8
9
10
11
# File 'actionpack/lib/action_dispatch/journey/nfa/builder.rb', line 8

def initialize(tt)
  @tt = tt
  @i  = -1
end

Instance Method Details

#terminal(node) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'actionpack/lib/action_dispatch/journey/nfa/builder.rb', line 51

def terminal(node)
  from_i = @i += 1 # new state
  to_i   = @i += 1 # new state

  @tt[from_i, to_i] = node
  @tt.accepting = to_i
  @tt.add_memo(to_i, node.memo)

  [from_i, to_i]
end

#visit_CAT(node) ⇒ Object



13
14
15
16
17
18
19
20
# File 'actionpack/lib/action_dispatch/journey/nfa/builder.rb', line 13

def visit_CAT(node)
  left  = visit(node.left)
  right = visit(node.right)

  @tt.merge(left.last, right.first)

  [left.first, right.last]
end

#visit_GROUP(node) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'actionpack/lib/action_dispatch/journey/nfa/builder.rb', line 22

def visit_GROUP(node)
  from  = @i += 1
  left  = visit(node.left)
  to    = @i += 1

  @tt.accepting = to

  @tt[from, left.first] = nil
  @tt[left.last, to] = nil
  @tt[from, to] = nil

  [from, to]
end

#visit_OR(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'actionpack/lib/action_dispatch/journey/nfa/builder.rb', line 36

def visit_OR(node)
  from = @i += 1
  children = node.children.map { |c| visit(c) }
  to   = @i += 1

  children.each do |child|
    @tt[from, child.first] = nil
    @tt[child.last, to]    = nil
  end

  @tt.accepting = to

  [from, to]
end