class ANTLR3::AST::BaseTree

A base implementation of an Abstract Syntax Tree Node. It mainly defines the methods and attributes required to implement the parent-node-children relationship that characterize a tree; it does not provide any logic concerning a node’s token payload.

Attributes

parent[RW]

Public Class Methods

new( node = nil ) click to toggle source
Calls superclass method
# File lib/antlr3/tree.rb, line 317
def initialize( node = nil )
  super()
  @parent = nil
  @child_index = 0
end

Public Instance Methods

add_child( child_tree ) click to toggle source
# File lib/antlr3/tree.rb, line 332
def add_child( child_tree )
  child_tree.nil? and return
  if child_tree.flat_list?
    self.equal?( child_tree.children ) and
      raise ArgumentError, "attempt to add child list to itself"
    child_tree.each_with_index do | child, index |
      child.parent = self
      child.child_index = length + index
    end
    concat( child_tree )
  else
    child_tree.child_index = length
    child_tree.parent = self
    self << child_tree
  end
  return( self )
end
children() click to toggle source
# File lib/antlr3/tree.rb, line 323
def children() self end
delete_child( index ) click to toggle source
# File lib/antlr3/tree.rb, line 367
def delete_child( index )
  killed = delete_at( index ) and freshen( index )
  return killed
end
detach() click to toggle source
# File lib/antlr3/tree.rb, line 350
def detach
  @parent = nil
  @child_index = -1
  return( self )
end
first_with_type( tree_type ) click to toggle source
# File lib/antlr3/tree.rb, line 328
def first_with_type( tree_type )
  find { | child | child.type == tree_type }
end
flat_list?() click to toggle source
# File lib/antlr3/tree.rb, line 386
def flat_list?
  false
end
freshen( offset = 0 ) click to toggle source
# File lib/antlr3/tree.rb, line 390
def freshen( offset = 0 )
  for i in offset ... length
    node = self[ i ]
    node.child_index = i
    node.parent = self
  end
end
inspect() click to toggle source
# File lib/antlr3/tree.rb, line 408
def inspect
  empty? and return to_s
  buffer = ''
  buffer << '(' << to_s << ' ' unless flat_list?
  buffer << map { | c | c.inspect }.join( ' ' )
  buffer << ')' unless flat_list?
  return( buffer )
end
prune() click to toggle source
# File lib/antlr3/tree.rb, line 436
def prune
  raise StopIteration
end
replace_children( start, stop, new_tree ) click to toggle source
# File lib/antlr3/tree.rb, line 372
  def replace_children( start, stop, new_tree )
    start >= length or stop >= length and
      raise IndexError, ( <<-END ).gsub!( /^\s+\| /,'' )
      | indices span beyond the number of children:
      |  children.length = #{ length }
      |  start = #{ start_index.inspect }
      |  stop  = #{ stop_index.inspect }
      END
    new_children = new_tree.flat_list? ? new_tree : [ new_tree ]
    self[ start .. stop ] = new_children
    freshen( start_index )
    return self
  end
root?() click to toggle source

protected :sanity_check, :freshen

# File lib/antlr3/tree.rb, line 443
def root?() @parent.nil? end
sanity_check( parent = nil, i = -1 ) click to toggle source
# File lib/antlr3/tree.rb, line 398
def sanity_check( parent = nil, i = -1 )
  parent == @parent or
    raise TreeInconsistency.failed_parent_check!( parent, @parent )
  i == @child_index or
    raise TreeInconsistency.failed_index_check!( i, @child_index )
  each_with_index do | child, index |
    child.sanity_check( self, index )
  end
end
set_child( index, tree ) click to toggle source
# File lib/antlr3/tree.rb, line 359
def set_child( index, tree )
  return if tree.nil?
  tree.flat_list? and raise ArgumentError, "Can't set single child to a list"
  tree.parent = self
  tree.child_index = index
  self[ index ] = tree
end
walk() { |cursor| ... } click to toggle source
# File lib/antlr3/tree.rb, line 417
def walk
  block_given? or return( enum_for( :walk ) )
  stack = []
  cursor = self
  while true
    begin
      yield( cursor )
      stack.push( Array[ *cursor ] ) unless cursor.empty?
    rescue StopIteration
      # skips adding children to prune the node
    ensure
      break if stack.empty?
      cursor = stack.last.shift
      stack.pop if stack.last.empty?
    end
  end
  return self
end