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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  parser_2: fn(List(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(List(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(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  open: fn(List(a)) -> Result(Parsed(a, c), ParseError(a)),
  close: fn(List(a)) -> Result(Parsed(a, d), ParseError(a)),
) -> fn(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  ),
) -> fn(List(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 eof() -> fn(List(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(List(a)) -> Result(Parsed(a, a), ParseError(a))

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

pub fn lazy(
  f: fn() -> fn(List(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  parser_2 p2: fn(List(a)) -> Result(Parsed(a, c), ParseError(a)),
) -> fn(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(List(a)) -> Result(Parsed(a, List(b)), ParseError(a))

Applies the given parser zero or more times.

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

Applies the given parser one or more times.

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

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

pub fn maybe(
  parser: fn(List(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(List(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(List(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(List(a)) -> Result(Parsed(a, a), ParseError(a))

Consumes an item that is included in the given list.

pub fn pair(
  parser_1 p1: fn(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  parser_2 p2: fn(List(a)) -> Result(Parsed(a, c), ParseError(a)),
) -> fn(List(a)) -> Result(Parsed(a, #(b, c)), ParseError(a))
pub fn recognize(
  parser: fn(List(a)) -> Result(Parsed(a, b), ParseError(a)),
) -> fn(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  parser_2 p2: fn(List(a)) -> Result(Parsed(a, c), ParseError(a)),
) -> fn(List(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(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  separator: fn(List(a)) -> Result(Parsed(a, c), ParseError(a)),
) -> fn(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  separator: fn(List(a)) -> Result(Parsed(a, c), ParseError(a)),
) -> fn(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  ),
) -> fn(List(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(List(a)) -> Result(Parsed(a, b), ParseError(a)),
  value: c,
) -> fn(List(a)) -> Result(Parsed(a, c), ParseError(a))

Maps the result of a parser to a constant value.

pub fn unwrap(
  parser: fn(List(a)) ->
    Result(Parsed(a, Option(b)), ParseError(a)),
  default: b,
) -> fn(List(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