blob: 9f968f7b680ac9868bda288f674d2a9502cdaa5d [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> {
10 inner: Vec<(T, Option<D>)>
11}
12
13impl<T, D> Delimited<T, D> {
14 pub fn new() -> Delimited<T, D> {
15 Delimited {
16 inner: Vec::new(),
17 }
18 }
19
20 pub fn is_empty(&self) -> bool {
21 self.inner.len() == 0
22 }
23
24 pub fn len(&self) -> usize {
25 self.inner.len()
26 }
27
28 pub fn get(&self, idx: usize) -> Element<&T, &D> {
29 let (ref t, ref d) = self.inner[idx];
30 match *d {
31 Some(ref d) => Element::Delimited(t, d),
32 None => Element::End(t),
33 }
34 }
35
36 pub fn get_mut(&mut self, idx: usize) -> Element<&mut T, &mut D> {
37 let (ref mut t, ref mut d) = self.inner[idx];
38 match *d {
39 Some(ref mut d) => Element::Delimited(t, d),
40 None => Element::End(t),
41 }
42 }
43
Alex Crichton0aa50e02017-07-07 20:59:03 -070044 pub fn first(&self) -> Option<Element<&T, &D>> {
45 self.inner.first().map(|&(ref t, ref d)| {
46 match *d {
47 Some(ref d) => Element::Delimited(t, d),
48 None => Element::End(t),
49 }
50 })
51 }
52
53 pub fn first_mut(&mut self) -> Option<Element<&mut T, &mut D>> {
54 self.inner.first_mut().map(|&mut (ref mut t, ref mut d)| {
55 match *d {
56 Some(ref mut d) => Element::Delimited(t, d),
57 None => Element::End(t),
58 }
59 })
60 }
61
62 pub fn last(&self) -> Option<Element<&T, &D>> {
63 self.inner.last().map(|&(ref t, ref d)| {
64 match *d {
65 Some(ref d) => Element::Delimited(t, d),
66 None => Element::End(t),
67 }
68 })
69 }
70
71 pub fn last_mut(&mut self) -> Option<Element<&mut T, &mut D>> {
72 self.inner.last_mut().map(|&mut (ref mut t, ref mut d)| {
73 match *d {
74 Some(ref mut d) => Element::Delimited(t, d),
75 None => Element::End(t),
76 }
77 })
78 }
79
Alex Crichtonccbb45d2017-05-23 10:58:24 -070080 pub fn iter(&self) -> Iter<T, D> {
81 Iter { inner: self.inner.iter() }
82 }
83
Alex Crichton164c5332017-07-06 13:18:34 -070084 pub fn iter_mut(&mut self) -> IterMut<T, D> {
85 IterMut { inner: self.inner.iter_mut() }
86 }
87
Alex Crichtonccbb45d2017-05-23 10:58:24 -070088 pub fn items(&self) -> Items<T, D> {
89 Items { inner: self.inner.iter() }
90 }
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
116 pub fn push_default(&mut self, token: T) where D: Default {
Alex Crichton337cd462017-07-06 14:47:25 -0700117 if self.is_empty() || self.trailing_delim() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700118 self.inner.push((token, None));
119 } else {
120 self.push_next(token, D::default());
121 }
122 }
123
124 pub fn pop(&mut self) -> Option<Element<T, D>> {
125 self.inner.pop().map(|e| {
126 match e {
127 (t, Some(d)) => Element::Delimited(t, d),
128 (t, None) => Element::End(t),
129 }
130 })
131 }
132
133 pub fn into_vec(self) -> Vec<T> {
134 self.inner.into_iter().map(|t| t.0).collect()
135 }
136
137 pub fn trailing_delim(&self) -> bool {
138 self.inner[self.inner.len() - 1].1.is_some()
139 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400140
141 /// Returns true if either this `Delimited` is empty, or it has a trailing
142 /// delimiter. This is useful within `ToTokens` implementations for `syn`.
143 #[doc(hidden)]
144 pub fn empty_or_trailing(&self) -> bool {
145 self.is_empty() || self.trailing_delim()
146 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700147}
148
Nika Layzelld73a3652017-10-24 08:57:05 -0400149#[cfg(feature = "extra-traits")]
150impl<T: Debug, D: Debug> Debug for Delimited<T, D> {
151 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
152 self.inner.fmt(f)
153 }
154}
155
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700156impl<T, D> From<Vec<(T, Option<D>)>> for Delimited<T, D> {
157 fn from(v: Vec<(T, Option<D>)>) -> Self {
158 Delimited {
159 inner: v,
160 }
161 }
162}
163
164impl<T, D> From<Vec<T>> for Delimited<T, D>
165 where D: Default,
166{
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 {
170 inner: v.into_iter().enumerate().map(|(i, item)| {
Alex Crichtonafb49d22017-07-06 14:47:37 -0700171 (item, if i + 1 == len {None} else {Some(D::default())})
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700172 }).collect(),
173 }
174 }
175}
176
177impl<T, D> FromIterator<Element<T, D>> for Delimited<T, D> {
178 fn from_iter<I: IntoIterator<Item = Element<T, D>>>(i: I) -> Self {
179 let mut ret = Delimited::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700180 ret.extend(i);
Alex Crichton954046c2017-05-30 21:49:42 -0700181 ret
182 }
183}
184
Alex Crichtond3743d12017-07-07 20:55:24 -0700185impl<T, D> FromIterator<T> for Delimited<T, D>
186 where D: Default,
187{
188 fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self {
189 let mut ret = Delimited::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700190 ret.extend(i);
Alex Crichtond3743d12017-07-07 20:55:24 -0700191 ret
192 }
193}
194
Alex Crichton24f12822017-07-14 07:15:32 -0700195impl<T, D> Extend<Element<T, D>> for Delimited<T, D> {
196 fn extend<I: IntoIterator<Item = Element<T, D>>>(&mut self, i: I) {
197 for element in i {
198 match element {
199 Element::Delimited(a, b) => self.inner.push((a, Some(b))),
200 Element::End(a) => self.inner.push((a, None)),
201 }
202 }
203 }
204}
205
206impl<T, D> Extend<T> for Delimited<T, D>
207 where D: Default,
208{
209 fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) {
210 for element in i {
211 self.push_default(element);
212 }
213 }
214}
215
Alex Crichton954046c2017-05-30 21:49:42 -0700216impl<'a, T, D> IntoIterator for &'a Delimited<T, D> {
217 type Item = Element<&'a T, &'a D>;
218 type IntoIter = Iter<'a, T, D>;
219
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500220 fn into_iter(self) -> Self::IntoIter {
Alex Crichton954046c2017-05-30 21:49:42 -0700221 <Delimited<T, D>>::iter(self)
222 }
223}
224
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500225impl<'a, T, D> IntoIterator for &'a mut Delimited<T, D> {
226 type Item = Element<&'a mut T, &'a mut D>;
227 type IntoIter = IterMut<'a, T, D>;
228
229 fn into_iter(self) -> Self::IntoIter {
230 <Delimited<T, D>>::iter_mut(self)
231 }
232}
233
Alex Crichton954046c2017-05-30 21:49:42 -0700234impl<T, D> IntoIterator for Delimited<T, D> {
235 type Item = Element<T, D>;
236 type IntoIter = IntoIter<T, D>;
237
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500238 fn into_iter(self) -> Self::IntoIter {
Alex Crichton954046c2017-05-30 21:49:42 -0700239 IntoIter { inner: self.inner.into_iter() }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700240 }
241}
242
243impl<T, D> Default for Delimited<T, D> {
244 fn default() -> Self {
245 Delimited::new()
246 }
247}
248
249pub struct Iter<'a, T: 'a, D: 'a> {
250 inner: slice::Iter<'a, (T, Option<D>)>,
251}
252
253impl<'a, T, D> Iterator for Iter<'a, T, D> {
254 type Item = Element<&'a T, &'a D>;
255
256 fn next(&mut self) -> Option<Element<&'a T, &'a D>> {
257 self.inner.next().map(|pair| {
258 match pair.1 {
259 Some(ref delimited) => Element::Delimited(&pair.0, delimited),
260 None => Element::End(&pair.0),
261 }
262 })
263 }
264}
265
Alex Crichton164c5332017-07-06 13:18:34 -0700266pub struct IterMut<'a, T: 'a, D: 'a> {
267 inner: slice::IterMut<'a, (T, Option<D>)>,
268}
269
270impl<'a, T, D> Iterator for IterMut<'a, T, D> {
271 type Item = Element<&'a mut T, &'a mut D>;
272
273 fn next(&mut self) -> Option<Element<&'a mut T, &'a mut D>> {
274 self.inner.next().map(|pair| {
275 match pair.1 {
276 Some(ref mut delimited) => Element::Delimited(&mut pair.0, delimited),
277 None => Element::End(&mut pair.0),
278 }
279 })
280 }
281}
282
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700283pub struct Items<'a, T: 'a, D: 'a> {
284 inner: slice::Iter<'a, (T, Option<D>)>,
285}
286
287impl<'a, T, D> Iterator for Items<'a, T, D> {
288 type Item = &'a T;
289
290 fn next(&mut self) -> Option<&'a T> {
291 self.inner.next().map(|pair| &pair.0)
292 }
293}
294
295pub struct IntoIter<T, D> {
296 inner: vec::IntoIter<(T, Option<D>)>,
297}
298
299impl<T, D> Iterator for IntoIter<T, D> {
300 type Item = Element<T, D>;
301
302 fn next(&mut self) -> Option<Element<T, D>> {
303 self.inner.next().map(|pair| {
304 match pair.1 {
305 Some(v) => Element::Delimited(pair.0, v),
306 None => Element::End(pair.0)
307 }
308 })
309 }
310}
311
312pub enum Element<T, D> {
313 Delimited(T, D),
314 End(T),
315}
316
317impl<T, D> Element<T, D> {
318 pub fn into_item(self) -> T {
319 match self {
320 Element::Delimited(t, _) |
321 Element::End(t) => t,
322 }
323 }
324
325 pub fn item(&self) -> &T {
326 match *self {
327 Element::Delimited(ref t, _) |
328 Element::End(ref t) => t,
329 }
330 }
331
332 pub fn item_mut(&mut self) -> &mut T {
333 match *self {
334 Element::Delimited(ref mut t, _) |
335 Element::End(ref mut t) => t,
336 }
337 }
338
339 pub fn delimiter(&self) -> Option<&D> {
340 match *self {
341 Element::Delimited(_, ref d) => Some(d),
342 Element::End(_) => None,
343 }
344 }
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400345
346 pub fn into_tuple(self) -> (T, Option<D>) {
347 match self {
348 Element::Delimited(t, d) => (t, Some(d)),
349 Element::End(t) => (t, None),
350 }
351 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700352}
353
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700354#[cfg(feature = "parsing")]
355mod parsing {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700356 use super::Delimited;
Michael Layzell760fd662017-05-31 22:46:05 -0400357 use {PResult, Cursor, Synom, parse_error};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700358
359 impl<T, D> Delimited<T, D>
360 where T: Synom,
361 D: Synom,
362 {
Michael Layzell760fd662017-05-31 22:46:05 -0400363 pub fn parse_separated(input: Cursor) -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700364 {
Alex Crichton954046c2017-05-30 21:49:42 -0700365 Self::parse(input, T::parse, false)
366 }
367
Michael Layzell760fd662017-05-31 22:46:05 -0400368 pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700369 {
370 Self::parse_separated_nonempty_with(input, T::parse)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700371 }
372
Michael Layzell760fd662017-05-31 22:46:05 -0400373 pub fn parse_terminated(input: Cursor) -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700374 {
Alex Crichton954046c2017-05-30 21:49:42 -0700375 Self::parse_terminated_with(input, T::parse)
376 }
Nika Layzellb49a9e52017-12-05 13:31:52 -0500377
378 pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self>
379 {
380 Self::parse_terminated_nonempty_with(input, T::parse)
381 }
Alex Crichton954046c2017-05-30 21:49:42 -0700382 }
383
384 impl<T, D> Delimited<T, D>
385 where D: Synom,
386 {
387 pub fn parse_separated_nonempty_with(
Michael Layzell760fd662017-05-31 22:46:05 -0400388 input: Cursor,
389 parse: fn(Cursor) -> PResult<T>)
390 -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700391 {
392 match Self::parse(input, parse, false) {
Michael Layzell760fd662017-05-31 22:46:05 -0400393 Ok((_, ref b)) if b.is_empty() => parse_error(),
Alex Crichton954046c2017-05-30 21:49:42 -0700394 other => other,
395 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700396 }
397
Alex Crichton954046c2017-05-30 21:49:42 -0700398 pub fn parse_terminated_with(
Michael Layzell760fd662017-05-31 22:46:05 -0400399 input: Cursor,
400 parse: fn(Cursor) -> PResult<T>)
401 -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700402 {
403 Self::parse(input, parse, true)
404 }
405
Nika Layzellb49a9e52017-12-05 13:31:52 -0500406 pub fn parse_terminated_nonempty_with(
407 input: Cursor,
408 parse: fn(Cursor) -> PResult<T>)
409 -> PResult<Self>
410 {
411 match Self::parse(input, parse, true) {
412 Ok((_, ref b)) if b.is_empty() => parse_error(),
413 other => other,
414 }
415 }
416
Michael Layzell760fd662017-05-31 22:46:05 -0400417 fn parse(mut input: Cursor,
418 parse: fn(Cursor) -> PResult<T>,
Alex Crichton954046c2017-05-30 21:49:42 -0700419 terminated: bool)
Michael Layzell760fd662017-05-31 22:46:05 -0400420 -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700421 {
422 let mut res = Delimited::new();
423
424 // get the first element
Alex Crichton954046c2017-05-30 21:49:42 -0700425 match parse(input) {
Michael Layzell760fd662017-05-31 22:46:05 -0400426 Err(_) => Ok((input, res)),
427 Ok((i, o)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400428 if i == input {
Michael Layzell760fd662017-05-31 22:46:05 -0400429 return parse_error();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700430 }
431 input = i;
432 res.push_first(o);
433
434 // get the separator first
Michael Layzell760fd662017-05-31 22:46:05 -0400435 while let Ok((i2, s)) = D::parse(input) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400436 if i2 == input {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700437 break;
438 }
439
440 // get the element next
Michael Layzell760fd662017-05-31 22:46:05 -0400441 if let Ok((i3, o3)) = parse(i2) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400442 if i3 == i2 {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700443 break;
444 }
445 res.push_next(o3, s);
446 input = i3;
447 } else {
448 break;
449 }
450 }
451 if terminated {
Michael Layzell760fd662017-05-31 22:46:05 -0400452 if let Ok((after, sep)) = D::parse(input) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700453 res.push_trailing(sep);
454 input = after;
455 }
456 }
Michael Layzell760fd662017-05-31 22:46:05 -0400457 Ok((input, res))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700458 }
459 }
460 }
461 }
462}
463
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700464#[cfg(feature = "printing")]
465mod printing {
466 use super::*;
467 use quote::{Tokens, ToTokens};
468
469
470 impl<T, D> ToTokens for Delimited<T, D>
471 where T: ToTokens,
472 D: ToTokens,
473 {
474 fn to_tokens(&self, tokens: &mut Tokens) {
475 tokens.append_all(self.iter())
476 }
477 }
478
479 impl<T, D> ToTokens for Element<T, D>
480 where T: ToTokens,
481 D: ToTokens,
482 {
483 fn to_tokens(&self, tokens: &mut Tokens) {
484 match *self {
485 Element::Delimited(ref a, ref b) => {
486 a.to_tokens(tokens);
487 b.to_tokens(tokens);
488 }
489 Element::End(ref a) => a.to_tokens(tokens),
490 }
491 }
492 }
493}