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