module Astarisk

Constants

BAR_SPACE
BAR_TOKEN
JUST_SPACE
NODE_TOKEN
VERSION

Public Class Methods

draw(node, out: STDERR, mode: :normal) click to toggle source
# File lib/astarisk.rb, line 12
def self.draw(node, out: STDERR, mode: :normal)
  out.print draw_graph(node, mode: mode)
  nil
end
draw_graph(node, mode: :normal) click to toggle source
# File lib/astarisk.rb, line 33
def self.draw_graph(node, mode: :normal)
  if !node.is_a?(RubyVM::AbstractSyntaxTree::Node)
    return stringify_node(node)
  end
  if node.children.empty?
    return stringify_node(node)
  end

  buffer = String.new(encoding: Encoding::UTF_8)
  buffer << stringify_node(node)

  num_children = node.children.size
  num_children.times do |i|
    terminal = (num_children == i + 1)
    child_graph_lines = draw_graph(node.children[i], mode: mode).lines
    buffer << BAR_TOKEN unless mode == :compact
    buffer << NODE_TOKEN + child_graph_lines.shift
    if terminal
      buffer << child_graph_lines.map{|line| JUST_SPACE + line }.join
    else
      buffer << child_graph_lines.map{|line| BAR_SPACE + line }.join
    end
  end

  buffer
end
stringify_node(node) click to toggle source
# File lib/astarisk.rb, line 17
def self.stringify_node(node)
  case node
  when RubyVM::AbstractSyntaxTree::Node
    "[#{node.type}]\n"
  when Array
    node.to_s + "\n"
  else
    "#{node.class.to_s}(#{node.inspect})\n"
  end
end