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
32
33
34
use crate::*;

#[derive(Debug, Clone)]
pub struct Or<A, B>(A, B)
where
    A: Scanner,
    B: Scanner<Input = A::Input, Output = A::Output>;

impl<A, B> Or<A, B>
where
    A: Scanner,
    B: Scanner<Input = A::Input, Output = A::Output>,
{
    pub(crate) fn new(a: A, b: B) -> Self {
        Self(a, b)
    }
}

impl<A, B> Scanner for Or<A, B>
where
    A: Scanner,
    B: Scanner<Input = A::Input, Output = A::Output>,
{
    type Input = A::Input;
    type Output = B::Output;

    fn scan(&self, stream: &mut Stream<Self::Input>) -> Res<Self> {
        let pos = stream.pos();
        match self.0.scan(stream) {
            Err(..) if pos == stream.pos() => self.1.scan(stream),
            other => other,
        }
    }
}