1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1996 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 (** Abstract syntax tree produced by parsing
17
18 {b Warning:} this module is unstable and part of
19 {{!Compiler_libs}compiler-libs}.
20
21 *)
22
23 open Asttypes
24
25 type constant =
26 Pconst_integer of string * char option
27 (* 3 3l 3L 3n
28
29 Suffixes [g-z][G-Z] are accepted by the parser.
30 Suffixes except 'l', 'L' and 'n' are rejected by the typechecker
31 *)
32 | Pconst_char of char
33 (* 'c' *)
34 | Pconst_string of string * string option
35 (* "constant"
36 {delim|other constant|delim}
37 *)
38 | Pconst_float of string * char option
39 (* 3.4 2e5 1.4e-4
40
41 Suffixes [g-z][G-Z] are accepted by the parser.
42 Suffixes are rejected by the typechecker.
43 *)
44
45 type location_stack = Location.t list
46
47 (** {1 Extension points} *)
48
49 type attribute = {
50 attr_name : string loc;
51 attr_payload : payload;
52 attr_loc : Location.t;
53 }
54 (* [@id ARG]
55 [@@id ARG]
56
57 Metadata containers passed around within the AST.
58 The compiler ignores unknown attributes.
59 *)
60
61 and extension = string loc * payload
62 (* [%id ARG]
63 [%%id ARG]
64
65 Sub-language placeholder -- rejected by the typechecker.
66 *)
67
68 and attributes = attribute list
69
70 and payload =
71 | PStr of structure
72 | PSig of signature (* : SIG *)
73 | PTyp of core_type (* : T *)
74 | PPat of pattern * expression option (* ? P or ? P when E *)
75
76 (** {1 Core language} *)
77
78 (* Type expressions *)
79
80 and core_type =
81 {
82 ptyp_desc: core_type_desc;
83 ptyp_loc: Location.t;
84 ptyp_loc_stack: location_stack;
85 ptyp_attributes: attributes; (* ... [@id1] [@id2] *)
86 }
87
88 and core_type_desc =
89 | Ptyp_any
90 (* _ *)
91 | Ptyp_var of string
92 (* 'a *)
93 | Ptyp_arrow of arg_label * core_type * core_type
94 (* T1 -> T2 Simple
95 ~l:T1 -> T2 Labelled
96 ?l:T1 -> T2 Optional
97 *)
98 | Ptyp_tuple of core_type list
99 (* T1 * ... * Tn
100
101 Invariant: n >= 2
102 *)
103 | Ptyp_constr of Longident.t loc * core_type list
104 (* tconstr
105 T tconstr
106 (T1, ..., Tn) tconstr
107 *)
108 | Ptyp_object of object_field list * closed_flag
109 (* < l1:T1; ...; ln:Tn > (flag = Closed)
110 < l1:T1; ...; ln:Tn; .. > (flag = Open)
111 *)
112 | Ptyp_class of Longident.t loc * core_type list
113 (* #tconstr
114 T #tconstr
115 (T1, ..., Tn) #tconstr
116 *)
117 | Ptyp_alias of core_type * string
118 (* T as 'a *)
119 | Ptyp_variant of row_field list * closed_flag * label list option
120 (* [ `A|`B ] (flag = Closed; labels = None)
121 [> `A|`B ] (flag = Open; labels = None)
122 [< `A|`B ] (flag = Closed; labels = Some [])
123 [< `A|`B > `X `Y ](flag = Closed; labels = Some ["X";"Y"])
124 *)
125 | Ptyp_poly of string loc list * core_type
126 (* 'a1 ... 'an. T
127
128 Can only appear in the following context:
129
130 - As the core_type of a Ppat_constraint node corresponding
131 to a constraint on a let-binding: let x : 'a1 ... 'an. T
132 = e ...
133
134 - Under Cfk_virtual for methods (not values).
135
136 - As the core_type of a Pctf_method node.
137
138 - As the core_type of a Pexp_poly node.
139
140 - As the pld_type field of a label_declaration.
141
142 - As a core_type of a Ptyp_object node.
143 *)
144
145 | Ptyp_package of package_type
146 (* (module S) *)
147 | Ptyp_extension of extension
148 (* [%id] *)
149
150 and package_type = Longident.t loc * (Longident.t loc * core_type) list
151 (*
152 (module S)
153 (module S with type t1 = T1 and ... and tn = Tn)
154 *)
155
156 and row_field = {
157 prf_desc : row_field_desc;
158 prf_loc : Location.t;
159 prf_attributes : attributes;
160 }
161
162 and row_field_desc =
163 | Rtag of label loc * bool * core_type list
164 (* [`A] ( true, [] )
165 [`A of T] ( false, [T] )
166 [`A of T1 & .. & Tn] ( false, [T1;...Tn] )
167 [`A of & T1 & .. & Tn] ( true, [T1;...Tn] )
168
169 - The 'bool' field is true if the tag contains a
170 constant (empty) constructor.
171 - '&' occurs when several types are used for the same constructor
172 (see 4.2 in the manual)
173 *)
174 | Rinherit of core_type
175 (* [ T ] *)
176
177 and object_field = {
178 pof_desc : object_field_desc;
179 pof_loc : Location.t;
180 pof_attributes : attributes;
181 }
182
183 and object_field_desc =
184 | Otag of label loc * core_type
185 | Oinherit of core_type
186
187 (* Patterns *)
188
189 and pattern =
190 {
191 ppat_desc: pattern_desc;
192 ppat_loc: Location.t;
193 ppat_loc_stack: location_stack;
194 ppat_attributes: attributes; (* ... [@id1] [@id2] *)
195 }
196
197 and pattern_desc =
198 | Ppat_any
199 (* _ *)
200 | Ppat_var of string loc
201 (* x *)
202 | Ppat_alias of pattern * string loc
203 (* P as 'a *)
204 | Ppat_constant of constant
205 (* 1, 'a', "true", 1.0, 1l, 1L, 1n *)
206 | Ppat_interval of constant * constant
207 (* 'a'..'z'
208
209 Other forms of interval are recognized by the parser
210 but rejected by the type-checker. *)
211 | Ppat_tuple of pattern list
212 (* (P1, ..., Pn)
213
214 Invariant: n >= 2
215 *)
216 | Ppat_construct of Longident.t loc * pattern option
217 (* C None
218 C P Some P
219 C (P1, ..., Pn) Some (Ppat_tuple [P1; ...; Pn])
220 *)
221 | Ppat_variant of label * pattern option
222 (* `A (None)
223 `A P (Some P)
224 *)
225 | Ppat_record of (Longident.t loc * pattern) list * closed_flag
226 (* { l1=P1; ...; ln=Pn } (flag = Closed)
227 { l1=P1; ...; ln=Pn; _} (flag = Open)
228
229 Invariant: n > 0
230 *)
231 | Ppat_array of pattern list
232 (* [| P1; ...; Pn |] *)
233 | Ppat_or of pattern * pattern
234 (* P1 | P2 *)
235 | Ppat_constraint of pattern * core_type
236 (* (P : T) *)
237 | Ppat_type of Longident.t loc
238 (* #tconst *)
239 | Ppat_lazy of pattern
240 (* lazy P *)
241 | Ppat_unpack of string option loc
242 (* (module P) Some "P"
243 (module _) None
244
245 Note: (module P : S) is represented as
246 Ppat_constraint(Ppat_unpack, Ptyp_package)
247 *)
248 | Ppat_exception of pattern
249 (* exception P *)
250 | Ppat_extension of extension
251 (* [%id] *)
252 | Ppat_open of Longident.t loc * pattern
253 (* M.(P) *)
254
255 (* Value expressions *)
256
257 and expression =
258 {
259 pexp_desc: expression_desc;
260 pexp_loc: Location.t;
261 pexp_loc_stack: location_stack;
262 pexp_attributes: attributes; (* ... [@id1] [@id2] *)
263 }
264
265 and expression_desc =
266 | Pexp_ident of Longident.t loc
267 (* x
268 M.x
269 *)
270 | Pexp_constant of constant
271 (* 1, 'a', "true", 1.0, 1l, 1L, 1n *)
272 | Pexp_let of rec_flag * value_binding list * expression
273 (* let P1 = E1 and ... and Pn = EN in E (flag = Nonrecursive)
274 let rec P1 = E1 and ... and Pn = EN in E (flag = Recursive)
275 *)
276 | Pexp_function of case list
277 (* function P1 -> E1 | ... | Pn -> En *)
278 | Pexp_fun of arg_label * expression option * pattern * expression
279 (* fun P -> E1 (Simple, None)
280 fun ~l:P -> E1 (Labelled l, None)
281 fun ?l:P -> E1 (Optional l, None)
282 fun ?l:(P = E0) -> E1 (Optional l, Some E0)
283
284 Notes:
285 - If E0 is provided, only Optional is allowed.
286 - "fun P1 P2 .. Pn -> E1" is represented as nested Pexp_fun.
287 - "let f P = E" is represented using Pexp_fun.
288 *)
289 | Pexp_apply of expression * (arg_label * expression) list
290 (* E0 ~l1:E1 ... ~ln:En
291 li can be empty (non labeled argument) or start with '?'
292 (optional argument).
293
294 Invariant: n > 0
295 *)
296 | Pexp_match of expression * case list
297 (* match E0 with P1 -> E1 | ... | Pn -> En *)
298 | Pexp_try of expression * case list
299 (* try E0 with P1 -> E1 | ... | Pn -> En *)
300 | Pexp_tuple of expression list
301 (* (E1, ..., En)
302
303 Invariant: n >= 2
304 *)
305 | Pexp_construct of Longident.t loc * expression option
306 (* C None
307 C E Some E
308 C (E1, ..., En) Some (Pexp_tuple[E1;...;En])
309 *)
310 | Pexp_variant of label * expression option
311 (* `A (None)
312 `A E (Some E)
313 *)
314 | Pexp_record of (Longident.t loc * expression) list * expression option
315 (* { l1=P1; ...; ln=Pn } (None)
316 { E0 with l1=P1; ...; ln=Pn } (Some E0)
317
318 Invariant: n > 0
319 *)
320 | Pexp_field of expression * Longident.t loc
321 (* E.l *)
322 | Pexp_setfield of expression * Longident.t loc * expression
323 (* E1.l <- E2 *)
324 | Pexp_array of expression list
325 (* [| E1; ...; En |] *)
326 | Pexp_ifthenelse of expression * expression * expression option
327 (* if E1 then E2 else E3 *)
328 | Pexp_sequence of expression * expression
329 (* E1; E2 *)
330 | Pexp_while of expression * expression
331 (* while E1 do E2 done *)
332 | Pexp_for of
333 pattern * expression * expression * direction_flag * expression
334 (* for i = E1 to E2 do E3 done (flag = Upto)
335 for i = E1 downto E2 do E3 done (flag = Downto)
336 *)
337 | Pexp_constraint of expression * core_type
338 (* (E : T) *)
339 | Pexp_coerce of expression * core_type option * core_type
340 (* (E :> T) (None, T)
341 (E : T0 :> T) (Some T0, T)
342 *)
343 | Pexp_send of expression * label loc
344 (* E # m *)
345 | Pexp_new of Longident.t loc
346 (* new M.c *)
347 | Pexp_setinstvar of label loc * expression
348 (* x <- 2 *)
349 | Pexp_override of (label loc * expression) list
350 (* {< x1 = E1; ...; Xn = En >} *)
351 | Pexp_letmodule of string option loc * module_expr * expression
352 (* let module M = ME in E *)
353 | Pexp_letexception of extension_constructor * expression
354 (* let exception C in E *)
355 | Pexp_assert of expression
356 (* assert E
357 Note: "assert false" is treated in a special way by the
358 type-checker. *)
359 | Pexp_lazy of expression
360 (* lazy E *)
361 | Pexp_poly of expression * core_type option
362 (* Used for method bodies.
363
364 Can only be used as the expression under Cfk_concrete
365 for methods (not values). *)
366 | Pexp_object of class_structure
367 (* object ... end *)
368 | Pexp_newtype of string loc * expression
369 (* fun (type t) -> E *)
370 | Pexp_pack of module_expr
371 (* (module ME)
372
373 (module ME : S) is represented as
374 Pexp_constraint(Pexp_pack, Ptyp_package S) *)
375 | Pexp_open of open_declaration * expression
376 (* M.(E)
377 let open M in E
378 let! open M in E *)
379 | Pexp_letop of letop
380 (* let* P = E in E
381 let* P = E and* P = E in E *)
382 | Pexp_extension of extension
383 (* [%id] *)
384 | Pexp_unreachable
385 (* . *)
386
387 and case = (* (P -> E) or (P when E0 -> E) *)
388 {
389 pc_lhs: pattern;
390 pc_guard: expression option;
391 pc_rhs: expression;
392 }
393
394 and letop =
395 {
396 let_ : binding_op;
397 ands : binding_op list;
398 body : expression;
399 }
400
401 and binding_op =
402 {
403 pbop_op : string loc;
404 pbop_pat : pattern;
405 pbop_exp : expression;
406 pbop_loc : Location.t;
407 }
408
409 (* Value descriptions *)
410
411 and value_description =
412 {
413 pval_name: string loc;
414 pval_type: core_type;
415 pval_prim: string list;
416 pval_attributes: attributes; (* ... [@@id1] [@@id2] *)
417 pval_loc: Location.t;
418 }
419
420 (*
421 val x: T (prim = [])
422 external x: T = "s1" ... "sn" (prim = ["s1";..."sn"])
423 *)
424
425 (* Type declarations *)
426
427 and type_declaration =
428 {
429 ptype_name: string loc;
430 ptype_params: (core_type * variance) list;
431 (* ('a1,...'an) t; None represents _*)
432 ptype_cstrs: (core_type * core_type * Location.t) list;
433 (* ... constraint T1=T1' ... constraint Tn=Tn' *)
434 ptype_kind: type_kind;
435 ptype_private: private_flag; (* = private ... *)
436 ptype_manifest: core_type option; (* = T *)
437 ptype_attributes: attributes; (* ... [@@id1] [@@id2] *)
438 ptype_loc: Location.t;
439 }
440
441 (*
442 type t (abstract, no manifest)
443 type t = T0 (abstract, manifest=T0)
444 type t = C of T | ... (variant, no manifest)
445 type t = T0 = C of T | ... (variant, manifest=T0)
446 type t = {l: T; ...} (record, no manifest)
447 type t = T0 = {l : T; ...} (record, manifest=T0)
448 type t = .. (open, no manifest)
449 *)
450
451 and type_kind =
452 | Ptype_abstract
453 | Ptype_variant of constructor_declaration list
454 | Ptype_record of label_declaration list
455 (* Invariant: non-empty list *)
456 | Ptype_open
457
458 and label_declaration =
459 {
460 pld_name: string loc;
461 pld_mutable: mutable_flag;
462 pld_type: core_type;
463 pld_loc: Location.t;
464 pld_attributes: attributes; (* l : T [@id1] [@id2] *)
465 }
466
467 (* { ...; l: T; ... } (mutable=Immutable)
468 { ...; mutable l: T; ... } (mutable=Mutable)
469
470 Note: T can be a Ptyp_poly.
471 *)
472
473 and constructor_declaration =
474 {
475 pcd_name: string loc;
476 pcd_args: constructor_arguments;
477 pcd_res: core_type option;
478 pcd_loc: Location.t;
479 pcd_attributes: attributes; (* C of ... [@id1] [@id2] *)
480 }
481
482 and constructor_arguments =
483 | Pcstr_tuple of core_type list
484 | Pcstr_record of label_declaration list
485
486 (*
487 | C of T1 * ... * Tn (res = None, args = Pcstr_tuple [])
488 | C: T0 (res = Some T0, args = [])
489 | C: T1 * ... * Tn -> T0 (res = Some T0, args = Pcstr_tuple)
490 | C of {...} (res = None, args = Pcstr_record)
491 | C: {...} -> T0 (res = Some T0, args = Pcstr_record)
492 | C of {...} as t (res = None, args = Pcstr_record)
493 *)
494
495 and type_extension =
496 {
497 ptyext_path: Longident.t loc;
498 ptyext_params: (core_type * variance) list;
499 ptyext_constructors: extension_constructor list;
500 ptyext_private: private_flag;
501 ptyext_loc: Location.t;
502 ptyext_attributes: attributes; (* ... [@@id1] [@@id2] *)
503 }
504 (*
505 type t += ...
506 *)
507
508 and extension_constructor =
509 {
510 pext_name: string loc;
511 pext_kind : extension_constructor_kind;
512 pext_loc : Location.t;
513 pext_attributes: attributes; (* C of ... [@id1] [@id2] *)
514 }
515
516 (* exception E *)
517 and type_exception =
518 {
519 ptyexn_constructor: extension_constructor;
520 ptyexn_loc: Location.t;
521 ptyexn_attributes: attributes; (* ... [@@id1] [@@id2] *)
522 }
523
524 and extension_constructor_kind =
525 Pext_decl of constructor_arguments * core_type option
526 (*
527 | C of T1 * ... * Tn ([T1; ...; Tn], None)
528 | C: T0 ([], Some T0)
529 | C: T1 * ... * Tn -> T0 ([T1; ...; Tn], Some T0)
530 *)
531 | Pext_rebind of Longident.t loc
532 (*
533 | C = D
534 *)
535
536 (** {1 Class language} *)
537
538 (* Type expressions for the class language *)
539
540 and class_type =
541 {
542 pcty_desc: class_type_desc;
543 pcty_loc: Location.t;
544 pcty_attributes: attributes; (* ... [@id1] [@id2] *)
545 }
546
547 and class_type_desc =
548 | Pcty_constr of Longident.t loc * core_type list
549 (* c
550 ['a1, ..., 'an] c *)
551 | Pcty_signature of class_signature
552 (* object ... end *)
553 | Pcty_arrow of arg_label * core_type * class_type
554 (* T -> CT Simple
555 ~l:T -> CT Labelled l
556 ?l:T -> CT Optional l
557 *)
558 | Pcty_extension of extension
559 (* [%id] *)
560 | Pcty_open of open_description * class_type
561 (* let open M in CT *)
562
563 and class_signature =
564 {
565 pcsig_self: core_type;
566 pcsig_fields: class_type_field list;
567 }
568 (* object('selfpat) ... end
569 object ... end (self = Ptyp_any)
570 *)
571
572 and class_type_field =
573 {
574 pctf_desc: class_type_field_desc;
575 pctf_loc: Location.t;
576 pctf_attributes: attributes; (* ... [@@id1] [@@id2] *)
577 }
578
579 and class_type_field_desc =
580 | Pctf_inherit of class_type
581 (* inherit CT *)
582 | Pctf_val of (label loc * mutable_flag * virtual_flag * core_type)
583 (* val x: T *)
584 | Pctf_method of (label loc * private_flag * virtual_flag * core_type)
585 (* method x: T
586
587 Note: T can be a Ptyp_poly.
588 *)
589 | Pctf_constraint of (core_type * core_type)
590 (* constraint T1 = T2 *)
591 | Pctf_attribute of attribute
592 (* [@@@id] *)
593 | Pctf_extension of extension
594 (* [%%id] *)
595
596 and 'a class_infos =
597 {
598 pci_virt: virtual_flag;
599 pci_params: (core_type * variance) list;
600 pci_name: string loc;
601 pci_expr: 'a;
602 pci_loc: Location.t;
603 pci_attributes: attributes; (* ... [@@id1] [@@id2] *)
604 }
605 (* class c = ...
606 class ['a1,...,'an] c = ...
607 class virtual c = ...
608
609 Also used for "class type" declaration.
610 *)
611
612 and class_description = class_type class_infos
613
614 and class_type_declaration = class_type class_infos
615
616 (* Value expressions for the class language *)
617
618 and class_expr =
619 {
620 pcl_desc: class_expr_desc;
621 pcl_loc: Location.t;
622 pcl_attributes: attributes; (* ... [@id1] [@id2] *)
623 }
624
625 and class_expr_desc =
626 | Pcl_constr of Longident.t loc * core_type list
627 (* c
628 ['a1, ..., 'an] c *)
629 | Pcl_structure of class_structure
630 (* object ... end *)
631 | Pcl_fun of arg_label * expression option * pattern * class_expr
632 (* fun P -> CE (Simple, None)
633 fun ~l:P -> CE (Labelled l, None)
634 fun ?l:P -> CE (Optional l, None)
635 fun ?l:(P = E0) -> CE (Optional l, Some E0)
636 *)
637 | Pcl_apply of class_expr * (arg_label * expression) list
638 (* CE ~l1:E1 ... ~ln:En
639 li can be empty (non labeled argument) or start with '?'
640 (optional argument).
641
642 Invariant: n > 0
643 *)
644 | Pcl_let of rec_flag * value_binding list * class_expr
645 (* let P1 = E1 and ... and Pn = EN in CE (flag = Nonrecursive)
646 let rec P1 = E1 and ... and Pn = EN in CE (flag = Recursive)
647 *)
648 | Pcl_constraint of class_expr * class_type
649 (* (CE : CT) *)
650 | Pcl_extension of extension
651 (* [%id] *)
652 | Pcl_open of open_description * class_expr
653 (* let open M in CE *)
654
655
656 and class_structure =
657 {
658 pcstr_self: pattern;
659 pcstr_fields: class_field list;
660 }
661 (* object(selfpat) ... end
662 object ... end (self = Ppat_any)
663 *)
664
665 and class_field =
666 {
667 pcf_desc: class_field_desc;
668 pcf_loc: Location.t;
669 pcf_attributes: attributes; (* ... [@@id1] [@@id2] *)
670 }
671
672 and class_field_desc =
673 | Pcf_inherit of override_flag * class_expr * string loc option
674 (* inherit CE
675 inherit CE as x
676 inherit! CE
677 inherit! CE as x
678 *)
679 | Pcf_val of (label loc * mutable_flag * class_field_kind)
680 (* val x = E
681 val virtual x: T
682 *)
683 | Pcf_method of (label loc * private_flag * class_field_kind)
684 (* method x = E (E can be a Pexp_poly)
685 method virtual x: T (T can be a Ptyp_poly)
686 *)
687 | Pcf_constraint of (core_type * core_type)
688 (* constraint T1 = T2 *)
689 | Pcf_initializer of expression
690 (* initializer E *)
691 | Pcf_attribute of attribute
692 (* [@@@id] *)
693 | Pcf_extension of extension
694 (* [%%id] *)
695
696 and class_field_kind =
697 | Cfk_virtual of core_type
698 | Cfk_concrete of override_flag * expression
699
700 and class_declaration = class_expr class_infos
701
702 (** {1 Module language} *)
703
704 (* Type expressions for the module language *)
705
706 and module_type =
707 {
708 pmty_desc: module_type_desc;
709 pmty_loc: Location.t;
710 pmty_attributes: attributes; (* ... [@id1] [@id2] *)
711 }
712
713 and module_type_desc =
714 | Pmty_ident of Longident.t loc
715 (* S *)
716 | Pmty_signature of signature
717 (* sig ... end *)
718 | Pmty_functor of functor_parameter * module_type
719 (* functor(X : MT1) -> MT2 *)
720 | Pmty_with of module_type * with_constraint list
721 (* MT with ... *)
722 | Pmty_typeof of module_expr
723 (* module type of ME *)
724 | Pmty_extension of extension
725 (* [%id] *)
726 | Pmty_alias of Longident.t loc
727 (* (module M) *)
728
729 and functor_parameter =
730 | Unit
731 (* () *)
732 | Named of string option loc * module_type
733 (* (X : MT) Some X, MT
734 (_ : MT) None, MT *)
735
736 and signature = signature_item list
737
738 and signature_item =
739 {
740 psig_desc: signature_item_desc;
741 psig_loc: Location.t;
742 }
743
744 and signature_item_desc =
745 | Psig_value of value_description
746 (*
747 val x: T
748 external x: T = "s1" ... "sn"
749 *)
750 | Psig_type of rec_flag * type_declaration list
751 (* type t1 = ... and ... and tn = ... *)
752 | Psig_typesubst of type_declaration list
753 (* type t1 := ... and ... and tn := ... *)
754 | Psig_typext of type_extension
755 (* type t1 += ... *)
756 | Psig_exception of type_exception
757 (* exception C of T *)
758 | Psig_module of module_declaration
759 (* module X = M
760 module X : MT *)
761 | Psig_modsubst of module_substitution
762 (* module X := M *)
763 | Psig_recmodule of module_declaration list
764 (* module rec X1 : MT1 and ... and Xn : MTn *)
765 | Psig_modtype of module_type_declaration
766 (* module type S = MT
767 module type S *)
768 | Psig_open of open_description
769 (* open X *)
770 | Psig_include of include_description
771 (* include MT *)
772 | Psig_class of class_description list
773 (* class c1 : ... and ... and cn : ... *)
774 | Psig_class_type of class_type_declaration list
775 (* class type ct1 = ... and ... and ctn = ... *)
776 | Psig_attribute of attribute
777 (* [@@@id] *)
778 | Psig_extension of extension * attributes
779 (* [%%id] *)
780
781 and module_declaration =
782 {
783 pmd_name: string option loc;
784 pmd_type: module_type;
785 pmd_attributes: attributes; (* ... [@@id1] [@@id2] *)
786 pmd_loc: Location.t;
787 }
788 (* S : MT *)
789
790 and module_substitution =
791 {
792 pms_name: string loc;
793 pms_manifest: Longident.t loc;
794 pms_attributes: attributes; (* ... [@@id1] [@@id2] *)
795 pms_loc: Location.t;
796 }
797
798 and module_type_declaration =
799 {
800 pmtd_name: string loc;
801 pmtd_type: module_type option;
802 pmtd_attributes: attributes; (* ... [@@id1] [@@id2] *)
803 pmtd_loc: Location.t;
804 }
805 (* S = MT
806 S (abstract module type declaration, pmtd_type = None)
807 *)
808
809 and 'a open_infos =
810 {
811 popen_expr: 'a;
812 popen_override: override_flag;
813 popen_loc: Location.t;
814 popen_attributes: attributes;
815 }
816 (* open! X - popen_override = Override (silences the 'used identifier
817 shadowing' warning)
818 open X - popen_override = Fresh
819 *)
820
821 and open_description = Longident.t loc open_infos
822 (* open M.N
823 open M(N).O *)
824
825 and open_declaration = module_expr open_infos
826 (* open M.N
827 open M(N).O
828 open struct ... end *)
829
830 and 'a include_infos =
831 {
832 pincl_mod: 'a;
833 pincl_loc: Location.t;
834 pincl_attributes: attributes;
835 }
836
837 and include_description = module_type include_infos
838 (* include MT *)
839
840 and include_declaration = module_expr include_infos
841 (* include ME *)
842
843 and with_constraint =
844 | Pwith_type of Longident.t loc * type_declaration
845 (* with type X.t = ...
846
847 Note: the last component of the longident must match
848 the name of the type_declaration. *)
849 | Pwith_module of Longident.t loc * Longident.t loc
850 (* with module X.Y = Z *)
851 | Pwith_typesubst of Longident.t loc * type_declaration
852 (* with type X.t := ..., same format as [Pwith_type] *)
853 | Pwith_modsubst of Longident.t loc * Longident.t loc
854 (* with module X.Y := Z *)
855
856 (* Value expressions for the module language *)
857
858 and module_expr =
859 {
860 pmod_desc: module_expr_desc;
861 pmod_loc: Location.t;
862 pmod_attributes: attributes; (* ... [@id1] [@id2] *)
863 }
864
865 and module_expr_desc =
866 | Pmod_ident of Longident.t loc
867 (* X *)
868 | Pmod_structure of structure
869 (* struct ... end *)
870 | Pmod_functor of functor_parameter * module_expr
871 (* functor(X : MT1) -> ME *)
872 | Pmod_apply of module_expr * module_expr
873 (* ME1(ME2) *)
874 | Pmod_constraint of module_expr * module_type
875 (* (ME : MT) *)
876 | Pmod_unpack of expression
877 (* (val E) *)
878 | Pmod_extension of extension
879 (* [%id] *)
880
881 and structure = structure_item list
882
883 and structure_item =
884 {
885 pstr_desc: structure_item_desc;
886 pstr_loc: Location.t;
887 }
888
889 and structure_item_desc =
890 | Pstr_eval of expression * attributes
891 (* E *)
892 | Pstr_value of rec_flag * value_binding list
893 (* let P1 = E1 and ... and Pn = EN (flag = Nonrecursive)
894 let rec P1 = E1 and ... and Pn = EN (flag = Recursive)
895 *)
896 | Pstr_primitive of value_description
897 (* val x: T
898 external x: T = "s1" ... "sn" *)
899 | Pstr_type of rec_flag * type_declaration list
900 (* type t1 = ... and ... and tn = ... *)
901 | Pstr_typext of type_extension
902 (* type t1 += ... *)
903 | Pstr_exception of type_exception
904 (* exception C of T
905 exception C = M.X *)
906 | Pstr_module of module_binding
907 (* module X = ME *)
908 | Pstr_recmodule of module_binding list
909 (* module rec X1 = ME1 and ... and Xn = MEn *)
910 | Pstr_modtype of module_type_declaration
911 (* module type S = MT *)
912 | Pstr_open of open_declaration
913 (* open X *)
914 | Pstr_class of class_declaration list
915 (* class c1 = ... and ... and cn = ... *)
916 | Pstr_class_type of class_type_declaration list
917 (* class type ct1 = ... and ... and ctn = ... *)
918 | Pstr_include of include_declaration
919 (* include ME *)
920 | Pstr_attribute of attribute
921 (* [@@@id] *)
922 | Pstr_extension of extension * attributes
923 (* [%%id] *)
924
925 and value_binding =
926 {
927 pvb_pat: pattern;
928 pvb_expr: expression;
929 pvb_attributes: attributes;
930 pvb_loc: Location.t;
931 }
932
933 and module_binding =
934 {
935 pmb_name: string option loc;
936 pmb_expr: module_expr;
937 pmb_attributes: attributes;
938 pmb_loc: Location.t;
939 }
940 (* X = ME *)
941
942 (** {1 Toplevel} *)
943
944 (* Toplevel phrases *)
945
946 type toplevel_phrase =
947 | Ptop_def of structure
948 | Ptop_dir of toplevel_directive
949 (* #use, #load ... *)
950
951 and toplevel_directive =
952 {
953 pdir_name : string loc;
954 pdir_arg : directive_argument option;
955 pdir_loc : Location.t;
956 }
957
958 and directive_argument =
959 {
960 pdira_desc : directive_argument_desc;
961 pdira_loc : Location.t;
962 }
963
964 and directive_argument_desc =
965 | Pdir_string of string
966 | Pdir_int of string * char option
967 | Pdir_ident of Longident.t
968 | Pdir_bool of bool
969