class YARP::NodeInspector

This object is responsible for generating the output for the inspect method implementations of child nodes.

Attributes

output[R]
prefix[R]

Public Class Methods

new(prefix = "") click to toggle source
# File yarp.rb, line 319
def initialize(prefix = "")
  @prefix = prefix
  @output = +""
end

Public Instance Methods

<<(line) click to toggle source

Appends a line to the output with the current prefix.

# File yarp.rb, line 325
def <<(line)
  output << "#{prefix}#{line}"
end
child_inspector(append) click to toggle source

Returns a new inspector that can be used to inspect a child node.

# File yarp.rb, line 369
def child_inspector(append)
  NodeInspector.new("#{prefix}#{append}")
end
child_node(node, append) click to toggle source

Generates a string that represents a child node.

# File yarp.rb, line 364
def child_node(node, append)
  node.inspect(child_inspector(append)).delete_prefix(prefix)
end
header(node) click to toggle source

This generates a string that is used as the header of the inspect output for any given node.

# File yarp.rb, line 331
def header(node)
  output = +"@ #{node.class.name.split("::").last} ("
  output << "location: (#{node.location.start_offset}...#{node.location.end_offset})"
  output << ", newline: true" if node.newline?
  output << ")\n"
  output
end
list(prefix, nodes) click to toggle source

Generates a string that represents a list of nodes. It handles properly using the box drawing characters to make the output look nice.

# File yarp.rb, line 341
def list(prefix, nodes)
  output = +"(length: #{nodes.length})\n"
  last_index = nodes.length - 1

  nodes.each_with_index do |node, index|
    pointer, preadd = (index == last_index) ? ["└── ", "    "] : ["├── ", "│   "]
    node_prefix = "#{prefix}#{preadd}"
    output << node.inspect(NodeInspector.new(node_prefix)).sub(node_prefix, "#{prefix}#{pointer}")
  end

  output
end
location(value) click to toggle source

Generates a string that represents a location field on a node.

# File yarp.rb, line 355
def location(value)
  if value
    "(#{value.start_offset}...#{value.end_offset}) = #{value.slice.inspect}"
  else
    "∅"
  end
end
to_str() click to toggle source

Returns the output as a string.

# File yarp.rb, line 374
def to_str
  output
end