Skip to main content

uxn_tal/
error.rs

1use std::fmt;
2impl fmt::Display for LabelReferenceErrorData {
3    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4        write!(
5            f,
6            "Label reference error at {}:{}:{} for label '{}'\nToken: {:?}\nSource: {}",
7            self.path, self.line, self.position, self.label, self.token, self.source_line
8        )
9    }
10}
11
12use thiserror::Error;
13
14/// Errors that can occur during TAL assembly
15#[derive(Error, Debug)]
16pub struct LabelReferenceErrorData {
17    pub path: String,
18    pub line: usize,
19    pub position: usize,
20    pub label: String,
21    pub token: crate::lexer::TokenWithPos,
22    pub source_line: String,
23}
24
25#[derive(Error, Debug)]
26pub enum AssemblerError {
27    #[error("Label reference error at {0:?}")]
28    LabelReferenceError(Box<LabelReferenceErrorData>),
29    #[error("Expected identifier at {path}:{line}:{position} after token: {after_token} (found '{found}')\nSource: {source_line}")]
30    ExpectedIdentifierError {
31        path: String,
32        line: usize,
33        position: usize,
34        after_token: String,
35        found: char,
36        source_line: String,
37    },
38    #[error("File read error at {path}: {message}")]
39    FileReadError { path: String, message: String },
40    #[error("Syntax error at {path}:{line}:{position} {message}\nSource: {source_line}")]
41    SyntaxError {
42        path: String,
43        line: usize,
44        position: usize,
45        message: String,
46        source_line: String,
47    },
48
49    #[error("Unknown opcode: {opcode}")]
50    UnknownOpcode { opcode: String },
51
52    #[error("Invalid number format: {value}")]
53    InvalidNumber { value: String },
54
55    #[error("UTF-8 error at {path}:{line}:{position} {message}\nSource: {source_line}")]
56    Utf8Error {
57        path: String,
58        line: usize,
59        position: usize,
60        message: String,
61        source_line: String,
62    },
63
64    #[error("Undefined label: {label}")]
65    UndefinedLabel { label: String },
66
67    #[error("Label already defined: {label}")]
68    DuplicateLabel { label: String },
69
70    #[error("Invalid addressing mode")]
71    InvalidAddressing,
72
73    #[error("ROM too large: {size} bytes (maximum 65536)")]
74    RomTooLarge { size: usize },
75
76    #[error("Invalid padding address: {address:04x}")]
77    InvalidPadding { address: u16 },
78
79    #[error("IO error: {0}")]
80    Io(#[from] std::io::Error),
81
82    #[error("Internal error: {message}")]
83    Internal { message: String },
84
85    #[error("Backend error: {message}")]
86    Backend { message: String },
87
88    #[error("Disassembly error: {message}")]
89    Disassembly { message: String },
90}
91
92/// Result type for assembler operations
93pub type Result<T> = std::result::Result<T, AssemblerError>;