datex_core/compiler/
error.rs

1use crate::compiler::ast_parser::{DatexExpression, ParserError};
2use std::fmt::Display;
3#[derive(Debug)]
4pub enum CompilerError {
5    UnexpectedTerm(DatexExpression),
6    ParserErrors(Vec<ParserError>),
7    SerializationError(binrw::Error),
8    BigDecimalOutOfBoundsError,
9    IntegerOutOfBoundsError,
10    InvalidPlaceholderCount,
11    NonStaticValue,
12    UndeclaredVariable(String),
13    ScopePopError,
14    InvalidSlotName(String),
15    AssignmentToConst(String),
16    OnceScopeUsedMultipleTimes,
17}
18impl From<Vec<ParserError>> for CompilerError {
19    fn from(value: Vec<ParserError>) -> Self {
20        CompilerError::ParserErrors(value)
21    }
22}
23
24impl Display for CompilerError {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            CompilerError::UnexpectedTerm(rule) => {
28                write!(f, "Unexpected term: {rule:?}")
29            }
30            CompilerError::ParserErrors(error) => {
31                write!(f, "Syntax error") // TODO #153
32            }
33            CompilerError::SerializationError(error) => {
34                write!(f, "Serialization error: {error}")
35            }
36            CompilerError::BigDecimalOutOfBoundsError => {
37                write!(f, "BigDecimal out of bounds error")
38            }
39            CompilerError::IntegerOutOfBoundsError => {
40                write!(f, "Integer out of bounds error")
41            }
42            CompilerError::InvalidPlaceholderCount => {
43                write!(f, "Invalid placeholder count")
44            }
45            CompilerError::NonStaticValue => {
46                write!(f, "Encountered non-static value")
47            }
48            CompilerError::UndeclaredVariable(var) => {
49                write!(f, "Use of undeclared variable: {var}")
50            }
51            CompilerError::ScopePopError => {
52                write!(f, "Could not pop scope, stack is empty")
53            }
54            CompilerError::InvalidSlotName(name) => {
55                write!(f, "Slot #{name} does not exist") 
56            }
57            CompilerError::AssignmentToConst(name) => {
58                write!(f, "Cannot assign to immutable variable: {name}")
59            }
60            CompilerError::OnceScopeUsedMultipleTimes => {
61                write!(f, "Scope cannot be used multiple times, set 'once' to false to use a scope multiple times")
62            }
63        }
64    }
65}