Oliver

Oliver

Elixir macros and parsers: code generation magic

Lately I have been macro-generating code from ASN1 definitions files.

I basically:

  • Read the file
  • Extract the interesting data with…
    • regular expressions or string processing into a map or
    • other simplified form
  • Use elixir macros to pull in this information and generate functions from it

Now so far that is only a rehash of the “Metaprogramming Elixir” book but bear with me.

Even though the ASN.1 is rather regular I do realize that using regular expressions is rather brittle.

So, what if you were instead using a proper parser (any which is accessible from elixir) to more reliably parse your input files and then post-process the abstract syntax trees? You get compile-time code generation magic!
Or so I hope…

Sometimes the things you can do with 10-20 lines of elixir are purely amazing. Even now my code for generating ASN.1 convenience functions is roughly that size for each function - half of it is the extraction of the information, half of it is the macro to generate the function(s). elixir always provides new ways to make my hacking happy. :smile:

Most Liked

dimitarvp

dimitarvp

Considered open-sourcing it?

dimitarvp

dimitarvp

Erlang docs on ASN.1:

Unless I misunderstand you.

BTW I worked with legacy ASN.1-dependent apps and I am very interested in any Elixir ASN.1 encoders and decoders. If you can open-source yours, I’ll be a fan!

Where Next?

Popular in Discussions Top

ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18634 126
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement