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:
-
the token stream start and stop indexes of tokens contained throughout the tree
-
that start and stop positions of the character input stream from which the tokens were defined
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
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
# 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
# File lib/antlr3/tree.rb, line 495 def copy_node return self.class.new( @token ) end
# File lib/antlr3/tree.rb, line 499 def flat_list? @token.nil? end
# 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
# File lib/antlr3/tree.rb, line 489 def initialize_copy( orig ) super clear @parent = nil end
# 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
# File lib/antlr3/tree.rb, line 540 def name @token.name rescue 'INVALID' end
# 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
# 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
# File lib/antlr3/tree.rb, line 525 def start_index @start_index == -1 and @token and return @token.index return @start_index end
# File lib/antlr3/tree.rb, line 530 def stop_index @stop_index == -1 and @token and return @token.index return @stop_index end
# File lib/antlr3/tree.rb, line 507 def text @token.text rescue nil end
# File lib/antlr3/tree.rb, line 576 def to_s flat_list? ? 'nil' : @token.text.to_s end
# File lib/antlr3/tree.rb, line 544 def token_range unknown_boundaries? and infer_boundaries @start_index .. @stop_index end
# File lib/antlr3/tree.rb, line 503 def type @token ? @token.type : 0 end
# File lib/antlr3/tree.rb, line 572 def unknown_boundaries? @start_index < 0 or @stop_index < 0 end