Class: Parser::Diagnostic
- Inherits:
-
Object
- Object
- Parser::Diagnostic
- Defined in:
- lib/parser/diagnostic.rb
Defined Under Namespace
Classes: Engine
Constant Summary collapse
- LEVELS =
[:note, :warning, :error, :fatal].freeze
Instance Attribute Summary collapse
-
#highlights ⇒ Object
readonly
Returns the value of attribute highlights.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Instance Method Summary collapse
-
#initialize(level, message, location, highlights = []) ⇒ Diagnostic
constructor
A new instance of Diagnostic.
- #render ⇒ Object
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, , location, highlights=[]) unless LEVELS.include?(level) raise ArgumentError, "Diagnostic#level must be one of #{LEVELS.join(', ')}; " \ "#{level.inspect} provided." end @level = level @message = .to_s.dup.freeze @location = location @highlights = highlights.dup.freeze freeze end |
Instance Attribute Details
#highlights ⇒ Object (readonly)
Returns the value of attribute highlights.
7 8 9 |
# File 'lib/parser/diagnostic.rb', line 7 def highlights @highlights end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
6 7 8 |
# File 'lib/parser/diagnostic.rb', line 6 def level @level end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
7 8 9 |
# File 'lib/parser/diagnostic.rb', line 7 def location @location end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
6 7 8 |
# File 'lib/parser/diagnostic.rb', line 6 def @message end |
Instance Method Details
#render ⇒ Object
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 |