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