use super::{ErrorTrait, Position}; use std::fmt; #[derive(Debug, PartialEq, Eq)] pub enum Error { OpenParenthesisExpected(Position), CloseParenthesisExpected(Position), CloseParenthesisUnexpected(Position), LambdaExpected(Position), DotExpected(Position), DotUnexpected(Position), TokenUnexpected(Position), ParameterExpected(Position), EndExpected(Position), EndUnexpected(Position), UnboundVariable(String, Position), NoExpression(Position), } impl ErrorTrait for Error { fn open_parenthesis_expected(position: Position) -> Self { Error::OpenParenthesisExpected(position) } fn close_parenthesis_expected(position: Position) -> Self { Error::CloseParenthesisExpected(position) } fn close_parenthesis_unexpected(position: Position) -> Self { Error::CloseParenthesisUnexpected(position) } fn lambda_expected(position: Position) -> Self { Error::LambdaExpected(position) } fn dot_expected(position: Position) -> Self { Error::DotExpected(position) } fn dot_unexpected(position: Position) -> Self { Error::DotUnexpected(position) } fn token_unexpected(position: Position) -> Self { Error::TokenUnexpected(position) } fn parameter_expected(position: Position) -> Self { Error::ParameterExpected(position) } fn end_expected(position: Position) -> Self { Error::EndExpected(position) } fn end_unexpected(position: Position) -> Self { Error::EndUnexpected(position) } fn unbound_variable(identifier: String, position: Position) -> Self { Error::UnboundVariable(identifier, position) } fn no_expression(position: Position) -> Self { Error::NoExpression(position) } } impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::OpenParenthesisExpected(pos) => { write!(f, "expected an opening parenthesis {}", pos) } Error::CloseParenthesisExpected(pos) => { write!(f, "expected a closing parenthesis {}", pos) } Error::CloseParenthesisUnexpected(pos) => { write!(f, "unexpected closing parenthesis {}", pos) } Error::LambdaExpected(pos) => write!(f, "expected a lambda {}", pos), Error::DotExpected(pos) => write!(f, "expected a dot {}", pos), Error::DotUnexpected(pos) => write!(f, "unexpected dot {}", pos), Error::TokenUnexpected(pos) => write!(f, "unexpected token {}", pos), Error::ParameterExpected(pos) => write!(f, "expected a parameter declaration {}", pos), Error::EndExpected(pos) => write!(f, "expected the end of the expression {}", pos), Error::EndUnexpected(pos) => write!(f, "unexpected end of the expression {}", pos), Error::UnboundVariable(name, pos) => write!(f, "unbound variable \"{}\" {}", name, pos), Error::NoExpression(pos) => write!(f, "expected an expression {}", pos), } } }