blob: ec753e8ed5ba07f35f903ce1a202edebdfbd5fe8 [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>) {
David Tolnaydc03aec2017-12-30 01:54:18 -050093 assert!(self.empty_or_trailing());
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 {
David Tolnaydc03aec2017-12-30 01:54:18 -0500120 if self.empty_or_trailing() {
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
David Tolnaydc03aec2017-12-30 01:54:18 -0500143 /// delimiter.
144 ///
145 /// Equivalent to `delimited.is_empty() || delimited.trailing_delim()`.
Michael Layzell3936ceb2017-07-08 00:28:36 -0400146 pub fn empty_or_trailing(&self) -> bool {
147 self.is_empty() || self.trailing_delim()
148 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700149}
150
Nika Layzelld73a3652017-10-24 08:57:05 -0400151#[cfg(feature = "extra-traits")]
152impl<T: Debug, D: Debug> Debug for Delimited<T, D> {
153 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
154 self.inner.fmt(f)
155 }
156}
157
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700158impl<T, D> From<Vec<(T, Option<D>)>> for Delimited<T, D> {
159 fn from(v: Vec<(T, Option<D>)>) -> Self {
David Tolnay51382052017-12-27 13:46:21 -0500160 Delimited { inner: v }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700161 }
162}
163
164impl<T, D> From<Vec<T>> for Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500165where
166 D: Default,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700167{
168 fn from(v: Vec<T>) -> Self {
Alex Crichtonafb49d22017-07-06 14:47:37 -0700169 let len = v.len();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700170 Delimited {
David Tolnay51382052017-12-27 13:46:21 -0500171 inner: v.into_iter()
172 .enumerate()
173 .map(|(i, item)| {
174 (
175 item,
176 if i + 1 == len {
177 None
178 } else {
179 Some(D::default())
180 },
181 )
182 })
183 .collect(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700184 }
185 }
186}
187
188impl<T, D> FromIterator<Element<T, D>> for Delimited<T, D> {
189 fn from_iter<I: IntoIterator<Item = Element<T, D>>>(i: I) -> Self {
190 let mut ret = Delimited::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700191 ret.extend(i);
Alex Crichton954046c2017-05-30 21:49:42 -0700192 ret
193 }
194}
195
Alex Crichtond3743d12017-07-07 20:55:24 -0700196impl<T, D> FromIterator<T> for Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500197where
198 D: Default,
Alex Crichtond3743d12017-07-07 20:55:24 -0700199{
200 fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self {
201 let mut ret = Delimited::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700202 ret.extend(i);
Alex Crichtond3743d12017-07-07 20:55:24 -0700203 ret
204 }
205}
206
Alex Crichton24f12822017-07-14 07:15:32 -0700207impl<T, D> Extend<Element<T, D>> for Delimited<T, D> {
208 fn extend<I: IntoIterator<Item = Element<T, D>>>(&mut self, i: I) {
209 for element in i {
210 match element {
211 Element::Delimited(a, b) => self.inner.push((a, Some(b))),
212 Element::End(a) => self.inner.push((a, None)),
213 }
214 }
215 }
216}
217
218impl<T, D> Extend<T> for Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500219where
220 D: Default,
Alex Crichton24f12822017-07-14 07:15:32 -0700221{
222 fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) {
223 for element in i {
224 self.push_default(element);
225 }
226 }
227}
228
Alex Crichton954046c2017-05-30 21:49:42 -0700229impl<'a, T, D> IntoIterator for &'a Delimited<T, D> {
230 type Item = Element<&'a T, &'a D>;
231 type IntoIter = Iter<'a, T, D>;
232
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500233 fn into_iter(self) -> Self::IntoIter {
Alex Crichton954046c2017-05-30 21:49:42 -0700234 <Delimited<T, D>>::iter(self)
235 }
236}
237
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500238impl<'a, T, D> IntoIterator for &'a mut Delimited<T, D> {
239 type Item = Element<&'a mut T, &'a mut D>;
240 type IntoIter = IterMut<'a, T, D>;
241
242 fn into_iter(self) -> Self::IntoIter {
243 <Delimited<T, D>>::iter_mut(self)
244 }
245}
246
Alex Crichton954046c2017-05-30 21:49:42 -0700247impl<T, D> IntoIterator for Delimited<T, D> {
248 type Item = Element<T, D>;
249 type IntoIter = IntoIter<T, D>;
250
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500251 fn into_iter(self) -> Self::IntoIter {
David Tolnay51382052017-12-27 13:46:21 -0500252 IntoIter {
253 inner: self.inner.into_iter(),
254 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700255 }
256}
257
258impl<T, D> Default for Delimited<T, D> {
259 fn default() -> Self {
260 Delimited::new()
261 }
262}
263
264pub struct Iter<'a, T: 'a, D: 'a> {
265 inner: slice::Iter<'a, (T, Option<D>)>,
266}
267
268impl<'a, T, D> Iterator for Iter<'a, T, D> {
269 type Item = Element<&'a T, &'a D>;
270
271 fn next(&mut self) -> Option<Element<&'a T, &'a D>> {
David Tolnay51382052017-12-27 13:46:21 -0500272 self.inner.next().map(|pair| match pair.1 {
273 Some(ref delimited) => Element::Delimited(&pair.0, delimited),
274 None => Element::End(&pair.0),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700275 })
276 }
277}
278
Alex Crichton164c5332017-07-06 13:18:34 -0700279pub struct IterMut<'a, T: 'a, D: 'a> {
280 inner: slice::IterMut<'a, (T, Option<D>)>,
281}
282
283impl<'a, T, D> Iterator for IterMut<'a, T, D> {
284 type Item = Element<&'a mut T, &'a mut D>;
285
286 fn next(&mut self) -> Option<Element<&'a mut T, &'a mut D>> {
David Tolnay51382052017-12-27 13:46:21 -0500287 self.inner.next().map(|pair| match pair.1 {
288 Some(ref mut delimited) => Element::Delimited(&mut pair.0, delimited),
289 None => Element::End(&mut pair.0),
Alex Crichton164c5332017-07-06 13:18:34 -0700290 })
291 }
292}
293
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700294pub struct Items<'a, T: 'a, D: 'a> {
295 inner: slice::Iter<'a, (T, Option<D>)>,
296}
297
298impl<'a, T, D> Iterator for Items<'a, T, D> {
299 type Item = &'a T;
300
301 fn next(&mut self) -> Option<&'a T> {
302 self.inner.next().map(|pair| &pair.0)
303 }
304}
305
306pub struct IntoIter<T, D> {
307 inner: vec::IntoIter<(T, Option<D>)>,
308}
309
310impl<T, D> Iterator for IntoIter<T, D> {
311 type Item = Element<T, D>;
312
313 fn next(&mut self) -> Option<Element<T, D>> {
David Tolnay51382052017-12-27 13:46:21 -0500314 self.inner.next().map(|pair| match pair.1 {
315 Some(v) => Element::Delimited(pair.0, v),
316 None => Element::End(pair.0),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700317 })
318 }
319}
320
321pub enum Element<T, D> {
322 Delimited(T, D),
323 End(T),
324}
325
326impl<T, D> Element<T, D> {
327 pub fn into_item(self) -> T {
328 match self {
David Tolnay51382052017-12-27 13:46:21 -0500329 Element::Delimited(t, _) | Element::End(t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700330 }
331 }
332
333 pub fn item(&self) -> &T {
334 match *self {
David Tolnay51382052017-12-27 13:46:21 -0500335 Element::Delimited(ref t, _) | Element::End(ref t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700336 }
337 }
338
339 pub fn item_mut(&mut self) -> &mut T {
340 match *self {
David Tolnay51382052017-12-27 13:46:21 -0500341 Element::Delimited(ref mut t, _) | Element::End(ref mut t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700342 }
343 }
344
345 pub fn delimiter(&self) -> Option<&D> {
346 match *self {
347 Element::Delimited(_, ref d) => Some(d),
348 Element::End(_) => None,
349 }
350 }
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400351
352 pub fn into_tuple(self) -> (T, Option<D>) {
353 match self {
354 Element::Delimited(t, d) => (t, Some(d)),
355 Element::End(t) => (t, None),
356 }
357 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700358}
359
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700360#[cfg(feature = "parsing")]
361mod parsing {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700362 use super::Delimited;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500363 use synom::Synom;
364 use cursor::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500365 use parse_error;
366 use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700367
368 impl<T, D> Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500369 where
370 T: Synom,
371 D: Synom,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700372 {
David Tolnay51382052017-12-27 13:46:21 -0500373 pub fn parse_separated(input: Cursor) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700374 Self::parse(input, T::parse, false)
375 }
376
David Tolnay51382052017-12-27 13:46:21 -0500377 pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700378 Self::parse_separated_nonempty_with(input, T::parse)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700379 }
380
David Tolnay51382052017-12-27 13:46:21 -0500381 pub fn parse_terminated(input: Cursor) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700382 Self::parse_terminated_with(input, T::parse)
383 }
Nika Layzellb49a9e52017-12-05 13:31:52 -0500384
David Tolnay51382052017-12-27 13:46:21 -0500385 pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> {
Nika Layzellb49a9e52017-12-05 13:31:52 -0500386 Self::parse_terminated_nonempty_with(input, T::parse)
387 }
Alex Crichton954046c2017-05-30 21:49:42 -0700388 }
389
390 impl<T, D> Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500391 where
392 D: Synom,
Alex Crichton954046c2017-05-30 21:49:42 -0700393 {
David Tolnaydc03aec2017-12-30 01:54:18 -0500394 pub fn parse_separated_with(
395 input: Cursor,
396 parse: fn(Cursor) -> PResult<T>,
397 ) -> PResult<Self> {
398 Self::parse(input, parse, false)
399 }
400
Alex Crichton954046c2017-05-30 21:49:42 -0700401 pub fn parse_separated_nonempty_with(
David Tolnay51382052017-12-27 13:46:21 -0500402 input: Cursor,
403 parse: fn(Cursor) -> PResult<T>,
404 ) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700405 match Self::parse(input, parse, false) {
Michael Layzell760fd662017-05-31 22:46:05 -0400406 Ok((_, ref b)) if b.is_empty() => parse_error(),
Alex Crichton954046c2017-05-30 21:49:42 -0700407 other => other,
408 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700409 }
410
Alex Crichton954046c2017-05-30 21:49:42 -0700411 pub fn parse_terminated_with(
David Tolnay51382052017-12-27 13:46:21 -0500412 input: Cursor,
413 parse: fn(Cursor) -> PResult<T>,
414 ) -> PResult<Self> {
Alex Crichton954046c2017-05-30 21:49:42 -0700415 Self::parse(input, parse, true)
416 }
417
Nika Layzellb49a9e52017-12-05 13:31:52 -0500418 pub fn parse_terminated_nonempty_with(
David Tolnay51382052017-12-27 13:46:21 -0500419 input: Cursor,
420 parse: fn(Cursor) -> PResult<T>,
421 ) -> PResult<Self> {
Nika Layzellb49a9e52017-12-05 13:31:52 -0500422 match Self::parse(input, parse, true) {
423 Ok((_, ref b)) if b.is_empty() => parse_error(),
424 other => other,
425 }
426 }
427
David Tolnay51382052017-12-27 13:46:21 -0500428 fn parse(
429 mut input: Cursor,
430 parse: fn(Cursor) -> PResult<T>,
431 terminated: bool,
432 ) -> PResult<Self> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700433 let mut res = Delimited::new();
434
435 // get the first element
Alex Crichton954046c2017-05-30 21:49:42 -0700436 match parse(input) {
Michael Layzell760fd662017-05-31 22:46:05 -0400437 Err(_) => Ok((input, res)),
438 Ok((i, o)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400439 if i == input {
Michael Layzell760fd662017-05-31 22:46:05 -0400440 return parse_error();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700441 }
442 input = i;
443 res.push_first(o);
444
445 // get the separator first
Michael Layzell760fd662017-05-31 22:46:05 -0400446 while let Ok((i2, s)) = D::parse(input) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400447 if i2 == input {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700448 break;
449 }
450
451 // get the element next
Michael Layzell760fd662017-05-31 22:46:05 -0400452 if let Ok((i3, o3)) = parse(i2) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400453 if i3 == i2 {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700454 break;
455 }
456 res.push_next(o3, s);
457 input = i3;
458 } else {
459 break;
460 }
461 }
462 if terminated {
Michael Layzell760fd662017-05-31 22:46:05 -0400463 if let Ok((after, sep)) = D::parse(input) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700464 res.push_trailing(sep);
465 input = after;
466 }
467 }
Michael Layzell760fd662017-05-31 22:46:05 -0400468 Ok((input, res))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700469 }
470 }
471 }
472 }
473}
474
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700475#[cfg(feature = "printing")]
476mod printing {
477 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500478 use quote::{ToTokens, Tokens};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700479
480 impl<T, D> ToTokens for Delimited<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500481 where
482 T: ToTokens,
483 D: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700484 {
485 fn to_tokens(&self, tokens: &mut Tokens) {
486 tokens.append_all(self.iter())
487 }
488 }
489
490 impl<T, D> ToTokens for Element<T, D>
David Tolnay51382052017-12-27 13:46:21 -0500491 where
492 T: ToTokens,
493 D: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700494 {
495 fn to_tokens(&self, tokens: &mut Tokens) {
496 match *self {
497 Element::Delimited(ref a, ref b) => {
498 a.to_tokens(tokens);
499 b.to_tokens(tokens);
500 }
501 Element::End(ref a) => a.to_tokens(tokens),
502 }
503 }
504 }
505}