1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- $Id: index.html,v 6.3 2012-01-09 14:22:20 deraugla Exp $ -->
<!-- Copyright (c) INRIA 2007-2012 -->
<title>Camlp5</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="stylesheet" type="text/css" href="styles/base.css"
title="Normal" />
</head>
<body>
<div id="menu">
</div>
<div id="content">
<h1 class="top">Introduction</h1>
<p>Camlp5 is a preprocessor and pretty-printer for OCaml programs. It
also provides parsing and printing tools.</p>
<p>As a preprocessor, it allows to:</p>
<ul>
<li>extend the syntax of OCaml,</li>
<li>redefine the whole syntax of the language.</li>
</ul>
<p>As a pretty printer, it allows to:</p>
<ul>
<li>display OCaml programs in an elegant way,</li>
<li>convert from one syntax to another,</li>
<li>check the results of syntax extensions.</li>
</ul>
<p>Camlp5 also provides some parsing and pretty printing tools:</p>
<ul>
<li>extensible grammars</li>
<li>extensible printers</li>
<li>stream parsers and lexers</li>
<li>pretty print module</li>
</ul>
<p>It works as a shell command and can also be used in the OCaml
toplevel.</p>
<div id="tableofcontents">
</div>
<h2>Shell usage</h2>
<p>The main shell commands are:</p>
<ul>
<li><tt>camlp5o</tt> : to treat files written in normal OCaml
syntax,</li>
<li><tt>camlp5r</tt> : to treat files written in an original syntax
named the <a href="revsynt.html"><em>revised syntax</em></a>.</li>
</ul>
<p>These commands can be given as parameters of the
option <tt>-pp</tt> of the OCaml compiler. Examples:</p>
<pre>
ocamlc -pp camlp5o foo.ml
ocamlc -pp camlp5r bar.ml
</pre>
<p>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.</p>
<p>In the toplevel, it is possible to preprocess the input phrases by
loading one of the files "<tt>camlp5o.cma</tt>" or
"<tt>camlp5r.cma</tt>". The common usage is:</p>
<pre>
ocaml -I +camlp5 camlp5o.cma
ocaml -I +camlp5 camlp5r.cma
</pre>
<p>It is possible that, in your installation, the Camlp5 library is
not in the OCaml directory. In this case, the commands must be:</p>
<pre>
ocaml -I `camlp5 -where` camlp5o.cma
ocaml -I `camlp5 -where` camlp5r.cma
</pre>
<p>In general, in this documentation, when a command requires:</p>
<pre>
-I +camlp5
</pre>
<p>it can be replaced by:</p>
<pre>
-I `camlp5 -where`
</pre>
<p>or, by:</p>
<pre>
-I <directory>
</pre>
<p>where "directory" is the directory path where the Camlp5 library
files are installed.</p>
<h2>Parsing and Printing kits</h2>
<p>Parsing and printing extensions are OCaml object files, i.e. files
with the extension "<tt>.cmo</tt>" or "<tt>.cma</tt>". They are the
result of the compilation of OCaml source files containing what is
necessary to do the parsing or printing. These object files are
named parsing and printing <em>kits</em>.</p>
<p>These files cannot be linked to produce executables because they
generally call functions and use variables defined only in Camlp5
core, typically belonging to the module "<tt>Pcaml</tt>". The kits
are designed to be loaded by the Camlp5 commands, either through
their command arguments or through directives in the source
files.</p>
<p>It is therefore important to compile the <em>kits</em> with the
option "<tt>-c</tt>" of the OCaml compiler (i.e. just compilation,
not producing an executable) and with the option "<tt>-I
+camlp5</tt>" (or "<tt>-I `camlp5 -where`</tt>") to inform the
compiler to find module interfaces in installed Camlp5 library.</p>
<p>In the OCaml toplevel, it is possible to use a kit by simply
loading it with the directive "<tt>#load</tt>".</p>
<h2>Extending syntax</h2>
<p>A syntax extension is a Camlp5 parsing kit. There are two ways to
use a syntax extension:</p>
<ul>
<li>Either by giving this object file as parameter to the Camlp5
command. For example:
<pre>
ocamlc -pp "camlp5o ./myext.cmo" foo.ml
</pre>
</li>
<li>Or by adding the directive "<tt>#load</tt>" in the source file:
<pre>
#load "./myext.cmo";;
</pre>
and then compile it simply like this:
<pre>
ocamlc -pp camlp5o foo.ml
</pre>
</li>
</ul>
<p>Several syntax extensions can be used for a single file. The way to
create one's own syntax extensions is explained in this document.</p>
<h2>Pretty printing</h2>
<p>As for syntax extensions, the pretty printing is defined or
extended through Camlp5 printing kits. Some pretty printing kits are
provided by Camlp5, the main ones being:</p>
<ul>
<li><tt>pr_o.cmo</tt>: to pretty print in normal syntax,</li>
<li><tt>pr_r.cmo</tt>: to pretty print in revised syntax.</li>
</ul>
<p>Examples: if we have a file, <tt>foo.ml</tt>, written in normal
syntax and and another one, <tt>bar.ml</tt>, written in revised
syntax, here are the commands to pretty print them in their own
syntax:</p>
<pre>
camlp5o pr_o.cmo foo.ml
camlp5r pr_r.cmo bar.ml
</pre>
<p>And how to convert them into the other syntax:</p>
<pre>
camlp5o pr_r.cmo foo.ml
camlp5r pr_o.cmo foo.ml
</pre>
<p>The way to create one's own pretty printing extensions is
explained in this document.</p>
<h2>Note: the revised syntax</h2>
<p>The <em>revised syntax</em> is a specific syntax whose aim is to
resolve some problems and inconsistencies of the normal OCaml
syntax. A chapter will explain the differences between the normal
and the revised syntax.</p>
<p>All examples of this documentation are written in that revised
syntax. Even if you don't know it, it is not difficult to
understand. The same examples can be written in normal syntax. In
case of problems, refer to the chapter describing it.</p>
<div class="trailer">
</div>
</div>
</body>
</html>
|