blob: 98c5b43696ba80124087b5d405b4176a35cb8c11 [file] [log] [blame]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001use std::iter::FromIterator;
2use std::slice;
3use std::vec;
Nika Layzelld73a3652017-10-24 08:57:05 -04004#[cfg(feature = "extra-traits")]
5use std::fmt::{self, Debug};
Alex Crichtonccbb45d2017-05-23 10:58:24 -07006
Nika Layzelld73a3652017-10-24 08:57:05 -04007#[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -07008#[cfg_attr(feature = "clone-impls", derive(Clone))]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07009pub struct Delimited<T, D> {
David Tolnay51382052017-12-27 13:46:21 -050010 inner: Vec<(T, Option<D>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070011}
12
13impl<T, D> Delimited<T, D> {
14 pub fn new() -> Delimited<T, D> {
David Tolnay51382052017-12-27 13:46:21 -050015 Delimited { inner: Vec::new() }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070016 }
17
18 pub fn is_empty(&self) -> bool {
19 self.inner.len() == 0
20 }
21
22 pub fn len(&self) -> usize {
23 self.inner.len()
24 }
25
26 pub fn get(&self, idx: usize) -> Element<&T, &D> {
27 let (ref t, ref d) = self.inner[idx];
28 match *d {
29 Some(ref d) => Element::Delimited(t, d),
30 None => Element::End(t),
31 }
32 }
33
34 pub fn get_mut(&mut self, idx: usize) -> Element<&mut T, &mut D> {
35 let (ref mut t, ref mut d) = self.inner[idx];
36 match *d {
37 Some(ref mut d) => Element::Delimited(t, d),
38 None => Element::End(t),
39 }
40 }
41
Alex Crichton0aa50e02017-07-07 20:59:03 -070042 pub fn first(&self) -> Option<Element<&T, &D>> {
David Tolnay51382052017-12-27 13:46:21 -050043 self.inner.first().map(|&(ref t, ref d)| match *d {
44 Some(ref d) => Element::Delimited(t, d),
45 None => Element::End(t),
Alex Crichton0aa50e02017-07-07 20:59:03 -070046 })
47 }
48
49 pub fn first_mut(&mut self) -> Option<Element<&mut T, &mut D>> {
David Tolnay51382052017-12-27 13:46:21 -050050 self.inner
51 .first_mut()
52 .map(|&mut (ref mut t, ref mut d)| match *d {
Alex Crichton0aa50e02017-07-07 20:59:03 -070053 Some(ref mut d) => Element::Delimited(t, d),
54 None => Element::End(t),
David Tolnay51382052017-12-27 13:46:21 -050055 })
Alex Crichton0aa50e02017-07-07 20:59:03 -070056 }
57
58 pub fn last(&self) -> Option<Element<&T, &D>> {
David Tolnay51382052017-12-27 13:46:21 -050059 self.inner.last().map(|&(ref t, ref d)| match *d {
60 Some(ref d) => Element::Delimited(t, d),
61 None => Element::End(t),
Alex Crichton0aa50e02017-07-07 20:59:03 -070062 })
63 }
64
65 pub fn last_mut(&mut self) -> Option<Element<&mut T, &mut D>> {
David Tolnay51382052017-12-27 13:46:21 -050066 self.inner
67 .last_mut()
68 .map(|&mut (ref mut t, ref mut d)| match *d {
Alex Crichton0aa50e02017-07-07 20:59:03 -070069 Some(ref mut d) => Element::Delimited(t, d),
70 None => Element::End(t),
David Tolnay51382052017-12-27 13:46:21 -050071 })
Alex Crichton0aa50e02017-07-07 20:59:03 -070072 }
73
Alex Crichtonccbb45d2017-05-23 10:58:24 -070074 pub fn iter(&self) -> Iter<T, D> {
David Tolnay51382052017-12-27 13:46:21 -050075 Iter {
76 inner: self.inner.iter(),
77 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070078 }
79
Alex Crichton164c5332017-07-06 13:18:34 -070080 pub fn iter_mut(&mut self) -> IterMut<T, D> {
David Tolnay51382052017-12-27 13:46:21 -050081 IterMut {
82 inner: self.inner.iter_mut(),
83 }
Alex Crichton164c5332017-07-06 13:18:34 -070084 }
85
Alex Crichtonccbb45d2017-05-23 10:58:24 -070086 pub fn items(&self) -> Items<T, D> {
David Tolnay51382052017-12-27 13:46:21 -050087 Items {
88 inner: self.inner.iter(),
89 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070090 }
91
92 pub fn push(&mut self, token: Element<T, D>) {
Alex Crichton954046c2017-05-30 21:49:42 -070093 assert!(self.is_empty() || self.trailing_delim());
Alex Crichtonccbb45d2017-05-23 10:58:24 -070094 match token {
95 Element::Delimited(t, d) => self.inner.push((t, Some(d))),
96 Element::End(t) => self.inner.push((t, None)),
97 }
98 }
99
100 pub fn push_first(&mut self, token: T) {
101 assert!(self.is_empty());
102 self.inner.push((token, None));
103 }
104
105 pub fn push_next(&mut self, token: T, delimiter: D) {
106 self.push_trailing(delimiter);
107 self.inner.push((token, None));
108 }
109
110 pub fn push_trailing(&mut self, delimiter: D) {
111 let len = self.len();
112 assert!(self.inner[len - 1].1.is_none());
113 self.inner[len - 1].1 = Some(delimiter);
114 }
115
David Tolnay51382052017-12-27 13:46:21 -0500116 pub fn push_default(&mut self, token: T)
117 where
118 D: Default,
119 {
Alex Crichton337cd462017-07-06 14:47:25 -0700120 if self.is_empty() || self.trailing_delim() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700121 self.inner.push((token, None));
122 } else {
123 self.push_next(token, D::default());
124 }
125 }
126
127 pub fn pop(&mut self) -> Option<Element<T, D>> {
David Tolnay51382052017-12-27 13:46:21 -0500128 self.inner.pop().map(|e| match e {
129 (t, Some(d)) => Element::Delimited(t, d),
130 (t, None) => Element::End(t),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700131 })
132 }
133
134 pub fn into_vec(self) -> Vec<T> {
135 self.inner.into_iter().map(|t| t.0).collect()
136 }
137
138 pub fn trailing_delim(&self) -> bool {
139 self.inner[self.inner.len() - 1].1.is_some()
140 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400141
142 /// Returns true if either this `Delimited` is empty, or it has a trailing
143 /// delimiter. This is useful within `ToTokens` implementations for `syn`.
144 #[doc(hidden)]
145 pub fn empty_or_trailing(&self) -> bool {
146 self.is_empty() || self.trailing_delim()
147 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700148}
149
Nika Layzelld73a3652017-10-24 08:57:05 -0400150#[cfg(feature = "extra-traits")]
151impl<T: Debug, D: Debug> Debug for Delimited<T, D> {
152 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153 self.inner.fmt(f)
154 }
155}
156
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700157impl<T, D> From<Vec<(T, Option<D>)>> for Delimited<T, D> {
158 fn from(v: Vec<(T, Option<D>)>) -> Self {
David Tolnay51382052017-12-27 13:46:21 -0500159 Delimited { inner: v }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700160 }
161}
162
163impl<T, D> From<Vec<T>> for Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500164where
165 D: Default,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700166{
167 fn from(v: Vec<T>) -> Self {
Alex Crichtonafb49d22017-07-06 14:47:37 -0700168 let len = v.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700169 Delimited {
David Tolnay51382052017-12-27 13:46:21 -0500170 inner: v.into_iter()
171 .enumerate()
172 .map(|(i, item)| {
173 (
174 item,
175 if i + 1 == len {
176 None
177 } else {
178 Some(D::default())
179 },
180 )
181 })
182 .collect(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700183 }
184 }
185}
186
187impl<T, D> FromIterator<Element<T, D>> for Delimited<T, D> {
188 fn from_iter<I: IntoIterator<Item = Element<T, D>>>(i: I) -> Self {
189 let mut ret = Delimited::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700190 ret.extend(i);
Alex Crichton954046c2017-05-30 21:49:42 -0700191 ret
192 }
193}
194
Alex Crichtond3743d12017-07-07 20:55:24 -0700195impl<T, D> FromIterator<T> for Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500196where
197 D: Default,
Alex Crichtond3743d12017-07-07 20:55:24 -0700198{
199 fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self {
200 let mut ret = Delimited::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700201 ret.extend(i);
Alex Crichtond3743d12017-07-07 20:55:24 -0700202 ret
203 }
204}
205
Alex Crichton24f12822017-07-14 07:15:32 -0700206impl<T, D> Extend<Element<T, D>> for Delimited<T, D> {
207 fn extend<I: IntoIterator<Item = Element<T, D>>>(&mut self, i: I) {
208 for element in i {
209 match element {
210 Element::Delimited(a, b) => self.inner.push((a, Some(b))),
211 Element::End(a) => self.inner.push((a, None)),
212 }
213 }
214 }
215}
216
217impl<T, D> Extend<T> for Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500218where
219 D: Default,
Alex Crichton24f12822017-07-14 07:15:32 -0700220{
221 fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) {
222 for element in i {
223 self.push_default(element);
224 }
225 }
226}
227
Alex Crichton954046c2017-05-30 21:49:42 -0700228impl<'a, T, D> IntoIterator for &'a Delimited<T, D> {
229 type Item = Element<&'a T, &'a D>;
230 type IntoIter = Iter<'a, T, D>;
231
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500232 fn into_iter(self) -> Self::IntoIter {
Alex Crichton954046c2017-05-30 21:49:42 -0700233 <Delimited<T, D>>::iter(self)
234 }
235}
236
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500237impl<'a, T, D> IntoIterator for &'a mut Delimited<T, D> {
238 type Item = Element<&'a mut T, &'a mut D>;
239 type IntoIter = IterMut<'a, T, D>;
240
241 fn into_iter(self) -> Self::IntoIter {
242 <Delimited<T, D>>::iter_mut(self)
243 }
244}
245
Alex Crichton954046c2017-05-30 21:49:42 -0700246impl<T, D> IntoIterator for Delimited<T, D> {
247 type Item = Element<T, D>;
248 type IntoIter = IntoIter<T, D>;
249
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500250 fn into_iter(self) -> Self::IntoIter {
David Tolnay51382052017-12-27 13:46:21 -0500251 IntoIter {
252 inner: self.inner.into_iter(),
253 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700254 }
255}
256
257impl<T, D> Default for Delimited<T, D> {
258 fn default() -> Self {
259 Delimited::new()
260 }
261}
262
263pub struct Iter<'a, T: 'a, D: 'a> {
264 inner: slice::Iter<'a, (T, Option<D>)>,
265}
266
267impl<'a, T, D> Iterator for Iter<'a, T, D> {
268 type Item = Element<&'a T, &'a D>;
269
270 fn next(&mut self) -> Option<Element<&'a T, &'a D>> {
David Tolnay51382052017-12-27 13:46:21 -0500271 self.inner.next().map(|pair| match pair.1 {
272 Some(ref delimited) => Element::Delimited(&pair.0, delimited),
273 None => Element::End(&pair.0),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700274 })
275 }
276}
277
Alex Crichton164c5332017-07-06 13:18:34 -0700278pub struct IterMut<'a, T: 'a, D: 'a> {
279 inner: slice::IterMut<'a, (T, Option<D>)>,
280}
281
282impl<'a, T, D> Iterator for IterMut<'a, T, D> {
283 type Item = Element<&'a mut T, &'a mut D>;
284
285 fn next(&mut self) -> Option<Element<&'a mut T, &'a mut D>> {
David Tolnay51382052017-12-27 13:46:21 -0500286 self.inner.next().map(|pair| match pair.1 {
287 Some(ref mut delimited) => Element::Delimited(&mut pair.0, delimited),
288 None => Element::End(&mut pair.0),
Alex Crichton164c5332017-07-06 13:18:34 -0700289 })
290 }
291}
292
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700293pub struct Items<'a, T: 'a, D: 'a> {
294 inner: slice::Iter<'a, (T, Option<D>)>,
295}
296
297impl<'a, T, D> Iterator for Items<'a, T, D> {
298 type Item = &'a T;
299
300 fn next(&mut self) -> Option<&'a T> {
301 self.inner.next().map(|pair| &pair.0)
302 }
303}
304
305pub struct IntoIter<T, D> {
306 inner: vec::IntoIter<(T, Option<D>)>,
307}
308
309impl<T, D> Iterator for IntoIter<T, D> {
310 type Item = Element<T, D>;
311
312 fn next(&mut self) -> Option<Element<T, D>> {
David Tolnay51382052017-12-27 13:46:21 -0500313 self.inner.next().map(|pair| match pair.1 {
314 Some(v) => Element::Delimited(pair.0, v),
315 None => Element::End(pair.0),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700316 })
317 }
318}
319
320pub enum Element<T, D> {
321 Delimited(T, D),
322 End(T),
323}
324
325impl<T, D> Element<T, D> {
326 pub fn into_item(self) -> T {
327 match self {
David Tolnay51382052017-12-27 13:46:21 -0500328 Element::Delimited(t, _) | Element::End(t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700329 }
330 }
331
332 pub fn item(&self) -> &T {
333 match *self {
David Tolnay51382052017-12-27 13:46:21 -0500334 Element::Delimited(ref t, _) | Element::End(ref t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700335 }
336 }
337
338 pub fn item_mut(&mut self) -> &mut T {
339 match *self {
David Tolnay51382052017-12-27 13:46:21 -0500340 Element::Delimited(ref mut t, _) | Element::End(ref mut t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700341 }
342 }
343
344 pub fn delimiter(&self) -> Option<&D> {
345 match *self {
346 Element::Delimited(_, ref d) => Some(d),
347 Element::End(_) => None,
348 }
349 }
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400350
351 pub fn into_tuple(self) -> (T, Option<D>) {
352 match self {
353 Element::Delimited(t, d) => (t, Some(d)),
354 Element::End(t) => (t, None),
355 }
356 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700357}
358
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700359#[cfg(feature = "parsing")]
360mod parsing {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700361 use super::Delimited;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500362 use synom::Synom;
363 use cursor::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500364 use parse_error;
365 use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700366
367 impl<T, D> Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500368 where
369 T: Synom,
370 D: Synom,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700371 {
David Tolnay51382052017-12-27 13:46:21 -0500372 pub fn parse_separated(input: Cursor) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700373 Self::parse(input, T::parse, false)
374 }
375
David Tolnay51382052017-12-27 13:46:21 -0500376 pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700377 Self::parse_separated_nonempty_with(input, T::parse)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700378 }
379
David Tolnay51382052017-12-27 13:46:21 -0500380 pub fn parse_terminated(input: Cursor) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700381 Self::parse_terminated_with(input, T::parse)
382 }
Nika Layzellb49a9e52017-12-05 13:31:52 -0500383
David Tolnay51382052017-12-27 13:46:21 -0500384 pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> {
Nika Layzellb49a9e52017-12-05 13:31:52 -0500385 Self::parse_terminated_nonempty_with(input, T::parse)
386 }
Alex Crichton954046c2017-05-30 21:49:42 -0700387 }
388
389 impl<T, D> Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500390 where
391 D: Synom,
Alex Crichton954046c2017-05-30 21:49:42 -0700392 {
393 pub fn parse_separated_nonempty_with(
David Tolnay51382052017-12-27 13:46:21 -0500394 input: Cursor,
395 parse: fn(Cursor) -> PResult<T>,
396 ) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700397 match Self::parse(input, parse, false) {
Michael Layzell760fd662017-05-31 22:46:05 -0400398 Ok((_, ref b)) if b.is_empty() => parse_error(),
Alex Crichton954046c2017-05-30 21:49:42 -0700399 other => other,
400 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700401 }
402
Alex Crichton954046c2017-05-30 21:49:42 -0700403 pub fn parse_terminated_with(
David Tolnay51382052017-12-27 13:46:21 -0500404 input: Cursor,
405 parse: fn(Cursor) -> PResult<T>,
406 ) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700407 Self::parse(input, parse, true)
408 }
409
Nika Layzellb49a9e52017-12-05 13:31:52 -0500410 pub fn parse_terminated_nonempty_with(
David Tolnay51382052017-12-27 13:46:21 -0500411 input: Cursor,
412 parse: fn(Cursor) -> PResult<T>,
413 ) -> PResult<Self> {
Nika Layzellb49a9e52017-12-05 13:31:52 -0500414 match Self::parse(input, parse, true) {
415 Ok((_, ref b)) if b.is_empty() => parse_error(),
416 other => other,
417 }
418 }
419
David Tolnay51382052017-12-27 13:46:21 -0500420 fn parse(
421 mut input: Cursor,
422 parse: fn(Cursor) -> PResult<T>,
423 terminated: bool,
424 ) -> PResult<Self> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700425 let mut res = Delimited::new();
426
427 // get the first element
Alex Crichton954046c2017-05-30 21:49:42 -0700428 match parse(input) {
Michael Layzell760fd662017-05-31 22:46:05 -0400429 Err(_) => Ok((input, res)),
430 Ok((i, o)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400431 if i == input {
Michael Layzell760fd662017-05-31 22:46:05 -0400432 return parse_error();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700433 }
434 input = i;
435 res.push_first(o);
436
437 // get the separator first
Michael Layzell760fd662017-05-31 22:46:05 -0400438 while let Ok((i2, s)) = D::parse(input) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400439 if i2 == input {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700440 break;
441 }
442
443 // get the element next
Michael Layzell760fd662017-05-31 22:46:05 -0400444 if let Ok((i3, o3)) = parse(i2) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400445 if i3 == i2 {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700446 break;
447 }
448 res.push_next(o3, s);
449 input = i3;
450 } else {
451 break;
452 }
453 }
454 if terminated {
Michael Layzell760fd662017-05-31 22:46:05 -0400455 if let Ok((after, sep)) = D::parse(input) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700456 res.push_trailing(sep);
457 input = after;
458 }
459 }
Michael Layzell760fd662017-05-31 22:46:05 -0400460 Ok((input, res))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700461 }
462 }
463 }
464 }
465}
466
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700467#[cfg(feature = "printing")]
468mod printing {
469 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500470 use quote::{ToTokens, Tokens};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700471
472 impl<T, D> ToTokens for Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500473 where
474 T: ToTokens,
475 D: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700476 {
477 fn to_tokens(&self, tokens: &mut Tokens) {
478 tokens.append_all(self.iter())
479 }
480 }
481
482 impl<T, D> ToTokens for Element<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500483 where
484 T: ToTokens,
485 D: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700486 {
487 fn to_tokens(&self, tokens: &mut Tokens) {
488 match *self {
489 Element::Delimited(ref a, ref b) => {
490 a.to_tokens(tokens);
491 b.to_tokens(tokens);
492 }
493 Element::End(ref a) => a.to_tokens(tokens),
494 }
495 }
496 }
497}