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
use {Result, Input};
use parsers::*;
#[macro_export]
macro_rules! any {
($input:expr, $case:expr, $($rest:expr),*) => (
match parse!($input, $case) {
$crate::Result::Ok(output) => {
$crate::Result::Ok(output)
}
$crate::Result::Err(_) => {
any!($input, $($rest),*)
}
}
);
($input:expr, $case:expr) => (
parse!($input, $case)
)
}
#[macro_export]
macro_rules! maybe {
($input:expr, $value:expr) => ({
match parse!($input, $value) {
$crate::Result::Ok(val) => {
$crate::Result::Ok(Some(val))
}
$crate::Result::Err(_) => {
$crate::Result::Ok(None)
}
}
})
}
#[macro_export]
macro_rules! collect {
($input:expr, $value:expr, $cond:expr) => ({
let mut values = Vec::new();
#[warn(unused_assignments)]
loop {
if let $crate::Result::Ok(_) = eof($input) {
break;
}
match parse!($input, $value) {
$crate::Result::Ok(value) => values.push(value),
$crate::Result::Err(_) => break
}
if let $crate::Result::Err(_) = parse!($input, $cond) {
break;
}
}
$crate::Result::Ok(values)
});
($input:expr, $value:expr) => ({
let mut values = Vec::new();
#[warn(unused_assignments)]
loop {
if let $crate::Result::Ok(_) = eof($input) {
break;
}
match parse!($input, $value) {
$crate::Result::Ok(value) => values.push(value),
$crate::Result::Err(_) => break
}
}
$crate::Result::Ok(values)
});
}
#[inline]
pub fn many<I: Input, O, F>(input: &mut I, f: F) -> Result<O, I>
where F: Fn(&mut I) -> Result<O, I>
{
loop {
let output = f(input)?;
if let Ok(_) = eof(input) {
return Ok(output);
}
}
}
#[inline(always)]
pub fn surrounded<I: Input, O, F, P>(input: &mut I, mut p: P, f: F) -> Result<O, I>
where F: Copy + FnMut(I::Token) -> bool,
P: FnMut(&mut I) -> Result<O, I>
{
skip_while(input, f)?;
let output = p(input)?;
skip_while(input, f)?;
Ok(output)
}