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:

Public Instance Methods

add_child( tree, child ) click to toggle source
# File lib/antlr3/tree.rb, line 690
def add_child( tree, child )
  tree.add_child( child ) if tree and child
end
child_count( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 694
def child_count( tree )
  tree.child_count
end
child_index( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 698
def child_index( tree )
  tree.child_index rescue 0
end
child_of( tree, index ) click to toggle source
# File lib/antlr3/tree.rb, line 702
def child_of( tree, index )
  tree.nil? ? nil : tree.child( index )
end
copy_node( tree_node ) click to toggle source
# File lib/antlr3/tree.rb, line 706
def copy_node( tree_node )
  tree_node and tree_node.dup
end
copy_tree( tree, parent = nil ) click to toggle source
# 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
delete_child( tree, index ) click to toggle source
# File lib/antlr3/tree.rb, line 722
def delete_child( tree, index )
  tree.delete_child( index )
end
each_ancestor( tree, include_tree = true ) { |tree| ... } click to toggle source
# 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
each_child( tree ) { |child_of( tree, i )| ... } click to toggle source
# 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
empty?( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 748
def empty?( tree )
  child_count( tree ).zero?
end
flat_list?( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 744
def flat_list?( tree )
  tree.flat_list?
end
parent( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 752
def parent( tree )
  tree.parent
end
replace_children( parent, start, stop, replacement ) click to toggle source
# File lib/antlr3/tree.rb, line 756
def replace_children( parent, start, stop, replacement )
  parent and parent.replace_children( start, stop, replacement )
end
rule_post_processing( root ) click to toggle source
# 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
set_child_index( tree, index ) click to toggle source
# File lib/antlr3/tree.rb, line 771
def set_child_index( tree, index )
  tree.child_index = index
end
set_parent( tree, parent ) click to toggle source
# File lib/antlr3/tree.rb, line 775
def set_parent( tree, parent )
  tree.parent = parent
end
set_token_boundaries( tree, start_token = nil, stop_token = nil ) click to toggle source
# 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
text_of( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 789
def text_of( tree )
  tree.text rescue nil
end
token( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 793
def token( tree )
  CommonTree === tree ? tree.token : nil
end
token_start_index( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 797
def token_start_index( tree )
  tree ? tree.token_start_index : -1
end
token_stop_index( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 801
def token_stop_index( tree )
  tree ? tree.token_stop_index : -1
end
type_name( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 805
def type_name( tree )
  tree.name rescue 'INVALID'
end
type_of( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 809
def type_of( tree )
  tree.type rescue INVALID_TOKEN_TYPE
end
unique_id( node ) click to toggle source
# File lib/antlr3/tree.rb, line 813
def unique_id( node )
  node.hash
end