Struct parser_compose::ParserContext
source · pub struct ParserContext<T> { /* private fields */ }
Expand description
Container for input into parsers
All parsers accept a ParserContext
as input and must return one if they
succeed.
The structure contains whatever input is yet to be parsed as well as our absolute position within the original input, which is useful for reporting errors.
Note: if you are manually extracting the values from the input, you will need to update the position before returning the context. An example will clarify:
use parser_compose::{Parser, ParserContext, Res};
fn easy(ctx: ParserContext<&str>) -> Res<&str, (&str, &str)> {
// Here, the sequence combinator will take care of updating the context
// before returning if both parsers matched.
("A", "B").try_parse(ctx)
}
fn manual(mut ctx: ParserContext<&str>) -> Res<&str, &str> {
let (offset, _) = ("A", "B").peek().try_parse(ctx)?;
// In some cases, you might want to manually extract the value yourself.
// In this contrived example, doing so lets you return a single string
// slice instead of a tuple.
let value = &ctx.input()[..offset];
// But make sure you update the position in the context before passing it on
ctx.advance_by(offset);
Ok((value, ctx))
}
let msg = "AB";
let (value, rest) = easy.try_parse(msg.into()).unwrap();
assert_eq!(value, ("A", "B"));
assert_eq!(rest.input(), "");
let (value, rest) = manual.try_parse(msg.into()).unwrap();
assert_eq!(value, "AB");
assert_eq!(rest.input(), "");
Regular slices (i.e. &[T]
) and string slices (i.e. &str
) can be converted into this
structure by using ParserContext::from()
, or using .into()
in
contexts where the compiler can infer the type.
Implementations§
source§impl<T> ParserContext<T>where
T: Clone,
impl<T> ParserContext<T>where T: Clone,
sourcepub fn advance_by(&mut self, count: usize)
pub fn advance_by(&mut self, count: usize)
Increments the input position by count
. If there is less than count
elements left in
the input, the position will be set to the length of the input.
Advancing the position affects what subsequent calls to .input()
return
source§impl<T> ParserContext<T>
impl<T> ParserContext<T>
sourcepub fn end_of_input(&self) -> bool
pub fn end_of_input(&self) -> bool
Returns true
if this instance’s position is at the end of the input
sourcepub fn get_position(&self) -> usize
pub fn get_position(&self) -> usize
Returns the current input position
Trait Implementations§
source§impl<T: Clone> Clone for ParserContext<T>
impl<T: Clone> Clone for ParserContext<T>
source§fn clone(&self) -> ParserContext<T>
fn clone(&self) -> ParserContext<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: Debug> Debug for ParserContext<T>
impl<T: Debug> Debug for ParserContext<T>
source§impl<T: PartialEq> PartialEq for ParserContext<T>
impl<T: PartialEq> PartialEq for ParserContext<T>
source§fn eq(&self, other: &ParserContext<T>) -> bool
fn eq(&self, other: &ParserContext<T>) -> bool
self
and other
values to be equal, and is used
by ==
.