This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
temple/lib/temple/ast/anonymous_functions.ex

37 lines
829 B
Elixir
Raw Normal View History

defmodule Temple.Ast.AnonymousFunctions do
2021-01-02 18:21:48 +00:00
@moduledoc false
@behaviour Temple.Parser
use TypedStruct
2021-04-09 03:04:26 +00:00
typedstruct do
field :elixir_ast, Macro.t()
field :children, [map()]
end
@impl true
def applicable?({_, _, args}) do
import Temple.Ast.Utils, only: [split_args: 1]
2021-03-25 03:01:13 +00:00
args
|> split_args()
|> elem(1)
|> Enum.any?(fn x -> match?({:fn, _, _}, x) end)
end
def applicable?(_), do: false
@impl true
2021-03-25 03:01:13 +00:00
def run({_name, _, args} = expression) do
{_do_and_else, args} = Temple.Ast.Utils.split_args(args)
2021-03-25 03:01:13 +00:00
{_args, func_arg, _args2} = Temple.Ast.Utils.split_on_fn(args, {[], nil, []})
2021-03-25 03:01:13 +00:00
{_func, _, [{_arrow, _, [[{_arg, _, _}], block]}]} = func_arg
children = Temple.Parser.parse(block)
Temple.Ast.new(__MODULE__, elixir_ast: expression, children: children)
2021-03-25 03:01:13 +00:00
end
end