ramble_old

Types

pub type FinalParseResult(t, a) {
  FinalParseResult(
    value: Result(a, ParseError(t)),
    errors: List(ParseError(t)),
  )
}

Constructors

  • FinalParseResult(
      value: Result(a, ParseError(t)),
      errors: List(ParseError(t)),
    )
pub type ParseError(t) {
  Unexpected(
    pos: Position,
    token: Token(t),
    expected: List(Token(t)),
  )
}

Constructors

  • Unexpected(
      pos: Position,
      token: Token(t),
      expected: List(Token(t)),
    )
pub type ParseResult(t, a) {
  ParseResult(
    value: Result(a, ParseError(t)),
    rest: List(t),
    new_pos: Position,
    errors: List(ParseError(t)),
  )
}

Constructors

  • ParseResult(
      value: Result(a, ParseError(t)),
      rest: List(t),
      new_pos: Position,
      errors: List(ParseError(t)),
    )
pub type Parser(t, a) {
  Parser(
    parse: fn(
      List(t),
      Position,
      ParserConfig(t),
      Option(Parser(t, t)),
    ) ->
      ParseResult(t, a),
  )
}

Constructors

  • Parser(
      parse: fn(
        List(t),
        Position,
        ParserConfig(t),
        Option(Parser(t, t)),
      ) ->
        ParseResult(t, a),
    )
pub type ParserConfig(t) {
  ParserConfig(
    newline: t,
    eof: t,
    source_name: String,
    token_to_string: fn(t) -> String,
  )
}

Constructors

  • ParserConfig(
      newline: t,
      eof: t,
      source_name: String,
      token_to_string: fn(t) -> String,
    )
pub type Position {
  Position(row: Int, col: Int)
}

Constructors

  • Position(row: Int, col: Int)
pub type Token(t) {
  Token(t)
  LabeledToken(String)
}

Constructors

  • Token(t)
  • LabeledToken(String)

Functions

pub fn all(ps: List(Parser(a, b))) -> Parser(a, b)

Do each parser in the list, returning the result of the last parser.

pub fn alphanum() -> Parser(String, String)

Parse an alphanumeric character.

pub fn any_token() -> Parser(a, a)
pub fn choice(ps: List(Parser(a, b))) -> Parser(a, b)

Parse with the first parser in the list that doesn’t fail.

pub fn desperate_custom_parser(
  parse: fn(
    List(a),
    Position,
    ParserConfig(a),
    Option(Parser(a, a)),
  ) -> ParseResult(a, b),
) -> Parser(a, b)

This grants full control over parsing by allowing a custom parse function. This is like making the Parser type not-opaque. Use this with caution, and be sure to mind the invariants of the library. It should be very rare that you need to use this.

pub fn digit() -> Parser(String, String)

Parse a digit.

pub fn digits() -> Parser(String, String)

Parse a sequence of digits.

pub fn do_and_recover(
  p: Parser(a, b),
  recovery r: Parser(a, a),
) -> Parser(a, b)
pub fn either(p: Parser(a, b), q: Parser(a, b)) -> Parser(a, b)

Parse the first parser, or the second if the first fails.

pub fn end() -> Parser(a, Nil)

Parses successfully only when at the end of the input string.

pub fn extract(
  p: Parser(a, b),
  f: fn(b) -> Parser(a, c),
) -> Parser(a, c)

A monadic bind for pleasant interplay with gleam’s use syntax. example:

fn identifier() -> Parser(String, String) {
    use pos <- extract(pos())
    use first <- extract(lowercase_letter())
    use rest <- extract(many(either(alphanum(), char("_"))))
    return(Ident(pos, first <> string.concat(rest)))
}
pub fn extract_and_recover(
  p: Parser(a, b),
  recovery r: Parser(a, a),
  then f: fn(b) -> Parser(a, c),
) -> Parser(a, c)
pub fn fail() -> Parser(a, b)

Immediately fail regardless of the next input.

pub fn get_config() -> Parser(a, ParserConfig(a))
pub fn get_errors(res: ParseResult(a, b)) -> List(ParseError(a))
pub fn go(
  p: Parser(String, a),
  src: String,
  config: ParserConfig(String),
) -> FinalParseResult(String, a)
pub fn go_on_tokens(
  p: Parser(a, b),
  src: List(a),
  config: ParserConfig(a),
) -> FinalParseResult(a, b)
pub fn label(
  label: String,
  p: fn() -> Parser(a, b),
) -> Parser(a, b)
pub fn lazy(p: fn() -> Parser(a, b)) -> Parser(a, b)

Run a parser as normal, but the parser itself isn’t evaluated until it is used. This is needed for recursive grammars, such as E := n | E + E where n is a number. Example: lazy(digit) instead of digit().

pub fn letter() -> Parser(String, String)

Parse a lowercase or uppercase letter.

pub fn lowercase_letter() -> Parser(String, String)

Parse a lowercase letter.

pub fn main() -> Nil
pub fn many(p: Parser(a, b)) -> Parser(a, List(b))

Keep trying the parser until it fails, and return the array of parsed results. This cannot fail because it parses zero or more times!

pub fn many1(p: Parser(a, b)) -> Parser(a, List(b))

Keep trying the parser until it fails, and return the array of parsed results. This can fail, because it must parse successfully at least once!

pub fn many1_as_string(p: Parser(a, String)) -> Parser(a, String)

Parse a certain string as many times as possible, returning everything that was parsed. This can fail, because it must parse successfully at least once!

pub fn many_as_string(p: Parser(a, String)) -> Parser(a, String)

Parse a certain string as many times as possible, returning everything that was parsed. This cannot fail because it parses zero or more times!

pub fn map(p: Parser(a, b), f: fn(b) -> c) -> Parser(a, c)

Do p, then apply f to the result if it succeeded.

pub fn not(p: Parser(a, b)) -> Parser(a, Nil)

Negate a parser: if it succeeds, this fails, and vice versa. This is useful for keyword parsing. Example: seq(string("if"), not(either(alphanum(), char("_"))))

pub fn optional(p: Parser(a, b)) -> Parser(a, Result(b, Nil))

Try running a parser, but still succeed (with Error(Nil)) if it failed.

pub fn peek(p: Parser(a, b)) -> Parser(a, Bool)
pub fn position() -> Parser(a, Position)

Get the current parser position.

pub fn recover() -> Parser(a, Nil)
pub fn return(x: a) -> Parser(b, a)

A monadic return for pleasant interplay with gleam’s use syntax. see extract and extract_and_recover for more details and examples.

pub fn satisfy(when pred: fn(a) -> Bool) -> Parser(a, a)

Parse a character if it matches the predicate.

pub fn sep(
  parser: Parser(a, b),
  by s: Parser(a, c),
) -> Parser(a, List(b))

Parse a sequence separated by the given separator parser.

pub fn sep1(
  parser: Parser(a, b),
  by s: Parser(a, c),
) -> Parser(a, List(b))

Parse a sequence separated by the given separator parser. This only succeeds if at least one element of the sequence was parsed.

pub fn seq(p: Parser(a, b), q: Parser(a, c)) -> Parser(a, c)

Do the first parser, ignore its result, then do the second parser.

pub fn string(s: String) -> Parser(String, String)

Parse an exact string of characters.

pub fn token(c: a) -> Parser(a, a)

Parse a specific token.

pub fn try(
  p: Parser(a, b),
  f: fn(b) -> Result(b, ParseError(a)),
) -> Parser(a, b)

Do p, the apply f to the result if it succeeded. f itself can fail with the user-defined error type, and if it does the result is a UserError with the error.

pub fn until(
  do p: Parser(a, b),
  until terminator: Parser(a, a),
) -> Parser(a, List(b))

Parse zero or more repetitions of a parser, collecting the results into a list. Stop when the terminator parser succeeds, even if the looping parser would also succeed. The terminator parser’s results are not consumed. The main motivator for until is multiline comments ending in */, -->, -}, *), etc.

pub fn uppercase_letter() -> Parser(String, String)

Parse an uppercase letter.

pub fn whitespace() -> Parser(String, String)

Parse zero or more whitespace characters.

pub fn whitespace1() -> Parser(String, String)

Parse one or more whitespace characters.

Search Document