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