Class: RbsActivesupport::Parser::CommentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs_activesupport/parser/comment_parser.rb,
sig/rbs_activesupport/parser/comment_parser.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommentParser

: void



11
12
13
14
# File 'lib/rbs_activesupport/parser/comment_parser.rb', line 11

def initialize #: void
  @line_comments = {}
  @trailing_comments = {}
end

Instance Attribute Details

#line_commentsHash[Integer, String] (readonly)

: Hash[Integer, String]

Returns:

  • (Hash[Integer, String])


8
9
10
# File 'lib/rbs_activesupport/parser/comment_parser.rb', line 8

def line_comments
  @line_comments
end

#trailing_commentsHash[Integer, String] (readonly)

: Hash[Integer, String]

Returns:

  • (Hash[Integer, String])


9
10
11
# File 'lib/rbs_activesupport/parser/comment_parser.rb', line 9

def trailing_comments
  @trailing_comments
end

Instance Method Details

#parse(string) ⇒ self

Parameters:

  • string (String)

Returns:

  • (self)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rbs_activesupport/parser/comment_parser.rb', line 17

def parse(string) #: self
  # @type var code_lines: Hash[Integer, bool]
  code_lines = {}
  Ripper.lex(string).each do |(line, _), type, token, _|
    case type
    when :on_sp, :on_ignored_nl
      # ignore
    when :on_comment
      if code_lines[line]
        trailing_comments[line] = token.chomp
      else
        line_comments[line] = token.chomp
      end
      :here
    else
      code_lines[line] = true
    end
  end

  self
end