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
use super::*;
pub type Padding<E> = Repeated<Ignored<Filter<fn(&char) -> bool, E>, char>>;
pub type Padded<P, O> = PaddedBy<PaddingFor<Padding<<P as Parser<char, O>>::Error>, P, Vec<()>, O>, Padding<<P as Parser<char, O>>::Error>, O, Vec<()>>;
pub trait TextParser<O>: Parser<char, O> {
fn padded(self) -> Padded<Self, O> where Self: Sized {
whitespace().padding_for(self).padded_by(whitespace())
}
}
impl<O, P: Parser<char, O>> TextParser<O> for P {}
pub fn whitespace<E: Error<char>>() -> Padding<E> {
filter((|c: &char| c.is_whitespace()) as _).ignored().repeated()
}
pub fn newline<E: Error<char>>() -> impl Parser<char, (), Error = E> {
just('\r').or_not().padding_for(just('\n'))
.or(just('\x0B'))
.or(just('\x0C'))
.or(just('\x0D'))
.or(just('\u{0085}'))
.or(just('\u{2028}'))
.or(just('\u{2029}'))
.ignored()
}
pub fn digits<E: Error<char>>() -> Repeated<Filter<fn(&char) -> bool, E>> {
filter(char::is_ascii_digit as _).repeated_at_least(1)
}
pub fn int<E: Error<char>>() -> impl Parser<char, Vec<char>, Error = E> + Copy + Clone {
filter(|c: &char| c.is_ascii_digit() && *c != '0').map(Some)
.chain(filter(char::is_ascii_digit).repeated())
.or(just('0').map(|c| vec![c]))
}
pub fn ident<E: Error<char>>() -> impl Parser<char, Vec<char>, Error = E> + Copy + Clone {
filter(|c: &char| c.is_ascii_alphabetic() || *c == '_').map(Some)
.chain(filter(|c: &char| c.is_ascii_alphanumeric() || *c == '_').repeated())
}