Pelecanus
Pelecanus is simple parser combinator for elixir.
Usage
In Pelecanus, parsers are just (anonymous) pure function
(state -> {:ok, state, value} | {:ok, state} | {:error, reason})
.
Useful functions that return parser are provided.
use Pelecanus, sigil_p: true
# create parser state
init = State.init("pelican come!")
# Terminal Symbols can be written almost regex
parser = ~p(\w+)
{:ok, _state, matched} = parser.(init)
# PEG like operators can be used
parser = sequence [~p(\w+), ~p( ), ~p(\w+), option ~p(!)]
{:ok, _state, list} = parser.(init)
You can simply define custom parser;
int =
fn state ->
with {:ok, s, v} <- ~p(\d+).(state),
do: {:ok, s, String.to_integer v}
end
{:ok, _state, 1758} = int.(State.init("1758"))
passing state contain context field. You can use this as you like.
Installation
If available in Hex, the package can be installed
by adding pelecanus
to your list of dependencies in mix.exs
:
def deps do
[
{:pelecanus, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/pelecanus.