std.parser

A simple parser combinator library.

Types

type Position = Int

type Error = { position : Position, message : String }

type ParseResult a = Result Error { value : a, buffer : OffsetString }

type Parser a = OffsetString -> ParseResult a

Parser is a monad which parses a String into structured values

Values

let functor : Functor Parser

A Functor represents an action on a parameterized type which does not change the structure with the mapped type.

The following laws should hold:

  • map id == id
  • map (f << g) == map f << map g

let applicative : Applicative Parser

A Functor with application.

The following laws should hold:

  • wrap id <*> v = v
  • wrap (<<) <> u <> v <> w = u <> (v <*> w)
  • wrap f <*> wrap x = wrap (f x)
  • u <> wrap y = wrap (\g -> g x) <> u

let alternative : Alternative Parser

A monoid on applicative functors.

let monad : Monad Parser

A generalised interface for imperatively sequencing actions

let parser : forall a . Parser a -> Parser a

The identity function, where id x == x

let any : Parser Char

Parses any character. Only errors if the stream is out of input

let between l r x : forall c b a . Parser a -> Parser b -> Parser c -> Parser c

Parses x between l and r, returning the result of x

let token expected : Char -> Parser Char

Succeeds if the next token is expected

let many p : forall a . Parser a -> Parser (List a)

Parses with p zero or more times

let many1 p : forall a . Parser a -> Parser (List a)

Parses with p one or more times

let satisfy predicate : (Char -> Bool) -> Parser Char

Succeeds if predicate returns True, fails if False is returned

let satisfy_map predicate : forall a . (Char -> Option a) -> Parser a

Succeeds if predicate returns Some, fails if None is returned

let spaces : Parser String

Skips over whitespace characters

let take1 predicate : (Char -> Bool) -> Parser String

Parses one or more tokens passing predicate and returns the String between the start and end of those tokens

let take predicate : (Char -> Bool) -> Parser String

Parses zero or more tokens passing predicate and returns the String between the start and end of those tokens

let lazy_parser f : forall a . (() -> Parser a) -> Parser a

Creates a parser from a factory function. Useful to prevent mutually recursive parser from looping forever

let fail message : forall a . String -> Parser a

Fails the parser with message as the cause

let recognize p : forall a . Parser a -> Parser String

Parses using p and returns the String between the start and of what p parsed

let skip_many p : forall a . Parser a -> Parser ()

Parses with p zero or more times, ignoring the result of the parser

let skip_many1 p : forall a . Parser a -> Parser ()

Parses with p one or more times, ignoring the result of the parser

let one_of s : String -> Parser Char

Parses one of the characters of s

let sep_by parser sep : forall b a . Parser a -> Parser b -> Parser (List a)

Parses parser separated by sep

let sep_by1 parser sep : forall b a . Parser a -> Parser b -> Parser (List a)

Parses at least one element of parser separated by sep

let chainl1 p op : forall a . Parser a -> Parser (a -> a -> a) -> Parser a

Like sep_by1 but applies the function returned by op on the left fold of successive parses

let chainl p op v : forall a . Parser a -> Parser (a -> a -> a) -> a -> Parser a

Like sep_by but applies the function returned by op on the left fold of successive parses

let <?> p message : forall a . Parser a -> String -> Parser a

Returns message as what was expected by p

let alpha_num : Parser Char

Succeds if the next token is alphanumeric

let letter : Parser Char

Succeds if the next token is a letter

let digit : Parser Char

Succeds if the next token is a digit

let space : Parser Char

Succeds if the next token is a space

let tab : Parser Char

Succeds if the next token is a tab

let parse p input : forall a . Parser a -> String -> Result String a

Parses input using p