rbs 1.7.0.beta.5 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rbs/parser.y DELETED
@@ -1,1805 +0,0 @@
1
- class RBS::Parser
2
- token tUIDENT tLIDENT tUNDERSCOREIDENT tNAMESPACE tINTERFACEIDENT tGLOBALIDENT
3
- tLKEYWORD tUKEYWORD tLKEYWORD_Q_E tUKEYWORD_Q_E
4
- tIVAR tCLASSVAR
5
- tANNOTATION
6
- tSTRING tSYMBOL tINTEGER tWRITE_ATTR
7
- kLPAREN kRPAREN kLBRACKET kRBRACKET kLBRACE kRBRACE
8
- kVOID kNIL kTRUE kFALSE kANY kUNTYPED kTOP kBOT kSELF kSELFQ kINSTANCE kCLASS kBOOL kSINGLETON kTYPE kDEF kMODULE
9
- kPRIVATE kPUBLIC kALIAS
10
- kCOLON kCOLON2 kCOMMA kBAR kAMP kHAT kARROW kQUESTION kEXCLAMATION kSTAR kSTAR2 kFATARROW kEQ kDOT kDOT3 kLT
11
- kINTERFACE kEND kINCLUDE kEXTEND kATTRREADER kATTRWRITER kATTRACCESSOR tOPERATOR tQUOTEDMETHOD tQUOTEDIDENT
12
- kPREPEND kEXTENSION kINCOMPATIBLE
13
- type_TYPE type_SIGNATURE type_METHODTYPE tEOF
14
- kOUT kIN kUNCHECKED kOVERLOAD
15
- tPARAMNAME
16
-
17
- prechigh
18
- nonassoc kQUESTION
19
- left kAMP
20
- left kBAR
21
- nonassoc kARROW
22
- preclow
23
-
24
- expect 5
25
-
26
- rule
27
-
28
- target:
29
- type_TYPE type eof {
30
- result = val[1]
31
- }
32
- | type_SIGNATURE signatures eof {
33
- result = val[1]
34
- }
35
- | type_METHODTYPE method_type eof {
36
- result = val[1]
37
- }
38
-
39
- eof: | tEOF
40
-
41
- signatures:
42
- { result = [] }
43
- | signatures signature {
44
- result = val[0].push(val[1])
45
- }
46
-
47
- signature:
48
- type_decl
49
- | const_decl
50
- | global_decl
51
- | interface_decl
52
- | module_decl
53
- | class_decl
54
-
55
- start_new_scope: { start_new_variables_scope }
56
- start_merged_scope: { start_merged_variables_scope }
57
-
58
- annotations:
59
- { result = [] }
60
- | tANNOTATION annotations {
61
- result = val[1].unshift(Annotation.new(string: val[0].value, location: val[0].location))
62
- }
63
-
64
- class_decl:
65
- annotations kCLASS start_new_scope class_name module_type_params super_class class_members kEND {
66
- reset_variable_scope
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
- )
80
- result = Declarations::Class.new(
81
- name: val[3].value,
82
- type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
83
- super_class: val[5]&.value,
84
- members: val[6],
85
- annotations: val[0],
86
- location: location,
87
- comment: leading_comment(val[0].first&.location || location)
88
- )
89
- }
90
-
91
- super_class:
92
- { result = nil }
93
- | kLT class_name {
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)
100
- }
101
- | kLT class_name kLBRACKET type_list kRBRACKET {
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)
108
- }
109
-
110
- module_decl:
111
- annotations kMODULE start_new_scope class_name module_type_params colon_module_self_types class_members kEND {
112
- reset_variable_scope
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
-
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
- )
139
- result = Declarations::Module.new(
140
- name: val[3].value,
141
- type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
142
- self_types: val[5].value,
143
- members: val[6],
144
- annotations: val[0],
145
- location: location,
146
- comment: leading_comment(val[0].first&.location || location)
147
- )
148
- }
149
- | annotations kMODULE start_new_scope namespace tUKEYWORD module_self_types class_members kEND {
150
- reset_variable_scope
151
-
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
-
167
- result = Declarations::Module.new(
168
- name: RBS::TypeName.new(name: val[4].value, namespace: val[3]&.value || RBS::Namespace.empty),
169
- type_params: Declarations::ModuleTypeParams.empty,
170
- self_types: val[5],
171
- members: val[6],
172
- annotations: val[0],
173
- location: location,
174
- comment: leading_comment(val[0].first&.location || location)
175
- )
176
- }
177
-
178
- colon_module_self_types:
179
- { result = LocatedValue.new(value: [], location: nil) }
180
- | kCOLON module_self_types {
181
- result = LocatedValue.new(value: val[1], location: val[0].location)
182
- }
183
-
184
- module_self_types:
185
- module_self_type {
186
- result = [val[0]]
187
- }
188
- | module_self_types kCOMMA module_self_type {
189
- result = val[0].push(val[2])
190
- }
191
-
192
- module_self_type:
193
- qualified_name kLBRACKET type_list kRBRACKET {
194
- name = val[0].value
195
- args = val[2]
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
- )
201
-
202
- case
203
- when name.class?
204
- result = Declarations::Module::Self.new(name: name, args: args, location: location)
205
- when name.interface?
206
- result = Declarations::Module::Self.new(name: name, args: args, location: location)
207
- else
208
- raise SemanticsError.new("Module self type should be instance or interface", subject: val[0], location: val[0].location)
209
- end
210
- }
211
- | qualified_name {
212
- name = val[0].value
213
- args = []
214
- location = val[0].location.with_children(
215
- required: { name: val[0].location },
216
- optional: { args: nil }
217
- )
218
-
219
- case
220
- when name.class?
221
- result = Declarations::Module::Self.new(name: name, args: args, location: location)
222
- when name.interface?
223
- result = Declarations::Module::Self.new(name: name, args: args, location: location)
224
- else
225
- raise SemanticsError.new("Module self type should be instance or interface", subject: val[0], location: val[0].location)
226
- end
227
- }
228
-
229
- class_members:
230
- { result = [] }
231
- | class_members class_member {
232
- result = val[0].push(val[1])
233
- }
234
-
235
- class_member:
236
- method_member
237
- | include_member
238
- | extend_member
239
- | prepend_member
240
- | var_type_member
241
- | attribute_member
242
- | kPUBLIC {
243
- result = Members::Public.new(location: val[0].location)
244
- }
245
- | kPRIVATE {
246
- result = Members::Private.new(location: val[0].location)
247
- }
248
- | alias_member
249
- | signature
250
-
251
- attribute_kind:
252
- { result = LocatedValue.new(value: :instance, location: nil) }
253
- | kSELF kDOT { result = LocatedValue.new(value: :singleton, location: val[0].location + val[1].location) }
254
-
255
- attribute_member:
256
- annotations kATTRREADER attribute_kind keyword type {
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
- )
263
- result = Members::AttrReader.new(name: val[3].value,
264
- ivar_name: nil,
265
- type: val[4],
266
- kind: val[2].value,
267
- annotations: val[0],
268
- location: location,
269
- comment: leading_comment(val[0].first&.location || location))
270
- }
271
- | annotations kATTRREADER attribute_kind method_name attr_var_opt kCOLON type {
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
- )
289
- result = Members::AttrReader.new(name: val[3].value.to_sym,
290
- ivar_name: ivar_name,
291
- type: val[6],
292
- kind: val[2].value,
293
- annotations: val[0],
294
- location: location,
295
- comment: leading_comment(val[0].first&.location || location))
296
- }
297
- | annotations kATTRWRITER attribute_kind keyword type {
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
- )
304
- result = Members::AttrWriter.new(name: val[3].value,
305
- ivar_name: nil,
306
- kind: val[2].value,
307
- type: val[4],
308
- annotations: val[0],
309
- location: location,
310
- comment: leading_comment(val[0].first&.location || location))
311
- }
312
- | annotations kATTRWRITER attribute_kind method_name attr_var_opt kCOLON type {
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
-
331
- result = Members::AttrWriter.new(name: val[3].value.to_sym,
332
- ivar_name: ivar_name,
333
- kind: val[2].value,
334
- type: val[6],
335
- annotations: val[0],
336
- location: location,
337
- comment: leading_comment(val[0].first&.location || location))
338
- }
339
- | annotations kATTRACCESSOR attribute_kind keyword type {
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
-
347
- result = Members::AttrAccessor.new(name: val[3].value,
348
- ivar_name: nil,
349
- kind: val[2].value,
350
- type: val[4],
351
- annotations: val[0],
352
- location: location,
353
- comment: leading_comment(val[0].first&.location || location))
354
- }
355
- | annotations kATTRACCESSOR attribute_kind method_name attr_var_opt kCOLON type {
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
-
374
- result = Members::AttrAccessor.new(name: val[3].value.to_sym,
375
- ivar_name: ivar_name,
376
- kind: val[2].value,
377
- type: val[6],
378
- annotations: val[0],
379
- location: location,
380
- comment: leading_comment(val[0].first&.location || location))
381
- }
382
-
383
- attr_var_opt:
384
- { result = nil }
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
- }
392
-
393
- var_type_member:
394
- tIVAR kCOLON type {
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
-
400
- result = Members::InstanceVariable.new(
401
- name: val[0].value,
402
- type: val[2],
403
- location: location,
404
- comment: leading_comment(location)
405
- )
406
- }
407
- | tCLASSVAR kCOLON type {
408
- type = val[2]
409
-
410
- if type.is_a?(Types::Variable)
411
- type = Types::ClassInstance.new(
412
- name: TypeName.new(name: type.name, namespace: Namespace.empty),
413
- args: [],
414
- location: type.location
415
- )
416
- end
417
-
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
-
423
- result = Members::ClassVariable.new(
424
- name: val[0].value,
425
- type: type,
426
- location: location,
427
- comment: leading_comment(location)
428
- )
429
- }
430
- | kSELF kDOT tIVAR kCOLON type {
431
- type = val[4]
432
-
433
- if type.is_a?(Types::Variable)
434
- type = Types::ClassInstance.new(
435
- name: TypeName.new(name: type.name, namespace: Namespace.empty),
436
- args: [],
437
- location: type.location
438
- )
439
- end
440
-
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
-
446
- result = Members::ClassInstanceVariable.new(
447
- name: val[2].value,
448
- type: type,
449
- location: location,
450
- comment: leading_comment(location)
451
- )
452
- }
453
-
454
- interface_decl:
455
- annotations kINTERFACE start_new_scope interface_name module_type_params interface_members kEND {
456
- reset_variable_scope
457
-
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
- )
463
- result = Declarations::Interface.new(
464
- name: val[3].value,
465
- type_params: val[4]&.value || Declarations::ModuleTypeParams.empty,
466
- members: val[5],
467
- annotations: val[0],
468
- location: location,
469
- comment: leading_comment(val[0].first&.location || location)
470
- )
471
- }
472
-
473
- interface_members:
474
- { result = [] }
475
- | interface_members interface_member {
476
- result = val[0].push(val[1])
477
- }
478
-
479
- interface_member:
480
- method_member {
481
- unless val[0].kind == :instance
482
- raise SemanticsError.new("Interface cannot have singleton method", subject: val[0], location: val[0].location)
483
- end
484
-
485
- if val[0].types.last == :super
486
- raise SemanticsError.new("Interface method cannot have `super` type", subject: val[0], location: val[0].location)
487
- end
488
-
489
- result = val[0]
490
- }
491
- | include_member {
492
- unless val[0].name.interface?
493
- raise SemanticsError.new("Interface should include an interface", subject: val[0], location: val[0].location)
494
- end
495
-
496
- result = val[0]
497
- }
498
- | alias_member
499
-
500
- include_member:
501
- annotations kINCLUDE qualified_name {
502
- if val[2].value.alias?
503
- raise SemanticsError.new("Should include module or interface", subject: val[2].value, location: val[2].location)
504
- end
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
-
511
- result = Members::Include.new(name: val[2].value,
512
- args: [],
513
- annotations: val[0],
514
- location: location,
515
- comment: leading_comment(val[0].first&.location || location))
516
- }
517
- | annotations kINCLUDE qualified_name kLBRACKET type_list kRBRACKET {
518
- if val[2].value.alias?
519
- raise SemanticsError.new("Should include module or interface", subject: val[2].value, location: val[2].location)
520
- end
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
-
527
- result = Members::Include.new(name: val[2].value,
528
- args: val[4],
529
- annotations: val[0],
530
- location: location,
531
- comment: leading_comment(val[0].first&.location || location))
532
- }
533
-
534
- extend_member:
535
- annotations kEXTEND qualified_name {
536
- if val[2].value.alias?
537
- raise SemanticsError.new("Should extend module or interface", subject: val[2].value, location: val[2].location)
538
- end
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
-
545
- result = Members::Extend.new(name: val[2].value,
546
- args: [],
547
- annotations: val[0],
548
- location: location,
549
- comment: leading_comment(val[0].first&.location || location))
550
- }
551
- | annotations kEXTEND qualified_name kLBRACKET type_list kRBRACKET {
552
- if val[2].value.alias?
553
- raise SemanticsError.new("Should extend module or interface", subject: val[2].value, location: val[2].location)
554
- end
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
-
561
- result = Members::Extend.new(name: val[2].value,
562
- args: val[4],
563
- annotations: val[0],
564
- location: location,
565
- comment: leading_comment(val[0].first&.location || location))
566
- }
567
-
568
- prepend_member:
569
- annotations kPREPEND qualified_name {
570
- unless val[2].value.class?
571
- raise SemanticsError.new("Should prepend module", subject: val[2].value, location: val[2].location)
572
- end
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
-
579
- result = Members::Prepend.new(name: val[2].value,
580
- args: [],
581
- annotations: val[0],
582
- location: location,
583
- comment: leading_comment(val[0].first&.location || location))
584
- }
585
- | annotations kPREPEND qualified_name kLBRACKET type_list kRBRACKET {
586
- unless val[2].value.class?
587
- raise SemanticsError.new("Should prepend module", subject: val[2].value, location: val[2].location)
588
- end
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
-
595
- result = Members::Prepend.new(name: val[2].value,
596
- args: val[4],
597
- annotations: val[0],
598
- location: location,
599
- comment: leading_comment(val[0].first&.location || location))
600
- }
601
-
602
- overload:
603
- { result = nil }
604
- | kOVERLOAD {
605
- RBS.logger.warn "`overload def` syntax is deprecated. Use `...` syntax instead."
606
- result = val[0]
607
- }
608
-
609
- method_member:
610
- annotations attributes overload kDEF method_kind def_name method_types {
611
- location = val[3].location + val[6].last.location
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
-
623
- last_type = val[6].last
624
- if last_type.is_a?(LocatedValue) && last_type.value == :dot3
625
- overload = true
626
- optional_children[:overload] = last_type.location
627
- val[6].pop
628
- else
629
- overload = false
630
- end
631
-
632
- result = Members::MethodDefinition.new(
633
- name: val[5].value,
634
- kind: kind,
635
- types: val[6],
636
- annotations: val[0],
637
- location: location.with_children(required: required_children, optional: optional_children),
638
- comment: leading_comment(val[0].first&.location || val[2]&.location || val[3].location),
639
- overload: overload || !!val[2]
640
- )
641
- }
642
-
643
- attributes:
644
- | attributes kINCOMPATIBLE {
645
- RBS.logger.warn "`incompatible` method attribute is deprecated and ignored."
646
- }
647
-
648
- method_kind:
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) }
652
-
653
- method_types:
654
- method_type { result = [val[0]] }
655
- | kDOT3 { result = [LocatedValue.new(value: :dot3, location: val[0].location)] }
656
- | method_type kBAR method_types {
657
- result = val[2].unshift(val[0])
658
- }
659
-
660
- method_type:
661
- start_merged_scope type_params proc_type {
662
- reset_variable_scope
663
-
664
- location = (val[1] || val[2]).location + val[2].location
665
- type_params = val[1]&.value || []
666
-
667
- type, block = val[2].value
668
-
669
- result = MethodType.new(type_params: type_params,
670
- type: type,
671
- block: block,
672
- location: location)
673
- }
674
-
675
- params_opt:
676
- { result = nil }
677
- | kLPAREN params kRPAREN {
678
- result = LocatedValue.new(value: val[1], location: val[0].location + val[2].location)
679
- }
680
-
681
- block:
682
- kLBRACE simple_function_type kRBRACE {
683
- block = Types::Block.new(type: val[1].value, required: true)
684
- result = LocatedValue.new(value: block, location: val[0].location + val[2].location)
685
- }
686
- | kQUESTION kLBRACE simple_function_type kRBRACE {
687
- block = Types::Block.new(type: val[2].value, required: false)
688
- result = LocatedValue.new(value: block, location: val[0].location + val[3].location)
689
- }
690
-
691
- def_name:
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
- }
700
- | method_name kCOLON {
701
- result = LocatedValue.new(value: val[0].value.to_sym,
702
- location: val[0].location + val[1].location)
703
- }
704
-
705
- method_name:
706
- tOPERATOR
707
- | kAMP | kHAT | kSTAR | kLT | kEXCLAMATION | kSTAR2 | kBAR
708
- | method_name0
709
- | method_name0 kQUESTION {
710
- unless val[0].location.pred?(val[1].location)
711
- raise SyntaxError.new(token_str: "kQUESTION", error_value: val[1])
712
- end
713
-
714
- result = LocatedValue.new(value: "#{val[0].value}?",
715
- location: val[0].location + val[1].location)
716
- }
717
- | method_name0 kEXCLAMATION {
718
- unless val[0].location.pred?(val[1].location)
719
- raise SyntaxError.new(token_str: "kEXCLAMATION", error_value: val[1])
720
- end
721
-
722
- result = LocatedValue.new(value: "#{val[0].value}!",
723
- location: val[0].location + val[1].location)
724
- }
725
- | tQUOTEDMETHOD
726
- | tQUOTEDIDENT
727
- | tWRITE_ATTR
728
-
729
- method_name0: tUIDENT | tLIDENT | tINTERFACEIDENT | tUNDERSCOREIDENT | identifier_keywords
730
-
731
- identifier_keywords:
732
- kCLASS | kVOID | kNIL | kTRUE | kFALSE | kANY | kUNTYPED | kTOP | kBOT | kINSTANCE | kBOOL | kSINGLETON
733
- | kTYPE | kMODULE | kPRIVATE | kPUBLIC | kEND | kINCLUDE | kEXTEND | kPREPEND
734
- | kATTRREADER | kATTRACCESSOR | kATTRWRITER | kDEF | kEXTENSION | kSELF | kINCOMPATIBLE
735
- | kUNCHECKED | kINTERFACE | kALIAS | kOUT | kIN | kOVERLOAD
736
-
737
- module_type_params:
738
- { result = nil }
739
- | kLBRACKET module_type_params0 kRBRACKET {
740
- val[1].each {|p| insert_bound_variable(p.name) }
741
-
742
- result = LocatedValue.new(value: val[1], location: val[0].location + val[2].location)
743
- }
744
-
745
- module_type_params0:
746
- module_type_param {
747
- result = Declarations::ModuleTypeParams.new()
748
- result.add(val[0])
749
- }
750
- | module_type_params0 kCOMMA module_type_param {
751
- result = val[0].add(val[2])
752
- }
753
-
754
- module_type_param:
755
- type_param_check type_param_variance tUIDENT {
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
- )
774
- }
775
-
776
- type_param_variance:
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) }
780
-
781
- type_param_check:
782
- { result = LocatedValue.new(value: false, location: nil) }
783
- | kUNCHECKED { result = LocatedValue.new(value: true, location: val[0].location) }
784
-
785
- type_params:
786
- { result = nil }
787
- | kLBRACKET type_params0 kRBRACKET {
788
- val[1].each {|var| insert_bound_variable(var) }
789
-
790
- result = LocatedValue.new(value: val[1],
791
- location: val[0].location + val[2].location)
792
- }
793
-
794
- type_params0:
795
- tUIDENT {
796
- result = [val[0].value.to_sym]
797
- }
798
- | type_params0 kCOMMA tUIDENT {
799
- result = val[0].push(val[2].value.to_sym)
800
- }
801
-
802
- alias_member:
803
- annotations kALIAS method_name method_name {
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
- )
809
- result = Members::Alias.new(
810
- new_name: val[2].value.to_sym,
811
- old_name: val[3].value.to_sym,
812
- kind: :instance,
813
- annotations: val[0],
814
- location: location,
815
- comment: leading_comment(val[0].first&.location || location)
816
- )
817
- }
818
- | annotations kALIAS kSELF kDOT method_name kSELF kDOT method_name {
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
- )
827
- result = Members::Alias.new(
828
- new_name: val[4].value.to_sym,
829
- old_name: val[7].value.to_sym,
830
- kind: :singleton,
831
- annotations: val[0],
832
- location: location,
833
- comment: leading_comment(val[0].first&.location || location)
834
- )
835
- }
836
-
837
- type_decl:
838
- annotations kTYPE type_alias_name kEQ type {
839
- location = val[1].location + val[4].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
- )
850
- }
851
-
852
- const_decl:
853
- class_name kCOLON type {
854
- location = val[0].location + val[2].location
855
- location = location.with_children(
856
- required: { name: val[0].location, colon: val[1].location }
857
- )
858
- result = Declarations::Constant.new(name: val[0].value,
859
- type: val[2],
860
- location: location,
861
- comment: leading_comment(location))
862
- }
863
- | namespace tUKEYWORD type {
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
-
872
- name = TypeName.new(name: val[1].value, namespace: val[0]&.value || Namespace.empty)
873
- result = Declarations::Constant.new(name: name,
874
- type: val[2],
875
- location: location,
876
- comment: leading_comment(location))
877
- }
878
-
879
- global_decl:
880
- tGLOBALIDENT kCOLON type {
881
- location = val[0].location + val[2].location
882
- location = location.with_children(
883
- required: { name: val[0].location, colon: val[1].location }
884
- )
885
- result = Declarations::Global.new(name: val[0].value.to_sym,
886
- type: val[2],
887
- location: location,
888
- comment: leading_comment(location))
889
- }
890
-
891
- type:
892
- simple_type
893
- | type kBAR type {
894
- types = case l = val[0]
895
- when Types::Union
896
- l.types + [val[2]]
897
- else
898
- [l, val[2]]
899
- end
900
-
901
- result = Types::Union.new(types: types, location: val[0].location + val[2].location)
902
- }
903
- | type kAMP type {
904
- types = case l = val[0]
905
- when Types::Intersection
906
- l.types + [val[2]]
907
- else
908
- [l, val[2]]
909
- end
910
-
911
- result = Types::Intersection.new(types: types,
912
- location: val[0].location + val[2].location)
913
- }
914
-
915
- simple_type:
916
- kVOID {
917
- result = Types::Bases::Void.new(location: val[0].location)
918
- }
919
- | kANY {
920
- RBS.logger.warn "`any` type is deprecated. Use `untyped` instead. (#{val[0].location.to_s})"
921
- result = Types::Bases::Any.new(location: val[0].location)
922
- }
923
- | kUNTYPED {
924
- result = Types::Bases::Any.new(location: val[0].location)
925
- }
926
- | kBOOL {
927
- result = Types::Bases::Bool.new(location: val[0].location)
928
- }
929
- | kNIL {
930
- result = Types::Bases::Nil.new(location: val[0].location)
931
- }
932
- | kTOP {
933
- result = Types::Bases::Top.new(location: val[0].location)
934
- }
935
- | kBOT {
936
- result = Types::Bases::Bottom.new(location: val[0].location)
937
- }
938
- | kSELF {
939
- result = Types::Bases::Self.new(location: val[0].location)
940
- }
941
- | kSELFQ {
942
- result = Types::Optional.new(type: Types::Bases::Self.new(location: val[0].location),
943
- location: val[0].location)
944
- }
945
- | kINSTANCE {
946
- result = Types::Bases::Instance.new(location: val[0].location)
947
- }
948
- | kCLASS {
949
- result = Types::Bases::Class.new(location: val[0].location)
950
- }
951
- | kTRUE {
952
- result = Types::Literal.new(literal: true, location: val[0].location)
953
- }
954
- | kFALSE {
955
- result = Types::Literal.new(literal: false, location: val[0].location)
956
- }
957
- | tINTEGER {
958
- result = Types::Literal.new(literal: val[0].value, location: val[0].location)
959
- }
960
- | tSTRING {
961
- result = Types::Literal.new(literal: val[0].value, location: val[0].location)
962
- }
963
- | tSYMBOL {
964
- result = Types::Literal.new(literal: val[0].value, location: val[0].location)
965
- }
966
- | qualified_name {
967
- name = val[0].value
968
- args = []
969
- location = val[0].location
970
-
971
- case
972
- when name.class?
973
- if is_bound_variable?(name.name)
974
- result = Types::Variable.new(name: name.name, location: location)
975
- else
976
- location = location.with_children(
977
- required: { name: val[0].location },
978
- optional: { args: nil }
979
- )
980
- result = Types::ClassInstance.new(name: name, args: args, location: location)
981
- end
982
- when name.alias?
983
- location = location.with_children(
984
- required: { name: val[0].location },
985
- optional: { args: nil }
986
- )
987
- result = Types::Alias.new(name: name, location: location)
988
- when name.interface?
989
- location = location.with_children(
990
- required: { name: val[0].location },
991
- optional: { args: nil }
992
- )
993
- result = Types::Interface.new(name: name, args: args, location: location)
994
- end
995
- }
996
- | qualified_name kLBRACKET type_list kRBRACKET {
997
- name = val[0].value
998
- args = val[2]
999
- location = val[0].location + val[3].location
1000
-
1001
- case
1002
- when name.class?
1003
- if is_bound_variable?(name.name)
1004
- raise SemanticsError.new("#{name.name} is type variable and cannot be applied", subject: name, location: location)
1005
- end
1006
- location = location.with_children(
1007
- required: { name: val[0].location },
1008
- optional: { args: val[1].location + val[3].location }
1009
- )
1010
- result = Types::ClassInstance.new(name: name, args: args, location: location)
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
- )
1016
- result = Types::Interface.new(name: name, args: args, location: location)
1017
- else
1018
- raise SyntaxError.new(token_str: "kLBRACKET", error_value: val[1])
1019
- end
1020
- }
1021
- | kLBRACKET kRBRACKET {
1022
- location = val[0].location + val[1].location
1023
- result = Types::Tuple.new(types: [], location: location)
1024
- }
1025
- | kLBRACKET type_list comma_opt kRBRACKET {
1026
- location = val[0].location + val[3].location
1027
- types = val[1]
1028
- result = Types::Tuple.new(types: types, location: location)
1029
- }
1030
- | kLPAREN type kRPAREN {
1031
- type = val[1].dup
1032
- type.instance_eval do
1033
- @location = val[0].location + val[2].location
1034
- end
1035
- result = type
1036
- }
1037
- | kSINGLETON kLPAREN class_name kRPAREN {
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)
1043
- }
1044
- | kHAT proc_type {
1045
- type, block = val[1].value
1046
- result = Types::Proc.new(type: type, block: block, location: val[0].location + val[1].location)
1047
- }
1048
- | simple_type kQUESTION {
1049
- result = Types::Optional.new(type: val[0], location: val[0].location + val[1].location)
1050
- }
1051
- | record_type
1052
-
1053
- type_list:
1054
- type {
1055
- result = [val[0]]
1056
- }
1057
- | type_list kCOMMA type {
1058
- result = val[0] + [val[2]]
1059
- }
1060
-
1061
- record_type:
1062
- kLBRACE record_fields comma_opt kRBRACE {
1063
- result = Types::Record.new(
1064
- fields: val[1],
1065
- location: val[0].location + val[3].location
1066
- )
1067
- }
1068
-
1069
- record_fields:
1070
- record_field {
1071
- result = val[0]
1072
- }
1073
- | record_fields kCOMMA record_field {
1074
- result = val[0].merge!(val[2])
1075
- }
1076
-
1077
- record_field:
1078
- tSYMBOL kFATARROW type {
1079
- result = { val[0].value => val[2] }
1080
- }
1081
- | tSTRING kFATARROW type {
1082
- result = { val[0].value => val[2] }
1083
- }
1084
- | tINTEGER kFATARROW type {
1085
- result = { val[0].value => val[2] }
1086
- }
1087
- | keyword type {
1088
- result = { val[0].value => val[1] }
1089
- }
1090
- | identifier_keywords kCOLON type {
1091
- result = { val[0].value => val[2] }
1092
- }
1093
- | tQUOTEDIDENT kCOLON type {
1094
- result = { val[0].value => val[2] }
1095
- }
1096
- | tQUOTEDMETHOD kCOLON type {
1097
- result = { val[0].value => val[2] }
1098
- }
1099
-
1100
- keyword_name:
1101
- keyword
1102
- | identifier_keywords kCOLON {
1103
- result = val[0]
1104
- }
1105
-
1106
- keyword: tLKEYWORD | tUKEYWORD | tLKEYWORD_Q_E | tUKEYWORD_Q_E
1107
-
1108
- proc_type:
1109
- params_opt block kARROW simple_type {
1110
- location = (val[0] || val[1] || val[2]).location + val[3].location
1111
-
1112
- params = val[0]&.value || [[], [], nil, [], {}, {}, nil]
1113
-
1114
- type = Types::Function.new(
1115
- required_positionals: params[0],
1116
- optional_positionals: params[1],
1117
- rest_positionals: params[2],
1118
- trailing_positionals: params[3],
1119
- required_keywords: params[4],
1120
- optional_keywords: params[5],
1121
- rest_keywords: params[6],
1122
- return_type: val[3]
1123
- )
1124
-
1125
- block = val[1].value
1126
-
1127
- result = LocatedValue.new(value: [type, block], location: location)
1128
- }
1129
- | simple_function_type {
1130
- result = LocatedValue.new(value: [val[0].value, nil], location: val[0].location)
1131
- }
1132
-
1133
- simple_function_type:
1134
- kLPAREN params kRPAREN kARROW simple_type {
1135
- location = val[0].location + val[4].location
1136
- type = Types::Function.new(
1137
- required_positionals: val[1][0],
1138
- optional_positionals: val[1][1],
1139
- rest_positionals: val[1][2],
1140
- trailing_positionals: val[1][3],
1141
- required_keywords: val[1][4],
1142
- optional_keywords: val[1][5],
1143
- rest_keywords: val[1][6],
1144
- return_type: val[4],
1145
- )
1146
-
1147
- result = LocatedValue.new(value: type, location: location)
1148
- }
1149
- | kARROW simple_type {
1150
- location = val[0].location + val[1].location
1151
- type = Types::Function.new(
1152
- required_positionals: [],
1153
- optional_positionals: [],
1154
- rest_positionals: nil,
1155
- trailing_positionals: [],
1156
- required_keywords: {},
1157
- optional_keywords: {},
1158
- rest_keywords: nil,
1159
- return_type: val[1]
1160
- )
1161
-
1162
- result = LocatedValue.new(value: type, location: location)
1163
- }
1164
-
1165
- params:
1166
- required_positional kCOMMA params {
1167
- result = val[2]
1168
- result[0].unshift(val[0])
1169
- }
1170
- | required_positional {
1171
- result = empty_params_result
1172
- result[0].unshift(val[0])
1173
- }
1174
- | optional_positional_params
1175
-
1176
- optional_positional_params:
1177
- optional_positional kCOMMA optional_positional_params {
1178
- result = val[2]
1179
- result[1].unshift(val[0])
1180
- }
1181
- | optional_positional {
1182
- result = empty_params_result
1183
- result[1].unshift(val[0])
1184
- }
1185
- | rest_positional_param
1186
-
1187
- rest_positional_param:
1188
- rest_positional kCOMMA trailing_positional_params {
1189
- result = val[2]
1190
- result[2] = val[0]
1191
- }
1192
- | rest_positional {
1193
- result = empty_params_result
1194
- result[2] = val[0]
1195
- }
1196
- | trailing_positional_params
1197
-
1198
- trailing_positional_params:
1199
- required_positional kCOMMA trailing_positional_params {
1200
- result = val[2]
1201
- result[3].unshift(val[0])
1202
- }
1203
- | required_positional {
1204
- result = empty_params_result
1205
- result[3].unshift(val[0])
1206
- }
1207
- | keyword_params
1208
-
1209
- keyword_params:
1210
- {
1211
- result = empty_params_result
1212
- }
1213
- | required_keyword kCOMMA keyword_params {
1214
- result = val[2]
1215
- result[4].merge!(val[0])
1216
- }
1217
- | required_keyword {
1218
- result = empty_params_result
1219
- result[4].merge!(val[0])
1220
- }
1221
- | optional_keyword kCOMMA keyword_params {
1222
- result = val[2]
1223
- result[5].merge!(val[0])
1224
- }
1225
- | optional_keyword {
1226
- result = empty_params_result
1227
- result[5].merge!(val[0])
1228
- }
1229
- | rest_keyword {
1230
- result = empty_params_result
1231
- result[6] = val[0]
1232
- }
1233
-
1234
- required_positional:
1235
- type var_name_opt {
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
- )
1247
- }
1248
-
1249
- optional_positional:
1250
- kQUESTION type var_name_opt {
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
- )
1262
- }
1263
-
1264
- rest_positional:
1265
- kSTAR type var_name_opt {
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
- )
1277
- }
1278
-
1279
- required_keyword:
1280
- keyword_name type var_name_opt {
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
- )
1293
- result = { val[0].value => param }
1294
- }
1295
-
1296
- optional_keyword:
1297
- kQUESTION keyword_name type var_name_opt {
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
- )
1310
- result = { val[1].value => param }
1311
- }
1312
-
1313
- rest_keyword:
1314
- kSTAR2 type var_name_opt {
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
- )
1327
- }
1328
-
1329
- var_name_opt:
1330
- | tLIDENT | tINTERFACEIDENT | tQUOTEDIDENT | tUNDERSCOREIDENT | tPARAMNAME
1331
-
1332
- qualified_name:
1333
- namespace simple_name {
1334
- namespace = val[0]&.value || Namespace.empty
1335
- name = val[1].value.to_sym
1336
- type_name = TypeName.new(namespace: namespace, name: name)
1337
- location = (loc0 = val[0]&.location) ? loc0 + val[1].location : val[1].location
1338
- result = LocatedValue.new(value: type_name, location: location)
1339
- }
1340
-
1341
- simple_name:
1342
- tUIDENT | tLIDENT | tINTERFACEIDENT
1343
-
1344
- interface_name:
1345
- namespace tINTERFACEIDENT {
1346
- namespace = val[0]&.value || Namespace.empty
1347
- name = val[1].value.to_sym
1348
- type_name = TypeName.new(namespace: namespace, name: name)
1349
- location = (loc0 = val[0]&.location) ? loc0 + val[1].location : val[1].location
1350
- result = LocatedValue.new(value: type_name, location: location)
1351
- }
1352
-
1353
- class_name:
1354
- namespace tUIDENT {
1355
- namespace = val[0]&.value || Namespace.empty
1356
- name = val[1].value.to_sym
1357
- type_name = TypeName.new(namespace: namespace, name: name)
1358
- location = (loc0 = val[0]&.location) ? loc0 + val[1].location : val[1].location
1359
- result = LocatedValue.new(value: type_name, location: location)
1360
- }
1361
-
1362
- type_alias_name:
1363
- namespace tLIDENT {
1364
- namespace = val[0]&.value || Namespace.empty
1365
- name = val[1].value.to_sym
1366
- type_name = TypeName.new(namespace: namespace, name: name)
1367
- location = (loc0 = val[0]&.location) ? loc0 + val[1].location : val[1].location
1368
- result = LocatedValue.new(value: type_name, location: location)
1369
- }
1370
-
1371
-
1372
- namespace:
1373
- {
1374
- result = nil
1375
- }
1376
- | kCOLON2 {
1377
- result = LocatedValue.new(value: Namespace.root, location: val[0].location)
1378
- }
1379
- | kCOLON2 tNAMESPACE {
1380
- namespace = Namespace.parse(val[1].value).absolute!
1381
- result = LocatedValue.new(value: namespace, location: val[0].location + val[1].location)
1382
- }
1383
- | tNAMESPACE {
1384
- namespace = Namespace.parse(val[0].value)
1385
- result = LocatedValue.new(value: namespace, location: val[0].location)
1386
- }
1387
-
1388
- comma_opt:
1389
- kCOMMA | # empty
1390
-
1391
- end
1392
-
1393
- ---- inner
1394
-
1395
- Types = RBS::Types
1396
- Namespace = RBS::Namespace
1397
- TypeName = RBS::TypeName
1398
- Declarations = RBS::AST::Declarations
1399
- Members = RBS::AST::Members
1400
- MethodType = RBS::MethodType
1401
- Annotation = RBS::AST::Annotation
1402
-
1403
- class LocatedValue
1404
- attr_reader :location
1405
- attr_reader :value
1406
-
1407
- def initialize(location:, value:)
1408
- @location = location
1409
- @value = value
1410
- end
1411
- end
1412
-
1413
- attr_reader :input
1414
- attr_reader :buffer
1415
- attr_reader :eof_re
1416
-
1417
- def initialize(type, buffer:, eof_re:)
1418
- super()
1419
- @type = type
1420
- @buffer = buffer
1421
- @input = CharScanner.new(buffer.content)
1422
- @eof_re = eof_re
1423
- @eof = false
1424
- @bound_variables_stack = []
1425
- @comments = {}
1426
- @ascii_only = buffer.content.ascii_only?
1427
- end
1428
-
1429
- def start_merged_variables_scope
1430
- set = @bound_variables_stack.last&.dup || Set.new
1431
- @bound_variables_stack.push set
1432
- end
1433
-
1434
- def start_new_variables_scope
1435
- @bound_variables_stack.push Set.new
1436
- end
1437
-
1438
- def reset_variable_scope
1439
- @bound_variables_stack.pop
1440
- end
1441
-
1442
- def insert_bound_variable(var)
1443
- @bound_variables_stack.last << var
1444
- end
1445
-
1446
- def is_bound_variable?(var)
1447
- (@bound_variables_stack.last || Set.new).member?(var)
1448
- end
1449
-
1450
- def self.parse_signature(input, eof_re: nil)
1451
- case input
1452
- when RBS::Buffer
1453
- buffer = input
1454
- else
1455
- buffer = RBS::Buffer.new(name: nil, content: input.to_s)
1456
- end
1457
-
1458
- self.new(:SIGNATURE, buffer: buffer, eof_re: eof_re).do_parse
1459
- end
1460
-
1461
- def self.parse_type(input, variables: [], eof_re: nil)
1462
- case input
1463
- when RBS::Buffer
1464
- buffer = input
1465
- else
1466
- buffer = RBS::Buffer.new(name: nil, content: input.to_s)
1467
- end
1468
-
1469
- self.new(:TYPE, buffer: buffer, eof_re: eof_re).yield_self do |parser|
1470
- parser.start_new_variables_scope
1471
-
1472
- variables.each do |var|
1473
- parser.insert_bound_variable var
1474
- end
1475
-
1476
- parser.do_parse
1477
- ensure
1478
- parser.reset_variable_scope
1479
- end
1480
- end
1481
-
1482
- def self.parse_method_type(input, variables: [], eof_re: nil)
1483
- case input
1484
- when RBS::Buffer
1485
- buffer = input
1486
- else
1487
- buffer = RBS::Buffer.new(name: nil, content: input.to_s)
1488
- end
1489
-
1490
- self.new(:METHODTYPE, buffer: buffer, eof_re: eof_re).yield_self do |parser|
1491
- parser.start_new_variables_scope
1492
-
1493
- variables.each do |var|
1494
- parser.insert_bound_variable var
1495
- end
1496
-
1497
- parser.do_parse
1498
- ensure
1499
- parser.reset_variable_scope
1500
- end
1501
- end
1502
-
1503
- def leading_comment(location)
1504
- @comments[location.start_line-1]
1505
- end
1506
-
1507
- def push_comment(string, location)
1508
- if (comment = leading_comment(location)) && comment.location.start_column == location.start_column
1509
- comment.concat(string: "#{string}\n", location: location)
1510
- @comments[comment.location.end_line] = comment
1511
- else
1512
- new_comment = AST::Comment.new(string: "#{string}\n", location: location)
1513
- @comments[new_comment.location.end_line] = new_comment
1514
- end
1515
- end
1516
-
1517
- def new_token(type, value = input.matched)
1518
- charpos = charpos(input)
1519
- matched = input.matched
1520
-
1521
- if matched
1522
- start_index = charpos - matched.size
1523
- end_index = charpos
1524
-
1525
- location = RBS::Location.new(buffer: buffer,
1526
- start_pos: start_index,
1527
- end_pos: end_index)
1528
-
1529
- [type, LocatedValue.new(location: location, value: value)]
1530
- else
1531
- # scanner hasn't matched yet
1532
- [false, nil]
1533
- end
1534
- end
1535
-
1536
- def charpos(scanner)
1537
- scanner.charpos
1538
- end
1539
-
1540
- def empty_params_result
1541
- [
1542
- [],
1543
- [],
1544
- nil,
1545
- [],
1546
- {},
1547
- {},
1548
- nil
1549
- ]
1550
- end
1551
-
1552
- KEYWORDS = {
1553
- "class" => :kCLASS,
1554
- "type" => :kTYPE,
1555
- "def" => :kDEF,
1556
- "self" => :kSELF,
1557
- "void" => :kVOID,
1558
- "any" => :kANY,
1559
- "untyped" => :kUNTYPED,
1560
- "top" => :kTOP,
1561
- "bot" => :kBOT,
1562
- "instance" => :kINSTANCE,
1563
- "bool" => :kBOOL,
1564
- "nil" => :kNIL,
1565
- "true" => :kTRUE,
1566
- "false" => :kFALSE,
1567
- "singleton" => :kSINGLETON,
1568
- "interface" => :kINTERFACE,
1569
- "end" => :kEND,
1570
- "include" => :kINCLUDE,
1571
- "extend" => :kEXTEND,
1572
- "prepend" => :kPREPEND,
1573
- "module" => :kMODULE,
1574
- "attr_reader" => :kATTRREADER,
1575
- "attr_writer" => :kATTRWRITER,
1576
- "attr_accessor" => :kATTRACCESSOR,
1577
- "public" => :kPUBLIC,
1578
- "private" => :kPRIVATE,
1579
- "alias" => :kALIAS,
1580
- "extension" => :kEXTENSION,
1581
- "incompatible" => :kINCOMPATIBLE,
1582
- "unchecked" => :kUNCHECKED,
1583
- "overload" => :kOVERLOAD,
1584
- "out" => :kOUT,
1585
- "in" => :kIN,
1586
- }
1587
- KEYWORDS_RE = /#{Regexp.union(*KEYWORDS.keys)}\b/
1588
-
1589
- PUNCTS = {
1590
- "===" => :tOPERATOR,
1591
- "==" => :tOPERATOR,
1592
- "=~" => :tOPERATOR,
1593
- "!~" => :tOPERATOR,
1594
- "!=" => :tOPERATOR,
1595
- ">=" => :tOPERATOR,
1596
- "<<" => :tOPERATOR,
1597
- "<=>" => :tOPERATOR,
1598
- "<=" => :tOPERATOR,
1599
- ">>" => :tOPERATOR,
1600
- ">" => :tOPERATOR,
1601
- "~" => :tOPERATOR,
1602
- "+@" => :tOPERATOR,
1603
- "+" => :tOPERATOR,
1604
- "[]=" => :tOPERATOR,
1605
- "[]" => :tOPERATOR,
1606
- "::" => :kCOLON2,
1607
- ":" => :kCOLON,
1608
- "(" => :kLPAREN,
1609
- ")" => :kRPAREN,
1610
- "[" => :kLBRACKET,
1611
- "]" => :kRBRACKET,
1612
- "{" => :kLBRACE,
1613
- "}" => :kRBRACE,
1614
- "," => :kCOMMA,
1615
- "|" => :kBAR,
1616
- "&" => :kAMP,
1617
- "^" => :kHAT,
1618
- "->" => :kARROW,
1619
- "=>" => :kFATARROW,
1620
- "=" => :kEQ,
1621
- "?" => :kQUESTION,
1622
- "!" => :kEXCLAMATION,
1623
- "**" => :kSTAR2,
1624
- "*" => :kSTAR,
1625
- "..." => :kDOT3,
1626
- "." => :kDOT,
1627
- "<" => :kLT,
1628
- "-@" => :tOPERATOR,
1629
- "-" => :tOPERATOR,
1630
- "/" => :tOPERATOR,
1631
- "`" => :tOPERATOR,
1632
- "%" => :tOPERATOR,
1633
- }
1634
- PUNCTS_RE = Regexp.union(*PUNCTS.keys)
1635
-
1636
- ANNOTATION_RE = Regexp.union(/%a\{.*?\}/,
1637
- /%a\[.*?\]/,
1638
- /%a\(.*?\)/,
1639
- /%a\<.*?\>/,
1640
- /%a\|.*?\|/)
1641
-
1642
- escape_sequences = %w[a b e f n r s t v "].map { |l| "\\\\#{l}" }
1643
- DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE = /(#{escape_sequences.join("|")})/
1644
-
1645
- def next_token
1646
- if @type
1647
- type = @type
1648
- @type = nil
1649
- return [:"type_#{type}", nil]
1650
- end
1651
-
1652
- return new_token(false, '') if @eof
1653
-
1654
- while true
1655
- return new_token(false, '') if input.eos?
1656
-
1657
- case
1658
- when input.scan(/\s+/)
1659
- # skip
1660
- when input.scan(/#(( *)|( ?(?<string>.*)))\n/)
1661
- charpos = charpos(input)
1662
- start_index = charpos - input.matched.size
1663
- end_index = charpos-1
1664
-
1665
- location = RBS::Location.new(buffer: buffer,
1666
- start_pos: start_index,
1667
- end_pos: end_index)
1668
-
1669
- push_comment input[:string] || "", location
1670
- else
1671
- break
1672
- end
1673
- end
1674
-
1675
- case
1676
- when eof_re && input.scan(eof_re)
1677
- @eof = true
1678
- [:tEOF, input.matched]
1679
- when input.scan(/`[a-zA-Z_]\w*`/)
1680
- s = input.matched.yield_self {|s| s[1, s.length-2] }
1681
- new_token(:tQUOTEDIDENT, s)
1682
- when input.scan(/`(\\`|[^` :])+`/)
1683
- s = input.matched.yield_self {|s| s[1, s.length-2] }.gsub(/\\`/, '`')
1684
- new_token(:tQUOTEDMETHOD, s)
1685
- when input.scan(ANNOTATION_RE)
1686
- s = input.matched.yield_self {|s| s[3, s.length-4] }.strip
1687
- new_token(:tANNOTATION, s)
1688
- when input.scan(/self\?/)
1689
- new_token(:kSELFQ, "self?")
1690
- when input.scan(/(([a-zA-Z]\w*)|(_\w+))=/)
1691
- new_token(:tWRITE_ATTR)
1692
- when input.scan(KEYWORDS_RE)
1693
- new_token(KEYWORDS[input.matched], input.matched.to_sym)
1694
- when input.scan(/:((@{,2}|\$)?\w+(\?|\!)?|[|&\/%~`^]|<=>|={2,3}|=~|[<>]{2}|[<>]=?|[-+]@?|\*{1,2}|\[\]=?|![=~]?)\b?/)
1695
- s = input.matched.yield_self {|s| s[1, s.length] }.to_sym
1696
- new_token(:tSYMBOL, s)
1697
- when input.scan(/[+-]?\d[\d_]*/)
1698
- new_token(:tINTEGER, input.matched.to_i)
1699
- when input.scan(PUNCTS_RE)
1700
- new_token(PUNCTS[input.matched])
1701
- when input.scan(/(::)?([A-Z]\w*::)+/)
1702
- new_token(:tNAMESPACE)
1703
- when input.scan(/[a-z_]\w*:/)
1704
- new_token(:tLKEYWORD, input.matched.chop.to_sym)
1705
- when input.scan(/[a-z_]\w*[?!]:/)
1706
- new_token(:tLKEYWORD_Q_E, input.matched.chop.to_sym)
1707
- when input.scan(/[A-Z]\w*:/)
1708
- new_token(:tUKEYWORD, input.matched.chop.to_sym)
1709
- when input.scan(/[A-Z]\w*[?!]:/)
1710
- new_token(:tUKEYWORD_Q_E, input.matched.chop.to_sym)
1711
- when input.scan(/\$([A-Za-z_]\w*|[~*$?!@\/\\;,.=:<>"&`'+]|\d+|-[0-9_A-Za-z])/)
1712
- new_token(:tGLOBALIDENT)
1713
- when input.scan(/@[a-zA-Z_]\w*/)
1714
- new_token(:tIVAR, input.matched.to_sym)
1715
- when input.scan(/@@[a-zA-Z_]\w*/)
1716
- new_token(:tCLASSVAR, input.matched.to_sym)
1717
- when input.scan(/_[A-Z]\w*\b/)
1718
- new_token(:tINTERFACEIDENT)
1719
- when input.scan(/[A-Z]\w*\b/)
1720
- new_token(:tUIDENT)
1721
- when input.scan(/[a-z]\w*\b/)
1722
- new_token(:tLIDENT)
1723
- when input.scan(/_[a-z]\w*\b/)
1724
- new_token(:tUNDERSCOREIDENT)
1725
- when input.scan(/_\w*\b/)
1726
- new_token(:tPARAMNAME)
1727
- when input.scan(/"(\\"|[^"])*"/)
1728
- s = input.matched.yield_self {|s| s[1, s.length - 2] }
1729
- .gsub(DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE) do |match|
1730
- case match
1731
- when '\\a' then "\a"
1732
- when '\\b' then "\b"
1733
- when '\\e' then "\e"
1734
- when '\\f' then "\f"
1735
- when '\\n' then "\n"
1736
- when '\\r' then "\r"
1737
- when '\\s' then "\s"
1738
- when '\\t' then "\t"
1739
- when '\\v' then "\v"
1740
- when '\\"' then '"'
1741
- end
1742
- end
1743
- new_token(:tSTRING, s)
1744
- when input.scan(/'(\\'|[^'])*'/)
1745
- s = input.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\'/, "'")
1746
- new_token(:tSTRING, s)
1747
- else
1748
- text = input.peek(10)
1749
- start_index = charpos(input)
1750
- end_index = start_index + text.length
1751
- location = RBS::Location.new(buffer: buffer, start_pos: start_index, end_pos: end_index)
1752
- raise LexerError.new(input: text, location: location)
1753
- end
1754
- end
1755
-
1756
- def on_error(token_id, error_value, value_stack)
1757
- raise SyntaxError.new(token_str: token_to_str(token_id), error_value: error_value, value_stack: value_stack)
1758
- end
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
-
1770
- class SyntaxError < ParsingError
1771
- attr_reader :token_str, :error_value, :value_stack
1772
-
1773
- def initialize(token_str:, error_value:, value_stack: nil)
1774
- @token_str = token_str
1775
- @error_value = error_value
1776
- @value_stack = value_stack
1777
-
1778
- super "parse error on value: #{error_value.inspect} (#{token_str})"
1779
- end
1780
- end
1781
-
1782
- class SemanticsError < ParsingError
1783
- attr_reader :subject, :location, :original_message
1784
-
1785
- def initialize(message, subject:, location:)
1786
- @subject = subject
1787
- @location = location
1788
- @original_message = message
1789
-
1790
- super "parse error on #{location}: #{message}"
1791
- end
1792
- end
1793
-
1794
- class LexerError < ParsingError
1795
- attr_reader :location, :input
1796
-
1797
- def initialize(input:, location:)
1798
- @input = input
1799
- @location = location
1800
-
1801
- super "Unexpected string: #{input}..."
1802
- end
1803
- end
1804
-
1805
- ---- footer