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
|
<!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: pragma.html,v 6.3 2012-01-09 14:22:20 deraugla Exp $ -->
<!-- Copyright (c) INRIA 2007-2012 -->
<title>pragma</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">Pragma directive</h1>
<p>The directive "<code>#pragma</code>" allows to evaluate expressions
at parse time, useful, for example, to test syntax extensions by the
statement EXTEND without having to compile it in a separate file.</p>
<p>To use it, add the syntax extension "<code>pa_pragma.cmo</code>" in
the Camlp5 command line. It adds the ability to use this directive.</p>
<p>As an example, let's add syntax for the statement 'repeat' and
use it immediately:</p>
<pre>
#pragma
EXTEND
GLOBAL: expr;
expr: LEVEL "top"
[ [ "repeat"; e1 = sequence; "until"; e2 = SELF ->
<:expr< do { $e1$; while not $e2$ do { $e1$ } } >> ] ]
;
sequence:
[ [ el = LIST1 expr_semi -> <:expr< do { $list:el$ } >> ] ]
;
expr_semi:
[ [ e = expr; ";" -> e ] ]
;
END;
let i = ref 1 in
repeat print_int i.val; print_endline ""; incr i; until i.val = 10;
</pre>
<p>The compilation of this example (naming it "foo.ml") can be done with
the command:</p>
<pre>
ocamlc -pp "camlp5r q_MLast.cmo pa_extend.cmo pa_pragma.cmo" -I +camlp5 foo.ml
</pre>
<p>Notice that it is still experimental and probably incomplete, for the
moment.</p>
<div class="trailer">
</div>
</div>
</body>
</html>
|