class Rattler::Runtime::ParseNode

ParseNode is intended as s a convenient class to use as a parsing result type.

Public Class Methods

parsed(children, attrs={}) click to toggle source

Create a parse node from the results of a parsing expression.

@param [Array] children the children of the parse node @param [Hash] attrs any attributes for the parse node

@return [ParseNode] a new parse node

# File lib/rattler/runtime/parse_node.rb, line 16
def self.parsed(children, attrs={})
  self.new(children, attrs.reject {|_, val| val.nil? })
end

Public Instance Methods

[](*args) click to toggle source

Access the parse node’s children.

@overload [](index)

Return the node's child at +index+.
@param [Integer] index index of the child
@return the child at +index+

@overload [](start, length)

Return an array of the node's children starting at +start+ and
continuing for +length+ children.
@param [Integer] start the index of the first child
@param [Integer] length the number of children to return
@return [Array] the node's children starting at +start+ and continuing
  for +length+ children

@overload [](range)

Return an array of the node's children specified by +range+.
@param [Range] range the range of children
@return [Array] the node's children specified by +range+

@overload [](label)

Return the labeled child associated with +label+.
@param [Symbol] a label
@return the labeled child associated with +label+
Calls superclass method Rattler::Util::Node::[]
# File lib/rattler/runtime/parse_node.rb, line 41
def [](*args)
  if args.size == 1 and args.first.is_a?(Symbol)
    labeled[args[0]]
  else
    super
  end
end
eql?(other) click to toggle source

Return true if the node has the same value as other, i.e. other is an instance of the same class and has equal children and attributes and the children are labeled the same.

@return [Boolean] true the node has the same value as other

Calls superclass method Rattler::Util::Node#eql?
# File lib/rattler/runtime/parse_node.rb, line 60
def eql?(other) #:nodoc
  super &&
  self.labeled == other.labeled
end
labeled() click to toggle source

Return a hash associating labels with the labeled children @return [Hash] a hash associating labels with the labeled children

# File lib/rattler/runtime/parse_node.rb, line 51
def labeled
  attrs.fetch(:labeled, {})
end
method_missing(symbol, *args) click to toggle source

Allow labeled children to be accessed as methods.

Calls superclass method Rattler::Util::Node#method_missing
# File lib/rattler/runtime/parse_node.rb, line 66
def method_missing(symbol, *args)
  (args.empty? and labeled.has_key?(symbol)) ? labeled[symbol] : super
end