Parser
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.
Charfor character-level parsing. Contravariant: aParser<Any, O, U>can be used wherever aParser<Char, O, U>is expected.O — the output value type produced on success. Covariant; may be nullable. A
Parser<I, String, U>satisfiesParser<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
Functions
Runs this parser against input.