EBNF Rule: Choice

Prev:EBNF Rule: Group
Next:EBNF Rule: Ignore

Any rule element separated by a pipe character (|) is considered a choice. Choices are matched against the input stream in order. If a match succeeds then the remaining choices are discarded and the result of the match is the AST result of the choice.

USING: prettyprint peg.ebnf ; "a" EBNF[[ rule="a" | "b" | "c" ]] .

USING: prettyprint peg.ebnf ; "b" EBNF[[ rule="a" | "b" | "c" ]] .

USING: prettyprint peg.ebnf ; "d" EBNF[[ rule="a" | "b" | "c" ]] .
Peg parsing error at character position 0. Expected 'a' or 'b' or 'c' Got 'd'

Due to parser caching, rules can't re-use parsers that have already failed earlier in the choice.