Class: RBS::Inline::Parser

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/rbs/inline/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

: void



43
44
45
46
47
48
# File 'lib/rbs/inline/parser.rb', line 43

def initialize() #: void
  @decls = []
  @surrounding_decls = []
  @comments = {}
  @current_module_function = false
end

Instance Attribute Details

#commentsObject (readonly)

ParsingResult associated with the line number at the end

“‘rb # Hello # world <= The comments hash includes `2` (line 2) to the two lines “`

> [!IMPORTANT] > The values will be removed during parsing.



31
32
33
# File 'lib/rbs/inline/parser.rb', line 31

def comments
  @comments
end

#current_module_functionObject (readonly)

The current module_function applied to single ‘def` node



41
42
43
# File 'lib/rbs/inline/parser.rb', line 41

def current_module_function
  @current_module_function
end

#current_visibilityObject (readonly)

The current visibility applied to single ‘def` node

Assuming it’s directly inside ‘private` or `public` calls. `nil` when the `def` node is not inside `private` or `public` calls.



38
39
40
# File 'lib/rbs/inline/parser.rb', line 38

def current_visibility
  @current_visibility
end

#declsObject (readonly)

The top level declarations



15
16
17
# File 'lib/rbs/inline/parser.rb', line 15

def decls
  @decls
end

#surrounding_declsObject (readonly)

The surrounding declarations



19
20
21
# File 'lib/rbs/inline/parser.rb', line 19

def surrounding_decls
  @surrounding_decls
end

Class Method Details

.parse(result, opt_in:) ⇒ Object

Parses the given Prism result to a three tuple

Returns a three tuple of:

  1. An array of ‘use` directives

  2. An array of declarations

  3. An array of RBS declarations given as ‘@rbs!` annotation at top-level

Note that only RBS declarations are allowed in the top-level ‘@rbs!` annotations. RBS members are ignored in the array.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rbs/inline/parser.rb', line 64

def self.parse(result, opt_in:)
  instance = Parser.new()

  annots = AnnotationParser.parse(result.comments)
  annots.each do |result|
    instance.comments[result.line_range.end] = result
  end

  with_enable_magic_comment = result.comments.any? {|comment| comment.location.slice =~ /\A# rbs_inline: enabled\Z/}
  with_disable_magic_comment = result.comments.any? {|comment| comment.location.slice =~ /\A# rbs_inline: disabled\Z/}

  return if with_disable_magic_comment # Skips if `rbs_inline: disabled`

  if opt_in
    # opt-in means the `rbs_inline: enable` is required.
    return unless with_enable_magic_comment
  end

  uses = [] #: Array[AST::Annotations::Use]
  annots.each do |annot|
    annot.each_annotation do |annotation|
      if annotation.is_a?(AST::Annotations::Use)
        uses << annotation
      end
    end
  end

  instance.visit(result.value)

  rbs_embeddeds = [] #: Array[AST::Members::RBSEmbedded]

  instance.comments.each_value do |comment|
    comment.each_annotation do |annotation|
      if annotation.is_a?(AST::Annotations::Embedded)
        rbs_embeddeds << AST::Members::RBSEmbedded.new(comment, annotation)
      end
    end
  end

  rbs_decls = rbs_embeddeds.flat_map do |embedded|
    if (members = embedded.members).is_a?(Array)
      members.select do |member|
        member.is_a?(RBS::AST::Declarations::Base)
      end
    else
      []
    end #: Array[RBS::AST::Declarations::t]
  end

  [
    uses,
    instance.decls,
    rbs_decls
  ]
end

Instance Method Details

#application_annotation(node) ⇒ Object

Fetch Application annotation which is associated to ‘node`

The application annotation is removed from ‘comments`.



405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/rbs/inline/parser.rb', line 405

def application_annotation(node)
  comment_line, app_comment = comments.find do |_, comment|
    comment.line_range.begin == node.location.end_line
  end

  if app_comment && comment_line
    comments.delete(comment_line)
    app_comment.each_annotation.find do |annotation|
      annotation.is_a?(AST::Annotations::Application)
    end #: AST::Annotations::Application?
  end
end

#assertion_annotation(node) ⇒ Object

Fetch TypeAssertion annotation which is associated to ‘node`

The assertion annotation is removed from ‘comments`.



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/rbs/inline/parser.rb', line 424

def assertion_annotation(node)
  if node.is_a?(Prism::Location)
    location = node
  else
    location = node.location
  end
  comment_line, app_comment = comments.find do |_, comment|
    comment.line_range.begin == location.end_line
  end

  if app_comment && comment_line
    comments.delete(comment_line)
    app_comment.each_annotation.find do |annotation|
      annotation.is_a?(AST::Annotations::TypeAssertion)
    end #: AST::Annotations::TypeAssertion?
  end
end

#current_class_module_declObject



121
122
123
# File 'lib/rbs/inline/parser.rb', line 121

def current_class_module_decl
  surrounding_decls.last
end

#current_class_module_decl!Object



126
127
128
# File 'lib/rbs/inline/parser.rb', line 126

def current_class_module_decl!
  current_class_module_decl or raise
end

#ignored_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


391
392
393
394
395
396
397
# File 'lib/rbs/inline/parser.rb', line 391

def ignored_node?(node)
  if comment = comments.fetch(node.location.start_line - 1, nil)
    comment.each_annotation.any? { _1.is_a?(AST::Annotations::Skip) }
  else
    false
  end
end

#inner_annotations(start_line, end_line) ⇒ Object

Returns an array of annotations from comments that is located between start_line and end_line

“‘rb module Foo # line 1 (start_line)

# foo
# bar

end # line 4 (end_line) “‘



236
237
238
239
240
241
242
243
244
245
# File 'lib/rbs/inline/parser.rb', line 236

def inner_annotations(start_line, end_line) #: Array[AnnotationParser::ParsingResult]
  annotations = comments.each_value.select do |annotation|
    range = annotation.line_range
    start_line < range.begin && range.end < end_line
  end

  annotations.each do |annot|
    comments.delete(annot.line_range.end)
  end
end

#load_inner_annotations(start_line, end_line, members) ⇒ Object

Load inner declarations and delete them from ‘#comments` hash

It also sorts the ‘members` by `#start_line“ ascending.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rbs/inline/parser.rb', line 157

def load_inner_annotations(start_line, end_line, members) #: void
  comments = inner_annotations(start_line, end_line)

  comments.each do |comment|
    comment.each_annotation do |annotation|
      case annotation
      when AST::Annotations::IvarType
        members << AST::Members::RBSIvar.new(comment, annotation)
      when AST::Annotations::Embedded
        members << AST::Members::RBSEmbedded.new(comment, annotation)
      end
    end
  end

  members.sort_by! { _1.start_line }
end

#process_nesting_node(node) ⇒ Object



380
381
382
383
384
385
386
387
# File 'lib/rbs/inline/parser.rb', line 380

def process_nesting_node(node)
  yield unless ignored_node?(node)
ensure
  # Delete all inner annotations
  inner_annotations(node.location.start_line, node.location.end_line)
  comments.delete(node.location.start_line)
  comments.delete(node.location.end_line)
end

#push_class_module_decl(decl) ⇒ Object

: (with_members) { () -> void } -> void



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rbs/inline/parser.rb', line 131

def push_class_module_decl(decl)
  if current = current_class_module_decl
    current.members << decl
  else
    decls << decl
  end

  if block_given?
    surrounding_decls.push(decl)
    begin
      yield
    ensure
      surrounding_decls.pop()
    end
  end
end

#push_visibility(new_visibility, &block) ⇒ Object



368
369
370
371
372
373
374
375
376
377
# File 'lib/rbs/inline/parser.rb', line 368

def push_visibility(new_visibility, &block)
  old_visibility = current_visibility

  begin
    @current_visibility = new_visibility
    yield
  ensure
    @current_visibility = old_visibility
  end
end

#visit_alias_method_node(node) ⇒ Object



265
266
267
268
269
270
271
272
273
274
# File 'lib/rbs/inline/parser.rb', line 265

def visit_alias_method_node(node)
  return if ignored_node?(node)
  return unless current_class_module_decl

  if node.location
    comment = comments.delete(node.location.start_line - 1)
  end
  current_class_module_decl!.members << AST::Members::RubyAlias.new(node, comment)
  super
end

#visit_block_node(node) ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/rbs/inline/parser.rb', line 488

def visit_block_node(node)
  process_nesting_node(node) do
    comment = comments.delete(node.location.start_line - 1)
    block = AST::Declarations::BlockDecl.new(node, comment)

    push_class_module_decl(block) do
      super
    end

    load_inner_annotations(node.location.start_line, node.location.end_line, block.members)
  end
end

#visit_call_node(node) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/rbs/inline/parser.rb', line 277

def visit_call_node(node)
  return if ignored_node?(node)
  return super unless current_class_module_decl

  case node.name
  when :include, :prepend, :extend
    case node.receiver
    when nil, Prism::SelfNode
      comment = comments.delete(node.location.start_line - 1)
      app = application_annotation(node)

      current_class_module_decl!.members << AST::Members::RubyMixin.new(node, comment, app)

      return
    end
  when :attr_reader, :attr_accessor, :attr_writer
    case node.receiver
    when nil, Prism::SelfNode
      comment = comments.delete(node.location.start_line - 1)

      comment_line, assertion_comment = comments.find do |_, comment|
        comment.line_range.begin == node.location.end_line
      end
      if assertion_comment && comment_line
        comments.delete(comment_line)
        assertion = assertion_comment.each_annotation.find do |annotation|
          annotation.is_a?(AST::Annotations::TypeAssertion)
        end #: AST::Annotations::TypeAssertion?
      end

      current_class_module_decl!.members << AST::Members::RubyAttr.new(node, comment, current_visibility, assertion)

      return
    end
  when :public, :private
    case node.receiver
    when nil, Prism::SelfNode
      if node.arguments && node.arguments.arguments.size > 0
        if node.name == :public
          push_visibility(:public) { super }
        end

        if node.name == :private
          push_visibility(:private) { super }
        end

        return
      else
        if node.name == :public
          current_class_module_decl!.members << AST::Members::RubyPublic.new(node)
          return
        end

        if node.name == :private
          current_class_module_decl!.members << AST::Members::RubyPrivate.new(node)
          return
        end
      end
    end
  when :module_function
    if node.arguments && node.arguments.arguments.size > 0
      args = node.arguments.arguments.filter_map do |arg|
        case arg
        when Prism::SymbolNode
          arg.unescaped.to_sym
        end
      end

      current_decl = current_class_module_decl

      if current_decl
        node.arguments.arguments.each do |arg|
          current_decl.members.each do |member|
            if member.is_a?(AST::Members::RubyDef) && args.include?(member.node.name)
              member.singleton_instance = true
              break
            end
          end
        end
      end
    else
      @current_module_function = true
    end
  end

  super
end

#visit_class_node(node) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rbs/inline/parser.rb', line 175

def visit_class_node(node)
  process_nesting_node(node) do
    visit node.constant_path
    visit node.superclass

    associated_comment = comments.delete(node.location.start_line - 1)
    if node.superclass
      app_comment = application_annotation(node.superclass)
    end

    class_decl = AST::Declarations::ClassDecl.new(node, associated_comment, app_comment)

    push_class_module_decl(class_decl) do
      visit node.body
    end

    load_inner_annotations(node.location.start_line, node.location.end_line, class_decl.members)
  end
end

#visit_constant_write_node(node) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/rbs/inline/parser.rb', line 443

def visit_constant_write_node(node)
  return if ignored_node?(node)

  comment = comments.delete(node.location.start_line - 1)

  case
  when data_node = AST::Declarations::DataAssignDecl.data_define?(node)
    type_decls = {} #: Hash[Integer, AST::Annotations::TypeAssertion]

    inner_annotations(node.location.start_line, node.location.end_line).flat_map do |comment|
      comment.each_annotation do |annotation|
        if annotation.is_a?(AST::Annotations::TypeAssertion)
          start_line = annotation.source.comments[0].location.start_line
          type_decls[start_line] = annotation
        end
      end
    end

    decl = AST::Declarations::DataAssignDecl.new(node, data_node, comment, type_decls)
  when struct_node = AST::Declarations::StructAssignDecl.struct_new?(node)
    type_decls = {} #: Hash[Integer, AST::Annotations::TypeAssertion]

    inner_annotations(node.location.start_line, node.location.end_line).flat_map do |comment|
      comment.each_annotation do |annotation|
        if annotation.is_a?(AST::Annotations::TypeAssertion)
          start_line = annotation.source.comments[0].location.start_line
          type_decls[start_line] = annotation
        end
      end
    end

    decl = AST::Declarations::StructAssignDecl.new(node, struct_node, comment, type_decls)
  else
    assertion = assertion_annotation(node)
    decl = AST::Declarations::ConstantDecl.new(node, comment, assertion)
  end

  if current = current_class_module_decl
    current.members << decl
  else
    decls << decl
  end
end

#visit_def_node(node) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/rbs/inline/parser.rb', line 248

def visit_def_node(node)
  process_nesting_node(node) do
    return unless current_class_module_decl

    current_decl = current_class_module_decl!

    if node.location
      associated_comment = comments.delete(node.location.start_line - 1)
    end

    assertion = assertion_annotation(node.rparen_loc || node&.parameters&.location || node.name_loc)

    current_decl.members << AST::Members::RubyDef.new(node, associated_comment, current_visibility, current_module_function, assertion)
  end
end

#visit_module_node(node) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rbs/inline/parser.rb', line 210

def visit_module_node(node)
  process_nesting_node(node) do
    visit node.constant_path

    associated_comment = comments.delete(node.location.start_line - 1)

    module_decl = AST::Declarations::ModuleDecl.new(node, associated_comment)
    push_class_module_decl(module_decl) do
      visit node.body
    end

    load_inner_annotations(node.location.start_line, node.location.end_line, module_decl.members)
  end
end

#visit_singleton_class_node(node) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rbs/inline/parser.rb', line 196

def visit_singleton_class_node(node)
  process_nesting_node(node) do
    associated_comment = comments.delete(node.location.start_line - 1)
    singleton_decl = AST::Declarations::SingletonClassDecl.new(node, associated_comment)

    push_class_module_decl(singleton_decl) do
      visit node.body
    end

    load_inner_annotations(node.location.start_line, node.location.end_line, singleton_decl.members)
  end
end