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

231 lines
7.7 KiB
Rust
Raw Normal View History

2024-08-09 01:59:09 +00:00
//! Top-level error handling for the Dust language.
2024-09-02 15:04:08 +00:00
use annotate_snippets::{Level, Message, Renderer, Snippet};
2024-08-11 21:59:52 +00:00
use std::fmt::Display;
2024-08-09 00:58:56 +00:00
2024-09-02 15:04:08 +00:00
use crate::{AnalysisError, ContextError, LexError, ParseError, RuntimeError, Span};
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-31 11:24:45 +00:00
ContextError(ContextError),
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 {
2024-08-23 11:36:10 +00:00
analysis_errors: Vec<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-31 11:24:45 +00:00
impl<'src> From<ContextError> for DustError<'src> {
fn from(error: ContextError) -> Self {
Self::ContextError(error)
}
}
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,
}
}
2024-08-23 16:16:56 +00:00
pub fn analysis<T: Into<Vec<AnalysisError>>>(analysis_errors: T, source: &'src str) -> Self {
2024-08-20 08:38:15 +00:00
DustError::Analysis {
2024-08-23 16:16:56 +00:00
analysis_errors: analysis_errors.into(),
2024-08-20 08:38:15 +00:00
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-31 11:24:45 +00:00
DustError::ContextError(_) => "Context error",
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
}
}
pub fn source(&self) -> &'src str {
match self {
2024-08-31 11:24:45 +00:00
DustError::ContextError(_) => "",
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-09-02 15:04:08 +00:00
fn footer(&self) -> Vec<(&'static str, Span, String)> {
2024-08-23 11:36:10 +00:00
match self {
2024-08-31 11:24:45 +00:00
DustError::ContextError(_) => vec![],
2024-09-02 15:04:08 +00:00
DustError::Runtime { runtime_error, .. } => {
let mut error_data = vec![(
"Runtime error",
runtime_error.position(),
runtime_error.to_string(),
)];
if let RuntimeError::Expression { error, position } = runtime_error {
error_data.push((
"Error occured at this expression",
*position,
error.to_string(),
));
}
error_data
}
2024-08-23 11:36:10 +00:00
DustError::Analysis {
analysis_errors, ..
} => analysis_errors
.iter()
.map(|error| ("Analysis error", error.position(), error.to_string()))
.collect(),
DustError::Parse { parse_error, .. } => vec![(
"Parse error",
parse_error.position(),
parse_error.to_string(),
)],
DustError::Lex { lex_error, .. } => {
vec![("Lex error", lex_error.position(), lex_error.to_string())]
2024-08-20 21:01:30 +00:00
}
}
}
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-09-02 15:04:08 +00:00
match self {
DustError::ContextError(_) => {
let message = Level::Error.title("Context error");
2024-08-20 21:01:30 +00:00
2024-09-02 15:04:08 +00:00
report.push_str(&renderer.render(message).to_string());
}
DustError::Runtime {
runtime_error,
source,
} => {
let error = runtime_error.root_error();
let position = error.position();
let label = error.to_string();
let message = Level::Error
.title("Runtime error")
.snippet(
Snippet::source(source)
.fold(true)
.annotation(Level::Error.span(position.0..position.1).label(&label)),
)
.footer(
Level::Error
.title("This error occured during the execution of the Dust program."),
);
report.push_str(&renderer.render(message).to_string());
2024-09-03 12:29:31 +00:00
report.push_str("\n\n");
2024-09-02 15:04:08 +00:00
}
DustError::Analysis {
analysis_errors,
source,
2024-09-03 12:29:31 +00:00
} => {
for error in analysis_errors {
let position = error.position();
let label = error.to_string();
let message =
Level::Warning
.title("Analysis error")
.snippet(Snippet::source(source).fold(true).annotation(
Level::Warning.span(position.0..position.1).label(&label),
))
.footer(
Level::Warning
.title("This error was found without running the program."),
);
report.push_str(&renderer.render(message).to_string());
report.push_str("\n\n");
}
}
2024-09-02 15:04:08 +00:00
DustError::Parse {
parse_error,
source,
2024-09-03 12:29:31 +00:00
} => {
if let ParseError::Lex(lex_error) = parse_error {
let lex_error_report = DustError::lex(lex_error.clone(), source).report();
report.push_str(&lex_error_report);
return report;
}
let position = parse_error.position();
let label = parse_error.to_string();
let message = Level::Error.title("Parse error").snippet(
Snippet::source(source)
.fold(true)
.annotation(Level::Error.span(position.0..position.1).label(&label)),
);
report.push_str(&renderer.render(message).to_string());
}
DustError::Lex { lex_error, source } => {
let position = lex_error.position();
let label = lex_error.to_string();
let message = Level::Error.title("Lex error").snippet(
Snippet::source(source)
.fold(true)
.annotation(Level::Error.span(position.0..position.1).label(&label)),
);
report.push_str(&renderer.render(message).to_string());
}
2024-08-20 21:01:30 +00:00
}
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-31 11:24:45 +00:00
DustError::ContextError(context_error) => write!(f, "{context_error}"),
2024-08-20 08:38:15 +00:00
DustError::Runtime { runtime_error, .. } => write!(f, "{runtime_error}"),
2024-08-23 11:36:10 +00:00
DustError::Analysis {
analysis_errors, ..
} => {
for error in analysis_errors {
write!(f, "{error} ")?;
}
Ok(())
}
2024-08-20 08:38:15 +00:00
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
}
}