ParserInput

class ParserInput<out I : Any, U : Any>(val input: List<I>, val index: Int, val userContext: U)

An immutable, position-aware view into a token sequence, optionally carrying a user-defined context value.

Every successful parse step produces a new ParserInput advanced past the consumed tokens; the underlying input list and userContext are shared by reference, so advancing is allocation-light.

Type parameters

  • I — the token type (e.g. Char). Covariant: a ParserInput<Char, U> can be read wherever a ParserInput<Any, U> is expected.

  • U — the user context type. Invariant. Use Unit when no context is needed.

Example

data class ParseState(val source: String)

val input = ParserInput.of("hello".toList(), ParseState("hello.kt"))
println(input.current()) // 'h'
println(input.advance().current()) // 'e'
println(input.userContext.source) // "hello.kt"

Parameters

input

the full token sequence being parsed.

index

the current position within input.

userContext

an arbitrary value threaded through parsing unchanged.

See also

Constructors

Link copied to clipboard
constructor(input: List<I>, index: Int, userContext: U)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val index: Int
Link copied to clipboard
val input: List<I>
Link copied to clipboard

true when index is at or past the end of input; no more tokens to consume.

Link copied to clipboard

Functions

Link copied to clipboard

Returns a new ParserInput advanced by one position, sharing the same input list and userContext.

Link copied to clipboard
fun current(): I

Returns the token at the current index.