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 {
  Eof
  Eol
  Float
  Integer
  OctalDigit
  BinaryDigit
  DecimalDigit
  HexadecimalDigit
  DecimalDigitOrPoint
  AsciiLetter
  LowercaseAsciiLetter
  UppercaseAsciiLetter
  Whitespace
  String(String)
}

Constructors

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

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)
  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)
  • 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 apppend_to_string(
  value: String,
  appendage: String,
) -> String

A mapper to append the parsed string of the child parser to the string value of the parent parser.

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

Parses an ASCII letter.

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

Parses a binary integer.

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

Parses a decimal integer.

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.

This function will be adjusted to support different numeric formats in later versions.

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_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 an integer of different numeric formats (binary, decimal, hexadecimal and octal).

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(a), ParserFailure(b)),
  acc: fn(c, a) -> c,
) -> fn(Parsed(c)) -> Result(Parsed(c), ParserFailure(b))

Applies the initial value to the given parser zero to n times until it fails. The acc callback decides how to apply the parsed value to the value of the parent parser.

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

Applies the initial value to the given parser one to n times until it fails. The acc callback decides how to apply the parsed value to the value of the parent parser.

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_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 prepend_to_list(value: List(a), appendage: a) -> List(a)

A mapper to prepend the parsed value of the child parser to the list value of the parent parser.

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(String)) ->
    Result(Parsed(String), ParserFailure(a)),
) -> fn(Parsed(b)) -> Result(Parsed(b), ParserFailure(a))

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

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 until(
  terminator: fn(Parsed(String)) ->
    Result(Parsed(String), ParserFailure(a)),
  mapper: fn(b, String) -> b,
) -> fn(Parsed(b)) -> Result(Parsed(b), ParserFailure(a))

Applies the given parser zero to n times until it succeeds. The mapper decides how to apply the parsed string to the value of the parent parser.

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. The mapper decides how to apply the parsed whitespace to the value of the parent parser.

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. The mapper decides how to apply the parsed whitespace to the value of the parent parser.

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

Search Document