Class: TireSwing::Visitor
Overview
Represents a visitor for an AST. See TireSwing::VisitorDefinition more more information.
Class Method Summary collapse
-
.visit(node, *args) ⇒ Object
Visit the given node using the visitor mapping defined with .visits.
-
.visits(*constants, &blk) ⇒ Object
Describe a visitor block for a given node type.
Class 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.
24 25 26 27 28 29 30 31 |
# File 'lib/tire_swing/visitor.rb', line 24 def visit(node, *args) block = visitor_for(node) if args.empty? block.call(node) else block.call(node, *args) end 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 |