Module: ANTLR3::AST::TreeAdaptor

Includes:
Constants, Error, TokenFactory
Included in:
CommonTreeAdaptor
Defined in:
lib/antlr3/tree.rb

Overview

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

Constant Summary

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_TOKEN, Constants::INVALID_TOKEN_TYPE, 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 Error

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

Methods included from TokenFactory

#create_token

Instance Method Details

- (Object) add_child(tree, child)



690
691
692
# File 'lib/antlr3/tree.rb', line 690

def add_child( tree, child )
  tree.add_child( child ) if tree and child
end

- (Object) child_count(tree)



694
695
696
# File 'lib/antlr3/tree.rb', line 694

def child_count( tree )
  tree.child_count
end

- (Object) child_index(tree)



698
699
700
# File 'lib/antlr3/tree.rb', line 698

def child_index( tree )
  tree.child_index rescue 0
end

- (Object) child_of(tree, index)



702
703
704
# File 'lib/antlr3/tree.rb', line 702

def child_of( tree, index )
  tree.nil? ? nil : tree.child( index )
end

- (Object) copy_node(tree_node)



706
707
708
# File 'lib/antlr3/tree.rb', line 706

def copy_node( tree_node )
  tree_node and tree_node.dup
end

- (Object) copy_tree(tree, parent = nil)



710
711
712
713
714
715
716
717
718
719
720
# 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

- (Object) delete_child(tree, index)



722
723
724
# File 'lib/antlr3/tree.rb', line 722

def delete_child( tree, index )
  tree.delete_child( index )
end

- (Object) each_ancestor(tree, include_tree = true)



735
736
737
738
739
740
741
742
# 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

- (Object) each_child(tree)



727
728
729
730
731
732
733
# 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

- (Boolean) empty?(tree)

Returns:

  • (Boolean)


748
749
750
# File 'lib/antlr3/tree.rb', line 748

def empty?( tree )
  child_count( tree ).zero?
end

- (Boolean) flat_list?(tree)

Returns:

  • (Boolean)


744
745
746
# File 'lib/antlr3/tree.rb', line 744

def flat_list?( tree )
  tree.flat_list?
end

- (Object) parent(tree)



752
753
754
# File 'lib/antlr3/tree.rb', line 752

def parent( tree )
  tree.parent
end

- (Object) replace_children(parent, start, stop, replacement)



756
757
758
# File 'lib/antlr3/tree.rb', line 756

def replace_children( parent, start, stop, replacement )
  parent and parent.replace_children( start, stop, replacement )
end

- (Object) rule_post_processing(root)



760
761
762
763
764
765
766
767
768
769
# 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

- (Object) set_child_index(tree, index)



771
772
773
# File 'lib/antlr3/tree.rb', line 771

def set_child_index( tree, index )
  tree.child_index = index
end

- (Object) set_parent(tree, parent)



775
776
777
# File 'lib/antlr3/tree.rb', line 775

def set_parent( tree, parent )
  tree.parent = parent
end

- (Object) set_token_boundaries(tree, start_token = nil, stop_token = nil)



779
780
781
782
783
784
785
786
787
# 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

- (Object) text_of(tree)



789
790
791
# File 'lib/antlr3/tree.rb', line 789

def text_of( tree )
  tree.text rescue nil
end

- (Object) token(tree)



793
794
795
# File 'lib/antlr3/tree.rb', line 793

def token( tree )
  CommonTree === tree ? tree.token : nil
end

- (Object) token_start_index(tree)



797
798
799
# File 'lib/antlr3/tree.rb', line 797

def token_start_index( tree )
  tree ? tree.token_start_index : -1
end

- (Object) token_stop_index(tree)



801
802
803
# File 'lib/antlr3/tree.rb', line 801

def token_stop_index( tree )
  tree ? tree.token_stop_index : -1
end

- (Object) type_name(tree)



805
806
807
# File 'lib/antlr3/tree.rb', line 805

def type_name( tree )
  tree.name rescue 'INVALID'
end

- (Object) type_of(tree)



809
810
811
# File 'lib/antlr3/tree.rb', line 809

def type_of( tree )
  tree.type rescue INVALID_TOKEN_TYPE
end

- (Object) unique_id(node)



813
814
815
# File 'lib/antlr3/tree.rb', line 813

def unique_id( node )
  node.hash
end