pickle

Types

The type to represent a kind of token that was expected at a specific position of the input that couldn’t be found.

pub type ExpectedToken {
  Eol
  Eof
  NonEof
  Float
  OctalDigit
  BinaryDigit
  DecimalDigit
  HexadecimalDigit
  DecimalDigitOrPoint
  AsciiLetter
  LowercaseAsciiLetter
  UppercaseAsciiLetter
  Whitespace
  String(String)
}

Constructors

  • Eol
  • Eof
  • NonEof
  • Float
  • OctalDigit
  • BinaryDigit
  • DecimalDigit
  • HexadecimalDigit
  • DecimalDigitOrPoint
  • AsciiLetter
  • LowercaseAsciiLetter
  • UppercaseAsciiLetter
  • Whitespace
  • String(String)

Parsed

opaque

The type to store unconsumed tokens, the position of the parser and the current value.

pub opaque type Parsed(a)

The parser type that is an alias for a function that is responsible for parsing input.

pub type Parser(a, b, c) =
  fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c))

The error type to represent a reason why a parser failed to parse the input.

pub type ParserFailure(a) {
  UnexpectedToken(
    expected_token: ExpectedToken,
    actual_token: String,
    pos: ParserPosition,
  )
  UnexpectedEof(
    expected_token: ExpectedToken,
    pos: ParserPosition,
  )
  OneOfError(failures: List(ParserFailure(a)))
  GuardError(error: a, pos: ParserPosition)
  Until1Error(pos: ParserPosition)
  NotError(error: a, pos: ParserPosition)
  CustomError(error: a)
}

Constructors

  • UnexpectedToken(
      expected_token: ExpectedToken,
      actual_token: String,
      pos: ParserPosition,
    )
  • UnexpectedEof(expected_token: ExpectedToken, pos: ParserPosition)
  • OneOfError(failures: List(ParserFailure(a)))
  • GuardError(error: a, pos: ParserPosition)
  • Until1Error(pos: ParserPosition)
  • NotError(error: a, pos: ParserPosition)
  • CustomError(error: a)

The type to represent the current position of the parser.

pub type ParserPosition {
  ParserPosition(row: Int, col: Int)
}

Constructors

  • ParserPosition(row: Int, col: Int)

Functions

pub fn any(
  mapper: fn(a, String) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a single token of any kind and fails if there is no further input left to parse.

pub fn ascii_letter(
  mapper: fn(a, String) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses an ASCII letter.

pub fn binary_digit(
  mapper: fn(a, Int) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a single binary digit (0|1).

pub fn binary_integer(
  mapper: fn(a, Int) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a binary integer.

pub fn digit(
  mapper: fn(a, Int) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a single decimal digit (0-9).

pub fn do(
  initial_value: a,
  parser: fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c)),
  mapper: fn(d, b) -> d,
) -> fn(Parsed(d)) -> Result(Parsed(d), ParserFailure(c))

Applies the initial value to the given parser.

This parser is especially useful when you want or need to transform a value of a different type than the one the parent parser holds (e.g., a type that the AST type of the parent parser can contain).

pub fn drop(value: a, : b) -> a

A mapper to drop the parsed value of the child parser, thus leaving the value of the parent parser unchanged.

pub fn eof() -> fn(Parsed(a)) ->
  Result(Parsed(a), ParserFailure(b))

Parses successfully when there is no further input left to parse.

pub fn eol(
  mapper: fn(a, String) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses an end-of-line character.

pub fn float(
  mapper: fn(a, Float) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a decimal float.

pub fn guard(
  predicate: fn(a) -> Bool,
  error: b,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Validates the value of the parser.

If the validation fails, a GuardError with the given error will be returned.

pub fn hexadecimal_digit(
  mapper: fn(a, Int) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a single hexadecimal digit (0-9, a-f, A-F).

pub fn hexadecimal_integer(
  mapper: fn(a, Int) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a hexadecimal integer.

pub fn integer(
  mapper: fn(a, Int) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a decimal integer.

pub fn lookahead(
  parser: fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c)),
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(c))

Looksahead whether the given parser succeeds and backtracks if it does.

pub fn lowercase_ascii_letter(
  mapper: fn(a, String) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a lowercase ASCII letter.

pub fn many(
  initial_value: a,
  parser: fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c)),
  acc: fn(d, b) -> d,
) -> fn(Parsed(d)) -> Result(Parsed(d), ParserFailure(c))

Applies the initial value to the given parser zero to n times until it fails.

pub fn many1(
  initial_value: a,
  parser: fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c)),
  acc: fn(d, b) -> d,
) -> fn(Parsed(d)) -> Result(Parsed(d), ParserFailure(c))

Applies the initial value to the given parser one to n times until it fails.

The given parser must succeed at least once.

pub fn map(
  mapper: fn(a) -> b,
) -> fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c))

Maps the value of the parser.

pub fn map_error(
  parser: fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b)),
  mapper: fn(ParserFailure(b)) -> b,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Maps the error returned by the parser.

pub fn not(
  parser: fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c)),
  error: c,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(c))

Succeeds and backtracks if the given parser fails.

The error parameter is meant to convey more information to consumers and its value is wrapped in a NotError.

pub fn octal_digit(
  mapper: fn(a, Int) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a single octal digit (0-7).

pub fn octal_integer(
  mapper: fn(a, Int) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses an octal integer.

pub fn one_of(
  parsers: List(
    fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b)),
  ),
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Applies each given parser in order until one succeeds. If all parsers failed, the collected failures wrapped in an OneOfError will be returned.

pub fn optional(
  parser: fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b)),
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Applies the given parser, and if it fails, ignores its failure and backtracks.

pub fn parse(
  input: String,
  initial_value: a,
  parser: fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c)),
) -> Result(b, ParserFailure(c))

Applies the provided input and initial value to the given parser to parse the input and transform the initial value.

pub fn return(
  value: a,
) -> fn(Parsed(b)) -> Result(Parsed(a), ParserFailure(c))

Replaces the value of the parser with the given value.

pub fn skip_until(
  terminator: fn(Parsed(a)) ->
    Result(Parsed(b), ParserFailure(c)),
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(c))

Applies the given parser zero to n times until the given terminator succeeds and drops the parsed tokens.

It fails if the given terminator doesn’t succeed before no further tokens are left to parse.

pub fn skip_until1(
  terminator: fn(Parsed(a)) ->
    Result(Parsed(b), ParserFailure(c)),
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(c))

Applies the given parser one to n times until the given terminator succeeds and drops the parsed tokens.

It fails if no single token could be skipped or the given terminator doesn’t succeed before no further tokens are left to parse.

If the terminator succeeds before no single token could be skipped, an Until1Error with the current parser position will be returned.

pub fn skip_whitespace() -> fn(Parsed(a)) ->
  Result(Parsed(a), ParserFailure(b))

Parses whitespace zero to n times until encountering a non-whitespace token and drops the parsed whitespace.

pub fn skip_whitespace1() -> fn(Parsed(a)) ->
  Result(Parsed(a), ParserFailure(b))

Parses whitespace one to n times until encountering a non-whitespace token and drops the parsed whitespace.

It fails if not at least one whitespace token could be parsed.

pub fn string(
  expected: String,
  mapper: fn(a, String) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses a specific string.

pub fn then(
  prev: fn(Parsed(a)) -> Result(Parsed(b), ParserFailure(c)),
  successor: fn(Parsed(b)) -> Result(Parsed(d), ParserFailure(c)),
) -> fn(Parsed(a)) -> Result(Parsed(d), ParserFailure(c))

Chains two given parsers.

If prev fails, successor won’t be invoked.

pub fn times(
  parser: fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b)),
  parse_times: Int,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Applies the given parser a specified amount of times.

pub fn until(
  initial_value: a,
  parser: fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b)),
  terminator: fn(Parsed(c)) ->
    Result(Parsed(d), ParserFailure(b)),
  acc: fn(c, a) -> c,
) -> fn(Parsed(c)) -> Result(Parsed(c), ParserFailure(b))

Applies the given parser zero to n times until the given terminator succeeds.

It fails if the given parser fails or the given terminator doesn’t succeed before no further tokens are left to parse.

pub fn until1(
  initial_value: a,
  parser: fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b)),
  terminator: fn(Parsed(c)) ->
    Result(Parsed(d), ParserFailure(b)),
  acc: fn(c, a) -> c,
) -> fn(Parsed(c)) -> Result(Parsed(c), ParserFailure(b))

Applies the given parser one to n times until the given terminator succeeds.

It fails if the given parser fails or could not be applied once, or the given terminator doesn’t succeed before no further tokens are left to parse.

If the terminator succeeds before the given parser could succeed, an Until1Error with the current parser position will be returned.

pub fn uppercase_ascii_letter(
  mapper: fn(a, String) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses an uppercase ASCII letter.

pub fn whitespace(
  mapper: fn(a, String) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses whitespace zero to n times until encountering a non-whitespace token.

pub fn whitespace1(
  mapper: fn(a, String) -> a,
) -> fn(Parsed(a)) -> Result(Parsed(a), ParserFailure(b))

Parses whitespace one to n times until encountering a non-whitespace token.

It fails if not at least one whitespace token could be parsed.

Search Document