classafPegger::Grammar
sys::Obj afPegger::Grammar
@
Js
(Advanced) Use to programmatically define PEG rules.
Rules may be used before they, themselves are defined:
grammar := Grammar() grammar["a"] := Rules.sequence { grammar["b"], grammar["c"], } grammar["b"] := Rules.zeroOrMore(Rules.spaceChar) grammar["c"] := Rules.oneOrMore(Rules.alphaNumChar)
Sometimes it's easier to hold rules in local variables:
grammar := Grammar() a := grammar["a"] b := grammar["b"] c := grammar["c"] grammar["a"] := Rules.sequence { b, c, } grammar["b"] := Rules.zeroOrMore(Rules.spaceChar) grammar["c"] := Rules.oneOrMore(Rules.alphaNumChar)
All named rules must be defined before the grammar is used.
It is not mandatory to use the Grammar
class to create rules and grammars, but it is usually easier.
- definition
Str definition()
Pretty prints the grammar definition.
- get
Returns the named rule, or creates a proxy if it doesn't exist.
- parseGrammar
static Grammar parseGrammar(Str grammar)
Parses grammar definitions, and returns the root rule (if given) or the first rule parsed. For example:
Grammar.fromDefs("a <- [abc] / [xyz] / b b <- \space+ [^abc]")
See Peg.parseGrammar
- rules
Rule[] rules()
Returns all the named rules.
- set
@
Operator
Rule set(Str name, Rule rule)Sets the real implementation / definition of the named rule.
- validate
This validate()
Validates that all named rules have been defined.
After validation, Grammars and their Rules should be thread safe - use
Unsafe()
if required.