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.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
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 record...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement