rbs 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -0
  3. data/Rakefile +2 -0
  4. data/core/array.rbs +1 -1
  5. data/core/enumerable.rbs +1 -1
  6. data/core/hash.rbs +13 -5
  7. data/core/io.rbs +3 -3
  8. data/core/module.rbs +1 -1
  9. data/core/numeric.rbs +10 -0
  10. data/core/proc.rbs +1 -1
  11. data/core/random.rbs +4 -2
  12. data/core/range.rbs +2 -2
  13. data/core/struct.rbs +3 -2
  14. data/core/thread.rbs +1 -1
  15. data/docs/CONTRIBUTING.md +5 -3
  16. data/docs/sigs.md +18 -1
  17. data/docs/syntax.md +11 -11
  18. data/lib/rbs.rb +1 -0
  19. data/lib/rbs/ast/annotation.rb +2 -2
  20. data/lib/rbs/ast/comment.rb +2 -2
  21. data/lib/rbs/ast/declarations.rb +37 -22
  22. data/lib/rbs/ast/members.rb +26 -26
  23. data/lib/rbs/cli.rb +3 -0
  24. data/lib/rbs/constant_table.rb +4 -1
  25. data/lib/rbs/definition.rb +1 -1
  26. data/lib/rbs/definition_builder.rb +14 -0
  27. data/lib/rbs/definition_builder/ancestor_builder.rb +1 -0
  28. data/lib/rbs/definition_builder/method_builder.rb +4 -2
  29. data/lib/rbs/location.rb +106 -2
  30. data/lib/rbs/locator.rb +205 -0
  31. data/lib/rbs/method_type.rb +2 -2
  32. data/lib/rbs/parser.rb +1050 -713
  33. data/lib/rbs/parser.y +403 -71
  34. data/lib/rbs/test/hook.rb +8 -2
  35. data/lib/rbs/type_name.rb +2 -3
  36. data/lib/rbs/type_name_resolver.rb +1 -1
  37. data/lib/rbs/types.rb +36 -34
  38. data/lib/rbs/version.rb +1 -1
  39. data/lib/rbs/writer.rb +4 -2
  40. data/sig/annotation.rbs +1 -1
  41. data/sig/cli.rbs +31 -21
  42. data/sig/comment.rbs +1 -1
  43. data/sig/declarations.rbs +106 -21
  44. data/sig/environment.rbs +2 -2
  45. data/sig/location.rbs +84 -3
  46. data/sig/locator.rbs +44 -0
  47. data/sig/members.rbs +76 -12
  48. data/sig/method_builder.rbs +1 -1
  49. data/sig/method_types.rbs +1 -1
  50. data/sig/polyfill.rbs +13 -8
  51. data/sig/rbs.rbs +8 -4
  52. data/sig/typename.rbs +1 -1
  53. data/sig/types.rbs +60 -19
  54. data/sig/util.rbs +0 -4
  55. data/sig/writer.rbs +8 -2
  56. data/stdlib/rubygems/0/requirement.rbs +84 -2
  57. data/stdlib/rubygems/0/version.rbs +2 -1
  58. data/stdlib/shellwords/0/shellwords.rbs +252 -0
  59. data/steep/Gemfile.lock +16 -13
  60. metadata +5 -2
data/lib/rbs/parser.y CHANGED
@@ -65,10 +65,21 @@ rule
65
65
  reset_variable_scope
66
66
 
67
67
  location = val[1].location + val[7].location
68
+ location = location.with_children(
69
+ required: {
70
+ keyword: val[1].location,
71
+ name: val[3].location,
72
+ end: val[7].location
73
+ },
74
+ optional: {
75
+ type_params: val[4]&.location,
76
+ lt: val[5]&.location
77
+ }
78
+ )
68
79
  result = Declarations::Class.new(
69
80
  name: val[3].value,
70
81
  type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
71
- super_class: val[5],
82
+ super_class: val[5]&.value,
72
83
  members: val[6],
73
84
  annotations: val[0],
74
85
  location: location,
@@ -79,40 +90,84 @@ rule
79
90
  super_class:
80
91
  { result = nil }
81
92
  | kLT class_name {
82
- result = Declarations::Class::Super.new(name: val[1].value,
83
- args: [],
84
- location: val[1].location)
93
+ loc = val[1].location.with_children(
94
+ required: { name: val[1].location },
95
+ optional: { args: nil }
96
+ )
97
+ sup = Declarations::Class::Super.new(name: val[1].value, args: [], location: loc)
98
+ result = LocatedValue.new(value: sup, location: val[0].location)
85
99
  }
86
100
  | kLT class_name kLBRACKET type_list kRBRACKET {
87
- result = Declarations::Class::Super.new(name: val[1].value,
88
- args: val[3],
89
- location: val[1].location + val[4].location)
101
+ loc = (val[1].location + val[4].location).with_children(
102
+ required: { name: val[1].location },
103
+ optional: { args: val[2].location + val[4].location }
104
+ )
105
+ sup = Declarations::Class::Super.new(name: val[1].value, args: val[3], location: loc)
106
+ result = LocatedValue.new(value: sup, location: val[0].location)
90
107
  }
91
108
 
92
109
  module_decl:
93
110
  annotations kMODULE start_new_scope class_name module_type_params colon_module_self_types class_members kEND {
94
111
  reset_variable_scope
95
112
 
113
+ colon_loc = val[5].location
114
+ self_loc = val[5].value.yield_self do |params|
115
+ case params.size
116
+ when 0
117
+ nil
118
+ when 1
119
+ params[0].location
120
+ else
121
+ params.first.location + params.last.location
122
+ end
123
+ end
124
+
96
125
  location = val[1].location + val[7].location
126
+ location = location.with_children(
127
+ required: {
128
+ keyword: val[1].location,
129
+ name: val[3].location,
130
+ end: val[7].location
131
+ },
132
+ optional: {
133
+ type_params: val[4]&.location,
134
+ colon: colon_loc,
135
+ self_types: self_loc
136
+ }
137
+ )
97
138
  result = Declarations::Module.new(
98
139
  name: val[3].value,
99
140
  type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
100
- self_types: val[5],
141
+ self_types: val[5].value,
101
142
  members: val[6],
102
143
  annotations: val[0],
103
144
  location: location,
104
145
  comment: leading_comment(val[0].first&.location || location)
105
146
  )
106
147
  }
107
- | annotations kMODULE start_new_scope tUKEYWORD module_self_types class_members kEND {
148
+ | annotations kMODULE start_new_scope namespace tUKEYWORD module_self_types class_members kEND {
108
149
  reset_variable_scope
109
150
 
110
- location = val[1].location + val[6].location
151
+ location = val[1].location + val[7].location
152
+ name_loc, colon_loc = split_kw_loc(val[4].location)
153
+ self_loc = case val[5].size
154
+ when 0
155
+ nil
156
+ when 1
157
+ val[5][0].location
158
+ else
159
+ val[5].first.location + val[5].last.location
160
+ end
161
+ location = location.with_children(
162
+ required: { keyword: val[1].location, name: name_loc, end: val[7].location },
163
+ optional: { colon: colon_loc, type_params: nil, self_types: self_loc }
164
+ )
165
+
111
166
  result = Declarations::Module.new(
112
- name: val[3].value,
167
+ name: RBS::TypeName.new(name: val[4].value, namespace: val[3]&.value || RBS::Namespace.empty),
113
168
  type_params: Declarations::ModuleTypeParams.empty,
114
- self_types: val[4],
115
- members: val[5],
169
+ self_types: val[5],
170
+ members: val[6],
116
171
  annotations: val[0],
117
172
  location: location,
118
173
  comment: leading_comment(val[0].first&.location || location)
@@ -120,9 +175,9 @@ rule
120
175
  }
121
176
 
122
177
  colon_module_self_types:
123
- { result = [] }
178
+ { result = LocatedValue.new(value: [], location: nil) }
124
179
  | kCOLON module_self_types {
125
- result = val[1]
180
+ result = LocatedValue.new(value: val[1], location: val[0].location)
126
181
  }
127
182
 
128
183
  module_self_types:
@@ -138,6 +193,10 @@ rule
138
193
  name = val[0].value
139
194
  args = val[2]
140
195
  location = val[0].location + val[3].location
196
+ location = location.with_children(
197
+ required: { name: val[0].location },
198
+ optional: { args: val[1].location + val[3].location }
199
+ )
141
200
 
142
201
  case
143
202
  when name.class?
@@ -151,7 +210,10 @@ rule
151
210
  | qualified_name {
152
211
  name = val[0].value
153
212
  args = []
154
- location = val[0].location
213
+ location = val[0].location.with_children(
214
+ required: { name: val[0].location },
215
+ optional: { args: nil }
216
+ )
155
217
 
156
218
  case
157
219
  when name.class?
@@ -186,35 +248,61 @@ rule
186
248
  | signature
187
249
 
188
250
  attribute_kind:
189
- { result = :instance }
190
- | kSELF kDOT { result = :singleton }
251
+ { result = LocatedValue.new(value: :instance, location: nil) }
252
+ | kSELF kDOT { result = LocatedValue.new(value: :singleton, location: val[0].location + val[1].location) }
191
253
 
192
254
  attribute_member:
193
255
  annotations kATTRREADER attribute_kind keyword type {
194
256
  location = val[1].location + val[4].location
257
+ name_loc, colon_loc = split_kw_loc(val[3].location)
258
+ location = location.with_children(
259
+ required: { keyword: val[1].location, name: name_loc, colon: colon_loc },
260
+ optional: { ivar: nil, ivar_name: nil, kind: val[2].location }
261
+ )
195
262
  result = Members::AttrReader.new(name: val[3].value,
196
263
  ivar_name: nil,
197
264
  type: val[4],
198
- kind: val[2],
265
+ kind: val[2].value,
199
266
  annotations: val[0],
200
267
  location: location,
201
268
  comment: leading_comment(val[0].first&.location || location))
202
269
  }
203
270
  | annotations kATTRREADER attribute_kind method_name attr_var_opt kCOLON type {
204
271
  location = val[1].location + val[6].location
272
+ ivar_loc = val[4]&.location
273
+ case name_value = val[4]&.value
274
+ when LocatedValue
275
+ ivar_name = name_value.value
276
+ ivar_name_loc = name_value.location
277
+ when false
278
+ ivar_name = false
279
+ ivar_name_loc = nil
280
+ else
281
+ ivar_name = nil
282
+ ivar_loc = nil
283
+ end
284
+ location = location.with_children(
285
+ required: { keyword: val[1].location, name: val[3].location, colon: val[5].location },
286
+ optional: { ivar: ivar_loc, ivar_name: ivar_name_loc, kind: val[2].location }
287
+ )
205
288
  result = Members::AttrReader.new(name: val[3].value.to_sym,
206
- ivar_name: val[4],
289
+ ivar_name: ivar_name,
207
290
  type: val[6],
208
- kind: val[2],
291
+ kind: val[2].value,
209
292
  annotations: val[0],
210
293
  location: location,
211
294
  comment: leading_comment(val[0].first&.location || location))
212
295
  }
213
296
  | annotations kATTRWRITER attribute_kind keyword type {
214
297
  location = val[1].location + val[4].location
298
+ name_loc, colon_loc = split_kw_loc(val[3].location)
299
+ location = location.with_children(
300
+ required: { keyword: val[1].location, name: name_loc, colon: colon_loc },
301
+ optional: { ivar: nil, ivar_name: nil, kind: val[2].location }
302
+ )
215
303
  result = Members::AttrWriter.new(name: val[3].value,
216
304
  ivar_name: nil,
217
- kind: val[2],
305
+ kind: val[2].value,
218
306
  type: val[4],
219
307
  annotations: val[0],
220
308
  location: location,
@@ -222,9 +310,26 @@ rule
222
310
  }
223
311
  | annotations kATTRWRITER attribute_kind method_name attr_var_opt kCOLON type {
224
312
  location = val[1].location + val[6].location
313
+ ivar_loc = val[4]&.location
314
+ case name_value = val[4]&.value
315
+ when LocatedValue
316
+ ivar_name = name_value.value
317
+ ivar_name_loc = name_value.location
318
+ when false
319
+ ivar_name = false
320
+ ivar_name_loc = nil
321
+ else
322
+ ivar_name = nil
323
+ ivar_loc = nil
324
+ end
325
+ location = location.with_children(
326
+ required: { keyword: val[1].location, name: val[3].location, colon: val[5].location },
327
+ optional: { ivar: ivar_loc, ivar_name: ivar_name_loc, kind: val[2].location }
328
+ )
329
+
225
330
  result = Members::AttrWriter.new(name: val[3].value.to_sym,
226
- ivar_name: val[4],
227
- kind: val[2],
331
+ ivar_name: ivar_name,
332
+ kind: val[2].value,
228
333
  type: val[6],
229
334
  annotations: val[0],
230
335
  location: location,
@@ -232,9 +337,15 @@ rule
232
337
  }
233
338
  | annotations kATTRACCESSOR attribute_kind keyword type {
234
339
  location = val[1].location + val[4].location
340
+ name_loc, colon_loc = split_kw_loc(val[3].location)
341
+ location = location.with_children(
342
+ required: { keyword: val[1].location, name: name_loc, colon: colon_loc },
343
+ optional: { ivar: nil, ivar_name: nil, kind: val[2].location }
344
+ )
345
+
235
346
  result = Members::AttrAccessor.new(name: val[3].value,
236
347
  ivar_name: nil,
237
- kind: val[2],
348
+ kind: val[2].value,
238
349
  type: val[4],
239
350
  annotations: val[0],
240
351
  location: location,
@@ -242,9 +353,26 @@ rule
242
353
  }
243
354
  | annotations kATTRACCESSOR attribute_kind method_name attr_var_opt kCOLON type {
244
355
  location = val[1].location + val[6].location
356
+ ivar_loc = val[4]&.location
357
+ case name_value = val[4]&.value
358
+ when LocatedValue
359
+ ivar_name = name_value.value
360
+ ivar_name_loc = name_value.location
361
+ when false
362
+ ivar_name = false
363
+ ivar_name_loc = nil
364
+ else
365
+ ivar_name = nil
366
+ ivar_loc = nil
367
+ end
368
+ location = location.with_children(
369
+ required: { keyword: val[1].location, name: val[3].location, colon: val[5].location },
370
+ optional: { ivar: ivar_loc, ivar_name: ivar_name_loc, kind: val[2].location }
371
+ )
372
+
245
373
  result = Members::AttrAccessor.new(name: val[3].value.to_sym,
246
- ivar_name: val[4],
247
- kind: val[2],
374
+ ivar_name: ivar_name,
375
+ kind: val[2].value,
248
376
  type: val[6],
249
377
  annotations: val[0],
250
378
  location: location,
@@ -253,12 +381,21 @@ rule
253
381
 
254
382
  attr_var_opt:
255
383
  { result = nil }
256
- | kLPAREN kRPAREN { result = false }
257
- | kLPAREN tIVAR kRPAREN { result = val[1].value }
384
+ | kLPAREN kRPAREN { result = LocatedValue.new(value: false, location: val[0].location + val[1].location) }
385
+ | kLPAREN tIVAR kRPAREN {
386
+ result = LocatedValue.new(
387
+ value: val[1],
388
+ location: val[0].location + val[2].location
389
+ )
390
+ }
258
391
 
259
392
  var_type_member:
260
393
  tIVAR kCOLON type {
261
- location = val[0].location + val[2].location
394
+ location = (val[0].location + val[2].location).with_children(
395
+ required: { name: val[0].location, colon: val[1].location },
396
+ optional: { kind: nil }
397
+ )
398
+
262
399
  result = Members::InstanceVariable.new(
263
400
  name: val[0].value,
264
401
  type: val[2],
@@ -277,7 +414,11 @@ rule
277
414
  )
278
415
  end
279
416
 
280
- location = val[0].location + val[2].location
417
+ location = (val[0].location + val[2].location).with_children(
418
+ required: { name: val[0].location, colon: val[1].location },
419
+ optional: { kind: nil }
420
+ )
421
+
281
422
  result = Members::ClassVariable.new(
282
423
  name: val[0].value,
283
424
  type: type,
@@ -296,7 +437,11 @@ rule
296
437
  )
297
438
  end
298
439
 
299
- location = val[0].location + val[4].location
440
+ location = (val[0].location + val[4].location).with_children(
441
+ required: { name: val[2].location, colon: val[3].location },
442
+ optional: { kind: val[0].location + val[1].location }
443
+ )
444
+
300
445
  result = Members::ClassInstanceVariable.new(
301
446
  name: val[2].value,
302
447
  type: type,
@@ -310,6 +455,10 @@ rule
310
455
  reset_variable_scope
311
456
 
312
457
  location = val[1].location + val[6].location
458
+ location = location.with_children(
459
+ required: { keyword: val[1].location, name: val[3].location, end: val[6].location },
460
+ optional: { type_params: val[4]&.location }
461
+ )
313
462
  result = Declarations::Interface.new(
314
463
  name: val[3].value,
315
464
  type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
@@ -352,7 +501,12 @@ rule
352
501
  if val[2].value.alias?
353
502
  raise SemanticsError.new("Should include module or interface", subject: val[2].value, location: val[2].location)
354
503
  end
355
- location = val[1].location + val[2].location
504
+
505
+ location = (val[1].location + val[2].location).with_children(
506
+ required: { keyword: val[1].location, name: val[2].location },
507
+ optional: { args: nil }
508
+ )
509
+
356
510
  result = Members::Include.new(name: val[2].value,
357
511
  args: [],
358
512
  annotations: val[0],
@@ -363,7 +517,12 @@ rule
363
517
  if val[2].value.alias?
364
518
  raise SemanticsError.new("Should include module or interface", subject: val[2].value, location: val[2].location)
365
519
  end
366
- location = val[1].location + val[5].location
520
+
521
+ location = (val[1].location + val[5].location).with_children(
522
+ required: { keyword: val[1].location, name: val[2].location },
523
+ optional: { args: val[3].location + val[5].location }
524
+ )
525
+
367
526
  result = Members::Include.new(name: val[2].value,
368
527
  args: val[4],
369
528
  annotations: val[0],
@@ -376,7 +535,12 @@ rule
376
535
  if val[2].value.alias?
377
536
  raise SemanticsError.new("Should extend module or interface", subject: val[2].value, location: val[2].location)
378
537
  end
379
- location = val[1].location + val[2].location
538
+
539
+ location = (val[1].location + val[2].location).with_children(
540
+ required: { keyword: val[1].location, name: val[2].location },
541
+ optional: { args: nil }
542
+ )
543
+
380
544
  result = Members::Extend.new(name: val[2].value,
381
545
  args: [],
382
546
  annotations: val[0],
@@ -387,7 +551,12 @@ rule
387
551
  if val[2].value.alias?
388
552
  raise SemanticsError.new("Should extend module or interface", subject: val[2].value, location: val[2].location)
389
553
  end
390
- location = val[1].location + val[5].location
554
+
555
+ location = (val[1].location + val[5].location).with_children(
556
+ required: { keyword: val[1].location, name: val[2].location },
557
+ optional: { args: val[3].location + val[5].location }
558
+ )
559
+
391
560
  result = Members::Extend.new(name: val[2].value,
392
561
  args: val[4],
393
562
  annotations: val[0],
@@ -400,7 +569,12 @@ rule
400
569
  unless val[2].value.class?
401
570
  raise SemanticsError.new("Should prepend module", subject: val[2].value, location: val[2].location)
402
571
  end
403
- location = val[1].location + val[2].location
572
+
573
+ location = (val[1].location + val[2].location).with_children(
574
+ required: { keyword: val[1].location, name: val[2].location },
575
+ optional: { args: nil }
576
+ )
577
+
404
578
  result = Members::Prepend.new(name: val[2].value,
405
579
  args: [],
406
580
  annotations: val[0],
@@ -411,7 +585,12 @@ rule
411
585
  unless val[2].value.class?
412
586
  raise SemanticsError.new("Should prepend module", subject: val[2].value, location: val[2].location)
413
587
  end
414
- location = val[1].location + val[5].location
588
+
589
+ location = (val[1].location + val[5].location).with_children(
590
+ required: { keyword: val[1].location, name: val[2].location },
591
+ optional: { args: val[3].location + val[5].location }
592
+ )
593
+
415
594
  result = Members::Prepend.new(name: val[2].value,
416
595
  args: val[4],
417
596
  annotations: val[0],
@@ -429,10 +608,22 @@ rule
429
608
  method_member:
430
609
  annotations attributes overload kDEF method_kind def_name method_types {
431
610
  location = val[3].location + val[6].last.location
611
+ children = {}
612
+
613
+ required_children = { keyword: val[3].location, name: val[5].location }
614
+ optional_children = { kind: nil, overload: nil }
615
+
616
+ if val[4]
617
+ kind = val[4].value
618
+ optional_children[:kind] = val[4].location
619
+ else
620
+ kind = :instance
621
+ end
432
622
 
433
623
  last_type = val[6].last
434
624
  if last_type.is_a?(LocatedValue) && last_type.value == :dot3
435
625
  overload = true
626
+ optional_children[:overload] = last_type.location
436
627
  val[6].pop
437
628
  else
438
629
  overload = false
@@ -440,10 +631,10 @@ rule
440
631
 
441
632
  result = Members::MethodDefinition.new(
442
633
  name: val[5].value,
443
- kind: val[4],
634
+ kind: kind,
444
635
  types: val[6],
445
636
  annotations: val[0],
446
- location: location,
637
+ location: location.with_children(required: required_children, optional: optional_children),
447
638
  comment: leading_comment(val[0].first&.location || val[2]&.location || val[3].location),
448
639
  overload: overload || !!val[2]
449
640
  )
@@ -455,9 +646,9 @@ rule
455
646
  }
456
647
 
457
648
  method_kind:
458
- { result = :instance }
459
- | kSELF kDOT { result = :singleton }
460
- | kSELFQ kDOT { result = :singleton_instance }
649
+ { result = nil }
650
+ | kSELF kDOT { result = LocatedValue.new(value: :singleton, location: val[0].location + val[1].location) }
651
+ | kSELFQ kDOT { result = LocatedValue.new(value: :singleton_instance, location: val[0].location + val[1].location) }
461
652
 
462
653
  method_types:
463
654
  method_type { result = [val[0]] }
@@ -498,7 +689,14 @@ rule
498
689
  }
499
690
 
500
691
  def_name:
501
- keyword
692
+ keyword {
693
+ loc = val[0].location
694
+
695
+ result = LocatedValue.new(
696
+ value: val[0].value,
697
+ location: Location.new(buffer: loc.buffer, start_pos: loc.start_pos, end_pos: loc.end_pos - 1)
698
+ )
699
+ }
502
700
  | method_name kCOLON {
503
701
  result = LocatedValue.new(value: val[0].value.to_sym,
504
702
  location: val[0].location + val[1].location)
@@ -555,19 +753,34 @@ rule
555
753
 
556
754
  module_type_param:
557
755
  type_param_check type_param_variance tUIDENT {
558
- result = Declarations::ModuleTypeParams::TypeParam.new(name: val[2].value.to_sym,
559
- variance: val[1],
560
- skip_validation: val[0])
756
+ loc = case
757
+ when l0 = val[0].location
758
+ l0 + val[2].location
759
+ when l1 = val[1].location
760
+ l1 + val[2].location
761
+ else
762
+ val[2].location
763
+ end
764
+ loc = loc.with_children(
765
+ required: { name: val[2].location },
766
+ optional: { variance: val[1].location, unchecked: val[0].location }
767
+ )
768
+ result = Declarations::ModuleTypeParams::TypeParam.new(
769
+ name: val[2].value.to_sym,
770
+ variance: val[1].value,
771
+ skip_validation: val[0].value,
772
+ location: loc
773
+ )
561
774
  }
562
775
 
563
776
  type_param_variance:
564
- { result = :invariant }
565
- | kOUT { result = :covariant }
566
- | kIN { result = :contravariant }
777
+ { result = LocatedValue.new(value: :invariant, location: nil) }
778
+ | kOUT { result = LocatedValue.new(value: :covariant, location: val[0].location) }
779
+ | kIN { result = LocatedValue.new(value: :contravariant, location: val[0].location) }
567
780
 
568
781
  type_param_check:
569
- { result = false }
570
- | kUNCHECKED { result = true }
782
+ { result = LocatedValue.new(value: false, location: nil) }
783
+ | kUNCHECKED { result = LocatedValue.new(value: true, location: val[0].location) }
571
784
 
572
785
  type_params:
573
786
  { result = nil }
@@ -589,6 +802,10 @@ rule
589
802
  alias_member:
590
803
  annotations kALIAS method_name method_name {
591
804
  location = val[1].location + val[3].location
805
+ location = location.with_children(
806
+ required: { keyword: val[1].location, new_name: val[2].location, old_name: val[3].location },
807
+ optional: { new_kind: nil, old_kind: nil }
808
+ )
592
809
  result = Members::Alias.new(
593
810
  new_name: val[2].value.to_sym,
594
811
  old_name: val[3].value.to_sym,
@@ -600,6 +817,13 @@ rule
600
817
  }
601
818
  | annotations kALIAS kSELF kDOT method_name kSELF kDOT method_name {
602
819
  location = val[1].location + val[7].location
820
+ location = location.with_children(
821
+ required: { keyword: val[1].location, new_name: val[4].location, old_name: val[7].location },
822
+ optional: {
823
+ new_kind: val[2].location + val[3].location,
824
+ old_kind: val[5].location + val[6].location
825
+ }
826
+ )
603
827
  result = Members::Alias.new(
604
828
  new_name: val[4].value.to_sym,
605
829
  old_name: val[7].value.to_sym,
@@ -613,16 +837,24 @@ rule
613
837
  type_decl:
614
838
  annotations kTYPE type_alias_name kEQ type {
615
839
  location = val[1].location + val[4].location
616
- result = Declarations::Alias.new(name: val[2].value,
617
- type: val[4],
618
- annotations: val[0],
619
- location: location,
620
- comment: leading_comment(val[0].first&.location || location))
840
+ location = location.with_children(
841
+ required: { keyword: val[1].location, name: val[2].location, eq: val[3].location }
842
+ )
843
+ result = Declarations::Alias.new(
844
+ name: val[2].value,
845
+ type: val[4],
846
+ annotations: val[0],
847
+ location: location,
848
+ comment: leading_comment(val[0].first&.location || location)
849
+ )
621
850
  }
622
851
 
623
852
  const_decl:
624
853
  class_name kCOLON type {
625
854
  location = val[0].location + val[2].location
855
+ location = location.with_children(
856
+ required: { name: val[0].location, colon: val[1].location }
857
+ )
626
858
  result = Declarations::Constant.new(name: val[0].value,
627
859
  type: val[2],
628
860
  location: location,
@@ -630,6 +862,13 @@ rule
630
862
  }
631
863
  | namespace tUKEYWORD type {
632
864
  location = (val[0] || val[1]).location + val[2].location
865
+
866
+ lhs_loc = (val[0] || val[1]).location + val[1].location
867
+ name_loc, colon_loc = split_kw_loc(lhs_loc)
868
+ location = location.with_children(
869
+ required: { name: name_loc, colon: colon_loc }
870
+ )
871
+
633
872
  name = TypeName.new(name: val[1].value, namespace: val[0]&.value || Namespace.empty)
634
873
  result = Declarations::Constant.new(name: name,
635
874
  type: val[2],
@@ -640,6 +879,9 @@ rule
640
879
  global_decl:
641
880
  tGLOBALIDENT kCOLON type {
642
881
  location = val[0].location + val[2].location
882
+ location = location.with_children(
883
+ required: { name: val[0].location, colon: val[1].location }
884
+ )
643
885
  result = Declarations::Global.new(name: val[0].value.to_sym,
644
886
  type: val[2],
645
887
  location: location,
@@ -731,11 +973,23 @@ rule
731
973
  if is_bound_variable?(name.name)
732
974
  result = Types::Variable.new(name: name.name, location: location)
733
975
  else
976
+ location = location.with_children(
977
+ required: { name: val[0].location },
978
+ optional: { args: nil }
979
+ )
734
980
  result = Types::ClassInstance.new(name: name, args: args, location: location)
735
981
  end
736
982
  when name.alias?
983
+ location = location.with_children(
984
+ required: { name: val[0].location },
985
+ optional: { args: nil }
986
+ )
737
987
  result = Types::Alias.new(name: name, location: location)
738
988
  when name.interface?
989
+ location = location.with_children(
990
+ required: { name: val[0].location },
991
+ optional: { args: nil }
992
+ )
739
993
  result = Types::Interface.new(name: name, args: args, location: location)
740
994
  end
741
995
  }
@@ -749,8 +1003,16 @@ rule
749
1003
  if is_bound_variable?(name.name)
750
1004
  raise SemanticsError.new("#{name.name} is type variable and cannot be applied", subject: name, location: location)
751
1005
  end
1006
+ location = location.with_children(
1007
+ required: { name: val[0].location },
1008
+ optional: { args: val[1].location + val[3].location }
1009
+ )
752
1010
  result = Types::ClassInstance.new(name: name, args: args, location: location)
753
1011
  when name.interface?
1012
+ location = location.with_children(
1013
+ required: { name: val[0].location },
1014
+ optional: { args: val[1].location + val[3].location }
1015
+ )
754
1016
  result = Types::Interface.new(name: name, args: args, location: location)
755
1017
  else
756
1018
  raise SyntaxError.new(token_str: "kLBRACKET", error_value: val[1])
@@ -773,8 +1035,11 @@ rule
773
1035
  result = type
774
1036
  }
775
1037
  | kSINGLETON kLPAREN class_name kRPAREN {
776
- result = Types::ClassSingleton.new(name: val[2].value,
777
- location: val[0].location + val[3].location)
1038
+ location = val[0].location + val[3].location
1039
+ location = location.with_children(
1040
+ required: { name: val[2].location }
1041
+ )
1042
+ result = Types::ClassSingleton.new(name: val[2].value, location: location)
778
1043
  }
779
1044
  | kHAT proc_type {
780
1045
  type, block = val[1].value
@@ -968,40 +1233,97 @@ rule
968
1233
 
969
1234
  required_positional:
970
1235
  type var_name_opt {
971
- result = Types::Function::Param.new(type: val[0],
972
- name: val[1]&.value&.to_sym)
1236
+ loc = val[0].location
1237
+ if var_name = val[1]
1238
+ loc = loc + var_name.location
1239
+ end
1240
+ loc = loc.with_children(optional: { name: var_name&.location })
1241
+
1242
+ result = Types::Function::Param.new(
1243
+ type: val[0],
1244
+ name: var_name&.value&.to_sym,
1245
+ location: loc
1246
+ )
973
1247
  }
974
1248
 
975
1249
  optional_positional:
976
1250
  kQUESTION type var_name_opt {
977
- result = Types::Function::Param.new(type: val[1],
978
- name: val[2]&.value&.to_sym)
1251
+ loc = val[0].location + val[1].location
1252
+ if var_name = val[2]
1253
+ loc = loc + var_name.location
1254
+ end
1255
+ loc = loc.with_children(optional: { name: var_name&.location })
1256
+
1257
+ result = Types::Function::Param.new(
1258
+ type: val[1],
1259
+ name: val[2]&.value&.to_sym,
1260
+ location: loc
1261
+ )
979
1262
  }
980
1263
 
981
1264
  rest_positional:
982
1265
  kSTAR type var_name_opt {
983
- result = Types::Function::Param.new(type: val[1],
984
- name: val[2]&.value&.to_sym)
1266
+ loc = val[0].location + val[1].location
1267
+ if var_name = val[2]
1268
+ loc = loc + var_name.location
1269
+ end
1270
+ loc = loc.with_children(optional: { name: var_name&.location })
1271
+
1272
+ result = Types::Function::Param.new(
1273
+ type: val[1],
1274
+ name: val[2]&.value&.to_sym,
1275
+ location: loc
1276
+ )
985
1277
  }
986
1278
 
987
1279
  required_keyword:
988
1280
  keyword_name type var_name_opt {
989
- param = Types::Function::Param.new(type: val[1],
990
- name: val[2]&.value&.to_sym)
1281
+ loc = val[0].location + val[1].location
1282
+ if var_name = val[2]
1283
+ loc = loc + var_name.location
1284
+ end
1285
+
1286
+ loc = loc.with_children(optional: { name: var_name&.location })
1287
+
1288
+ param = Types::Function::Param.new(
1289
+ type: val[1],
1290
+ name: val[2]&.value&.to_sym,
1291
+ location: loc
1292
+ )
991
1293
  result = { val[0].value => param }
992
1294
  }
993
1295
 
994
1296
  optional_keyword:
995
1297
  kQUESTION keyword_name type var_name_opt {
996
- param = Types::Function::Param.new(type: val[2],
997
- name: val[3]&.value&.to_sym)
1298
+ loc = val[0].location + val[2].location
1299
+ if var_name = val[3]
1300
+ loc = loc + var_name.location
1301
+ end
1302
+
1303
+ loc = loc.with_children(optional: { name: var_name&.location })
1304
+
1305
+ param = Types::Function::Param.new(
1306
+ type: val[2],
1307
+ name: val[3]&.value&.to_sym,
1308
+ location: loc
1309
+ )
998
1310
  result = { val[1].value => param }
999
1311
  }
1000
1312
 
1001
1313
  rest_keyword:
1002
1314
  kSTAR2 type var_name_opt {
1003
- result = Types::Function::Param.new(type: val[1],
1004
- name: val[2]&.value&.to_sym)
1315
+ loc = val[0].location + val[1].location
1316
+ if var_name = val[2]
1317
+ loc = loc + var_name.location
1318
+ end
1319
+
1320
+ loc = loc.with_children(optional: { name: var_name&.location })
1321
+
1322
+ result = Types::Function::Param.new(
1323
+ type: val[1],
1324
+ name: val[2]&.value&.to_sym,
1325
+ location: loc
1326
+ )
1005
1327
  }
1006
1328
 
1007
1329
  var_name_opt:
@@ -1431,6 +1753,16 @@ def on_error(token_id, error_value, value_stack)
1431
1753
  raise SyntaxError.new(token_str: token_to_str(token_id), error_value: error_value, value_stack: value_stack)
1432
1754
  end
1433
1755
 
1756
+ def split_kw_loc(loc)
1757
+ buf = loc.buffer
1758
+ start_pos = loc.start_pos
1759
+ end_pos = loc.end_pos
1760
+ [
1761
+ Location.new(buffer: buf, start_pos: start_pos, end_pos: end_pos - 1),
1762
+ Location.new(buffer: buf, start_pos: end_pos - 1, end_pos: end_pos)
1763
+ ]
1764
+ end
1765
+
1434
1766
  class SyntaxError < ParsingError
1435
1767
  attr_reader :token_str, :error_value, :value_stack
1436
1768