The Driver >

Introduction

This guide is intended at authors or future authors of PPX rewriters. If you don't know what a PPX is, or if you are looking for a guide intended at PPX users, read the OCaml official guide on meta-programming first, although the beginning of this guide may be of interest to everyone.

Preprocessing in OCaml

OCaml doesn't have a macro system, that is, there is no official part of the OCaml language that will be executed at compile time in order to generate or alter the source code. However, OCaml does have an official part of its syntax dedicated to this: extension nodes and attributes; both of them introduced in OCaml 4.02. The preprocessing itself, though, is left to external programs, written by the community and specialised for their own tasks. However, without a unification framework, the following issues arise:

ppxlib

The goal of ppxlib is to solve these problems, by providing a unifying framework for writing preprocessors. It sits in between the OCaml compiler and toolchain, and the PPX authors provide an API for them. One could sum up the ppxlib features as:

This Guide

This guide is separated into several parts.

First, we focus on the driver that performs the AST transformations: how it is generated, executed, and more importantly, what it does exactly, from migrating the AST to the different rewriting phases it goes through.

After that, we explain the different kinds of transformations that ppxlib supports, and how to register them to the driver. This section only describes the transformations and their properties, not how to actually manipulate the AST.

The part where we discuss how to manipulate the AST is split in three pages: generating AST nodes to generate OCaml code, destructing AST nodes to extract information and act differently depending on what is extracted, and traversing the AST to use fold, iter, and map on the AST. This code-manipulation part explains using the modules Ast_builder, Ast_pattern, and the ppxlib's PPX Metaquot.

We finally discuss several good practices, such as how to properly report errors, how to test your PPX, or how to migrate from other PPX libraries, such as OMP and ppx_deriving.

We end by including some examples, which you can also find in the examples folder of ppxlib's repository.

PPXs and ppxlib History

The preprocessing history of OCaml started long before ppxlib. However, this section is not yet written. You can find more information in these resources: 1 2 3 5. You can also contribute to the ppxlib documentation by opening a pull request in the repository.

The Driver >