rasn_compiler/
error.rs

1use std::error::Error;
2use std::fmt::Display;
3
4use crate::{
5    lexer::error::LexerError,
6    prelude::{ir::GrammarError, GeneratorError},
7    validator::error::LinkerError,
8};
9
10#[derive(Debug, Clone, PartialEq)]
11pub enum CompilerError {
12    Lexer(LexerError),
13    Grammar(GrammarError),
14    Linker(LinkerError),
15    Generator(GeneratorError),
16}
17
18impl CompilerError {
19    pub fn contextualize(&self, input: &str) -> String {
20        match self {
21            CompilerError::Lexer(lexer_error) => lexer_error.contextualize(input),
22            e => format!("{e}"),
23        }
24    }
25}
26
27impl Display for CompilerError {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            CompilerError::Lexer(lexer_error) => Display::fmt(lexer_error, f),
31            CompilerError::Grammar(grammar_error) => Display::fmt(grammar_error, f),
32            CompilerError::Linker(linker_error) => Display::fmt(linker_error, f),
33            CompilerError::Generator(generator_error) => Display::fmt(generator_error, f),
34        }
35    }
36}
37
38impl Error for CompilerError {}
39
40impl From<LexerError> for CompilerError {
41    fn from(value: LexerError) -> Self {
42        Self::Lexer(value)
43    }
44}
45
46impl From<LinkerError> for CompilerError {
47    fn from(value: LinkerError) -> Self {
48        Self::Linker(value)
49    }
50}
51
52impl From<GeneratorError> for CompilerError {
53    fn from(value: GeneratorError) -> Self {
54        Self::Generator(value)
55    }
56}
57
58impl From<GrammarError> for CompilerError {
59    fn from(value: GrammarError) -> Self {
60        Self::Grammar(value)
61    }
62}