pears/combinators

Types

A predicate function that returns a boolean value for a given input item of type i.

pub type Predicate(i) =
  fn(i) -> Bool

Functions

pub fn alt(
  parser_1: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  parser_2: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))

Tries to apply the first parser, if it fails it applies the second parser.

pub fn any() -> fn(Input(a)) ->
  Result(Parsed(a, a), ParseError(a))

Consumes any single item from the input except for the end of file.

pub fn between(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  open: fn(Input(a)) -> Result(Parsed(a, c), ParseError(a)),
  close: fn(Input(a)) -> Result(Parsed(a, d), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))

Applies the given parser between the open and close parsers, returning the result of the innner parser.

pub fn choice(
  parsers: List(
    fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  ),
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))

Tries to apply the given parsers in order and returns the result of the first one that succeeds.

pub fn do_choice(
  parsers: List(
    fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  ),
  expected: List(String),
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))
pub fn eof() -> fn(Input(a)) ->
  Result(Parsed(a, Nil), ParseError(a))

Consumes the end of the input and returns a Nil value. Fails if there is any input left.

pub fn just(
  item: a,
) -> fn(Input(a)) -> Result(Parsed(a, a), ParseError(a))

Consumes a single item from the input that is equal to the given item.

pub fn labelled(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  label: String,
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))
pub fn lazy(
  f: fn() -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))

Lazily applies the given parser allowing for recursive parsers.

Examples

Parsing a recursive list of chars:

pub type Tree(a) {
  Leaf(a)
  Node(List(Tree(a)))
}

fn tree_parser() -> Parser(Char, Tree(Int)) {
  let tree = lazy(tree_parser)
  let leaf = map(number(), Leaf)
  let node =
    tree
    |> sep_by0(just(","))
    |> between(just("["), just("]"))
    |> map(Node)
  alt(leaf, node)
}
pub fn left(
  parser_1 p1: fn(Input(a)) ->
    Result(Parsed(a, b), ParseError(a)),
  parser_2 p2: fn(Input(a)) ->
    Result(Parsed(a, c), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))

Applies the first parser and then the second parser, returning the result of the first parser.

pub fn many0(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, List(b)), ParseError(a))

Applies the given parser zero or more times.

pub fn many1(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, List(b)), ParseError(a))

Applies the given parser one or more times.

pub fn map(
  p: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  fun: fn(b) -> c,
) -> fn(Input(a)) -> Result(Parsed(a, c), ParseError(a))

Maps the result of a parser to a new value using a mapper function.

pub fn map_error(
  p: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  fun: fn(ParseError(a)) -> ParseError(a),
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))

Maps the error of a parser to a new error using a mapper function.

pub fn maybe(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, Option(b)), ParseError(a))

Applies the given parser and wraps the result in an Option, returning None if the parser fails instead of an error.

pub fn none_of(
  items: List(a),
) -> fn(Input(a)) -> Result(Parsed(a, a), ParseError(a))

Consumes an item that is not included in the given list.

pub fn one_of(
  items: List(a),
) -> fn(Input(a)) -> Result(Parsed(a, a), ParseError(a))

Consumes an item that is included in the given list.

pub fn pair(
  parser_1 p1: fn(Input(a)) ->
    Result(Parsed(a, b), ParseError(a)),
  parser_2 p2: fn(Input(a)) ->
    Result(Parsed(a, c), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, #(b, c)), ParseError(a))
pub fn recognize(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, List(a)), ParseError(a))

Creates a parser that returns the consumed tokens instead of it’s parsed value.

pub fn right(
  parser_1 p1: fn(Input(a)) ->
    Result(Parsed(a, b), ParseError(a)),
  parser_2 p2: fn(Input(a)) ->
    Result(Parsed(a, c), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, c), ParseError(a))

Applies the first parser and then the second parser, returning the result of the second parser.

pub fn satisfying(
  f: fn(a) -> Bool,
) -> fn(Input(a)) -> Result(Parsed(a, a), ParseError(a))

Consumes a single item from the input that satisfies the given predicate function.

pub fn sep_by0(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  separator: fn(Input(a)) -> Result(Parsed(a, c), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, List(b)), ParseError(a))

Parses zero or more occurrences of the given parser separated by the given separator.

pub fn sep_by1(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  separator: fn(Input(a)) -> Result(Parsed(a, c), ParseError(a)),
) -> fn(Input(a)) -> Result(Parsed(a, List(b)), ParseError(a))

Parses one or more occurrences of the given parser separated by the given separator.

pub fn seq(
  parsers: List(
    fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  ),
) -> fn(Input(a)) -> Result(Parsed(a, List(b)), ParseError(a))

Applies the given parsers in sequence and returns a list of the results

pub fn to(
  parser: fn(Input(a)) -> Result(Parsed(a, b), ParseError(a)),
  value: c,
) -> fn(Input(a)) -> Result(Parsed(a, c), ParseError(a))

Maps the result of a parser to a constant value.

pub fn unwrap(
  parser: fn(Input(a)) ->
    Result(Parsed(a, Option(b)), ParseError(a)),
  default: b,
) -> fn(Input(a)) -> Result(Parsed(a, b), ParseError(a))

Maps a parser that returns an Option to a parser that returns a value or a default value.

Search Document