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>) { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 93 | assert!(self.is_empty() || self.trailing_delim()); |
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 | { |
Alex Crichton | 337cd46 | 2017-07-06 14:47:25 -0700 | [diff] [blame] | 120 | if self.is_empty() || self.trailing_delim() { |
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 |
| 143 | /// delimiter. This is useful within `ToTokens` implementations for `syn`. |
| 144 | #[doc(hidden)] |
| 145 | pub fn empty_or_trailing(&self) -> bool { |
| 146 | self.is_empty() || self.trailing_delim() |
| 147 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 150 | #[cfg(feature = "extra-traits")] |
| 151 | impl<T: Debug, D: Debug> Debug for Delimited<T, D> { |
| 152 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 153 | self.inner.fmt(f) |
| 154 | } |
| 155 | } |
| 156 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 157 | impl<T, D> From<Vec<(T, Option<D>)>> for Delimited<T, D> { |
| 158 | fn from(v: Vec<(T, Option<D>)>) -> Self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 159 | Delimited { inner: v } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | impl<T, D> From<Vec<T>> for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 164 | where |
| 165 | D: Default, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 166 | { |
| 167 | fn from(v: Vec<T>) -> Self { |
Alex Crichton | afb49d2 | 2017-07-06 14:47:37 -0700 | [diff] [blame] | 168 | let len = v.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 169 | Delimited { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 170 | inner: v.into_iter() |
| 171 | .enumerate() |
| 172 | .map(|(i, item)| { |
| 173 | ( |
| 174 | item, |
| 175 | if i + 1 == len { |
| 176 | None |
| 177 | } else { |
| 178 | Some(D::default()) |
| 179 | }, |
| 180 | ) |
| 181 | }) |
| 182 | .collect(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | impl<T, D> FromIterator<Element<T, D>> for Delimited<T, D> { |
| 188 | fn from_iter<I: IntoIterator<Item = Element<T, D>>>(i: I) -> Self { |
| 189 | let mut ret = Delimited::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 190 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 191 | ret |
| 192 | } |
| 193 | } |
| 194 | |
Alex Crichton | d3743d1 | 2017-07-07 20:55:24 -0700 | [diff] [blame] | 195 | impl<T, D> FromIterator<T> for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 196 | where |
| 197 | D: Default, |
Alex Crichton | d3743d1 | 2017-07-07 20:55:24 -0700 | [diff] [blame] | 198 | { |
| 199 | fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self { |
| 200 | let mut ret = Delimited::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 201 | ret.extend(i); |
Alex Crichton | d3743d1 | 2017-07-07 20:55:24 -0700 | [diff] [blame] | 202 | ret |
| 203 | } |
| 204 | } |
| 205 | |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 206 | impl<T, D> Extend<Element<T, D>> for Delimited<T, D> { |
| 207 | fn extend<I: IntoIterator<Item = Element<T, D>>>(&mut self, i: I) { |
| 208 | for element in i { |
| 209 | match element { |
| 210 | Element::Delimited(a, b) => self.inner.push((a, Some(b))), |
| 211 | Element::End(a) => self.inner.push((a, None)), |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | impl<T, D> Extend<T> for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 218 | where |
| 219 | D: Default, |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 220 | { |
| 221 | fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) { |
| 222 | for element in i { |
| 223 | self.push_default(element); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 228 | impl<'a, T, D> IntoIterator for &'a Delimited<T, D> { |
| 229 | type Item = Element<&'a T, &'a D>; |
| 230 | type IntoIter = Iter<'a, T, D>; |
| 231 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 232 | fn into_iter(self) -> Self::IntoIter { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 233 | <Delimited<T, D>>::iter(self) |
| 234 | } |
| 235 | } |
| 236 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 237 | impl<'a, T, D> IntoIterator for &'a mut Delimited<T, D> { |
| 238 | type Item = Element<&'a mut T, &'a mut D>; |
| 239 | type IntoIter = IterMut<'a, T, D>; |
| 240 | |
| 241 | fn into_iter(self) -> Self::IntoIter { |
| 242 | <Delimited<T, D>>::iter_mut(self) |
| 243 | } |
| 244 | } |
| 245 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 246 | impl<T, D> IntoIterator for Delimited<T, D> { |
| 247 | type Item = Element<T, D>; |
| 248 | type IntoIter = IntoIter<T, D>; |
| 249 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 250 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 251 | IntoIter { |
| 252 | inner: self.inner.into_iter(), |
| 253 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | impl<T, D> Default for Delimited<T, D> { |
| 258 | fn default() -> Self { |
| 259 | Delimited::new() |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | pub struct Iter<'a, T: 'a, D: 'a> { |
| 264 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 265 | } |
| 266 | |
| 267 | impl<'a, T, D> Iterator for Iter<'a, T, D> { |
| 268 | type Item = Element<&'a T, &'a D>; |
| 269 | |
| 270 | fn next(&mut self) -> Option<Element<&'a T, &'a D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 271 | self.inner.next().map(|pair| match pair.1 { |
| 272 | Some(ref delimited) => Element::Delimited(&pair.0, delimited), |
| 273 | None => Element::End(&pair.0), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 274 | }) |
| 275 | } |
| 276 | } |
| 277 | |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 278 | pub struct IterMut<'a, T: 'a, D: 'a> { |
| 279 | inner: slice::IterMut<'a, (T, Option<D>)>, |
| 280 | } |
| 281 | |
| 282 | impl<'a, T, D> Iterator for IterMut<'a, T, D> { |
| 283 | type Item = Element<&'a mut T, &'a mut D>; |
| 284 | |
| 285 | fn next(&mut self) -> Option<Element<&'a mut T, &'a mut D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 286 | self.inner.next().map(|pair| match pair.1 { |
| 287 | Some(ref mut delimited) => Element::Delimited(&mut pair.0, delimited), |
| 288 | None => Element::End(&mut pair.0), |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 289 | }) |
| 290 | } |
| 291 | } |
| 292 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 293 | pub struct Items<'a, T: 'a, D: 'a> { |
| 294 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 295 | } |
| 296 | |
| 297 | impl<'a, T, D> Iterator for Items<'a, T, D> { |
| 298 | type Item = &'a T; |
| 299 | |
| 300 | fn next(&mut self) -> Option<&'a T> { |
| 301 | self.inner.next().map(|pair| &pair.0) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | pub struct IntoIter<T, D> { |
| 306 | inner: vec::IntoIter<(T, Option<D>)>, |
| 307 | } |
| 308 | |
| 309 | impl<T, D> Iterator for IntoIter<T, D> { |
| 310 | type Item = Element<T, D>; |
| 311 | |
| 312 | fn next(&mut self) -> Option<Element<T, D>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 313 | self.inner.next().map(|pair| match pair.1 { |
| 314 | Some(v) => Element::Delimited(pair.0, v), |
| 315 | None => Element::End(pair.0), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 316 | }) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | pub enum Element<T, D> { |
| 321 | Delimited(T, D), |
| 322 | End(T), |
| 323 | } |
| 324 | |
| 325 | impl<T, D> Element<T, D> { |
| 326 | pub fn into_item(self) -> T { |
| 327 | match self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 328 | Element::Delimited(t, _) | Element::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | pub fn item(&self) -> &T { |
| 333 | match *self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 334 | Element::Delimited(ref t, _) | Element::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | pub fn item_mut(&mut self) -> &mut T { |
| 339 | match *self { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 340 | Element::Delimited(ref mut t, _) | Element::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | pub fn delimiter(&self) -> Option<&D> { |
| 345 | match *self { |
| 346 | Element::Delimited(_, ref d) => Some(d), |
| 347 | Element::End(_) => None, |
| 348 | } |
| 349 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 350 | |
| 351 | pub fn into_tuple(self) -> (T, Option<D>) { |
| 352 | match self { |
| 353 | Element::Delimited(t, d) => (t, Some(d)), |
| 354 | Element::End(t) => (t, None), |
| 355 | } |
| 356 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 359 | #[cfg(feature = "parsing")] |
| 360 | mod parsing { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 361 | use super::Delimited; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 362 | use synom::Synom; |
| 363 | use cursor::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame^] | 364 | use parse_error; |
| 365 | use synom::PResult; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 366 | |
| 367 | impl<T, D> Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 368 | where |
| 369 | T: Synom, |
| 370 | D: Synom, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 371 | { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 372 | pub fn parse_separated(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 373 | Self::parse(input, T::parse, false) |
| 374 | } |
| 375 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 376 | pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 377 | Self::parse_separated_nonempty_with(input, T::parse) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 378 | } |
| 379 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 380 | pub fn parse_terminated(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 381 | Self::parse_terminated_with(input, T::parse) |
| 382 | } |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 383 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 384 | pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> { |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 385 | Self::parse_terminated_nonempty_with(input, T::parse) |
| 386 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | impl<T, D> Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 390 | where |
| 391 | D: Synom, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 392 | { |
| 393 | pub fn parse_separated_nonempty_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 394 | input: Cursor, |
| 395 | parse: fn(Cursor) -> PResult<T>, |
| 396 | ) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 397 | match Self::parse(input, parse, false) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 398 | Ok((_, ref b)) if b.is_empty() => parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 399 | other => other, |
| 400 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 403 | pub fn parse_terminated_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 404 | input: Cursor, |
| 405 | parse: fn(Cursor) -> PResult<T>, |
| 406 | ) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 407 | Self::parse(input, parse, true) |
| 408 | } |
| 409 | |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 410 | pub fn parse_terminated_nonempty_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 411 | input: Cursor, |
| 412 | parse: fn(Cursor) -> PResult<T>, |
| 413 | ) -> PResult<Self> { |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 414 | match Self::parse(input, parse, true) { |
| 415 | Ok((_, ref b)) if b.is_empty() => parse_error(), |
| 416 | other => other, |
| 417 | } |
| 418 | } |
| 419 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 420 | fn parse( |
| 421 | mut input: Cursor, |
| 422 | parse: fn(Cursor) -> PResult<T>, |
| 423 | terminated: bool, |
| 424 | ) -> PResult<Self> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 425 | let mut res = Delimited::new(); |
| 426 | |
| 427 | // get the first element |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 428 | match parse(input) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 429 | Err(_) => Ok((input, res)), |
| 430 | Ok((i, o)) => { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 431 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 432 | return parse_error(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 433 | } |
| 434 | input = i; |
| 435 | res.push_first(o); |
| 436 | |
| 437 | // get the separator first |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 438 | while let Ok((i2, s)) = D::parse(input) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 439 | if i2 == input { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 440 | break; |
| 441 | } |
| 442 | |
| 443 | // get the element next |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 444 | if let Ok((i3, o3)) = parse(i2) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 445 | if i3 == i2 { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | res.push_next(o3, s); |
| 449 | input = i3; |
| 450 | } else { |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | if terminated { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 455 | if let Ok((after, sep)) = D::parse(input) { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 456 | res.push_trailing(sep); |
| 457 | input = after; |
| 458 | } |
| 459 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 460 | Ok((input, res)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 467 | #[cfg(feature = "printing")] |
| 468 | mod printing { |
| 469 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 470 | use quote::{ToTokens, Tokens}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 471 | |
| 472 | impl<T, D> ToTokens for Delimited<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 473 | where |
| 474 | T: ToTokens, |
| 475 | D: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 476 | { |
| 477 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 478 | tokens.append_all(self.iter()) |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | impl<T, D> ToTokens for Element<T, D> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 483 | where |
| 484 | T: ToTokens, |
| 485 | D: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 486 | { |
| 487 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 488 | match *self { |
| 489 | Element::Delimited(ref a, ref b) => { |
| 490 | a.to_tokens(tokens); |
| 491 | b.to_tokens(tokens); |
| 492 | } |
| 493 | Element::End(ref a) => a.to_tokens(tokens), |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |