Grammar

class Grammar(val lexemes: List<Lexeme<*>>, val rules: Map<String, Rule<*>>, val startRule: Rule<*>?)

Represents a complete Context-Free Grammar definition with lexing and parsing capabilities.

A Grammar encapsulates both the lexical structure (via lexemes) and syntactic structure (via rules) of a formal language. It provides methods to transform raw character input into tokens (tokenize) and to parse token streams into Abstract Syntax Trees (parse).

Grammars are typically constructed using the DSL provided by Grammar.Builder through the companion object's invoke operators.

Constructors

Link copied to clipboard
constructor(lexemes: List<Lexeme<*>>, rules: Map<String, Rule<*>>, startRule: Rule<*>?)

Types

Link copied to clipboard
class Builder

A DSL builder for constructing Grammar instances.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The ordered list of lexeme definitions used for tokenization

Link copied to clipboard
val rules: Map<String, Rule<*>>

A map of rule names to their Rule definitions

Link copied to clipboard
val startRule: Rule<*>?

The default rule to use when parsing without an explicit rule specified

Functions

Link copied to clipboard
fun lex(input: CharSequence, discardEnabled: Boolean = true): List<Token>
Link copied to clipboard
inline fun <T : Token> lexAs(input: CharSequence, discardEnabled: Boolean = true): List<T>
Link copied to clipboard
inline fun <N : Node> parse(input: List<Token>): N

Parses the input token sequence into an AST node of type N using this Grammar's default startRule.

inline fun <N : Node> parse(input: CharSequence, lexerDiscardEnabled: Boolean = true): N

Parses the input CharSequence into an AST node of type N using this Grammar's default startRule.

inline fun <N : Node> parse(input: List<Token>, rule: String): N

Parses the input token sequence into an AST node of type N using the specified rule name.

inline fun <N : Node> parse(input: List<Token>, rule: Rule<Node>): N

Parses the input token sequence into an AST node of type N using the specified rule.

inline fun <N : Node> parse(input: CharSequence, rule: String, lexerDiscardEnabled: Boolean = true): N

Parses the input CharSequence into an AST node of type N using the specified rule name.

inline fun <N : Node> parse(input: CharSequence, rule: Rule<Node>, lexerDiscardEnabled: Boolean = true): N

Parses character input into an AST node of type N using the specified rule.

Link copied to clipboard
fun tokenize(input: CharSequence, discardEnabled: Boolean = true): List<Token>

Transforms the given input into a list of tokens using this Grammar's lexemes.

Link copied to clipboard
inline fun <T : Token> tokenizeAs(input: CharSequence, discardEnabled: Boolean = true): List<T>

Transforms the given input into a list of tokens of type T.