~ubuntu-branches/ubuntu/hardy/ocaml-doc/hardy

« back to all changes in this revision

Viewing changes to camlp4.html/manual008.html

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2007-09-08 01:49:22 UTC
  • mfrom: (0.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070908014922-lvihyehz0ndq7suu
Tags: 3.10-1
* New upstream release.
* Removed camlp4 documentation since it is not up-to-date.
* Updated to standards version 3.7.2, no changes needed.
* Updated my email address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
2
 
            "http://www.w3.org/TR/REC-html40/loose.dtd">
3
 
<HTML>
4
 
<HEAD>
5
 
 
6
 
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7
 
<META name="GENERATOR" content="hevea 1.06">
8
 
<TITLE>
9
 
 Other extensions
10
 
</TITLE>
11
 
</HEAD>
12
 
<BODY TEXT=black BGCOLOR=white>
13
 
<A HREF="manual007.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
14
 
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
15
 
<A HREF="manual009.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
16
 
<HR>
17
 
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
18
 
<TR><TD BGCOLOR="#2de52d"><DIV ALIGN=center><TABLE>
19
 
<TR><TD><A NAME="htoc43"><B><FONT SIZE=6>Chapter&nbsp;7</FONT></B></A></TD>
20
 
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=6>Other extensions</FONT></B></TD>
21
 
</TR></TABLE></DIV></TD>
22
 
</TR></TABLE>
23
 
<A NAME="c:manother"></A>
24
 
<A NAME="toc29"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
25
 
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
26
 
<TR><TD><A NAME="htoc44"><B><FONT SIZE=5>7.1</FONT></B></A></TD>
27
 
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Extensible functions</FONT></B></TD>
28
 
</TR></TABLE></DIV></TD>
29
 
</TR></TABLE><BR>
30
 
Camlp4 provides a system of functions defined by pattern matching,
31
 
which are extensible.<BR>
32
 
<BR>
33
 
 
34
 
 
35
 
The library module is <CODE>Extfun</CODE> and the syntax to be loaded is
36
 
<CODE>"pa_extfun.cmo"</CODE>. The empty function is <CODE>Extfun.empty</CODE>.
37
 
You can extend a function using the statement <CODE>extfun</CODE> whose
38
 
syntax is:<BR>
39
 
<BR>
40
 
 
41
 
 
42
 
<DIV ALIGN=center>
43
 
<TABLE CELLSPACING=2 CELLPADDING=0>
44
 
<TR><TD ALIGN=left NOWRAP><CODE>extfun</CODE> <EM>expression</EM> <CODE>with</CODE></TD>
45
 
</TR>
46
 
<TR><TD ALIGN=left NOWRAP><CODE>[</CODE> <EM>pattern-1</EM> <CODE>-&gt;</CODE> <EM>expression-1</EM></TD>
47
 
</TR>
48
 
<TR><TD ALIGN=left NOWRAP><CODE>|</CODE> <EM>pattern-2</EM> <CODE>-&gt;</CODE> <EM>expression-2</EM></TD>
49
 
</TR>
50
 
<TR><TD ALIGN=left NOWRAP>...</TD>
51
 
</TR>
52
 
<TR><TD ALIGN=left NOWRAP><CODE>|</CODE> <EM>pattern-n</EM> <CODE>-&gt;</CODE> <EM>expression-n</EM> <CODE>]</CODE></TD>
53
 
</TR></TABLE>
54
 
</DIV><BR>
55
 
 
56
 
 
57
 
The patterns are ordered in lexicographic order (for example, in a
58
 
tuple, first comparing the first elements of the tuple). Variables are
59
 
inserted after constructors. In an extension, the patterns do not need
60
 
to be in a ``good'' order, since they are sorted. Non exhaustive
61
 
pattern matching do not generate a warning.<BR>
62
 
<BR>
63
 
 
64
 
 
65
 
``Or'' patterns can be used only at the first level. In this case:
66
 
<PRE>
67
 
          pat1 | pat2 -&gt; expr
68
 
</PRE>The binding is split into two cases (the expr is duplicated):
69
 
<PRE>
70
 
            pat1 -&gt; expr
71
 
          | pat2 -&gt; expr
72
 
</PRE>
73
 
Internal ``or'' patterns inside patterns are not accepted.<BR>
74
 
<BR>
75
 
 
76
 
 
77
 
The statement <CODE>extfun</CODE> returns another extensible function. The
78
 
type of extensible functions is <CODE>('a, 'b) Extfun.t</CODE>. To use an
79
 
extensible function, one must use the function <CODE>Extfun.apply</CODE>
80
 
which transforms it in a function of type <CODE>'a -&gt; 'b</CODE>. If matching
81
 
failed, such a function raises the exception <CODE>Extfun.Failure</CODE>.<BR>
82
 
<BR>
83
 
 
84
 
 
85
 
The contents (patterns) of an extensible function can be displayed
86
 
using <CODE>Extfun.print</CODE>.<BR>
87
 
<BR>
88
 
 
89
 
 
90
 
Remark: extensible functions are not efficient: when applied, all
91
 
patterns are tested, one by one, until one of them matches.<BR>
92
 
<BR>
93
 
 
94
 
 
95
 
Extensible functions are used in Camlp4 extensible pretty printing.<BR>
96
 
<BR>
97
 
<A NAME="toc30"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
98
 
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
99
 
<TR><TD><A NAME="htoc45"><B><FONT SIZE=5>7.2</FONT></B></A></TD>
100
 
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Functional streams</FONT></B></TD>
101
 
</TR></TABLE></DIV></TD>
102
 
</TR></TABLE><BR>
103
 
The functional streams are another implementation of streams. Like
104
 
normal streams, their contents can be accessible only one element at a
105
 
time. But the elements are not removed. A functional stream parser
106
 
returns the couple of a result and the remaining stream.<BR>
107
 
<BR>
108
 
 
109
 
 
110
 
The library module is <CODE>Fstream</CODE> and the syntax to be loaded is
111
 
<CODE>"pa_fstream.cmo"</CODE>. The syntax of a functional stream is:<BR>
112
 
<BR>
113
 
 
114
 
 
115
 
<TABLE CELLSPACING=2 CELLPADDING=0>
116
 
<TR><TD ALIGN=left NOWRAP>        </TD>
117
 
<TD ALIGN=left NOWRAP><EM>functional-stream</EM> ::=</TD>
118
 
</TR>
119
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
120
 
<TD ALIGN=left NOWRAP>    <CODE>fstream</CODE>
121
 
<CODE>[:</CODE> <EM>list-of-components-separated-by-semicolon</EM> <CODE>:]</CODE></TD>
122
 
</TR>
123
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
124
 
<TD ALIGN=left NOWRAP><EM>component</EM> ::=</TD>
125
 
</TR>
126
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
127
 
<TD ALIGN=left NOWRAP>        <CODE>`</CODE> <EM>stream-element</EM></TD>
128
 
</TR>
129
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
130
 
<TD ALIGN=left NOWRAP>      <CODE>|</CODE> <EM>stream</EM></TD>
131
 
</TR></TABLE><BR>
132
 
 
133
 
 
134
 
and a functional parser, applying to a functional stream is:<BR>
135
 
<BR>
136
 
 
137
 
 
138
 
<TABLE CELLSPACING=2 CELLPADDING=0>
139
 
<TR><TD ALIGN=left NOWRAP>        </TD>
140
 
<TD ALIGN=left NOWRAP><EM>functional-parser</EM> ::=</TD>
141
 
</TR>
142
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
143
 
<TD ALIGN=left NOWRAP>     <CODE>fparser</CODE></TD>
144
 
</TR>
145
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
146
 
<TD ALIGN=left NOWRAP>     <CODE>[</CODE> <EM>stream-pattern-1</EM> <CODE>-&gt;</CODE> <EM>expression-1</EM></TD>
147
 
</TR>
148
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
149
 
<TD ALIGN=left NOWRAP>     <CODE>|</CODE> <EM>stream-pattern-2</EM> <CODE>-&gt;</CODE> <EM>expression-2</EM></TD>
150
 
</TR>
151
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
152
 
<TD ALIGN=left NOWRAP>     ..</TD>
153
 
</TR>
154
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
155
 
<TD ALIGN=left NOWRAP>     <CODE>|</CODE> <EM>stream-pattern-n</EM> <CODE>-&gt;</CODE>
156
 
<EM>expression-n</EM> <CODE>]</CODE></TD>
157
 
</TR>
158
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
159
 
<TD ALIGN=left NOWRAP><EM>stream-pattern</EM> ::=</TD>
160
 
</TR>
161
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
162
 
<TD ALIGN=left NOWRAP>    <CODE>[:</CODE> <EM>list-of-components-separated-by-semicolon</EM> <CODE>:]</CODE></TD>
163
 
</TR>
164
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
165
 
<TD ALIGN=left NOWRAP><EM>component</EM> ::=</TD>
166
 
</TR>
167
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
168
 
<TD ALIGN=left NOWRAP>        <CODE>`</CODE> <EM>stream-pattern-element</EM></TD>
169
 
</TR>
170
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
171
 
<TD ALIGN=left NOWRAP>      <CODE>|</CODE> <EM>pattern</EM> <CODE>=</CODE> <EM>expression</EM></TD>
172
 
</TR>
173
 
<TR><TD ALIGN=left NOWRAP>&nbsp;</TD>
174
 
<TD ALIGN=left NOWRAP>      <CODE>|</CODE> <EM>stream-pattern</EM></TD>
175
 
</TR></TABLE><BR>
176
 
 
177
 
 
178
 
The functional stream patterns elements syntax are actually the same
179
 
than in normal stream pattern.<BR>
180
 
<BR>
181
 
 
182
 
 
183
 
A functional stream is of type <CODE>'a Fstream.t</CODE> and a functional
184
 
stream parser of type
185
 
<CODE>'a Fstream.t -&gt; ('a * 'a Fstream.t) option</CODE>. When a parser
186
 
fails, it returns <CODE>None</CODE>, otherwise <CODE>Some</CODE> of the result and
187
 
the remaining stream. The elements in the initial stream are not
188
 
removed.<BR>
189
 
<BR>
190
 
 
191
 
 
192
 
A functional parser use limited backtrack. It is a backtrack in a
193
 
sense that when a rule fails, the next rule is tested with the initial
194
 
stream. If no rule applies, the functional parser returns
195
 
<CODE>None</CODE>. There is no Error exception causing the parsing to be
196
 
abandoned.<BR>
197
 
<BR>
198
 
 
199
 
 
200
 
The backtrack is limited in a sense that if a rule is
201
 
<CODE>[: p1 = e1; p2 = e2 :]</CODE>, if <CODE>e2</CODE> fails, the rule is
202
 
abandoned: there is no attempt to try the next possible rule inside
203
 
<CODE>e1</CODE> (which would suppose continuations).<BR>
204
 
<BR>
205
 
 
206
 
 
207
 
The functions available in the module <CODE>Fstream</CODE> are like the ones
208
 
in <CODE>Stream</CODE>. But there is no function ``Fstream.peek'', only
209
 
<CODE>Fstream.next</CODE>.<BR>
210
 
<BR>
211
 
 
212
 
 
213
 
Functional parsers have a drawback that in case of syntax error, one
214
 
cannot know where, since the parsing continues until all rules have
215
 
been tested. To turn around this problem, the function
216
 
<CODE>Fstream.count_frozen</CODE> returns the number of unfrozen tokens in
217
 
the stream, allowing to find the location of the error, providing a
218
 
location array have been used (which is normal usage in stream parsing
219
 
and grammars). It works if the stream had not been unfrozen before.
220
 
<BR>
221
 
<BR>
222
 
<I><FONT COLOR=maroon>
223
 
<br>
224
 
For remarks about Camlp4, write to:
225
 
<img src="http://cristal.inria.fr/~ddr/images/email.jpg" alt=email align=top>
226
 
</FONT></I><HR>
227
 
<A HREF="manual007.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
228
 
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
229
 
<A HREF="manual009.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
230
 
</BODY>
231
 
</HTML>