Lately I have been macro-generating code from ASN1 definitions files.
- 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.