Trait aoc_parse::ParseIter

source ·
pub trait ParseIter {
    type RawOutput;

    fn next_parse(&mut self) -> Option<Result<usize, ParseError>>;
    fn take_data(&mut self) -> Self::RawOutput;
}
Expand description

A parser in action. Some parsers can match in several different ways (for example, in foo* bar backtracking is accomplished by foo* first matching as much as possible, then backing off one match at a time), so this is an iterator.

This doesn’t return a RawOutput value from next_parse but instead waits until you’re sure you have a complete, successful parse, and are thus ready to destroy the iterator. This helps us avoid building values only to drop them later when some downstream parser fails to match, so it makes backtracking faster. It also means we don’t call .map closures until there is a successful overall match and the values are actually needed.

Required Associated Types§

The type this iterator can produce on a successful match.

Required Methods§

Try parsing the input.

The first time this is called, it should return either Some(Ok(end)) or Some(Err(err)) indicating that parsing either succeeded or failed.

Subsequently, it should return either Some(Ok(end)) or Some(None) to indicate that there either is or isn’t another, less preferable match.

Consume this iterator to extract data. This is called only after a successful next_parse call that returns Some(Ok(offset)).

This would take self by value, except that’s not compatible with trait objects. (Box<Self> is, so this could change someday.)

Implementors§