class ANTLR3::AST::Visitor

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.

Public Class Methods

new( adaptor = nil ) { |self| ... } click to toggle source
# 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

Public Instance Methods

post_action( &block ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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