Trait parsell::Uncommitted [] [src]

pub trait Uncommitted<Ch, Str, Output> {
    type State;
    fn init(&self, string: &mut Str) -> Option<ParseResult<Self::State, Output>>;
}

A trait for uncommitted parsers.

An uncommitted parser can decide based on the first token of input whether it will commit to parsing, or immediately backtrack and try another option.

The advantage of uncommitted parsers over committed parsers is they support choice: p.or_else(q) will try p, and commit if it commits, but if it backtracks will then try q. For example:

fn default(_: Option<char>) -> String { String::from("?") }
let parser =
   character(char::is_numeric).plus(String::new)
       .or_else(character(char::is_alphabetic).plus(String::new))
       .or_else(CHARACTER.map(default));
match parser.init_str("123abc").unwrap() {
   Done(result) => assert_eq!(result, "123"),
   _ => panic!("Can't happen"),
}
match parser.init_str("abc123").unwrap() {
   Done(result) => assert_eq!(result, "abc"),
   _ => panic!("Can't happen"),
}
match parser.init_str("!@#").unwrap() {
   Done(result) => assert_eq!(result, "?"),
   _ => panic!("Can't happen"),
}

Semantically, a parser with input S and output T is a partial function S+ → T whose domain is prefix-closed (that is, if s·t is in the domain, then s is in the domain).

Associated Types

type State

Required Methods

fn init(&self, string: &mut Str) -> Option<ParseResult<Self::State, Output>>

Parse a string of data.

Implementors