Contents

Zero-Or-More

The "*" operator following a parsing expression causes it to match repeatedly and succeed even if the expression never matches, i.e. it matches zero or more times.

Scenario Outline: Capturing expression

  • Given a grammar with:
    letters <- ALPHA*
  • When I parse <input>
  • Then the parse result should be <result>
  • And the parse position should be <pos>

Examples:

inputresultpos
"A"["A"]1
"foo"["f", "o", "o"]3
"234"[]0

Scenario Outline: Non-Capturing expression

  • Given a grammar with:
    dashes <- ~"-"*
  • When I parse <input>
  • Then the parse result should be <result>
  • And the parse position should be <pos>

Examples:

inputresultpos
"-"true1
"---"true3
"foo"true0

Scenario Outline: Choice of capturing or non-capturing

  • Given a grammar with:
    expr  <- ("a" / ~"b")*
  • When I parse <input>
  • Then the parse result should be <result>
  • And the parse position should be <pos>

Examples:

inputresultpos
"aa"["a", "a"]2
"aba"["a", "a"]3
"bbc"[]2

Scenario: Semantic attribute returning true

  • Given a grammar with:
    expr  <- (ALPHA { true })*
  • When I parse "foo "
  • Then the parse result should be []
  • And the parse position should be 3

Source: features/peg_syntax/zero_or_more.feature at commit 8b4efde2, Rattler (MIT license).