mod error_std;
use std::{
convert::Infallible,
error::Error,
fmt::{self, Debug, Display, Formatter},
ops::Range,
};
use url::Url;
use yggdrasil_shared::DiagnosticLevel;
pub type Result<T> = std::result::Result<T, NoteError>;
pub type MaybeRanged = Option<Range<usize>>;
#[derive(Debug)]
pub struct NoteError {
pub kind: Box<NoteErrorKind>,
pub level: DiagnosticLevel,
pub file: Option<Url>,
pub range: Option<Range<usize>>,
}
#[derive(Debug)]
pub enum NoteErrorKind {
IOError(std::io::Error),
FormatError(std::fmt::Error),
SyntaxError(String),
TypeMismatch(String),
RuntimeError(String),
UndefinedVariable {
name: String,
},
Unreachable,
}
impl NoteError {
#[inline]
pub fn set_url(&mut self, url: Url) {
self.file = Some(url);
}
#[inline]
#[cfg(any(unix, windows, target_os = "redox"))]
pub fn set_path(&mut self, url: &std::path::Path) -> Result<()> {
self.file = Some(Url::from_file_path(url)?);
Ok(())
}
#[inline]
pub fn set_range(&mut self, start: usize, end: usize) {
self.range = Some(Range { start, end });
}
#[inline]
pub fn unreachable() -> Self {
Self { kind: Box::new(NoteErrorKind::Unreachable), level: DiagnosticLevel::None, file: None, range: None }
}
#[inline]
pub fn undefined_variable(name: impl Into<String>) -> NoteError {
let kind = NoteErrorKind::UndefinedVariable { name: name.into() };
Self { kind: Box::new(kind), level: DiagnosticLevel::None, file: None, range: None }
}
}
impl NoteError {
pub fn is_deprecated(&self) -> bool {
false
}
pub fn is_unnecessary(&self) -> bool {
false
}
}
macro_rules! error_msg {
($name:ident => $t:ident) => {
pub fn $name(msg: impl Into<String>) -> NoteError {
let kind = NoteErrorKind::$t(msg.into());
Self { kind: Box::new(kind), level: DiagnosticLevel::None, file: None, range: None }
}
};
($($name:ident => $t:ident),+ $(,)?) => (
impl NoteError { $(error_msg!($name=>$t);)+ }
);
}
error_msg![
syntax_error => SyntaxError,
type_mismatch => TypeMismatch,
runtime_error => RuntimeError,
];
impl Error for NoteError {}
impl Display for NoteError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let path = match &self.file {
Some(s) => s.path(),
None => "<Anonymous>",
};
match &self.range {
Some(s) => writeln!(f, "at ({}, {}) of {}", s.start, s.end, path)?,
None => writeln!(f, "at {}", path)?,
}
write!(f, "{indent:indent$}{}", self.kind, indent = 4)
}
}
impl Display for NoteErrorKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::IOError(e) => {
write!(f, "{}", e)
}
Self::FormatError(e) => {
write!(f, "{}", e)
}
Self::SyntaxError(msg) => {
f.write_str("SyntaxError: ")?;
f.write_str(msg)
}
Self::TypeMismatch(msg) => {
f.write_str("TypeError: ")?;
f.write_str(msg)
}
Self::RuntimeError(msg) => {
f.write_str("RuntimeError: ")?;
f.write_str(msg)
}
Self::UndefinedVariable { name } => {
write!(f, "RuntimeError: Variable {} not found in scope", name)
}
Self::Unreachable => {
f.write_str("InternalError: ")?;
f.write_str("Entered unreachable code!")
}
}
}
}