Parser

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.

Parser is a functional interface — any lambda or function reference with the matching signature (ParserInput<I, U>) -> ParseResult<I, O, U> can be used directly as a Parser.

Type parameters

  • I — the token (input element) type, e.g. Char for character-level parsing. Contravariant: a Parser<Any, O, U> can be used wherever a Parser<Char, O, U> is expected.

  • O — the output value type produced on success. Covariant; may be nullable. A Parser<I, String, U> satisfies Parser<I, CharSequence?, U>.

  • U — the user context type threaded through parsing without being consumed. Invariant: use Unit when no context is needed.

Example

// A parser that always succeeds and returns the current index
val position: Parser<Char, Int, Unit> = Parser { input -> Success(input.index, input.index, input) }

See also

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
abstract operator fun invoke(input: ParserInput<@UnsafeVariance I, U>): ParseResult<@UnsafeVariance I, O, U>

Runs this parser against input.

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
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
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.