Sha256: a58d6a8191dd73f8e839ea65bbc8d2993d82d4315d9cd9ce135c9da4849df59c

Contents?: true

Size: 1.02 KB

Versions: 24

Compression:

Stored size: 1.02 KB

Contents

module Parser

  class Diagnostic
    LEVELS = [:note, :warning, :error, :fatal].freeze

    attr_reader :level, :message
    attr_reader :location, :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

    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

24 entries across 24 versions & 1 rubygems

Version Path
parser-2.0.0.beta9 lib/parser/diagnostic.rb
parser-1.4.2 lib/parser/diagnostic.rb
parser-2.0.0.beta8 lib/parser/diagnostic.rb
parser-2.0.0.beta7 lib/parser/diagnostic.rb
parser-2.0.0.beta6 lib/parser/diagnostic.rb
parser-2.0.0.beta5 lib/parser/diagnostic.rb
parser-2.0.0.beta4 lib/parser/diagnostic.rb
parser-1.4.1 lib/parser/diagnostic.rb
parser-2.0.0.beta3 lib/parser/diagnostic.rb
parser-2.0.0.beta2 lib/parser/diagnostic.rb
parser-2.0.0.beta1 lib/parser/diagnostic.rb
parser-1.4.0 lib/parser/diagnostic.rb
parser-1.3.4 lib/parser/diagnostic.rb
parser-1.3.3 lib/parser/diagnostic.rb
parser-1.3.2 lib/parser/diagnostic.rb
parser-1.3.1 lib/parser/diagnostic.rb
parser-1.3.0 lib/parser/diagnostic.rb
parser-1.2.0 lib/parser/diagnostic.rb
parser-1.1.0 lib/parser/diagnostic.rb
parser-1.0.1 lib/parser/diagnostic.rb