[−][src]Function nom_supreme::multi::parse_separated_terminated
pub fn parse_separated_terminated<Input, ParseOutput, SepOutput, TermOutput, ParseErr, Accum>(
parser: impl Parser<Input, ParseOutput, ParseErr>,
separator: impl Parser<Input, SepOutput, ParseErr>,
terminator: impl Parser<Input, TermOutput, ParseErr>,
init: impl FnMut() -> Accum,
fold: impl FnMut(Accum, ParseOutput) -> Accum
) -> impl Parser<Input, Accum, ParseErr> where
Input: Clone + PartialEq,
ParseErr: ParseError<Input>,
The perfected folding parser. Parses a series of 1 more more things,
separated by some separator
, terminated by some terminator
, folding
all of them together.
When parsing begins, an accumulator value is created with init(). Then,
each parsed item will be folded into the accumulator via the fold
function. After parsing each item, parse_separated_terminated
will
attempt to parse a terminator
. If it succeeds, it will return the
accumulator; otherwise, it will attempt to parse a separator. If it fails
to parse either a separator or a terminator, it will return an error;
otherwise, it will continue on to parse and fold the next item.
If you want 0 or more things to be parsed, wrap this in
opt
or alt
.
This parser will stop as soon as it finds a terminator
. If you wish to
have a terminator
parser that is the same as your separator
, you'll
need to add some extra context to the terminator parser; perhaps a
lookahead with peek
.
This parser exists to provide meaningful parse errors. By requiring a terminator, we can ensure that it doesn't suffer from the normal folding parser problem of unconditionally returning success because all parse failures simply end the fold without knowing if there's a larger problem.
parse_separated_terminated
will attempt to smartly allow 0-length
matches. It will allow subparsers to have 0-length matches, but if a full
loop is made without any progress being made, it will return an error.