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
31
use crate::*;

#[derive(Debug, Clone)]
pub struct Token<A: Clone + PartialEq>(A);

impl<A: Clone + PartialEq> Token<A> {
    pub(crate) fn new(a: A) -> Self {
        Self(a)
    }
}

impl<A: Clone + PartialEq> Scanner for Token<A> {
    type Input = A;
    type Output = A;

    fn scan(&self, stream: &mut Stream<Self::Input>) -> Res<Self> {
        let res = stream
            .peek()
            .ok_or_else(|| Error::new(stream.pos(), None, Expected::Token(self.0.clone())))?;
        if res == self.0 {
            stream.next();
            Ok(res)
        } else {
            Err(Error::new(
                stream.pos(),
                Some(res),
                Expected::Token(self.0.clone()),
            ))
        }
    }
}