RuleBuilder

class RuleBuilder<N : Node>(name: String?)

A DSL builder for defining production rules within a Grammar.

RuleBuilder provides a fluent API for constructing Rule instances by defining one or more productions. Each production is built by chaining PatternElement DSL constructor calls (token, reference, sequence, etc.), followed by a call to produces to finalize each Production.

All productions must include at least one element, and all Rules must include at least one production. If these conditions are not met, an IllegalStateException is thrown when finalizing the RuleBuilder.

Parameters

N

The type of node produced by this rule's productions

name

The name of this rule (unique within a Grammar), or null for anonymous rules

Constructors

Link copied to clipboard
constructor(name: String?)

Functions

Link copied to clipboard
fun alternation(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds an AlternationElement that matches any one of the specified alternatives.

Link copied to clipboard
fun build(): Rule<N>

Finalizes the current production and returns the built Rule instance.

Link copied to clipboard
fun chain(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds a OneOrMoreElement that matches 1 or more times.

Link copied to clipboard
fun choice(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds an AlternationElement that matches any one of the specified alternatives.

Link copied to clipboard
fun concatenation(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds a SequenceElement that matches all specified elements in order.

Link copied to clipboard
inline fun element(crossinline element: () -> PatternElement): RuleBuilder<N>

Adds a custom PatternElement to the current production.

Link copied to clipboard
fun <G : Node> group(pattern: RuleBuilder<G>.() -> Unit): RuleBuilder<N>

Adds an anonymous inline nonterminal rule reference.

Link copied to clipboard

Marks the current production as left-recursive.

Link copied to clipboard
fun <G : Node> node(pattern: RuleBuilder<G>.() -> Unit): RuleBuilder<N>

Adds an anonymous inline nonterminal rule reference.

Link copied to clipboard
fun nonterminal(rule: () -> Rule<*>): RuleBuilder<N>

Adds a nonterminal ReferenceElement that matches the given rule.

Link copied to clipboard
fun oneOrMore(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds a OneOrMoreElement that matches 1 or more times.

Link copied to clipboard
fun option(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds an OptionalElement (matches 0 or 1 times).

Link copied to clipboard

Finalizes the current production with a default Generic node factory.

fun produces(producer: Node.ParserCapture.() -> N): RuleBuilder<N>

Finalizes the current Production with a Node factory function.

Link copied to clipboard
fun reference(rule: () -> Rule<*>): RuleBuilder<N>

Adds a nonterminal ReferenceElement that matches the given rule.

Link copied to clipboard
fun repeat(min: Int = 0, max: Int = Int.MAX_VALUE, pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds a RepeatableElement that matches between min and max occurrences.

Link copied to clipboard
fun sequence(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds a SequenceElement that matches all specified elements in order.

Link copied to clipboard
inline fun <T : Token> token(): RuleBuilder<N>

Adds a TokenElement that matches tokens of type T.

inline fun <T : Token> token(noinline predicate: (T) -> Boolean): RuleBuilder<N>

Adds a TokenElement that matches tokens of type T satisfying the given predicate.

fun token(lexeme: Lexeme<*>): RuleBuilder<N>

Adds a TokenElement that matches tokens produced by the specified lexeme.

Link copied to clipboard
fun zeroOrMore(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds a RepeatableElement that matches 0 or more times.

Link copied to clipboard
fun zeroOrOne(pattern: RuleBuilder<*>.() -> Unit): RuleBuilder<N>

Adds an OptionalElement (matches 0 or 1 times).