blob: 0cb650e903af15f16d4ecd0f76dd98a208dcaf63 [file] [log] [blame]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001use std::iter::FromIterator;
2use std::slice;
3use std::vec;
4
Alex Crichton7b9e02f2017-05-30 15:54:33 -07005#[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash, Debug))]
6#[cfg_attr(feature = "clone-impls", derive(Clone))]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07007pub struct Delimited<T, D> {
8 inner: Vec<(T, Option<D>)>
9}
10
11impl<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 Crichton164c5332017-07-06 13:18:34 -070046 pub fn iter_mut(&mut self) -> IterMut<T, D> {
47 IterMut { inner: self.inner.iter_mut() }
48 }
49
Alex Crichtonccbb45d2017-05-23 10:58:24 -070050 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 Crichton954046c2017-05-30 21:49:42 -070055 assert!(self.is_empty() || self.trailing_delim());
Alex Crichtonccbb45d2017-05-23 10:58:24 -070056 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 Crichton954046c2017-05-30 21:49:42 -070079 if self.is_empty() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070080 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
104impl<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
112impl<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
125impl<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 Crichton954046c2017-05-30 21:49:42 -0700134 ret
135 }
136}
137
138impl<'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
147impl<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 Crichtonccbb45d2017-05-23 10:58:24 -0700153 }
154}
155
156impl<T, D> Default for Delimited<T, D> {
157 fn default() -> Self {
158 Delimited::new()
159 }
160}
161
162pub struct Iter<'a, T: 'a, D: 'a> {
163 inner: slice::Iter<'a, (T, Option<D>)>,
164}
165
166impl<'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 Crichton164c5332017-07-06 13:18:34 -0700179pub struct IterMut<'a, T: 'a, D: 'a> {
180 inner: slice::IterMut<'a, (T, Option<D>)>,
181}
182
183impl<'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 Crichtonccbb45d2017-05-23 10:58:24 -0700196pub struct Items<'a, T: 'a, D: 'a> {
197 inner: slice::Iter<'a, (T, Option<D>)>,
198}
199
200impl<'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
208pub struct IntoIter<T, D> {
209 inner: vec::IntoIter<(T, Option<D>)>,
210}
211
212impl<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
225pub enum Element<T, D> {
226 Delimited(T, D),
227 End(T),
228}
229
230impl<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 Crichton7b9e02f2017-05-30 15:54:33 -0700260#[cfg(feature = "parsing")]
261mod parsing {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700262 use super::Delimited;
Michael Layzell760fd662017-05-31 22:46:05 -0400263 use {PResult, Cursor, Synom, parse_error};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700264
265 impl<T, D> Delimited<T, D>
266 where T: Synom,
267 D: Synom,
268 {
Michael Layzell760fd662017-05-31 22:46:05 -0400269 pub fn parse_separated(input: Cursor) -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700270 {
Alex Crichton954046c2017-05-30 21:49:42 -0700271 Self::parse(input, T::parse, false)
272 }
273
Michael Layzell760fd662017-05-31 22:46:05 -0400274 pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700275 {
276 Self::parse_separated_nonempty_with(input, T::parse)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700277 }
278
Michael Layzell760fd662017-05-31 22:46:05 -0400279 pub fn parse_terminated(input: Cursor) -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700280 {
Alex Crichton954046c2017-05-30 21:49:42 -0700281 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 Layzell760fd662017-05-31 22:46:05 -0400289 input: Cursor,
290 parse: fn(Cursor) -> PResult<T>)
291 -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700292 {
293 match Self::parse(input, parse, false) {
Michael Layzell760fd662017-05-31 22:46:05 -0400294 Ok((_, ref b)) if b.is_empty() => parse_error(),
Alex Crichton954046c2017-05-30 21:49:42 -0700295 other => other,
296 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700297 }
298
Alex Crichton954046c2017-05-30 21:49:42 -0700299 pub fn parse_terminated_with(
Michael Layzell760fd662017-05-31 22:46:05 -0400300 input: Cursor,
301 parse: fn(Cursor) -> PResult<T>)
302 -> PResult<Self>
Alex Crichton954046c2017-05-30 21:49:42 -0700303 {
304 Self::parse(input, parse, true)
305 }
306
Michael Layzell760fd662017-05-31 22:46:05 -0400307 fn parse(mut input: Cursor,
308 parse: fn(Cursor) -> PResult<T>,
Alex Crichton954046c2017-05-30 21:49:42 -0700309 terminated: bool)
Michael Layzell760fd662017-05-31 22:46:05 -0400310 -> PResult<Self>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700311 {
312 let mut res = Delimited::new();
313
314 // get the first element
Alex Crichton954046c2017-05-30 21:49:42 -0700315 match parse(input) {
Michael Layzell760fd662017-05-31 22:46:05 -0400316 Err(_) => Ok((input, res)),
317 Ok((i, o)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400318 if i == input {
Michael Layzell760fd662017-05-31 22:46:05 -0400319 return parse_error();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700320 }
321 input = i;
322 res.push_first(o);
323
324 // get the separator first
Michael Layzell760fd662017-05-31 22:46:05 -0400325 while let Ok((i2, s)) = D::parse(input) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400326 if i2 == input {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700327 break;
328 }
329
330 // get the element next
Michael Layzell760fd662017-05-31 22:46:05 -0400331 if let Ok((i3, o3)) = parse(i2) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400332 if i3 == i2 {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700333 break;
334 }
335 res.push_next(o3, s);
336 input = i3;
337 } else {
338 break;
339 }
340 }
341 if terminated {
Michael Layzell760fd662017-05-31 22:46:05 -0400342 if let Ok((after, sep)) = D::parse(input) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700343 res.push_trailing(sep);
344 input = after;
345 }
346 }
Michael Layzell760fd662017-05-31 22:46:05 -0400347 Ok((input, res))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700348 }
349 }
350 }
351 }
352}
353
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700354#[cfg(feature = "printing")]
355mod 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}