about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/errors.rs
blob: 513708df7e85df543e6aad961fd20992eaaeea84 (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
use crate::scanner::ScannerError;

use std::fmt;

#[derive(Debug)]
pub enum ErrorKind {
    UnexpectedChar(char),
    UnterminatedString,
    InternalError(&'static str),
}

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

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[line NYI] Error: {:?}", 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,
            },
        }
    }
}

pub type LoxResult<T> = Result<T, Error>;