nom_whitespace/
whitespace.rs

1#[doc(hidden)]
2#[macro_export]
3macro_rules! nom_ws_error_position (
4  ($($args:tt)*) => (error_position!($($args)*));
5);
6
7#[doc(hidden)]
8#[macro_export]
9macro_rules! nom_ws_error_node_position (
10  ($($args:tt)*) => (error_node_position!($($args)*));
11);
12
13#[doc(hidden)]
14#[macro_export]
15macro_rules! nom_call (
16  ($($args:tt)*) => (call!($($args)*));
17);
18
19#[doc(hidden)]
20#[macro_export]
21macro_rules! nom_tuple (
22  ($($args:tt)*) => (tuple!($($args)*));
23);
24
25#[doc(hidden)]
26#[macro_export]
27macro_rules! nom_permutation_init (
28  ($($args:tt)*) => (permutation_init!($($args)*));
29);
30
31#[doc(hidden)]
32#[macro_export]
33macro_rules! nom_permutation_unwrap (
34  ($($args:tt)*) => (permutation_unwrap!($($args)*));
35);
36
37#[doc(hidden)]
38#[macro_export(local_inner_macros)]
39macro_rules! succ (
40  (0, $submac:ident ! ($($rest:tt)*)) => ($submac!(1, $($rest)*));
41  (1, $submac:ident ! ($($rest:tt)*)) => ($submac!(2, $($rest)*));
42  (2, $submac:ident ! ($($rest:tt)*)) => ($submac!(3, $($rest)*));
43  (3, $submac:ident ! ($($rest:tt)*)) => ($submac!(4, $($rest)*));
44  (4, $submac:ident ! ($($rest:tt)*)) => ($submac!(5, $($rest)*));
45  (5, $submac:ident ! ($($rest:tt)*)) => ($submac!(6, $($rest)*));
46  (6, $submac:ident ! ($($rest:tt)*)) => ($submac!(7, $($rest)*));
47  (7, $submac:ident ! ($($rest:tt)*)) => ($submac!(8, $($rest)*));
48  (8, $submac:ident ! ($($rest:tt)*)) => ($submac!(9, $($rest)*));
49  (9, $submac:ident ! ($($rest:tt)*)) => ($submac!(10, $($rest)*));
50  (10, $submac:ident ! ($($rest:tt)*)) => ($submac!(11, $($rest)*));
51  (11, $submac:ident ! ($($rest:tt)*)) => ($submac!(12, $($rest)*));
52  (12, $submac:ident ! ($($rest:tt)*)) => ($submac!(13, $($rest)*));
53  (13, $submac:ident ! ($($rest:tt)*)) => ($submac!(14, $($rest)*));
54  (14, $submac:ident ! ($($rest:tt)*)) => ($submac!(15, $($rest)*));
55  (15, $submac:ident ! ($($rest:tt)*)) => ($submac!(16, $($rest)*));
56  (16, $submac:ident ! ($($rest:tt)*)) => ($submac!(17, $($rest)*));
57  (17, $submac:ident ! ($($rest:tt)*)) => ($submac!(18, $($rest)*));
58  (18, $submac:ident ! ($($rest:tt)*)) => ($submac!(19, $($rest)*));
59  (19, $submac:ident ! ($($rest:tt)*)) => ($submac!(20, $($rest)*));
60);
61
62// HACK: for some reason, Rust 1.11 does not accept $res.$it in
63// nom_permutation_unwrap. This is a bit ugly, but it will have no
64// impact on the generated code
65#[doc(hidden)]
66#[macro_export(local_inner_macros)]
67macro_rules! acc (
68  (0, $tup:expr) => ($tup.0);
69  (1, $tup:expr) => ($tup.1);
70  (2, $tup:expr) => ($tup.2);
71  (3, $tup:expr) => ($tup.3);
72  (4, $tup:expr) => ($tup.4);
73  (5, $tup:expr) => ($tup.5);
74  (6, $tup:expr) => ($tup.6);
75  (7, $tup:expr) => ($tup.7);
76  (8, $tup:expr) => ($tup.8);
77  (9, $tup:expr) => ($tup.9);
78  (10, $tup:expr) => ($tup.10);
79  (11, $tup:expr) => ($tup.11);
80  (12, $tup:expr) => ($tup.12);
81  (13, $tup:expr) => ($tup.13);
82  (14, $tup:expr) => ($tup.14);
83  (15, $tup:expr) => ($tup.15);
84  (16, $tup:expr) => ($tup.16);
85  (17, $tup:expr) => ($tup.17);
86  (18, $tup:expr) => ($tup.18);
87  (19, $tup:expr) => ($tup.19);
88  (20, $tup:expr) => ($tup.20);
89);
90
91#[macro_export(local_inner_macros)]
92macro_rules! wrap_sep (
93  ($i:expr, $separator:expr, $submac:ident!( $($args:tt)* )) => ({
94    use $crate::lib::std::result::Result::*;
95    use $crate::lib::nom::{Err,Convert,IResult};
96
97    fn unify_types<I,O,P,E>(_: &IResult<I,O,E>, _: &IResult<I,P,E>) {}
98
99    let sep_res = ($separator)($i);
100    match sep_res {
101      Ok((i1,_))    => {
102        let res = $submac!(i1, $($args)*);
103        unify_types(&sep_res, &res);
104        res
105      },
106      Err(e) => Err(Err::convert(e)),
107    }
108  });
109  ($i:expr, $separator:expr, $f:expr) => (
110    wrap_sep!($i, $separator, nom_nom_call!($f))
111  );
112);
113
114#[doc(hidden)]
115#[macro_export(local_inner_macros)]
116macro_rules! pair_sep (
117  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
118    nom_tuple!(
119      $i,
120      sep!($separator, $submac!($($args)*)),
121      sep!($separator, $submac2!($($args2)*))
122    )
123  );
124  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => (
125    pair_sep!($i, $separator, $submac!($($args)*), nom_call!($g));
126  );
127  ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => (
128    pair_sep!($i, $separator, nom_call!($f), $submac!($($args)*));
129  );
130  ($i:expr, $separator:path, $f:expr, $g:expr) => (
131    pair_sep!($i, $separator, nom_call!($f), nom_call!($g));
132  );
133);
134
135#[doc(hidden)]
136#[macro_export(local_inner_macros)]
137macro_rules! delimited_sep (
138  ($i:expr, $separator:path, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => ({
139    use $crate::lib::std::result::Result::*;
140
141    match tuple_sep!($i, $separator, (), $submac1!($($args1)*), $($rest)*) {
142      Err(e) => Err(e),
143      Ok((remaining, (_,o,_))) => {
144        Ok((remaining, o))
145      }
146    }
147  });
148  ($i:expr, $separator:path, $f:expr, $($rest:tt)+) => (
149    delimited_sep!($i, $separator, nom_call!($f), $($rest)*);
150  );
151);
152
153#[doc(hidden)]
154#[macro_export(local_inner_macros)]
155macro_rules! separated_pair_sep (
156  ($i:expr, $separator:path, $submac1:ident!( $($args1:tt)* ), $($rest:tt)+) => ({
157    use $crate::lib::std::result::Result::*;
158
159    match tuple_sep!($i, $separator, (), $submac1!($($args1)*), $($rest)*) {
160      Err(e) => Err(e),
161      Ok((remaining, (o1,_,o2))) => {
162        Ok((remaining, (o1,o2)))
163      }
164    }
165  });
166  ($i:expr, $separator:path, $f:expr, $($rest:tt)+) => (
167    separated_pair_sep!($i, $separator, nom_call!($f), $($rest)*);
168  );
169);
170
171#[doc(hidden)]
172#[macro_export(local_inner_macros)]
173macro_rules! preceded_sep (
174  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ({
175    use $crate::lib::std::result::Result::*;
176
177    match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) {
178      Err(e) => Err(e),
179      Ok((remaining, (_,o))) => {
180        Ok((remaining, o))
181      }
182    }
183  });
184  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => (
185    preceded_sep!($i, $separator, $submac!($($args)*), nom_call!($g));
186  );
187  ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => (
188    preceded_sep!($i, $separator, nom_call!($f), $submac!($($args)*));
189  );
190  ($i:expr, $separator:path, $f:expr, $g:expr) => (
191    preceded_sep!($i, $separator, nom_call!($f), nom_call!($g));
192  );
193);
194
195#[doc(hidden)]
196#[macro_export(local_inner_macros)]
197macro_rules! terminated_sep (
198  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => ({
199    use $crate::lib::std::result::Result::*;
200
201    match pair_sep!($i, $separator, $submac!($($args)*), $submac2!($($args2)*)) {
202      Err(e) => Err(e),
203      Ok((remaining, (o,_))) => {
204        Ok((remaining, o))
205      }
206    }
207  });
208  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => (
209    terminated_sep!($i, $separator, $submac!($($args)*), nom_call!($g));
210  );
211  ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => (
212    terminated_sep!($i, $separator, nom_call!($f), $submac!($($args)*));
213  );
214  ($i:expr, $separator:path, $f:expr, $g:expr) => (
215    terminated_sep!($i, $separator, nom_call!($f), nom_call!($g));
216  );
217);
218
219/// Internal parser, do not use directly
220#[doc(hidden)]
221#[macro_export(local_inner_macros)]
222macro_rules! tuple_sep (
223  ($i:expr, $separator:path, ($($parsed:tt),*), $e:path, $($rest:tt)*) => (
224    tuple_sep!($i, $separator, ($($parsed),*), nom_call!($e), $($rest)*);
225  );
226  ($i:expr, $separator:path, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => (
227    {
228      use $crate::lib::std::result::Result::*;
229
230      match sep!($i, $separator, $submac!($($args)*)) {
231        Err(e) => Err(e),
232        Ok((i,o))     => {
233          tuple_sep!(i, $separator, (o), $($rest)*)
234        }
235      }
236    }
237  );
238  ($i:expr, $separator:path, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => (
239    {
240      use $crate::lib::std::result::Result::*;
241
242      match sep!($i, $separator, $submac!($($args)*)) {
243        Err(e) => Err(e),
244        Ok((i,o))     => {
245          tuple_sep!(i, $separator, ($($parsed)* , o), $($rest)*)
246        }
247      }
248    }
249  );
250  ($i:expr, $separator:path, ($($parsed:tt),*), $e:path) => (
251    tuple_sep!($i, $separator, ($($parsed),*), nom_call!($e));
252  );
253  ($i:expr, $separator:path, (), $submac:ident!( $($args:tt)* )) => (
254    {
255      use $crate::lib::std::result::Result::*;
256
257      match sep!($i, $separator, $submac!($($args)*)) {
258        Err(e) => Err(e),
259        Ok((i,o))     => {
260          Ok((i, (o)))
261        }
262      }
263    }
264  );
265  ($i:expr, $separator:path, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => (
266    {
267      use $crate::lib::std::result::Result::*;
268
269      match sep!($i, $separator, $submac!($($args)*)) {
270        Err(e) => Err(e),
271        Ok((i,o))     => {
272          Ok((i, ($($parsed),* , o)))
273        }
274      }
275    }
276  );
277  ($i:expr, $separator:path, ($($parsed:expr),*)) => (
278    {
279      ::sts::result::Result::Ok(($i, ($($parsed),*)))
280    }
281  );
282);
283
284#[doc(hidden)]
285#[macro_export(local_inner_macros)]
286macro_rules! do_parse_sep (
287  (__impl $i:expr, $separator:path, ( $($rest:expr),* )) => (
288    $crate::lib::std::result::Result::Ok(($i, ( $($rest),* )))
289  );
290
291  (__impl $i:expr, $separator:path, $e:ident >> $($rest:tt)*) => (
292    do_parse_sep!(__impl $i, $separator, nom_call!($e) >> $($rest)*);
293  );
294  (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => (
295    {
296      use $crate::lib::std::result::Result::*;
297
298      match sep!($i, $separator, $submac!($($args)*)) {
299        Err(e) => Err(e),
300        Ok((i,_))     => {
301          do_parse_sep!(__impl i, $separator, $($rest)*)
302        },
303      }
304    }
305  );
306
307  (__impl $i:expr, $separator:path, $field:ident : $e:ident >> $($rest:tt)*) => (
308    do_parse_sep!(__impl $i, $separator, $field: nom_call!($e) >> $($rest)*);
309  );
310
311  (__impl $i:expr, $separator:path, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => (
312    {
313      use $crate::lib::std::result::Result::*;
314
315      match sep!($i, $separator, $submac!($($args)*)) {
316        Err(e) => Err(e),
317        Ok((i,o))     => {
318          let $field = o;
319          do_parse_sep!(__impl i, $separator, $($rest)*)
320        },
321      }
322    }
323  );
324
325  // ending the chain
326  (__impl $i:expr, $separator:path, $e:ident >> ( $($rest:tt)* )) => (
327    do_parse_sep!(__impl $i, $separator, nom_call!($e) >> ( $($rest)* ));
328  );
329
330  (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({
331    use $crate::lib::std::result::Result::*;
332
333    match sep!($i, $separator, $submac!($($args)*)) {
334      Err(e) => Err(e),
335      Ok((i,_))     => {
336        Ok((i, ( $($rest)* )))
337      },
338    }
339  });
340
341  (__impl $i:expr, $separator:path, $field:ident : $e:ident >> ( $($rest:tt)* )) => (
342    do_parse_sep!(__impl $i, $separator, $field: nom_call!($e) >> ( $($rest)* ) );
343  );
344
345  (__impl $i:expr, $separator:path, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ({
346    use $crate::lib::std::result::Result::*;
347
348    match sep!($i, $separator, $submac!($($args)*)) {
349      Err(e) => Err(e),
350      Ok((i,o))     => {
351        let $field = o;
352        Ok((i, ( $($rest)* )))
353      },
354    }
355  });
356
357  ($i:expr, $separator:path, $($rest:tt)*) => (
358    {
359      do_parse_sep!(__impl $i, $separator, $($rest)*)
360    }
361  );
362);
363
364#[doc(hidden)]
365#[macro_export(local_inner_macros)]
366macro_rules! permutation_sep (
367  ($i:expr, $separator:path, $($rest:tt)*) => (
368    {
369      use $crate::lib::std::result::Result::*;
370      use $crate::lib::std::option::Option::*;
371      use $crate::lib::nom::{Err,ErrorKind,Convert};
372
373      let mut res    = nom_permutation_init!((), $($rest)*);
374      let mut input  = $i;
375      let mut error  = None;
376      let mut needed = None;
377
378      loop {
379        let mut all_done = true;
380        permutation_iterator_sep!(0, input, $separator, all_done, needed, res, $($rest)*);
381
382        //if we reach that part, it means none of the parsers were able to read anything
383        if !all_done {
384          //FIXME: should wrap the error returned by the child parser
385          error = Option::Some(nom_ws_error_position!(input, ErrorKind::Permutation));
386        }
387        break;
388      }
389
390      if let Some(need) = needed {
391        Err(Err::convert(need))
392      } else {
393        if let Some(unwrapped_res) = { nom_permutation_unwrap!(0, (), res, $($rest)*) } {
394          Ok((input, unwrapped_res))
395        } else {
396          if let Some(e) = error {
397            Err(Err::Error(nom_ws_error_node_position!($i, ErrorKind::Permutation, e)))
398          } else {
399            Err(Err::Error(nom_ws_error_position!($i, ErrorKind::Permutation)))
400          }
401        }
402      }
403    }
404  );
405);
406
407#[doc(hidden)]
408#[macro_export(local_inner_macros)]
409macro_rules! permutation_iterator_sep (
410  ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident?, $($rest:tt)*) => (
411    permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, nom_call!($e), $($rest)*);
412  );
413  ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident, $($rest:tt)*) => (
414    permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, nom_call!($e), $($rest)*);
415  );
416
417  ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?, $($rest:tt)*) => ({
418    permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, $submac!($($args)*), $($rest)*);
419  });
420  ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)*) => ({
421    use $crate::lib::std::result::Result::*;
422    use $crate::lib::nom::Err;
423
424    if acc!($it, $res) == $crate::lib::std::option::Option::None {
425      match {sep!($i, $separator, $submac!($($args)*))} {
426        Ok((i,o))     => {
427          $i = i;
428          acc!($it, $res) = $crate::lib::std::option::Option::Some(o);
429          continue;
430        },
431        Err(Err::Error(_)) => {
432          $all_done = false;
433        },
434        Err(e) => {
435          $needed = $crate::lib::std::option::Option::Some(e);
436          break;
437        }
438      };
439    }
440    succ!($it, permutation_iterator_sep!($i, $separator, $all_done, $needed, $res, $($rest)*));
441  });
442
443  ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident?) => (
444    permutation_iterator_sep!($it, $i, $separator, $all_done, $res, nom_call!($e));
445  );
446  ($it:tt,$i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $e:ident) => (
447    permutation_iterator_sep!($it, $i, $separator, $all_done, $res, nom_call!($e));
448  );
449
450  ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )?) => ({
451    permutation_iterator_sep!($it, $i, $separator, $all_done, $needed, $res, $submac!($($args)*));
452  });
453  ($it:tt, $i:expr, $separator:path, $all_done:expr, $needed:expr, $res:expr, $submac:ident!( $($args:tt)* )) => ({
454    use $crate::lib::std::result::Result::*;
455    use $crate::lib::nom::Err;
456
457    if acc!($it, $res) == $crate::lib::std::option::Option::None {
458      match sep!($i, $separator, $submac!($($args)*)) {
459        Ok((i,o))     => {
460          $i = i;
461          acc!($it, $res) = $crate::lib::std::option::Option::Some(o);
462          continue;
463        },
464        Err(Err::Error(_)) => {
465          $all_done = false;
466        },
467        Err(e) => {
468          $needed = $crate::lib::std::option::Option::Some(e);
469          break;
470        }
471      };
472    }
473  });
474);
475
476#[doc(hidden)]
477#[macro_export(local_inner_macros)]
478macro_rules! alt_sep (
479  (__impl $i:expr, $separator:path, $e:path | $($rest:tt)*) => (
480    alt_sep!(__impl $i, $separator, nom_call!($e) | $($rest)*);
481  );
482
483  (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => (
484    {
485      use $crate::lib::std::result::Result::*;
486      use $crate::lib::nom::Err;
487
488      let res = sep!($i, $separator, $subrule!($($args)*));
489      match res {
490        Ok((_,_))          => res,
491        Err(Err::Error(_)) => alt_sep!(__impl $i, $separator, $($rest)*),
492        Err(e)            => Err(e),
493      }
494    }
495  );
496
497  (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => (
498    {
499      use $crate::lib::std::result::Result::*;
500      use $crate::lib::nom::Err;
501
502      match sep!($i, $separator, $subrule!( $($args)* )) {
503        Ok((i,o))               => Ok((i,$gen(o))),
504        Err(Err::Error(_))      => {
505          alt_sep!(__impl $i, $separator, $($rest)*)
506        },
507        Err(e)            => Err(e),
508      }
509    }
510  );
511
512  (__impl $i:expr, $separator:path, $e:path => { $gen:expr } | $($rest:tt)*) => (
513    alt_sep!(__impl $i, $separator, nom_call!($e) => { $gen } | $($rest)*);
514  );
515
516  (__impl $i:expr, $separator:path, $e:path => { $gen:expr }) => (
517    alt_sep!(__impl $i, $separator, nom_call!($e) => { $gen });
518  );
519
520  (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => (
521    {
522      use $crate::lib::std::result::Result::*;
523      use $crate::lib::nom::Err;
524
525      match sep!($i, $separator, $subrule!( $($args)* )) {
526        Ok((i,o))     => Ok((i,$gen(o))),
527        Err(Err::Error(e))      => {
528          fn unify_types<T>(_: &T, _: &T) {}
529          let e2 = nom_ws_error_position!($i, $crate::lib::nom::ErrorKind::Alt);
530          unify_types(&e, &e2);
531          Err(Err::Error(e2))
532        },
533        Err(e)            => Err(e),
534      }
535    }
536  );
537
538  (__impl $i:expr, $separator:path, $e:path) => (
539    alt_sep!(__impl $i, $separator, nom_call!($e));
540  );
541
542  (__impl $i:expr, $separator:path, $subrule:ident!( $($args:tt)*)) => (
543    {
544      use $crate::lib::std::result::Result::*;
545      use $crate::lib::nom::Err;
546
547      match sep!($i, $separator, $subrule!( $($args)* )) {
548        Ok((i,o))     => Ok((i,o)),
549        Err(Err::Error(e))      => {
550          fn unify_types<T>(_: &T, _: &T) {}
551          let e2 = nom_ws_error_position!($i, $crate::lib::nom::ErrorKind::Alt);
552          unify_types(&e, &e2);
553          Err(Err::Error(e2))
554        },
555        Err(e)            => Err(e),
556      }
557    }
558  );
559
560  (__impl $i:expr) => ({
561    use $crate::lib::std::result::Result::*;
562    use $crate::lib::nom::{Err,Needed,IResult};
563
564    Err(Err::Error(nom_ws_error_position!($i, $crate::lib::nom::ErrorKind::Alt)))
565  });
566
567  (__impl $i:expr, $separator:path) => ({
568    use $crate::lib::std::result::Result::*;
569    use $crate::lib::nom::{Err,Needed,IResult};
570
571    Err(Err::Error(nom_ws_error_position!($i, $crate::lib::nom::ErrorKind::Alt)))
572  });
573
574  ($i:expr, $separator:path, $($rest:tt)*) => (
575    {
576      alt_sep!(__impl $i, $separator, $($rest)*)
577    }
578  );
579);
580
581#[doc(hidden)]
582#[macro_export(local_inner_macros)]
583macro_rules! alt_complete_sep (
584  ($i:expr, $separator:path, $e:path | $($rest:tt)*) => (
585    alt_complete_sep!($i, $separator, complete!(nom_call!($e)) | $($rest)*);
586  );
587
588  ($i:expr, $separator:path, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => (
589    {
590      use $crate::lib::std::result::Result::*;
591
592      let res = complete!($i, sep!($separator, $subrule!($($args)*)));
593      match res {
594        Ok((_,_)) => res,
595        _ => alt_complete_sep!($i, $separator, $($rest)*),
596      }
597    }
598  );
599
600  ($i:expr, $separator:path, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => (
601    {
602      use $crate::lib::std::result::Result::*;
603      use $crate::lib::nom::{Err,Needed,IResult};
604
605      match complete!($i, sep!($separator, $subrule!($($args)*))) {
606        Ok((i,o)) => Ok((i,$gen(o))),
607        _ => alt_complete_sep!($i, $separator, $($rest)*),
608      }
609    }
610  );
611
612  ($i:expr, $separator:path, $e:path => { $gen:expr } | $($rest:tt)*) => (
613    alt_complete_sep!($i, $separator, complete!(nom_call!($e)) => { $gen } | $($rest)*);
614  );
615
616  // Tail (non-recursive) rules
617
618  ($i:expr, $separator:path, $e:path => { $gen:expr }) => (
619    alt_complete_sep!($i, $separator, nom_call!($e) => { $gen });
620  );
621
622  ($i:expr, $separator:path, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => (
623    alt_sep!(__impl $i, $separator, complete!($subrule!($($args)*)) => { $gen })
624  );
625
626  ($i:expr, $separator:path, $e:path) => (
627    alt_complete_sep!($i, $separator, nom_call!($e));
628  );
629
630  ($i:expr, $separator:path, $subrule:ident!( $($args:tt)*)) => (
631    alt_sep!(__impl $i, $separator, complete!($subrule!($($args)*)))
632  );
633);
634
635#[doc(hidden)]
636#[macro_export(local_inner_macros)]
637macro_rules! switch_sep (
638  (__impl $i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => (
639    {
640      use $crate::lib::std::result::Result::*;
641      use $crate::lib::nom::Err;
642
643      match sep!($i, $separator, $submac!($($args)*)) {
644        Err(Err::Error(e))      => Err(Err::Error(nom_ws_error_node_position!(
645            $i, $crate::lib::nom::ErrorKind::Switch, e
646        ))),
647        Err(Err::Failure(e))    => Err(Err::Failure(
648            nom_ws_error_node_position!($i, $crate::lib::nom::ErrorKind::Switch, e))),
649        Err(e) => Err(e),
650        Ok((i, o))    => {
651          match o {
652            $($p => match sep!(i, $separator, $subrule!($($args2)*)) {
653              Err(Err::Error(e)) => Err(Err::Error(nom_ws_error_node_position!(
654                  $i, $crate::lib::nom::ErrorKind::Switch, e
655              ))),
656              Err(Err::Failure(e))    => Err(Err::Failure(
657                  nom_ws_error_node_position!($i, $crate::lib::nom::ErrorKind::Switch, e))),
658              a => a,
659            }),*,
660            _    => Err(Err::Error(nom_ws_error_position!($i, $crate::lib::nom::ErrorKind::Switch)))
661          }
662        }
663      }
664    }
665  );
666  ($i:expr, $separator:path, $submac:ident!( $($args:tt)*), $($rest:tt)*) => (
667    {
668      switch_sep!(__impl $i, $separator, $submac!($($args)*), $($rest)*)
669    }
670  );
671  ($i:expr, $separator:path, $e:path, $($rest:tt)*) => (
672    {
673      switch_sep!(__impl $i, $separator, nom_call!($e), $($rest)*)
674    }
675  );
676);
677
678#[doc(hidden)]
679#[cfg(feature = "alloc")]
680#[macro_export(local_inner_macros)]
681macro_rules! separated_list_sep (
682  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => (
683    separated_list!(
684      $i,
685      sep!($separator, $submac!($($args)*)),
686      sep!($separator, $submac2!($($args2)*))
687    )
688  );
689  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* ), $g:expr) => (
690    separated_list_sep!($i, $separator, $submac!($($args)*), nom_call!($g));
691  );
692  ($i:expr, $separator:path, $f:expr, $submac:ident!( $($args:tt)* )) => (
693    separated_list_sep!($i, $separator, nom_call!($f), $submac!($($args)*));
694  );
695  ($i:expr, $separator:path, $f:expr, $g:expr) => (
696    separated_list_sep!($i, $separator, nom_call!($f), nom_call!($g));
697  );
698);
699
700/// helper macros to build a separator parser
701///
702/// ```
703/// # #[macro_use] extern crate nom;
704/// named!(pub space, eat_separator!(&b" \t"[..]));
705/// # fn main() {}
706/// ```
707#[macro_export(local_inner_macros)]
708macro_rules! eat_separator (
709  ($i:expr, $arr:expr) => (
710    {
711      use $crate::lib::nom::{FindToken, InputTakeAtPosition};
712      let input = $i;
713      input.split_at_position(|c| !$arr.find_token(c))
714    }
715  );
716);
717
718/// sep is the parser rewriting macro for whitespace separated formats
719///
720/// it takes as argument a space eating function and a parser tree,
721/// and will intersperse the space parser everywhere
722///
723/// ```ignore
724/// #[macro_export(local_inner_macros)]
725/// macro_rules! ws (
726///   ($i:expr, $($args:tt)*) => (
727///     {
728///       use sp;
729///       sep!($i, sp, $($args)*)
730///     }
731///   )
732/// );
733/// ```
734#[macro_export(local_inner_macros)]
735macro_rules! sep (
736  ($i:expr,  $separator:path, tuple ! ($($rest:tt)*) ) => {
737    tuple_sep!($i, $separator, (), $($rest)*)
738  };
739  ($i:expr,  $separator:path, pair ! ($($rest:tt)*) ) => {
740    wrap_sep!($i,
741      $separator,
742      pair_sep!($separator, $($rest)*)
743    )
744  };
745  ($i:expr,  $separator:path, delimited ! ($($rest:tt)*) ) => {
746    wrap_sep!($i,
747      $separator,
748      delimited_sep!($separator, $($rest)*)
749    )
750  };
751  ($i:expr,  $separator:path, separated_pair ! ($($rest:tt)*) ) => {
752    wrap_sep!($i,
753      $separator,
754      separated_pair_sep!($separator, $($rest)*)
755    )
756  };
757  ($i:expr,  $separator:path, preceded ! ($($rest:tt)*) ) => {
758    wrap_sep!($i,
759      $separator,
760      preceded_sep!($separator, $($rest)*)
761    )
762  };
763  ($i:expr,  $separator:path, terminated ! ($($rest:tt)*) ) => {
764    wrap_sep!($i,
765      $separator,
766      terminated_sep!($separator, $($rest)*)
767    )
768  };
769  ($i:expr,  $separator:path, do_parse ! ($($rest:tt)*) ) => {
770    wrap_sep!($i,
771      $separator,
772      do_parse_sep!($separator, $($rest)*)
773    )
774  };
775  ($i:expr,  $separator:path, permutation ! ($($rest:tt)*) ) => {
776    wrap_sep!($i,
777      $separator,
778      permutation_sep!($separator, $($rest)*)
779    )
780  };
781  ($i:expr,  $separator:path, alt ! ($($rest:tt)*) ) => {
782    wrap_sep!($i,
783      $separator,
784      alt_sep!($separator, $($rest)*)
785    )
786  };
787  ($i:expr,  $separator:path, alt_complete ! ($($rest:tt)*) ) => {
788    wrap_sep!($i,
789      $separator,
790      alt_complete_sep!($separator, $($rest)*)
791    )
792  };
793  ($i:expr,  $separator:path, switch ! ($($rest:tt)*) ) => {
794    wrap_sep!($i,
795      $separator,
796      switch_sep!($separator, $($rest)*)
797    )
798  };
799  ($i:expr,  $separator:path, separated_list ! ($($rest:tt)*) ) => {
800    wrap_sep!($i,
801      $separator,
802      separated_list_sep!($separator, $($rest)*)
803    )
804  };
805  ($i:expr,  $separator:path, many0 ! ($($rest:tt)*) ) => {
806    many0!($i, wrap_sep!($separator, $($rest)*))
807  };
808  ($i:expr,  $separator:path, many1 ! ($($rest:tt)*) ) => {
809    many1!($i, wrap_sep!($separator, $($rest)*))
810  };
811  ($i:expr, $separator:path, return_error!( $($args:tt)* )) => {
812    return_error!($i, wrap_sep!($separator, $($args)*))
813  };
814//FIXME: missing separated_nonempty_list,
815// many_till, many_m_n, count, count_fixed, fold_many0, fold_many1,
816// fold_many_m_n
817  ($i:expr, $separator:path, $submac:ident!( $($args:tt)* )) => {
818    wrap_sep!($i, $separator, $submac!($($args)*))
819  };
820  ($i:expr, $separator:path, $f:expr) => {
821    wrap_sep!($i, $separator, nom_call!($f))
822  };
823);
824
825use lib::nom::IResult;
826use lib::nom::{AsChar, FindToken, InputTakeAtPosition};
827#[allow(unused_imports)]
828pub fn sp<'a, T>(input: T) -> IResult<T, T>
829where
830  T: InputTakeAtPosition,
831  <T as InputTakeAtPosition>::Item: AsChar + Clone,
832  &'a str: FindToken<<T as InputTakeAtPosition>::Item>,
833{
834  input.split_at_position(|item| {
835    let c = item.clone().as_char();
836    !(c == ' ' || c == '\t' || c == '\r' || c == '\n')
837  })
838  //this could be written as followed, but not using FindToken is faster
839  //eat_separator!(input, " \t\r\n")
840}
841
842/// `ws!(I -> IResult<I,O>) => I -> IResult<I, O>`
843///
844/// transforms a parser to automatically consume
845/// whitespace between each token. By default,
846/// it takes the following characters: `" \t\r\n"`.
847///
848/// If you need a whitespace parser consuming a
849/// different set of characters, you can make
850/// your own by reusing the `sep!` combinator.
851///
852/// To use `ws!`, pass your parser as argument:
853///
854/// ```
855/// # #[macro_use] extern crate nom;
856/// # fn main() {
857/// named!(tuple<&[u8], (&[u8], &[u8]) >,
858///   ws!(tuple!( take!(3), tag!("de") ))
859/// );
860///
861/// assert_eq!(
862///   tuple(&b" \t abc de fg"[..]),
863///  Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..])))
864/// );
865/// # }
866/// ```
867///
868#[macro_export(local_inner_macros)]
869macro_rules! ws (
870  ($i:expr, $($args:tt)*) => (
871    {
872      use $crate::sp;
873      use $crate::lib::nom::Convert;
874      use $crate::lib::nom::Err;
875      use $crate::lib::std::result::Result::*;
876
877      match sep!($i, sp, $($args)*) {
878        Err(e) => Err(e),
879        Ok((i1,o))    => {
880          match (sp)(i1) {
881            Err(e) => Err(Err::convert(e)),
882            Ok((i2,_))    => Ok((i2, o))
883          }
884        }
885      }
886    }
887  )
888);
889
890#[cfg(test)]
891#[allow(dead_code)]
892mod tests {
893  #[cfg(feature = "alloc")]
894  use lib::std::string::{String, ToString};
895  use nom::{Err, IResult, Needed};
896  use super::sp;
897  use nom::ErrorKind;
898  use nom::types::CompleteStr;
899
900  #[test]
901  fn spaaaaace() {
902    assert_eq!(sp(&b" \t abc "[..]), Ok((&b"abc "[..], &b" \t "[..])));
903  }
904
905  #[test]
906  fn tag() {
907    named!(abc, ws!(tag!("abc")));
908
909    assert_eq!(abc(&b" \t abc def"[..]), Ok((&b"def"[..], &b"abc"[..])));
910  }
911
912  #[test]
913  fn pair() {
914    named!(pair_2<&[u8], (&[u8], &[u8]) >,
915      ws!(pair!( take!(3), tag!("de") ))
916    );
917
918    assert_eq!(
919      pair_2(&b" \t abc de fg"[..]),
920      Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..])))
921    );
922  }
923
924  #[test]
925  fn preceded() {
926    named!(prec<&[u8], &[u8] >,
927      ws!(preceded!( take!(3), tag!("de") ))
928    );
929
930    assert_eq!(prec(&b" \t abc de fg"[..]), Ok((&b"fg"[..], &b"de"[..])));
931  }
932
933  #[test]
934  fn terminated() {
935    named!(term<&[u8], &[u8] >,
936      ws!(terminated!( take!(3), tag!("de") ))
937    );
938
939    assert_eq!(term(&b" \t abc de fg"[..]), Ok((&b"fg"[..], &b"abc"[..])));
940  }
941
942  #[test]
943  fn tuple() {
944    //trace_macros!(true);
945    named!(tuple_2<&[u8], (&[u8], &[u8]) >,
946      ws!(tuple!( take!(3), tag!("de") ))
947    );
948    //trace_macros!(false);
949
950    assert_eq!(
951      tuple_2(&b" \t abc de fg"[..]),
952      Ok((&b"fg"[..], (&b"abc"[..], &b"de"[..])))
953    );
954  }
955
956  #[test]
957  fn levels() {
958    //trace_macros!(true);
959    named!(level_2<&[u8], (&[u8], (&[u8], &[u8])) >,
960      ws!(pair!(take!(3), tuple!( tag!("de"), tag!("fg ") )))
961    );
962    //trace_macros!(false);
963
964    assert_eq!(
965      level_2(&b" \t abc de fg \t hi "[..]),
966      Ok((&b"hi "[..], (&b"abc"[..], (&b"de"[..], &b"fg "[..]))))
967    );
968  }
969
970  #[test]
971  fn do_parse() {
972    fn ret_int1(i: &[u8]) -> IResult<&[u8], u8> {
973      Ok((i, 1))
974    };
975    fn ret_int2(i: &[u8]) -> IResult<&[u8], u8> {
976      Ok((i, 2))
977    };
978
979    //trace_macros!(true);
980    named!(do_parser<&[u8], (u8, u8)>,
981      ws!(do_parse!(
982        tag!("abcd")       >>
983        opt!(tag!("abcd")) >>
984        aa: ret_int1       >>
985        tag!("efgh")       >>
986        bb: ret_int2       >>
987        tag!("efgh")       >>
988        (aa, bb)
989      ))
990    );
991
992    //trace_macros!(false);
993
994    assert_eq!(
995      do_parser(&b"abcd abcd\tefghefghX"[..]),
996      Ok((&b"X"[..], (1, 2)))
997    );
998    assert_eq!(
999      do_parser(&b"abcd\tefgh      efgh X"[..]),
1000      Ok((&b"X"[..], (1, 2)))
1001    );
1002    assert_eq!(
1003      do_parser(&b"abcd  ab"[..]),
1004      Err(Err::Incomplete(Needed::Size(4)))
1005    );
1006    assert_eq!(
1007      do_parser(&b" abcd\tefgh\tef"[..]),
1008      Err(Err::Incomplete(Needed::Size(4)))
1009    );
1010  }
1011
1012  #[test]
1013  fn permutation() {
1014    //trace_macros!(true);
1015    named!(
1016      perm<(&[u8], &[u8], &[u8])>,
1017      ws!(permutation!(tag!("abcd"), tag!("efg"), tag!("hi")))
1018    );
1019    //trace_macros!(false);
1020
1021    let expected = (&b"abcd"[..], &b"efg"[..], &b"hi"[..]);
1022
1023    let a = &b"abcd\tefg \thijk"[..];
1024    assert_eq!(perm(a), Ok((&b"jk"[..], expected)));
1025    let b = &b"  efg  \tabcdhi jk"[..];
1026    assert_eq!(perm(b), Ok((&b"jk"[..], expected)));
1027    let c = &b" hi   efg\tabcdjk"[..];
1028    assert_eq!(perm(c), Ok((&b"jk"[..], expected)));
1029
1030    let d = &b"efg  xyzabcdefghi"[..];
1031    assert_eq!(
1032      perm(d),
1033      Err(Err::Error(nom_ws_error_node_position!(
1034        &b"efg  xyzabcdefghi"[..],
1035        ErrorKind::Permutation,
1036        nom_ws_error_position!(&b"  xyzabcdefghi"[..], ErrorKind::Permutation)
1037      )))
1038    );
1039
1040    let e = &b" efg \tabc"[..];
1041    assert_eq!(perm(e), Err(Err::Incomplete(Needed::Size(4))));
1042  }
1043
1044  #[cfg(feature = "alloc")]
1045  #[derive(Debug, Clone, PartialEq)]
1046  pub struct ErrorStr(String);
1047
1048  #[cfg(feature = "alloc")]
1049  impl From<u32> for ErrorStr {
1050    fn from(i: u32) -> Self {
1051      ErrorStr(format!("custom error code: {}", i))
1052    }
1053  }
1054
1055  #[cfg(feature = "alloc")]
1056  impl<'a> From<&'a str> for ErrorStr {
1057    fn from(i: &'a str) -> Self {
1058      ErrorStr(format!("custom error message: {}", i))
1059    }
1060  }
1061
1062  #[cfg(feature = "alloc")]
1063  #[test]
1064  fn alt() {
1065    fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
1066      Ok((&b""[..], input))
1067    }
1068
1069    #[allow(unused_variables)]
1070    fn dont_work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
1071      use Context;
1072      Err(Err::Error(Context::Code(
1073        &b""[..],
1074        ErrorKind::Custom(ErrorStr("abcd".to_string())),
1075      )))
1076    }
1077
1078    fn work2(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
1079      Ok((input, &b""[..]))
1080    }
1081
1082    fn alt1(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
1083      alt!(i, dont_work | dont_work)
1084    }
1085    fn alt2(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
1086      alt!(i, dont_work | work)
1087    }
1088    fn alt3(i: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
1089      alt!(i, dont_work | dont_work | work2 | dont_work)
1090    }
1091
1092    let a = &b"\tabcd"[..];
1093    assert_eq!(
1094      alt1(a),
1095      Err(Err::Error(nom_ws_error_position!(a, ErrorKind::Alt::<ErrorStr>)))
1096    );
1097    assert_eq!(alt2(a), Ok((&b""[..], a)));
1098    assert_eq!(alt3(a), Ok((a, &b""[..])));
1099
1100    named!(alt4<CompleteStr, CompleteStr>, ws!(alt!(tag!("abcd") | tag!("efgh"))));
1101    assert_eq!(
1102      alt4(CompleteStr("\tabcd")),
1103      Ok((CompleteStr(""), CompleteStr(r"abcd")))
1104    );
1105    assert_eq!(
1106      alt4(CompleteStr("  efgh ")),
1107      Ok((CompleteStr(""), CompleteStr("efgh")))
1108    );
1109
1110    // test the alternative syntax
1111    named!(alt5<CompleteStr, bool>, ws!(alt!(tag!("abcd") => { |_| false } | tag!("efgh") => { |_| true })));
1112    assert_eq!(alt5(CompleteStr("\tabcd")), Ok((CompleteStr(""), false)));
1113    assert_eq!(alt5(CompleteStr("  efgh ")), Ok((CompleteStr(""), true)));
1114  }
1115
1116  /*FIXME: alt_complete works, but ws will return Incomplete on end of input
1117  #[test]
1118  fn alt_complete() {
1119    named!(ac<&[u8], &[u8]>,
1120      ws!(alt_complete!(tag!("abcd") | tag!("ef") | tag!("ghi") | tag!("kl")))
1121    );
1122
1123    let a = &b""[..];
1124    assert_eq!(ac(a), Err(Err::Error(nom_ws_error_position!(a, ErrorKind::Alt))));
1125    let a = &b" \tef "[..];
1126    assert_eq!(ac(a),Ok((&b""[..], &b"ef"[..])));
1127    let a = &b" cde"[..];
1128    assert_eq!(ac(a), Err(Err::Error(nom_ws_error_position!(&a[1..], ErrorKind::Alt))));
1129  }
1130  */
1131
1132  #[allow(unused_variables)]
1133  #[test]
1134  fn switch() {
1135    named!(sw<CompleteStr,CompleteStr>,
1136      ws!(switch!(take!(4),
1137        CompleteStr("abcd") => take!(2) |
1138        CompleteStr("efgh") => take!(4)
1139      ))
1140    );
1141
1142    let a = CompleteStr(" abcd ef gh");
1143    assert_eq!(sw(a), Ok((CompleteStr("gh"), CompleteStr("ef"))));
1144
1145    let b = CompleteStr("\tefgh ijkl ");
1146    assert_eq!(sw(b), Ok((CompleteStr(""), CompleteStr("ijkl"))));
1147    let c = CompleteStr("afghijkl");
1148    assert_eq!(
1149      sw(c),
1150      Err(Err::Error(nom_ws_error_position!(
1151        CompleteStr("afghijkl"),
1152        ErrorKind::Switch
1153      )))
1154    );
1155  }
1156
1157  named!(str_parse(&str) -> &str, ws!(tag!("test")));
1158  #[allow(unused_variables)]
1159  #[test]
1160  fn str_test() {
1161    assert_eq!(str_parse(" \n   test\t a\nb"), Ok(("a\nb", "test")));
1162  }
1163
1164  // test whitespace parser generation for alt
1165  named!(space, tag!(" "));
1166  #[cfg(feature = "alloc")]
1167  named!(pipeline_statement<&[u8], ()>,
1168    ws!(
1169      do_parse!(
1170      tag!("pipeline") >>
1171      attributes: delimited!(char!('{'),
1172                             separated_list!(char!(','), alt!(
1173                               space |
1174                               space
1175                             )),
1176                             char!('}')) >>
1177
1178      ({
1179        let _ = attributes;
1180        ()
1181      })
1182    )
1183  )
1184  );
1185
1186  #[cfg(feature = "alloc")]
1187  named!(
1188    fail<&[u8]>,
1189    map!(many_till!(take!(1), ws!(tag!("."))), |(r, _)| r[0])
1190  );
1191}