Trait shrimple_parser::Parser

source ·
pub trait Parser<'input, T = (), Expectation = Infallible>: Sized + FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> {
Show 16 methods // Provided methods fn filter( self, f: impl FnOnce(&T) -> bool ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> { ... } fn map<U>( self, f: impl FnOnce(T) -> U ) -> impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> { ... } fn or( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> { ... } fn or_map_rest( self, f: impl FnOnce(&'input str) -> T ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> { ... } fn or_value( self, value: T ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> { ... } fn and<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, (T, U), Expectation> { ... } fn add<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T::Appended<U>, Expectation> where T: Tuple { ... } fn then<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> { ... } fn skip<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> { ... } fn maybe_skip<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> { ... } fn expect<NewExpectation>( self, expected: NewExpectation ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, NewExpectation> { ... } fn narrow_expectation<NewExpectation>( self, expected: NewExpectation ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, NewExpectation> { ... } fn get_span( self ) -> impl FnOnce(&'input str) -> ParsingResult<'input, (T, &'input str), Expectation> { ... } fn add_span( self ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T::Appended<&'input str>, Expectation> where T: Tuple { ... } fn repeat( self ) -> impl FnOnce(&'input str) -> ParsingResult<'input, (), Expectation> where Self: Clone { ... } fn parse_with_err_loc<'path>( self, path: &'path Path, input: &'input str ) -> Result<T, FullParsingError<'path, Expectation>> { ... }
}
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 FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>

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 FnOnce(&'input str) -> ParsingResult<'input, U, Expectation>

Transforms the first output of the parser, if present.

source

fn or( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>

Replaces a recoverable error with the result of parser.

source

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

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 FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>

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

source

fn and<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, (T, U), Expectation>

Parses the rest of the input after the first parser, returning both outputs & short-circuiting on an error. See also Parser::add.

source

fn add<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T::Appended<U>, Expectation>
where T: Tuple,

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.

source

fn then<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation>

Like Parser::and, but discards the output of the first parser.

source

fn skip<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>

Same as Parser::and but discards the output of the second parser

source

fn maybe_skip<U>( self, parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation> ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>

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<NewExpectation>( self, expected: NewExpectation ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, NewExpectation>

Sets the expectation from the parser, making all errors unrecoverable.

source

fn narrow_expectation<NewExpectation>( self, expected: NewExpectation ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, NewExpectation>

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

source

fn get_span( self ) -> impl FnOnce(&'input str) -> ParsingResult<'input, (T, &'input str), Expectation>

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 FnOnce(&'input str) -> ParsingResult<'input, T::Appended<&'input str>, Expectation>
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 FnOnce(&'input str) -> ParsingResult<'input, (), Expectation>
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 parse_with_err_loc<'path>( self, path: &'path Path, input: &'input str ) -> Result<T, FullParsingError<'path, Expectation>>

Calls the parser and augments the parsing error, if present, with location in the input. path is the reported path to the file where the error occured.

Object Safety§

This trait is not object safe.

Implementors§

source§

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