rasn_compiler/
error.rs

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