Coccinelle release 1.0.0-rc13
[bpt/coccinelle.git] / bundles / menhirLib / menhir-20120123 / src / standard.mly
1 /**************************************************************************/
2 /* */
3 /* Menhir */
4 /* */
5 /* François Pottier, INRIA Rocquencourt */
6 /* Yann Régis-Gianas, PPS, Université Paris Diderot */
7 /* */
8 /* Copyright 2005-2008 Institut National de Recherche en Informatique */
9 /* et en Automatique. All rights reserved. This file is distributed */
10 /* under the terms of the GNU Library General Public License, with the */
11 /* special exception on linking described in file LICENSE. */
12 /* */
13 /**************************************************************************/
14
15 (* This is menhir's standard library. It offers a number of
16 parameterized nonterminal definitions, such as options and lists,
17 that should be useful in a number of circumstances. *)
18
19 %%
20
21 (* ------------------------------------------------------------------------- *)
22 (* Options. *)
23
24 (* [option(X)] recognizes either nothing or [X]. It produces a value
25 of type ['a option] if [X] produces a value of type ['a]. *)
26
27 %public option(X):
28 /* nothing */
29 { None }
30 | x = X
31 { Some x }
32
33 (* [ioption(X)] is identical to [option(X)], except its definition is
34 inlined. This has the effect of duplicating the production that
35 refers to it, possibly eliminating an LR(1) conflict. *)
36
37 %public %inline ioption(X):
38 /* nothing */
39 { None }
40 | x = X
41 { Some x }
42
43 (* [boption(X)] recognizes either nothing or [X]. It produces a value
44 of type [bool]. *)
45
46 %public boption(X):
47 /* nothing */
48 { false }
49 | X
50 { true }
51
52 (* [loption(X)] recognizes either nothing or [X]. It produces a value
53 of type ['a list] if [X] produces a value of type ['a list]. *)
54
55 %public loption(X):
56 /* nothing */
57 { [] }
58 | x = X
59 { x }
60
61 (* ------------------------------------------------------------------------- *)
62 (* Sequences. *)
63
64 (* [pair(X, Y)] recognizes the sequence [X Y]. It produces a value of
65 type ['a * 'b] if [X] and [Y] produce values of type ['a] and ['b],
66 respectively. *)
67
68 %public %inline pair(X, Y):
69 x = X; y = Y
70 { (x, y) }
71
72 (* [separated_pair(X, sep, Y)] recognizes the sequence [X sep Y]. It
73 produces a value of type ['a * 'b] if [X] and [Y] produce values of
74 type ['a] and ['b], respectively. *)
75
76 %public %inline separated_pair(X, sep, Y):
77 x = X; sep; y = Y
78 { (x, y) }
79
80 (* [preceded(opening, X)] recognizes the sequence [opening X]. It
81 passes on the value produced by [X], so that it produces a value of
82 type ['a] if [X] produces a value of type ['a]. *)
83
84 %public %inline preceded(opening, X):
85 opening; x = X
86 { x }
87
88 (* [terminated(X, closing)] recognizes the sequence [X closing]. It
89 passes on the value produced by [X], so that it produces a value of
90 type ['a] if [X] produces a value of type ['a]. *)
91
92 %public %inline terminated(X, closing):
93 x = X; closing
94 { x }
95
96 (* [delimited(opening, X, closing)] recognizes the sequence [opening X
97 closing]. It passes on the value produced by [X], so that it
98 produces a value of type ['a] if [X] produces a value of type
99 ['a]. *)
100
101 %public %inline delimited(opening, X, closing):
102 opening; x = X; closing
103 { x }
104
105 (* ------------------------------------------------------------------------- *)
106 (* Lists. *)
107
108 (* [list(X)] recognizes a possibly empty list of [X]'s. It produces a
109 value of type ['a list] if [X] produces a value of type ['a]. The
110 front element of the list is the first element that was parsed. *)
111
112 %public list(X):
113 /* nothing */
114 { [] }
115 | x = X; xs = list(X)
116 { x :: xs }
117
118 (* [nonempty_list(X)] recognizes a nonempty list of [X]'s. It produces
119 a value of type ['a list] if [X] produces a value of type ['a]. The
120 front element of the list is the first element that was parsed. *)
121
122 %public nonempty_list(X):
123 x = X
124 { [ x ] }
125 | x = X; xs = nonempty_list(X)
126 { x :: xs }
127
128 (* [separated_list(separator, X)] recognizes a possibly empty list of
129 [X]'s, separated with [separator]'s. It produces a value of type
130 ['a list] if [X] produces a value of type ['a]. The front element
131 of the list is the first element that was parsed. *)
132
133 %public %inline separated_list(separator, X):
134 xs = loption(separated_nonempty_list(separator, X))
135 { xs }
136
137 (* [separated_nonempty_list(separator, X)] recognizes a nonempty list
138 of [X]'s, separated with [separator]'s. It produces a value of type
139 ['a list] if [X] produces a value of type ['a]. The front element
140 of the list is the first element that was parsed. *)
141
142 %public separated_nonempty_list(separator, X):
143 x = X
144 { [ x ] }
145 | x = X; separator; xs = separated_nonempty_list(separator, X)
146 { x :: xs }
147
148 %%
149