parser_gleam/parser

Types

pub type Parser(i, a) =
  fn(Stream(i)) -> ParseResult(i, a)

Functions

pub fn alt(fa: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), that: fn() ->
    fn(Stream(a)) -> Result(ParseSuccess(a, b), ParseError(a))) -> fn(
  Stream(a),
) -> Result(ParseSuccess(a, b), ParseError(a))
pub fn ap(mab: fn(Stream(a)) ->
    Result(ParseSuccess(a, fn(b) -> c), ParseError(a)), ma: fn(
    Stream(a),
  ) -> Result(ParseSuccess(a, b), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, c), ParseError(a))
pub fn ap_first(fb: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(
  fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a)),
) -> fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a))
pub fn ap_second(fb: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(
  fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a)),
) -> fn(Stream(a)) -> Result(ParseSuccess(a, b), ParseError(a))
pub fn between(left: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), right: fn(
    Stream(a),
  ) -> Result(ParseSuccess(a, b), ParseError(a))) -> fn(
  fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a)),
) -> fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a))

Matches the provided parser p that occurs between the provided left and right parsers.

p is polymorphic in its return type, because in general bounds and actual parser could return different types.

pub fn chain(ma: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), f: fn(b) ->
    fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a))) -> fn(
  Stream(a),
) -> Result(ParseSuccess(a, c), ParseError(a))
pub fn chain_first(ma: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), f: fn(b) ->
    fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a))) -> fn(
  Stream(a),
) -> Result(ParseSuccess(a, b), ParseError(a))
pub fn chain_rec(a: a, f: fn(a) ->
    fn(Stream(b)) ->
      Result(ParseSuccess(b, Result(c, a)), ParseError(b))) -> fn(
  Stream(b),
) -> Result(ParseSuccess(b, c), ParseError(b))
pub fn cut(p: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, b), ParseError(a))

The cut parser combinator takes a parser and produces a new parser for which all errors are fatal, causing either to stop trying further parsers and return immediately with a fatal error.

pub fn cut_with(p1: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), p2: fn(Stream(a)) ->
    Result(ParseSuccess(a, c), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, c), ParseError(a))

Takes two parsers p1 and p2, returning a parser which will match p1 first, discard the result, then either match p2 or produce a fatal error.

pub fn either(p: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), f: fn() ->
    fn(Stream(a)) -> Result(ParseSuccess(a, b), ParseError(a))) -> fn(
  Stream(a),
) -> Result(ParseSuccess(a, b), ParseError(a))

The either combinator takes two parsers, runs the first on the input stream, and if that fails, it will backtrack and attempt the second parser on the same input. Basically, try parser 1, then try parser 2.

If the first parser fails with an error flagged as fatal (see cut), the second parser will not be attempted.

This is equivalent to the alt operation.

pub fn eof() -> fn(Stream(a)) ->
  Result(ParseSuccess(a, Nil), ParseError(a))

Matches the end of the stream.

pub fn expected(p: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), message: String) -> fn(
  Stream(a),
) -> Result(ParseSuccess(a, b), ParseError(a))

A parser combinator which returns the provided parser unchanged, except that if it fails, the provided error message will be returned in the ParseError`.

pub fn fail() -> fn(Stream(a)) ->
  Result(ParseSuccess(a, b), ParseError(a))

The fail parser will just fail immediately without consuming any input

pub fn fail_at(i: Stream(a)) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, b), ParseError(a))

The failAt parser will fail immediately without consuming any input, but will report the failure at the provided input position.

pub fn filter(predicate: fn(a) -> Bool) -> fn(
  fn(Stream(b)) -> Result(ParseSuccess(b, a), ParseError(b)),
) -> fn(Stream(b)) -> Result(ParseSuccess(b, a), ParseError(b))

Filters the result of a parser based upon a Refinement or a Predicate.

pub fn flatten(mma: fn(Stream(a)) ->
    Result(
      ParseSuccess(
        a,
        fn(Stream(a)) ->
          Result(ParseSuccess(a, b), ParseError(a)),
      ),
      ParseError(a),
    )) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, b), ParseError(a))
pub fn get_monoid(m: Monoid(a)) -> Monoid(
  fn(Stream(b)) -> Result(ParseSuccess(b, a), ParseError(b)),
)
pub fn get_semigroup(s: Semigroup(a)) -> Semigroup(
  fn(Stream(b)) -> Result(ParseSuccess(b, a), ParseError(b)),
)
pub fn item() -> fn(Stream(a)) ->
  Result(ParseSuccess(a, a), ParseError(a))

The item parser consumes a single value, regardless of what it is, and returns it as its result.

pub fn look_ahead(p: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, b), ParseError(a))

Takes a Parser and tries to match it without consuming any input.

pub fn many(p: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, List(b)), ParseError(a))

The many combinator takes a parser, and returns a new parser which will run the parser repeatedly on the input stream until it fails, returning a list of the result values of each parse operation as its result, or the empty list if the parser never succeeded.

Read that as “match this parser zero or more times and give me a list of the results.”

pub fn many1(parser: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, NonEmptyList(b)), ParseError(a))

The many1 combinator is just like the many combinator, except it requires its wrapped parser to match at least once. The resulting list is thus guaranteed to contain at least one value.

pub fn many1_till(parser: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), terminator: fn(
    Stream(a),
  ) -> Result(ParseSuccess(a, c), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, NonEmptyList(b)), ParseError(a))

The many1Till combinator is just like the manyTill combinator, except it requires the value parser to match at least once before the terminator parser. The resulting list is thus guaranteed to contain at least one value.

pub fn many_till(parser: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), terminator: fn(
    Stream(a),
  ) -> Result(ParseSuccess(a, c), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, List(b)), ParseError(a))

The manyTill combinator takes a value parser and a terminator parser, and returns a new parser that will run the value parser repeatedly on the input stream, returning a list of the result values of each parse operation as its result, or the empty list if the parser never succeeded.

pub fn map(ma: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), f: fn(b) -> c) -> fn(
  Stream(a),
) -> Result(ParseSuccess(a, c), ParseError(a))
pub fn maybe(m: Monoid(a)) -> fn(
  fn(Stream(b)) -> Result(ParseSuccess(b, a), ParseError(b)),
) -> fn(Stream(b)) -> Result(ParseSuccess(b, a), ParseError(b))

The maybe parser combinator creates a parser which will run the provided parser on the input, and if it fails, it will returns the empty value (as defined by empty) as a result, without consuming any input.

pub fn of(a: a) -> fn(Stream(b)) ->
  Result(ParseSuccess(b, a), ParseError(b))
pub fn optional(parser: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, Option(b)), ParseError(a))

Returns Some<A> if the specified parser succeeds, otherwise returns None.

pub fn sat(predicate: fn(a) -> Bool) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, a), ParseError(a))

The sat parser constructor takes a predicate function, and will consume a single character if calling that predicate function with the character as its argument returns true. If it returns false, the parser will fail.

pub fn sep_by(sep: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), p: fn(Stream(a)) ->
    Result(ParseSuccess(a, c), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, List(c)), ParseError(a))

Matches the provided parser p zero or more times, but requires the parser sep to match once in between each match of p. In other words, use sep to match separator characters in between matches of p.

pub fn sep_by1(sep: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), p: fn(Stream(a)) ->
    Result(ParseSuccess(a, c), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, NonEmptyList(c)), ParseError(a))

Matches the provided parser p one or more times, but requires the parser sep to match once in between each match of p. In other words, use sep to match separator characters in between matches of p.

pub fn sep_by_cut(sep: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), p: fn(Stream(a)) ->
    Result(ParseSuccess(a, c), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, NonEmptyList(c)), ParseError(a))

Like sepBy1, but cut on the separator, so that matching a sep not followed by a p will cause a fatal error.

pub fn seq(fa: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a)), f: fn(b) ->
    fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a))) -> fn(
  Stream(a),
) -> Result(ParseSuccess(a, c), ParseError(a))

The seq combinator takes a parser, and a function which will receive the result of that parser if it succeeds, and which should return another parser, which will be run immediately after the initial parser. In this way, you can join parsers together in a sequence, producing more complex parsers.

This is equivalent to the monadic chain operation.

pub fn succeed(a: a) -> fn(Stream(b)) ->
  Result(ParseSuccess(b, a), ParseError(b))

The succeed parser constructor creates a parser which will simply return the value provided as its argument, without consuming any input.

This is equivalent to the monadic of.

pub fn surrounded_by(bound: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(
  fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a)),
) -> fn(Stream(a)) -> Result(ParseSuccess(a, c), ParseError(a))

Matches the provided parser p that is surrounded by the bound parser. Shortcut for between(bound, bound).

pub fn take_until(predicate: fn(a) -> Bool) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, List(a)), ParseError(a))

Takes a Predicate and continues parsing until the given Predicate is satisfied.

pub fn with_start(p: fn(Stream(a)) ->
    Result(ParseSuccess(a, b), ParseError(a))) -> fn(Stream(a)) ->
  Result(ParseSuccess(a, #(b, Stream(a))), ParseError(a))

Converts a parser into one which will return the point in the stream where it started parsing in addition to its parsed value.

Useful if you want to keep track of where in the input stream a parsed token came from.

pub fn zero() -> fn(Stream(a)) ->
  Result(ParseSuccess(a, b), ParseError(a))
Search Document