Method: RBS::Prototype::RBI#parse

Defined in:
lib/rbs/prototype/rbi.rb

#parse(string) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbs/prototype/rbi.rb', line 18

def parse(string)
  comments = Ripper.lex(string).yield_self do |tokens|
    tokens.each.with_object({}) do |token, hash| #$ Hash[Integer, AST::Comment]
      if token[1] == :on_comment
        line = token[0][0]
        body = token[2][2..-1] or raise

        body = "\n" if body.empty?

        comment = AST::Comment.new(string: body, location: nil)
        if (prev_comment = hash.delete(line - 1))
          hash[line] = AST::Comment.new(
            string: prev_comment.string + comment.string,
            location: nil
          )
        else
          hash[line] = comment
        end
      end
    end
  end
  process RubyVM::AbstractSyntaxTree.parse(string), comments: comments
end