diff --git a/docs/grammar.md b/docs/grammar.md index 94c7d17..b9fe7f6 100644 --- a/docs/grammar.md +++ b/docs/grammar.md @@ -83,6 +83,29 @@ Terminals also support grammar operators, such as `|`, `+`, `*` and `?`. Terminals are a linear construct, and therefore may not contain themselves (recursion isn't allowed). +### Templates + +Templates are expanded when preprocessing the grammar. + +Definition syntax: + +```ebnf + my_template{param1, param2, ...}: +``` + +Use syntax: + +```ebnf +some_rule: my_template{arg1, arg2, ...} +``` + +Example: +```ebnf +_seperated{x, sep}: x (sep x)* // Define a sequence of 'x sep x sep x ...' + +num_list: "[" _seperated{NUMBER, ","} "]" // Will match "[1, 2, 3]" etc. +``` + ### Priority Terminals can be assigned priority only when using a lexer (future versions may support Earley's dynamic lexing). @@ -256,3 +279,4 @@ Note that `%ignore` directives cannot be imported. Imported rules will abide by ### %declare Declare a terminal without defining it. Useful for plugins. +