crftng-intrprtrs/src/lexer/error.rs
2022-03-21 20:17:51 +01:00

37 lines
805 B
Rust

use core::fmt;
use std::error::Error;
#[derive(Debug)]
pub struct LexingError {
pub line: usize,
pub kind: LexingErrorKind,
}
impl fmt::Display for LexingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Error: {}\n on line: {}", self.kind, self.line)
}
}
impl Error for LexingError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.kind)
}
}
#[derive(Debug)]
pub enum LexingErrorKind {
UnmatchedQuote,
UnexpectedCharacter(char),
}
impl fmt::Display for LexingErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LexingErrorKind::UnmatchedQuote => write!(f, "Unmatched \" character"),
LexingErrorKind::UnexpectedCharacter(c) => write!(f, "Unexpected character {c}"),
}
}
}
impl Error for LexingErrorKind {}