ion7-grammar / compose

module

compose

Functions

rewrite_refs

Deep-copy an AST node, rewriting ref names according to a mapping. Recursively traverses the AST and replaces every `ref` node whose name appears in `name_map` with a new ref using the mapped name. All other node kinds are deep-copied unchanged. seq/alt items arrays are copied element by element so the original AST is never mutated. Used when merging rules from one grammar into another with a prefix, so that internal references point to the new prefixed names.

rewrite_refs(node, name_map)
nodetableAST node to transform (any kind).
name_maptable{ old_name = new_name } substitution map.
→ tableNew AST node with all matching refs rewritten.

prefix_grammar

Add all rules from source_b into target_b with a prefix. All internal refs are rewritten to use the new prefixed names. The root rule of source_b becomes prefix-root_name in target_b.

prefix_grammar(target_b, source_b, prefix)
target_bBuilderDestination builder.
source_bBuilderSource builder.
prefixstringPrefix to prepend (e.g. "a", "b").
→ stringThe new name of source_b's root rule in target_b.

Compose.union

Union: match either grammar a or grammar b. All rules from both grammars are added with "a-" and "b-" prefixes. Internal refs are rewritten so no broken references occur.

Compose.union(a, b, opts)
aGrammar_obj|Builder
bGrammar_obj|Builder
optstable?{ root = "root" }
→ Builder

Compose.sequence

Sequence: match grammar a followed immediately by grammar b.

Compose.sequence(a, b, opts)
aGrammar_obj|Builder
bGrammar_obj|Builder
optstable?{ root = "root", separator = node? }
→ Builder

Compose.repeat_g

Repeat: match grammar g between min and max times.

Compose.repeat_g(g, min, max, sep)
gGrammar_obj|Builder
minnumber?Minimum repetitions (default: 0).
maxnumber?Maximum repetitions (default: -1 = unlimited).
septable|string|nilOptional AST separator node (or literal string) between repetitions.
→ Builder

Compose.optional

Optional: match grammar g or the empty string.

Compose.optional(g)
gGrammar_obj|Builder
→ Builder

Compose.wrap

Wrap: surround grammar with prefix and suffix literals.

Compose.wrap(g, pre, suf, ws)
gGrammar_obj|Builder
prestringPrefix literal (e.g. "[").
sufstringSuffix literal (e.g. "]").
wsboolean?Insert whitespace between delimiters (default: true).
→ Builder

Compose.interleave

Interleave: match grammar g with sep between each occurrence. Equivalent to: g (sep g)* - one or more gs separated by sep.

Compose.interleave(g, sep, min, max)
gGrammar_obj|Builder
sepstring|tableSeparator: a literal string (e.g. ",") or a pre-built AST node.
minnumber?Minimum elements (default: 1).
maxnumber?Maximum elements (default: -1 = unlimited).
→ Builder

Compose.annotate

Annotate: wrap a grammar with a named alias rule.

Compose.annotate(g, name)
gGrammar_obj|Builder
namestringNew root rule name.
→ Builder