module ANTLR3::AST::TreeAdaptor
Since a tree can be represented by a multitude of formats, ANTLR’s tree-related code mandates the use of Tree
Adaptor objects to build and manipulate any actual trees. Using an adaptor object permits a single recognizer to work with any number of different tree structures without adding rigid interface requirements on customized tree structures. For example, if you want to represent trees using simple arrays of arrays, you just need to design an appropriate tree adaptor and provide it to the parser.
Tree
adaptors are tasked with:
-
copying and creating tree nodes and tokens
-
defining parent-child relationships between nodes
-
cleaning up / normalizing a full tree structure after construction
-
reading and writing the attributes ANTLR expects of tree nodes
-
providing node access and iteration
Public Instance Methods
# File lib/antlr3/tree.rb, line 690 def add_child( tree, child ) tree.add_child( child ) if tree and child end
# File lib/antlr3/tree.rb, line 694 def child_count( tree ) tree.child_count end
# File lib/antlr3/tree.rb, line 698 def child_index( tree ) tree.child_index rescue 0 end
# File lib/antlr3/tree.rb, line 702 def child_of( tree, index ) tree.nil? ? nil : tree.child( index ) end
# File lib/antlr3/tree.rb, line 706 def copy_node( tree_node ) tree_node and tree_node.dup end
# File lib/antlr3/tree.rb, line 710 def copy_tree( tree, parent = nil ) tree or return nil new_tree = copy_node( tree ) set_child_index( new_tree, child_index( tree ) ) set_parent( new_tree, parent ) each_child( tree ) do | child | new_sub_tree = copy_tree( child, new_tree ) add_child( new_tree, new_sub_tree ) end return new_tree end
# File lib/antlr3/tree.rb, line 722 def delete_child( tree, index ) tree.delete_child( index ) end
# File lib/antlr3/tree.rb, line 735 def each_ancestor( tree, include_tree = true ) block_given? or return enum_for( :each_ancestor, tree, include_tree ) if include_tree begin yield( tree ) end while tree = parent_of( tree ) else while tree = parent_of( tree ) do yield( tree ) end end end
# File lib/antlr3/tree.rb, line 727 def each_child( tree ) block_given? or return enum_for( :each_child, tree ) for i in 0 ... child_count( tree ) yield( child_of( tree, i ) ) end return tree end
# File lib/antlr3/tree.rb, line 748 def empty?( tree ) child_count( tree ).zero? end
# File lib/antlr3/tree.rb, line 744 def flat_list?( tree ) tree.flat_list? end
# File lib/antlr3/tree.rb, line 752 def parent( tree ) tree.parent end
# File lib/antlr3/tree.rb, line 756 def replace_children( parent, start, stop, replacement ) parent and parent.replace_children( start, stop, replacement ) end
# File lib/antlr3/tree.rb, line 760 def rule_post_processing( root ) if root and root.flat_list? case root.child_count when 0 then root = nil when 1 root = root.child( 0 ).detach end end return root end
# File lib/antlr3/tree.rb, line 771 def set_child_index( tree, index ) tree.child_index = index end
# File lib/antlr3/tree.rb, line 775 def set_parent( tree, parent ) tree.parent = parent end
# File lib/antlr3/tree.rb, line 779 def set_token_boundaries( tree, start_token = nil, stop_token = nil ) return unless tree start = stop = 0 start_token and start = start_token.index stop_token and stop = stop_token.index tree.start_index = start tree.stop_index = stop return tree end
# File lib/antlr3/tree.rb, line 789 def text_of( tree ) tree.text rescue nil end
# File lib/antlr3/tree.rb, line 793 def token( tree ) CommonTree === tree ? tree.token : nil end
# File lib/antlr3/tree.rb, line 797 def token_start_index( tree ) tree ? tree.token_start_index : -1 end
# File lib/antlr3/tree.rb, line 801 def token_stop_index( tree ) tree ? tree.token_stop_index : -1 end
# File lib/antlr3/tree.rb, line 805 def type_name( tree ) tree.name rescue 'INVALID' end
# File lib/antlr3/tree.rb, line 809 def type_of( tree ) tree.type rescue INVALID_TOKEN_TYPE end
# File lib/antlr3/tree.rb, line 813 def unique_id( node ) node.hash end