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
|
<!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: directives.html,v 6.3 2012-01-09 14:22:20 deraugla Exp $ -->
<!-- Copyright (c) INRIA 2007-2012 -->
<title>Directives</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">Directives</h1>
<p>Directives in normal or revised syntax are statements at top level,
or, more generally, as signature items or structure items, which
stops the preprocessor for evaluate things - which can change the
behaviour of the preprocessor, for example to add syntax, load
syntax extensions and so on. After the directive is evaluated,
the parsing resumes.</p>
<p>Directives begin with '#', followed by an identifier, and,
optionnally by an expression. They are usable in source files the
and generally in the ocaml toplevel too.</p>
<p>Four predefined directives exist: #load, #directory, #option and
#use. It is also possible to add other directives. An example of
that is the parsing kit <a href="pragma.html">pa_pragma.cmo</a>
which adds a new directive #pragma.</p>
<div id="tableofcontents">
</div>
<h2>Predefined directives</h2>
<p>The predefined directives are:</p>
<h3>#load "name"</h3>
<p>Loads an object file (ocaml cmo or cma file) in the core,
evaluating it. This is typically to be used in the ocaml toplevel
to add an syntax extension kit.</p>
<p>For example, in the toplevel, loading
the <a href="quot.html">quotation</a> expander
of <a href="ast_strict.html">ocaml syntax trees</a>:</p>
<pre>
# #load "q_MLast.cmo";
# value loc = Ploc.dummy;
value loc : Ploc.t = <abstr>
# <:expr< fun x -> x >>;
- : MLast.expr =
MLast.ExFun <abstr>
(Ploc.VaVal
[(MLast.PaLid <abstr> (Ploc.VaVal "x"), Ploc.VaVal None,
MLast.ExLid <abstr> (Ploc.VaVal "x"))])
</pre>
<p>In a source file, the '#load' directive is equivalent to put the
object file as camlp5 parameter among the 'load options':</p>
<pre>
$ cat myfile.ml
#load "pa_extend.cmo";
value g = Grammar.gcreate (Plexer.gmake ());
value e = Grammar.Entry.create g "e";
EXTEND e: [[ i = INT -> i ]]; END;
$ ocamlc -pp camlp5r -I +camlp5 -c myfile.ml
</pre>
<p>which is equivalent to, without using '#load':</p>
<pre>
$ cat myfile2.ml
value g = Grammar.gcreate (Plexer.gmake ());
value e = Grammar.Entry.create g "e";
EXTEND e: [[ i = INT -> i ]]; END;
</pre>
<p>and compiling it like this:</p>
<pre>
$ ocamlc -pp 'camlp5r pa_extend.cmo' -I +camlp5 -c myfile2.ml
</pre>
<h3>#directory "name"</h3>
<p>Adds a new directory in the camlp5 path searching for loaded files
(using the directive #load above). This is equivalent to the option
'-I' of the camlp5 command. See the camlp5 man page.</p>
<h3>#use "name"</h3>
<p>Loads a source file name. Useful in the ocaml toplevel to test a
source file.</p>
<h3>#option "option"</h3>
<p>Adds an option as if it were added in camlp5 command line (to be
used in a source file, not in the ocaml toplevel). Implemented
only on options without an extra parameter.</p>
<p>For example, the syntax
kit <a href="grammars.html">pa_extend.cmo</a> adds an option named
'-split_ext'. This can be viewed through the command:</p>
<pre>
camlp5r pa_extend.cmo -help
</pre>
<p>Thanks to the directive '#option', the following command in the
shell:</p>
<pre>
$ camlp5r pa_extend.cmo -split_ext file.ml
</pre>
<p>can be used only as:</p>
<pre>
$ camlp5r file.ml
</pre>
<p>providing the file starts with:</p>
<pre>
#load "pa_extend.cmo";
#option "-split_ext";
</pre>
<h2>User directives</h2>
<p>It is possible to add any extra directive. The syntax
kit <a href="pragma.html">pragma.cmo</a>, for example, adds a
directive named '#pragma'.</p>
<p>A user syntax kit can add its directives using the function
"add_directive" of the module <a href="pcaml.html">Pcaml</a>.</p>
<div class="trailer">
</div>
</div>
</body>
</html>
|