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

Source

Str definition()

Pretty prints the grammar definition.

get

Source

@Operator
Rule get(Str name)

Returns the named rule, or creates a proxy if it doesn't exist.

parseGrammar

Source

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

Source

Rule[] rules()

Returns all the named rules.

set

Source

@Operator
Rule set(Str name, Rule rule)

Sets the real implementation / definition of the named rule.

validate

Source

This validate()

Validates that all named rules have been defined.

After validation, Grammars and their Rules should be thread safe - use Unsafe() if required.