aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/ast/error.rs
blob: 55983762e36e7ff71c3ccb3cece88b6e95ddaffa (plain)
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
use std::{error, fmt, num::ParseIntError, rc::Rc};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
    // Number
    InvalidIntegerLiteral(Rc<str>, ParseIntError),

    // String
    UnclosedString(Rc<str>),
    UnexpectedEscapeChar(char),

    // Par
    UnexpectedClosePar,
    UnclosedPar,

    UnexpectedToken,

    UnexpectedEof,

    RecursionLimit,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::InvalidIntegerLiteral(number, err) => {
                write!(f, "invalid integer literal {number}: {err}")
            }
            Error::UnclosedString(string) => write!(f, "unclosed string {string:?}"),
            Error::UnexpectedEscapeChar(ch) => write!(f, "unexpected escape char {ch:?}"),
            Error::UnexpectedClosePar => write!(f, "unexpected `)`"),
            Error::UnclosedPar => write!(f, "unclosed `(`"),
            Error::UnexpectedToken => write!(f, "unexpected token"),
            Error::UnexpectedEof => write!(f, "unexpected eof"),
            Error::RecursionLimit => write!(f, "recursion limit"),
        }
    }
}

impl error::Error for Error {}