Module: ANTLR3::AST::TreeAdaptor
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
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_NODE, 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
-
#add_child(tree, child) ⇒ Object
-
#child_count(tree) ⇒ Object
-
#child_index(tree) ⇒ Object
-
#child_of(tree, index) ⇒ Object
-
#copy_node(tree_node) ⇒ Object
-
#copy_tree(tree, parent = nil) ⇒ Object
-
#delete_child(tree, index) ⇒ Object
-
#each_ancestor(tree, include_tree = true) ⇒ Object
-
#each_child(tree) ⇒ Object
-
#empty?(tree) ⇒ Boolean
-
#flat_list?(tree) ⇒ Boolean
-
#parent(tree) ⇒ Object
-
#replace_children(parent, start, stop, replacement) ⇒ Object
-
#rule_post_processing(root) ⇒ Object
-
#set_child_index(tree, index) ⇒ Object
-
#set_parent(tree, parent) ⇒ Object
-
#set_token_boundaries(tree, start_token = nil, stop_token = nil) ⇒ Object
-
#text_of(tree) ⇒ Object
-
#token(tree) ⇒ Object
-
#token_start_index(tree) ⇒ Object
-
#token_stop_index(tree) ⇒ Object
-
#type_name(tree) ⇒ Object
-
#type_of(tree) ⇒ Object
-
#unique_id(node) ⇒ Object
Methods included from Error
EarlyExit, FailedPredicate, MismatchedNotSet, MismatchedRange, MismatchedSet, MismatchedToken, MismatchedTreeNode, MissingToken, NoViableAlternative, RewriteCardinalityError, RewriteEarlyExit, RewriteEmptyStream, UnwantedToken
#create_token
Instance Method Details
#add_child(tree, child) ⇒ Object
690
691
692
|
# File 'lib/antlr3/tree.rb', line 690
def add_child( tree, child )
tree.add_child( child ) if tree and child
end
|
#child_count(tree) ⇒ Object
694
695
696
|
# File 'lib/antlr3/tree.rb', line 694
def child_count( tree )
tree.child_count
end
|
#child_index(tree) ⇒ Object
698
699
700
|
# File 'lib/antlr3/tree.rb', line 698
def child_index( tree )
tree.child_index rescue 0
end
|
#child_of(tree, index) ⇒ Object
702
703
704
|
# File 'lib/antlr3/tree.rb', line 702
def child_of( tree, index )
tree.nil? ? nil : tree.child( index )
end
|
#copy_node(tree_node) ⇒ Object
706
707
708
|
# File 'lib/antlr3/tree.rb', line 706
def copy_node( tree_node )
tree_node and tree_node.dup
end
|
#copy_tree(tree, parent = nil) ⇒ Object
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
|
#delete_child(tree, index) ⇒ Object
722
723
724
|
# File 'lib/antlr3/tree.rb', line 722
def delete_child( tree, index )
tree.delete_child( index )
end
|
#each_ancestor(tree, include_tree = true) ⇒ Object
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
|
#each_child(tree) ⇒ Object
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
|
#empty?(tree) ⇒ Boolean
748
749
750
|
# File 'lib/antlr3/tree.rb', line 748
def empty?( tree )
child_count( tree ).zero?
end
|
#flat_list?(tree) ⇒ Boolean
744
745
746
|
# File 'lib/antlr3/tree.rb', line 744
def flat_list?( tree )
tree.flat_list?
end
|
#parent(tree) ⇒ Object
752
753
754
|
# File 'lib/antlr3/tree.rb', line 752
def parent( tree )
tree.parent
end
|
#replace_children(parent, start, stop, replacement) ⇒ Object
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
|
#rule_post_processing(root) ⇒ Object
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
|
#set_child_index(tree, index) ⇒ Object
771
772
773
|
# File 'lib/antlr3/tree.rb', line 771
def set_child_index( tree, index )
tree.child_index = index
end
|
#set_parent(tree, parent) ⇒ Object
775
776
777
|
# 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) ⇒ Object
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
|
#text_of(tree) ⇒ Object
789
790
791
|
# File 'lib/antlr3/tree.rb', line 789
def text_of( tree )
tree.text rescue nil
end
|
#token(tree) ⇒ Object
793
794
795
|
# File 'lib/antlr3/tree.rb', line 793
def token( tree )
CommonTree === tree ? tree.token : nil
end
|
#token_start_index(tree) ⇒ Object
797
798
799
|
# File 'lib/antlr3/tree.rb', line 797
def token_start_index( tree )
tree ? tree.token_start_index : -1
end
|
#token_stop_index(tree) ⇒ Object
801
802
803
|
# File 'lib/antlr3/tree.rb', line 801
def token_stop_index( tree )
tree ? tree.token_stop_index : -1
end
|
#type_name(tree) ⇒ Object
805
806
807
|
# File 'lib/antlr3/tree.rb', line 805
def type_name( tree )
tree.name rescue 'INVALID'
end
|
#type_of(tree) ⇒ Object
809
810
811
|
# File 'lib/antlr3/tree.rb', line 809
def type_of( tree )
tree.type rescue INVALID_TOKEN_TYPE
end
|
#unique_id(node) ⇒ Object
813
814
815
|
# File 'lib/antlr3/tree.rb', line 813
def unique_id( node )
node.hash
end
|