blob: 6c99dd4707858af4ad19bc2addd27a5d9a29231f [file] [log] [blame]
David Tolnay14cbdeb2016-10-01 12:13:59 -07001use nom::IResult;
2use space::{whitespace, word_break};
David Tolnayb79ee962016-09-04 09:39:20 -07003
4macro_rules! punct {
5 ($i:expr, $punct:expr) => {
David Tolnay13e5da42016-09-04 16:18:34 -07006 $crate::helper::punct($i, $punct)
David Tolnayb79ee962016-09-04 09:39:20 -07007 };
8}
9
David Tolnay13e5da42016-09-04 16:18:34 -070010pub fn punct<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> {
David Tolnay14cbdeb2016-10-01 12:13:59 -070011 let input = match whitespace(input) {
12 IResult::Done(rest, _) => rest,
13 IResult::Error => input,
14 };
15 if input.starts_with(token) {
16 IResult::Done(&input[token.len()..], token)
17 } else {
18 IResult::Error
David Tolnay13e5da42016-09-04 16:18:34 -070019 }
David Tolnay13e5da42016-09-04 16:18:34 -070020}
21
David Tolnay10413f02016-09-30 09:12:02 -070022macro_rules! keyword {
23 ($i:expr, $keyword:expr) => {
24 $crate::helper::keyword($i, $keyword)
25 };
26}
27
28pub fn keyword<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> {
29 match punct(input, token) {
30 IResult::Done(rest, _) => {
31 match word_break(rest) {
32 IResult::Done(_, _) => IResult::Done(rest, token),
33 IResult::Error => IResult::Error,
34 }
35 }
36 IResult::Error => IResult::Error,
37 }
38}
39
David Tolnayb5a7b142016-09-13 22:46:39 -070040macro_rules! option {
41 ($i:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayf6ccb832016-09-04 15:00:56 -070042 match $submac!($i, $($args)*) {
David Tolnayfa0edf22016-09-23 22:58:24 -070043 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, Some(o)),
44 $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, None),
David Tolnayf6ccb832016-09-04 15:00:56 -070045 }
David Tolnayb5a7b142016-09-13 22:46:39 -070046 };
David Tolnayf6ccb832016-09-04 15:00:56 -070047
David Tolnayb5a7b142016-09-13 22:46:39 -070048 ($i:expr, $f:expr) => {
49 option!($i, call!($f));
50 };
51}
52
53macro_rules! opt_vec {
54 ($i:expr, $submac:ident!( $($args:tt)* )) => {
David Tolnayb79ee962016-09-04 09:39:20 -070055 match $submac!($i, $($args)*) {
David Tolnayfa0edf22016-09-23 22:58:24 -070056 $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, o),
57 $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, Vec::new()),
David Tolnayb79ee962016-09-04 09:39:20 -070058 }
David Tolnayb5a7b142016-09-13 22:46:39 -070059 };
60}
David Tolnayb79ee962016-09-04 09:39:20 -070061
62macro_rules! epsilon {
63 ($i:expr,) => {
David Tolnayfa0edf22016-09-23 22:58:24 -070064 $crate::nom::IResult::Done($i, ())
65 };
66}
67
68macro_rules! tap {
69 ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => {
70 match $submac!($i, $($args)*) {
71 $crate::nom::IResult::Done(i, o) => {
72 let $name = o;
73 $e;
74 $crate::nom::IResult::Done(i, ())
75 }
76 $crate::nom::IResult::Error => $crate::nom::IResult::Error,
77 }
78 };
79
80 ($i:expr, $name:ident : $f:expr => $e:expr) => {
81 tap!($i, $name: call!($f) => $e);
David Tolnayb79ee962016-09-04 09:39:20 -070082 };
83}