rasn_compiler/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::fmt::Display;

use crate::{
    lexer::error::LexerError,
    prelude::{ir::GrammarError, GeneratorError},
    validator::error::LinkerError,
};

#[derive(Debug, Clone, PartialEq)]
pub enum CompilerError {
    Lexer(LexerError),
    Grammar(GrammarError),
    Linker(LinkerError),
    Generator(GeneratorError),
}

impl CompilerError {
    pub fn contextualize(&self, input: &str) -> String {
        match self {
            CompilerError::Lexer(lexer_error) => lexer_error.contextualize(input),
            e => format!("{e}"),
        }
    }
}

impl Display for CompilerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CompilerError::Lexer(lexer_error) => Display::fmt(lexer_error, f),
            CompilerError::Grammar(grammar_error) => Display::fmt(grammar_error, f),
            CompilerError::Linker(linker_error) => Display::fmt(linker_error, f),
            CompilerError::Generator(generator_error) => Display::fmt(generator_error, f),
        }
    }
}

impl From<LexerError> for CompilerError {
    fn from(value: LexerError) -> Self {
        Self::Lexer(value)
    }
}

impl From<LinkerError> for CompilerError {
    fn from(value: LinkerError) -> Self {
        Self::Linker(value)
    }
}

impl From<GeneratorError> for CompilerError {
    fn from(value: GeneratorError) -> Self {
        Self::Generator(value)
    }
}

impl From<GrammarError> for CompilerError {
    fn from(value: GrammarError) -> Self {
        Self::Grammar(value)
    }
}