crftng-intrprtrs/src/lexer/error.rs

38 lines
805 B
Rust
Raw Normal View History

2022-03-21 15:47:35 +01:00
use core::fmt;
use std::error::Error;
#[derive(Debug)]
pub struct LexingError {
pub line: usize,
2022-03-21 18:42:47 +01:00
pub kind: LexingErrorKind,
2022-03-21 15:47:35 +01:00
}
impl fmt::Display for LexingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2022-03-21 18:42:47 +01:00
write!(f, "Error: {}\n on line: {}", self.kind, self.line)
2022-03-21 15:47:35 +01:00
}
}
impl Error for LexingError {
2022-03-21 18:42:47 +01:00
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.kind)
2022-03-21 15:47:35 +01:00
}
}
2022-03-21 18:42:47 +01:00
#[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 {}