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
Public Class Methods
Constructor
# File lib/parse/ast.rb, line 21 def initialize name self._name = name end
Public Instance Methods
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
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 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
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 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
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
Get the length of the range.
# File lib/parse/ast.rb, line 87 def _length _range.last - _range.first end
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
Get the root node.
# File lib/parse/ast.rb, line 71 def _root node = self while (n2 = node._parent) node = n2 end node end
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
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
# File lib/parse/ast.rb, line 45 def children name=nil a = [] each(name) do |node| a << node end a end
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
Get the contents for inspection.
# File lib/parse/ast.rb, line 118 def inspect "#{_name ? _name : self.class}[#{_range}] #{to_s.inspect}" end
# File lib/parse/ast.rb, line 151 def method_missing name, *args find {|node| name == node._name} end
Get the source text covered by this node.
# File lib/parse/ast.rb, line 137 def to_s _source[_range] end
Get the stripped text as a Symbol.
# File lib/parse/ast.rb, line 142 def to_sym _strip.to_sym end