How to create a macro like def

Hello, I try to create a custom macro to implement some functions and structure I need in my project and my goal is to make a custom tools which is created by macro.

Please see my macro

delete_list_item(:roles, DeleteErrorComponent, false, do: fn x ->
    IO.inspect(x)
  end, before: fn x -> IO.inspect(x) end)

This Syntax is not good for me and I need a macro like this:

admin_ui delete_list_item(:roles, DeleteErrorComponent, false) do 
  call_function1
  call_function2
  call_function3
  or a block code
  
before
  call_function1
  call_function2
  call_function3
  or a block code
end

My problems

  • I couldn’t run all functions, then I use anonymous function.
  • I couldn’t create a starter like admin_ui like def macro (def somthing)
  • I should write a comma to run before but I need this like top code

I saw this link: elixir/lib/elixir/lib/kernel.ex at v1.12.3 · elixir-lang/elixir · GitHub

But I couldn’t understand all of them, I’m trying to figure out how elixir kernel did this

Thanks

The keywords that introduce block-like constructs (do, after, else, rescue, catch) are special-cased in the tokenizer:

so adding additional ones like before in your example is impossible, short of modifying the language source.

1 Like