plymio_ast_vorm v0.1.0 Plymio.Ast.Vorm.Field View Source

Convenience wrappers for using a vorm held in a struct.

Most of these wrappers assumes a vorm is, or will be, held in the :vorm field of a struct and allow function such as add to be called where the first argument is the struct. For example struct_vorm_add looks like:

def struct_vorm_add(struct, new_forms) do
  with {:ok, %{vorm: %Plymio.Ast.Vorm{} = vorm} = struct <- struct |> struct_vorm_ensure,
       {:ok, %Plymio.Ast.Vorm{} = vorm} <- vorm |> Plymio.Ast.Vorm.add(new_forms)  do
    {:ok, struct |> struct!(vorm: vorm)
  else
    {:error, _} = result -> result
  end
end

Since most of the wrappers work in the same way, only struct_vorm_add will be documented below with illustrative examples.

Documentation Terms

In the documentation below these terms, usually in italics, are used to mean the same thing (e.g. state).

vorm

An instance of Plymio.Ast.Vorm (%Plymio.Ast.Vorm{}).

vorm field

The field called :vorm in a struct intended to hold a vorm.

state

A instance of a struct with a vorm field.

Link to this section Summary

Link to this section Types

Link to this type error() View Source
error() :: struct()
Link to this type vorm() View Source
vorm() :: %Plymio.Ast.Vorm{forms: term()}

Link to this section Functions

Link to this function struct_forms_update(t, new_forms) View Source
struct_forms_update(t(), any()) :: {:ok, t()} | {:error, error()}

struct_forms_update/2 take a state and a second argument.

If the second argument is a vorm, the state is updated with the vorm’s forms and {:ok, state} returned.

Otherwise the second argument is assumed to be one or more forms and the state is updated with them, returning {:ok, state}.

Examples

iex> {:ok, vorm} = Plymio.Ast.Vorm.new(form: quote(do: x = x + 1))
...> t = %PlymioAstVormFieldTest{}
...> {:ok, t} = t |> struct_forms_update(vorm)
...> t.vorm |> helper_vorm_test_forms!(binding: [x: 3])
{4, ["x = x + 1"]}

iex> t = %PlymioAstVormFieldTest{}
...> {:ok, t} = t |> struct_forms_update(forms: quote(do: x = x + 1))
...> t.vorm |> helper_vorm_test_forms!(binding: [x: 3])
{4, ["x = x + 1"]}

iex> t = %PlymioAstVormFieldTest{}
...> {:ok, t} = t |> struct_forms_update(:a_valid_ast)
...> t.vorm |> helper_vorm_test_forms!(binding: [x: 3])
{:a_valid_ast, [":a_valid_ast"]}
Link to this function struct_vorm_add(state, var1) View Source

struct_vorm_add/2 appends new_forms to the vorm forms of the vorm in the vorm field.

struct_vorm_ensure/1 is called first to ensure the vorm field is a vorm.

If the add suceeds, {:ok, state} is returned.

Examples

iex> t = %PlymioAstVormFieldTest{}
...> {:ok, t} = t |> struct_vorm_add(quote(do: x = x + 1))
...> t.vorm |> helper_vorm_test_forms!(binding: [x: 7])
{8, ["x = x + 1"]}

iex> t = %PlymioAstVormFieldTest{}
...> {:ok, t} = t |> struct_vorm_add(quote(do: x = x + 1))
...> {:ok, t} = t |> struct_vorm_add([quote(do: x = x * x), quote(do: x = x - 1)])
...> t.vorm |> helper_vorm_test_forms!(binding: [x: 7])
{63, ["x = x + 1", "x = x * x", "x = x - 1"]}
Link to this function struct_vorm_ensure(t) View Source
struct_vorm_ensure(t()) :: {:ok, t()} | {:error, error()}

struct_vorm_ensure/1 take a state and checks if the vorm field already holds a vorm.

If it has {:ok, state} is returned.

If the vorm field is nil, a new, empty vorm is created, stored in the vorm field, and {:ok, state} returned.

Any other value in the vorm field will cause an error.

Examples

iex> t = %PlymioAstVormFieldTest{}
...> match?(nil, t.vorm)
true

iex> {:ok, t} = %PlymioAstVormFieldTest{}
...> |> struct_vorm_ensure
...> match?(%Plymio.Ast.Vorm{}, t.vorm)
true

iex> {:ok, vorm} = Plymio.Ast.Vorm.new(form: quote(do: x = x + 1))
...> {:ok, t} = %PlymioAstVormFieldTest{vorm: vorm}
...> |> struct_vorm_ensure
...> t.vorm |> helper_vorm_test_forms!(binding: [x: 3])
{4, ["x = x + 1"]}

iex> %PlymioAstVormFieldTest{vorm: :not_a_vorm}
...> |> struct_vorm_ensure
{:error, %Plymio.Ast.Vorm.Error{error: nil, message: "vorm field invalid",
   value: %PlymioAstVormFieldTest{vorm: :not_a_vorm}}}
Link to this function struct_vorm_express(state) View Source
Link to this function struct_vorm_fetch(state, var1) View Source
Link to this function struct_vorm_filter(state, var1) View Source
Link to this function struct_vorm_insert(state, var1, var2) View Source
Link to this function struct_vorm_pipe_after(state, var1, var2) View Source
Link to this function struct_vorm_pipe_before(state, var1, var2) View Source
Link to this function struct_vorm_postwalk(state, var1, var2) View Source
Link to this function struct_vorm_prewalk(state, var1, var2) View Source
Link to this function struct_vorm_produce(state) View Source
Link to this function struct_vorm_put(state, var1) View Source
Link to this function struct_vorm_reduce(state, var1) View Source
Link to this function struct_vorm_reject(state, var1) View Source
Link to this function struct_vorm_replace(state, var1, var2) View Source
Link to this function struct_vorm_reset(state) View Source
struct_vorm_reset(t()) :: {:ok, t()} | {:error, error()}

struct_vorm_reset/1 take a state and resets its vorm field to nil returning {:ok, state}.

Examples

iex> t = %PlymioAstVormFieldTest{}
...> {:ok, t} = t |> struct_vorm_reset
...> match?(nil, t.vorm)
true

iex> {:ok, vorm} = Plymio.Ast.Vorm.new(form: quote(do: x = x + 1))
...> {:ok, t} = %PlymioAstVormFieldTest{vorm: vorm}
...> |> struct_vorm_reset
...> match?(nil, t.vorm)
true
Link to this function struct_vorm_transform(state, var1) View Source
Link to this function struct_vorm_transform(state, var1, var2) View Source
Link to this function struct_vorm_traverse(state, var1, var2, var3, var4) View Source
Link to this function struct_vorm_update(t, new_forms) View Source
struct_vorm_update(t(), any()) :: {:ok, t()} | {:error, error()}

struct_vorm_update/2 take a state and a second argument.

If the second argument is a vorm, the state is updated with it and {:ok, state} returned.

Otherwise the second argument is passed to new/1 and the new vorm stored in the state.

Examples

iex> {:ok, vorm} = Plymio.Ast.Vorm.new(form: quote(do: x = x + 1))
...> t = %PlymioAstVormFieldTest{}
...> {:ok, t} = t |> struct_vorm_update(vorm)
...> t.vorm |> helper_vorm_test_forms!(binding: [x: 3])
{4, ["x = x + 1"]}

iex> t = %PlymioAstVormFieldTest{}
...> {:ok, t} = t |> struct_vorm_update(forms: quote(do: x = x + 1))
...> t.vorm |> helper_vorm_test_forms!(binding: [x: 3])
{4, ["x = x + 1"]}

iex> t = %PlymioAstVormFieldTest{}
...> t |> struct_vorm_update(:not_new_opts)
{:error, %Plymio.Ast.Vorm.Error{error: nil, message: "new vorm opts invalid", value: :not_new_opts}}