rbs 0.19.0 → 0.20.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.
@@ -20,7 +20,7 @@ class RBS::Parser
20
20
  nonassoc kARROW
21
21
  preclow
22
22
 
23
- expect 2
23
+ expect 5
24
24
 
25
25
  rule
26
26
 
@@ -202,57 +202,67 @@ rule
202
202
  | alias_member
203
203
  | signature
204
204
 
205
+ attribute_kind:
206
+ { result = :instance }
207
+ | kSELF kDOT { result = :singleton }
208
+
205
209
  attribute_member:
206
- annotations kATTRREADER keyword type {
207
- location = val[1].location + val[3].location
208
- result = Members::AttrReader.new(name: val[2].value,
210
+ annotations kATTRREADER attribute_kind keyword type {
211
+ location = val[1].location + val[4].location
212
+ result = Members::AttrReader.new(name: val[3].value,
209
213
  ivar_name: nil,
210
- type: val[3],
214
+ type: val[4],
215
+ kind: val[2],
211
216
  annotations: val[0],
212
217
  location: location,
213
218
  comment: leading_comment(val[0].first&.location || location))
214
219
  }
215
- | annotations kATTRREADER method_name attr_var_opt kCOLON type {
216
- location = val[1].location + val[5].location
217
- result = Members::AttrReader.new(name: val[2].value.to_sym,
218
- ivar_name: val[3],
219
- type: val[5],
220
+ | annotations kATTRREADER attribute_kind method_name attr_var_opt kCOLON type {
221
+ location = val[1].location + val[6].location
222
+ result = Members::AttrReader.new(name: val[3].value.to_sym,
223
+ ivar_name: val[4],
224
+ type: val[6],
225
+ kind: val[2],
220
226
  annotations: val[0],
221
227
  location: location,
222
228
  comment: leading_comment(val[0].first&.location || location))
223
229
  }
224
- | annotations kATTRWRITER keyword type {
225
- location = val[1].location + val[3].location
226
- result = Members::AttrWriter.new(name: val[2].value,
230
+ | annotations kATTRWRITER attribute_kind keyword type {
231
+ location = val[1].location + val[4].location
232
+ result = Members::AttrWriter.new(name: val[3].value,
227
233
  ivar_name: nil,
228
- type: val[3],
234
+ kind: val[2],
235
+ type: val[4],
229
236
  annotations: val[0],
230
237
  location: location,
231
238
  comment: leading_comment(val[0].first&.location || location))
232
239
  }
233
- | annotations kATTRWRITER method_name attr_var_opt kCOLON type {
234
- location = val[1].location + val[5].location
235
- result = Members::AttrWriter.new(name: val[2].value.to_sym,
236
- ivar_name: val[3],
237
- type: val[5],
240
+ | annotations kATTRWRITER attribute_kind method_name attr_var_opt kCOLON type {
241
+ location = val[1].location + val[6].location
242
+ result = Members::AttrWriter.new(name: val[3].value.to_sym,
243
+ ivar_name: val[4],
244
+ kind: val[2],
245
+ type: val[6],
238
246
  annotations: val[0],
239
247
  location: location,
240
248
  comment: leading_comment(val[0].first&.location || location))
241
249
  }
242
- | annotations kATTRACCESSOR keyword type {
243
- location = val[1].location + val[3].location
244
- result = Members::AttrAccessor.new(name: val[2].value,
250
+ | annotations kATTRACCESSOR attribute_kind keyword type {
251
+ location = val[1].location + val[4].location
252
+ result = Members::AttrAccessor.new(name: val[3].value,
245
253
  ivar_name: nil,
246
- type: val[3],
254
+ kind: val[2],
255
+ type: val[4],
247
256
  annotations: val[0],
248
257
  location: location,
249
258
  comment: leading_comment(val[0].first&.location || location))
250
259
  }
251
- | annotations kATTRACCESSOR method_name attr_var_opt kCOLON type {
252
- location = val[1].location + val[5].location
253
- result = Members::AttrAccessor.new(name: val[2].value.to_sym,
254
- ivar_name: val[3],
255
- type: val[5],
260
+ | annotations kATTRACCESSOR attribute_kind method_name attr_var_opt kCOLON type {
261
+ location = val[1].location + val[6].location
262
+ result = Members::AttrAccessor.new(name: val[3].value.to_sym,
263
+ ivar_name: val[4],
264
+ kind: val[2],
265
+ type: val[6],
256
266
  annotations: val[0],
257
267
  location: location,
258
268
  comment: leading_comment(val[0].first&.location || location))
@@ -474,26 +484,13 @@ rule
474
484
  }
475
485
 
476
486
  method_type:
477
- start_merged_scope type_params params_opt block_opt kARROW simple_type {
487
+ start_merged_scope type_params proc_type {
478
488
  reset_variable_scope
479
489
 
480
- location = (val[1] || val[2] || val[3] || val[4]).location + val[5].location
490
+ location = (val[1] || val[2]).location + val[2].location
481
491
  type_params = val[1]&.value || []
482
492
 
483
- params = val[2]&.value || empty_params_result
484
-
485
- type = Types::Function.new(
486
- required_positionals: params[0],
487
- optional_positionals: params[1],
488
- rest_positionals: params[2],
489
- trailing_positionals: params[3],
490
- required_keywords: params[4],
491
- optional_keywords: params[5],
492
- rest_keywords: params[6],
493
- return_type: val[5]
494
- )
495
-
496
- block = val[3]&.value
493
+ type, block = val[2].value
497
494
 
498
495
  result = MethodType.new(type_params: type_params,
499
496
  type: type,
@@ -507,14 +504,13 @@ rule
507
504
  result = LocatedValue.new(value: val[1], location: val[0].location + val[2].location)
508
505
  }
509
506
 
510
- block_opt:
511
- { result = nil }
512
- | kLBRACE function_type kRBRACE {
513
- block = MethodType::Block.new(type: val[1].value, required: true)
507
+ block:
508
+ kLBRACE simple_function_type kRBRACE {
509
+ block = Types::Block.new(type: val[1].value, required: true)
514
510
  result = LocatedValue.new(value: block, location: val[0].location + val[2].location)
515
511
  }
516
- | kQUESTION kLBRACE function_type kRBRACE {
517
- block = MethodType::Block.new(type: val[2].value, required: false)
512
+ | kQUESTION kLBRACE simple_function_type kRBRACE {
513
+ block = Types::Block.new(type: val[2].value, required: false)
518
514
  result = LocatedValue.new(value: block, location: val[0].location + val[3].location)
519
515
  }
520
516
 
@@ -797,8 +793,9 @@ rule
797
793
  result = Types::ClassSingleton.new(name: val[2].value,
798
794
  location: val[0].location + val[3].location)
799
795
  }
800
- | kHAT function_type {
801
- result = Types::Proc.new(type: val[1].value, location: val[0].location + val[1].location)
796
+ | kHAT proc_type {
797
+ type, block = val[1].value
798
+ result = Types::Proc.new(type: type, block: block, location: val[0].location + val[1].location)
802
799
  }
803
800
  | simple_type kQUESTION {
804
801
  result = Types::Optional.new(type: val[0], location: val[0].location + val[1].location)
@@ -851,7 +848,32 @@ rule
851
848
 
852
849
  keyword: tLKEYWORD | tUKEYWORD | tLKEYWORD_Q_E | tUKEYWORD_Q_E
853
850
 
854
- function_type:
851
+ proc_type:
852
+ params_opt block kARROW simple_type {
853
+ location = (val[0] || val[1] || val[2]).location + val[3].location
854
+
855
+ params = val[0]&.value || [[], [], nil, [], {}, {}, nil]
856
+
857
+ type = Types::Function.new(
858
+ required_positionals: params[0],
859
+ optional_positionals: params[1],
860
+ rest_positionals: params[2],
861
+ trailing_positionals: params[3],
862
+ required_keywords: params[4],
863
+ optional_keywords: params[5],
864
+ rest_keywords: params[6],
865
+ return_type: val[3]
866
+ )
867
+
868
+ block = val[1].value
869
+
870
+ result = LocatedValue.new(value: [type, block], location: location)
871
+ }
872
+ | simple_function_type {
873
+ result = LocatedValue.new(value: [val[0].value, nil], location: val[0].location)
874
+ }
875
+
876
+ simple_function_type:
855
877
  kLPAREN params kRPAREN kARROW simple_type {
856
878
  location = val[0].location + val[4].location
857
879
  type = Types::Function.new(
@@ -883,7 +905,7 @@ rule
883
905
  result = LocatedValue.new(value: type, location: location)
884
906
  }
885
907
 
886
- params:
908
+ params:
887
909
  required_positional kCOMMA params {
888
910
  result = val[2]
889
911
  result[0].unshift(val[0])
@@ -1294,6 +1316,10 @@ ANNOTATION_RE = Regexp.union(/%a\{.*?\}/,
1294
1316
  /%a\(.*?\)/,
1295
1317
  /%a\<.*?\>/,
1296
1318
  /%a\|.*?\|/)
1319
+
1320
+ escape_sequences = %w[a b e f n r s t v "].map { |l| "\\\\#{l}" }
1321
+ DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE = /(#{escape_sequences.join("|")})/
1322
+
1297
1323
  def next_token
1298
1324
  if @type
1299
1325
  type = @type
@@ -1373,7 +1399,21 @@ def next_token
1373
1399
  when input.scan(/[a-z_]\w*\b/)
1374
1400
  new_token(:tLIDENT)
1375
1401
  when input.scan(/"(\\"|[^"])*"/)
1376
- s = input.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\"/, '"')
1402
+ s = input.matched.yield_self {|s| s[1, s.length - 2] }
1403
+ .gsub(DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE) do |match|
1404
+ case match
1405
+ when '\\a' then "\a"
1406
+ when '\\b' then "\b"
1407
+ when '\\e' then "\e"
1408
+ when '\\f' then "\f"
1409
+ when '\\n' then "\n"
1410
+ when '\\r' then "\r"
1411
+ when '\\s' then "\s"
1412
+ when '\\t' then "\t"
1413
+ when '\\v' then "\v"
1414
+ when '\\"' then '"'
1415
+ end
1416
+ end
1377
1417
  new_token(:tSTRING, s)
1378
1418
  when input.scan(/'(\\'|[^'])*'/)
1379
1419
  s = input.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\'/, "'")
@@ -15,6 +15,14 @@ module RBS
15
15
  :instance
16
16
  end
17
17
  end
18
+
19
+ def attribute_kind
20
+ if singleton
21
+ :singleton
22
+ else
23
+ :instance
24
+ end
25
+ end
18
26
  end
19
27
 
20
28
  attr_reader :source_decls
@@ -202,6 +210,7 @@ module RBS
202
210
  name: name,
203
211
  ivar_name: nil,
204
212
  type: Types::Bases::Any.new(location: nil),
213
+ kind: context.attribute_kind,
205
214
  location: nil,
206
215
  comment: comments[node.first_lineno - 1],
207
216
  annotations: []
@@ -215,6 +224,7 @@ module RBS
215
224
  name: name,
216
225
  ivar_name: nil,
217
226
  type: Types::Bases::Any.new(location: nil),
227
+ kind: context.attribute_kind,
218
228
  location: nil,
219
229
  comment: comments[node.first_lineno - 1],
220
230
  annotations: []
@@ -228,6 +238,7 @@ module RBS
228
238
  name: name,
229
239
  ivar_name: nil,
230
240
  type: Types::Bases::Any.new(location: nil),
241
+ kind: context.attribute_kind,
231
242
  location: nil,
232
243
  comment: comments[node.first_lineno - 1],
233
244
  annotations: []
@@ -549,7 +560,7 @@ module RBS
549
560
  method_block = nil
550
561
 
551
562
  if block
552
- method_block = MethodType::Block.new(
563
+ method_block = Types::Block.new(
553
564
  required: true,
554
565
  type: Types::Function.empty(untyped)
555
566
  )
@@ -557,7 +568,7 @@ module RBS
557
568
 
558
569
  if body_node
559
570
  if (yields = any_node?(body_node) {|n| n.type == :YIELD })
560
- method_block = MethodType::Block.new(
571
+ method_block = Types::Block.new(
561
572
  required: true,
562
573
  type: Types::Function.empty(untyped)
563
574
  )
@@ -387,19 +387,19 @@ module RBS
387
387
  if block
388
388
  if (type = vars[block])
389
389
  if type.is_a?(Types::Proc)
390
- method_block = MethodType::Block.new(required: true, type: type.type)
390
+ method_block = Types::Block.new(required: true, type: type.type)
391
391
  elsif type.is_a?(Types::Bases::Any)
392
- method_block = MethodType::Block.new(
392
+ method_block = Types::Block.new(
393
393
  required: true,
394
394
  type: Types::Function.empty(Types::Bases::Any.new(location: nil))
395
395
  )
396
396
  # Handle an optional block like `T.nilable(T.proc.void)`.
397
397
  elsif type.is_a?(Types::Optional) && type.type.is_a?(Types::Proc)
398
- method_block = MethodType::Block.new(required: false, type: type.type.type)
398
+ method_block = Types::Block.new(required: false, type: type.type.type)
399
399
  else
400
400
  STDERR.puts "Unexpected block type: #{type}"
401
401
  PP.pp args_node, STDERR
402
- method_block = MethodType::Block.new(
402
+ method_block = Types::Block.new(
403
403
  required: true,
404
404
  type: Types::Function.empty(Types::Bases::Any.new(location: nil))
405
405
  )
@@ -485,7 +485,7 @@ module RBS
485
485
  Types::Tuple.new(types: types, location: nil)
486
486
  else
487
487
  if proc_type?(type_node)
488
- Types::Proc.new(type: method_type(nil, type_node, variables: variables).type, location: nil)
488
+ Types::Proc.new(type: method_type(nil, type_node, variables: variables).type, block: nil, location: nil)
489
489
  else
490
490
  STDERR.puts "Unexpected type_node:"
491
491
  PP.pp type_node, STDERR
@@ -103,7 +103,7 @@ module RBS
103
103
  when :keyrest
104
104
  rest_keywords = Types::Function::Param.new(name: nil, type: untyped)
105
105
  when :block
106
- block = MethodType::Block.new(
106
+ block = Types::Block.new(
107
107
  type: Types::Function.empty(untyped).update(rest_positionals: Types::Function::Param.new(name: nil, type: untyped)),
108
108
  required: true
109
109
  )
@@ -81,7 +81,8 @@ def #{with_name}(*args, &block)
81
81
 
82
82
  if block_given?
83
83
  receiver = self
84
- result = __send__(:"#{without_name}", *args) do |*block_args|
84
+
85
+ wrapped_block = proc do |*block_args|
85
86
  return_from_block = false
86
87
 
87
88
  begin
@@ -117,7 +118,9 @@ def #{with_name}(*args, &block)
117
118
  end
118
119
 
119
120
  block_result
120
- end
121
+ end.ruby2_keywords
122
+
123
+ result = __send__(:"#{without_name}", *args, &wrapped_block)
121
124
  else
122
125
  result = __send__(:"#{without_name}", *args)
123
126
  end
@@ -947,44 +947,100 @@ module RBS
947
947
  end
948
948
  end
949
949
 
950
+ class Block
951
+ attr_reader :type
952
+ attr_reader :required
953
+
954
+ def initialize(type:, required:)
955
+ @type = type
956
+ @required = required
957
+ end
958
+
959
+ def ==(other)
960
+ other.is_a?(Block) &&
961
+ other.type == type &&
962
+ other.required == required
963
+ end
964
+
965
+ def to_json(*a)
966
+ {
967
+ type: type,
968
+ required: required
969
+ }.to_json(*a)
970
+ end
971
+
972
+ def sub(s)
973
+ self.class.new(
974
+ type: type.sub(s),
975
+ required: required
976
+ )
977
+ end
978
+
979
+ def map_type(&block)
980
+ Block.new(
981
+ required: required,
982
+ type: type.map_type(&block)
983
+ )
984
+ end
985
+ end
986
+
950
987
  class Proc
951
988
  attr_reader :type
989
+ attr_reader :block
952
990
  attr_reader :location
953
991
 
954
- def initialize(location:, type:)
992
+ def initialize(location:, type:, block:)
955
993
  @type = type
994
+ @block = block
956
995
  @location = location
957
996
  end
958
997
 
959
998
  def ==(other)
960
- other.is_a?(Proc) && other.type == type
999
+ other.is_a?(Proc) && other.type == type && other.block == block
961
1000
  end
962
1001
 
963
1002
  alias eql? ==
964
1003
 
965
1004
  def hash
966
- self.class.hash ^ type.hash
1005
+ self.class.hash ^ type.hash ^ block.hash
967
1006
  end
968
1007
 
969
1008
  def free_variables(set = Set[])
970
1009
  type.free_variables(set)
1010
+ block&.type&.free_variables(set)
1011
+ set
971
1012
  end
972
1013
 
973
1014
  def to_json(*a)
974
- { class: :proc, type: type, location: location }.to_json(*a)
1015
+ {
1016
+ class: :proc,
1017
+ type: type,
1018
+ block: block,
1019
+ location: location
1020
+ }.to_json(*a)
975
1021
  end
976
1022
 
977
1023
  def sub(s)
978
- self.class.new(type: type.sub(s), location: location)
1024
+ self.class.new(type: type.sub(s), block: block&.sub(s), location: location)
979
1025
  end
980
1026
 
981
1027
  def to_s(level = 0)
982
- "^(#{type.param_to_s}) -> #{type.return_to_s}".lstrip
1028
+ case
1029
+ when b = block
1030
+ if b.required
1031
+ "^(#{type.param_to_s}) { (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
1032
+ else
1033
+ "^(#{type.param_to_s}) ?{ (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
1034
+ end
1035
+ else
1036
+ "^(#{type.param_to_s}) -> #{type.return_to_s}"
1037
+ end
983
1038
  end
984
1039
 
985
1040
  def each_type(&block)
986
1041
  if block
987
1042
  type.each_type(&block)
1043
+ self.block&.type&.each_type(&block)
988
1044
  else
989
1045
  enum_for :each_type
990
1046
  end
@@ -993,6 +1049,7 @@ module RBS
993
1049
  def map_type_name(&block)
994
1050
  Proc.new(
995
1051
  type: type.map_type_name(&block),
1052
+ block: self.block&.map_type {|type| type.map_type_name(&block) },
996
1053
  location: location
997
1054
  )
998
1055
  end