Reid/src/errors.rs

42 lines
1.2 KiB
Rust

use super::parser::Position;
use std::fmt;
use std::fmt::Display;
use std::io;
#[derive(Debug)]
pub enum GenericError {
StdIOError(io::Error),
}
impl From<io::Error> for GenericError {
fn from(error: io::Error) -> Self {
Self::StdIOError(error)
}
}
#[derive(Debug)]
pub enum CompilerError {
Fatal,
ExpectedToken(Position, char),
ExpectedExpression(Position, Box<CompilerError>),
ExpectedIdent(Position),
ExpectedStatement(Position),
ExpectedPattern(Position),
}
impl Display for CompilerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let text = match self {
CompilerError::Fatal => "Fatal error".to_string(),
CompilerError::ExpectedToken(pos, c) => format!("Expected token '{}' at {}", c, pos),
CompilerError::ExpectedExpression(pos, err) => {
format!("Expected expression at {}.\n {}", pos, err)
}
CompilerError::ExpectedIdent(pos) => format!("Expected ident at {}", pos),
CompilerError::ExpectedStatement(pos) => format!("Expected statement at {}", pos),
CompilerError::ExpectedPattern(pos) => format!("Expected pattern at {}", pos),
};
write!(f, "{}", text)
}
}