1#[cfg(test)]
4mod tests;
5
6use crate::error::ErrorKind;
7use crate::error::ParseError;
8use crate::internal::{Err, IResult, Parser};
9
10pub trait Alt<I, O, E> {
14 fn choice(&mut self, input: I) -> IResult<I, O, E>;
16}
17
18pub fn alt<I: Clone, O, E: ParseError<I>, List: Alt<I, O, E>>(
48 mut l: List,
49) -> impl FnMut(I) -> IResult<I, O, E> {
50 move |i: I| l.choice(i)
51}
52
53pub trait Permutation<I, O, E> {
57 fn permutation(&mut self, input: I) -> IResult<I, O, E>;
59}
60
61pub fn permutation<I: Clone, O, E: ParseError<I>, List: Permutation<I, O, E>>(
107 mut l: List,
108) -> impl FnMut(I) -> IResult<I, O, E> {
109 move |i: I| l.permutation(i)
110}
111
112macro_rules! alt_trait(
113 ($first:ident $second:ident $($id: ident)+) => (
114 alt_trait!(__impl $first $second; $($id)+);
115 );
116 (__impl $($current:ident)*; $head:ident $($id: ident)+) => (
117 alt_trait_impl!($($current)*);
118
119 alt_trait!(__impl $($current)* $head; $($id)+);
120 );
121 (__impl $($current:ident)*; $head:ident) => (
122 alt_trait_impl!($($current)*);
123 alt_trait_impl!($($current)* $head);
124 );
125);
126
127macro_rules! alt_trait_impl(
128 ($($id:ident)+) => (
129 impl<
130 Input: Clone, Output, Error: ParseError<Input>,
131 $($id: Parser<Input, Output, Error>),+
132 > Alt<Input, Output, Error> for ( $($id),+ ) {
133
134 fn choice(&mut self, input: Input) -> IResult<Input, Output, Error> {
135 match self.0.parse(input.clone()) {
136 Err(Err::Error(e)) => alt_trait_inner!(1, self, input, e, $($id)+),
137 res => res,
138 }
139 }
140 }
141 );
142);
143
144macro_rules! alt_trait_inner(
145 ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident $($id:ident)+) => (
146 match $self.$it.parse($input.clone()) {
147 Err(Err::Error(e)) => {
148 let err = $err.or(e);
149 succ!($it, alt_trait_inner!($self, $input, err, $($id)+))
150 }
151 res => res,
152 }
153 );
154 ($it:tt, $self:expr, $input:expr, $err:expr, $head:ident) => (
155 Err(Err::Error(Error::append($input, ErrorKind::Alt, $err)))
156 );
157);
158
159alt_trait!(A B C D E F G H I J K L M N O P Q R S T U);
160
161impl<Input, Output, Error: ParseError<Input>, A: Parser<Input, Output, Error>>
163 Alt<Input, Output, Error> for (A,)
164{
165 fn choice(&mut self, input: Input) -> IResult<Input, Output, Error> {
166 self.0.parse(input)
167 }
168}
169
170macro_rules! permutation_trait(
171 (
172 $name1:ident $ty1:ident $item1:ident
173 $name2:ident $ty2:ident $item2:ident
174 $($name3:ident $ty3:ident $item3:ident)*
175 ) => (
176 permutation_trait!(__impl $name1 $ty1 $item1, $name2 $ty2 $item2; $($name3 $ty3 $item3)*);
177 );
178 (
179 __impl $($name:ident $ty:ident $item:ident),+;
180 $name1:ident $ty1:ident $item1:ident $($name2:ident $ty2:ident $item2:ident)*
181 ) => (
182 permutation_trait_impl!($($name $ty $item),+);
183 permutation_trait!(__impl $($name $ty $item),+ , $name1 $ty1 $item1; $($name2 $ty2 $item2)*);
184 );
185 (__impl $($name:ident $ty:ident $item:ident),+;) => (
186 permutation_trait_impl!($($name $ty $item),+);
187 );
188);
189
190macro_rules! permutation_trait_impl(
191 ($($name:ident $ty:ident $item:ident),+) => (
192 impl<
193 Input: Clone, $($ty),+ , Error: ParseError<Input>,
194 $($name: Parser<Input, $ty, Error>),+
195 > Permutation<Input, ( $($ty),+ ), Error> for ( $($name),+ ) {
196
197 fn permutation(&mut self, mut input: Input) -> IResult<Input, ( $($ty),+ ), Error> {
198 let mut res = ($(Option::<$ty>::None),+);
199
200 loop {
201 let mut err: Option<Error> = None;
202 permutation_trait_inner!(0, self, input, res, err, $($name)+);
203
204 if let Some(err) = err {
207 return Err(Err::Error(Error::append(input, ErrorKind::Permutation, err)));
209 }
210
211 match res {
213 ($(Some($item)),+) => return Ok((input, ($($item),+))),
214 _ => unreachable!(),
215 }
216 }
217 }
218 }
219 );
220);
221
222macro_rules! permutation_trait_inner(
223 ($it:tt, $self:expr, $input:ident, $res:expr, $err:expr, $head:ident $($id:ident)*) => (
224 if $res.$it.is_none() {
225 match $self.$it.parse($input.clone()) {
226 Ok((i, o)) => {
227 $input = i;
228 $res.$it = Some(o);
229 continue;
230 }
231 Err(Err::Error(e)) => {
232 $err = Some(match $err {
233 Some(err) => err.or(e),
234 None => e,
235 });
236 }
237 Err(e) => return Err(e),
238 };
239 }
240 succ!($it, permutation_trait_inner!($self, $input, $res, $err, $($id)*));
241 );
242 ($it:tt, $self:expr, $input:ident, $res:expr, $err:expr,) => ();
243);
244
245permutation_trait!(
246 FnA A a
247 FnB B b
248 FnC C c
249 FnD D d
250 FnE E e
251 FnF F f
252 FnG G g
253 FnH H h
254 FnI I i
255 FnJ J j
256 FnK K k
257 FnL L l
258 FnM M m
259 FnN N n
260 FnO O o
261 FnP P p
262 FnQ Q q
263 FnR R r
264 FnS S s
265 FnT T t
266 FnU U u
267);