astarisk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 89c3ad862d9e7f790afd1b44a6d596f6fd1a8e26fe112513af7cff6babffdc3c
4
+ data.tar.gz: f63aa540c158fc02fdec3e4000afa68a6b5b2b319cc658ef59c0ace2a8d40733
5
+ SHA512:
6
+ metadata.gz: d53e9fcd2d09bbd110807fe414940b9e84512107576336b08cb5cc340c37328ccd4133c2ac78ee5c6104cf87414a855c8264492c43180992c68bf3a222f9541f
7
+ data.tar.gz: 0eac090ddb93559896f5df6bd70ef18d90102ee11a8c135d02522373c0e3d34db22afc20f056cc772233359fc528b9dde49d89ea3193e6808c54cc2ad6f667fc
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *~
10
+ Gemfile.lock
11
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in astarisk.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 TAGOMORI Satoshi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,95 @@
1
+ # Astarisk
2
+
3
+ Astarisk is a tool to visualize `RubyVM::AbstractSyntaxTree` nodes on console.
4
+
5
+ That is named as "AST a risk".
6
+
7
+ 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.
8
+
9
+ ## Usage
10
+
11
+ **NOTE: Use Ruby 2.6.0 preview3 or later**
12
+
13
+ 1. Get an AST using methods blow:
14
+ * `RubyVM::AbstractSyntaxTree#parse` (from ruby code)
15
+ * `RubyVM::AbstractSyntaxTree#parse_file` (from file path)
16
+ * `RubyVM::AbstractSyntaxTree#of` (from an object of Method or Proc)
17
+ 2. Get a graph string
18
+ * `Astarisk.draw_graph(ast)`
19
+ 3. Or draw graph on STDERR (in default)
20
+ * `Astarisk.draw(ast)`
21
+
22
+ Output example from a blank Proc object:
23
+
24
+ ```
25
+ [NODE_SCOPE]
26
+ |
27
+ +--[]
28
+ |
29
+ +--[NODE_ARGS]
30
+ | |
31
+ | +--Integer(0)
32
+ | |
33
+ | +--NilClass(nil)
34
+ | |
35
+ | +--NilClass(nil)
36
+ | |
37
+ | +--NilClass(nil)
38
+ | |
39
+ | +--Integer(0)
40
+ | |
41
+ | +--NilClass(nil)
42
+ | |
43
+ | +--NilClass(nil)
44
+ | |
45
+ | +--NilClass(nil)
46
+ | |
47
+ | +--NilClass(nil)
48
+ | |
49
+ | +--NilClass(nil)
50
+ |
51
+ +--[NODE_BEGIN]
52
+ |
53
+ +--NilClass(nil)
54
+ ```
55
+
56
+ Output example in compact format:
57
+
58
+ ```
59
+ [NODE_SCOPE]
60
+ +--[]
61
+ +--[NODE_ARGS]
62
+ | +--Integer(0)
63
+ | +--NilClass(nil)
64
+ | +--NilClass(nil)
65
+ | +--NilClass(nil)
66
+ | +--Integer(0)
67
+ | +--NilClass(nil)
68
+ | +--NilClass(nil)
69
+ | +--NilClass(nil)
70
+ | +--NilClass(nil)
71
+ | +--NilClass(nil)
72
+ +--[NODE_BEGIN]
73
+ +--NilClass(nil)
74
+ ```
75
+
76
+ ## Methods
77
+
78
+ ### Astarisk.draw
79
+
80
+ Draw a graph on specified I/O destination (default: STDERR)
81
+
82
+ Arguments:
83
+ * AST node (an instance of `RubyVM::AbstractSyntaxTree::Node`)
84
+
85
+ Keyword arguments:
86
+ * out: output I/O to draw graph (default: `STDERR`)
87
+ * mode: symbol to specify `:compact` mode (default: `:normal`)
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tagomoris/astarisk.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.test_files = FileList['test/**/test_*.rb']
7
+ test.warning = true
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,25 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "astarisk/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "astarisk"
8
+ spec.version = Astarisk::VERSION
9
+ spec.authors = ["TAGOMORI Satoshi"]
10
+ spec.email = ["tagomoris@gmail.com"]
11
+
12
+ spec.summary = %q{AST visualizer}
13
+ spec.description = %q{Draw a graph of node tree of RubyVM::AbstractSyntaxTree}
14
+ spec.homepage = "https://github.com/tagomoris/astarisk"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "test-unit"
25
+ end
@@ -0,0 +1,58 @@
1
+ require "astarisk/version"
2
+
3
+ if !RubyVM.const_defined?("AbstractSyntaxTree") && !RubyVM.const_defined?("AST")
4
+ raise "use Ruby 2.6.0-preview3 or later"
5
+ end
6
+
7
+ if !RubyVM.const_defined?("AbstractSyntaxTree") && RubyVM.const_defined?("AST")
8
+ RubyVM.const_set("AbstractSyntaxTree", RubyVM::AST)
9
+ end
10
+
11
+ module Astarisk
12
+ def self.draw(node, out: STDERR, mode: :normal)
13
+ STDERR.print draw_graph(node, mode: mode)
14
+ end
15
+
16
+ def self.stringify_node(node)
17
+ case node
18
+ when RubyVM::AbstractSyntaxTree::Node
19
+ "[#{node.type}]\n"
20
+ when Array
21
+ node.to_s + "\n"
22
+ else
23
+ "#{node.class.to_s}(#{node.inspect})\n"
24
+ end
25
+ end
26
+
27
+ BAR_TOKEN = " |\n".freeze
28
+ NODE_TOKEN = " +--".freeze
29
+ BAR_SPACE = " | ".freeze
30
+ JUST_SPACE = " ".freeze
31
+
32
+ def self.draw_graph(node, mode: :normal)
33
+ if !node.is_a?(RubyVM::AbstractSyntaxTree::Node)
34
+ return stringify_node(node)
35
+ end
36
+ if node.children.empty?
37
+ return stringify_node(node)
38
+ end
39
+
40
+ buffer = String.new(encoding: Encoding::UTF_8)
41
+ buffer << stringify_node(node)
42
+
43
+ num_children = node.children.size
44
+ num_children.times do |i|
45
+ terminal = (num_children == i + 1)
46
+ child_graph_lines = draw_graph(node.children[i], mode: mode).lines
47
+ buffer << BAR_TOKEN unless mode == :compact
48
+ buffer << NODE_TOKEN + child_graph_lines.shift
49
+ if terminal
50
+ buffer << child_graph_lines.map{|line| JUST_SPACE + line }.join
51
+ else
52
+ buffer << child_graph_lines.map{|line| BAR_SPACE + line }.join
53
+ end
54
+ end
55
+
56
+ buffer
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Astarisk
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: astarisk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TAGOMORI Satoshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Draw a graph of node tree of RubyVM::AbstractSyntaxTree
56
+ email:
57
+ - tagomoris@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - astarisk.gemspec
68
+ - lib/astarisk.rb
69
+ - lib/astarisk/version.rb
70
+ homepage: https://github.com/tagomoris/astarisk
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.7.6
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: AST visualizer
94
+ test_files: []