Package-level declarations

Types

Link copied to clipboard
data class Failure<out I : Any, U : Any>(val message: String, val index: Int, val input: ParserInput<I, U>) : ParseResult<I, Nothing, U>

Indicates that the parser did not match.

Link copied to clipboard
fun interface Parser<in I : Any, out O, U : Any>

A parser that consumes tokens of type I and produces a value of type O, with access to an arbitrary user-defined context of type U.

Link copied to clipboard
sealed class ParseResult<out I : Any, out O, U : Any>

The result of running a Parser against a ParserInput.

Link copied to clipboard
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.

Link copied to clipboard
data class Success<out I : Any, out O, U : Any>(val value: O, val nextIndex: Int, val input: ParserInput<I, U>) : ParseResult<I, O, U>

Indicates that the parser matched successfully.

Properties

Link copied to clipboard
val <I : Any, O, U : Any> Parser<I, O, U>.lookAhead: Parser<I, O, U>

Positive lookahead — runs this parser and returns its value without consuming any input.

Link copied to clipboard
val <I : Any, O, U : Any> Parser<I, O, U>.many: Parser<I, List<O>, U>

Zero-or-more: runs this parser repeatedly until it fails, collecting results into a List. Always succeeds.

Link copied to clipboard
val <I : Any, O, U : Any> Parser<I, O, U>.many1: Parser<I, List<O>, U>

One-or-more: runs this parser repeatedly until it fails. Fails if this parser does not match at least once.

Link copied to clipboard
val <I : Any, O : Any, U : Any> Parser<I, O, U>.optional: Parser<I, O?, U>

Zero-or-one: succeeds with the parsed value or null if this parser fails.

Functions

Link copied to clipboard
fun <I : Any, O, R, U : Any> Parser<I, O, U>.bind(transform: (O) -> Parser<I, R, U>): Parser<I, R, U>

Flat-maps the success value of this parser to a second parser, then runs it.

Link copied to clipboard
infix fun <I : Any, O, U : Any> Parser<I, O, U>.label(message: String): Parser<I, O, U>

Replaces this parser's failure message with message on failure.

Link copied to clipboard
fun <I : Any, O, R, U : Any> Parser<I, O, U>.map(transform: (O) -> R): Parser<I, R, U>

Transforms the success value of this parser using transform.

Link copied to clipboard
operator fun <I : Any, O, U : Any> Parser<I, O, U>.not(): Parser<I, Unit, U>

Negative lookahead — succeeds with Unit when this parser would fail, fails when this parser would succeed. Never consumes input.

Link copied to clipboard
infix fun <I : Any, O, U : Any> Parser<I, O, U>.or(other: Parser<I, O, U>): Parser<I, O, U>

Tries this parser and, if it fails, tries other at the same position.

Link copied to clipboard
fun <I : Any, O1, O2, U : Any> pAnd(first: Parser<I, O1, U>, second: Parser<I, O2, U>): Parser<I, Pair<O1, O2>, U>

Returns a Parser that runs first and then second in sequence, combining their outputs into a Pair.

Link copied to clipboard
fun <I : Any, U : Any> pAny(): Parser<I, I, U>

Returns a Parser that consumes and returns any single token, failing only when the input is exhausted.

Link copied to clipboard
fun <I : Any, O1, O2, O3, U : Any> pBetween(open: Parser<I, O1, U>, close: Parser<I, O3, U>, inner: Parser<I, O2, U>): Parser<I, O2, U>

Returns a Parser that runs open, then inner, then close in sequence, succeeding with inner's value and discarding the delimiter values.

Link copied to clipboard
fun <I : Any, O, R, U : Any> pBind(parser: Parser<I, O, U>, transform: (O) -> Parser<I, R, U>): Parser<I, R, U>

Returns a Parser that runs parser and, on success, passes the output value to transform to obtain and immediately run a second parser.

Link copied to clipboard
fun <I : Any, O, U : Any> pChoice(vararg parsers: Parser<I, O, U>): Parser<I, O, U>

Vararg overload of pChoice.

fun <I : Any, O, U : Any> pChoice(parsers: List<Parser<I, O, U>>): Parser<I, O, U>

Returns a Parser that tries each parser in parsers in order, returning the first success.

Link copied to clipboard
fun <I : Any, U : Any> pEof(): Parser<I, Unit, U>

Returns a Parser that succeeds with Unit only when all input has been consumed, and fails if any tokens remain.

Link copied to clipboard
fun <I : Any, O, U : Any> pLabel(parser: Parser<I, O, U>, message: String): Parser<I, O, U>

Returns a Parser that runs parser and, if it fails, replaces the failure message with message.

Link copied to clipboard
fun <I : Any, O, U : Any> pLookAhead(parser: Parser<I, O, U>): Parser<I, O, U>

Returns a Parser that runs parser and, if it succeeds, returns its value without consuming any input.

Link copied to clipboard
operator fun <I : Any, O1, O2, U : Any> Parser<I, O1, U>.plus(other: Parser<I, O2, U>): Parser<I, Pair<O1, O2>, U>

Sequences this parser with other, collecting both outputs into a Pair.

Link copied to clipboard
fun <I : Any, O, U : Any> pMany(parser: Parser<I, O, U>): Parser<I, List<O>, U>

Returns a Parser that runs parser repeatedly until it fails, collecting each output into a List. Always succeeds, returning an empty list if parser fails on the first attempt.

Link copied to clipboard
fun <I : Any, O, U : Any> pMany1(parser: Parser<I, O, U>): Parser<I, List<O>, U>

Returns a Parser that runs parser one or more times, collecting each output into a List. Fails if parser does not match at least once.

Link copied to clipboard
fun <I : Any, O, R, U : Any> pMap(parser: Parser<I, O, U>, transform: (O) -> R): Parser<I, R, U>

Returns a Parser that runs parser and applies transform to the output value if it succeeds.

Link copied to clipboard
fun <I : Any, O, U : Any> pNot(parser: Parser<I, O, U>): Parser<I, Unit, U>

Returns a Parser that succeeds with Unit when parser would fail at the current position, and fails when parser would succeed. No input is consumed in either case.

Link copied to clipboard
fun <I : Any, O : Any, U : Any> pOptional(parser: Parser<I, O, U>): Parser<I, O?, U>

Returns a Parser that tries parser and, if it fails, succeeds with null at the same position.

Link copied to clipboard
fun <I : Any, O, U : Any> pOr(first: Parser<I, O, U>, second: Parser<I, O, U>): Parser<I, O, U>

Returns a Parser that tries first and, if it fails, tries second at the same position.

Link copied to clipboard
fun <I : Any, O, U : Any> pRepeat(count: Int, parser: Parser<I, O, U>): Parser<I, List<O>, U>

Returns a Parser that runs parser exactly count times in sequence, collecting each output into a List.

Link copied to clipboard
fun <I : Any, U : Any> pSatisfy(predicate: (I) -> Boolean): Parser<I, I, U>

Returns a Parser that consumes exactly one token if predicate returns true for it, and fails otherwise.

Link copied to clipboard
fun <I : Any, O, S, U : Any> pSepBy(item: Parser<I, O, U>, separator: Parser<I, S, U>): Parser<I, List<O>, U>

Returns a Parser that parses zero or more occurrences of item separated by separator, collecting the item values (not the separators) into a List.

Link copied to clipboard
fun <I : Any, O, S, U : Any> pSepBy1(item: Parser<I, O, U>, separator: Parser<I, S, U>): Parser<I, List<O>, U>

Returns a Parser that parses one or more occurrences of item separated by separator, collecting the item values (not the separators) into a non-empty List.

Link copied to clipboard
fun <I : Any, O, U : Any> pSequence(parsers: List<Parser<I, O, U>>): Parser<I, List<O>, U>

Returns a Parser that runs each parser in parsers in order, collecting every output into a List.

Link copied to clipboard
infix fun <I : Any, O, S, U : Any> Parser<I, O, U>.sepBy(separator: Parser<I, S, U>): Parser<I, List<O>, U>

Parses zero or more occurrences of this parser separated by separator, collecting item values (not separators) into a List.

Link copied to clipboard
infix fun <I : Any, O, S, U : Any> Parser<I, O, U>.sepBy1(separator: Parser<I, S, U>): Parser<I, List<O>, U>

Parses one or more occurrences of this parser separated by separator, collecting item values (not separators) into a non-empty List.

Link copied to clipboard
operator fun <I : Any, O, U : Any> Parser<I, O, U>.times(count: Int): Parser<I, List<O>, U>

Repeats this parser exactly count times, collecting outputs into a List.