ParserState

open class ParserState

Represents an ongoing parsing computation.

Inherit this class if you need to keep your own parsing state as well.

Constructors

Link copied to clipboard
fun ParserState()

Functions

Link copied to clipboard
fun char(): Char

Reads the next character in the input. If the parser is already at the end of the input, parsing fails.

Link copied to clipboard
fun eof()

Matches the end of the input string.

Link copied to clipboard
fun fail(msg: String): Nothing

Unconditionally fail parsing with the given message.

Link copied to clipboard
fun propagateLastFailure(newMsg: String? = null): Nothing

Propagate the last thrown parser failure. If newMsg is not null, the failure's reason message is replaced by the given one.

Link copied to clipboard
fun regex(pattern: Regex): String

Attempts to match pattern at the current position in the input, and returns the character sequence that matches, if any. Fails if the pattern could not be matched at this position in the inut.

Link copied to clipboard
fun string(expected: String): String

Reads the next expected.length characters from the input. Fails if they are not exactly expected.

Properties

Link copied to clipboard
lateinit var input: String

The entire input string the parser is working with.

Link copied to clipboard
val next: Char

Inspect the next character in the input string, without advancing the parser position. Fails if the parser is already at the end of the input.

Link copied to clipboard
var position: Int = 0

Current position of the parser.

Link copied to clipboard
val rest: String

Returns the rest of the string being parsed, without advancing the parser position.

Extensions

Link copied to clipboard
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> 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> 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> 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> 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> 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 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 <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> ParserState.many1(crossinline p: Parser<T>): List<T>

Atomically parses one or more instances of p.

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