rbs-inline 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -5
- data/Rakefile +12 -0
- data/lib/rbs/inline/annotation_parser/tokenizer.rb +361 -0
- data/lib/rbs/inline/annotation_parser.rb +548 -326
- data/lib/rbs/inline/ast/annotations.rb +427 -138
- data/lib/rbs/inline/ast/comment_lines.rb +17 -20
- data/lib/rbs/inline/ast/declarations.rb +64 -28
- data/lib/rbs/inline/ast/members.rb +129 -115
- data/lib/rbs/inline/ast/tree.rb +37 -22
- data/lib/rbs/inline/cli.rb +12 -12
- data/lib/rbs/inline/node_utils.rb +1 -1
- data/lib/rbs/inline/parser.rb +134 -65
- data/lib/rbs/inline/version.rb +1 -1
- data/lib/rbs/inline/writer.rb +195 -118
- data/lib/rbs/inline.rb +1 -0
- data/sig/generated/rbs/inline/annotation_parser/tokenizer.rbs +221 -0
- data/sig/generated/rbs/inline/annotation_parser.rbs +148 -92
- data/sig/generated/rbs/inline/ast/annotations.rbs +141 -37
- data/sig/generated/rbs/inline/ast/comment_lines.rbs +8 -4
- data/sig/generated/rbs/inline/ast/declarations.rbs +26 -10
- data/sig/generated/rbs/inline/ast/members.rbs +26 -15
- data/sig/generated/rbs/inline/ast/tree.rbs +23 -17
- data/sig/generated/rbs/inline/cli.rbs +3 -3
- data/sig/generated/rbs/inline/node_utils.rbs +1 -1
- data/sig/generated/rbs/inline/parser.rbs +35 -18
- data/sig/generated/rbs/inline/writer.rbs +54 -22
- metadata +4 -2
@@ -6,18 +6,20 @@ module RBS
|
|
6
6
|
module Members
|
7
7
|
# @rbs!
|
8
8
|
# type ruby = RubyDef | RubyAlias | RubyMixin | RubyAttr | RubyPublic | RubyPrivate
|
9
|
+
#
|
9
10
|
# type rbs = RBSIvar | RBSEmbedded
|
11
|
+
#
|
10
12
|
# type t = ruby | rbs
|
11
13
|
|
12
14
|
class Base
|
13
|
-
attr_reader :location
|
15
|
+
attr_reader :location #: Prism::Location
|
14
16
|
|
15
17
|
# @rbs location: Prism::Location
|
16
|
-
def initialize(location)
|
18
|
+
def initialize(location) #: void
|
17
19
|
@location = location
|
18
20
|
end
|
19
21
|
|
20
|
-
def start_line
|
22
|
+
def start_line #: Integer
|
21
23
|
location.start_line
|
22
24
|
end
|
23
25
|
end
|
@@ -26,8 +28,8 @@ module RBS
|
|
26
28
|
end
|
27
29
|
|
28
30
|
class RubyDef < RubyBase
|
29
|
-
attr_reader :node
|
30
|
-
attr_reader :comments
|
31
|
+
attr_reader :node #: Prism::DefNode
|
32
|
+
attr_reader :comments #: AnnotationParser::ParsingResult?
|
31
33
|
|
32
34
|
# The visibility directly attached to the `def` node
|
33
35
|
#
|
@@ -37,15 +39,17 @@ module RBS
|
|
37
39
|
# def foo() end # <= nil
|
38
40
|
# private def foo() end # <= :private
|
39
41
|
# ```
|
40
|
-
attr_reader :visibility
|
42
|
+
attr_reader :visibility #: RBS::AST::Members::visibility?
|
41
43
|
|
42
|
-
|
44
|
+
# Assertion given at the end of the method name
|
45
|
+
#
|
46
|
+
attr_reader :assertion #: Annotations::TypeAssertion?
|
43
47
|
|
44
48
|
# @rbs node: Prism::DefNode
|
45
49
|
# @rbs comments: AnnotationParser::ParsingResult?
|
46
50
|
# @rbs visibility: RBS::AST::Members::visibility?
|
47
|
-
# @rbs assertion: Annotations::
|
48
|
-
def initialize(node, comments, visibility, assertion)
|
51
|
+
# @rbs assertion: Annotations::TypeAssertion?
|
52
|
+
def initialize(node, comments, visibility, assertion) #: void
|
49
53
|
@node = node
|
50
54
|
@comments = comments
|
51
55
|
@visibility = visibility
|
@@ -55,39 +59,53 @@ module RBS
|
|
55
59
|
end
|
56
60
|
|
57
61
|
# Returns the name of the method
|
58
|
-
def method_name
|
62
|
+
def method_name #: Symbol
|
59
63
|
node.name
|
60
64
|
end
|
61
65
|
|
62
|
-
|
66
|
+
# Returns `nil` if no `@rbs METHOD-TYPE` or `#:` annotation is given
|
67
|
+
#
|
68
|
+
# Returns an empty array if only `...` method type is given.
|
69
|
+
#
|
70
|
+
def annotated_method_types #: Array[MethodType]?
|
63
71
|
if comments
|
64
|
-
comments.
|
65
|
-
annotation.is_a?(Annotations::
|
66
|
-
end
|
67
|
-
|
68
|
-
|
72
|
+
method_type_annotations = comments.each_annotation.select do |annotation|
|
73
|
+
annotation.is_a?(Annotations::MethodTypeAssertion) || annotation.is_a?(Annotations::Method) || annotation.is_a?(Annotations::Dot3Assertion)
|
74
|
+
end
|
75
|
+
|
76
|
+
return nil if method_type_annotations.empty?
|
77
|
+
|
78
|
+
method_type_annotations.each_with_object([]) do |annotation, method_types| #$ Array[MethodType]
|
79
|
+
case annotation
|
80
|
+
when Annotations::MethodTypeAssertion
|
81
|
+
method_types << annotation.method_type
|
82
|
+
when Annotations::Method
|
83
|
+
annotation.each_method_type do
|
84
|
+
method_types << _1
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
69
88
|
end
|
70
89
|
end
|
71
90
|
|
72
|
-
def return_type
|
91
|
+
def return_type #: Types::t?
|
73
92
|
if assertion
|
74
|
-
|
75
|
-
return assertion.type?
|
76
|
-
end
|
93
|
+
return assertion.type
|
77
94
|
end
|
95
|
+
|
78
96
|
if comments
|
79
|
-
annot = comments.
|
97
|
+
annot = comments.each_annotation.find {|annot| annot.is_a?(Annotations::ReturnType ) } #: Annotations::ReturnType?
|
80
98
|
if annot
|
81
99
|
annot.type
|
82
100
|
end
|
83
101
|
end
|
84
102
|
end
|
85
103
|
|
86
|
-
def var_type_hash
|
104
|
+
def var_type_hash #: Hash[Symbol, Types::t?]
|
87
105
|
types = {} #: Hash[Symbol, Types::t?]
|
88
106
|
|
89
107
|
if comments
|
90
|
-
comments.
|
108
|
+
comments.each_annotation.each do |annotation|
|
91
109
|
if annotation.is_a?(Annotations::VarType)
|
92
110
|
name = annotation.name
|
93
111
|
type = annotation.type
|
@@ -102,11 +120,42 @@ module RBS
|
|
102
120
|
types
|
103
121
|
end
|
104
122
|
|
105
|
-
def
|
106
|
-
if
|
107
|
-
|
108
|
-
|
123
|
+
def splat_param_type_annotation #: Annotations::SplatParamType?
|
124
|
+
if comments
|
125
|
+
comments.each_annotation.find do |annotation|
|
126
|
+
annotation.is_a?(Annotations::SplatParamType)
|
127
|
+
end #: Annotations::SplatParamType?
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def double_splat_param_type_annotation #: Annotations::DoubleSplatParamType?
|
132
|
+
if comments
|
133
|
+
comments.each_annotation.find do |annotation|
|
134
|
+
annotation.is_a?(Annotations::DoubleSplatParamType)
|
135
|
+
end #: Annotations::DoubleSplatParamType?
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def overloading? #: bool
|
140
|
+
if comments
|
141
|
+
comments.each_annotation do |annotation|
|
142
|
+
if annotation.is_a?(Annotations::Method)
|
143
|
+
return true if annotation.overloading
|
144
|
+
end
|
145
|
+
if annotation.is_a?(Annotations::Dot3Assertion)
|
146
|
+
return true
|
147
|
+
end
|
148
|
+
end
|
149
|
+
false
|
150
|
+
else
|
151
|
+
false
|
152
|
+
end
|
153
|
+
end
|
109
154
|
|
155
|
+
def method_overloads #: Array[RBS::AST::Members::MethodDefinition::Overload]
|
156
|
+
case
|
157
|
+
when method_types = annotated_method_types
|
158
|
+
method_types.map do |method_type|
|
110
159
|
RBS::AST::Members::MethodDefinition::Overload.new(
|
111
160
|
method_type: method_type,
|
112
161
|
annotations: []
|
@@ -144,22 +193,15 @@ module RBS
|
|
144
193
|
end
|
145
194
|
|
146
195
|
if (rest = node.parameters.rest).is_a?(Prism::RestParameterNode)
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
if rest_type
|
153
|
-
if rest_type.is_a?(Types::ClassInstance)
|
154
|
-
if rest_type.name.name == :Array && rest_type.name.namespace.empty?
|
155
|
-
rest_type = rest_type.args[0]
|
156
|
-
end
|
157
|
-
end
|
196
|
+
splat_param_type = splat_param_type_annotation
|
197
|
+
|
198
|
+
if splat_param_type && splat_param_type.type
|
199
|
+
splat_type = splat_param_type.type
|
158
200
|
end
|
159
201
|
|
160
202
|
rest_positionals = Types::Function::Param.new(
|
161
203
|
name: rest.name,
|
162
|
-
type:
|
204
|
+
type: splat_type || Types::Bases::Any.new(location: nil),
|
163
205
|
location: nil
|
164
206
|
)
|
165
207
|
end
|
@@ -183,60 +225,31 @@ module RBS
|
|
183
225
|
end
|
184
226
|
|
185
227
|
if (kw_rest = node.parameters.keyword_rest).is_a?(Prism::KeywordRestParameterNode)
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
if rest_type
|
192
|
-
if rest_type.is_a?(Types::ClassInstance)
|
193
|
-
if rest_type.name.name == :Hash && rest_type.name.namespace.empty?
|
194
|
-
rest_type = rest_type.args[1]
|
195
|
-
end
|
196
|
-
end
|
228
|
+
double_splat_param_type = double_splat_param_type_annotation
|
229
|
+
|
230
|
+
if double_splat_param_type && double_splat_param_type.type
|
231
|
+
double_splat_type = double_splat_param_type.type
|
197
232
|
end
|
198
233
|
|
199
234
|
rest_keywords = Types::Function::Param.new(
|
200
235
|
name: kw_rest.name,
|
201
|
-
type:
|
236
|
+
type: double_splat_type || Types::Bases::Any.new(location: nil),
|
202
237
|
location: nil)
|
203
238
|
end
|
204
239
|
|
205
240
|
if node.parameters.block
|
206
|
-
if (block_name = node.parameters.block.name) && (var_type = var_type_hash[block_name])
|
207
|
-
if var_type.is_a?(Types::Optional)
|
208
|
-
optional = true
|
209
|
-
var_type = var_type.type
|
210
|
-
else
|
211
|
-
optional = false
|
212
|
-
end
|
213
|
-
|
214
|
-
if var_type.is_a?(Types::Proc)
|
215
|
-
block = Types::Block.new(type: var_type.type, self_type: var_type.self_type, required: !optional)
|
216
|
-
end
|
217
|
-
else
|
218
|
-
block = Types::Block.new(
|
219
|
-
type: Types::UntypedFunction.new(return_type: Types::Bases::Any.new(location: nil)),
|
220
|
-
required: false,
|
221
|
-
self_type: nil
|
222
|
-
)
|
223
|
-
end
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
if annotation = yields_annotation
|
228
|
-
case annotation.block_type
|
229
|
-
when Types::Block
|
230
|
-
block = annotation.block_type
|
231
|
-
else
|
232
241
|
block = Types::Block.new(
|
233
242
|
type: Types::UntypedFunction.new(return_type: Types::Bases::Any.new(location: nil)),
|
234
|
-
required:
|
243
|
+
required: false,
|
235
244
|
self_type: nil
|
236
245
|
)
|
237
246
|
end
|
238
247
|
end
|
239
248
|
|
249
|
+
if type = block_type_annotation&.type
|
250
|
+
block = type
|
251
|
+
end
|
252
|
+
|
240
253
|
[
|
241
254
|
RBS::AST::Members::MethodDefinition::Overload.new(
|
242
255
|
method_type: RBS::MethodType.new(
|
@@ -260,9 +273,9 @@ module RBS
|
|
260
273
|
end
|
261
274
|
end
|
262
275
|
|
263
|
-
def method_annotations
|
276
|
+
def method_annotations #: Array[RBS::AST::Annotation]
|
264
277
|
if comments
|
265
|
-
comments.
|
278
|
+
comments.each_annotation.flat_map do |annotation|
|
266
279
|
if annotation.is_a?(AST::Annotations::RBSAnnotation)
|
267
280
|
annotation.contents.map do |string|
|
268
281
|
RBS::AST::Annotation.new(
|
@@ -279,44 +292,45 @@ module RBS
|
|
279
292
|
end
|
280
293
|
end
|
281
294
|
|
282
|
-
|
295
|
+
# Returns the `@rbs override` annotation
|
296
|
+
def override_annotation #: AST::Annotations::Override?
|
283
297
|
if comments
|
284
|
-
comments.
|
298
|
+
comments.each_annotation.find do |annotation|
|
285
299
|
annotation.is_a?(AST::Annotations::Override)
|
286
300
|
end #: AST::Annotations::Override?
|
287
301
|
end
|
288
302
|
end
|
289
303
|
|
290
|
-
def
|
304
|
+
def block_type_annotation #: AST::Annotations::BlockType?
|
291
305
|
if comments
|
292
|
-
comments.
|
293
|
-
annotation.is_a?(AST::Annotations::
|
294
|
-
end #: AST::Annotations::
|
306
|
+
comments.each_annotation.find do |annotation|
|
307
|
+
annotation.is_a?(AST::Annotations::BlockType)
|
308
|
+
end #: AST::Annotations::BlockType?
|
295
309
|
end
|
296
310
|
end
|
297
311
|
end
|
298
312
|
|
299
313
|
class RubyAlias < RubyBase
|
300
|
-
attr_reader :node
|
301
|
-
attr_reader :comments
|
314
|
+
attr_reader :node #: Prism::AliasMethodNode
|
315
|
+
attr_reader :comments #: AnnotationParser::ParsingResult?
|
302
316
|
|
303
317
|
# @rbs node: Prism::AliasMethodNode
|
304
318
|
# @rbs comments: AnnotationParser::ParsingResult?
|
305
|
-
def initialize(node, comments)
|
319
|
+
def initialize(node, comments) #: void
|
306
320
|
@node = node
|
307
321
|
@comments = comments
|
308
322
|
|
309
323
|
super(node.location)
|
310
324
|
end
|
311
325
|
|
312
|
-
# @rbs
|
326
|
+
# @rbs return: Symbol -- the name of *old* method
|
313
327
|
def old_name
|
314
328
|
raise unless node.old_name.is_a?(Prism::SymbolNode)
|
315
329
|
value = node.old_name.value or raise
|
316
330
|
value.to_sym
|
317
331
|
end
|
318
332
|
|
319
|
-
# @rbs
|
333
|
+
# @rbs return: Symbol -- the name of *new* method
|
320
334
|
def new_name
|
321
335
|
raise unless node.new_name.is_a?(Prism::SymbolNode)
|
322
336
|
value = node.new_name.value or raise
|
@@ -328,18 +342,18 @@ module RBS
|
|
328
342
|
include Declarations::ConstantUtil
|
329
343
|
|
330
344
|
# CallNode that calls `include`, `prepend`, and `extend` method
|
331
|
-
attr_reader :node
|
345
|
+
attr_reader :node #: Prism::CallNode
|
332
346
|
|
333
347
|
# Comments attached to the call node
|
334
|
-
attr_reader :comments
|
348
|
+
attr_reader :comments #: AnnotationParser::ParsingResult?
|
335
349
|
|
336
350
|
# Possible following type application annotation
|
337
|
-
attr_reader :application
|
351
|
+
attr_reader :application #: Annotations::Application?
|
338
352
|
|
339
353
|
# @rbs node: Prism::CallNode
|
340
354
|
# @rbs comments: AnnotationParser::ParsingResult?
|
341
355
|
# @rbs application: Annotations::Application?
|
342
|
-
# @rbs
|
356
|
+
# @rbs return: void
|
343
357
|
def initialize(node, comments, application)
|
344
358
|
super(node.location)
|
345
359
|
|
@@ -348,7 +362,7 @@ module RBS
|
|
348
362
|
@application = application
|
349
363
|
end
|
350
364
|
|
351
|
-
# @rbs
|
365
|
+
# @rbs return: ::RBS::AST::Members::Include
|
352
366
|
# | ::RBS::AST::Members::Extend
|
353
367
|
# | ::RBS::AST::Members::Prepend
|
354
368
|
# | nil
|
@@ -397,14 +411,14 @@ module RBS
|
|
397
411
|
end
|
398
412
|
|
399
413
|
class RubyAttr < RubyBase
|
400
|
-
attr_reader :node
|
401
|
-
attr_reader :comments
|
402
|
-
attr_reader :assertion
|
414
|
+
attr_reader :node #: Prism::CallNode
|
415
|
+
attr_reader :comments #: AnnotationParser::ParsingResult?
|
416
|
+
attr_reader :assertion #: Annotations::TypeAssertion?
|
403
417
|
|
404
418
|
# @rbs node: Prism::CallNode
|
405
419
|
# @rbs comments: AnnotationParser::ParsingResult?
|
406
|
-
# @rbs assertion: Annotations::
|
407
|
-
# @rbs
|
420
|
+
# @rbs assertion: Annotations::TypeAssertion?
|
421
|
+
# @rbs return: void
|
408
422
|
def initialize(node, comments, assertion)
|
409
423
|
super(node.location)
|
410
424
|
|
@@ -416,7 +430,7 @@ module RBS
|
|
416
430
|
# @rbs return Array[RBS::AST::Members::AttrReader | RBS::AST::Members::AttrWriter | RBS::AST::Members::AttrAccessor]?
|
417
431
|
def rbs
|
418
432
|
if comments
|
419
|
-
comment = RBS::AST::Comment.new(string: comments.content, location: nil)
|
433
|
+
comment = RBS::AST::Comment.new(string: comments.content(trim: true), location: nil)
|
420
434
|
end
|
421
435
|
|
422
436
|
klass =
|
@@ -461,7 +475,7 @@ module RBS
|
|
461
475
|
#
|
462
476
|
# Returns `untyped` when not annotated.
|
463
477
|
#
|
464
|
-
def attribute_type
|
478
|
+
def attribute_type #: Types::t
|
465
479
|
type = assertion&.type
|
466
480
|
raise if type.is_a?(MethodType)
|
467
481
|
|
@@ -472,10 +486,10 @@ module RBS
|
|
472
486
|
# `private` call without arguments
|
473
487
|
#
|
474
488
|
class RubyPrivate < RubyBase
|
475
|
-
attr_reader :node
|
489
|
+
attr_reader :node #: Prism::CallNode
|
476
490
|
|
477
491
|
# @rbs node: Prism::CallNode
|
478
|
-
def initialize(node)
|
492
|
+
def initialize(node) #: void
|
479
493
|
super(node.location)
|
480
494
|
@node = node
|
481
495
|
end
|
@@ -484,10 +498,10 @@ module RBS
|
|
484
498
|
# `public` call without arguments
|
485
499
|
#
|
486
500
|
class RubyPublic < RubyBase
|
487
|
-
attr_reader :node
|
501
|
+
attr_reader :node #: Prism::CallNode
|
488
502
|
|
489
503
|
# @rbs node: Prism::CallNode
|
490
|
-
def initialize(node)
|
504
|
+
def initialize(node) #: void
|
491
505
|
super(node.location)
|
492
506
|
@node = node
|
493
507
|
end
|
@@ -497,20 +511,20 @@ module RBS
|
|
497
511
|
end
|
498
512
|
|
499
513
|
class RBSIvar < RBSBase
|
500
|
-
attr_reader :annotation
|
514
|
+
attr_reader :annotation #: Annotations::IvarType
|
501
515
|
|
502
|
-
attr_reader :comment
|
516
|
+
attr_reader :comment #: AnnotationParser::ParsingResult
|
503
517
|
|
504
518
|
# @rbs comment: AnnotationParser::ParsingResult
|
505
519
|
# @rbs annotation: Annotations::IvarType
|
506
|
-
def initialize(comment, annotation)
|
520
|
+
def initialize(comment, annotation) #: void
|
507
521
|
@comment = comment
|
508
522
|
@annotation = annotation
|
509
523
|
|
510
524
|
super(comment.comments[0].location)
|
511
525
|
end
|
512
526
|
|
513
|
-
def rbs
|
527
|
+
def rbs #: RBS::AST::Members::InstanceVariable | RBS::AST::Members::ClassInstanceVariable | nil
|
514
528
|
if annotation.type
|
515
529
|
if annotation.comment
|
516
530
|
string = annotation.comment.delete_prefix("--").lstrip
|
@@ -537,13 +551,13 @@ module RBS
|
|
537
551
|
end
|
538
552
|
|
539
553
|
class RBSEmbedded < RBSBase
|
540
|
-
attr_reader :annotation
|
554
|
+
attr_reader :annotation #: Annotations::Embedded
|
541
555
|
|
542
|
-
attr_reader :comment
|
556
|
+
attr_reader :comment #: AnnotationParser::ParsingResult
|
543
557
|
|
544
558
|
# @rbs comment: AnnotationParser::ParsingResult
|
545
559
|
# @rbs annotation: Annotations::Embedded
|
546
|
-
def initialize(comment, annotation)
|
560
|
+
def initialize(comment, annotation) #: void
|
547
561
|
@comment = comment
|
548
562
|
@annotation = annotation
|
549
563
|
|
@@ -554,7 +568,7 @@ module RBS
|
|
554
568
|
#
|
555
569
|
# Returns `RBS::ParsingError` when the `content` has syntax error.
|
556
570
|
#
|
557
|
-
def members
|
571
|
+
def members #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t] | RBS::ParsingError
|
558
572
|
source = <<~RBS
|
559
573
|
module EmbeddedModuleTest
|
560
574
|
#{annotation.content}
|
data/lib/rbs/inline/ast/tree.rb
CHANGED
@@ -3,26 +3,27 @@
|
|
3
3
|
module RBS
|
4
4
|
module Inline
|
5
5
|
module AST
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
# @rbs!
|
7
|
+
# type token = [Symbol, String]
|
8
|
+
#
|
9
|
+
# type tree = token | Tree | Types::t | MethodType | nil
|
10
10
|
|
11
|
-
|
12
|
-
attr_reader :
|
11
|
+
class Tree
|
12
|
+
attr_reader :trees #: Array[tree]
|
13
|
+
attr_reader :type #: Symbol
|
13
14
|
|
14
15
|
# Children but without `tWHITESPACE` tokens
|
15
|
-
attr_reader :non_trivia_trees
|
16
|
+
attr_reader :non_trivia_trees #: Array[tree]
|
16
17
|
|
17
18
|
# @rbs type: Symbol
|
18
|
-
def initialize(type)
|
19
|
+
def initialize(type) #: void
|
19
20
|
@type = type
|
20
21
|
@trees = []
|
21
22
|
@non_trivia_trees = []
|
22
23
|
end
|
23
24
|
|
24
25
|
# @rbs tok: tree
|
25
|
-
# @rbs
|
26
|
+
# @rbs return: self
|
26
27
|
def <<(tok)
|
27
28
|
trees << tok
|
28
29
|
unless tok.is_a?(Array) && tok[0] == :tWHITESPACE
|
@@ -32,7 +33,7 @@ module RBS
|
|
32
33
|
end
|
33
34
|
|
34
35
|
# Returns the source code associated to the tree
|
35
|
-
def to_s
|
36
|
+
def to_s #: String
|
36
37
|
buf = +""
|
37
38
|
|
38
39
|
trees.each do |tree|
|
@@ -51,12 +52,26 @@ module RBS
|
|
51
52
|
buf
|
52
53
|
end
|
53
54
|
|
55
|
+
# Returns `true` if token at the given index is of the given type
|
56
|
+
def token?(type, at:)
|
57
|
+
if tok = nth_token?(at)
|
58
|
+
tok[0] == type
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns `true` if tree at the given index is of the given type
|
63
|
+
def tree?(type, at:)
|
64
|
+
if tree = nth_tree?(at)
|
65
|
+
tree.type == type
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
54
69
|
# Returns n-th token from the children
|
55
70
|
#
|
56
71
|
# Raises if the value is not a token or nil.
|
57
72
|
#
|
58
73
|
# @rbs index: Integer
|
59
|
-
# @rbs
|
74
|
+
# @rbs return: token?
|
60
75
|
def nth_token(index)
|
61
76
|
tok = non_trivia_trees[index]
|
62
77
|
case tok
|
@@ -72,7 +87,7 @@ module RBS
|
|
72
87
|
# Returns `nil` if the value is not a token.
|
73
88
|
#
|
74
89
|
# @rbs index: Integer
|
75
|
-
# @rbs
|
90
|
+
# @rbs return: token?
|
76
91
|
def nth_token?(index)
|
77
92
|
tok = non_trivia_trees[index]
|
78
93
|
case tok
|
@@ -88,7 +103,7 @@ module RBS
|
|
88
103
|
# Raises if the value is not token.
|
89
104
|
#
|
90
105
|
# @rbs index: Integer
|
91
|
-
# @rbs
|
106
|
+
# @rbs return: token
|
92
107
|
def nth_token!(index)
|
93
108
|
nth_token(index) || raise
|
94
109
|
end
|
@@ -98,7 +113,7 @@ module RBS
|
|
98
113
|
# Raises if the value is not a tree or nil.
|
99
114
|
#
|
100
115
|
# @rbs index: Integer
|
101
|
-
# @rbs
|
116
|
+
# @rbs return: Tree?
|
102
117
|
def nth_tree(index)
|
103
118
|
tok = non_trivia_trees[index]
|
104
119
|
case tok
|
@@ -114,7 +129,7 @@ module RBS
|
|
114
129
|
# Returns `nil` if the value is not a tree or nil.
|
115
130
|
#
|
116
131
|
# @rbs index: Integer
|
117
|
-
# @rbs
|
132
|
+
# @rbs return: Tree?
|
118
133
|
def nth_tree?(index)
|
119
134
|
tok = non_trivia_trees[index]
|
120
135
|
case tok
|
@@ -130,7 +145,7 @@ module RBS
|
|
130
145
|
# Raises if the value is not a tree.
|
131
146
|
#
|
132
147
|
# @rbs index: Integer
|
133
|
-
# @rbs
|
148
|
+
# @rbs return: Tree
|
134
149
|
def nth_tree!(index)
|
135
150
|
nth_tree(index) || raise
|
136
151
|
end
|
@@ -141,7 +156,7 @@ module RBS
|
|
141
156
|
# Raises if the value is not a type or nil.
|
142
157
|
#
|
143
158
|
# @rbs index: Integer
|
144
|
-
# @rbs
|
159
|
+
# @rbs return: Types::t?
|
145
160
|
def nth_type(index)
|
146
161
|
tok = non_trivia_trees[index]
|
147
162
|
case tok
|
@@ -157,7 +172,7 @@ module RBS
|
|
157
172
|
# Returns `nil` if the value is not a type.
|
158
173
|
#
|
159
174
|
# @rbs index: Integer
|
160
|
-
# @rbs
|
175
|
+
# @rbs return: Types::t?
|
161
176
|
def nth_type?(index)
|
162
177
|
tok = non_trivia_trees[index]
|
163
178
|
case tok
|
@@ -173,7 +188,7 @@ module RBS
|
|
173
188
|
# Raises if the value is not a type.
|
174
189
|
#
|
175
190
|
# @rbs index: Integer
|
176
|
-
# @rbs
|
191
|
+
# @rbs return: Types::t
|
177
192
|
def nth_type!(index)
|
178
193
|
nth_type(index) || raise
|
179
194
|
end
|
@@ -183,7 +198,7 @@ module RBS
|
|
183
198
|
# Raises if the value is not a method type or `nil`.
|
184
199
|
#
|
185
200
|
# @rbs index: Integer
|
186
|
-
# @rbs
|
201
|
+
# @rbs return: MethodType?
|
187
202
|
def nth_method_type(index)
|
188
203
|
tok = non_trivia_trees[index]
|
189
204
|
case tok
|
@@ -199,7 +214,7 @@ module RBS
|
|
199
214
|
# Returns `nil` if the value is not a method type.
|
200
215
|
#
|
201
216
|
# @rbs index: Integer
|
202
|
-
# @rbs
|
217
|
+
# @rbs return: MethodType?
|
203
218
|
def nth_method_type?(index)
|
204
219
|
tok = non_trivia_trees[index]
|
205
220
|
case tok
|
@@ -215,7 +230,7 @@ module RBS
|
|
215
230
|
# Raises if the value is not a method tree.
|
216
231
|
#
|
217
232
|
# @rbs index: Integer
|
218
|
-
# @rbs
|
233
|
+
# @rbs return: MethodType
|
219
234
|
def nth_method_type!(index)
|
220
235
|
nth_method_type(index) || raise
|
221
236
|
end
|