1use inspector::Inspector;
9
10pub use crate::stream::{BoxedExactSizeStream, BoxedStream, IterInput, Stream};
11
12use super::*;
13use alloc::string::ToString;
14#[cfg(feature = "std")]
15use std::io::{BufReader, Read, Seek};
16
17pub trait Input<'src>: 'src {
31 type Span: Span;
41
42 type Token: 'src;
48
49 type MaybeToken: IntoMaybe<'src, Self::Token>;
77
78 type Cursor: Clone;
86
87 type Cache;
93
94 fn begin(self) -> (Self::Cursor, Self::Cache);
96
97 fn cursor_location(cursor: &Self::Cursor) -> usize;
112
113 unsafe fn next_maybe(
124 cache: &mut Self::Cache,
125 cursor: &mut Self::Cursor,
126 ) -> Option<Self::MaybeToken>;
127
128 unsafe fn span(cache: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span;
135
136 fn with_context<S: Span>(self, context: S::Context) -> WithContext<S, Self>
172 where
173 Self: Sized,
174 {
175 WithContext {
176 input: self,
177 context,
178 phantom: EmptyPhantom::new(),
179 }
180 }
181
182 fn map<T, S, F>(self, eoi: S, f: F) -> MappedInput<'src, T, S, Self, F>
196 where
197 Self: Sized,
198 F: Fn(
199 Self::MaybeToken,
200 ) -> (
201 <Self::MaybeToken as IntoMaybe<'src, Self::Token>>::Proj<T>,
202 <Self::MaybeToken as IntoMaybe<'src, Self::Token>>::Proj<S>,
203 ) + 'src,
204 T: 'src,
205 S: Span + 'src,
206 {
207 MappedInput {
208 input: self,
209 eoi,
210 mapper: f,
211 phantom: EmptyPhantom::new(),
212 }
213 }
214
215 fn split_token_span<T, S>(self, eoi: S) -> MappedInput<'src, T, S, Self>
252 where
253 Self: Input<'src, Token = (T, S), MaybeToken = &'src (T, S)> + Sized,
254 T: 'src,
255 S: Span + 'src,
256 {
257 self.map(eoi, |(t, s)| (t, s))
258 }
259
260 fn split_spanned<T, S>(self, eoi: S) -> MappedInput<'src, T, S, Self>
264 where
265 Self: Input<'src, Token = S::Spanned, MaybeToken = &'src S::Spanned> + Sized,
266 T: 'src,
267 S: WrappingSpan<T> + 'src,
268 {
269 self.map(eoi, |spanned| (S::inner_of(spanned), S::span_of(spanned)))
270 }
271
272 fn map_span<S: Span, F>(self, map_fn: F) -> MappedSpan<S, Self, F>
277 where
278 Self: Input<'src> + Sized,
279 F: Fn(Self::Span) -> S,
280 {
281 MappedSpan {
282 input: self,
283 map_fn,
284 phantom: PhantomData,
285 }
286 }
287}
288
289pub trait ExactSizeInput<'src>: Input<'src> {
294 unsafe fn span_from(cache: &mut Self::Cache, range: RangeFrom<&Self::Cursor>) -> Self::Span;
300}
301
302pub trait SliceInput<'src>: ExactSizeInput<'src> {
308 type Slice: Clone;
310
311 fn full_slice(cache: &mut Self::Cache) -> Self::Slice;
317
318 unsafe fn slice(cache: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice;
324
325 unsafe fn slice_from(cache: &mut Self::Cache, from: RangeFrom<&Self::Cursor>) -> Self::Slice;
331}
332
333pub trait StrInput<'src>: Sealed + ValueInput<'src, Cursor = usize> + SliceInput<'src>
340where
341 Self::Token: Char,
342{
343 #[doc(hidden)]
344 fn stringify(slice: Self::Slice) -> String;
345}
346
347pub trait ValueInput<'src>: Input<'src> {
349 unsafe fn next(cache: &mut Self::Cache, cursor: &mut Self::Cursor) -> Option<Self::Token>;
355}
356
357pub trait BorrowInput<'src>: Input<'src> {
361 unsafe fn next_ref(
367 cache: &mut Self::Cache,
368 cursor: &mut Self::Cursor,
369 ) -> Option<&'src Self::Token>;
370}
371
372impl<'src> Input<'src> for &'src str {
373 type Cursor = usize;
374 type Span = SimpleSpan<usize>;
375
376 type Token = char;
377 type MaybeToken = char;
378
379 type Cache = Self;
380
381 #[inline]
382 fn begin(self) -> (Self::Cursor, Self::Cache) {
383 (0, self)
384 }
385
386 #[inline]
387 fn cursor_location(cursor: &Self::Cursor) -> usize {
388 *cursor
389 }
390
391 #[inline(always)]
392 unsafe fn next_maybe(
393 this: &mut Self::Cache,
394 cursor: &mut Self::Cursor,
395 ) -> Option<Self::MaybeToken> {
396 if *cursor < this.len() {
397 let c = this
400 .get_unchecked(*cursor..)
401 .chars()
402 .next()
403 .unwrap_unchecked();
404 *cursor += c.len_utf8();
405 Some(c)
406 } else {
407 None
408 }
409 }
410
411 #[inline(always)]
412 unsafe fn span(_this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span {
413 (*range.start..*range.end).into()
414 }
415}
416
417impl<'src> ExactSizeInput<'src> for &'src str {
418 #[inline(always)]
419 unsafe fn span_from(this: &mut Self::Cache, range: RangeFrom<&Self::Cursor>) -> Self::Span {
420 (*range.start..this.len()).into()
421 }
422}
423
424impl<'src> ValueInput<'src> for &'src str {
425 #[inline(always)]
426 unsafe fn next(this: &mut Self::Cache, cursor: &mut Self::Cursor) -> Option<Self::Token> {
427 Self::next_maybe(this, cursor)
428 }
429}
430
431impl Sealed for &str {}
432impl<'src> StrInput<'src> for &'src str {
433 #[doc(hidden)]
434 fn stringify(slice: Self::Slice) -> String {
435 slice.to_string()
436 }
437}
438
439impl<'src> SliceInput<'src> for &'src str {
440 type Slice = &'src str;
441
442 #[inline(always)]
443 fn full_slice(this: &mut Self::Cache) -> Self::Slice {
444 *this
445 }
446
447 #[inline(always)]
448 unsafe fn slice(this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice {
449 &this[*range.start..*range.end]
450 }
451
452 #[inline(always)]
453 unsafe fn slice_from(this: &mut Self::Cache, from: RangeFrom<&Self::Cursor>) -> Self::Slice {
454 &this[*from.start..]
455 }
456}
457
458impl<'src, T> Input<'src> for &'src [T] {
459 type Cursor = usize;
460 type Span = SimpleSpan<usize>;
461
462 type Token = T;
463 type MaybeToken = &'src T;
464
465 type Cache = Self;
466
467 #[inline]
468 fn begin(self) -> (Self::Cursor, Self::Cache) {
469 (0, self)
470 }
471
472 #[inline]
473 fn cursor_location(cursor: &Self::Cursor) -> usize {
474 *cursor
475 }
476
477 #[inline(always)]
478 unsafe fn next_maybe(
479 this: &mut Self::Cache,
480 cursor: &mut Self::Cursor,
481 ) -> Option<Self::MaybeToken> {
482 if let Some(tok) = this.get(*cursor) {
483 *cursor += 1;
484 Some(tok)
485 } else {
486 None
487 }
488 }
489
490 #[inline(always)]
491 unsafe fn span(_this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span {
492 (*range.start..*range.end).into()
493 }
494}
495
496impl<'src, T> ExactSizeInput<'src> for &'src [T] {
497 #[inline(always)]
498 unsafe fn span_from(this: &mut Self::Cache, range: RangeFrom<&Self::Cursor>) -> Self::Span {
499 (*range.start..this.len()).into()
500 }
501}
502
503impl Sealed for &[u8] {}
504impl<'src> StrInput<'src> for &'src [u8] {
505 #[doc(hidden)]
506 fn stringify(slice: Self::Slice) -> String {
507 slice
508 .iter()
509 .map(|e| char::from(*e))
511 .collect()
512 }
513}
514
515impl<'src, T> SliceInput<'src> for &'src [T] {
516 type Slice = &'src [T];
517
518 #[inline(always)]
519 fn full_slice(this: &mut Self::Cache) -> Self::Slice {
520 *this
521 }
522
523 #[inline(always)]
524 unsafe fn slice(this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice {
525 &this[*range.start..*range.end]
526 }
527
528 #[inline(always)]
529 unsafe fn slice_from(this: &mut Self::Cache, from: RangeFrom<&Self::Cursor>) -> Self::Slice {
530 &this[*from.start..]
531 }
532}
533
534impl<'src, T: Clone> ValueInput<'src> for &'src [T] {
535 #[inline(always)]
536 unsafe fn next(this: &mut Self::Cache, cursor: &mut Self::Cursor) -> Option<Self::Token> {
537 Self::next_maybe(this, cursor).cloned()
538 }
539}
540
541impl<'src, T> BorrowInput<'src> for &'src [T] {
542 #[inline(always)]
543 unsafe fn next_ref(
544 this: &mut Self::Cache,
545 cursor: &mut Self::Cursor,
546 ) -> Option<&'src Self::Token> {
547 Self::next_maybe(this, cursor)
548 }
549}
550
551impl<'src, T: 'src, const N: usize> Input<'src> for &'src [T; N] {
552 type Cursor = usize;
553 type Span = SimpleSpan<usize>;
554
555 type Token = T;
556 type MaybeToken = &'src T;
557
558 type Cache = Self;
559
560 #[inline]
561 fn begin(self) -> (Self::Cursor, Self::Cache) {
562 (0, self)
563 }
564
565 #[inline]
566 fn cursor_location(cursor: &Self::Cursor) -> usize {
567 *cursor
568 }
569
570 #[inline(always)]
571 unsafe fn next_maybe(
572 this: &mut Self::Cache,
573 cursor: &mut Self::Cursor,
574 ) -> Option<Self::MaybeToken> {
575 if let Some(tok) = this.get(*cursor) {
576 *cursor += 1;
577 Some(tok)
578 } else {
579 None
580 }
581 }
582
583 #[inline(always)]
584 unsafe fn span(_this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span {
585 (*range.start..*range.end).into()
586 }
587}
588
589impl<'src, T: 'src, const N: usize> ExactSizeInput<'src> for &'src [T; N] {
590 #[inline(always)]
591 unsafe fn span_from(this: &mut Self::Cache, range: RangeFrom<&Self::Cursor>) -> Self::Span {
592 (*range.start..this.len()).into()
593 }
594}
595
596impl<const N: usize> Sealed for &[u8; N] {}
597impl<'src, const N: usize> StrInput<'src> for &'src [u8; N] {
598 #[doc(hidden)]
599 fn stringify(slice: Self::Slice) -> String {
600 <&[u8]>::stringify(slice)
601 }
602}
603
604impl<'src, T: 'src, const N: usize> SliceInput<'src> for &'src [T; N] {
605 type Slice = &'src [T];
606
607 #[inline(always)]
608 fn full_slice(this: &mut Self::Cache) -> Self::Slice {
609 *this
610 }
611
612 #[inline(always)]
613 unsafe fn slice(this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice {
614 &this[*range.start..*range.end]
615 }
616
617 #[inline(always)]
618 unsafe fn slice_from(this: &mut Self::Cache, from: RangeFrom<&Self::Cursor>) -> Self::Slice {
619 &this[*from.start..]
620 }
621}
622
623impl<'src, T: Clone + 'src, const N: usize> ValueInput<'src> for &'src [T; N] {
624 #[inline(always)]
625 unsafe fn next(this: &mut Self::Cache, cursor: &mut Self::Cursor) -> Option<Self::Token> {
626 Self::next_maybe(this, cursor).cloned()
627 }
628}
629
630impl<'src, T: 'src, const N: usize> BorrowInput<'src> for &'src [T; N] {
631 #[inline(always)]
632 unsafe fn next_ref(
633 this: &mut Self::Cache,
634 cursor: &mut Self::Cursor,
635 ) -> Option<&'src Self::Token> {
636 Self::next_maybe(this, cursor)
637 }
638}
639
640#[derive(Copy, Clone)]
642pub struct MappedInput<
643 'src,
644 T,
645 S,
646 I,
647 F = fn(
648 <I as Input<'src>>::MaybeToken,
649 ) -> (
650 <<I as Input<'src>>::MaybeToken as IntoMaybe<'src, <I as Input<'src>>::Token>>::Proj<T>,
651 <<I as Input<'src>>::MaybeToken as IntoMaybe<'src, <I as Input<'src>>::Token>>::Proj<S>,
652 ),
653> {
654 input: I,
655 eoi: S,
656 mapper: F,
657 #[allow(dead_code)]
658 phantom: EmptyPhantom<&'src T>,
659}
660
661impl<'src, T, S, I, F> Input<'src> for MappedInput<'src, T, S, I, F>
662where
663 I: Input<'src>,
664 T: 'src,
665 S: Span + Clone + 'src,
666 F: Fn(
667 I::MaybeToken,
668 ) -> (
669 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<T>,
670 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<S>,
671 ) + 'src,
672{
673 type Cursor = (I::Cursor, Option<S::Offset>);
674 type Span = S;
675
676 type Token = T;
677 type MaybeToken = <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<Self::Token>;
678
679 type Cache = (I::Cache, F, S);
680
681 #[inline]
682 fn begin(self) -> (Self::Cursor, Self::Cache) {
683 let (cursor, cache) = self.input.begin();
684 ((cursor, None), (cache, self.mapper, self.eoi))
685 }
686
687 #[inline]
688 fn cursor_location(cursor: &Self::Cursor) -> usize {
689 I::cursor_location(&cursor.0)
690 }
691
692 #[inline(always)]
693 unsafe fn next_maybe(
694 (cache, mapper, _): &mut Self::Cache,
695 cursor: &mut Self::Cursor,
696 ) -> Option<Self::MaybeToken> {
697 I::next_maybe(cache, &mut cursor.0).map(|tok| {
698 let (tok, span) = mapper(tok);
699 cursor.1 = Some(span.borrow().end());
700 tok
701 })
702 }
703
704 #[inline]
705 unsafe fn span(
706 (cache, mapper, eoi): &mut Self::Cache,
707 range: Range<&Self::Cursor>,
708 ) -> Self::Span {
709 match I::next_maybe(cache, &mut range.start.0.clone()) {
710 Some(tok) => {
711 let start = mapper(tok).1.borrow().start();
712 let end = range.end.1.clone().unwrap_or_else(|| eoi.end());
713 S::new(eoi.context(), start..end)
714 }
715 None => S::new(eoi.context(), eoi.end()..eoi.end()),
716 }
717 }
718}
719
720impl<'src, T, S, I, F> ExactSizeInput<'src> for MappedInput<'src, T, S, I, F>
721where
722 I: ExactSizeInput<'src>,
723 T: 'src,
724 S: Span + Clone + 'src,
725 F: Fn(
726 I::MaybeToken,
727 ) -> (
728 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<T>,
729 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<S>,
730 ) + 'src,
731{
732 #[inline(always)]
733 unsafe fn span_from(
734 (cache, mapper, eoi): &mut Self::Cache,
735 range: RangeFrom<&Self::Cursor>,
736 ) -> Self::Span {
737 let start = I::next_maybe(cache, &mut range.start.0.clone())
738 .map(|tok| mapper(tok).1.borrow().start())
739 .unwrap_or_else(|| eoi.end());
740 S::new(eoi.context(), start..eoi.end())
741 }
742}
743
744impl<'src, T, S, I, F> ValueInput<'src> for MappedInput<'src, T, S, I, F>
745where
746 I: ValueInput<'src>,
747 T: Clone + 'src,
748 S: Span + Clone + 'src,
749 F: Fn(
750 I::MaybeToken,
751 ) -> (
752 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<T>,
753 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<S>,
754 ) + 'src,
755{
756 #[inline(always)]
757 unsafe fn next(
758 (cache, mapper, _): &mut Self::Cache,
759 cursor: &mut Self::Cursor,
760 ) -> Option<Self::Token> {
761 I::next_maybe(cache, &mut cursor.0).map(|tok| {
762 let (tok, span) = mapper(tok);
763 cursor.1 = Some(span.borrow().end());
764 tok.borrow().clone()
765 })
766 }
767}
768
769impl<'src, T, S, I, F> BorrowInput<'src> for MappedInput<'src, T, S, I, F>
770where
771 I: Input<'src> + BorrowInput<'src>,
772 I::MaybeToken: From<&'src I::Token>,
773 Self::MaybeToken: Into<&'src Self::Token>,
774 T: 'src,
775 S: Span + Clone + 'src,
776 F: Fn(
777 I::MaybeToken,
778 ) -> (
779 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<T>,
780 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<S>,
781 ) + 'src,
782{
783 #[inline(always)]
784 unsafe fn next_ref(
785 (cache, mapper, _): &mut Self::Cache,
786 cursor: &mut Self::Cursor,
787 ) -> Option<&'src Self::Token> {
788 I::next_ref(cache, &mut cursor.0).map(|tok| {
789 let (tok, span) = mapper(tok.into());
790 cursor.1 = Some(span.borrow().end());
791 tok.into()
792 })
793 }
794}
795
796impl<'src, T, S, I, F> SliceInput<'src> for MappedInput<'src, T, S, I, F>
797where
798 I: Input<'src> + SliceInput<'src, Token = (T, S)>,
799 T: 'src,
800 S: Span + Clone + 'src,
801 F: Fn(
802 I::MaybeToken,
803 ) -> (
804 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<T>,
805 <I::MaybeToken as IntoMaybe<'src, I::Token>>::Proj<S>,
806 ) + 'src,
807{
808 type Slice = I::Slice;
809
810 #[inline(always)]
811 fn full_slice((cache, _, _): &mut Self::Cache) -> Self::Slice {
812 I::full_slice(cache)
813 }
814
815 #[inline(always)]
816 unsafe fn slice((cache, _, _): &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice {
817 I::slice(cache, &range.start.0..&range.end.0)
818 }
819
820 #[inline(always)]
821 unsafe fn slice_from(
822 (cache, _, _): &mut Self::Cache,
823 from: RangeFrom<&Self::Cursor>,
824 ) -> Self::Slice {
825 I::slice_from(cache, &from.start.0..)
826 }
827}
828
829#[derive(Copy, Clone)]
832pub struct MappedSpan<S: Span, I, F> {
833 input: I,
834 map_fn: F,
835 phantom: PhantomData<S>,
836}
837
838impl<'src, S, I: Input<'src>, F: 'src> Input<'src> for MappedSpan<S, I, F>
839where
840 S: Span + Clone + 'src,
841 S::Context: Clone + 'src,
842 S::Offset: From<<I::Span as Span>::Offset>,
843 F: Fn(I::Span) -> S,
844{
845 type Cursor = I::Cursor;
846 type Span = S;
847
848 type Token = I::Token;
849 type MaybeToken = I::MaybeToken;
850
851 type Cache = (I::Cache, F);
852
853 #[inline(always)]
854 fn begin(self) -> (Self::Cursor, Self::Cache) {
855 let (cursor, cache) = self.input.begin();
856 (cursor, (cache, self.map_fn))
857 }
858
859 #[inline]
860 fn cursor_location(cursor: &Self::Cursor) -> usize {
861 I::cursor_location(cursor)
862 }
863
864 #[inline(always)]
865 unsafe fn next_maybe(
866 (cache, _): &mut Self::Cache,
867 cursor: &mut Self::Cursor,
868 ) -> Option<Self::MaybeToken> {
869 I::next_maybe(cache, cursor)
870 }
871
872 #[inline]
873 unsafe fn span((cache, mapper): &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span {
874 let inner_span = I::span(cache, range);
875 (mapper)(inner_span)
876 }
877}
878
879impl<'src, S, I: Input<'src>, F: 'src> ExactSizeInput<'src> for MappedSpan<S, I, F>
880where
881 I: ExactSizeInput<'src>,
882 S: Span + Clone + 'src,
883 S::Context: Clone + 'src,
884 S::Offset: From<<I::Span as Span>::Offset>,
885 F: Fn(I::Span) -> S,
886{
887 #[inline(always)]
888 unsafe fn span_from(
889 (cache, mapper): &mut Self::Cache,
890 range: RangeFrom<&Self::Cursor>,
891 ) -> Self::Span {
892 let inner_span = I::span_from(cache, range);
893 (mapper)(inner_span)
894 }
895}
896
897impl<'src, S, I: ValueInput<'src>, F: 'src> ValueInput<'src> for MappedSpan<S, I, F>
898where
899 S: Span + Clone + 'src,
900 S::Context: Clone + 'src,
901 S::Offset: From<<I::Span as Span>::Offset>,
902 F: Fn(I::Span) -> S,
903{
904 #[inline(always)]
905 unsafe fn next((cache, _): &mut Self::Cache, cursor: &mut Self::Cursor) -> Option<Self::Token> {
906 I::next(cache, cursor)
907 }
908}
909
910impl<'src, S, I: BorrowInput<'src>, F: 'src> BorrowInput<'src> for MappedSpan<S, I, F>
911where
912 S: Span + Clone + 'src,
913 S::Context: Clone + 'src,
914 S::Offset: From<<I::Span as Span>::Offset>,
915 F: Fn(I::Span) -> S,
916{
917 #[inline(always)]
918 unsafe fn next_ref(
919 (cache, _): &mut Self::Cache,
920 cursor: &mut Self::Cursor,
921 ) -> Option<&'src Self::Token> {
922 I::next_ref(cache, cursor)
923 }
924}
925
926impl<'src, S, I: SliceInput<'src>, F: 'src> SliceInput<'src> for MappedSpan<S, I, F>
927where
928 S: Span + Clone + 'src,
929 S::Context: Clone + 'src,
930 S::Offset: From<<I::Span as Span>::Offset>,
931 F: Fn(I::Span) -> S,
932{
933 type Slice = I::Slice;
934
935 #[inline(always)]
936 fn full_slice((cache, _): &mut Self::Cache) -> Self::Slice {
937 I::full_slice(cache)
938 }
939
940 #[inline(always)]
941 unsafe fn slice((cache, _): &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice {
942 I::slice(cache, range)
943 }
944
945 #[inline(always)]
946 unsafe fn slice_from(
947 (cache, _): &mut Self::Cache,
948 from: RangeFrom<&Self::Cursor>,
949 ) -> Self::Slice {
950 I::slice_from(cache, from)
951 }
952}
953
954impl<'src, S, I, F: 'src> Sealed for MappedSpan<S, I, F>
955where
956 I: Input<'src>,
957 S: Span + Clone + 'src,
958 S::Context: Clone + 'src,
959 S::Offset: From<<I::Span as Span>::Offset>,
960 F: Fn(I::Span) -> S,
961{
962}
963impl<'src, S, I, F: 'src> StrInput<'src> for MappedSpan<S, I, F>
964where
965 I: StrInput<'src>,
966 I::Token: Char,
967 S: Span + Clone + 'src,
968 S::Context: Clone + 'src,
969 S::Offset: From<<I::Span as Span>::Offset>,
970 F: Fn(I::Span) -> S,
971{
972 #[doc(hidden)]
973 fn stringify(slice: Self::Slice) -> String {
974 I::stringify(slice)
975 }
976}
977
978#[derive(Copy, Clone)]
981pub struct WithContext<S: Span, I> {
982 input: I,
983 context: S::Context,
984 #[allow(dead_code)]
985 phantom: EmptyPhantom<S>,
986}
987
988impl<'src, S, I: Input<'src>> Input<'src> for WithContext<S, I>
989where
990 S: Span + Clone + 'src,
991 S::Context: Clone + 'src,
992 S::Offset: From<<I::Span as Span>::Offset>,
993{
994 type Cursor = I::Cursor;
995 type Span = S;
996
997 type Token = I::Token;
998 type MaybeToken = I::MaybeToken;
999
1000 type Cache = (I::Cache, S::Context);
1001
1002 #[inline(always)]
1003 fn begin(self) -> (Self::Cursor, Self::Cache) {
1004 let (cursor, cache) = self.input.begin();
1005 (cursor, (cache, self.context))
1006 }
1007
1008 #[inline]
1009 fn cursor_location(cursor: &Self::Cursor) -> usize {
1010 I::cursor_location(cursor)
1011 }
1012
1013 #[inline(always)]
1014 unsafe fn next_maybe(
1015 (cache, _): &mut Self::Cache,
1016 cursor: &mut Self::Cursor,
1017 ) -> Option<Self::MaybeToken> {
1018 I::next_maybe(cache, cursor)
1019 }
1020
1021 #[inline]
1022 unsafe fn span((cache, ctx): &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span {
1023 let inner_span = I::span(cache, range);
1024 S::new(
1025 ctx.clone(),
1026 inner_span.start().into()..inner_span.end().into(),
1027 )
1028 }
1029}
1030
1031impl<'src, S, I: Input<'src>> ExactSizeInput<'src> for WithContext<S, I>
1032where
1033 I: ExactSizeInput<'src>,
1034 S: Span + Clone + 'src,
1035 S::Context: Clone + 'src,
1036 S::Offset: From<<I::Span as Span>::Offset>,
1037{
1038 #[inline]
1039 unsafe fn span_from(
1040 (cache, ctx): &mut Self::Cache,
1041 range: RangeFrom<&Self::Cursor>,
1042 ) -> Self::Span {
1043 let inner_span = I::span_from(cache, range);
1044 S::new(
1045 ctx.clone(),
1046 inner_span.start().into()..inner_span.end().into(),
1047 )
1048 }
1049}
1050
1051impl<'src, S, I: ValueInput<'src>> ValueInput<'src> for WithContext<S, I>
1052where
1053 S: Span + Clone + 'src,
1054 S::Context: Clone + 'src,
1055 S::Offset: From<<I::Span as Span>::Offset>,
1056{
1057 #[inline(always)]
1058 unsafe fn next((cache, _): &mut Self::Cache, cursor: &mut Self::Cursor) -> Option<Self::Token> {
1059 I::next(cache, cursor)
1060 }
1061}
1062
1063impl<'src, S, I: BorrowInput<'src>> BorrowInput<'src> for WithContext<S, I>
1064where
1065 S: Span + Clone + 'src,
1066 S::Context: Clone + 'src,
1067 S::Offset: From<<I::Span as Span>::Offset>,
1068{
1069 #[inline(always)]
1070 unsafe fn next_ref(
1071 (cache, _): &mut Self::Cache,
1072 cursor: &mut Self::Cursor,
1073 ) -> Option<&'src Self::Token> {
1074 I::next_ref(cache, cursor)
1075 }
1076}
1077
1078impl<'src, S, I: SliceInput<'src>> SliceInput<'src> for WithContext<S, I>
1079where
1080 S: Span + Clone + 'src,
1081 S::Context: Clone + 'src,
1082 S::Offset: From<<I::Span as Span>::Offset>,
1083{
1084 type Slice = I::Slice;
1085
1086 #[inline(always)]
1087 fn full_slice((cache, _): &mut Self::Cache) -> Self::Slice {
1088 I::full_slice(cache)
1089 }
1090
1091 #[inline(always)]
1092 unsafe fn slice((cache, _): &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice {
1093 I::slice(cache, range)
1094 }
1095
1096 #[inline(always)]
1097 unsafe fn slice_from(
1098 (cache, _): &mut Self::Cache,
1099 from: RangeFrom<&Self::Cursor>,
1100 ) -> Self::Slice {
1101 I::slice_from(cache, from)
1102 }
1103}
1104
1105impl<S: Span, I> Sealed for WithContext<S, I> {}
1106impl<'src, S, I> StrInput<'src> for WithContext<S, I>
1107where
1108 I: StrInput<'src>,
1109 I::Token: Char,
1110 S: Span + Clone + 'src,
1111 S::Context: Clone + 'src,
1112 S::Offset: From<<I::Span as Span>::Offset>,
1113{
1114 #[doc(hidden)]
1115 fn stringify(slice: Self::Slice) -> String {
1116 I::stringify(slice)
1117 }
1118}
1119
1120#[cfg(feature = "std")]
1125pub struct IoInput<R> {
1126 reader: BufReader<R>,
1127 last_cursor: usize,
1128}
1129
1130#[cfg(feature = "std")]
1131impl<R: Read + Seek> IoInput<R> {
1132 pub fn new(reader: R) -> IoInput<R> {
1134 IoInput {
1135 reader: BufReader::new(reader),
1136 last_cursor: 0,
1137 }
1138 }
1139}
1140
1141#[cfg(feature = "std")]
1142impl<'src, R: Read + Seek + 'src> Input<'src> for IoInput<R> {
1143 type Cursor = usize;
1144 type Span = SimpleSpan;
1145
1146 type Token = u8;
1147 type MaybeToken = u8;
1148
1149 type Cache = Self;
1150
1151 fn begin(self) -> (Self::Cursor, Self::Cache) {
1152 (0, self)
1153 }
1154
1155 #[inline(always)]
1156 fn cursor_location(cursor: &Self::Cursor) -> usize {
1157 *cursor
1158 }
1159
1160 #[inline(always)]
1161 unsafe fn next_maybe(
1162 this: &mut Self::Cache,
1163 cursor: &mut Self::Cursor,
1164 ) -> Option<Self::MaybeToken> {
1165 Self::next(this, cursor)
1166 }
1167
1168 #[inline]
1169 unsafe fn span(_this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span {
1170 (*range.start..*range.end).into()
1171 }
1172}
1173
1174#[cfg(feature = "std")]
1175impl<'src, R: Read + Seek + 'src> ValueInput<'src> for IoInput<R> {
1176 unsafe fn next(this: &mut Self::Cache, cursor: &mut Self::Cursor) -> Option<Self::Token> {
1177 if *cursor != this.last_cursor {
1178 let seek = *cursor as i64 - this.last_cursor as i64;
1179
1180 this.reader.seek_relative(seek).unwrap();
1181
1182 this.last_cursor = *cursor;
1183 }
1184
1185 let mut out = 0;
1186
1187 let r = this.reader.read_exact(std::slice::from_mut(&mut out));
1188
1189 match r {
1190 Ok(()) => {
1191 this.last_cursor += 1;
1192 *cursor += 1;
1193 Some(out)
1194 }
1195 Err(_) => None,
1196 }
1197 }
1198}
1199
1200pub struct Checkpoint<'src, 'parse, I: Input<'src>, C> {
1204 cursor: Cursor<'src, 'parse, I>,
1205 pub(crate) err_count: usize,
1206 pub(crate) inspector: C,
1207 phantom: PhantomData<fn(&'parse ()) -> &'parse ()>, }
1209
1210impl<'src, 'parse, I: Input<'src>, C> Checkpoint<'src, 'parse, I, C> {
1211 pub fn cursor(&self) -> &Cursor<'src, 'parse, I> {
1213 &self.cursor
1214 }
1215
1216 pub fn inspector(&self) -> &C {
1218 &self.inspector
1219 }
1220}
1221
1222impl<'src, I: Input<'src>, C: Clone> Clone for Checkpoint<'src, '_, I, C> {
1223 #[inline(always)]
1224 fn clone(&self) -> Self {
1225 Self {
1226 cursor: self.cursor.clone(),
1227 err_count: self.err_count,
1228 inspector: self.inspector.clone(),
1229 phantom: PhantomData,
1230 }
1231 }
1232}
1233
1234#[repr(transparent)]
1238pub struct Cursor<'src, 'parse, I: Input<'src>> {
1239 pub(crate) inner: I::Cursor,
1240 phantom: PhantomData<fn(&'parse ()) -> &'parse ()>, }
1242
1243impl<'src, I: Input<'src>> Cursor<'src, '_, I> {
1244 pub fn inner(&self) -> &I::Cursor {
1246 &self.inner
1247 }
1248}
1249
1250impl<'src, I: Input<'src>> Clone for Cursor<'src, '_, I> {
1251 #[inline(always)]
1252 fn clone(&self) -> Self {
1253 Self {
1254 inner: self.inner.clone(),
1255 phantom: PhantomData,
1256 }
1257 }
1258}
1259
1260impl<'src, I: Input<'src>> Eq for Cursor<'src, '_, I> {}
1261impl<'src, I: Input<'src>> PartialEq for Cursor<'src, '_, I> {
1262 fn eq(&self, other: &Self) -> bool {
1263 I::cursor_location(&self.inner)
1264 .cmp(&I::cursor_location(&other.inner))
1265 .is_eq()
1266 }
1267}
1268
1269impl<'src, I: Input<'src>> PartialOrd for Cursor<'src, '_, I> {
1270 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1271 Some(self.cmp(other))
1272 }
1273}
1274
1275impl<'src, I: Input<'src>> Ord for Cursor<'src, '_, I> {
1276 fn cmp(&self, other: &Self) -> Ordering {
1277 I::cursor_location(&self.inner).cmp(&I::cursor_location(&other.inner))
1278 }
1279}
1280
1281pub(crate) struct Errors<T, E> {
1282 pub(crate) alt: Option<Located<T, E>>,
1283 pub(crate) secondary: Vec<Located<T, E>>,
1284}
1285
1286impl<T, E> Errors<T, E> {
1287 #[inline]
1289 pub(crate) fn secondary_errors_since(&mut self, err_count: usize) -> &mut [Located<T, E>] {
1290 self.secondary.get_mut(err_count..).unwrap_or(&mut [])
1291 }
1292}
1293
1294impl<T, E> Default for Errors<T, E> {
1295 fn default() -> Self {
1296 Self {
1297 alt: None,
1298 secondary: Vec::new(),
1299 }
1300 }
1301}
1302
1303pub(crate) struct InputOwn<'src, 's, I: Input<'src>, E: ParserExtra<'src, I>> {
1306 pub(crate) start: I::Cursor,
1307 pub(crate) cache: I::Cache,
1308 pub(crate) errors: Errors<I::Cursor, E::Error>,
1309 pub(crate) state: MaybeMut<'s, E::State>,
1310 pub(crate) ctx: E::Context,
1311 #[cfg(feature = "memoization")]
1312 pub(crate) memos: HashMap<(usize, usize), Option<Located<I::Cursor, E::Error>>>,
1313}
1314
1315impl<'src, 's, I, E> InputOwn<'src, 's, I, E>
1316where
1317 I: Input<'src>,
1318 E: ParserExtra<'src, I>,
1319{
1320 #[cfg_attr(not(test), allow(dead_code))]
1321 pub(crate) fn new(input: I) -> InputOwn<'src, 's, I, E>
1322 where
1323 E::State: Default,
1324 E::Context: Default,
1325 {
1326 let (start, cache) = input.begin();
1327 InputOwn {
1328 start,
1329 cache,
1330 errors: Errors::default(),
1331 state: MaybeMut::Val(E::State::default()),
1332 ctx: E::Context::default(),
1333 #[cfg(feature = "memoization")]
1334 memos: HashMap::default(),
1335 }
1336 }
1337
1338 pub(crate) fn new_state(input: I, state: &'s mut E::State) -> InputOwn<'src, 's, I, E>
1339 where
1340 E::Context: Default,
1341 {
1342 let (start, cache) = input.begin();
1343 InputOwn {
1344 start,
1345 cache,
1346 errors: Errors::default(),
1347 state: MaybeMut::Ref(state),
1348 ctx: E::Context::default(),
1349 #[cfg(feature = "memoization")]
1350 memos: HashMap::default(),
1351 }
1352 }
1353
1354 pub(crate) fn as_ref_start<'parse>(&'parse mut self) -> InputRef<'src, 'parse, I, E> {
1355 InputRef {
1356 cursor: self.start.clone(),
1357 cache: &mut self.cache,
1358 errors: &mut self.errors,
1359 state: &mut self.state,
1360 ctx: &self.ctx,
1361 #[cfg(feature = "memoization")]
1362 memos: &mut self.memos,
1363 }
1364 }
1365
1366 pub(crate) fn into_errs(self) -> Vec<E::Error> {
1367 self.errors
1368 .secondary
1369 .into_iter()
1370 .map(|err| err.err)
1371 .collect()
1372 }
1373}
1374
1375pub struct InputRef<'src, 'parse, I: Input<'src>, E: ParserExtra<'src, I>> {
1377 cursor: I::Cursor,
1378 pub(crate) cache: &'parse mut I::Cache,
1379 pub(crate) errors: &'parse mut Errors<I::Cursor, E::Error>,
1380 pub(crate) state: &'parse mut E::State,
1381 pub(crate) ctx: &'parse E::Context,
1382 #[cfg(feature = "memoization")]
1383 pub(crate) memos: &'parse mut HashMap<(usize, usize), Option<Located<I::Cursor, E::Error>>>,
1384}
1385
1386impl<'src, 'parse, I: Input<'src>, E: ParserExtra<'src, I>> InputRef<'src, 'parse, I, E> {
1387 #[inline]
1388 pub(crate) fn with_ctx<'sub_parse, EM, O>(
1389 &'sub_parse mut self,
1390 new_ctx: &'sub_parse EM::Context,
1391 f: impl FnOnce(&mut InputRef<'src, 'sub_parse, I, EM>) -> O,
1392 ) -> O
1393 where
1394 'parse: 'sub_parse,
1395 EM: ParserExtra<'src, I, Error = E::Error, State = E::State>,
1396 {
1397 let mut new_inp = InputRef {
1398 cursor: self.cursor.clone(),
1399 cache: self.cache,
1400 state: self.state,
1401 ctx: new_ctx,
1402 errors: self.errors,
1403 #[cfg(feature = "memoization")]
1404 memos: self.memos,
1405 };
1406 let res = f(&mut new_inp);
1407 self.cursor = new_inp.cursor;
1408 res
1409 }
1410
1411 #[inline]
1412 pub(crate) fn with_state<'sub_parse, S, O>(
1413 &'sub_parse mut self,
1414 new_state: &'sub_parse mut S,
1415 f: impl FnOnce(&mut InputRef<'src, 'sub_parse, I, extra::Full<E::Error, S, E::Context>>) -> O,
1416 ) -> O
1417 where
1418 'parse: 'sub_parse,
1419 S: Inspector<'src, I>,
1420 {
1421 let mut new_inp = InputRef {
1422 cursor: self.cursor.clone(),
1423 cache: self.cache,
1424 state: new_state,
1425 ctx: self.ctx,
1426 errors: self.errors,
1427 #[cfg(feature = "memoization")]
1428 memos: self.memos,
1429 };
1430 let res = f(&mut new_inp);
1431 self.cursor = new_inp.cursor;
1432 res
1433 }
1434
1435 #[inline]
1436 pub(crate) fn with_input<'sub_parse, J, F, O>(
1437 &'sub_parse mut self,
1438 start: J::Cursor,
1439 cache: &'sub_parse mut J::Cache,
1440 new_errors: &'sub_parse mut Errors<J::Cursor, F::Error>,
1441 f: impl FnOnce(&mut InputRef<'src, 'sub_parse, J, F>) -> O,
1442 #[cfg(feature = "memoization")] memos: &'sub_parse mut HashMap<
1443 (usize, usize),
1444 Option<Located<J::Cursor, E::Error>>,
1445 >,
1446 ) -> O
1447 where
1448 'parse: 'sub_parse,
1449 J: Input<'src>,
1450 F: ParserExtra<'src, J, State = E::State, Context = E::Context, Error = E::Error>,
1451 {
1452 let mut new_inp = InputRef {
1453 cursor: start,
1454 cache,
1455 state: self.state,
1456 ctx: self.ctx,
1457 errors: new_errors,
1458 #[cfg(feature = "memoization")]
1459 memos,
1460 };
1461 let out = f(&mut new_inp);
1462 self.errors.secondary.extend(
1463 new_inp
1464 .errors
1465 .secondary
1466 .drain(..)
1467 .map(|err| Located::at(self.cursor.clone(), err.err)),
1468 );
1469 if let Some(alt) = new_inp.errors.alt.take() {
1470 self.errors.alt = Some(Located::at(self.cursor.clone(), alt.err));
1471 }
1472 out
1473 }
1474
1475 #[inline(always)]
1479 pub fn cursor(&self) -> Cursor<'src, 'parse, I> {
1480 Cursor {
1481 inner: self.cursor.clone(),
1483 phantom: PhantomData,
1484 }
1485 }
1486
1487 #[inline(always)]
1491 pub fn save(
1492 &self,
1493 ) -> Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint> {
1494 let cursor = self.cursor();
1495 let inspector = self.state.on_save(&cursor);
1496 Checkpoint {
1497 cursor,
1498 err_count: self.errors.secondary.len(),
1499 inspector,
1500 phantom: PhantomData,
1501 }
1502 }
1503
1504 #[inline(always)]
1508 pub fn rewind(
1509 &mut self,
1510 checkpoint: Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>,
1511 ) {
1512 self.errors.secondary.truncate(checkpoint.err_count);
1513 self.state.on_rewind(&checkpoint);
1514 self.cursor = checkpoint.cursor.inner;
1515 }
1516
1517 #[inline(always)]
1518 pub(crate) fn rewind_input(
1519 &mut self,
1520 checkpoint: Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>,
1521 ) {
1522 self.cursor = checkpoint.cursor.inner;
1523 }
1524
1525 #[inline(always)]
1527 pub fn state(&mut self) -> &mut E::State {
1528 self.state
1529 }
1530
1531 #[inline(always)]
1537 pub fn ctx(&self) -> &E::Context {
1538 self.ctx
1539 }
1540
1541 #[inline]
1542 pub(crate) fn skip_while<F: FnMut(&I::Token) -> bool>(&mut self, mut f: F)
1543 where
1544 I: Input<'src>,
1545 {
1546 loop {
1547 let mut cursor = self.cursor.clone();
1548 let token = unsafe { I::next_maybe(self.cache, &mut cursor) };
1550 if token.as_ref().filter(|tok| f((*tok).borrow())).is_none() {
1551 break;
1552 } else {
1553 if let Some(t) = &token {
1554 self.state.on_token(t.borrow());
1555 }
1556 self.cursor = cursor;
1557 }
1558 }
1559 }
1560
1561 #[inline(always)]
1562 pub(crate) fn next_inner(&mut self) -> Option<I::Token>
1563 where
1564 I: ValueInput<'src>,
1565 {
1566 let token = unsafe { I::next(self.cache, &mut self.cursor) };
1568 if let Some(t) = &token {
1569 self.state.on_token(t);
1570 }
1571 token
1572 }
1573
1574 #[inline(always)]
1575 pub(crate) fn next_maybe_inner(&mut self) -> Option<I::MaybeToken> {
1576 let token = unsafe { I::next_maybe(self.cache, &mut self.cursor) };
1578 if let Some(t) = &token {
1579 self.state.on_token(t.borrow());
1580 }
1581 token
1582 }
1583
1584 #[inline(always)]
1585 pub(crate) fn next_ref_inner(&mut self) -> Option<&'src I::Token>
1586 where
1587 I: BorrowInput<'src>,
1588 {
1589 let token = unsafe { I::next_ref(self.cache, &mut self.cursor) };
1591 if let Some(t) = &token {
1592 self.state.on_token(t);
1593 }
1594 token
1595 }
1596
1597 pub fn parse<O, P: Parser<'src, I, O, E>>(&mut self, parser: P) -> Result<O, E::Error> {
1613 match parser.go::<Emit>(self) {
1614 Ok(out) => Ok(out),
1615 Err(()) => Err(self.take_alt().unwrap().err),
1617 }
1618 }
1619
1620 pub fn check<O, P: Parser<'src, I, O, E>>(&mut self, parser: P) -> Result<(), E::Error> {
1626 match parser.go::<Check>(self) {
1627 Ok(()) => Ok(()),
1628 Err(()) => Err(self.take_alt().unwrap().err),
1630 }
1631 }
1632
1633 #[inline(always)]
1643 pub fn next_maybe(&mut self) -> Option<MaybeRef<'src, I::Token>> {
1644 self.next_maybe_inner().map(Into::into)
1645 }
1646
1647 #[inline(always)]
1651 pub fn next(&mut self) -> Option<I::Token>
1652 where
1653 I: ValueInput<'src>,
1654 {
1655 self.next_inner()
1656 }
1657
1658 #[inline(always)]
1662 pub fn next_ref(&mut self) -> Option<&'src I::Token>
1663 where
1664 I: BorrowInput<'src>,
1665 {
1666 self.next_ref_inner()
1667 }
1668
1669 #[inline(always)]
1673 pub fn peek_maybe(&mut self) -> Option<MaybeRef<'src, I::Token>> {
1674 unsafe { I::next_maybe(self.cache, &mut self.cursor.clone()).map(Into::into) }
1676 }
1677
1678 #[inline(always)]
1680 pub fn peek(&mut self) -> Option<I::Token>
1681 where
1682 I: ValueInput<'src>,
1683 {
1684 unsafe { I::next(self.cache, &mut self.cursor.clone()) }
1686 }
1687
1688 #[inline(always)]
1690 pub fn peek_ref(&mut self) -> Option<&'src I::Token>
1691 where
1692 I: BorrowInput<'src>,
1693 {
1694 unsafe { I::next_ref(self.cache, &mut self.cursor.clone()) }
1696 }
1697
1698 #[inline(always)]
1700 pub fn skip(&mut self)
1701 where
1702 I: ValueInput<'src>,
1703 {
1704 let _ = self.next_inner();
1705 }
1706
1707 #[cfg_attr(not(feature = "regex"), allow(dead_code))]
1709 #[inline]
1710 pub fn full_slice(&mut self) -> I::Slice
1711 where
1712 I: SliceInput<'src>,
1713 {
1714 I::full_slice(self.cache)
1715 }
1716
1717 #[inline]
1719 pub fn slice(&mut self, range: Range<&Cursor<'src, 'parse, I>>) -> I::Slice
1720 where
1721 I: SliceInput<'src>,
1722 {
1723 unsafe { I::slice(self.cache, &range.start.inner..&range.end.inner) }
1725 }
1726
1727 #[inline]
1729 pub fn slice_from(&mut self, range: RangeFrom<&Cursor<'src, 'parse, I>>) -> I::Slice
1730 where
1731 I: SliceInput<'src>,
1732 {
1733 unsafe { I::slice_from(self.cache, &range.start.inner..) }
1735 }
1736
1737 #[inline]
1739 pub fn slice_since(&mut self, range: RangeFrom<&Cursor<'src, 'parse, I>>) -> I::Slice
1740 where
1741 I: SliceInput<'src>,
1742 {
1743 unsafe { I::slice(self.cache, &range.start.inner..&self.cursor) }
1745 }
1746
1747 #[cfg_attr(not(feature = "lexical-numbers"), allow(dead_code))]
1748 #[inline(always)]
1749 pub(crate) fn slice_trailing_inner(&mut self) -> I::Slice
1750 where
1751 I: SliceInput<'src>,
1752 {
1753 unsafe { I::slice_from(self.cache, &self.cursor..) }
1755 }
1756
1757 #[inline(always)]
1768 pub fn span_from(&mut self, range: RangeFrom<&Cursor<'src, 'parse, I>>) -> I::Span
1769 where
1770 I: ExactSizeInput<'src>,
1771 {
1772 unsafe { I::span_from(self.cache, &range.start.inner..) }
1775 }
1776
1777 #[inline(always)]
1779 pub fn span_since(&mut self, before: &Cursor<'src, 'parse, I>) -> I::Span {
1780 unsafe { I::span(self.cache, &before.inner..&self.cursor) }
1783 }
1784
1785 #[inline(always)]
1787 #[cfg(any(feature = "regex", feature = "lexical-numbers"))]
1788 pub(crate) unsafe fn skip_bytes(&mut self, skip: usize)
1789 where
1790 I: SliceInput<'src, Cursor = usize>,
1791 {
1792 self.cursor += skip;
1793 }
1794
1795 #[inline]
1798 pub fn emit(&mut self, error: E::Error) {
1799 self.emit_inner(None, error);
1800 }
1801
1802 #[inline]
1804 pub fn emit_at(&mut self, cursor: Cursor<'src, 'parse, I>, error: E::Error) {
1805 self.emit_inner(Some(cursor), error);
1806 }
1807
1808 #[inline]
1809 fn emit_inner(&mut self, cursor: impl Into<Option<Cursor<'src, 'parse, I>>>, error: E::Error) {
1810 let cursor = cursor
1811 .into()
1812 .map(|c| c.inner)
1813 .unwrap_or_else(|| self.cursor.clone());
1814 self.errors.secondary.push(Located::at(cursor, error));
1815 }
1816
1817 #[inline]
1818 pub(crate) fn add_alt<Exp, L>(
1819 &mut self,
1820 expected: Exp,
1821 found: Option<MaybeRef<'src, I::Token>>,
1822 span: I::Span,
1823 ) where
1824 Exp: IntoIterator<Item = L>,
1825 E::Error: LabelError<'src, I, L>,
1826 {
1827 if core::mem::size_of::<E::Error>() == 0 {
1829 self.errors.alt = Some(Located::at(
1830 self.cursor.clone(),
1831 LabelError::expected_found(expected, found, span),
1832 ));
1833 return;
1834 }
1835
1836 let at = &self.cursor;
1837
1838 self.errors.alt = Some(match self.errors.alt.take() {
1840 Some(alt) => match I::cursor_location(&alt.pos).cmp(&I::cursor_location(at)) {
1841 Ordering::Equal => {
1842 Located::at(alt.pos, alt.err.merge_expected_found(expected, found, span))
1843 }
1844 Ordering::Greater => alt,
1845 Ordering::Less => Located::at(
1846 at.clone(),
1847 alt.err.replace_expected_found(expected, found, span),
1848 ),
1849 },
1850 None => Located::at(
1851 at.clone(),
1852 LabelError::expected_found(expected, found, span),
1853 ),
1854 });
1855 }
1856
1857 #[inline]
1858 pub(crate) fn add_alt_err(&mut self, at: &I::Cursor, err: E::Error) {
1859 if core::mem::size_of::<E::Error>() == 0 {
1861 self.errors.alt = Some(Located::at(self.cursor.clone(), err));
1862 return;
1863 }
1864
1865 self.errors.alt = Some(match self.errors.alt.take() {
1867 Some(alt) => match I::cursor_location(&alt.pos).cmp(&I::cursor_location(at)) {
1868 Ordering::Equal => Located::at(alt.pos, alt.err.merge(err)),
1869 Ordering::Greater => alt,
1870 Ordering::Less => Located::at(at.clone(), err),
1871 },
1872 None => Located::at(at.clone(), err),
1873 });
1874 }
1875
1876 pub(crate) fn take_alt(&mut self) -> Option<Located<I::Cursor, E::Error>> {
1878 self.errors.alt.take()
1879 }
1880}
1881
1882pub struct Emitter<E> {
1884 emitted: Vec<E>,
1885}
1886
1887impl<E> Emitter<E> {
1888 #[inline]
1889 pub(crate) fn new() -> Emitter<E> {
1890 Emitter {
1891 emitted: Vec::new(),
1892 }
1893 }
1894
1895 #[inline]
1896 pub(crate) fn errors(self) -> Vec<E> {
1897 self.emitted
1898 }
1899
1900 #[inline]
1902 pub fn emit(&mut self, err: E) {
1903 self.emitted.push(err)
1904 }
1905}
1906
1907pub struct MapExtra<'src, 'b, I: Input<'src>, E: ParserExtra<'src, I>> {
1909 before: &'b I::Cursor,
1910 after: &'b I::Cursor,
1911 cache: &'b mut I::Cache,
1912 state: &'b mut E::State,
1913 ctx: &'b E::Context,
1914 emitted: &'b mut Errors<I::Cursor, E::Error>,
1915}
1916
1917impl<'src, 'b, I: Input<'src>, E: ParserExtra<'src, I>> MapExtra<'src, 'b, I, E> {
1918 #[inline(always)]
1919 pub(crate) fn new<'parse>(
1920 before: &'b Cursor<'src, 'parse, I>,
1921 inp: &'b mut InputRef<'src, 'parse, I, E>,
1922 ) -> Self {
1923 Self {
1924 before: &before.inner,
1925 after: &inp.cursor,
1926 cache: inp.cache,
1927 ctx: inp.ctx,
1928 state: inp.state,
1929 emitted: &mut inp.errors,
1930 }
1931 }
1932
1933 #[inline(always)]
1935 pub fn span(&mut self) -> I::Span {
1936 unsafe { I::span(self.cache, self.before..self.after) }
1940 }
1941
1942 #[inline(always)]
1944 pub fn slice(&mut self) -> I::Slice
1945 where
1946 I: SliceInput<'src>,
1947 {
1948 unsafe { I::slice(self.cache, self.before..self.after) }
1950 }
1951
1952 #[inline(always)]
1954 pub fn state(&mut self) -> &mut E::State {
1955 self.state
1956 }
1957
1958 #[inline(always)]
1960 pub fn ctx(&self) -> &E::Context {
1961 self.ctx
1962 }
1963
1964 pub fn emit(&mut self, err: E::Error) {
1966 self.emitted
1967 .secondary
1968 .push(Located::at(self.before.clone(), err));
1969 }
1970}