rbs-inline 0.4.0 → 0.6.0

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.
@@ -12,26 +12,108 @@ module RBS
12
12
  # | Generic
13
13
  # | ModuleSelf
14
14
  # | Skip
15
- # | Assertion
15
+ # | MethodTypeAssertion | TypeAssertion | SyntaxErrorAssertion | Dot3Assertion
16
16
  # | Application
17
17
  # | RBSAnnotation
18
18
  # | Override
19
19
  # | IvarType
20
- # | Yields
21
20
  # | Embedded
21
+ # | Method
22
+ # | SplatParamType
23
+ # | DoubleSplatParamType
24
+ # | BlockType
25
+ # | ModuleDecl
26
+ # | ClassDecl
22
27
  # # | Def
23
28
  # # | AttrReader | AttrWriter | AttrAccessor
24
29
  # # | Include | Extend | Prepend
25
30
  # # | Alias
26
31
 
32
+ module Utils
33
+ # @rbs (Tree) -> RBS::AST::TypeParam?
34
+ def translate_type_param(tree)
35
+ unchecked = tree.nth_token?(0) != nil
36
+ inout =
37
+ case tree.nth_token?(1)&.[](0)
38
+ when nil
39
+ :invariant
40
+ when :kIN
41
+ :contravariant
42
+ when :kOUT
43
+ :covariant
44
+ end #: RBS::AST::TypeParam::variance
45
+
46
+ name = tree.nth_token?(2)&.last
47
+
48
+ if bound = tree.nth_tree?(3)
49
+ if type = bound.nth_type?(1)
50
+ case type
51
+ when Types::ClassSingleton, Types::ClassInstance, Types::Interface
52
+ upper_bound = type
53
+ end
54
+ end
55
+ end
56
+
57
+ if name
58
+ RBS::AST::TypeParam.new(
59
+ name: name.to_sym,
60
+ variance: inout,
61
+ upper_bound: upper_bound,
62
+ location: nil
63
+ ).unchecked!(unchecked)
64
+ end
65
+ end
66
+
67
+ # @rbs (Types::t) -> RBS::AST::Declarations::Class::Super?
68
+ def translate_super_class(type)
69
+ case type
70
+ when Types::ClassInstance
71
+ RBS::AST::Declarations::Class::Super.new(
72
+ name: type.name,
73
+ args: type.args,
74
+ location: nil
75
+ )
76
+ end
77
+ end
78
+
79
+ # Assumes the tree is generated through `#parse_module_name`
80
+ #
81
+ # Returns a type name, or `nil` if the tree is something invalid.
82
+ #
83
+ # @param tree -- A tree object that is generated through `#parse_module_name`
84
+ #
85
+ # @rbs (AST::Tree) -> TypeName?
86
+ def translate_type_name(tree)
87
+ tokens = tree.non_trivia_trees
88
+
89
+ absolute = tokens.shift
90
+ path = [] #: Array[Symbol]
91
+
92
+ tokens.each_slice(2) do |name, colon2|
93
+ if colon2
94
+ name or return nil
95
+ name.is_a?(Array) or return nil
96
+
97
+ path << name[1].to_sym
98
+ end
99
+ end
100
+
101
+ last_token = tokens.pop
102
+ last_token.is_a?(Array) or return nil
103
+ name = last_token[1].to_sym
104
+
105
+ namespace = Namespace.new(path: path, absolute: absolute ? true : false)
106
+ TypeName.new(namespace: namespace, name: name)
107
+ end
108
+ end
27
109
 
28
110
  class Base
29
- attr_reader :source #:: CommentLines
30
- attr_reader :tree #:: Tree
111
+ attr_reader :source #: CommentLines
112
+ attr_reader :tree #: Tree
31
113
 
32
114
  # @rbs tree: Tree
33
115
  # @rbs source: CommentLines
34
- # @rbs returns void
116
+ # @rbs return: void
35
117
  def initialize(tree, source)
36
118
  @tree = tree
37
119
  @source = source
@@ -39,11 +121,11 @@ module RBS
39
121
  end
40
122
 
41
123
  class VarType < Base
42
- attr_reader :name #:: Symbol
124
+ attr_reader :name #: Symbol
43
125
 
44
- attr_reader :type #:: Types::t?
126
+ attr_reader :type #: Types::t?
45
127
 
46
- attr_reader :comment #:: String?
128
+ attr_reader :comment #: String?
47
129
 
48
130
  # @rbs override
49
131
  def initialize(tree, source)
@@ -65,7 +147,7 @@ module RBS
65
147
  end
66
148
  end
67
149
 
68
- #:: () -> bool
150
+ #: () -> bool
69
151
  def complete?
70
152
  if name && type
71
153
  true
@@ -75,11 +157,100 @@ module RBS
75
157
  end
76
158
  end
77
159
 
78
- # `@rbs returns T`
160
+ class SpecialVarTypeAnnotation < Base
161
+ attr_reader :name #: Symbol?
162
+
163
+ attr_reader :type #: Types::t?
164
+
165
+ attr_reader :comment #: String?
166
+
167
+ attr_reader :type_source #: String
168
+
169
+ # @rbs override
170
+ def initialize(tree, source)
171
+ @tree = tree
172
+ @source = source
173
+
174
+ annotation = tree.nth_tree!(1)
175
+
176
+ if name = annotation.nth_token?(1)
177
+ @name = name[1].to_sym
178
+ end
179
+
180
+ if type = annotation.nth_type?(3)
181
+ @type = type
182
+ @type_source = type.location&.source || raise
183
+ else
184
+ @type = nil
185
+ @type_source = annotation.nth_tree!(3)&.to_s || raise
186
+ end
187
+
188
+ if comment = annotation.nth_tree(4)
189
+ @comment = comment.to_s
190
+ end
191
+ end
192
+ end
193
+
194
+ # `@rbs *x: T`
195
+ class SplatParamType < SpecialVarTypeAnnotation
196
+ end
197
+
198
+ # `@rbs` **x: T
199
+ class DoubleSplatParamType < SpecialVarTypeAnnotation
200
+ end
201
+
202
+ # `@rbs &block: METHOD-TYPE` or `@rbs &block: ? METHOD-TYPE`
203
+ class BlockType < Base
204
+ attr_reader :name #: Symbol?
205
+
206
+ attr_reader :type #: Types::Block?
207
+
208
+ attr_reader :comment #: String?
209
+
210
+ attr_reader :type_source #: String
211
+
212
+ # @rbs override
213
+ def initialize(tree, source)
214
+ @tree = tree
215
+ @source = source
216
+
217
+ annotation = tree.nth_tree!(1)
218
+
219
+ if name = annotation.nth_token?(1)
220
+ @name = name[1].to_sym
221
+ end
222
+
223
+ optional = annotation.nth_token?(3)
224
+
225
+ if type_token = annotation.nth_token?(4)
226
+ block_src = type_token[1]
227
+
228
+ proc_type = ::RBS::Parser.parse_type("^" + block_src, require_eof: true) rescue RBS::ParsingError
229
+ if proc_type.is_a?(Types::Proc)
230
+ @type = Types::Block.new(
231
+ type: proc_type.type,
232
+ required: !optional,
233
+ self_type: proc_type.self_type
234
+ )
235
+ end
236
+
237
+ @type_source = block_src
238
+ else
239
+ @type = nil
240
+ @type_source = ""
241
+ end
242
+
243
+ if comment = annotation.nth_tree(5)
244
+ @comment = comment.to_s
245
+ end
246
+ end
247
+ end
248
+
249
+ # `@rbs return: T`
79
250
  class ReturnType < Base
80
- attr_reader :type #:: Types::t?
251
+ attr_reader :type #: Types::t?
81
252
 
82
- attr_reader :comment #:: String?
253
+ attr_reader :comment #: String?
83
254
 
84
255
  # @rbs override
85
256
  def initialize(tree, source)
@@ -88,16 +259,16 @@ module RBS
88
259
 
89
260
  return_type_decl = tree.nth_tree!(1)
90
261
 
91
- if type = return_type_decl.nth_type?(1)
262
+ if type = return_type_decl.nth_type?(2)
92
263
  @type = type
93
264
  end
94
265
 
95
- if comment = return_type_decl.nth_tree(2)
266
+ if comment = return_type_decl.nth_tree(3)
96
267
  @comment = comment.to_s
97
268
  end
98
269
  end
99
270
 
100
- # @rbs returns bool
271
+ # @rbs return: bool
101
272
  def complete?
102
273
  if type
103
274
  true
@@ -110,13 +281,13 @@ module RBS
110
281
  # `@rbs @foo: T` or `@rbs self.@foo: T`
111
282
  #
112
283
  class IvarType < Base
113
- attr_reader :name #:: Symbol
284
+ attr_reader :name #: Symbol
114
285
 
115
- attr_reader :type #:: Types::t?
286
+ attr_reader :type #: Types::t?
116
287
 
117
- attr_reader :class_instance #:: bool
288
+ attr_reader :class_instance #: bool
118
289
 
119
- attr_reader :comment #:: String?
290
+ attr_reader :comment #: String?
120
291
 
121
292
  # @rbs override
122
293
  def initialize(tree, source)
@@ -134,54 +305,62 @@ module RBS
134
305
  end
135
306
  end
136
307
 
137
- # `#:: TYPE`
138
- #
139
- class Assertion < Base
140
- attr_reader :type #:: Types::t | MethodType | nil
308
+ class MethodTypeAssertion < Base
309
+ attr_reader :method_type #: MethodType
141
310
 
311
+ # @rbs override
142
312
  def initialize(tree, source)
143
313
  @source = source
144
314
  @tree = tree
145
315
 
146
- @type = tree.nth_method_type?(1) || tree.nth_type?(1)
316
+ @method_type = tree.nth_method_type!(1)
147
317
  end
148
318
 
149
- # @rbs returns bool
150
- def complete?
151
- if type
152
- true
153
- else
154
- false
155
- end
319
+ def type_source #: String
320
+ method_type.location&.source || raise
156
321
  end
322
+ end
157
323
 
158
- # Returns a type if it's type
159
- #
160
- def type? #:: Types::t?
161
- case type
162
- when MethodType, nil
163
- nil
164
- else
165
- type
166
- end
324
+ class TypeAssertion < Base
325
+ attr_reader :type #: Types::t
326
+
327
+ # @rbs override
328
+ def initialize(tree, source)
329
+ @source = source
330
+ @tree = tree
331
+
332
+ @type = tree.nth_type!(1)
167
333
  end
168
334
 
169
- # Returns a method type if it's a method type
170
- #
171
- def method_type? #:: MethodType?
172
- case type
173
- when MethodType
174
- type
175
- else
176
- nil
177
- end
335
+ def type_source #: String
336
+ type.location&.source || raise
337
+ end
338
+ end
339
+
340
+ class SyntaxErrorAssertion < Base
341
+ attr_reader :error_string #: String
342
+
343
+ # @rbs override
344
+ def initialize(tree, source)
345
+ @tree = tree
346
+ @source = source
347
+
348
+ @error_string = tree.nth_tree(1).to_s
349
+ end
350
+ end
351
+
352
+ class Dot3Assertion < Base
353
+ # @rbs override
354
+ def initialize(tree, source)
355
+ @tree = tree
356
+ @source = source
178
357
  end
179
358
  end
180
359
 
181
360
  # `#[TYPE, ..., TYPE]`
182
361
  #
183
362
  class Application < Base
184
- attr_reader :types #:: Array[Types::t]?
363
+ attr_reader :types #: Array[Types::t]?
185
364
 
186
365
  # @rbs override
187
366
  def initialize(tree, source)
@@ -204,63 +383,15 @@ module RBS
204
383
  end
205
384
  end
206
385
 
207
- # @rbs returns bool
386
+ # @rbs return: bool
208
387
  def complete?
209
388
  types ? true : false
210
389
  end
211
390
  end
212
391
 
213
- # `# @rbs yields () -> void -- Comment`
214
- #
215
- class Yields < Base
216
- # The type of block
217
- #
218
- # * Types::Block when syntactically correct input is given
219
- # * String when syntax error is reported
220
- # * `nil` when nothing is given
221
- #
222
- attr_reader :block_type #:: Types::Block | String | nil
223
-
224
- # The content of the comment or `nil`
225
- #
226
- attr_reader :comment #:: String?
227
-
228
- # If `[optional]` token is inserted just after `yields` token
229
- #
230
- # The Types::Block instance has correct `required` attribute based on the `[optional]` token.
231
- # This is for the other cases, syntax error or omitted.
232
- #
233
- attr_reader :optional #:: bool
234
-
235
- # @rbs override
236
- def initialize(tree, comments)
237
- @tree = tree
238
- @source = comments
239
-
240
- yields_tree = tree.nth_tree!(1)
241
- @optional = yields_tree.nth_token?(1).is_a?(Array)
242
- if block_token = yields_tree.nth_token(2)
243
- block_src = block_token[1]
244
- proc_src = "^" + block_src
245
- proc_type = ::RBS::Parser.parse_type(proc_src, require_eof: true) rescue RBS::ParsingError
246
- if proc_type.is_a?(Types::Proc)
247
- @block_type = Types::Block.new(
248
- type: proc_type.type,
249
- required: !optional,
250
- self_type: proc_type.self_type
251
- )
252
- else
253
- @block_type = block_src
254
- end
255
- end
256
-
257
- @comment = yields_tree.nth_tree?(3)&.to_s
258
- end
259
- end
260
-
261
392
  # `# @rbs %a{a} %a{a} ...`
262
393
  class RBSAnnotation < Base
263
- attr_reader :contents #:: Array[String]
394
+ attr_reader :contents #: Array[String]
264
395
 
265
396
  # @rbs override
266
397
  def initialize(tree, comments)
@@ -273,6 +404,12 @@ module RBS
273
404
  token[1]
274
405
  end
275
406
  end
407
+
408
+ def annotations #: Array[RBS::AST::Annotation]
409
+ contents.map do |content|
410
+ RBS::AST::Annotation.new(string: content[3..-2] || raise, location: nil)
411
+ end
412
+ end
276
413
  end
277
414
 
278
415
  # `# @rbs skip`
@@ -288,8 +425,8 @@ module RBS
288
425
  # `# @rbs inherits T`
289
426
  #
290
427
  class Inherits < Base
291
- attr_reader :super_name #:: TypeName?
292
- attr_reader :args #:: Array[Types::t]?
428
+ attr_reader :super_name #: TypeName?
429
+ attr_reader :args #: Array[Types::t]?
293
430
 
294
431
  # @rbs override
295
432
  def initialize(tree, source)
@@ -320,7 +457,7 @@ module RBS
320
457
 
321
458
  # `# @rbs use [USES]`
322
459
  class Use < Base
323
- attr_reader :clauses #:: Array[RBS::AST::Directives::Use::clause]
460
+ attr_reader :clauses #: Array[RBS::AST::Directives::Use::clause]
324
461
 
325
462
  # @rbs override
326
463
  def initialize(tree, source)
@@ -372,9 +509,9 @@ module RBS
372
509
 
373
510
  # `# @rbs module-self [MODULE_SELF]`
374
511
  class ModuleSelf < Base
375
- attr_reader :constraint #:: RBS::AST::Declarations::Module::Self?
512
+ attr_reader :constraint #: RBS::AST::Declarations::Module::Self?
376
513
 
377
- attr_reader :comment #:: String?
514
+ attr_reader :comment #: String?
378
515
 
379
516
  # @rbs override
380
517
  def initialize(tree, source)
@@ -410,9 +547,11 @@ module RBS
410
547
  class Generic < Base
411
548
  # TypeParam object or `nil` if syntax error
412
549
  #
413
- attr_reader :type_param #:: RBS::AST::TypeParam?
550
+ attr_reader :type_param #: RBS::AST::TypeParam?
414
551
 
415
- attr_reader :comment #:: String?
552
+ attr_reader :comment #: String?
553
+
554
+ include Utils
416
555
 
417
556
  # @rbs override
418
557
  def initialize(tree, source)
@@ -420,38 +559,10 @@ module RBS
420
559
  @source = source
421
560
 
422
561
  generic_tree = tree.nth_tree!(1)
423
- unchecked = generic_tree.nth_token?(1) != nil
424
- inout =
425
- case generic_tree.nth_token?(2)&.[](0)
426
- when nil
427
- :invariant
428
- when :kIN
429
- :contravariant
430
- when :kOUT
431
- :covariant
432
- end #: RBS::AST::TypeParam::variance
433
562
 
434
- name = generic_tree.nth_token?(3)&.last
435
-
436
- if bound = generic_tree.nth_tree?(4)
437
- if type = bound.nth_type?(1)
438
- case type
439
- when Types::ClassSingleton, Types::ClassInstance, Types::Interface
440
- upper_bound = type
441
- end
442
- end
443
- end
563
+ @type_param = translate_type_param(generic_tree.nth_tree!(1))
444
564
 
445
- if name
446
- @type_param = RBS::AST::TypeParam.new(
447
- name: name.to_sym,
448
- variance: inout,
449
- upper_bound: upper_bound,
450
- location: nil
451
- ).unchecked!(unchecked)
452
- end
453
-
454
- if comment = generic_tree.nth_tree?(5)
565
+ if comment = generic_tree.nth_tree?(2)
455
566
  @comment = comment.to_s
456
567
  end
457
568
  end
@@ -459,7 +570,7 @@ module RBS
459
570
 
460
571
  # `# @rbs!` annotation
461
572
  class Embedded < Base
462
- attr_reader :content #:: String
573
+ attr_reader :content #: String
463
574
 
464
575
  # @rbs override
465
576
  def initialize(tree, source)
@@ -469,6 +580,190 @@ module RBS
469
580
  @content = tree.nth_token!(1)[1]
470
581
  end
471
582
  end
583
+
584
+ # `@rbs METHOD-TYPE``
585
+ #
586
+ class Method < Base
587
+ # @rbs! type method_type = [MethodType, method_type?] | String
588
+
589
+ attr_reader :method_types #: method_type?
590
+
591
+ # `true` if the method definition is overloading something
592
+ #
593
+ attr_reader :overloading #: bool
594
+
595
+ attr_reader :type #: MethodType?
596
+
597
+ attr_reader :method_type_source #: String
598
+
599
+ # @rbs override
600
+ def initialize(tree, source)
601
+ @tree = tree
602
+ @source = source
603
+
604
+ overloads = tree.nth_tree!(1)
605
+ @method_types = construct_method_types(overloads.non_trivia_trees.dup)
606
+ @overloading = overloads.token?(:kDOT3, at: -1)
607
+ end
608
+
609
+ #: (Array[tree]) -> method_type?
610
+ def construct_method_types(children)
611
+ first = children.shift
612
+
613
+ case first
614
+ when MethodType
615
+ children.shift
616
+ [
617
+ first,
618
+ construct_method_types(children)
619
+ ]
620
+ when Array
621
+ # `...`
622
+ nil
623
+ when AST::Tree
624
+ # Syntax error
625
+ first.to_s
626
+ end
627
+ end
628
+
629
+ # @rbs () { (MethodType) -> void } -> void
630
+ # | () -> Enumerator[MethodType, void]
631
+ def each_method_type
632
+ if block_given?
633
+ type = self.method_types
634
+
635
+ while true
636
+ if type.is_a?(Array)
637
+ yield type[0]
638
+ type = type[1]
639
+ else
640
+ break
641
+ end
642
+ end
643
+ else
644
+ enum_for :each_method_type
645
+ end
646
+ end
647
+
648
+ # Returns the parsing error overload string
649
+ #
650
+ # Returns `nil` if no parsing error found.
651
+ #
652
+ def error_source #: String?
653
+ type = self.method_types
654
+
655
+ while true
656
+ if type.is_a?(Array)
657
+ type = type[1]
658
+ else
659
+ break
660
+ end
661
+ end
662
+
663
+ if type.is_a?(String)
664
+ type
665
+ end
666
+ end
667
+ end
668
+
669
+ # `@rbs module Foo`
670
+ class ModuleDecl < Base
671
+ attr_reader :name #: TypeName?
672
+
673
+ attr_reader :type_params #: Array[RBS::AST::TypeParam]
674
+
675
+ attr_reader :self_types #: Array[RBS::AST::Declarations::Module::Self]
676
+
677
+ include Utils
678
+
679
+ # @rbs override
680
+ def initialize(tree, comments)
681
+ @tree = tree
682
+ @source = comments
683
+
684
+ decl_tree = tree.nth_tree!(1)
685
+
686
+ if type_name = translate_type_name(decl_tree.nth_tree!(1))
687
+ if type_name.class?
688
+ @name = type_name
689
+ end
690
+ end
691
+
692
+ @type_params = []
693
+ if type_params = decl_tree.nth_tree(2)
694
+ params = type_params.non_trivia_trees
695
+ params.shift
696
+ params.pop
697
+ params.each_slice(2) do |param, _comma|
698
+ raise unless param.is_a?(Tree)
699
+ if type_param = translate_type_param(param)
700
+ @type_params << type_param
701
+ end
702
+ end
703
+ end
704
+
705
+ @self_types = []
706
+ if selfs_tree = decl_tree.nth_tree(3)
707
+ self_trees = selfs_tree.non_trivia_trees
708
+ self_trees.shift
709
+
710
+ self_trees.each_slice(2) do |type, _comma|
711
+ case type
712
+ when Types::ClassInstance, Types::Interface
713
+ @self_types << RBS::AST::Declarations::Module::Self.new(
714
+ name: type.name,
715
+ args: type.args,
716
+ location: type.location
717
+ )
718
+ end
719
+ end
720
+ end
721
+ end
722
+ end
723
+
724
+ # `@rbs class Foo`
725
+ class ClassDecl < Base
726
+ attr_reader :name #: TypeName?
727
+
728
+ attr_reader :type_params #: Array[RBS::AST::TypeParam]
729
+
730
+ attr_reader :super_class #: RBS::AST::Declarations::Class::Super?
731
+
732
+ include Utils
733
+
734
+ # @rbs override
735
+ def initialize(tree, comments)
736
+ @tree = tree
737
+ @source = comments
738
+
739
+ decl_tree = tree.nth_tree!(1)
740
+
741
+ if type_name = translate_type_name(decl_tree.nth_tree!(1))
742
+ if type_name.class?
743
+ @name = type_name
744
+ end
745
+ end
746
+
747
+ @type_params = []
748
+ if type_params = decl_tree.nth_tree(2)
749
+ params = type_params.non_trivia_trees
750
+ params.shift
751
+ params.pop
752
+ params.each_slice(2) do |param, _comma|
753
+ raise unless param.is_a?(Tree)
754
+ if type_param = translate_type_param(param)
755
+ @type_params << type_param
756
+ end
757
+ end
758
+ end
759
+
760
+ if super_tree = decl_tree.nth_tree(3)
761
+ if type = super_tree.nth_type?(1)
762
+ @super_class = translate_super_class(type)
763
+ end
764
+ end
765
+ end
766
+ end
472
767
  end
473
768
  end
474
769
  end