ripper_tree 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89cb766c8c2dd322da787fe17f6d3025ab232f92
4
+ data.tar.gz: 97c61dc8df0ed9ab31d8c1b175b29acdaba962ad
5
+ SHA512:
6
+ metadata.gz: 175567907cd8a09722638a3a1a921d2d0b9081d08b79cf2bc8a165687b710a0208c4986e698ab1258d0c4860002835ee7baf45c83c17cb139fe11f6f04f07613
7
+ data.tar.gz: 17860486ee33db836993e27365358754f01950d03ba2b7ff5a0a82e3abaf8fe1e38c4e1489d366e24b1d6c5cb3b0011298c8692cdda21677129015653b2fe1d4
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /vendor/bundle/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ # RubyMine
16
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --require spec_helper
3
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3.3
7
+ - 2.4.0
8
+ before_install: gem install bundler -v 1.14.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at k128585@ie.u-ryukyu.ac.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ripper_tree.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 siman-man
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.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # RipperTree
2
+
3
+ [![Build Status](https://travis-ci.org/siman-man/ripper_tree.svg?branch=master)](https://travis-ci.org/siman-man/ripper_tree)
4
+
5
+ RipperTree is like `tree` command for Ripper#sexp.
6
+
7
+ ## Installation
8
+
9
+ ```
10
+ $ gem install ripper_tree
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ You can use `rtree` command.
16
+
17
+ sample.rb
18
+
19
+ ```ruby
20
+ 1 + 1
21
+ ```
22
+
23
+ exec `rtree` command.
24
+
25
+ ```
26
+ $ rtree sample.rb
27
+ ```
28
+
29
+ result
30
+
31
+ ```ruby
32
+ :program
33
+ └──── :binary
34
+ ├──── :@int [1] 1:0
35
+ ├──── :+
36
+ └──── :@int [1] 1:4
37
+ ```
38
+
39
+ if use in ruby code.
40
+
41
+ ```rb
42
+ require 'ripper_tree'
43
+ require 'pp'
44
+
45
+ code =<<-CODE
46
+ puts "hello world"
47
+ CODE
48
+
49
+ pp Ripper.sexp(code)
50
+ puts
51
+ puts RipperTree.create(code)
52
+ ```
53
+
54
+ result
55
+
56
+ ```ruby
57
+ [:program,
58
+ [[:command,
59
+ [:@ident, "puts", [1, 0]],
60
+ [:args_add_block,
61
+ [[:string_literal,
62
+ [:string_content, [:@tstring_content, "hello world", [1, 6]]]]],
63
+ false]]]]
64
+
65
+ :program
66
+ └──── :command
67
+ ├──── :@ident [puts] 1:0
68
+ └──── :args_add_block
69
+ ├──── :string_literal
70
+ │ └──── :string_content
71
+ │ └──── :@tstring_content ["hello world"] 1:6
72
+ └──── false
73
+ ```
74
+
75
+ and can use `-e "command"` option
76
+
77
+ ```
78
+ $ rtree -e "1+2*3"
79
+ ```
80
+
81
+ result
82
+
83
+ ```ruby
84
+ :program
85
+ └──── :binary
86
+ ├──── :@int [1] 1:0
87
+ ├──── :+
88
+ └──── :binary
89
+ ├──── :@int [2] 1:2
90
+ ├──── :*
91
+ └──── :@int [3] 1:4
92
+ ```
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/siman-man/ripper_tree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
97
+
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ripper_tree"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/rtree ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ripper_tree'
4
+ require 'optparse'
5
+
6
+ options = { color: true, space_size: 8 }
7
+
8
+ OptionParser.new do |opt|
9
+ opt.banner = 'Usage: rtree [options] programfile'
10
+ opt.on('-e `command`', 'one line of script.') { |code| options[:code] = code }
11
+ opt.on('-s `space size`', 'set space size [default size is 8].') { |size| options[:space_size] = size.to_i }
12
+ opt.on('-v', 'display the version.') { options[:version] = true }
13
+ opt.on('--no-color', 'disable color option') { options[:color] = false }
14
+
15
+ opt.parse!(ARGV)
16
+ end
17
+
18
+ RipperTree::OPTIONS.merge!(options)
19
+
20
+ case
21
+ when options[:code]
22
+ puts RipperTree.create(options[:code])
23
+ when options[:version]
24
+ puts "RipperTree version: #{RipperTree::VERSION}"
25
+ else
26
+ puts RipperTree.create(File.read(ARGV.first, encoding: 'utf-8'))
27
+ end
@@ -0,0 +1,3 @@
1
+ class RipperTree
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,183 @@
1
+ require 'ripper'
2
+ require 'colorize'
3
+
4
+ class RipperTree
5
+ OPTIONS = { space_size: 8, color: true }
6
+ T_LINE = '├────'
7
+ I_LINE = '│'
8
+ L_LINE = '└────'
9
+ VALUE_TOKEN = /@CHAR|@ident|@kw|@regexp_end|@period|@op|@int|@float|@rational|@label|@ivar|@imaginary|@const|@cvar|@gvar/.freeze
10
+
11
+ def self.create(src)
12
+ new.tap do |rtree|
13
+ rtree.parse(Ripper.sexp(src))
14
+ end
15
+ end
16
+
17
+ def initialize
18
+ @queue = []
19
+ end
20
+
21
+ def to_s
22
+ RipperTree::OPTIONS[:color] ? @queue.join : @queue.join.uncolorize
23
+ end
24
+
25
+ def get_line(end_line: nil, space: '')
26
+ end_line ? "#{space}#{L_LINE} " : "#{space}#{T_LINE} "
27
+ end
28
+
29
+ def get_space(end_line: nil)
30
+ end_line ? ' ' * RipperTree::OPTIONS[:space_size] : I_LINE + ' ' * (RipperTree::OPTIONS[:space_size]-1)
31
+ end
32
+
33
+ def output_id(id)
34
+ "#{id.inspect}\n".colorize(:magenta)
35
+ end
36
+
37
+ def output_string_node(node)
38
+ "#{node[0].inspect.colorize(:blue)} [#{node[1].inspect.colorize(:red)}] #{node[2].join(':')}\n"
39
+ end
40
+
41
+ def output_value_node(node)
42
+ "#{node[0].inspect.colorize(:blue)} [#{node[1]}] #{node[2].join(':')}\n"
43
+ end
44
+
45
+ def parse_method_arguments(parent, space: ' ')
46
+ id = parent.first
47
+
48
+ @queue << output_id(id)
49
+
50
+ params = parent[1..-1].zip(%i(req_param opt_param rest_param key_param keyreq_param keyrest_param block_param))
51
+
52
+ until params.empty?
53
+ param, arg_type = params.shift
54
+
55
+ @queue << get_line(end_line: params.empty?, space: space)
56
+
57
+ if param.instance_of?(Array)
58
+ @queue << output_id(arg_type)
59
+
60
+ if arg_type == :keyrest_param
61
+ s = space + get_space(end_line: params.empty? && param.empty?)
62
+ @queue << "#{s}#{L_LINE} "
63
+ @queue << output_value_node(param)
64
+ next
65
+ end
66
+
67
+ arg_count = 0
68
+
69
+ until param.empty?
70
+ e = param.shift
71
+ arg_count += 1
72
+ next if e == :rest_param
73
+ next if e == :blockarg
74
+ next if e.nil?
75
+
76
+ s = space + get_space(end_line: params.empty? && param.empty?)
77
+ @queue << get_line(end_line: param.empty?, space: s)
78
+
79
+ case arg_type
80
+ when :opt_param
81
+ ident = e.first
82
+ val = e.last
83
+ @queue << "arg#{arg_count}\n"
84
+
85
+ ss = s + get_space(end_line: param.empty?)
86
+ @queue << get_line(end_line: false, space: ss)
87
+ @queue << output_value_node(ident)
88
+ @queue << get_line(end_line: true, space: ss)
89
+
90
+ case val.first
91
+ when /var_ref/
92
+ v = val.last
93
+ @queue << output_value_node(v)
94
+ when /symbol_literal|string_literal|array|hash|call|paren/
95
+ sss = ss + get_space(end_line: param.empty?)
96
+ parse(val, space: sss)
97
+ else
98
+ @queue << output_value_node(val)
99
+ end
100
+ else
101
+ parse(e, space: space + s)
102
+ end
103
+ end
104
+ else
105
+ if param =~ /\d+/
106
+ @queue << ":keyrest\n"
107
+ else
108
+ @queue << "#{param.inspect}\n"
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ def parse(parent, space: ' ')
115
+ if parent.first.instance_of?(Array)
116
+ parse(parent.first, space: space)
117
+ return
118
+ end
119
+
120
+ id = parent.first
121
+
122
+ case id
123
+ when /@tstring_content/
124
+ @queue << output_string_node(parent)
125
+ return
126
+ when VALUE_TOKEN
127
+ @queue << output_value_node(parent)
128
+ return
129
+ when /array/
130
+ @queue << output_id(id)
131
+ children = parent[1].nil? ? [] : parent[1]
132
+
133
+ until children.empty?
134
+ child = children.shift
135
+ next if child == :args_add_star
136
+ next if child == []
137
+
138
+ @queue << get_line(end_line: children.empty?, space: space)
139
+ parse(child, space: space + get_space(end_line: children.empty?))
140
+ end
141
+
142
+ return
143
+ when /params/
144
+ parse_method_arguments(parent, space: space)
145
+ return
146
+ when /void_stmt/
147
+ @queue << output_id(id)
148
+ return
149
+ else
150
+ @queue << output_id(id)
151
+ end
152
+
153
+ child_count = parent[1..-1].count { |e| e.instance_of?(Array) }
154
+
155
+ if child_count.zero?
156
+ unless parent[1..-1].all?(&:nil?)
157
+ @queue << "#{space}#{parent[1..-1].map(&:inspect).join(', ')}"
158
+ end
159
+ else
160
+ children = parent[1..-1]
161
+
162
+ until children.empty?
163
+ child = children.shift
164
+
165
+ if child.instance_of?(Array)
166
+ if child.all? { |e| e.instance_of?(Array) }
167
+ until child.empty?
168
+ node = child.shift
169
+ @queue << get_line(end_line: children.empty? && child.empty?, space: space)
170
+ parse(node, space: space + get_space(end_line: children.empty? && child.empty?))
171
+ end
172
+ else
173
+ @queue << get_line(end_line: children.empty?, space: space)
174
+ parse(child, space: space + get_space(end_line: children.empty?))
175
+ end
176
+ else
177
+ @queue << get_line(end_line: children.empty?, space: space)
178
+ @queue << "#{child.inspect}\n"
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'ripper_tree/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "ripper_tree"
9
+ spec.version = RipperTree::VERSION
10
+ spec.authors = ["siman-man"]
11
+ spec.email = ["k128585@ie.u-ryukyu.ac.jp"]
12
+
13
+ spec.summary = %q{tree command like viewer}
14
+ spec.description = %q{tree command like viewer}
15
+ spec.homepage = "https://github.com/siman-man/ripper_tree"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = '>= 2.1.0'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency 'colorize'
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.14"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ripper_tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - siman-man
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: tree command like viewer
70
+ email:
71
+ - k128585@ie.u-ryukyu.ac.jp
72
+ executables:
73
+ - rtree
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - exe/rtree
88
+ - lib/ripper_tree.rb
89
+ - lib/ripper_tree/version.rb
90
+ - ripper_tree.gemspec
91
+ homepage: https://github.com/siman-man/ripper_tree
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.1.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.10
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: tree command like viewer
115
+ test_files: []