blob: c87b6394bd14dd72085f7db26ae5390121f7b814 [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;
David Tolnaybb987132018-01-08 13:51:19 -080033use std::ops::{Index, IndexMut};
Mateusz Naściszewski14111202018-04-11 21:17:41 +020034use std::option;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070035use std::slice;
36use std::vec;
Nika Layzelld73a3652017-10-24 08:57:05 -040037#[cfg(feature = "extra-traits")]
38use std::fmt::{self, Debug};
Alex Crichtonccbb45d2017-05-23 10:58:24 -070039
David Tolnayf3198012018-01-06 20:00:42 -080040#[cfg(feature = "parsing")]
41use synom::{Synom, PResult};
42#[cfg(feature = "parsing")]
43use buffer::Cursor;
44#[cfg(feature = "parsing")]
45use parse_error;
46
47/// A punctuated sequence of syntax tree nodes of type `T` separated by
48/// punctuation of type `P`.
49///
50/// Refer to the [module documentation] for details about punctuated sequences.
51///
52/// [module documentation]: index.html
Nika Layzelld73a3652017-10-24 08:57:05 -040053#[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070054#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayf2cfd722017-12-31 18:02:51 -050055pub struct Punctuated<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020056 inner: Vec<(T, P)>,
57 last: Option<Box<T>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070058}
59
David Tolnayf2cfd722017-12-31 18:02:51 -050060impl<T, P> Punctuated<T, P> {
David Tolnayf3198012018-01-06 20:00:42 -080061 /// Creates an empty punctuated sequence.
David Tolnayf2cfd722017-12-31 18:02:51 -050062 pub fn new() -> Punctuated<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020063 Punctuated { inner: Vec::new(), last: None }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070064 }
65
David Tolnayf3198012018-01-06 20:00:42 -080066 /// Determines whether this punctuated sequence is empty, meaning it
67 /// contains no syntax tree nodes or punctuation.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070068 pub fn is_empty(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020069 self.inner.len() == 0 && self.last.is_none()
Alex Crichtonccbb45d2017-05-23 10:58:24 -070070 }
71
David Tolnayf3198012018-01-06 20:00:42 -080072 /// Returns the number of syntax tree nodes in this punctuated sequence.
73 ///
74 /// This is the number of nodes of type `T`, not counting the punctuation of
75 /// type `P`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076 pub fn len(&self) -> usize {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020077 self.inner.len() + if self.last.is_some() { 1 } else { 0 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070078 }
79
David Tolnayf3198012018-01-06 20:00:42 -080080 /// Borrows the first punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080081 pub fn first(&self) -> Option<Pair<&T, &P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020082 self.pairs().next()
Alex Crichton0aa50e02017-07-07 20:59:03 -070083 }
84
David Tolnayf3198012018-01-06 20:00:42 -080085 /// Borrows the last punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080086 pub fn last(&self) -> Option<Pair<&T, &P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020087 if self.last.is_some() {
88 self.last.as_ref()
89 .map(|t| Pair::End(t.as_ref()))
90 } else {
91 self.inner.last()
92 .map(|&(ref t, ref d)| Pair::Punctuated(t, d))
93 }
Alex Crichton0aa50e02017-07-07 20:59:03 -070094 }
95
David Tolnayf3198012018-01-06 20:00:42 -080096 /// Mutably borrows the last punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080097 pub fn last_mut(&mut self) -> Option<Pair<&mut T, &mut P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020098 if self.last.is_some() {
99 self.last.as_mut()
100 .map(|t| Pair::End(t.as_mut()))
101 } else {
102 self.inner.last_mut()
103 .map(|&mut (ref mut t, ref mut d)| Pair::Punctuated(t, d))
104 }
Alex Crichton0aa50e02017-07-07 20:59:03 -0700105 }
106
David Tolnayf3198012018-01-06 20:00:42 -0800107 /// Returns an iterator over borrowed syntax tree nodes of type `&T`.
David Tolnay8095c302018-03-31 19:34:17 +0200108 pub fn iter(&self) -> Iter<T> {
David Tolnay51382052017-12-27 13:46:21 -0500109 Iter {
David Tolnay8095c302018-03-31 19:34:17 +0200110 inner: Box::new(PrivateIter {
111 inner: self.inner.iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200112 last: self.last.as_ref().map(|t| t.as_ref()).into_iter(),
David Tolnay8095c302018-03-31 19:34:17 +0200113 }),
David Tolnay51382052017-12-27 13:46:21 -0500114 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700115 }
116
David Tolnayf3198012018-01-06 20:00:42 -0800117 /// Returns an iterator over mutably borrowed syntax tree nodes of type
118 /// `&mut T`.
David Tolnay8095c302018-03-31 19:34:17 +0200119 pub fn iter_mut(&mut self) -> IterMut<T> {
David Tolnaya0834b42018-01-01 21:30:02 -0800120 IterMut {
David Tolnay8095c302018-03-31 19:34:17 +0200121 inner: Box::new(PrivateIterMut {
122 inner: self.inner.iter_mut(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200123 last: self.last.as_mut().map(|t| t.as_mut()).into_iter(),
David Tolnay8095c302018-03-31 19:34:17 +0200124 }),
David Tolnaya0834b42018-01-01 21:30:02 -0800125 }
126 }
127
David Tolnayf3198012018-01-06 20:00:42 -0800128 /// Returns an iterator over the contents of this sequence as borrowed
129 /// punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800130 pub fn pairs(&self) -> Pairs<T, P> {
131 Pairs {
David Tolnay6eff4da2018-01-01 20:27:45 -0800132 inner: self.inner.iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200133 last: self.last.as_ref().map(|t| t.as_ref()).into_iter(),
David Tolnay6eff4da2018-01-01 20:27:45 -0800134 }
135 }
136
David Tolnayf3198012018-01-06 20:00:42 -0800137 /// Returns an iterator over the contents of this sequence as mutably
138 /// borrowed punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800139 pub fn pairs_mut(&mut self) -> PairsMut<T, P> {
140 PairsMut {
David Tolnay51382052017-12-27 13:46:21 -0500141 inner: self.inner.iter_mut(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200142 last: self.last.as_mut().map(|t| t.as_mut()).into_iter(),
David Tolnay51382052017-12-27 13:46:21 -0500143 }
Alex Crichton164c5332017-07-06 13:18:34 -0700144 }
145
David Tolnayf3198012018-01-06 20:00:42 -0800146 /// Returns an iterator over the contents of this sequence as owned
147 /// punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800148 pub fn into_pairs(self) -> IntoPairs<T, P> {
149 IntoPairs {
David Tolnay6eff4da2018-01-01 20:27:45 -0800150 inner: self.inner.into_iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200151 last: self.last.map(|t| *t).into_iter(),
David Tolnay6eff4da2018-01-01 20:27:45 -0800152 }
153 }
154
David Tolnayf3198012018-01-06 20:00:42 -0800155 /// Appends a syntax tree node onto the end of this punctuated sequence. The
156 /// sequence must previously have a trailing punctuation.
157 ///
158 /// Use [`push`] instead if the punctuated sequence may or may not already
159 /// have trailing punctuation.
160 ///
161 /// [`push`]: #method.push
162 ///
163 /// # Panics
164 ///
165 /// Panics if the sequence does not already have a trailing punctuation when
166 /// this method is called.
David Tolnay56080682018-01-06 14:01:52 -0800167 pub fn push_value(&mut self, value: T) {
David Tolnaydc03aec2017-12-30 01:54:18 -0500168 assert!(self.empty_or_trailing());
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200169 self.last = Some(Box::new(value));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700170 }
171
David Tolnayf3198012018-01-06 20:00:42 -0800172 /// Appends a trailing punctuation onto the end of this punctuated sequence.
173 /// The sequence must be non-empty and must not already have trailing
174 /// punctuation.
175 ///
176 /// # Panics
177 ///
178 /// Panics if the sequence is empty or already has a trailing punctuation.
David Tolnaya0834b42018-01-01 21:30:02 -0800179 pub fn push_punct(&mut self, punctuation: P) {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200180 assert!(self.last.is_some());
David Tolnay03163082018-04-11 12:10:18 -0700181 let last = self.last.take().unwrap();
182 self.inner.push((*last, punctuation));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700183 }
184
David Tolnayf3198012018-01-06 20:00:42 -0800185 /// Removes the last punctuated pair from this sequence, or `None` if the
186 /// sequence is empty.
David Tolnay56080682018-01-06 14:01:52 -0800187 pub fn pop(&mut self) -> Option<Pair<T, P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200188 if self.last.is_some() {
David Tolnay03163082018-04-11 12:10:18 -0700189 self.last.take().map(|t| Pair::End(*t))
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200190 } else {
191 self.inner.pop().map(|(t, d)| Pair::Punctuated(t, d))
192 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700193 }
194
David Tolnayf3198012018-01-06 20:00:42 -0800195 /// Determines whether this punctuated sequence ends with a trailing
196 /// punctuation.
David Tolnaya0834b42018-01-01 21:30:02 -0800197 pub fn trailing_punct(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200198 self.last.is_none() && !self.is_empty()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700199 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400200
David Tolnayf2cfd722017-12-31 18:02:51 -0500201 /// Returns true if either this `Punctuated` is empty, or it has a trailing
202 /// punctuation.
David Tolnaydc03aec2017-12-30 01:54:18 -0500203 ///
David Tolnaya0834b42018-01-01 21:30:02 -0800204 /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`.
Michael Layzell3936ceb2017-07-08 00:28:36 -0400205 pub fn empty_or_trailing(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200206 self.last.is_none()
Michael Layzell3936ceb2017-07-08 00:28:36 -0400207 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700208}
209
David Tolnaya0834b42018-01-01 21:30:02 -0800210impl<T, P> Punctuated<T, P>
211where
212 P: Default,
213{
David Tolnayf3198012018-01-06 20:00:42 -0800214 /// Appends a syntax tree node onto the end of this punctuated sequence.
215 ///
216 /// If there is not a trailing punctuation in this sequence when this method
217 /// is called, the default value of punctuation type `P` is inserted before
218 /// the given value of type `T`.
David Tolnay56080682018-01-06 14:01:52 -0800219 pub fn push(&mut self, value: T) {
David Tolnaya0834b42018-01-01 21:30:02 -0800220 if !self.empty_or_trailing() {
221 self.push_punct(Default::default());
222 }
David Tolnay56080682018-01-06 14:01:52 -0800223 self.push_value(value);
David Tolnaya0834b42018-01-01 21:30:02 -0800224 }
David Tolnayb77d1802018-01-11 16:18:35 -0800225
226 /// Inserts an element at position `index`.
227 ///
228 /// # Panics
229 ///
230 /// Panics if `index` is greater than the number of elements previously in
231 /// this punctuated sequence.
232 pub fn insert(&mut self, index: usize, value: T) {
233 assert!(index <= self.len());
234
235 if index == self.len() {
236 self.push(value);
237 } else {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200238 self.inner.insert(index, (value, Default::default()));
David Tolnayb77d1802018-01-11 16:18:35 -0800239 }
240 }
David Tolnaya0834b42018-01-01 21:30:02 -0800241}
242
Nika Layzelld73a3652017-10-24 08:57:05 -0400243#[cfg(feature = "extra-traits")]
David Tolnayf2cfd722017-12-31 18:02:51 -0500244impl<T: Debug, P: Debug> Debug for Punctuated<T, P> {
Nika Layzelld73a3652017-10-24 08:57:05 -0400245 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200246 let mut list = f.debug_list();
247 list.entries(&self.inner);
248 for t in self.last.iter() {
249 list.entry(&*t);
250 }
251 list.finish()
Nika Layzelld73a3652017-10-24 08:57:05 -0400252 }
253}
254
David Tolnay9ef24bc2018-01-09 10:43:55 -0800255impl<T, P> FromIterator<T> for Punctuated<T, P>
256where
257 P: Default,
258{
259 fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self {
260 let mut ret = Punctuated::new();
261 ret.extend(i);
262 ret
263 }
264}
265
266impl<T, P> Extend<T> for Punctuated<T, P>
267where
268 P: Default,
269{
270 fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) {
271 for value in i {
272 self.push(value);
273 }
274 }
275}
276
David Tolnay56080682018-01-06 14:01:52 -0800277impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> {
278 fn from_iter<I: IntoIterator<Item = Pair<T, P>>>(i: I) -> Self {
David Tolnayf2cfd722017-12-31 18:02:51 -0500279 let mut ret = Punctuated::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700280 ret.extend(i);
Alex Crichton954046c2017-05-30 21:49:42 -0700281 ret
282 }
283}
284
David Tolnay56080682018-01-06 14:01:52 -0800285impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> {
286 fn extend<I: IntoIterator<Item = Pair<T, P>>>(&mut self, i: I) {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200287 assert!(self.empty_or_trailing());
288 let mut nomore = false;
David Tolnay56080682018-01-06 14:01:52 -0800289 for pair in i {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200290 if nomore {
291 panic!("Punctuated extended with items after a Pair::End");
292 }
David Tolnay56080682018-01-06 14:01:52 -0800293 match pair {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200294 Pair::Punctuated(a, b) => self.inner.push((a, b)),
295 Pair::End(a) => {
296 self.last = Some(Box::new(a));
297 nomore = true;
298 }
Alex Crichton24f12822017-07-14 07:15:32 -0700299 }
300 }
301 }
302}
303
David Tolnayf2cfd722017-12-31 18:02:51 -0500304impl<T, P> IntoIterator for Punctuated<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800305 type Item = T;
David Tolnayf2cfd722017-12-31 18:02:51 -0500306 type IntoIter = IntoIter<T, P>;
Alex Crichton954046c2017-05-30 21:49:42 -0700307
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500308 fn into_iter(self) -> Self::IntoIter {
David Tolnay51382052017-12-27 13:46:21 -0500309 IntoIter {
310 inner: self.inner.into_iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200311 last: self.last.map(|t| *t).into_iter(),
David Tolnay51382052017-12-27 13:46:21 -0500312 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700313 }
314}
315
David Tolnay6eff4da2018-01-01 20:27:45 -0800316impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> {
317 type Item = &'a T;
David Tolnay8095c302018-03-31 19:34:17 +0200318 type IntoIter = Iter<'a, T>;
David Tolnay6eff4da2018-01-01 20:27:45 -0800319
320 fn into_iter(self) -> Self::IntoIter {
321 Punctuated::iter(self)
322 }
323}
324
David Tolnaya0834b42018-01-01 21:30:02 -0800325impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> {
326 type Item = &'a mut T;
David Tolnay8095c302018-03-31 19:34:17 +0200327 type IntoIter = IterMut<'a, T>;
David Tolnaya0834b42018-01-01 21:30:02 -0800328
329 fn into_iter(self) -> Self::IntoIter {
330 Punctuated::iter_mut(self)
331 }
332}
333
David Tolnayf2cfd722017-12-31 18:02:51 -0500334impl<T, P> Default for Punctuated<T, P> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700335 fn default() -> Self {
David Tolnayf2cfd722017-12-31 18:02:51 -0500336 Punctuated::new()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700337 }
338}
339
David Tolnayf3198012018-01-06 20:00:42 -0800340/// An iterator over borrowed pairs of type `Pair<&T, &P>`.
341///
342/// Refer to the [module documentation] for details about punctuated sequences.
343///
344/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800345pub struct Pairs<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200346 inner: slice::Iter<'a, (T, P)>,
347 last: option::IntoIter<&'a T>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700348}
349
David Tolnay56080682018-01-06 14:01:52 -0800350impl<'a, T, P> Iterator for Pairs<'a, T, P> {
351 type Item = Pair<&'a T, &'a P>;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700352
David Tolnay6eff4da2018-01-01 20:27:45 -0800353 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200354 self.inner.next().map(|&(ref t, ref p)| Pair::Punctuated(t, p))
David Tolnay6e4a9c22018-04-11 12:20:08 -0700355 .or_else(|| self.last.next().map(Pair::End))
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700356 }
357}
358
David Tolnayf3198012018-01-06 20:00:42 -0800359/// An iterator over mutably borrowed pairs of type `Pair<&mut T, &mut P>`.
360///
361/// Refer to the [module documentation] for details about punctuated sequences.
362///
363/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800364pub struct PairsMut<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200365 inner: slice::IterMut<'a, (T, P)>,
366 last: option::IntoIter<&'a mut T>,
Alex Crichton164c5332017-07-06 13:18:34 -0700367}
368
David Tolnay56080682018-01-06 14:01:52 -0800369impl<'a, T, P> Iterator for PairsMut<'a, T, P> {
370 type Item = Pair<&'a mut T, &'a mut P>;
Alex Crichton164c5332017-07-06 13:18:34 -0700371
David Tolnay6eff4da2018-01-01 20:27:45 -0800372 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200373 self.inner.next().map(|&mut (ref mut t, ref mut p)| Pair::Punctuated(t, p))
David Tolnay6e4a9c22018-04-11 12:20:08 -0700374 .or_else(|| self.last.next().map(Pair::End))
Alex Crichton164c5332017-07-06 13:18:34 -0700375 }
376}
377
David Tolnayf3198012018-01-06 20:00:42 -0800378/// An iterator over owned pairs of type `Pair<T, P>`.
379///
380/// Refer to the [module documentation] for details about punctuated sequences.
381///
382/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800383pub struct IntoPairs<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200384 inner: vec::IntoIter<(T, P)>,
385 last: option::IntoIter<T>,
David Tolnay6eff4da2018-01-01 20:27:45 -0800386}
387
David Tolnay56080682018-01-06 14:01:52 -0800388impl<T, P> Iterator for IntoPairs<T, P> {
389 type Item = Pair<T, P>;
David Tolnay6eff4da2018-01-01 20:27:45 -0800390
391 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200392 self.inner.next().map(|(t, p)| Pair::Punctuated(t, p))
David Tolnay6e4a9c22018-04-11 12:20:08 -0700393 .or_else(|| self.last.next().map(Pair::End))
David Tolnay6eff4da2018-01-01 20:27:45 -0800394 }
395}
396
David Tolnayf3198012018-01-06 20:00:42 -0800397/// An iterator over owned values of type `T`.
398///
399/// Refer to the [module documentation] for details about punctuated sequences.
400///
401/// [module documentation]: index.html
David Tolnayf2cfd722017-12-31 18:02:51 -0500402pub struct IntoIter<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200403 inner: vec::IntoIter<(T, P)>,
404 last: option::IntoIter<T>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700405}
406
David Tolnayf2cfd722017-12-31 18:02:51 -0500407impl<T, P> Iterator for IntoIter<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800408 type Item = T;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700409
David Tolnay6eff4da2018-01-01 20:27:45 -0800410 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200411 self.inner.next().map(|pair| pair.0).or_else(|| self.last.next())
David Tolnay6eff4da2018-01-01 20:27:45 -0800412 }
413}
414
David Tolnayf3198012018-01-06 20:00:42 -0800415/// An iterator over borrowed values of type `&T`.
416///
417/// Refer to the [module documentation] for details about punctuated sequences.
418///
419/// [module documentation]: index.html
David Tolnay8095c302018-03-31 19:34:17 +0200420pub struct Iter<'a, T: 'a> {
421 inner: Box<Iterator<Item = &'a T> + 'a>,
422}
423
424struct PrivateIter<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200425 inner: slice::Iter<'a, (T, P)>,
426 last: option::IntoIter<&'a T>,
David Tolnay6eff4da2018-01-01 20:27:45 -0800427}
428
David Tolnay96a09d92018-01-16 22:24:03 -0800429#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay8095c302018-03-31 19:34:17 +0200430impl<'a, T> Iter<'a, T> {
David Tolnay96a09d92018-01-16 22:24:03 -0800431 // Not public API.
432 #[doc(hidden)]
433 pub fn private_empty() -> Self {
434 Iter {
David Tolnay8095c302018-03-31 19:34:17 +0200435 inner: Box::new(iter::empty()),
David Tolnay96a09d92018-01-16 22:24:03 -0800436 }
437 }
438}
439
David Tolnay8095c302018-03-31 19:34:17 +0200440impl<'a, T> Iterator for Iter<'a, T> {
441 type Item = &'a T;
442
443 fn next(&mut self) -> Option<Self::Item> {
444 self.inner.next()
445 }
446}
447
448impl<'a, T, P> Iterator for PrivateIter<'a, T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800449 type Item = &'a T;
450
451 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200452 self.inner.next().map(|pair| &pair.0).or_else(|| self.last.next())
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700453 }
454}
455
David Tolnayf3198012018-01-06 20:00:42 -0800456/// An iterator over mutably borrowed values of type `&mut T`.
457///
458/// Refer to the [module documentation] for details about punctuated sequences.
459///
460/// [module documentation]: index.html
David Tolnay8095c302018-03-31 19:34:17 +0200461pub struct IterMut<'a, T: 'a> {
462 inner: Box<Iterator<Item = &'a mut T> + 'a>,
463}
464
465struct PrivateIterMut<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200466 inner: slice::IterMut<'a, (T, P)>,
467 last: option::IntoIter<&'a mut T>,
David Tolnaya0834b42018-01-01 21:30:02 -0800468}
469
David Tolnay8095c302018-03-31 19:34:17 +0200470impl<'a, T> Iterator for IterMut<'a, T> {
471 type Item = &'a mut T;
472
473 fn next(&mut self) -> Option<Self::Item> {
474 self.inner.next()
475 }
476}
477
478impl<'a, T, P> Iterator for PrivateIterMut<'a, T, P> {
David Tolnaya0834b42018-01-01 21:30:02 -0800479 type Item = &'a mut T;
480
481 fn next(&mut self) -> Option<Self::Item> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200482 self.inner.next().map(|pair| &mut pair.0).or_else(|| self.last.next())
David Tolnaya0834b42018-01-01 21:30:02 -0800483 }
484}
485
David Tolnayf3198012018-01-06 20:00:42 -0800486/// A single syntax tree node of type `T` followed by its trailing punctuation
487/// of type `P` if any.
488///
489/// Refer to the [module documentation] for details about punctuated sequences.
490///
491/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800492pub enum Pair<T, P> {
David Tolnayf2cfd722017-12-31 18:02:51 -0500493 Punctuated(T, P),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700494 End(T),
495}
496
David Tolnay56080682018-01-06 14:01:52 -0800497impl<T, P> Pair<T, P> {
David Tolnayf3198012018-01-06 20:00:42 -0800498 /// Extracts the syntax tree node from this punctuated pair, discarding the
499 /// following punctuation.
David Tolnay56080682018-01-06 14:01:52 -0800500 pub fn into_value(self) -> T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700501 match self {
David Tolnay56080682018-01-06 14:01:52 -0800502 Pair::Punctuated(t, _) | Pair::End(t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700503 }
504 }
505
David Tolnayf3198012018-01-06 20:00:42 -0800506 /// Borrows the syntax tree node from this punctuated pair.
David Tolnay56080682018-01-06 14:01:52 -0800507 pub fn value(&self) -> &T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700508 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800509 Pair::Punctuated(ref t, _) | Pair::End(ref t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700510 }
511 }
512
David Tolnayf3198012018-01-06 20:00:42 -0800513 /// Mutably borrows the syntax tree node from this punctuated pair.
David Tolnay56080682018-01-06 14:01:52 -0800514 pub fn value_mut(&mut self) -> &mut T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700515 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800516 Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700517 }
518 }
519
David Tolnayf3198012018-01-06 20:00:42 -0800520 /// Borrows the punctuation from this punctuated pair, unless this pair is
521 /// the final one and there is no trailing punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500522 pub fn punct(&self) -> Option<&P> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700523 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800524 Pair::Punctuated(_, ref d) => Some(d),
525 Pair::End(_) => None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700526 }
527 }
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400528
David Tolnayf3198012018-01-06 20:00:42 -0800529 /// Creates a punctuated pair out of a syntax tree node and an optional
530 /// following punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500531 pub fn new(t: T, d: Option<P>) -> Self {
David Tolnay660fd1f2017-12-31 01:52:57 -0500532 match d {
David Tolnay56080682018-01-06 14:01:52 -0800533 Some(d) => Pair::Punctuated(t, d),
534 None => Pair::End(t),
David Tolnay660fd1f2017-12-31 01:52:57 -0500535 }
536 }
537
David Tolnayf3198012018-01-06 20:00:42 -0800538 /// Produces this punctuated pair as a tuple of syntax tree node and
539 /// optional following punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500540 pub fn into_tuple(self) -> (T, Option<P>) {
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400541 match self {
David Tolnay56080682018-01-06 14:01:52 -0800542 Pair::Punctuated(t, d) => (t, Some(d)),
543 Pair::End(t) => (t, None),
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400544 }
545 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700546}
547
David Tolnaybb987132018-01-08 13:51:19 -0800548impl<T, P> Index<usize> for Punctuated<T, P> {
549 type Output = T;
550
551 fn index(&self, index: usize) -> &Self::Output {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200552 if index == self.len() - 1 {
553 match self.last {
554 Some(ref t) => t,
555 None => &self.inner[index].0
556 }
557 } else {
558 &self.inner[index].0
559 }
David Tolnaybb987132018-01-08 13:51:19 -0800560 }
561}
562
563impl<T, P> IndexMut<usize> for Punctuated<T, P> {
564 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200565 if index == self.len() - 1 {
566 match self.last {
567 Some(ref mut t) => t,
568 None => &mut self.inner[index].0
569 }
570 } else {
571 &mut self.inner[index].0
572 }
David Tolnaybb987132018-01-08 13:51:19 -0800573 }
574}
575
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700576#[cfg(feature = "parsing")]
David Tolnayf3198012018-01-06 20:00:42 -0800577impl<T, P> Punctuated<T, P>
578where
579 T: Synom,
580 P: Synom,
581{
582 /// Parse **zero or more** syntax tree nodes with punctuation in between and
583 /// **no trailing** punctuation.
584 pub fn parse_separated(input: Cursor) -> PResult<Self> {
585 Self::parse_separated_with(input, T::parse)
586 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700587
David Tolnayf3198012018-01-06 20:00:42 -0800588 /// Parse **one or more** syntax tree nodes with punctuation in bewteen and
589 /// **no trailing** punctuation.
590 /// allowing trailing punctuation.
591 pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> {
592 Self::parse_separated_nonempty_with(input, T::parse)
593 }
Alex Crichton954046c2017-05-30 21:49:42 -0700594
David Tolnayf3198012018-01-06 20:00:42 -0800595 /// Parse **zero or more** syntax tree nodes with punctuation in between and
596 /// **optional trailing** punctuation.
597 pub fn parse_terminated(input: Cursor) -> PResult<Self> {
598 Self::parse_terminated_with(input, T::parse)
599 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700600
David Tolnayf3198012018-01-06 20:00:42 -0800601 /// Parse **one or more** syntax tree nodes with punctuation in between and
602 /// **optional trailing** punctuation.
603 pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> {
604 Self::parse_terminated_nonempty_with(input, T::parse)
605 }
606}
Nika Layzellb49a9e52017-12-05 13:31:52 -0500607
David Tolnayf3198012018-01-06 20:00:42 -0800608#[cfg(feature = "parsing")]
609impl<T, P> Punctuated<T, P>
610where
611 P: Synom,
612{
613 /// Parse **zero or more** syntax tree nodes using the given parser with
614 /// punctuation in between and **no trailing** punctuation.
615 pub fn parse_separated_with(
616 input: Cursor,
617 parse: fn(Cursor) -> PResult<T>,
618 ) -> PResult<Self> {
619 Self::parse(input, parse, false)
620 }
621
622 /// Parse **one or more** syntax tree nodes using the given parser with
623 /// punctuation in between and **no trailing** punctuation.
624 pub fn parse_separated_nonempty_with(
625 input: Cursor,
626 parse: fn(Cursor) -> PResult<T>,
627 ) -> PResult<Self> {
628 match Self::parse(input, parse, false) {
629 Ok((ref b, _)) if b.is_empty() => parse_error(),
630 other => other,
Nika Layzellb49a9e52017-12-05 13:31:52 -0500631 }
Alex Crichton954046c2017-05-30 21:49:42 -0700632 }
633
David Tolnayf3198012018-01-06 20:00:42 -0800634 /// Parse **zero or more** syntax tree nodes using the given parser with
635 /// punctuation in between and **optional trailing** punctuation.
636 pub fn parse_terminated_with(
637 input: Cursor,
638 parse: fn(Cursor) -> PResult<T>,
639 ) -> PResult<Self> {
640 Self::parse(input, parse, true)
641 }
642
643 /// Parse **one or more** syntax tree nodes using the given parser with
644 /// punctuation in between and **optional trailing** punctuation.
645 pub fn parse_terminated_nonempty_with(
646 input: Cursor,
647 parse: fn(Cursor) -> PResult<T>,
648 ) -> PResult<Self> {
649 match Self::parse(input, parse, true) {
650 Ok((ref b, _)) if b.is_empty() => parse_error(),
651 other => other,
David Tolnaydc03aec2017-12-30 01:54:18 -0500652 }
David Tolnayf3198012018-01-06 20:00:42 -0800653 }
David Tolnaydc03aec2017-12-30 01:54:18 -0500654
David Tolnayf3198012018-01-06 20:00:42 -0800655 fn parse(
656 mut input: Cursor,
657 parse: fn(Cursor) -> PResult<T>,
658 terminated: bool,
659 ) -> PResult<Self> {
660 let mut res = Punctuated::new();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700661
David Tolnayf3198012018-01-06 20:00:42 -0800662 // get the first element
663 match parse(input) {
664 Err(_) => Ok((res, input)),
665 Ok((o, i)) => {
666 if i == input {
667 return parse_error();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700668 }
David Tolnayf3198012018-01-06 20:00:42 -0800669 input = i;
670 res.push_value(o);
671
672 // get the separator first
673 while let Ok((s, i2)) = P::parse(input) {
674 if i2 == input {
675 break;
676 }
677
678 // get the element next
679 if let Ok((o3, i3)) = parse(i2) {
680 if i3 == i2 {
681 break;
682 }
683 res.push_punct(s);
684 res.push_value(o3);
685 input = i3;
686 } else {
687 break;
688 }
689 }
690 if terminated {
691 if let Ok((sep, after)) = P::parse(input) {
692 res.push_punct(sep);
693 input = after;
694 }
695 }
696 Ok((res, input))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700697 }
698 }
699 }
700}
701
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700702#[cfg(feature = "printing")]
703mod printing {
704 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500705 use quote::{ToTokens, Tokens};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700706
David Tolnayf2cfd722017-12-31 18:02:51 -0500707 impl<T, P> ToTokens for Punctuated<T, P>
David Tolnay51382052017-12-27 13:46:21 -0500708 where
709 T: ToTokens,
David Tolnayf2cfd722017-12-31 18:02:51 -0500710 P: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700711 {
712 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay56080682018-01-06 14:01:52 -0800713 tokens.append_all(self.pairs())
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700714 }
715 }
716
David Tolnay56080682018-01-06 14:01:52 -0800717 impl<T, P> ToTokens for Pair<T, P>
David Tolnay51382052017-12-27 13:46:21 -0500718 where
719 T: ToTokens,
David Tolnayf2cfd722017-12-31 18:02:51 -0500720 P: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700721 {
722 fn to_tokens(&self, tokens: &mut Tokens) {
723 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800724 Pair::Punctuated(ref a, ref b) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700725 a.to_tokens(tokens);
726 b.to_tokens(tokens);
727 }
David Tolnay56080682018-01-06 14:01:52 -0800728 Pair::End(ref a) => a.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700729 }
730 }
731 }
732}