2024-02-25 18:49:26 +00:00
|
|
|
use std::sync::PoisonError;
|
|
|
|
|
2024-03-07 03:15:35 +00:00
|
|
|
use ariadne::{Color, Label, Report, ReportKind};
|
2024-03-06 20:36:58 +00:00
|
|
|
use chumsky::{prelude::Rich, span::SimpleSpan};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
2024-03-06 22:32:31 +00:00
|
|
|
use crate::{
|
|
|
|
abstract_tree::{Identifier, Type},
|
|
|
|
lexer::Token,
|
|
|
|
};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
2024-03-06 20:36:58 +00:00
|
|
|
pub enum Error {
|
2024-03-06 22:32:31 +00:00
|
|
|
Parse {
|
|
|
|
expected: String,
|
|
|
|
span: SimpleSpan,
|
|
|
|
},
|
|
|
|
Lex {
|
|
|
|
expected: String,
|
|
|
|
span: SimpleSpan,
|
|
|
|
},
|
2024-02-25 18:49:26 +00:00
|
|
|
Runtime(RuntimeError),
|
2024-03-06 22:32:31 +00:00
|
|
|
Validation {
|
|
|
|
error: ValidationError,
|
|
|
|
span: SimpleSpan,
|
|
|
|
},
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 03:15:35 +00:00
|
|
|
impl Error {
|
|
|
|
pub fn report(&self) -> Report {
|
|
|
|
match self {
|
|
|
|
Error::Parse { expected, span } => Report::build(
|
|
|
|
ReportKind::Custom("Parsing Error", Color::White),
|
|
|
|
(),
|
|
|
|
span.start,
|
|
|
|
)
|
|
|
|
.with_label(
|
|
|
|
Label::new(span.start..span.end).with_message(format!("Expected {expected}.")),
|
|
|
|
)
|
|
|
|
.finish(),
|
|
|
|
Error::Lex { expected, span } => {
|
|
|
|
let expected = match expected.as_str() {
|
|
|
|
"" => "something else",
|
|
|
|
expected => expected,
|
|
|
|
};
|
|
|
|
|
|
|
|
Report::build(
|
|
|
|
ReportKind::Custom("Lexing Error", Color::White),
|
|
|
|
(),
|
|
|
|
span.start,
|
|
|
|
)
|
|
|
|
.with_label(
|
|
|
|
Label::new(span.start..span.end).with_message(format!("Expected {expected}.")),
|
|
|
|
)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
Error::Runtime(_) => todo!(),
|
|
|
|
Error::Validation { error, span } => {
|
|
|
|
let mut report = Report::build(
|
2024-03-09 11:55:19 +00:00
|
|
|
ReportKind::Custom("Validation Error", Color::White),
|
2024-03-07 03:15:35 +00:00
|
|
|
(),
|
|
|
|
span.start,
|
|
|
|
);
|
|
|
|
|
|
|
|
match error {
|
|
|
|
ValidationError::ExpectedBoolean => {
|
|
|
|
report = report.with_label(
|
|
|
|
Label::new(span.start..span.end).with_message("Expected boolean."),
|
|
|
|
);
|
|
|
|
}
|
2024-03-07 11:33:54 +00:00
|
|
|
ValidationError::ExpectedIntegerOrFloat => {
|
|
|
|
report = report.with_label(
|
|
|
|
Label::new(span.start..span.end)
|
|
|
|
.with_message("Expected integer or float."),
|
|
|
|
);
|
|
|
|
}
|
2024-03-07 03:15:35 +00:00
|
|
|
ValidationError::RwLockPoison(_) => todo!(),
|
|
|
|
ValidationError::TypeCheck(TypeCheckError { actual, expected }) => {
|
|
|
|
report = report.with_label(Label::new(span.start..span.end).with_message(
|
|
|
|
format!("Type error. Expected {expected} but got {actual}."),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
ValidationError::VariableNotFound(identifier) => {
|
|
|
|
report = report
|
|
|
|
.with_label(Label::new(span.start..span.end).with_message(format!(
|
|
|
|
"The variable {identifier} does not exist."
|
|
|
|
)));
|
|
|
|
}
|
2024-03-07 17:29:07 +00:00
|
|
|
ValidationError::CannotIndex(_) => todo!(),
|
|
|
|
ValidationError::CannotIndexWith(_, _) => todo!(),
|
2024-03-08 17:24:11 +00:00
|
|
|
ValidationError::InterpreterExpectedReturn => todo!(),
|
2024-03-09 13:10:54 +00:00
|
|
|
ValidationError::ExpectedFunction => todo!(),
|
|
|
|
ValidationError::ExpectedValue => todo!(),
|
2024-03-07 03:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
report.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
impl From<Rich<'_, char>> for Error {
|
|
|
|
fn from(error: Rich<'_, char>) -> Self {
|
|
|
|
Error::Lex {
|
|
|
|
expected: error.expected().map(|error| error.to_string()).collect(),
|
|
|
|
span: error.span().clone(),
|
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
impl<'src> From<Rich<'_, Token<'src>>> for Error {
|
|
|
|
fn from(error: Rich<'_, Token<'src>>) -> Self {
|
|
|
|
Error::Parse {
|
|
|
|
expected: error.expected().map(|error| error.to_string()).collect(),
|
|
|
|
span: error.span().clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RuntimeError> for Error {
|
2024-02-25 18:49:26 +00:00
|
|
|
fn from(error: RuntimeError) -> Self {
|
|
|
|
Error::Runtime(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum RuntimeError {
|
|
|
|
RwLockPoison(RwLockPoisonError),
|
2024-02-29 02:04:38 +00:00
|
|
|
ValidationFailure(ValidationError),
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RwLockPoisonError> for RuntimeError {
|
|
|
|
fn from(error: RwLockPoisonError) -> Self {
|
|
|
|
RuntimeError::RwLockPoison(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 02:04:38 +00:00
|
|
|
impl From<ValidationError> for RuntimeError {
|
|
|
|
fn from(error: ValidationError) -> Self {
|
|
|
|
RuntimeError::ValidationFailure(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum ValidationError {
|
2024-03-07 17:29:07 +00:00
|
|
|
CannotIndex(Type),
|
|
|
|
CannotIndexWith(Type, Type),
|
2024-02-29 02:04:38 +00:00
|
|
|
ExpectedBoolean,
|
2024-03-09 13:10:54 +00:00
|
|
|
ExpectedFunction,
|
2024-03-07 11:33:54 +00:00
|
|
|
ExpectedIntegerOrFloat,
|
2024-03-09 13:10:54 +00:00
|
|
|
ExpectedValue,
|
2024-03-08 17:24:11 +00:00
|
|
|
InterpreterExpectedReturn,
|
2024-03-06 17:15:03 +00:00
|
|
|
RwLockPoison(RwLockPoisonError),
|
|
|
|
TypeCheck(TypeCheckError),
|
2024-03-06 22:32:31 +00:00
|
|
|
VariableNotFound(Identifier),
|
2024-02-29 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RwLockPoisonError> for ValidationError {
|
|
|
|
fn from(error: RwLockPoisonError) -> Self {
|
|
|
|
ValidationError::RwLockPoison(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 17:15:03 +00:00
|
|
|
impl From<TypeCheckError> for ValidationError {
|
|
|
|
fn from(error: TypeCheckError) -> Self {
|
|
|
|
ValidationError::TypeCheck(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 18:49:26 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct RwLockPoisonError;
|
|
|
|
|
|
|
|
impl<T> From<PoisonError<T>> for RwLockPoisonError {
|
|
|
|
fn from(_: PoisonError<T>) -> Self {
|
|
|
|
RwLockPoisonError
|
|
|
|
}
|
|
|
|
}
|
2024-03-06 17:15:03 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct TypeCheckError {
|
|
|
|
pub actual: Type,
|
|
|
|
pub expected: Type,
|
|
|
|
}
|