blob: ca34d229598f64d8edeb55eede6d82723358e712 [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
Alex Crichtonccbb45d2017-05-23 10:58:24 -070030use std::iter::FromIterator;
31use std::slice;
32use std::vec;
Nika Layzelld73a3652017-10-24 08:57:05 -040033#[cfg(feature = "extra-traits")]
34use std::fmt::{self, Debug};
Alex Crichtonccbb45d2017-05-23 10:58:24 -070035
David Tolnayf3198012018-01-06 20:00:42 -080036#[cfg(feature = "parsing")]
37use synom::{Synom, PResult};
38#[cfg(feature = "parsing")]
39use buffer::Cursor;
40#[cfg(feature = "parsing")]
41use parse_error;
42
43/// A punctuated sequence of syntax tree nodes of type `T` separated by
44/// punctuation of type `P`.
45///
46/// Refer to the [module documentation] for details about punctuated sequences.
47///
48/// [module documentation]: index.html
Nika Layzelld73a3652017-10-24 08:57:05 -040049#[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070050#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayf2cfd722017-12-31 18:02:51 -050051pub struct Punctuated<T, P> {
52 inner: Vec<(T, Option<P>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070053}
54
David Tolnayf2cfd722017-12-31 18:02:51 -050055impl<T, P> Punctuated<T, P> {
David Tolnayf3198012018-01-06 20:00:42 -080056 /// Creates an empty punctuated sequence.
David Tolnayf2cfd722017-12-31 18:02:51 -050057 pub fn new() -> Punctuated<T, P> {
58 Punctuated { inner: Vec::new() }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070059 }
60
David Tolnayf3198012018-01-06 20:00:42 -080061 /// Determines whether this punctuated sequence is empty, meaning it
62 /// contains no syntax tree nodes or punctuation.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070063 pub fn is_empty(&self) -> bool {
64 self.inner.len() == 0
65 }
66
David Tolnayf3198012018-01-06 20:00:42 -080067 /// Returns the number of syntax tree nodes in this punctuated sequence.
68 ///
69 /// This is the number of nodes of type `T`, not counting the punctuation of
70 /// type `P`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070071 pub fn len(&self) -> usize {
72 self.inner.len()
73 }
74
David Tolnayf3198012018-01-06 20:00:42 -080075 /// Borrows the first punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080076 pub fn first(&self) -> Option<Pair<&T, &P>> {
David Tolnay51382052017-12-27 13:46:21 -050077 self.inner.first().map(|&(ref t, ref d)| match *d {
David Tolnay56080682018-01-06 14:01:52 -080078 Some(ref d) => Pair::Punctuated(t, d),
79 None => Pair::End(t),
Alex Crichton0aa50e02017-07-07 20:59:03 -070080 })
81 }
82
David Tolnayf3198012018-01-06 20:00:42 -080083 /// Borrows the last punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080084 pub fn last(&self) -> Option<Pair<&T, &P>> {
David Tolnay51382052017-12-27 13:46:21 -050085 self.inner.last().map(|&(ref t, ref d)| match *d {
David Tolnay56080682018-01-06 14:01:52 -080086 Some(ref d) => Pair::Punctuated(t, d),
87 None => Pair::End(t),
Alex Crichton0aa50e02017-07-07 20:59:03 -070088 })
89 }
90
David Tolnayf3198012018-01-06 20:00:42 -080091 /// Mutably borrows the last punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080092 pub fn last_mut(&mut self) -> Option<Pair<&mut T, &mut P>> {
David Tolnay51382052017-12-27 13:46:21 -050093 self.inner
94 .last_mut()
95 .map(|&mut (ref mut t, ref mut d)| match *d {
David Tolnay56080682018-01-06 14:01:52 -080096 Some(ref mut d) => Pair::Punctuated(t, d),
97 None => Pair::End(t),
David Tolnay51382052017-12-27 13:46:21 -050098 })
Alex Crichton0aa50e02017-07-07 20:59:03 -070099 }
100
David Tolnayf3198012018-01-06 20:00:42 -0800101 /// Returns an iterator over borrowed syntax tree nodes of type `&T`.
David Tolnayf2cfd722017-12-31 18:02:51 -0500102 pub fn iter(&self) -> Iter<T, P> {
David Tolnay51382052017-12-27 13:46:21 -0500103 Iter {
104 inner: self.inner.iter(),
105 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700106 }
107
David Tolnayf3198012018-01-06 20:00:42 -0800108 /// Returns an iterator over mutably borrowed syntax tree nodes of type
109 /// `&mut T`.
David Tolnaya0834b42018-01-01 21:30:02 -0800110 pub fn iter_mut(&mut self) -> IterMut<T, P> {
111 IterMut {
112 inner: self.inner.iter_mut(),
113 }
114 }
115
David Tolnayf3198012018-01-06 20:00:42 -0800116 /// Returns an iterator over the contents of this sequence as borrowed
117 /// punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800118 pub fn pairs(&self) -> Pairs<T, P> {
119 Pairs {
David Tolnay6eff4da2018-01-01 20:27:45 -0800120 inner: self.inner.iter(),
121 }
122 }
123
David Tolnayf3198012018-01-06 20:00:42 -0800124 /// Returns an iterator over the contents of this sequence as mutably
125 /// borrowed punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800126 pub fn pairs_mut(&mut self) -> PairsMut<T, P> {
127 PairsMut {
David Tolnay51382052017-12-27 13:46:21 -0500128 inner: self.inner.iter_mut(),
129 }
Alex Crichton164c5332017-07-06 13:18:34 -0700130 }
131
David Tolnayf3198012018-01-06 20:00:42 -0800132 /// Returns an iterator over the contents of this sequence as owned
133 /// punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800134 pub fn into_pairs(self) -> IntoPairs<T, P> {
135 IntoPairs {
David Tolnay6eff4da2018-01-01 20:27:45 -0800136 inner: self.inner.into_iter(),
137 }
138 }
139
David Tolnayf3198012018-01-06 20:00:42 -0800140 /// Appends a syntax tree node onto the end of this punctuated sequence. The
141 /// sequence must previously have a trailing punctuation.
142 ///
143 /// Use [`push`] instead if the punctuated sequence may or may not already
144 /// have trailing punctuation.
145 ///
146 /// [`push`]: #method.push
147 ///
148 /// # Panics
149 ///
150 /// Panics if the sequence does not already have a trailing punctuation when
151 /// this method is called.
David Tolnay56080682018-01-06 14:01:52 -0800152 pub fn push_value(&mut self, value: T) {
David Tolnaydc03aec2017-12-30 01:54:18 -0500153 assert!(self.empty_or_trailing());
David Tolnay56080682018-01-06 14:01:52 -0800154 self.inner.push((value, None));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700155 }
156
David Tolnayf3198012018-01-06 20:00:42 -0800157 /// Appends a trailing punctuation onto the end of this punctuated sequence.
158 /// The sequence must be non-empty and must not already have trailing
159 /// punctuation.
160 ///
161 /// # Panics
162 ///
163 /// Panics if the sequence is empty or already has a trailing punctuation.
David Tolnaya0834b42018-01-01 21:30:02 -0800164 pub fn push_punct(&mut self, punctuation: P) {
David Tolnay660fd1f2017-12-31 01:52:57 -0500165 assert!(!self.is_empty());
166 let last = self.inner.last_mut().unwrap();
167 assert!(last.1.is_none());
David Tolnayf2cfd722017-12-31 18:02:51 -0500168 last.1 = Some(punctuation);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700169 }
170
David Tolnayf3198012018-01-06 20:00:42 -0800171 /// Removes the last punctuated pair from this sequence, or `None` if the
172 /// sequence is empty.
David Tolnay56080682018-01-06 14:01:52 -0800173 pub fn pop(&mut self) -> Option<Pair<T, P>> {
174 self.inner.pop().map(|(t, d)| Pair::new(t, d))
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700175 }
176
David Tolnayf3198012018-01-06 20:00:42 -0800177 /// Determines whether this punctuated sequence ends with a trailing
178 /// punctuation.
David Tolnaya0834b42018-01-01 21:30:02 -0800179 pub fn trailing_punct(&self) -> bool {
David Tolnay61037c62018-01-05 16:21:03 -0800180 self.inner
181 .last()
182 .map(|last| last.1.is_some())
183 .unwrap_or(false)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700184 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400185
David Tolnayf2cfd722017-12-31 18:02:51 -0500186 /// Returns true if either this `Punctuated` is empty, or it has a trailing
187 /// punctuation.
David Tolnaydc03aec2017-12-30 01:54:18 -0500188 ///
David Tolnaya0834b42018-01-01 21:30:02 -0800189 /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`.
Michael Layzell3936ceb2017-07-08 00:28:36 -0400190 pub fn empty_or_trailing(&self) -> bool {
David Tolnay61037c62018-01-05 16:21:03 -0800191 self.inner
192 .last()
193 .map(|last| last.1.is_some())
194 .unwrap_or(true)
Michael Layzell3936ceb2017-07-08 00:28:36 -0400195 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700196}
197
David Tolnaya0834b42018-01-01 21:30:02 -0800198impl<T, P> Punctuated<T, P>
199where
200 P: Default,
201{
David Tolnayf3198012018-01-06 20:00:42 -0800202 /// Appends a syntax tree node onto the end of this punctuated sequence.
203 ///
204 /// If there is not a trailing punctuation in this sequence when this method
205 /// is called, the default value of punctuation type `P` is inserted before
206 /// the given value of type `T`.
David Tolnay56080682018-01-06 14:01:52 -0800207 pub fn push(&mut self, value: T) {
David Tolnaya0834b42018-01-01 21:30:02 -0800208 if !self.empty_or_trailing() {
209 self.push_punct(Default::default());
210 }
David Tolnay56080682018-01-06 14:01:52 -0800211 self.push_value(value);
David Tolnaya0834b42018-01-01 21:30:02 -0800212 }
213}
214
Nika Layzelld73a3652017-10-24 08:57:05 -0400215#[cfg(feature = "extra-traits")]
David Tolnayf2cfd722017-12-31 18:02:51 -0500216impl<T: Debug, P: Debug> Debug for Punctuated<T, P> {
Nika Layzelld73a3652017-10-24 08:57:05 -0400217 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
218 self.inner.fmt(f)
219 }
220}
221
David Tolnay56080682018-01-06 14:01:52 -0800222impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> {
223 fn from_iter<I: IntoIterator<Item = Pair<T, P>>>(i: I) -> Self {
David Tolnayf2cfd722017-12-31 18:02:51 -0500224 let mut ret = Punctuated::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700225 ret.extend(i);
Alex Crichton954046c2017-05-30 21:49:42 -0700226 ret
227 }
228}
229
David Tolnay56080682018-01-06 14:01:52 -0800230impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> {
231 fn extend<I: IntoIterator<Item = Pair<T, P>>>(&mut self, i: I) {
232 for pair in i {
233 match pair {
234 Pair::Punctuated(a, b) => self.inner.push((a, Some(b))),
235 Pair::End(a) => self.inner.push((a, None)),
Alex Crichton24f12822017-07-14 07:15:32 -0700236 }
237 }
238 }
239}
240
David Tolnayf2cfd722017-12-31 18:02:51 -0500241impl<T, P> IntoIterator for Punctuated<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800242 type Item = T;
David Tolnayf2cfd722017-12-31 18:02:51 -0500243 type IntoIter = IntoIter<T, P>;
Alex Crichton954046c2017-05-30 21:49:42 -0700244
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500245 fn into_iter(self) -> Self::IntoIter {
David Tolnay51382052017-12-27 13:46:21 -0500246 IntoIter {
247 inner: self.inner.into_iter(),
248 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700249 }
250}
251
David Tolnay6eff4da2018-01-01 20:27:45 -0800252impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> {
253 type Item = &'a T;
254 type IntoIter = Iter<'a, T, P>;
255
256 fn into_iter(self) -> Self::IntoIter {
257 Punctuated::iter(self)
258 }
259}
260
David Tolnaya0834b42018-01-01 21:30:02 -0800261impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> {
262 type Item = &'a mut T;
263 type IntoIter = IterMut<'a, T, P>;
264
265 fn into_iter(self) -> Self::IntoIter {
266 Punctuated::iter_mut(self)
267 }
268}
269
David Tolnayf2cfd722017-12-31 18:02:51 -0500270impl<T, P> Default for Punctuated<T, P> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700271 fn default() -> Self {
David Tolnayf2cfd722017-12-31 18:02:51 -0500272 Punctuated::new()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700273 }
274}
275
David Tolnayf3198012018-01-06 20:00:42 -0800276/// An iterator over borrowed pairs of type `Pair<&T, &P>`.
277///
278/// Refer to the [module documentation] for details about punctuated sequences.
279///
280/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800281pub struct Pairs<'a, T: 'a, P: 'a> {
David Tolnayf2cfd722017-12-31 18:02:51 -0500282 inner: slice::Iter<'a, (T, Option<P>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700283}
284
David Tolnay56080682018-01-06 14:01:52 -0800285impl<'a, T, P> Iterator for Pairs<'a, T, P> {
286 type Item = Pair<&'a T, &'a P>;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700287
David Tolnay6eff4da2018-01-01 20:27:45 -0800288 fn next(&mut self) -> Option<Self::Item> {
David Tolnay51382052017-12-27 13:46:21 -0500289 self.inner.next().map(|pair| match pair.1 {
David Tolnay56080682018-01-06 14:01:52 -0800290 Some(ref p) => Pair::Punctuated(&pair.0, p),
291 None => Pair::End(&pair.0),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700292 })
293 }
294}
295
David Tolnayf3198012018-01-06 20:00:42 -0800296/// An iterator over mutably borrowed pairs of type `Pair<&mut T, &mut P>`.
297///
298/// Refer to the [module documentation] for details about punctuated sequences.
299///
300/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800301pub struct PairsMut<'a, T: 'a, P: 'a> {
David Tolnayf2cfd722017-12-31 18:02:51 -0500302 inner: slice::IterMut<'a, (T, Option<P>)>,
Alex Crichton164c5332017-07-06 13:18:34 -0700303}
304
David Tolnay56080682018-01-06 14:01:52 -0800305impl<'a, T, P> Iterator for PairsMut<'a, T, P> {
306 type Item = Pair<&'a mut T, &'a mut P>;
Alex Crichton164c5332017-07-06 13:18:34 -0700307
David Tolnay6eff4da2018-01-01 20:27:45 -0800308 fn next(&mut self) -> Option<Self::Item> {
David Tolnay51382052017-12-27 13:46:21 -0500309 self.inner.next().map(|pair| match pair.1 {
David Tolnay56080682018-01-06 14:01:52 -0800310 Some(ref mut p) => Pair::Punctuated(&mut pair.0, p),
311 None => Pair::End(&mut pair.0),
Alex Crichton164c5332017-07-06 13:18:34 -0700312 })
313 }
314}
315
David Tolnayf3198012018-01-06 20:00:42 -0800316/// An iterator over owned pairs of type `Pair<T, P>`.
317///
318/// Refer to the [module documentation] for details about punctuated sequences.
319///
320/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800321pub struct IntoPairs<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800322 inner: vec::IntoIter<(T, Option<P>)>,
323}
324
David Tolnay56080682018-01-06 14:01:52 -0800325impl<T, P> Iterator for IntoPairs<T, P> {
326 type Item = Pair<T, P>;
David Tolnay6eff4da2018-01-01 20:27:45 -0800327
328 fn next(&mut self) -> Option<Self::Item> {
329 self.inner.next().map(|pair| match pair.1 {
David Tolnay56080682018-01-06 14:01:52 -0800330 Some(p) => Pair::Punctuated(pair.0, p),
331 None => Pair::End(pair.0),
David Tolnay6eff4da2018-01-01 20:27:45 -0800332 })
333 }
334}
335
David Tolnayf3198012018-01-06 20:00:42 -0800336/// An iterator over owned values of type `T`.
337///
338/// Refer to the [module documentation] for details about punctuated sequences.
339///
340/// [module documentation]: index.html
David Tolnayf2cfd722017-12-31 18:02:51 -0500341pub struct IntoIter<T, P> {
342 inner: vec::IntoIter<(T, Option<P>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700343}
344
David Tolnayf2cfd722017-12-31 18:02:51 -0500345impl<T, P> Iterator for IntoIter<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800346 type Item = T;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700347
David Tolnay6eff4da2018-01-01 20:27:45 -0800348 fn next(&mut self) -> Option<Self::Item> {
349 self.inner.next().map(|pair| pair.0)
350 }
351}
352
David Tolnayf3198012018-01-06 20:00:42 -0800353/// An iterator over borrowed values of type `&T`.
354///
355/// Refer to the [module documentation] for details about punctuated sequences.
356///
357/// [module documentation]: index.html
David Tolnay6eff4da2018-01-01 20:27:45 -0800358pub struct Iter<'a, T: 'a, P: 'a> {
359 inner: slice::Iter<'a, (T, Option<P>)>,
360}
361
362impl<'a, T, P> Iterator for Iter<'a, T, P> {
363 type Item = &'a T;
364
365 fn next(&mut self) -> Option<Self::Item> {
366 self.inner.next().map(|pair| &pair.0)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700367 }
368}
369
David Tolnayf3198012018-01-06 20:00:42 -0800370/// An iterator over mutably borrowed values of type `&mut T`.
371///
372/// Refer to the [module documentation] for details about punctuated sequences.
373///
374/// [module documentation]: index.html
David Tolnaya0834b42018-01-01 21:30:02 -0800375pub struct IterMut<'a, T: 'a, P: 'a> {
376 inner: slice::IterMut<'a, (T, Option<P>)>,
377}
378
379impl<'a, T, P> Iterator for IterMut<'a, T, P> {
380 type Item = &'a mut T;
381
382 fn next(&mut self) -> Option<Self::Item> {
383 self.inner.next().map(|pair| &mut pair.0)
384 }
385}
386
David Tolnayf3198012018-01-06 20:00:42 -0800387/// A single syntax tree node of type `T` followed by its trailing punctuation
388/// of type `P` if any.
389///
390/// Refer to the [module documentation] for details about punctuated sequences.
391///
392/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800393pub enum Pair<T, P> {
David Tolnayf2cfd722017-12-31 18:02:51 -0500394 Punctuated(T, P),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700395 End(T),
396}
397
David Tolnay56080682018-01-06 14:01:52 -0800398impl<T, P> Pair<T, P> {
David Tolnayf3198012018-01-06 20:00:42 -0800399 /// Extracts the syntax tree node from this punctuated pair, discarding the
400 /// following punctuation.
David Tolnay56080682018-01-06 14:01:52 -0800401 pub fn into_value(self) -> T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700402 match self {
David Tolnay56080682018-01-06 14:01:52 -0800403 Pair::Punctuated(t, _) | Pair::End(t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700404 }
405 }
406
David Tolnayf3198012018-01-06 20:00:42 -0800407 /// Borrows the syntax tree node from this punctuated pair.
David Tolnay56080682018-01-06 14:01:52 -0800408 pub fn value(&self) -> &T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700409 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800410 Pair::Punctuated(ref t, _) | Pair::End(ref t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700411 }
412 }
413
David Tolnayf3198012018-01-06 20:00:42 -0800414 /// Mutably borrows the syntax tree node from this punctuated pair.
David Tolnay56080682018-01-06 14:01:52 -0800415 pub fn value_mut(&mut self) -> &mut T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700416 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800417 Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700418 }
419 }
420
David Tolnayf3198012018-01-06 20:00:42 -0800421 /// Borrows the punctuation from this punctuated pair, unless this pair is
422 /// the final one and there is no trailing punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500423 pub fn punct(&self) -> Option<&P> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700424 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800425 Pair::Punctuated(_, ref d) => Some(d),
426 Pair::End(_) => None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700427 }
428 }
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400429
David Tolnayf3198012018-01-06 20:00:42 -0800430 /// Creates a punctuated pair out of a syntax tree node and an optional
431 /// following punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500432 pub fn new(t: T, d: Option<P>) -> Self {
David Tolnay660fd1f2017-12-31 01:52:57 -0500433 match d {
David Tolnay56080682018-01-06 14:01:52 -0800434 Some(d) => Pair::Punctuated(t, d),
435 None => Pair::End(t),
David Tolnay660fd1f2017-12-31 01:52:57 -0500436 }
437 }
438
David Tolnayf3198012018-01-06 20:00:42 -0800439 /// Produces this punctuated pair as a tuple of syntax tree node and
440 /// optional following punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500441 pub fn into_tuple(self) -> (T, Option<P>) {
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400442 match self {
David Tolnay56080682018-01-06 14:01:52 -0800443 Pair::Punctuated(t, d) => (t, Some(d)),
444 Pair::End(t) => (t, None),
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400445 }
446 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700447}
448
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700449#[cfg(feature = "parsing")]
David Tolnayf3198012018-01-06 20:00:42 -0800450impl<T, P> Punctuated<T, P>
451where
452 T: Synom,
453 P: Synom,
454{
455 /// Parse **zero or more** syntax tree nodes with punctuation in between and
456 /// **no trailing** punctuation.
457 pub fn parse_separated(input: Cursor) -> PResult<Self> {
458 Self::parse_separated_with(input, T::parse)
459 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700460
David Tolnayf3198012018-01-06 20:00:42 -0800461 /// Parse **one or more** syntax tree nodes with punctuation in bewteen and
462 /// **no trailing** punctuation.
463 /// allowing trailing punctuation.
464 pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> {
465 Self::parse_separated_nonempty_with(input, T::parse)
466 }
Alex Crichton954046c2017-05-30 21:49:42 -0700467
David Tolnayf3198012018-01-06 20:00:42 -0800468 /// Parse **zero or more** syntax tree nodes with punctuation in between and
469 /// **optional trailing** punctuation.
470 pub fn parse_terminated(input: Cursor) -> PResult<Self> {
471 Self::parse_terminated_with(input, T::parse)
472 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700473
David Tolnayf3198012018-01-06 20:00:42 -0800474 /// Parse **one or more** syntax tree nodes with punctuation in between and
475 /// **optional trailing** punctuation.
476 pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> {
477 Self::parse_terminated_nonempty_with(input, T::parse)
478 }
479}
Nika Layzellb49a9e52017-12-05 13:31:52 -0500480
David Tolnayf3198012018-01-06 20:00:42 -0800481#[cfg(feature = "parsing")]
482impl<T, P> Punctuated<T, P>
483where
484 P: Synom,
485{
486 /// Parse **zero or more** syntax tree nodes using the given parser with
487 /// punctuation in between and **no trailing** punctuation.
488 pub fn parse_separated_with(
489 input: Cursor,
490 parse: fn(Cursor) -> PResult<T>,
491 ) -> PResult<Self> {
492 Self::parse(input, parse, false)
493 }
494
495 /// Parse **one or more** syntax tree nodes using the given parser with
496 /// punctuation in between and **no trailing** punctuation.
497 pub fn parse_separated_nonempty_with(
498 input: Cursor,
499 parse: fn(Cursor) -> PResult<T>,
500 ) -> PResult<Self> {
501 match Self::parse(input, parse, false) {
502 Ok((ref b, _)) if b.is_empty() => parse_error(),
503 other => other,
Nika Layzellb49a9e52017-12-05 13:31:52 -0500504 }
Alex Crichton954046c2017-05-30 21:49:42 -0700505 }
506
David Tolnayf3198012018-01-06 20:00:42 -0800507 /// Parse **zero or more** syntax tree nodes using the given parser with
508 /// punctuation in between and **optional trailing** punctuation.
509 pub fn parse_terminated_with(
510 input: Cursor,
511 parse: fn(Cursor) -> PResult<T>,
512 ) -> PResult<Self> {
513 Self::parse(input, parse, true)
514 }
515
516 /// Parse **one or more** syntax tree nodes using the given parser with
517 /// punctuation in between and **optional trailing** punctuation.
518 pub fn parse_terminated_nonempty_with(
519 input: Cursor,
520 parse: fn(Cursor) -> PResult<T>,
521 ) -> PResult<Self> {
522 match Self::parse(input, parse, true) {
523 Ok((ref b, _)) if b.is_empty() => parse_error(),
524 other => other,
David Tolnaydc03aec2017-12-30 01:54:18 -0500525 }
David Tolnayf3198012018-01-06 20:00:42 -0800526 }
David Tolnaydc03aec2017-12-30 01:54:18 -0500527
David Tolnayf3198012018-01-06 20:00:42 -0800528 fn parse(
529 mut input: Cursor,
530 parse: fn(Cursor) -> PResult<T>,
531 terminated: bool,
532 ) -> PResult<Self> {
533 let mut res = Punctuated::new();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700534
David Tolnayf3198012018-01-06 20:00:42 -0800535 // get the first element
536 match parse(input) {
537 Err(_) => Ok((res, input)),
538 Ok((o, i)) => {
539 if i == input {
540 return parse_error();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700541 }
David Tolnayf3198012018-01-06 20:00:42 -0800542 input = i;
543 res.push_value(o);
544
545 // get the separator first
546 while let Ok((s, i2)) = P::parse(input) {
547 if i2 == input {
548 break;
549 }
550
551 // get the element next
552 if let Ok((o3, i3)) = parse(i2) {
553 if i3 == i2 {
554 break;
555 }
556 res.push_punct(s);
557 res.push_value(o3);
558 input = i3;
559 } else {
560 break;
561 }
562 }
563 if terminated {
564 if let Ok((sep, after)) = P::parse(input) {
565 res.push_punct(sep);
566 input = after;
567 }
568 }
569 Ok((res, input))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700570 }
571 }
572 }
573}
574
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700575#[cfg(feature = "printing")]
576mod printing {
577 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500578 use quote::{ToTokens, Tokens};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700579
David Tolnayf2cfd722017-12-31 18:02:51 -0500580 impl<T, P> ToTokens for Punctuated<T, P>
David Tolnay51382052017-12-27 13:46:21 -0500581 where
582 T: ToTokens,
David Tolnayf2cfd722017-12-31 18:02:51 -0500583 P: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700584 {
585 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay56080682018-01-06 14:01:52 -0800586 tokens.append_all(self.pairs())
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700587 }
588 }
589
David Tolnay56080682018-01-06 14:01:52 -0800590 impl<T, P> ToTokens for Pair<T, P>
David Tolnay51382052017-12-27 13:46:21 -0500591 where
592 T: ToTokens,
David Tolnayf2cfd722017-12-31 18:02:51 -0500593 P: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700594 {
595 fn to_tokens(&self, tokens: &mut Tokens) {
596 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800597 Pair::Punctuated(ref a, ref b) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700598 a.to_tokens(tokens);
599 b.to_tokens(tokens);
600 }
David Tolnay56080682018-01-06 14:01:52 -0800601 Pair::End(ref a) => a.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700602 }
603 }
604 }
605}