Sha256: a19e0830393cb3a4b6445a6259f5516f02433a49c8e792b76cbdaa7770243980

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module Parser

  ##
  # @!attribute [r] level
  #  @return [Symbol]
  #
  # @!attribute [r] message
  #  @return [String]
  #
  # @!attribute [r] location
  #  @return [Parser::Source::Map]
  #
  # @!attribute [r] highlights
  #  @return [Array]
  #
  class Diagnostic
    ##
    # Collection of the available diagnostic levels.
    #
    # @return [Array]
    #
    LEVELS = [:note, :warning, :error, :fatal].freeze

    attr_reader :level, :message
    attr_reader :location, :highlights

    ##
    # @param [Symbol] level
    # @param [String] message
    # @param [Parser::Source::Range] location
    # @param [Array<Parser::Source::Range>] highlights
    #
    def initialize(level, message, location, highlights=[])
      unless LEVELS.include?(level)
        raise ArgumentError,
              "Diagnostic#level must be one of #{LEVELS.join(', ')}; " \
              "#{level.inspect} provided."
      end

      @level       = level
      @message     = message.to_s.dup.freeze
      @location    = location
      @highlights  = highlights.dup.freeze

      freeze
    end

    ##
    # Renders the diagnostic message as an array of three lines.
    #
    # @return [Array<String>]
    #
    def render
      source_line    = @location.source_line
      highlight_line = ' ' * source_line.length

      @highlights.each do |hilight|
        range = hilight.column_range
        highlight_line[range] = '~' * hilight.size
      end

      range = @location.column_range
      highlight_line[range] = '^' * @location.size

      [
        "#{@location.to_s}: #{@level}: #{@message}",
        source_line,
        highlight_line,
      ]
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
parser-2.0.0.pre2 lib/parser/diagnostic.rb
parser-2.0.0.pre1 lib/parser/diagnostic.rb
parser-2.0.0.beta10 lib/parser/diagnostic.rb