Module: Astarisk
- Defined in:
- lib/astarisk.rb,
lib/astarisk/version.rb
Constant Summary collapse
- BAR_TOKEN =
" |\n".freeze
- NODE_TOKEN =
" +--".freeze
- BAR_SPACE =
" | ".freeze
- JUST_SPACE =
" ".freeze
- VERSION =
"0.1.0"
Class Method Summary collapse
- .draw(node, out: STDERR, mode: :normal) ⇒ Object
- .draw_graph(node, mode: :normal) ⇒ Object
- .stringify_node(node) ⇒ Object
Class Method Details
.draw(node, out: STDERR, mode: :normal) ⇒ Object
12 13 14 |
# File 'lib/astarisk.rb', line 12 def self.draw(node, out: STDERR, mode: :normal) STDERR.print draw_graph(node, mode: mode) end |
.draw_graph(node, mode: :normal) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/astarisk.rb', line 32 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) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/astarisk.rb', line 16 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 |