use std::{fmt::Display, sync};
use crate::ObjectType;
#[allow(unused_imports)]
use crate::{Builder, Stack, PromiseBuilder, Promises};
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, PartialEq)]
pub struct Error {
pub kind: ErrorKind,
pub loc: Location,
}
impl Error {
pub(crate) fn new(kind: ErrorKind, loc: Location) -> Self {
Self {
kind,
loc
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}: Error: {}", self.loc, self.kind)
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Location {
pub line: usize,
pub col: usize,
pub file_name: Option<sync::Arc<String>>,
}
impl Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}:{}", self.file_name.as_ref().map_or("<provided>", |a| a.as_str()), self.line, self.col)
}
}
impl Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorKind::NotEnoughArgs { expected, got } => write!(f, "Not Enough Arguments: expected {expected}, got {got}!"),
ErrorKind::IncorrectType { expected, got } => write!(f, "Incorrect Argument Type: expected {expected:?}, got {got:?}!"),
ErrorKind::UnknownVar { name } => write!(f, "Unknown Variable: \"{name}\"!"),
ErrorKind::UnfulfilledPromise { expected } => write!(f, "Unfulfilled Promise: \"{expected}\"!\nNote: This is ususally the fault of the program interpreting the code. You should file a bug report if possible."),
ErrorKind::UnpairedEnd => write!(f, "`End` Without Start of Block!"),
ErrorKind::UnclosedIf => write!(f, "`if` Statement Without Matching `end`!"),
ErrorKind::UnclosedWhile => write!(f, "`while` Statement Without Matching `end`!"),
ErrorKind::WhileWithoutDo => write!(f, "`while` Statement Without `do`!"),
ErrorKind::DoWithoutWhile => write!(f, "Unexpected `do`!"),
ErrorKind::UnclosedString => write!(f, "Unclosed String!"),
ErrorKind::IncorrectNumber => write!(f, "Unparseable Number!"),
ErrorKind::UnknownEscape => write!(f, "Unknown Escaped Character!"),
}
}
}
#[derive(Debug, PartialEq)]
pub enum ErrorKind {
NotEnoughArgs {
expected: usize,
got: usize
},
IncorrectType {
expected: &'static [ObjectType],
got: Vec<ObjectType>,
},
UnknownVar {
name: String
},
UnfulfilledPromise {
expected: String
},
UnpairedEnd,
UnclosedIf,
UnclosedWhile,
WhileWithoutDo,
DoWithoutWhile,
UnclosedString,
IncorrectNumber,
UnknownEscape,
}