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

212 lines
5.3 KiB
Rust
Raw Normal View History

2024-06-21 22:28:12 +00:00
use std::{io, sync::PoisonError as StdPoisonError};
2024-02-25 18:49:26 +00:00
use chumsky::{prelude::Rich, span::Span};
2024-02-25 18:49:26 +00:00
2024-03-06 22:32:31 +00:00
use crate::{
2024-06-24 14:26:38 +00:00
abstract_tree::{r#type::Type, Expression, SourcePosition, TypeConstructor},
2024-03-25 04:16:55 +00:00
identifier::Identifier,
2024-03-06 22:32:31 +00:00
lexer::Token,
};
2024-02-25 18:49:26 +00:00
#[derive(Debug, PartialEq)]
2024-06-18 23:42:04 +00:00
pub enum DustError {
2024-06-17 21:38:24 +00:00
Lex {
2024-03-06 22:32:31 +00:00
expected: String,
2024-03-16 19:01:45 +00:00
span: (usize, usize),
2024-06-17 21:38:24 +00:00
reason: String,
2024-03-06 22:32:31 +00:00
},
2024-06-17 21:38:24 +00:00
Parse {
2024-03-06 22:32:31 +00:00
expected: String,
2024-03-16 19:01:45 +00:00
span: (usize, usize),
2024-06-17 21:38:24 +00:00
found: Option<String>,
2024-03-06 22:32:31 +00:00
},
2024-03-17 17:36:31 +00:00
Runtime {
error: RuntimeError,
position: SourcePosition,
},
2024-03-17 11:31:45 +00:00
Validation {
error: ValidationError,
2024-03-17 11:48:06 +00:00
position: SourcePosition,
2024-03-17 11:31:45 +00:00
},
2024-02-25 18:49:26 +00:00
}
2024-06-18 23:42:04 +00:00
impl From<Rich<'_, char>> for DustError {
2024-03-06 20:36:58 +00:00
fn from(error: Rich<'_, char>) -> Self {
2024-06-18 23:42:04 +00:00
DustError::Lex {
2024-03-06 20:36:58 +00:00
expected: error.expected().map(|error| error.to_string()).collect(),
2024-03-16 19:01:45 +00:00
span: (error.span().start(), error.span().end()),
2024-03-20 12:36:18 +00:00
reason: error.reason().to_string(),
2024-03-06 20:36:58 +00:00
}
2024-02-25 18:49:26 +00:00
}
}
2024-06-18 23:42:04 +00:00
impl<'src> From<Rich<'_, Token<'src>>> for DustError {
2024-03-06 20:36:58 +00:00
fn from(error: Rich<'_, Token<'src>>) -> Self {
2024-06-18 23:42:04 +00:00
DustError::Parse {
2024-03-06 20:36:58 +00:00
expected: error.expected().map(|error| error.to_string()).collect(),
2024-03-16 19:01:45 +00:00
span: (error.span().start(), error.span().end()),
2024-05-18 18:27:42 +00:00
found: error.found().map(|token| token.to_string()),
2024-03-06 20:36:58 +00:00
}
}
}
2024-03-17 21:39:39 +00:00
#[derive(Debug)]
2024-02-25 18:49:26 +00:00
pub enum RuntimeError {
2024-03-17 21:39:39 +00:00
Io(io::Error),
2024-06-21 22:28:12 +00:00
RwLockPoison(PoisonError),
ValidationFailure(ValidationError),
2024-06-04 18:47:15 +00:00
SerdeJson(serde_json::Error),
2024-07-01 14:40:36 +00:00
Use(Vec<DustError>),
2024-02-25 18:49:26 +00:00
}
2024-06-21 22:28:12 +00:00
impl From<PoisonError> for RuntimeError {
fn from(error: PoisonError) -> Self {
2024-02-25 18:49:26 +00:00
RuntimeError::RwLockPoison(error)
}
}
2024-06-21 22:28:12 +00:00
impl<T> From<StdPoisonError<T>> for RuntimeError {
fn from(_: StdPoisonError<T>) -> Self {
RuntimeError::RwLockPoison(PoisonError)
2024-03-20 21:05:37 +00:00
}
}
impl From<ValidationError> for RuntimeError {
fn from(error: ValidationError) -> Self {
RuntimeError::ValidationFailure(error)
}
}
2024-03-17 21:39:39 +00:00
impl From<io::Error> for RuntimeError {
fn from(error: io::Error) -> Self {
RuntimeError::Io(error)
}
}
2024-06-04 18:47:15 +00:00
impl From<serde_json::Error> for RuntimeError {
fn from(error: serde_json::Error) -> Self {
RuntimeError::SerdeJson(error)
}
}
2024-03-17 21:39:39 +00:00
impl PartialEq for RuntimeError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(RuntimeError::Io(_), RuntimeError::Io(_)) => false,
(RuntimeError::RwLockPoison(_), RuntimeError::RwLockPoison(_)) => true,
(RuntimeError::ValidationFailure(left), RuntimeError::ValidationFailure(right)) => {
left == right
}
_ => false,
}
}
}
#[derive(Debug, PartialEq)]
pub enum ValidationError {
/// A node needs to be initialized before it can be validated.
Uninitialized,
2024-06-24 11:13:54 +00:00
BuiltInFunctionFailure(&'static str),
CannotAssignToNone(SourcePosition),
2024-03-17 22:03:43 +00:00
CannotIndex {
r#type: Type,
position: SourcePosition,
},
2024-03-18 01:07:03 +00:00
CannotIndexWith {
collection_type: Type,
2024-03-18 07:24:41 +00:00
collection_position: SourcePosition,
2024-03-18 01:07:03 +00:00
index_type: Type,
2024-03-18 07:24:41 +00:00
index_position: SourcePosition,
2024-03-18 01:07:03 +00:00
},
CannotUsePath(String),
2024-06-04 18:47:15 +00:00
ExpectedString {
actual: Type,
position: SourcePosition,
2024-06-22 21:17:35 +00:00
},
ExpectedList {
actual: Type,
position: SourcePosition,
2024-06-04 18:47:15 +00:00
},
2024-03-17 20:59:52 +00:00
ExpectedBoolean {
actual: Type,
position: SourcePosition,
},
ExpectedFunction {
actual: Type,
position: SourcePosition,
},
2024-03-18 07:24:41 +00:00
ExpectedIntegerOrFloat(SourcePosition),
2024-03-23 21:51:40 +00:00
ExpectedIntegerFloatOrString {
actual: Type,
position: SourcePosition,
},
FullTypeNotKnown {
identifier: Identifier,
position: SourcePosition,
},
ExpectedValueStatement(SourcePosition),
ExpectedNonValueStatement(SourcePosition),
2024-06-21 22:28:12 +00:00
RwLockPoison(PoisonError),
2024-03-17 04:49:01 +00:00
TypeCheck {
/// The mismatch that caused the error.
conflict: TypeConflict,
/// The position of the item that gave the "actual" type.
2024-03-17 11:48:06 +00:00
actual_position: SourcePosition,
2024-03-17 04:49:01 +00:00
/// The position of the item that gave the "expected" type.
2024-06-17 14:10:06 +00:00
expected_position: Option<SourcePosition>,
},
2024-06-24 14:26:38 +00:00
WrongTypeArguments {
parameters: Vec<Identifier>,
arguments: Vec<TypeConstructor>,
2024-03-17 04:49:01 +00:00
},
2024-06-24 14:26:38 +00:00
WrongValueArguments {
parameters: Vec<(Identifier, Type)>,
arguments: Vec<Expression>,
2024-03-18 12:15:30 +00:00
},
2024-03-25 04:16:55 +00:00
VariableNotFound {
identifier: Identifier,
position: SourcePosition,
},
2024-06-24 09:26:49 +00:00
FieldNotFound {
2024-03-18 07:24:41 +00:00
identifier: Identifier,
position: SourcePosition,
},
EnumDefinitionNotFound {
identifier: Identifier,
position: Option<SourcePosition>,
},
EnumVariantNotFound {
identifier: Identifier,
position: SourcePosition,
},
}
2024-06-21 22:28:12 +00:00
impl From<PoisonError> for ValidationError {
fn from(error: PoisonError) -> Self {
ValidationError::RwLockPoison(error)
}
}
2024-06-21 22:28:12 +00:00
impl<T> From<StdPoisonError<T>> for ValidationError {
fn from(_: StdPoisonError<T>) -> Self {
ValidationError::RwLockPoison(PoisonError)
2024-03-19 22:31:52 +00:00
}
}
2024-02-25 18:49:26 +00:00
#[derive(Debug, PartialEq)]
2024-06-21 22:28:12 +00:00
pub struct PoisonError;
2024-02-25 18:49:26 +00:00
2024-06-21 22:28:12 +00:00
impl<T> From<StdPoisonError<T>> for PoisonError {
fn from(_: StdPoisonError<T>) -> Self {
PoisonError
2024-02-25 18:49:26 +00:00
}
}
2024-03-06 17:15:03 +00:00
#[derive(Debug, PartialEq)]
2024-03-17 04:49:01 +00:00
pub struct TypeConflict {
2024-03-06 17:15:03 +00:00
pub actual: Type,
pub expected: Type,
}