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.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Util::Node

#==, [], #attrs, #can_equal?, #child, #children, #each, #empty?, #initialize, #inspect, #name, #pretty_print, #pretty_print_cycle, #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.



66
67
68
# 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

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:



16
17
18
# File 'lib/rattler/runtime/parse_node.rb', line 16

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



41
42
43
44
45
46
47
# 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) ⇒ 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



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

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



51
52
53
# File 'lib/rattler/runtime/parse_node.rb', line 51

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

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


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

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