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.
@@ -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)
@@ -288,8 +419,8 @@ module RBS
288
419
  # `# @rbs inherits T`
289
420
  #
290
421
  class Inherits < Base
291
- attr_reader :super_name #:: TypeName?
292
- attr_reader :args #:: Array[Types::t]?
422
+ attr_reader :super_name #: TypeName?
423
+ attr_reader :args #: Array[Types::t]?
293
424
 
294
425
  # @rbs override
295
426
  def initialize(tree, source)
@@ -320,7 +451,7 @@ module RBS
320
451
 
321
452
  # `# @rbs use [USES]`
322
453
  class Use < Base
323
- attr_reader :clauses #:: Array[RBS::AST::Directives::Use::clause]
454
+ attr_reader :clauses #: Array[RBS::AST::Directives::Use::clause]
324
455
 
325
456
  # @rbs override
326
457
  def initialize(tree, source)
@@ -372,9 +503,9 @@ module RBS
372
503
 
373
504
  # `# @rbs module-self [MODULE_SELF]`
374
505
  class ModuleSelf < Base
375
- attr_reader :constraint #:: RBS::AST::Declarations::Module::Self?
506
+ attr_reader :constraint #: RBS::AST::Declarations::Module::Self?
376
507
 
377
- attr_reader :comment #:: String?
508
+ attr_reader :comment #: String?
378
509
 
379
510
  # @rbs override
380
511
  def initialize(tree, source)
@@ -410,9 +541,11 @@ module RBS
410
541
  class Generic < Base
411
542
  # TypeParam object or `nil` if syntax error
412
543
  #
413
- attr_reader :type_param #:: RBS::AST::TypeParam?
544
+ attr_reader :type_param #: RBS::AST::TypeParam?
545
+
546
+ attr_reader :comment #: String?
414
547
 
415
- attr_reader :comment #:: String?
548
+ include Utils
416
549
 
417
550
  # @rbs override
418
551
  def initialize(tree, source)
@@ -420,38 +553,10 @@ module RBS
420
553
  @source = source
421
554
 
422
555
  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
-
434
- name = generic_tree.nth_token?(3)&.last
435
556
 
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
557
+ @type_param = translate_type_param(generic_tree.nth_tree!(1))
444
558
 
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)
559
+ if comment = generic_tree.nth_tree?(2)
455
560
  @comment = comment.to_s
456
561
  end
457
562
  end
@@ -459,7 +564,7 @@ module RBS
459
564
 
460
565
  # `# @rbs!` annotation
461
566
  class Embedded < Base
462
- attr_reader :content #:: String
567
+ attr_reader :content #: String
463
568
 
464
569
  # @rbs override
465
570
  def initialize(tree, source)
@@ -469,6 +574,190 @@ module RBS
469
574
  @content = tree.nth_token!(1)[1]
470
575
  end
471
576
  end
577
+
578
+ # `@rbs METHOD-TYPE``
579
+ #
580
+ class Method < Base
581
+ # @rbs! type method_type = [MethodType, method_type?] | String
582
+
583
+ attr_reader :method_types #: method_type?
584
+
585
+ # `true` if the method definition is overloading something
586
+ #
587
+ attr_reader :overloading #: bool
588
+
589
+ attr_reader :type #: MethodType?
590
+
591
+ attr_reader :method_type_source #: String
592
+
593
+ # @rbs override
594
+ def initialize(tree, source)
595
+ @tree = tree
596
+ @source = source
597
+
598
+ overloads = tree.nth_tree!(1)
599
+ @method_types = construct_method_types(overloads.non_trivia_trees.dup)
600
+ @overloading = overloads.token?(:kDOT3, at: -1)
601
+ end
602
+
603
+ #: (Array[tree]) -> method_type?
604
+ def construct_method_types(children)
605
+ first = children.shift
606
+
607
+ case first
608
+ when MethodType
609
+ children.shift
610
+ [
611
+ first,
612
+ construct_method_types(children)
613
+ ]
614
+ when Array
615
+ # `...`
616
+ nil
617
+ when AST::Tree
618
+ # Syntax error
619
+ first.to_s
620
+ end
621
+ end
622
+
623
+ # @rbs () { (MethodType) -> void } -> void
624
+ # | () -> Enumerator[MethodType, void]
625
+ def each_method_type
626
+ if block_given?
627
+ type = self.method_types
628
+
629
+ while true
630
+ if type.is_a?(Array)
631
+ yield type[0]
632
+ type = type[1]
633
+ else
634
+ break
635
+ end
636
+ end
637
+ else
638
+ enum_for :each_method_type
639
+ end
640
+ end
641
+
642
+ # Returns the parsing error overload string
643
+ #
644
+ # Returns `nil` if no parsing error found.
645
+ #
646
+ def error_source #: String?
647
+ type = self.method_types
648
+
649
+ while true
650
+ if type.is_a?(Array)
651
+ type = type[1]
652
+ else
653
+ break
654
+ end
655
+ end
656
+
657
+ if type.is_a?(String)
658
+ type
659
+ end
660
+ end
661
+ end
662
+
663
+ # `@rbs module Foo`
664
+ class ModuleDecl < Base
665
+ attr_reader :name #: TypeName?
666
+
667
+ attr_reader :type_params #: Array[RBS::AST::TypeParam]
668
+
669
+ attr_reader :self_types #: Array[RBS::AST::Declarations::Module::Self]
670
+
671
+ include Utils
672
+
673
+ # @rbs override
674
+ def initialize(tree, comments)
675
+ @tree = tree
676
+ @source = comments
677
+
678
+ decl_tree = tree.nth_tree!(1)
679
+
680
+ if type_name = translate_type_name(decl_tree.nth_tree!(1))
681
+ if type_name.class?
682
+ @name = type_name
683
+ end
684
+ end
685
+
686
+ @type_params = []
687
+ if type_params = decl_tree.nth_tree(2)
688
+ params = type_params.non_trivia_trees
689
+ params.shift
690
+ params.pop
691
+ params.each_slice(2) do |param, _comma|
692
+ raise unless param.is_a?(Tree)
693
+ if type_param = translate_type_param(param)
694
+ @type_params << type_param
695
+ end
696
+ end
697
+ end
698
+
699
+ @self_types = []
700
+ if selfs_tree = decl_tree.nth_tree(3)
701
+ self_trees = selfs_tree.non_trivia_trees
702
+ self_trees.shift
703
+
704
+ self_trees.each_slice(2) do |type, _comma|
705
+ case type
706
+ when Types::ClassInstance, Types::Interface
707
+ @self_types << RBS::AST::Declarations::Module::Self.new(
708
+ name: type.name,
709
+ args: type.args,
710
+ location: type.location
711
+ )
712
+ end
713
+ end
714
+ end
715
+ end
716
+ end
717
+
718
+ # `@rbs class Foo`
719
+ class ClassDecl < Base
720
+ attr_reader :name #: TypeName?
721
+
722
+ attr_reader :type_params #: Array[RBS::AST::TypeParam]
723
+
724
+ attr_reader :super_class #: RBS::AST::Declarations::Class::Super?
725
+
726
+ include Utils
727
+
728
+ # @rbs override
729
+ def initialize(tree, comments)
730
+ @tree = tree
731
+ @source = comments
732
+
733
+ decl_tree = tree.nth_tree!(1)
734
+
735
+ if type_name = translate_type_name(decl_tree.nth_tree!(1))
736
+ if type_name.class?
737
+ @name = type_name
738
+ end
739
+ end
740
+
741
+ @type_params = []
742
+ if type_params = decl_tree.nth_tree(2)
743
+ params = type_params.non_trivia_trees
744
+ params.shift
745
+ params.pop
746
+ params.each_slice(2) do |param, _comma|
747
+ raise unless param.is_a?(Tree)
748
+ if type_param = translate_type_param(param)
749
+ @type_params << type_param
750
+ end
751
+ end
752
+ end
753
+
754
+ if super_tree = decl_tree.nth_tree(3)
755
+ if type = super_tree.nth_type?(1)
756
+ @super_class = translate_super_class(type)
757
+ end
758
+ end
759
+ end
760
+ end
472
761
  end
473
762
  end
474
763
  end