ParserContext

interface ParserContext<T, K : Token<T>, E>

A parsing context, created by a Parser.parse call, which exposes an interface for pratt-parsing.

Author

AdrianTodt

Parameters

T

The parser's (and underlying grammar's) token type.

E

The parser's (and underlying grammar's) expression result.

Functions

back
Link copied to clipboard
common
abstract fun back(): K

Move the index backwards one token and returns it.

eat
Link copied to clipboard
common
abstract fun eat(): K

Eats the current token, advancing the index by one.

abstract fun eat(type: T): K

Eats the current token, advancing the index by one. Throws a SyntaxException if the token type doesn't match.

match
Link copied to clipboard
common
abstract fun match(type: T): Boolean

Equivalent to nextIs, but eats the current token if true.

matchAny
Link copied to clipboard
common
abstract fun matchAny(vararg type: T): Boolean

Equivalent to nextIsAny, but eats the current token if true.

nextIs
Link copied to clipboard
common
abstract fun nextIs(type: T): Boolean

Peeks the next token and if the token types are equal, returns true.

nextIsAny
Link copied to clipboard
common
abstract fun nextIsAny(vararg types: T): Boolean

Peeks the next token and if the token type is equal to any of the types, returns true.

parseExpression
Link copied to clipboard
common
abstract fun parseExpression(precedence: Int = 0): E

Parses the expression using this parser's grammar.

open fun Grammar<T, K, E>.parseExpression(precedence: Int = 0): E

Parses the expression using another grammar.

peek
Link copied to clipboard
common
abstract fun peek(distance: Int = 0): K

Peeks a token a distance far away of the reader.

peekAheadUntil
Link copied to clipboard
common
abstract fun peekAheadUntil(vararg type: T): List<K>

Peeks the tokens ahead until a token of any of the types is found.

skipUntil
Link copied to clipboard
common
abstract fun skipUntil(vararg type: T)

Skips tokens ahead until a token of any of the types is found.

withGrammar
Link copied to clipboard
common
abstract fun withGrammar(grammar: Grammar<T, K, E>): ParserContext<T, K, E>

Creates a child parser context with the specified grammar.

Properties

eof
Link copied to clipboard
common
abstract val eof: Boolean

A property which is true if there's no more tokens.

grammar
Link copied to clipboard
common
abstract val grammar: Grammar<T, K, E>

The grammar of this parser's context.

index
Link copied to clipboard
common
abstract var index: Int

The current index in the list of tokens.

last
Link copied to clipboard
common
abstract val last: Token<T>

A property which contains the last token.

source
Link copied to clipboard
common
abstract val source: Source

The source of this grammar's tokens.

Extensions

eatMulti
Link copied to clipboard
common
fun <T, K : Token<T>> ParserContext<T, K, *>.eatMulti(vararg types: T): List<K>

Eats a sequence of tokens in a row of the specified types, returning a list of the consumed tokens if all the specified types matched successfully.

eatSequence
Link copied to clipboard
common
fun <T, K : Token<T>> ParserContext<T, K, *>.eatSequence(vararg types: T): List<K>

Eats a sequence of tokens in a row of the specified types, returning a list of the consumed tokens if all the specified types matched successfully.

ensureEOF
Link copied to clipboard
common
fun <R> ParserContext<*, *, *>.ensureEOF(block: () -> R): R

After executing a given block, throws a SyntaxException if remaining tokens are found in the ParserContext. Otherwise, returns the block's computed result.