atto/ops

Parser combinators for constructing layout, such as many and choice.

Values

pub fn between(
  open: atto.Parser(b, t, s, c, e),
  p: atto.Parser(a, t, s, c, e),
  close: atto.Parser(d, t, s, c, e),
) -> atto.Parser(a, t, s, c, e)

Parse p between open and close, returning the result of p.

Examples

ops.between(token("("), token("a"), token(")"))
|> run(text.new("(a)"), Nil)
// -> Ok("a")
pub fn choice(
  ps: List(atto.Parser(a, t, s, c, e)),
) -> atto.Parser(a, t, s, c, e)

Try each parser in order, returning the first successful result. If a parser fails but consumes input, that error is returned.

Examples

ops.choice([token("a"), token("b")])
|> run(text.new("a"), Nil)
// -> Ok("a")
ops.choice([text.match("foo"), text.match("bar")])
|> run(text.new("f123"), Nil)
// -> Error(ParseError(Pos(1, 1), Token("f"), Set([Msg("Regex failed")]))
pub fn many(
  p: atto.Parser(a, t, s, c, e),
) -> atto.Parser(List(a), t, s, c, e)

Parse zero or more p.

Examples

ops.many(token("a"))
|> run(text.new("aaa"), Nil)
// -> Ok(["a", "a", "a"])
ops.many(token("a"))
|> run(text.new("bbb"), Nil)
// -> Ok([])
pub fn maybe(
  p: atto.Parser(a, t, s, c, e),
) -> atto.Parser(Result(a, Nil), t, s, c, e)

Try to apply a parser, returning Nil if it fails without consuming input.

Examples

ops.maybe(token("a"))
|> run(text.new("a"), Nil)
// -> Ok(Ok("a"))
ops.maybe(token("a"))
|> run(text.new("b"), Nil)
// -> Ok(Error(Nil))
pub fn sep(
  p: atto.Parser(a, t, s, c, e),
  by by: atto.Parser(b, t, s, c, e),
) -> atto.Parser(List(a), t, s, c, e)

Zero or more parsers separated by a delimiter.

pub fn sep1(
  p: atto.Parser(a, t, s, c, e),
  by by: atto.Parser(b, t, s, c, e),
) -> atto.Parser(List(a), t, s, c, e)

Parse one or more p separated by a delimiter.

Examples

ops.sep_by_1(token("a"), token(","))
|> run(text.new("a,a,a"), Nil)
// -> Ok(["a", "a", "a"])
pub fn some(
  p: atto.Parser(a, t, s, c, e),
) -> atto.Parser(List(a), t, s, c, e)

Parse one or more p.

Examples

ops.some(token("a"))
|> run(text.new("aaa"), Nil)
// -> Ok(["a", "a", "a"])
ops.some(token("a"))
|> run(text.new("bbb"), Nil)
// -> Error(ParseError(Pos(1, 1), Token("b"), set.insert(set.new(), Token("a"))))
Search Document