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
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use crate::*;

/// This trait is the core of the combinatorial sequencing
///
/// The [scan](Scanner::scan) method must be implemented, it takes a
/// [Stream](crate::stream::Stream) and produces a
/// [Result](crate::error::Result) of [Self::Output](Scanner::Output) from the
/// [Self::Input](Scanner::Input)
///
/// [Input](Scanner::Input) is the input type
///
/// [Output](Scanner::Output) is the production type
pub trait Scanner {
    type Input;
    type Output;

    fn scan(&self, stream: &mut Stream<Self::Input>) -> Result<Self::Output, Self::Input>;

    /// Maps a function over the sequence
    fn map<T, F>(self, f: F) -> Map<T, Self, F>
    where
        Self: Sized,
        F: Fn(Self::Output) -> T,
    {
        Map::new(f, self)
    }

    /// Produces an `or` squence with the current and next combinator
    fn or<T>(self, x: T) -> Or<Self, T>
    where
        Self: Sized,
        T: Scanner<Input = Self::Input, Output = Self::Output>,
    {
        Or::new(self, x)
    }

    /// Produces an `and` sequence with the current and next combinator
    fn and<T>(self, x: T) -> And<Self, T>
    where
        Self: Sized,
        T: Scanner<Input = Self::Input>,
    {
        And::new(self, x)
    }

    /// Skips the next element if it matches the type
    fn skip<T>(self, x: T) -> Skip<Self, T>
    where
        Self: Sized,
        T: Scanner<Input = Self::Input>,
    {
        Skip::new(self, x)
    }

    /// Produces a sequence where this element is optional
    fn optional(self) -> Optional<Self>
    where
        Self: Sized,
    {
        Optional::new(self)
    }

    /// When a successful production has happened, run a function producing a new sequence
    fn then<B, F>(self, f: F) -> Then<Self, F, B>
    where
        Self: Sized,
        F: Fn(Self::Output) -> B,
        B: Scanner<Input = Self::Input>,
    {
        Then::new(self, f)
    }

    /// Repeat the previous combinator until an error is produced
    fn many(self) -> Many<Self>
    where
        Self: Sized,
    {
        Many::new(self, None, None)
    }

    /// Repeat the previous combinator *at least once* until an error is produced
    fn many1(self) -> Many<Self>
    where
        Self: Sized,
    {
        Many::new(self, Some(1), None)
    }

    /// Repeat the previous combinator *at least `n`* times until an error is produced
    fn many_n(self, n: usize) -> Many<Self>
    where
        Self: Sized,
    {
        Many::new(self, Some(n), Some(n))
    }

    /// Chains two combinators together
    fn with<T>(self, x: T) -> With<Self, T>
    where
        Self: Sized,
        T: Scanner<Input = Self::Input>,
    {
        With::new(self, x)
    }

    /// Produces a value from this sequence
    fn value<T: Clone>(self, x: T) -> With<Self, Value<T, Self::Input>>
    where
        Self: Sized,
    {
        self.with(crate::value(x))
    }

    /// Produces an error message at this point in the sequence
    fn message(self, msg: Expected<Self::Input>) -> Message<Self>
    where
        Self: Sized,
        Self::Input: Clone,
    {
        Message::new(self, msg)
    }

    /// Try to do the combinator, otherwise rollback to the last successful one
    fn attempt(self) -> Attempt<Self>
    where
        Self: Sized,
    {
        Attempt::new(self)
    }
}

impl<A: Scanner> Scanner for Box<A> {
    type Input = A::Input;
    type Output = A::Output;

    fn scan(&self, stream: &mut Stream<Self::Input>) -> Res<Self> {
        (**self).scan(stream)
    }
}

impl<A: Scanner> Scanner for &A {
    type Input = A::Input;
    type Output = A::Output;

    fn scan(&self, stream: &mut Stream<Self::Input>) -> Res<Self> {
        (**self).scan(stream)
    }
}

impl<A: Scanner> Scanner for &mut A {
    type Input = A::Input;
    type Output = A::Output;

    fn scan(&self, stream: &mut Stream<Self::Input>) -> Res<Self> {
        (**self).scan(stream)
    }
}