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
Defined Under Namespace
Classes: VisitMethodChecker, VisitMethodError
Class Method Summary collapse
-
.visit_method(method_name) ⇒ Object
This method is here to help folks write visitors.
-
.visit_methods ⇒ Object
This is the list of all of the valid visit methods.
Instance Method Summary collapse
Class Method Details
.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.
53 54 55 56 57 |
# File 'lib/syntax_tree/basic_visitor.rb', line 53 def visit_method(method_name) return if visit_methods.include?(method_name) raise VisitMethodError, method_name end |
.visit_methods ⇒ Object
This is the list of all of the valid visit methods.
60 61 62 63 |
# File 'lib/syntax_tree/basic_visitor.rb', line 60 def visit_methods @visit_methods ||= Visitor.instance_methods.grep(/^visit_(?!child_nodes)/) end |
Instance Method Details
#visit(node) ⇒ Object
66 67 68 |
# File 'lib/syntax_tree/basic_visitor.rb', line 66 def visit(node) node&.accept(self) end |
#visit_all(nodes) ⇒ Object
70 71 72 |
# File 'lib/syntax_tree/basic_visitor.rb', line 70 def visit_all(nodes) nodes.map { |node| visit(node) } end |
#visit_child_nodes(node) ⇒ Object
74 75 76 |
# File 'lib/syntax_tree/basic_visitor.rb', line 74 def visit_child_nodes(node) visit_all(node.child_nodes) end |