about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/treewalk/errors.rs
blob: 391663d51b1836d52bc170dbfca213ed083683cb (plain) (blame)
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 crate::scanner::ScannerError;
use crate::treewalk::interpreter::Value;

use std::fmt;

#[derive(Debug)]
pub enum ErrorKind {
    UnexpectedChar(char),
    UnterminatedString,
    UnmatchedParens,
    ExpectedExpression(String),
    ExpectedSemicolon,
    ExpectedClosingBrace,
    ExpectedToken(&'static str),
    TypeError(String),
    UndefinedVariable(String),
    InternalError(String),
    InvalidAssignmentTarget(String),
    RuntimeError(String),
    StaticError(String),

    // This variant is not an error, rather it is used for
    // short-circuiting out of a function body that hits a `return`
    // statement.
    //
    // It's implemented this way because in the original book the
    // author uses exceptions for control flow, and this is the
    // closest equivalent that I had available without diverging too
    // much.
    FunctionReturn(Value),
}

#[derive(Debug)]
pub struct Error {
    pub line: usize,
    pub kind: ErrorKind,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[line {}] Error: {:?}", self.line, self.kind)
    }
}

impl From<ScannerError> for Error {
    fn from(err: ScannerError) -> Self {
        match err {
            ScannerError::UnexpectedChar { line, unexpected } => Error {
                line,
                kind: ErrorKind::UnexpectedChar(unexpected),
            },

            ScannerError::UnterminatedString { line } => Error {
                line,
                kind: ErrorKind::UnterminatedString,
            },
        }
    }
}