Class: Parser::Diagnostic

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

Defined Under Namespace

Classes: Engine

Constant Summary

LEVELS =

Collection of the available diagnostic levels.

Returns:

  • (Array)
[:note, :warning, :error, :fatal].freeze

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Diagnostic) initialize(level, reason, arguments, location, highlights = [])

Returns a new instance of Diagnostic

Parameters:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/parser/diagnostic.rb', line 47

def initialize(level, reason, arguments, location, highlights=[])
  unless LEVELS.include?(level)
    raise ArgumentError,
          "Diagnostic#level must be one of #{LEVELS.join(', ')}; " \
          "#{level.inspect} provided."
  end
  raise 'Expected a location' unless location

  @level       = level
  @reason      = reason
  @arguments   = (arguments || {}).dup.freeze
  @location    = location
  @highlights  = highlights.dup.freeze

  freeze
end

Instance Attribute Details

- (Symbol) arguments (readonly)

Returns extended arguments that describe the error

Returns:

  • (Symbol)

    extended arguments that describe the error

See Also:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parser/diagnostic.rb', line 29

class Diagnostic
  ##
  # Collection of the available diagnostic levels.
  #
  # @return [Array]
  #
  LEVELS = [:note, :warning, :error, :fatal].freeze

  attr_reader :level, :reason, :arguments
  attr_reader :location, :highlights

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

    @level       = level
    @reason      = reason
    @arguments   = (arguments || {}).dup.freeze
    @location    = location
    @highlights  = highlights.dup.freeze

    freeze
  end

  ##
  # @return [String] the rendered message.
  #
  def message
    MESSAGES[@reason] % @arguments
  end

  ##
  # Renders the diagnostic message as a clang-like diagnostic.
  #
  # @example
  #  diagnostic.render # =>
  #  # [
  #  #   "(fragment:0):1:5: error: unexpected token $end",
  #  #   "foo +",
  #  #   "    ^"
  #  # ]
  #
  # @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

- (Array<Parser::Source::Range>) highlights (readonly)

Supplementary error-related source ranges.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parser/diagnostic.rb', line 29

class Diagnostic
  ##
  # Collection of the available diagnostic levels.
  #
  # @return [Array]
  #
  LEVELS = [:note, :warning, :error, :fatal].freeze

  attr_reader :level, :reason, :arguments
  attr_reader :location, :highlights

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

    @level       = level
    @reason      = reason
    @arguments   = (arguments || {}).dup.freeze
    @location    = location
    @highlights  = highlights.dup.freeze

    freeze
  end

  ##
  # @return [String] the rendered message.
  #
  def message
    MESSAGES[@reason] % @arguments
  end

  ##
  # Renders the diagnostic message as a clang-like diagnostic.
  #
  # @example
  #  diagnostic.render # =>
  #  # [
  #  #   "(fragment:0):1:5: error: unexpected token $end",
  #  #   "foo +",
  #  #   "    ^"
  #  # ]
  #
  # @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

- (Symbol) level (readonly)

Returns diagnostic level

Returns:

  • (Symbol)

    diagnostic level

See Also:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parser/diagnostic.rb', line 29

class Diagnostic
  ##
  # Collection of the available diagnostic levels.
  #
  # @return [Array]
  #
  LEVELS = [:note, :warning, :error, :fatal].freeze

  attr_reader :level, :reason, :arguments
  attr_reader :location, :highlights

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

    @level       = level
    @reason      = reason
    @arguments   = (arguments || {}).dup.freeze
    @location    = location
    @highlights  = highlights.dup.freeze

    freeze
  end

  ##
  # @return [String] the rendered message.
  #
  def message
    MESSAGES[@reason] % @arguments
  end

  ##
  # Renders the diagnostic message as a clang-like diagnostic.
  #
  # @example
  #  diagnostic.render # =>
  #  # [
  #  #   "(fragment:0):1:5: error: unexpected token $end",
  #  #   "foo +",
  #  #   "    ^"
  #  # ]
  #
  # @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

- (Parser::Source::Range) location (readonly)

Main error-related source range.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parser/diagnostic.rb', line 29

class Diagnostic
  ##
  # Collection of the available diagnostic levels.
  #
  # @return [Array]
  #
  LEVELS = [:note, :warning, :error, :fatal].freeze

  attr_reader :level, :reason, :arguments
  attr_reader :location, :highlights

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

    @level       = level
    @reason      = reason
    @arguments   = (arguments || {}).dup.freeze
    @location    = location
    @highlights  = highlights.dup.freeze

    freeze
  end

  ##
  # @return [String] the rendered message.
  #
  def message
    MESSAGES[@reason] % @arguments
  end

  ##
  # Renders the diagnostic message as a clang-like diagnostic.
  #
  # @example
  #  diagnostic.render # =>
  #  # [
  #  #   "(fragment:0):1:5: error: unexpected token $end",
  #  #   "foo +",
  #  #   "    ^"
  #  # ]
  #
  # @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

- (String) message (readonly)

Returns the rendered message.

Returns:

  • (String)

    the rendered message.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parser/diagnostic.rb', line 29

class Diagnostic
  ##
  # Collection of the available diagnostic levels.
  #
  # @return [Array]
  #
  LEVELS = [:note, :warning, :error, :fatal].freeze

  attr_reader :level, :reason, :arguments
  attr_reader :location, :highlights

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

    @level       = level
    @reason      = reason
    @arguments   = (arguments || {}).dup.freeze
    @location    = location
    @highlights  = highlights.dup.freeze

    freeze
  end

  ##
  # @return [String] the rendered message.
  #
  def message
    MESSAGES[@reason] % @arguments
  end

  ##
  # Renders the diagnostic message as a clang-like diagnostic.
  #
  # @example
  #  diagnostic.render # =>
  #  # [
  #  #   "(fragment:0):1:5: error: unexpected token $end",
  #  #   "foo +",
  #  #   "    ^"
  #  # ]
  #
  # @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

- (Symbol) reason (readonly)

Returns reason for error

Returns:

  • (Symbol)

    reason for error

See Also:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parser/diagnostic.rb', line 29

class Diagnostic
  ##
  # Collection of the available diagnostic levels.
  #
  # @return [Array]
  #
  LEVELS = [:note, :warning, :error, :fatal].freeze

  attr_reader :level, :reason, :arguments
  attr_reader :location, :highlights

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

    @level       = level
    @reason      = reason
    @arguments   = (arguments || {}).dup.freeze
    @location    = location
    @highlights  = highlights.dup.freeze

    freeze
  end

  ##
  # @return [String] the rendered message.
  #
  def message
    MESSAGES[@reason] % @arguments
  end

  ##
  # Renders the diagnostic message as a clang-like diagnostic.
  #
  # @example
  #  diagnostic.render # =>
  #  # [
  #  #   "(fragment:0):1:5: error: unexpected token $end",
  #  #   "foo +",
  #  #   "    ^"
  #  # ]
  #
  # @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

Instance Method Details

- (Array<String>) render

Renders the diagnostic message as a clang-like diagnostic.

Examples:

diagnostic.render # =>
# [
#   "(fragment:0):1:5: error: unexpected token $end",
#   "foo +",
#   "    ^"
# ]

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/parser/diagnostic.rb', line 84

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