Project

astarisk

0.0
No commit activity in last 3 years
No release in over 3 years
Draw a graph of node tree of RubyVM::AbstractSyntaxTree
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.16
~> 10.0
 Project Readme

Astarisk

Astarisk is a tool to visualize RubyVM::AbstractSyntaxTree nodes on console.

That is named as "AST a risk".

NOTE: RubyVM::AbstractSyntaxTree is defined as RubyVM::AST in Ruby 2.6.0 preview3, but it'll be renamed to RubyVM::AbstractSyntaxTree in later versions.

Usage

NOTE: Use Ruby 2.6.0 preview3 or later

  1. Get an AST using methods blow:
  • RubyVM::AbstractSyntaxTree#parse (from ruby code)
  • RubyVM::AbstractSyntaxTree#parse_file (from file path)
  • RubyVM::AbstractSyntaxTree#of (from an object of Method or Proc)
  1. Get a graph string
  • Astarisk.draw_graph(ast)
  1. Or draw graph on STDERR (in default)
  • Astarisk.draw(ast)

Output example from a blank Proc object:

[NODE_SCOPE]
 |
 +--[]
 |
 +--[NODE_ARGS]
 |   |
 |   +--Integer(0)
 |   |
 |   +--NilClass(nil)
 |   |
 |   +--NilClass(nil)
 |   |
 |   +--NilClass(nil)
 |   |
 |   +--Integer(0)
 |   |
 |   +--NilClass(nil)
 |   |
 |   +--NilClass(nil)
 |   |
 |   +--NilClass(nil)
 |   |
 |   +--NilClass(nil)
 |   |
 |   +--NilClass(nil)
 |
 +--[NODE_BEGIN]
     |
     +--NilClass(nil)

Output example in compact format:

[NODE_SCOPE]
 +--[]
 +--[NODE_ARGS]
 |   +--Integer(0)
 |   +--NilClass(nil)
 |   +--NilClass(nil)
 |   +--NilClass(nil)
 |   +--Integer(0)
 |   +--NilClass(nil)
 |   +--NilClass(nil)
 |   +--NilClass(nil)
 |   +--NilClass(nil)
 |   +--NilClass(nil)
 +--[NODE_BEGIN]
     +--NilClass(nil)

Methods

Astarisk.draw

Draw a graph on specified I/O destination (default: STDERR)

Arguments:

  • AST node (an instance of RubyVM::AbstractSyntaxTree::Node)

Keyword arguments:

  • out: output I/O to draw graph (default: STDERR)
  • mode: symbol to specify :compact mode (default: :normal)

Return value: nil

Astarisk.draw_graph

Draw a graph and return it as a String

Argument:

  • AST node (an instance of RubyVM::AbstractSyntaxTree::Node)

Keyword arguments:

  • mode: symbol to specify :compact mode (default: :normal)

Return value: a String of graph

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tagomoris/astarisk.

License

The gem is available as open source under the terms of the MIT License.