Grammar
LBNF
Lux uses LBNF (BNF variant) to describe the grammar.
Defining rules
To define rule simply do this:
rule-name = expression;
Rule name must only contain letters, digits, hyphens and underscores:
first-valid-rule-name
invalid $rule name&
2nd-valid-rule-name
Root rule
To define root rule (rule that will be used to match the whole input) use !:
!root-rule-name = expression;
Rule name can be only defined once!
Patterns
Basic rule for LBNF is pattern. Pattern must be surrounded by quotes:
"LBNF pattern"
Don’t use whitespaces and line breaks in patterns: use inline and multiline indent instead!
Includes
You can include rules by name and use them in other rules:
first-rule = "pattern";
second-rule = first-rule;
Groups
You can group rules by using parentheses:
rule = (a | b) c;
Chains
Chains are rule sets that can be used to define a rule sequence or a rule variants.
rule = "a" | "b"; -- "or" chain
rule = "a" "b"; -- "and" chain
Modifiers
Rule modifiers describe how many times the rule can be repeated:
rule = [abc]; -- optional (0 or 1)
rule = {abc}; -- repeation (0 or more)
You can specify repeation count:
rule = {abc}<min..max>;
{abc} is same as {abc}<0..inf>
[abc] is same as {abc}<0..1>
Advanced usage
You can create rules manually using Grammar._or, Grammar._and, Grammar.repeation, Grammar.optional, Grammar.pattern, Grammar.include and Grammar.custom.
Custom rules
You can define custom rules using Grammar.custom:
Grammar.custom(function(parser, elements)
return true, elements, #elements -- success, result, count
end)