Skip to main content

lasm/
error.rs

1use thiserror::Error;
2
3#[cfg(feature = "llvm")]
4use inkwell::error::Error as InkwellError;
5#[cfg(feature = "cranelift")]
6use cranelift_module::ModuleError;
7
8/// Location information for errors
9#[derive(Debug, Clone, PartialEq)]
10pub struct Location {
11    pub line: usize,
12    pub column: usize,
13    pub line_text: String,
14}
15
16impl Location {
17    /// Generates a string with a caret (^) pointing to the error location.
18    pub fn caret(&self) -> String {
19        format!("{}^", " ".repeat(self.column))
20    }
21}
22
23/// Error type for LASM
24#[derive(Error, Debug)]
25pub enum LasmError {
26    /// Errors that occur during parsing, with a message and location.
27    #[error("Parse error: {message}")]
28    Parse {
29        message: String,
30        location: Location,
31    },
32    /// Errors that occur during type checking, with a message.
33    #[error("Type error: {0}")]
34    Type(String),
35    /// Errors that occur during compilation, with a message and optional location.
36    #[error("Compile error: {message}")]
37    Compile { 
38        message: String,
39        location: Option<Location>,
40    },
41    /// Errors that occur during runtime, with a message.
42    #[error("Runtime error: {0}")]
43    Runtime(String),
44    /// Propagated io errors from file reading/writing.
45    #[error("IO error: {0}")]
46    Io(#[from] std::io::Error),
47    /// Propagated LLVM errors when the `llvm` feature is enabled.
48    #[cfg(feature = "llvm")]
49    #[error("LLVM error: {0}")]
50    LLVM(#[from] InkwellError),
51    /// Propagated Cranelift errors when the `cranelift` feature is enabled.
52    #[cfg(feature = "cranelift")]
53    #[error("Module error: {0}")]
54    Module(#[from] ModuleError),
55}