lexer_rs/lexer/
simple_parse_error.rs

1//a Imports
2use crate::{LexerError, UserPosn};
3
4//a SimpleParseError
5//tp SimpleParseError
6/// A simple implementation of a type supporting LexerError
7///
8/// An error in parsing a token
9///
10/// P : UserPosn
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct SimpleParseError<P>
13where
14    P: UserPosn,
15{
16    /// The character which could not be matched to a token
17    pub ch: char,
18
19    /// The position of the character in the stream
20    pub pos: P,
21}
22
23//ip Error for SimpleParseError
24impl<P> std::error::Error for SimpleParseError<P> where P: UserPosn {}
25
26//ip LexerError for SimpleParseError
27impl<P> LexerError<P> for SimpleParseError<P>
28where
29    P: UserPosn,
30{
31    fn failed_to_parse(pos: P, ch: char) -> Self {
32        Self { ch, pos }
33    }
34}
35
36//ip Display for SimpleParseError
37impl<P> std::fmt::Display for SimpleParseError<P>
38where
39    P: UserPosn,
40{
41    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
42        write!(fmt, "Failed to parse: unexpected char '{}' at ", self.ch)?;
43        self.pos.error_fmt(fmt)
44    }
45}