Package cc.ekblad.konbini

Types

Link copied to clipboard
data class Chain<T, S>(val terms: List<T>, val separators: List<S>)

A parsed chain of elements, and the separators separating each element of the chain.

Link copied to clipboard
typealias Parser<T> = ParserState.() -> T
Link copied to clipboard
sealed class ParserResult<in T>

The result of parsing a string.

Link copied to clipboard
open class ParserState

Represents an ongoing parsing computation.

Functions

Link copied to clipboard
inline fun <T> atomically(crossinline p: Parser<T>): Parser<T>

Creates an atomically parser.

inline fun <T> ParserState.atomically(p: Parser<T>): T

Executes the given parser atomically. The parser either succeeds, or the entire parser fails.

Link copied to clipboard
inline fun <B, T> bracket(    crossinline before: Parser<B>,     crossinline after: Parser<B>,     crossinline p: Parser<T>): ParserState.() -> T

Creates a ParserState.bracket parser.

inline fun <B, T> ParserState.bracket(    crossinline before: Parser<B>,     crossinline after: Parser<B>,     crossinline p: Parser<T>): T

Parses p, requiring it to be preceded and followed by the before and after chars respectively. If any part of the parser fails, the entire parser fails. Returns the result of p.

Link copied to clipboard
inline fun <T, S> chain(crossinline p: Parser<T>, crossinline separator: Parser<S>): ParserState.() -> Chain<T, S>

Creates a ParserState.chain parser.

inline fun <T, S> ParserState.chain(crossinline p: Parser<T>, crossinline separator: Parser<S>): Chain<T, S>

Parses zero or more instances of p, separated by separator, and returns the elements with their respective separators. As this parser potentially matches zero elements, it will never fail.

Link copied to clipboard
inline fun <T, S> chain1(crossinline p: Parser<T>, crossinline separator: Parser<S>): ParserState.() -> Chain<T, S>

Creates a ParserState.chain1 parser.

inline fun <T, S> ParserState.chain1(crossinline p: Parser<T>, crossinline separator: Parser<S>): Chain<T, S>

Parses one or more instances of p, separated by separator, and returns the elements with their respective separators.

Link copied to clipboard
inline fun <T, S> chainl(    crossinline p: Parser<T>,     crossinline separator: Parser<S>,     crossinline combine: (T, T, S) -> T): ParserState.() -> T

Creates a ParserState.chainl parser.

inline fun <T, S> ParserState.chainl(    crossinline p: Parser<T>,     crossinline separator: Parser<S>,     crossinline combine: (T, T, S) -> T): T

Parses one or more instances of p, separated by separator, and combines the elements left-to-right using the given combine function.

Link copied to clipboard
inline fun <T, S> chainr(    crossinline p: Parser<T>,     crossinline separator: Parser<S>,     crossinline combine: (T, T, S) -> T): ParserState.() -> T

Creates a ParserState.chainr parser.

inline fun <T, S> ParserState.chainr(    crossinline p: Parser<T>,     crossinline separator: Parser<S>,     crossinline combine: (T, T, S) -> T): T

Parses one or more instances of p, separated by separator, and combines the elements right-to-left using the given combine function.

Link copied to clipboard
inline fun char(vararg expected: Char): ParserState.() -> Char

Creates a ParserState.char parser.

inline fun ParserState.char(vararg expected: Char): Char

Consumes the next character and fails if it is not in the given list of expected characters. If the list of expected characters is empty, the parser will match any character.

Link copied to clipboard
inline fun fail(reason: String): ParserState.() -> Nothing

Creates a ParserState.fail parser.

Link copied to clipboard
inline fun <T> Parser<T>.failsWith(msg: String): Parser<T>

Replaces the error message of the receiver parser with msg. Position information is unaffected.

Link copied to clipboard
inline fun <T> many(crossinline p: Parser<T>): ParserState.() -> List<T>

Creates a ParserState.many parser.

inline fun <T> ParserState.many(crossinline p: Parser<T>): List<T>

Parses zero or more instances of p. As this parser potentially matches zero elements, it will never fail.

Link copied to clipboard
inline fun <T> many1(noinline p: Parser<T>): ParserState.() -> List<T>

Creates a ParserState.many1 parser.

inline fun <T> ParserState.many1(crossinline p: Parser<T>): List<T>

Atomically parses one or more instances of p.

Link copied to clipboard
inline fun <T, U> Parser<T>.map(crossinline f: (T) -> U): Parser<U>

Applies the given function to the result of the receiver parser if it succeeds.

Link copied to clipboard
inline fun <T> oneOf(vararg ps: Parser<T>): ParserState.() -> T
inline fun <T> oneOf(vararg ps: Pair<String, Parser<T>>): ParserState.() -> T

Creates a ParserState.oneOf parser.

inline fun <T> ParserState.oneOf(vararg ps: Parser<T>): T

Tries the given parsers in order, returning the result of the first one to succeed. Fails with the error message of the last parser, if none of the given parsers succeed.

inline fun <T> ParserState.oneOf(vararg ps: Pair<String, Parser<T>>): T

Tries the given parsers in order, returning the result of the first one to succeed. Fails if none of the given parsers succeed.

Link copied to clipboard
fun <T> Parser<T>.parse(input: String, skipWhitespace: Boolean = false): ParserResult<T>

Applies the receiver parser to the given input. If skipWhitespace is true, any whitespace at the beginning of the input is skipped.

fun <S : ParserState, T> S.() -> T.parse(    input: String,     skipWhitespace: Boolean = false,     state: S): ParserResult<T>

Like parse, but with a custom parser state.

Link copied to clipboard
fun <T> parser(p: ParserState.() -> T): Parser<T>

Create a parser that performs the given parsing computation.

Link copied to clipboard
fun <T> Parser<T>.parseToEnd(input: String, ignoreWhitespace: Boolean = false): ParserResult<T>

Applies the receiver parser to the given input. If ignoreWhitespace is true, any whitespace at the beginning or end of the input is ignored.

fun <S : ParserState, T> S.() -> T.parseToEnd(    input: String,     ignoreWhitespace: Boolean = false,     state: S): ParserResult<T>

Like parseToEnd, but with a custom parser state.

Link copied to clipboard
inline fun regex(pattern: String): Parser<String>
inline fun regex(pattern: Regex): Parser<String>

Creates ParserState.regex parser.

inline fun ParserState.regex(pattern: String): String

Matches the given regular expression pattern, returning the text that matched it. Fails if pattern could not be matched at the current point in the parser input.

Link copied to clipboard
inline fun string(expected: String): ParserState.() -> String

Creates a ParserState.string parser.

Link copied to clipboard
inline fun <T, U> Parser<T>.then(crossinline p: Parser<U>): Parser<Pair<T, U>>

Applies the receiver parser, then applies p. This parser is not atomic.

Link copied to clipboard
inline fun <T> tryParse(crossinline p: Parser<T>): Parser<T?>

Creates a tryParse parser.

inline fun <T> ParserState.tryParse(p: Parser<T>): T?

Executes the given parser atomically. If it succeeds, the result of p is returned. If it fails, null is returned.

Properties

Link copied to clipboard
val boolean: Parser<Boolean>

Caser-sensitive parser that matches either true and false.

Link copied to clipboard
var char: Parser<Char>

Creates a parser which matches exactly one character.

Link copied to clipboard
val decimal: Parser<Double>

Parser that matches a floating point number. Supports both decimal and scientific notation.

Link copied to clipboard
val doubleQuotedString: Parser<String>

Parser matching a double-quoted string, possibly containing escape codes. Supported escape codes are \\, \", \n, \r, \t, and \b.

Link copied to clipboard
val integer: Parser<Long>

Parser that matches a 64-bit signed integer in base 10.

Link copied to clipboard
val singleQuotedString: Parser<String>

Parser matching a single-quoted string, possibly containing escape codes. Supported escape codes are \\, \', \n, \r, \t, and \b.

Link copied to clipboard
val whitespace: Parser<String>

Parser matching zero or more whitespace characters.

Link copied to clipboard
val whitespace1: Parser<String>

Parser matching one or more whitespace characters.