Getting the AST (Abstract Syntax Tree) representation of an Elixir source is pretty simple.
Let’s say we want to get the AST of this file:
1 2 3 4 5 6 7 |
|
We can do it right away from iex
:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
In our case, the ast
variable will contain the full AST of the source code.
In case you want to get the AST of a single line, it’s even simpler:
1 2 3 4 5 6 7 8 9 10 |
|
For more context, I recommend reading the introduction to meta-programming in Elixir on Elixir’s official site.
In case you’re interested in parsing Elixir, Tokenizing and parsing in Elixir with yecc and leex by Andrea Leopardi is a very recommended reading.
Have fun with Elixir!