Skip to main content

harn_parser/parser/
error.rs

1use harn_lexer::Span;
2use std::fmt;
3
4/// Parser errors.
5#[derive(Debug, Clone, PartialEq)]
6pub enum ParserError {
7    Unexpected {
8        got: String,
9        expected: String,
10        span: Span,
11    },
12    UnexpectedEof {
13        expected: String,
14        span: Span,
15    },
16}
17
18impl fmt::Display for ParserError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            ParserError::Unexpected {
22                got,
23                expected,
24                span,
25            } => write!(
26                f,
27                "Expected {expected}, got {got} at {}:{}",
28                span.line, span.column
29            ),
30            ParserError::UnexpectedEof { expected, .. } => {
31                write!(f, "Unexpected end of file, expected {expected}")
32            }
33        }
34    }
35}
36
37impl std::error::Error for ParserError {}