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

Link to this macro capture(frag, opts) (macro)

Generate a capture matching expression.

Examples

iex> capture(object(), as: :val)
"(?<val>{})"
iex> capture(object(), as: "val")
"(?<val>{})"
Link to this macro list(entries \\ []) (macro)

Generate a list matching expression.

Examples

iex> list([1, 2])
"[1,2]"
Link to this macro object(constraints \\ []) (macro)

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\":_}"
Link to this macro safe(frag) (macro)

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, %{}}
Link to this macro set(constraints \\ [], modifiers \\ []) (macro)

Generate a set matching expression.

Examples

iex> set([42])
"<42>"
iex> set([42], [global: true])
"<42>/g"
iex> set([object()], [deep: true])
"<!{}!>"
iex> set([object()], [deep: true, global: true])
"<!{}!>/g"