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
use {Parser, Result};

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Error<'a> {
    Char(char),
    Str(&'a str),
    Satisfy,
    TakeWhile1,
    Any,
    End,
}

impl<'a> Parser<&'a str> for char {
    type Output = char;
    type Error = Error<'static>;

    fn parse(&mut self, input: &'a str, from: usize) -> Result<Self::Output, Self::Error> {
        if input[from..].starts_with(*self) {
            Ok((from + self.len_utf8(), *self))
        } else {
            Err((from, Error::Char(*self)))
        }
    }
}

impl<'a, 'tmp> Parser<&'a str> for &'tmp str {
    type Output = &'a str;
    type Error = Error<'tmp>;

    fn parse(&mut self, input: &'a str, from: usize) -> Result<Self::Output, Self::Error> {
        if input[from..].starts_with(*self) {
            let to = from + self.len();
            Ok((to, &input[from..to]))
        } else {
            Err((from, Error::Str(*self)))
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Satisfy<F>(pub F) where F: FnMut(char) -> bool;

impl<'a, F> Parser<&'a str> for Satisfy<F>
    where F: FnMut(char) -> bool
{
    type Output = char;
    type Error = Error<'static>;

    fn parse(&mut self, input: &'a str, from: usize) -> Result<Self::Output, Self::Error> {
        if let Some(char) = input[from..].chars().next() {
            if self.0(char) {
                return Ok((from + char.len_utf8(), char));
            }
        }
        Err((from, Error::Satisfy))
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct TakeWhile<F>(pub F) where F: FnMut(char) -> bool;

impl<'a, F> Parser<&'a str> for TakeWhile<F>
    where F: FnMut(char) -> bool
{
    type Output = &'a str;
    type Error = Error<'static>;

    fn parse(&mut self, input: &'a str, from: usize) -> Result<Self::Output, Self::Error> {
        let mut chars = input[from..].chars();
        match chars.by_ref().skip_while(|&char| self.0(char)).next() {
            Some(char) => {
                let to = input.len() - chars.as_str().len() - char.len_utf8();
                Ok((to, &input[from..to]))
            }
            None => Ok((input.len(), &input[from..])),
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct TakeWhile1<F>(pub F) where F: FnMut(char) -> bool;

impl<'a, F> Parser<&'a str> for TakeWhile1<F>
    where F: FnMut(char) -> bool
{
    type Output = &'a str;
    type Error = Error<'static>;

    fn parse(&mut self, input: &'a str, from: usize) -> Result<Self::Output, Self::Error> {
        match TakeWhile(&mut self.0).parse(input, from) {
            Ok((_, "")) => Err((from, Error::TakeWhile1)),
            otherwise => otherwise,
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Capture<P>(pub P);

impl<'a, P> Parser<&'a str> for Capture<P>
    where P: Parser<&'a str>
{
    type Output = &'a str;
    type Error = P::Error;

    fn parse(&mut self, input: &'a str, from: usize) -> Result<Self::Output, Self::Error> {
        let (to, _) = self.0.parse(input, from)?;
        Ok((to, &input[from..to]))
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Any;

impl<'a> Parser<&'a str> for Any {
    type Output = char;
    type Error = Error<'static>;

    fn parse(&mut self, input: &'a str, from: usize) -> Result<Self::Output, Self::Error> {
        if let Some(char) = input[from..].chars().next() {
            Ok((from + char.len_utf8(), char))
        } else {
            Err((from, Error::Any))
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct End;

impl<'a> Parser<&'a str> for End {
    type Output = ();
    type Error = Error<'static>;

    fn parse(&mut self, input: &'a str, from: usize) -> Result<Self::Output, Self::Error> {
        if input.len() == from {
            Ok((from, ()))
        } else {
            Err((from, Error::End))
        }
    }
}