Class: ANTLR3::AST::Visitor
- Inherits:
-
Object
- Object
- ANTLR3::AST::Visitor
- Defined in:
- lib/antlr3/tree/visitor.rb
Overview
AST::Visitor is an extra utility class for working with tree objects. Visitor objects are similar to plain vanilla iterators, but provide two action hooks, instead a standard single iteration block. The visit(tree)
method walks through each node of the tree (top-down left-to-right). If pre_action
is defined, a node is yielded to the block when it has been initially entered. If post_action
is defined, a node is yielded to the block after all of its children have been visited.
Instance Method Summary collapse
-
#initialize(adaptor = nil) ⇒ Visitor
constructor
A new instance of Visitor.
- #post_action(&block) ⇒ Object
- #pre_action(&block) ⇒ Object
- #visit(tree, pre_action = nil, post_action = nil) ⇒ Object
Constructor Details
#initialize(adaptor = nil) ⇒ Visitor
Returns a new instance of Visitor.
51 52 53 54 55 56 |
# File 'lib/antlr3/tree/visitor.rb', line 51 def initialize( adaptor = nil ) @adaptor = adaptor || CommonTreeAdaptor.new() @pre_action = nil @post_action = nil block_given? and yield( self ) end |
Instance Method Details
#post_action(&block) ⇒ Object
63 64 65 66 |
# File 'lib/antlr3/tree/visitor.rb', line 63 def post_action( &block ) block_given? and @post_action = block return @post_action end |
#pre_action(&block) ⇒ Object
58 59 60 61 |
# File 'lib/antlr3/tree/visitor.rb', line 58 def pre_action( &block ) block_given? and @pre_action = block return @pre_action end |
#visit(tree, pre_action = nil, post_action = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/antlr3/tree/visitor.rb', line 68 def visit( tree, pre_action = nil, post_action = nil ) flat = @adaptor.flat_list?( tree ) before = pre_action || @pre_action after = post_action || @post_action tree = before.call( tree ) unless before.nil? or flat @adaptor.child_count( tree ).times do |index| child = @adaptor.child_of( tree, index ) visit( child, pre_action, post_action ) end tree = after.call( tree ) unless after.nil? or flat return tree end |