exjpet v0.1.0 Exjpet.Expression
Matching expressions may be uneasy to write. This module provides a set of macros that may be helpful.
Examples
iex> list([:some, 1, 2, :any])
"[*,1,2,_]"
iex> object()
"{}"
iex> object(with_value: capture(:any, as: "foo"))
"{_:(?<foo>_)}"
Link to this section Summary
Functions
Generate a capture matching expression
Generate a list matching expression
Generate a object matching expression
Make any code fragment safe for being used as part of a matching expression. Especially useful when an elixir code fragment evalutes to a string
Generate a set matching expression
Link to this section Functions
Generate a capture matching expression.
Examples
iex> capture(object(), as: :val)
"(?<val>{})"
iex> capture(object(), as: "val")
"(?<val>{})"
Generate a object matching expression.
Examples
iex> object(with_value: 42, with_key: "foo")
"{_:42,\"foo\":_}"
iex> object(with_value: object(with: [key: "neh", value: 42]), with_key: "foo")
"{_:{\"neh\":42},\"foo\":_}"
Make any code fragment safe for being used as part of a matching expression. Especially useful when an elixir code fragment evalutes to a string.
The following code sample yields an invalid expression …
iex> import Exjpet.Expression
Exjpet.Expression
iex> a = "foo"
"foo"
iex> expr = list [a]
"[foo]"
iex> try do
...> Exjpet.compile(expr)
...> rescue
...> e -> :invalid_expression
...> end
:invalid_expression
… whereas using safe/1
produces the expected expression.
iex> import Exjpet.Expression
Exjpet.Expression
iex> a = "foo"
"foo"
iex> expr = list [safe(a)]
"[\"foo\"]"
iex> epm = Exjpet.compile(expr)
iex> Exjpet.run(["foo"], epm)
{true, %{}}