1use super::*;
9
10pub type Ignored<P, O> = To<P, O, ()>;
12
13pub type IgnoreThen<A, B, O, U> = Map<Then<A, B>, fn((O, U)) -> U, (O, U)>;
15
16pub type ThenIgnore<A, B, O, U> = Map<Then<A, B>, fn((O, U)) -> O, (O, U)>;
18
19#[must_use]
21#[derive(Copy, Clone)]
22pub struct Or<A, B>(pub(crate) A, pub(crate) B);
23
24impl<I: Clone, O, A: Parser<I, O, Error = E>, B: Parser<I, O, Error = E>, E: Error<I>> Parser<I, O>
25 for Or<A, B>
26{
27 type Error = E;
28
29 #[inline]
30 fn parse_inner<D: Debugger>(
31 &self,
32 debugger: &mut D,
33 stream: &mut StreamOf<I, E>,
34 ) -> PResult<I, O, E> {
35 let pre_state = stream.save();
36
37 #[allow(deprecated)]
38 let a_res = debugger.invoke(&self.0, stream);
39 let a_state = stream.save();
40
41 if a_res.0.is_empty() {
45 if let (a_errors, Ok(a_out)) = a_res {
46 return (a_errors, Ok(a_out));
47 }
48 }
49
50 stream.revert(pre_state);
51
52 #[allow(deprecated)]
53 let b_res = debugger.invoke(&self.1, stream);
54 let b_state = stream.save();
55
56 if b_res.0.is_empty() {
57 if let (b_errors, Ok(b_out)) = b_res {
58 return (b_errors, Ok(b_out));
59 }
60 }
61
62 #[inline]
63 fn choose_between<I: Clone, O, E: Error<I>>(
64 a_res: PResult<I, O, E>,
65 a_state: usize,
66 b_res: PResult<I, O, E>,
67 b_state: usize,
68 stream: &mut StreamOf<I, E>,
69 ) -> PResult<I, O, E> {
70 fn zip_with<A, B, R, F: FnOnce(A, B) -> R>(
71 a: Option<A>,
72 b: Option<B>,
73 f: F,
74 ) -> Option<R> {
75 match (a, b) {
76 (Some(a), Some(b)) => Some(f(a, b)),
77 _ => None,
78 }
79 }
80
81 let is_a = match (&a_res, &b_res) {
82 ((a_errors, Ok(a_out)), (b_errors, Ok(b_out))) => {
83 match a_errors.len().cmp(&b_errors.len()) {
84 Ordering::Greater => false,
85 Ordering::Less => true,
86 Ordering::Equal => {
87 match zip_with(a_errors.last(), b_errors.last(), |a, b| a.at.cmp(&b.at))
88 {
89 Some(Ordering::Greater) => true,
90 Some(Ordering::Less) => false,
91 _ => match zip_with(a_out.1.as_ref(), b_out.1.as_ref(), |a, b| {
92 a.at.cmp(&b.at)
93 }) {
94 Some(Ordering::Greater) => true,
95 Some(Ordering::Less) => false,
96 _ => true,
97 },
98 }
99 }
100 }
101 }
102 ((_a_errors, Ok(_)), (_b_errors, Err(_))) => true,
104 ((_a_errors, Err(_)), (_b_errors, Ok(_))) => false,
106 ((a_errors, Err(a_err)), (b_errors, Err(b_err))) => match a_err.at.cmp(&b_err.at) {
107 Ordering::Greater => true,
108 Ordering::Less => false,
109 Ordering::Equal => match a_errors.len().cmp(&b_errors.len()) {
110 Ordering::Greater => false,
111 Ordering::Less => true,
112 Ordering::Equal => {
113 match zip_with(a_errors.last(), b_errors.last(), |a, b| a.at.cmp(&b.at))
114 {
115 Some(Ordering::Greater) => true,
116 Some(Ordering::Less) => false,
117 _ => {
120 return (
121 a_res.0,
122 Err(a_res.1.err().unwrap().max(b_res.1.err().unwrap())),
123 )
124 }
125 }
126 }
127 },
128 },
129 };
130
131 if is_a {
132 stream.revert(a_state);
133 (
134 a_res.0,
135 a_res.1.map(|(out, alt)| {
136 (
137 out,
138 merge_alts(alt, b_res.1.map(|(_, alt)| alt).unwrap_or_else(Some)),
139 )
140 }),
141 )
142 } else {
143 stream.revert(b_state);
144 (
145 b_res.0,
146 b_res.1.map(|(out, alt)| {
147 (
148 out,
149 merge_alts(alt, a_res.1.map(|(_, alt)| alt).unwrap_or_else(Some)),
150 )
151 }),
152 )
153 }
154 }
155
156 choose_between(a_res, a_state, b_res, b_state, stream)
157 }
158
159 #[inline]
160 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
161 #[allow(deprecated)]
162 self.parse_inner(d, s)
163 }
164 #[inline]
165 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
166 #[allow(deprecated)]
167 self.parse_inner(d, s)
168 }
169}
170
171#[must_use]
173#[derive(Copy, Clone)]
174pub struct OrNot<A>(pub(crate) A);
175
176impl<I: Clone, O, A: Parser<I, O, Error = E>, E: Error<I>> Parser<I, Option<O>> for OrNot<A> {
177 type Error = E;
178
179 #[inline]
180 fn parse_inner<D: Debugger>(
181 &self,
182 debugger: &mut D,
183 stream: &mut StreamOf<I, E>,
184 ) -> PResult<I, Option<O>, E> {
185 match stream.try_parse(|stream| {
186 #[allow(deprecated)]
187 debugger.invoke(&self.0, stream)
188 }) {
189 (errors, Ok((out, alt))) => (errors, Ok((Some(out), alt))),
190 (_, Err(err)) => (Vec::new(), Ok((None, Some(err)))),
191 }
192 }
193
194 #[inline]
195 fn parse_inner_verbose(
196 &self,
197 d: &mut Verbose,
198 s: &mut StreamOf<I, E>,
199 ) -> PResult<I, Option<O>, E> {
200 #[allow(deprecated)]
201 self.parse_inner(d, s)
202 }
203 #[inline]
204 fn parse_inner_silent(
205 &self,
206 d: &mut Silent,
207 s: &mut StreamOf<I, E>,
208 ) -> PResult<I, Option<O>, E> {
209 #[allow(deprecated)]
210 self.parse_inner(d, s)
211 }
212}
213
214#[must_use]
216pub struct Not<A, O>(pub(crate) A, pub(crate) PhantomData<O>);
217
218impl<A: Copy, O> Copy for Not<A, O> {}
219impl<A: Clone, O> Clone for Not<A, O> {
220 fn clone(&self) -> Self {
221 Self(self.0.clone(), PhantomData)
222 }
223}
224
225impl<I: Clone, O, A: Parser<I, O, Error = E>, E: Error<I>> Parser<I, I> for Not<A, O> {
226 type Error = E;
227
228 #[inline]
229 fn parse_inner<D: Debugger>(
230 &self,
231 debugger: &mut D,
232 stream: &mut StreamOf<I, E>,
233 ) -> PResult<I, I, E> {
234 let before = stream.save();
235 match stream.try_parse(|stream| {
236 #[allow(deprecated)]
237 debugger.invoke(&self.0, stream)
238 }) {
239 (_, Ok(_)) => {
240 stream.revert(before);
241 let (at, span, found) = stream.next();
242 (
243 Vec::new(),
244 Err(Located::at(
245 at,
246 E::expected_input_found(span, Vec::new(), found),
247 )),
248 )
249 }
250 (_, Err(_)) => {
251 stream.revert(before);
252 let (at, span, found) = stream.next();
253 (
254 Vec::new(),
255 if let Some(found) = found {
256 Ok((found, None))
257 } else {
258 Err(Located::at(
259 at,
260 E::expected_input_found(span, Vec::new(), None),
261 ))
262 },
263 )
264 }
265 }
266 }
267
268 #[inline]
269 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, I, E> {
270 #[allow(deprecated)]
271 self.parse_inner(d, s)
272 }
273 #[inline]
274 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, I, E> {
275 #[allow(deprecated)]
276 self.parse_inner(d, s)
277 }
278}
279
280#[must_use]
282#[derive(Copy, Clone)]
283pub struct Then<A, B>(pub(crate) A, pub(crate) B);
284
285impl<I: Clone, O, U, A: Parser<I, O, Error = E>, B: Parser<I, U, Error = E>, E: Error<I>>
286 Parser<I, (O, U)> for Then<A, B>
287{
288 type Error = E;
289
290 #[inline]
291 fn parse_inner<D: Debugger>(
292 &self,
293 debugger: &mut D,
294 stream: &mut StreamOf<I, E>,
295 ) -> PResult<I, (O, U), E> {
296 match {
297 #[allow(deprecated)]
298 debugger.invoke(&self.0, stream)
299 } {
300 (mut a_errors, Ok((a_out, a_alt))) => match {
301 #[allow(deprecated)]
302 debugger.invoke(&self.1, stream)
303 } {
304 (mut b_errors, Ok((b_out, b_alt))) => {
305 a_errors.append(&mut b_errors);
306 (a_errors, Ok(((a_out, b_out), merge_alts(a_alt, b_alt))))
307 }
308 (mut b_errors, Err(b_err)) => {
309 a_errors.append(&mut b_errors);
310 (a_errors, Err(b_err.max(a_alt)))
311 }
312 },
313 (a_errors, Err(a_err)) => (a_errors, Err(a_err)),
314 }
315 }
316
317 #[inline]
318 fn parse_inner_verbose(
319 &self,
320 d: &mut Verbose,
321 s: &mut StreamOf<I, E>,
322 ) -> PResult<I, (O, U), E> {
323 #[allow(deprecated)]
324 self.parse_inner(d, s)
325 }
326 #[inline]
327 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, (O, U), E> {
328 #[allow(deprecated)]
329 self.parse_inner(d, s)
330 }
331}
332
333#[must_use]
335pub struct ThenWith<I, O1, O2, A, B, F>(
336 pub(crate) A,
337 pub(crate) F,
338 pub(crate) PhantomData<(I, O1, O2, B)>,
339);
340
341impl<I, O1, O2, A: Clone, B, F: Clone> Clone for ThenWith<I, O1, O2, A, B, F> {
342 fn clone(&self) -> Self {
343 ThenWith(self.0.clone(), self.1.clone(), PhantomData)
344 }
345}
346
347impl<I, O1, O2, A: Copy, B, F: Copy> Copy for ThenWith<I, O1, O2, A, B, F> {}
348
349impl<
350 I: Clone,
351 O1,
352 O2,
353 A: Parser<I, O1, Error = E>,
354 B: Parser<I, O2, Error = E>,
355 F: Fn(O1) -> B,
356 E: Error<I>,
357 > Parser<I, O2> for ThenWith<I, O1, O2, A, B, F>
358{
359 type Error = E;
360
361 #[inline]
362 fn parse_inner<D: Debugger>(
363 &self,
364 debugger: &mut D,
365 stream: &mut StreamOf<I, E>,
366 ) -> PResult<I, O2, E> {
367 let state = stream.save();
368
369 match {
370 #[allow(deprecated)]
371 debugger.invoke(&self.0, stream)
372 } {
373 (mut first_errs, Ok((first_out, first_alt))) => {
374 let second_out = self.1(first_out);
375 match {
376 #[allow(deprecated)]
377 debugger.invoke(&second_out, stream)
378 } {
379 (second_errs, Ok((second_out, second_alt))) => {
380 first_errs.extend(second_errs);
381 (first_errs, Ok((second_out, first_alt.or(second_alt))))
382 }
383 (second_errs, Err(e)) => {
384 stream.revert(state);
385 first_errs.extend(second_errs);
386 (first_errs, Err(e))
387 }
388 }
389 }
390 (errs, Err(e)) => {
391 stream.revert(state);
392 (errs, Err(e))
393 }
394 }
395 }
396
397 #[inline]
398 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O2, E> {
399 #[allow(deprecated)]
400 self.parse_inner(d, s)
401 }
402 #[inline]
403 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O2, E> {
404 #[allow(deprecated)]
405 self.parse_inner(d, s)
406 }
407}
408
409#[must_use]
411#[derive(Copy, Clone)]
412pub struct DelimitedBy<A, L, R, U, V> {
413 pub(crate) item: A,
414 pub(crate) start: L,
415 pub(crate) end: R,
416 pub(crate) phantom: PhantomData<(U, V)>,
417}
418
419impl<
420 I: Clone,
421 O,
422 A: Parser<I, O, Error = E>,
423 L: Parser<I, U, Error = E> + Clone,
424 R: Parser<I, V, Error = E> + Clone,
425 U,
426 V,
427 E: Error<I>,
428 > Parser<I, O> for DelimitedBy<A, L, R, U, V>
429{
430 type Error = E;
431
432 #[inline]
433 fn parse_inner<D: Debugger>(
434 &self,
435 debugger: &mut D,
436 stream: &mut StreamOf<I, E>,
437 ) -> PResult<I, O, E> {
438 #[allow(deprecated)]
440 let (errors, res) = debugger.invoke(
441 &self
442 .start
443 .clone()
444 .ignore_then(&self.item)
445 .then_ignore(self.end.clone()),
446 stream,
447 );
448 (errors, res)
449 }
450
451 #[inline]
452 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
453 #[allow(deprecated)]
454 self.parse_inner(d, s)
455 }
456 #[inline]
457 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
458 #[allow(deprecated)]
459 self.parse_inner(d, s)
460 }
461}
462
463#[must_use]
465#[derive(Copy, Clone)]
466pub struct Repeated<A>(pub(crate) A, pub(crate) usize, pub(crate) Option<usize>);
467
468impl<A> Repeated<A> {
469 pub fn at_least(mut self, min: usize) -> Self {
471 self.1 = min;
472 self
473 }
474
475 pub fn at_most(mut self, max: usize) -> Self {
477 self.2 = Some(max);
478 self
479 }
480
481 pub fn exactly(mut self, n: usize) -> Self {
518 self.1 = n;
519 self.2 = Some(n);
520 self
521 }
522}
523
524impl<I: Clone, O, A: Parser<I, O, Error = E>, E: Error<I>> Parser<I, Vec<O>> for Repeated<A> {
525 type Error = E;
526
527 #[inline]
528 fn parse_inner<D: Debugger>(
529 &self,
530 debugger: &mut D,
531 stream: &mut StreamOf<I, E>,
532 ) -> PResult<I, Vec<O>, E> {
533 let mut errors = Vec::new();
534 let mut outputs = Vec::new();
535 let mut alt = None;
536 let mut old_offset = None;
537
538 loop {
539 if self.2.map_or(false, |max| outputs.len() >= max) {
540 break (errors, Ok((outputs, alt)));
541 }
542
543 if let ControlFlow::Break(b) = stream.attempt(|stream| match { #[allow(deprecated)] debugger.invoke(&self.0, stream) } {
544 (mut a_errors, Ok((a_out, a_alt))) => {
545 errors.append(&mut a_errors);
546 alt = merge_alts(alt.take(), a_alt);
547 outputs.push(a_out);
548
549 if old_offset == Some(stream.offset()) {
550 panic!("Repeated parser iteration succeeded but consumed no inputs (i.e: continuing \
551 iteration would likely lead to an infinite loop, if the parser is pure). This is \
552 likely indicative of a parser bug. Consider using a more specific error recovery \
553 strategy.");
554 } else {
555 old_offset = Some(stream.offset());
556 }
557
558 (true, ControlFlow::Continue(()))
559 },
560 (mut a_errors, Err(a_err)) if outputs.len() < self.1 => {
561 errors.append(&mut a_errors);
562 (true, ControlFlow::Break((
563 core::mem::take(&mut errors),
564 Err(a_err),
565 )))
566 },
567 (a_errors, Err(a_err)) => {
568 let alt = merge_alts(
572 alt.take(),
573 merge_alts(
574 Some(a_err),
575 a_errors.into_iter().next(),
576 ),
577 );
578 (false, ControlFlow::Break((
579 core::mem::take(&mut errors),
580 Ok((core::mem::take(&mut outputs), alt)),
581 )))
582 },
583 }) {
584 break b;
585 }
586 }
587 }
588
589 #[inline]
590 fn parse_inner_verbose(
591 &self,
592 d: &mut Verbose,
593 s: &mut StreamOf<I, E>,
594 ) -> PResult<I, Vec<O>, E> {
595 #[allow(deprecated)]
596 self.parse_inner(d, s)
597 }
598 #[inline]
599 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, Vec<O>, E> {
600 #[allow(deprecated)]
601 self.parse_inner(d, s)
602 }
603}
604
605#[must_use]
607pub struct SeparatedBy<A, B, U> {
608 pub(crate) item: A,
609 pub(crate) delimiter: B,
610 pub(crate) at_least: usize,
611 pub(crate) at_most: Option<usize>,
612 pub(crate) allow_leading: bool,
613 pub(crate) allow_trailing: bool,
614 pub(crate) phantom: PhantomData<U>,
615}
616
617impl<A, B, U> SeparatedBy<A, B, U> {
618 pub fn allow_leading(mut self) -> Self {
641 self.allow_leading = true;
642 self
643 }
644
645 pub fn allow_trailing(mut self) -> Self {
663 self.allow_trailing = true;
664 self
665 }
666
667 pub fn at_least(mut self, n: usize) -> Self {
680 self.at_least = n;
681 self
682 }
683
684 pub fn at_most(mut self, n: usize) -> Self {
708 self.at_most = Some(n);
709 self
710 }
711
712 pub fn exactly(mut self, n: usize) -> Self {
730 self.at_least = n;
731 self.at_most = Some(n);
732 self
733 }
734}
735
736impl<A: Copy, B: Copy, U> Copy for SeparatedBy<A, B, U> {}
737impl<A: Clone, B: Clone, U> Clone for SeparatedBy<A, B, U> {
738 fn clone(&self) -> Self {
739 Self {
740 item: self.item.clone(),
741 delimiter: self.delimiter.clone(),
742 at_least: self.at_least,
743 at_most: self.at_most,
744 allow_leading: self.allow_leading,
745 allow_trailing: self.allow_trailing,
746 phantom: PhantomData,
747 }
748 }
749}
750
751impl<I: Clone, O, U, A: Parser<I, O, Error = E>, B: Parser<I, U, Error = E>, E: Error<I>>
752 Parser<I, Vec<O>> for SeparatedBy<A, B, U>
753{
754 type Error = E;
755
756 #[inline]
757 fn parse_inner<D: Debugger>(
758 &self,
759 debugger: &mut D,
760 stream: &mut StreamOf<I, E>,
761 ) -> PResult<I, Vec<O>, E> {
762 if let Some(at_most) = self.at_most {
763 assert!(
764 self.at_least <= at_most,
765 "SeparatedBy cannot parse at least {} and at most {}",
766 self.at_least,
767 at_most
768 );
769 }
770
771 enum State<I, E> {
772 Terminated(Located<I, E>),
773 Continue,
774 }
775
776 fn parse_or_not<U, B: Parser<I, U, Error = E>, I: Clone, E: Error<I>, D: Debugger>(
777 delimiter: &B,
778 stream: &mut StreamOf<I, E>,
779 debugger: &mut D,
780 alt: Option<Located<I, E>>,
781 ) -> Option<Located<I, E>> {
782 match stream.try_parse(|stream| {
783 #[allow(deprecated)]
784 debugger.invoke(&delimiter, stream)
785 }) {
786 (d_errors, Ok((_, d_alt))) => merge_alts(alt, merge_alts(d_alt, d_errors)),
788 (d_errors, Err(d_err)) => merge_alts(alt, merge_alts(Some(d_err), d_errors)),
789 }
790 }
791
792 fn parse<O, A: Parser<I, O, Error = E>, I: Clone, E: Error<I>, D: Debugger>(
793 item: &A,
794 stream: &mut StreamOf<I, E>,
795 debugger: &mut D,
796 outputs: &mut Vec<O>,
797 errors: &mut Vec<Located<I, E>>,
798 alt: Option<Located<I, E>>,
799 ) -> (State<I, E>, Option<Located<I, E>>) {
800 match stream.try_parse(|stream| {
801 #[allow(deprecated)]
802 debugger.invoke(item, stream)
803 }) {
804 (mut i_errors, Ok((i_out, i_alt))) => {
805 outputs.push(i_out);
806 errors.append(&mut i_errors);
807 (State::Continue, merge_alts(alt, i_alt))
808 }
809 (mut i_errors, Err(i_err)) => {
810 errors.append(&mut i_errors);
811 (State::Terminated(i_err), alt)
812 }
813 }
814 }
815
816 let mut outputs = Vec::new();
817 let mut errors = Vec::new();
818 let mut alt = None;
819
820 if self.allow_leading {
821 alt = parse_or_not(&self.delimiter, stream, debugger, alt);
822 }
823
824 let (mut state, mut alt) =
825 parse(&self.item, stream, debugger, &mut outputs, &mut errors, alt);
826
827 let mut offset = stream.save();
828 let error: Option<Located<I, E>>;
829 loop {
830 if let State::Terminated(err) = state {
831 error = Some(err);
832 break;
833 }
834 offset = stream.save();
835
836 if self
837 .at_most
838 .map_or(false, |at_most| outputs.len() >= at_most)
839 {
840 error = None;
841 break;
842 }
843
844 match stream.try_parse(|stream| {
845 #[allow(deprecated)]
846 debugger.invoke(&self.delimiter, stream)
847 }) {
848 (mut d_errors, Ok((_, d_alt))) => {
849 errors.append(&mut d_errors);
850 alt = merge_alts(alt, d_alt);
851
852 let (i_state, i_alt) =
853 parse(&self.item, stream, debugger, &mut outputs, &mut errors, alt);
854 state = i_state;
855 alt = i_alt;
856 }
857 (mut d_errors, Err(d_err)) => {
858 errors.append(&mut d_errors);
859 state = State::Terminated(d_err);
860 }
861 }
862 }
863 stream.revert(offset);
864
865 if self.allow_trailing && !outputs.is_empty() {
866 alt = parse_or_not(&self.delimiter, stream, debugger, alt);
867 }
868
869 if outputs.len() >= self.at_least {
870 alt = merge_alts(alt, error);
871 (errors, Ok((outputs, alt)))
872 } else if let Some(error) = error {
873 (errors, Err(error))
875 } else {
876 (errors, Ok((outputs, alt)))
877 }
878 }
879
880 #[inline]
881 fn parse_inner_verbose(
882 &self,
883 d: &mut Verbose,
884 s: &mut StreamOf<I, E>,
885 ) -> PResult<I, Vec<O>, E> {
886 #[allow(deprecated)]
887 self.parse_inner(d, s)
888 }
889 #[inline]
890 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, Vec<O>, E> {
891 #[allow(deprecated)]
892 self.parse_inner(d, s)
893 }
894}
895
896#[must_use]
898pub struct Debug<A>(
899 pub(crate) A,
900 pub(crate) Rc<dyn fmt::Display>,
901 pub(crate) core::panic::Location<'static>,
902);
903
904impl<A: Clone> Clone for Debug<A> {
905 fn clone(&self) -> Self {
906 Self(self.0.clone(), self.1.clone(), self.2)
907 }
908}
909
910impl<I: Clone, O, A: Parser<I, O, Error = E>, E: Error<I>> Parser<I, O> for Debug<A> {
911 type Error = E;
912
913 #[inline]
914 fn parse_inner<D: Debugger>(
915 &self,
916 debugger: &mut D,
917 stream: &mut StreamOf<I, E>,
918 ) -> PResult<I, O, E> {
919 debugger.scope(
920 || ParserInfo::new("Name", self.1.clone(), self.2),
921 |debugger| {
922 #[allow(deprecated)]
923 let (errors, res) = debugger.invoke(&self.0, stream);
924
925 (errors, res)
926 },
927 )
928 }
929
930 #[inline]
931 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
932 #[allow(deprecated)]
933 self.parse_inner(d, s)
934 }
935 #[inline]
936 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
937 #[allow(deprecated)]
938 self.parse_inner(d, s)
939 }
940}
941
942#[must_use]
944pub struct Map<A, F, O>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<O>);
945
946impl<A: Copy, F: Copy, O> Copy for Map<A, F, O> {}
947impl<A: Clone, F: Clone, O> Clone for Map<A, F, O> {
948 fn clone(&self) -> Self {
949 Self(self.0.clone(), self.1.clone(), PhantomData)
950 }
951}
952
953impl<I: Clone, O, A: Parser<I, O, Error = E>, U, F: Fn(O) -> U, E: Error<I>> Parser<I, U>
954 for Map<A, F, O>
955{
956 type Error = E;
957
958 #[inline]
959 fn parse_inner<D: Debugger>(
960 &self,
961 debugger: &mut D,
962 stream: &mut StreamOf<I, E>,
963 ) -> PResult<I, U, E> {
964 #[allow(deprecated)]
965 let (errors, res) = debugger.invoke(&self.0, stream);
966
967 (errors, res.map(|(out, alt)| ((&self.1)(out), alt)))
968 }
969
970 #[inline]
971 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
972 #[allow(deprecated)]
973 self.parse_inner(d, s)
974 }
975 #[inline]
976 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
977 #[allow(deprecated)]
978 self.parse_inner(d, s)
979 }
980}
981
982#[must_use]
984pub struct MapWithSpan<A, F, O>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<O>);
985
986impl<A: Copy, F: Copy, O> Copy for MapWithSpan<A, F, O> {}
987impl<A: Clone, F: Clone, O> Clone for MapWithSpan<A, F, O> {
988 fn clone(&self) -> Self {
989 Self(self.0.clone(), self.1.clone(), PhantomData)
990 }
991}
992
993impl<I: Clone, O, A: Parser<I, O, Error = E>, U, F: Fn(O, E::Span) -> U, E: Error<I>> Parser<I, U>
994 for MapWithSpan<A, F, O>
995{
996 type Error = E;
997
998 #[inline]
999 fn parse_inner<D: Debugger>(
1000 &self,
1001 debugger: &mut D,
1002 stream: &mut StreamOf<I, E>,
1003 ) -> PResult<I, U, E> {
1004 let start = stream.save();
1005 #[allow(deprecated)]
1006 let (errors, res) = debugger.invoke(&self.0, stream);
1007
1008 (
1009 errors,
1010 res.map(|(out, alt)| ((self.1)(out, stream.span_since(start)), alt)),
1011 )
1012 }
1013
1014 #[inline]
1015 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1016 #[allow(deprecated)]
1017 self.parse_inner(d, s)
1018 }
1019 #[inline]
1020 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1021 #[allow(deprecated)]
1022 self.parse_inner(d, s)
1023 }
1024}
1025
1026#[must_use]
1028pub struct Validate<A, U, F>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<U>);
1029
1030impl<A: Copy, U, F: Copy> Copy for Validate<A, U, F> {}
1031impl<A: Clone, U, F: Clone> Clone for Validate<A, U, F> {
1032 fn clone(&self) -> Self {
1033 Self(self.0.clone(), self.1.clone(), PhantomData)
1034 }
1035}
1036
1037impl<
1038 I: Clone,
1039 O,
1040 U,
1041 A: Parser<I, O, Error = E>,
1042 F: Fn(O, E::Span, &mut dyn FnMut(E)) -> U,
1043 E: Error<I>,
1044 > Parser<I, U> for Validate<A, O, F>
1045{
1046 type Error = E;
1047
1048 #[inline]
1049 fn parse_inner<D: Debugger>(
1050 &self,
1051 debugger: &mut D,
1052 stream: &mut StreamOf<I, E>,
1053 ) -> PResult<I, U, E> {
1054 let start = stream.save();
1055 #[allow(deprecated)]
1056 let (mut errors, res) = debugger.invoke(&self.0, stream);
1057
1058 let pos = stream.save();
1059 let span = stream.span_since(start);
1060
1061 let res = res.map(|(out, alt)| {
1062 (
1063 (&self.1)(out, span, &mut |e| errors.push(Located::at(pos, e))),
1064 alt,
1065 )
1066 });
1067
1068 (errors, res)
1069 }
1070
1071 #[inline]
1072 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1073 #[allow(deprecated)]
1074 self.parse_inner(d, s)
1075 }
1076 #[inline]
1077 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1078 #[allow(deprecated)]
1079 self.parse_inner(d, s)
1080 }
1081}
1082
1083#[must_use]
1085pub struct Foldl<A, F, O, U>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<(O, U)>);
1086
1087impl<A: Copy, F: Copy, O, U> Copy for Foldl<A, F, O, U> {}
1088impl<A: Clone, F: Clone, O, U> Clone for Foldl<A, F, O, U> {
1089 fn clone(&self) -> Self {
1090 Self(self.0.clone(), self.1.clone(), PhantomData)
1091 }
1092}
1093
1094impl<
1095 I: Clone,
1096 O,
1097 A: Parser<I, (O, U), Error = E>,
1098 U: IntoIterator,
1099 F: Fn(O, U::Item) -> O,
1100 E: Error<I>,
1101 > Parser<I, O> for Foldl<A, F, O, U>
1102{
1103 type Error = E;
1104
1105 #[inline]
1106 fn parse_inner<D: Debugger>(
1107 &self,
1108 debugger: &mut D,
1109 stream: &mut StreamOf<I, E>,
1110 ) -> PResult<I, O, E> {
1111 #[allow(deprecated)]
1112 debugger.invoke(
1113 &(&self.0).map(|(head, tail)| tail.into_iter().fold(head, &self.1)),
1114 stream,
1115 )
1116 }
1117
1118 #[inline]
1119 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1120 #[allow(deprecated)]
1121 self.parse_inner(d, s)
1122 }
1123 #[inline]
1124 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1125 #[allow(deprecated)]
1126 self.parse_inner(d, s)
1127 }
1128}
1129
1130#[must_use]
1132pub struct Foldr<A, F, O, U>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<(O, U)>);
1133
1134impl<A: Copy, F: Copy, O, U> Copy for Foldr<A, F, O, U> {}
1135impl<A: Clone, F: Clone, O, U> Clone for Foldr<A, F, O, U> {
1136 fn clone(&self) -> Self {
1137 Self(self.0.clone(), self.1.clone(), PhantomData)
1138 }
1139}
1140
1141impl<
1142 I: Clone,
1143 O: IntoIterator,
1144 A: Parser<I, (O, U), Error = E>,
1145 U,
1146 F: Fn(O::Item, U) -> U,
1147 E: Error<I>,
1148 > Parser<I, U> for Foldr<A, F, O, U>
1149where
1150 O::IntoIter: DoubleEndedIterator,
1151{
1152 type Error = E;
1153
1154 #[inline]
1155 fn parse_inner<D: Debugger>(
1156 &self,
1157 debugger: &mut D,
1158 stream: &mut StreamOf<I, E>,
1159 ) -> PResult<I, U, E> {
1160 #[allow(deprecated)]
1161 debugger.invoke(
1162 &(&self.0).map(|(init, end)| init.into_iter().rev().fold(end, |b, a| (&self.1)(a, b))),
1163 stream,
1164 )
1165 }
1166
1167 #[inline]
1168 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1169 #[allow(deprecated)]
1170 self.parse_inner(d, s)
1171 }
1172 #[inline]
1173 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1174 #[allow(deprecated)]
1175 self.parse_inner(d, s)
1176 }
1177}
1178
1179#[must_use]
1181#[derive(Copy, Clone)]
1182pub struct MapErr<A, F>(pub(crate) A, pub(crate) F);
1183
1184impl<I: Clone, O, A: Parser<I, O, Error = E>, F: Fn(E) -> E, E: Error<I>> Parser<I, O>
1185 for MapErr<A, F>
1186{
1187 type Error = E;
1188
1189 #[inline]
1190 fn parse_inner<D: Debugger>(
1191 &self,
1192 debugger: &mut D,
1193 stream: &mut StreamOf<I, E>,
1194 ) -> PResult<I, O, E> {
1195 #[allow(deprecated)]
1196 let (errors, res) = debugger.invoke(&self.0, stream);
1197 let mapper = |e: Located<I, E>| e.map(&self.1);
1198 (
1199 errors, res .map_err(mapper),
1202 )
1203 }
1204
1205 #[inline]
1206 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1207 #[allow(deprecated)]
1208 self.parse_inner(d, s)
1209 }
1210 #[inline]
1211 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1212 #[allow(deprecated)]
1213 self.parse_inner(d, s)
1214 }
1215}
1216
1217#[must_use]
1219#[derive(Copy, Clone)]
1220pub struct MapErrWithSpan<A, F>(pub(crate) A, pub(crate) F);
1221
1222impl<I: Clone, O, A: Parser<I, O, Error = E>, F: Fn(E, E::Span) -> E, E: Error<I>> Parser<I, O>
1223 for MapErrWithSpan<A, F>
1224{
1225 type Error = E;
1226
1227 #[inline]
1228 fn parse_inner<D: Debugger>(
1229 &self,
1230 debugger: &mut D,
1231 stream: &mut StreamOf<I, E>,
1232 ) -> PResult<I, O, E> {
1233 let start = stream.save();
1234 #[allow(deprecated)]
1235 let (errors, res) = debugger.invoke(&self.0, stream);
1236 let mapper = |e: Located<I, E>| {
1237 let at = e.at;
1238 e.map(|e| {
1239 let span = stream.attempt(|stream| {
1240 stream.revert(at);
1241 (false, stream.span_since(start))
1242 });
1243 (self.1)(e, span)
1244 })
1245 };
1246 (errors, res.map_err(mapper))
1247 }
1248
1249 #[inline]
1250 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1251 #[allow(deprecated)]
1252 self.parse_inner(d, s)
1253 }
1254 #[inline]
1255 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1256 #[allow(deprecated)]
1257 self.parse_inner(d, s)
1258 }
1259}
1260
1261#[must_use]
1263pub struct TryMap<A, F, O>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<O>);
1264
1265impl<A: Copy, F: Copy, O> Copy for TryMap<A, F, O> {}
1266impl<A: Clone, F: Clone, O> Clone for TryMap<A, F, O> {
1267 fn clone(&self) -> Self {
1268 Self(self.0.clone(), self.1.clone(), PhantomData)
1269 }
1270}
1271
1272impl<
1273 I: Clone,
1274 O,
1275 A: Parser<I, O, Error = E>,
1276 U,
1277 F: Fn(O, E::Span) -> Result<U, E>,
1278 E: Error<I>,
1279 > Parser<I, U> for TryMap<A, F, O>
1280{
1281 type Error = E;
1282
1283 #[inline]
1284 fn parse_inner<D: Debugger>(
1285 &self,
1286 debugger: &mut D,
1287 stream: &mut StreamOf<I, E>,
1288 ) -> PResult<I, U, E> {
1289 let start = stream.save();
1290 #[allow(deprecated)]
1291 let (errors, res) = debugger.invoke(&self.0, stream);
1292
1293 let res = match res.map(|(out, alt)| ((&self.1)(out, stream.span_since(start)), alt)) {
1294 Ok((Ok(out), alt)) => Ok((out, alt)),
1295 Ok((Err(a_err), _)) => Err(Located::at(stream.save(), a_err)),
1296 Err(err) => Err(err),
1297 };
1298
1299 (errors, res)
1300 }
1301
1302 #[inline]
1303 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1304 #[allow(deprecated)]
1305 self.parse_inner(d, s)
1306 }
1307 #[inline]
1308 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1309 #[allow(deprecated)]
1310 self.parse_inner(d, s)
1311 }
1312}
1313
1314#[must_use]
1316#[derive(Copy, Clone)]
1317pub struct OrElse<A, F>(pub(crate) A, pub(crate) F);
1318
1319impl<I: Clone, O, A: Parser<I, O, Error = E>, F: Fn(E) -> Result<O, E>, E: Error<I>> Parser<I, O>
1320 for OrElse<A, F>
1321{
1322 type Error = E;
1323
1324 #[inline]
1325 fn parse_inner<D: Debugger>(
1326 &self,
1327 debugger: &mut D,
1328 stream: &mut StreamOf<I, E>,
1329 ) -> PResult<I, O, E> {
1330 let start = stream.save();
1331
1332 #[allow(deprecated)]
1333 let (errors, res) = debugger.invoke(&self.0, stream);
1334
1335 let res = match res {
1336 Ok(out) => Ok(out),
1337 Err(err) => match (&self.1)(err.error) {
1338 Err(e) => Err(Located {
1339 at: err.at,
1340 error: e,
1341 phantom: PhantomData,
1342 }),
1343 Ok(out) => {
1344 stream.revert(start);
1345 Ok((out, None))
1346 },
1347 },
1348 };
1349
1350 (errors, res)
1351 }
1352
1353 #[inline]
1354 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1355 #[allow(deprecated)]
1356 self.parse_inner(d, s)
1357 }
1358 #[inline]
1359 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1360 #[allow(deprecated)]
1361 self.parse_inner(d, s)
1362 }
1363}
1364
1365#[must_use]
1367#[derive(Copy, Clone)]
1368pub struct Label<A, L>(pub(crate) A, pub(crate) L);
1369
1370impl<I: Clone, O, A: Parser<I, O, Error = E>, L: Into<E::Label> + Clone, E: Error<I>> Parser<I, O>
1371 for Label<A, L>
1372{
1373 type Error = E;
1374
1375 #[inline]
1376 fn parse_inner<D: Debugger>(
1377 &self,
1378 debugger: &mut D,
1379 stream: &mut StreamOf<I, E>,
1380 ) -> PResult<I, O, E> {
1381 #[allow(deprecated)]
1383 let (errors, res) = debugger.invoke(&self.0, stream);
1384 let res = res
1385 .map(|(o, alt)| {
1386 (
1387 o,
1388 alt.map(|l| l.map(|e| e.with_label(self.1.clone().into()))),
1389 )
1390 })
1391 .map_err(|e| {
1392 e.map(|e| e.with_label(self.1.clone().into()))
1397 });
1401 (
1402 errors
1403 .into_iter()
1404 .map(|e| e.map(|e| e.with_label(self.1.clone().into())))
1405 .collect(),
1406 res,
1407 )
1408 }
1409
1410 #[inline]
1411 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1412 #[allow(deprecated)]
1413 self.parse_inner(d, s)
1414 }
1415 #[inline]
1416 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1417 #[allow(deprecated)]
1418 self.parse_inner(d, s)
1419 }
1420}
1421
1422#[must_use]
1424pub struct To<A, O, U>(pub(crate) A, pub(crate) U, pub(crate) PhantomData<O>);
1425
1426impl<A: Copy, U: Copy, O> Copy for To<A, O, U> {}
1427impl<A: Clone, U: Clone, O> Clone for To<A, O, U> {
1428 fn clone(&self) -> Self {
1429 Self(self.0.clone(), self.1.clone(), PhantomData)
1430 }
1431}
1432
1433impl<I: Clone, O, A: Parser<I, O, Error = E>, U: Clone, E: Error<I>> Parser<I, U> for To<A, O, U> {
1434 type Error = E;
1435
1436 #[inline]
1437 fn parse_inner<D: Debugger>(
1438 &self,
1439 debugger: &mut D,
1440 stream: &mut StreamOf<I, E>,
1441 ) -> PResult<I, U, E> {
1442 #[allow(deprecated)]
1443 debugger.invoke(&(&self.0).map(|_| self.1.clone()), stream)
1444 }
1445
1446 #[inline]
1447 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1448 #[allow(deprecated)]
1449 self.parse_inner(d, s)
1450 }
1451 #[inline]
1452 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, U, E> {
1453 #[allow(deprecated)]
1454 self.parse_inner(d, s)
1455 }
1456}
1457
1458#[must_use]
1460#[derive(Copy, Clone)]
1461pub struct Rewind<A>(pub(crate) A);
1462
1463impl<I: Clone, O, E: Error<I>, A> Parser<I, O> for Rewind<A>
1464where
1465 A: Parser<I, O, Error = E>,
1466{
1467 type Error = E;
1468
1469 fn parse_inner<D: Debugger>(
1470 &self,
1471 debugger: &mut D,
1472 stream: &mut StreamOf<I, Self::Error>,
1473 ) -> PResult<I, O, Self::Error>
1474 where
1475 Self: Sized,
1476 {
1477 let rewind_from = stream.save();
1478 match {
1479 #[allow(deprecated)]
1480 debugger.invoke(&self.0, stream)
1481 } {
1482 (errors, Ok((out, alt))) => {
1483 stream.revert(rewind_from);
1484 (errors, Ok((out, alt)))
1485 }
1486 (errors, Err(err)) => (errors, Err(err)),
1487 }
1488 }
1489
1490 fn parse_inner_verbose(
1491 &self,
1492 d: &mut Verbose,
1493 s: &mut StreamOf<I, Self::Error>,
1494 ) -> PResult<I, O, Self::Error> {
1495 #[allow(deprecated)]
1496 self.parse_inner(d, s)
1497 }
1498
1499 fn parse_inner_silent(
1500 &self,
1501 d: &mut Silent,
1502 s: &mut StreamOf<I, Self::Error>,
1503 ) -> PResult<I, O, Self::Error> {
1504 #[allow(deprecated)]
1505 self.parse_inner(d, s)
1506 }
1507}
1508
1509#[must_use]
1511pub struct Unwrapped<A, U, E>(
1512 pub(crate) &'static Location<'static>,
1513 pub(crate) A,
1514 pub(crate) PhantomData<(U, E)>,
1515);
1516
1517impl<A: Clone, U, E> Clone for Unwrapped<A, U, E> {
1518 fn clone(&self) -> Self {
1519 Unwrapped(self.0, self.1.clone(), PhantomData)
1520 }
1521}
1522impl<A: Copy, U, E> Copy for Unwrapped<A, U, E> {}
1523
1524impl<I: Clone, O, A: Parser<I, Result<O, U>, Error = E>, U: fmt::Debug, E: Error<I>> Parser<I, O>
1525 for Unwrapped<A, U, E>
1526{
1527 type Error = E;
1528
1529 #[inline]
1530 fn parse_inner<D: Debugger>(
1531 &self,
1532 debugger: &mut D,
1533 stream: &mut StreamOf<I, E>,
1534 ) -> PResult<I, O, E> {
1535 #[allow(deprecated)]
1536 let (errors, res) = debugger.invoke(&self.1, stream);
1537
1538 (
1539 errors,
1540 res.map(|(out, alt)| {
1541 (
1542 out.unwrap_or_else(|err| {
1543 panic!(
1544 "Parser defined at {} failed to unwrap. Error: {:?}",
1545 self.0, err
1546 )
1547 }),
1548 alt,
1549 )
1550 }),
1551 )
1552 }
1553
1554 #[inline]
1555 fn parse_inner_verbose(&self, d: &mut Verbose, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1556 #[allow(deprecated)]
1557 self.parse_inner(d, s)
1558 }
1559 #[inline]
1560 fn parse_inner_silent(&self, d: &mut Silent, s: &mut StreamOf<I, E>) -> PResult<I, O, E> {
1561 #[allow(deprecated)]
1562 self.parse_inner(d, s)
1563 }
1564}
1565
1566#[cfg(test)]
1567mod tests {
1568 use alloc::vec;
1569 use error::Simple;
1570 use text::TextParser;
1571
1572 use super::*;
1573
1574 #[test]
1575 fn delimited_by_complex() {
1576 let parser = just::<_, _, Simple<char>>('-')
1577 .delimited_by(text::ident().padded(), text::int(10).padded())
1578 .separated_by(just(','));
1579
1580 assert_eq!(
1581 parser.parse("one - 1,two - 2,three - 3"),
1582 Ok(vec!['-', '-', '-'])
1583 );
1584 }
1585
1586 #[test]
1587 fn separated_by_at_least() {
1588 let parser = just::<_, _, Simple<char>>('-')
1589 .separated_by(just(','))
1590 .at_least(3);
1591
1592 assert_eq!(parser.parse("-,-,-"), Ok(vec!['-', '-', '-']));
1593 }
1594
1595 #[test]
1596 fn separated_by_at_least_without_leading() {
1597 let parser = just::<_, _, Simple<char>>('-')
1598 .separated_by(just(','))
1599 .at_least(3);
1600
1601 assert!(parser.parse(",-,-,-").is_err());
1602 }
1603
1604 #[test]
1605 fn separated_by_at_least_without_trailing() {
1606 let parser = just::<_, _, Simple<char>>('-')
1607 .separated_by(just(','))
1608 .at_least(3)
1609 .then(end());
1610
1611 assert!(parser.parse("-,-,-,").is_err());
1612 }
1613
1614 #[test]
1615 fn separated_by_at_least_with_leading() {
1616 let parser = just::<_, _, Simple<char>>('-')
1617 .separated_by(just(','))
1618 .allow_leading()
1619 .at_least(3);
1620
1621 assert_eq!(parser.parse(",-,-,-"), Ok(vec!['-', '-', '-']));
1622 assert!(parser.parse(",-,-").is_err());
1623 }
1624
1625 #[test]
1626 fn separated_by_at_least_with_trailing() {
1627 let parser = just::<_, _, Simple<char>>('-')
1628 .separated_by(just(','))
1629 .allow_trailing()
1630 .at_least(3);
1631
1632 assert_eq!(parser.parse("-,-,-,"), Ok(vec!['-', '-', '-']));
1633 assert!(parser.parse("-,-,").is_err());
1634 }
1635
1636 #[test]
1637 fn separated_by_leaves_last_separator() {
1638 let parser = just::<_, _, Simple<char>>('-')
1639 .separated_by(just(','))
1640 .chain(just(','));
1641 assert_eq!(parser.parse("-,-,-,"), Ok(vec!['-', '-', '-', ',']))
1642 }
1643}