Class: Refract::BasicVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/refract/basic_visitor.rb

Direct Known Subclasses

Formatter, MutationVisitor, Visitor

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBasicVisitor

Returns a new instance of BasicVisitor.



5
6
7
# File 'lib/refract/basic_visitor.rb', line 5

def initialize
  @stack = []
end

Class Method Details

.visit(node_class) ⇒ Object



9
10
11
# File 'lib/refract/basic_visitor.rb', line 9

def self.visit(node_class, &)
  define_method("visit_#{node_class.type}", &)
end

Instance Method Details

#around_visit(node) {|node| ... } ⇒ Object

Yields:

  • (node)


13
14
15
# File 'lib/refract/basic_visitor.rb', line 13

def around_visit(node)
  yield(node)
end

#visit(node) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/refract/basic_visitor.rb', line 17

def visit(node)
  return unless node

  @stack.push(node)
  around_visit(node) do |n|
    n.accept(self).tap { @stack.pop }
  end
end

#visit_each(nodes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/refract/basic_visitor.rb', line 26

def visit_each(nodes)
  nodes = nodes.compact

  i, len = 0, nodes.length
  if block_given?
    while i < len
      node = nodes[i]
      i += 1
      visit node
      yield unless i == len
    end
  else
    while i < len
      visit nodes[i]
      i += 1
    end
  end
end