proc_to_ast 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/lib/proc_to_ast.rb +17 -29
- data/lib/proc_to_ast/version.rb +1 -1
- data/spec/proc_to_ast_spec.rb +26 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5095ab2d916faa3c0fa5d451e7eb2aca7986033c
|
4
|
+
data.tar.gz: 6e5fb00aabf284d7b201398687ed7d7d85449e4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5573a429abb1df2000f597e755cca7b1871e4ec3cbb64109286463aed93672f1c2820bbfe40fbfe4eb74a046d78364f2f55a36049f65d2db56af1c474bce6cce
|
7
|
+
data.tar.gz: e17eaab3f8e06e26b55d77725301a53acfa38622cdaa87e65300923e7b811b85fc39c0d5e6e79b675eaaf2598b33781425f97ce3025b8cb2c5df0d8b3a808708
|
data/.travis.yml
CHANGED
data/lib/proc_to_ast.rb
CHANGED
@@ -20,31 +20,21 @@ module ProcToAst
|
|
20
20
|
#
|
21
21
|
# @param filename [String] reading file path
|
22
22
|
# @param linenum [Integer] start line number
|
23
|
-
# @param retry_limit [Integer]
|
24
23
|
# @return [Parser::AST::Node] Proc AST
|
25
|
-
def parse(filename, linenum
|
24
|
+
def parse(filename, linenum)
|
26
25
|
@filename, @linenum = filename, linenum
|
27
|
-
file = File.open(filename, "rb")
|
28
|
-
|
29
|
-
(linenum - 1).times { file.gets }
|
30
26
|
buf = []
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
return node unless node.nil?
|
42
|
-
retry if try_count < retry_limit
|
43
|
-
|
44
|
-
raise e
|
45
|
-
ensure
|
46
|
-
file.close
|
27
|
+
File.open(filename, "rb").each_with_index do |line, index|
|
28
|
+
next if index < linenum - 1
|
29
|
+
buf << line
|
30
|
+
begin
|
31
|
+
return do_parse(buf.join)
|
32
|
+
rescue ::Parser::SyntaxError
|
33
|
+
node = trim_and_retry(buf)
|
34
|
+
return node if node
|
35
|
+
end
|
47
36
|
end
|
37
|
+
fail(::Parser::SyntaxError, 'Unknown error')
|
48
38
|
end
|
49
39
|
|
50
40
|
private
|
@@ -90,19 +80,17 @@ module ProcToAst
|
|
90
80
|
end
|
91
81
|
|
92
82
|
class Proc
|
93
|
-
# @param retry_limit [Integer]
|
94
83
|
# @return [Parser::AST::Node] Proc AST
|
95
|
-
def to_ast
|
84
|
+
def to_ast
|
96
85
|
filename, linenum = source_location
|
97
86
|
parser = ProcToAst::Parser.new
|
98
|
-
parser.parse(filename, linenum
|
87
|
+
parser.parse(filename, linenum)
|
99
88
|
end
|
100
89
|
|
101
90
|
# @param highlight [Boolean] enable output highlight
|
102
|
-
# @param retry_limit [Integer]
|
103
91
|
# @return [String] proc source code
|
104
|
-
def to_source(highlight: false
|
105
|
-
source = Unparser.unparse(to_ast
|
92
|
+
def to_source(highlight: false)
|
93
|
+
source = Unparser.unparse(to_ast)
|
106
94
|
if highlight
|
107
95
|
CodeRay.scan(source, :ruby).terminal
|
108
96
|
else
|
@@ -110,8 +98,8 @@ class Proc
|
|
110
98
|
end
|
111
99
|
end
|
112
100
|
|
113
|
-
def to_raw_source(highlight: false
|
114
|
-
source = to_ast
|
101
|
+
def to_raw_source(highlight: false)
|
102
|
+
source = to_ast.loc.expression.source
|
115
103
|
|
116
104
|
if highlight
|
117
105
|
CodeRay.scan(source, :ruby).terminal
|
data/lib/proc_to_ast/version.rb
CHANGED
data/spec/proc_to_ast_spec.rb
CHANGED
@@ -23,6 +23,32 @@ describe Proc do
|
|
23
23
|
expect(pr.to_ast).to be_a(AST::Node)
|
24
24
|
end
|
25
25
|
|
26
|
+
it "long proc" do
|
27
|
+
pr = Proc.new do |b|
|
28
|
+
p 1
|
29
|
+
p 1
|
30
|
+
p 1
|
31
|
+
p 1
|
32
|
+
p 1
|
33
|
+
p 1
|
34
|
+
p 1
|
35
|
+
p 1
|
36
|
+
p 1
|
37
|
+
p 1
|
38
|
+
p 1
|
39
|
+
p 1
|
40
|
+
p 1
|
41
|
+
p 1
|
42
|
+
p 1
|
43
|
+
p 1
|
44
|
+
p 1
|
45
|
+
p 1
|
46
|
+
p 1
|
47
|
+
end
|
48
|
+
|
49
|
+
expect(pr.to_ast).to be_a(AST::Node)
|
50
|
+
end
|
51
|
+
|
26
52
|
it "converts block passing method" do
|
27
53
|
def receive_block(&block)
|
28
54
|
block.to_ast
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proc_to_ast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joker1007
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.4.6
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: Convert Proc object to AST::Node
|