Class: ANTLR3::AST::CommonTreeNodeStream
- Inherits:
-
Object
- Object
- ANTLR3::AST::CommonTreeNodeStream
show all
- Includes:
- TreeNodeStream, Enumerable
- Defined in:
- lib/antlr3/tree.rb
Overview
An implementation of TreeNodeStream tailed for streams based on CommonTree objects. CommonTreeNodeStreams are the default input streams for tree parsers.
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 collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of CommonTreeNodeStream.
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
|
# File 'lib/antlr3/tree.rb', line 985
def initialize( *args )
options = args.last.is_a?( ::Hash ) ? args.pop : {}
case n = args.length
when 1
@root = args.first
@token_stream = @adaptor = @nodes = @down = @up = @eof = nil
when 2
@adaptor, @root = args
@token_stream = @nodes = @down = @up = @eof = nil
when 3
parent, start, stop = *args
@adaptor = parent.adaptor
@root = parent.root
@nodes = parent.nodes[ start ... stop ]
@down = parent.down
@up = parent.up
@eof = parent.eof
@token_stream = parent.token_stream
when 0
raise ArgumentError, "wrong number of arguments (0 for 1)"
else raise ArgumentError, "wrong number of arguments (#{ n } for 3)"
end
@adaptor ||= options.fetch( :adaptor ) { CommonTreeAdaptor.new }
@token_stream ||= options[ :token_stream ]
@down ||= options.fetch( :down ) { @adaptor.create_from_type( DOWN, 'DOWN' ) }
@up ||= options.fetch( :up ) { @adaptor.create_from_type( UP, 'UP' ) }
@eof ||= options.fetch( :eof ) { @adaptor.create_from_type( EOF, 'EOF' ) }
@nodes ||= []
@unique_navigation_nodes = options.fetch( :unique_navigation_nodes, false )
@position = -1
@last_marker = nil
@calls = []
end
|
Instance Attribute Details
#adaptor ⇒ Object
Returns the value of attribute adaptor.
983
984
985
|
# File 'lib/antlr3/tree.rb', line 983
def adaptor
@adaptor
end
|
#position ⇒ Object
Also known as:
index
Returns the value of attribute position.
983
984
985
|
# File 'lib/antlr3/tree.rb', line 983
def position
@position
end
|
#token_stream ⇒ Object
Returns the value of attribute token_stream.
982
983
984
|
# File 'lib/antlr3/tree.rb', line 982
def token_stream
@token_stream
end
|
#unique_navigation_nodes=(value) ⇒ Object
Sets the attribute unique_navigation_nodes
1086
1087
1088
|
# File 'lib/antlr3/tree.rb', line 1086
def unique_navigation_nodes=(value)
@unique_navigation_nodes = value
end
|
Instance Method Details
#<<(k) ⇒ Object
1100
1101
1102
|
# File 'lib/antlr3/tree.rb', line 1100
def <<( k )
self >> -k
end
|
#add_navigation_node(type) ⇒ Object
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
|
# File 'lib/antlr3/tree.rb', line 1036
def add_navigation_node( type )
navigation_node =
case type
when DOWN
has_unique_navigation_nodes? ? @adaptor.create_from_type( DOWN, 'DOWN' ) : @down
else
has_unique_navigation_nodes? ? @adaptor.create_from_type( UP, 'UP' ) : @up
end
@nodes << navigation_node
end
|
#at(index) ⇒ Object
1047
1048
1049
1050
|
# File 'lib/antlr3/tree.rb', line 1047
def at( index )
@position == -1 and fill_buffer
@nodes.at( index )
end
|
#consume ⇒ Object
1088
1089
1090
1091
1092
1093
|
# File 'lib/antlr3/tree.rb', line 1088
def consume
@position == -1 and fill_buffer
node = @nodes.fetch( @position, @eof )
@position += 1
return( node )
end
|
#current_symbol ⇒ Object
1061
1062
1063
|
# File 'lib/antlr3/tree.rb', line 1061
def current_symbol
look
end
|
#each ⇒ Object
1179
1180
1181
1182
1183
1184
|
# File 'lib/antlr3/tree.rb', line 1179
def each
@position == -1 and fill_buffer
block_given? or return enum_for( :each )
for node in @nodes do yield( node ) end
self
end
|
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
|
# File 'lib/antlr3/tree.rb', line 1155
def ( start = nil, stop = nil )
start.nil? || stop.nil? and return nil
@position == -1 and fill_buffer
if @token_stream
from = @adaptor.token_start_index( start )
to =
case @adaptor.type_of( stop )
when UP then @adaptor.token_stop_index( start )
when EOF then to = @nodes.length - 2
else @adaptor.token_stop_index( stop )
end
return @token_stream.( from, to )
end
buffer = ''
for node in @nodes
if node == start ... node == stop buffer << @adaptor.text_of( node ) end
end
return( buffer )
end
|
#fill_buffer(tree = @root) ⇒ Object
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
|
# File 'lib/antlr3/tree.rb', line 1020
def fill_buffer( tree = @root )
@nodes << tree unless nil_tree = @adaptor.flat_list?( tree )
unless @adaptor.empty?( tree )
add_navigation_node( DOWN ) unless nil_tree
@adaptor.each_child( tree ) { | c | fill_buffer( c ) }
add_navigation_node( UP ) unless nil_tree
end
@position = 0 if tree == @root
return( self )
end
|
#has_unique_navigation_nodes? ⇒ Boolean
1083
1084
1085
|
# File 'lib/antlr3/tree.rb', line 1083
def has_unique_navigation_nodes?
return @unique_navigation_nodes
end
|
#inspect ⇒ Object
1150
1151
1152
1153
|
# File 'lib/antlr3/tree.rb', line 1150
def inspect
@position == -1 and fill_buffer
@nodes.map { |nd| @adaptor.type_name( nd ) }.join( ' ' )
end
|
#look(k = 1) ⇒ Object
1052
1053
1054
1055
1056
1057
1058
1059
|
# File 'lib/antlr3/tree.rb', line 1052
def look( k = 1 )
@position == -1 and fill_buffer
k == 0 and return nil
k < 0 and return self.look_behind( -k )
absolute = @position + k - 1
@nodes.fetch( absolute, @eof )
end
|
#look_behind(k = 1) ⇒ Object
1065
1066
1067
1068
1069
|
# File 'lib/antlr3/tree.rb', line 1065
def look_behind( k = 1 )
k == 0 and return nil
absolute = @position - k
return( absolute < 0 ? nil : @nodes.fetch( absolute, @eof ) )
end
|
#mark ⇒ Object
1104
1105
1106
1107
1108
|
# File 'lib/antlr3/tree.rb', line 1104
def mark
@position == -1 and fill_buffer
@last_marker = @position
return @last_marker
end
|
#node_index(node) ⇒ Object
1031
1032
1033
1034
|
# File 'lib/antlr3/tree.rb', line 1031
def node_index( node )
@position == -1 and fill_buffer
return @nodes.index( node )
end
|
#peek(i = 1) ⇒ Object
Also known as:
>>
1095
1096
1097
|
# File 'lib/antlr3/tree.rb', line 1095
def peek( i = 1 )
@adaptor.type_of look( i )
end
|
#pop ⇒ Object
1130
1131
1132
1133
|
# File 'lib/antlr3/tree.rb', line 1130
def pop
pos = @calls.pop and seek( pos )
return pos
end
|
#push(index) ⇒ Object
1125
1126
1127
1128
|
# File 'lib/antlr3/tree.rb', line 1125
def push( index )
@calls << @position
seek( index )
end
|
#release(marker = nil) ⇒ Object
1110
1111
1112
|
# File 'lib/antlr3/tree.rb', line 1110
def release( marker = nil )
end
|
#replace_children(parent, start, stop, replacement) ⇒ Object
1141
1142
1143
|
# File 'lib/antlr3/tree.rb', line 1141
def replace_children( parent, start, stop, replacement )
parent and @adaptor.replace_children( parent, start, stop, replacement )
end
|
#reset ⇒ Object
1135
1136
1137
1138
1139
|
# File 'lib/antlr3/tree.rb', line 1135
def reset
@position = 0
@last_marker = 0
@calls = []
end
|
#rewind(marker = @last_marker, release = true) ⇒ Object
1116
1117
1118
|
# File 'lib/antlr3/tree.rb', line 1116
def rewind( marker = @last_marker, release = true )
seek( marker )
end
|
#seek(index) ⇒ Object
1120
1121
1122
1123
|
# File 'lib/antlr3/tree.rb', line 1120
def seek( index )
@position == -1 and fill_buffer
@position = index
end
|
#size ⇒ Object
1145
1146
1147
1148
|
# File 'lib/antlr3/tree.rb', line 1145
def size
@position == -1 and fill_buffer
return @nodes.length
end
|
#source_name ⇒ Object
1075
1076
1077
|
# File 'lib/antlr3/tree.rb', line 1075
def source_name
self.token_stream.source_name
end
|
#to_a ⇒ Object
1188
1189
1190
|
# File 'lib/antlr3/tree.rb', line 1188
def to_a
return @nodes.dup
end
|
#tree_adaptor ⇒ Object
1079
1080
1081
|
# File 'lib/antlr3/tree.rb', line 1079
def tree_adaptor
@adaptor
end
|
#tree_source ⇒ Object
1071
1072
1073
|
# File 'lib/antlr3/tree.rb', line 1071
def tree_source
@root
end
|