Class: SyntaxTree::BasicVisitor
- Inherits:
-
Object
- Object
- SyntaxTree::BasicVisitor
- Defined in:
- lib/syntax_tree/basic_visitor.rb
Overview
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.
Direct Known Subclasses
FieldVisitor, MutationVisitor, Translation::Parser, Visitor, YARV::Compiler, YARV::Compiler::RubyVisitor
Defined Under Namespace
Classes: VisitMethodChecker, VisitMethodError, VisitMethodsChecker
Class Method Summary collapse
-
.valid_visit_methods ⇒ Object
This is the list of all of the valid visit methods.
-
.visit_method(method_name) ⇒ Object
This method is here to help folks write visitors.
-
.visit_methods ⇒ Object
This method is here to help folks write visitors.
Instance Method Summary collapse
Class Method Details
.valid_visit_methods ⇒ Object
This is the list of all of the valid visit methods.
72 73 74 75 |
# 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) ⇒ Object
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.
86 87 88 89 90 |
# 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 ⇒ Object
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.
97 98 99 100 101 102 |
# File 'lib/syntax_tree/basic_visitor.rb', line 97 def visit_methods checker = VisitMethodsChecker.new extend(checker) yield checker.disable! end |
Instance Method Details
#visit(node) ⇒ Object
105 106 107 |
# File 'lib/syntax_tree/basic_visitor.rb', line 105 def visit(node) node&.accept(self) end |
#visit_all(nodes) ⇒ Object
109 110 111 |
# File 'lib/syntax_tree/basic_visitor.rb', line 109 def visit_all(nodes) nodes.map { |node| visit(node) } end |
#visit_child_nodes(node) ⇒ Object
113 114 115 |
# File 'lib/syntax_tree/basic_visitor.rb', line 113 def visit_child_nodes(node) visit_all(node.child_nodes) end |