class ANTLR3::AST::CommonTreeNodeStream

An implementation of TreeNodeStream tailed for streams based on CommonTree objects. CommonTreeNodeStreams are the default input streams for tree parsers.

Attributes

adaptor[R]
index[R]
last_marker[R]
position[R]
token_stream[RW]
unique_navigation_nodes[W]

Public Class Methods

new( *args ) click to toggle source
# 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

Public Instance Methods

<<( k ) click to toggle source
# File lib/antlr3/tree.rb, line 1100
def <<( k )
  self >> -k
end
>>( i = 1 )
Alias for: peek
add_navigation_node( type ) click to toggle source
# 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 ) click to toggle source
# File lib/antlr3/tree.rb, line 1047
def at( index )
  @position == -1 and fill_buffer
  @nodes.at( index )
end
consume() click to toggle source
# 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() click to toggle source
# File lib/antlr3/tree.rb, line 1061
def current_symbol
  look
end
each() { |node| ... } click to toggle source
# 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
extract_text( start = nil, stop = nil ) click to toggle source
# File lib/antlr3/tree.rb, line 1155
def extract_text( 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.extract_text( from, to )
  end
  
  buffer = ''
  for node in @nodes
    if node == start ... node == stop  # <-- hey look, it's the flip flop operator
      buffer << @adaptor.text_of( node ) #|| ' ' << @adaptor.type_of( node ).to_s )
    end
  end
  return( buffer )
end
Also aliased as: to_s
fill_buffer( tree = @root ) click to toggle source
# 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?() click to toggle source
# File lib/antlr3/tree.rb, line 1083
def has_unique_navigation_nodes?
  return @unique_navigation_nodes
end
inspect() click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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() click to toggle source
# File lib/antlr3/tree.rb, line 1104
def mark
  @position == -1 and fill_buffer
  @last_marker = @position
  return @last_marker
end
node_index( node ) click to toggle source
# File lib/antlr3/tree.rb, line 1031
def node_index( node )
  @position == -1 and fill_buffer
  return @nodes.index( node )
end
peek( i = 1 ) click to toggle source
# File lib/antlr3/tree.rb, line 1095
def peek( i = 1 )
  @adaptor.type_of look( i )
end
Also aliased as: >>
pop() click to toggle source
# File lib/antlr3/tree.rb, line 1130
def pop
  pos = @calls.pop and seek( pos )
  return pos
end
push( index ) click to toggle source
# File lib/antlr3/tree.rb, line 1125
def push( index )
  @calls << @position
  seek( index )
end
release( marker = nil ) click to toggle source
# File lib/antlr3/tree.rb, line 1110
def release( marker = nil )
  # do nothing?
end
replace_children( parent, start, stop, replacement ) click to toggle source
# 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() click to toggle source
# File lib/antlr3/tree.rb, line 1135
def reset
  @position = 0
  @last_marker = 0
  @calls = []
end
rewind( marker = @last_marker, release = true ) click to toggle source
# File lib/antlr3/tree.rb, line 1116
def rewind( marker = @last_marker, release = true )
  seek( marker )
end
seek( index ) click to toggle source
# File lib/antlr3/tree.rb, line 1120
def seek( index )
  @position == -1 and fill_buffer
  @position = index
end
size() click to toggle source
# File lib/antlr3/tree.rb, line 1145
def size
  @position == -1 and fill_buffer
  return @nodes.length
end
source_name() click to toggle source
# File lib/antlr3/tree.rb, line 1075
def source_name
  self.token_stream.source_name
end
to_a() click to toggle source
# File lib/antlr3/tree.rb, line 1188
def to_a
  return @nodes.dup
end
to_s( start = nil, stop = nil )
Alias for: extract_text
tree_adaptor() click to toggle source
# File lib/antlr3/tree.rb, line 1079
def tree_adaptor
  @adaptor
end
tree_source() click to toggle source
# File lib/antlr3/tree.rb, line 1071
def tree_source
  @root
end