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)
-
- (Visitor) initialize(adaptor = nil)
constructor
A new instance of Visitor.
- - (Object) post_action(&block)
- - (Object) pre_action(&block)
- - (Object) visit(tree, pre_action = nil, post_action = nil)
Constructor Details
- (Visitor) initialize(adaptor = nil)
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
- (Object) post_action(&block)
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 |
- (Object) pre_action(&block)
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 |
- (Object) visit(tree, pre_action = nil, post_action = nil)
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 |