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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use core::marker::PhantomData;
use parsers::basic::*;
use parsers::execution::*;
use parsers::monadic::*;
use parsers::parser::*;
use parsers::response::*;

// -------------------------------------------------------------------------------------------------
// Parser type definition
// -------------------------------------------------------------------------------------------------

pub struct Or<E, R, A>(pub E, pub R, PhantomData<A>) where E: Parser<A>, R: Parser<A>;

impl<E, R, A> Parser<A> for Or<E, R, A> where E: Parser<A>, R: Parser<A> {}

pub trait OrOperation<E, R, A> where E: Parser<A>, R: Parser<A> {
    fn or(self, R) -> Or<E, R, A>;
}

impl<E, R, A> OrOperation<E, R, A> for E where E: Parser<A>, R: Parser<A> {
    fn or(self, a: R) -> Or<E, R, A> {
        Or(self, a, PhantomData)
    }
}

// -------------------------------------------------------------------------------------------------

pub struct And<E, A, R, B>(pub E, pub R, PhantomData<A>, PhantomData<B>) where E: Parser<A>, R: Parser<B>;

impl<E, A, R, B> Parser<(A, B)> for And<E, A, R, B> where E: Parser<A>, R: Parser<B> {}

pub trait AndOperation<E, A, R, B> where E: Parser<A>, R: Parser<B> {
    fn then(self, R) -> And<E, A, R, B>;
    fn then_left(self, b: R) -> FMap<And<E, A, R, B>, (A, B), A>;
    fn then_right(self, b: R) -> FMap<And<E, A, R, B>, (A, B), B>;
}

impl<E, A, R, B> AndOperation<E, A, R, B> for E where E: Parser<A>, R: Parser<B> {
    fn then(self, b: R) -> And<E, A, R, B> {
        And(self, b, PhantomData, PhantomData)
    }
    fn then_left(self, b: R) -> FMap<And<E, A, R, B>, (A, B), A> {
        And(self, b, PhantomData, PhantomData).fmap(Box::new(|(a, _)| a))
    }
    fn then_right(self, b: R) -> FMap<And<E, A, R, B>, (A, B), B> {
        And(self, b, PhantomData, PhantomData).fmap(Box::new(|(_, b)| b))
    }
}

// -------------------------------------------------------------------------------------------------

pub struct Opt<E, A>(E, PhantomData<A>) where E: Parser<A>;

impl<E, A> Parser<Option<A>> for Opt<E, A> where E: Parser<A> {}

pub fn opt<E, A>(p: E) -> Opt<E, A> where E: Parser<A> {
    Opt(p, PhantomData)
}

//  -------------------------------------------------------------------------------------------------

pub struct Repeat<E, A>(bool, E, PhantomData<A>) where E: Parser<A>;

impl<E, A> Parser<Vec<A>> for Repeat<E, A> where E: Parser<A> {}

pub fn optrep<E, A>(p: E) -> Repeat<E, A> where E: Parser<A> {
    Repeat(true, p, PhantomData)
}

pub fn rep<E, A>(p: E) -> Repeat<E, A> where E: Parser<A> {
    Repeat(false, p, PhantomData)
}

//  -------------------------------------------------------------------------------------------------

pub trait RepeatOperation<E, A> where E: Parser<A> {
    fn opt(self) -> Opt<E, A>;
    fn rep(self) -> Repeat<E, A>;
    fn optrep(self) -> Repeat<E, A>;
}

impl<E, A> RepeatOperation<E, A> for E where E: Parser<A> {
    fn opt(self) -> Opt<E, A> {
        opt(self)
    }

    fn rep(self) -> Repeat<E, A> {
        rep(self)
    }

    fn optrep(self) -> Repeat<E, A> {
        optrep(self)
    }
}

//  -------------------------------------------------------------------------------------------------

pub type TypeWhile = Repeat<Satisfy<Try<Any, u8>, u8>, u8>;

pub fn take_while(f: Box<(Fn(&u8) -> bool)>) -> TypeWhile {
    optrep(do_try(any()).satisfy(f))
}

pub type TakeOne = Satisfy<Try<Any, u8>, u8>;

pub fn take_one(f: Box<(Fn(&u8) -> bool)>) -> TakeOne {
    do_try(any()).satisfy(f)
}

// -------------------------------------------------------------------------------------------------
// Parser execution
// -------------------------------------------------------------------------------------------------

impl<E, R, A> Executable<A> for Or<E, R, A>
    where E: Executable<A> + Parser<A>,
          R: Executable<A> + Parser<A>
{
    fn execute(&self, s: &[u8], o: usize) -> Response<A> {
        let Or(p1, p2, _) = self;

        match p1.execute(s, o) {
            Response::Success(a1, i1, b1) => Response::Success(a1, i1, b1),
            Response::Reject(_, b1) => {
                match p2.execute(s, o) {
                    Response::Success(a2, i2, b2) => Response::Success(a2, i2, b1 || b2),
                    Response::Reject(i2, b2) => Response::Reject(i2, b1 || b2)
                }
            }
        }
    }
}

// -------------------------------------------------------------------------------------------------

impl<E, A, R, B> Executable<(A, B)> for And<E, A, R, B>
    where E: Executable<A> + Parser<A>,
          R: Executable<B> + Parser<B>
{
    fn execute(&self, s: &[u8], o: usize) -> Response<(A, B)> {
        let And(p1, p2, _, _) = self;

        match p1.execute(s, o) {
            Response::Success(a1, i1, b1) => {
                match p2.execute(s, i1) {
                    Response::Success(a2, i2, b2) => Response::Success((a1, a2), i2, b1 || b2),
                    Response::Reject(i2, b2) => Response::Reject(i2, b1 || b2),
                }
            }
            Response::Reject(i1, b1) => Response::Reject(i1, b1)
        }
    }
}

// -------------------------------------------------------------------------------------------------

impl<E, A> Executable<Option<A>> for Opt<E, A>
    where E: Executable<A> + Parser<A>
{
    fn execute(&self, s: &[u8], o: usize) -> Response<Option<A>> {
        let Opt(p, _) = self;

        match p.execute(s, o) {
            Response::Success(a, o, b) => Response::Success(Some(a), o, b),
            Response::Reject(_, false) => Response::Success(None, o, false),
            Response::Reject(o, true) => Response::Reject(o, true)
        }
    }
}

// -------------------------------------------------------------------------------------------------

impl<E, A> Executable<Vec<A>> for Repeat<E, A>
    where E: Executable<A> + Parser<A>
{
    fn execute(&self, s: &[u8], o: usize) -> Response<Vec<A>> {
        let Repeat(opt, p, _) = self;

        let mut result: Vec<A> = Vec::with_capacity(13);
        let mut offset = o;
        let mut consumed = false;

        loop {
            match p.execute(s, offset) {
                Response::Success(a1, i1, b1) => {
                    result.push(a1);
                    offset = i1;
                    consumed = consumed || b1;
                }
                _ => {
                    if *opt || result.len() > 0 {
                        return Response::Success(result, offset, consumed);
                    }

                    return Response::Reject(offset, consumed);
                }
            }
        }
    }
}

// -------------------------------------------------------------------------------------------------