Class: ANTLR3::AST::CommonTreeAdaptor

Inherits:
Object
  • Object
show all
Extended by:
ClassMacros
Includes:
TreeAdaptor, Constants
Defined in:
lib/antlr3/tree.rb

Overview

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

Direct Known Subclasses

Wizard::PatternAdaptor

Constant Summary

Constants included from Constants

Constants::BUILT_IN_TOKEN_NAMES, Constants::DEFAULT, Constants::DOWN, Constants::EOF, Constants::EOF_TOKEN, Constants::EOR_TOKEN_TYPE, Constants::HIDDEN, Constants::INVALID, Constants::INVALID_TOKEN, Constants::MEMO_RULE_FAILED, Constants::MEMO_RULE_UNKNOWN, Constants::MIN_TOKEN_TYPE, Constants::SKIP_TOKEN, Constants::UP

Instance Attribute Summary

Attributes included from TokenFactory

#token_class

Instance Method Summary collapse

Methods included from TreeAdaptor

#add_child, #child_count, #child_index, #child_of, #copy_node, #copy_tree, #delete_child, #each_ancestor, #flat_list?, #parent, #replace_children, #set_child_index, #set_parent, #set_token_boundaries, #text_of, #token, #token_start_index, #token_stop_index, #type_name, #type_of, #unique_id

Methods included from Error

EarlyExit, FailedPredicate, MismatchedNotSet, MismatchedRange, MismatchedSet, MismatchedToken, MismatchedTreeNode, MissingToken, NoViableAlternative, RewriteCardinalityError, RewriteEarlyExit, RewriteEmptyStream, UnwantedToken

Methods included from TokenFactory

#create_token

Constructor Details

#initialize(token_class = ANTLR3::CommonToken) ⇒ CommonTreeAdaptor

Returns a new instance of CommonTreeAdaptor


831
832
833
# File 'lib/antlr3/tree.rb', line 831

def initialize( token_class = ANTLR3::CommonToken )
  @token_class = token_class
end

Instance Method Details

#become_root(new_root, old_root) ⇒ Object


840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# 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) ⇒ Object


879
880
881
882
883
884
885
886
887
888
889
890
# 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) ⇒ Object


871
872
873
# File 'lib/antlr3/tree.rb', line 871

def create_error_node( input, start, stop, exc )
  CommonErrorNode.new( input, start, stop, exc )
end

#create_flat_listObject Also known as: create_flat_list!


835
836
837
# File 'lib/antlr3/tree.rb', line 835

def create_flat_list
  return create_with_payload( nil )
end

#create_from_token(token_type, from_token, text = nil) ⇒ Object


858
859
860
861
862
863
864
# 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) ⇒ Object


866
867
868
869
# 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) ⇒ Object


875
876
877
# File 'lib/antlr3/tree.rb', line 875

def create_with_payload( payload )
  return CommonTree.new( payload )
end

#each_child(tree) ⇒ Object


917
918
919
920
921
922
# 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) ⇒ Boolean

Returns:

  • (Boolean)

913
914
915
# File 'lib/antlr3/tree.rb', line 913

def empty?( tree )
  tree.empty?
end

#rule_post_processing(root) ⇒ Object


904
905
906
907
908
909
910
911
# 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