David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 9 | //! A punctuated sequence of syntax tree nodes separated by punctuation. |
| 10 | //! |
| 11 | //! Lots of things in Rust are punctuated sequences. |
| 12 | //! |
| 13 | //! - The fields of a struct are `Punctuated<Field, Token![,]>`. |
| 14 | //! - The segments of a path are `Punctuated<PathSegment, Token![::]>`. |
| 15 | //! - The bounds on a generic parameter are `Punctuated<TypeParamBound, Token![+]>`. |
| 16 | //! - The arguments to a function call are `Punctuated<Expr, Token![,]>`. |
| 17 | //! |
| 18 | //! This module provides a common representation for these punctuated sequences |
| 19 | //! in the form of the [`Punctuated<T, P>`] type. We store a vector of pairs of |
| 20 | //! syntax tree node + punctuation, where every node in the sequence is followed |
| 21 | //! by punctuation except for possibly the final one. |
| 22 | //! |
| 23 | //! [`Punctuated<T, P>`]: struct.Punctuated.html |
| 24 | //! |
| 25 | //! ```text |
| 26 | //! a_function_call(arg1, arg2, arg3); |
| 27 | //! ^^^^^ ~~~~~ ^^^^ |
| 28 | //! ``` |
| 29 | |
David Tolnay | 6c0a609 | 2018-03-31 22:47:39 +0200 | [diff] [blame] | 30 | #[cfg(any(feature = "full", feature = "derive"))] |
| 31 | use std::iter; |
| 32 | use std::iter::FromIterator; |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 33 | use std::ops::{Index, IndexMut}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 34 | use std::slice; |
| 35 | use std::vec; |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 36 | #[cfg(feature = "extra-traits")] |
| 37 | use std::fmt::{self, Debug}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 38 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 39 | #[cfg(feature = "parsing")] |
| 40 | use synom::{Synom, PResult}; |
| 41 | #[cfg(feature = "parsing")] |
| 42 | use buffer::Cursor; |
| 43 | #[cfg(feature = "parsing")] |
| 44 | use parse_error; |
| 45 | |
| 46 | /// A punctuated sequence of syntax tree nodes of type `T` separated by |
| 47 | /// punctuation of type `P`. |
| 48 | /// |
| 49 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 50 | /// |
| 51 | /// [module documentation]: index.html |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 52 | #[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 53 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 54 | pub struct Punctuated<T, P> { |
| 55 | inner: Vec<(T, Option<P>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 56 | } |
| 57 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 58 | impl<T, P> Punctuated<T, P> { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 59 | /// Creates an empty punctuated sequence. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 60 | pub fn new() -> Punctuated<T, P> { |
| 61 | Punctuated { inner: Vec::new() } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 62 | } |
| 63 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 64 | /// Determines whether this punctuated sequence is empty, meaning it |
| 65 | /// contains no syntax tree nodes or punctuation. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 66 | pub fn is_empty(&self) -> bool { |
| 67 | self.inner.len() == 0 |
| 68 | } |
| 69 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 70 | /// Returns the number of syntax tree nodes in this punctuated sequence. |
| 71 | /// |
| 72 | /// This is the number of nodes of type `T`, not counting the punctuation of |
| 73 | /// type `P`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 74 | pub fn len(&self) -> usize { |
| 75 | self.inner.len() |
| 76 | } |
| 77 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 78 | /// Borrows the first punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 79 | pub fn first(&self) -> Option<Pair<&T, &P>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 80 | self.inner.first().map(|&(ref t, ref d)| match *d { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 81 | Some(ref d) => Pair::Punctuated(t, d), |
| 82 | None => Pair::End(t), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 83 | }) |
| 84 | } |
| 85 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 86 | /// Borrows the last punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 87 | pub fn last(&self) -> Option<Pair<&T, &P>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 88 | self.inner.last().map(|&(ref t, ref d)| match *d { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 89 | Some(ref d) => Pair::Punctuated(t, d), |
| 90 | None => Pair::End(t), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 91 | }) |
| 92 | } |
| 93 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 94 | /// Mutably borrows the last punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 95 | pub fn last_mut(&mut self) -> Option<Pair<&mut T, &mut P>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 96 | self.inner |
| 97 | .last_mut() |
| 98 | .map(|&mut (ref mut t, ref mut d)| match *d { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 99 | Some(ref mut d) => Pair::Punctuated(t, d), |
| 100 | None => Pair::End(t), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 101 | }) |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 102 | } |
| 103 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 104 | /// Returns an iterator over borrowed syntax tree nodes of type `&T`. |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 105 | pub fn iter(&self) -> Iter<T> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 106 | Iter { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 107 | inner: Box::new(PrivateIter { |
| 108 | inner: self.inner.iter(), |
| 109 | }), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 110 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 111 | } |
| 112 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 113 | /// Returns an iterator over mutably borrowed syntax tree nodes of type |
| 114 | /// `&mut T`. |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 115 | pub fn iter_mut(&mut self) -> IterMut<T> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 116 | IterMut { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 117 | inner: Box::new(PrivateIterMut { |
| 118 | inner: self.inner.iter_mut(), |
| 119 | }), |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 123 | /// Returns an iterator over the contents of this sequence as borrowed |
| 124 | /// punctuated pairs. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 125 | pub fn pairs(&self) -> Pairs<T, P> { |
| 126 | Pairs { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 127 | inner: self.inner.iter(), |
| 128 | } |
| 129 | } |
| 130 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 131 | /// Returns an iterator over the contents of this sequence as mutably |
| 132 | /// borrowed punctuated pairs. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 133 | pub fn pairs_mut(&mut self) -> PairsMut<T, P> { |
| 134 | PairsMut { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 135 | inner: self.inner.iter_mut(), |
| 136 | } |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 137 | } |
| 138 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 139 | /// Returns an iterator over the contents of this sequence as owned |
| 140 | /// punctuated pairs. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 141 | pub fn into_pairs(self) -> IntoPairs<T, P> { |
| 142 | IntoPairs { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 143 | inner: self.inner.into_iter(), |
| 144 | } |
| 145 | } |
| 146 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 147 | /// Appends a syntax tree node onto the end of this punctuated sequence. The |
| 148 | /// sequence must previously have a trailing punctuation. |
| 149 | /// |
| 150 | /// Use [`push`] instead if the punctuated sequence may or may not already |
| 151 | /// have trailing punctuation. |
| 152 | /// |
| 153 | /// [`push`]: #method.push |
| 154 | /// |
| 155 | /// # Panics |
| 156 | /// |
| 157 | /// Panics if the sequence does not already have a trailing punctuation when |
| 158 | /// this method is called. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 159 | pub fn push_value(&mut self, value: T) { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 160 | assert!(self.empty_or_trailing()); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 161 | self.inner.push((value, None)); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 162 | } |
| 163 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 164 | /// Appends a trailing punctuation onto the end of this punctuated sequence. |
| 165 | /// The sequence must be non-empty and must not already have trailing |
| 166 | /// punctuation. |
| 167 | /// |
| 168 | /// # Panics |
| 169 | /// |
| 170 | /// Panics if the sequence is empty or already has a trailing punctuation. |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 171 | pub fn push_punct(&mut self, punctuation: P) { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 172 | assert!(!self.is_empty()); |
| 173 | let last = self.inner.last_mut().unwrap(); |
| 174 | assert!(last.1.is_none()); |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 175 | last.1 = Some(punctuation); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 176 | } |
| 177 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 178 | /// Removes the last punctuated pair from this sequence, or `None` if the |
| 179 | /// sequence is empty. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 180 | pub fn pop(&mut self) -> Option<Pair<T, P>> { |
| 181 | self.inner.pop().map(|(t, d)| Pair::new(t, d)) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 182 | } |
| 183 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 184 | /// Determines whether this punctuated sequence ends with a trailing |
| 185 | /// punctuation. |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 186 | pub fn trailing_punct(&self) -> bool { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 187 | self.inner |
| 188 | .last() |
| 189 | .map(|last| last.1.is_some()) |
| 190 | .unwrap_or(false) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 191 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 192 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 193 | /// Returns true if either this `Punctuated` is empty, or it has a trailing |
| 194 | /// punctuation. |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 195 | /// |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 196 | /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 197 | pub fn empty_or_trailing(&self) -> bool { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 198 | self.inner |
| 199 | .last() |
| 200 | .map(|last| last.1.is_some()) |
| 201 | .unwrap_or(true) |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 202 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 203 | } |
| 204 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 205 | impl<T, P> Punctuated<T, P> |
| 206 | where |
| 207 | P: Default, |
| 208 | { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 209 | /// Appends a syntax tree node onto the end of this punctuated sequence. |
| 210 | /// |
| 211 | /// If there is not a trailing punctuation in this sequence when this method |
| 212 | /// is called, the default value of punctuation type `P` is inserted before |
| 213 | /// the given value of type `T`. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 214 | pub fn push(&mut self, value: T) { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 215 | if !self.empty_or_trailing() { |
| 216 | self.push_punct(Default::default()); |
| 217 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 218 | self.push_value(value); |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 219 | } |
David Tolnay | b77d180 | 2018-01-11 16:18:35 -0800 | [diff] [blame] | 220 | |
| 221 | /// Inserts an element at position `index`. |
| 222 | /// |
| 223 | /// # Panics |
| 224 | /// |
| 225 | /// Panics if `index` is greater than the number of elements previously in |
| 226 | /// this punctuated sequence. |
| 227 | pub fn insert(&mut self, index: usize, value: T) { |
| 228 | assert!(index <= self.len()); |
| 229 | |
| 230 | if index == self.len() { |
| 231 | self.push(value); |
| 232 | } else { |
| 233 | self.inner.insert(index, (value, Some(Default::default()))); |
| 234 | } |
| 235 | } |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 238 | #[cfg(feature = "extra-traits")] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 239 | impl<T: Debug, P: Debug> Debug for Punctuated<T, P> { |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 240 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 241 | self.inner.fmt(f) |
| 242 | } |
| 243 | } |
| 244 | |
David Tolnay | 9ef24bc | 2018-01-09 10:43:55 -0800 | [diff] [blame] | 245 | impl<T, P> FromIterator<T> for Punctuated<T, P> |
| 246 | where |
| 247 | P: Default, |
| 248 | { |
| 249 | fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self { |
| 250 | let mut ret = Punctuated::new(); |
| 251 | ret.extend(i); |
| 252 | ret |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | impl<T, P> Extend<T> for Punctuated<T, P> |
| 257 | where |
| 258 | P: Default, |
| 259 | { |
| 260 | fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) { |
| 261 | for value in i { |
| 262 | self.push(value); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 267 | impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> { |
| 268 | fn from_iter<I: IntoIterator<Item = Pair<T, P>>>(i: I) -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 269 | let mut ret = Punctuated::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 270 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 271 | ret |
| 272 | } |
| 273 | } |
| 274 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 275 | impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> { |
| 276 | fn extend<I: IntoIterator<Item = Pair<T, P>>>(&mut self, i: I) { |
| 277 | for pair in i { |
| 278 | match pair { |
| 279 | Pair::Punctuated(a, b) => self.inner.push((a, Some(b))), |
| 280 | Pair::End(a) => self.inner.push((a, None)), |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 286 | impl<T, P> IntoIterator for Punctuated<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 287 | type Item = T; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 288 | type IntoIter = IntoIter<T, P>; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 289 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 290 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 291 | IntoIter { |
| 292 | inner: self.inner.into_iter(), |
| 293 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 297 | impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> { |
| 298 | type Item = &'a T; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 299 | type IntoIter = Iter<'a, T>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 300 | |
| 301 | fn into_iter(self) -> Self::IntoIter { |
| 302 | Punctuated::iter(self) |
| 303 | } |
| 304 | } |
| 305 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 306 | impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> { |
| 307 | type Item = &'a mut T; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 308 | type IntoIter = IterMut<'a, T>; |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 309 | |
| 310 | fn into_iter(self) -> Self::IntoIter { |
| 311 | Punctuated::iter_mut(self) |
| 312 | } |
| 313 | } |
| 314 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 315 | impl<T, P> Default for Punctuated<T, P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 316 | fn default() -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 317 | Punctuated::new() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 321 | /// An iterator over borrowed pairs of type `Pair<&T, &P>`. |
| 322 | /// |
| 323 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 324 | /// |
| 325 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 326 | pub struct Pairs<'a, T: 'a, P: 'a> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 327 | inner: slice::Iter<'a, (T, Option<P>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 328 | } |
| 329 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 330 | impl<'a, T, P> Iterator for Pairs<'a, T, P> { |
| 331 | type Item = Pair<&'a T, &'a P>; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 332 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 333 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 334 | self.inner.next().map(|pair| match pair.1 { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 335 | Some(ref p) => Pair::Punctuated(&pair.0, p), |
| 336 | None => Pair::End(&pair.0), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 337 | }) |
| 338 | } |
| 339 | } |
| 340 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 341 | /// An iterator over mutably borrowed pairs of type `Pair<&mut T, &mut P>`. |
| 342 | /// |
| 343 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 344 | /// |
| 345 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 346 | pub struct PairsMut<'a, T: 'a, P: 'a> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 347 | inner: slice::IterMut<'a, (T, Option<P>)>, |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 348 | } |
| 349 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 350 | impl<'a, T, P> Iterator for PairsMut<'a, T, P> { |
| 351 | type Item = Pair<&'a mut T, &'a mut P>; |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 352 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 353 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 354 | self.inner.next().map(|pair| match pair.1 { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 355 | Some(ref mut p) => Pair::Punctuated(&mut pair.0, p), |
| 356 | None => Pair::End(&mut pair.0), |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 357 | }) |
| 358 | } |
| 359 | } |
| 360 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 361 | /// An iterator over owned pairs of type `Pair<T, P>`. |
| 362 | /// |
| 363 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 364 | /// |
| 365 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 366 | pub struct IntoPairs<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 367 | inner: vec::IntoIter<(T, Option<P>)>, |
| 368 | } |
| 369 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 370 | impl<T, P> Iterator for IntoPairs<T, P> { |
| 371 | type Item = Pair<T, P>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 372 | |
| 373 | fn next(&mut self) -> Option<Self::Item> { |
| 374 | self.inner.next().map(|pair| match pair.1 { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 375 | Some(p) => Pair::Punctuated(pair.0, p), |
| 376 | None => Pair::End(pair.0), |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 377 | }) |
| 378 | } |
| 379 | } |
| 380 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 381 | /// An iterator over owned values of type `T`. |
| 382 | /// |
| 383 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 384 | /// |
| 385 | /// [module documentation]: index.html |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 386 | pub struct IntoIter<T, P> { |
| 387 | inner: vec::IntoIter<(T, Option<P>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 388 | } |
| 389 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 390 | impl<T, P> Iterator for IntoIter<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 391 | type Item = T; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 392 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 393 | fn next(&mut self) -> Option<Self::Item> { |
| 394 | self.inner.next().map(|pair| pair.0) |
| 395 | } |
| 396 | } |
| 397 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 398 | /// An iterator over borrowed values of type `&T`. |
| 399 | /// |
| 400 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 401 | /// |
| 402 | /// [module documentation]: index.html |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 403 | pub struct Iter<'a, T: 'a> { |
| 404 | inner: Box<Iterator<Item = &'a T> + 'a>, |
| 405 | } |
| 406 | |
| 407 | struct PrivateIter<'a, T: 'a, P: 'a> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 408 | inner: slice::Iter<'a, (T, Option<P>)>, |
| 409 | } |
| 410 | |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 411 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 412 | impl<'a, T> Iter<'a, T> { |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 413 | // Not public API. |
| 414 | #[doc(hidden)] |
| 415 | pub fn private_empty() -> Self { |
| 416 | Iter { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 417 | inner: Box::new(iter::empty()), |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 422 | impl<'a, T> Iterator for Iter<'a, T> { |
| 423 | type Item = &'a T; |
| 424 | |
| 425 | fn next(&mut self) -> Option<Self::Item> { |
| 426 | self.inner.next() |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | impl<'a, T, P> Iterator for PrivateIter<'a, T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 431 | type Item = &'a T; |
| 432 | |
| 433 | fn next(&mut self) -> Option<Self::Item> { |
| 434 | self.inner.next().map(|pair| &pair.0) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 438 | /// An iterator over mutably borrowed values of type `&mut T`. |
| 439 | /// |
| 440 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 441 | /// |
| 442 | /// [module documentation]: index.html |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 443 | pub struct IterMut<'a, T: 'a> { |
| 444 | inner: Box<Iterator<Item = &'a mut T> + 'a>, |
| 445 | } |
| 446 | |
| 447 | struct PrivateIterMut<'a, T: 'a, P: 'a> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 448 | inner: slice::IterMut<'a, (T, Option<P>)>, |
| 449 | } |
| 450 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 451 | impl<'a, T> Iterator for IterMut<'a, T> { |
| 452 | type Item = &'a mut T; |
| 453 | |
| 454 | fn next(&mut self) -> Option<Self::Item> { |
| 455 | self.inner.next() |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | impl<'a, T, P> Iterator for PrivateIterMut<'a, T, P> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 460 | type Item = &'a mut T; |
| 461 | |
| 462 | fn next(&mut self) -> Option<Self::Item> { |
| 463 | self.inner.next().map(|pair| &mut pair.0) |
| 464 | } |
| 465 | } |
| 466 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 467 | /// A single syntax tree node of type `T` followed by its trailing punctuation |
| 468 | /// of type `P` if any. |
| 469 | /// |
| 470 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 471 | /// |
| 472 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 473 | pub enum Pair<T, P> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 474 | Punctuated(T, P), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 475 | End(T), |
| 476 | } |
| 477 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 478 | impl<T, P> Pair<T, P> { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 479 | /// Extracts the syntax tree node from this punctuated pair, discarding the |
| 480 | /// following punctuation. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 481 | pub fn into_value(self) -> T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 482 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 483 | Pair::Punctuated(t, _) | Pair::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 487 | /// Borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 488 | pub fn value(&self) -> &T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 489 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 490 | Pair::Punctuated(ref t, _) | Pair::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 494 | /// Mutably borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 495 | pub fn value_mut(&mut self) -> &mut T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 496 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 497 | Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 501 | /// Borrows the punctuation from this punctuated pair, unless this pair is |
| 502 | /// the final one and there is no trailing punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 503 | pub fn punct(&self) -> Option<&P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 504 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 505 | Pair::Punctuated(_, ref d) => Some(d), |
| 506 | Pair::End(_) => None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 509 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 510 | /// Creates a punctuated pair out of a syntax tree node and an optional |
| 511 | /// following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 512 | pub fn new(t: T, d: Option<P>) -> Self { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 513 | match d { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 514 | Some(d) => Pair::Punctuated(t, d), |
| 515 | None => Pair::End(t), |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 519 | /// Produces this punctuated pair as a tuple of syntax tree node and |
| 520 | /// optional following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 521 | pub fn into_tuple(self) -> (T, Option<P>) { |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 522 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 523 | Pair::Punctuated(t, d) => (t, Some(d)), |
| 524 | Pair::End(t) => (t, None), |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 525 | } |
| 526 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 527 | } |
| 528 | |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 529 | impl<T, P> Index<usize> for Punctuated<T, P> { |
| 530 | type Output = T; |
| 531 | |
| 532 | fn index(&self, index: usize) -> &Self::Output { |
| 533 | &self.inner[index].0 |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | impl<T, P> IndexMut<usize> for Punctuated<T, P> { |
| 538 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 539 | &mut self.inner[index].0 |
| 540 | } |
| 541 | } |
| 542 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 543 | #[cfg(feature = "parsing")] |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 544 | impl<T, P> Punctuated<T, P> |
| 545 | where |
| 546 | T: Synom, |
| 547 | P: Synom, |
| 548 | { |
| 549 | /// Parse **zero or more** syntax tree nodes with punctuation in between and |
| 550 | /// **no trailing** punctuation. |
| 551 | pub fn parse_separated(input: Cursor) -> PResult<Self> { |
| 552 | Self::parse_separated_with(input, T::parse) |
| 553 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 554 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 555 | /// Parse **one or more** syntax tree nodes with punctuation in bewteen and |
| 556 | /// **no trailing** punctuation. |
| 557 | /// allowing trailing punctuation. |
| 558 | pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> { |
| 559 | Self::parse_separated_nonempty_with(input, T::parse) |
| 560 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 561 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 562 | /// Parse **zero or more** syntax tree nodes with punctuation in between and |
| 563 | /// **optional trailing** punctuation. |
| 564 | pub fn parse_terminated(input: Cursor) -> PResult<Self> { |
| 565 | Self::parse_terminated_with(input, T::parse) |
| 566 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 567 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 568 | /// Parse **one or more** syntax tree nodes with punctuation in between and |
| 569 | /// **optional trailing** punctuation. |
| 570 | pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> { |
| 571 | Self::parse_terminated_nonempty_with(input, T::parse) |
| 572 | } |
| 573 | } |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 574 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 575 | #[cfg(feature = "parsing")] |
| 576 | impl<T, P> Punctuated<T, P> |
| 577 | where |
| 578 | P: Synom, |
| 579 | { |
| 580 | /// Parse **zero or more** syntax tree nodes using the given parser with |
| 581 | /// punctuation in between and **no trailing** punctuation. |
| 582 | pub fn parse_separated_with( |
| 583 | input: Cursor, |
| 584 | parse: fn(Cursor) -> PResult<T>, |
| 585 | ) -> PResult<Self> { |
| 586 | Self::parse(input, parse, false) |
| 587 | } |
| 588 | |
| 589 | /// Parse **one or more** syntax tree nodes using the given parser with |
| 590 | /// punctuation in between and **no trailing** punctuation. |
| 591 | pub fn parse_separated_nonempty_with( |
| 592 | input: Cursor, |
| 593 | parse: fn(Cursor) -> PResult<T>, |
| 594 | ) -> PResult<Self> { |
| 595 | match Self::parse(input, parse, false) { |
| 596 | Ok((ref b, _)) if b.is_empty() => parse_error(), |
| 597 | other => other, |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 598 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 599 | } |
| 600 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 601 | /// Parse **zero or more** syntax tree nodes using the given parser with |
| 602 | /// punctuation in between and **optional trailing** punctuation. |
| 603 | pub fn parse_terminated_with( |
| 604 | input: Cursor, |
| 605 | parse: fn(Cursor) -> PResult<T>, |
| 606 | ) -> PResult<Self> { |
| 607 | Self::parse(input, parse, true) |
| 608 | } |
| 609 | |
| 610 | /// Parse **one or more** syntax tree nodes using the given parser with |
| 611 | /// punctuation in between and **optional trailing** punctuation. |
| 612 | pub fn parse_terminated_nonempty_with( |
| 613 | input: Cursor, |
| 614 | parse: fn(Cursor) -> PResult<T>, |
| 615 | ) -> PResult<Self> { |
| 616 | match Self::parse(input, parse, true) { |
| 617 | Ok((ref b, _)) if b.is_empty() => parse_error(), |
| 618 | other => other, |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 619 | } |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 620 | } |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 621 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 622 | fn parse( |
| 623 | mut input: Cursor, |
| 624 | parse: fn(Cursor) -> PResult<T>, |
| 625 | terminated: bool, |
| 626 | ) -> PResult<Self> { |
| 627 | let mut res = Punctuated::new(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 628 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 629 | // get the first element |
| 630 | match parse(input) { |
| 631 | Err(_) => Ok((res, input)), |
| 632 | Ok((o, i)) => { |
| 633 | if i == input { |
| 634 | return parse_error(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 635 | } |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 636 | input = i; |
| 637 | res.push_value(o); |
| 638 | |
| 639 | // get the separator first |
| 640 | while let Ok((s, i2)) = P::parse(input) { |
| 641 | if i2 == input { |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | // get the element next |
| 646 | if let Ok((o3, i3)) = parse(i2) { |
| 647 | if i3 == i2 { |
| 648 | break; |
| 649 | } |
| 650 | res.push_punct(s); |
| 651 | res.push_value(o3); |
| 652 | input = i3; |
| 653 | } else { |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | if terminated { |
| 658 | if let Ok((sep, after)) = P::parse(input) { |
| 659 | res.push_punct(sep); |
| 660 | input = after; |
| 661 | } |
| 662 | } |
| 663 | Ok((res, input)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 669 | #[cfg(feature = "printing")] |
| 670 | mod printing { |
| 671 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 672 | use quote::{ToTokens, Tokens}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 673 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 674 | impl<T, P> ToTokens for Punctuated<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 675 | where |
| 676 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 677 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 678 | { |
| 679 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 680 | tokens.append_all(self.pairs()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 684 | impl<T, P> ToTokens for Pair<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 685 | where |
| 686 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 687 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 688 | { |
| 689 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 690 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 691 | Pair::Punctuated(ref a, ref b) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 692 | a.to_tokens(tokens); |
| 693 | b.to_tokens(tokens); |
| 694 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 695 | Pair::End(ref a) => a.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | } |
| 699 | } |