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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
<!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: macros.html,v 6.3 2012-01-09 14:22:20 deraugla Exp $ -->
<!-- Copyright (c) INRIA 2007-2012 -->
<title>macros</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">Macros</h1>
<p>Camlp5 provides a system of macros, added by the parsing kit
"<tt>pa_macro.cmo</tt>". Macros are values evaluated at parsing
time.</p>
<p>When loaded, the parsing kit extends the syntax of the language and
adds command options.</p>
<div id="tableofcontents">
</div>
<h2>Added syntax</h2>
<p>The parsing kit "<tt>pa_macro.cmo</tt>" extends the structure items
(= toplevel phrases), the expressions and the patterns by the
following grammar rules:</p>
<pre>
str-item ::= str-macro-def
sig-item ::= sig-macro-def
expr ::= macro-expr
patt ::= macro-patt
cons-decl ::= macro-cons-decl
rec-lab-decl ::= macro-rec-lab-decl
match-assoc ::= macro-match-assoc
str_macro-def ::= "DEFINE" uident
| "DEFINE" uident "=" expr
| "DEFINE" uident params "=" expr
| "UNDEF" uident
| "IFDEF" dexpr "THEN" st-or-mac else-str "END"
| "IFNDEF" dexpr "THEN" st-or-mac else-str "END"
else-str ::= "ELSIFDEF" dexpr "THEN" st-or-mac else-str
| "ELSIFNDEF" dexpr "THEN" st-or-mac else-str
| "ELSE" st-or-mac else-str
| <nothing>
sig_macro-def ::= "DEFINE" uident
| "DEFINE" uident params "=" type
| "UNDEF" uident
| "IFDEF" dexpr "THEN" sg-or-mac else-sig "END"
| "IFNDEF" dexpr "THEN" sg-or-mac else-sig "END"
else-sig ::= "ELSIFDEF" dexpr "THEN" sg-or-mac else-sig
| "ELSIFNDEF" dexpr "THEN" sg-or-mac else-sig
| "ELSE" st-or-mac else-sig
| <nothing>
macro-expr ::= "IFDEF" dexpr "THEN" expr else-expr "END"
| "IFNDEF" dexpr "THEN" expr else-expr "END"
| "__FILE__"
| "__LOCATION__"
else-expr ::= "ELSIFDEF" dexpr "THEN" expr else-expr
| "ELSIFNDEF" dexpr "THEN" expr else-expr
| "ELSE" expr else-expr
macro-patt ::= "IFDEF" dexpr "THEN" patt "ELSE" patt "END"
| "IFNDEF" dexpr "THEN" patt "ELSE" patt "END"
macro-cons-decl ::= "IFDEF" dexpr "THEN" cons-decl "END"
| "IFDEF" dexpr "THEN" cons-decl
"ELSE" cons-decl "END"
| "IFNDEF" dexpr "THEN" cons-decl "END"
| "IFNDEF" dexpr "THEN" cons-decl
"ELSE" cons-decl "END"
macro-rec-lab-decl ::= "IFDEF" dexpr "THEN" rec-lab-decl "END"
| "IFDEF" dexpr "THEN" rec-lab-decl
"ELSE" rec-lab-decl "END"
| "IFNDEF" dexpr "THEN" rec-lab-decl "END"
| "IFNDEF" dexpr "THEN" rec-lab-decl
"ELSE" rec-lab-decl "END"
macro-match-assoc ::= "IFDEF" dexpr "THEN" match_assoc "END"
| "IFDEF" dexpr "THEN" match-assoc
"ELSE" match-assoc "END"
| "IFNDEF" dexpr "THEN" match-assoc "END"
| "IFNDEF" dexpr "THEN" match-assoc
"ELSE" match-assoc "END"
st-or-mac ::= str_macro-def
| structure
sg-or-mac ::= sig-macro-def
| signature
params ::= ident params
| ident
dexpr ::= dexpr "OR" dexpr
| dexpr "AND" dexpr
| "NOT" dexpr
| uident
| "(" dexpr ")"
uident ::= 'A'-'Z' ident
ident ::= ident-char*
ident-char ::= ('a'-'a' | 'A'-'Z' | '0'-'9' | '_' | ''' |
misc-byte)
misc-byte ::= '\128'-'\255'
</pre>
<p>When a macro has been defined, as name e.g. "NAME", the expressions
and patterns are extended this way:</p>
<pre>
expr ::= "NAME"
| "NAME" "(" expr-params ")"
patt ::= "NAME"
| "NAME" "(" patt-params ")"
expr-params := expr "," expr-params
| expr
patt-params := patt "," patt-params
| patt
</pre>
<p>Notice that the identifiers "<tt>DEFINE</tt>", "<tt>UNDEF</tt>",
"<tt>IFDEF</tt>", "<tt>IFNDEF</tt>", "<tt>ELSE</tt>",
"<tt>END</tt>", "<tt>OR</tt>", "<tt>AND</tt>" and "<tt>NOT</tt>" are
new keywords (they cannot be used as identifiers of constructors or
modules.</p>
<p>However, the identifiers "<tt>__FILE__</tt>" and
"<tt>__LOCATION__</tt>" and the new defined macro names are not new
identifiers.</p>
<h2>Added command options</h2>
<p>The parsing kit "<tt>pa_macro.cmo</tt>" also add two options usable
in all Camlp5 commands:</p>
<dl>
<dt><tt>-D uident</tt></dt>
<dd>Define the uident in question like would have been a DEFINE
(without parameter) in the code.</dd>
<dt><tt>-U uident</tt></dt>
<dd>Undefine the uident in question like would have been a UNDEF in
the code.</dd>
<dt><tt>-defined</tt></dt>
<dd>Print the defined macros and exit.</dd>
</dl>
<h2>Semantics</h2>
<p>The statement "<tt>DEFINE</tt>" defines a new macro with optional
parameters and an optional value. The macro name must start with an
uppercase letter.</p>
<p>The test of a macro can be done either:
<ul>
<li>in structure items</li>
<li>in signature items</li>
<li>in expressions</li>
<li>in patterns</li>
<li>in a constructor declaration</li>
<li>in a match case</li>
</ul>
<p>using the statement "<tt>IFDEF</tt>". Its non-existence can be
tested by "<tt>IFNDEF</tt>". In expressions and patterns, the
"<tt>ELSE</tt>" part is required, not in structure items.</p>
<p>The expression behind the "<tt>IFDEF</tt>" or the "<tt>IFNDEF</tt>"
statement may use the operators "<tt>OR</tt>", "<tt>AND</tt>" and
"<tt>NOT</tt>" and contain parentheses.</p>
<p>Notice that in an "<tt>IFDEF</tt>" where the value is True
(resp. False), the "<tt>ELSE</tt>" (resp "<tt>THEN</tt>") part does
not need to be semantically correct (well typed), since it does not
appear in the resulting syntax tree. Same for "<tt>IFNDEF</tt>" and
for possible macros parameters which are not used in the
associated expression.</p>
<p>If a macro is defined twice, its first version is lost.</p>
<p>The statement "<tt>UNDEF</tt>" removes a macro definition.</p>
<p>When associated with a value, the "<tt>DEFINE</tt>" macro acts like
a variable (or like a function call if it has parameters), except that
the parameters are evaluated at parse time and can also be used also
in pattern positions. Notice that this is a way to define constants
by name in patterns. For example:</p>
<pre>
DEFINE WW1 = 1914;
DEFINE WW2 = 1939;
value war_or_year =
fun
[ WW1 -> "world war I"
| WW2 -> "world war II"
| _ -> "not a war" ]
;
</pre>
<p>In the definition of a macro, if the expression contains an
evaluation, the evaluation is not done by Camlp5 but just
transmitted as code. In this case, it does not work in pattern
position. Example in the toplevel:</p>
<pre>
# DEFINE PLUS(x, y) = x + y;
# PLUS(3, 4);
- : int = 7
# fun [ PLUS(3, 4) -> () ];
Toplevel input:
# fun [ PLUS(3, 4) -> () ];
^^^^^^^^^^
Failure: this is not a constructor, it cannot be applied in a pattern
</pre>
<p>On the other hand, if the expression does not contain evaluation,
this is possible:</p>
<pre>
# DEFINE FOO(x, y) = (x, Some y);
# FOO(True, "bar");
- : (bool * option string) = (True, Some "bar")
# fun [ FOO(_, "hello") -> 0 | _ -> 1 ];
- : ('a * option string) -> int = <fun>
</pre>
<p>The macro "<tt>__FILE__</tt>" is replaced by the current compiled source
file name. In the OCaml toplevel, its value is the empty string.</p>
<p>The macro "<tt>__LOCATION__</tt>" is replaced by the the current location
(two integers in number of characters from the beginning of the file,
starting at zero) of the macro itself.</p>
<p>In signatures, the macro definitions can return types which can be
used in type definitions.</p>
<p>In constructor declarations and in match cases, it is possible to
conditionally define some cases by "<tt>IFDEF</tt>" or
"<tt>IFNDEF</tt>". For example:</p>
<pre>
type t =
[ A of int
| IFNDEF FOO THEN
B of string
END
| C of bool ]
;
match x with
[ A i -> j
| IFNDEF FOO THEN
B s -> toto
END
| C b -> e ];
</pre>
<h2>Predefined macros</h2>
<p>The macro "<tt>CAMLP5</tt>" is always predefined.</p>
<p>The macro "<tt>OCAML_oversion</tt>" is predefined, where
"<tt>oversion</tt>" is the OCaml version the Camlp5 program has been
compiled with, where all characters but numbers are replaced by
underscores. For example, if using OCaml 3.09.3, the macro
"<tt>OCAML_3_09_3</tt>" is defined.</p>
<p>Moreover, for <em>some</em> Camlp5 versions (and all the versions
which follows them), the macro "<tt>CAMLP5_version</tt>" is defined
where "<tt>version</tt>" is the Camlp5 version where all characters
but numbers are replaced by underscores. For example, in version
4.02, the macro "<tt>CAMLP5_4_02</tt>" had been defined and this
macro have appeared in all versions of Camlp5 since 4.02.</p>
<p>To see which macros are predefined, type:</p>
<pre>
camlp5r pa_macro.cmo -defined
</pre>
<div class="trailer">
</div>
</div>
</body>
</html>
|