class Prism::Node

This represents a node in the tree. It is the parent class of all of the various node types.

Attributes

source[R]

A pointer to the source that this node was created from.

Public Instance Methods

end_offset() click to toggle source

The end offset of the node in the source. This method is effectively a delegate method to the location object.

# File lib/prism/node.rb, line 34
def end_offset
  location = @location
  location.is_a?(Location) ? location.end_offset : ((location >> 32) + (location & 0xFFFFFFFF))
end
location() click to toggle source

A Location instance that represents the location of this node in the source.

# File lib/prism/node.rb, line 19
def location
  location = @location
  return location if location.is_a?(Location)
  @location = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end
pretty_print(q) click to toggle source

Similar to inspect, but respects the current level of indentation given by the pretty print object.

# File lib/prism/node.rb, line 58
def pretty_print(q)
  q.seplist(inspect.chomp.each_line, -> { q.breakable }) do |line|
    q.text(line.chomp)
  end
  q.current_group.break
end
slice() click to toggle source

Slice the location of the node from the source.

# File lib/prism/node.rb, line 52
def slice
  location.slice
end
start_offset() click to toggle source

The start offset of the node in the source. This method is effectively a delegate method to the location object.

# File lib/prism/node.rb, line 27
def start_offset
  location = @location
  location.is_a?(Location) ? location.start_offset : location >> 32
end
to_dot() click to toggle source

Convert this node into a graphviz dot graph string.

# File lib/prism/node.rb, line 66
def to_dot
  # @type self: node
  DotVisitor.new.tap { |visitor| accept(visitor) }.to_dot
end

Node interface

↑ top

Public Instance Methods

accept(visitor) click to toggle source

Accepts a visitor and calls back into the specialized visit function.

# File lib/prism/node.rb, line 79
def accept(visitor)
  raise NoMethodError, "undefined method `accept' for #{inspect}"
end
child_nodes() click to toggle source

Returns an array of child nodes, including ‘nil`s in the place of optional nodes that were not present.

# File lib/prism/node.rb, line 85
def child_nodes
  raise NoMethodError, "undefined method `child_nodes' for #{inspect}"
end
Also aliased as: deconstruct
comment_targets() click to toggle source

Returns an array of child nodes and locations that could potentially have comments attached to them.

# File lib/prism/node.rb, line 99
def comment_targets
  raise NoMethodError, "undefined method `comment_targets' for #{inspect}"
end
compact_child_nodes() click to toggle source

Returns an array of child nodes, excluding any ‘nil`s in the place of optional nodes that were not present.

# File lib/prism/node.rb, line 93
def compact_child_nodes
  raise NoMethodError, "undefined method `compact_child_nodes' for #{inspect}"
end
deconstruct()
Alias for: child_nodes
inspect(inspector = NodeInspector.new) click to toggle source

Returns a string representation of the node.

# File lib/prism/node.rb, line 110
def inspect(inspector = NodeInspector.new)
  raise NoMethodError, "undefined method `inspect' for #{inspect}"
end
type() click to toggle source

Returns a symbol symbolizing the type of node that this represents. This is particularly useful for case statements and array comparisons.

# File lib/prism/node.rb, line 105
def type
  raise NoMethodError, "undefined method `type' for #{inspect}"
end