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 | |
| 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 Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 42 | pub fn first(&self) -> Option<Element<&T, &D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 43 | self.inner.first().map(|&(ref t, ref d)| match *d { |
| 44 | Some(ref d) => Element::Delimited(t, d), |
| 45 | None => Element::End(t), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 46 | }) |
| 47 | } |
| 48 | |
| 49 | pub fn first_mut(&mut self) -> Option<Element<&mut T, &mut D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 50 | self.inner |
| 51 | .first_mut() |
| 52 | .map(|&mut (ref mut t, ref mut d)| match *d { |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 53 | Some(ref mut d) => Element::Delimited(t, d), |
| 54 | None => Element::End(t), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 55 | }) |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | pub fn last(&self) -> Option<Element<&T, &D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 59 | self.inner.last().map(|&(ref t, ref d)| match *d { |
| 60 | Some(ref d) => Element::Delimited(t, d), |
| 61 | None => Element::End(t), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 62 | }) |
| 63 | } |
| 64 | |
| 65 | pub fn last_mut(&mut self) -> Option<Element<&mut T, &mut D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 66 | self.inner |
| 67 | .last_mut() |
| 68 | .map(|&mut (ref mut t, ref mut d)| match *d { |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 69 | Some(ref mut d) => Element::Delimited(t, d), |
| 70 | None => Element::End(t), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 71 | }) |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 74 | pub fn iter(&self) -> Iter<T, D> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 75 | Iter { |
| 76 | inner: self.inner.iter(), |
| 77 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 80 | pub fn iter_mut(&mut self) -> IterMut<T, D> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 81 | IterMut { |
| 82 | inner: self.inner.iter_mut(), |
| 83 | } |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 86 | pub fn items(&self) -> Items<T, D> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 87 | Items { |
| 88 | inner: self.inner.iter(), |
| 89 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | pub fn push(&mut self, token: Element<T, D>) { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 93 | assert!(self.empty_or_trailing()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 94 | 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 Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 116 | pub fn push_default(&mut self, token: T) |
| 117 | where |
| 118 | D: Default, |
| 119 | { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 120 | if self.empty_or_trailing() { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 121 | 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 Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 128 | self.inner.pop().map(|e| match e { |
| 129 | (t, Some(d)) => Element::Delimited(t, d), |
| 130 | (t, None) => Element::End(t), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 131 | }) |
| 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 Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 141 | |
| 142 | /// 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^] | 143 | /// delimiter. |
| 144 | /// |
| 145 | /// Equivalent to `delimited.is_empty() || delimited.trailing_delim()`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 146 | pub fn empty_or_trailing(&self) -> bool { |
| 147 | self.is_empty() || self.trailing_delim() |
| 148 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 151 | #[cfg(feature = "extra-traits")] |
| 152 | impl<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 Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 158 | impl<T, D> From<Vec<(T, Option<D>)>> for Delimited<T, D> { |
| 159 | fn from(v: Vec<(T, Option<D>)>) -> Self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 160 | Delimited { inner: v } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | impl<T, D> From<Vec<T>> for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 165 | where |
| 166 | D: Default, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 167 | { |
| 168 | fn from(v: Vec<T>) -> Self { |
Alex Crichton | afb49d2 | 2017-07-06 14:47:37 -0700 | [diff] [blame] | 169 | let len = v.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 170 | Delimited { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 171 | 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 Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | impl<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 Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 191 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 192 | ret |
| 193 | } |
| 194 | } |
| 195 | |
Alex Crichton | d3743d1 | 2017-07-07 20:55:24 -0700 | [diff] [blame] | 196 | impl<T, D> FromIterator<T> for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 197 | where |
| 198 | D: Default, |
Alex Crichton | d3743d1 | 2017-07-07 20:55:24 -0700 | [diff] [blame] | 199 | { |
| 200 | fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self { |
| 201 | let mut ret = Delimited::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 202 | ret.extend(i); |
Alex Crichton | d3743d1 | 2017-07-07 20:55:24 -0700 | [diff] [blame] | 203 | ret |
| 204 | } |
| 205 | } |
| 206 | |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 207 | impl<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 | |
| 218 | impl<T, D> Extend<T> for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 219 | where |
| 220 | D: Default, |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 221 | { |
| 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 Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 229 | impl<'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 Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 233 | fn into_iter(self) -> Self::IntoIter { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 234 | <Delimited<T, D>>::iter(self) |
| 235 | } |
| 236 | } |
| 237 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 238 | impl<'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 Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 247 | impl<T, D> IntoIterator for Delimited<T, D> { |
| 248 | type Item = Element<T, D>; |
| 249 | type IntoIter = IntoIter<T, D>; |
| 250 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 251 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 252 | IntoIter { |
| 253 | inner: self.inner.into_iter(), |
| 254 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | impl<T, D> Default for Delimited<T, D> { |
| 259 | fn default() -> Self { |
| 260 | Delimited::new() |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | pub struct Iter<'a, T: 'a, D: 'a> { |
| 265 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 266 | } |
| 267 | |
| 268 | impl<'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 Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 272 | self.inner.next().map(|pair| match pair.1 { |
| 273 | Some(ref delimited) => Element::Delimited(&pair.0, delimited), |
| 274 | None => Element::End(&pair.0), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 275 | }) |
| 276 | } |
| 277 | } |
| 278 | |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 279 | pub struct IterMut<'a, T: 'a, D: 'a> { |
| 280 | inner: slice::IterMut<'a, (T, Option<D>)>, |
| 281 | } |
| 282 | |
| 283 | impl<'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 Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 287 | 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 Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 290 | }) |
| 291 | } |
| 292 | } |
| 293 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 294 | pub struct Items<'a, T: 'a, D: 'a> { |
| 295 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 296 | } |
| 297 | |
| 298 | impl<'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 | |
| 306 | pub struct IntoIter<T, D> { |
| 307 | inner: vec::IntoIter<(T, Option<D>)>, |
| 308 | } |
| 309 | |
| 310 | impl<T, D> Iterator for IntoIter<T, D> { |
| 311 | type Item = Element<T, D>; |
| 312 | |
| 313 | fn next(&mut self) -> Option<Element<T, D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 314 | self.inner.next().map(|pair| match pair.1 { |
| 315 | Some(v) => Element::Delimited(pair.0, v), |
| 316 | None => Element::End(pair.0), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 317 | }) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | pub enum Element<T, D> { |
| 322 | Delimited(T, D), |
| 323 | End(T), |
| 324 | } |
| 325 | |
| 326 | impl<T, D> Element<T, D> { |
| 327 | pub fn into_item(self) -> T { |
| 328 | match self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 329 | Element::Delimited(t, _) | Element::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | pub fn item(&self) -> &T { |
| 334 | match *self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 335 | Element::Delimited(ref t, _) | Element::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | pub fn item_mut(&mut self) -> &mut T { |
| 340 | match *self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 341 | Element::Delimited(ref mut t, _) | Element::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 342 | } |
| 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 Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 351 | |
| 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 Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 360 | #[cfg(feature = "parsing")] |
| 361 | mod parsing { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 362 | use super::Delimited; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 363 | use synom::Synom; |
| 364 | use cursor::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 365 | use parse_error; |
| 366 | use synom::PResult; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 367 | |
| 368 | impl<T, D> Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 369 | where |
| 370 | T: Synom, |
| 371 | D: Synom, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 372 | { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 373 | pub fn parse_separated(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 374 | Self::parse(input, T::parse, false) |
| 375 | } |
| 376 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 377 | pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 378 | Self::parse_separated_nonempty_with(input, T::parse) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 379 | } |
| 380 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 381 | pub fn parse_terminated(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 382 | Self::parse_terminated_with(input, T::parse) |
| 383 | } |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 384 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 385 | pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> { |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 386 | Self::parse_terminated_nonempty_with(input, T::parse) |
| 387 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | impl<T, D> Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 391 | where |
| 392 | D: Synom, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 393 | { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 394 | 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 Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 401 | pub fn parse_separated_nonempty_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 402 | input: Cursor, |
| 403 | parse: fn(Cursor) -> PResult<T>, |
| 404 | ) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 405 | match Self::parse(input, parse, false) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 406 | Ok((_, ref b)) if b.is_empty() => parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 407 | other => other, |
| 408 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 411 | pub fn parse_terminated_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 412 | input: Cursor, |
| 413 | parse: fn(Cursor) -> PResult<T>, |
| 414 | ) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 415 | Self::parse(input, parse, true) |
| 416 | } |
| 417 | |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 418 | pub fn parse_terminated_nonempty_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 419 | input: Cursor, |
| 420 | parse: fn(Cursor) -> PResult<T>, |
| 421 | ) -> PResult<Self> { |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 422 | match Self::parse(input, parse, true) { |
| 423 | Ok((_, ref b)) if b.is_empty() => parse_error(), |
| 424 | other => other, |
| 425 | } |
| 426 | } |
| 427 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 428 | fn parse( |
| 429 | mut input: Cursor, |
| 430 | parse: fn(Cursor) -> PResult<T>, |
| 431 | terminated: bool, |
| 432 | ) -> PResult<Self> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 433 | let mut res = Delimited::new(); |
| 434 | |
| 435 | // get the first element |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 436 | match parse(input) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 437 | Err(_) => Ok((input, res)), |
| 438 | Ok((i, o)) => { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 439 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 440 | return parse_error(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 441 | } |
| 442 | input = i; |
| 443 | res.push_first(o); |
| 444 | |
| 445 | // get the separator first |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 446 | while let Ok((i2, s)) = D::parse(input) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 447 | if i2 == input { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 448 | break; |
| 449 | } |
| 450 | |
| 451 | // get the element next |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 452 | if let Ok((i3, o3)) = parse(i2) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 453 | if i3 == i2 { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 454 | break; |
| 455 | } |
| 456 | res.push_next(o3, s); |
| 457 | input = i3; |
| 458 | } else { |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | if terminated { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 463 | if let Ok((after, sep)) = D::parse(input) { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 464 | res.push_trailing(sep); |
| 465 | input = after; |
| 466 | } |
| 467 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 468 | Ok((input, res)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 475 | #[cfg(feature = "printing")] |
| 476 | mod printing { |
| 477 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 478 | use quote::{ToTokens, Tokens}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 479 | |
| 480 | impl<T, D> ToTokens for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 481 | where |
| 482 | T: ToTokens, |
| 483 | D: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 484 | { |
| 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 Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 491 | where |
| 492 | T: ToTokens, |
| 493 | D: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 494 | { |
| 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 | } |