Function lip::sequence

source ·
pub fn sequence<'a, A: Clone + 'a, StartOutput: Clone + 'a, StartParser, ItemParser, SepOutput: Clone + 'a, SepParser, SpacesParser, EndOutput: Clone + 'a, EndParser, S: Clone + 'a>(
    start: StartParser,
    item: impl Fn() -> ItemParser,
    separator: impl Fn() -> SepParser,
    spaces: impl Fn() -> SpacesParser,
    end: EndParser,
    trailing: Trailing
) -> impl Parser<'a, Output = Vec<A>, State = S>where
    StartParser: Parser<'a, Output = StartOutput, State = S> + 'a,
    ItemParser: Parser<'a, Output = A, State = S> + 'a,
    SepParser: Parser<'a, Output = SepOutput, State = S> + 'a,
    SpacesParser: Parser<'a, Output = (), State = S> + 'a,
    EndParser: Parser<'a, Output = EndOutput, State = S> + 'a,
Expand description

Parse a sequence like lists or code blocks.

Example: Parse a list containing the string “abc” with optional trailing comma:

assert_succeed(sequence(
  token("["),
  || token("abc"),
  || token(","),
  || space0(),
  token("]"),
  Trailing::Optional),
"[abc, abc, abc]", vec!["abc", "abc", "abc"]);

Note: spaces are inserted between every part. So if you use space1 instead of space0, you need to put more spaces before and after separators, between start symbol and first item and between last item or separator and the end symbol:

assert_succeed(sequence(
  token("["),
  || token("abc"),
  || token(","),
  || space1(),
  token("]"),
  Trailing::Optional),
"[ abc , abc , abc ]", vec!["abc", "abc", "abc"]);