Trait Parser

Source
pub trait Parser {
    type Output;
    type RawOutput: ParserOutput<UserType = Self::Output>;
    type Iter<'parse>: ParseIter<'parse, RawOutput = Self::RawOutput>
       where Self: 'parse;

    // Required method
    fn parse_iter<'parse>(
        &'parse self,
        context: &mut ParseContext<'parse>,
        start: usize,
    ) -> Result<Self::Iter<'parse>, Reported>;

    // Provided methods
    fn parse(&self, s: &str) -> Result<Self::Output, ParseError> { ... }
    fn parse_raw(&self, s: &str) -> Result<Self::RawOutput, ParseError> { ... }
}
Expand description

Trait implemented by all parsers.

This is implemented by the built-in parsers, like i32, as well as user-defined parsers created with parser!.

To run a parser, pass some text to the parse method.

Required Associated Types§

Source

type Output

The type of value this parser produces from text.

Source

type RawOutput: ParserOutput<UserType = Self::Output>

The type this parser produces internally before converting to the output type.

Some combinators use the RawOutput to determine how types should combine. For example, if A::RawOutput = (), then A produces no output; and if B::RawOutput = (i32,) then B produces an integer; SequenceParser<A, B>::RawOutput will then be (i32,), the result of concatenating the two raw tuples, rather than ((), i32).

However, RawOutput is very often a singleton tuple, and these are awkward for users, so we convert to the Output type before presenting a result to the user.

Source

type Iter<'parse>: ParseIter<'parse, RawOutput = Self::RawOutput> where Self: 'parse

The type that implements matching, backtracking, and type conversion for this parser, an implementation detail.

Required Methods§

Source

fn parse_iter<'parse>( &'parse self, context: &mut ParseContext<'parse>, start: usize, ) -> Result<Self::Iter<'parse>, Reported>

Produce a parse iterator. This is an internal implementation detail of the parser and shouldn’t normally be called directly from application code.

Provided Methods§

Source

fn parse(&self, s: &str) -> Result<Self::Output, ParseError>

Fully parse the given source string s and return the resulting value.

This is the main way of using a Parser.

This succeeds only if this parser matches the entire input string. It’s an error if any unmatched characters are left over at the end of s.

Source

fn parse_raw(&self, s: &str) -> Result<Self::RawOutput, ParseError>

Like parse but produce the output in its raw form.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Parser for char

Source§

type Output = ()

Source§

type RawOutput = ()

Source§

type Iter<'parse> = ExactParseIter

Source§

fn parse_iter<'parse>( &'parse self, context: &mut ParseContext<'parse>, start: usize, ) -> Result<ExactParseIter, Reported>

Source§

impl Parser for str

Source§

type Output = ()

Source§

type RawOutput = ()

Source§

type Iter<'parse> = ExactParseIter

Source§

fn parse_iter<'parse>( &'parse self, context: &mut ParseContext<'parse>, start: usize, ) -> Result<ExactParseIter, Reported>

Source§

impl<'a, P> Parser for &'a P
where P: Parser + ?Sized,

Source§

type Output = <P as Parser>::Output

Source§

type RawOutput = <P as Parser>::RawOutput

Source§

type Iter<'parse> = <P as Parser>::Iter<'parse> where P: 'parse, 'a: 'parse

Source§

fn parse_iter<'parse>( &'parse self, context: &mut ParseContext<'parse>, start: usize, ) -> Result<Self::Iter<'parse>, Reported>

Implementors§