1use crate::combinator::trace;
2use crate::combinator::trace_result;
3use crate::combinator::DisplayDebug;
4#[cfg(feature = "unstable-recover")]
5use crate::error::FromRecoverableError;
6use crate::error::{AddContext, ErrMode, ErrorKind, FromExternalError, ParserError};
7use crate::lib::std::borrow::Borrow;
8use crate::lib::std::ops::Range;
9#[cfg(feature = "unstable-recover")]
10use crate::stream::Recover;
11use crate::stream::StreamIsPartial;
12use crate::stream::{Location, Stream};
13use crate::*;
14
15/// Implementation of [`Parser::by_ref`]
16#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
17pub struct ByRef<'p, P> {
18 p: &'p mut P,
19}
20
21impl<'p, P> ByRef<'p, P> {
22 #[inline(always)]
23 pub(crate) fn new(p: &'p mut P) -> Self {
24 Self { p }
25 }
26}
27
28impl<'p, I, O, E, P> Parser<I, O, E> for ByRef<'p, P>
29where
30 P: Parser<I, O, E>,
31{
32 #[inline(always)]
33 fn parse_next(&mut self, i: &mut I) -> PResult<O, E> {
34 self.p.parse_next(input:i)
35 }
36}
37
38/// Implementation of [`Parser::map`]
39#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
40pub struct Map<F, G, I, O, O2, E>
41where
42 F: Parser<I, O, E>,
43 G: FnMut(O) -> O2,
44{
45 parser: F,
46 map: G,
47 i: core::marker::PhantomData<I>,
48 o: core::marker::PhantomData<O>,
49 o2: core::marker::PhantomData<O2>,
50 e: core::marker::PhantomData<E>,
51}
52
53impl<F, G, I, O, O2, E> Map<F, G, I, O, O2, E>
54where
55 F: Parser<I, O, E>,
56 G: FnMut(O) -> O2,
57{
58 #[inline(always)]
59 pub(crate) fn new(parser: F, map: G) -> Self {
60 Self {
61 parser,
62 map,
63 i: Default::default(),
64 o: Default::default(),
65 o2: Default::default(),
66 e: Default::default(),
67 }
68 }
69}
70
71impl<F, G, I, O, O2, E> Parser<I, O2, E> for Map<F, G, I, O, O2, E>
72where
73 F: Parser<I, O, E>,
74 G: FnMut(O) -> O2,
75{
76 #[inline]
77 fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> {
78 match self.parser.parse_next(input:i) {
79 Err(e: ErrMode) => Err(e),
80 Ok(o: O) => Ok((self.map)(o)),
81 }
82 }
83}
84
85/// Implementation of [`Parser::try_map`]
86#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
87pub struct TryMap<F, G, I, O, O2, E, E2>
88where
89 F: Parser<I, O, E>,
90 G: FnMut(O) -> Result<O2, E2>,
91 I: Stream,
92 E: FromExternalError<I, E2>,
93{
94 parser: F,
95 map: G,
96 i: core::marker::PhantomData<I>,
97 o: core::marker::PhantomData<O>,
98 o2: core::marker::PhantomData<O2>,
99 e: core::marker::PhantomData<E>,
100 e2: core::marker::PhantomData<E2>,
101}
102
103impl<F, G, I, O, O2, E, E2> TryMap<F, G, I, O, O2, E, E2>
104where
105 F: Parser<I, O, E>,
106 G: FnMut(O) -> Result<O2, E2>,
107 I: Stream,
108 E: FromExternalError<I, E2>,
109{
110 #[inline(always)]
111 pub(crate) fn new(parser: F, map: G) -> Self {
112 Self {
113 parser,
114 map,
115 i: Default::default(),
116 o: Default::default(),
117 o2: Default::default(),
118 e: Default::default(),
119 e2: Default::default(),
120 }
121 }
122}
123
124impl<F, G, I, O, O2, E, E2> Parser<I, O2, E> for TryMap<F, G, I, O, O2, E, E2>
125where
126 F: Parser<I, O, E>,
127 G: FnMut(O) -> Result<O2, E2>,
128 I: Stream,
129 E: FromExternalError<I, E2>,
130{
131 #[inline]
132 fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> {
133 let start: ::Checkpoint = input.checkpoint();
134 let o: O = self.parser.parse_next(input)?;
135 let res: Result> = (self.map)(o).map_err(|err: E2| {
136 input.reset(&start);
137 ErrMode::from_external_error(input, kind:ErrorKind::Verify, e:err)
138 });
139 trace_result(name:"verify", &res);
140 res
141 }
142}
143
144/// Implementation of [`Parser::verify_map`]
145#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
146pub struct VerifyMap<F, G, I, O, O2, E>
147where
148 F: Parser<I, O, E>,
149 G: FnMut(O) -> Option<O2>,
150 I: Stream,
151 E: ParserError<I>,
152{
153 parser: F,
154 map: G,
155 i: core::marker::PhantomData<I>,
156 o: core::marker::PhantomData<O>,
157 o2: core::marker::PhantomData<O2>,
158 e: core::marker::PhantomData<E>,
159}
160
161impl<F, G, I, O, O2, E> VerifyMap<F, G, I, O, O2, E>
162where
163 F: Parser<I, O, E>,
164 G: FnMut(O) -> Option<O2>,
165 I: Stream,
166 E: ParserError<I>,
167{
168 #[inline(always)]
169 pub(crate) fn new(parser: F, map: G) -> Self {
170 Self {
171 parser,
172 map,
173 i: Default::default(),
174 o: Default::default(),
175 o2: Default::default(),
176 e: Default::default(),
177 }
178 }
179}
180
181impl<F, G, I, O, O2, E> Parser<I, O2, E> for VerifyMap<F, G, I, O, O2, E>
182where
183 F: Parser<I, O, E>,
184 G: FnMut(O) -> Option<O2>,
185 I: Stream,
186 E: ParserError<I>,
187{
188 #[inline]
189 fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> {
190 let start: ::Checkpoint = input.checkpoint();
191 let o: O = self.parser.parse_next(input)?;
192 let res: Result> = (self.map)(o).ok_or_else(|| {
193 input.reset(&start);
194 ErrMode::from_error_kind(input, kind:ErrorKind::Verify)
195 });
196 trace_result(name:"verify", &res);
197 res
198 }
199}
200
201/// Implementation of [`Parser::and_then`]
202#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
203pub struct AndThen<F, G, I, O, O2, E>
204where
205 F: Parser<I, O, E>,
206 G: Parser<O, O2, E>,
207 O: StreamIsPartial,
208 I: Stream,
209{
210 outer: F,
211 inner: G,
212 i: core::marker::PhantomData<I>,
213 o: core::marker::PhantomData<O>,
214 o2: core::marker::PhantomData<O2>,
215 e: core::marker::PhantomData<E>,
216}
217
218impl<F, G, I, O, O2, E> AndThen<F, G, I, O, O2, E>
219where
220 F: Parser<I, O, E>,
221 G: Parser<O, O2, E>,
222 O: StreamIsPartial,
223 I: Stream,
224{
225 #[inline(always)]
226 pub(crate) fn new(outer: F, inner: G) -> Self {
227 Self {
228 outer,
229 inner,
230 i: Default::default(),
231 o: Default::default(),
232 o2: Default::default(),
233 e: Default::default(),
234 }
235 }
236}
237
238impl<F, G, I, O, O2, E> Parser<I, O2, E> for AndThen<F, G, I, O, O2, E>
239where
240 F: Parser<I, O, E>,
241 G: Parser<O, O2, E>,
242 O: StreamIsPartial,
243 I: Stream,
244{
245 #[inline(always)]
246 fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> {
247 let start: ::Checkpoint = i.checkpoint();
248 let mut o: O = self.outer.parse_next(input:i)?;
249 let _ = o.complete();
250 let o2: O2 = self.inner.parse_next(&mut o).map_err(|err: ErrMode| {
251 i.reset(&start);
252 err
253 })?;
254 Ok(o2)
255 }
256}
257
258/// Implementation of [`Parser::parse_to`]
259#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
260pub struct ParseTo<P, I, O, O2, E>
261where
262 P: Parser<I, O, E>,
263 I: Stream,
264 O: crate::stream::ParseSlice<O2>,
265 E: ParserError<I>,
266{
267 p: P,
268 i: core::marker::PhantomData<I>,
269 o: core::marker::PhantomData<O>,
270 o2: core::marker::PhantomData<O2>,
271 e: core::marker::PhantomData<E>,
272}
273
274impl<P, I, O, O2, E> ParseTo<P, I, O, O2, E>
275where
276 P: Parser<I, O, E>,
277 I: Stream,
278 O: crate::stream::ParseSlice<O2>,
279 E: ParserError<I>,
280{
281 #[inline(always)]
282 pub(crate) fn new(p: P) -> Self {
283 Self {
284 p,
285 i: Default::default(),
286 o: Default::default(),
287 o2: Default::default(),
288 e: Default::default(),
289 }
290 }
291}
292
293impl<P, I, O, O2, E> Parser<I, O2, E> for ParseTo<P, I, O, O2, E>
294where
295 P: Parser<I, O, E>,
296 I: Stream,
297 O: crate::stream::ParseSlice<O2>,
298 E: ParserError<I>,
299{
300 #[inline]
301 fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> {
302 let start: ::Checkpoint = i.checkpoint();
303 let o: O = self.p.parse_next(input:i)?;
304 let res: Result> = o.parse_slice().ok_or_else(|| {
305 i.reset(&start);
306 ErrMode::from_error_kind(input:i, kind:ErrorKind::Verify)
307 });
308 trace_result(name:"verify", &res);
309 res
310 }
311}
312
313/// Implementation of [`Parser::flat_map`]
314#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
315pub struct FlatMap<F, G, H, I, O, O2, E>
316where
317 F: Parser<I, O, E>,
318 G: FnMut(O) -> H,
319 H: Parser<I, O2, E>,
320{
321 f: F,
322 g: G,
323 h: core::marker::PhantomData<H>,
324 i: core::marker::PhantomData<I>,
325 o: core::marker::PhantomData<O>,
326 o2: core::marker::PhantomData<O2>,
327 e: core::marker::PhantomData<E>,
328}
329
330impl<F, G, H, I, O, O2, E> FlatMap<F, G, H, I, O, O2, E>
331where
332 F: Parser<I, O, E>,
333 G: FnMut(O) -> H,
334 H: Parser<I, O2, E>,
335{
336 #[inline(always)]
337 pub(crate) fn new(f: F, g: G) -> Self {
338 Self {
339 f,
340 g,
341 h: Default::default(),
342 i: Default::default(),
343 o: Default::default(),
344 o2: Default::default(),
345 e: Default::default(),
346 }
347 }
348}
349
350impl<F, G, H, I, O, O2, E> Parser<I, O2, E> for FlatMap<F, G, H, I, O, O2, E>
351where
352 F: Parser<I, O, E>,
353 G: FnMut(O) -> H,
354 H: Parser<I, O2, E>,
355{
356 #[inline(always)]
357 fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> {
358 let o: O = self.f.parse_next(input:i)?;
359 (self.g)(o).parse_next(input:i)
360 }
361}
362
363/// Implementation of [`Parser::complete_err`]
364#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
365pub struct CompleteErr<F> {
366 f: F,
367}
368
369impl<F> CompleteErr<F> {
370 #[inline(always)]
371 pub(crate) fn new(f: F) -> Self {
372 Self { f }
373 }
374}
375
376impl<F, I, O, E> Parser<I, O, E> for CompleteErr<F>
377where
378 I: Stream,
379 F: Parser<I, O, E>,
380 E: ParserError<I>,
381{
382 #[inline]
383 fn parse_next(&mut self, input: &mut I) -> PResult<O, E> {
384 traceimpl Parser(name:"complete_err", |input: &mut I| {
385 match (self.f).parse_next(input) {
386 Err(ErrMode::Incomplete(_)) => {
387 Err(ErrMode::from_error_kind(input, kind:ErrorKind::Complete))
388 }
389 rest: Result> => rest,
390 }
391 })
392 .parse_next(input)
393 }
394}
395
396/// Implementation of [`Parser::verify`]
397#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
398pub struct Verify<F, G, I, O, O2, E>
399where
400 F: Parser<I, O, E>,
401 G: FnMut(&O2) -> bool,
402 I: Stream,
403 O: Borrow<O2>,
404 O2: ?Sized,
405 E: ParserError<I>,
406{
407 parser: F,
408 filter: G,
409 i: core::marker::PhantomData<I>,
410 o: core::marker::PhantomData<O>,
411 o2: core::marker::PhantomData<O2>,
412 e: core::marker::PhantomData<E>,
413}
414
415impl<F, G, I, O, O2, E> Verify<F, G, I, O, O2, E>
416where
417 F: Parser<I, O, E>,
418 G: FnMut(&O2) -> bool,
419 I: Stream,
420 O: Borrow<O2>,
421 O2: ?Sized,
422 E: ParserError<I>,
423{
424 #[inline(always)]
425 pub(crate) fn new(parser: F, filter: G) -> Self {
426 Self {
427 parser,
428 filter,
429 i: Default::default(),
430 o: Default::default(),
431 o2: Default::default(),
432 e: Default::default(),
433 }
434 }
435}
436
437impl<F, G, I, O, O2, E> Parser<I, O, E> for Verify<F, G, I, O, O2, E>
438where
439 F: Parser<I, O, E>,
440 G: FnMut(&O2) -> bool,
441 I: Stream,
442 O: Borrow<O2>,
443 O2: ?Sized,
444 E: ParserError<I>,
445{
446 #[inline]
447 fn parse_next(&mut self, input: &mut I) -> PResult<O, E> {
448 let start: ::Checkpoint = input.checkpoint();
449 let o: O = self.parser.parse_next(input)?;
450 let res: Result> = (self.filter)(o.borrow()).then_some(o).ok_or_else(|| {
451 input.reset(&start);
452 ErrMode::from_error_kind(input, kind:ErrorKind::Verify)
453 });
454 trace_result(name:"verify", &res);
455 res
456 }
457}
458
459/// Implementation of [`Parser::value`]
460#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
461pub struct Value<F, I, O, O2, E>
462where
463 F: Parser<I, O, E>,
464 O2: Clone,
465{
466 parser: F,
467 val: O2,
468 i: core::marker::PhantomData<I>,
469 o: core::marker::PhantomData<O>,
470 e: core::marker::PhantomData<E>,
471}
472
473impl<F, I, O, O2, E> Value<F, I, O, O2, E>
474where
475 F: Parser<I, O, E>,
476 O2: Clone,
477{
478 #[inline(always)]
479 pub(crate) fn new(parser: F, val: O2) -> Self {
480 Self {
481 parser,
482 val,
483 i: Default::default(),
484 o: Default::default(),
485 e: Default::default(),
486 }
487 }
488}
489
490impl<F, I, O, O2, E> Parser<I, O2, E> for Value<F, I, O, O2, E>
491where
492 F: Parser<I, O, E>,
493 O2: Clone,
494{
495 #[inline]
496 fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> {
497 (self.parser).parse_next(input).map(|_| self.val.clone())
498 }
499}
500
501/// Implementation of [`Parser::default_value`]
502#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
503pub struct DefaultValue<F, I, O, O2, E>
504where
505 F: Parser<I, O, E>,
506 O2: core::default::Default,
507{
508 parser: F,
509 o2: core::marker::PhantomData<O2>,
510 i: core::marker::PhantomData<I>,
511 o: core::marker::PhantomData<O>,
512 e: core::marker::PhantomData<E>,
513}
514
515impl<F, I, O, O2, E> DefaultValue<F, I, O, O2, E>
516where
517 F: Parser<I, O, E>,
518 O2: core::default::Default,
519{
520 #[inline(always)]
521 pub(crate) fn new(parser: F) -> Self {
522 Self {
523 parser,
524 o2: Default::default(),
525 i: Default::default(),
526 o: Default::default(),
527 e: Default::default(),
528 }
529 }
530}
531
532impl<F, I, O, O2, E> Parser<I, O2, E> for DefaultValue<F, I, O, O2, E>
533where
534 F: Parser<I, O, E>,
535 O2: core::default::Default,
536{
537 #[inline]
538 fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> {
539 (self.parser).parse_next(input).map(|_| O2::default())
540 }
541}
542
543/// Implementation of [`Parser::void`]
544#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
545pub struct Void<F, I, O, E>
546where
547 F: Parser<I, O, E>,
548{
549 parser: F,
550 i: core::marker::PhantomData<I>,
551 o: core::marker::PhantomData<O>,
552 e: core::marker::PhantomData<E>,
553}
554
555impl<F, I, O, E> Void<F, I, O, E>
556where
557 F: Parser<I, O, E>,
558{
559 #[inline(always)]
560 pub(crate) fn new(parser: F) -> Self {
561 Self {
562 parser,
563 i: Default::default(),
564 o: Default::default(),
565 e: Default::default(),
566 }
567 }
568}
569
570impl<F, I, O, E> Parser<I, (), E> for Void<F, I, O, E>
571where
572 F: Parser<I, O, E>,
573{
574 #[inline(always)]
575 fn parse_next(&mut self, input: &mut I) -> PResult<(), E> {
576 (self.parser).parse_next(input).map(|_| ())
577 }
578}
579
580/// Implementation of [`Parser::recognize`]
581#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
582pub struct Recognize<F, I, O, E>
583where
584 F: Parser<I, O, E>,
585 I: Stream,
586{
587 parser: F,
588 i: core::marker::PhantomData<I>,
589 o: core::marker::PhantomData<O>,
590 e: core::marker::PhantomData<E>,
591}
592
593impl<F, I, O, E> Recognize<F, I, O, E>
594where
595 F: Parser<I, O, E>,
596 I: Stream,
597{
598 #[inline(always)]
599 pub(crate) fn new(parser: F) -> Self {
600 Self {
601 parser,
602 i: Default::default(),
603 o: Default::default(),
604 e: Default::default(),
605 }
606 }
607}
608
609impl<I, O, E, F> Parser<I, <I as Stream>::Slice, E> for Recognize<F, I, O, E>
610where
611 F: Parser<I, O, E>,
612 I: Stream,
613{
614 #[inline]
615 fn parse_next(&mut self, input: &mut I) -> PResult<<I as Stream>::Slice, E> {
616 let checkpoint: ::Checkpoint = input.checkpoint();
617 match (self.parser).parse_next(input) {
618 Ok(_) => {
619 let offset: usize = input.offset_from(&checkpoint);
620 input.reset(&checkpoint);
621 let recognized: ::Slice = input.next_slice(offset);
622 Ok(recognized)
623 }
624 Err(e: ErrMode) => Err(e),
625 }
626 }
627}
628
629/// Implementation of [`Parser::with_recognized`]
630#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
631pub struct WithRecognized<F, I, O, E>
632where
633 F: Parser<I, O, E>,
634 I: Stream,
635{
636 parser: F,
637 i: core::marker::PhantomData<I>,
638 o: core::marker::PhantomData<O>,
639 e: core::marker::PhantomData<E>,
640}
641
642impl<F, I, O, E> WithRecognized<F, I, O, E>
643where
644 F: Parser<I, O, E>,
645 I: Stream,
646{
647 #[inline(always)]
648 pub(crate) fn new(parser: F) -> Self {
649 Self {
650 parser,
651 i: Default::default(),
652 o: Default::default(),
653 e: Default::default(),
654 }
655 }
656}
657
658impl<F, I, O, E> Parser<I, (O, <I as Stream>::Slice), E> for WithRecognized<F, I, O, E>
659where
660 F: Parser<I, O, E>,
661 I: Stream,
662{
663 #[inline]
664 fn parse_next(&mut self, input: &mut I) -> PResult<(O, <I as Stream>::Slice), E> {
665 let checkpoint: ::Checkpoint = input.checkpoint();
666 match (self.parser).parse_next(input) {
667 Ok(result: O) => {
668 let offset: usize = input.offset_from(&checkpoint);
669 input.reset(&checkpoint);
670 let recognized: ::Slice = input.next_slice(offset);
671 Ok((result, recognized))
672 }
673 Err(e: ErrMode) => Err(e),
674 }
675 }
676}
677
678/// Implementation of [`Parser::span`]
679#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
680pub struct Span<F, I, O, E>
681where
682 F: Parser<I, O, E>,
683 I: Stream + Location,
684{
685 parser: F,
686 i: core::marker::PhantomData<I>,
687 o: core::marker::PhantomData<O>,
688 e: core::marker::PhantomData<E>,
689}
690
691impl<F, I, O, E> Span<F, I, O, E>
692where
693 F: Parser<I, O, E>,
694 I: Stream + Location,
695{
696 #[inline(always)]
697 pub(crate) fn new(parser: F) -> Self {
698 Self {
699 parser,
700 i: Default::default(),
701 o: Default::default(),
702 e: Default::default(),
703 }
704 }
705}
706
707impl<I, O, E, F> Parser<I, Range<usize>, E> for Span<F, I, O, E>
708where
709 F: Parser<I, O, E>,
710 I: Stream + Location,
711{
712 #[inline]
713 fn parse_next(&mut self, input: &mut I) -> PResult<Range<usize>, E> {
714 let start: usize = input.location();
715 self.parser.parse_next(input).map(op:move |_| {
716 let end: usize = input.location();
717 start..end
718 })
719 }
720}
721
722/// Implementation of [`Parser::with_span`]
723#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
724pub struct WithSpan<F, I, O, E>
725where
726 F: Parser<I, O, E>,
727 I: Stream + Location,
728{
729 parser: F,
730 i: core::marker::PhantomData<I>,
731 o: core::marker::PhantomData<O>,
732 e: core::marker::PhantomData<E>,
733}
734
735impl<F, I, O, E> WithSpan<F, I, O, E>
736where
737 F: Parser<I, O, E>,
738 I: Stream + Location,
739{
740 #[inline(always)]
741 pub(crate) fn new(parser: F) -> Self {
742 Self {
743 parser,
744 i: Default::default(),
745 o: Default::default(),
746 e: Default::default(),
747 }
748 }
749}
750
751impl<F, I, O, E> Parser<I, (O, Range<usize>), E> for WithSpan<F, I, O, E>
752where
753 F: Parser<I, O, E>,
754 I: Stream + Location,
755{
756 #[inline]
757 fn parse_next(&mut self, input: &mut I) -> PResult<(O, Range<usize>), E> {
758 let start: usize = input.location();
759 self.parser.parse_next(input).map(op:move |output: O| {
760 let end: usize = input.location();
761 (output, (start..end))
762 })
763 }
764}
765
766/// Implementation of [`Parser::output_into`]
767#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
768pub struct OutputInto<F, I, O, O2, E>
769where
770 F: Parser<I, O, E>,
771 O: Into<O2>,
772{
773 parser: F,
774 i: core::marker::PhantomData<I>,
775 o: core::marker::PhantomData<O>,
776 o2: core::marker::PhantomData<O2>,
777 e: core::marker::PhantomData<E>,
778}
779
780impl<F, I, O, O2, E> OutputInto<F, I, O, O2, E>
781where
782 F: Parser<I, O, E>,
783 O: Into<O2>,
784{
785 #[inline(always)]
786 pub(crate) fn new(parser: F) -> Self {
787 Self {
788 parser,
789 i: Default::default(),
790 o: Default::default(),
791 o2: Default::default(),
792 e: Default::default(),
793 }
794 }
795}
796
797impl<F, I, O, O2, E> Parser<I, O2, E> for OutputInto<F, I, O, O2, E>
798where
799 F: Parser<I, O, E>,
800 O: Into<O2>,
801{
802 #[inline]
803 fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> {
804 self.parser.parse_next(i).map(|o: O| o.into())
805 }
806}
807
808/// Implementation of [`Parser::err_into`]
809#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
810pub struct ErrInto<F, I, O, E, E2>
811where
812 F: Parser<I, O, E>,
813 E: Into<E2>,
814{
815 parser: F,
816 i: core::marker::PhantomData<I>,
817 o: core::marker::PhantomData<O>,
818 e: core::marker::PhantomData<E>,
819 e2: core::marker::PhantomData<E2>,
820}
821
822impl<F, I, O, E, E2> ErrInto<F, I, O, E, E2>
823where
824 F: Parser<I, O, E>,
825 E: Into<E2>,
826{
827 #[inline(always)]
828 pub(crate) fn new(parser: F) -> Self {
829 Self {
830 parser,
831 i: Default::default(),
832 o: Default::default(),
833 e: Default::default(),
834 e2: Default::default(),
835 }
836 }
837}
838
839impl<F, I, O, E, E2> Parser<I, O, E2> for ErrInto<F, I, O, E, E2>
840where
841 F: Parser<I, O, E>,
842 E: Into<E2>,
843{
844 #[inline]
845 fn parse_next(&mut self, i: &mut I) -> PResult<O, E2> {
846 match self.parser.parse_next(input:i) {
847 Ok(ok: O) => Ok(ok),
848 Err(ErrMode::Backtrack(e: E)) => Err(ErrMode::Backtrack(e.into())),
849 Err(ErrMode::Cut(e: E)) => Err(ErrMode::Cut(e.into())),
850 Err(ErrMode::Incomplete(e: Needed)) => Err(ErrMode::Incomplete(e)),
851 }
852 }
853}
854
855/// Implementation of [`Parser::context`]
856#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
857pub struct Context<F, I, O, E, C>
858where
859 F: Parser<I, O, E>,
860 I: Stream,
861 E: AddContext<I, C>,
862 C: Clone + crate::lib::std::fmt::Debug,
863{
864 parser: F,
865 context: C,
866 i: core::marker::PhantomData<I>,
867 o: core::marker::PhantomData<O>,
868 e: core::marker::PhantomData<E>,
869}
870
871impl<F, I, O, E, C> Context<F, I, O, E, C>
872where
873 F: Parser<I, O, E>,
874 I: Stream,
875 E: AddContext<I, C>,
876 C: Clone + crate::lib::std::fmt::Debug,
877{
878 #[inline(always)]
879 pub(crate) fn new(parser: F, context: C) -> Self {
880 Self {
881 parser,
882 context,
883 i: Default::default(),
884 o: Default::default(),
885 e: Default::default(),
886 }
887 }
888}
889
890impl<F, I, O, E, C> Parser<I, O, E> for Context<F, I, O, E, C>
891where
892 F: Parser<I, O, E>,
893 I: Stream,
894 E: AddContext<I, C>,
895 C: Clone + crate::lib::std::fmt::Debug,
896{
897 #[inline]
898 fn parse_next(&mut self, i: &mut I) -> PResult<O, E> {
899 let context: C = self.context.clone();
900 trace(DisplayDebug(self.context.clone()), move |i: &mut I| {
901 let start = i.checkpoint();
902 (self.parser)
903 .parse_next(i)
904 .map_err(|err| err.add_context(i, &start, context.clone()))
905 })
906 .parse_next(input:i)
907 }
908}
909
910/// Implementation of [`Parser::retry_after`]
911#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
912#[cfg(feature = "unstable-recover")]
913pub struct RetryAfter<P, R, I, O, E>
914where
915 P: Parser<I, O, E>,
916 R: Parser<I, (), E>,
917 I: Stream,
918 I: Recover<E>,
919 E: FromRecoverableError<I, E>,
920{
921 parser: P,
922 recover: R,
923 i: core::marker::PhantomData<I>,
924 o: core::marker::PhantomData<O>,
925 e: core::marker::PhantomData<E>,
926}
927
928#[cfg(feature = "unstable-recover")]
929impl<P, R, I, O, E> RetryAfter<P, R, I, O, E>
930where
931 P: Parser<I, O, E>,
932 R: Parser<I, (), E>,
933 I: Stream,
934 I: Recover<E>,
935 E: FromRecoverableError<I, E>,
936{
937 #[inline(always)]
938 pub(crate) fn new(parser: P, recover: R) -> Self {
939 Self {
940 parser,
941 recover,
942 i: Default::default(),
943 o: Default::default(),
944 e: Default::default(),
945 }
946 }
947}
948
949#[cfg(feature = "unstable-recover")]
950impl<P, R, I, O, E> Parser<I, O, E> for RetryAfter<P, R, I, O, E>
951where
952 P: Parser<I, O, E>,
953 R: Parser<I, (), E>,
954 I: Stream,
955 I: Recover<E>,
956 E: FromRecoverableError<I, E>,
957{
958 #[inline(always)]
959 fn parse_next(&mut self, i: &mut I) -> PResult<O, E> {
960 if I::is_recovery_supported() {
961 retry_after_inner(&mut self.parser, &mut self.recover, i)
962 } else {
963 self.parser.parse_next(i)
964 }
965 }
966}
967
968#[cfg(feature = "unstable-recover")]
969fn retry_after_inner<P, R, I, O, E>(parser: &mut P, recover: &mut R, i: &mut I) -> PResult<O, E>
970where
971 P: Parser<I, O, E>,
972 R: Parser<I, (), E>,
973 I: Stream,
974 I: Recover<E>,
975 E: FromRecoverableError<I, E>,
976{
977 loop {
978 let token_start = i.checkpoint();
979 let mut err = match parser.parse_next(i) {
980 Ok(o) => {
981 return Ok(o);
982 }
983 Err(ErrMode::Incomplete(e)) => return Err(ErrMode::Incomplete(e)),
984 Err(err) => err,
985 };
986 let err_start = i.checkpoint();
987 let err_start_eof_offset = i.eof_offset();
988 if recover.parse_next(i).is_ok() {
989 let i_eof_offset = i.eof_offset();
990 if err_start_eof_offset == i_eof_offset {
991 // Didn't advance so bubble the error up
992 } else if let Err(err_) = i.record_err(&token_start, &err_start, err) {
993 err = err_;
994 } else {
995 continue;
996 }
997 }
998
999 i.reset(&err_start);
1000 err = err.map(|err| E::from_recoverable_error(&token_start, &err_start, i, err));
1001 return Err(err);
1002 }
1003}
1004
1005/// Implementation of [`Parser::resume_after`]
1006#[cfg(feature = "unstable-recover")]
1007#[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))]
1008pub struct ResumeAfter<P, R, I, O, E>
1009where
1010 P: Parser<I, O, E>,
1011 R: Parser<I, (), E>,
1012 I: Stream,
1013 I: Recover<E>,
1014 E: FromRecoverableError<I, E>,
1015{
1016 parser: P,
1017 recover: R,
1018 i: core::marker::PhantomData<I>,
1019 o: core::marker::PhantomData<O>,
1020 e: core::marker::PhantomData<E>,
1021}
1022
1023#[cfg(feature = "unstable-recover")]
1024impl<P, R, I, O, E> ResumeAfter<P, R, I, O, E>
1025where
1026 P: Parser<I, O, E>,
1027 R: Parser<I, (), E>,
1028 I: Stream,
1029 I: Recover<E>,
1030 E: FromRecoverableError<I, E>,
1031{
1032 #[inline(always)]
1033 pub(crate) fn new(parser: P, recover: R) -> Self {
1034 Self {
1035 parser,
1036 recover,
1037 i: Default::default(),
1038 o: Default::default(),
1039 e: Default::default(),
1040 }
1041 }
1042}
1043
1044#[cfg(feature = "unstable-recover")]
1045impl<P, R, I, O, E> Parser<I, Option<O>, E> for ResumeAfter<P, R, I, O, E>
1046where
1047 P: Parser<I, O, E>,
1048 R: Parser<I, (), E>,
1049 I: Stream,
1050 I: Recover<E>,
1051 E: FromRecoverableError<I, E>,
1052{
1053 #[inline(always)]
1054 fn parse_next(&mut self, i: &mut I) -> PResult<Option<O>, E> {
1055 if I::is_recovery_supported() {
1056 resume_after_inner(&mut self.parser, &mut self.recover, i)
1057 } else {
1058 self.parser.parse_next(i).map(Some)
1059 }
1060 }
1061}
1062
1063#[cfg(feature = "unstable-recover")]
1064fn resume_after_inner<P, R, I, O, E>(
1065 parser: &mut P,
1066 recover: &mut R,
1067 i: &mut I,
1068) -> PResult<Option<O>, E>
1069where
1070 P: Parser<I, O, E>,
1071 R: Parser<I, (), E>,
1072 I: Stream,
1073 I: Recover<E>,
1074 E: FromRecoverableError<I, E>,
1075{
1076 let token_start = i.checkpoint();
1077 let mut err = match parser.parse_next(i) {
1078 Ok(o) => {
1079 return Ok(Some(o));
1080 }
1081 Err(ErrMode::Incomplete(e)) => return Err(ErrMode::Incomplete(e)),
1082 Err(err) => err,
1083 };
1084 let err_start = i.checkpoint();
1085 if recover.parse_next(i).is_ok() {
1086 if let Err(err_) = i.record_err(&token_start, &err_start, err) {
1087 err = err_;
1088 } else {
1089 return Ok(None);
1090 }
1091 }
1092
1093 i.reset(&err_start);
1094 err = err.map(|err| E::from_recoverable_error(&token_start, &err_start, i, err));
1095 Err(err)
1096}
1097