//! Parser error types. use thiserror::Error; use el_lexer::{Span, Token}; #[derive(Debug, Clone, Error)] #[error("{kind} at {span}")] pub struct ParseError { pub kind: ParseErrorKind, pub span: Span, } impl ParseError { pub fn new(kind: ParseErrorKind, span: Span) -> Self { Self { kind, span } } } #[derive(Debug, Clone, Error)] pub enum ParseErrorKind { #[error("unexpected token {got}, expected {expected}")] UnexpectedToken { expected: String, got: String }, #[error("unexpected end of file")] UnexpectedEof, #[error("invalid expression starting with {0}")] InvalidExprStart(String), #[error("invalid type expression: {0}")] InvalidTypeExpr(String), #[error("invalid pattern: {0}")] InvalidPattern(String), #[error("expected identifier, got {0}")] ExpectedIdent(String), } impl ParseError { pub fn expected(expected: impl Into, got: &Token, span: Span) -> Self { Self::new( ParseErrorKind::UnexpectedToken { expected: expected.into(), got: got.to_string(), }, span, ) } pub fn eof(span: Span) -> Self { Self::new(ParseErrorKind::UnexpectedEof, span) } }