blob: 04fd9853d9cf2d1b8d9f0e5f685b365d27a185e9 [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
220 fn into_iter(self) -> Iter<'a, T, D> {
221 <Delimited<T, D>>::iter(self)
222 }
223}
224
225impl<T, D> IntoIterator for Delimited<T, D> {
226 type Item = Element<T, D>;
227 type IntoIter = IntoIter<T, D>;
228
229 fn into_iter(self) -> IntoIter<T, D> {
230 IntoIter { inner: self.inner.into_iter() }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700231 }
232}
233
234impl<T, D> Default for Delimited<T, D> {
235 fn default() -> Self {
236 Delimited::new()
237 }
238}
239
240pub struct Iter<'a, T: 'a, D: 'a> {
241 inner: slice::Iter<'a, (T, Option<D>)>,
242}
243
244impl<'a, T, D> Iterator for Iter<'a, T, D> {
245 type Item = Element<&'a T, &'a D>;
246
247 fn next(&mut self) -> Option<Element<&'a T, &'a D>> {
248 self.inner.next().map(|pair| {
249 match pair.1 {
250 Some(ref delimited) => Element::Delimited(&pair.0, delimited),
251 None => Element::End(&pair.0),
252 }
253 })
254 }
255}
256
Alex Crichton164c5332017-07-06 13:18:34 -0700257pub struct IterMut<'a, T: 'a, D: 'a> {
258 inner: slice::IterMut<'a, (T, Option<D>)>,
259}
260
261impl<'a, T, D> Iterator for IterMut<'a, T, D> {
262 type Item = Element<&'a mut T, &'a mut D>;
263
264 fn next(&mut self) -> Option<Element<&'a mut T, &'a mut D>> {
265 self.inner.next().map(|pair| {
266 match pair.1 {
267 Some(ref mut delimited) => Element::Delimited(&mut pair.0, delimited),
268 None => Element::End(&mut pair.0),
269 }
270 })
271 }
272}
273
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700274pub struct Items<'a, T: 'a, D: 'a> {
275 inner: slice::Iter<'a, (T, Option<D>)>,
276}
277
278impl<'a, T, D> Iterator for Items<'a, T, D> {
279 type Item = &'a T;
280
281 fn next(&mut self) -> Option<&'a T> {
282 self.inner.next().map(|pair| &pair.0)
283 }
284}
285
286pub struct IntoIter<T, D> {
287 inner: vec::IntoIter<(T, Option<D>)>,
288}
289
290impl<T, D> Iterator for IntoIter<T, D> {
291 type Item = Element<T, D>;
292
293 fn next(&mut self) -> Option<Element<T, D>> {
294 self.inner.next().map(|pair| {
295 match pair.1 {
296 Some(v) => Element::Delimited(pair.0, v),
297 None => Element::End(pair.0)
298 }
299 })
300 }
301}
302
303pub enum Element<T, D> {
304 Delimited(T, D),
305 End(T),
306}
307
308impl<T, D> Element<T, D> {
309 pub fn into_item(self) -> T {
310 match self {
311 Element::Delimited(t, _) |
312 Element::End(t) => t,
313 }
314 }
315
316 pub fn item(&self) -> &T {
317 match *self {
318 Element::Delimited(ref t, _) |
319 Element::End(ref t) => t,
320 }
321 }
322
323 pub fn item_mut(&mut self) -> &mut T {
324 match *self {
325 Element::Delimited(ref mut t, _) |
326 Element::End(ref mut t) => t,
327 }
328 }
329
330 pub fn delimiter(&self) -> Option<&D> {
331 match *self {
332 Element::Delimited(_, ref d) => Some(d),
333 Element::End(_) => None,
334 }
335 }
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400336
337 pub fn into_tuple(self) -> (T, Option<D>) {
338 match self {
339 Element::Delimited(t, d) => (t, Some(d)),
340 Element::End(t) => (t, None),
341 }
342 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700343}
344
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700345#[cfg(feature = "parsing")]
346mod parsing {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700347 use super::Delimited;
Michael Layzell760fd662017-05-31 22:46:05 -0400348 use {PResult, Cursor, Synom, parse_error};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700349
350 impl<T, D> Delimited<T, D>
351 where T: Synom,
352 D: Synom,
353 {
Michael Layzell760fd662017-05-31 22:46:05 -0400354 pub fn parse_separated(input: Cursor) -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700355 {
Alex Crichton954046c2017-05-30 21:49:42 -0700356 Self::parse(input, T::parse, false)
357 }
358
Michael Layzell760fd662017-05-31 22:46:05 -0400359 pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700360 {
361 Self::parse_separated_nonempty_with(input, T::parse)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700362 }
363
Michael Layzell760fd662017-05-31 22:46:05 -0400364 pub fn parse_terminated(input: Cursor) -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700365 {
Alex Crichton954046c2017-05-30 21:49:42 -0700366 Self::parse_terminated_with(input, T::parse)
367 }
368 }
369
370 impl<T, D> Delimited<T, D>
371 where D: Synom,
372 {
373 pub fn parse_separated_nonempty_with(
Michael Layzell760fd662017-05-31 22:46:05 -0400374 input: Cursor,
375 parse: fn(Cursor) -> PResult<T>)
376 -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700377 {
378 match Self::parse(input, parse, false) {
Michael Layzell760fd662017-05-31 22:46:05 -0400379 Ok((_, ref b)) if b.is_empty() => parse_error(),
Alex Crichton954046c2017-05-30 21:49:42 -0700380 other => other,
381 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700382 }
383
Alex Crichton954046c2017-05-30 21:49:42 -0700384 pub fn parse_terminated_with(
Michael Layzell760fd662017-05-31 22:46:05 -0400385 input: Cursor,
386 parse: fn(Cursor) -> PResult<T>)
387 -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700388 {
389 Self::parse(input, parse, true)
390 }
391
Michael Layzell760fd662017-05-31 22:46:05 -0400392 fn parse(mut input: Cursor,
393 parse: fn(Cursor) -> PResult<T>,
Alex Crichton954046c2017-05-30 21:49:42 -0700394 terminated: bool)
Michael Layzell760fd662017-05-31 22:46:05 -0400395 -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700396 {
397 let mut res = Delimited::new();
398
399 // get the first element
Alex Crichton954046c2017-05-30 21:49:42 -0700400 match parse(input) {
Michael Layzell760fd662017-05-31 22:46:05 -0400401 Err(_) => Ok((input, res)),
402 Ok((i, o)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400403 if i == input {
Michael Layzell760fd662017-05-31 22:46:05 -0400404 return parse_error();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700405 }
406 input = i;
407 res.push_first(o);
408
409 // get the separator first
Michael Layzell760fd662017-05-31 22:46:05 -0400410 while let Ok((i2, s)) = D::parse(input) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400411 if i2 == input {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700412 break;
413 }
414
415 // get the element next
Michael Layzell760fd662017-05-31 22:46:05 -0400416 if let Ok((i3, o3)) = parse(i2) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400417 if i3 == i2 {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700418 break;
419 }
420 res.push_next(o3, s);
421 input = i3;
422 } else {
423 break;
424 }
425 }
426 if terminated {
Michael Layzell760fd662017-05-31 22:46:05 -0400427 if let Ok((after, sep)) = D::parse(input) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700428 res.push_trailing(sep);
429 input = after;
430 }
431 }
Michael Layzell760fd662017-05-31 22:46:05 -0400432 Ok((input, res))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700433 }
434 }
435 }
436 }
437}
438
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700439#[cfg(feature = "printing")]
440mod printing {
441 use super::*;
442 use quote::{Tokens, ToTokens};
443
444
445 impl<T, D> ToTokens for Delimited<T, D>
446 where T: ToTokens,
447 D: ToTokens,
448 {
449 fn to_tokens(&self, tokens: &mut Tokens) {
450 tokens.append_all(self.iter())
451 }
452 }
453
454 impl<T, D> ToTokens for Element<T, D>
455 where T: ToTokens,
456 D: ToTokens,
457 {
458 fn to_tokens(&self, tokens: &mut Tokens) {
459 match *self {
460 Element::Delimited(ref a, ref b) => {
461 a.to_tokens(tokens);
462 b.to_tokens(tokens);
463 }
464 Element::End(ref a) => a.to_tokens(tokens),
465 }
466 }
467 }
468}