1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Alain Frisch, LexiFi *)
6 (* *)
7 (* Copyright 2012 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 (** Helpers to produce Parsetree fragments
17
18 {b Warning} This module is unstable and part of
19 {{!Compiler_libs}compiler-libs}.
20
21 *)
22
23 open Asttypes
24 open Docstrings
25 open Parsetree
26
27 type 'a with_loc = 'a Location.loc
28 type loc = Location.t
29
30 type lid = Longident.t with_loc
31 type str = string with_loc
32 type str_opt = string option with_loc
33 type attrs = attribute list
34
35 (** {1 Default locations} *)
36
37 val default_loc: loc ref
38 (** Default value for all optional location arguments. *)
39
40 val with_default_loc: loc -> (unit -> 'a) -> 'a
41 (** Set the [default_loc] within the scope of the execution
42 of the provided function. *)
43
44 (** {1 Constants} *)
45
46 module Const : sig
47 val char : char -> constant
48 val string : ?quotation_delimiter:string -> string -> constant
49 val integer : ?suffix:char -> string -> constant
50 val int : ?suffix:char -> int -> constant
51 val int32 : ?suffix:char -> int32 -> constant
52 val int64 : ?suffix:char -> int64 -> constant
53 val nativeint : ?suffix:char -> nativeint -> constant
54 val float : ?suffix:char -> string -> constant
55 end
56
57 (** {1 Attributes} *)
58 module Attr : sig
59 val mk: ?loc:loc -> str -> payload -> attribute
60 end
61
62 (** {1 Core language} *)
63
64 (** Type expressions *)
65 module Typ :
66 sig
67 val mk: ?loc:loc -> ?attrs:attrs -> core_type_desc -> core_type
68 val attr: core_type -> attribute -> core_type
69
70 val any: ?loc:loc -> ?attrs:attrs -> unit -> core_type
71 val var: ?loc:loc -> ?attrs:attrs -> string -> core_type
72 val arrow: ?loc:loc -> ?attrs:attrs -> arg_label -> core_type -> core_type
73 -> core_type
74 val tuple: ?loc:loc -> ?attrs:attrs -> core_type list -> core_type
75 val constr: ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> core_type
76 val object_: ?loc:loc -> ?attrs:attrs -> object_field list
77 -> closed_flag -> core_type
78 val class_: ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> core_type
79 val alias: ?loc:loc -> ?attrs:attrs -> core_type -> string -> core_type
80 val variant: ?loc:loc -> ?attrs:attrs -> row_field list -> closed_flag
81 -> label list option -> core_type
82 val poly: ?loc:loc -> ?attrs:attrs -> str list -> core_type -> core_type
83 val package: ?loc:loc -> ?attrs:attrs -> lid -> (lid * core_type) list
84 -> core_type
85 val extension: ?loc:loc -> ?attrs:attrs -> extension -> core_type
86
87 val force_poly: core_type -> core_type
88
89 val varify_constructors: str list -> core_type -> core_type
90 (** [varify_constructors newtypes te] is type expression [te], of which
91 any of nullary type constructor [tc] is replaced by type variable of
92 the same name, if [tc]'s name appears in [newtypes].
93 Raise [Syntaxerr.Variable_in_scope] if any type variable inside [te]
94 appears in [newtypes].
95 @since 4.05
96 *)
97 end
98
99 (** Patterns *)
100 module Pat:
101 sig
102 val mk: ?loc:loc -> ?attrs:attrs -> pattern_desc -> pattern
103 val attr:pattern -> attribute -> pattern
104
105 val any: ?loc:loc -> ?attrs:attrs -> unit -> pattern
106 val var: ?loc:loc -> ?attrs:attrs -> str -> pattern
107 val alias: ?loc:loc -> ?attrs:attrs -> pattern -> str -> pattern
108 val constant: ?loc:loc -> ?attrs:attrs -> constant -> pattern
109 val interval: ?loc:loc -> ?attrs:attrs -> constant -> constant -> pattern
110 val tuple: ?loc:loc -> ?attrs:attrs -> pattern list -> pattern
111 val construct: ?loc:loc -> ?attrs:attrs -> lid -> pattern option -> pattern
112 val variant: ?loc:loc -> ?attrs:attrs -> label -> pattern option -> pattern
113 val record: ?loc:loc -> ?attrs:attrs -> (lid * pattern) list -> closed_flag
114 -> pattern
115 val array: ?loc:loc -> ?attrs:attrs -> pattern list -> pattern
116 val or_: ?loc:loc -> ?attrs:attrs -> pattern -> pattern -> pattern
117 val constraint_: ?loc:loc -> ?attrs:attrs -> pattern -> core_type -> pattern
118 val type_: ?loc:loc -> ?attrs:attrs -> lid -> pattern
119 val lazy_: ?loc:loc -> ?attrs:attrs -> pattern -> pattern
120 val unpack: ?loc:loc -> ?attrs:attrs -> str_opt -> pattern
121 val open_: ?loc:loc -> ?attrs:attrs -> lid -> pattern -> pattern
122 val exception_: ?loc:loc -> ?attrs:attrs -> pattern -> pattern
123 val extension: ?loc:loc -> ?attrs:attrs -> extension -> pattern
124 end
125
126 (** Expressions *)
127 module Exp:
128 sig
129 val mk: ?loc:loc -> ?attrs:attrs -> expression_desc -> expression
130 val attr: expression -> attribute -> expression
131
132 val ident: ?loc:loc -> ?attrs:attrs -> lid -> expression
133 val constant: ?loc:loc -> ?attrs:attrs -> constant -> expression
134 val let_: ?loc:loc -> ?attrs:attrs -> rec_flag -> value_binding list
135 -> expression -> expression
136 val fun_: ?loc:loc -> ?attrs:attrs -> arg_label -> expression option
137 -> pattern -> expression -> expression
138 val function_: ?loc:loc -> ?attrs:attrs -> case list -> expression
139 val apply: ?loc:loc -> ?attrs:attrs -> expression
140 -> (arg_label * expression) list -> expression
141 val match_: ?loc:loc -> ?attrs:attrs -> expression -> case list
142 -> expression
143 val try_: ?loc:loc -> ?attrs:attrs -> expression -> case list -> expression
144 val tuple: ?loc:loc -> ?attrs:attrs -> expression list -> expression
145 val construct: ?loc:loc -> ?attrs:attrs -> lid -> expression option
146 -> expression
147 val variant: ?loc:loc -> ?attrs:attrs -> label -> expression option
148 -> expression
149 val record: ?loc:loc -> ?attrs:attrs -> (lid * expression) list
150 -> expression option -> expression
151 val field: ?loc:loc -> ?attrs:attrs -> expression -> lid -> expression
152 val setfield: ?loc:loc -> ?attrs:attrs -> expression -> lid -> expression
153 -> expression
154 val array: ?loc:loc -> ?attrs:attrs -> expression list -> expression
155 val ifthenelse: ?loc:loc -> ?attrs:attrs -> expression -> expression
156 -> expression option -> expression
157 val sequence: ?loc:loc -> ?attrs:attrs -> expression -> expression
158 -> expression
159 val while_: ?loc:loc -> ?attrs:attrs -> expression -> expression
160 -> expression
161 val for_: ?loc:loc -> ?attrs:attrs -> pattern -> expression -> expression
162 -> direction_flag -> expression -> expression
163 val coerce: ?loc:loc -> ?attrs:attrs -> expression -> core_type option
164 -> core_type -> expression
165 val constraint_: ?loc:loc -> ?attrs:attrs -> expression -> core_type
166 -> expression
167 val send: ?loc:loc -> ?attrs:attrs -> expression -> str -> expression
168 val new_: ?loc:loc -> ?attrs:attrs -> lid -> expression
169 val setinstvar: ?loc:loc -> ?attrs:attrs -> str -> expression -> expression
170 val override: ?loc:loc -> ?attrs:attrs -> (str * expression) list
171 -> expression
172 val letmodule: ?loc:loc -> ?attrs:attrs -> str_opt -> module_expr
173 -> expression -> expression
174 val letexception:
175 ?loc:loc -> ?attrs:attrs -> extension_constructor -> expression
176 -> expression
177 val assert_: ?loc:loc -> ?attrs:attrs -> expression -> expression
178 val lazy_: ?loc:loc -> ?attrs:attrs -> expression -> expression
179 val poly: ?loc:loc -> ?attrs:attrs -> expression -> core_type option
180 -> expression
181 val object_: ?loc:loc -> ?attrs:attrs -> class_structure -> expression
182 val newtype: ?loc:loc -> ?attrs:attrs -> str -> expression -> expression
183 val pack: ?loc:loc -> ?attrs:attrs -> module_expr -> expression
184 val open_: ?loc:loc -> ?attrs:attrs -> open_declaration -> expression
185 -> expression
186 val letop: ?loc:loc -> ?attrs:attrs -> binding_op
187 -> binding_op list -> expression -> expression
188 val extension: ?loc:loc -> ?attrs:attrs -> extension -> expression
189 val unreachable: ?loc:loc -> ?attrs:attrs -> unit -> expression
190
191 val case: pattern -> ?guard:expression -> expression -> case
192 val binding_op: str -> pattern -> expression -> loc -> binding_op
193 end
194
195 (** Value declarations *)
196 module Val:
197 sig
198 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs ->
199 ?prim:string list -> str -> core_type -> value_description
200 end
201
202 (** Type declarations *)
203 module Type:
204 sig
205 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?text:text ->
206 ?params:(core_type * variance) list ->
207 ?cstrs:(core_type * core_type * loc) list ->
208 ?kind:type_kind -> ?priv:private_flag -> ?manifest:core_type -> str ->
209 type_declaration
210
211 val constructor: ?loc:loc -> ?attrs:attrs -> ?info:info ->
212 ?args:constructor_arguments -> ?res:core_type -> str ->
213 constructor_declaration
214 val field: ?loc:loc -> ?attrs:attrs -> ?info:info ->
215 ?mut:mutable_flag -> str -> core_type -> label_declaration
216 end
217
218 (** Type extensions *)
219 module Te:
220 sig
221 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs ->
222 ?params:(core_type * variance) list -> ?priv:private_flag ->
223 lid -> extension_constructor list -> type_extension
224
225 val mk_exception: ?loc:loc -> ?attrs:attrs -> ?docs:docs ->
226 extension_constructor -> type_exception
227
228 val constructor: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?info:info ->
229 str -> extension_constructor_kind -> extension_constructor
230
231 val decl: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?info:info ->
232 ?args:constructor_arguments -> ?res:core_type -> str ->
233 extension_constructor
234 val rebind: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?info:info ->
235 str -> lid -> extension_constructor
236 end
237
238 (** {1 Module language} *)
239
240 (** Module type expressions *)
241 module Mty:
242 sig
243 val mk: ?loc:loc -> ?attrs:attrs -> module_type_desc -> module_type
244 val attr: module_type -> attribute -> module_type
245
246 val ident: ?loc:loc -> ?attrs:attrs -> lid -> module_type
247 val alias: ?loc:loc -> ?attrs:attrs -> lid -> module_type
248 val signature: ?loc:loc -> ?attrs:attrs -> signature -> module_type
249 val functor_: ?loc:loc -> ?attrs:attrs ->
250 functor_parameter -> module_type -> module_type
251 val with_: ?loc:loc -> ?attrs:attrs -> module_type ->
252 with_constraint list -> module_type
253 val typeof_: ?loc:loc -> ?attrs:attrs -> module_expr -> module_type
254 val extension: ?loc:loc -> ?attrs:attrs -> extension -> module_type
255 end
256
257 (** Module expressions *)
258 module Mod:
259 sig
260 val mk: ?loc:loc -> ?attrs:attrs -> module_expr_desc -> module_expr
261 val attr: module_expr -> attribute -> module_expr
262
263 val ident: ?loc:loc -> ?attrs:attrs -> lid -> module_expr
264 val structure: ?loc:loc -> ?attrs:attrs -> structure -> module_expr
265 val functor_: ?loc:loc -> ?attrs:attrs ->
266 functor_parameter -> module_expr -> module_expr
267 val apply: ?loc:loc -> ?attrs:attrs -> module_expr -> module_expr ->
268 module_expr
269 val constraint_: ?loc:loc -> ?attrs:attrs -> module_expr -> module_type ->
270 module_expr
271 val unpack: ?loc:loc -> ?attrs:attrs -> expression -> module_expr
272 val extension: ?loc:loc -> ?attrs:attrs -> extension -> module_expr
273 end
274
275 (** Signature items *)
276 module Sig:
277 sig
278 val mk: ?loc:loc -> signature_item_desc -> signature_item
279
280 val value: ?loc:loc -> value_description -> signature_item
281 val type_: ?loc:loc -> rec_flag -> type_declaration list -> signature_item
282 val type_subst: ?loc:loc -> type_declaration list -> signature_item
283 val type_extension: ?loc:loc -> type_extension -> signature_item
284 val exception_: ?loc:loc -> type_exception -> signature_item
285 val module_: ?loc:loc -> module_declaration -> signature_item
286 val mod_subst: ?loc:loc -> module_substitution -> signature_item
287 val rec_module: ?loc:loc -> module_declaration list -> signature_item
288 val modtype: ?loc:loc -> module_type_declaration -> signature_item
289 val open_: ?loc:loc -> open_description -> signature_item
290 val include_: ?loc:loc -> include_description -> signature_item
291 val class_: ?loc:loc -> class_description list -> signature_item
292 val class_type: ?loc:loc -> class_type_declaration list -> signature_item
293 val extension: ?loc:loc -> ?attrs:attrs -> extension -> signature_item
294 val attribute: ?loc:loc -> attribute -> signature_item
295 val text: text -> signature_item list
296 end
297
298 (** Structure items *)
299 module Str:
300 sig
301 val mk: ?loc:loc -> structure_item_desc -> structure_item
302
303 val eval: ?loc:loc -> ?attrs:attributes -> expression -> structure_item
304 val value: ?loc:loc -> rec_flag -> value_binding list -> structure_item
305 val primitive: ?loc:loc -> value_description -> structure_item
306 val type_: ?loc:loc -> rec_flag -> type_declaration list -> structure_item
307 val type_extension: ?loc:loc -> type_extension -> structure_item
308 val exception_: ?loc:loc -> type_exception -> structure_item
309 val module_: ?loc:loc -> module_binding -> structure_item
310 val rec_module: ?loc:loc -> module_binding list -> structure_item
311 val modtype: ?loc:loc -> module_type_declaration -> structure_item
312 val open_: ?loc:loc -> open_declaration -> structure_item
313 val class_: ?loc:loc -> class_declaration list -> structure_item
314 val class_type: ?loc:loc -> class_type_declaration list -> structure_item
315 val include_: ?loc:loc -> include_declaration -> structure_item
316 val extension: ?loc:loc -> ?attrs:attrs -> extension -> structure_item
317 val attribute: ?loc:loc -> attribute -> structure_item
318 val text: text -> structure_item list
319 end
320
321 (** Module declarations *)
322 module Md:
323 sig
324 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?text:text ->
325 str_opt -> module_type -> module_declaration
326 end
327
328 (** Module substitutions *)
329 module Ms:
330 sig
331 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?text:text ->
332 str -> lid -> module_substitution
333 end
334
335 (** Module type declarations *)
336 module Mtd:
337 sig
338 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?text:text ->
339 ?typ:module_type -> str -> module_type_declaration
340 end
341
342 (** Module bindings *)
343 module Mb:
344 sig
345 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?text:text ->
346 str_opt -> module_expr -> module_binding
347 end
348
349 (** Opens *)
350 module Opn:
351 sig
352 val mk: ?loc: loc -> ?attrs:attrs -> ?docs:docs ->
353 ?override:override_flag -> 'a -> 'a open_infos
354 end
355
356 (** Includes *)
357 module Incl:
358 sig
359 val mk: ?loc: loc -> ?attrs:attrs -> ?docs:docs -> 'a -> 'a include_infos
360 end
361
362 (** Value bindings *)
363 module Vb:
364 sig
365 val mk: ?loc: loc -> ?attrs:attrs -> ?docs:docs -> ?text:text ->
366 pattern -> expression -> value_binding
367 end
368
369
370 (** {1 Class language} *)
371
372 (** Class type expressions *)
373 module Cty:
374 sig
375 val mk: ?loc:loc -> ?attrs:attrs -> class_type_desc -> class_type
376 val attr: class_type -> attribute -> class_type
377
378 val constr: ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> class_type
379 val signature: ?loc:loc -> ?attrs:attrs -> class_signature -> class_type
380 val arrow: ?loc:loc -> ?attrs:attrs -> arg_label -> core_type ->
381 class_type -> class_type
382 val extension: ?loc:loc -> ?attrs:attrs -> extension -> class_type
383 val open_: ?loc:loc -> ?attrs:attrs -> open_description -> class_type
384 -> class_type
385 end
386
387 (** Class type fields *)
388 module Ctf:
389 sig
390 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs ->
391 class_type_field_desc -> class_type_field
392 val attr: class_type_field -> attribute -> class_type_field
393
394 val inherit_: ?loc:loc -> ?attrs:attrs -> class_type -> class_type_field
395 val val_: ?loc:loc -> ?attrs:attrs -> str -> mutable_flag ->
396 virtual_flag -> core_type -> class_type_field
397 val method_: ?loc:loc -> ?attrs:attrs -> str -> private_flag ->
398 virtual_flag -> core_type -> class_type_field
399 val constraint_: ?loc:loc -> ?attrs:attrs -> core_type -> core_type ->
400 class_type_field
401 val extension: ?loc:loc -> ?attrs:attrs -> extension -> class_type_field
402 val attribute: ?loc:loc -> attribute -> class_type_field
403 val text: text -> class_type_field list
404 end
405
406 (** Class expressions *)
407 module Cl:
408 sig
409 val mk: ?loc:loc -> ?attrs:attrs -> class_expr_desc -> class_expr
410 val attr: class_expr -> attribute -> class_expr
411
412 val constr: ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> class_expr
413 val structure: ?loc:loc -> ?attrs:attrs -> class_structure -> class_expr
414 val fun_: ?loc:loc -> ?attrs:attrs -> arg_label -> expression option ->
415 pattern -> class_expr -> class_expr
416 val apply: ?loc:loc -> ?attrs:attrs -> class_expr ->
417 (arg_label * expression) list -> class_expr
418 val let_: ?loc:loc -> ?attrs:attrs -> rec_flag -> value_binding list ->
419 class_expr -> class_expr
420 val constraint_: ?loc:loc -> ?attrs:attrs -> class_expr -> class_type ->
421 class_expr
422 val extension: ?loc:loc -> ?attrs:attrs -> extension -> class_expr
423 val open_: ?loc:loc -> ?attrs:attrs -> open_description -> class_expr
424 -> class_expr
425 end
426
427 (** Class fields *)
428 module Cf:
429 sig
430 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> class_field_desc ->
431 class_field
432 val attr: class_field -> attribute -> class_field
433
434 val inherit_: ?loc:loc -> ?attrs:attrs -> override_flag -> class_expr ->
435 str option -> class_field
436 val val_: ?loc:loc -> ?attrs:attrs -> str -> mutable_flag ->
437 class_field_kind -> class_field
438 val method_: ?loc:loc -> ?attrs:attrs -> str -> private_flag ->
439 class_field_kind -> class_field
440 val constraint_: ?loc:loc -> ?attrs:attrs -> core_type -> core_type ->
441 class_field
442 val initializer_: ?loc:loc -> ?attrs:attrs -> expression -> class_field
443 val extension: ?loc:loc -> ?attrs:attrs -> extension -> class_field
444 val attribute: ?loc:loc -> attribute -> class_field
445 val text: text -> class_field list
446
447 val virtual_: core_type -> class_field_kind
448 val concrete: override_flag -> expression -> class_field_kind
449
450 end
451
452 (** Classes *)
453 module Ci:
454 sig
455 val mk: ?loc:loc -> ?attrs:attrs -> ?docs:docs -> ?text:text ->
456 ?virt:virtual_flag -> ?params:(core_type * variance) list ->
457 str -> 'a -> 'a class_infos
458 end
459
460 (** Class signatures *)
461 module Csig:
462 sig
463 val mk: core_type -> class_type_field list -> class_signature
464 end
465
466 (** Class structures *)
467 module Cstr:
468 sig
469 val mk: pattern -> class_field list -> class_structure
470 end
471
472 (** Row fields *)
473 module Rf:
474 sig
475 val mk: ?loc:loc -> ?attrs:attrs -> row_field_desc -> row_field
476 val tag: ?loc:loc -> ?attrs:attrs ->
477 label with_loc -> bool -> core_type list -> row_field
478 val inherit_: ?loc:loc -> core_type -> row_field
479 end
480
481 (** Object fields *)
482 module Of:
483 sig
484 val mk: ?loc:loc -> ?attrs:attrs ->
485 object_field_desc -> object_field
486 val tag: ?loc:loc -> ?attrs:attrs ->
487 label with_loc -> core_type -> object_field
488 val inherit_: ?loc:loc -> core_type -> object_field
489 end
490