expr_solver/
error.rs

1//! Error types for parsing, linking, and program operations.
2
3use crate::span::Span;
4use crate::span::SpanError;
5use thiserror::Error;
6
7/// Errors that can occur during parsing.
8#[derive(Error, Debug)]
9pub enum ParseError {
10    #[error("Unexpected token: {message}")]
11    UnexpectedToken { message: String, span: Span },
12    #[error("Unexpected end of input")]
13    UnexpectedEof { span: Span },
14    #[error("Invalid number literal: {message}")]
15    InvalidNumber { message: String, span: Span },
16}
17
18impl SpanError for ParseError {
19    fn span(&self) -> Span {
20        match self {
21            ParseError::UnexpectedToken { span, .. } => *span,
22            ParseError::UnexpectedEof { span } => *span,
23            ParseError::InvalidNumber { span, .. } => *span,
24        }
25    }
26}
27
28/// Errors that can occur during linking.
29#[derive(Error, Debug)]
30pub enum LinkError {
31    #[error("Missing symbol: '{name}' is required by bytecode but not in symbol table")]
32    MissingSymbol { name: String },
33
34    #[error("Type mismatch for symbol '{name}': expected {expected}, found {found}")]
35    TypeMismatch {
36        name: String,
37        expected: String,
38        found: String,
39    },
40
41    #[error("Symbol table error: {0}")]
42    SymbolTableError(#[from] crate::symbol::SymbolError),
43}
44
45/// Errors that can occur during program operations.
46#[derive(Error, Debug)]
47pub enum ProgramError {
48    #[error("{0}")]
49    ParseError(String),
50
51    #[error("Link error: {0}")]
52    LinkError(#[from] LinkError),
53
54    #[error("Serialization error: {0}")]
55    SerializationError(#[from] bincode::error::EncodeError),
56
57    #[error("Deserialization error: {0}")]
58    DeserializationError(#[from] bincode::error::DecodeError),
59
60    #[error("Incompatible program version: expected {expected}, got {found}")]
61    IncompatibleVersion { expected: String, found: String },
62
63    #[error("Invalid symbol index: {0}")]
64    InvalidSymbolIndex(usize),
65
66    #[error("IO error: {0}")]
67    IoError(#[from] std::io::Error),
68}