lexerus/error.rs
1use std::fmt::Debug;
2
3use super::*;
4
5#[derive(Debug)]
6/// Information about the error which occured when the
7/// [Lexer::lex] failed.
8pub struct Error<'code> {
9 pub buffer: Buffer<'code>,
10 /// This is included not just for debug information,
11 /// but also to allow [lexerus_derive::Lexer]
12 /// to obtain some basic information about how many
13 /// tokens were matched before the program
14 /// failed. This is useful when debugging `enum`
15 /// because it would not be immediately obvious
16 /// which match pattern failed.
17 pub matched: usize,
18 pub kind: Kind,
19}
20
21impl<'code> ::std::fmt::Display for Error<'code> {
22 fn fmt(
23 &self,
24 f: &mut std::fmt::Formatter<'_>,
25 ) -> std::fmt::Result {
26 ::std::fmt::Debug::fmt(&self, f)
27 }
28}
29
30#[cfg(test)]
31impl<'code> ::std::error::Error for Error<'code> {}
32
33#[derive(Debug, PartialEq, Eq)]
34/// Describes the type of error found.
35pub enum Kind {
36 /// This means that the token was not found
37 NotFound(&'static str),
38 /// This means that an unexpected token was encountered.
39 UnexpectedToken(&'static str),
40}