David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 1 | //! A punctuated sequence of syntax tree nodes separated by punctuation. |
| 2 | //! |
| 3 | //! Lots of things in Rust are punctuated sequences. |
| 4 | //! |
| 5 | //! - The fields of a struct are `Punctuated<Field, Token![,]>`. |
| 6 | //! - The segments of a path are `Punctuated<PathSegment, Token![::]>`. |
| 7 | //! - The bounds on a generic parameter are `Punctuated<TypeParamBound, Token![+]>`. |
| 8 | //! - The arguments to a function call are `Punctuated<Expr, Token![,]>`. |
| 9 | //! |
| 10 | //! This module provides a common representation for these punctuated sequences |
| 11 | //! in the form of the [`Punctuated<T, P>`] type. We store a vector of pairs of |
| 12 | //! syntax tree node + punctuation, where every node in the sequence is followed |
| 13 | //! by punctuation except for possibly the final one. |
| 14 | //! |
| 15 | //! [`Punctuated<T, P>`]: struct.Punctuated.html |
| 16 | //! |
| 17 | //! ```text |
| 18 | //! a_function_call(arg1, arg2, arg3); |
| 19 | //! ^^^^^ ~~~~~ ^^^^ |
| 20 | //! ``` |
| 21 | |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 22 | #[cfg(feature = "extra-traits")] |
| 23 | use std::fmt::{self, Debug}; |
David Tolnay | 6c0a609 | 2018-03-31 22:47:39 +0200 | [diff] [blame] | 24 | #[cfg(any(feature = "full", feature = "derive"))] |
| 25 | use std::iter; |
| 26 | use std::iter::FromIterator; |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 27 | use std::ops::{Index, IndexMut}; |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 28 | use std::option; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 29 | use std::slice; |
| 30 | use std::vec; |
| 31 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 32 | #[cfg(feature = "parsing")] |
David Tolnay | ecf0fbc | 2018-08-30 18:28:33 -0700 | [diff] [blame] | 33 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 34 | #[cfg(any(feature = "full", feature = "derive"))] |
| 35 | use private; |
David Tolnay | 52619f6 | 2018-08-31 09:30:01 -0700 | [diff] [blame] | 36 | #[cfg(feature = "parsing")] |
| 37 | use token::Token; |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 38 | |
| 39 | /// A punctuated sequence of syntax tree nodes of type `T` separated by |
| 40 | /// punctuation of type `P`. |
| 41 | /// |
| 42 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 43 | /// |
| 44 | /// [module documentation]: index.html |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 45 | #[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 46 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 47 | pub struct Punctuated<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 48 | inner: Vec<(T, P)>, |
| 49 | last: Option<Box<T>>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 50 | } |
| 51 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 52 | impl<T, P> Punctuated<T, P> { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 53 | /// Creates an empty punctuated sequence. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 54 | pub fn new() -> Punctuated<T, P> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 55 | Punctuated { |
| 56 | inner: Vec::new(), |
| 57 | last: None, |
| 58 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 59 | } |
| 60 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 61 | /// Determines whether this punctuated sequence is empty, meaning it |
| 62 | /// contains no syntax tree nodes or punctuation. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 63 | pub fn is_empty(&self) -> bool { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 64 | self.inner.len() == 0 && self.last.is_none() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 65 | } |
| 66 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 67 | /// Returns the number of syntax tree nodes in this punctuated sequence. |
| 68 | /// |
| 69 | /// This is the number of nodes of type `T`, not counting the punctuation of |
| 70 | /// type `P`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 71 | pub fn len(&self) -> usize { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 72 | self.inner.len() + if self.last.is_some() { 1 } else { 0 } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 73 | } |
| 74 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 75 | /// Borrows the first punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 76 | pub fn first(&self) -> Option<Pair<&T, &P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 77 | self.pairs().next() |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 78 | } |
| 79 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 80 | /// Borrows the last punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 81 | pub fn last(&self) -> Option<Pair<&T, &P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 82 | if self.last.is_some() { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 83 | self.last.as_ref().map(|t| Pair::End(t.as_ref())) |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 84 | } else { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 85 | self.inner |
| 86 | .last() |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 87 | .map(|&(ref t, ref d)| Pair::Punctuated(t, d)) |
| 88 | } |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 89 | } |
| 90 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 91 | /// Mutably borrows the last punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 92 | pub fn last_mut(&mut self) -> Option<Pair<&mut T, &mut P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 93 | if self.last.is_some() { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 94 | self.last.as_mut().map(|t| Pair::End(t.as_mut())) |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 95 | } else { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 96 | self.inner |
| 97 | .last_mut() |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 98 | .map(|&mut (ref mut t, ref mut d)| Pair::Punctuated(t, d)) |
| 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(), |
David Tolnay | 124e50b | 2019-02-28 23:53:36 -0800 | [diff] [blame] | 107 | last: self.last.as_ref().map(Box::as_ref).into_iter(), |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 108 | }), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 109 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 110 | } |
| 111 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 112 | /// Returns an iterator over mutably borrowed syntax tree nodes of type |
| 113 | /// `&mut T`. |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 114 | pub fn iter_mut(&mut self) -> IterMut<T> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 115 | IterMut { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 116 | inner: Box::new(PrivateIterMut { |
| 117 | inner: self.inner.iter_mut(), |
David Tolnay | 124e50b | 2019-02-28 23:53:36 -0800 | [diff] [blame] | 118 | last: self.last.as_mut().map(Box::as_mut).into_iter(), |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 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(), |
David Tolnay | 124e50b | 2019-02-28 23:53:36 -0800 | [diff] [blame] | 128 | last: self.last.as_ref().map(Box::as_ref).into_iter(), |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 132 | /// Returns an iterator over the contents of this sequence as mutably |
| 133 | /// borrowed punctuated pairs. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 134 | pub fn pairs_mut(&mut self) -> PairsMut<T, P> { |
| 135 | PairsMut { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 136 | inner: self.inner.iter_mut(), |
David Tolnay | 124e50b | 2019-02-28 23:53:36 -0800 | [diff] [blame] | 137 | last: self.last.as_mut().map(Box::as_mut).into_iter(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 138 | } |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 139 | } |
| 140 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 141 | /// Returns an iterator over the contents of this sequence as owned |
| 142 | /// punctuated pairs. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 143 | pub fn into_pairs(self) -> IntoPairs<T, P> { |
| 144 | IntoPairs { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 145 | inner: self.inner.into_iter(), |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 146 | last: self.last.map(|t| *t).into_iter(), |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 150 | /// Appends a syntax tree node onto the end of this punctuated sequence. The |
| 151 | /// sequence must previously have a trailing punctuation. |
| 152 | /// |
| 153 | /// Use [`push`] instead if the punctuated sequence may or may not already |
| 154 | /// have trailing punctuation. |
| 155 | /// |
| 156 | /// [`push`]: #method.push |
| 157 | /// |
| 158 | /// # Panics |
| 159 | /// |
| 160 | /// Panics if the sequence does not already have a trailing punctuation when |
| 161 | /// this method is called. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 162 | pub fn push_value(&mut self, value: T) { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 163 | assert!(self.empty_or_trailing()); |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 164 | self.last = Some(Box::new(value)); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 165 | } |
| 166 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 167 | /// Appends a trailing punctuation onto the end of this punctuated sequence. |
| 168 | /// The sequence must be non-empty and must not already have trailing |
| 169 | /// punctuation. |
| 170 | /// |
| 171 | /// # Panics |
| 172 | /// |
| 173 | /// Panics if the sequence is empty or already has a trailing punctuation. |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 174 | pub fn push_punct(&mut self, punctuation: P) { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 175 | assert!(self.last.is_some()); |
David Tolnay | 0316308 | 2018-04-11 12:10:18 -0700 | [diff] [blame] | 176 | let last = self.last.take().unwrap(); |
| 177 | self.inner.push((*last, punctuation)); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 178 | } |
| 179 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 180 | /// Removes the last punctuated pair from this sequence, or `None` if the |
| 181 | /// sequence is empty. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 182 | pub fn pop(&mut self) -> Option<Pair<T, P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 183 | if self.last.is_some() { |
David Tolnay | 0316308 | 2018-04-11 12:10:18 -0700 | [diff] [blame] | 184 | self.last.take().map(|t| Pair::End(*t)) |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 185 | } else { |
| 186 | self.inner.pop().map(|(t, d)| Pair::Punctuated(t, d)) |
| 187 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 188 | } |
| 189 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 190 | /// Determines whether this punctuated sequence ends with a trailing |
| 191 | /// punctuation. |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 192 | pub fn trailing_punct(&self) -> bool { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 193 | self.last.is_none() && !self.is_empty() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 194 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 195 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 196 | /// Returns true if either this `Punctuated` is empty, or it has a trailing |
| 197 | /// punctuation. |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 198 | /// |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 199 | /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 200 | pub fn empty_or_trailing(&self) -> bool { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 201 | self.last.is_none() |
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 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 204 | /// Appends a syntax tree node onto the end of this punctuated sequence. |
| 205 | /// |
| 206 | /// If there is not a trailing punctuation in this sequence when this method |
| 207 | /// is called, the default value of punctuation type `P` is inserted before |
| 208 | /// the given value of type `T`. |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 209 | pub fn push(&mut self, value: T) |
| 210 | where |
| 211 | P: Default, |
| 212 | { |
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. |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 225 | pub fn insert(&mut self, index: usize, value: T) |
| 226 | where |
| 227 | P: Default, |
| 228 | { |
David Tolnay | b77d180 | 2018-01-11 16:18:35 -0800 | [diff] [blame] | 229 | assert!(index <= self.len()); |
| 230 | |
| 231 | if index == self.len() { |
| 232 | self.push(value); |
| 233 | } else { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 234 | self.inner.insert(index, (value, Default::default())); |
David Tolnay | b77d180 | 2018-01-11 16:18:35 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 237 | |
David Tolnay | 0f2a4ac | 2018-09-01 12:44:25 -0700 | [diff] [blame] | 238 | /// Parses zero or more occurrences of `T` separated by punctuation of type |
| 239 | /// `P`, with optional trailing punctuation. |
| 240 | /// |
| 241 | /// Parsing continues until the end of this parse stream. The entire content |
| 242 | /// of this parse stream must consist of `T` and `P`. |
David Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 243 | /// |
| 244 | /// *This function is available if Syn is built with the `"parsing"` |
| 245 | /// feature.* |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 246 | #[cfg(feature = "parsing")] |
| 247 | pub fn parse_terminated(input: ParseStream) -> Result<Self> |
| 248 | where |
| 249 | T: Parse, |
| 250 | P: Parse, |
| 251 | { |
| 252 | Self::parse_terminated_with(input, T::parse) |
| 253 | } |
| 254 | |
David Tolnay | 0f2a4ac | 2018-09-01 12:44:25 -0700 | [diff] [blame] | 255 | /// Parses zero or more occurrences of `T` using the given parse function, |
| 256 | /// separated by punctuation of type `P`, with optional trailing |
| 257 | /// punctuation. |
| 258 | /// |
| 259 | /// Like [`parse_terminated`], the entire content of this stream is expected |
| 260 | /// to be parsed. |
| 261 | /// |
| 262 | /// [`parse_terminated`]: #method.parse_terminated |
David Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 263 | /// |
| 264 | /// *This function is available if Syn is built with the `"parsing"` |
| 265 | /// feature.* |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 266 | #[cfg(feature = "parsing")] |
| 267 | pub fn parse_terminated_with( |
| 268 | input: ParseStream, |
| 269 | parser: fn(ParseStream) -> Result<T>, |
| 270 | ) -> Result<Self> |
| 271 | where |
| 272 | P: Parse, |
| 273 | { |
| 274 | let mut punctuated = Punctuated::new(); |
| 275 | |
| 276 | loop { |
| 277 | if input.is_empty() { |
| 278 | break; |
| 279 | } |
| 280 | let value = parser(input)?; |
| 281 | punctuated.push_value(value); |
| 282 | if input.is_empty() { |
| 283 | break; |
| 284 | } |
| 285 | let punct = input.parse()?; |
| 286 | punctuated.push_punct(punct); |
| 287 | } |
| 288 | |
| 289 | Ok(punctuated) |
| 290 | } |
| 291 | |
David Tolnay | 0f2a4ac | 2018-09-01 12:44:25 -0700 | [diff] [blame] | 292 | /// Parses one or more occurrences of `T` separated by punctuation of type |
| 293 | /// `P`, not accepting trailing punctuation. |
| 294 | /// |
| 295 | /// Parsing continues as long as punctuation `P` is present at the head of |
| 296 | /// the stream. This method returns upon parsing a `T` and observing that it |
| 297 | /// is not followed by a `P`, even if there are remaining tokens in the |
| 298 | /// stream. |
David Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 299 | /// |
| 300 | /// *This function is available if Syn is built with the `"parsing"` |
| 301 | /// feature.* |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 302 | #[cfg(feature = "parsing")] |
| 303 | pub fn parse_separated_nonempty(input: ParseStream) -> Result<Self> |
| 304 | where |
| 305 | T: Parse, |
| 306 | P: Token + Parse, |
| 307 | { |
| 308 | Self::parse_separated_nonempty_with(input, T::parse) |
| 309 | } |
| 310 | |
David Tolnay | 0f2a4ac | 2018-09-01 12:44:25 -0700 | [diff] [blame] | 311 | /// Parses one or more occurrences of `T` using the given parse function, |
| 312 | /// separated by punctuation of type `P`, not accepting trailing |
| 313 | /// punctuation. |
| 314 | /// |
| 315 | /// Like [`parse_separated_nonempty`], may complete early without parsing |
| 316 | /// the entire content of this stream. |
| 317 | /// |
| 318 | /// [`parse_separated_nonempty`]: #method.parse_separated_nonempty |
David Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 319 | /// |
| 320 | /// *This function is available if Syn is built with the `"parsing"` |
| 321 | /// feature.* |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 322 | #[cfg(feature = "parsing")] |
| 323 | pub fn parse_separated_nonempty_with( |
| 324 | input: ParseStream, |
| 325 | parser: fn(ParseStream) -> Result<T>, |
| 326 | ) -> Result<Self> |
| 327 | where |
| 328 | P: Token + Parse, |
| 329 | { |
| 330 | let mut punctuated = Punctuated::new(); |
| 331 | |
| 332 | loop { |
| 333 | let value = parser(input)?; |
| 334 | punctuated.push_value(value); |
| 335 | if !P::peek(input.cursor()) { |
| 336 | break; |
| 337 | } |
| 338 | let punct = input.parse()?; |
| 339 | punctuated.push_punct(punct); |
| 340 | } |
| 341 | |
| 342 | Ok(punctuated) |
| 343 | } |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 346 | #[cfg(feature = "extra-traits")] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 347 | impl<T: Debug, P: Debug> Debug for Punctuated<T, P> { |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 348 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 349 | let mut list = f.debug_list(); |
David Tolnay | 0c18c51 | 2018-07-22 10:39:10 -0700 | [diff] [blame] | 350 | for &(ref t, ref p) in &self.inner { |
| 351 | list.entry(t); |
| 352 | list.entry(p); |
| 353 | } |
| 354 | if let Some(ref last) = self.last { |
| 355 | list.entry(last); |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 356 | } |
| 357 | list.finish() |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
David Tolnay | 9ef24bc | 2018-01-09 10:43:55 -0800 | [diff] [blame] | 361 | impl<T, P> FromIterator<T> for Punctuated<T, P> |
| 362 | where |
| 363 | P: Default, |
| 364 | { |
| 365 | fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self { |
| 366 | let mut ret = Punctuated::new(); |
| 367 | ret.extend(i); |
| 368 | ret |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | impl<T, P> Extend<T> for Punctuated<T, P> |
| 373 | where |
| 374 | P: Default, |
| 375 | { |
| 376 | fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) { |
| 377 | for value in i { |
| 378 | self.push(value); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 383 | impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> { |
| 384 | fn from_iter<I: IntoIterator<Item = Pair<T, P>>>(i: I) -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 385 | let mut ret = Punctuated::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 386 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 387 | ret |
| 388 | } |
| 389 | } |
| 390 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 391 | impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> { |
| 392 | fn extend<I: IntoIterator<Item = Pair<T, P>>>(&mut self, i: I) { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 393 | assert!(self.empty_or_trailing()); |
| 394 | let mut nomore = false; |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 395 | for pair in i { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 396 | if nomore { |
| 397 | panic!("Punctuated extended with items after a Pair::End"); |
| 398 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 399 | match pair { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 400 | Pair::Punctuated(a, b) => self.inner.push((a, b)), |
| 401 | Pair::End(a) => { |
| 402 | self.last = Some(Box::new(a)); |
| 403 | nomore = true; |
| 404 | } |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 410 | impl<T, P> IntoIterator for Punctuated<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 411 | type Item = T; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 412 | type IntoIter = IntoIter<T, P>; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 413 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 414 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 415 | IntoIter { |
| 416 | inner: self.inner.into_iter(), |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 417 | last: self.last.map(|t| *t).into_iter(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 418 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 422 | impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> { |
| 423 | type Item = &'a T; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 424 | type IntoIter = Iter<'a, T>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 425 | |
| 426 | fn into_iter(self) -> Self::IntoIter { |
| 427 | Punctuated::iter(self) |
| 428 | } |
| 429 | } |
| 430 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 431 | impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> { |
| 432 | type Item = &'a mut T; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 433 | type IntoIter = IterMut<'a, T>; |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 434 | |
| 435 | fn into_iter(self) -> Self::IntoIter { |
| 436 | Punctuated::iter_mut(self) |
| 437 | } |
| 438 | } |
| 439 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 440 | impl<T, P> Default for Punctuated<T, P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 441 | fn default() -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 442 | Punctuated::new() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 446 | /// An iterator over borrowed pairs of type `Pair<&T, &P>`. |
| 447 | /// |
| 448 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 449 | /// |
| 450 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 451 | pub struct Pairs<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 452 | inner: slice::Iter<'a, (T, P)>, |
| 453 | last: option::IntoIter<&'a T>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 454 | } |
| 455 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 456 | impl<'a, T, P> Iterator for Pairs<'a, T, P> { |
| 457 | type Item = Pair<&'a T, &'a P>; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 458 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 459 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 460 | self.inner |
| 461 | .next() |
| 462 | .map(|&(ref t, ref p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame] | 463 | .or_else(|| self.last.next().map(Pair::End)) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 467 | impl<'a, T, P> ExactSizeIterator for Pairs<'a, T, P> { |
| 468 | fn len(&self) -> usize { |
| 469 | self.inner.len() + self.last.len() |
| 470 | } |
| 471 | } |
| 472 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 473 | /// An iterator over mutably borrowed pairs of type `Pair<&mut T, &mut P>`. |
| 474 | /// |
| 475 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 476 | /// |
| 477 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 478 | pub struct PairsMut<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 479 | inner: slice::IterMut<'a, (T, P)>, |
| 480 | last: option::IntoIter<&'a mut T>, |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 481 | } |
| 482 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 483 | impl<'a, T, P> Iterator for PairsMut<'a, T, P> { |
| 484 | type Item = Pair<&'a mut T, &'a mut P>; |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 485 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 486 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 487 | self.inner |
| 488 | .next() |
| 489 | .map(|&mut (ref mut t, ref mut p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame] | 490 | .or_else(|| self.last.next().map(Pair::End)) |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 494 | impl<'a, T, P> ExactSizeIterator for PairsMut<'a, T, P> { |
| 495 | fn len(&self) -> usize { |
| 496 | self.inner.len() + self.last.len() |
| 497 | } |
| 498 | } |
| 499 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 500 | /// An iterator over owned pairs of type `Pair<T, P>`. |
| 501 | /// |
| 502 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 503 | /// |
| 504 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 505 | pub struct IntoPairs<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 506 | inner: vec::IntoIter<(T, P)>, |
| 507 | last: option::IntoIter<T>, |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 508 | } |
| 509 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 510 | impl<T, P> Iterator for IntoPairs<T, P> { |
| 511 | type Item = Pair<T, P>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 512 | |
| 513 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 514 | self.inner |
| 515 | .next() |
| 516 | .map(|(t, p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame] | 517 | .or_else(|| self.last.next().map(Pair::End)) |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 521 | impl<T, P> ExactSizeIterator for IntoPairs<T, P> { |
| 522 | fn len(&self) -> usize { |
| 523 | self.inner.len() + self.last.len() |
| 524 | } |
| 525 | } |
| 526 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 527 | /// An iterator over owned values of type `T`. |
| 528 | /// |
| 529 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 530 | /// |
| 531 | /// [module documentation]: index.html |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 532 | pub struct IntoIter<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 533 | inner: vec::IntoIter<(T, P)>, |
| 534 | last: option::IntoIter<T>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 535 | } |
| 536 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 537 | impl<T, P> Iterator for IntoIter<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 538 | type Item = T; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 539 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 540 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 541 | self.inner |
| 542 | .next() |
| 543 | .map(|pair| pair.0) |
| 544 | .or_else(|| self.last.next()) |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 548 | impl<T, P> ExactSizeIterator for IntoIter<T, P> { |
| 549 | fn len(&self) -> usize { |
| 550 | self.inner.len() + self.last.len() |
| 551 | } |
| 552 | } |
| 553 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 554 | /// An iterator over borrowed values of type `&T`. |
| 555 | /// |
| 556 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 557 | /// |
| 558 | /// [module documentation]: index.html |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 559 | pub struct Iter<'a, T: 'a> { |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 560 | inner: Box<ExactSizeIterator<Item = &'a T> + 'a>, |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | struct PrivateIter<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 564 | inner: slice::Iter<'a, (T, P)>, |
| 565 | last: option::IntoIter<&'a T>, |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 566 | } |
| 567 | |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 568 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 569 | impl private { |
| 570 | pub fn empty_punctuated_iter<'a, T>() -> Iter<'a, T> { |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 571 | Iter { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 572 | inner: Box::new(iter::empty()), |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 577 | impl<'a, T> Iterator for Iter<'a, T> { |
| 578 | type Item = &'a T; |
| 579 | |
| 580 | fn next(&mut self) -> Option<Self::Item> { |
| 581 | self.inner.next() |
| 582 | } |
| 583 | } |
| 584 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 585 | impl<'a, T> ExactSizeIterator for Iter<'a, T> { |
| 586 | fn len(&self) -> usize { |
| 587 | self.inner.len() |
| 588 | } |
| 589 | } |
| 590 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 591 | impl<'a, T, P> Iterator for PrivateIter<'a, T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 592 | type Item = &'a T; |
| 593 | |
| 594 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 595 | self.inner |
| 596 | .next() |
| 597 | .map(|pair| &pair.0) |
| 598 | .or_else(|| self.last.next()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 602 | impl<'a, T, P> ExactSizeIterator for PrivateIter<'a, T, P> { |
| 603 | fn len(&self) -> usize { |
| 604 | self.inner.len() + self.last.len() |
| 605 | } |
| 606 | } |
| 607 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 608 | /// An iterator over mutably borrowed values of type `&mut T`. |
| 609 | /// |
| 610 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 611 | /// |
| 612 | /// [module documentation]: index.html |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 613 | pub struct IterMut<'a, T: 'a> { |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 614 | inner: Box<ExactSizeIterator<Item = &'a mut T> + 'a>, |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | struct PrivateIterMut<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 618 | inner: slice::IterMut<'a, (T, P)>, |
| 619 | last: option::IntoIter<&'a mut T>, |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Michael Bradshaw | 0b13ae6 | 2018-08-02 23:43:15 -0600 | [diff] [blame] | 622 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 623 | impl private { |
| 624 | pub fn empty_punctuated_iter_mut<'a, T>() -> IterMut<'a, T> { |
Michael Bradshaw | 0b13ae6 | 2018-08-02 23:43:15 -0600 | [diff] [blame] | 625 | IterMut { |
| 626 | inner: Box::new(iter::empty()), |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 631 | impl<'a, T> Iterator for IterMut<'a, T> { |
| 632 | type Item = &'a mut T; |
| 633 | |
| 634 | fn next(&mut self) -> Option<Self::Item> { |
| 635 | self.inner.next() |
| 636 | } |
| 637 | } |
| 638 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 639 | impl<'a, T> ExactSizeIterator for IterMut<'a, T> { |
| 640 | fn len(&self) -> usize { |
| 641 | self.inner.len() |
| 642 | } |
| 643 | } |
| 644 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 645 | impl<'a, T, P> Iterator for PrivateIterMut<'a, T, P> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 646 | type Item = &'a mut T; |
| 647 | |
| 648 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 649 | self.inner |
| 650 | .next() |
| 651 | .map(|pair| &mut pair.0) |
| 652 | .or_else(|| self.last.next()) |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 656 | impl<'a, T, P> ExactSizeIterator for PrivateIterMut<'a, T, P> { |
| 657 | fn len(&self) -> usize { |
| 658 | self.inner.len() + self.last.len() |
| 659 | } |
| 660 | } |
| 661 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 662 | /// A single syntax tree node of type `T` followed by its trailing punctuation |
| 663 | /// of type `P` if any. |
| 664 | /// |
| 665 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 666 | /// |
| 667 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 668 | pub enum Pair<T, P> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 669 | Punctuated(T, P), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 670 | End(T), |
| 671 | } |
| 672 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 673 | impl<T, P> Pair<T, P> { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 674 | /// Extracts the syntax tree node from this punctuated pair, discarding the |
| 675 | /// following punctuation. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 676 | pub fn into_value(self) -> T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 677 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 678 | Pair::Punctuated(t, _) | Pair::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 682 | /// Borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 683 | pub fn value(&self) -> &T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 684 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 685 | Pair::Punctuated(ref t, _) | Pair::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 689 | /// Mutably borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 690 | pub fn value_mut(&mut self) -> &mut T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 691 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 692 | Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 696 | /// Borrows the punctuation from this punctuated pair, unless this pair is |
| 697 | /// the final one and there is no trailing punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 698 | pub fn punct(&self) -> Option<&P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 699 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 700 | Pair::Punctuated(_, ref d) => Some(d), |
| 701 | Pair::End(_) => None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 702 | } |
| 703 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 704 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 705 | /// Creates a punctuated pair out of a syntax tree node and an optional |
| 706 | /// following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 707 | pub fn new(t: T, d: Option<P>) -> Self { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 708 | match d { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 709 | Some(d) => Pair::Punctuated(t, d), |
| 710 | None => Pair::End(t), |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 714 | /// Produces this punctuated pair as a tuple of syntax tree node and |
| 715 | /// optional following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 716 | pub fn into_tuple(self) -> (T, Option<P>) { |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 717 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 718 | Pair::Punctuated(t, d) => (t, Some(d)), |
| 719 | Pair::End(t) => (t, None), |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 720 | } |
| 721 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 722 | } |
| 723 | |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 724 | impl<T, P> Index<usize> for Punctuated<T, P> { |
| 725 | type Output = T; |
| 726 | |
| 727 | fn index(&self, index: usize) -> &Self::Output { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 728 | if index == self.len() - 1 { |
| 729 | match self.last { |
| 730 | Some(ref t) => t, |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 731 | None => &self.inner[index].0, |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 732 | } |
| 733 | } else { |
| 734 | &self.inner[index].0 |
| 735 | } |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | impl<T, P> IndexMut<usize> for Punctuated<T, P> { |
| 740 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 741 | if index == self.len() - 1 { |
| 742 | match self.last { |
| 743 | Some(ref mut t) => t, |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 744 | None => &mut self.inner[index].0, |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 745 | } |
| 746 | } else { |
| 747 | &mut self.inner[index].0 |
| 748 | } |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 752 | #[cfg(feature = "printing")] |
| 753 | mod printing { |
| 754 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 755 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 756 | use quote::{ToTokens, TokenStreamExt}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 757 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 758 | impl<T, P> ToTokens for Punctuated<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 759 | where |
| 760 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 761 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 762 | { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 763 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 764 | tokens.append_all(self.pairs()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 768 | impl<T, P> ToTokens for Pair<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 769 | where |
| 770 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 771 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 772 | { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 773 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 774 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 775 | Pair::Punctuated(ref a, ref b) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 776 | a.to_tokens(tokens); |
| 777 | b.to_tokens(tokens); |
| 778 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 779 | Pair::End(ref a) => a.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |