class Parser::Source::Comment

A comment in the source code.

@!attribute [r] text

@return [String]

@!attribute [r] location

@return [Parser::Source::Range]

@api public

Attributes

loc[R]
location[R]
text[R]

Public Class Methods

associate(ast, comments) click to toggle source

Associate ‘comments` with `ast` nodes by their corresponding node.

@param [Parser::AST::Node] ast @param [Array<Comment>] comments @return [Hash<Parser::AST::Node, Array<Comment>>] @see Parser::Source::Comment::Associator#associate @deprecated Use {associate_locations}.

# File lib/parser/source/comment.rb, line 32
def self.associate(ast, comments)
  associator = Associator.new(ast, comments)
  associator.associate
end
associate_by_identity(ast, comments) click to toggle source

Associate ‘comments` with `ast` nodes using identity.

@param [Parser::AST::Node] ast @param [Array<Comment>] comments @return [Hash<Parser::Source::Node, Array<Comment>>] @see Parser::Source::Comment::Associator#associate_by_identity

# File lib/parser/source/comment.rb, line 59
def self.associate_by_identity(ast, comments)
  associator = Associator.new(ast, comments)
  associator.associate_by_identity
end
associate_locations(ast, comments) click to toggle source

Associate ‘comments` with `ast` nodes by their location in the source.

@param [Parser::AST::Node] ast @param [Array<Comment>] comments @return [Hash<Parser::Source::Map, Array<Comment>>] @see Parser::Source::Comment::Associator#associate_locations

# File lib/parser/source/comment.rb, line 46
def self.associate_locations(ast, comments)
  associator = Associator.new(ast, comments)
  associator.associate_locations
end
new(range) click to toggle source

@param [Parser::Source::Range] range

# File lib/parser/source/comment.rb, line 67
def initialize(range)
  @location = Parser::Source::Map.new(range)
  @text     = range.source.freeze

  freeze
end

Public Instance Methods

==(other) click to toggle source

Compares comments. Two comments are equal if they correspond to the same source range.

@param [Object] other @return [Boolean]

# File lib/parser/source/comment.rb, line 120
def ==(other)
  other.is_a?(Source::Comment) &&
    @location == other.location
end
document?() click to toggle source

@see type @return [Boolean] true if this is a block comment.

# File lib/parser/source/comment.rb, line 109
def document?
  type == :document
end
inline?() click to toggle source

@see type @return [Boolean] true if this is an inline comment.

# File lib/parser/source/comment.rb, line 101
def inline?
  type == :inline
end
inspect() click to toggle source

@return [String] a human-readable representation of this comment

# File lib/parser/source/comment.rb, line 128
def inspect
  "#<Parser::Source::Comment #{@location.expression.to_s} #{text.inspect}>"
end
type() click to toggle source

Type of this comment.

* Inline comments correspond to `:inline`:

      # whatever

* Block comments correspond to `:document`:

      =begin
      hi i am a document
      =end

@return [Symbol]

# File lib/parser/source/comment.rb, line 89
def type
  if text.start_with?("#".freeze)
    :inline
  elsif text.start_with?("=begin".freeze)
    :document
  end
end