datex_core/compiler/
error.rs

1use crate::ast::{DatexExpression, error::error::ParseError};
2use std::fmt::Display;
3#[derive(Debug)]
4pub enum CompilerError {
5    UnexpectedTerm(Box<DatexExpression>),
6    ParseErrors(Vec<ParseError>),
7    SerializationError(binrw::Error),
8    BigDecimalOutOfBoundsError,
9    IntegerOutOfBoundsError,
10    InvalidPlaceholderCount,
11    NonStaticValue,
12    UndeclaredVariable(String),
13    InvalidRedeclaration(String),
14    SubvariantNotFound(String, String),
15    ScopePopError,
16    InvalidSlotName(String),
17    AssignmentToConst(String),
18    AssignmentToImmutableReference(String),
19    AssignmentToImmutableValue(String),
20    OnceScopeUsedMultipleTimes,
21}
22impl From<Vec<ParseError>> for CompilerError {
23    fn from(value: Vec<ParseError>) -> Self {
24        CompilerError::ParseErrors(value)
25    }
26}
27
28impl Display for CompilerError {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match self {
31            CompilerError::InvalidRedeclaration(name) => {
32                write!(f, "Invalid redeclaration of {name}")
33            }
34            CompilerError::UnexpectedTerm(rule) => {
35                write!(f, "Unexpected term: {rule:?}")
36            }
37            CompilerError::ParseErrors(error) => {
38                for e in error {
39                    writeln!(f, "{}", e.message())?;
40                }
41                Ok(())
42            }
43            CompilerError::SubvariantNotFound(name, variant) => {
44                write!(f, "Subvariant {variant} does not exist for {name}")
45            }
46            CompilerError::SerializationError(error) => {
47                write!(f, "Serialization error: {error}")
48            }
49            CompilerError::BigDecimalOutOfBoundsError => {
50                write!(f, "BigDecimal out of bounds error")
51            }
52            CompilerError::IntegerOutOfBoundsError => {
53                write!(f, "Integer out of bounds error")
54            }
55            CompilerError::InvalidPlaceholderCount => {
56                write!(f, "Invalid placeholder count")
57            }
58            CompilerError::NonStaticValue => {
59                write!(f, "Encountered non-static value")
60            }
61            CompilerError::UndeclaredVariable(var) => {
62                write!(f, "Use of undeclared variable: {var}")
63            }
64            CompilerError::ScopePopError => {
65                write!(f, "Could not pop scope, stack is empty")
66            }
67            CompilerError::InvalidSlotName(name) => {
68                write!(f, "Slot #{name} does not exist")
69            }
70            CompilerError::AssignmentToConst(name) => {
71                write!(f, "Cannot assign to immutable variable: {name}")
72            }
73            CompilerError::OnceScopeUsedMultipleTimes => {
74                write!(
75                    f,
76                    "Scope cannot be used multiple times, set 'once' to false to use a scope multiple times"
77                )
78            }
79            CompilerError::AssignmentToImmutableValue(name) => {
80                write!(f, "Cannot assign to immutable value: {name}")
81            }
82            CompilerError::AssignmentToImmutableReference(name) => {
83                write!(f, "Cannot assign to immutable reference: {name}")
84            }
85        }
86    }
87}