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
impl<P, F, Ch, Str, Output> Uncommitted<Ch, Str, Output> for Map<P, F> where P: UncommittedInfer<Ch, Str>, F: 'static + Copy + Function<P::Output, Output=Output>
impl<P, F, Ch, Str, Output> Uncommitted<Ch, Str, Output> for VariantMap<P, F> where P: Uncommitted<Ch, Str, F::Input>, F: 'static + Copy + VariantFunction<Output>
impl<P, Q, Ch, Str, POutput, PStaticOutput, QOutput> Uncommitted<Ch, Str, (POutput, QOutput)> for AndThen<P, Q> where P: Uncommitted<Ch, Str, POutput>, Q: 'static + Copy + Committed<Ch, Str, QOutput>, POutput: ToStatic<Static=PStaticOutput> + Downcast<PStaticOutput>
impl<P, Q, Ch, Str, Output> Uncommitted<Ch, Str, Output> for OrElse<P, Q> where P: Uncommitted<Ch, Str, Output>, Q: Uncommitted<Ch, Str, Output>
impl<P, F, Ch, Str> Uncommitted<Ch, Str, F::Output> for Plus<P, F> where P: 'static + Copy + UncommittedInfer<Ch, Str>, F: 'static + Factory, Str: PeekableIterator, P::State: Stateful<Ch, Str, P::Output>, F::Output: Consumer<P::Output>
impl<P, F, Ch, Str> Uncommitted<Ch, Str, F::Output> for Star<P, F> where P: 'static + Copy + UncommittedInfer<Ch, Str>, F: 'static + Factory, Str: PeekableIterator, P::State: Stateful<Ch, Str, P::Output>, F::Output: Consumer<P::Output>
impl<P, Ch, Str, Output> Uncommitted<Ch, Str, Option<Output>> for Opt<P> where Str: PeekableIterator, P: Uncommitted<Ch, Str, Output>
impl<P, Ch, Str> Uncommitted<Ch, Str, ()> for Discard<P> where P: UncommittedInfer<Ch, Str>
impl<F, Ch, Str> Uncommitted<Ch, Str, F::Output> for Emit<F> where Str: PeekableIterator, F: 'static + Copy + Factory
impl<F, Ch, Str> Uncommitted<Ch, Str, Ch> for Character<F> where Str: PeekableIterator<Item=Ch>, F: Copy + Function<Ch, Output=bool>, Ch: Copy
impl<F, Ch, Str> Uncommitted<Ch, Str, Ch> for CharacterRef<F> where Str: PeekableIterator<Item=Ch>, F: Copy + for<'a> Function<&'a Ch, Output=bool>
impl<Ch, Str> Uncommitted<Ch, Str, Option<Ch>> for AnyCharacter where Str: Iterator<Item=Ch>
impl<'a, P> Uncommitted<char, Chars<'a>, Cow<'a, str>> for Buffered<P> where P: UncommittedInfer<char, Chars<'a>>
impl<P, F, Ch, Str, Output> Uncommitted<Ch, Str, Output> for Boxed<P, F> where P: Uncommitted<Ch, Str, Output>, F: Function<BoxableState<P::State>>, F::Output: 'static + Stateful<Ch, Str, Output>