class Parlour::TypeParser::NodePath
Represents a path of indices which can be traversed to reach a specific node in an AST.
Attributes
indices[R]
@return [Array<Integer>] The path of indices.
Public Class Methods
new(indices)
click to toggle source
Creates a new {NodePath}.
@param [Array<Integer>] indices The path of indices.
# File lib/parlour/type_parser.rb, line 31 def initialize(indices) @indices = indices end
Public Instance Methods
child(index)
click to toggle source
@param [Integer] index The index of the child whose path to return. @return [NodePath] The path to the child at the given index.
# File lib/parlour/type_parser.rb, line 48 def child(index) NodePath.new(indices + [index]) end
parent()
click to toggle source
@return [NodePath] The parent path for the node at this path.
# File lib/parlour/type_parser.rb, line 37 def parent if indices.empty? raise IndexError, 'cannot get parent of an empty path' else NodePath.new(T.must(indices[0...-1])) end end
sibling(offset)
click to toggle source
@param [Integer] offset The sibling offset to use. 0 is the current
node, -1 is the previous node, or 3 is is the node three nodes after this one.
@return [NodePath] The path to the sibling with the given context.
# File lib/parlour/type_parser.rb, line 57 def sibling(offset) if indices.empty? raise IndexError, 'cannot get sibling of an empty path' else *xs, x = indices x = T.must(x) raise ArgumentError, "sibling offset of #{offset} results in " \ "negative index of #{x + offset}" if x + offset < 0 NodePath.new(T.must(xs) + [x + offset]) end end
traverse(start)
click to toggle source
Follows this path of indices from an AST node.
@param [Parser::AST::Node] start The AST node to start from. @return [Parser::AST::Node] The resulting AST node.
# File lib/parlour/type_parser.rb, line 74 def traverse(start) current = T.unsafe(start) indices.each do |index| raise IndexError, 'path does not exist' if index >= current.to_a.length current = current.to_a[index] end current end