mathengine_parser/
error.rs1use mathengine_lexer::Token;
2use std::fmt;
3
4#[derive(Debug, Clone, PartialEq)]
5pub enum ParseError {
6 UnexpectedToken {
7 expected: String,
8 found: Token,
9 position: usize,
10 },
11 UnexpectedEndOfInput {
12 expected: String,
13 },
14 InvalidExpression {
15 message: String,
16 position: usize,
17 },
18 EmptyTokenStream,
19}
20
21impl fmt::Display for ParseError {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 match self {
24 ParseError::UnexpectedToken {
25 expected,
26 found,
27 position,
28 } => {
29 write!(
30 f,
31 "Expected {} but found {:?} at position {}",
32 expected, found, position
33 )
34 }
35 ParseError::UnexpectedEndOfInput { expected } => {
36 write!(f, "Expected {} but reached end of input", expected)
37 }
38 ParseError::InvalidExpression { message, position } => {
39 write!(
40 f,
41 "Invalid expression at position {}: {}",
42 position, message
43 )
44 }
45 ParseError::EmptyTokenStream => {
46 write!(f, "Cannot parse empty token stream")
47 }
48 }
49 }
50}
51
52impl std::error::Error for ParseError {}