LexerContext

interface LexerContext<T>

A lexing context, created by a Lexer.parse call, which exposes an interface for advanced lexing.

Author

AdrianTodt

Parameters

T

The type of tokens the lexer generates.

Functions

hasNext
Link copied to clipboard
common
abstract fun hasNext(): Boolean

Checks if there's more characters ahead.

match
Link copied to clipboard
common
abstract fun match(expect: Char): Boolean

Peeks the next character and, if equals the expected character, consumes it. Returns true if the peeked character were equals the expected consumer and consumed.

next
Link copied to clipboard
common
abstract fun next(): Char

Returns the next character of the reader.

nextString
Link copied to clipboard
common
abstract fun nextString(length: Int): String

Returns a predefined length of characters of the reader, as a String.

parseOnce
Link copied to clipboard
common
abstract fun parseOnce(): List<T>

Lexes once and return the processed tokens.

peek
Link copied to clipboard
common
abstract fun peek(): Char

Peeks the next character of the reader.

abstract fun peek(distance: Int): Char

Peeks a character a distance far away of the reader.

peekString
Link copied to clipboard
common
abstract fun peekString(length: Int): String

Peeks a string with a predefined length of the reader.

process
Link copied to clipboard
common
abstract fun process(token: T)

Calls the Lexer.parse's token consumer.

Properties

index
Link copied to clipboard
common
abstract val index: Int

The current index.

reader
Link copied to clipboard
common
abstract val reader: StringReader

This context' reader.

source
Link copied to clipboard
common
abstract val source: Source

The original source of this context.

Extensions

processToken
Link copied to clipboard
common
fun <T> LexerContext<in Token<T>>.processToken(type: T, back: Int = 1, length: Int = back, offset: Int = 0)

Emits a token to the lexer, based off the current state of the LexerContext.

fun <T> LexerContext<in StringToken<T>>.processToken(type: T, value: String, back: Int = value.length, length: Int = back, offset: Int = 0)

Emits a token with a value, based off the current state of the LexerContext.

readIdentifier
Link copied to clipboard
common
fun LexerContext<*>.readIdentifier(firstChar: Char? = null): String

Reads a C-like identifier.

readNumber
Link copied to clipboard
common
fun LexerContext<*>.readNumber(c: Char): LexicalNumber

Reads a number.

readString
Link copied to clipboard
common
fun LexerContext<*>.readString(delimiter: Char): String

Reads a String up until a delimiter.

section
Link copied to clipboard
common
fun LexerContext<*>.section(back: Int = 1, length: Int = back, offset: Int = 0): Section

Creates a section, based off the current state of the LexerContext.

token
Link copied to clipboard
common
fun <T> LexerContext<in Token<T>>.token(type: T, back: Int = 1, length: Int = back, offset: Int = 0): Token<T>

Creates a token, based off the current state of the LexerContext.

fun <T> LexerContext<in StringToken<T>>.token(type: T, value: String, back: Int = value.length, length: Int = back, offset: Int = 0): StringToken<T>

Creates a token with a value, based off the current state of the LexerContext.