1.2 Preprocessing Objective Caml
1.2.1 The compiler
The standard use of Camlp4 is as a preprocessor for Objective Caml
with the printer ``pr_dump.cmo
''. The Objective Caml compiler
``ocamlc
'' recognizes it is a dump, and does not apply another
parsing. In case of typing errors, the error is normally showed
in the input file.
We remind that the command ``ocaml
'' has an option
``-pp
'' for calling a preprocessor which automatically deals
with intermediate files (see the documentation of Objective Caml). So,
the normal use of the combinaison Camlp4-Objective Caml is done by the
command:
ocamlc -pp "camlp4 camlp4-options" ocaml-options input-files
It is often necessary to add the Camlp4 library directory in
ocaml's path, using option ``-I
''. For information, the Camlp4
command has an option ``-where
'' echoing the full name of this
directory.
1.2.2 Remark
It is possible to use Camlp4 with ``pr_o.cmo
'', instead of
``pr_dump.cmo
'' for Objective
Caml preprocessing, but it is not recommended, for:
-
The parsing would be done twice: one of the user input file by
Camlp4, which pretty prints an intermediate file (which takes some
time), and one of the result of Camlp4 by the Objective Caml parser.
- Objective Caml may find more syntax errors, for Camlp4 with
pa_o.cmo
accepts a syntax larger than Objective Caml's (some
syntax restriction is done in pr_dump.cmo
)
- In case of typing
errors, Objective Caml would show the error location in the resulting
file of Camlp4, what is not convenient.
However, there is a case where it is interesting to use
``pr_o.cmo
'' for objective Caml preprocessing. When the error
is located in a quotation (chapter 2) or in a syntax
extension (chapter 4), it is not always obvious to
understand where the error is exactly located, and there may be no
location for the error. Using temporarily camlp4 with
``pr_o.cmo
'' allows to see the exact location of the error,
which can be understood by the programmer of the quotation expander or
the syntax extension.
1.2.3 The toplevel
The Objective Caml toplevel, ``ocaml
'' can use Camlp4. To do
this, it must be started with the option ``-I
Camlp4
library directory'', or, under Unix, ``-I `camlp4 -where`
''.
Then, one must load:
-
Either the file ``
camlp4o.cma
'' to continue with
Objective Caml syntax.
- Or the file ``
camlp4r.cma
'' to continue with
the Revised syntax.
To change the behavior of Camlp4, just load the needed module with
the directives ``#load
'' and ``#use
'': for example, each
load of ``pa_o.cmo
'' or ``pa_r.cmo
'' would change
the current input syntax to be used in the next sentences.
The files included by the directive ``#use
'' are treated by the
current Camlp4 syntax too. Important remark: a syntax modification
takes place after the complete load of a file, not after each sentence
inside the file.
It is not possible to load a Camlp4 printing syntax file in the
toplevel. The printing results are done by the Objective Caml toplevel
in standard Objective Caml syntax.