Ergo.Combinators.choice
You're seeing just the function
choice
, go back to Ergo.Combinators module for more information.
Link to this function
choice(parsers, opts \\ [])
The choice/2
parser takes a list of parsers and a list (defaults to []) of
options. It tries to match each in turn, returning :ok if it does. Otherwise,
if none match, it returns :error.
Valid options are:
label: "text"
a label output in debuggingast: fn
a function passed the AST value for processing if the parser succeedsctx: fn
a function passed theContext
for processing if the parser succeedserr: fn
a function passed theContext
for processing if the parser fails
Examples
iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators}
iex> parser = choice([literal("Foo"), literal("Bar"), literal("Hello"), literal("World")], label: "Foo|Bar|Hello|World")
iex> context = Ergo.parse(parser, "Hello World")
iex> assert %Context{status: :ok, ast: "Hello", input: " World", index: 5, col: 6} = context
iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators}
iex> parser = choice([literal("Hello"), literal("World")], ast: &String.upcase/1)
iex> context = Ergo.parse(parser, "Hello World")
iex> assert %Context{status: :ok, ast: "HELLO"} = context
iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators}
iex> fun = fn %Context{ast: ast} = ctx -> %{ctx | ast: String.upcase(ast)} end
iex> parser = choice([literal("Hello"), literal("World")], ctx: fun)
iex> context = Ergo.parse(parser, "Hello World")
iex> assert %Context{status: :ok, ast: "HELLO"} = context
iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators}
iex> parser = choice([literal("Foo"), literal("Bar")], label: "Foo|Bar")
iex> context = Ergo.parse(parser, "Hello World")
iex> %Context{status: {:error, [{:no_valid_choice, {1, 1}, "Foo|Bar did not find a valid choice"}]}, ast: nil, input: "Hello World"} = context