Class: TireSwing::Visitor
Overview
Represents a visitor for an AST. See TireSwing::VisitorDefinition more more information.
Class Method Summary collapse
-
.visit(node, *args) ⇒ Object
delegates to a new instance of the visitor class.
-
.visitor_for(node) ⇒ Object
Look up a visitor block for this node.
-
.visits(*constants, &blk) ⇒ Object
Describe a visitor block for a given node type.
Instance Method Summary collapse
-
#visit(node, *args) ⇒ Object
Visit the given node using the visitor mapping defined with .visits.
Class Method Details
.visit(node, *args) ⇒ Object
delegates to a new instance of the visitor class
20 21 22 23 |
# File 'lib/tire_swing/visitor.rb', line 20 def visit(node, *args) visitor = new visitor.visit(node, *args) end |
.visitor_for(node) ⇒ Object
Look up a visitor block for this node
26 27 28 |
# File 'lib/tire_swing/visitor.rb', line 26 def visitor_for(node) nodes[node.class] or raise "could not find visitor definition for #{node.class}: #{node.inspect}" end |
.visits(*constants, &blk) ⇒ Object
Describe a visitor block for a given node type. If no block is given, an empty lambda is used. The block always takes at least one argument, an instance of the node you’re visiting, as well as any additional arguments if desired.
13 14 15 16 17 |
# File 'lib/tire_swing/visitor.rb', line 13 def visits(*constants, &blk) constants.each do |constant| nodes[constant] = (blk || lambda {}) end end |
Instance Method Details
#visit(node, *args) ⇒ Object
Visit the given node using the visitor mapping defined with .visits. This finds the block to call for the given node and calls it with the node and additional arguments, if any.
Raises an exception if no visitor is found for the given node.
44 45 46 47 48 49 50 51 |
# File 'lib/tire_swing/visitor.rb', line 44 def visit(node, *args) block = self.class.visitor_for(node) if args.empty? block.call(node) else block.call(node, *args) end end |