Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1 | use std::iter::FromIterator; |
| 2 | use std::slice; |
| 3 | use std::vec; |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 4 | #[cfg(feature = "extra-traits")] |
| 5 | use std::fmt::{self, Debug}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 6 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 7 | #[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 8 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 9 | pub struct Delimited<T, D> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 10 | inner: Vec<(T, Option<D>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 11 | } |
| 12 | |
| 13 | impl<T, D> Delimited<T, D> { |
| 14 | pub fn new() -> Delimited<T, D> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 15 | Delimited { inner: Vec::new() } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 16 | } |
| 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 | |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 26 | pub fn first(&self) -> Option<Element<&T, &D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 27 | self.inner.first().map(|&(ref t, ref d)| match *d { |
| 28 | Some(ref d) => Element::Delimited(t, d), |
| 29 | None => Element::End(t), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 30 | }) |
| 31 | } |
| 32 | |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 33 | pub fn last(&self) -> Option<Element<&T, &D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 34 | self.inner.last().map(|&(ref t, ref d)| match *d { |
| 35 | Some(ref d) => Element::Delimited(t, d), |
| 36 | None => Element::End(t), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 37 | }) |
| 38 | } |
| 39 | |
| 40 | pub fn last_mut(&mut self) -> Option<Element<&mut T, &mut D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 41 | self.inner |
| 42 | .last_mut() |
| 43 | .map(|&mut (ref mut t, ref mut d)| match *d { |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 44 | Some(ref mut d) => Element::Delimited(t, d), |
| 45 | None => Element::End(t), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 46 | }) |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 49 | pub fn iter(&self) -> Iter<T, D> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 50 | Iter { |
| 51 | inner: self.inner.iter(), |
| 52 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 55 | pub fn iter_mut(&mut self) -> IterMut<T, D> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 56 | IterMut { |
| 57 | inner: self.inner.iter_mut(), |
| 58 | } |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 59 | } |
| 60 | |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 61 | pub fn push(&mut self, token: T) { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 62 | assert!(self.empty_or_trailing()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 63 | self.inner.push((token, None)); |
| 64 | } |
| 65 | |
| 66 | pub fn push_trailing(&mut self, delimiter: D) { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 67 | assert!(!self.is_empty()); |
| 68 | let last = self.inner.last_mut().unwrap(); |
| 69 | assert!(last.1.is_none()); |
| 70 | last.1 = Some(delimiter); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | pub fn pop(&mut self) -> Option<Element<T, D>> { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 74 | self.inner.pop().map(|(t, d)| Element::new(t, d)) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | pub fn trailing_delim(&self) -> bool { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 78 | self.inner.last().map(|last| last.1.is_some()).unwrap_or(false) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 79 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 80 | |
| 81 | /// Returns true if either this `Delimited` is empty, or it has a trailing |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 82 | /// delimiter. |
| 83 | /// |
| 84 | /// Equivalent to `delimited.is_empty() || delimited.trailing_delim()`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 85 | pub fn empty_or_trailing(&self) -> bool { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 86 | self.inner.last().map(|last| last.1.is_some()).unwrap_or(true) |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 87 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 90 | #[cfg(feature = "extra-traits")] |
| 91 | impl<T: Debug, D: Debug> Debug for Delimited<T, D> { |
| 92 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 93 | self.inner.fmt(f) |
| 94 | } |
| 95 | } |
| 96 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 97 | impl<T, D> FromIterator<Element<T, D>> for Delimited<T, D> { |
| 98 | fn from_iter<I: IntoIterator<Item = Element<T, D>>>(i: I) -> Self { |
| 99 | let mut ret = Delimited::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 100 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 101 | ret |
| 102 | } |
| 103 | } |
| 104 | |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 105 | impl<T, D> Extend<Element<T, D>> for Delimited<T, D> { |
| 106 | fn extend<I: IntoIterator<Item = Element<T, D>>>(&mut self, i: I) { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 107 | for elem in i { |
| 108 | match elem { |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 109 | Element::Delimited(a, b) => self.inner.push((a, Some(b))), |
| 110 | Element::End(a) => self.inner.push((a, None)), |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 116 | impl<'a, T, D> IntoIterator for &'a Delimited<T, D> { |
| 117 | type Item = Element<&'a T, &'a D>; |
| 118 | type IntoIter = Iter<'a, T, D>; |
| 119 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 120 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 121 | Delimited::iter(self) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 125 | impl<'a, T, D> IntoIterator for &'a mut Delimited<T, D> { |
| 126 | type Item = Element<&'a mut T, &'a mut D>; |
| 127 | type IntoIter = IterMut<'a, T, D>; |
| 128 | |
| 129 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 130 | Delimited::iter_mut(self) |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 134 | impl<T, D> IntoIterator for Delimited<T, D> { |
| 135 | type Item = Element<T, D>; |
| 136 | type IntoIter = IntoIter<T, D>; |
| 137 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 138 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 139 | IntoIter { |
| 140 | inner: self.inner.into_iter(), |
| 141 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | impl<T, D> Default for Delimited<T, D> { |
| 146 | fn default() -> Self { |
| 147 | Delimited::new() |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | pub struct Iter<'a, T: 'a, D: 'a> { |
| 152 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 153 | } |
| 154 | |
| 155 | impl<'a, T, D> Iterator for Iter<'a, T, D> { |
| 156 | type Item = Element<&'a T, &'a D>; |
| 157 | |
| 158 | fn next(&mut self) -> Option<Element<&'a T, &'a D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 159 | self.inner.next().map(|pair| match pair.1 { |
| 160 | Some(ref delimited) => Element::Delimited(&pair.0, delimited), |
| 161 | None => Element::End(&pair.0), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 162 | }) |
| 163 | } |
| 164 | } |
| 165 | |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 166 | pub struct IterMut<'a, T: 'a, D: 'a> { |
| 167 | inner: slice::IterMut<'a, (T, Option<D>)>, |
| 168 | } |
| 169 | |
| 170 | impl<'a, T, D> Iterator for IterMut<'a, T, D> { |
| 171 | type Item = Element<&'a mut T, &'a mut D>; |
| 172 | |
| 173 | fn next(&mut self) -> Option<Element<&'a mut T, &'a mut D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 174 | self.inner.next().map(|pair| match pair.1 { |
| 175 | Some(ref mut delimited) => Element::Delimited(&mut pair.0, delimited), |
| 176 | None => Element::End(&mut pair.0), |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 177 | }) |
| 178 | } |
| 179 | } |
| 180 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 181 | pub struct IntoIter<T, D> { |
| 182 | inner: vec::IntoIter<(T, Option<D>)>, |
| 183 | } |
| 184 | |
| 185 | impl<T, D> Iterator for IntoIter<T, D> { |
| 186 | type Item = Element<T, D>; |
| 187 | |
| 188 | fn next(&mut self) -> Option<Element<T, D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 189 | self.inner.next().map(|pair| match pair.1 { |
| 190 | Some(v) => Element::Delimited(pair.0, v), |
| 191 | None => Element::End(pair.0), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 192 | }) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | pub enum Element<T, D> { |
| 197 | Delimited(T, D), |
| 198 | End(T), |
| 199 | } |
| 200 | |
| 201 | impl<T, D> Element<T, D> { |
| 202 | pub fn into_item(self) -> T { |
| 203 | match self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 204 | Element::Delimited(t, _) | Element::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | pub fn item(&self) -> &T { |
| 209 | match *self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 210 | Element::Delimited(ref t, _) | Element::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | pub fn item_mut(&mut self) -> &mut T { |
| 215 | match *self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 216 | Element::Delimited(ref mut t, _) | Element::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | pub fn delimiter(&self) -> Option<&D> { |
| 221 | match *self { |
| 222 | Element::Delimited(_, ref d) => Some(d), |
| 223 | Element::End(_) => None, |
| 224 | } |
| 225 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 226 | |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 227 | pub fn new(t: T, d: Option<D>) -> Self { |
| 228 | match d { |
| 229 | Some(d) => Element::Delimited(t, d), |
| 230 | None => Element::End(t), |
| 231 | } |
| 232 | } |
| 233 | |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 234 | pub fn into_tuple(self) -> (T, Option<D>) { |
| 235 | match self { |
| 236 | Element::Delimited(t, d) => (t, Some(d)), |
| 237 | Element::End(t) => (t, None), |
| 238 | } |
| 239 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 242 | #[cfg(feature = "parsing")] |
| 243 | mod parsing { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 244 | use super::Delimited; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 245 | use synom::Synom; |
| 246 | use cursor::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 247 | use parse_error; |
| 248 | use synom::PResult; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 249 | |
| 250 | impl<T, D> Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 251 | where |
| 252 | T: Synom, |
| 253 | D: Synom, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 254 | { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 255 | pub fn parse_separated(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 256 | Self::parse(input, T::parse, false) |
| 257 | } |
| 258 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 259 | pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 260 | Self::parse_separated_nonempty_with(input, T::parse) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 261 | } |
| 262 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 263 | pub fn parse_terminated(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 264 | Self::parse_terminated_with(input, T::parse) |
| 265 | } |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 266 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 267 | pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> { |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 268 | Self::parse_terminated_nonempty_with(input, T::parse) |
| 269 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | impl<T, D> Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 273 | where |
| 274 | D: Synom, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 275 | { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 276 | pub fn parse_separated_with( |
| 277 | input: Cursor, |
| 278 | parse: fn(Cursor) -> PResult<T>, |
| 279 | ) -> PResult<Self> { |
| 280 | Self::parse(input, parse, false) |
| 281 | } |
| 282 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 283 | pub fn parse_separated_nonempty_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 284 | input: Cursor, |
| 285 | parse: fn(Cursor) -> PResult<T>, |
| 286 | ) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 287 | match Self::parse(input, parse, false) { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame^] | 288 | Ok((ref b, _)) if b.is_empty() => parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 289 | other => other, |
| 290 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 293 | pub fn parse_terminated_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 294 | input: Cursor, |
| 295 | parse: fn(Cursor) -> PResult<T>, |
| 296 | ) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 297 | Self::parse(input, parse, true) |
| 298 | } |
| 299 | |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 300 | pub fn parse_terminated_nonempty_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 301 | input: Cursor, |
| 302 | parse: fn(Cursor) -> PResult<T>, |
| 303 | ) -> PResult<Self> { |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 304 | match Self::parse(input, parse, true) { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame^] | 305 | Ok((ref b, _)) if b.is_empty() => parse_error(), |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 306 | other => other, |
| 307 | } |
| 308 | } |
| 309 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 310 | fn parse( |
| 311 | mut input: Cursor, |
| 312 | parse: fn(Cursor) -> PResult<T>, |
| 313 | terminated: bool, |
| 314 | ) -> PResult<Self> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 315 | let mut res = Delimited::new(); |
| 316 | |
| 317 | // get the first element |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 318 | match parse(input) { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame^] | 319 | Err(_) => Ok((res, input)), |
| 320 | Ok((o, i)) => { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 321 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 322 | return parse_error(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 323 | } |
| 324 | input = i; |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 325 | res.push(o); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 326 | |
| 327 | // get the separator first |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame^] | 328 | while let Ok((s, i2)) = D::parse(input) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 329 | if i2 == input { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | |
| 333 | // get the element next |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame^] | 334 | if let Ok((o3, i3)) = parse(i2) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 335 | if i3 == i2 { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 336 | break; |
| 337 | } |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 338 | res.push_trailing(s); |
| 339 | res.push(o3); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 340 | input = i3; |
| 341 | } else { |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | if terminated { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame^] | 346 | if let Ok((sep, after)) = D::parse(input) { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 347 | res.push_trailing(sep); |
| 348 | input = after; |
| 349 | } |
| 350 | } |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame^] | 351 | Ok((res, input)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 358 | #[cfg(feature = "printing")] |
| 359 | mod printing { |
| 360 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 361 | use quote::{ToTokens, Tokens}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 362 | |
| 363 | impl<T, D> ToTokens for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 364 | where |
| 365 | T: ToTokens, |
| 366 | D: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 367 | { |
| 368 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 369 | tokens.append_all(self.iter()) |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | impl<T, D> ToTokens for Element<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 374 | where |
| 375 | T: ToTokens, |
| 376 | D: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 377 | { |
| 378 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 379 | match *self { |
| 380 | Element::Delimited(ref a, ref b) => { |
| 381 | a.to_tokens(tokens); |
| 382 | b.to_tokens(tokens); |
| 383 | } |
| 384 | Element::End(ref a) => a.to_tokens(tokens), |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |