58 lines
1.6 KiB
Rust
58 lines
1.6 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, Option<Box<CompilerError>>),
|
|
ExpectedIdent(Position),
|
|
ExpectedStatement(Position, Option<Box<CompilerError>>),
|
|
ExpectedPattern(Position),
|
|
}
|
|
|
|
impl CompilerError {
|
|
fn from_opt(from: &Option<Box<CompilerError>>) -> String {
|
|
if let Some(err) = from {
|
|
format!("\n {}", err)
|
|
} else {
|
|
String::new()
|
|
}
|
|
}
|
|
}
|
|
|
|
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 {}{}",
|
|
pos,
|
|
CompilerError::from_opt(err)
|
|
),
|
|
CompilerError::ExpectedIdent(pos) => format!("Expected ident at {}", pos),
|
|
CompilerError::ExpectedStatement(pos, err) => format!(
|
|
"Expected statement at {}{}",
|
|
pos,
|
|
CompilerError::from_opt(err)
|
|
),
|
|
CompilerError::ExpectedPattern(pos) => format!("Expected pattern at {}", pos),
|
|
};
|
|
write!(f, "{}", text)
|
|
}
|
|
}
|