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