rbs 1.1.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +5 -1
  3. data/.gitignore +2 -0
  4. data/CHANGELOG.md +81 -0
  5. data/README.md +1 -1
  6. data/Rakefile +11 -0
  7. data/Steepfile +1 -0
  8. data/core/array.rbs +2 -2
  9. data/core/basic_object.rbs +1 -1
  10. data/core/enumerable.rbs +1 -1
  11. data/core/hash.rbs +13 -5
  12. data/core/io.rbs +4 -4
  13. data/core/kernel.rbs +2 -2
  14. data/core/marshal.rbs +4 -3
  15. data/core/module.rbs +1 -1
  16. data/core/numeric.rbs +10 -0
  17. data/core/proc.rbs +1 -1
  18. data/core/random.rbs +4 -2
  19. data/core/range.rbs +2 -2
  20. data/core/struct.rbs +3 -2
  21. data/core/thread.rbs +1 -1
  22. data/docs/CONTRIBUTING.md +5 -3
  23. data/docs/rbs_by_example.md +328 -0
  24. data/docs/sigs.md +21 -2
  25. data/docs/stdlib.md +1 -1
  26. data/docs/syntax.md +11 -14
  27. data/lib/rbs.rb +1 -0
  28. data/lib/rbs/ast/annotation.rb +2 -2
  29. data/lib/rbs/ast/comment.rb +2 -2
  30. data/lib/rbs/ast/declarations.rb +37 -22
  31. data/lib/rbs/ast/members.rb +26 -26
  32. data/lib/rbs/cli.rb +3 -0
  33. data/lib/rbs/constant_table.rb +4 -1
  34. data/lib/rbs/definition.rb +1 -1
  35. data/lib/rbs/definition_builder.rb +16 -18
  36. data/lib/rbs/definition_builder/ancestor_builder.rb +10 -2
  37. data/lib/rbs/definition_builder/method_builder.rb +4 -2
  38. data/lib/rbs/errors.rb +36 -0
  39. data/lib/rbs/location.rb +106 -2
  40. data/lib/rbs/locator.rb +205 -0
  41. data/lib/rbs/method_type.rb +2 -2
  42. data/lib/rbs/parser.rb +1315 -962
  43. data/lib/rbs/parser.y +411 -75
  44. data/lib/rbs/prototype/rb.rb +7 -3
  45. data/lib/rbs/prototype/runtime.rb +118 -42
  46. data/lib/rbs/test/hook.rb +8 -2
  47. data/lib/rbs/type_name.rb +2 -3
  48. data/lib/rbs/type_name_resolver.rb +1 -1
  49. data/lib/rbs/types.rb +36 -34
  50. data/lib/rbs/version.rb +1 -1
  51. data/lib/rbs/writer.rb +4 -2
  52. data/rbs.gemspec +1 -1
  53. data/sig/ancestor_builder.rbs +2 -0
  54. data/sig/annotation.rbs +1 -1
  55. data/sig/cli.rbs +31 -21
  56. data/sig/comment.rbs +1 -1
  57. data/sig/declarations.rbs +106 -21
  58. data/sig/environment.rbs +2 -2
  59. data/sig/errors.rbs +15 -0
  60. data/sig/location.rbs +84 -3
  61. data/sig/locator.rbs +44 -0
  62. data/sig/members.rbs +76 -12
  63. data/sig/method_builder.rbs +1 -1
  64. data/sig/method_types.rbs +1 -1
  65. data/sig/namespace.rbs +1 -1
  66. data/sig/polyfill.rbs +4 -17
  67. data/sig/rbs.rbs +8 -4
  68. data/sig/typename.rbs +1 -1
  69. data/sig/types.rbs +67 -20
  70. data/sig/util.rbs +0 -4
  71. data/sig/writer.rbs +8 -2
  72. data/stdlib/dbm/0/dbm.rbs +43 -30
  73. data/stdlib/mutex_m/0/mutex_m.rbs +1 -1
  74. data/stdlib/net-http/0/net-http.rbs +1846 -0
  75. data/stdlib/optparse/0/optparse.rbs +1214 -0
  76. data/stdlib/resolv/0/resolv.rbs +1504 -0
  77. data/stdlib/rubygems/0/requirement.rbs +84 -2
  78. data/stdlib/rubygems/0/rubygems.rbs +2 -2
  79. data/stdlib/rubygems/0/version.rbs +2 -1
  80. data/stdlib/shellwords/0/shellwords.rbs +252 -0
  81. data/stdlib/socket/0/addrinfo.rbs +469 -0
  82. data/stdlib/socket/0/basic_socket.rbs +503 -0
  83. data/stdlib/socket/0/ip_socket.rbs +72 -0
  84. data/stdlib/socket/0/socket.rbs +2687 -0
  85. data/stdlib/socket/0/tcp_server.rbs +177 -0
  86. data/stdlib/socket/0/tcp_socket.rbs +35 -0
  87. data/stdlib/socket/0/udp_socket.rbs +111 -0
  88. data/stdlib/socket/0/unix_server.rbs +154 -0
  89. data/stdlib/socket/0/unix_socket.rbs +132 -0
  90. data/stdlib/timeout/0/timeout.rbs +5 -0
  91. data/steep/Gemfile.lock +19 -16
  92. metadata +18 -11
  93. data/bin/annotate-with-rdoc +0 -153
  94. data/bin/console +0 -14
  95. data/bin/query-rdoc +0 -103
  96. data/bin/rbs-prof +0 -9
  97. data/bin/run_in_md.rb +0 -49
  98. data/bin/setup +0 -8
  99. data/bin/sort +0 -89
  100. data/bin/steep +0 -4
  101. data/bin/test_runner.rb +0 -29
data/lib/rbs/parser.y CHANGED
@@ -1,5 +1,5 @@
1
1
  class RBS::Parser
2
- token tUIDENT tLIDENT tNAMESPACE tINTERFACEIDENT tGLOBALIDENT
2
+ token tUIDENT tLIDENT tUNDERSCOREIDENT tNAMESPACE tINTERFACEIDENT tGLOBALIDENT
3
3
  tLKEYWORD tUKEYWORD tLKEYWORD_Q_E tUKEYWORD_Q_E
4
4
  tIVAR tCLASSVAR
5
5
  tANNOTATION
@@ -12,6 +12,7 @@ class RBS::Parser
12
12
  kPREPEND kEXTENSION kINCOMPATIBLE
13
13
  type_TYPE type_SIGNATURE type_METHODTYPE tEOF
14
14
  kOUT kIN kUNCHECKED kOVERLOAD
15
+ kUNDERSCORE
15
16
 
16
17
  prechigh
17
18
  nonassoc kQUESTION
@@ -65,10 +66,21 @@ rule
65
66
  reset_variable_scope
66
67
 
67
68
  location = val[1].location + val[7].location
69
+ location = location.with_children(
70
+ required: {
71
+ keyword: val[1].location,
72
+ name: val[3].location,
73
+ end: val[7].location
74
+ },
75
+ optional: {
76
+ type_params: val[4]&.location,
77
+ lt: val[5]&.location
78
+ }
79
+ )
68
80
  result = Declarations::Class.new(
69
81
  name: val[3].value,
70
82
  type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
71
- super_class: val[5],
83
+ super_class: val[5]&.value,
72
84
  members: val[6],
73
85
  annotations: val[0],
74
86
  location: location,
@@ -79,40 +91,84 @@ rule
79
91
  super_class:
80
92
  { result = nil }
81
93
  | kLT class_name {
82
- result = Declarations::Class::Super.new(name: val[1].value,
83
- args: [],
84
- location: val[1].location)
94
+ loc = val[1].location.with_children(
95
+ required: { name: val[1].location },
96
+ optional: { args: nil }
97
+ )
98
+ sup = Declarations::Class::Super.new(name: val[1].value, args: [], location: loc)
99
+ result = LocatedValue.new(value: sup, location: val[0].location)
85
100
  }
86
101
  | 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)
102
+ loc = (val[1].location + val[4].location).with_children(
103
+ required: { name: val[1].location },
104
+ optional: { args: val[2].location + val[4].location }
105
+ )
106
+ sup = Declarations::Class::Super.new(name: val[1].value, args: val[3], location: loc)
107
+ result = LocatedValue.new(value: sup, location: val[0].location)
90
108
  }
91
109
 
92
110
  module_decl:
93
111
  annotations kMODULE start_new_scope class_name module_type_params colon_module_self_types class_members kEND {
94
112
  reset_variable_scope
95
113
 
114
+ colon_loc = val[5].location
115
+ self_loc = val[5].value.yield_self do |params|
116
+ case params.size
117
+ when 0
118
+ nil
119
+ when 1
120
+ params[0].location
121
+ else
122
+ params.first.location + params.last.location
123
+ end
124
+ end
125
+
96
126
  location = val[1].location + val[7].location
127
+ location = location.with_children(
128
+ required: {
129
+ keyword: val[1].location,
130
+ name: val[3].location,
131
+ end: val[7].location
132
+ },
133
+ optional: {
134
+ type_params: val[4]&.location,
135
+ colon: colon_loc,
136
+ self_types: self_loc
137
+ }
138
+ )
97
139
  result = Declarations::Module.new(
98
140
  name: val[3].value,
99
141
  type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
100
- self_types: val[5],
142
+ self_types: val[5].value,
101
143
  members: val[6],
102
144
  annotations: val[0],
103
145
  location: location,
104
146
  comment: leading_comment(val[0].first&.location || location)
105
147
  )
106
148
  }
107
- | annotations kMODULE start_new_scope tUKEYWORD module_self_types class_members kEND {
149
+ | annotations kMODULE start_new_scope namespace tUKEYWORD module_self_types class_members kEND {
108
150
  reset_variable_scope
109
151
 
110
- location = val[1].location + val[6].location
152
+ location = val[1].location + val[7].location
153
+ name_loc, colon_loc = split_kw_loc(val[4].location)
154
+ self_loc = case val[5].size
155
+ when 0
156
+ nil
157
+ when 1
158
+ val[5][0].location
159
+ else
160
+ val[5].first.location + val[5].last.location
161
+ end
162
+ location = location.with_children(
163
+ required: { keyword: val[1].location, name: name_loc, end: val[7].location },
164
+ optional: { colon: colon_loc, type_params: nil, self_types: self_loc }
165
+ )
166
+
111
167
  result = Declarations::Module.new(
112
- name: val[3].value,
168
+ name: RBS::TypeName.new(name: val[4].value, namespace: val[3]&.value || RBS::Namespace.empty),
113
169
  type_params: Declarations::ModuleTypeParams.empty,
114
- self_types: val[4],
115
- members: val[5],
170
+ self_types: val[5],
171
+ members: val[6],
116
172
  annotations: val[0],
117
173
  location: location,
118
174
  comment: leading_comment(val[0].first&.location || location)
@@ -120,9 +176,9 @@ rule
120
176
  }
121
177
 
122
178
  colon_module_self_types:
123
- { result = [] }
179
+ { result = LocatedValue.new(value: [], location: nil) }
124
180
  | kCOLON module_self_types {
125
- result = val[1]
181
+ result = LocatedValue.new(value: val[1], location: val[0].location)
126
182
  }
127
183
 
128
184
  module_self_types:
@@ -138,6 +194,10 @@ rule
138
194
  name = val[0].value
139
195
  args = val[2]
140
196
  location = val[0].location + val[3].location
197
+ location = location.with_children(
198
+ required: { name: val[0].location },
199
+ optional: { args: val[1].location + val[3].location }
200
+ )
141
201
 
142
202
  case
143
203
  when name.class?
@@ -151,7 +211,10 @@ rule
151
211
  | qualified_name {
152
212
  name = val[0].value
153
213
  args = []
154
- location = val[0].location
214
+ location = val[0].location.with_children(
215
+ required: { name: val[0].location },
216
+ optional: { args: nil }
217
+ )
155
218
 
156
219
  case
157
220
  when name.class?
@@ -186,35 +249,61 @@ rule
186
249
  | signature
187
250
 
188
251
  attribute_kind:
189
- { result = :instance }
190
- | kSELF kDOT { result = :singleton }
252
+ { result = LocatedValue.new(value: :instance, location: nil) }
253
+ | kSELF kDOT { result = LocatedValue.new(value: :singleton, location: val[0].location + val[1].location) }
191
254
 
192
255
  attribute_member:
193
256
  annotations kATTRREADER attribute_kind keyword type {
194
257
  location = val[1].location + val[4].location
258
+ name_loc, colon_loc = split_kw_loc(val[3].location)
259
+ location = location.with_children(
260
+ required: { keyword: val[1].location, name: name_loc, colon: colon_loc },
261
+ optional: { ivar: nil, ivar_name: nil, kind: val[2].location }
262
+ )
195
263
  result = Members::AttrReader.new(name: val[3].value,
196
264
  ivar_name: nil,
197
265
  type: val[4],
198
- kind: val[2],
266
+ kind: val[2].value,
199
267
  annotations: val[0],
200
268
  location: location,
201
269
  comment: leading_comment(val[0].first&.location || location))
202
270
  }
203
271
  | annotations kATTRREADER attribute_kind method_name attr_var_opt kCOLON type {
204
272
  location = val[1].location + val[6].location
273
+ ivar_loc = val[4]&.location
274
+ case name_value = val[4]&.value
275
+ when LocatedValue
276
+ ivar_name = name_value.value
277
+ ivar_name_loc = name_value.location
278
+ when false
279
+ ivar_name = false
280
+ ivar_name_loc = nil
281
+ else
282
+ ivar_name = nil
283
+ ivar_loc = nil
284
+ end
285
+ location = location.with_children(
286
+ required: { keyword: val[1].location, name: val[3].location, colon: val[5].location },
287
+ optional: { ivar: ivar_loc, ivar_name: ivar_name_loc, kind: val[2].location }
288
+ )
205
289
  result = Members::AttrReader.new(name: val[3].value.to_sym,
206
- ivar_name: val[4],
290
+ ivar_name: ivar_name,
207
291
  type: val[6],
208
- kind: val[2],
292
+ kind: val[2].value,
209
293
  annotations: val[0],
210
294
  location: location,
211
295
  comment: leading_comment(val[0].first&.location || location))
212
296
  }
213
297
  | annotations kATTRWRITER attribute_kind keyword type {
214
298
  location = val[1].location + val[4].location
299
+ name_loc, colon_loc = split_kw_loc(val[3].location)
300
+ location = location.with_children(
301
+ required: { keyword: val[1].location, name: name_loc, colon: colon_loc },
302
+ optional: { ivar: nil, ivar_name: nil, kind: val[2].location }
303
+ )
215
304
  result = Members::AttrWriter.new(name: val[3].value,
216
305
  ivar_name: nil,
217
- kind: val[2],
306
+ kind: val[2].value,
218
307
  type: val[4],
219
308
  annotations: val[0],
220
309
  location: location,
@@ -222,9 +311,26 @@ rule
222
311
  }
223
312
  | annotations kATTRWRITER attribute_kind method_name attr_var_opt kCOLON type {
224
313
  location = val[1].location + val[6].location
314
+ ivar_loc = val[4]&.location
315
+ case name_value = val[4]&.value
316
+ when LocatedValue
317
+ ivar_name = name_value.value
318
+ ivar_name_loc = name_value.location
319
+ when false
320
+ ivar_name = false
321
+ ivar_name_loc = nil
322
+ else
323
+ ivar_name = nil
324
+ ivar_loc = nil
325
+ end
326
+ location = location.with_children(
327
+ required: { keyword: val[1].location, name: val[3].location, colon: val[5].location },
328
+ optional: { ivar: ivar_loc, ivar_name: ivar_name_loc, kind: val[2].location }
329
+ )
330
+
225
331
  result = Members::AttrWriter.new(name: val[3].value.to_sym,
226
- ivar_name: val[4],
227
- kind: val[2],
332
+ ivar_name: ivar_name,
333
+ kind: val[2].value,
228
334
  type: val[6],
229
335
  annotations: val[0],
230
336
  location: location,
@@ -232,9 +338,15 @@ rule
232
338
  }
233
339
  | annotations kATTRACCESSOR attribute_kind keyword type {
234
340
  location = val[1].location + val[4].location
341
+ name_loc, colon_loc = split_kw_loc(val[3].location)
342
+ location = location.with_children(
343
+ required: { keyword: val[1].location, name: name_loc, colon: colon_loc },
344
+ optional: { ivar: nil, ivar_name: nil, kind: val[2].location }
345
+ )
346
+
235
347
  result = Members::AttrAccessor.new(name: val[3].value,
236
348
  ivar_name: nil,
237
- kind: val[2],
349
+ kind: val[2].value,
238
350
  type: val[4],
239
351
  annotations: val[0],
240
352
  location: location,
@@ -242,9 +354,26 @@ rule
242
354
  }
243
355
  | annotations kATTRACCESSOR attribute_kind method_name attr_var_opt kCOLON type {
244
356
  location = val[1].location + val[6].location
357
+ ivar_loc = val[4]&.location
358
+ case name_value = val[4]&.value
359
+ when LocatedValue
360
+ ivar_name = name_value.value
361
+ ivar_name_loc = name_value.location
362
+ when false
363
+ ivar_name = false
364
+ ivar_name_loc = nil
365
+ else
366
+ ivar_name = nil
367
+ ivar_loc = nil
368
+ end
369
+ location = location.with_children(
370
+ required: { keyword: val[1].location, name: val[3].location, colon: val[5].location },
371
+ optional: { ivar: ivar_loc, ivar_name: ivar_name_loc, kind: val[2].location }
372
+ )
373
+
245
374
  result = Members::AttrAccessor.new(name: val[3].value.to_sym,
246
- ivar_name: val[4],
247
- kind: val[2],
375
+ ivar_name: ivar_name,
376
+ kind: val[2].value,
248
377
  type: val[6],
249
378
  annotations: val[0],
250
379
  location: location,
@@ -253,12 +382,21 @@ rule
253
382
 
254
383
  attr_var_opt:
255
384
  { result = nil }
256
- | kLPAREN kRPAREN { result = false }
257
- | kLPAREN tIVAR kRPAREN { result = val[1].value }
385
+ | kLPAREN kRPAREN { result = LocatedValue.new(value: false, location: val[0].location + val[1].location) }
386
+ | kLPAREN tIVAR kRPAREN {
387
+ result = LocatedValue.new(
388
+ value: val[1],
389
+ location: val[0].location + val[2].location
390
+ )
391
+ }
258
392
 
259
393
  var_type_member:
260
394
  tIVAR kCOLON type {
261
- location = val[0].location + val[2].location
395
+ location = (val[0].location + val[2].location).with_children(
396
+ required: { name: val[0].location, colon: val[1].location },
397
+ optional: { kind: nil }
398
+ )
399
+
262
400
  result = Members::InstanceVariable.new(
263
401
  name: val[0].value,
264
402
  type: val[2],
@@ -277,7 +415,11 @@ rule
277
415
  )
278
416
  end
279
417
 
280
- location = val[0].location + val[2].location
418
+ location = (val[0].location + val[2].location).with_children(
419
+ required: { name: val[0].location, colon: val[1].location },
420
+ optional: { kind: nil }
421
+ )
422
+
281
423
  result = Members::ClassVariable.new(
282
424
  name: val[0].value,
283
425
  type: type,
@@ -296,7 +438,11 @@ rule
296
438
  )
297
439
  end
298
440
 
299
- location = val[0].location + val[4].location
441
+ location = (val[0].location + val[4].location).with_children(
442
+ required: { name: val[2].location, colon: val[3].location },
443
+ optional: { kind: val[0].location + val[1].location }
444
+ )
445
+
300
446
  result = Members::ClassInstanceVariable.new(
301
447
  name: val[2].value,
302
448
  type: type,
@@ -310,6 +456,10 @@ rule
310
456
  reset_variable_scope
311
457
 
312
458
  location = val[1].location + val[6].location
459
+ location = location.with_children(
460
+ required: { keyword: val[1].location, name: val[3].location, end: val[6].location },
461
+ optional: { type_params: val[4]&.location }
462
+ )
313
463
  result = Declarations::Interface.new(
314
464
  name: val[3].value,
315
465
  type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
@@ -352,7 +502,12 @@ rule
352
502
  if val[2].value.alias?
353
503
  raise SemanticsError.new("Should include module or interface", subject: val[2].value, location: val[2].location)
354
504
  end
355
- location = val[1].location + val[2].location
505
+
506
+ location = (val[1].location + val[2].location).with_children(
507
+ required: { keyword: val[1].location, name: val[2].location },
508
+ optional: { args: nil }
509
+ )
510
+
356
511
  result = Members::Include.new(name: val[2].value,
357
512
  args: [],
358
513
  annotations: val[0],
@@ -363,7 +518,12 @@ rule
363
518
  if val[2].value.alias?
364
519
  raise SemanticsError.new("Should include module or interface", subject: val[2].value, location: val[2].location)
365
520
  end
366
- location = val[1].location + val[5].location
521
+
522
+ location = (val[1].location + val[5].location).with_children(
523
+ required: { keyword: val[1].location, name: val[2].location },
524
+ optional: { args: val[3].location + val[5].location }
525
+ )
526
+
367
527
  result = Members::Include.new(name: val[2].value,
368
528
  args: val[4],
369
529
  annotations: val[0],
@@ -376,7 +536,12 @@ rule
376
536
  if val[2].value.alias?
377
537
  raise SemanticsError.new("Should extend module or interface", subject: val[2].value, location: val[2].location)
378
538
  end
379
- location = val[1].location + val[2].location
539
+
540
+ location = (val[1].location + val[2].location).with_children(
541
+ required: { keyword: val[1].location, name: val[2].location },
542
+ optional: { args: nil }
543
+ )
544
+
380
545
  result = Members::Extend.new(name: val[2].value,
381
546
  args: [],
382
547
  annotations: val[0],
@@ -387,7 +552,12 @@ rule
387
552
  if val[2].value.alias?
388
553
  raise SemanticsError.new("Should extend module or interface", subject: val[2].value, location: val[2].location)
389
554
  end
390
- location = val[1].location + val[5].location
555
+
556
+ location = (val[1].location + val[5].location).with_children(
557
+ required: { keyword: val[1].location, name: val[2].location },
558
+ optional: { args: val[3].location + val[5].location }
559
+ )
560
+
391
561
  result = Members::Extend.new(name: val[2].value,
392
562
  args: val[4],
393
563
  annotations: val[0],
@@ -400,7 +570,12 @@ rule
400
570
  unless val[2].value.class?
401
571
  raise SemanticsError.new("Should prepend module", subject: val[2].value, location: val[2].location)
402
572
  end
403
- location = val[1].location + val[2].location
573
+
574
+ location = (val[1].location + val[2].location).with_children(
575
+ required: { keyword: val[1].location, name: val[2].location },
576
+ optional: { args: nil }
577
+ )
578
+
404
579
  result = Members::Prepend.new(name: val[2].value,
405
580
  args: [],
406
581
  annotations: val[0],
@@ -411,7 +586,12 @@ rule
411
586
  unless val[2].value.class?
412
587
  raise SemanticsError.new("Should prepend module", subject: val[2].value, location: val[2].location)
413
588
  end
414
- location = val[1].location + val[5].location
589
+
590
+ location = (val[1].location + val[5].location).with_children(
591
+ required: { keyword: val[1].location, name: val[2].location },
592
+ optional: { args: val[3].location + val[5].location }
593
+ )
594
+
415
595
  result = Members::Prepend.new(name: val[2].value,
416
596
  args: val[4],
417
597
  annotations: val[0],
@@ -430,9 +610,20 @@ rule
430
610
  annotations attributes overload kDEF method_kind def_name method_types {
431
611
  location = val[3].location + val[6].last.location
432
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
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,44 +1233,101 @@ 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:
1008
- | tLIDENT | tINTERFACEIDENT | tQUOTEDIDENT
1330
+ | tLIDENT | tINTERFACEIDENT | tQUOTEDIDENT | tUNDERSCOREIDENT | kUNDERSCORE
1009
1331
 
1010
1332
  qualified_name:
1011
1333
  namespace simple_name {
@@ -1392,12 +1714,16 @@ def next_token
1392
1714
  new_token(:tIVAR, input.matched.to_sym)
1393
1715
  when input.scan(/@@[a-zA-Z_]\w*/)
1394
1716
  new_token(:tCLASSVAR, input.matched.to_sym)
1395
- when input.scan(/_[a-zA-Z]\w*\b/)
1717
+ when input.scan(/_[A-Z]\w*\b/)
1396
1718
  new_token(:tINTERFACEIDENT)
1397
1719
  when input.scan(/[A-Z]\w*\b/)
1398
1720
  new_token(:tUIDENT)
1399
- when input.scan(/[a-z_]\w*\b/)
1721
+ when input.scan(/[a-z]\w*\b/)
1400
1722
  new_token(:tLIDENT)
1723
+ when input.scan(/_[a-z]\w*\b/)
1724
+ new_token(:tUNDERSCOREIDENT)
1725
+ when input.scan(/_/)
1726
+ new_token(:kUNDERSCORE)
1401
1727
  when input.scan(/"(\\"|[^"])*"/)
1402
1728
  s = input.matched.yield_self {|s| s[1, s.length - 2] }
1403
1729
  .gsub(DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE) do |match|
@@ -1431,6 +1757,16 @@ def on_error(token_id, error_value, value_stack)
1431
1757
  raise SyntaxError.new(token_str: token_to_str(token_id), error_value: error_value, value_stack: value_stack)
1432
1758
  end
1433
1759
 
1760
+ def split_kw_loc(loc)
1761
+ buf = loc.buffer
1762
+ start_pos = loc.start_pos
1763
+ end_pos = loc.end_pos
1764
+ [
1765
+ Location.new(buffer: buf, start_pos: start_pos, end_pos: end_pos - 1),
1766
+ Location.new(buffer: buf, start_pos: end_pos - 1, end_pos: end_pos)
1767
+ ]
1768
+ end
1769
+
1434
1770
  class SyntaxError < ParsingError
1435
1771
  attr_reader :token_str, :error_value, :value_stack
1436
1772