ion7-grammar / ast.builder

class

Builder

local Grammar = require "ion7.grammar"

local b = Grammar.builder()
b:rule("digit", Grammar.char("0-9"))
b:rule("root",  Grammar.plus(Grammar.ref("digit")))
print(b:compile())
-- root ::= [0-9]+
-- digit ::= [0-9]

Functions

Builder.new

Create a new GrammarBuilder.

Builder.new(opts)
optstable?
→ Builder

Builder:rule

Define a named rule. Calling :rule() with the same name twice replaces the previous definition.

Builder:rule(name, body)
namestringRule name.
bodytableAST node.
→ Builderself (fluent)

Builder:root

Set the root rule name used by :compile().

Builder:root(name)
namestring
→ Builderself

Builder:merge

Merge rules from another Builder or rule list. Rules whose names already exist in self are skipped (no overwrite).

Builder:merge(other)
otherBuilder|table
→ Builderself

Builder:compile

Compile all rules to a GBNF string.

Builder:compile(opts)
optstable?
→ string

Builder:rules

Return a shallow copy of this builder's rule list.

Builder:rules()
→ tableArray of { name, body } pairs.

Builder:names

List defined rule names in definition order.

Builder:names()
→ tableArray of rule name strings.