blob: 90a249998f8af8a0a488b7bc30f34a8174cc7a98 [file] [log] [blame]
David Tolnay86eca752016-09-04 11:26:41 -07001#![cfg(feature = "parsing")]
2
David Tolnayb5a7b142016-09-13 22:46:39 -07003use nom::IResult;
David Tolnayb79ee962016-09-04 09:39:20 -07004
5macro_rules! punct {
6 ($i:expr, $punct:expr) => {
David Tolnay13e5da42016-09-04 16:18:34 -07007 $crate::helper::punct($i, $punct)
David Tolnayb79ee962016-09-04 09:39:20 -07008 };
9}
10
David Tolnay13e5da42016-09-04 16:18:34 -070011pub fn punct<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> {
12 let mut chars = input.char_indices();
13 while let Some((i, ch)) = chars.next() {
14 if !ch.is_whitespace() {
15 return if input[i..].starts_with(token) {
16 let end = i + token.len();
17 IResult::Done(&input[end..], &input[i..end])
18 } else {
David Tolnayb5a7b142016-09-13 22:46:39 -070019 IResult::Error
David Tolnay13e5da42016-09-04 16:18:34 -070020 };
21 }
22 }
David Tolnayb5a7b142016-09-13 22:46:39 -070023 IResult::Error
David Tolnay13e5da42016-09-04 16:18:34 -070024}
25
David Tolnayb5a7b142016-09-13 22:46:39 -070026macro_rules! option {
27 ($i:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayf6ccb832016-09-04 15:00:56 -070028 match $submac!($i, $($args)*) {
29 ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, Some(o)),
David Tolnayb5a7b142016-09-13 22:46:39 -070030 ::nom::IResult::Error => ::nom::IResult::Done($i, None),
David Tolnayf6ccb832016-09-04 15:00:56 -070031 }
David Tolnayb5a7b142016-09-13 22:46:39 -070032 };
David Tolnayf6ccb832016-09-04 15:00:56 -070033
David Tolnayb5a7b142016-09-13 22:46:39 -070034 ($i:expr, $f:expr) => {
35 option!($i, call!($f));
36 };
37}
38
39macro_rules! opt_vec {
40 ($i:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb79ee962016-09-04 09:39:20 -070041 match $submac!($i, $($args)*) {
42 ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, o),
David Tolnayb5a7b142016-09-13 22:46:39 -070043 ::nom::IResult::Error => ::nom::IResult::Done($i, Vec::new()),
David Tolnayb79ee962016-09-04 09:39:20 -070044 }
David Tolnayb5a7b142016-09-13 22:46:39 -070045 };
46}
David Tolnayb79ee962016-09-04 09:39:20 -070047
48macro_rules! epsilon {
49 ($i:expr,) => {
50 call!($i, {
51 fn epsilon<T>(input: T) -> ::nom::IResult<T, ()> {
52 ::nom::IResult::Done(input, ())
53 }
54 epsilon
55 })
56 };
57}