1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
2
"http://www.w3.org/TR/REC-html40/loose.dtd">
6
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7
<META name="GENERATOR" content="hevea 1.06">
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>
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 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>
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>
30
Camlp4 provides a system of functions defined by pattern matching,
31
which are extensible.<BR>
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
43
<TABLE CELLSPACING=2 CELLPADDING=0>
44
<TR><TD ALIGN=left NOWRAP><CODE>extfun</CODE> <EM>expression</EM> <CODE>with</CODE></TD>
46
<TR><TD ALIGN=left NOWRAP><CODE>[</CODE> <EM>pattern-1</EM> <CODE>-></CODE> <EM>expression-1</EM></TD>
48
<TR><TD ALIGN=left NOWRAP><CODE>|</CODE> <EM>pattern-2</EM> <CODE>-></CODE> <EM>expression-2</EM></TD>
50
<TR><TD ALIGN=left NOWRAP>...</TD>
52
<TR><TD ALIGN=left NOWRAP><CODE>|</CODE> <EM>pattern-n</EM> <CODE>-></CODE> <EM>expression-n</EM> <CODE>]</CODE></TD>
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>
65
``Or'' patterns can be used only at the first level. In this case:
67
pat1 | pat2 -> expr
68
</PRE>The binding is split into two cases (the expr is duplicated):
73
Internal ``or'' patterns inside patterns are not accepted.<BR>
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 -> 'b</CODE>. If matching
81
failed, such a function raises the exception <CODE>Extfun.Failure</CODE>.<BR>
85
The contents (patterns) of an extensible function can be displayed
86
using <CODE>Extfun.print</CODE>.<BR>
90
Remark: extensible functions are not efficient: when applied, all
91
patterns are tested, one by one, until one of them matches.<BR>
95
Extensible functions are used in Camlp4 extensible pretty printing.<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>
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>
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>
115
<TABLE CELLSPACING=2 CELLPADDING=0>
116
<TR><TD ALIGN=left NOWRAP> </TD>
117
<TD ALIGN=left NOWRAP><EM>functional-stream</EM> ::=</TD>
119
<TR><TD ALIGN=left NOWRAP> </TD>
120
<TD ALIGN=left NOWRAP> <CODE>fstream</CODE>
121
<CODE>[:</CODE> <EM>list-of-components-separated-by-semicolon</EM> <CODE>:]</CODE></TD>
123
<TR><TD ALIGN=left NOWRAP> </TD>
124
<TD ALIGN=left NOWRAP><EM>component</EM> ::=</TD>
126
<TR><TD ALIGN=left NOWRAP> </TD>
127
<TD ALIGN=left NOWRAP> <CODE>`</CODE> <EM>stream-element</EM></TD>
129
<TR><TD ALIGN=left NOWRAP> </TD>
130
<TD ALIGN=left NOWRAP> <CODE>|</CODE> <EM>stream</EM></TD>
134
and a functional parser, applying to a functional stream is:<BR>
138
<TABLE CELLSPACING=2 CELLPADDING=0>
139
<TR><TD ALIGN=left NOWRAP> </TD>
140
<TD ALIGN=left NOWRAP><EM>functional-parser</EM> ::=</TD>
142
<TR><TD ALIGN=left NOWRAP> </TD>
143
<TD ALIGN=left NOWRAP> <CODE>fparser</CODE></TD>
145
<TR><TD ALIGN=left NOWRAP> </TD>
146
<TD ALIGN=left NOWRAP> <CODE>[</CODE> <EM>stream-pattern-1</EM> <CODE>-></CODE> <EM>expression-1</EM></TD>
148
<TR><TD ALIGN=left NOWRAP> </TD>
149
<TD ALIGN=left NOWRAP> <CODE>|</CODE> <EM>stream-pattern-2</EM> <CODE>-></CODE> <EM>expression-2</EM></TD>
151
<TR><TD ALIGN=left NOWRAP> </TD>
152
<TD ALIGN=left NOWRAP> ..</TD>
154
<TR><TD ALIGN=left NOWRAP> </TD>
155
<TD ALIGN=left NOWRAP> <CODE>|</CODE> <EM>stream-pattern-n</EM> <CODE>-></CODE>
156
<EM>expression-n</EM> <CODE>]</CODE></TD>
158
<TR><TD ALIGN=left NOWRAP> </TD>
159
<TD ALIGN=left NOWRAP><EM>stream-pattern</EM> ::=</TD>
161
<TR><TD ALIGN=left NOWRAP> </TD>
162
<TD ALIGN=left NOWRAP> <CODE>[:</CODE> <EM>list-of-components-separated-by-semicolon</EM> <CODE>:]</CODE></TD>
164
<TR><TD ALIGN=left NOWRAP> </TD>
165
<TD ALIGN=left NOWRAP><EM>component</EM> ::=</TD>
167
<TR><TD ALIGN=left NOWRAP> </TD>
168
<TD ALIGN=left NOWRAP> <CODE>`</CODE> <EM>stream-pattern-element</EM></TD>
170
<TR><TD ALIGN=left NOWRAP> </TD>
171
<TD ALIGN=left NOWRAP> <CODE>|</CODE> <EM>pattern</EM> <CODE>=</CODE> <EM>expression</EM></TD>
173
<TR><TD ALIGN=left NOWRAP> </TD>
174
<TD ALIGN=left NOWRAP> <CODE>|</CODE> <EM>stream-pattern</EM></TD>
178
The functional stream patterns elements syntax are actually the same
179
than in normal stream pattern.<BR>
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 -> ('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
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
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>
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>
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.
222
<I><FONT COLOR=maroon>
224
For remarks about Camlp4, write to:
225
<img src="http://cristal.inria.fr/~ddr/images/email.jpg" alt=email align=top>
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>