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

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
fireproofsocks
This is more of a general question, but I’m wondering how other people in the community think about the pattern matching in function sign...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
laiboonh
Hi all, I am trying to convince my team to use liveview over the current react. What are some of the points where one should consider us...
New
jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
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...
3271 126479 1222
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New

We're in Beta

About us Mission Statement