Exception: LexerKit::DiagnosticError

Inherits:
Error
  • Object
show all
Defined in:
lib/lexer_kit/errors.rb

Overview

Base class for errors with diagnostic information (source location, span, etc.) Provides rich error messages with source code context.

Direct Known Subclasses

BuildError, CompileError, ParseError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, source: nil, span: nil, level: :error, notes: nil) ⇒ DiagnosticError

Returns a new instance of DiagnosticError.



26
27
28
29
30
# File 'lib/lexer_kit/errors.rb', line 26

def initialize(message, source: nil, span: nil, level: :error, notes: nil)
  @source = source
  @diagnostic = Core::Diagnostic.new(level: level, message: message, span: span, notes: notes) if source && span
  super(message)
end

Instance Attribute Details

#diagnosticObject (readonly)

Returns the value of attribute diagnostic.



24
25
26
# File 'lib/lexer_kit/errors.rb', line 24

def diagnostic
  @diagnostic
end

#sourceObject (readonly)

Returns the value of attribute source.



24
25
26
# File 'lib/lexer_kit/errors.rb', line 24

def source
  @source
end

Class Method Details

.from_location(location, message, level: :error, notes: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/lexer_kit/errors.rb', line 38

def self.from_location(location, message, level: :error, notes: nil)
  return new(message) unless location&.path && File.file?(location.path)

  bytes = File.binread(location.path)
  source = Core::Source.new(bytes, filename: location.path)
  span = source.span_for_line(location.lineno)
  new(message, source: source, span: span, level: level, notes: notes)
rescue Errno::ENOENT, Errno::EACCES
  new(message)
end

Instance Method Details

#render(color: $stderr.tty?) ⇒ Object



32
33
34
35
36
# File 'lib/lexer_kit/errors.rb', line 32

def render(color: $stderr.tty?)
  return message unless @diagnostic && @source

  @diagnostic.render(@source, color: color)
end