aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/error.rs
blob: 4f90d1422f84278b28e8335eb20e7444e3b879ef (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::lisp::lex::{Span, SpanDisplay};

use std::fmt;

#[derive(Debug, PartialEq, Clone)]
pub enum LispError {
    Parse(ParseError),
    Eval(EvalError),
}

impl fmt::Display for LispError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Parse(p) => write!(f, "parse error: {}", p.kind),
            Self::Eval(e) => write!(f, "eval error: {}", e),
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct ParseError {
    pub span: Span,
    pub kind: ParseErrorKind,
}

impl ParseError {
    pub fn new(span: Span, kind: ParseErrorKind) -> Self {
        Self { span, kind }
    }
    pub fn fmt(&self, f: &mut fmt::Formatter<'_>, text: &str) -> fmt::Result {
        let SpanDisplay { line, col, .. } = SpanDisplay::highlight_span(self.span, text);
        write!(f, "line {}, col {}: {}", line, col, self.kind)
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum ParseErrorKind {
    InvalidLiteral,
    InvalidToken,
    InvalidChar(char),
    LiteralParseError,
    MissingCloseParen,
    UnbalancedComma,
    UnexpectedEof,
    UnexpectedToken {
        expected: &'static str,
        found: &'static str,
    },
    UnmatchedParen,
    UnterminatedString,
}

impl fmt::Display for ParseErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseErrorKind::InvalidChar(ch) => write!(f, "invalid character {}", ch),
            ParseErrorKind::InvalidToken => write!(f, "invalid token"),
            ParseErrorKind::UnexpectedEof => write!(f, "unexpected end of file"),
            ParseErrorKind::InvalidLiteral => write!(f, "invalid literal"),
            ParseErrorKind::UnmatchedParen => write!(f, "unmatched `)`"),
            ParseErrorKind::UnbalancedComma => write!(f, "unbalanced comma"),
            ParseErrorKind::UnexpectedToken { expected, found } => {
                write!(
                    f,
                    "unexpected token, got `{}`, expected `{}`",
                    found, expected
                )
            }
            ParseErrorKind::LiteralParseError => write!(f, "error parsing literal"),
            ParseErrorKind::MissingCloseParen => write!(f, "missing `)`"),
            ParseErrorKind::UnterminatedString => write!(f, "unterminated string literal"),
        }
    }
}

impl From<ParseError> for LispError {
    fn from(p: ParseError) -> Self {
        LispError::Parse(p)
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum EvalError {
    ArgumentCount(Option<u32>), // expected
    BadForm,
    UnboundVariable(String),
    DivByZero,
    TypeMismatch,
}

impl fmt::Display for EvalError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ArgumentCount(i) => {
                write!(f, "invalid number of arguments, expected atleast {:?}", i)
            }
            Self::BadForm => write!(f, "bad expression form"),
            Self::UnboundVariable(s) => write!(f, "unbound variable {}", s),
            Self::TypeMismatch => write!(f, "mismatched types"),
            Self::DivByZero => write!(f, "attempt to divide by zero"),
        }
    }
}

impl From<EvalError> for LispError {
    fn from(e: EvalError) -> Self {
        LispError::Eval(e)
    }
}