Sha256: f23b33cf29b7ba07e31be372d82cc426fa4e6b737505f3dd060ab86bc142b9c6

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Parser
  module Source

    ##
    # @api public
    #
    # @!attribute [r] text
    #  @return String
    #
    # @!attribute [r] location
    #  @return Parser::Source::Range
    class Comment
      attr_reader  :text

      attr_reader  :location
      alias_method :loc, :location

      ##
      # @see Parser::Source::Comment::Associator
      def self.associate(ast, comments)
        associator = Associator.new(comments, ast)
        associator.associate
      end

      ##
      # @param [Parser::Source::Range] location
      def initialize(location)
        @location = location
        @text     = location.source.freeze

        freeze
      end

      ##
      # Returns the type of this comment.
      #
      #   * Inline comments correspond to `:inline`:
      #
      #         # whatever
      #
      #   * Block comments correspond to `:document`:
      #
      #         =begin
      #         hi i am a document
      #         =end
      def type
        case text
        when /^#/
          :inline
        when /^=begin/
          :document
        end
      end

      ##
      # @see [#type]
      # @return [TrueClass|FalseClass]
      def inline?
        type == :inline
      end

      ##
      # @see [#type]
      # @return [TrueClass|FalseClass]
      def document?
        type == :document
      end

      ##
      # Compares comments. Two comments are identical if they
      # correspond to the same source range.
      # @param [Object] other
      # @return [TrueClass|FalseClass]
      def ==(other)
        other.is_a?(Source::Comment) &&
          @location == other.location
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parser-2.0.0.pre3 lib/parser/source/comment.rb