Bunch v0.3.0 Bunch.Macro View Source

A bunch of helpers for implementing macros.

Link to this section Summary

Functions

Receives an AST and traverses it expanding all the nodes

Imitates import functionality by finding and replacing bare function calls (like foo()) in AST with fully-qualified call (like Some.Module.foo())

Imitates import functionality by finding and replacing bare function calls (like foo()) in AST with fully-qualified call (like Some.Module.foo())

Works like Macro.prewalk/2, but allows to skip particular nodes

Link to this section Functions

Receives an AST and traverses it expanding all the nodes.

This function uses Macro.expand/2 under the hood. Check it out for more information and examples.

Link to this function

inject_call(ast, arg) View Source
inject_call(Macro.t(), {module(), atom()}) :: Macro.t()

Imitates import functionality by finding and replacing bare function calls (like foo()) in AST with fully-qualified call (like Some.Module.foo())

Receives AST fragment as first parameter and a pair {Some.Module, :foo} as second

Link to this function

inject_calls(ast, functions) View Source
inject_calls(Macro.t(), [{module(), atom()}]) :: Macro.t()

Imitates import functionality by finding and replacing bare function calls (like foo()) in AST with fully-qualified call (like Some.Module.foo())

Receives AST fragment as first parameter and list of pairs {Some.Module, :foo} as second

Link to this function

prewalk_while(ast, fun) View Source
prewalk_while(Macro.t(), (Macro.t() -> {:enter | :skip, Macro.t()})) ::
  Macro.t()

Works like Macro.prewalk/2, but allows to skip particular nodes.

Example

iex> code = quote do fun(1, 2, opts: [key: :val]) end
iex> code |> Bunch.Macro.prewalk_while(fn node ->
...>   if Keyword.keyword?(node) do
...>     {:skip, node ++ [default: 1]}
...>   else
...>     {:enter, node}
...>   end
...> end)
quote do fun(1, 2, opts: [key: :val], default: 1) end