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