blob: 0d17db2923ef19ee7cdc66bc9730ef0758089751 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnayf3198012018-01-06 20:00:42 -08009//! 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 Tolnay6c0a6092018-03-31 22:47:39 +020030#[cfg(any(feature = "full", feature = "derive"))]
31use std::iter;
32use std::iter::FromIterator;
Mateusz Naściszewski14111202018-04-11 21:17:41 +020033use std::mem;
David Tolnaybb987132018-01-08 13:51:19 -080034use std::ops::{Index, IndexMut};
Mateusz Naściszewski14111202018-04-11 21:17:41 +020035use std::option;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070036use std::slice;
37use std::vec;
Nika Layzelld73a3652017-10-24 08:57:05 -040038#[cfg(feature = "extra-traits")]
39use std::fmt::{self, Debug};
Alex Crichtonccbb45d2017-05-23 10:58:24 -070040
David Tolnayf3198012018-01-06 20:00:42 -080041#[cfg(feature = "parsing")]
42use synom::{Synom, PResult};
43#[cfg(feature = "parsing")]
44use buffer::Cursor;
45#[cfg(feature = "parsing")]
46use parse_error;
47
48/// A punctuated sequence of syntax tree nodes of type `T` separated by
49/// punctuation of type `P`.
50///
51/// Refer to the [module documentation] for details about punctuated sequences.
52///
53/// [module documentation]: index.html
Nika Layzelld73a3652017-10-24 08:57:05 -040054#[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070055#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayf2cfd722017-12-31 18:02:51 -050056pub struct Punctuated<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020057 inner: Vec<(T, P)>,
58 last: Option<Box<T>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070059}
60
David Tolnayf2cfd722017-12-31 18:02:51 -050061impl<T, P> Punctuated<T, P> {
David Tolnayf3198012018-01-06 20:00:42 -080062 /// Creates an empty punctuated sequence.
David Tolnayf2cfd722017-12-31 18:02:51 -050063 pub fn new() -> Punctuated<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020064 Punctuated { inner: Vec::new(), last: None }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065 }
66
David Tolnayf3198012018-01-06 20:00:42 -080067 /// Determines whether this punctuated sequence is empty, meaning it
68 /// contains no syntax tree nodes or punctuation.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070069 pub fn is_empty(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020070 self.inner.len() == 0 && self.last.is_none()
Alex Crichtonccbb45d2017-05-23 10:58:24 -070071 }
72
David Tolnayf3198012018-01-06 20:00:42 -080073 /// Returns the number of syntax tree nodes in this punctuated sequence.
74 ///
75 /// This is the number of nodes of type `T`, not counting the punctuation of
76 /// type `P`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070077 pub fn len(&self) -> usize {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020078 self.inner.len() + if self.last.is_some() { 1 } else { 0 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070079 }
80
David Tolnayf3198012018-01-06 20:00:42 -080081 /// Borrows the first punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080082 pub fn first(&self) -> Option<Pair<&T, &P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020083 self.pairs().next()
Alex Crichton0aa50e02017-07-07 20:59:03 -070084 }
85
David Tolnayf3198012018-01-06 20:00:42 -080086 /// Borrows the last punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080087 pub fn last(&self) -> Option<Pair<&T, &P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020088 if self.last.is_some() {
89 self.last.as_ref()
90 .map(|t| Pair::End(t.as_ref()))
91 } else {
92 self.inner.last()
93 .map(|&(ref t, ref d)| Pair::Punctuated(t, d))
94 }
Alex Crichton0aa50e02017-07-07 20:59:03 -070095 }
96
David Tolnayf3198012018-01-06 20:00:42 -080097 /// Mutably borrows the last punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080098 pub fn last_mut(&mut self) -> Option<Pair<&mut T, &mut P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020099 if self.last.is_some() {
100 self.last.as_mut()
101 .map(|t| Pair::End(t.as_mut()))
102 } else {
103 self.inner.last_mut()
104 .map(|&mut (ref mut t, ref mut d)| Pair::Punctuated(t, d))
105 }
Alex Crichton0aa50e02017-07-07 20:59:03 -0700106 }
107
David Tolnayf3198012018-01-06 20:00:42 -0800108 /// Returns an iterator over borrowed syntax tree nodes of type `&T`.
David Tolnay8095c302018-03-31 19:34:17 +0200109 pub fn iter(&self) -> Iter<T> {
David Tolnay51382052017-12-27 13:46:21 -0500110 Iter {
David Tolnay8095c302018-03-31 19:34:17 +0200111 inner: Box::new(PrivateIter {
112 inner: self.inner.iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200113 last: self.last.as_ref().map(|t| t.as_ref()).into_iter(),
David Tolnay8095c302018-03-31 19:34:17 +0200114 }),
David Tolnay51382052017-12-27 13:46:21 -0500115 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700116 }
117
David Tolnayf3198012018-01-06 20:00:42 -0800118 /// Returns an iterator over mutably borrowed syntax tree nodes of type
119 /// `&mut T`.
David Tolnay8095c302018-03-31 19:34:17 +0200120 pub fn iter_mut(&mut self) -> IterMut<T> {
David Tolnaya0834b42018-01-01 21:30:02 -0800121 IterMut {
David Tolnay8095c302018-03-31 19:34:17 +0200122 inner: Box::new(PrivateIterMut {
123 inner: self.inner.iter_mut(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200124 last: self.last.as_mut().map(|t| t.as_mut()).into_iter(),
David Tolnay8095c302018-03-31 19:34:17 +0200125 }),
David Tolnaya0834b42018-01-01 21:30:02 -0800126 }
127 }
128
David Tolnayf3198012018-01-06 20:00:42 -0800129 /// Returns an iterator over the contents of this sequence as borrowed
130 /// punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800131 pub fn pairs(&self) -> Pairs<T, P> {
132 Pairs {
David Tolnay6eff4da2018-01-01 20:27:45 -0800133 inner: self.inner.iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200134 last: self.last.as_ref().map(|t| t.as_ref()).into_iter(),
David Tolnay6eff4da2018-01-01 20:27:45 -0800135 }
136 }
137
David Tolnayf3198012018-01-06 20:00:42 -0800138 /// Returns an iterator over the contents of this sequence as mutably
139 /// borrowed punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800140 pub fn pairs_mut(&mut self) -> PairsMut<T, P> {
141 PairsMut {
David Tolnay51382052017-12-27 13:46:21 -0500142 inner: self.inner.iter_mut(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200143 last: self.last.as_mut().map(|t| t.as_mut()).into_iter(),
David Tolnay51382052017-12-27 13:46:21 -0500144 }
Alex Crichton164c5332017-07-06 13:18:34 -0700145 }
146
David Tolnayf3198012018-01-06 20:00:42 -0800147 /// Returns an iterator over the contents of this sequence as owned
148 /// punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800149 pub fn into_pairs(self) -> IntoPairs<T, P> {
150 IntoPairs {
David Tolnay6eff4da2018-01-01 20:27:45 -0800151 inner: self.inner.into_iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200152 last: self.last.map(|t| *t).into_iter(),
David Tolnay6eff4da2018-01-01 20:27:45 -0800153 }
154 }
155
David Tolnayf3198012018-01-06 20:00:42 -0800156 /// Appends a syntax tree node onto the end of this punctuated sequence. The
157 /// sequence must previously have a trailing punctuation.
158 ///
159 /// Use [`push`] instead if the punctuated sequence may or may not already
160 /// have trailing punctuation.
161 ///
162 /// [`push`]: #method.push
163 ///
164 /// # Panics
165 ///
166 /// Panics if the sequence does not already have a trailing punctuation when
167 /// this method is called.
David Tolnay56080682018-01-06 14:01:52 -0800168 pub fn push_value(&mut self, value: T) {
David Tolnaydc03aec2017-12-30 01:54:18 -0500169 assert!(self.empty_or_trailing());
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200170 self.last = Some(Box::new(value));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700171 }
172
David Tolnayf3198012018-01-06 20:00:42 -0800173 /// Appends a trailing punctuation onto the end of this punctuated sequence.
174 /// The sequence must be non-empty and must not already have trailing
175 /// punctuation.
176 ///
177 /// # Panics
178 ///
179 /// Panics if the sequence is empty or already has a trailing punctuation.
David Tolnaya0834b42018-01-01 21:30:02 -0800180 pub fn push_punct(&mut self, punctuation: P) {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200181 assert!(!self.is_empty()); // redundant
182 assert!(self.last.is_some());
183 let last = mem::replace(&mut self.last, None);
184 self.inner.push((*last.unwrap(), punctuation));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700185 }
186
David Tolnayf3198012018-01-06 20:00:42 -0800187 /// Removes the last punctuated pair from this sequence, or `None` if the
188 /// sequence is empty.
David Tolnay56080682018-01-06 14:01:52 -0800189 pub fn pop(&mut self) -> Option<Pair<T, P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200190 if self.last.is_some() {
191 mem::replace(&mut self.last, None).map(|t| Pair::End(*t))
192 } else {
193 self.inner.pop().map(|(t, d)| Pair::Punctuated(t, d))
194 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700195 }
196
David Tolnayf3198012018-01-06 20:00:42 -0800197 /// Determines whether this punctuated sequence ends with a trailing
198 /// punctuation.
David Tolnaya0834b42018-01-01 21:30:02 -0800199 pub fn trailing_punct(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200200 self.last.is_none() && !self.is_empty()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700201 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400202
David Tolnayf2cfd722017-12-31 18:02:51 -0500203 /// Returns true if either this `Punctuated` is empty, or it has a trailing
204 /// punctuation.
David Tolnaydc03aec2017-12-30 01:54:18 -0500205 ///
David Tolnaya0834b42018-01-01 21:30:02 -0800206 /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`.
Michael Layzell3936ceb2017-07-08 00:28:36 -0400207 pub fn empty_or_trailing(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200208 self.last.is_none()
Michael Layzell3936ceb2017-07-08 00:28:36 -0400209 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700210}
211
David Tolnaya0834b42018-01-01 21:30:02 -0800212impl<T, P> Punctuated<T, P>
213where
214 P: Default,
215{
David Tolnayf3198012018-01-06 20:00:42 -0800216 /// Appends a syntax tree node onto the end of this punctuated sequence.
217 ///
218 /// If there is not a trailing punctuation in this sequence when this method
219 /// is called, the default value of punctuation type `P` is inserted before
220 /// the given value of type `T`.
David Tolnay56080682018-01-06 14:01:52 -0800221 pub fn push(&mut self, value: T) {
David Tolnaya0834b42018-01-01 21:30:02 -0800222 if !self.empty_or_trailing() {
223 self.push_punct(Default::default());
224 }
David Tolnay56080682018-01-06 14:01:52 -0800225 self.push_value(value);
David Tolnaya0834b42018-01-01 21:30:02 -0800226 }
David Tolnayb77d1802018-01-11 16:18:35 -0800227
228 /// Inserts an element at position `index`.
229 ///
230 /// # Panics
231 ///
232 /// Panics if `index` is greater than the number of elements previously in
233 /// this punctuated sequence.
234 pub fn insert(&mut self, index: usize, value: T) {
235 assert!(index <= self.len());
236
237 if index == self.len() {
238 self.push(value);
239 } else {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200240 self.inner.insert(index, (value, Default::default()));
David Tolnayb77d1802018-01-11 16:18:35 -0800241 }
242 }
David Tolnaya0834b42018-01-01 21:30:02 -0800243}
244
Nika Layzelld73a3652017-10-24 08:57:05 -0400245#[cfg(feature = "extra-traits")]
David Tolnayf2cfd722017-12-31 18:02:51 -0500246impl<T: Debug, P: Debug> Debug for Punctuated<T, P> {
Nika Layzelld73a3652017-10-24 08:57:05 -0400247 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200248 let mut list = f.debug_list();
249 list.entries(&self.inner);
250 for t in self.last.iter() {
251 list.entry(&*t);
252 }
253 list.finish()
Nika Layzelld73a3652017-10-24 08:57:05 -0400254 }
255}
256
David Tolnay9ef24bc2018-01-09 10:43:55 -0800257impl<T, P> FromIterator<T> for Punctuated<T, P>
258where
259 P: Default,
260{
261 fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self {
262 let mut ret = Punctuated::new();
263 ret.extend(i);
264 ret
265 }
266}
267
268impl<T, P> Extend<T> for Punctuated<T, P>
269where
270 P: Default,
271{
272 fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) {
273 for value in i {
274 self.push(value);
275 }
276 }
277}
278
David Tolnay56080682018-01-06 14:01:52 -0800279impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> {
280 fn from_iter<I: IntoIterator<Item = Pair<T, P>>>(i: I) -> Self {
David Tolnayf2cfd722017-12-31 18:02:51 -0500281 let mut ret = Punctuated::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700282 ret.extend(i);
Alex Crichton954046c2017-05-30 21:49:42 -0700283 ret
284 }
285}
286
David Tolnay56080682018-01-06 14:01:52 -0800287impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> {
288 fn extend<I: IntoIterator<Item = Pair<T, P>>>(&mut self, i: I) {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200289 assert!(self.empty_or_trailing());
290 let mut nomore = false;
David Tolnay56080682018-01-06 14:01:52 -0800291 for pair in i {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200292 if nomore {
293 panic!("Punctuated extended with items after a Pair::End");
294 }
David Tolnay56080682018-01-06 14:01:52 -0800295 match pair {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200296 Pair::Punctuated(a, b) => self.inner.push((a, b)),
297 Pair::End(a) => {
298 self.last = Some(Box::new(a));
299 nomore = true;
300 }
Alex Crichton24f12822017-07-14 07:15:32 -0700301 }
302 }
303 }
304}
305
David Tolnayf2cfd722017-12-31 18:02:51 -0500306impl<T, P> IntoIterator for Punctuated<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800307 type Item = T;
David Tolnayf2cfd722017-12-31 18:02:51 -0500308 type IntoIter = IntoIter<T, P>;
Alex Crichton954046c2017-05-30 21:49:42 -0700309
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500310 fn into_iter(self) -> Self::IntoIter {
David Tolnay51382052017-12-27 13:46:21 -0500311 IntoIter {
312 inner: self.inner.into_iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200313 last: self.last.map(|t| *t).into_iter(),
David Tolnay51382052017-12-27 13:46:21 -0500314 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700315 }
316}
317
David Tolnay6eff4da2018-01-01 20:27:45 -0800318impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> {
319 type Item = &'a T;
David Tolnay8095c302018-03-31 19:34:17 +0200320 type IntoIter = Iter<'a, T>;
David Tolnay6eff4da2018-01-01 20:27:45 -0800321
322 fn into_iter(self) -> Self::IntoIter {
323 Punctuated::iter(self)
324 }
325}
326
David Tolnaya0834b42018-01-01 21:30:02 -0800327impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> {
328 type Item = &'a mut T;
David Tolnay8095c302018-03-31 19:34:17 +0200329 type IntoIter = IterMut<'a, T>;
David Tolnaya0834b42018-01-01 21:30:02 -0800330
331 fn into_iter(self) -> Self::IntoIter {
332 Punctuated::iter_mut(self)
333 }
334}
335
David Tolnayf2cfd722017-12-31 18:02:51 -0500336impl<T, P> Default for Punctuated<T, P> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700337 fn default() -> Self {
David Tolnayf2cfd722017-12-31 18:02:51 -0500338 Punctuated::new()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700339 }
340}
341
David Tolnayf3198012018-01-06 20:00:42 -0800342/// An iterator over borrowed pairs of type `Pair<&T, &P>`.
343///
344/// Refer to the [module documentation] for details about punctuated sequences.
345///
346/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800347pub struct Pairs<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200348 inner: slice::Iter<'a, (T, P)>,
349 last: option::IntoIter<&'a T>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700350}
351
David Tolnay56080682018-01-06 14:01:52 -0800352impl<'a, T, P> Iterator for Pairs<'a, T, P> {
353 type Item = Pair<&'a T, &'a P>;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700354
David Tolnay6eff4da2018-01-01 20:27:45 -0800355 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200356 self.inner.next().map(|&(ref t, ref p)| Pair::Punctuated(t, p))
357 .or_else(|| self.last.next().map(|t| Pair::End(t)))
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700358 }
359}
360
David Tolnayf3198012018-01-06 20:00:42 -0800361/// An iterator over mutably borrowed pairs of type `Pair<&mut T, &mut P>`.
362///
363/// Refer to the [module documentation] for details about punctuated sequences.
364///
365/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800366pub struct PairsMut<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200367 inner: slice::IterMut<'a, (T, P)>,
368 last: option::IntoIter<&'a mut T>,
Alex Crichton164c5332017-07-06 13:18:34 -0700369}
370
David Tolnay56080682018-01-06 14:01:52 -0800371impl<'a, T, P> Iterator for PairsMut<'a, T, P> {
372 type Item = Pair<&'a mut T, &'a mut P>;
Alex Crichton164c5332017-07-06 13:18:34 -0700373
David Tolnay6eff4da2018-01-01 20:27:45 -0800374 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200375 self.inner.next().map(|&mut (ref mut t, ref mut p)| Pair::Punctuated(t, p))
376 .or_else(|| self.last.next().map(|t| Pair::End(t)))
Alex Crichton164c5332017-07-06 13:18:34 -0700377 }
378}
379
David Tolnayf3198012018-01-06 20:00:42 -0800380/// An iterator over owned pairs of type `Pair<T, P>`.
381///
382/// Refer to the [module documentation] for details about punctuated sequences.
383///
384/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800385pub struct IntoPairs<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200386 inner: vec::IntoIter<(T, P)>,
387 last: option::IntoIter<T>,
David Tolnay6eff4da2018-01-01 20:27:45 -0800388}
389
David Tolnay56080682018-01-06 14:01:52 -0800390impl<T, P> Iterator for IntoPairs<T, P> {
391 type Item = Pair<T, P>;
David Tolnay6eff4da2018-01-01 20:27:45 -0800392
393 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200394 self.inner.next().map(|(t, p)| Pair::Punctuated(t, p))
395 .or_else(|| self.last.next().map(|t| Pair::End(t)))
David Tolnay6eff4da2018-01-01 20:27:45 -0800396 }
397}
398
David Tolnayf3198012018-01-06 20:00:42 -0800399/// An iterator over owned values of type `T`.
400///
401/// Refer to the [module documentation] for details about punctuated sequences.
402///
403/// [module documentation]: index.html
David Tolnayf2cfd722017-12-31 18:02:51 -0500404pub struct IntoIter<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200405 inner: vec::IntoIter<(T, P)>,
406 last: option::IntoIter<T>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700407}
408
David Tolnayf2cfd722017-12-31 18:02:51 -0500409impl<T, P> Iterator for IntoIter<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800410 type Item = T;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700411
David Tolnay6eff4da2018-01-01 20:27:45 -0800412 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200413 self.inner.next().map(|pair| pair.0).or_else(|| self.last.next())
David Tolnay6eff4da2018-01-01 20:27:45 -0800414 }
415}
416
David Tolnayf3198012018-01-06 20:00:42 -0800417/// An iterator over borrowed values of type `&T`.
418///
419/// Refer to the [module documentation] for details about punctuated sequences.
420///
421/// [module documentation]: index.html
David Tolnay8095c302018-03-31 19:34:17 +0200422pub struct Iter<'a, T: 'a> {
423 inner: Box<Iterator<Item = &'a T> + 'a>,
424}
425
426struct PrivateIter<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200427 inner: slice::Iter<'a, (T, P)>,
428 last: option::IntoIter<&'a T>,
David Tolnay6eff4da2018-01-01 20:27:45 -0800429}
430
David Tolnay96a09d92018-01-16 22:24:03 -0800431#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay8095c302018-03-31 19:34:17 +0200432impl<'a, T> Iter<'a, T> {
David Tolnay96a09d92018-01-16 22:24:03 -0800433 // Not public API.
434 #[doc(hidden)]
435 pub fn private_empty() -> Self {
436 Iter {
David Tolnay8095c302018-03-31 19:34:17 +0200437 inner: Box::new(iter::empty()),
David Tolnay96a09d92018-01-16 22:24:03 -0800438 }
439 }
440}
441
David Tolnay8095c302018-03-31 19:34:17 +0200442impl<'a, T> Iterator for Iter<'a, T> {
443 type Item = &'a T;
444
445 fn next(&mut self) -> Option<Self::Item> {
446 self.inner.next()
447 }
448}
449
450impl<'a, T, P> Iterator for PrivateIter<'a, T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800451 type Item = &'a T;
452
453 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200454 self.inner.next().map(|pair| &pair.0).or_else(|| self.last.next())
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700455 }
456}
457
David Tolnayf3198012018-01-06 20:00:42 -0800458/// An iterator over mutably borrowed values of type `&mut T`.
459///
460/// Refer to the [module documentation] for details about punctuated sequences.
461///
462/// [module documentation]: index.html
David Tolnay8095c302018-03-31 19:34:17 +0200463pub struct IterMut<'a, T: 'a> {
464 inner: Box<Iterator<Item = &'a mut T> + 'a>,
465}
466
467struct PrivateIterMut<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200468 inner: slice::IterMut<'a, (T, P)>,
469 last: option::IntoIter<&'a mut T>,
David Tolnaya0834b42018-01-01 21:30:02 -0800470}
471
David Tolnay8095c302018-03-31 19:34:17 +0200472impl<'a, T> Iterator for IterMut<'a, T> {
473 type Item = &'a mut T;
474
475 fn next(&mut self) -> Option<Self::Item> {
476 self.inner.next()
477 }
478}
479
480impl<'a, T, P> Iterator for PrivateIterMut<'a, T, P> {
David Tolnaya0834b42018-01-01 21:30:02 -0800481 type Item = &'a mut T;
482
483 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200484 self.inner.next().map(|pair| &mut pair.0).or_else(|| self.last.next())
David Tolnaya0834b42018-01-01 21:30:02 -0800485 }
486}
487
David Tolnayf3198012018-01-06 20:00:42 -0800488/// A single syntax tree node of type `T` followed by its trailing punctuation
489/// of type `P` if any.
490///
491/// Refer to the [module documentation] for details about punctuated sequences.
492///
493/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800494pub enum Pair<T, P> {
David Tolnayf2cfd722017-12-31 18:02:51 -0500495 Punctuated(T, P),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700496 End(T),
497}
498
David Tolnay56080682018-01-06 14:01:52 -0800499impl<T, P> Pair<T, P> {
David Tolnayf3198012018-01-06 20:00:42 -0800500 /// Extracts the syntax tree node from this punctuated pair, discarding the
501 /// following punctuation.
David Tolnay56080682018-01-06 14:01:52 -0800502 pub fn into_value(self) -> T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700503 match self {
David Tolnay56080682018-01-06 14:01:52 -0800504 Pair::Punctuated(t, _) | Pair::End(t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700505 }
506 }
507
David Tolnayf3198012018-01-06 20:00:42 -0800508 /// Borrows the syntax tree node from this punctuated pair.
David Tolnay56080682018-01-06 14:01:52 -0800509 pub fn value(&self) -> &T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700510 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800511 Pair::Punctuated(ref t, _) | Pair::End(ref t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700512 }
513 }
514
David Tolnayf3198012018-01-06 20:00:42 -0800515 /// Mutably borrows the syntax tree node from this punctuated pair.
David Tolnay56080682018-01-06 14:01:52 -0800516 pub fn value_mut(&mut self) -> &mut T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700517 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800518 Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700519 }
520 }
521
David Tolnayf3198012018-01-06 20:00:42 -0800522 /// Borrows the punctuation from this punctuated pair, unless this pair is
523 /// the final one and there is no trailing punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500524 pub fn punct(&self) -> Option<&P> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700525 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800526 Pair::Punctuated(_, ref d) => Some(d),
527 Pair::End(_) => None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700528 }
529 }
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400530
David Tolnayf3198012018-01-06 20:00:42 -0800531 /// Creates a punctuated pair out of a syntax tree node and an optional
532 /// following punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500533 pub fn new(t: T, d: Option<P>) -> Self {
David Tolnay660fd1f2017-12-31 01:52:57 -0500534 match d {
David Tolnay56080682018-01-06 14:01:52 -0800535 Some(d) => Pair::Punctuated(t, d),
536 None => Pair::End(t),
David Tolnay660fd1f2017-12-31 01:52:57 -0500537 }
538 }
539
David Tolnayf3198012018-01-06 20:00:42 -0800540 /// Produces this punctuated pair as a tuple of syntax tree node and
541 /// optional following punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500542 pub fn into_tuple(self) -> (T, Option<P>) {
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400543 match self {
David Tolnay56080682018-01-06 14:01:52 -0800544 Pair::Punctuated(t, d) => (t, Some(d)),
545 Pair::End(t) => (t, None),
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400546 }
547 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700548}
549
David Tolnaybb987132018-01-08 13:51:19 -0800550impl<T, P> Index<usize> for Punctuated<T, P> {
551 type Output = T;
552
553 fn index(&self, index: usize) -> &Self::Output {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200554 if index == self.len() - 1 {
555 match self.last {
556 Some(ref t) => t,
557 None => &self.inner[index].0
558 }
559 } else {
560 &self.inner[index].0
561 }
David Tolnaybb987132018-01-08 13:51:19 -0800562 }
563}
564
565impl<T, P> IndexMut<usize> for Punctuated<T, P> {
566 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200567 if index == self.len() - 1 {
568 match self.last {
569 Some(ref mut t) => t,
570 None => &mut self.inner[index].0
571 }
572 } else {
573 &mut self.inner[index].0
574 }
David Tolnaybb987132018-01-08 13:51:19 -0800575 }
576}
577
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700578#[cfg(feature = "parsing")]
David Tolnayf3198012018-01-06 20:00:42 -0800579impl<T, P> Punctuated<T, P>
580where
581 T: Synom,
582 P: Synom,
583{
584 /// Parse **zero or more** syntax tree nodes with punctuation in between and
585 /// **no trailing** punctuation.
586 pub fn parse_separated(input: Cursor) -> PResult<Self> {
587 Self::parse_separated_with(input, T::parse)
588 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700589
David Tolnayf3198012018-01-06 20:00:42 -0800590 /// Parse **one or more** syntax tree nodes with punctuation in bewteen and
591 /// **no trailing** punctuation.
592 /// allowing trailing punctuation.
593 pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> {
594 Self::parse_separated_nonempty_with(input, T::parse)
595 }
Alex Crichton954046c2017-05-30 21:49:42 -0700596
David Tolnayf3198012018-01-06 20:00:42 -0800597 /// Parse **zero or more** syntax tree nodes with punctuation in between and
598 /// **optional trailing** punctuation.
599 pub fn parse_terminated(input: Cursor) -> PResult<Self> {
600 Self::parse_terminated_with(input, T::parse)
601 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700602
David Tolnayf3198012018-01-06 20:00:42 -0800603 /// Parse **one or more** syntax tree nodes with punctuation in between and
604 /// **optional trailing** punctuation.
605 pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> {
606 Self::parse_terminated_nonempty_with(input, T::parse)
607 }
608}
Nika Layzellb49a9e52017-12-05 13:31:52 -0500609
David Tolnayf3198012018-01-06 20:00:42 -0800610#[cfg(feature = "parsing")]
611impl<T, P> Punctuated<T, P>
612where
613 P: Synom,
614{
615 /// Parse **zero or more** syntax tree nodes using the given parser with
616 /// punctuation in between and **no trailing** punctuation.
617 pub fn parse_separated_with(
618 input: Cursor,
619 parse: fn(Cursor) -> PResult<T>,
620 ) -> PResult<Self> {
621 Self::parse(input, parse, false)
622 }
623
624 /// Parse **one or more** syntax tree nodes using the given parser with
625 /// punctuation in between and **no trailing** punctuation.
626 pub fn parse_separated_nonempty_with(
627 input: Cursor,
628 parse: fn(Cursor) -> PResult<T>,
629 ) -> PResult<Self> {
630 match Self::parse(input, parse, false) {
631 Ok((ref b, _)) if b.is_empty() => parse_error(),
632 other => other,
Nika Layzellb49a9e52017-12-05 13:31:52 -0500633 }
Alex Crichton954046c2017-05-30 21:49:42 -0700634 }
635
David Tolnayf3198012018-01-06 20:00:42 -0800636 /// Parse **zero or more** syntax tree nodes using the given parser with
637 /// punctuation in between and **optional trailing** punctuation.
638 pub fn parse_terminated_with(
639 input: Cursor,
640 parse: fn(Cursor) -> PResult<T>,
641 ) -> PResult<Self> {
642 Self::parse(input, parse, true)
643 }
644
645 /// Parse **one or more** syntax tree nodes using the given parser with
646 /// punctuation in between and **optional trailing** punctuation.
647 pub fn parse_terminated_nonempty_with(
648 input: Cursor,
649 parse: fn(Cursor) -> PResult<T>,
650 ) -> PResult<Self> {
651 match Self::parse(input, parse, true) {
652 Ok((ref b, _)) if b.is_empty() => parse_error(),
653 other => other,
David Tolnaydc03aec2017-12-30 01:54:18 -0500654 }
David Tolnayf3198012018-01-06 20:00:42 -0800655 }
David Tolnaydc03aec2017-12-30 01:54:18 -0500656
David Tolnayf3198012018-01-06 20:00:42 -0800657 fn parse(
658 mut input: Cursor,
659 parse: fn(Cursor) -> PResult<T>,
660 terminated: bool,
661 ) -> PResult<Self> {
662 let mut res = Punctuated::new();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700663
David Tolnayf3198012018-01-06 20:00:42 -0800664 // get the first element
665 match parse(input) {
666 Err(_) => Ok((res, input)),
667 Ok((o, i)) => {
668 if i == input {
669 return parse_error();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700670 }
David Tolnayf3198012018-01-06 20:00:42 -0800671 input = i;
672 res.push_value(o);
673
674 // get the separator first
675 while let Ok((s, i2)) = P::parse(input) {
676 if i2 == input {
677 break;
678 }
679
680 // get the element next
681 if let Ok((o3, i3)) = parse(i2) {
682 if i3 == i2 {
683 break;
684 }
685 res.push_punct(s);
686 res.push_value(o3);
687 input = i3;
688 } else {
689 break;
690 }
691 }
692 if terminated {
693 if let Ok((sep, after)) = P::parse(input) {
694 res.push_punct(sep);
695 input = after;
696 }
697 }
698 Ok((res, input))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700699 }
700 }
701 }
702}
703
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700704#[cfg(feature = "printing")]
705mod printing {
706 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500707 use quote::{ToTokens, Tokens};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700708
David Tolnayf2cfd722017-12-31 18:02:51 -0500709 impl<T, P> ToTokens for Punctuated<T, P>
David Tolnay51382052017-12-27 13:46:21 -0500710 where
711 T: ToTokens,
David Tolnayf2cfd722017-12-31 18:02:51 -0500712 P: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700713 {
714 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay56080682018-01-06 14:01:52 -0800715 tokens.append_all(self.pairs())
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700716 }
717 }
718
David Tolnay56080682018-01-06 14:01:52 -0800719 impl<T, P> ToTokens for Pair<T, P>
David Tolnay51382052017-12-27 13:46:21 -0500720 where
721 T: ToTokens,
David Tolnayf2cfd722017-12-31 18:02:51 -0500722 P: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700723 {
724 fn to_tokens(&self, tokens: &mut Tokens) {
725 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800726 Pair::Punctuated(ref a, ref b) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700727 a.to_tokens(tokens);
728 b.to_tokens(tokens);
729 }
David Tolnay56080682018-01-06 14:01:52 -0800730 Pair::End(ref a) => a.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700731 }
732 }
733 }
734}