Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 1 | use IResult; |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 2 | use space::{skip_whitespace, word_break}; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 4 | /// Parse a piece of punctuation like "+" or "+=". |
| 5 | /// |
| 6 | /// See also `keyword!` for parsing keywords, which are subtly different from |
| 7 | /// punctuation. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 8 | /// |
| 9 | /// - **Syntax:** `punct!("...")` |
| 10 | /// - **Output:** `&str` |
| 11 | /// |
| 12 | /// ```rust |
| 13 | /// extern crate syn; |
| 14 | /// #[macro_use] extern crate synom; |
| 15 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 16 | /// // Parse zero or more bangs. |
| 17 | /// named!(many_bangs -> Vec<&str>, |
| 18 | /// many0!(punct!("!")) |
| 19 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 20 | /// |
| 21 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 22 | /// let input = "!! !"; |
| 23 | /// let parsed = many_bangs(input).expect("bangs"); |
| 24 | /// assert_eq!(parsed, ["!", "!", "!"]); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 25 | /// } |
| 26 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 27 | #[macro_export] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 28 | macro_rules! punct { |
| 29 | ($i:expr, $punct:expr) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 30 | $crate::helper::punct($i, $punct) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 31 | }; |
| 32 | } |
| 33 | |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 34 | // Not public API. |
| 35 | #[doc(hidden)] |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 36 | pub fn punct<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 37 | let input = skip_whitespace(input); |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 38 | if input.starts_with(token) { |
| 39 | IResult::Done(&input[token.len()..], token) |
| 40 | } else { |
| 41 | IResult::Error |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 42 | } |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 43 | } |
| 44 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 45 | /// Parse a keyword like "fn" or "struct". |
| 46 | /// |
| 47 | /// See also `punct!` for parsing punctuation, which are subtly different from |
| 48 | /// keywords. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 49 | /// |
| 50 | /// - **Syntax:** `keyword!("...")` |
| 51 | /// - **Output:** `&str` |
| 52 | /// |
| 53 | /// ```rust |
| 54 | /// extern crate syn; |
| 55 | /// #[macro_use] extern crate synom; |
| 56 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 57 | /// use synom::IResult; |
| 58 | /// |
| 59 | /// // Parse zero or more "bang" keywords. |
| 60 | /// named!(many_bangs -> Vec<&str>, |
| 61 | /// terminated!( |
| 62 | /// many0!(keyword!("bang")), |
| 63 | /// punct!(";") |
| 64 | /// ) |
| 65 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 66 | /// |
| 67 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 68 | /// let input = "bang bang bang;"; |
| 69 | /// let parsed = many_bangs(input).expect("bangs"); |
| 70 | /// assert_eq!(parsed, ["bang", "bang", "bang"]); |
| 71 | /// |
| 72 | /// let input = "bangbang;"; |
| 73 | /// let err = many_bangs(input); |
| 74 | /// assert_eq!(err, IResult::Error); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 75 | /// } |
| 76 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 77 | #[macro_export] |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 78 | macro_rules! keyword { |
| 79 | ($i:expr, $keyword:expr) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 80 | $crate::helper::keyword($i, $keyword) |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 81 | }; |
| 82 | } |
| 83 | |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 84 | // Not public API. |
| 85 | #[doc(hidden)] |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 86 | pub fn keyword<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
| 87 | match punct(input, token) { |
| 88 | IResult::Done(rest, _) => { |
| 89 | match word_break(rest) { |
| 90 | IResult::Done(_, _) => IResult::Done(rest, token), |
| 91 | IResult::Error => IResult::Error, |
| 92 | } |
| 93 | } |
| 94 | IResult::Error => IResult::Error, |
| 95 | } |
| 96 | } |
| 97 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 98 | /// Turn a failed parse into `None` and a successful parse into `Some`. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 99 | /// |
| 100 | /// - **Syntax:** `option!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 101 | /// - **Output:** `Option<THING>` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 102 | /// |
| 103 | /// ```rust |
| 104 | /// extern crate syn; |
| 105 | /// #[macro_use] extern crate synom; |
| 106 | /// |
| 107 | /// named!(maybe_bang -> Option<&str>, option!(punct!("!"))); |
| 108 | /// |
| 109 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 110 | /// let input = "!"; |
| 111 | /// let parsed = maybe_bang(input).expect("maybe bang"); |
| 112 | /// assert_eq!(parsed, Some("!")); |
| 113 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 114 | /// let input = ""; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 115 | /// let parsed = maybe_bang(input).expect("maybe bang"); |
| 116 | /// assert_eq!(parsed, None); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 117 | /// } |
| 118 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 119 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 120 | macro_rules! option { |
| 121 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 122 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 123 | $crate::IResult::Done(i, o) => $crate::IResult::Done(i, Some(o)), |
| 124 | $crate::IResult::Error => $crate::IResult::Done($i, None), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 125 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 126 | }; |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 127 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 128 | ($i:expr, $f:expr) => { |
| 129 | option!($i, call!($f)); |
| 130 | }; |
| 131 | } |
| 132 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 133 | /// Turn a failed parse into an empty vector. The argument parser must itself |
| 134 | /// return a vector. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 135 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 136 | /// This is often more convenient than `option!(...)` when the argument produces |
| 137 | /// a vector. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 138 | /// |
| 139 | /// - **Syntax:** `opt_vec!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 140 | /// - **Output:** `THING`, which must be `Vec<T>` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 141 | /// |
| 142 | /// ```rust |
| 143 | /// extern crate syn; |
| 144 | /// #[macro_use] extern crate synom; |
| 145 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 146 | /// use syn::{Lifetime, Ty}; |
| 147 | /// use syn::parse::{lifetime, ty}; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 148 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 149 | /// named!(bound_lifetimes -> (Vec<Lifetime>, Ty), tuple!( |
| 150 | /// opt_vec!(do_parse!( |
| 151 | /// keyword!("for") >> |
| 152 | /// punct!("<") >> |
| 153 | /// lifetimes: terminated_list!(punct!(","), lifetime) >> |
| 154 | /// punct!(">") >> |
| 155 | /// (lifetimes) |
| 156 | /// )), |
| 157 | /// ty |
| 158 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 159 | /// |
| 160 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 161 | /// let input = "for<'a, 'b> fn(&'a A) -> &'b B"; |
| 162 | /// let parsed = bound_lifetimes(input).expect("bound lifetimes"); |
| 163 | /// assert_eq!(parsed.0, [Lifetime::new("'a"), Lifetime::new("'b")]); |
| 164 | /// println!("{:?}", parsed); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 165 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 166 | /// let input = "From<String>"; |
| 167 | /// let parsed = bound_lifetimes(input).expect("bound lifetimes"); |
| 168 | /// assert!(parsed.0.is_empty()); |
| 169 | /// println!("{:?}", parsed); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 170 | /// } |
| 171 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 172 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 173 | macro_rules! opt_vec { |
| 174 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 175 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 176 | $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), |
| 177 | $crate::IResult::Error => $crate::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 178 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 179 | }; |
| 180 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 181 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 182 | /// Parses nothing and always succeeds. |
| 183 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 184 | /// This can be useful as a fallthrough case in `alt!`. |
| 185 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 186 | /// - **Syntax:** `epsilon!()` |
| 187 | /// - **Output:** `()` |
| 188 | /// |
| 189 | /// ```rust |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 190 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 191 | /// #[macro_use] extern crate synom; |
| 192 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 193 | /// use syn::Mutability; |
| 194 | /// |
| 195 | /// named!(mutability -> Mutability, alt!( |
| 196 | /// keyword!("mut") => { |_| Mutability::Mutable } |
| 197 | /// | |
| 198 | /// epsilon!() => { |_| Mutability::Immutable } |
| 199 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 200 | /// |
| 201 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 202 | /// let input = "mut"; |
| 203 | /// let parsed = mutability(input).expect("mutability"); |
| 204 | /// assert_eq!(parsed, Mutability::Mutable); |
| 205 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 206 | /// let input = ""; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 207 | /// let parsed = mutability(input).expect("mutability"); |
| 208 | /// assert_eq!(parsed, Mutability::Immutable); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 209 | /// } |
| 210 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 211 | #[macro_export] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 212 | macro_rules! epsilon { |
| 213 | ($i:expr,) => { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 214 | $crate::IResult::Done($i, ()) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 215 | }; |
| 216 | } |
| 217 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 218 | /// Run a parser, binding the result to a name, and then evaluating an |
| 219 | /// expression. |
| 220 | /// |
| 221 | /// Discards the result of the expression and parser. |
| 222 | /// |
| 223 | /// - **Syntax:** `tap!(NAME : THING => EXPR)` |
| 224 | /// - **Output:** `()` |
| 225 | /// |
| 226 | /// ```rust |
| 227 | /// extern crate syn; |
| 228 | /// #[macro_use] extern crate synom; |
| 229 | /// |
| 230 | /// use syn::{Expr, ExprKind}; |
| 231 | /// use syn::parse::expr; |
| 232 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 233 | /// named!(expr_with_arrow_call -> Expr, do_parse!( |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 234 | /// mut e: expr >> |
| 235 | /// many0!(tap!(arg: tuple!(punct!("=>"), expr) => { |
| 236 | /// e = Expr { |
| 237 | /// node: ExprKind::Call(Box::new(e), vec![arg.1]), |
| 238 | /// attrs: Vec::new(), |
| 239 | /// }; |
| 240 | /// })) >> |
| 241 | /// (e) |
| 242 | /// )); |
| 243 | /// |
| 244 | /// fn main() { |
| 245 | /// let input = "something => argument1 => argument2"; |
| 246 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 247 | /// let parsed = expr_with_arrow_call(input).expect("expr with arrow call"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 248 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 249 | /// println!("{:?}", parsed); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 250 | /// } |
| 251 | /// ``` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 252 | #[doc(hidden)] |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 253 | #[macro_export] |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 254 | macro_rules! tap { |
| 255 | ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => { |
| 256 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 257 | $crate::IResult::Done(i, o) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 258 | let $name = o; |
| 259 | $e; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 260 | $crate::IResult::Done(i, ()) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 261 | } |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 262 | $crate::IResult::Error => $crate::IResult::Error, |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 263 | } |
| 264 | }; |
| 265 | |
| 266 | ($i:expr, $name:ident : $f:expr => $e:expr) => { |
| 267 | tap!($i, $name: call!($f) => $e); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 268 | }; |
| 269 | } |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 270 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 271 | /// Zero or more values separated by some separator. Does not allow a trailing |
| 272 | /// seperator. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 273 | /// |
| 274 | /// The implementation requires that the first parameter is a `punct!` macro, |
| 275 | /// and the second is a named parser. |
| 276 | /// |
| 277 | /// - **Syntax:** `separated_list!(punct!("..."), THING)` |
| 278 | /// - **Output:** `Vec<THING>` |
| 279 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 280 | /// You may also be looking for: |
| 281 | /// |
| 282 | /// - `separated_nonempty_list!` - one or more values |
| 283 | /// - `terminated_list!` - zero or more, allows trailing separator |
| 284 | /// - `many0!` - zero or more, no separator |
| 285 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 286 | /// ```rust |
| 287 | /// extern crate syn; |
| 288 | /// #[macro_use] extern crate synom; |
| 289 | /// |
| 290 | /// use syn::Expr; |
| 291 | /// use syn::parse::expr; |
| 292 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 293 | /// named!(expr_list -> Vec<Expr>, |
| 294 | /// separated_list!(punct!(","), expr) |
| 295 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 296 | /// |
| 297 | /// fn main() { |
| 298 | /// let input = "1 + 1, things, Construct { this: thing }"; |
| 299 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 300 | /// let parsed = expr_list(input).expect("expr list"); |
| 301 | /// assert_eq!(parsed.len(), 3); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 302 | /// } |
| 303 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 304 | #[macro_export] |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 305 | macro_rules! separated_list { |
| 306 | ($i:expr, punct!($sep:expr), $f:expr) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 307 | $crate::helper::separated_list($i, $sep, $f, false) |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 308 | }; |
| 309 | } |
| 310 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 311 | /// Zero or more values separated by some separator. A trailing separator is |
| 312 | /// allowed. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 313 | /// |
| 314 | /// The implementation requires that the first parameter is a `punct!` macro, |
| 315 | /// and the second is a named parser. |
| 316 | /// |
| 317 | /// - **Syntax:** `terminated_list!(punct!("..."), THING)` |
| 318 | /// - **Output:** `Vec<THING>` |
| 319 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 320 | /// You may also be looking for: |
| 321 | /// |
| 322 | /// - `separated_list!` - zero or more, allows trailing separator |
| 323 | /// - `separated_nonempty_list!` - one or more values |
| 324 | /// - `many0!` - zero or more, no separator |
| 325 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 326 | /// ```rust |
| 327 | /// extern crate syn; |
| 328 | /// #[macro_use] extern crate synom; |
| 329 | /// |
| 330 | /// use syn::Expr; |
| 331 | /// use syn::parse::expr; |
| 332 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 333 | /// named!(expr_list -> Vec<Expr>, |
| 334 | /// terminated_list!(punct!(","), expr) |
| 335 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 336 | /// |
| 337 | /// fn main() { |
| 338 | /// let input = "1 + 1, things, Construct { this: thing },"; |
| 339 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 340 | /// let parsed = expr_list(input).expect("expr list"); |
| 341 | /// assert_eq!(parsed.len(), 3); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 342 | /// } |
| 343 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 344 | #[macro_export] |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 345 | macro_rules! terminated_list { |
| 346 | ($i:expr, punct!($sep:expr), $f:expr) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 347 | $crate::helper::separated_list($i, $sep, $f, true) |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 348 | }; |
| 349 | } |
| 350 | |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 351 | // Not public API. |
| 352 | #[doc(hidden)] |
David Tolnay | c7f646a | 2016-10-16 10:54:39 -0700 | [diff] [blame] | 353 | pub fn separated_list<'a, T>(mut input: &'a str, |
| 354 | sep: &'static str, |
| 355 | f: fn(&'a str) -> IResult<&'a str, T>, |
| 356 | terminated: bool) |
| 357 | -> IResult<&'a str, Vec<T>> { |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 358 | let mut res = Vec::new(); |
| 359 | |
| 360 | // get the first element |
| 361 | match f(input) { |
| 362 | IResult::Error => IResult::Done(input, Vec::new()), |
| 363 | IResult::Done(i, o) => { |
| 364 | if i.len() == input.len() { |
| 365 | IResult::Error |
| 366 | } else { |
| 367 | res.push(o); |
| 368 | input = i; |
| 369 | |
| 370 | // get the separator first |
| 371 | while let IResult::Done(i2, _) = punct(input, sep) { |
| 372 | if i2.len() == input.len() { |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | // get the element next |
| 377 | if let IResult::Done(i3, o3) = f(i2) { |
| 378 | if i3.len() == i2.len() { |
| 379 | break; |
| 380 | } |
| 381 | res.push(o3); |
| 382 | input = i3; |
| 383 | } else { |
| 384 | break; |
| 385 | } |
| 386 | } |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 387 | if terminated { |
| 388 | if let IResult::Done(after, _) = punct(input, sep) { |
| 389 | input = after; |
| 390 | } |
| 391 | } |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 392 | IResult::Done(input, res) |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | } |