Skip to main content

runec/
error.rs

1use crate::token::Token;
2use std::error::Error;
3use std::fmt;
4use std::path::PathBuf;
5
6/// Top-level error returned from `main()`. User-facing diagnostics flow
7/// through `DiagnosticsCollector` and never go through this Display impl —
8/// these variants only carry a process-exit cause for `Result<(), Box<dyn Error>>`.
9#[derive(Debug)]
10pub enum CompilerError {
11    FileNotFound(String),
12    IO(std::io::Error),
13    TypeError(String),
14    CodeGenError(String),
15}
16
17impl Error for CompilerError {}
18
19impl fmt::Display for CompilerError {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        match self {
22            CompilerError::IO(err) => writeln!(f, "IOError: {}", err),
23            CompilerError::FileNotFound(err) => writeln!(f, "FileNotFoundError: {}", err),
24            CompilerError::TypeError(err) => writeln!(f, "TypeError: {}", err),
25            CompilerError::CodeGenError(err) => writeln!(f, "CodeGenError: {}", err),
26        }
27    }
28}
29
30/// Lexer error payload. Converted into `Diagnostic` at the call site
31/// (`main.rs`); never rendered directly. No `Display` impl by design — all
32/// user-facing formatting lives in `Diagnostic`.
33#[derive(Debug)]
34pub struct LexingError {
35    pub path: PathBuf,
36    pub message: String,
37    pub line: usize,
38    pub position: usize,
39}
40
41impl Error for LexingError {}
42
43impl fmt::Display for LexingError {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        // Stub — kept only to satisfy `Error`. Formatting goes through
46        // Diagnostic; if you see this output, a phase forgot to convert.
47        write!(f, "{} ({}:{})", self.message, self.line, self.position)
48    }
49}
50
51impl LexingError {
52    pub fn new(path: PathBuf, message: String, line: usize, position: usize) -> Self {
53        Self {
54            path,
55            message,
56            line,
57            position,
58        }
59    }
60}
61
62/// Parser error payload. Converted into `Diagnostic` at the call site
63/// (`main.rs`); never rendered directly. See `LexingError`.
64#[derive(Debug)]
65pub struct SyntaxError {
66    pub path: PathBuf,
67    pub message: String,
68    pub line: usize,
69    pub position: usize,
70}
71
72impl Error for SyntaxError {}
73
74impl fmt::Display for SyntaxError {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        write!(f, "{} ({}:{})", self.message, self.line, self.position)
77    }
78}
79
80impl SyntaxError {
81    pub fn from_token(path: PathBuf, token: &Token, message: String) -> Self {
82        Self {
83            path,
84            message,
85            line: token.line,
86            position: token.column,
87        }
88    }
89}