Lexer

class Lexer(val lexemes: List<Lexeme<*>>, val factoryOverrides: Map<Lexeme<*>, (value: CharSequence) -> Token> = emptyMap())

The Lexer transforms raw character input into a stream of tokens based on defined lexemes.

A Lexer processes input sequentially, attempting to match each Lexeme, in declaration order, at the current position. When a Lexeme produces a match, the corresponding Token is emitted and the Lexer advances. If no Lexeme matches at the current position, a LexerException is thrown.

A Lexer may include a definable map of factory overrides for any Lexeme declared in lexemes; if a particular Lexeme has a factory override defined, it will be used to generate Tokens for each match of that Lexeme, with precedence over Lexeme.defaultFactory (but not over factories individually specified by LexemeMatch.factory).

See also

Constructors

Link copied to clipboard
constructor(vararg lexemes: Lexeme<*>)

Creates a Lexer from the given lexemes.

constructor(factoryOverrides: Map<Lexeme<*>, (value: CharSequence) -> Token>, vararg lexemes: Lexeme<*>)

Creates a Lexer from the given lexemes and factoryOverrides.

constructor(lexemes: List<Lexeme<*>>, factoryOverrides: Map<Lexeme<*>, (value: CharSequence) -> Token> = emptyMap())

Types

Link copied to clipboard

Exception thrown when lexing fails.

Properties

Link copied to clipboard

An optional Map of Lexemes to a factory override which produces Tokens, given the CharSequence which captures the Token (equal to Token.value)

Link copied to clipboard

The ordered list of Lexeme definitions to match against input

Functions

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

Transforms the given character input into a list of tokens.