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§
sourcefn filter(
self,
f: impl FnOnce(&T) -> bool
) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>
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.
sourcefn map<U>(
self,
f: impl FnOnce(T) -> U
) -> impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation>
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.
sourcefn or(
self,
parser: impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>
) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>
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.
sourcefn or_map_rest(
self,
f: impl FnOnce(&'input str) -> T
) -> 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>
Replaces a recoverable error with the transformed remains of the input. The returned remains of the input are an empty string.
sourcefn or_value(
self,
value: T
) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>
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.
sourcefn and<U>(
self,
parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation>
) -> impl FnOnce(&'input str) -> ParsingResult<'input, (T, U), Expectation>
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.
sourcefn 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 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.
sourcefn then<U>(
self,
parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation>
) -> impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation>
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.
sourcefn skip<U>(
self,
parser: impl FnOnce(&'input str) -> ParsingResult<'input, U, Expectation>
) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, Expectation>
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
sourcefn maybe_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>
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.
sourcefn expect<NewExpectation>(
self,
expected: NewExpectation
) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, NewExpectation>
fn expect<NewExpectation>( self, expected: NewExpectation ) -> impl FnOnce(&'input str) -> ParsingResult<'input, T, NewExpectation>
Sets the expectation from the parser, making all errors unrecoverable.
sourcefn narrow_expectation<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>
Changes the expectation from the parser, unless the error is recoverable.
sourcefn get_span(
self
) -> impl FnOnce(&'input str) -> ParsingResult<'input, (T, &'input str), Expectation>
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.
sourcefn add_span(
self
) -> impl FnOnce(&'input str) -> ParsingResult<'input, T::Appended<&'input str>, Expectation>where
T: Tuple,
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.
sourcefn repeat(
self
) -> impl FnOnce(&'input str) -> ParsingResult<'input, (), Expectation>where
Self: Clone,
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.
sourcefn parse_with_err_loc<'path>(
self,
path: &'path Path,
input: &'input str
) -> Result<T, FullParsingError<'path, Expectation>>
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.