Class: ANTLR3::AST::CommonTree

Inherits:
BaseTree show all
Defined in:
lib/antlr3/tree.rb

Overview

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.

Direct Known Subclasses

CommonErrorNode, Wizard::Pattern

Instance Attribute Summary

Attributes inherited from BaseTree

#parent

Attributes included from Tree

#child_index, #token

Instance Method Summary collapse

Methods inherited from BaseTree

#add_child, #children, #delete_child, #detach, #first_with_type, #freshen, #inspect, #prune, #replace_children, #root?, #sanity_check, #set_child, #walk

Methods included from Tree

#ancestors, #depth, #each_ancestor, #has_child?, #leaf?, #root, #root?, #siblings, #walk

Methods inherited from Array

#pad, #pad!

Constructor Details

#initialize(payload = nil) ⇒ CommonTree

Returns a new instance of CommonTree.


473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# 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

Instance Method Details

#columnObject


518
519
520
521
522
523
# 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_nodeObject


495
496
497
# File 'lib/antlr3/tree.rb', line 495

def copy_node
  return self.class.new( @token )
end

#flat_list?Boolean

Returns:

  • (Boolean)

499
500
501
# File 'lib/antlr3/tree.rb', line 499

def flat_list?
  @token.nil?
end

#infer_boundariesObject


559
560
561
562
563
564
565
566
567
568
569
570
# 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) ⇒ Object


489
490
491
492
493
# File 'lib/antlr3/tree.rb', line 489

def initialize_copy( orig )
  super
  clear
  @parent = nil
end

#lineObject


511
512
513
514
515
516
# 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

#nameObject


540
541
542
# File 'lib/antlr3/tree.rb', line 540

def name
  @token.name rescue 'INVALID'
end

#pretty_print(printer) ⇒ Object


580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# 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_rangeObject


549
550
551
552
553
554
555
556
557
# 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_indexObject Also known as: token_start_index


525
526
527
528
# File 'lib/antlr3/tree.rb', line 525

def start_index
  @start_index == -1 and @token and return @token.index
  return @start_index
end

#stop_indexObject Also known as: token_stop_index


530
531
532
533
# File 'lib/antlr3/tree.rb', line 530

def stop_index
  @stop_index == -1 and @token and return @token.index
  return @stop_index
end

#textObject


507
508
509
# File 'lib/antlr3/tree.rb', line 507

def text
  @token.text rescue nil
end

#to_sObject


576
577
578
# File 'lib/antlr3/tree.rb', line 576

def to_s
  flat_list? ? 'nil' : @token.text.to_s
end

#token_rangeObject


544
545
546
547
# File 'lib/antlr3/tree.rb', line 544

def token_range
  unknown_boundaries? and infer_boundaries
  @start_index .. @stop_index
end

#typeObject


503
504
505
# File 'lib/antlr3/tree.rb', line 503

def type
  @token ? @token.type : 0
end

#unknown_boundaries?Boolean

Returns:

  • (Boolean)

572
573
574
# File 'lib/antlr3/tree.rb', line 572

def unknown_boundaries?
  @start_index < 0 or @stop_index < 0
end