ion7-grammar / ast.compiler

module

ast.compiler

local compiler = require "ion7.grammar.ast.compiler"
local gbnf = compiler.compile(rules, "root", true)

Functions

escape_literal

Escape a raw string so it is safe to embed inside GBNF double-quoted literal syntax: backslashes and double-quotes are escaped; newline and tab are converted to their two-character escape sequences.

escape_literal(s)
sstringRaw string value.
→ stringEscaped string (no surrounding quotes).

compile_literal

Compile a literal node to a double-quoted GBNF string.

compile_literal(node)

compile_char

Compile a char-class node to a GBNF bracket expression.

compile_char(node)

compile_ref

Compile a ref node to its rule name.

compile_ref(node)

compile_seq

Compile a seq node: items joined by spaces.

compile_seq(node, prec)

compile_alt

Compile an alt node: alternatives joined by " | ".

compile_alt(node, prec)

compile_rep

Compile a rep node using GBNF suffix operators or {n,m} notation.

compile_rep(node)

compile_group

Compile a group node.

compile_group(node)

compile_node

Dispatch compilation to the correct handler based on node.kind.

compile_node(node, prec)

compiler.compile

Compile a set of named rules to a GBNF string. The root rule is always placed first in the output regardless of definition order. When `whitespace` is true and a `ws` rule is missing but referenced, a default `ws ::= [ \t\n]*` rule is appended.

compiler.compile(rules, root, whitespace)
rulestableArray of { name, body } pairs.
rootstring?Root rule name (default: "root").
whitespaceboolean?Inject ws rule if referenced but absent (default: true).
→ stringGBNF string (no trailing newline).

raises — When the root rule is not found in the rules array.