Trait shrimple_parser::Parser

source ·
pub trait Parser<'input, T = (), Reason = Infallible>: Sized + FnOnce(&'input str) -> ParsingResult<'input, T, Reason> {
Show 18 methods // Provided methods fn filter( self, f: impl FnOnce(&T) -> bool ) -> impl Parser<'input, T, Reason> { ... } fn map<U>(self, f: impl FnOnce(T) -> U) -> impl Parser<'input, U, Reason> { ... } fn call<F>(self, f: F) -> impl Parser<'input, F::Output, Reason> where F: FnOnce<T>, T: Tuple { ... } fn or<NewReason>( self, parser: impl Parser<'input, T, NewReason> ) -> impl Parser<'input, T, NewReason> where Reason: Into<NewReason> { ... } fn or_map_rest( self, f: impl FnOnce(&'input str) -> T ) -> impl Parser<'input, T, Reason> { ... } fn or_value(self, value: T) -> impl Parser<'input, T, Reason> { ... } fn and<U, NewReason>( self, parser: impl Parser<'input, U, NewReason> ) -> impl Parser<'input, (T, U), NewReason> where Reason: Into<NewReason> { ... } fn add<U, NewReason>( self, parser: impl Parser<'input, U, NewReason> ) -> impl Parser<'input, T::Appended<U>, NewReason> where T: Tuple, Reason: Into<NewReason> { ... } fn then<U, NewReason>( self, parser: impl Parser<'input, U, NewReason> ) -> impl Parser<'input, U, NewReason> where Reason: Into<NewReason> { ... } fn skip<U, NewReason>( self, parser: impl Parser<'input, U, NewReason> ) -> impl Parser<'input, T, NewReason> where Reason: Into<NewReason> { ... } fn maybe_skip<U, OtherReason>( self, parser: impl Parser<'input, U, OtherReason> ) -> impl Parser<'input, T, Reason> { ... } fn expect<NewReason>( self, expected: NewReason ) -> impl Parser<'input, T, NewReason> { ... } fn narrow_reason<NewReason>( self, expected: NewReason ) -> impl Parser<'input, T, NewReason> { ... } fn get_span(self) -> impl Parser<'input, (T, &'input str), Reason> { ... } fn add_span(self) -> impl Parser<'input, T::Appended<&'input str>, Reason> where T: Tuple { ... } fn repeat(self) -> impl Parser<'input, (), Reason> where Self: Clone { ... } fn dbg(self, label: impl Display) -> impl Parser<'input, T, Reason> where T: Debug { ... } fn with_full_error<'path>( self, path: &'path Path, full_input: &'input str ) -> impl FnOnce(&'input str) -> Result<(&'input str, T), FullParsingError<'path, Reason>> { ... }
}
Expand description

The core of the crate, a trait representing a function that takes some string as input and returns either a tuple of (the rest of the input, the output) or a ParsingError.

Provided Methods§

source

fn filter(self, f: impl FnOnce(&T) -> bool) -> impl Parser<'input, T, Reason>

Turns output into a recoverable error if the output doesn’t meet a condition.

source

fn map<U>(self, f: impl FnOnce(T) -> U) -> impl Parser<'input, U, Reason>

Transforms the output of the parser, if present.

source

fn call<F>(self, f: F) -> impl Parser<'input, F::Output, Reason>
where F: FnOnce<T>, T: Tuple,

Available on crate feature nightly only.

Like Parser::map, but calls the provdied function using the Nightly FnOnce::call_once method, effectively spreading the output as the arguments of the function.

The following nIghtly Rust code:

use shrimple_parser::Parser;
parser.call(u32::pow)

is equivalent to the following stable Rust code:

use shrimple_parser::Parser;
parser.map(|(x, y)| u32::pow(x, y))

T for this method is constrained not by the crate::Tuple trait, but by the unstable standard trait core::marker::Tuple, which means that T can be a tuple of absolutely any length.

See also: crate::call, a macro for a stable alternative to this method.

source

fn or<NewReason>( self, parser: impl Parser<'input, T, NewReason> ) -> impl Parser<'input, T, NewReason>
where Reason: Into<NewReason>,

Replaces a recoverable error with the result of parser. The reason for the first parser is adapted to the one of the second parser.

source

fn or_map_rest( self, f: impl FnOnce(&'input str) -> T ) -> impl Parser<'input, T, Reason>

Replaces a recoverable error with the transformed remains of the input. The returned remains of the input are an empty string.

source

fn or_value(self, value: T) -> impl Parser<'input, T, Reason>

Replaces a recoverable error with value & the rest of the input in the recoverable error.

source

fn and<U, NewReason>( self, parser: impl Parser<'input, U, NewReason> ) -> impl Parser<'input, (T, U), NewReason>
where Reason: Into<NewReason>,

Parses the rest of the input after the first parser, returning both outputs & short-circuiting on an error. The reason for the errors of the first parser is adapted to the one of the second parser. See also Parser::add.

source

fn add<U, NewReason>( self, parser: impl Parser<'input, U, NewReason> ) -> impl Parser<'input, T::Appended<U>, NewReason>
where T: Tuple, Reason: Into<NewReason>,

Like Parser::and, but specific to parsers that output a tuple: the new output is appended to the tuple of other tuples using the Tuple trait. The reason for the errors of the first parser is adapted to the one of the second parser.

source

fn then<U, NewReason>( self, parser: impl Parser<'input, U, NewReason> ) -> impl Parser<'input, U, NewReason>
where Reason: Into<NewReason>,

Like Parser::and, but discards the output of the first parser. The reason for the errors of the first parser is adapted to the one of the second parser.

source

fn skip<U, NewReason>( self, parser: impl Parser<'input, U, NewReason> ) -> impl Parser<'input, T, NewReason>
where Reason: Into<NewReason>,

Same as Parser::and but discards the output of the second parser The reason for the errors the first parser is adapted to the one of the second parser.

source

fn maybe_skip<U, OtherReason>( self, parser: impl Parser<'input, U, OtherReason> ) -> impl Parser<'input, T, Reason>

Same as Parser::skip but discards the error of the second parser as well. Effectively, all this function does is advance the input to right after the second parser, if it succeeds, otherwise the input stays as if only the first parser was called.

source

fn expect<NewReason>( self, expected: NewReason ) -> impl Parser<'input, T, NewReason>

Sets the reason for errors returned from the parser, making all errors fatal.

source

fn narrow_reason<NewReason>( self, expected: NewReason ) -> impl Parser<'input, T, NewReason>

Changes the reason from the parser, unless the error is recoverable.

source

fn get_span(self) -> impl Parser<'input, (T, &'input str), Reason>

Adds the part of the input that was consumed by the parser to the outputs. If the input increased in length after the parser (which should not happen), an empty string is added. See also Parser::add_span, which adds the span to the tuple of other outputs.

source

fn add_span(self) -> impl Parser<'input, T::Appended<&'input str>, Reason>
where T: Tuple,

Like Parser::get_span, but adds the output to the tuple of other outputs using the Tuple trait.

source

fn repeat(self) -> impl Parser<'input, (), Reason>
where Self: Clone,

Repeats the parser until a recoverable error is met, discarding all the output. Beware parsers with non-trivially cloneable captured variables: the parser is called repeatedly by being cloned.

source

fn dbg(self, label: impl Display) -> impl Parser<'input, T, Reason>
where T: Debug,

Prints the output using its Debug implementation & the first 16 bytes of the rest of the input, all along with a custom provided message.

source

fn with_full_error<'path>( self, path: &'path Path, full_input: &'input str ) -> impl FnOnce(&'input str) -> Result<(&'input str, T), FullParsingError<'path, Reason>>

Aaugments the parsing error, if present, with location in the input. path is the reported path to the file where the error occured. Note that the input passed here is only used for error reporting, not as the input to the parser.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'input, T, Reason, F> Parser<'input, T, Reason> for F
where F: FnOnce(&'input str) -> ParsingResult<'input, T, Reason>,