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.last.is_some()); |
David Tolnay | 0316308 | 2018-04-11 12:10:18 -0700 | [diff] [blame] | 181 | let last = self.last.take().unwrap(); |
| 182 | self.inner.push((*last, punctuation)); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 183 | } |
| 184 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 185 | /// Removes the last punctuated pair from this sequence, or `None` if the |
| 186 | /// sequence is empty. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 187 | pub fn pop(&mut self) -> Option<Pair<T, P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 188 | if self.last.is_some() { |
David Tolnay | 0316308 | 2018-04-11 12:10:18 -0700 | [diff] [blame] | 189 | self.last.take().map(|t| Pair::End(*t)) |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 190 | } else { |
| 191 | self.inner.pop().map(|(t, d)| Pair::Punctuated(t, d)) |
| 192 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 193 | } |
| 194 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 195 | /// Determines whether this punctuated sequence ends with a trailing |
| 196 | /// punctuation. |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 197 | pub fn trailing_punct(&self) -> bool { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 198 | self.last.is_none() && !self.is_empty() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 199 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 200 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 201 | /// Returns true if either this `Punctuated` is empty, or it has a trailing |
| 202 | /// punctuation. |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 203 | /// |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 204 | /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 205 | pub fn empty_or_trailing(&self) -> bool { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 206 | self.last.is_none() |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 207 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 208 | } |
| 209 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 210 | impl<T, P> Punctuated<T, P> |
| 211 | where |
| 212 | P: Default, |
| 213 | { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 214 | /// Appends a syntax tree node onto the end of this punctuated sequence. |
| 215 | /// |
| 216 | /// If there is not a trailing punctuation in this sequence when this method |
| 217 | /// is called, the default value of punctuation type `P` is inserted before |
| 218 | /// the given value of type `T`. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 219 | pub fn push(&mut self, value: T) { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 220 | if !self.empty_or_trailing() { |
| 221 | self.push_punct(Default::default()); |
| 222 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 223 | self.push_value(value); |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 224 | } |
David Tolnay | b77d180 | 2018-01-11 16:18:35 -0800 | [diff] [blame] | 225 | |
| 226 | /// Inserts an element at position `index`. |
| 227 | /// |
| 228 | /// # Panics |
| 229 | /// |
| 230 | /// Panics if `index` is greater than the number of elements previously in |
| 231 | /// this punctuated sequence. |
| 232 | pub fn insert(&mut self, index: usize, value: T) { |
| 233 | assert!(index <= self.len()); |
| 234 | |
| 235 | if index == self.len() { |
| 236 | self.push(value); |
| 237 | } else { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 238 | self.inner.insert(index, (value, Default::default())); |
David Tolnay | b77d180 | 2018-01-11 16:18:35 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 243 | #[cfg(feature = "extra-traits")] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 244 | impl<T: Debug, P: Debug> Debug for Punctuated<T, P> { |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 245 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 246 | let mut list = f.debug_list(); |
| 247 | list.entries(&self.inner); |
| 248 | for t in self.last.iter() { |
| 249 | list.entry(&*t); |
| 250 | } |
| 251 | list.finish() |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
David Tolnay | 9ef24bc | 2018-01-09 10:43:55 -0800 | [diff] [blame] | 255 | impl<T, P> FromIterator<T> for Punctuated<T, P> |
| 256 | where |
| 257 | P: Default, |
| 258 | { |
| 259 | fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self { |
| 260 | let mut ret = Punctuated::new(); |
| 261 | ret.extend(i); |
| 262 | ret |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | impl<T, P> Extend<T> for Punctuated<T, P> |
| 267 | where |
| 268 | P: Default, |
| 269 | { |
| 270 | fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) { |
| 271 | for value in i { |
| 272 | self.push(value); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 277 | impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> { |
| 278 | fn from_iter<I: IntoIterator<Item = Pair<T, P>>>(i: I) -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 279 | let mut ret = Punctuated::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 280 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 281 | ret |
| 282 | } |
| 283 | } |
| 284 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 285 | impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> { |
| 286 | 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] | 287 | assert!(self.empty_or_trailing()); |
| 288 | let mut nomore = false; |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 289 | for pair in i { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 290 | if nomore { |
| 291 | panic!("Punctuated extended with items after a Pair::End"); |
| 292 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 293 | match pair { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 294 | Pair::Punctuated(a, b) => self.inner.push((a, b)), |
| 295 | Pair::End(a) => { |
| 296 | self.last = Some(Box::new(a)); |
| 297 | nomore = true; |
| 298 | } |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 304 | impl<T, P> IntoIterator for Punctuated<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 305 | type Item = T; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 306 | type IntoIter = IntoIter<T, P>; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 307 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 308 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 309 | IntoIter { |
| 310 | inner: self.inner.into_iter(), |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 311 | last: self.last.map(|t| *t).into_iter(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 312 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 316 | impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> { |
| 317 | type Item = &'a T; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 318 | type IntoIter = Iter<'a, T>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 319 | |
| 320 | fn into_iter(self) -> Self::IntoIter { |
| 321 | Punctuated::iter(self) |
| 322 | } |
| 323 | } |
| 324 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 325 | impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> { |
| 326 | type Item = &'a mut T; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 327 | type IntoIter = IterMut<'a, T>; |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 328 | |
| 329 | fn into_iter(self) -> Self::IntoIter { |
| 330 | Punctuated::iter_mut(self) |
| 331 | } |
| 332 | } |
| 333 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 334 | impl<T, P> Default for Punctuated<T, P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 335 | fn default() -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 336 | Punctuated::new() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 340 | /// An iterator over borrowed pairs of type `Pair<&T, &P>`. |
| 341 | /// |
| 342 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 343 | /// |
| 344 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 345 | pub struct Pairs<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 346 | inner: slice::Iter<'a, (T, P)>, |
| 347 | last: option::IntoIter<&'a T>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 348 | } |
| 349 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 350 | impl<'a, T, P> Iterator for Pairs<'a, T, P> { |
| 351 | type Item = Pair<&'a T, &'a P>; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 352 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 353 | fn next(&mut self) -> Option<Self::Item> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 354 | self.inner.next().map(|&(ref t, ref p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame^] | 355 | .or_else(|| self.last.next().map(Pair::End)) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 359 | /// An iterator over mutably borrowed pairs of type `Pair<&mut T, &mut P>`. |
| 360 | /// |
| 361 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 362 | /// |
| 363 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 364 | pub struct PairsMut<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 365 | inner: slice::IterMut<'a, (T, P)>, |
| 366 | last: option::IntoIter<&'a mut T>, |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 367 | } |
| 368 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 369 | impl<'a, T, P> Iterator for PairsMut<'a, T, P> { |
| 370 | type Item = Pair<&'a mut T, &'a mut P>; |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 371 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 372 | fn next(&mut self) -> Option<Self::Item> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 373 | self.inner.next().map(|&mut (ref mut t, ref mut p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame^] | 374 | .or_else(|| self.last.next().map(Pair::End)) |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 378 | /// An iterator over owned pairs of type `Pair<T, P>`. |
| 379 | /// |
| 380 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 381 | /// |
| 382 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 383 | pub struct IntoPairs<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 384 | inner: vec::IntoIter<(T, P)>, |
| 385 | last: option::IntoIter<T>, |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 386 | } |
| 387 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 388 | impl<T, P> Iterator for IntoPairs<T, P> { |
| 389 | type Item = Pair<T, P>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 390 | |
| 391 | fn next(&mut self) -> Option<Self::Item> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 392 | self.inner.next().map(|(t, p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame^] | 393 | .or_else(|| self.last.next().map(Pair::End)) |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 397 | /// An iterator over owned values of type `T`. |
| 398 | /// |
| 399 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 400 | /// |
| 401 | /// [module documentation]: index.html |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 402 | pub struct IntoIter<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 403 | inner: vec::IntoIter<(T, P)>, |
| 404 | last: option::IntoIter<T>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 405 | } |
| 406 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 407 | impl<T, P> Iterator for IntoIter<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 408 | type Item = T; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 409 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 410 | fn next(&mut self) -> Option<Self::Item> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 411 | self.inner.next().map(|pair| pair.0).or_else(|| self.last.next()) |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 415 | /// An iterator over borrowed values of type `&T`. |
| 416 | /// |
| 417 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 418 | /// |
| 419 | /// [module documentation]: index.html |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 420 | pub struct Iter<'a, T: 'a> { |
| 421 | inner: Box<Iterator<Item = &'a T> + 'a>, |
| 422 | } |
| 423 | |
| 424 | struct PrivateIter<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 425 | inner: slice::Iter<'a, (T, P)>, |
| 426 | last: option::IntoIter<&'a T>, |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 427 | } |
| 428 | |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 429 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 430 | impl<'a, T> Iter<'a, T> { |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 431 | // Not public API. |
| 432 | #[doc(hidden)] |
| 433 | pub fn private_empty() -> Self { |
| 434 | Iter { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 435 | inner: Box::new(iter::empty()), |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 440 | impl<'a, T> Iterator for Iter<'a, T> { |
| 441 | type Item = &'a T; |
| 442 | |
| 443 | fn next(&mut self) -> Option<Self::Item> { |
| 444 | self.inner.next() |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | impl<'a, T, P> Iterator for PrivateIter<'a, T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 449 | type Item = &'a T; |
| 450 | |
| 451 | fn next(&mut self) -> Option<Self::Item> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 452 | self.inner.next().map(|pair| &pair.0).or_else(|| self.last.next()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 456 | /// An iterator over mutably borrowed values of type `&mut T`. |
| 457 | /// |
| 458 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 459 | /// |
| 460 | /// [module documentation]: index.html |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 461 | pub struct IterMut<'a, T: 'a> { |
| 462 | inner: Box<Iterator<Item = &'a mut T> + 'a>, |
| 463 | } |
| 464 | |
| 465 | struct PrivateIterMut<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 466 | inner: slice::IterMut<'a, (T, P)>, |
| 467 | last: option::IntoIter<&'a mut T>, |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 468 | } |
| 469 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 470 | impl<'a, T> Iterator for IterMut<'a, T> { |
| 471 | type Item = &'a mut T; |
| 472 | |
| 473 | fn next(&mut self) -> Option<Self::Item> { |
| 474 | self.inner.next() |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | impl<'a, T, P> Iterator for PrivateIterMut<'a, T, P> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 479 | type Item = &'a mut T; |
| 480 | |
| 481 | fn next(&mut self) -> Option<Self::Item> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 482 | 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] | 483 | } |
| 484 | } |
| 485 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 486 | /// A single syntax tree node of type `T` followed by its trailing punctuation |
| 487 | /// of type `P` if any. |
| 488 | /// |
| 489 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 490 | /// |
| 491 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 492 | pub enum Pair<T, P> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 493 | Punctuated(T, P), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 494 | End(T), |
| 495 | } |
| 496 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 497 | impl<T, P> Pair<T, P> { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 498 | /// Extracts the syntax tree node from this punctuated pair, discarding the |
| 499 | /// following punctuation. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 500 | pub fn into_value(self) -> T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 501 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 502 | Pair::Punctuated(t, _) | Pair::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 506 | /// Borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 507 | pub fn value(&self) -> &T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 508 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 509 | Pair::Punctuated(ref t, _) | Pair::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 513 | /// Mutably borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 514 | pub fn value_mut(&mut self) -> &mut T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 515 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 516 | Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 520 | /// Borrows the punctuation from this punctuated pair, unless this pair is |
| 521 | /// the final one and there is no trailing punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 522 | pub fn punct(&self) -> Option<&P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 523 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 524 | Pair::Punctuated(_, ref d) => Some(d), |
| 525 | Pair::End(_) => None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 528 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 529 | /// Creates a punctuated pair out of a syntax tree node and an optional |
| 530 | /// following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 531 | pub fn new(t: T, d: Option<P>) -> Self { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 532 | match d { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 533 | Some(d) => Pair::Punctuated(t, d), |
| 534 | None => Pair::End(t), |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 538 | /// Produces this punctuated pair as a tuple of syntax tree node and |
| 539 | /// optional following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 540 | pub fn into_tuple(self) -> (T, Option<P>) { |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 541 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 542 | Pair::Punctuated(t, d) => (t, Some(d)), |
| 543 | Pair::End(t) => (t, None), |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 544 | } |
| 545 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 546 | } |
| 547 | |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 548 | impl<T, P> Index<usize> for Punctuated<T, P> { |
| 549 | type Output = T; |
| 550 | |
| 551 | fn index(&self, index: usize) -> &Self::Output { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 552 | if index == self.len() - 1 { |
| 553 | match self.last { |
| 554 | Some(ref t) => t, |
| 555 | None => &self.inner[index].0 |
| 556 | } |
| 557 | } else { |
| 558 | &self.inner[index].0 |
| 559 | } |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | impl<T, P> IndexMut<usize> for Punctuated<T, P> { |
| 564 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 565 | if index == self.len() - 1 { |
| 566 | match self.last { |
| 567 | Some(ref mut t) => t, |
| 568 | None => &mut self.inner[index].0 |
| 569 | } |
| 570 | } else { |
| 571 | &mut self.inner[index].0 |
| 572 | } |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 576 | #[cfg(feature = "parsing")] |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 577 | impl<T, P> Punctuated<T, P> |
| 578 | where |
| 579 | T: Synom, |
| 580 | P: Synom, |
| 581 | { |
| 582 | /// Parse **zero or more** syntax tree nodes with punctuation in between and |
| 583 | /// **no trailing** punctuation. |
| 584 | pub fn parse_separated(input: Cursor) -> PResult<Self> { |
| 585 | Self::parse_separated_with(input, T::parse) |
| 586 | } |
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 | /// Parse **one or more** syntax tree nodes with punctuation in bewteen and |
| 589 | /// **no trailing** punctuation. |
| 590 | /// allowing trailing punctuation. |
| 591 | pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> { |
| 592 | Self::parse_separated_nonempty_with(input, T::parse) |
| 593 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 594 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 595 | /// Parse **zero or more** syntax tree nodes with punctuation in between and |
| 596 | /// **optional trailing** punctuation. |
| 597 | pub fn parse_terminated(input: Cursor) -> PResult<Self> { |
| 598 | Self::parse_terminated_with(input, T::parse) |
| 599 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 600 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 601 | /// Parse **one or more** syntax tree nodes with punctuation in between and |
| 602 | /// **optional trailing** punctuation. |
| 603 | pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> { |
| 604 | Self::parse_terminated_nonempty_with(input, T::parse) |
| 605 | } |
| 606 | } |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 607 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 608 | #[cfg(feature = "parsing")] |
| 609 | impl<T, P> Punctuated<T, P> |
| 610 | where |
| 611 | P: Synom, |
| 612 | { |
| 613 | /// Parse **zero or more** syntax tree nodes using the given parser with |
| 614 | /// punctuation in between and **no trailing** punctuation. |
| 615 | pub fn parse_separated_with( |
| 616 | input: Cursor, |
| 617 | parse: fn(Cursor) -> PResult<T>, |
| 618 | ) -> PResult<Self> { |
| 619 | Self::parse(input, parse, false) |
| 620 | } |
| 621 | |
| 622 | /// Parse **one or more** syntax tree nodes using the given parser with |
| 623 | /// punctuation in between and **no trailing** punctuation. |
| 624 | pub fn parse_separated_nonempty_with( |
| 625 | input: Cursor, |
| 626 | parse: fn(Cursor) -> PResult<T>, |
| 627 | ) -> PResult<Self> { |
| 628 | match Self::parse(input, parse, false) { |
| 629 | Ok((ref b, _)) if b.is_empty() => parse_error(), |
| 630 | other => other, |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 631 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 632 | } |
| 633 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 634 | /// Parse **zero or more** syntax tree nodes using the given parser with |
| 635 | /// punctuation in between and **optional trailing** punctuation. |
| 636 | pub fn parse_terminated_with( |
| 637 | input: Cursor, |
| 638 | parse: fn(Cursor) -> PResult<T>, |
| 639 | ) -> PResult<Self> { |
| 640 | Self::parse(input, parse, true) |
| 641 | } |
| 642 | |
| 643 | /// Parse **one or more** syntax tree nodes using the given parser with |
| 644 | /// punctuation in between and **optional trailing** punctuation. |
| 645 | pub fn parse_terminated_nonempty_with( |
| 646 | input: Cursor, |
| 647 | parse: fn(Cursor) -> PResult<T>, |
| 648 | ) -> PResult<Self> { |
| 649 | match Self::parse(input, parse, true) { |
| 650 | Ok((ref b, _)) if b.is_empty() => parse_error(), |
| 651 | other => other, |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 652 | } |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 653 | } |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 654 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 655 | fn parse( |
| 656 | mut input: Cursor, |
| 657 | parse: fn(Cursor) -> PResult<T>, |
| 658 | terminated: bool, |
| 659 | ) -> PResult<Self> { |
| 660 | let mut res = Punctuated::new(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 661 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 662 | // get the first element |
| 663 | match parse(input) { |
| 664 | Err(_) => Ok((res, input)), |
| 665 | Ok((o, i)) => { |
| 666 | if i == input { |
| 667 | return parse_error(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 668 | } |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 669 | input = i; |
| 670 | res.push_value(o); |
| 671 | |
| 672 | // get the separator first |
| 673 | while let Ok((s, i2)) = P::parse(input) { |
| 674 | if i2 == input { |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | // get the element next |
| 679 | if let Ok((o3, i3)) = parse(i2) { |
| 680 | if i3 == i2 { |
| 681 | break; |
| 682 | } |
| 683 | res.push_punct(s); |
| 684 | res.push_value(o3); |
| 685 | input = i3; |
| 686 | } else { |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | if terminated { |
| 691 | if let Ok((sep, after)) = P::parse(input) { |
| 692 | res.push_punct(sep); |
| 693 | input = after; |
| 694 | } |
| 695 | } |
| 696 | Ok((res, input)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 702 | #[cfg(feature = "printing")] |
| 703 | mod printing { |
| 704 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 705 | use quote::{ToTokens, Tokens}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 706 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 707 | impl<T, P> ToTokens for Punctuated<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 708 | where |
| 709 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 710 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 711 | { |
| 712 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 713 | tokens.append_all(self.pairs()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 717 | impl<T, P> ToTokens for Pair<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 718 | where |
| 719 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 720 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 721 | { |
| 722 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 723 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 724 | Pair::Punctuated(ref a, ref b) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 725 | a.to_tokens(tokens); |
| 726 | b.to_tokens(tokens); |
| 727 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 728 | Pair::End(ref a) => a.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | } |
| 732 | } |