Class: Parser::Diagnostic

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/diagnostic.rb

Defined Under Namespace

Classes: Engine

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, message, location, highlights = []) ⇒ Diagnostic

Returns a new instance of Diagnostic.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/parser/diagnostic.rb', line 9

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

Instance Attribute Details

#highlightsObject (readonly)

Returns the value of attribute highlights.



7
8
9
# File 'lib/parser/diagnostic.rb', line 7

def highlights
  @highlights
end

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/parser/diagnostic.rb', line 6

def level
  @level
end

#locationObject (readonly)

Returns the value of attribute location.



7
8
9
# File 'lib/parser/diagnostic.rb', line 7

def location
  @location
end

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/parser/diagnostic.rb', line 6

def message
  @message
end

Instance Method Details

#renderObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/parser/diagnostic.rb', line 24

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