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 | |
| 46 | pub fn into_iter(self) -> IntoIter<T, D> { |
| 47 | IntoIter { inner: self.inner.into_iter() } |
| 48 | } |
| 49 | |
| 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>) { |
| 55 | assert!(self.len() == 0 || self.trailing_delim()); |
| 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 { |
| 79 | if self.len() == 0 { |
| 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 | } |
| 134 | return ret |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl<T, D> Default for Delimited<T, D> { |
| 139 | fn default() -> Self { |
| 140 | Delimited::new() |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | pub struct Iter<'a, T: 'a, D: 'a> { |
| 145 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 146 | } |
| 147 | |
| 148 | impl<'a, T, D> Iterator for Iter<'a, T, D> { |
| 149 | type Item = Element<&'a T, &'a D>; |
| 150 | |
| 151 | fn next(&mut self) -> Option<Element<&'a T, &'a D>> { |
| 152 | self.inner.next().map(|pair| { |
| 153 | match pair.1 { |
| 154 | Some(ref delimited) => Element::Delimited(&pair.0, delimited), |
| 155 | None => Element::End(&pair.0), |
| 156 | } |
| 157 | }) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | pub struct Items<'a, T: 'a, D: 'a> { |
| 162 | inner: slice::Iter<'a, (T, Option<D>)>, |
| 163 | } |
| 164 | |
| 165 | impl<'a, T, D> Iterator for Items<'a, T, D> { |
| 166 | type Item = &'a T; |
| 167 | |
| 168 | fn next(&mut self) -> Option<&'a T> { |
| 169 | self.inner.next().map(|pair| &pair.0) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | pub struct IntoIter<T, D> { |
| 174 | inner: vec::IntoIter<(T, Option<D>)>, |
| 175 | } |
| 176 | |
| 177 | impl<T, D> Iterator for IntoIter<T, D> { |
| 178 | type Item = Element<T, D>; |
| 179 | |
| 180 | fn next(&mut self) -> Option<Element<T, D>> { |
| 181 | self.inner.next().map(|pair| { |
| 182 | match pair.1 { |
| 183 | Some(v) => Element::Delimited(pair.0, v), |
| 184 | None => Element::End(pair.0) |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | pub enum Element<T, D> { |
| 191 | Delimited(T, D), |
| 192 | End(T), |
| 193 | } |
| 194 | |
| 195 | impl<T, D> Element<T, D> { |
| 196 | pub fn into_item(self) -> T { |
| 197 | match self { |
| 198 | Element::Delimited(t, _) | |
| 199 | Element::End(t) => t, |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | pub fn item(&self) -> &T { |
| 204 | match *self { |
| 205 | Element::Delimited(ref t, _) | |
| 206 | Element::End(ref t) => t, |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | pub fn item_mut(&mut self) -> &mut T { |
| 211 | match *self { |
| 212 | Element::Delimited(ref mut t, _) | |
| 213 | Element::End(ref mut t) => t, |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | pub fn delimiter(&self) -> Option<&D> { |
| 218 | match *self { |
| 219 | Element::Delimited(_, ref d) => Some(d), |
| 220 | Element::End(_) => None, |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame^] | 225 | #[cfg(feature = "parsing")] |
| 226 | mod parsing { |
| 227 | use proc_macro2::TokenTree; |
| 228 | |
| 229 | use super::Delimited; |
| 230 | use {IResult, Synom}; |
| 231 | |
| 232 | impl<T, D> Delimited<T, D> |
| 233 | where T: Synom, |
| 234 | D: Synom, |
| 235 | { |
| 236 | pub fn parse_separated(input: &[TokenTree]) |
| 237 | -> IResult<&[TokenTree], Self> |
| 238 | { |
| 239 | Self::parse(input, false) |
| 240 | } |
| 241 | |
| 242 | pub fn parse_terminated(input: &[TokenTree]) |
| 243 | -> IResult<&[TokenTree], Self> |
| 244 | { |
| 245 | Self::parse(input, true) |
| 246 | } |
| 247 | |
| 248 | fn parse(mut input: &[TokenTree], terminated: bool) |
| 249 | -> IResult<&[TokenTree], Self> |
| 250 | { |
| 251 | let mut res = Delimited::new(); |
| 252 | |
| 253 | // get the first element |
| 254 | match T::parse(input) { |
| 255 | IResult::Error => IResult::Done(input, res), |
| 256 | IResult::Done(i, o) => { |
| 257 | if i.len() == input.len() { |
| 258 | return IResult::Error |
| 259 | } |
| 260 | input = i; |
| 261 | res.push_first(o); |
| 262 | |
| 263 | // get the separator first |
| 264 | while let IResult::Done(i2, s) = D::parse(input) { |
| 265 | if i2.len() == input.len() { |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | // get the element next |
| 270 | if let IResult::Done(i3, o3) = T::parse(i2) { |
| 271 | if i3.len() == i2.len() { |
| 272 | break; |
| 273 | } |
| 274 | res.push_next(o3, s); |
| 275 | input = i3; |
| 276 | } else { |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | if terminated { |
| 281 | if let IResult::Done(after, sep) = D::parse(input) { |
| 282 | res.push_trailing(sep); |
| 283 | input = after; |
| 284 | } |
| 285 | } |
| 286 | IResult::Done(input, res) |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 293 | #[cfg(feature = "printing")] |
| 294 | mod printing { |
| 295 | use super::*; |
| 296 | use quote::{Tokens, ToTokens}; |
| 297 | |
| 298 | |
| 299 | impl<T, D> ToTokens for Delimited<T, D> |
| 300 | where T: ToTokens, |
| 301 | D: ToTokens, |
| 302 | { |
| 303 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 304 | tokens.append_all(self.iter()) |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | impl<T, D> ToTokens for Element<T, D> |
| 309 | where T: ToTokens, |
| 310 | D: ToTokens, |
| 311 | { |
| 312 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 313 | match *self { |
| 314 | Element::Delimited(ref a, ref b) => { |
| 315 | a.to_tokens(tokens); |
| 316 | b.to_tokens(tokens); |
| 317 | } |
| 318 | Element::End(ref a) => a.to_tokens(tokens), |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |