kex docs Standard Library

Parsing

module Parsing

Parser combinators for user-defined text formats.

An Input is immutable: successful parsers return the parsed value together with an advanced cursor. Failure leaves the caller free to try an alternative.

type ParseError

Expected carries what the grammar WANTED rather than what it found — the difference between "unexpected d" and "expected version(". It is what label and string report.

Variants

  • Unexpected(String, Integer)
  • Expected(String, Integer)
  • NoMatch(Integer)

record Input

Fields

input
String
pos
Integer optional

Methods

peekAt

peekAt(offset: Integer) -> Char?

advanceBy

advanceBy(count: Integer) -> Input

charWhen

charWhen(pred: (Char -> Bool)) -> Result<(Char, Input), ParseError>

char

char(expected: Char) -> Result<(Char, Input), ParseError>

whiteSpaces

whiteSpaces : Input

many

many(f: (Input -> Result<(T, Input), ParseError>)) -> ([T], Input)

some

some(f: (Input -> Result<(T, Input), ParseError>)) -> Result<([T], Input), ParseError>

string

string(expected: String) -> Result<(String, Input), ParseError>

An exact literal. A keyword grammar is mostly literals — version( is one token to a reader and eight calls to char — and matching it here reports the failure at the START of the literal, which is where a person looking at the error expects the caret.

takeWhile

takeWhile(pred: (Char -> Bool)) -> (String, Input)

Every character while pred holds, as a String. many(charWhen(...)) gives a [Char] the caller has to join, and a run of characters is almost always wanted as text. Cannot fail: an empty run is an empty String, which is what makes it safe for the optional parts of a grammar.

choice

choice(alts: ([(Input) -> Result<(T, Input), ParseError>])) -> Result<(T, Input), ParseError>