grammar
templates
input string
input
—
rule = expr — one rule per line. To continue a choice onto the next line, start the continuation line with | (see the balanced-parens template).
'text' | a literal string (also "text"). \n \t \\ \' \" are recognized escapes. |
[a-z0-9_] | a character class. [^...] negates it. |
. | any single character. |
$ | end of input — matches only once nothing is left. |
a b | sequence — a then b, written by placing them side by side. |
a | b | ordered choice — try a; only if it fails, try b. |
a* a+ a? | zero-or-more, one-or-more, optional. |
&a !a | lookahead — succeed (or fail) without consuming anything. |
( ... ) | grouping, for when a quantifier or choice needs to apply to more than one term. |
This is a real PEG (parsing expression grammar): choice is ordered and never ambiguous, and — unlike context-free grammars — a rule can't call itself as the very first thing it tries without consuming input. The left-recursion trap template shows what happens when one does.