1
0
dust/dust-lang/src/dust_error.rs

138 lines
4.3 KiB
Rust
Raw Normal View History

2024-08-09 01:59:09 +00:00
//! Top-level error handling for the Dust language.
2024-08-20 18:45:43 +00:00
use annotate_snippets::{Level, Renderer, Snippet};
2024-08-11 21:59:52 +00:00
use std::fmt::Display;
2024-08-09 00:58:56 +00:00
2024-08-20 11:20:44 +00:00
use crate::{ast::Span, AnalysisError, LexError, ParseError, RuntimeError};
2024-08-09 00:58:56 +00:00
2024-08-09 01:59:09 +00:00
/// An error that occurred during the execution of the Dust language and its
/// corresponding source code.
2024-08-09 00:58:56 +00:00
#[derive(Debug, Clone, PartialEq)]
2024-08-11 21:59:52 +00:00
pub enum DustError<'src> {
2024-08-20 08:38:15 +00:00
Runtime {
runtime_error: RuntimeError,
2024-08-11 21:59:52 +00:00
source: &'src str,
},
2024-08-20 08:38:15 +00:00
Analysis {
analysis_error: AnalysisError,
2024-08-11 21:59:52 +00:00
source: &'src str,
},
2024-08-20 08:38:15 +00:00
Parse {
2024-08-11 21:59:52 +00:00
parse_error: ParseError,
source: &'src str,
},
2024-08-20 08:38:15 +00:00
Lex {
2024-08-11 21:59:52 +00:00
lex_error: LexError,
source: &'src str,
},
2024-08-09 00:58:56 +00:00
}
2024-08-09 01:47:49 +00:00
impl<'src> DustError<'src> {
2024-08-20 08:38:15 +00:00
pub fn runtime(runtime_error: RuntimeError, source: &'src str) -> Self {
DustError::Runtime {
runtime_error,
source,
}
}
pub fn analysis(analysis_error: AnalysisError, source: &'src str) -> Self {
DustError::Analysis {
analysis_error,
source,
}
}
pub fn parse(parse_error: ParseError, source: &'src str) -> Self {
DustError::Parse {
parse_error,
source,
}
}
pub fn lex(lex_error: LexError, source: &'src str) -> Self {
DustError::Lex { lex_error, source }
}
2024-08-11 21:59:52 +00:00
pub fn title(&self) -> &'static str {
match self {
2024-08-20 08:38:15 +00:00
DustError::Runtime { .. } => "Runtime error",
DustError::Analysis { .. } => "Analysis error",
DustError::Parse { .. } => "Parse error",
DustError::Lex { .. } => "Lex error",
2024-08-11 21:59:52 +00:00
}
}
2024-08-20 21:01:30 +00:00
pub fn position(&self) -> Span {
2024-08-11 21:59:52 +00:00
match self {
2024-08-20 08:38:15 +00:00
DustError::Runtime { runtime_error, .. } => runtime_error.position(),
2024-08-20 15:07:13 +00:00
DustError::Analysis { analysis_error, .. } => analysis_error.position(),
2024-08-20 21:01:30 +00:00
DustError::Parse { parse_error, .. } => parse_error.position(),
DustError::Lex { lex_error, .. } => lex_error.position(),
2024-08-11 21:59:52 +00:00
}
}
pub fn source(&self) -> &'src str {
match self {
2024-08-20 08:38:15 +00:00
DustError::Runtime { source, .. } => source,
DustError::Analysis { source, .. } => source,
DustError::Parse { source, .. } => source,
DustError::Lex { source, .. } => source,
2024-08-11 21:59:52 +00:00
}
2024-08-09 01:47:49 +00:00
}
2024-08-20 21:01:30 +00:00
pub fn primary_error_data(&self) -> (&'static str, Span, String) {
(self.title(), self.position(), self.to_string())
}
pub fn secondary_error_data(&self) -> Option<(&'static str, Span, String)> {
if let DustError::Runtime { runtime_error, .. } = self {
match runtime_error {
RuntimeError::Expression { error, .. } => {
Some(("Expression error", error.position(), error.to_string()))
}
RuntimeError::Statement { error, .. } => {
Some(("Statement error", error.position(), error.to_string()))
}
_ => None,
}
} else {
None
}
}
2024-08-09 01:47:49 +00:00
pub fn report(&self) -> String {
2024-08-20 21:01:30 +00:00
let mut report = String::new();
let renderer = Renderer::styled();
2024-08-20 11:20:44 +00:00
2024-08-20 21:01:30 +00:00
let (title, span, label) = self.primary_error_data();
let message = Level::Error.title(title).snippet(
Snippet::source(self.source())
.annotation(Level::Info.span(span.0..span.1).label(&label)),
);
report.push_str(&format!("{}", renderer.render(message)));
if let Some((title, span, label)) = self.secondary_error_data() {
let message = Level::Error.title(title).snippet(
2024-08-20 11:20:44 +00:00
Snippet::source(self.source())
.annotation(Level::Info.span(span.0..span.1).label(&label)),
2024-08-20 21:01:30 +00:00
);
report.push_str(&format!("{}", renderer.render(message)));
}
2024-08-09 01:47:49 +00:00
2024-08-20 21:01:30 +00:00
report
2024-08-09 01:47:49 +00:00
}
}
2024-08-09 00:58:56 +00:00
impl Display for DustError<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2024-08-11 21:59:52 +00:00
match self {
2024-08-20 08:38:15 +00:00
DustError::Runtime { runtime_error, .. } => write!(f, "{runtime_error}"),
DustError::Analysis { analysis_error, .. } => write!(f, "{analysis_error}"),
DustError::Parse { parse_error, .. } => write!(f, "{parse_error}"),
DustError::Lex { lex_error, .. } => write!(f, "{lex_error}"),
2024-08-11 21:59:52 +00:00
}
2024-08-09 00:58:56 +00:00
}
}