Skip to main content

chumsky/
input.rs

1//! Token input streams and tools converting to and from them..
2//!
3//! *“What’s up?” “I don’t know,” said Marvin, “I’ve never been there.”*
4//!
5//! [`Input`] is the primary trait used to feed input data into a chumsky parser. You can create them in a number of
6//! ways: from strings, slices, arrays, etc.
7
8use 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
17/// A trait for types that represents a stream of input tokens. Unlike [`Iterator`], this type
18/// supports backtracking and a few other features required by the crate.
19///
20/// `Input` abstracts over streams which yield tokens by value or reference, and which may or may not have
21/// slices of tokens taken from them for use in parser output. There are multiple traits that inherit from
22/// `Input` and are implemented by types to indicate support for these specific abilities. Various combinators
23/// on the [`Parser`] trait may require that the input type implement one or more of these more specific traits.
24///
25/// Some common input types, and which traits they implement are:
26/// - `&str`: [`SliceInput`], [`StrInput`], [`ValueInput`], [`ExactSizeInput`]
27/// - `&[T]`: [`SliceInput`], [`ValueInput`], [`BorrowInput`], [`ExactSizeInput`]
28/// - `Stream<I>`: [`ValueInput`], [`ExactSizeInput`] if `I: ExactSizeIterator`
29/// - `IterInput<I>`: [`ValueInput`], [`ExactSizeInput`] if `I: ExactSizeIterator`
30pub trait Input<'src>: 'src {
31    /// The type of a span on this input.
32    ///
33    /// Spans allow the output of a parser to be tagged with the location that they appear in the input. This is
34    /// important for tools that want to produce ergonomic error messages for users to read.
35    ///
36    /// For an example of how spans can be used by a parser, see [`Parser::map_with`].
37    ///
38    /// If you want to change the input's span before using it for parsing, see [`Input::map_span`], [`Input::map`],
39    /// and [`Input::with_context`].
40    type Span: Span;
41
42    /// The type of singular tokens generated by the input.
43    ///
44    /// This should be the 'by-value' type of tokens emitted. For example, `<&[T] as Input>::Token = T` and
45    /// `<&str as Input>::Token = char`. Your token does not need to be capable of directly emitting tokens of this
46    /// type (see [`Input::MaybeToken`]).
47    type Token: 'src;
48
49    /// The token type returned by [`Input::next_maybe`], allowing abstracting over by-value and by-reference inputs.
50    ///
51    /// This type should be defines as either
52    ///
53    /// ```ignore
54    /// type MaybeToken = Self::Token;
55    /// ```
56    ///
57    /// or
58    ///
59    /// ```ignore
60    /// type MaybeToken = &'src Self::Token;
61    /// ```
62    ///
63    /// Some inputs (like `&str`) can *only* generate their tokens by-value (a `&str` does not exactly contain any
64    /// addressable `char`s due to the nature of [UTF-8 encoding](https://en.wikipedia.org/wiki/UTF-8#Description)).
65    ///
66    /// Other inputs (like `&[T]`) can *only* generate their tokens by-reference (`T` might not implement `Clone` or
67    /// might be expensive to clone, but we still want to be able to parse slices containing `T`s).
68    ///
69    /// Combinators like [`just`] need to be able to work with both categories of input, and `MaybeToken` is the glue
70    /// that allows that to happen. It does this by providing a way of expressing that a token is 'maybe borrowed', in
71    /// a similar way to [`std::borrow::Cow`] or [`std::convert::AsRef`], via the [`IntoMaybe`] trait.
72    ///
73    /// It is good practice for inputs that generate their tokens by-value to implement [`ValueInput`], and inputs that
74    /// generate their tokens by-reference to implement [`BorrowInput`]. Additionally, it is perfectly fine for inputs
75    /// to implement *both* of these traits at once.
76    type MaybeToken: IntoMaybe<'src, Self::Token>;
77
78    /// The type used to keep track of the current location in the stream.
79    ///
80    /// Chumsky regularly copies cursors around to perform input rewinding (for the sake of
81    /// [backtracking](https://en.wikipedia.org/wiki/Backtracking), and other things). Therefore, cursors should be
82    /// cheap to clone and should have trivial [`Drop`] logic where possible. If your input type needs to perform more
83    /// substantial bookkeeping, you should use [`Input::Cache`] to store heavy-weight input state and keep cursors as
84    /// lightweight as possible, such as simple indices into the cache data.
85    type Cursor: Clone;
86
87    /// A type that contains cached or constant data pertaining to an input.
88    ///
89    /// Most input types (such as `&[T]` and `&str`) don't need a cache because they're just pulling tokens straight
90    /// out of memory. If your input type falls into a similar category, `()` can be used. However, some inputs -
91    /// like [`Stream`] - need to do extra bookkeeping to support backtracking, and a cache facilitates this behaviour.
92    type Cache;
93
94    /// Create an initial cursor and cache at the start of the input.
95    fn begin(self) -> (Self::Cursor, Self::Cache);
96
97    /// Return the 'location' associated with the given cursor.
98    ///
99    /// Unfortunately, 'location' is not yet a well-defined concept. It is used internally by chumsky to compare the
100    /// location of errors and input tokens, but there is no particular requirement that cursor locations be contiguous
101    /// or start from zero - chumsky only *compares* locations with one-another.
102    ///
103    /// We would recommend that you make the cursor location monotonic. This means that each successive cursor position
104    /// should have an increasing location. Most of the time using an index or increasing memory address is perfectly
105    /// sufficient. Some inputs might also choose to use the start offset of a span, although we'd recommend against
106    /// this if your input's spans are non-continuous.
107    ///
108    /// There are no particular safety requirements placed on this function. You can probably cause chumsky to generate
109    /// absurd error messages or take bizarre parse paths through the input [if you abuse it](https://xkcd.com/221/),
110    /// however.
111    fn cursor_location(cursor: &Self::Cursor) -> usize;
112
113    /// Pull the next token, if any, from the input.
114    ///
115    /// For alternatives with stronger guarantees, see [`ValueInput::next`] and `BorrowInput::next_ref`.
116    ///
117    /// The vast majority of input types can implement either [`ValueInput`] or [`BorrowInput`], so the implementation
118    /// of this function usually just calls their corresponding `next` function.
119    ///
120    /// # Safety
121    ///
122    /// `cursor` must be generated by `Input::begin`, and must not be shared between multiple inputs.
123    unsafe fn next_maybe(
124        cache: &mut Self::Cache,
125        cursor: &mut Self::Cursor,
126    ) -> Option<Self::MaybeToken>;
127
128    /// Create a span going from the start cursor to the end cursor (exclusive).
129    ///
130    /// # Safety
131    ///
132    /// As with [`Input::next_maybe`], the cursors passed to this function must be generated by [`Input::begin`] and
133    /// must not be shared between multiple inputs.
134    unsafe fn span(cache: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span;
135
136    // /// Split an input that produces tokens of type `(T, S)` into one that produces tokens of type `T` and spans of
137    // /// type `S`.
138    // ///
139    // /// This is commonly required for lexers that generate token-span tuples. For example, `logos`'
140    // /// [`SpannedIter`](https://docs.rs/logos/0.12.0/logos/struct.Lexer.html#method.spanned) lexer generates such
141    // /// pairs.
142    // ///
143    // /// Also required is an 'End Of Input' (EoI) span. This span is arbitrary, but is used by the input to produce
144    // /// sensible spans that extend to the end of the input or are zero-width. Most implementations simply use some
145    // /// equivalent of `len..len` (i.e: a span where both the start and end cursors are set to the end of the input).
146    // /// However, what you choose for this span is up to you: but consider that the context, start, and end of the span
147    // /// will be recombined to create new spans as required by the parser.
148    // ///
149    // /// Although `Spanned` does implement [`BorrowInput`], please be aware that, as you might anticipate, the slices
150    // /// will be those of the original input (usually `&[(T, S)]`) and not `&[T]` so as to avoid the need to copy
151    // /// around sections of the input.
152    // fn spanned<T, S>(self, eoi: S) -> SpannedInput<T, S, Self>
153    // where
154    //     Self: Input<'src, Token = (T, S)> + Sized,
155    //     T: 'src,
156    //     S: Span + Clone + 'src,
157    // {
158    //     SpannedInput {
159    //         input: self,
160    //         eoi,
161    //         phantom: PhantomData,
162    //     }
163    // }
164
165    /// Add extra context within spans generated by this input.
166    ///
167    /// This is useful if you wish to include extra context that applies to all spans emitted during a parse, such as
168    /// an identifier that corresponds to the file the spans originated from.
169    ///
170    /// Returns spans containing your provided context as the Span::Context
171    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    /// Map tokens of the input into separate token and span types, using the provided function.
183    ///
184    /// This is useful when you want your parser to consume tokens that have their span carried with them (lexers will
185    /// usually emit such tokens).
186    ///
187    /// Also required is an 'End of Input' (EoI) span. This span is arbitrary, but is used by the input to produce
188    /// sensible spans that extend to the end of the input or are zero-width. Most implementations simply use some
189    /// equivalent of `len..len` (i.e: a span where both the start and end cursors are set to the end of the input).
190    /// However, what you choose for this span is up to you: but consider that the context, start, and end of the span
191    /// will be recombined to create new spans as required by the parser.
192    ///
193    /// Although `MappedInput` does implement [`SliceInput`], please be aware that, as you might anticipate, the slices
194    /// will be those of the original input and not `&[T]`, to avoid the need to copy around sections of the input.
195    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    /// Take an input (such as a slice) with token type `(T, S)` and turns it into one that produces tokens of type `T` and spans of type `S`.
216    ///
217    /// This is useful when you can't make your parser generic over an input type and need to name the input type, such as with [`Parser::nested_in`].
218    ///
219    /// This is largely an ergonomic convenience over calling [`input.map(eoi, |(t, s)| (t, s))`](`Input::map`).
220    ///
221    /// # Examples
222    /// ```
223    /// use chumsky::{input::{Input as _, MappedInput}, prelude::*};
224    ///
225    /// enum TokenTree<'src> { Num(i64), Sum(&'src [(Self, SimpleSpan)]) }
226    ///
227    /// type Input<'src> = MappedInput<'src, TokenTree<'src>, SimpleSpan, &'src [(TokenTree<'src>, SimpleSpan)]>;
228    ///
229    /// // This parser will parse a recursive input composed of `TokenTree`s.
230    /// fn eval<'src>() -> impl Parser<'src, Input<'src>, i64> {
231    ///     recursive(|tree| {
232    ///         let sum = tree.repeated().fold(0, |sum, x| sum + x)
233    ///             .nested_in(select_ref! { TokenTree::Sum(xs) = e => xs.split_token_span(e.span()) });
234    ///     
235    ///         select_ref! { TokenTree::Num(x) => *x }.or(sum)
236    ///     })
237    /// }
238    ///
239    /// let example = [
240    ///     (TokenTree::Sum(&[
241    ///         (TokenTree::Num(1), SimpleSpan::from(0..1)), // 1
242    ///         (TokenTree::Sum(&[
243    ///             (TokenTree::Num(2), SimpleSpan::from(2..3)), // 2
244    ///             (TokenTree::Num(3), SimpleSpan::from(4..5)), // 3
245    ///         ]), SimpleSpan::from(2..5))
246    ///     ]), SimpleSpan::from(0..5)),
247    /// ];
248    ///
249    /// assert_eq!(eval().parse(example[..].split_token_span((0..4).into())).into_result(), Ok(6));
250    /// ```
251    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    /// Take an input (such as a slice) with token type `T` where `T` is a spanned type, and split it into its inner value and token.
261    ///
262    /// See the documentation for [`Input::split_token_span`], which is nearly identical in concept.
263    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    /// Map the spans output for this input to a different output span.
273    ///
274    /// This is useful if you wish to include extra context that applies to all spans emitted during a parse, such as
275    /// an identifier that corresponds to the file the spans originated from.
276    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
289/// Implemented by inputs that have a known size.
290///
291/// In this case, 'size' means 'number of generated tokens/spans'. You can think of this trait as filling the same
292/// niche as [`ExactSizeIterator`].
293pub trait ExactSizeInput<'src>: Input<'src> {
294    /// Get a span from a start cursor to the end of the input.
295    ///
296    /// # Safety
297    ///
298    /// As with functions on [`Input`], the cursors provided must be generated by this input.
299    unsafe fn span_from(cache: &mut Self::Cache, range: RangeFrom<&Self::Cursor>) -> Self::Span;
300}
301
302/// Implemented by inputs from which slices can be extracted.
303///
304/// Note that there's nothing to suggest that the slice *needs* to contain instances of the token type: it's quite
305/// valid for an input to produce symbolic tokens, but slices corresponding to the original source code. However, it is
306/// conventional to do so.
307pub trait SliceInput<'src>: ExactSizeInput<'src> {
308    /// The unsized slice type of this input. For [`&str`] it's `&str`, and for [`&[T]`] it will be `&[T]`.
309    type Slice: Clone;
310
311    /// Get the full slice of the input
312    ///
313    /// # Safety
314    ///
315    /// As with functions on [`Input`], the cursors provided must be generated by this input.
316    fn full_slice(cache: &mut Self::Cache) -> Self::Slice;
317
318    /// Get a slice from a start and end cursor
319    ///
320    /// # Safety
321    ///
322    /// As with functions on [`Input`], the cursors provided must be generated by this input.
323    unsafe fn slice(cache: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice;
324
325    /// Get a slice from a start cursor to the end of the input
326    ///
327    /// # Safety
328    ///
329    /// As with functions on [`Input`], the cursors provided must be generated by this input.
330    unsafe fn slice_from(cache: &mut Self::Cache, from: RangeFrom<&Self::Cursor>) -> Self::Slice;
331}
332
333/// A trait for types that represent string-like streams of input tokens.
334///
335/// Currently, this trait is only implemented by [`&[u8]` and `&str` (and other inputs that behave like them).
336///
337/// This trait is sealed because future versions of chumsky might place extra requirements upon inputs that implement
338/// it.
339pub 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
347/// Implemented by inputs that can have produce tokens by value.
348pub trait ValueInput<'src>: Input<'src> {
349    /// Get the next cursor from the provided one, and the next token if it exists
350    ///
351    /// # Safety
352    ///
353    /// As with functions on [`Input`], the cursors provided must be generated by this input.
354    unsafe fn next(cache: &mut Self::Cache, cursor: &mut Self::Cursor) -> Option<Self::Token>;
355}
356
357/// Implemented by inputs that can have tokens borrowed from them.
358///
359/// The borrowed tokens have the same lifetime as the original input.
360pub trait BorrowInput<'src>: Input<'src> {
361    /// Borrowed version of [`ValueInput::next`] with the same safety requirements.
362    ///
363    /// # Safety
364    ///
365    /// As with functions on [`Input`], the cursors provided must be generated by this input.
366    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            // SAFETY: `cursor < self.len()` above guarantees cursor is in-bounds
398            //         We only ever return cursors that are at a character boundary
399            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| core::ascii::Char::from_u8(e).unwrap_or(AsciiChar::Substitute).to_char())
510            .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/// See [`Input::map`].
641#[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/// An input wrapper that maps the span type of your input
830/// into your custom span [`Input::map_span`].
831#[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/// An input wrapper that returns a custom span, with the user-defined context
979/// contained in the Span::Context. See [`Input::with_context`].
980#[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/// Input type which supports seekable readers. Uses a [`BufReader`] internally to buffer input and
1121/// avoid unnecessary IO calls.
1122///
1123/// Only available with the `std` feature
1124#[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    /// Create a new `IoReader` from a seekable reader.
1133    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
1200/// Represents a location in an input that can be rewound to.
1201///
1202/// Checkpoints can be created with [`InputRef::save`] and rewound to with [`InputRef::rewind`].
1203pub 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 ()>, // Invariance
1208}
1209
1210impl<'src, 'parse, I: Input<'src>, C> Checkpoint<'src, 'parse, I, C> {
1211    /// Get the [`Cursor`] that this checkpoint corresponds to.
1212    pub fn cursor(&self) -> &Cursor<'src, 'parse, I> {
1213        &self.cursor
1214    }
1215
1216    /// Get the [`Checkpoint`][Inspector::Checkpoint] that this marker corresponds to.
1217    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/// Represents a location in an input.
1235///
1236/// If you to rewind to an old input location, see [`Checkpoint`].
1237#[repr(transparent)]
1238pub struct Cursor<'src, 'parse, I: Input<'src>> {
1239    pub(crate) inner: I::Cursor,
1240    phantom: PhantomData<fn(&'parse ()) -> &'parse ()>, // Invariance
1241}
1242
1243impl<'src, I: Input<'src>> Cursor<'src, '_, I> {
1244    /// Get the input's internal cursor.
1245    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    /// Returns a slice of the secondary errors (if any) have been emitted since the given checkpoint was created.
1288    #[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
1303/// Internal type representing the owned parts of an input - used at the top level by a call to
1304/// `parse`.
1305pub(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
1375/// Internal type representing an input as well as all the necessary context for parsing.
1376pub 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    /// Get the internal cursor of the input at this moment in time.
1476    ///
1477    /// Can be used for generating spans or slices. See [`InputRef::span_from`] and [`InputRef::slice`].
1478    #[inline(always)]
1479    pub fn cursor(&self) -> Cursor<'src, 'parse, I> {
1480        Cursor {
1481            // TODO: Find ways to avoid this clone, if possible
1482            inner: self.cursor.clone(),
1483            phantom: PhantomData,
1484        }
1485    }
1486
1487    /// Save the current parse state as a [`Checkpoint`].
1488    ///
1489    /// You can rewind back to this state later with [`InputRef::rewind`].
1490    #[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    /// Reset the parse state to that represented by the given [`Checkpoint`].
1505    ///
1506    /// You can create a checkpoint with which to perform rewinding using [`InputRef::save`].
1507    #[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    /// Get a mutable reference to the state associated with the current parse.
1526    #[inline(always)]
1527    pub fn state(&mut self) -> &mut E::State {
1528        self.state
1529    }
1530
1531    /// Get a reference to the context fed to the current parser.
1532    ///
1533    /// See [`ConfigParser::configure`], [`Parser::ignore_with_ctx`] and
1534    /// [`Parser::then_with_ctx`] for more information about context-sensitive
1535    /// parsing.
1536    #[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            // SAFETY: cursor was generated by previous call to `Input::next`
1549            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        // SAFETY: cursor was generated by previous call to `Input::next`
1567        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        // SAFETY: cursor was generated by previous call to `Input::next`
1577        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        // SAFETY: cursor was generated by previous call to `Input::next`
1590        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    /// Attempt to parse this input using the given parser.
1598    ///
1599    /// # Important Notice
1600    ///
1601    /// Parsers that return `Err(...)` are permitted to leave the input in an **unspecified** (but not
1602    /// [undefined](https://en.wikipedia.org/wiki/Undefined_behavior)) state.
1603    ///
1604    /// The only well-specified action you are permitted to perform on the input after an error has occurred is
1605    /// rewinding to a checkpoint created *before* the error occurred via [`InputRef::rewind`].
1606    ///
1607    /// This state is not consistent between releases of chumsky, compilations of the final binary, or even invocations
1608    /// of the parser. You should not rely on this state for anything, and choosing to rely on it means that your
1609    /// parser may break in unexpected ways at any time.
1610    ///
1611    /// You have been warned.
1612    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            // Can't fail!
1616            Err(()) => Err(self.take_alt().unwrap().err),
1617        }
1618    }
1619
1620    /// A check-only version of [`InputRef::parse`].
1621    ///
1622    /// # Import Notice
1623    ///
1624    /// See [`InputRef::parse`] about unspecified behavior associated with this function.
1625    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            // Can't fail!
1629            Err(()) => Err(self.take_alt().unwrap().err),
1630        }
1631    }
1632
1633    /// Get the next token in the input. Returns `None` if the end of the input has been reached.
1634    ///
1635    /// This function is more flexible than either [`InputRef::next`] or [`InputRef::next_ref`] since it
1636    /// only requires that the [`Input`] trait be implemented for `I` (instead of either [`ValueInput`] or
1637    /// [`BorrowInput`]). However, that increased flexibility for the end user comes with a trade-off for the
1638    /// implementation: this function returns a [`MaybeRef<I::Token>`] that provides only a temporary reference to the
1639    /// token.
1640    ///
1641    /// See [`InputRef::next_ref`] if you want get a reference to the next token instead.
1642    #[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    /// Get the next token in the input by value. Returns `None` if the end of the input has been reached.
1648    ///
1649    /// See [`InputRef::next_ref`] if you want get a reference to the next token instead.
1650    #[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    /// Get a reference to the next token in the input. Returns `None` if the end of the input has been reached.
1659    ///
1660    /// See [`InputRef::next`] if you want get the next token by value instead.
1661    #[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    /// Peek the next token in the input. Returns `None` if the end of the input has been reached.
1670    ///
1671    /// See [`InputRef::next_maybe`] for more information about what this function guarantees.
1672    #[inline(always)]
1673    pub fn peek_maybe(&mut self) -> Option<MaybeRef<'src, I::Token>> {
1674        // SAFETY: cursor was generated by previous call to `Input::next`
1675        unsafe { I::next_maybe(self.cache, &mut self.cursor.clone()).map(Into::into) }
1676    }
1677
1678    /// Peek the next token in the input. Returns `None` if the end of the input has been reached.
1679    #[inline(always)]
1680    pub fn peek(&mut self) -> Option<I::Token>
1681    where
1682        I: ValueInput<'src>,
1683    {
1684        // SAFETY: cursor was generated by previous call to `Input::next`
1685        unsafe { I::next(self.cache, &mut self.cursor.clone()) }
1686    }
1687
1688    /// Peek the next token in the input. Returns `None` if the end of the input has been reached.
1689    #[inline(always)]
1690    pub fn peek_ref(&mut self) -> Option<&'src I::Token>
1691    where
1692        I: BorrowInput<'src>,
1693    {
1694        // SAFETY: cursor was generated by previous call to `Input::next`
1695        unsafe { I::next_ref(self.cache, &mut self.cursor.clone()) }
1696    }
1697
1698    /// Skip the next token in the input.
1699    #[inline(always)]
1700    pub fn skip(&mut self)
1701    where
1702        I: ValueInput<'src>,
1703    {
1704        let _ = self.next_inner();
1705    }
1706
1707    /// Get full slice of raw input.
1708    #[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    /// Get a slice of the input that covers the given cursor range.
1718    #[inline]
1719    pub fn slice(&mut self, range: Range<&Cursor<'src, 'parse, I>>) -> I::Slice
1720    where
1721        I: SliceInput<'src>,
1722    {
1723        // SAFETY: cursor was generated by previous call to `Input::next`
1724        unsafe { I::slice(self.cache, &range.start.inner..&range.end.inner) }
1725    }
1726
1727    /// Get a slice of the input that covers the given cursor range.
1728    #[inline]
1729    pub fn slice_from(&mut self, range: RangeFrom<&Cursor<'src, 'parse, I>>) -> I::Slice
1730    where
1731        I: SliceInput<'src>,
1732    {
1733        // SAFETY: cursor was generated by previous call to `Input::next`
1734        unsafe { I::slice_from(self.cache, &range.start.inner..) }
1735    }
1736
1737    /// Get a slice of the input that covers the given cursor range.
1738    #[inline]
1739    pub fn slice_since(&mut self, range: RangeFrom<&Cursor<'src, 'parse, I>>) -> I::Slice
1740    where
1741        I: SliceInput<'src>,
1742    {
1743        // SAFETY: cursor was generated by previous call to `Input::next`
1744        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        // SAFETY: cursor was generated by previous call to `Input::next`
1754        unsafe { I::slice_from(self.cache, &self.cursor..) }
1755    }
1756
1757    // /// Get a span over the input that covers the given cursor range.
1758    // #[inline(always)]
1759    // pub fn span(&self, range: Range<&Cursor<'src, 'parse, I>>) -> I::Span {
1760    //     // SAFETY: `Cursor` is invariant over 'parse, so we know that this cursor came from the same input
1761    //     // See `https://plv.mpi-sws.org/rustbelt/ghostcell/`
1762    //     unsafe { I::span(self.cache, &range.start.inner..&range.end.inner) }
1763    // }
1764
1765    /// Get a span over the input that goes from the given cursor to the end of the input.
1766    // TODO: Unify with `InputRef::span`
1767    #[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        // SAFETY: `Cursor` is invariant over 'parse, so we know that this cursor came from the same input
1773        // See `https://plv.mpi-sws.org/rustbelt/ghostcell/`
1774        unsafe { I::span_from(self.cache, &range.start.inner..) }
1775    }
1776
1777    /// Generate a span that extends from the provided [`Cursor`] to the current input position.
1778    #[inline(always)]
1779    pub fn span_since(&mut self, before: &Cursor<'src, 'parse, I>) -> I::Span {
1780        // SAFETY: `Cursor` is invariant over 'parse, so we know that this cursor came from the same input
1781        // See `https://plv.mpi-sws.org/rustbelt/ghostcell/`
1782        unsafe { I::span(self.cache, &before.inner..&self.cursor) }
1783    }
1784
1785    /// SAFETY: Previous cursor + skip must not exceed length
1786    #[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    /// Emits a non-fatal error at the current position. This is equivalent to
1796    /// `emit_at(self.cursor(), error)`.
1797    #[inline]
1798    pub fn emit(&mut self, error: E::Error) {
1799        self.emit_inner(None, error);
1800    }
1801
1802    /// Emits a non-fatal error at the given cursor position.
1803    #[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        // Fast path: if the error doesn't carry meaningful information, avoid unnecessary decision-making!
1828        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        // Prioritize errors before choosing whether to generate the alt (avoids unnecessary error creation)
1839        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        // Fast path: if the error doesn't carry meaningful information, avoid unnecessary decision-making!
1860        if core::mem::size_of::<E::Error>() == 0 {
1861            self.errors.alt = Some(Located::at(self.cursor.clone(), err));
1862            return;
1863        }
1864
1865        // Prioritize errors
1866        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    // Take the alt error, if one exists
1877    pub(crate) fn take_alt(&mut self) -> Option<Located<I::Cursor, E::Error>> {
1878        self.errors.alt.take()
1879    }
1880}
1881
1882/// Struct used in [`Parser::validate`] to collect user-emitted errors
1883pub 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    /// Emit a non-fatal error
1901    #[inline]
1902    pub fn emit(&mut self, err: E) {
1903        self.emitted.push(err)
1904    }
1905}
1906
1907/// See [`Parser::map_with`].
1908pub 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    /// Get the span corresponding to the output.
1934    #[inline(always)]
1935    pub fn span(&mut self) -> I::Span {
1936        // SAFETY: The cursors both came from the same input
1937        // TODO: Should this make `MapExtra::new` unsafe? Probably, but it's an internal API and we simply wouldn't
1938        // ever abuse it in this way, even accidentally.
1939        unsafe { I::span(self.cache, self.before..self.after) }
1940    }
1941
1942    /// Get the slice corresponding to the output.
1943    #[inline(always)]
1944    pub fn slice(&mut self) -> I::Slice
1945    where
1946        I: SliceInput<'src>,
1947    {
1948        // SAFETY: The cursors both came from the same input
1949        unsafe { I::slice(self.cache, self.before..self.after) }
1950    }
1951
1952    /// Get the parser state.
1953    #[inline(always)]
1954    pub fn state(&mut self) -> &mut E::State {
1955        self.state
1956    }
1957
1958    /// Get the current parser context.
1959    #[inline(always)]
1960    pub fn ctx(&self) -> &E::Context {
1961        self.ctx
1962    }
1963
1964    /// Emits an non-fatal error.
1965    pub fn emit(&mut self, err: E::Error) {
1966        self.emitted
1967            .secondary
1968            .push(Located::at(self.before.clone(), err));
1969    }
1970}