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