This repository has been archived on 2026-08-20. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
el-retired/crates/el-parser/src/error.rs
T

55 lines
1.2 KiB
Rust

//! 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<String>, 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)
}
}