class SyntaxTree::BasicVisitor

BasicVisitor is the parent class of the Visitor class that provides the ability to walk down the tree. It does not define any handlers, so you should extend this class if you want your visitor to raise an error if you attempt to visit a node that you don’t handle.

Public Class Methods

valid_visit_methods() click to toggle source

This is the list of all of the valid visit methods.

# File lib/syntax_tree/basic_visitor.rb, line 72
def valid_visit_methods
  @valid_visit_methods ||=
    Visitor.instance_methods.grep(/^visit_(?!child_nodes)/)
end
visit_method(method_name) click to toggle source

This method is here to help folks write visitors.

It’s not always easy to ensure you’re writing the correct method name in the visitor since it’s perfectly valid to define methods that don’t override these parent methods.

If you use this method, you can ensure you’re writing the correct method name. It will raise an error if the visit method you’re defining isn’t actually a method on the parent visitor.

# File lib/syntax_tree/basic_visitor.rb, line 86
def visit_method(method_name)
  return if valid_visit_methods.include?(method_name)

  raise VisitMethodError, method_name
end
visit_methods() { || ... } click to toggle source

This method is here to help folks write visitors.

Within the given block, every method that is defined will be checked to ensure it’s a valid visit method using the BasicVisitor::visit_method method defined above.

# File lib/syntax_tree/basic_visitor.rb, line 97
def visit_methods
  checker = VisitMethodsChecker.new
  extend(checker)
  yield
  checker.disable!
end

Public Instance Methods

visit(node) click to toggle source
# File lib/syntax_tree/basic_visitor.rb, line 105
def visit(node)
  node&.accept(self)
end
visit_all(nodes) click to toggle source
# File lib/syntax_tree/basic_visitor.rb, line 109
def visit_all(nodes)
  nodes.map { |node| visit(node) }
end
visit_child_nodes(node) click to toggle source
# File lib/syntax_tree/basic_visitor.rb, line 113
def visit_child_nodes(node)
  visit_all(node.child_nodes)
end