yard2steep 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/example/sample1/lib/example1.rb +11 -1
- data/example/sample1/sig/example1.rbi +11 -1
- data/lib/yard2steep/ast/constant_node.rb +7 -5
- data/lib/yard2steep/comments.rb +131 -0
- data/lib/yard2steep/gen.rb +1 -1
- data/lib/yard2steep/parser.rb +86 -134
- data/lib/yard2steep/type/ast.rb +22 -9
- data/lib/yard2steep/type/parser.rb +17 -16
- data/lib/yard2steep/type.rb +1 -1
- data/lib/yard2steep/version.rb +1 -1
- data/sig/steep-scaffold/td.rbi +33 -16
- data/sig/yard2steep/yard2steep/ast/class_node.rbi +1 -1
- data/sig/yard2steep/yard2steep/ast/constant_node.rbi +3 -1
- data/sig/yard2steep/yard2steep/ast/p_node.rbi +1 -1
- data/sig/yard2steep/yard2steep/ast/p_type_node.rbi +1 -1
- data/sig/yard2steep/yard2steep/comments.rbi +15 -0
- data/sig/yard2steep/yard2steep/parser.rbi +4 -13
- data/sig/yard2steep/yard2steep/type/ast.rbi +10 -6
- data/sig/yard2steep/yard2steep/type/parser.rbi +3 -3
- data/sig/yard2steep/yard2steep/type.rbi +2 -2
- data/sig/yard2steep/yard2steep/version.rbi +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6288038fd78f492af6aa413b15c40ac3c6f3ada8068e56a3cf4ec252cfccec1d
|
4
|
+
data.tar.gz: 9fb5f345434b127bd61f96f1081874b8157698a8f7eaf5a8e6779ff891480642
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 478577c07127351b4080ccbcf48f87656b65cafd6481ebd188bf3d110d26fc04da2513d520d6e99182981bbb84450ebab1c9651ce14569dd6ca3c1941684afaa
|
7
|
+
data.tar.gz: 8146d573e9f52dbac2309df6651c7b39733f3e5a6d267ec279985a6ec6459866bc61f84e4c4061aa75257444443a03eabbb91c192aadeae13ad62c8307bb02b6
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Generate [steep](https://github.com/soutaro/steep) type definition file from yard annotation.
|
4
4
|
|
5
|
-
:warning: **This is highly experimental project. Current version is 0.1.
|
5
|
+
:warning: **This is highly experimental project. Current version is 0.1.2.**
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -31,7 +31,17 @@ class MyClass
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
CONSTANT
|
34
|
+
CONSTANT = "This is constant"
|
35
|
+
CONSTANT2 = /this is re/
|
36
|
+
CONSTANT3 = :symbol_value
|
37
|
+
CONSTANT4 = 1..2
|
38
|
+
CONSTANT5 = 34
|
39
|
+
CONSTANT6 = 2.34
|
40
|
+
CONSTANT7 = [1, 2]
|
41
|
+
CONSTANT8 = { a: 3 }
|
42
|
+
CONSTANT9 = true
|
43
|
+
CONSTANT10 = false
|
44
|
+
CONSTANT11 = nil
|
35
45
|
|
36
46
|
# This for should not be used.
|
37
47
|
# @return [String]
|
@@ -19,7 +19,17 @@ class MyClass
|
|
19
19
|
def present?: (any) -> any
|
20
20
|
def mysum: (any) { (any) -> any } -> any
|
21
21
|
end
|
22
|
-
MyClass::CONSTANT:
|
22
|
+
MyClass::CONSTANT: String
|
23
|
+
MyClass::CONSTANT2: Regexp
|
24
|
+
MyClass::CONSTANT3: Symbol
|
25
|
+
MyClass::CONSTANT4: Range<any>
|
26
|
+
MyClass::CONSTANT5: Integer
|
27
|
+
MyClass::CONSTANT6: Float
|
28
|
+
MyClass::CONSTANT7: Array<any>
|
29
|
+
MyClass::CONSTANT8: Hash<any, any>
|
30
|
+
MyClass::CONSTANT9: bool
|
31
|
+
MyClass::CONSTANT10: bool
|
32
|
+
MyClass::CONSTANT11: nil
|
23
33
|
module MyClass::InnerClass
|
24
34
|
def double: (source: Integer) -> Integer
|
25
35
|
end
|
@@ -1,15 +1,17 @@
|
|
1
1
|
module Yard2steep
|
2
2
|
module AST
|
3
3
|
class ConstantNode
|
4
|
-
# @dynamic name, klass
|
5
|
-
attr_reader :name, :klass
|
4
|
+
# @dynamic name, klass, v_type
|
5
|
+
attr_reader :name, :klass, :v_type
|
6
6
|
|
7
7
|
# @param [String] name
|
8
8
|
# @param [String] klass
|
9
|
-
|
9
|
+
# @param [String] v_type
|
10
|
+
def initialize(name:, klass:, v_type:)
|
10
11
|
Util.assert! { name.is_a?(String) }
|
11
|
-
@name
|
12
|
-
@klass
|
12
|
+
@name = name
|
13
|
+
@klass = klass
|
14
|
+
@v_type = v_type
|
13
15
|
end
|
14
16
|
|
15
17
|
# @return [String]
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'yard2steep/type'
|
2
|
+
|
3
|
+
module Yard2steep
|
4
|
+
class Comments
|
5
|
+
S_RE = /[\s\t]*/
|
6
|
+
TYPE_WITH_PAREN_RE = /
|
7
|
+
\[
|
8
|
+
(
|
9
|
+
[^\]]
|
10
|
+
*
|
11
|
+
)
|
12
|
+
\]
|
13
|
+
/x
|
14
|
+
COMMENT_RE = /^
|
15
|
+
\#
|
16
|
+
#{S_RE}
|
17
|
+
@(?:param|return)
|
18
|
+
#{S_RE}
|
19
|
+
#{TYPE_WITH_PAREN_RE}
|
20
|
+
/x
|
21
|
+
PARAM_RE = /
|
22
|
+
\#
|
23
|
+
#{S_RE}
|
24
|
+
@param
|
25
|
+
#{S_RE}
|
26
|
+
#{TYPE_WITH_PAREN_RE}
|
27
|
+
#{S_RE}
|
28
|
+
(\w+)
|
29
|
+
/x
|
30
|
+
RETURN_RE = /
|
31
|
+
\#
|
32
|
+
#{S_RE}
|
33
|
+
@return
|
34
|
+
#{S_RE}
|
35
|
+
#{TYPE_WITH_PAREN_RE}
|
36
|
+
/x
|
37
|
+
|
38
|
+
# @param [String] text
|
39
|
+
def initialize(text)
|
40
|
+
@comments_map = extract(text)
|
41
|
+
end
|
42
|
+
|
43
|
+
# @param [Integer] m_loc represents location of method definition
|
44
|
+
# @return [Array(Hash { String => String }, String)]
|
45
|
+
def parse_from(m_loc)
|
46
|
+
Util.assert! { m_loc >= 0 }
|
47
|
+
reset_context!
|
48
|
+
|
49
|
+
l = m_loc - 1
|
50
|
+
while l >= 0
|
51
|
+
comment = @comments_map[l]
|
52
|
+
break unless comment # nil when no more comment exist
|
53
|
+
|
54
|
+
parse_comment!(comment)
|
55
|
+
l -= 1
|
56
|
+
end
|
57
|
+
|
58
|
+
[@p_types, @r_type]
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
# @return [void]
|
64
|
+
def reset_context!
|
65
|
+
@p_types = {}
|
66
|
+
@r_type = nil
|
67
|
+
end
|
68
|
+
|
69
|
+
# @param [String] text
|
70
|
+
# @return [Hash{ String => String }]
|
71
|
+
def extract(text)
|
72
|
+
# NOTE: `Ripper.lex` returns array of array such as
|
73
|
+
# [
|
74
|
+
# [[1, 0], :on_comment, "# @param [Array] contents\n", EXPR_BEG],
|
75
|
+
# ...
|
76
|
+
# ]
|
77
|
+
r = {}
|
78
|
+
Ripper.lex(text).each do |t|
|
79
|
+
# Check token type
|
80
|
+
type = t[1]
|
81
|
+
next if type != :on_comment
|
82
|
+
# Check comment body
|
83
|
+
comment = t[2]
|
84
|
+
next unless comment.match?(COMMENT_RE)
|
85
|
+
|
86
|
+
line = t[0][0]
|
87
|
+
r[line] = comment
|
88
|
+
end
|
89
|
+
|
90
|
+
r
|
91
|
+
end
|
92
|
+
|
93
|
+
# @param [String] comment
|
94
|
+
# @return [void]
|
95
|
+
def parse_comment!(comment)
|
96
|
+
return if try_param_comment(comment)
|
97
|
+
return if try_return_comment(comment)
|
98
|
+
raise "Must not reach here!"
|
99
|
+
end
|
100
|
+
|
101
|
+
# @param [String] comment
|
102
|
+
# @return [bool]
|
103
|
+
def try_param_comment(comment)
|
104
|
+
m = comment.match(PARAM_RE)
|
105
|
+
return false unless m
|
106
|
+
|
107
|
+
type = normalize_type(m[1])
|
108
|
+
name = m[2]
|
109
|
+
@p_types[name] = type
|
110
|
+
|
111
|
+
true
|
112
|
+
end
|
113
|
+
|
114
|
+
# @param [String] comment
|
115
|
+
# @return [bool]
|
116
|
+
def try_return_comment(comment)
|
117
|
+
m = comment.match(RETURN_RE)
|
118
|
+
return false unless m
|
119
|
+
|
120
|
+
@r_type = normalize_type(m[1])
|
121
|
+
|
122
|
+
true
|
123
|
+
end
|
124
|
+
|
125
|
+
# @param [String] type
|
126
|
+
# @return [String]
|
127
|
+
def normalize_type(type)
|
128
|
+
Type.translate(type)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
data/lib/yard2steep/gen.rb
CHANGED
@@ -94,7 +94,7 @@ module Yard2steep
|
|
94
94
|
def gen_c!(c_node, off:)
|
95
95
|
Util.assert! { c_node.is_a?(AST::ConstantNode) }
|
96
96
|
# NOTE: Use any as constant type.
|
97
|
-
emit! "#{c_node.long_name}:
|
97
|
+
emit! "#{c_node.long_name}: #{c_node.v_type}\n", off: off
|
98
98
|
end
|
99
99
|
|
100
100
|
# @param [AST::MethodNode] m_node
|
data/lib/yard2steep/parser.rb
CHANGED
@@ -1,44 +1,11 @@
|
|
1
1
|
require 'ripper'
|
2
2
|
|
3
|
-
require 'yard2steep/
|
3
|
+
require 'yard2steep/comments'
|
4
4
|
require 'yard2steep/ast'
|
5
5
|
require 'yard2steep/util'
|
6
6
|
|
7
7
|
module Yard2steep
|
8
8
|
class Parser
|
9
|
-
S_RE = /[\s\t]*/
|
10
|
-
TYPE_WITH_PAREN_RE = /
|
11
|
-
\[
|
12
|
-
(
|
13
|
-
[^\]]
|
14
|
-
*
|
15
|
-
)
|
16
|
-
\]
|
17
|
-
/x
|
18
|
-
COMMENT_RE = /^
|
19
|
-
\#
|
20
|
-
#{S_RE}
|
21
|
-
@(?:param|return)
|
22
|
-
#{S_RE}
|
23
|
-
#{TYPE_WITH_PAREN_RE}
|
24
|
-
/x
|
25
|
-
PARAM_RE = /
|
26
|
-
\#
|
27
|
-
#{S_RE}
|
28
|
-
@param
|
29
|
-
#{S_RE}
|
30
|
-
#{TYPE_WITH_PAREN_RE}
|
31
|
-
#{S_RE}
|
32
|
-
(\w+)
|
33
|
-
/x
|
34
|
-
RETURN_RE = /
|
35
|
-
\#
|
36
|
-
#{S_RE}
|
37
|
-
@return
|
38
|
-
#{S_RE}
|
39
|
-
#{TYPE_WITH_PAREN_RE}
|
40
|
-
/x
|
41
|
-
|
42
9
|
ANY_TYPE = 'any'
|
43
10
|
ANY_BLOCK_TYPE = '{ (any) -> any }'
|
44
11
|
|
@@ -48,8 +15,6 @@ module Yard2steep
|
|
48
15
|
|
49
16
|
# NOTE: set at parse
|
50
17
|
@file = nil
|
51
|
-
|
52
|
-
@comments_map = {}
|
53
18
|
end
|
54
19
|
|
55
20
|
# @param [String] file
|
@@ -62,7 +27,7 @@ module Yard2steep
|
|
62
27
|
@file = file
|
63
28
|
debug_print!("Start parsing: #{file}")
|
64
29
|
|
65
|
-
@
|
30
|
+
@comments = Comments.new(text)
|
66
31
|
|
67
32
|
main = AST::ClassNode.create_main
|
68
33
|
@ast = main
|
@@ -181,7 +146,7 @@ module Yard2steep
|
|
181
146
|
# @return [void]
|
182
147
|
def parse_method_impl(m_name, m_loc, params)
|
183
148
|
within_context do
|
184
|
-
|
149
|
+
@p_types, @r_type = @comments.parse_from(m_loc)
|
185
150
|
|
186
151
|
p_list = parse_params(params)
|
187
152
|
|
@@ -197,54 +162,6 @@ module Yard2steep
|
|
197
162
|
end
|
198
163
|
end
|
199
164
|
|
200
|
-
# @param [Integer] m_loc represents location of method definition
|
201
|
-
# @return [void]
|
202
|
-
def extract_p_types!(m_loc)
|
203
|
-
Util.assert! { m_loc >= 0 }
|
204
|
-
l = m_loc - 1
|
205
|
-
while l >= 0
|
206
|
-
comment = @comments_map[l]
|
207
|
-
break unless comment # nil when no more comment exist
|
208
|
-
|
209
|
-
parse_comment!(comment)
|
210
|
-
l -= 1
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
# @param [String] comment
|
215
|
-
# @return [void]
|
216
|
-
def parse_comment!(comment)
|
217
|
-
return if try_param_comment(comment)
|
218
|
-
return if try_return_comment(comment)
|
219
|
-
raise "Must not reach here!"
|
220
|
-
end
|
221
|
-
|
222
|
-
# @param [String] comment
|
223
|
-
# @return [bool]
|
224
|
-
def try_param_comment(comment)
|
225
|
-
m = comment.match(PARAM_RE)
|
226
|
-
return false unless m
|
227
|
-
|
228
|
-
p = AST::PTypeNode.new(
|
229
|
-
p_type: normalize_type(m[1]),
|
230
|
-
p_name: m[2],
|
231
|
-
kind: AST::PTypeNode::KIND[:normal],
|
232
|
-
)
|
233
|
-
@p_types[p.p_name] = p
|
234
|
-
|
235
|
-
true
|
236
|
-
end
|
237
|
-
|
238
|
-
# @param [String] comment
|
239
|
-
# @return [bool]
|
240
|
-
def try_return_comment(comment)
|
241
|
-
m = comment.match(RETURN_RE)
|
242
|
-
return false unless m
|
243
|
-
|
244
|
-
@r_type = normalize_type(m[1])
|
245
|
-
|
246
|
-
true
|
247
|
-
end
|
248
165
|
|
249
166
|
# @param [Array] r_ast
|
250
167
|
# @return [Array<AST::PNode>]
|
@@ -504,12 +421,74 @@ module Yard2steep
|
|
504
421
|
}
|
505
422
|
var_type = r_ast[1][1][0]
|
506
423
|
if var_type == :@const
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
424
|
+
parse_assign_constant(name: r_ast[1][1][1], v_ast: r_ast[2])
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
# @param [String] name
|
429
|
+
# @param [Array] v_ast
|
430
|
+
# @return [void]
|
431
|
+
def parse_assign_constant(name:, v_ast:)
|
432
|
+
c = AST::ConstantNode.new(
|
433
|
+
name: name,
|
434
|
+
klass: @current_class,
|
435
|
+
v_type: type_of(v_ast),
|
436
|
+
)
|
437
|
+
@current_class.append_constant(c)
|
438
|
+
end
|
439
|
+
|
440
|
+
# @param [Array] r_ast
|
441
|
+
# @return [String]
|
442
|
+
def type_of(r_ast)
|
443
|
+
# NOTE: r_ast is array such as
|
444
|
+
#
|
445
|
+
# String example:
|
446
|
+
# [:string_literal,
|
447
|
+
# [:string_content,
|
448
|
+
# [:@tstring_content, "String...", [34, 15]]
|
449
|
+
# ]
|
450
|
+
# ]
|
451
|
+
#
|
452
|
+
# Regexp example:
|
453
|
+
# [:regexp_literal,
|
454
|
+
# [
|
455
|
+
# [:@tstring_content, "this is re", [35, 15]]
|
456
|
+
# ],
|
457
|
+
# [:@regexp_end, "/", [35, 25]]
|
458
|
+
# ]
|
459
|
+
#
|
460
|
+
# nil example:
|
461
|
+
# [:var_ref, [:@kw, "nil", [40, 14]]]
|
462
|
+
|
463
|
+
case r_ast[0]
|
464
|
+
when :string_literal
|
465
|
+
'String'
|
466
|
+
when :regexp_literal
|
467
|
+
'Regexp'
|
468
|
+
when :symbol_literal
|
469
|
+
'Symbol'
|
470
|
+
when :dot2
|
471
|
+
'Range<any>'
|
472
|
+
when :@int
|
473
|
+
'Integer'
|
474
|
+
when :@float
|
475
|
+
'Float'
|
476
|
+
when :array
|
477
|
+
'Array<any>' # TODO(south37): check type of element
|
478
|
+
when :hash
|
479
|
+
'Hash<any, any>' # TODO(south37): check type of element
|
480
|
+
when :var_ref
|
481
|
+
Util.assert! { r_ast[1].is_a?(Array) && r_ast[1][0] == :@kw }
|
482
|
+
case r_ast[1][1]
|
483
|
+
when 'true', 'false'
|
484
|
+
'bool'
|
485
|
+
when 'nil'
|
486
|
+
'nil'
|
487
|
+
else
|
488
|
+
'any'
|
489
|
+
end
|
490
|
+
else
|
491
|
+
'any'
|
513
492
|
end
|
514
493
|
end
|
515
494
|
|
@@ -657,30 +636,6 @@ module Yard2steep
|
|
657
636
|
end
|
658
637
|
end
|
659
638
|
|
660
|
-
# @param [String] text
|
661
|
-
# @return [Hash{ String => String }]
|
662
|
-
def extract_comments(text)
|
663
|
-
# NOTE: `Ripper.lex` returns array of array such as
|
664
|
-
# [
|
665
|
-
# [[1, 0], :on_comment, "# @param [Array] contents\n", EXPR_BEG],
|
666
|
-
# ...
|
667
|
-
# ]
|
668
|
-
r = {}
|
669
|
-
Ripper.lex(text).each do |t|
|
670
|
-
# Check token type
|
671
|
-
type = t[1]
|
672
|
-
next if type != :on_comment
|
673
|
-
# Check comment body
|
674
|
-
comment = t[2]
|
675
|
-
next unless comment.match?(COMMENT_RE)
|
676
|
-
|
677
|
-
line = t[0][0]
|
678
|
-
r[line] = comment
|
679
|
-
end
|
680
|
-
|
681
|
-
r
|
682
|
-
end
|
683
|
-
|
684
639
|
##
|
685
640
|
# Helper
|
686
641
|
|
@@ -694,35 +649,32 @@ module Yard2steep
|
|
694
649
|
# @return [AST::PTypeNode]
|
695
650
|
def type_node(p)
|
696
651
|
if @p_types[p]
|
697
|
-
@p_types[p]
|
652
|
+
type = @p_types[p]
|
698
653
|
else
|
699
|
-
|
700
|
-
p_type: ANY_TYPE,
|
701
|
-
p_name: p,
|
702
|
-
kind: AST::PTypeNode::KIND[:normal],
|
703
|
-
)
|
654
|
+
type = ANY_TYPE
|
704
655
|
end
|
656
|
+
|
657
|
+
AST::PTypeNode.new(
|
658
|
+
p_type: type,
|
659
|
+
p_name: p,
|
660
|
+
kind: AST::PTypeNode::KIND[:normal],
|
661
|
+
)
|
705
662
|
end
|
706
663
|
|
707
664
|
# @param [String] p
|
708
665
|
# @return [AST::PTypeNode]
|
709
666
|
def block_type_node(p)
|
710
667
|
if @p_types[p]
|
711
|
-
@p_types[p]
|
668
|
+
type = @p_types[p]
|
712
669
|
else
|
713
|
-
|
714
|
-
p_type: ANY_BLOCK_TYPE,
|
715
|
-
p_name: p,
|
716
|
-
kind: AST::PTypeNode::KIND[:block],
|
717
|
-
)
|
670
|
+
type = ANY_BLOCK_TYPE
|
718
671
|
end
|
719
|
-
end
|
720
672
|
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
673
|
+
AST::PTypeNode.new(
|
674
|
+
p_type: type,
|
675
|
+
p_name: p,
|
676
|
+
kind: AST::PTypeNode::KIND[:block],
|
677
|
+
)
|
726
678
|
end
|
727
679
|
end
|
728
680
|
end
|
data/lib/yard2steep/type/ast.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
module Yard2steep
|
2
2
|
class Type
|
3
3
|
class TypeBase
|
4
|
-
|
5
|
-
|
6
|
-
def self.union2s(types)
|
7
|
-
types.map { |t| t.to_s }.uniq.join(' | ')
|
4
|
+
def to_s
|
5
|
+
raise "Must be implemented in child class"
|
8
6
|
end
|
9
7
|
end
|
10
8
|
|
11
9
|
class AnyType < TypeBase
|
10
|
+
# @return [String]
|
12
11
|
def to_s
|
13
12
|
'any'
|
14
13
|
end
|
@@ -26,26 +25,40 @@ module Yard2steep
|
|
26
25
|
end
|
27
26
|
|
28
27
|
class ArrayType < TypeBase
|
29
|
-
# @param [
|
28
|
+
# @param [TypeBase] type
|
30
29
|
def initialize(type:)
|
31
30
|
@type = type
|
32
31
|
end
|
33
32
|
|
33
|
+
# @return [String]
|
34
34
|
def to_s
|
35
|
-
"Array<#{
|
35
|
+
"Array<#{@type}>"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
class HashType < TypeBase
|
40
|
-
# @param [
|
41
|
-
# @param [
|
40
|
+
# @param [TypeBase] key
|
41
|
+
# @param [TypeBase] val
|
42
42
|
def initialize(key:, val:)
|
43
43
|
@key = key
|
44
44
|
@val = val
|
45
45
|
end
|
46
46
|
|
47
|
+
# @return [String]
|
48
|
+
def to_s
|
49
|
+
"Hash<#{@key}, #{@val}>"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class UnionType < TypeBase
|
54
|
+
def initialize(types:)
|
55
|
+
Util.assert! { types.size > 0 }
|
56
|
+
@types = types
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [String]
|
47
60
|
def to_s
|
48
|
-
|
61
|
+
@types.map { |t| t.to_s }.uniq.join(' | ')
|
49
62
|
end
|
50
63
|
end
|
51
64
|
end
|
@@ -25,7 +25,7 @@ module Yard2steep
|
|
25
25
|
@types.push(parse_type)
|
26
26
|
end
|
27
27
|
|
28
|
-
@types
|
28
|
+
UnionType.new(types: @types)
|
29
29
|
end
|
30
30
|
|
31
31
|
private
|
@@ -69,7 +69,7 @@ module Yard2steep
|
|
69
69
|
r.push(parse_type)
|
70
70
|
|
71
71
|
while t = peek
|
72
|
-
break
|
72
|
+
break unless t == ','
|
73
73
|
expect!(',')
|
74
74
|
r.push(parse_type)
|
75
75
|
end
|
@@ -85,19 +85,19 @@ module Yard2steep
|
|
85
85
|
case peek
|
86
86
|
when '<'
|
87
87
|
expect!('<')
|
88
|
-
|
89
|
-
|
88
|
+
type = UnionType.new(
|
89
|
+
types: parse_multiple_types('>')
|
90
90
|
)
|
91
91
|
when '('
|
92
92
|
expect!('(')
|
93
|
-
|
94
|
-
|
93
|
+
type = UnionType.new(
|
94
|
+
types: parse_multiple_types(')')
|
95
95
|
)
|
96
96
|
else
|
97
|
-
|
98
|
-
type: [AnyType.new]
|
99
|
-
)
|
97
|
+
type = AnyType.new
|
100
98
|
end
|
99
|
+
|
100
|
+
ArrayType.new(type: type)
|
101
101
|
end
|
102
102
|
|
103
103
|
# @return [HashType]
|
@@ -107,20 +107,18 @@ module Yard2steep
|
|
107
107
|
case peek
|
108
108
|
when '{'
|
109
109
|
expect!('{')
|
110
|
-
key = parse_multiple_types('=')
|
111
|
-
Util.assert! { key.size > 0 }
|
112
|
-
|
110
|
+
key = UnionType.new(types: parse_multiple_types('='))
|
113
111
|
expect!('>')
|
114
|
-
val = parse_multiple_types('}')
|
115
|
-
|
112
|
+
val = UnionType.new(types: parse_multiple_types('}'))
|
113
|
+
|
116
114
|
HashType.new(
|
117
115
|
key: key,
|
118
116
|
val: val,
|
119
117
|
)
|
120
118
|
else
|
121
119
|
HashType.new(
|
122
|
-
key:
|
123
|
-
val:
|
120
|
+
key: AnyType.new,
|
121
|
+
val: AnyType.new,
|
124
122
|
)
|
125
123
|
end
|
126
124
|
end
|
@@ -131,15 +129,18 @@ module Yard2steep
|
|
131
129
|
Util.assert! { t == token }
|
132
130
|
end
|
133
131
|
|
132
|
+
# @return [String]
|
134
133
|
def get
|
135
134
|
@tokens.shift
|
136
135
|
end
|
137
136
|
|
137
|
+
# @return [String]
|
138
138
|
def peek
|
139
139
|
@tokens[0]
|
140
140
|
end
|
141
141
|
|
142
142
|
# @param [String] message
|
143
|
+
# @return [void]
|
143
144
|
def debug_print!(message)
|
144
145
|
# TODO(south37) Add flag
|
145
146
|
# print message
|
data/lib/yard2steep/type.rb
CHANGED
data/lib/yard2steep/version.rb
CHANGED
data/sig/steep-scaffold/td.rbi
CHANGED
@@ -1,15 +1,34 @@
|
|
1
|
+
class Yard2steep::Comments
|
2
|
+
@comments_map: any
|
3
|
+
@p_types: any
|
4
|
+
@r_type: any
|
5
|
+
def initialize: (any) -> any
|
6
|
+
def parse_from: (any) -> Array<any>
|
7
|
+
def reset_context!: () -> any
|
8
|
+
def extract: (any) -> any
|
9
|
+
def parse_comment!: (any) -> any
|
10
|
+
def try_param_comment: (any) -> bool
|
11
|
+
def try_return_comment: (any) -> bool
|
12
|
+
def normalize_type: (any) -> any
|
13
|
+
end
|
14
|
+
|
15
|
+
Yard2steep::Comments::S_RE: Regexp
|
16
|
+
Yard2steep::Comments::TYPE_WITH_PAREN_RE: Regexp
|
17
|
+
Yard2steep::Comments::COMMENT_RE: Regexp
|
18
|
+
Yard2steep::Comments::PARAM_RE: Regexp
|
19
|
+
Yard2steep::Comments::RETURN_RE: Regexp
|
1
20
|
module Yard2steep::AST
|
2
21
|
end
|
3
22
|
|
4
23
|
class Yard2steep::Parser
|
5
24
|
@debug: any
|
6
25
|
@file: any
|
7
|
-
@
|
26
|
+
@comments: any
|
8
27
|
@ast: any
|
9
28
|
@current_class: any
|
10
|
-
@r_type: any
|
11
29
|
@p_types: any
|
12
|
-
|
30
|
+
@r_type: any
|
31
|
+
def initialize: () -> any
|
13
32
|
def parse: (any, any, ?debug: bool) -> any
|
14
33
|
def parse_program: (any) -> any
|
15
34
|
def parse_stmts: (any) -> any
|
@@ -17,10 +36,6 @@ class Yard2steep::Parser
|
|
17
36
|
def parse_defs: (any) -> any
|
18
37
|
def parse_def: (any) -> any
|
19
38
|
def parse_method_impl: (any, any, any) -> any
|
20
|
-
def extract_p_types!: (any) -> void
|
21
|
-
def parse_comment!: (any) -> any
|
22
|
-
def try_param_comment: (any) -> bool
|
23
|
-
def try_return_comment: (any) -> bool
|
24
39
|
def parse_params: (any) -> any
|
25
40
|
def parse_paren_params: (any) -> any
|
26
41
|
def parse_no_paren_params: (any) -> any
|
@@ -28,22 +43,17 @@ class Yard2steep::Parser
|
|
28
43
|
def parse_class_or_module: (any) -> any
|
29
44
|
def parse_bodystmt: (any) -> any
|
30
45
|
def parse_assign: (any) -> void
|
46
|
+
def parse_assign_constant: (name: any, v_ast: any) -> any
|
47
|
+
def type_of: (any) -> String
|
31
48
|
def parse_command: (any) -> void
|
32
49
|
def parse_command_args_add_block: (any) -> any
|
33
50
|
def parse_method_add_arg: (any) -> void
|
34
51
|
def parse_attr_reader: (any) -> any
|
35
|
-
def extract_comments: (any) -> any
|
36
52
|
def debug_print!: (any) -> void
|
37
53
|
def type_node: (any) -> any
|
38
54
|
def block_type_node: (any) -> any
|
39
|
-
def normalize_type: (any) -> any
|
40
55
|
end
|
41
56
|
|
42
|
-
Yard2steep::Parser::S_RE: Regexp
|
43
|
-
Yard2steep::Parser::TYPE_WITH_PAREN_RE: Regexp
|
44
|
-
Yard2steep::Parser::COMMENT_RE: Regexp
|
45
|
-
Yard2steep::Parser::PARAM_RE: Regexp
|
46
|
-
Yard2steep::Parser::RETURN_RE: Regexp
|
47
57
|
Yard2steep::Parser::ANY_TYPE: String
|
48
58
|
Yard2steep::Parser::ANY_BLOCK_TYPE: String
|
49
59
|
class Yard2steep::CLI
|
@@ -73,7 +83,7 @@ class Yard2steep::Engine
|
|
73
83
|
end
|
74
84
|
|
75
85
|
class Yard2steep::Type::TypeBase
|
76
|
-
def
|
86
|
+
def to_s: () -> any
|
77
87
|
end
|
78
88
|
|
79
89
|
class Yard2steep::Type::AnyType
|
@@ -99,6 +109,12 @@ class Yard2steep::Type::HashType
|
|
99
109
|
def to_s: () -> String
|
100
110
|
end
|
101
111
|
|
112
|
+
class Yard2steep::Type::UnionType
|
113
|
+
@types: any
|
114
|
+
def initialize: (types: any) -> any
|
115
|
+
def to_s: () -> any
|
116
|
+
end
|
117
|
+
|
102
118
|
class Yard2steep::Type::Parser
|
103
119
|
@tokens: any
|
104
120
|
@types: any
|
@@ -167,7 +183,8 @@ Yard2steep::AST::ClassNode::KIND: Array<any>
|
|
167
183
|
class Yard2steep::AST::ConstantNode
|
168
184
|
@name: any
|
169
185
|
@klass: any
|
170
|
-
|
186
|
+
@v_type: any
|
187
|
+
def initialize: (name: any, klass: any, v_type: any) -> any
|
171
188
|
def long_name: () -> String
|
172
189
|
end
|
173
190
|
|
@@ -1,8 +1,10 @@
|
|
1
1
|
class Yard2steep::AST::ConstantNode
|
2
2
|
@name: any
|
3
3
|
@klass: any
|
4
|
+
@v_type: any
|
4
5
|
def name: -> any
|
5
6
|
def klass: -> any
|
6
|
-
def
|
7
|
+
def v_type: -> any
|
8
|
+
def initialize: (name: String, klass: String, v_type: String) -> any
|
7
9
|
def long_name: -> String
|
8
10
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Yard2steep::Comments
|
2
|
+
def initialize: (String) -> any
|
3
|
+
def parse_from: (Integer) -> Array<Hash<String, String> | String>
|
4
|
+
def reset_context!: -> void
|
5
|
+
def extract: (String) -> Hash<String, String>
|
6
|
+
def parse_comment!: (String) -> void
|
7
|
+
def try_param_comment: (String) -> bool
|
8
|
+
def try_return_comment: (String) -> bool
|
9
|
+
def normalize_type: (String) -> String
|
10
|
+
end
|
11
|
+
Yard2steep::Comments::S_RE: Regexp
|
12
|
+
Yard2steep::Comments::TYPE_WITH_PAREN_RE: Regexp
|
13
|
+
Yard2steep::Comments::COMMENT_RE: Regexp
|
14
|
+
Yard2steep::Comments::PARAM_RE: Regexp
|
15
|
+
Yard2steep::Comments::RETURN_RE: Regexp
|
@@ -7,10 +7,6 @@ class Yard2steep::Parser
|
|
7
7
|
def parse_defs: (Array<any>) -> void
|
8
8
|
def parse_def: (Array<any>) -> void
|
9
9
|
def parse_method_impl: (String, Integer, Array<any>) -> void
|
10
|
-
def extract_p_types!: (Integer) -> void
|
11
|
-
def parse_comment!: (String) -> void
|
12
|
-
def try_param_comment: (String) -> bool
|
13
|
-
def try_return_comment: (String) -> bool
|
14
10
|
def parse_params: (Array<any>) -> Array<AST::PNode>
|
15
11
|
def parse_paren_params: (Array<any>) -> Array<AST::PNode>
|
16
12
|
def parse_no_paren_params: (Array<any>) -> Array<AST::PNode>
|
@@ -18,20 +14,15 @@ class Yard2steep::Parser
|
|
18
14
|
def parse_class_or_module: (Array<any>) -> void
|
19
15
|
def parse_bodystmt: (Array<any>) -> void
|
20
16
|
def parse_assign: (Array<any>) -> void
|
17
|
+
def parse_assign_constant: (name: String, v_ast: Array<any>) -> void
|
18
|
+
def type_of: (Array<any>) -> String
|
21
19
|
def parse_command: (Array<any>) -> void
|
22
20
|
def parse_command_args_add_block: (Array<any>) -> Array<String>
|
23
21
|
def parse_method_add_arg: (Array<any>) -> void
|
24
22
|
def parse_attr_reader: (any) -> void
|
25
|
-
def extract_comments: (String) -> Hash<String, String>
|
26
23
|
def debug_print!: (String) -> void
|
27
24
|
def type_node: (String) -> AST::PTypeNode
|
28
25
|
def block_type_node: (String) -> AST::PTypeNode
|
29
|
-
def normalize_type: (String) -> String
|
30
26
|
end
|
31
|
-
Yard2steep::Parser::
|
32
|
-
Yard2steep::Parser::
|
33
|
-
Yard2steep::Parser::COMMENT_RE: any
|
34
|
-
Yard2steep::Parser::PARAM_RE: any
|
35
|
-
Yard2steep::Parser::RETURN_RE: any
|
36
|
-
Yard2steep::Parser::ANY_TYPE: any
|
37
|
-
Yard2steep::Parser::ANY_BLOCK_TYPE: any
|
27
|
+
Yard2steep::Parser::ANY_TYPE: String
|
28
|
+
Yard2steep::Parser::ANY_BLOCK_TYPE: String
|
@@ -1,18 +1,22 @@
|
|
1
1
|
class Yard2steep::Type::TypeBase
|
2
|
-
def
|
2
|
+
def to_s: -> any
|
3
3
|
end
|
4
4
|
class Yard2steep::Type::AnyType <: Yard2steep::Type::TypeBase
|
5
|
-
def to_s: ->
|
5
|
+
def to_s: -> String
|
6
6
|
end
|
7
7
|
class Yard2steep::Type::NormalType <: Yard2steep::Type::TypeBase
|
8
8
|
def initialize: (type: String) -> any
|
9
9
|
def to_s: -> any
|
10
10
|
end
|
11
11
|
class Yard2steep::Type::ArrayType <: Yard2steep::Type::TypeBase
|
12
|
-
def initialize: (type:
|
13
|
-
def to_s: ->
|
12
|
+
def initialize: (type: TypeBase) -> any
|
13
|
+
def to_s: -> String
|
14
14
|
end
|
15
15
|
class Yard2steep::Type::HashType <: Yard2steep::Type::TypeBase
|
16
|
-
def initialize: (key:
|
17
|
-
def to_s: ->
|
16
|
+
def initialize: (key: TypeBase, val: TypeBase) -> any
|
17
|
+
def to_s: -> String
|
18
|
+
end
|
19
|
+
class Yard2steep::Type::UnionType <: Yard2steep::Type::TypeBase
|
20
|
+
def initialize: (types: any) -> any
|
21
|
+
def to_s: -> String
|
18
22
|
end
|
@@ -8,7 +8,7 @@ class Yard2steep::Type::Parser
|
|
8
8
|
def parse_array: -> ArrayType
|
9
9
|
def parse_hash: -> HashType
|
10
10
|
def expect!: (String) -> any
|
11
|
-
def get: ->
|
12
|
-
def peek: ->
|
13
|
-
def debug_print!: (String) ->
|
11
|
+
def get: -> String
|
12
|
+
def peek: -> String
|
13
|
+
def debug_print!: (String) -> void
|
14
14
|
end
|
@@ -1 +1 @@
|
|
1
|
-
Yard2steep::VERSION:
|
1
|
+
Yard2steep::VERSION: String
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yard2steep
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nao Minami
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/yard2steep/ast/p_type_node.rb
|
130
130
|
- lib/yard2steep/cli.rb
|
131
131
|
- lib/yard2steep/cli/option.rb
|
132
|
+
- lib/yard2steep/comments.rb
|
132
133
|
- lib/yard2steep/engine.rb
|
133
134
|
- lib/yard2steep/gen.rb
|
134
135
|
- lib/yard2steep/parser.rb
|
@@ -148,6 +149,7 @@ files:
|
|
148
149
|
- sig/yard2steep/yard2steep/ast/p_type_node.rbi
|
149
150
|
- sig/yard2steep/yard2steep/cli.rbi
|
150
151
|
- sig/yard2steep/yard2steep/cli/option.rbi
|
152
|
+
- sig/yard2steep/yard2steep/comments.rbi
|
151
153
|
- sig/yard2steep/yard2steep/engine.rbi
|
152
154
|
- sig/yard2steep/yard2steep/gen.rbi
|
153
155
|
- sig/yard2steep/yard2steep/parser.rbi
|