Class Astrolabe::Node
In: lib/astrolabe/node.rb
Parent: Parser::AST::Node

`Astrolabe::Node` is a subclass of `Parser::AST::Node`. It provides an access to parent node and an object-oriented way to handle AST with the power of `Enumerable`.

Though not described in the auto-generated API documentation, it has predicate methods for every node type. These methods would be useful especially when combined with `Enumerable` methods.

@example

  node.send_type?    # Equivalent to: `node.type == :send`
  node.op_asgn_type? # Equivalent to: `node.type == :op_asgn`

  # Non-word characters (other than a-zA-Z0-9_) in type names are omitted.
  node.defined_type? # Equivalent to: `node.type == :defined?`

  # Find the first lvar node under the receiver node.
  lvar_node = node.each_descendant.find(&:lvar_type?)

Methods

Public Class methods

Public Instance methods

Returns an array of ancestor nodes. This is a shorthand for `node.each_ancestor.to_a`.

@return [Array<Node>] an array of ancestor nodes

Returns an array of child nodes. This is a shorthand for `node.each_child_node.to_a`.

@return [Array<Node>] an array of child nodes

Returns an array of descendant nodes. This is a shorthand for `node.each_descendant.to_a`.

@return [Array<Node>] an array of descendant nodes

Calls the given block for each ancestor node in the order from parent to root. If no block is given, an `Enumerator` is returned.

@overload each_ancestor

  Yield all nodes.

@overload each_ancestor(type)

  Yield only nodes matching the type.
  @param [Symbol] type a node type

@overload each_ancestor(type_a, type_b, …)

  Yield only nodes matching any of the types.
  @param [Symbol] type_a a node type
  @param [Symbol] type_b a node type

@overload each_ancestor(types)

  Yield only nodes matching any of types in the array.
  @param [Array<Symbol>] types an array containing node types

@yieldparam [Node] node each ancestor node @return [self] if a block is given @return [Enumerator] if no block is given

Calls the given block for each child node. If no block is given, an `Enumerator` is returned.

Note that this is different from `node.children.each { |child| … }` which yields all children including non-node element.

@overload each_child_node

  Yield all nodes.

@overload each_child_node(type)

  Yield only nodes matching the type.
  @param [Symbol] type a node type

@overload each_child_node(type_a, type_b, …)

  Yield only nodes matching any of the types.
  @param [Symbol] type_a a node type
  @param [Symbol] type_b a node type

@overload each_child_node(types)

  Yield only nodes matching any of types in the array.
  @param [Array<Symbol>] types an array containing node types

@yieldparam [Node] node each child node @return [self] if a block is given @return [Enumerator] if no block is given

Calls the given block for each descendant node with depth first order. If no block is given, an `Enumerator` is returned.

@overload each_descendant

  Yield all nodes.

@overload each_descendant(type)

  Yield only nodes matching the type.
  @param [Symbol] type a node type

@overload each_descendant(type_a, type_b, …)

  Yield only nodes matching any of the types.
  @param [Symbol] type_a a node type
  @param [Symbol] type_b a node type

@overload each_descendant(types)

  Yield only nodes matching any of types in the array.
  @param [Array<Symbol>] types an array containing node types

@yieldparam [Node] node each descendant node @return [self] if a block is given @return [Enumerator] if no block is given

Calls the given block for the receiver and each descendant node with depth first order. If no block is given, an `Enumerator` is returned.

This method would be useful when you treat the receiver node as a root of tree and want to iterate all nodes in the tree.

@overload each_node

  Yield all nodes.

@overload each_node(type)

  Yield only nodes matching the type.
  @param [Symbol] type a node type

@overload each_node(type_a, type_b, …)

  Yield only nodes matching any of the types.
  @param [Symbol] type_a a node type
  @param [Symbol] type_b a node type

@overload each_node(types)

  Yield only nodes matching any of types in the array.
  @param [Array<Symbol>] types an array containing node types

@yieldparam [Node] node each node @return [self] if a block is given @return [Enumerator] if no block is given

Returns the parent node, or `nil` if the receiver is a root node.

@return [Node, nil] the parent node or `nil`

Returns whether the receiver is a root node or not.

@return [Boolean] whether the receiver is a root node or not

Returns the index of the receiver node in its siblings.

@return [Integer] the index of the receiver node in its siblings

Protected Instance methods

[Validate]