View Source Elnom.Branch (elnom v0.1.0)

Choice combinators.

Summary

Functions

Tests a list of parsers one by one until one succeeds.

Applies a list of parsers in any order.

Functions

Tests a list of parsers one by one until one succeeds.

It takes as argument a tuple or list of parsers.

iex> parser = alt({alpha1(), digit1()})
iex> parser.("abc")
{:ok, "", "abc"}
iex> parser.("123456")
{:ok, "", "123456"}
iex> parser.("abc123")
{:ok, "123", "abc"}
iex> parser.(" ")
{:error, %Error{kind: :digit, buffer: " "}}

Applies a list of parsers in any order.

Permutation will succeed if all of the child parsers succeeded. It takes as argument a tuple of parsers, and returns a tuple of the parser results.

iex> parser = permutation({alpha1(), digit1()})
iex> parser.("abc123")
{:ok, "", {"abc", "123"}}
iex> parser.("123abc")
{:ok, "", {"abc", "123"}}
iex> parser.("abc;")
{:error, %Error{kind: :digit, buffer: ";"}}

The parsers are applied greedily: if there are multiple unapplied parsers that could parse the next slice of input, the first one is used.

iex> parser = permutation({anychar(), char("a")})
iex> parser.("ba")
{:ok, "", {"b", "a"}}
iex> parser.("ab")
{:error, %Error{kind: :char, buffer: "b"}}