Rule

class Rule<out N : Node>(val name: String, val productions: List<Rule.Production<N>>)

Represents a nonterminal symbol in a Grammar with one or more potential productions.

A Rule defines how a nonterminal can be expanded during parsing. Each Rule contains one or more productions, each representing an alternative way to match the rule against the input token stream. When parsing, the rule attempts each production in order until one succeeds.

Rules support left recursion through a special two-phase matching process: non-left-recursive productions are tried first to establish a base match, then left-recursive productions are applied iteratively to extend the match as far as possible.

Where more than one production could potentially match at the current position, the first successful production in declaration order is used.

Parameters

N

The type of Node produced by this Rule's productions

Constructors

Link copied to clipboard
constructor(name: String, productions: List<Rule.Production<N>>)

Types

Link copied to clipboard
data class Production<out N : Node>(val leftRecursion: Boolean, val pattern: SequenceElement, val producer: Node.ParserCapture.() -> N)

Represents one alternative pattern that a Rule is allowed to match against input.

Link copied to clipboard
data class Result<out N : Node>(val node: N, val captureLength: Int)

Represents a successful parse result for a Rule.

Properties

Link copied to clipboard

The name of this Rule, necessarily unique within the Grammar

Link copied to clipboard

The list of production alternatives for this Rule

Functions

Link copied to clipboard
fun parse(position: Int): Rule.Result<N>
Link copied to clipboard
open override fun toString(): String