Skip to main content

Input

Trait Input 

Source
pub trait Input<'src>: 'src {
    type Span: Span;
    type Token: 'src;
    type MaybeToken: IntoMaybe<'src, Self::Token>;
    type Cursor: Clone;
    type Cache;

    // Required methods
    fn begin(self) -> (Self::Cursor, Self::Cache);
    fn cursor_location(cursor: &Self::Cursor) -> usize;
    unsafe fn next_maybe(
        cache: &mut Self::Cache,
        cursor: &mut Self::Cursor,
    ) -> Option<Self::MaybeToken>;
    unsafe fn span(
        cache: &mut Self::Cache,
        range: Range<&Self::Cursor>,
    ) -> Self::Span;

    // Provided methods
    fn with_context<S>(
        self,
        context: <S as Span>::Context,
    ) -> WithContext<S, Self>
       where S: Span,
             Self: Sized { ... }
    fn map<T, S, F>(self, eoi: S, f: F) -> MappedInput<T, S, Self, F>
       where Self: Sized,
             F: Fn(Self::MaybeToken) -> (<Self::MaybeToken as IntoMaybe<'src, Self::Token>>::Proj<T>, <Self::MaybeToken as IntoMaybe<'src, Self::Token>>::Proj<S>) + 'src,
             T: 'src,
             S: Span + 'src { ... }
    fn map_span<S, F>(self, map_fn: F) -> MappedSpan<S, Self, F>
       where S: Span,
             Self: Sized + Input<'src>,
             F: Fn(Self::Span) -> S { ... }
}
Expand description

A trait for types that represents a stream of input tokens. Unlike Iterator, this type supports backtracking and a few other features required by the crate.

Input abstracts over streams which yield tokens by value or reference, and which may or may not have slices of tokens taken from them for use in parser output. There are multiple traits that inherit from Input and are implemented by types to indicate support for these specific abilities. Various combinators on the Parser trait may require that the input type implement one or more of these more specific traits.

Some common input types, and which traits they implement are:

Required Associated Types§

Source

type Span: Span

The type of a span on this input.

Spans allow the output of a parser to be tagged with the location that they appear in the input. This is important for tools that want to produce ergonomic error messages for users to read.

For an example of how spans can be used by a parser, see Parser::map_with.

If you want to change the input’s span before using it for parsing, see Input::map_span, Input::map, and Input::with_context.

Source

type Token: 'src

The type of singular tokens generated by the input.

This should be the ‘by-value’ type of tokens emitted. For example, <&[T] as Input>::Token = T and <&str as Input>::Token = char. Your token does not need to be capable of directly emitting tokens of this type (see Input::MaybeToken).

Source

type MaybeToken: IntoMaybe<'src, Self::Token>

The token type returned by Input::next_maybe, allowing abstracting over by-value and by-reference inputs.

This type should be defines as either

ⓘ
type MaybeToken = Self::Token;

or

ⓘ
type MaybeToken = &'src Self::Token;

Some inputs (like &str) can only generate their tokens by-value (a &str does not exactly contain any addressable chars due to the nature of UTF-8 encoding).

Other inputs (like &[T]) can only generate their tokens by-reference (T might not implement Clone or might be expensive to clone, but we still want to be able to parse slices containing Ts).

Combinators like just need to be able to work with both categories of input, and MaybeToken is the glue that allows that to happen. It does this by providing a way of expressing that a token is ‘maybe borrowed’, in a similar way to [std::borrow::Cow] or [std::convert::AsRef], via the IntoMaybe trait.

It is good practice for inputs that generate their tokens by-value to implement ValueInput, and inputs that generate their tokens by-reference to implement BorrowInput. Additionally, it is perfectly fine for inputs to implement both of these traits at once.

Source

type Cursor: Clone

The type used to keep track of the current location in the stream.

Chumsky regularly copies cursors around to perform input rewinding (for the sake of backtracking, and other things). Therefore, cursors should be cheap to clone and should have trivial Drop logic where possible. If your input type needs to perform more substantial bookkeeping, you should use Input::Cache to store heavy-weight input state and keep cursors as lightweight as possible, such as simple indices into the cache data.

Source

type Cache

A type that contains cached or constant data pertaining to an input.

Most input types (such as &[T] and &str) don’t need a cache because they’re just pulling tokens straight out of memory. If your input type falls into a similar category, () can be used. However, some inputs - like Stream - need to do extra bookkeeping to support backtracking, and a cache facilitates this behaviour.

Required Methods§

Source

fn begin(self) -> (Self::Cursor, Self::Cache)

Create an initial cursor and cache at the start of the input.

Source

fn cursor_location(cursor: &Self::Cursor) -> usize

Return the ‘location’ associated with the given cursor.

Unfortunately, ‘location’ is not yet a well-defined concept. It is used internally by chumsky to compare the location of errors and input tokens, but there is no particular requirement that cursor locations be contiguous or start from zero - chumsky only compares locations with one-another.

We would recommend that you make the cursor location monotonic. This means that each successive cursor position should have an increasing location. Most of the time using an index or increasing memory address is perfectly sufficient. Some inputs might also choose to use the start offset of a span, although we’d recommend against this if your input’s spans are non-continuous.

There are no particular safety requirements placed on this function. You can probably cause chumsky to generate absurd error messages or take bizarre parse paths through the input if you abuse it, however.

Source

unsafe fn next_maybe( cache: &mut Self::Cache, cursor: &mut Self::Cursor, ) -> Option<Self::MaybeToken>

Pull the next token, if any, from the input.

For alternatives with stronger guarantees, see ValueInput::next and BorrowInput::next_ref.

The vast majority of input types can implement either ValueInput or BorrowInput, so the implementation of this function usually just calls their corresponding next function.

§Safety

cursor must be generated by Input::begin, and must not be shared between multiple inputs.

Source

unsafe fn span( cache: &mut Self::Cache, range: Range<&Self::Cursor>, ) -> Self::Span

Create a span going from the start cursor to the end cursor (exclusive).

§Safety

As with Input::next_maybe, the cursors passed to this function must be generated by Input::begin and must not be shared between multiple inputs.

Provided Methods§

Source

fn with_context<S>(self, context: <S as Span>::Context) -> WithContext<S, Self>
where S: Span, Self: Sized,

Add extra context within spans generated by this input.

This is useful if you wish to include extra context that applies to all spans emitted during a parse, such as an identifier that corresponds to the file the spans originated from.

Returns spans containing your provided context as the Span::Context

Source

fn map<T, S, F>(self, eoi: S, f: F) -> MappedInput<T, S, Self, F>
where Self: Sized, F: Fn(Self::MaybeToken) -> (<Self::MaybeToken as IntoMaybe<'src, Self::Token>>::Proj<T>, <Self::MaybeToken as IntoMaybe<'src, Self::Token>>::Proj<S>) + 'src, T: 'src, S: Span + 'src,

Map tokens of the input into separate token and span types, using the provided function.

This is useful when you want your parser to consume tokens that have their span carried with them (lexers will usually emit such tokens).

Also required is an ‘End of Input’ (EoI) span. This span is arbitrary, but is used by the input to produce sensible spans that extend to the end of the input or are zero-width. Most implementations simply use some equivalent of len..len (i.e: a span where both the start and end cursors are set to the end of the input). However, what you choose for this span is up to you: but consider that the context, start, and end of the span will be recombined to create new spans as required by the parser.

Although MappedInput does implement SliceInput, please be aware that, as you might anticipate, the slices will be those of the original input and not &[T], to avoid the need to copy around sections of the input.

Source

fn map_span<S, F>(self, map_fn: F) -> MappedSpan<S, Self, F>
where S: Span, Self: Sized + Input<'src>, F: Fn(Self::Span) -> S,

Map the spans output for this input to a different output span.

This is useful if you wish to include extra context that applies to all spans emitted during a parse, such as an identifier that corresponds to the file the spans originated from.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'src, T, const N: usize> Input<'src> for &'src [T; N]
where T: 'src,

Source§

type Cursor = usize

Source§

type Span = SimpleSpan

Source§

type Token = T

Source§

type MaybeToken = &'src T

Source§

type Cache = &'src [T; N]

Source§

fn begin( self, ) -> (<&'src [T; N] as Input<'src>>::Cursor, <&'src [T; N] as Input<'src>>::Cache)

Source§

fn cursor_location(cursor: &<&'src [T; N] as Input<'src>>::Cursor) -> usize

Source§

unsafe fn next_maybe( this: &mut <&'src [T; N] as Input<'src>>::Cache, cursor: &mut <&'src [T; N] as Input<'src>>::Cursor, ) -> Option<<&'src [T; N] as Input<'src>>::MaybeToken>

Source§

unsafe fn span( _this: &mut <&'src [T; N] as Input<'src>>::Cache, range: Range<&<&'src [T; N] as Input<'src>>::Cursor>, ) -> <&'src [T; N] as Input<'src>>::Span

Source§

impl<'src, T> Input<'src> for &'src [T]

Source§

type Cursor = usize

Source§

type Span = SimpleSpan

Source§

type Token = T

Source§

type MaybeToken = &'src T

Source§

type Cache = &'src [T]

Source§

fn begin( self, ) -> (<&'src [T] as Input<'src>>::Cursor, <&'src [T] as Input<'src>>::Cache)

Source§

fn cursor_location(cursor: &<&'src [T] as Input<'src>>::Cursor) -> usize

Source§

unsafe fn next_maybe( this: &mut <&'src [T] as Input<'src>>::Cache, cursor: &mut <&'src [T] as Input<'src>>::Cursor, ) -> Option<<&'src [T] as Input<'src>>::MaybeToken>

Source§

unsafe fn span( _this: &mut <&'src [T] as Input<'src>>::Cache, range: Range<&<&'src [T] as Input<'src>>::Cursor>, ) -> <&'src [T] as Input<'src>>::Span ⓘ

Source§

impl<'src> Input<'src> for &'src str

Source§

type Cursor = usize

Source§

type Span = SimpleSpan

Source§

type Token = char

Source§

type MaybeToken = char

Source§

type Cache = &'src str

Source§

fn begin( self, ) -> (<&'src str as Input<'src>>::Cursor, <&'src str as Input<'src>>::Cache)

Source§

fn cursor_location(cursor: &<&'src str as Input<'src>>::Cursor) -> usize

Source§

unsafe fn next_maybe( this: &mut <&'src str as Input<'src>>::Cache, cursor: &mut <&'src str as Input<'src>>::Cursor, ) -> Option<<&'src str as Input<'src>>::MaybeToken>

Source§

unsafe fn span( _this: &mut <&'src str as Input<'src>>::Cache, range: Range<&<&'src str as Input<'src>>::Cursor>, ) -> <&'src str as Input<'src>>::Span

Implementors§

Source§

impl<'a, I> Input<'a> for Stream<I>
where I: Iterator + 'a, <I as Iterator>::Item: Clone,

Source§

impl<'src, I, T, S> Input<'src> for IterInput<I, S>
where T: 'src, I: Iterator<Item = (T, S)> + Clone + 'src, S: Span + 'src,

Source§

type Cursor = (I, usize, Option<<S as Span>::Offset>)

Source§

type Span = S

Source§

type Token = T

Source§

type MaybeToken = T

Source§

type Cache = S

Source§

impl<'src, S, I, F> Input<'src> for MappedSpan<S, I, F>
where I: Input<'src>, F: 'src + Fn(<I as Input<'src>>::Span) -> S, S: Span + Clone + 'src, <S as Span>::Context: Clone + 'src, <S as Span>::Offset: From<<<I as Input<'src>>::Span as Span>::Offset>,

Source§

type Cursor = <I as Input<'src>>::Cursor

Source§

type Span = S

Source§

type Token = <I as Input<'src>>::Token

Source§

type MaybeToken = <I as Input<'src>>::MaybeToken

Source§

type Cache = (<I as Input<'src>>::Cache, F)

Source§

impl<'src, S, I> Input<'src> for WithContext<S, I>
where I: Input<'src>, S: Span + Clone + 'src, <S as Span>::Context: Clone + 'src, <S as Span>::Offset: From<<<I as Input<'src>>::Span as Span>::Offset>,

Source§

type Cursor = <I as Input<'src>>::Cursor

Source§

type Span = S

Source§

type Token = <I as Input<'src>>::Token

Source§

type MaybeToken = <I as Input<'src>>::MaybeToken

Source§

type Cache = (<I as Input<'src>>::Cache, <S as Span>::Context)

Source§

impl<'src, T, S, I, F> Input<'src> for MappedInput<T, S, I, F>
where I: Input<'src>, T: 'src, S: Span + Clone + 'src, F: Fn(<I as Input<'src>>::MaybeToken) -> (<<I as Input<'src>>::MaybeToken as IntoMaybe<'src, <I as Input<'src>>::Token>>::Proj<T>, <<I as Input<'src>>::MaybeToken as IntoMaybe<'src, <I as Input<'src>>::Token>>::Proj<S>) + 'src,

Source§

type Cursor = (<I as Input<'src>>::Cursor, Option<<S as Span>::Offset>)

Source§

type Span = S

Source§

type Token = T

Source§

type MaybeToken = <<I as Input<'src>>::MaybeToken as IntoMaybe<'src, <I as Input<'src>>::Token>>::Proj<<MappedInput<T, S, I, F> as Input<'src>>::Token>

Source§

type Cache = (<I as Input<'src>>::Cache, F, S)

Source§

impl<'src> Input<'src> for &'src Graphemes