Class: Rattler::Runtime::ParseNode

Inherits:
Util::Node show all
Defined in:
lib/rattler/runtime/parse_node.rb

Overview

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

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Util::Node

#==, [], #attrs, #can_equal?, #child, #children, #each, #empty?, #initialize, #inspect, #name, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

This class inherits a constructor from Rattler::Util::Node

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Allow labeled children to be accessed as methods.



76
77
78
# File 'lib/rattler/runtime/parse_node.rb', line 76

def method_missing(symbol, *args)
  (args.empty? and labeled.has_key?(symbol)) ? labeled[symbol] : super
end

Class Method Details

.parsed(children, attrs = {}) ⇒ ParseNode

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

Parameters:

  • children (Array)

    the children of the parse node

  • attrs (Hash) (defaults to: {})

    any attributes for the parse node

Returns:



26
27
28
# File 'lib/rattler/runtime/parse_node.rb', line 26

def self.parsed(children, attrs={})
  self.new(children, attrs.reject {|_, val| val.nil? })
end

Instance Method Details

#[](index) ⇒ Object #[](start, length) ⇒ Array #[](range) ⇒ Array #[](label) ⇒ Object

Access the parse node’s children.

Overloads:

  • #[](index) ⇒ Object

    Return the node’s child at index.

    Parameters:

    • index (Integer)

      index of the child

    Returns:

    • the child at index

  • #[](start, length) ⇒ Array

    Return an array of the node’s children starting at start and continuing for length children.

    Parameters:

    • start (Integer)

      the index of the first child

    • length (Integer)

      the number of children to return

    Returns:

    • (Array)

      the node’s children starting at start and continuing for length children

  • #[](range) ⇒ Array

    Return an array of the node’s children specified by range.

    Parameters:

    • range (Range)

      the range of children

    Returns:

    • (Array)

      the node’s children specified by range

  • #[](label) ⇒ Object

    Return the labeled child associated with label.

    Parameters:

    • a (Symbol)

      label

    Returns:

    • the labeled child associated with label



51
52
53
54
55
56
57
# File 'lib/rattler/runtime/parse_node.rb', line 51

def [](*args)
  if args.size == 1 and args.first.is_a?(Symbol)
    labeled[args[0]]
  else
    super
  end
end

#eql?(other) ⇒ Boolean

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.

Returns:

  • (Boolean)

    true the node has the same value as other



70
71
72
73
# File 'lib/rattler/runtime/parse_node.rb', line 70

def eql?(other) #:nodoc
  super &&
  self.labeled == other.labeled
end

#labeledHash

Return a hash associating labels with the labeled children

Returns:

  • (Hash)

    a hash associating labels with the labeled children



61
62
63
# File 'lib/rattler/runtime/parse_node.rb', line 61

def labeled
  attrs.fetch(:labeled, {})
end

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/rattler/runtime/parse_node.rb', line 81

def respond_to?(symbol) #:nodoc:
  super || labeled.has_key?(symbol)
end