rbs 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +28 -0
  3. data/.gitignore +12 -0
  4. data/.rubocop.yml +15 -0
  5. data/BSDL +22 -0
  6. data/CHANGELOG.md +9 -0
  7. data/COPYING +56 -0
  8. data/Gemfile +6 -0
  9. data/README.md +93 -0
  10. data/Rakefile +142 -0
  11. data/bin/annotate-with-rdoc +157 -0
  12. data/bin/console +14 -0
  13. data/bin/query-rdoc +103 -0
  14. data/bin/setup +10 -0
  15. data/bin/sort +89 -0
  16. data/bin/test_runner.rb +16 -0
  17. data/docs/CONTRIBUTING.md +97 -0
  18. data/docs/sigs.md +148 -0
  19. data/docs/stdlib.md +152 -0
  20. data/docs/syntax.md +528 -0
  21. data/exe/rbs +7 -0
  22. data/lib/rbs.rb +64 -0
  23. data/lib/rbs/ast/annotation.rb +27 -0
  24. data/lib/rbs/ast/comment.rb +27 -0
  25. data/lib/rbs/ast/declarations.rb +395 -0
  26. data/lib/rbs/ast/members.rb +362 -0
  27. data/lib/rbs/buffer.rb +50 -0
  28. data/lib/rbs/builtin_names.rb +55 -0
  29. data/lib/rbs/cli.rb +558 -0
  30. data/lib/rbs/constant.rb +26 -0
  31. data/lib/rbs/constant_table.rb +150 -0
  32. data/lib/rbs/definition.rb +170 -0
  33. data/lib/rbs/definition_builder.rb +919 -0
  34. data/lib/rbs/environment.rb +281 -0
  35. data/lib/rbs/environment_loader.rb +136 -0
  36. data/lib/rbs/environment_walker.rb +124 -0
  37. data/lib/rbs/errors.rb +187 -0
  38. data/lib/rbs/location.rb +102 -0
  39. data/lib/rbs/method_type.rb +123 -0
  40. data/lib/rbs/namespace.rb +91 -0
  41. data/lib/rbs/parser.y +1344 -0
  42. data/lib/rbs/prototype/rb.rb +553 -0
  43. data/lib/rbs/prototype/rbi.rb +587 -0
  44. data/lib/rbs/prototype/runtime.rb +381 -0
  45. data/lib/rbs/substitution.rb +46 -0
  46. data/lib/rbs/test.rb +26 -0
  47. data/lib/rbs/test/errors.rb +61 -0
  48. data/lib/rbs/test/hook.rb +294 -0
  49. data/lib/rbs/test/setup.rb +58 -0
  50. data/lib/rbs/test/spy.rb +325 -0
  51. data/lib/rbs/test/test_helper.rb +183 -0
  52. data/lib/rbs/test/type_check.rb +254 -0
  53. data/lib/rbs/type_name.rb +70 -0
  54. data/lib/rbs/types.rb +936 -0
  55. data/lib/rbs/variance_calculator.rb +138 -0
  56. data/lib/rbs/vendorer.rb +47 -0
  57. data/lib/rbs/version.rb +3 -0
  58. data/lib/rbs/writer.rb +269 -0
  59. data/lib/ruby/signature.rb +7 -0
  60. data/rbs.gemspec +46 -0
  61. data/stdlib/abbrev/abbrev.rbs +60 -0
  62. data/stdlib/base64/base64.rbs +71 -0
  63. data/stdlib/benchmark/benchmark.rbs +372 -0
  64. data/stdlib/builtin/array.rbs +1997 -0
  65. data/stdlib/builtin/basic_object.rbs +280 -0
  66. data/stdlib/builtin/binding.rbs +177 -0
  67. data/stdlib/builtin/builtin.rbs +45 -0
  68. data/stdlib/builtin/class.rbs +145 -0
  69. data/stdlib/builtin/comparable.rbs +116 -0
  70. data/stdlib/builtin/complex.rbs +400 -0
  71. data/stdlib/builtin/constants.rbs +37 -0
  72. data/stdlib/builtin/data.rbs +5 -0
  73. data/stdlib/builtin/deprecated.rbs +2 -0
  74. data/stdlib/builtin/dir.rbs +413 -0
  75. data/stdlib/builtin/encoding.rbs +607 -0
  76. data/stdlib/builtin/enumerable.rbs +404 -0
  77. data/stdlib/builtin/enumerator.rbs +260 -0
  78. data/stdlib/builtin/errno.rbs +781 -0
  79. data/stdlib/builtin/errors.rbs +582 -0
  80. data/stdlib/builtin/exception.rbs +194 -0
  81. data/stdlib/builtin/false_class.rbs +40 -0
  82. data/stdlib/builtin/fiber.rbs +68 -0
  83. data/stdlib/builtin/fiber_error.rbs +12 -0
  84. data/stdlib/builtin/file.rbs +1076 -0
  85. data/stdlib/builtin/file_test.rbs +59 -0
  86. data/stdlib/builtin/float.rbs +696 -0
  87. data/stdlib/builtin/gc.rbs +243 -0
  88. data/stdlib/builtin/hash.rbs +1029 -0
  89. data/stdlib/builtin/integer.rbs +707 -0
  90. data/stdlib/builtin/io.rbs +683 -0
  91. data/stdlib/builtin/kernel.rbs +576 -0
  92. data/stdlib/builtin/marshal.rbs +161 -0
  93. data/stdlib/builtin/match_data.rbs +271 -0
  94. data/stdlib/builtin/math.rbs +369 -0
  95. data/stdlib/builtin/method.rbs +185 -0
  96. data/stdlib/builtin/module.rbs +1104 -0
  97. data/stdlib/builtin/nil_class.rbs +82 -0
  98. data/stdlib/builtin/numeric.rbs +409 -0
  99. data/stdlib/builtin/object.rbs +824 -0
  100. data/stdlib/builtin/proc.rbs +429 -0
  101. data/stdlib/builtin/process.rbs +1227 -0
  102. data/stdlib/builtin/random.rbs +267 -0
  103. data/stdlib/builtin/range.rbs +226 -0
  104. data/stdlib/builtin/rational.rbs +424 -0
  105. data/stdlib/builtin/rb_config.rbs +57 -0
  106. data/stdlib/builtin/regexp.rbs +1083 -0
  107. data/stdlib/builtin/ruby_vm.rbs +14 -0
  108. data/stdlib/builtin/signal.rbs +55 -0
  109. data/stdlib/builtin/string.rbs +1901 -0
  110. data/stdlib/builtin/string_io.rbs +284 -0
  111. data/stdlib/builtin/struct.rbs +40 -0
  112. data/stdlib/builtin/symbol.rbs +228 -0
  113. data/stdlib/builtin/thread.rbs +1108 -0
  114. data/stdlib/builtin/thread_group.rbs +23 -0
  115. data/stdlib/builtin/time.rbs +1047 -0
  116. data/stdlib/builtin/trace_point.rbs +290 -0
  117. data/stdlib/builtin/true_class.rbs +46 -0
  118. data/stdlib/builtin/unbound_method.rbs +153 -0
  119. data/stdlib/builtin/warning.rbs +17 -0
  120. data/stdlib/coverage/coverage.rbs +62 -0
  121. data/stdlib/csv/csv.rbs +773 -0
  122. data/stdlib/erb/erb.rbs +392 -0
  123. data/stdlib/find/find.rbs +40 -0
  124. data/stdlib/ipaddr/ipaddr.rbs +247 -0
  125. data/stdlib/json/json.rbs +335 -0
  126. data/stdlib/pathname/pathname.rbs +1093 -0
  127. data/stdlib/prime/integer-extension.rbs +23 -0
  128. data/stdlib/prime/prime.rbs +188 -0
  129. data/stdlib/securerandom/securerandom.rbs +9 -0
  130. data/stdlib/set/set.rbs +301 -0
  131. data/stdlib/tmpdir/tmpdir.rbs +53 -0
  132. metadata +292 -0
@@ -0,0 +1,553 @@
1
+ module RBS
2
+ module Prototype
3
+ class RB
4
+ attr_reader :source_decls
5
+ attr_reader :toplevel_members
6
+
7
+ def initialize
8
+ @source_decls = []
9
+ @toplevel_members = []
10
+ end
11
+
12
+ def decls
13
+ decls = []
14
+
15
+ decls.push(*source_decls)
16
+
17
+ unless toplevel_members.empty?
18
+ top = AST::Declarations::Extension.new(
19
+ name: TypeName.new(name: :Object, namespace: Namespace.empty),
20
+ extension_name: :Toplevel,
21
+ members: toplevel_members,
22
+ annotations: [],
23
+ comment: nil,
24
+ location: nil,
25
+ type_params: AST::Declarations::ModuleTypeParams.empty
26
+ )
27
+ decls << top
28
+ end
29
+
30
+ decls.uniq
31
+ end
32
+
33
+ def parse(string)
34
+ comments = Ripper.lex(string).yield_self do |tokens|
35
+ tokens.each.with_object({}) do |token, hash|
36
+ if token[1] == :on_comment
37
+ line = token[0][0]
38
+ body = token[2][2..]
39
+
40
+ body = "\n" if body.empty?
41
+
42
+ comment = AST::Comment.new(string: body, location: nil)
43
+ if (prev_comment = hash[line - 1])
44
+ hash[line - 1] = nil
45
+ hash[line] = AST::Comment.new(string: prev_comment.string + comment.string,
46
+ location: nil)
47
+ else
48
+ hash[line] = comment
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ process RubyVM::AbstractSyntaxTree.parse(string), namespace: Namespace.empty, current_module: nil, comments: comments, singleton: false
55
+ end
56
+
57
+ def nested_name(name)
58
+ (current_namespace + const_to_name(name).to_namespace).to_type_name.relative!
59
+ end
60
+
61
+ def process(node, namespace:, current_module:, comments:, singleton:)
62
+ case node.type
63
+ when :CLASS
64
+ class_name, super_class, *class_body = node.children
65
+ kls = AST::Declarations::Class.new(
66
+ name: const_to_name(class_name).with_prefix(namespace).relative!,
67
+ super_class: super_class && AST::Declarations::Class::Super.new(name: const_to_name(super_class), args: []),
68
+ type_params: AST::Declarations::ModuleTypeParams.empty,
69
+ members: [],
70
+ annotations: [],
71
+ location: nil,
72
+ comment: comments[node.first_lineno - 1]
73
+ )
74
+
75
+ source_decls.push kls
76
+
77
+ each_node class_body do |child|
78
+ process child, namespace: kls.name.to_namespace, current_module: kls, comments: comments, singleton: false
79
+ end
80
+ when :MODULE
81
+ module_name, *module_body = node.children
82
+
83
+ mod = AST::Declarations::Module.new(
84
+ name: const_to_name(module_name).with_prefix(namespace).relative!,
85
+ type_params: AST::Declarations::ModuleTypeParams.empty,
86
+ self_type: nil,
87
+ members: [],
88
+ annotations: [],
89
+ location: nil,
90
+ comment: comments[node.first_lineno - 1]
91
+ )
92
+
93
+ source_decls.push mod
94
+
95
+ each_node module_body do |child|
96
+ process child, namespace: mod.name.to_namespace, current_module: mod, comments: comments, singleton: false
97
+ end
98
+ when :SCLASS
99
+ this = node.children[0]
100
+ if this.type != :SELF
101
+ RBS.logger.warn "`class <<` syntax with not-self may be compiled to incorrect code: #{this}"
102
+ end
103
+
104
+ body = node.children[1]
105
+ each_child(body) do |child|
106
+ process child, namespace: namespace, current_module: current_module, comments: comments, singleton: true
107
+ end
108
+ when :DEFN, :DEFS
109
+ if node.type == :DEFN
110
+ def_name, def_body = node.children
111
+ kind = singleton ? :singleton : :instance
112
+ else
113
+ _, def_name, def_body = node.children
114
+ kind = :singleton
115
+ end
116
+
117
+ types = [
118
+ MethodType.new(
119
+ type_params: [],
120
+ type: function_type_from_body(def_body),
121
+ block: block_from_body(def_body),
122
+ location: nil
123
+ )
124
+ ]
125
+
126
+ member = AST::Members::MethodDefinition.new(
127
+ name: def_name,
128
+ location: nil,
129
+ annotations: [],
130
+ types: types,
131
+ kind: kind,
132
+ comment: comments[node.first_lineno - 1],
133
+ attributes: []
134
+ )
135
+
136
+ if current_module
137
+ current_module.members.push member
138
+ else
139
+ toplevel_members.push member
140
+ end
141
+ when :FCALL
142
+ if current_module
143
+ # Inside method definition cannot reach here.
144
+ args = node.children[1]&.children || []
145
+
146
+ case node.children[0]
147
+ when :include
148
+ args.each do |arg|
149
+ if (name = const_to_name(arg))
150
+ current_module.members << AST::Members::Include.new(
151
+ name: name,
152
+ args: [],
153
+ annotations: [],
154
+ location: nil,
155
+ comment: comments[node.first_lineno - 1]
156
+ )
157
+ end
158
+ end
159
+ when :extend
160
+ args.each do |arg|
161
+ if (name = const_to_name(arg))
162
+ current_module.members << AST::Members::Extend.new(
163
+ name: name,
164
+ args: [],
165
+ annotations: [],
166
+ location: nil,
167
+ comment: comments[node.first_lineno - 1]
168
+ )
169
+ end
170
+ end
171
+ when :attr_reader
172
+ args.each do |arg|
173
+ if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
174
+ current_module.members << AST::Members::AttrReader.new(
175
+ name: arg.children[0],
176
+ ivar_name: nil,
177
+ type: Types::Bases::Any.new(location: nil),
178
+ location: nil,
179
+ comment: comments[node.first_lineno - 1],
180
+ annotations: []
181
+ )
182
+ end
183
+ end
184
+ when :attr_accessor
185
+ args.each do |arg|
186
+ if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
187
+ current_module.members << AST::Members::AttrAccessor.new(
188
+ name: arg.children[0],
189
+ ivar_name: nil,
190
+ type: Types::Bases::Any.new(location: nil),
191
+ location: nil,
192
+ comment: comments[node.first_lineno - 1],
193
+ annotations: []
194
+ )
195
+ end
196
+ end
197
+ when :attr_writer
198
+ args.each do |arg|
199
+ if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
200
+ current_module.members << AST::Members::AttrWriter.new(
201
+ name: arg.children[0],
202
+ ivar_name: nil,
203
+ type: Types::Bases::Any.new(location: nil),
204
+ location: nil,
205
+ comment: comments[node.first_lineno - 1],
206
+ annotations: []
207
+ )
208
+ end
209
+ end
210
+ end
211
+ end
212
+ each_child node do |child|
213
+ process child, namespace: namespace, current_module: current_module, comments: comments, singleton: singleton
214
+ end
215
+
216
+ when :CDECL
217
+ type_name = case
218
+ when node.children[0].is_a?(Symbol)
219
+ ns = if current_module
220
+ current_module.name.to_namespace
221
+ else
222
+ Namespace.empty
223
+ end
224
+ TypeName.new(name: node.children[0], namespace: ns)
225
+ else
226
+ name = const_to_name(node.children[0])
227
+ if current_module
228
+ name.with_prefix current_module.name.to_namespace
229
+ else
230
+ name
231
+ end.relative!
232
+ end
233
+
234
+ source_decls << AST::Declarations::Constant.new(
235
+ name: type_name,
236
+ type: node_type(node.children.last),
237
+ location: nil,
238
+ comment: comments[node.first_lineno - 1]
239
+ )
240
+ else
241
+ each_child node do |child|
242
+ process child, namespace: namespace, current_module: current_module, comments: comments, singleton: singleton
243
+ end
244
+ end
245
+ end
246
+
247
+ def const_to_name(node)
248
+ case node&.type
249
+ when :CONST
250
+ TypeName.new(name: node.children[0], namespace: Namespace.empty)
251
+ when :COLON2
252
+ if node.children[0]
253
+ namespace = const_to_name(node.children[0]).to_namespace
254
+ else
255
+ namespace = Namespace.empty
256
+ end
257
+
258
+ TypeName.new(name: node.children[1], namespace: namespace)
259
+ when :COLON3
260
+ TypeName.new(name: node.children[0], namespace: Namespace.root)
261
+ end
262
+ end
263
+
264
+ def each_node(nodes)
265
+ nodes.each do |child|
266
+ if child.is_a?(RubyVM::AbstractSyntaxTree::Node)
267
+ yield child
268
+ end
269
+ end
270
+ end
271
+
272
+ def each_child(node, &block)
273
+ each_node node.children, &block
274
+ end
275
+
276
+ def function_type_from_body(node)
277
+ table_node, args_node, *_ = node.children
278
+
279
+ pre_num, _pre_init, opt, _first_post, post_num, _post_init, rest, kw, kwrest, _block = args_node.children
280
+
281
+ return_type = function_return_type_from_body(node)
282
+
283
+ fun = Types::Function.empty(return_type)
284
+
285
+ table_node.take(pre_num).each do |name|
286
+ fun.required_positionals << Types::Function::Param.new(name: name, type: untyped)
287
+ end
288
+
289
+ while opt&.type == :OPT_ARG
290
+ lvasgn, opt = opt.children
291
+ name = lvasgn.children[0]
292
+ fun.optional_positionals << Types::Function::Param.new(
293
+ name: name,
294
+ type: node_type(lvasgn.children[1])
295
+ )
296
+ end
297
+
298
+ if rest
299
+ fun = fun.update(rest_positionals: Types::Function::Param.new(name: rest, type: untyped))
300
+ end
301
+
302
+ table_node.drop(fun.required_positionals.size + fun.optional_positionals.size + (fun.rest_positionals ? 1 : 0)).take(post_num).each do |name|
303
+ fun.trailing_positionals << Types::Function::Param.new(name: name, type: untyped)
304
+ end
305
+
306
+ while kw
307
+ lvasgn, kw = kw.children
308
+ name, value = lvasgn.children
309
+
310
+ case value
311
+ when nil, :NODE_SPECIAL_REQUIRED_KEYWORD
312
+ fun.required_keywords[name] = Types::Function::Param.new(name: name, type: untyped)
313
+ when RubyVM::AbstractSyntaxTree::Node
314
+ fun.optional_keywords[name] = Types::Function::Param.new(name: name, type: node_type(value))
315
+ else
316
+ raise "Unexpected keyword arg value: #{value}"
317
+ end
318
+ end
319
+
320
+ if kwrest && kwrest.children.any?
321
+ fun = fun.update(rest_keywords: Types::Function::Param.new(name: kwrest.children[0], type: untyped))
322
+ end
323
+
324
+ fun
325
+ end
326
+
327
+ def function_return_type_from_body(node)
328
+ body = node.children[2]
329
+ return Types::Bases::Nil.new(location: nil) unless body
330
+
331
+ literal_to_type(body)
332
+ end
333
+
334
+ def literal_to_type(node)
335
+ case node.type
336
+ when :STR
337
+ lit = node.children[0]
338
+ if lit.match?(/\A[ -~]+\z/)
339
+ Types::Literal.new(literal: lit, location: nil)
340
+ else
341
+ BuiltinNames::String.instance_type
342
+ end
343
+ when :DSTR, :XSTR
344
+ BuiltinNames::String.instance_type
345
+ when :DSYM
346
+ BuiltinNames::Symbol.instance_type
347
+ when :DREGX
348
+ BuiltinNames::Regexp.instance_type
349
+ when :TRUE
350
+ BuiltinNames::TrueClass.instance_type
351
+ when :FALSE
352
+ BuiltinNames::FalseClass.instance_type
353
+ when :NIL
354
+ Types::Bases::Nil.new(location: nil)
355
+ when :LIT
356
+ lit = node.children[0]
357
+ name = lit.class.name
358
+ case lit
359
+ when Symbol
360
+ if lit.match?(/\A[ -~]+\z/)
361
+ Types::Literal.new(literal: lit, location: nil)
362
+ else
363
+ BuiltinNames::Symbol.instance_type
364
+ end
365
+ when Integer
366
+ Types::Literal.new(literal: lit, location: nil)
367
+ else
368
+ type_name = TypeName.new(name: name, namespace: Namespace.root)
369
+ Types::ClassInstance.new(name: type_name, args: [], location: nil)
370
+ end
371
+ when :ZLIST, :ZARRAY
372
+ BuiltinNames::Array.instance_type([untyped])
373
+ when :LIST, :ARRAY
374
+ elem_types = node.children.compact.map { |e| literal_to_type(e) }
375
+ t = types_to_union_type(elem_types)
376
+ BuiltinNames::Array.instance_type([t])
377
+ when :DOT2, :DOT3
378
+ types = node.children.map { |c| literal_to_type(c) }
379
+ type = range_element_type(types)
380
+ BuiltinNames::Range.instance_type([type])
381
+ when :HASH
382
+ list = node.children[0]
383
+ if list
384
+ children = list.children.compact
385
+ else
386
+ children = []
387
+ end
388
+
389
+ key_types = []
390
+ value_types = []
391
+ children.each_slice(2) do |k, v|
392
+ key_types << literal_to_type(k)
393
+ value_types << literal_to_type(v)
394
+ end
395
+
396
+ if key_types.all? { |t| t.is_a?(Types::Literal) }
397
+ fields = key_types.map { |t| t.literal }.zip(value_types).to_h
398
+ Types::Record.new(fields: fields, location: nil)
399
+ else
400
+ key_type = types_to_union_type(key_types)
401
+ value_type = types_to_union_type(value_types)
402
+ BuiltinNames::Hash.instance_type([key_type, value_type])
403
+ end
404
+ else
405
+ untyped
406
+ end
407
+ end
408
+
409
+ def types_to_union_type(types)
410
+ return untyped if types.empty?
411
+ return untyped if types.include?(untyped)
412
+
413
+ Types::Union.new(types: types.uniq, location: nil)
414
+ end
415
+
416
+ def range_element_type(types)
417
+ types = types.reject { |t| t == untyped }
418
+ return untyped if types.empty?
419
+
420
+ types = types.map do |t|
421
+ if t.is_a?(Types::Literal)
422
+ type_name = TypeName.new(name: t.literal.class.name, namespace: Namespace.root)
423
+ Types::ClassInstance.new(name: type_name, args: [], location: nil)
424
+ else
425
+ t
426
+ end
427
+ end.uniq
428
+
429
+ if types.size == 1
430
+ types.first
431
+ else
432
+ untyped
433
+ end
434
+ end
435
+
436
+ def block_from_body(node)
437
+ _, args_node, body_node = node.children
438
+
439
+ _pre_num, _pre_init, _opt, _first_post, _post_num, _post_init, _rest, _kw, _kwrest, block = args_node.children
440
+
441
+ method_block = nil
442
+
443
+ if block
444
+ method_block = MethodType::Block.new(
445
+ required: true,
446
+ type: Types::Function.empty(untyped)
447
+ )
448
+ end
449
+
450
+ if body_node
451
+ if (yields = any_node?(body_node) {|n| n.type == :YIELD })
452
+ method_block = MethodType::Block.new(
453
+ required: true,
454
+ type: Types::Function.empty(untyped)
455
+ )
456
+
457
+ yields.each do |yield_node|
458
+ array_content = yield_node.children[0]&.children&.compact || []
459
+
460
+ positionals, keywords = if keyword_hash?(array_content.last)
461
+ [array_content.take(array_content.size - 1), array_content.last]
462
+ else
463
+ [array_content, nil]
464
+ end
465
+
466
+ if (diff = positionals.size - method_block.type.required_positionals.size) > 0
467
+ diff.times do
468
+ method_block.type.required_positionals << Types::Function::Param.new(
469
+ type: untyped,
470
+ name: nil
471
+ )
472
+ end
473
+ end
474
+
475
+ if keywords
476
+ keywords.children[0].children.each_slice(2) do |key_node, value_node|
477
+ if key_node
478
+ key = key_node.children[0]
479
+ method_block.type.required_keywords[key] ||=
480
+ Types::Function::Param.new(
481
+ type: untyped,
482
+ name: nil
483
+ )
484
+ end
485
+ end
486
+ end
487
+ end
488
+ end
489
+ end
490
+
491
+ method_block
492
+ end
493
+
494
+ def keyword_hash?(node)
495
+ if node
496
+ if node.type == :HASH
497
+ node.children[0].children.compact.each_slice(2).all? {|key, _|
498
+ key.type == :LIT && key.children[0].is_a?(Symbol)
499
+ }
500
+ end
501
+ end
502
+ end
503
+
504
+ def any_node?(node, nodes: [], &block)
505
+ if yield(node)
506
+ nodes << node
507
+ end
508
+
509
+ each_child node do |child|
510
+ any_node? child, nodes: nodes, &block
511
+ end
512
+
513
+ nodes.empty? ? nil : nodes
514
+ end
515
+
516
+ def node_type(node, default: Types::Bases::Any.new(location: nil))
517
+ case node.type
518
+ when :LIT
519
+ case node.children[0]
520
+ when Symbol
521
+ BuiltinNames::Symbol.instance_type
522
+ when Integer
523
+ BuiltinNames::Integer.instance_type
524
+ when Float
525
+ BuiltinNames::Float.instance_type
526
+ else
527
+ default
528
+ end
529
+ when :STR, :DSTR
530
+ BuiltinNames::String.instance_type
531
+ when :NIL
532
+ # This type is technical non-sense, but may help practically.
533
+ Types::Optional.new(
534
+ type: Types::Bases::Any.new(location: nil),
535
+ location: nil
536
+ )
537
+ when :TRUE, :FALSE
538
+ Types::Bases::Bool.new(location: nil)
539
+ when :ARRAY, :LIST
540
+ BuiltinNames::Array.instance_type(default)
541
+ when :HASH
542
+ BuiltinNames::Hash.instance_type(default, default)
543
+ else
544
+ default
545
+ end
546
+ end
547
+
548
+ def untyped
549
+ @untyped ||= Types::Bases::Any.new(location: nil)
550
+ end
551
+ end
552
+ end
553
+ end