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



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

def initialize() #: void
  @decls = []
  @surrounding_decls = []
  @comments = {}
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_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.



60
61
62
63
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
# File 'lib/rbs/inline/parser.rb', line 60

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.



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

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.



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/rbs/inline/parser.rb', line 397

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



117
118
119
# File 'lib/rbs/inline/parser.rb', line 117

def current_class_module_decl
  surrounding_decls.last
end

#current_class_module_decl!Object



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

def current_class_module_decl!
  current_class_module_decl or raise
end

#ignored_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


364
365
366
367
368
369
370
# File 'lib/rbs/inline/parser.rb', line 364

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) “‘



232
233
234
235
236
237
238
239
240
241
# File 'lib/rbs/inline/parser.rb', line 232

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.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rbs/inline/parser.rb', line 153

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



353
354
355
356
357
358
359
360
# File 'lib/rbs/inline/parser.rb', line 353

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rbs/inline/parser.rb', line 127

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



341
342
343
344
345
346
347
348
349
350
# File 'lib/rbs/inline/parser.rb', line 341

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



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

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



461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/rbs/inline/parser.rb', line 461

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



275
276
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
# File 'lib/rbs/inline/parser.rb', line 275

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

  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, 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
  end

  super
end

#visit_class_node(node) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rbs/inline/parser.rb', line 171

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



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/rbs/inline/parser.rb', line 416

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



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

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, assertion)

    super
  end
end

#visit_module_node(node) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rbs/inline/parser.rb', line 206

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



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rbs/inline/parser.rb', line 192

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