class ANTLR3::AST::CommonTree

The default Tree class implementation used by ANTLR tree-related code.

A CommonTree object is a tree node that wraps a token payload (or a nil value) and contains zero or more child tree nodes. Additionally, it tracks information about the range of data collectively spanned by the tree node:

Tracking this information simplifies tasks like extracting a block of code or rewriting the input stream. However, depending on the purpose of the application, building trees with all of this extra information may be unnecessary. In such a case, a more bare-bones tree class could be written (optionally using the BaseTree class or the Token module). Define a customized TreeAdaptor class to handle tree construction and manipulation for the customized node class, and recognizers will be able to build, rewrite, and parse the customized lighter-weight trees.

Public Class Methods

new( payload = nil ) click to toggle source
Calls superclass method ANTLR3::AST::BaseTree::new
# File lib/antlr3/tree.rb, line 473
def initialize( payload = nil )
  super()
  @start_index = -1
  @stop_index = -1
  @child_index = -1
  case payload
  when CommonTree then   # copy-constructor style init
    @token       = payload.token
    @start_index = payload.start_index
    @stop_index  = payload.stop_index
  when nil, Token then @token = payload
  else raise ArgumentError,
    "Invalid argument type: %s (%p)" % [ payload.class, payload ]
  end
end

Public Instance Methods

column() click to toggle source
# File lib/antlr3/tree.rb, line 518
def column
  if @token.nil? or @token.column == -1
    return( empty? ? 0 : first.column )
  end
  return @token.column
end
copy_node() click to toggle source
# File lib/antlr3/tree.rb, line 495
def copy_node
  return self.class.new( @token )
end
flat_list?() click to toggle source
# File lib/antlr3/tree.rb, line 499
def flat_list?
  @token.nil?
end
infer_boundaries() click to toggle source
# File lib/antlr3/tree.rb, line 559
def infer_boundaries
  if empty? and @start_index < 0 || @stop_index < 0
    @start_index = @stop_index = @token.index rescue -1
    return
  end
  for child in self do child.infer_boundaries end
  return if @start_index >= 0 and @stop_index >= 0
  
  @start_index = first.start_index
  @stop_index  = last.stop_index
  return nil
end
initialize_copy( orig ) click to toggle source
Calls superclass method
# File lib/antlr3/tree.rb, line 489
def initialize_copy( orig )
  super
  clear
  @parent = nil
end
line() click to toggle source
# File lib/antlr3/tree.rb, line 511
def line
  if @token.nil? or @token.line == 0
    return ( empty? ? 0 : first.line )
  end
  return @token.line
end
name() click to toggle source
# File lib/antlr3/tree.rb, line 540
def name
  @token.name rescue 'INVALID'
end
pretty_print( printer ) click to toggle source
# File lib/antlr3/tree.rb, line 580
def pretty_print( printer )
  text = @token ? @token.text : 'nil'
  text =~ /\s+/ and
    text = text.dump
  
  if empty?
    printer.text( text )
  else
    endpoints = @token ? [ "(#{ text }", ')' ] : [ '', '' ]
    printer.group( 1, *endpoints ) do
      for child in self
        printer.breakable
        printer.pp( child )
      end
    end
  end
end
source_range() click to toggle source
# File lib/antlr3/tree.rb, line 549
def source_range
  unknown_boundaries? and infer_boundaries
  tokens = map do | node |
    tk = node.token and tk.index >= 0 ? tk : nil
  end
  tokens.compact!
  first, last = tokens.minmax_by { |t| t.index }
  first.start .. last.stop
end
start_index() click to toggle source
# File lib/antlr3/tree.rb, line 525
def start_index
  @start_index == -1 and @token and return @token.index
  return @start_index
end
Also aliased as: token_start_index
stop_index() click to toggle source
# File lib/antlr3/tree.rb, line 530
def stop_index
  @stop_index == -1 and @token and return @token.index
  return @stop_index
end
Also aliased as: token_stop_index
text() click to toggle source
# File lib/antlr3/tree.rb, line 507
def text
  @token.text rescue nil
end
to_s() click to toggle source
# File lib/antlr3/tree.rb, line 576
def to_s
  flat_list? ? 'nil' : @token.text.to_s
end
token_range() click to toggle source
# File lib/antlr3/tree.rb, line 544
def token_range
  unknown_boundaries? and infer_boundaries
  @start_index .. @stop_index
end
token_start_index()
Alias for: start_index
token_stop_index()
Alias for: stop_index
type() click to toggle source
# File lib/antlr3/tree.rb, line 503
def type
  @token ? @token.type : 0
end
unknown_boundaries?() click to toggle source
# File lib/antlr3/tree.rb, line 572
def unknown_boundaries?
  @start_index < 0 or @stop_index < 0
end