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
use core::marker::PhantomData;
use parsers::execution::*;
use parsers::parser::*;
use parsers::response::*;
pub struct Return<E>(pub E);
impl<E> Parser<E> for Return<E> {}
pub fn returns<A>(v: A) -> Return<A> {
Return(v)
}
pub struct Fail();
impl<E> Parser<E> for Fail {}
pub fn fail() -> Fail {
return Fail();
}
pub struct Any();
impl Parser<u8> for Any {}
pub fn any() -> Any {
return Any();
}
pub struct Eos();
impl Parser<()> for Eos {}
pub fn eos() -> Eos {
return Eos();
}
pub struct Try<E, A>(pub E, pub PhantomData<A>) where E: Parser<A>;
impl<E, A> Parser<A> for Try<E, A> where E: Parser<A> {}
pub fn do_try<E, A>(p: E) -> Try<E, A> where E: Parser<A> {
Try(p, PhantomData)
}
pub struct Lookahead<E, A>(pub E, pub PhantomData<A>) where E: Parser<A>;
impl<E, A> Parser<A> for Lookahead<E, A> where E: Parser<A> {}
pub fn lookahead<E, A>(p: E) -> Lookahead<E, A> where E: Parser<A> {
Lookahead(p, PhantomData)
}
pub struct Lazy<E, A>(pub Box<Fn() -> E>, pub PhantomData<A>) where E: Parser<A>;
impl<E, A> Parser<A> for Lazy<E, A> where E: Parser<A> {}
pub fn lazy<E, A>(p: Box<Fn() -> E>) -> Lazy<E, A> where E: Parser<A> {
Lazy(p, PhantomData)
}
pub struct Satisfy<E, A>(pub E, pub Box<Fn(&A) -> bool>) where E: Parser<A>;
impl<E, A> Parser<A> for Satisfy<E, A> where E: Parser<A> {}
pub fn satisfy<E, A>(p: E, f: Box<Fn(&A) -> bool>) -> Satisfy<E, A> where E: Parser<A> {
Satisfy(p, f)
}
pub trait SatisfyOperation<E, A> where E: Parser<A> {
fn satisfy(self, f: Box<Fn(&A) -> bool>) -> Satisfy<E, A>;
}
impl<E, A> SatisfyOperation<E, A> for E where E: Parser<A> {
fn satisfy(self, f: Box<(Fn(&A) -> bool)>) -> Satisfy<E, A> {
satisfy(self, f)
}
}
impl<A> Executable<A> for Return<A> where A: Copy {
fn execute(&self, _: &[u8], o: usize) -> Response<A> {
let Return(v) = self;
Response::Success(v.clone(), o, false)
}
}
impl<A> Executable<A> for Fail {
fn execute(&self, _: &[u8], o: usize) -> Response<A> {
Response::Reject(o, false)
}
}
impl Executable<u8> for Any {
fn execute(&self, s: &[u8], o: usize) -> Response<u8> {
if o < s.len() {
return Response::Success(s[o], o + 1, true)
}
return Response::Reject(o, false);
}
}
impl Executable<()> for Eos {
fn execute(&self, s: &[u8], o: usize) -> Response<()> {
if o < s.len() {
return Response::Reject(o, false);
}
Response::Success((), o, false)
}
}
impl<A, E> Executable<A> for Try<E, A> where E: Executable<A> + Parser<A> {
fn execute(&self, s: &[u8], o: usize) -> Response<A> {
let Try(p, _) = self;
match p.execute(s, o) {
Response::Reject(o, _) => Response::Reject(o, false),
other => other
}
}
}
impl<A, E> Executable<A> for Lookahead<E, A> where E: Executable<A> + Parser<A> {
fn execute(&self, s: &[u8], o: usize) -> Response<A> {
let Lookahead(p, _) = self;
match p.execute(s, o) {
Response::Success(v, _, b) => Response::Success(v, o, b),
other => other
}
}
}
impl<A, E> Executable<A> for Satisfy<E, A> where E: Executable<A> + Parser<A> {
fn execute(&self, s: &[u8], o: usize) -> Response<A> {
let Satisfy(p, c) = self;
match p.execute(s, o) {
Response::Success(a, i, b) => {
if (c)(&a) {
Response::Success(a, i, b)
} else {
Response::Reject(i, b)
}
}
r => r,
}
}
}
impl<A, E> Executable<A> for Lazy<E, A> where E: Executable<A> + Parser<A> {
fn execute(&self, s: &[u8], o: usize) -> Response<A> {
let Lazy(p, _) = self;
p().execute(s, o)
}
}