Trait parsimonious::StatefulParserOf [] [src]

pub trait StatefulParserOf<S> {
    type Output;
    fn parse(self, value: S) -> ParseResult<Self, S> where Self: Sized;
    fn done(self) -> Self::Output where Self: Sized;

    fn boxable(self) -> BoxableParser<Self> where Self: Sized { ... }
}

A trait for stateful parsers.

Stateful parsers are typically constructed by calling the init method of a stateless parser, for example:

let stateless = character_guard(char::is_alphanumeric).star(String::new);
let stateful = stateless.init();

Here, stateless is a ParserOf<&str,Output=String>, and stateful is a StatefulParserOf<&str,Output=String>.

Associated Types

type Output

The type of the data being produced by the parser.

Required Methods

fn parse(self, value: S) -> ParseResult<Self, S> where Self: Sized

Provides data to the parser.

If parser: StatefulParserOf<S,Output=T>, then parser.parse(data) either:

  • returns Done(rest, result) where rest: S is any remaining input, and result: T is the parsed output, or
  • returns Continue(parsing) where parsing: Self is the new state of the parser.

For example:

let parser = character_guard(char::is_alphabetic).star(String::new);
let stateful = parser.init();
match stateful.parse("abc") {
    Done(_,_) => panic!("can't happen"),
    Continue(parsing) => match parsing.parse("def!") {
        Continue(_) => panic!("can't happen"),
        Done(rest,result) => {
            assert_eq!(rest,"!");
            assert_eq!(result,"abcdef");
        }
    }
}

Note that parser.parse(data) consumes both the parser and the data. In particular, the parser is no longer available, so the following does not typecheck:

let parser = character(char::is_alphabetic).star(String::new);
let stateful = parser.init();
stateful.parse("abc");
stateful.parse("def!");

This helps with parser safety, as it stops a client from calling parse after a a stateful parser has finished.

fn done(self) -> Self::Output where Self: Sized

Tells the parser that it will not receive any more data.

If parser: StatefulParserOf<S,Output=T>, then parser.done() returns a result of type T for example:

let parser = character_guard(char::is_alphabetic).star(String::new);
let stateful = parser.init();
match stateful.parse("abc") {
    Done(_,_) => panic!("can't happen"),
    Continue(parsing) => match parsing.parse("def") {
        Done(_,_) => panic!("can't happen"),
        Continue(parsing) => assert_eq!(parsing.done(),"abcdef"),
    }
}

Note that parser.done() consumes the parser. In particular, the parser is no longer available, so the following does not typecheck:

let parser = character(char::is_alphabetic).star(String::new);
let stateful = parser.init();
stateful.done();
stateful.parse("def!");

This helps with parser safety, as it stops a client from calling parse after a a stateful parser has finished.

Provided Methods

fn boxable(self) -> BoxableParser<Self> where Self: Sized

Make this parser boxable.

Implementors