Exception: LexerKit::DiagnosticError
- 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
Instance Attribute Summary collapse
-
#diagnostic ⇒ Object
readonly
Returns the value of attribute diagnostic.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(message, source: nil, span: nil, level: :error, notes: nil) ⇒ DiagnosticError
constructor
A new instance of DiagnosticError.
- #render(color: $stderr.tty?) ⇒ Object
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(, source: nil, span: nil, level: :error, notes: nil) @source = source @diagnostic = Core::Diagnostic.new(level: level, message: , span: span, notes: notes) if source && span super() end |
Instance Attribute Details
#diagnostic ⇒ Object (readonly)
Returns the value of attribute diagnostic.
24 25 26 |
# File 'lib/lexer_kit/errors.rb', line 24 def diagnostic @diagnostic end |
#source ⇒ Object (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, , level: :error, notes: nil) return new() 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(, source: source, span: span, level: level, notes: notes) rescue Errno::ENOENT, Errno::EACCES new() 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 unless @diagnostic && @source @diagnostic.render(@source, color: color) end |