Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 1 | use {IResult, TokenTree, TokenKind, OpKind, Delimiter, InputBuf}; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 2 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 3 | /// Parse a piece of punctuation like "+" or "+=". |
| 4 | /// |
| 5 | /// See also `keyword!` for parsing keywords, which are subtly different from |
| 6 | /// punctuation. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 7 | /// |
| 8 | /// - **Syntax:** `punct!("...")` |
| 9 | /// - **Output:** `&str` |
| 10 | /// |
| 11 | /// ```rust |
| 12 | /// extern crate syn; |
| 13 | /// #[macro_use] extern crate synom; |
| 14 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 15 | /// // Parse zero or more bangs. |
| 16 | /// named!(many_bangs -> Vec<&str>, |
| 17 | /// many0!(punct!("!")) |
| 18 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 19 | /// |
| 20 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 21 | /// let input = "!! !"; |
| 22 | /// let parsed = many_bangs(input).expect("bangs"); |
| 23 | /// assert_eq!(parsed, ["!", "!", "!"]); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 24 | /// } |
| 25 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 26 | #[macro_export] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 27 | macro_rules! punct { |
| 28 | ($i:expr, $punct:expr) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 29 | $crate::helper::punct($i, $punct) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 30 | }; |
| 31 | } |
| 32 | |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 33 | // Not public API. |
| 34 | #[doc(hidden)] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 35 | pub fn punct<'a>(input: &'a [TokenTree], token: &'static str) -> IResult<&'a [TokenTree], &'a str> { |
| 36 | // Extract the chars from token, so we know how many tokens to expect, check |
| 37 | // if we are running past EOF, then confirm that the tokens exist as |
| 38 | // requested. |
| 39 | let expected = token.chars().collect::<Vec<_>>(); |
| 40 | if input.len() < expected.len() { |
| 41 | return IResult::Error; |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 42 | } |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 43 | for i in 0..expected.len() { |
| 44 | if let TokenKind::Op(c, ok) = input[i].kind { |
| 45 | if c != expected[i] { |
| 46 | return IResult::Error; |
| 47 | } |
| 48 | |
| 49 | // The last token in the sequence does not have to be marked as |
| 50 | // OpKind::Joint. Unfortunately OpKind doesn't implement |
| 51 | // Eq/PartialEq right now. |
| 52 | match ok { |
| 53 | OpKind::Alone if i != expected.len() - 1 => return IResult::Error, |
| 54 | _ => {} |
| 55 | } |
| 56 | } else { |
| 57 | return IResult::Error; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | IResult::Done(&input[expected.len()..], token) |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 62 | } |
| 63 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 64 | /// Parse a keyword like "fn" or "struct". |
| 65 | /// |
| 66 | /// See also `punct!` for parsing punctuation, which are subtly different from |
| 67 | /// keywords. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 68 | /// |
| 69 | /// - **Syntax:** `keyword!("...")` |
| 70 | /// - **Output:** `&str` |
| 71 | /// |
| 72 | /// ```rust |
| 73 | /// extern crate syn; |
| 74 | /// #[macro_use] extern crate synom; |
| 75 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 76 | /// use synom::IResult; |
| 77 | /// |
| 78 | /// // Parse zero or more "bang" keywords. |
| 79 | /// named!(many_bangs -> Vec<&str>, |
| 80 | /// terminated!( |
| 81 | /// many0!(keyword!("bang")), |
| 82 | /// punct!(";") |
| 83 | /// ) |
| 84 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 85 | /// |
| 86 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 87 | /// let input = "bang bang bang;"; |
| 88 | /// let parsed = many_bangs(input).expect("bangs"); |
| 89 | /// assert_eq!(parsed, ["bang", "bang", "bang"]); |
| 90 | /// |
| 91 | /// let input = "bangbang;"; |
| 92 | /// let err = many_bangs(input); |
| 93 | /// assert_eq!(err, IResult::Error); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 94 | /// } |
| 95 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 96 | #[macro_export] |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 97 | macro_rules! keyword { |
| 98 | ($i:expr, $keyword:expr) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 99 | $crate::helper::keyword($i, $keyword) |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 100 | }; |
| 101 | } |
| 102 | |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 103 | // Not public API. |
| 104 | #[doc(hidden)] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 105 | pub fn keyword<'a>(input: &'a [TokenTree], token: &'static str) -> IResult<&'a [TokenTree], &'static str> { |
| 106 | match input.first() { |
| 107 | Some(&TokenTree{ kind: TokenKind::Word(ref symbol), .. }) if &**symbol == token => |
| 108 | IResult::Done(&input[1..], token), |
| 109 | _ => IResult::Error, |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 113 | /// Turn a failed parse into `None` and a successful parse into `Some`. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 114 | /// |
| 115 | /// - **Syntax:** `option!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 116 | /// - **Output:** `Option<THING>` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 117 | /// |
| 118 | /// ```rust |
| 119 | /// extern crate syn; |
| 120 | /// #[macro_use] extern crate synom; |
| 121 | /// |
| 122 | /// named!(maybe_bang -> Option<&str>, option!(punct!("!"))); |
| 123 | /// |
| 124 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 125 | /// let input = "!"; |
| 126 | /// let parsed = maybe_bang(input).expect("maybe bang"); |
| 127 | /// assert_eq!(parsed, Some("!")); |
| 128 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 129 | /// let input = ""; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 130 | /// let parsed = maybe_bang(input).expect("maybe bang"); |
| 131 | /// assert_eq!(parsed, None); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 132 | /// } |
| 133 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 134 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 135 | macro_rules! option { |
| 136 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 137 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 138 | $crate::IResult::Done(i, o) => $crate::IResult::Done(i, Some(o)), |
| 139 | $crate::IResult::Error => $crate::IResult::Done($i, None), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 140 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 141 | }; |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 142 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 143 | ($i:expr, $f:expr) => { |
| 144 | option!($i, call!($f)); |
| 145 | }; |
| 146 | } |
| 147 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 148 | /// Turn a failed parse into an empty vector. The argument parser must itself |
| 149 | /// return a vector. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 150 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 151 | /// This is often more convenient than `option!(...)` when the argument produces |
| 152 | /// a vector. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 153 | /// |
| 154 | /// - **Syntax:** `opt_vec!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 155 | /// - **Output:** `THING`, which must be `Vec<T>` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 156 | /// |
| 157 | /// ```rust |
| 158 | /// extern crate syn; |
| 159 | /// #[macro_use] extern crate synom; |
| 160 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 161 | /// use syn::{Lifetime, Ty}; |
| 162 | /// use syn::parse::{lifetime, ty}; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 163 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 164 | /// named!(bound_lifetimes -> (Vec<Lifetime>, Ty), tuple!( |
| 165 | /// opt_vec!(do_parse!( |
| 166 | /// keyword!("for") >> |
| 167 | /// punct!("<") >> |
| 168 | /// lifetimes: terminated_list!(punct!(","), lifetime) >> |
| 169 | /// punct!(">") >> |
| 170 | /// (lifetimes) |
| 171 | /// )), |
| 172 | /// ty |
| 173 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 174 | /// |
| 175 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 176 | /// let input = "for<'a, 'b> fn(&'a A) -> &'b B"; |
| 177 | /// let parsed = bound_lifetimes(input).expect("bound lifetimes"); |
| 178 | /// assert_eq!(parsed.0, [Lifetime::new("'a"), Lifetime::new("'b")]); |
| 179 | /// println!("{:?}", parsed); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 180 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 181 | /// let input = "From<String>"; |
| 182 | /// let parsed = bound_lifetimes(input).expect("bound lifetimes"); |
| 183 | /// assert!(parsed.0.is_empty()); |
| 184 | /// println!("{:?}", parsed); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 185 | /// } |
| 186 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 187 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 188 | macro_rules! opt_vec { |
| 189 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 190 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 191 | $crate::IResult::Done(i, o) => $crate::IResult::Done(i, o), |
| 192 | $crate::IResult::Error => $crate::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 193 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 194 | }; |
| 195 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 196 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 197 | /// Parses nothing and always succeeds. |
| 198 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 199 | /// This can be useful as a fallthrough case in `alt!`. |
| 200 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 201 | /// - **Syntax:** `epsilon!()` |
| 202 | /// - **Output:** `()` |
| 203 | /// |
| 204 | /// ```rust |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 205 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 206 | /// #[macro_use] extern crate synom; |
| 207 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 208 | /// use syn::Mutability; |
| 209 | /// |
| 210 | /// named!(mutability -> Mutability, alt!( |
| 211 | /// keyword!("mut") => { |_| Mutability::Mutable } |
| 212 | /// | |
| 213 | /// epsilon!() => { |_| Mutability::Immutable } |
| 214 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 215 | /// |
| 216 | /// fn main() { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 217 | /// let input = "mut"; |
| 218 | /// let parsed = mutability(input).expect("mutability"); |
| 219 | /// assert_eq!(parsed, Mutability::Mutable); |
| 220 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 221 | /// let input = ""; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 222 | /// let parsed = mutability(input).expect("mutability"); |
| 223 | /// assert_eq!(parsed, Mutability::Immutable); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 224 | /// } |
| 225 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 226 | #[macro_export] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 227 | macro_rules! epsilon { |
| 228 | ($i:expr,) => { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 229 | $crate::IResult::Done($i, ()) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 230 | }; |
| 231 | } |
| 232 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 233 | /// Run a parser, binding the result to a name, and then evaluating an |
| 234 | /// expression. |
| 235 | /// |
| 236 | /// Discards the result of the expression and parser. |
| 237 | /// |
| 238 | /// - **Syntax:** `tap!(NAME : THING => EXPR)` |
| 239 | /// - **Output:** `()` |
| 240 | /// |
| 241 | /// ```rust |
| 242 | /// extern crate syn; |
| 243 | /// #[macro_use] extern crate synom; |
| 244 | /// |
| 245 | /// use syn::{Expr, ExprKind}; |
| 246 | /// use syn::parse::expr; |
| 247 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 248 | /// named!(expr_with_arrow_call -> Expr, do_parse!( |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 249 | /// mut e: expr >> |
| 250 | /// many0!(tap!(arg: tuple!(punct!("=>"), expr) => { |
| 251 | /// e = Expr { |
| 252 | /// node: ExprKind::Call(Box::new(e), vec![arg.1]), |
| 253 | /// attrs: Vec::new(), |
| 254 | /// }; |
| 255 | /// })) >> |
| 256 | /// (e) |
| 257 | /// )); |
| 258 | /// |
| 259 | /// fn main() { |
| 260 | /// let input = "something => argument1 => argument2"; |
| 261 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 262 | /// let parsed = expr_with_arrow_call(input).expect("expr with arrow call"); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 263 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 264 | /// println!("{:?}", parsed); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 265 | /// } |
| 266 | /// ``` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 267 | #[doc(hidden)] |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 268 | #[macro_export] |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 269 | macro_rules! tap { |
| 270 | ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => { |
| 271 | match $submac!($i, $($args)*) { |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 272 | $crate::IResult::Done(i, o) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 273 | let $name = o; |
| 274 | $e; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 275 | $crate::IResult::Done(i, ()) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 276 | } |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 277 | $crate::IResult::Error => $crate::IResult::Error, |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 278 | } |
| 279 | }; |
| 280 | |
| 281 | ($i:expr, $name:ident : $f:expr => $e:expr) => { |
| 282 | tap!($i, $name: call!($f) => $e); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 283 | }; |
| 284 | } |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 285 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 286 | /// Zero or more values separated by some separator. Does not allow a trailing |
| 287 | /// seperator. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 288 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 289 | /// - **Syntax:** `separated_list!(punct!("..."), THING)` |
| 290 | /// - **Output:** `Vec<THING>` |
| 291 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 292 | /// You may also be looking for: |
| 293 | /// |
| 294 | /// - `separated_nonempty_list!` - one or more values |
| 295 | /// - `terminated_list!` - zero or more, allows trailing separator |
| 296 | /// - `many0!` - zero or more, no separator |
| 297 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 298 | /// ```rust |
| 299 | /// extern crate syn; |
| 300 | /// #[macro_use] extern crate synom; |
| 301 | /// |
| 302 | /// use syn::Expr; |
| 303 | /// use syn::parse::expr; |
| 304 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 305 | /// named!(expr_list -> Vec<Expr>, |
| 306 | /// separated_list!(punct!(","), expr) |
| 307 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 308 | /// |
| 309 | /// fn main() { |
| 310 | /// let input = "1 + 1, things, Construct { this: thing }"; |
| 311 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 312 | /// let parsed = expr_list(input).expect("expr list"); |
| 313 | /// assert_eq!(parsed.len(), 3); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 314 | /// } |
| 315 | /// ``` |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 316 | /// |
| 317 | /// ```rust |
| 318 | /// extern crate syn; |
| 319 | /// #[macro_use] extern crate synom; |
| 320 | /// |
| 321 | /// use syn::Ident; |
| 322 | /// use syn::parse::ident; |
| 323 | /// |
| 324 | /// named!(run_on -> Vec<Ident>, |
| 325 | /// terminated!( |
| 326 | /// separated_list!(keyword!("and"), preceded!(punct!("$"), ident)), |
| 327 | /// punct!("...") |
| 328 | /// ) |
| 329 | /// ); |
| 330 | /// |
| 331 | /// fn main() { |
| 332 | /// let input = "$expr and $ident and $pat ..."; |
| 333 | /// |
| 334 | /// let parsed = run_on(input).expect("run-on sentence"); |
| 335 | /// assert_eq!(parsed.len(), 3); |
| 336 | /// assert_eq!(parsed[0], "expr"); |
| 337 | /// assert_eq!(parsed[1], "ident"); |
| 338 | /// assert_eq!(parsed[2], "pat"); |
| 339 | /// } |
| 340 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 341 | #[macro_export] |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 342 | macro_rules! separated_list { |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 343 | // Try to use this branch if possible - makes a difference in compile time. |
David Tolnay | 0882b9c | 2017-02-27 13:07:15 -0800 | [diff] [blame] | 344 | ($i:expr, punct!($sep:expr), $f:ident) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 345 | $crate::helper::separated_list($i, $sep, $f, false) |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 346 | }; |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 347 | |
| 348 | ($i:expr, $sepmac:ident!( $($separgs:tt)* ), $fmac:ident!( $($fargs:tt)* )) => {{ |
| 349 | let mut res = ::std::vec::Vec::new(); |
| 350 | let mut input = $i; |
| 351 | |
| 352 | // get the first element |
| 353 | match $fmac!(input, $($fargs)*) { |
| 354 | $crate::IResult::Error => $crate::IResult::Done(input, res), |
| 355 | $crate::IResult::Done(i, o) => { |
| 356 | if i.len() == input.len() { |
| 357 | $crate::IResult::Error |
| 358 | } else { |
| 359 | res.push(o); |
| 360 | input = i; |
| 361 | |
| 362 | // get the separator first |
| 363 | while let $crate::IResult::Done(i2, _) = $sepmac!(input, $($separgs)*) { |
| 364 | if i2.len() == input.len() { |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | // get the element next |
| 369 | if let $crate::IResult::Done(i3, o3) = $fmac!(i2, $($fargs)*) { |
| 370 | if i3.len() == i2.len() { |
| 371 | break; |
| 372 | } |
| 373 | res.push(o3); |
| 374 | input = i3; |
| 375 | } else { |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | $crate::IResult::Done(input, res) |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | }}; |
| 384 | |
| 385 | ($i:expr, $sepmac:ident!( $($separgs:tt)* ), $f:expr) => { |
Christopher Bacher | 9620ae0 | 2017-03-02 00:44:25 +0100 | [diff] [blame] | 386 | separated_list!($i, $sepmac!($($separgs)*), call!($f)) |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | ($i:expr, $sep:expr, $fmac:ident!( $($fargs:tt)* )) => { |
David Tolnay | 6e825ca | 2017-03-01 15:53:40 -0800 | [diff] [blame] | 390 | separated_list!($i, call!($sep), $fmac!($($fargs)*)) |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | ($i:expr, $sep:expr, $f:expr) => { |
| 394 | separated_list!($i, call!($sep), call!($f)) |
| 395 | }; |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 396 | } |
| 397 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 398 | /// Zero or more values separated by some separator. A trailing separator is |
| 399 | /// allowed. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 400 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 401 | /// - **Syntax:** `terminated_list!(punct!("..."), THING)` |
| 402 | /// - **Output:** `Vec<THING>` |
| 403 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 404 | /// You may also be looking for: |
| 405 | /// |
| 406 | /// - `separated_list!` - zero or more, allows trailing separator |
| 407 | /// - `separated_nonempty_list!` - one or more values |
| 408 | /// - `many0!` - zero or more, no separator |
| 409 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 410 | /// ```rust |
| 411 | /// extern crate syn; |
| 412 | /// #[macro_use] extern crate synom; |
| 413 | /// |
| 414 | /// use syn::Expr; |
| 415 | /// use syn::parse::expr; |
| 416 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 417 | /// named!(expr_list -> Vec<Expr>, |
| 418 | /// terminated_list!(punct!(","), expr) |
| 419 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 420 | /// |
| 421 | /// fn main() { |
| 422 | /// let input = "1 + 1, things, Construct { this: thing },"; |
| 423 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 424 | /// let parsed = expr_list(input).expect("expr list"); |
| 425 | /// assert_eq!(parsed.len(), 3); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 426 | /// } |
| 427 | /// ``` |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 428 | /// |
| 429 | /// ```rust |
| 430 | /// extern crate syn; |
| 431 | /// #[macro_use] extern crate synom; |
| 432 | /// |
| 433 | /// use syn::Ident; |
| 434 | /// use syn::parse::ident; |
| 435 | /// |
| 436 | /// named!(run_on -> Vec<Ident>, |
| 437 | /// terminated!( |
| 438 | /// terminated_list!(keyword!("and"), preceded!(punct!("$"), ident)), |
| 439 | /// punct!("...") |
| 440 | /// ) |
| 441 | /// ); |
| 442 | /// |
| 443 | /// fn main() { |
| 444 | /// let input = "$expr and $ident and $pat and ..."; |
| 445 | /// |
| 446 | /// let parsed = run_on(input).expect("run-on sentence"); |
| 447 | /// assert_eq!(parsed.len(), 3); |
| 448 | /// assert_eq!(parsed[0], "expr"); |
| 449 | /// assert_eq!(parsed[1], "ident"); |
| 450 | /// assert_eq!(parsed[2], "pat"); |
| 451 | /// } |
| 452 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 453 | #[macro_export] |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 454 | macro_rules! terminated_list { |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 455 | // Try to use this branch if possible - makes a difference in compile time. |
| 456 | ($i:expr, punct!($sep:expr), $f:ident) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 457 | $crate::helper::separated_list($i, $sep, $f, true) |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 458 | }; |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 459 | |
| 460 | ($i:expr, $sepmac:ident!( $($separgs:tt)* ), $fmac:ident!( $($fargs:tt)* )) => {{ |
| 461 | let mut res = ::std::vec::Vec::new(); |
| 462 | let mut input = $i; |
| 463 | |
| 464 | // get the first element |
| 465 | match $fmac!(input, $($fargs)*) { |
| 466 | $crate::IResult::Error => $crate::IResult::Done(input, res), |
| 467 | $crate::IResult::Done(i, o) => { |
| 468 | if i.len() == input.len() { |
| 469 | $crate::IResult::Error |
| 470 | } else { |
| 471 | res.push(o); |
| 472 | input = i; |
| 473 | |
| 474 | // get the separator first |
| 475 | while let $crate::IResult::Done(i2, _) = $sepmac!(input, $($separgs)*) { |
| 476 | if i2.len() == input.len() { |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | // get the element next |
| 481 | if let $crate::IResult::Done(i3, o3) = $fmac!(i2, $($fargs)*) { |
| 482 | if i3.len() == i2.len() { |
| 483 | break; |
| 484 | } |
| 485 | res.push(o3); |
| 486 | input = i3; |
| 487 | } else { |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | if let $crate::IResult::Done(after, _) = $sepmac!(input, $($separgs)*) { |
| 492 | input = after; |
| 493 | } |
| 494 | $crate::IResult::Done(input, res) |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | }}; |
| 499 | |
| 500 | ($i:expr, $sepmac:ident!( $($separgs:tt)* ), $f:expr) => { |
David Tolnay | 6e825ca | 2017-03-01 15:53:40 -0800 | [diff] [blame] | 501 | terminated_list!($i, $sepmac!($($separgs)*), call!($f)) |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | ($i:expr, $sep:expr, $fmac:ident!( $($fargs:tt)* )) => { |
David Tolnay | 6e825ca | 2017-03-01 15:53:40 -0800 | [diff] [blame] | 505 | terminated_list!($i, call!($sep), $fmac!($($fargs)*)) |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | ($i:expr, $sep:expr, $f:expr) => { |
| 509 | terminated_list!($i, call!($sep), call!($f)) |
| 510 | }; |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 511 | } |
| 512 | |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 513 | // Not public API. |
| 514 | #[doc(hidden)] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 515 | pub fn separated_list<'a, T>(mut input: &'a [TokenTree], |
David Tolnay | c7f646a | 2016-10-16 10:54:39 -0700 | [diff] [blame] | 516 | sep: &'static str, |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 517 | f: fn(&'a [TokenTree]) -> IResult<&'a [TokenTree], T>, |
David Tolnay | c7f646a | 2016-10-16 10:54:39 -0700 | [diff] [blame] | 518 | terminated: bool) |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 519 | -> IResult<&'a [TokenTree], Vec<T>> { |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 520 | let mut res = Vec::new(); |
| 521 | |
| 522 | // get the first element |
| 523 | match f(input) { |
David Tolnay | dff1880 | 2017-02-27 11:57:03 -0800 | [diff] [blame] | 524 | IResult::Error => IResult::Done(input, res), |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 525 | IResult::Done(i, o) => { |
| 526 | if i.len() == input.len() { |
| 527 | IResult::Error |
| 528 | } else { |
| 529 | res.push(o); |
| 530 | input = i; |
| 531 | |
| 532 | // get the separator first |
| 533 | while let IResult::Done(i2, _) = punct(input, sep) { |
| 534 | if i2.len() == input.len() { |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | // get the element next |
| 539 | if let IResult::Done(i3, o3) = f(i2) { |
| 540 | if i3.len() == i2.len() { |
| 541 | break; |
| 542 | } |
| 543 | res.push(o3); |
| 544 | input = i3; |
| 545 | } else { |
| 546 | break; |
| 547 | } |
| 548 | } |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 549 | if terminated { |
| 550 | if let IResult::Done(after, _) = punct(input, sep) { |
| 551 | input = after; |
| 552 | } |
| 553 | } |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 554 | IResult::Done(input, res) |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 559 | |
| 560 | #[macro_export] |
| 561 | macro_rules! delim { |
| 562 | ($i:expr, $delim:ident, $fmac:ident!( $($fargs:tt)* )) => { |
| 563 | match $crate::helper::delim_impl($i, $crate::Delimiter::$delim) { |
| 564 | Some((i, ib)) => { |
| 565 | match $fmac!(&*ib, $($fargs)*) { |
| 566 | $crate::IResult::Done(rest, val) => { |
| 567 | if rest.is_empty() { |
| 568 | $crate::IResult::Done(i, val) |
| 569 | } else { |
| 570 | $crate::IResult::Error |
| 571 | } |
| 572 | } |
| 573 | _ => $crate::IResult::Error, |
| 574 | } |
| 575 | } |
| 576 | _ => $crate::IResult::Error, |
| 577 | } |
| 578 | }; |
| 579 | ($i:expr, $delim:ident, $f:expr) => { |
| 580 | delim!($i, $delim, call!($f)) |
| 581 | }; |
| 582 | } |
| 583 | |
| 584 | // Not a public API |
| 585 | #[doc(hidden)] |
| 586 | pub fn delim_impl(input: &[TokenTree], |
| 587 | expected_delim: Delimiter) |
| 588 | -> Option<(&[TokenTree], InputBuf)> { |
| 589 | // NOTE: The `as u32` hack is being used as `Delimiter` doesn't implement |
| 590 | // `PartialEq` or `Eq` despite being a simple c-style enum. |
| 591 | match input.first() { |
| 592 | Some(&TokenTree { |
| 593 | kind: TokenKind::Sequence(delim, ref stream), |
| 594 | .. |
| 595 | }) if delim as u32 == expected_delim as u32 => { |
| 596 | Some((&input[1..], InputBuf::new(stream.clone()))) |
| 597 | } |
| 598 | _ => None |
| 599 | } |
| 600 | } |