expr_solver/
error.rs

1//! Error types for parsing, linking, and program operations.
2
3use crate::SymbolError;
4use crate::span::Span;
5use crate::span::SpanError;
6use thiserror::Error;
7
8/// Errors that can occur during parsing.
9#[derive(Error, Debug)]
10pub enum ParseError {
11    #[error("Unexpected token: {message}")]
12    UnexpectedToken { message: String, span: Span },
13    #[error("Unexpected end of input")]
14    UnexpectedEof { span: Span },
15    #[error("Invalid number literal: {message}")]
16    InvalidNumber { message: String, span: Span },
17}
18
19impl SpanError for ParseError {
20    fn span(&self) -> Span {
21        match self {
22            ParseError::UnexpectedToken { span, .. } => *span,
23            ParseError::UnexpectedEof { span } => *span,
24            ParseError::InvalidNumber { span, .. } => *span,
25        }
26    }
27}
28
29/// Errors that can occur during IR (bytecode) generation.
30#[derive(Error, Debug)]
31pub enum IrError {
32    #[error("{0}")]
33    SymbolError(#[from] SymbolError),
34}
35
36/// Errors that can occur during the linking process.
37#[derive(Error, Debug)]
38pub enum LinkerError {
39    #[error("Link error: Type mismatch for symbol '{name}': expected {expected}, found {found}")]
40    TypeMismatch {
41        name: String,
42        expected: String,
43        found: String,
44    },
45
46    #[error("{0}")]
47    SymbolError(#[from] SymbolError),
48}
49
50/// Errors that can occur during program operations.
51#[derive(Error, Debug)]
52pub enum ProgramError {
53    #[error("{0}")]
54    ParseError(String),
55
56    #[error("{0}")]
57    IrError(#[from] IrError),
58
59    #[error("{0}")]
60    LinkerError(#[from] LinkerError),
61
62    #[error("{0}")]
63    VmError(#[from] crate::vm::VmError),
64
65    #[error("{0}")]
66    SymError(#[from] SymbolError),
67
68    #[cfg(feature = "serialization")]
69    #[error("Serialization error: {0}")]
70    SerializationError(#[from] bincode::error::EncodeError),
71
72    #[cfg(feature = "serialization")]
73    #[error("Deserialization error: {0}")]
74    DeserializationError(#[from] bincode::error::DecodeError),
75
76    #[cfg(feature = "serialization")]
77    #[error("Incompatible program version: expected {expected}, got {found}")]
78    IncompatibleVersion { expected: String, found: String },
79
80    #[cfg(feature = "serialization")]
81    #[error("IO error: {0}")]
82    IoError(#[from] std::io::Error),
83}