Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1 | use std::iter::FromIterator; |
| 2 | use std::slice; |
| 3 | use std::vec; |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 4 | #[cfg(feature = "extra-traits")] |
| 5 | use std::fmt::{self, Debug}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 6 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 7 | #[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 8 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 9 | pub struct Delimited<T, D> { |
| 10 | inner: Vec<(T, Option<D>)> |
| 11 | } |
| 12 | |
| 13 | impl<T, D> Delimited<T, D> { |
| 14 | pub fn new() -> Delimited<T, D> { |
| 15 | Delimited { |
| 16 | inner: Vec::new(), |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | pub fn is_empty(&self) -> bool { |
| 21 | self.inner.len() == 0 |
| 22 | } |
| 23 | |
| 24 | pub fn len(&self) -> usize { |
| 25 | self.inner.len() |
| 26 | } |
| 27 | |
| 28 | pub fn get(&self, idx: usize) -> Element<&T, &D> { |
| 29 | let (ref t, ref d) = self.inner[idx]; |
| 30 | match *d { |
| 31 | Some(ref d) => Element::Delimited(t, d), |
| 32 | None => Element::End(t), |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | pub fn get_mut(&mut self, idx: usize) -> Element<&mut T, &mut D> { |
| 37 | let (ref mut t, ref mut d) = self.inner[idx]; |
| 38 | match *d { |
| 39 | Some(ref mut d) => Element::Delimited(t, d), |
| 40 | None => Element::End(t), |
| 41 | } |
| 42 | } |
| 43 | |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 44 | pub fn first(&self) -> Option<Element<&T, &D>> { |
| 45 | self.inner.first().map(|&(ref t, ref d)| { |
| 46 | match *d { |
| 47 | Some(ref d) => Element::Delimited(t, d), |
| 48 | None => Element::End(t), |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | pub fn first_mut(&mut self) -> Option<Element<&mut T, &mut D>> { |
| 54 | self.inner.first_mut().map(|&mut (ref mut t, ref mut d)| { |
| 55 | match *d { |
| 56 | Some(ref mut d) => Element::Delimited(t, d), |
| 57 | None => Element::End(t), |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | pub fn last(&self) -> Option<Element<&T, &D>> { |
| 63 | self.inner.last().map(|&(ref t, ref d)| { |
| 64 | match *d { |
| 65 | Some(ref d) => Element::Delimited(t, d), |
| 66 | None => Element::End(t), |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | pub fn last_mut(&mut self) -> Option<Element<&mut T, &mut D>> { |
| 72 | self.inner.last_mut().map(|&mut (ref mut t, ref mut d)| { |
| 73 | match *d { |
| 74 | Some(ref mut d) => Element::Delimited(t, d), |
| 75 | None => Element::End(t), |
| 76 | } |
| 77 | }) |
| 78 | } |
| 79 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 80 | pub fn iter(&self) -> Iter<T, D> { |
| 81 | Iter { inner: self.inner.iter() } |
| 82 | } |
| 83 | |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 84 | pub fn iter_mut(&mut self) -> IterMut<T, D> { |
| 85 | IterMut { inner: self.inner.iter_mut() } |
| 86 | } |
| 87 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 88 | pub fn items(&self) -> Items<T, D> { |
| 89 | Items { inner: self.inner.iter() } |
| 90 | } |
| 91 | |
| 92 | pub fn push(&mut self, token: Element<T, D>) { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 93 | assert!(self.is_empty() || self.trailing_delim()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 94 | match token { |
| 95 | Element::Delimited(t, d) => self.inner.push((t, Some(d))), |
| 96 | Element::End(t) => self.inner.push((t, None)), |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | pub fn push_first(&mut self, token: T) { |
| 101 | assert!(self.is_empty()); |
| 102 | self.inner.push((token, None)); |
| 103 | } |
| 104 | |
| 105 | pub fn push_next(&mut self, token: T, delimiter: D) { |
| 106 | self.push_trailing(delimiter); |
| 107 | self.inner.push((token, None)); |
| 108 | } |
| 109 | |
| 110 | pub fn push_trailing(&mut self, delimiter: D) { |
| 111 | let len = self.len(); |
| 112 | assert!(self.inner[len - 1].1.is_none()); |
| 113 | self.inner[len - 1].1 = Some(delimiter); |
| 114 | } |
| 115 | |
| 116 | pub fn push_default(&mut self, token: T) where D: Default { |
Alex Crichton | 337cd46 | 2017-07-06 14:47:25 -0700 | [diff] [blame] | 117 | if self.is_empty() || self.trailing_delim() { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 118 | self.inner.push((token, None)); |
| 119 | } else { |
| 120 | self.push_next(token, D::default()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | pub fn pop(&mut self) -> Option<Element<T, D>> { |
| 125 | self.inner.pop().map(|e| { |
| 126 | match e { |
| 127 | (t, Some(d)) => Element::Delimited(t, d), |
| 128 | (t, None) => Element::End(t), |
| 129 | } |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | pub fn into_vec(self) -> Vec<T> { |
| 134 | self.inner.into_iter().map(|t| t.0).collect() |
| 135 | } |
| 136 | |
| 137 | pub fn trailing_delim(&self) -> bool { |
| 138 | self.inner[self.inner.len() - 1].1.is_some() |
| 139 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 140 | |
| 141 | /// Returns true if either this `Delimited` is empty, or it has a trailing |
| 142 | /// delimiter. This is useful within `ToTokens` implementations for `syn`. |
| 143 | #[doc(hidden)] |
| 144 | pub fn empty_or_trailing(&self) -> bool { |
| 145 | self.is_empty() || self.trailing_delim() |
| 146 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 149 | #[cfg(feature = "extra-traits")] |
| 150 | impl<T: Debug, D: Debug> Debug for Delimited<T, D> { |
| 151 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 152 | self.inner.fmt(f) |
| 153 | } |
| 154 | } |
| 155 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 156 | impl<T, D> From<Vec<(T, Option<D>)>> for Delimited<T, D> { |
| 157 | fn from(v: Vec<(T, Option<D>)>) -> Self { |
| 158 | Delimited { |
| 159 | inner: v, |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | impl<T, D> From<Vec<T>> for Delimited<T, D> |
| 165 | where D: Default, |
| 166 | { |
| 167 | fn from(v: Vec<T>) -> Self { |
Alex Crichton | afb49d2 | 2017-07-06 14:47:37 -0700 | [diff] [blame] | 168 | let len = v.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 169 | Delimited { |
| 170 | inner: v.into_iter().enumerate().map(|(i, item)| { |
Alex Crichton | afb49d2 | 2017-07-06 14:47:37 -0700 | [diff] [blame] | 171 | (item, if i + 1 == len {None} else {Some(D::default())}) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 172 | }).collect(), |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | impl<T, D> FromIterator<Element<T, D>> for Delimited<T, D> { |
| 178 | fn from_iter<I: IntoIterator<Item = Element<T, D>>>(i: I) -> Self { |
| 179 | let mut ret = Delimited::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 180 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 181 | ret |
| 182 | } |
| 183 | } |
| 184 | |
Alex Crichton | d3743d1 | 2017-07-07 20:55:24 -0700 | [diff] [blame] | 185 | impl<T, D> FromIterator<T> for Delimited<T, D> |
| 186 | where D: Default, |
| 187 | { |
| 188 | fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self { |
| 189 | let mut ret = Delimited::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 190 | ret.extend(i); |
Alex Crichton | d3743d1 | 2017-07-07 20:55:24 -0700 | [diff] [blame] | 191 | ret |
| 192 | } |
| 193 | } |
| 194 | |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 195 | impl<T, D> Extend<Element<T, D>> for Delimited<T, D> { |
| 196 | fn extend<I: IntoIterator<Item = Element<T, D>>>(&mut self, i: I) { |
| 197 | for element in i { |
| 198 | match element { |
| 199 | Element::Delimited(a, b) => self.inner.push((a, Some(b))), |
| 200 | Element::End(a) => self.inner.push((a, None)), |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | impl<T, D> Extend<T> for Delimited<T, D> |
| 207 | where D: Default, |
| 208 | { |
| 209 | fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) { |
| 210 | for element in i { |
| 211 | self.push_default(element); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 216 | impl<'a, T, D> IntoIterator for &'a Delimited<T, D> { |
| 217 | type Item = Element<&'a T, &'a D>; |
| 218 | type IntoIter = Iter<'a, T, D>; |
| 219 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame^] | 220 | fn into_iter(self) -> Self::IntoIter { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 221 | <Delimited<T, D>>::iter(self) |
| 222 | } |
| 223 | } |
| 224 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame^] | 225 | impl<'a, T, D> IntoIterator for &'a mut Delimited<T, D> { |
| 226 | type Item = Element<&'a mut T, &'a mut D>; |
| 227 | type IntoIter = IterMut<'a, T, D>; |
| 228 | |
| 229 | fn into_iter(self) -> Self::IntoIter { |
| 230 | <Delimited<T, D>>::iter_mut(self) |
| 231 | } |
| 232 | } |
| 233 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 234 | impl<T, D> IntoIterator for Delimited<T, D> { |
| 235 | type Item = Element<T, D>; |
| 236 | type IntoIter = IntoIter<T, D>; |
| 237 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame^] | 238 | fn into_iter(self) -> Self::IntoIter { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 239 | IntoIter { inner: self.inner.into_iter() } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | impl<T, D> Default for Delimited<T, D> { |
| 244 | fn default() -> Self { |
| 245 | Delimited::new() |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | pub struct Iter<'a, T: 'a, D: 'a> { |
| 250 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 251 | } |
| 252 | |
| 253 | impl<'a, T, D> Iterator for Iter<'a, T, D> { |
| 254 | type Item = Element<&'a T, &'a D>; |
| 255 | |
| 256 | fn next(&mut self) -> Option<Element<&'a T, &'a D>> { |
| 257 | self.inner.next().map(|pair| { |
| 258 | match pair.1 { |
| 259 | Some(ref delimited) => Element::Delimited(&pair.0, delimited), |
| 260 | None => Element::End(&pair.0), |
| 261 | } |
| 262 | }) |
| 263 | } |
| 264 | } |
| 265 | |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 266 | pub struct IterMut<'a, T: 'a, D: 'a> { |
| 267 | inner: slice::IterMut<'a, (T, Option<D>)>, |
| 268 | } |
| 269 | |
| 270 | impl<'a, T, D> Iterator for IterMut<'a, T, D> { |
| 271 | type Item = Element<&'a mut T, &'a mut D>; |
| 272 | |
| 273 | fn next(&mut self) -> Option<Element<&'a mut T, &'a mut D>> { |
| 274 | self.inner.next().map(|pair| { |
| 275 | match pair.1 { |
| 276 | Some(ref mut delimited) => Element::Delimited(&mut pair.0, delimited), |
| 277 | None => Element::End(&mut pair.0), |
| 278 | } |
| 279 | }) |
| 280 | } |
| 281 | } |
| 282 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 283 | pub struct Items<'a, T: 'a, D: 'a> { |
| 284 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 285 | } |
| 286 | |
| 287 | impl<'a, T, D> Iterator for Items<'a, T, D> { |
| 288 | type Item = &'a T; |
| 289 | |
| 290 | fn next(&mut self) -> Option<&'a T> { |
| 291 | self.inner.next().map(|pair| &pair.0) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | pub struct IntoIter<T, D> { |
| 296 | inner: vec::IntoIter<(T, Option<D>)>, |
| 297 | } |
| 298 | |
| 299 | impl<T, D> Iterator for IntoIter<T, D> { |
| 300 | type Item = Element<T, D>; |
| 301 | |
| 302 | fn next(&mut self) -> Option<Element<T, D>> { |
| 303 | self.inner.next().map(|pair| { |
| 304 | match pair.1 { |
| 305 | Some(v) => Element::Delimited(pair.0, v), |
| 306 | None => Element::End(pair.0) |
| 307 | } |
| 308 | }) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | pub enum Element<T, D> { |
| 313 | Delimited(T, D), |
| 314 | End(T), |
| 315 | } |
| 316 | |
| 317 | impl<T, D> Element<T, D> { |
| 318 | pub fn into_item(self) -> T { |
| 319 | match self { |
| 320 | Element::Delimited(t, _) | |
| 321 | Element::End(t) => t, |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | pub fn item(&self) -> &T { |
| 326 | match *self { |
| 327 | Element::Delimited(ref t, _) | |
| 328 | Element::End(ref t) => t, |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | pub fn item_mut(&mut self) -> &mut T { |
| 333 | match *self { |
| 334 | Element::Delimited(ref mut t, _) | |
| 335 | Element::End(ref mut t) => t, |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | pub fn delimiter(&self) -> Option<&D> { |
| 340 | match *self { |
| 341 | Element::Delimited(_, ref d) => Some(d), |
| 342 | Element::End(_) => None, |
| 343 | } |
| 344 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 345 | |
| 346 | pub fn into_tuple(self) -> (T, Option<D>) { |
| 347 | match self { |
| 348 | Element::Delimited(t, d) => (t, Some(d)), |
| 349 | Element::End(t) => (t, None), |
| 350 | } |
| 351 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 354 | #[cfg(feature = "parsing")] |
| 355 | mod parsing { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 356 | use super::Delimited; |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 357 | use {PResult, Cursor, Synom, parse_error}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 358 | |
| 359 | impl<T, D> Delimited<T, D> |
| 360 | where T: Synom, |
| 361 | D: Synom, |
| 362 | { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 363 | pub fn parse_separated(input: Cursor) -> PResult<Self> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 364 | { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 365 | Self::parse(input, T::parse, false) |
| 366 | } |
| 367 | |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 368 | pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 369 | { |
| 370 | Self::parse_separated_nonempty_with(input, T::parse) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 373 | pub fn parse_terminated(input: Cursor) -> PResult<Self> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 374 | { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 375 | Self::parse_terminated_with(input, T::parse) |
| 376 | } |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 377 | |
| 378 | pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> |
| 379 | { |
| 380 | Self::parse_terminated_nonempty_with(input, T::parse) |
| 381 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | impl<T, D> Delimited<T, D> |
| 385 | where D: Synom, |
| 386 | { |
| 387 | pub fn parse_separated_nonempty_with( |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 388 | input: Cursor, |
| 389 | parse: fn(Cursor) -> PResult<T>) |
| 390 | -> PResult<Self> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 391 | { |
| 392 | match Self::parse(input, parse, false) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 393 | Ok((_, ref b)) if b.is_empty() => parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 394 | other => other, |
| 395 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 398 | pub fn parse_terminated_with( |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 399 | input: Cursor, |
| 400 | parse: fn(Cursor) -> PResult<T>) |
| 401 | -> PResult<Self> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 402 | { |
| 403 | Self::parse(input, parse, true) |
| 404 | } |
| 405 | |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 406 | pub fn parse_terminated_nonempty_with( |
| 407 | input: Cursor, |
| 408 | parse: fn(Cursor) -> PResult<T>) |
| 409 | -> PResult<Self> |
| 410 | { |
| 411 | match Self::parse(input, parse, true) { |
| 412 | Ok((_, ref b)) if b.is_empty() => parse_error(), |
| 413 | other => other, |
| 414 | } |
| 415 | } |
| 416 | |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 417 | fn parse(mut input: Cursor, |
| 418 | parse: fn(Cursor) -> PResult<T>, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 419 | terminated: bool) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 420 | -> PResult<Self> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 421 | { |
| 422 | let mut res = Delimited::new(); |
| 423 | |
| 424 | // get the first element |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 425 | match parse(input) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 426 | Err(_) => Ok((input, res)), |
| 427 | Ok((i, o)) => { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 428 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 429 | return parse_error(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 430 | } |
| 431 | input = i; |
| 432 | res.push_first(o); |
| 433 | |
| 434 | // get the separator first |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 435 | while let Ok((i2, s)) = D::parse(input) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 436 | if i2 == input { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | |
| 440 | // get the element next |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 441 | if let Ok((i3, o3)) = parse(i2) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 442 | if i3 == i2 { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 443 | break; |
| 444 | } |
| 445 | res.push_next(o3, s); |
| 446 | input = i3; |
| 447 | } else { |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | if terminated { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 452 | if let Ok((after, sep)) = D::parse(input) { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 453 | res.push_trailing(sep); |
| 454 | input = after; |
| 455 | } |
| 456 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 457 | Ok((input, res)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 464 | #[cfg(feature = "printing")] |
| 465 | mod printing { |
| 466 | use super::*; |
| 467 | use quote::{Tokens, ToTokens}; |
| 468 | |
| 469 | |
| 470 | impl<T, D> ToTokens for Delimited<T, D> |
| 471 | where T: ToTokens, |
| 472 | D: ToTokens, |
| 473 | { |
| 474 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 475 | tokens.append_all(self.iter()) |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | impl<T, D> ToTokens for Element<T, D> |
| 480 | where T: ToTokens, |
| 481 | D: ToTokens, |
| 482 | { |
| 483 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 484 | match *self { |
| 485 | Element::Delimited(ref a, ref b) => { |
| 486 | a.to_tokens(tokens); |
| 487 | b.to_tokens(tokens); |
| 488 | } |
| 489 | Element::End(ref a) => a.to_tokens(tokens), |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |