proc_to_ast 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 91c5fe173fcebfbe9b300cae9ec8c670fe5faafe
4
+ data.tar.gz: 79c1ff86ee61c98ea86e6c5fcb09c651a461adb8
5
+ SHA512:
6
+ metadata.gz: 09568ba9f1b51ffde7d256d08f2e145a548379f38bba040ee40debb5a80a66a79ecaf7e60a0cc0358c3185d58da4ff6fdd7014c60ba193f9e9f06c750bb644e5
7
+ data.tar.gz: 4d077ea1bdb11c48a0e518d3252bc5a763e6306033c4f6cc22fe42102bb4fb66cfa7f3b0b246d0887b411292673e408f643486a5b77ccbe269a400f2624afe15
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.1.1
5
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in proc_to_ast.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 joker1007
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # proc_to_ast
2
+
3
+ Add `#to_ast` method to Proc.
4
+
5
+ `#to_ast` convert Proc to `Parser::AST::Node`, using [parser](https://github.com/whitequark/parser "whitequark/parser") gem.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'proc_to_ast'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install proc_to_ast
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'proc_to_ast'
25
+
26
+ foo = proc { p(1 + 1) }
27
+
28
+ foo.to_ast
29
+ # =>
30
+ # (block
31
+ # (send nil :proc)
32
+ # (args)
33
+ # (send nil :p
34
+ # (send
35
+ # (int 1) :+
36
+ # (int 1))))
37
+
38
+ foo.to_source
39
+ # => "proc do\n p(1 + 1)\nend"
40
+
41
+ foo.to_source(highlight: true)
42
+ # => "proc \e[32mdo\e[0m\n p(\e[1;34m1\e[0m + \e[1;34m1\e[0m)\n\e[32mend\e[0m"
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/[my-github-username]/proc_to_ast/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,79 @@
1
+ require "proc_to_ast/version"
2
+
3
+ require 'parser/current'
4
+ require 'unparser'
5
+ require 'coderay'
6
+
7
+ module ProcToAst
8
+ class Traverser
9
+ def traverse_node(node)
10
+ if node.type != :block
11
+ node.children.flat_map { |child|
12
+ if child.is_a?(AST::Node)
13
+ traverse_node(child)
14
+ end
15
+ }.compact
16
+ else
17
+ if proc_block?(node)
18
+ node
19
+ end
20
+ end
21
+ end
22
+
23
+ def proc_block?(node)
24
+ head = node.children[0]
25
+ return false unless head.type == :send
26
+
27
+ receiver, symbol = head.children
28
+
29
+ return true if receiver.nil? && (symbol == :proc || symbol == :lambda)
30
+
31
+ if receiver.is_a?(AST::Node) &&
32
+ receiver.type == :const &&
33
+ receiver.children[1] == :Proc &&
34
+ symbol == :new
35
+
36
+ return true
37
+ end
38
+
39
+ false
40
+ end
41
+ end
42
+ end
43
+
44
+ class Proc
45
+ def to_ast(retry_limit = 20)
46
+ filename, linenum = source_location
47
+ file = File.open(filename, "rb")
48
+
49
+ (linenum - 1).times { file.gets }
50
+ buf = []
51
+ try_count = 0
52
+
53
+ parser = Parser::CurrentRuby.default_parser
54
+ parser.diagnostics.consumer = ->(diagnostic) {} # suppress error message
55
+ begin
56
+ parser.reset
57
+ try_count += 1
58
+
59
+ buf << file.gets
60
+ source = buf.join.force_encoding(parser.default_encoding)
61
+
62
+ source_buffer = Parser::Source::Buffer.new(filename, linenum)
63
+ source_buffer.source = source
64
+ node = parser.parse(source_buffer)
65
+ ProcToAst::Traverser.new.traverse_node(node).first
66
+ rescue Parser::SyntaxError
67
+ retry if try_count < retry_limit
68
+ end
69
+ end
70
+
71
+ def to_source(highlight: false)
72
+ source = Unparser.unparse(to_ast)
73
+ if highlight
74
+ CodeRay.scan(source, :ruby).terminal
75
+ else
76
+ source
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module ProcToAst
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'proc_to_ast/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "proc_to_ast"
8
+ spec.version = ProcToAst::VERSION
9
+ spec.authors = ["joker1007"]
10
+ spec.email = ["kakyoin.hierophant@gmail.com"]
11
+ spec.summary = %q{Convert Proc object to AST::Node}
12
+ spec.description = %q{Add #to_ast method to Proc. #to_ast converts Proc object to AST::Node.}
13
+ spec.homepage = "https://github.com/joker1007/proc_to_ast"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "parser"
22
+ spec.add_runtime_dependency "unparser"
23
+ spec.add_runtime_dependency "coderay"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.6"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Proc do
4
+ describe "#to_ast" do
5
+ it "return Parser::AST::Node" do
6
+ ast = -> { 1 + 1 }.to_ast
7
+ expect(ast).to be_a(Parser::AST::Node)
8
+ expect(ast.type).to eq(:block)
9
+ end
10
+
11
+ it "converts proc variation" do
12
+
13
+ hoge = proc { p 1 }
14
+
15
+ _ = [1].map {|i| i * 2}; fuga = ->(a) {
16
+ p a
17
+ }
18
+
19
+ foo = Proc.new do |b|
20
+ puts b
21
+ end
22
+
23
+ expect(hoge.to_ast).to be_a(AST::Node)
24
+ expect(fuga.to_ast).to be_a(AST::Node)
25
+ expect(foo.to_ast).to be_a(AST::Node)
26
+ end
27
+ end
28
+
29
+ describe "#to_source" do
30
+ it "return source code string" do
31
+ _ = [1].map {|i| i * 2}; fuga = ->(a) {
32
+ p a
33
+ }
34
+
35
+ expect(fuga.to_source).to eq("lambda do |a|\n p(a)\nend")
36
+ expect(fuga.to_source(highlight: true)).to eq("lambda \e[32mdo\e[0m |a|\n p(a)\n\e[32mend\e[0m")
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'proc_to_ast'
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proc_to_ast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - joker1007
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
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: unparser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coderay
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: 'Add #to_ast method to Proc. #to_ast converts Proc object to AST::Node.'
98
+ email:
99
+ - kakyoin.hierophant@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/proc_to_ast.rb
112
+ - lib/proc_to_ast/version.rb
113
+ - proc_to_ast.gemspec
114
+ - spec/proc_to_ast_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/joker1007/proc_to_ast
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Convert Proc object to AST::Node
140
+ test_files:
141
+ - spec/proc_to_ast_spec.rb
142
+ - spec/spec_helper.rb