rensei 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rensei.rb +11 -0
- data/lib/rensei/node_to_hash.rb +54 -0
- data/lib/rensei/unparser.rb +1398 -0
- data/lib/rensei/version.rb +3 -0
- data/rensei.gemspec +23 -0
- data/sample/bocchi.rb +55 -0
- data/sample/sample.rb +12 -0
- metadata +59 -0
data/rensei.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'lib/rensei/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "rensei"
|
5
|
+
spec.version = Rensei::VERSION
|
6
|
+
spec.authors = ["manga_osyo"]
|
7
|
+
spec.email = ["manga.osyo@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Unparse from `RubyVM::AbstractSyntaxTree::Node` to Ruby code.}
|
10
|
+
spec.description = %q{Unparse from `RubyVM::AbstractSyntaxTree::Node` to Ruby code.}
|
11
|
+
spec.homepage = ""
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
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.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
end
|
data/sample/bocchi.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require "rensei"
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
# ノードの Hash をネストして操作する拡張する
|
5
|
+
def each_node(&block)
|
6
|
+
return enum_for(:each_node) unless block
|
7
|
+
self[:children].each { |node|
|
8
|
+
block.call self
|
9
|
+
if Hash === node
|
10
|
+
node.each_node(&block)
|
11
|
+
end
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# RubyVM::AbstractSyntaxTree::Node から Hash に変換する拡張
|
17
|
+
# RubyVM::AbstractSyntaxTree::Node#to_h で変換できる
|
18
|
+
using Rensei::NodeToHash
|
19
|
+
|
20
|
+
|
21
|
+
def bocchi &block
|
22
|
+
# ブロックの中身を AST に変換
|
23
|
+
ast = RubyVM::AbstractSyntaxTree.of(block)
|
24
|
+
|
25
|
+
# AST から必要な node だけ抽出
|
26
|
+
node = ast.children.last
|
27
|
+
|
28
|
+
# AST の node から Hash に変換
|
29
|
+
node_h = node.to_h
|
30
|
+
|
31
|
+
# AST の type の意味を変更
|
32
|
+
# hoge.foo から hoge&.foo に変換する
|
33
|
+
node_h.each_node { |node|
|
34
|
+
node[:type] = :QCALL if node[:type] == :CALL
|
35
|
+
}
|
36
|
+
|
37
|
+
# 変換した AST の Hash を Ruby のコードに戻す
|
38
|
+
code = Rensei.unparse(node_h)
|
39
|
+
|
40
|
+
# メソッドの呼び出し元のコンテキストで評価する
|
41
|
+
block.binding.eval(code)
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
hoge1 = nil
|
46
|
+
hoge2 = Struct.new(:foo).new(nil)
|
47
|
+
hoge3 = Struct.new(:foo).new(bar: "hogehoge")
|
48
|
+
|
49
|
+
# . を &. に変換して実行する
|
50
|
+
bocchi {
|
51
|
+
pp hoge1.foo[:bar] # => nil
|
52
|
+
pp hoge2.foo[:bar] # => nil
|
53
|
+
pp hoge3.foo[:bar] # => "hogehoge"
|
54
|
+
}
|
55
|
+
|
data/sample/sample.rb
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rensei
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- manga_osyo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Unparse from `RubyVM::AbstractSyntaxTree::Node` to Ruby code.
|
14
|
+
email:
|
15
|
+
- manga.osyo@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/rensei.rb
|
30
|
+
- lib/rensei/node_to_hash.rb
|
31
|
+
- lib/rensei/unparser.rb
|
32
|
+
- lib/rensei/version.rb
|
33
|
+
- rensei.gemspec
|
34
|
+
- sample/bocchi.rb
|
35
|
+
- sample/sample.rb
|
36
|
+
homepage: ''
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.3.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.1.4
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Unparse from `RubyVM::AbstractSyntaxTree::Node` to Ruby code.
|
59
|
+
test_files: []
|