class Peggy::Node

A node in an Abstract Syntax Tree. Every node in the tree maps to a production found in the parse. You can navigate to the node’s parent, first child, or next sibling. Nodes know their range of the source text.

Attributes

_first[RW]
_name[RW]
_next[RW]
_parent[RW]
_range[RW]
_source[RW]

Public Class Methods

new(name) click to toggle source

Constructor

# File lib/parse/ast.rb, line 21
def initialize name
  self._name = name
end

Public Instance Methods

<<(child) click to toggle source

Add a child.

# File lib/parse/ast.rb, line 26
    def << child
      child._parent = self
#puts "#{_name}[first #{_first} last #{_last}] << child #{child._name}"
      if _first
        _last._next = child
      else
        self._first = child
      end
    end
[](name) click to toggle source

Get the first node of the given name as a Symbol.

# File lib/parse/ast.rb, line 147
def [] name
  method_missing name.to_sym
end
_count(name=nil) click to toggle source

Count the children. If name is supplied only nodes matching the name are counted.

# File lib/parse/ast.rb, line 54
def _count name=nil
  c = 0
  each do |node|
    c += 1 if name.nil? || name == node._name
  end
  c
end
_depth() click to toggle source

Get the number of nodes up to the root.

# File lib/parse/ast.rb, line 63
def _depth
  depth = 0
  node = self
  depth += 1 while node=node._parent
  depth
end
_format() click to toggle source

Format the node pretty printing.

# File lib/parse/ast.rb, line 99
def _format
  result = "#{'  '*_depth}#{_name} #{_sample}\n"
  each do |node|
    result << node._format
  end
  result
end
_last() click to toggle source

Get the last child.

# File lib/parse/ast.rb, line 108
def _last
  node = _first
  return nil unless node
  while (n2 = node._next)
    node = n2
  end
  node
end
_length() click to toggle source

Get the length of the range.

# File lib/parse/ast.rb, line 87
def _length
  _range.last - _range.first
end
_option(option, default=nil) click to toggle source

Get an option set when tree was created.

# File lib/parse/ast.rb, line 80
def _option option, default=nil
  options = _root._options
  return nil unless options
  options[option] || options[option.to_sym] || default
end
_root() click to toggle source

Get the root node.

# File lib/parse/ast.rb, line 71
def _root
  node = self
  while (n2 = node._parent)
    node = n2
  end
  node
end
_sample() click to toggle source

Get some or all of the source text covered by this node, depending on the length.

# File lib/parse/ast.rb, line 92
def _sample
  return nil if _length == 0
  str = _source[_range]
  (str.length >= 40 ? str[0, 37] + '...' : str).inspect
end
_strip() click to toggle source

Get the source text minus any ignored nodes.

# File lib/parse/ast.rb, line 123
    def _strip
      return @str if @str
      str0 = str = _source[_range]
      return @str = str unless (ignore = _option :ignore) && _first
      remove = find_all{|node| node._name == ignore}
      remove.reverse_each do |node|
        from = node._range.first - _range.first
        str = str[0, from] + str[from + node._length..-1]
      end
# puts "before #{str0.inspect}, after #{str.inspect}" unless remove.empty?
      @str = str
    end
children(name=nil) click to toggle source
# File lib/parse/ast.rb, line 45
def children name=nil
  a = []
  each(name) do |node|
    a << node
  end
  a
end
each(name=nil) { |child| ... } click to toggle source

Iterate over each child. If name is supplied only nodes matching the name are iterated.

# File lib/parse/ast.rb, line 37
def each name=nil
  child = _first
  while child
    yield child if name.nil? || name == child._name
    child = child._next
  end
end
inspect() click to toggle source

Get the contents for inspection.

# File lib/parse/ast.rb, line 118
def inspect
  "#{_name ? _name : self.class}[#{_range}] #{to_s.inspect}"
end
method_missing(name, *args) click to toggle source
# File lib/parse/ast.rb, line 151
def method_missing name, *args
  find {|node| name == node._name}
end
to_s() click to toggle source

Get the source text covered by this node.

# File lib/parse/ast.rb, line 137
def to_s
  _source[_range]
end
to_sym() click to toggle source

Get the stripped text as a Symbol.

# File lib/parse/ast.rb, line 142
def to_sym
  _strip.to_sym
end