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