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
pub(crate) use std::marker::PhantomData;
pub mod combinators;
pub mod prelude;
pub mod str;

pub trait Parser<Iter>
where
    Self: Sized,
    Iter: Iterator + Clone,
{
    type Output;

    /// Tries to parse the provided Iterator. Returns the
    /// unparsed rest of the data and a value on success.
    fn parse(&self, i: Iter) -> Option<(Iter, Self::Output)>;

    /// Runs the Parser repeatedly until it fails. The results are stored in a `Vec`.
    fn many(self) -> combinators::Many<Iter, Self> {
        combinators::Many::new(self)
    }

    /// Make the Parser optional, so that it will succeed even if it fails.
    fn maybe(self) -> combinators::Maybe<Iter, Self> {
        combinators::Maybe::new(self)
    }

    /// Require that either this or the provided Parser succeed.
    /// If both succeed, the one that has consumed the most bytes will be chosen.
    fn or<Other: Parser<Iter>>(self, other: Other) -> combinators::Or<Iter, Self, Other> {
        combinators::Or::new(self, other)
    }

    /// Chain two Parsers together, requiring both to succeed.
    fn then<Other: Parser<Iter>>(self, other: Other) -> combinators::Then<Iter, Self, Other> {
        combinators::Then::new(self, other)
    }

    /// Apply the provided function to the Output of this Parser.
    fn transform<F: Fn(Self::Output) -> Option<T>, T>(
        self,
        f: F,
    ) -> combinators::Transform<Iter, Self, F, T> {
        combinators::Transform::new(self, f)
    }
}

/// Matches for a character.
impl<Iter> Parser<Iter> for char
where
    Iter: Iterator<Item = char> + Clone,
{
    type Output = char;
    fn parse(&self, mut i: Iter) -> Option<(Iter, Self::Output)> {
        if i.next()? == *self {
            Some((i, *self))
        } else {
            None
        }
    }
}

/// Matches for a string.
impl<'a, Iter> Parser<Iter> for &'a str
where
    Iter: Iterator<Item = char> + Clone,
{
    type Output = &'a str;
    fn parse(&self, mut i: Iter) -> Option<(Iter, Self::Output)> {
        let mut x = self.chars();
        while let Some(c) = x.next() {
            if i.clone().next()? == c {
                i.next()?;
            } else {
                return None;
            }
        }
        Some((i, self))
    }
}

impl<Iter, F, T> Parser<Iter> for F
where
    Iter: Iterator + Clone,
    F: Fn(Iter) -> Option<(Iter, T)>,
{
    type Output = T;
    fn parse(&self, i: Iter) -> Option<(Iter, Self::Output)> {
        (self)(i)
    }
}

/// `()` ensures that there is no remaining data.
impl<Iter> Parser<Iter> for ()
where
    Iter: Iterator + Clone,
{
    type Output = ();
    fn parse(&self, i: Iter) -> Option<(Iter, ())> {
        if i.clone().next().is_none() {
            Some((i, ()))
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;

    #[test]
    fn parse_str_positive() {
        assert_eq!(
            "abc".transform(|x| Some(x)).parse_str("abcdef").unwrap(),
            ("def", "abc")
        )
    }

    #[test]
    fn parse_str_negative() {
        assert_eq!("abcdef".transform(|x| Some(x)).parse_str("abc"), None)
    }

    #[test]
    fn parse_char_positive() {
        assert_eq!('👱'.parse_str("👱abc"), Some(("abc", '👱')))
    }

    #[test]
    fn parse_char_negative() {
        assert_eq!('👱'.parse_str("a"), None)
    }

    #[test]
    fn parse_void_positive() {
        assert_eq!(().parse_str(""), Some(("", ())))
    }

    #[test]
    fn parse_void_negative() {
        assert_eq!(().parse_str(" "), None)
    }
}