aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/ast/error.rs
blob: 8117513d0bcee209c477e1da802ac68e20cfb7e4 (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
41
42
43
44
45
use std::{
    error, fmt,
    num::{ParseFloatError, ParseIntError},
    rc::Rc,
};

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

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

    // Par
    UnexpectedRightPar,
    UnclosedLeftPar,

    UnexpectedEof,

    RecursionLimit,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::InvalidFloatLiteral(number, err) => {
                write!(f, "invalid float literal {number}: {err}")
            }
            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::UnexpectedRightPar => write!(f, "unexpected right par"),
            Error::UnclosedLeftPar => write!(f, "unclosed left par"),
            Error::UnexpectedEof => write!(f, "unexpected eof"),
            Error::RecursionLimit => write!(f, "recursion limit"),
        }
    }
}

impl error::Error for Error {}