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