After knowing what is an AST, how to build an AST and destructure it, we can now write our own PPX in OCaml.
The soul of a PPX is the transformation. We want to get our AST and transform it into something else, like a new AST or lint errors.
Those transformations can be divided into two categories that we will cover on nested folders:
And they can work in different phases:
- Lint (Global)
- Preprocess (Global)
- Instrumentation - Before (Global)
- Context-free
- Global Trasformation (Global)
- Instrumentation - After (Global)
The following diagram shows the order of the phases and Driver's methods:
Drive's methods phases diagram. (reference)PPXs commonly follow these steps:
- Match the AST we want.
- Work with the AST. For example:
- Returning a new AST. Add new functions, change the name of a variable, etc.
- Linting the code.
- or doing anything else. Really, you're programming, everything is possible!