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:
&str:SliceInput,StrInput,ValueInput,ExactSizeInput&[T]:SliceInput,ValueInput,BorrowInput,ExactSizeInputStream<I>:ValueInput,ExactSizeInputifI: ExactSizeIteratorIterInput<I>:ValueInput,ExactSizeInputifI: ExactSizeIterator
Required Associated Types§
Sourcetype Span: Span
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.
Sourcetype Token: 'src
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).
Sourcetype MaybeToken: IntoMaybe<'src, Self::Token>
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.
Sourcetype Cursor: Clone
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.
Sourcetype Cache
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§
Sourcefn begin(self) -> (Self::Cursor, Self::Cache)
fn begin(self) -> (Self::Cursor, Self::Cache)
Create an initial cursor and cache at the start of the input.
Sourcefn cursor_location(cursor: &Self::Cursor) -> usize
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.
Sourceunsafe fn next_maybe(
cache: &mut Self::Cache,
cursor: &mut Self::Cursor,
) -> Option<Self::MaybeToken>
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.
Sourceunsafe fn span(
cache: &mut Self::Cache,
range: Range<&Self::Cursor>,
) -> Self::Span
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§
Sourcefn with_context<S>(self, context: <S as Span>::Context) -> WithContext<S, Self>
fn with_context<S>(self, context: <S as Span>::Context) -> WithContext<S, Self>
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
Sourcefn 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<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.
Sourcefn map_span<S, F>(self, map_fn: F) -> MappedSpan<S, Self, F>
fn map_span<S, F>(self, map_fn: F) -> MappedSpan<S, Self, F>
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".