class AST2Dot

Constants

VERSION

Attributes

ast[R]
graph_edges[R]
node_labels[R]

Public Class Methods

new(code) click to toggle source
# File lib/ast2dot.rb, line 12
def initialize(code)
  @ast = RubyVM::AbstractSyntaxTree.parse(code)
  @template = File.read(File.expand_path('templates/default.dot.erb', __dir__))
end
render(code) click to toggle source
# File lib/ast2dot.rb, line 6
def self.render(code)
  new(code).render
end

Public Instance Methods

export() click to toggle source
# File lib/ast2dot.rb, line 17
def export
end
render() click to toggle source
# File lib/ast2dot.rb, line 20
def render
  @graph_edges = []
  @node_labels = Hash.new

  node_labelling(ast)

  graph_data = ERB.new(@template, nil, '-').result(binding)
  graph_data.lines.map(&:rstrip).join("\n")
end

Private Instance Methods

node_labelling(parent, index = 1) click to toggle source
# File lib/ast2dot.rb, line 32
def node_labelling(parent, index = 1)
  children_idx = parent.children.map do |child|
    next unless child.instance_of?(RubyVM::AbstractSyntaxTree::Node)

    index = node_labelling(child, index)
    index - 1
  end.compact

  children_idx.each do |idx|
    @graph_edges << { from: index, to: idx }
  end

  @node_labels[index] = parent.type

  index + 1
end