class ANTLR3::AST::CommonTreeAdaptor

The default tree adaptor used by ANTLR-generated tree code. It, of course, builds and manipulates CommonTree nodes.

Public Class Methods

new( token_class = ANTLR3::CommonToken ) click to toggle source
# File lib/antlr3/tree.rb, line 831
def initialize( token_class = ANTLR3::CommonToken )
  @token_class = token_class
end

Public Instance Methods

become_root( new_root, old_root ) click to toggle source
# File lib/antlr3/tree.rb, line 840
def become_root( new_root, old_root )
  new_root = create( new_root ) if new_root.is_a?( Token )
  old_root or return( new_root )
  
  new_root = create_with_payload( new_root ) unless CommonTree === new_root
  if new_root.flat_list?
    count = new_root.child_count
    if count == 1
      new_root = new_root.child( 0 )
    elsif count > 1
      raise TreeInconsistency.multiple_roots!
    end
  end
  
  new_root.add_child( old_root )
  return new_root
end
create( *args ) click to toggle source
# File lib/antlr3/tree.rb, line 879
def create( *args )
  n = args.length
  if n == 1 and args.first.is_a?( Token ) then create_with_payload( args[ 0 ] )
  elsif n == 2 and Integer === args.first and String === args[ 1 ]
    create_from_type( *args )
  elsif n >= 2 and Integer === args.first
    create_from_token( *args )
  else
    sig = args.map { |f| f.class }.join( ', ' )
    raise TypeError, "No create method with this signature found: (#{ sig })"
  end
end
create_error_node( input, start, stop, exc ) click to toggle source
# File lib/antlr3/tree.rb, line 871
def create_error_node( input, start, stop, exc )
  CommonErrorNode.new( input, start, stop, exc )
end
create_flat_list() click to toggle source
# File lib/antlr3/tree.rb, line 835
def create_flat_list
  return create_with_payload( nil )
end
Also aliased as: create_flat_list!
create_flat_list!()
Alias for: create_flat_list
create_from_token( token_type, from_token, text = nil ) click to toggle source
# File lib/antlr3/tree.rb, line 858
def create_from_token( token_type, from_token, text = nil )
  from_token = from_token.dup
  from_token.type = token_type
  from_token.text = text.to_s if text
  tree = create_with_payload( from_token )
  return tree
end
create_from_type( token_type, text ) click to toggle source
# File lib/antlr3/tree.rb, line 866
def create_from_type( token_type, text )
  from_token = create_token( token_type, DEFAULT_CHANNEL, text )
  create_with_payload( from_token )
end
create_with_payload( payload ) click to toggle source
# File lib/antlr3/tree.rb, line 875
def create_with_payload( payload )
  return CommonTree.new( payload )
end
each_child( tree ) { |child| ... } click to toggle source
# File lib/antlr3/tree.rb, line 917
def each_child( tree )
  block_given? or return enum_for( :each_child, tree )
  tree.each do | child |
    yield( child )
  end
end
empty?( tree ) click to toggle source
# File lib/antlr3/tree.rb, line 913
def empty?( tree )
  tree.empty?
end
rule_post_processing( root ) click to toggle source
# File lib/antlr3/tree.rb, line 904
def rule_post_processing( root )
  if root and root.flat_list?
    if root.empty? then root = nil
    elsif root.child_count == 1 then root = root.first.detach
    end
  end
  return root
end