Move out CompileError to core as generic form

This commit is contained in:
Jeong YunWon 2022-08-23 01:30:00 +09:00
parent e8230efe1a
commit 42b95a9a95
3 changed files with 31 additions and 1 deletions

View file

@ -17,3 +17,4 @@ num-bigint = { version = "0.4.3", features = ["serde"] }
num-complex = { version = "0.4.0", features = ["serde"] }
serde = { version = "1.0.136", features = ["derive"] }
static_assertions = "1.1.0"
thiserror = "1.0"

View file

@ -58,3 +58,32 @@ impl<T> BaseError<T> {
BaseError::from(self)
}
}
#[derive(Debug, thiserror::Error)]
pub struct CompileError<T> {
pub body: BaseError<T>,
pub statement: Option<String>,
}
impl<T> std::ops::Deref for CompileError<T> {
type Target = BaseError<T>;
fn deref(&self) -> &Self::Target {
&self.body
}
}
impl<T> std::fmt::Display for CompileError<T>
where
T: std::fmt::Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let loc = self.location;
if let Some(ref stmt) = self.statement {
// visualize the error when location and statement are provided
loc.fmt_with(f, &self.error)?;
write!(f, "\n{stmt}{arrow:>pad$}", pad = loc.column(), arrow = "^")
} else {
loc.fmt_with(f, &self.error)
}
}
}

View file

@ -7,6 +7,6 @@ mod location;
mod mode;
pub use bytecode::*;
pub use error::BaseError;
pub use error::{BaseError, CompileError};
pub use location::Location;
pub use mode::Mode;