backyard_parser/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::fmt::{ Display, Formatter };

use backyard_lexer::{ error::LexError, token::Token };

#[derive(Debug, Clone, PartialEq)]
pub enum ParserError {
  LexError(LexError),
  Internal,
  Eof,
  UnexpectedToken(Token),
}

impl Display for ParserError {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      ParserError::LexError(err) => write!(f, "{}", err),
      ParserError::Internal => { write!(f, "Internal parser error") }
      ParserError::Eof => { write!(f, "End of file") }
      ParserError::UnexpectedToken(token) => {
        write!(
          f,
          "Unexpected character '{}' at line {}, column {}",
          token.value,
          token.line,
          token.column
        )
      }
    }
  }
}