Overview

Camlp5 is a preprocessor and pretty-printer for ocaml programs.

As a preprocessor, it allows to:

As a pretty printer, it allows to:

It works as a shell command and can also be used in the ocaml toplevel.

Camlp5 also provides some parsing and pretty printing tools.

Usage

The main shell commands are:

These commands can be given as parameters of the option -pp of the ocaml compiler. Examples:

ocamlc -pp camlp5o foo.ml
ocamlc -pp camlp5r bar.ml

This way, the parsing is done by camlp5. In case of syntax errors, the parsing fails with an error message and the compilation is aborted. Otherwise, the ocaml compiler continues with the syntax tree provided by camlp5.

In the toplevel, it is possible to preprocess the input phrases by loading one of the files "camlp5o.cma" or "camlp5r.cma". The common usage is:

ocaml -I +camlp5 camlp5o.cma
ocaml -I +camlp5 camlp5r.cma

Extending syntax

A syntax extension is an ocaml object file, i.e. a file with the extension ".cmo". It is a result of the compilation of an ocaml source file containing what is necessary to do the syntax extensions. These object files are named parsing kits.

There are two ways to use a syntax extension:

Several syntax extensions can be used for a single file. The way to create one's own syntax extensions is explained further.

Pretty printing

Like for syntax extensions, pretty printing kits are ocaml object files. The main pretty printing kits provided by camlp5 are:

Examples: if we have a file, foo.ml, written in normal syntax and and another one, bar.ml, written in revised syntax, here are the commands to pretty print them in their own syntax:

camlp5o pr_o.cmo foo.ml
camlp5r pr_r.cmo bar.ml

And how to convert them into the other syntax:

camlp5o pr_r.cmo foo.ml
camlp5r pr_o.cmo foo.ml

The way to create one's own pretty printing extensions is explained further.


Copyright 2007 Daniel de Rauglaudre (INRIA)

Valid XHTML 1.1