Oliver

Oliver

DSL options within elixir modules?

As far as I can see, there are the following options for extending elixir with extensions or change the meaning of syntax:

The regular macros and the optional rules

So I can have my own macros with do-end blocks and rewrite any elixir code that is supplied within, like:

defmodule A do
  defmacro foo(do: block) do IO.inspect(block) end
end

defmodule B do
  import A
  foo do
     [1, 2, 3]
     x + y -> z
  end
end

So I’m allowed all elixir syntax in there and a few constructs like the -> but only within the typical limits how they are are used in elixir syntax itself. I could use the + and -> however to describe for example who syncs to whom or to describe a tree, do my own fancy flow control, etc.

It seems I cannot do:

defmodule B do
  import A
  foo do
    x -> y -> z
    1, 2, 3
    silly: :nilly
  end
end

So I cannot omit list brackets or use the -> more than once without line break or ; or have a keyword list while omitting brackets. (Please correct me if I’m missing something.)

So I could redefine to my heart’s content what elixir’s allowed constructs mean (or replace them arbitrarily) but not use its low-level primitives (comma lists without brackets, keyword lists without brackets, chaining the ->, etc). Do I understand it correctly?

[EDIT: I can see from examples I found since posting that I can get around some of these limitations by using the optional rules and faking a function call, so I could do this at least:

defmodule B do
  import A
  foo do
    something 1, 2, 3
    other silly: :nilly
  end
end

Because then the optional rules at least apply.]

Sigils

Anything in a sigil can pretty much be anything. I get a string and also what follows after the ending delimiter, and I can parse it in any way I see fit and if I do so inside a macro, I can even write code to replace the sigil.

So I could do this:

defmodule A do
  defmacro sigil_p(_, _) do
     quote do
       def silly, do: IO.puts("silly")
     end
  end
end

defmodule B do
   import A
   ~p"doesn't matter"
end

B.silly

So any of the allowed sigils could be used to inject a DSL anywhere in elixir, and since it allows a variety of delimiters, the sigils could emulate elixir constructs like lists in syntax and yet have additional or different semantics.

So I see these two mechanisms - which are undoubtedly powerful and very useful - and wonder if there are other ways to do this without writing such DSLs into separate files and let elixir be simply the compiler that transforms them into usable code.

I mean, I have no problem packing my custom syntax into sigils, but if the same could be done within a do-end block that would even be nicer.

To make it clear, I’m not complaining. I’m looking for all options to allow me to have more of my own syntax for my own DSLs, and this is what I found. If you know more, please share. :slight_smile:

Thank you for any feedback!

Most Liked

zachallaun

zachallaun

I think you have the gist of it! The issues you’re running up against with the do/end block is a limitation of the parser, which has to use its knowledge of Elixir syntax to construct an abstract syntax tree that obeys syntax precedence, associativity, etc.

Last Post!

vfsoraki

vfsoraki

You’re totally right.

Macro is for when you can use Elixir’s syntax, but you want to (re-)define what’s the meaning of some (or all!) of operators/functions/arguments/…. . For example, ExUnit is a kind of DSL.

Sigils are bare strings, you parse it as you will. The most obvious example for me is Heex to embed HTML in Elixir files.

Maybe look at both of them to see how they work.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement