1 (* ------------------------------------------------------------------------- *)
3 (* ------------------------------------------------------------------------- *)
6 (* $Id: pa_o.ml,v 6.33 2010-11-16 16:48:21 deraugla Exp $ *)
7 (* Copyright (c) INRIA 2007-2010 *)
15 Pcaml.syntax_name.val := "OCaml";
16 Pcaml.no_constructors_arity.val := True;
18 (* ------------------------------------------------------------------------- *)
19 (* The main/reloc.ml file. *)
20 (* ------------------------------------------------------------------------- *)
23 (* $Id: reloc.ml,v 6.19 2011-02-17 10:20:50 deraugla Exp $ *)
24 (* Copyright (c) INRIA 2007-2010 *)
32 [ Some x -> Some (f x)
41 [ Ploc.VaAnt s -> Ploc.VaAnt s
42 | Ploc.VaVal x -> Ploc.VaVal (f x) ]
46 value class_infos_map floc f x =
47 {ciLoc = floc x.ciLoc; ciVir = x.ciVir;
49 let (x1, x2) = x.ciPrm in
51 ciNam = x.ciNam; ciExp = f x.ciExp}
54 value anti_loc qloc sh loc loc1 =
56 ...<:reloc_expr<.....$lid:...xxxxxxxx...$...>>...
57 |..|-----------------------------------| qloc
59 |.........|------------| loc
62 let sh1 = Ploc.first_pos qloc + sh in
63 let sh2 = sh1 + Ploc.first_pos loc in
64 let line_nb_qloc = Ploc.line_nb qloc in
65 let line_nb_loc = Ploc.line_nb loc in
66 let line_nb_loc1 = Ploc.line_nb loc1 in
67 if line_nb_qloc < 0 || line_nb_loc < 0 || line_nb_loc1 < 0 then
69 (sh2 + Ploc.first_pos loc1, sh2 + Ploc.last_pos loc1)
71 Ploc.make_loc (Ploc.file_name loc)
72 (line_nb_qloc + line_nb_loc + line_nb_loc1 - 2)
73 (if line_nb_loc1 = 1 then
74 if line_nb_loc = 1 then Ploc.bol_pos qloc
75 else sh1 + Ploc.bol_pos loc
76 else sh2 + Ploc.bol_pos loc1)
77 (sh2 + Ploc.first_pos loc1, sh2 + Ploc.last_pos loc1) ""
80 value rec reloc_ctyp floc sh =
85 TyAcc loc (self x1) (self x2)
88 TyAli loc (self x1) (self x2)
94 TyApp loc (self x1) (self x2)
97 TyArr loc (self x1) (self x2)
102 let loc = floc loc in
103 TyLab loc x1 (self x2)
105 let loc = floc loc in
107 | TyMan loc x1 x2 x3 →
108 let loc = floc loc in
109 TyMan loc (self x1) x2 (self x3)
111 let loc = floc loc in
112 TyObj loc (vala_map (List.map (fun (x1, x2) → (x1, self x2))) x1) x2
114 let loc = floc loc in
115 TyOlb loc x1 (self x2)
117 let loc = floc loc in
118 TyPck loc (reloc_module_type floc sh x1)
120 let loc = floc loc in
121 TyPol loc x1 (self x2)
123 let loc = floc loc in
124 TyPot loc x1 (self x2)
126 let loc = floc loc in
129 let loc = floc loc in
132 (List.map (fun (loc, x1, x2, x3) → (floc loc, x1, x2, self x3)))
135 let loc = floc loc in
139 (fun (loc, x1, x2, x3) →
140 (floc loc, x1, vala_map (List.map self) x2,
141 option_map self x3)))
144 let loc = floc loc in
145 TyTup loc (vala_map (List.map self) x1)
147 let loc = floc loc in
150 let loc = floc loc in
151 TyVrn loc (vala_map (List.map (reloc_poly_variant floc sh)) x1) x2
153 let loc = floc loc in
154 TyXtr loc x1 (option_map (vala_map self) x2) ]
155 and reloc_poly_variant floc sh =
157 [ PvTag loc x1 x2 x3 →
158 let loc = floc loc in
159 PvTag loc x1 x2 (vala_map (List.map (reloc_ctyp floc sh)) x3)
161 let loc = floc loc in
162 PvInh loc (reloc_ctyp floc sh x1) ]
163 and reloc_patt floc sh =
164 self where rec self =
167 let loc = floc loc in
168 PaAcc loc (self x1) (self x2)
170 let loc = floc loc in
171 PaAli loc (self x1) (self x2)
173 let new_floc loc1 = anti_loc (floc loc) sh loc loc1 in
174 reloc_patt new_floc sh x1
176 let loc = floc loc in
179 let loc = floc loc in
180 PaApp loc (self x1) (self x2)
182 let loc = floc loc in
183 PaArr loc (vala_map (List.map self) x1)
185 let loc = floc loc in
188 let loc = floc loc in
191 let loc = floc loc in
194 let loc = floc loc in
198 (fun (x1, x2) → (self x1, vala_map (option_map self) x2)))
201 let loc = floc loc in
204 let loc = floc loc in
207 let loc = floc loc in
210 let loc = floc loc in
211 PaOlb loc (self x1) (vala_map (option_map (reloc_expr floc sh)) x2)
213 let loc = floc loc in
214 PaOrp loc (self x1) (self x2)
216 let loc = floc loc in
217 PaRec loc (vala_map (List.map (fun (x1, x2) → (self x1, self x2))) x1)
219 let loc = floc loc in
220 PaRng loc (self x1) (self x2)
222 let loc = floc loc in
225 let loc = floc loc in
226 PaTup loc (vala_map (List.map self) x1)
228 let loc = floc loc in
229 PaTyc loc (self x1) (reloc_ctyp floc sh x2)
231 let loc = floc loc in
234 let loc = floc loc in
237 let loc = floc loc in
238 PaUnp loc x1 (option_map (reloc_module_type floc sh) x2)
240 let loc = floc loc in
243 let loc = floc loc in
244 PaXtr loc x1 (option_map (vala_map self) x2) ]
245 and reloc_expr floc sh =
246 self where rec self =
249 let loc = floc loc in
250 ExAcc loc (self x1) (self x2)
252 let new_floc loc1 = anti_loc (floc loc) sh loc loc1 in
253 reloc_expr new_floc sh x1
255 let loc = floc loc in
256 ExApp loc (self x1) (self x2)
258 let loc = floc loc in
259 ExAre loc (self x1) (self x2)
261 let loc = floc loc in
262 ExArr loc (vala_map (List.map self) x1)
264 let loc = floc loc in
267 let loc = floc loc in
268 ExAss loc (self x1) (self x2)
270 let loc = floc loc in
271 ExBae loc (self x1) (vala_map (List.map self) x2)
273 let loc = floc loc in
275 | ExCoe loc x1 x2 x3 →
276 let loc = floc loc in
277 ExCoe loc (self x1) (option_map (reloc_ctyp floc sh) x2) (reloc_ctyp floc sh x3)
279 let loc = floc loc in
281 | ExFor loc x1 x2 x3 x4 x5 →
282 let loc = floc loc in
283 ExFor loc x1 (self x2) (self x3) x4 (vala_map (List.map self) x5)
285 let loc = floc loc in
290 (reloc_patt floc sh x1, vala_map (option_map self) x2, self x3)))
292 | ExIfe loc x1 x2 x3 →
293 let loc = floc loc in
294 ExIfe loc (self x1) (self x2) (self x3)
296 let loc = floc loc in
299 let loc = floc loc in
304 (reloc_patt floc sh x1, vala_map (option_map self) x2)))
307 let loc = floc loc in
309 | ExLet loc x1 x2 x3 →
310 let loc = floc loc in
312 (vala_map (List.map (fun (x1, x2) → (reloc_patt floc sh x1, self x2))) x2)
315 let loc = floc loc in
317 | ExLmd loc x1 x2 x3 →
318 let loc = floc loc in
319 ExLmd loc x1 (reloc_module_expr floc sh x2) (self x3)
321 let loc = floc loc in
326 (reloc_patt floc sh x1, vala_map (option_map self) x2, self x3)))
329 let loc = floc loc in
332 let loc = floc loc in
333 ExObj loc (vala_map (option_map (reloc_patt floc sh)) x1)
334 (vala_map (List.map (reloc_class_str_item floc sh)) x2)
336 let loc = floc loc in
337 ExOlb loc (reloc_patt floc sh x1) (vala_map (option_map self) x2)
339 let loc = floc loc in
340 ExOvr loc (vala_map (List.map (fun (x1, x2) → (x1, self x2))) x1)
342 let loc = floc loc in
343 ExPck loc (reloc_module_expr floc sh x1)
344 (option_map (reloc_module_type floc sh) x2)
346 let loc = floc loc in
348 (vala_map (List.map (fun (x1, x2) → (reloc_patt floc sh x1, self x2))) x1)
351 let loc = floc loc in
352 ExSeq loc (vala_map (List.map self) x1)
354 let loc = floc loc in
355 ExSnd loc (self x1) x2
357 let loc = floc loc in
358 ExSte loc (self x1) (self x2)
360 let loc = floc loc in
363 let loc = floc loc in
368 (reloc_patt floc sh x1, vala_map (option_map self) x2, self x3)))
371 let loc = floc loc in
372 ExTup loc (vala_map (List.map self) x1)
374 let loc = floc loc in
375 ExTyc loc (self x1) (reloc_ctyp floc sh x2)
377 let loc = floc loc in
380 let loc = floc loc in
383 let loc = floc loc in
384 ExWhi loc (self x1) (vala_map (List.map self) x2)
386 let loc = floc loc in
387 ExXtr loc x1 (option_map (vala_map self) x2) ]
388 and reloc_module_type floc sh =
389 self where rec self =
392 let loc = floc loc in
393 MtAcc loc (self x1) (self x2)
395 let loc = floc loc in
396 MtApp loc (self x1) (self x2)
397 | MtFun loc x1 x2 x3 →
398 let loc = floc loc in
399 MtFun loc x1 (self x2) (self x3)
401 let loc = floc loc in
404 let loc = floc loc in
407 let loc = floc loc in
408 MtSig loc (vala_map (List.map (reloc_sig_item floc sh)) x1)
410 let loc = floc loc in
411 MtTyo loc (reloc_module_expr floc sh x1)
413 let loc = floc loc in
416 let loc = floc loc in
417 MtWit loc (self x1) (vala_map (List.map (reloc_with_constr floc sh)) x2)
419 let loc = floc loc in
420 MtXtr loc x1 (option_map (vala_map self) x2) ]
421 and reloc_sig_item floc sh =
422 self where rec self =
425 let loc = floc loc in
427 (vala_map (List.map (class_infos_map floc (reloc_class_type floc sh))) x1)
429 let loc = floc loc in
431 (vala_map (List.map (class_infos_map floc (reloc_class_type floc sh))) x1)
433 let loc = floc loc in
434 SgDcl loc (vala_map (List.map self) x1)
436 let loc = floc loc in
437 SgDir loc x1 (vala_map (option_map (reloc_expr floc sh)) x2)
439 let loc = floc loc in
440 SgExc loc x1 (vala_map (List.map (reloc_ctyp floc sh)) x2)
441 | SgExt loc x1 x2 x3 →
442 let loc = floc loc in
443 SgExt loc x1 (reloc_ctyp floc sh x2) x3
445 let loc = floc loc in
446 SgInc loc (reloc_module_type floc sh x1)
448 let loc = floc loc in
450 (vala_map (List.map (fun (x1, x2) → (x1, reloc_module_type floc sh x2)))
453 let loc = floc loc in
454 SgMty loc x1 (reloc_module_type floc sh x2)
456 let loc = floc loc in
459 let loc = floc loc in
460 SgTyp loc (vala_map (List.map (reloc_type_decl floc sh)) x1)
462 let loc = floc loc in
464 (vala_map (List.map (fun (x1, loc) → (self x1, floc loc))) x2)
466 let loc = floc loc in
467 SgVal loc x1 (reloc_ctyp floc sh x2)
469 let loc = floc loc in
470 SgXtr loc x1 (option_map (vala_map self) x2) ]
471 and reloc_with_constr floc sh =
474 let loc = floc loc in
475 WcMod loc x1 (reloc_module_expr floc sh x2)
477 let loc = floc loc in
478 WcMos loc x1 (reloc_module_expr floc sh x2)
479 | WcTyp loc x1 x2 x3 x4 →
480 let loc = floc loc in
481 WcTyp loc x1 x2 x3 (reloc_ctyp floc sh x4)
482 | WcTys loc x1 x2 x3 →
483 let loc = floc loc in
484 WcTys loc x1 x2 (reloc_ctyp floc sh x3) ]
485 and reloc_module_expr floc sh =
486 self where rec self =
489 let loc = floc loc in
490 MeAcc loc (self x1) (self x2)
492 let loc = floc loc in
493 MeApp loc (self x1) (self x2)
494 | MeFun loc x1 x2 x3 →
495 let loc = floc loc in
496 MeFun loc x1 (reloc_module_type floc sh x2) (self x3)
498 let loc = floc loc in
499 MeStr loc (vala_map (List.map (reloc_str_item floc sh)) x1)
501 let loc = floc loc in
502 MeTyc loc (self x1) (reloc_module_type floc sh x2)
504 let loc = floc loc in
507 let loc = floc loc in
508 MeUnp loc (reloc_expr floc sh x1) (option_map (reloc_module_type floc sh) x2)
510 let loc = floc loc in
511 MeXtr loc x1 (option_map (vala_map self) x2) ]
512 and reloc_str_item floc sh =
513 self where rec self =
516 let loc = floc loc in
518 (vala_map (List.map (class_infos_map floc (reloc_class_expr floc sh))) x1)
520 let loc = floc loc in
522 (vala_map (List.map (class_infos_map floc (reloc_class_type floc sh))) x1)
524 let loc = floc loc in
525 StDcl loc (vala_map (List.map self) x1)
527 let loc = floc loc in
528 StDir loc x1 (vala_map (option_map (reloc_expr floc sh)) x2)
529 | StExc loc x1 x2 x3 →
530 let loc = floc loc in
531 StExc loc x1 (vala_map (List.map (reloc_ctyp floc sh)) x2) x3
533 let loc = floc loc in
534 StExp loc (reloc_expr floc sh x1)
535 | StExt loc x1 x2 x3 →
536 let loc = floc loc in
537 StExt loc x1 (reloc_ctyp floc sh x2) x3
539 let loc = floc loc in
540 StInc loc (reloc_module_expr floc sh x1)
542 let loc = floc loc in
544 (vala_map (List.map (fun (x1, x2) → (x1, reloc_module_expr floc sh x2)))
547 let loc = floc loc in
548 StMty loc x1 (reloc_module_type floc sh x2)
550 let loc = floc loc in
553 let loc = floc loc in
554 StTyp loc (vala_map (List.map (reloc_type_decl floc sh)) x1)
556 let loc = floc loc in
558 (vala_map (List.map (fun (x1, loc) → (self x1, floc loc))) x2)
560 let loc = floc loc in
563 (List.map (fun (x1, x2) → (reloc_patt floc sh x1, reloc_expr floc sh x2)))
566 let loc = floc loc in
567 StXtr loc x1 (option_map (vala_map self) x2) ]
568 and reloc_type_decl floc sh x =
569 {tdNam = vala_map (fun (loc, x1) → (floc loc, x1)) x.tdNam; tdPrm = x.tdPrm;
570 tdPrv = x.tdPrv; tdDef = reloc_ctyp floc sh x.tdDef;
572 vala_map (List.map (fun (x1, x2) → (reloc_ctyp floc sh x1, reloc_ctyp floc sh x2)))
574 and reloc_class_type floc sh =
575 self where rec self =
578 let loc = floc loc in
579 CtAcc loc (self x1) (self x2)
581 let loc = floc loc in
582 CtApp loc (self x1) (self x2)
584 let loc = floc loc in
585 CtCon loc (self x1) (vala_map (List.map (reloc_ctyp floc sh)) x2)
587 let loc = floc loc in
588 CtFun loc (reloc_ctyp floc sh x1) (self x2)
590 let loc = floc loc in
593 let loc = floc loc in
594 CtSig loc (vala_map (option_map (reloc_ctyp floc sh)) x1)
595 (vala_map (List.map (reloc_class_sig_item floc sh)) x2)
597 let loc = floc loc in
598 CtXtr loc x1 (option_map (vala_map self) x2) ]
599 and reloc_class_sig_item floc sh =
600 self where rec self =
603 let loc = floc loc in
604 CgCtr loc (reloc_ctyp floc sh x1) (reloc_ctyp floc sh x2)
606 let loc = floc loc in
607 CgDcl loc (vala_map (List.map self) x1)
609 let loc = floc loc in
610 CgInh loc (reloc_class_type floc sh x1)
611 | CgMth loc x1 x2 x3 →
612 let loc = floc loc in
613 CgMth loc x1 x2 (reloc_ctyp floc sh x3)
614 | CgVal loc x1 x2 x3 →
615 let loc = floc loc in
616 CgVal loc x1 x2 (reloc_ctyp floc sh x3)
617 | CgVir loc x1 x2 x3 →
618 let loc = floc loc in
619 CgVir loc x1 x2 (reloc_ctyp floc sh x3) ]
620 and reloc_class_expr floc sh =
621 self where rec self =
624 let loc = floc loc in
625 CeApp loc (self x1) (reloc_expr floc sh x2)
627 let loc = floc loc in
628 CeCon loc x1 (vala_map (List.map (reloc_ctyp floc sh)) x2)
630 let loc = floc loc in
631 CeFun loc (reloc_patt floc sh x1) (self x2)
632 | CeLet loc x1 x2 x3 →
633 let loc = floc loc in
636 (List.map (fun (x1, x2) → (reloc_patt floc sh x1, reloc_expr floc sh x2)))
640 let loc = floc loc in
641 CeStr loc (vala_map (option_map (reloc_patt floc sh)) x1)
642 (vala_map (List.map (reloc_class_str_item floc sh)) x2)
644 let loc = floc loc in
645 CeTyc loc (self x1) (reloc_class_type floc sh x2)
647 let loc = floc loc in
648 CeXtr loc x1 (option_map (vala_map self) x2) ]
649 and reloc_class_str_item floc sh =
650 self where rec self =
653 let loc = floc loc in
654 CrCtr loc (reloc_ctyp floc sh x1) (reloc_ctyp floc sh x2)
656 let loc = floc loc in
657 CrDcl loc (vala_map (List.map self) x1)
659 let loc = floc loc in
660 CrInh loc (reloc_class_expr floc sh x1) x2
662 let loc = floc loc in
663 CrIni loc (reloc_expr floc sh x1)
664 | CrMth loc x1 x2 x3 x4 x5 →
665 let loc = floc loc in
666 CrMth loc x1 x2 x3 (vala_map (option_map (reloc_ctyp floc sh)) x4)
667 (reloc_expr floc sh x5)
668 | CrVal loc x1 x2 x3 x4 →
669 let loc = floc loc in
670 CrVal loc x1 x2 x3 (reloc_expr floc sh x4)
671 | CrVav loc x1 x2 x3 →
672 let loc = floc loc in
673 CrVav loc x1 x2 (reloc_ctyp floc sh x3)
674 | CrVir loc x1 x2 x3 →
675 let loc = floc loc in
676 CrVir loc x1 x2 (reloc_ctyp floc sh x3) ]
679 (* Equality over syntax trees *)
682 reloc_expr (fun _ -> Ploc.dummy) 0 x =
683 reloc_expr (fun _ -> Ploc.dummy) 0 y
686 reloc_patt (fun _ -> Ploc.dummy) 0 x =
687 reloc_patt (fun _ -> Ploc.dummy) 0 y
690 reloc_ctyp (fun _ -> Ploc.dummy) 0 x =
691 reloc_ctyp (fun _ -> Ploc.dummy) 0 y
693 value eq_str_item x y =
694 reloc_str_item (fun _ -> Ploc.dummy) 0 x =
695 reloc_str_item (fun _ -> Ploc.dummy) 0 y
697 value eq_sig_item x y =
698 reloc_sig_item (fun _ -> Ploc.dummy) 0 x =
699 reloc_sig_item (fun _ -> Ploc.dummy) 0 y
701 value eq_module_expr x y =
702 reloc_module_expr (fun _ -> Ploc.dummy) 0 x =
703 reloc_module_expr (fun _ -> Ploc.dummy) 0 y
705 value eq_module_type x y =
706 reloc_module_type (fun _ -> Ploc.dummy) 0 x =
707 reloc_module_type (fun _ -> Ploc.dummy) 0 y
709 value eq_class_sig_item x y =
710 reloc_class_sig_item (fun _ -> Ploc.dummy) 0 x =
711 reloc_class_sig_item (fun _ -> Ploc.dummy) 0 y
713 value eq_class_str_item x y =
714 reloc_class_str_item (fun _ -> Ploc.dummy) 0 x =
715 reloc_class_str_item (fun _ -> Ploc.dummy) 0 y
717 value eq_reloc_class_type x y =
718 reloc_class_type (fun _ -> Ploc.dummy) 0 x =
719 reloc_class_type (fun _ -> Ploc.dummy) 0 y
721 value eq_class_expr x y =
722 reloc_class_expr (fun _ -> Ploc.dummy) 0 x =
723 reloc_class_expr (fun _ -> Ploc.dummy) 0 y
726 (* ------------------------------------------------------------------------- *)
728 (* ------------------------------------------------------------------------- *)
731 (* $Id: plexer.ml,v 6.11 2010-10-04 20:14:58 deraugla Exp $ *)
732 (* Copyright (c) INRIA 2007-2010 *)
734 #load "pa_lexer.cmo";
736 (* ------------------------------------------------------------------------- *)
737 (* Added by JRH as a backdoor to change lexical conventions. *)
738 (* ------------------------------------------------------------------------- *)
740 value jrh_lexer = ref False;
744 value no_quotations = ref False;
745 value error_on_unknown_keywords = ref False;
747 value dollar_for_antiquotation = ref True;
748 value specific_space_dot = ref False;
750 value force_antiquot_loc = ref False;
753 { after_space : mutable bool;
754 dollar_for_antiquotation : bool;
755 specific_space_dot : bool;
756 find_kwd : string -> string;
757 line_cnt : int -> char -> unit;
758 set_line_nb : unit -> unit;
759 make_lined_loc : (int * int) -> string -> Ploc.t }
762 value err ctx loc msg =
763 Ploc.raise (ctx.make_lined_loc loc "") (Plexing.Error msg)
766 (* ------------------------------------------------------------------------- *)
767 (* JRH's hack to make the case distinction "unmixed" versus "mixed" *)
768 (* ------------------------------------------------------------------------- *)
770 value is_uppercase s = String.uppercase s = s;
771 value is_only_lowercase s = String.lowercase s = s && not(is_uppercase s);
773 value jrh_identifier find_kwd id =
774 let jflag = jrh_lexer.val in
775 if id = "set_jrh_lexer" then
776 (let _ = jrh_lexer.val := True in ("",find_kwd "true"))
777 else if id = "unset_jrh_lexer" then
778 (let _ = jrh_lexer.val := False in ("",find_kwd "false"))
780 try ("", find_kwd id) with
783 if is_uppercase (String.sub id 0 1) then ("UIDENT", id)
785 else if is_uppercase (String.sub id 0 1) &&
786 is_only_lowercase (String.sub id 1 (String.length id - 1))
787 (***** JRH: Carl's alternative version
789 else if is_uppercase (String.sub id 0 1) then ("LIDENT", "__uc_"^id)
790 else ("LIDENT", id)];
792 then ("UIDENT", id) else ("LIDENT", id)];
794 (* ------------------------------------------------------------------------- *)
795 (* Back to original file with the mod of using the above. *)
796 (* ------------------------------------------------------------------------- *)
798 value keyword_or_error ctx loc s =
799 try ("", ctx.find_kwd s) with
801 if error_on_unknown_keywords.val then
802 err ctx loc ("illegal token: " ^ s)
806 value stream_peek_nth n strm =
807 loop n (Stream.npeek n strm) where rec loop n =
810 | [x] -> if n == 1 then Some x else None
811 | [_ :: l] -> loop (n - 1) l ]
814 value utf8_lexing = ref False;
816 value misc_letter buf strm =
817 if utf8_lexing.val then
818 match strm with lexer [ '\128'-'\225' | '\227'-'\255' ]
820 match strm with lexer [ '\128'-'\255' ]
823 value misc_punct buf strm =
824 if utf8_lexing.val then
825 match strm with lexer [ '\226' _ _ ]
827 match strm with parser []
832 [ [ 'A'-'Z' | 'a'-'z' | '0'-'9' | '_' | ''' | misc_letter ] ident! | ]
837 [ [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
838 '%' | '.' | ':' | '<' | '>' | '|' | '$' | misc_punct ]
845 [ [ '0'-'9' | 'A'-'Z' | 'a'-'z' | '_' | '!' | '%' | '&' | '*' | '+' | '-' |
846 '.' | '/' | ':' | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' | ''' |
847 '$' | '\128'-'\255' ] ident3!
851 value binary = lexer [ '0' | '1' ];
852 value octal = lexer [ '0'-'7' ];
853 value decimal = lexer [ '0'-'9' ];
854 value hexa = lexer [ '0'-'9' | 'a'-'f' | 'A'-'F' ];
858 [ "l"/ -> ("INT_l", $buf)
859 | "L"/ -> ("INT_L", $buf)
860 | "n"/ -> ("INT_n", $buf)
864 value rec digits_under kind =
866 [ kind (digits_under kind)!
867 | "_" (digits_under kind)!
873 [ kind (digits_under kind)!
874 | -> raise (Stream.Error "ill-formed integer constant") ]
877 value rec decimal_digits_under =
878 lexer [ [ '0'-'9' | '_' ] decimal_digits_under! | ]
881 value exponent_part =
883 [ [ 'e' | 'E' ] [ '+' | '-' | ]
884 '0'-'9' ? "ill-formed floating-point constant"
885 decimal_digits_under! ]
890 [ decimal_digits_under "." decimal_digits_under! exponent_part ->
892 | decimal_digits_under "." decimal_digits_under! -> ("FLOAT", $buf)
893 | decimal_digits_under exponent_part -> ("FLOAT", $buf)
894 | decimal_digits_under end_integer! ]
897 value char_after_bslash =
900 | _ [ "'"/ | _ [ "'"/ | ] ] ]
905 [ "\\" _ char_after_bslash!
906 | "\\" -> err ctx (bp, $pos) "char not terminated"
907 | ?= [ _ '''] _! "'"/ ]
911 parser bp [: `c :] -> do { ctx.line_cnt bp c; $add c }
914 value rec string ctx bp =
917 | "\\" (any ctx) (string ctx bp)!
918 | (any ctx) (string ctx bp)!
919 | -> err ctx (bp, $pos) "string not terminated" ]
922 value rec qstring ctx bp =
925 | (any ctx) (qstring ctx bp)!
926 | -> err ctx (bp, $pos) "quotation not terminated" ]
929 value comment ctx bp =
930 comment where rec comment =
934 | "(*" comment! comment!
936 | "\"" (string ctx bp)! [ -> $add "\"" ] comment!
939 | "'" (any ctx) comment!
941 | -> err ctx (bp, $pos) "comment not terminated" ]
944 value rec quotation ctx bp =
947 | ">" (quotation ctx bp)!
948 | "<<" (quotation ctx bp)! [ -> $add ">>" ]! (quotation ctx bp)!
949 | "<:" ident! "<" (quotation ctx bp)! [ -> $add ">>" ]! (quotation ctx bp)!
950 | "<:" ident! (quotation ctx bp)!
951 | "<" (quotation ctx bp)!
952 | "\\"/ [ '>' | '<' | '\\' ] (quotation ctx bp)!
953 | "\\" (quotation ctx bp)!
954 | (any ctx) (quotation ctx bp)!
955 | -> err ctx (bp, $pos) "quotation not terminated" ]
958 value less_expected = "character '<' expected";
960 value less ctx bp buf strm =
961 if no_quotations.val then
962 match strm with lexer
963 [ [ -> $add "<" ] ident2! -> keyword_or_error ctx (bp, $pos) $buf ]
965 match strm with lexer
966 [ "<"/ (quotation ctx bp) -> ("QUOTATION", ":" ^ $buf)
967 | ":"/ ident! "<"/ ? less_expected [ -> $add ":" ]! (quotation ctx bp) ->
969 | ":"/ ident! ":<"/ ? less_expected [ -> $add "@" ]! (quotation ctx bp) ->
971 | [ -> $add "<" ] ident2! -> keyword_or_error ctx (bp, $pos) $buf ]
974 value rec antiquot_rest ctx bp =
977 | "\\"/ (any ctx) (antiquot_rest ctx bp)!
978 | (any ctx) (antiquot_rest ctx bp)!
979 | -> err ctx (bp, $pos) "antiquotation not terminated" ]
982 value rec antiquot ctx bp =
985 | [ 'a'-'z' | 'A'-'Z' | '0'-'9' | '!' | '_' ] (antiquot ctx bp)!
986 | ":" (antiquot_rest ctx bp)! -> $buf
987 | "\\"/ (any ctx) (antiquot_rest ctx bp)! -> ":" ^ $buf
988 | (any ctx) (antiquot_rest ctx bp)! -> ":" ^ $buf
989 | -> err ctx (bp, $pos) "antiquotation not terminated" ]
992 value antiloc bp ep s = Printf.sprintf "%d,%d:%s" bp ep s;
994 value rec antiquot_loc ctx bp =
996 [ "$"/ -> antiloc bp $pos (":" ^ $buf)
997 | [ 'a'-'z' | 'A'-'Z' | '0'-'9' | '!' | '_' ] (antiquot_loc ctx bp)!
998 | ":" (antiquot_rest ctx bp)! -> antiloc bp $pos $buf
999 | "\\"/ (any ctx) (antiquot_rest ctx bp)! -> antiloc bp $pos (":" ^ $buf)
1000 | (any ctx) (antiquot_rest ctx bp)! -> antiloc bp $pos (":" ^ $buf)
1001 | -> err ctx (bp, $pos) "antiquotation not terminated" ]
1004 value dollar ctx bp buf strm =
1005 if not no_quotations.val && ctx.dollar_for_antiquotation then
1006 ("ANTIQUOT", antiquot ctx bp buf strm)
1007 else if force_antiquot_loc.val then
1008 ("ANTIQUOT_LOC", antiquot_loc ctx bp buf strm)
1010 match strm with lexer
1011 [ [ -> $add "$" ] ident2! -> ("", $buf) ]
1014 (* ANTIQUOT - specific case for QUESTIONIDENT and QUESTIONIDENTCOLON
1017 ?$abc:d$ ?abc:d ?abc
1018 ?$abc:d$: ?abc:d: ?abc:
1023 (* ANTIQUOT_LOC - specific case for QUESTIONIDENT and QUESTIONIDENTCOLON
1026 ?$abc:d$ ?8,13:abc:d ?abc
1027 ?$abc:d$: ?8,13:abc:d: ?abc:
1032 value question ctx bp buf strm =
1033 if ctx.dollar_for_antiquotation then
1034 match strm with parser
1035 [ [: `'$'; s = antiquot ctx bp $empty; `':' :] ->
1036 ("ANTIQUOT", "?" ^ s ^ ":")
1037 | [: `'$'; s = antiquot ctx bp $empty :] ->
1038 ("ANTIQUOT", "?" ^ s)
1040 match strm with lexer
1041 [ ident2! -> keyword_or_error ctx (bp, $pos) $buf ] ]
1042 else if force_antiquot_loc.val then
1043 match strm with parser
1044 [ [: `'$'; s = antiquot_loc ctx bp $empty; `':' :] ->
1045 ("ANTIQUOT_LOC", "?" ^ s ^ ":")
1046 | [: `'$'; s = antiquot_loc ctx bp $empty :] ->
1047 ("ANTIQUOT_LOC", "?" ^ s)
1049 match strm with lexer
1050 [ ident2! -> keyword_or_error ctx (bp, $pos) $buf ] ]
1052 match strm with lexer
1053 [ ident2! -> keyword_or_error ctx (bp, $pos) $buf ]
1056 value tilde ctx bp buf strm =
1057 if ctx.dollar_for_antiquotation then
1058 match strm with parser
1059 [ [: `'$'; s = antiquot ctx bp $empty; `':' :] ->
1060 ("ANTIQUOT", "~" ^ s ^ ":")
1061 | [: `'$'; s = antiquot ctx bp $empty :] ->
1062 ("ANTIQUOT", "~" ^ s)
1064 match strm with lexer
1065 [ ident2! -> keyword_or_error ctx (bp, $pos) $buf ] ]
1066 else if force_antiquot_loc.val then
1067 match strm with parser
1068 [ [: `'$'; s = antiquot_loc ctx bp $empty; `':' :] ->
1069 ("ANTIQUOT_LOC", "~" ^ s ^ ":")
1070 | [: `'$'; s = antiquot_loc ctx bp $empty :] ->
1071 ("ANTIQUOT_LOC", "~" ^ s)
1073 match strm with lexer
1074 [ ident2! -> keyword_or_error ctx (bp, $pos) $buf ] ]
1076 match strm with lexer
1077 [ ident2! -> keyword_or_error ctx (bp, $pos) $buf ]
1082 [ ":"/ -> ("TILDEIDENTCOLON", $buf)
1083 | -> ("TILDEIDENT", $buf) ]
1086 value questionident =
1088 [ ":"/ -> ("QUESTIONIDENTCOLON", $buf)
1089 | -> ("QUESTIONIDENT", $buf) ]
1092 value rec linedir n s =
1093 match stream_peek_nth n s with
1094 [ Some (' ' | '\t') -> linedir (n + 1) s
1095 | Some ('0'..'9') -> linedir_digits (n + 1) s
1097 and linedir_digits n s =
1098 match stream_peek_nth n s with
1099 [ Some ('0'..'9') -> linedir_digits (n + 1) s
1100 | _ -> linedir_quote n s ]
1101 and linedir_quote n s =
1102 match stream_peek_nth n s with
1103 [ Some (' ' | '\t') -> linedir_quote (n + 1) s
1108 value rec any_to_nl =
1115 value next_token_after_spaces ctx bp =
1119 jrh_identifier ctx.find_kwd id
1120 (********** JRH: original was
1121 try ("", ctx.find_kwd id) with [ Not_found -> ("UIDENT", id) ]
1123 | [ 'a'-'z' | '_' | misc_letter ] ident! ->
1125 jrh_identifier ctx.find_kwd id
1126 (********** JRH: original was
1127 try ("", ctx.find_kwd id) with [ Not_found -> ("LIDENT", id) ]
1130 | "0" [ 'o' | 'O' ] (digits octal)!
1131 | "0" [ 'x' | 'X' ] (digits hexa)!
1132 | "0" [ 'b' | 'B' ] (digits binary)!
1134 | "'"/ ?= [ '\\' 'a'-'z' 'a'-'z' ] -> keyword_or_error ctx (bp, $pos) "'"
1135 | "'"/ (char ctx bp) -> ("CHAR", $buf)
1136 | "'" -> keyword_or_error ctx (bp, $pos) "'"
1137 | "\""/ (string ctx bp)! -> ("STRING", $buf)
1138 (*** Line added by JRH ***)
1139 | "`"/ (qstring ctx bp)! -> ("QUOTATION", "tot:" ^ $buf)
1140 | "$"/ (dollar ctx bp)!
1141 | [ '!' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' ] ident2! ->
1142 keyword_or_error ctx (bp, $pos) $buf
1143 | "~"/ 'a'-'z' ident! tildeident!
1144 | "~"/ '_' ident! tildeident!
1145 | "~" (tilde ctx bp)
1146 | "?"/ 'a'-'z' ident! questionident!
1147 | "?" (question ctx bp)!
1148 | "<"/ (less ctx bp)!
1149 | ":]" -> keyword_or_error ctx (bp, $pos) $buf
1150 | "::" -> keyword_or_error ctx (bp, $pos) $buf
1151 | ":=" -> keyword_or_error ctx (bp, $pos) $buf
1152 | ":>" -> keyword_or_error ctx (bp, $pos) $buf
1153 | ":" -> keyword_or_error ctx (bp, $pos) $buf
1154 | ">]" -> keyword_or_error ctx (bp, $pos) $buf
1155 | ">}" -> keyword_or_error ctx (bp, $pos) $buf
1156 | ">" ident2! -> keyword_or_error ctx (bp, $pos) $buf
1157 | "|]" -> keyword_or_error ctx (bp, $pos) $buf
1158 | "|}" -> keyword_or_error ctx (bp, $pos) $buf
1159 | "|" ident2! -> keyword_or_error ctx (bp, $pos) $buf
1160 | "[" ?= [ "<<" | "<:" ] -> keyword_or_error ctx (bp, $pos) $buf
1161 | "[|" -> keyword_or_error ctx (bp, $pos) $buf
1162 | "[<" -> keyword_or_error ctx (bp, $pos) $buf
1163 | "[:" -> keyword_or_error ctx (bp, $pos) $buf
1164 | "[" -> keyword_or_error ctx (bp, $pos) $buf
1165 | "{" ?= [ "<<" | "<:" ] -> keyword_or_error ctx (bp, $pos) $buf
1166 | "{|" -> keyword_or_error ctx (bp, $pos) $buf
1167 | "{<" -> keyword_or_error ctx (bp, $pos) $buf
1168 | "{:" -> keyword_or_error ctx (bp, $pos) $buf
1169 | "{" -> keyword_or_error ctx (bp, $pos) $buf
1170 | ".." -> keyword_or_error ctx (bp, $pos) ".."
1173 if ctx.specific_space_dot && ctx.after_space then " ." else "."
1175 keyword_or_error ctx (bp, $pos) id
1176 | ";;" -> keyword_or_error ctx (bp, $pos) ";;"
1177 | ";" -> keyword_or_error ctx (bp, $pos) ";"
1178 | misc_punct ident2! -> keyword_or_error ctx (bp, $pos) $buf
1179 | "\\"/ ident3! -> ("LIDENT", $buf)
1180 | (any ctx) -> keyword_or_error ctx (bp, $pos) $buf ]
1183 value get_comment buf strm = $buf;
1185 value rec next_token ctx buf =
1187 [ [: `('\n' | '\r' as c); s :] ep -> do {
1188 if c = '\n' then incr Plexing.line_nb.val else ();
1189 Plexing.bol_pos.val.val := ep;
1191 ctx.after_space := True;
1192 next_token ctx ($add c) s
1194 | [: `(' ' | '\t' | '\026' | '\012' as c); s :] -> do {
1195 ctx.after_space := True;
1196 next_token ctx ($add c) s
1198 | [: `'#' when bp = Plexing.bol_pos.val.val; s :] ->
1199 let comm = get_comment buf () in
1200 if linedir 1 s then do {
1201 let buf = any_to_nl ($add '#') s in
1202 incr Plexing.line_nb.val;
1203 Plexing.bol_pos.val.val := Stream.count s;
1205 ctx.after_space := True;
1206 next_token ctx buf s
1209 let loc = ctx.make_lined_loc (bp, bp + 1) comm in
1210 (keyword_or_error ctx (bp, bp + 1) "#", loc)
1214 [ [: `'*'; buf = comment ctx bp ($add "(*") !; s :] -> do {
1216 ctx.after_space := True;
1217 next_token ctx buf s
1220 let loc = ctx.make_lined_loc (bp, ep) $buf in
1221 (keyword_or_error ctx (bp, ep) "(", loc) ] ! :] -> a
1222 | [: comm = get_comment buf;
1223 tok = next_token_after_spaces ctx bp $empty :] ep ->
1224 let loc = ctx.make_lined_loc (bp, max (bp + 1) ep) comm in
1226 | [: comm = get_comment buf; _ = Stream.empty :] ->
1227 let loc = ctx.make_lined_loc (bp, bp + 1) comm in
1228 (("EOI", ""), loc) ]
1231 value next_token_fun ctx glexr (cstrm, s_line_nb, s_bol_pos) =
1233 match Plexing.restore_lexing_info.val with
1234 [ Some (line_nb, bol_pos) -> do {
1235 s_line_nb.val := line_nb;
1236 s_bol_pos.val := bol_pos;
1237 Plexing.restore_lexing_info.val := None;
1240 Plexing.line_nb.val := s_line_nb;
1241 Plexing.bol_pos.val := s_bol_pos;
1242 let comm_bp = Stream.count cstrm in
1244 ctx.after_space := False;
1245 let (r, loc) = next_token ctx $empty cstrm in
1246 match glexr.val.Plexing.tok_comm with
1248 if Ploc.first_pos loc > comm_bp then
1249 let comm_loc = Ploc.make_unlined (comm_bp, Ploc.last_pos loc) in
1250 glexr.val.Plexing.tok_comm := Some [comm_loc :: list]
1256 [ Stream.Error str ->
1257 err ctx (Stream.count cstrm, Stream.count cstrm + 1) str ]
1260 value func kwd_table glexr =
1262 let line_nb = ref 0 in
1263 let bol_pos = ref 0 in
1264 {after_space = False;
1265 dollar_for_antiquotation = dollar_for_antiquotation.val;
1266 specific_space_dot = specific_space_dot.val;
1267 find_kwd = Hashtbl.find kwd_table;
1270 [ '\n' | '\r' -> do {
1271 if c = '\n' then incr Plexing.line_nb.val else ();
1272 Plexing.bol_pos.val.val := bp1 + 1;
1275 set_line_nb () = do {
1276 line_nb.val := Plexing.line_nb.val.val;
1277 bol_pos.val := Plexing.bol_pos.val.val;
1279 make_lined_loc loc comm =
1280 Ploc.make_loc Plexing.input_file.val line_nb.val bol_pos.val loc comm}
1282 Plexing.lexer_func_of_parser (next_token_fun ctx glexr)
1285 value rec check_keyword_stream =
1286 parser [: _ = check $empty; _ = Stream.empty :] -> True
1289 [ [ 'A'-'Z' | 'a'-'z' | misc_letter ] check_ident!
1290 | [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' |
1294 | "<" ?= [ ":" | "<" ]
1307 | "[" ?= [ "<<" | "<:" ]
1312 | "{" ?= [ "<<" | "<:" ]
1319 | misc_punct check_ident2!
1323 [ [ 'A'-'Z' | 'a'-'z' | '0'-'9' | '_' | ''' | misc_letter ]
1327 [ [ '!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' |
1328 '.' | ':' | '<' | '>' | '|' | misc_punct ]
1332 value check_keyword s =
1333 try check_keyword_stream (Stream.of_string s) with _ -> False
1336 value error_no_respect_rules p_con p_prm =
1340 (if p_con = "" then "\"" ^ p_prm ^ "\""
1341 else if p_prm = "" then p_con
1342 else p_con ^ " \"" ^ p_prm ^ "\"") ^
1343 " does not respect Plexer rules"))
1346 value error_ident_and_keyword p_con p_prm =
1349 ("the token \"" ^ p_prm ^ "\" is used as " ^ p_con ^
1353 value using_token kwd_table ident_table (p_con, p_prm) =
1356 if not (hashtbl_mem kwd_table p_prm) then
1357 if check_keyword p_prm then
1358 if hashtbl_mem ident_table p_prm then
1359 error_ident_and_keyword (Hashtbl.find ident_table p_prm) p_prm
1360 else Hashtbl.add kwd_table p_prm p_prm
1361 else error_no_respect_rules p_con p_prm
1364 if p_prm = "" then ()
1366 match p_prm.[0] with
1367 [ 'A'..'Z' -> error_no_respect_rules p_con p_prm
1369 if hashtbl_mem kwd_table p_prm then
1370 error_ident_and_keyword p_con p_prm
1371 else Hashtbl.add ident_table p_prm p_con ]
1373 if p_prm = "" then ()
1375 match p_prm.[0] with
1376 [ 'a'..'z' -> error_no_respect_rules p_con p_prm
1378 if hashtbl_mem kwd_table p_prm then
1379 error_ident_and_keyword p_con p_prm
1380 else Hashtbl.add ident_table p_prm p_con ]
1381 | "TILDEIDENT" | "TILDEIDENTCOLON" | "QUESTIONIDENT" |
1382 "QUESTIONIDENTCOLON" | "INT" | "INT_l" | "INT_L" | "INT_n" | "FLOAT" |
1383 "CHAR" | "STRING" | "QUOTATION" |
1384 "ANTIQUOT" | "ANTIQUOT_LOC" | "EOI" ->
1389 ("the constructor \"" ^ p_con ^
1390 "\" is not recognized by Plexer")) ]
1393 value removing_token kwd_table ident_table (p_con, p_prm) =
1395 [ "" -> Hashtbl.remove kwd_table p_prm
1396 | "LIDENT" | "UIDENT" ->
1397 if p_prm <> "" then Hashtbl.remove ident_table p_prm else ()
1403 [ ("", t) -> "'" ^ t ^ "'"
1404 | ("LIDENT", "") -> "lowercase identifier"
1405 | ("LIDENT", t) -> "'" ^ t ^ "'"
1406 | ("UIDENT", "") -> "uppercase identifier"
1407 | ("UIDENT", t) -> "'" ^ t ^ "'"
1408 | ("INT", "") -> "integer"
1409 | ("INT", s) -> "'" ^ s ^ "'"
1410 | ("FLOAT", "") -> "float"
1411 | ("STRING", "") -> "string"
1412 | ("CHAR", "") -> "char"
1413 | ("QUOTATION", "") -> "quotation"
1414 | ("ANTIQUOT", k) -> "antiquot \"" ^ k ^ "\""
1415 | ("EOI", "") -> "end of input"
1417 | (con, prm) -> con ^ " \"" ^ prm ^ "\"" ]
1420 value eq_before_colon p e =
1421 loop 0 where rec loop i =
1422 if i == String.length e then
1423 failwith "Internal error in Plexer: incorrect ANTIQUOT"
1424 else if i == String.length p then e.[i] == ':'
1425 else if p.[i] == e.[i] then loop (i + 1)
1429 value after_colon e =
1431 let i = String.index e ':' in
1432 String.sub e (i + 1) (String.length e - i - 1)
1437 value after_colon_except_last e =
1439 let i = String.index e ':' in
1440 String.sub e (i + 1) (String.length e - i - 2)
1447 [ ("ANTIQUOT", p_prm) ->
1448 if p_prm <> "" && (p_prm.[0] = '~' || p_prm.[0] = '?') then
1449 if p_prm.[String.length p_prm - 1] = ':' then
1450 let p_prm = String.sub p_prm 0 (String.length p_prm - 1) in
1452 [ ("ANTIQUOT", prm) ->
1453 if prm <> "" && prm.[String.length prm - 1] = ':' then
1454 if eq_before_colon p_prm prm then after_colon_except_last prm
1455 else raise Stream.Failure
1456 else raise Stream.Failure
1457 | _ -> raise Stream.Failure ]
1460 [ ("ANTIQUOT", prm) ->
1461 if prm <> "" && prm.[String.length prm - 1] = ':' then
1462 raise Stream.Failure
1463 else if eq_before_colon p_prm prm then after_colon prm
1464 else raise Stream.Failure
1465 | _ -> raise Stream.Failure ]
1468 [ ("ANTIQUOT", prm) when eq_before_colon p_prm prm -> after_colon prm
1469 | _ -> raise Stream.Failure ]
1470 | tok -> Plexing.default_match tok ]
1474 let kwd_table = Hashtbl.create 301 in
1475 let id_table = Hashtbl.create 301 in
1478 {Plexing.tok_func = fun []; tok_using = fun []; tok_removing = fun [];
1479 tok_match = fun []; tok_text = fun []; tok_comm = None}
1482 {Plexing.tok_func = func kwd_table glexr;
1483 tok_using = using_token kwd_table id_table;
1484 tok_removing = removing_token kwd_table id_table; tok_match = tok_match;
1485 tok_text = text; tok_comm = None}
1487 do { glexr.val := glex; glex }
1490 (* ------------------------------------------------------------------------- *)
1491 (* Back to etc/pa_o.ml *)
1492 (* ------------------------------------------------------------------------- *)
1495 let odfa = dollar_for_antiquotation.val in
1496 dollar_for_antiquotation.val := False;
1497 Grammar.Unsafe.gram_reinit gram (gmake ());
1498 dollar_for_antiquotation.val := odfa;
1499 Grammar.Unsafe.clear_entry interf;
1500 Grammar.Unsafe.clear_entry implem;
1501 Grammar.Unsafe.clear_entry top_phrase;
1502 Grammar.Unsafe.clear_entry use_file;
1503 Grammar.Unsafe.clear_entry module_type;
1504 Grammar.Unsafe.clear_entry module_expr;
1505 Grammar.Unsafe.clear_entry sig_item;
1506 Grammar.Unsafe.clear_entry str_item;
1507 Grammar.Unsafe.clear_entry signature;
1508 Grammar.Unsafe.clear_entry structure;
1509 Grammar.Unsafe.clear_entry expr;
1510 Grammar.Unsafe.clear_entry patt;
1511 Grammar.Unsafe.clear_entry ctyp;
1512 Grammar.Unsafe.clear_entry let_binding;
1513 Grammar.Unsafe.clear_entry type_decl;
1514 Grammar.Unsafe.clear_entry constructor_declaration;
1515 Grammar.Unsafe.clear_entry label_declaration;
1516 Grammar.Unsafe.clear_entry match_case;
1517 Grammar.Unsafe.clear_entry with_constr;
1518 Grammar.Unsafe.clear_entry poly_variant;
1519 Grammar.Unsafe.clear_entry class_type;
1520 Grammar.Unsafe.clear_entry class_expr;
1521 Grammar.Unsafe.clear_entry class_sig_item;
1522 Grammar.Unsafe.clear_entry class_str_item
1525 Pcaml.parse_interf.val := Grammar.Entry.parse interf;
1526 Pcaml.parse_implem.val := Grammar.Entry.parse implem;
1528 value mklistexp loc last =
1529 loop True where rec loop top =
1534 | None -> <:expr< [] >> ]
1537 if top then loc else Ploc.encl (MLast.loc_of_expr e1) loc
1539 <:expr< [$e1$ :: $loop False el$] >> ]
1542 value mklistpat loc last =
1543 loop True where rec loop top =
1548 | None -> <:patt< [] >> ]
1551 if top then loc else Ploc.encl (MLast.loc_of_patt p1) loc
1553 <:patt< [$p1$ :: $loop False pl$] >> ]
1556 (*** JRH pulled this outside so user can add new infixes here too ***)
1558 value ht = Hashtbl.create 73;
1560 (*** And JRH added all the new HOL Light infixes here already ***)
1562 value is_operator = do {
1563 let ct = Hashtbl.create 73 in
1564 List.iter (fun x -> Hashtbl.add ht x True)
1565 ["asr"; "land"; "lor"; "lsl"; "lsr"; "lxor"; "mod"; "or"; "o"; "upto";
1566 "F_F"; "THENC"; "THEN"; "THENL"; "ORELSE"; "ORELSEC";
1567 "THEN_TCL"; "ORELSE_TCL"];
1568 List.iter (fun x -> Hashtbl.add ct x True)
1569 ['!'; '&'; '*'; '+'; '-'; '/'; ':'; '<'; '='; '>'; '@'; '^'; '|'; '~';
1570 '?'; '%'; '.'; '$'];
1572 try Hashtbl.find ht x with
1573 [ Not_found -> try Hashtbl.find ct x.[0] with _ -> False ]
1576 (*** JRH added this so parenthesised operators undergo same mapping ***)
1578 value translate_operator =
1582 | "THENC" -> "thenc_"
1583 | "THENL" -> "thenl_"
1584 | "ORELSE" -> "orelse_"
1585 | "ORELSEC" -> "orelsec_"
1586 | "THEN_TCL" -> "then_tcl_"
1587 | "ORELSE_TCL" -> "orelse_tcl_"
1591 value operator_rparen =
1592 Grammar.Entry.of_parser gram "operator_rparen"
1594 match Stream.npeek 2 strm with
1595 [ [("", s); ("", ")")] when is_operator s -> do {
1598 translate_operator s
1600 | _ -> raise Stream.Failure ])
1603 value check_not_part_of_patt =
1604 Grammar.Entry.of_parser gram "check_not_part_of_patt"
1607 match Stream.npeek 4 strm with
1608 [ [("LIDENT", _); tok :: _] -> tok
1609 | [("", "("); ("", s); ("", ")"); tok] when is_operator s -> tok
1610 | _ -> raise Stream.Failure ]
1613 [ ("", "," | "as" | "|" | "::") -> raise Stream.Failure
1619 ['!'; '$'; '%'; '&'; '*'; '+'; '-'; '.'; '/'; ':'; '<'; '='; '>'; '?';
1622 loop where rec loop s i =
1623 if i == String.length s then True
1624 else if List.mem s.[i] list then loop s (i + 1)
1629 let list = ['!'; '?'; '~'] in
1630 let excl = ["!="; "??"; "?!"] in
1631 Grammar.Entry.of_parser gram "prefixop"
1635 not (List.mem x excl) && String.length x >= 2 &&
1636 List.mem x.[0] list && symbolchar x 1 :] ->
1641 let list = ['='; '<'; '>'; '|'; '&'; '$'] in
1642 let excl = ["<-"; "||"; "&&"] in
1643 Grammar.Entry.of_parser gram "infixop0"
1647 not (List.mem x excl) && (x = "$" || String.length x >= 2) &&
1648 List.mem x.[0] list && symbolchar x 1 :] ->
1653 let list = ['@'; '^'] in
1654 Grammar.Entry.of_parser gram "infixop1"
1658 String.length x >= 2 && List.mem x.[0] list &&
1659 symbolchar x 1 :] ->
1664 let list = ['+'; '-'] in
1665 Grammar.Entry.of_parser gram "infixop2"
1669 x <> "->" && String.length x >= 2 && List.mem x.[0] list &&
1670 symbolchar x 1 :] ->
1675 let list = ['*'; '/'; '%'] in
1676 Grammar.Entry.of_parser gram "infixop3"
1680 String.length x >= 2 && List.mem x.[0] list &&
1681 symbolchar x 1 :] ->
1686 Grammar.Entry.of_parser gram "infixop4"
1690 String.length x >= 3 && x.[0] == '*' && x.[1] == '*' &&
1691 symbolchar x 2 :] ->
1695 value test_constr_decl =
1696 Grammar.Entry.of_parser gram "test_constr_decl"
1698 match Stream.npeek 1 strm with
1699 [ [("UIDENT", _)] ->
1700 match Stream.npeek 2 strm with
1701 [ [_; ("", ".")] -> raise Stream.Failure
1702 | [_; ("", "(")] -> raise Stream.Failure
1704 | _ -> raise Stream.Failure ]
1706 | _ -> raise Stream.Failure ])
1709 value stream_peek_nth n strm =
1710 loop n (Stream.npeek n strm) where rec loop n =
1713 | [x] -> if n == 1 then Some x else None
1714 | [_ :: l] -> loop (n - 1) l ]
1717 (* horrible hack to be able to parse class_types *)
1719 value test_ctyp_minusgreater =
1720 Grammar.Entry.of_parser gram "test_ctyp_minusgreater"
1722 let rec skip_simple_ctyp n =
1723 match stream_peek_nth n strm with
1724 [ Some ("", "->") -> n
1725 | Some ("", "[" | "[<") ->
1726 skip_simple_ctyp (ignore_upto "]" (n + 1) + 1)
1727 | Some ("", "(") -> skip_simple_ctyp (ignore_upto ")" (n + 1) + 1)
1730 "as" | "'" | ":" | "*" | "." | "#" | "<" | ">" | ".." | ";" |
1732 skip_simple_ctyp (n + 1)
1733 | Some ("QUESTIONIDENT" | "LIDENT" | "UIDENT", _) ->
1734 skip_simple_ctyp (n + 1)
1735 | Some _ | None -> raise Stream.Failure ]
1736 and ignore_upto end_kwd n =
1737 match stream_peek_nth n strm with
1738 [ Some ("", prm) when prm = end_kwd -> n
1739 | Some ("", "[" | "[<") ->
1740 ignore_upto end_kwd (ignore_upto "]" (n + 1) + 1)
1741 | Some ("", "(") -> ignore_upto end_kwd (ignore_upto ")" (n + 1) + 1)
1742 | Some _ -> ignore_upto end_kwd (n + 1)
1743 | None -> raise Stream.Failure ]
1745 match Stream.peek strm with
1746 [ Some (("", "[") | ("LIDENT" | "UIDENT", _)) -> skip_simple_ctyp 1
1747 | Some ("", "object") -> raise Stream.Failure
1751 value test_label_eq =
1752 Grammar.Entry.of_parser gram "test_label_eq"
1753 (test 1 where rec test lev strm =
1754 match stream_peek_nth lev strm with
1755 [ Some (("UIDENT", _) | ("LIDENT", _) | ("", ".")) ->
1757 | Some ("ANTIQUOT_LOC", _) -> ()
1758 | Some ("", "=") -> ()
1759 | _ -> raise Stream.Failure ])
1762 value test_typevar_list_dot =
1763 Grammar.Entry.of_parser gram "test_typevar_list_dot"
1764 (let rec test lev strm =
1765 match stream_peek_nth lev strm with
1766 [ Some ("", "'") -> test2 (lev + 1) strm
1767 | Some ("", ".") -> ()
1768 | _ -> raise Stream.Failure ]
1769 and test2 lev strm =
1770 match stream_peek_nth lev strm with
1771 [ Some ("UIDENT" | "LIDENT", _) -> test (lev + 1) strm
1772 | _ -> raise Stream.Failure ]
1778 Grammar.Entry.of_parser gram "e_phony"
1782 Grammar.Entry.of_parser gram "p_phony"
1786 value constr_arity = ref [("Some", 1); ("Match_Failure", 1)];
1788 value rec is_expr_constr_call =
1790 [ <:expr< $uid:_$ >> -> True
1791 | <:expr< $uid:_$.$e$ >> -> is_expr_constr_call e
1792 | <:expr< $e$ $_$ >> -> is_expr_constr_call e
1796 value rec constr_expr_arity loc =
1798 [ <:expr< $uid:c$ >> ->
1799 try List.assoc c constr_arity.val with [ Not_found -> 0 ]
1800 | <:expr< $uid:_$.$e$ >> -> constr_expr_arity loc e
1804 value rec constr_patt_arity loc =
1806 [ <:patt< $uid:c$ >> ->
1807 try List.assoc c constr_arity.val with [ Not_found -> 0 ]
1808 | <:patt< $uid:_$.$p$ >> -> constr_patt_arity loc p
1814 [ <:expr< do { $list:el$ } >> -> el
1818 value mem_tvar s tpl =
1819 List.exists (fun (t, _) -> Pcaml.unvala t = Some s) tpl
1822 value choose_tvar tpl =
1823 let rec find_alpha v =
1824 let s = String.make 1 v in
1825 if mem_tvar s tpl then
1826 if v = 'z' then None else find_alpha (Char.chr (Char.code v + 1))
1827 else Some (String.make 1 v)
1830 let v = "a" ^ string_of_int n in
1831 if mem_tvar v tpl then make_n (succ n) else v
1833 match find_alpha 'a' with
1835 | None -> make_n 1 ]
1838 value quotation_content s = do {
1839 loop 0 where rec loop i =
1840 if i = String.length s then ("", s)
1841 else if s.[i] = ':' || s.[i] = '@' then
1843 (String.sub s 0 i, String.sub s i (String.length s - i))
1847 value concat_comm loc e =
1849 Ploc.with_comment loc
1850 (Ploc.comment loc ^ Ploc.comment (MLast.loc_of_expr e))
1853 let first = ref True in
1855 if first.val then do {first.val := False; loc}
1862 GLOBAL: sig_item str_item ctyp patt expr module_type module_expr
1863 signature structure class_type class_expr class_sig_item class_str_item
1864 let_binding type_decl constructor_declaration label_declaration
1865 match_case with_constr poly_variant;
1867 [ [ "functor"; "("; i = V UIDENT "uid" ""; ":"; t = module_type; ")";
1869 <:module_expr< functor ( $_uid:i$ : $t$ ) -> $me$ >>
1870 | "struct"; st = structure; "end" ->
1871 <:module_expr< struct $_list:st$ end >> ]
1872 | [ me1 = SELF; "."; me2 = SELF -> <:module_expr< $me1$ . $me2$ >> ]
1873 | [ me1 = SELF; "("; me2 = SELF; ")" -> <:module_expr< $me1$ $me2$ >> ]
1874 | [ i = mod_expr_ident -> i
1875 | "("; "val"; e = expr; ":"; mt = module_type; ")" ->
1876 <:module_expr< (value $e$ : $mt$) >>
1877 | "("; "val"; e = expr; ")" ->
1878 <:module_expr< (value $e$) >>
1879 | "("; me = SELF; ":"; mt = module_type; ")" ->
1880 <:module_expr< ( $me$ : $mt$ ) >>
1881 | "("; me = SELF; ")" -> <:module_expr< $me$ >> ] ]
1884 [ [ st = V (LIST0 [ s = str_item; OPT ";;" -> s ]) -> st ] ]
1888 [ i = SELF; "."; j = SELF -> <:module_expr< $i$ . $j$ >> ]
1889 | [ i = V UIDENT -> <:module_expr< $_uid:i$ >> ] ]
1893 [ "exception"; (_, c, tl, _) = constructor_declaration;
1895 <:str_item< exception $_uid:c$ of $_list:tl$ = $_list:b$ >>
1896 | "external"; i = V LIDENT "lid" ""; ":"; t = ctyp; "=";
1897 pd = V (LIST1 STRING) ->
1898 <:str_item< external $_lid:i$ : $t$ = $_list:pd$ >>
1899 | "external"; "("; i = operator_rparen; ":"; t = ctyp; "=";
1900 pd = V (LIST1 STRING) ->
1901 <:str_item< external $lid:i$ : $t$ = $_list:pd$ >>
1902 | "include"; me = module_expr -> <:str_item< include $me$ >>
1903 | "module"; r = V (FLAG "rec"); l = V (LIST1 mod_binding SEP "and") ->
1904 <:str_item< module $_flag:r$ $_list:l$ >>
1905 | "module"; "type"; i = V UIDENT "uid" ""; "="; mt = module_type ->
1906 <:str_item< module type $_uid:i$ = $mt$ >>
1907 | "open"; i = V mod_ident "list" "" ->
1908 <:str_item< open $_:i$ >>
1909 | "type"; tdl = V (LIST1 type_decl SEP "and") ->
1910 <:str_item< type $_list:tdl$ >>
1911 | "let"; r = V (FLAG "rec"); l = V (LIST1 let_binding SEP "and"); "in";
1913 let e = <:expr< let $_flag:r$ $_list:l$ in $x$ >> in
1914 <:str_item< $exp:e$ >>
1915 | "let"; r = V (FLAG "rec"); l = V (LIST1 let_binding SEP "and") ->
1917 [ <:vala< [(p, e)] >> ->
1919 [ <:patt< _ >> -> <:str_item< $exp:e$ >>
1920 | _ -> <:str_item< value $_flag:r$ $_list:l$ >> ]
1921 | _ -> <:str_item< value $_flag:r$ $_list:l$ >> ]
1922 | "let"; "module"; m = V UIDENT; mb = mod_fun_binding; "in"; e = expr ->
1923 <:str_item< let module $_uid:m$ = $mb$ in $e$ >>
1924 | e = expr -> <:str_item< $exp:e$ >> ] ]
1927 [ [ "="; sl = V mod_ident "list" -> sl
1928 | -> <:vala< [] >> ] ]
1931 [ [ i = V UIDENT; me = mod_fun_binding -> (i, me) ] ]
1935 [ "("; m = UIDENT; ":"; mt = module_type; ")"; mb = SELF ->
1936 <:module_expr< functor ( $uid:m$ : $mt$ ) -> $mb$ >>
1937 | ":"; mt = module_type; "="; me = module_expr ->
1938 <:module_expr< ( $me$ : $mt$ ) >>
1939 | "="; me = module_expr -> <:module_expr< $me$ >> ] ]
1943 [ [ "functor"; "("; i = V UIDENT "uid" ""; ":"; t = SELF; ")"; "->";
1945 <:module_type< functor ( $_uid:i$ : $t$ ) -> $mt$ >> ]
1946 | [ mt = SELF; "with"; wcl = V (LIST1 with_constr SEP "and") ->
1947 <:module_type< $mt$ with $_list:wcl$ >> ]
1948 | [ "sig"; sg = signature; "end" ->
1949 <:module_type< sig $_list:sg$ end >>
1950 | "module"; "type"; "of"; me = module_expr ->
1951 <:module_type< module type of $me$ >>
1952 | i = mod_type_ident -> i
1953 | "("; mt = SELF; ")" -> <:module_type< $mt$ >> ] ]
1956 [ [ sg = V (LIST0 [ s = sig_item; OPT ";;" -> s ]) -> sg ] ]
1960 [ m1 = SELF; "."; m2 = SELF -> <:module_type< $m1$ . $m2$ >>
1961 | m1 = SELF; "("; m2 = SELF; ")" -> <:module_type< $m1$ $m2$ >> ]
1962 | [ m = V UIDENT -> <:module_type< $_uid:m$ >>
1963 | m = V LIDENT -> <:module_type< $_lid:m$ >> ] ]
1967 [ "exception"; (_, c, tl, _) = constructor_declaration ->
1968 <:sig_item< exception $_uid:c$ of $_list:tl$ >>
1969 | "external"; i = V LIDENT "lid" ""; ":"; t = ctyp; "=";
1970 pd = V (LIST1 STRING) ->
1971 <:sig_item< external $_lid:i$ : $t$ = $_list:pd$ >>
1972 | "external"; "("; i = operator_rparen; ":"; t = ctyp; "=";
1973 pd = V (LIST1 STRING) ->
1974 <:sig_item< external $lid:i$ : $t$ = $_list:pd$ >>
1975 | "include"; mt = module_type ->
1976 <:sig_item< include $mt$ >>
1977 | "module"; rf = V (FLAG "rec");
1978 l = V (LIST1 mod_decl_binding SEP "and") ->
1979 <:sig_item< module $_flag:rf$ $_list:l$ >>
1980 | "module"; "type"; i = V UIDENT "uid" ""; "="; mt = module_type ->
1981 <:sig_item< module type $_uid:i$ = $mt$ >>
1982 | "module"; "type"; i = V UIDENT "uid" "" ->
1983 <:sig_item< module type $_uid:i$ = 'abstract >>
1984 | "open"; i = V mod_ident "list" "" ->
1985 <:sig_item< open $_:i$ >>
1986 | "type"; tdl = V (LIST1 type_decl SEP "and") ->
1987 <:sig_item< type $_list:tdl$ >>
1988 | "val"; i = V LIDENT "lid" ""; ":"; t = ctyp ->
1989 <:sig_item< value $_lid:i$ : $t$ >>
1990 | "val"; "("; i = operator_rparen; ":"; t = ctyp ->
1991 <:sig_item< value $lid:i$ : $t$ >> ] ]
1994 [ [ i = V UIDENT; mt = module_declaration -> (i, mt) ] ]
1998 [ ":"; mt = module_type -> <:module_type< $mt$ >>
1999 | "("; i = UIDENT; ":"; t = module_type; ")"; mt = SELF ->
2000 <:module_type< functor ( $uid:i$ : $t$ ) -> $mt$ >> ] ]
2002 (* "with" constraints (additional type equations over signature
2005 [ [ "type"; tpl = V type_parameters "list"; i = V mod_ident ""; "=";
2006 pf = V (FLAG "private"); t = ctyp ->
2007 <:with_constr< type $_:i$ $_list:tpl$ = $_flag:pf$ $t$ >>
2008 | "type"; tpl = V type_parameters "list"; i = V mod_ident ""; ":=";
2010 <:with_constr< type $_:i$ $_list:tpl$ := $t$ >>
2011 | "module"; i = V mod_ident ""; "="; me = module_expr ->
2012 <:with_constr< module $_:i$ = $me$ >>
2013 | "module"; i = V mod_ident ""; ":="; me = module_expr ->
2014 <:with_constr< module $_:i$ := $me$ >> ] ]
2016 (* Core expressions *)
2019 [ e1 = SELF; ";"; e2 = SELF ->
2020 <:expr< do { $list:[e1 :: get_seq e2]$ } >>
2021 | e1 = SELF; ";" -> e1
2022 | el = V e_phony "list" -> <:expr< do { $_list:el$ } >> ]
2024 [ "let"; o = V (FLAG "rec"); l = V (LIST1 let_binding SEP "and"); "in";
2025 x = expr LEVEL "top" ->
2026 <:expr< let $_flag:o$ $_list:l$ in $x$ >>
2027 | "let"; "module"; m = V UIDENT; mb = mod_fun_binding; "in";
2028 e = expr LEVEL "top" ->
2029 <:expr< let module $_uid:m$ = $mb$ in $e$ >>
2030 | "function"; OPT "|"; l = V (LIST1 match_case SEP "|") ->
2031 <:expr< fun [ $_list:l$ ] >>
2032 | "fun"; p = patt LEVEL "simple"; (eo, e) = fun_def ->
2033 <:expr< fun [$p$ $opt:eo$ -> $e$] >>
2034 | "match"; e = SELF; "with"; OPT "|";
2035 l = V (LIST1 match_case SEP "|") ->
2036 <:expr< match $e$ with [ $_list:l$ ] >>
2037 | "try"; e = SELF; "with"; OPT "|"; l = V (LIST1 match_case SEP "|") ->
2038 <:expr< try $e$ with [ $_list:l$ ] >>
2039 | "if"; e1 = SELF; "then"; e2 = expr LEVEL "expr1"; "else";
2040 e3 = expr LEVEL "expr1" ->
2041 <:expr< if $e1$ then $e2$ else $e3$ >>
2042 | "if"; e1 = SELF; "then"; e2 = expr LEVEL "expr1" ->
2043 <:expr< if $e1$ then $e2$ else () >>
2044 | "for"; i = V LIDENT; "="; e1 = SELF; df = V direction_flag "to";
2045 e2 = SELF; "do"; e = V SELF "list"; "done" ->
2046 let el = Pcaml.vala_map get_seq e in
2047 <:expr< for $_lid:i$ = $e1$ $_to:df$ $e2$ do { $_list:el$ } >>
2048 | "while"; e1 = SELF; "do"; e2 = V SELF "list"; "done" ->
2049 let el = Pcaml.vala_map get_seq e2 in
2050 <:expr< while $e1$ do { $_list:el$ } >> ]
2051 | [ e = SELF; ","; el = LIST1 NEXT SEP "," ->
2052 <:expr< ( $list:[e :: el]$ ) >> ]
2054 [ e1 = SELF; ":="; e2 = expr LEVEL "expr1" ->
2055 <:expr< $e1$.val := $e2$ >>
2056 | e1 = SELF; "<-"; e2 = expr LEVEL "expr1" -> <:expr< $e1$ := $e2$ >> ]
2058 [ e1 = SELF; "or"; e2 = SELF -> <:expr< $lid:"or"$ $e1$ $e2$ >>
2059 | e1 = SELF; "||"; e2 = SELF -> <:expr< $e1$ || $e2$ >> ]
2061 [ e1 = SELF; "&"; e2 = SELF -> <:expr< $lid:"&"$ $e1$ $e2$ >>
2062 | e1 = SELF; "&&"; e2 = SELF -> <:expr< $e1$ && $e2$ >> ]
2064 [ e1 = SELF; "<"; e2 = SELF -> <:expr< $e1$ < $e2$ >>
2065 | e1 = SELF; ">"; e2 = SELF -> <:expr< $e1$ > $e2$ >>
2066 | e1 = SELF; "<="; e2 = SELF -> <:expr< $e1$ <= $e2$ >>
2067 | e1 = SELF; ">="; e2 = SELF -> <:expr< $e1$ >= $e2$ >>
2068 | e1 = SELF; "="; e2 = SELF -> <:expr< $e1$ = $e2$ >>
2069 | e1 = SELF; "<>"; e2 = SELF -> <:expr< $e1$ <> $e2$ >>
2070 | e1 = SELF; "=="; e2 = SELF -> <:expr< $e1$ == $e2$ >>
2071 | e1 = SELF; "!="; e2 = SELF -> <:expr< $e1$ != $e2$ >>
2072 | e1 = SELF; op = infixop0; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
2074 [ e1 = SELF; "^"; e2 = SELF -> <:expr< $e1$ ^ $e2$ >>
2075 | e1 = SELF; "@"; e2 = SELF -> <:expr< $e1$ @ $e2$ >>
2076 | e1 = SELF; op = infixop1; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
2078 [ e1 = SELF; "::"; e2 = SELF -> <:expr< [$e1$ :: $e2$] >> ]
2080 [ e1 = SELF; "+"; e2 = SELF -> <:expr< $e1$ + $e2$ >>
2081 | e1 = SELF; "-"; e2 = SELF -> <:expr< $e1$ - $e2$ >>
2082 | e1 = SELF; op = infixop2; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
2084 [ e1 = SELF; "*"; e2 = SELF -> <:expr< $e1$ * $e2$ >>
2085 | e1 = SELF; "/"; e2 = SELF -> <:expr< $e1$ / $e2$ >>
2086 | e1 = SELF; "%"; e2 = SELF -> <:expr< $lid:"%"$ $e1$ $e2$ >>
2087 | e1 = SELF; "land"; e2 = SELF -> <:expr< $e1$ land $e2$ >>
2088 | e1 = SELF; "lor"; e2 = SELF -> <:expr< $e1$ lor $e2$ >>
2089 | e1 = SELF; "lxor"; e2 = SELF -> <:expr< $e1$ lxor $e2$ >>
2090 | e1 = SELF; "mod"; e2 = SELF -> <:expr< $e1$ mod $e2$ >>
2091 | e1 = SELF; op = infixop3; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
2093 [ e1 = SELF; "**"; e2 = SELF -> <:expr< $e1$ ** $e2$ >>
2094 | e1 = SELF; "asr"; e2 = SELF -> <:expr< $e1$ asr $e2$ >>
2095 | e1 = SELF; "lsl"; e2 = SELF -> <:expr< $e1$ lsl $e2$ >>
2096 | e1 = SELF; "lsr"; e2 = SELF -> <:expr< $e1$ lsr $e2$ >>
2097 | e1 = SELF; op = infixop4; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
2098 | "unary minus" NONA
2099 [ "-"; e = SELF -> <:expr< - $e$ >>
2100 | "-."; e = SELF -> <:expr< -. $e$ >> ]
2102 [ e1 = SELF; e2 = SELF ->
2104 if is_expr_constr_call e1 then
2106 [ <:expr< $e11$ $e12$ >> -> (e11, <:expr< $e12$ $e2$ >>)
2110 match constr_expr_arity loc e1 with
2111 [ 1 -> <:expr< $e1$ $e2$ >>
2114 [ <:expr< ( $list:el$ ) >> ->
2115 List.fold_left (fun e1 e2 -> <:expr< $e1$ $e2$ >>) e1 el
2116 | _ -> <:expr< $e1$ $e2$ >> ] ]
2117 | "assert"; e = SELF -> <:expr< assert $e$ >>
2118 | "lazy"; e = SELF -> <:expr< lazy ($e$) >> ]
2120 [ e1 = SELF; "."; "("; op = operator_rparen ->
2121 <:expr< $e1$ .( $lid:op$ ) >>
2122 | e1 = SELF; "."; "("; e2 = SELF; ")" ->
2123 <:expr< $e1$ .( $e2$ ) >>
2124 | e1 = SELF; "."; "["; e2 = SELF; "]" -> <:expr< $e1$ .[ $e2$ ] >>
2125 | e = SELF; "."; "{"; el = V (LIST1 expr LEVEL "+" SEP ","); "}" ->
2126 <:expr< $e$ .{ $_list:el$ } >>
2127 | e1 = SELF; "."; e2 = SELF ->
2130 [ <:expr< $x$ . $y$ >> -> loop <:expr< $m$ . $x$ >> y
2131 | e -> <:expr< $m$ . $e$ >> ]
2135 [ "!"; e = SELF -> <:expr< $e$ . val >>
2136 | "~-"; e = SELF -> <:expr< ~- $e$ >>
2137 | "~-."; e = SELF -> <:expr< ~-. $e$ >>
2138 | f = prefixop; e = SELF -> <:expr< $lid:f$ $e$ >> ]
2140 [ s = V INT -> <:expr< $_int:s$ >>
2141 | s = V INT_l -> <:expr< $_int32:s$ >>
2142 | s = V INT_L -> <:expr< $_int64:s$ >>
2143 | s = V INT_n -> <:expr< $_nativeint:s$ >>
2144 | s = V FLOAT -> <:expr< $_flo:s$ >>
2145 | s = V STRING -> <:expr< $_str:s$ >>
2146 | c = V CHAR -> <:expr< $_chr:c$ >>
2147 | UIDENT "True" -> <:expr< True_ >>
2148 | UIDENT "False" -> <:expr< False_ >>
2149 | i = expr_ident -> i
2150 | "false" -> <:expr< False >>
2151 | "true" -> <:expr< True >>
2152 | "["; "]" -> <:expr< [] >>
2153 | "["; el = expr1_semi_list; "]" -> <:expr< $mklistexp loc None el$ >>
2154 | "[|"; "|]" -> <:expr< [| |] >>
2155 | "[|"; el = V expr1_semi_list "list"; "|]" ->
2156 <:expr< [| $_list:el$ |] >>
2157 | "{"; test_label_eq; lel = V lbl_expr_list "list"; "}" ->
2158 <:expr< { $_list:lel$ } >>
2159 | "{"; e = expr LEVEL "."; "with"; lel = V lbl_expr_list "list"; "}" ->
2160 <:expr< { ($e$) with $_list:lel$ } >>
2161 | "("; ")" -> <:expr< () >>
2162 | "("; "module"; me = module_expr; ":"; mt = module_type; ")" ->
2163 <:expr< (module $me$ : $mt$) >>
2164 | "("; "module"; me = module_expr; ")" ->
2165 <:expr< (module $me$) >>
2166 | "("; op = operator_rparen -> <:expr< $lid:op$ >>
2167 | "("; el = V e_phony "list"; ")" -> <:expr< ($_list:el$) >>
2168 | "("; e = SELF; ":"; t = ctyp; ")" -> <:expr< ($e$ : $t$) >>
2169 | "("; e = SELF; ")" -> concat_comm loc <:expr< $e$ >>
2170 | "begin"; e = SELF; "end" -> concat_comm loc <:expr< $e$ >>
2171 | "begin"; "end" -> <:expr< () >>
2173 let con = quotation_content x in
2174 Pcaml.handle_expr_quotation loc con ] ]
2177 [ [ p = val_ident; e = fun_binding -> (p, e)
2178 | p = patt; "="; e = expr -> (p, e)
2179 | p = patt; ":"; t = poly_type; "="; e = expr ->
2180 (<:patt< ($p$ : $t$) >>, e) ] ]
2182 (*** JRH added the "translate_operator" here ***)
2184 [ [ check_not_part_of_patt; s = LIDENT -> <:patt< $lid:s$ >>
2185 | check_not_part_of_patt; "("; s = ANY; ")" ->
2186 let s' = translate_operator s in <:patt< $lid:s'$ >> ] ]
2190 [ p = patt LEVEL "simple"; e = SELF -> <:expr< fun $p$ -> $e$ >>
2191 | "="; e = expr -> <:expr< $e$ >>
2192 | ":"; t = poly_type; "="; e = expr -> <:expr< ($e$ : $t$) >> ] ]
2195 [ [ x1 = patt; w = V (OPT [ "when"; e = expr -> e ]); "->"; x2 = expr ->
2199 [ [ le = lbl_expr; ";"; lel = SELF -> [le :: lel]
2200 | le = lbl_expr; ";" -> [le]
2201 | le = lbl_expr -> [le] ] ]
2204 [ [ i = patt_label_ident; "="; e = expr LEVEL "expr1" -> (i, e) ] ]
2207 [ [ el = LIST1 (expr LEVEL "expr1") SEP ";" OPT_SEP -> el ] ]
2211 [ p = patt LEVEL "simple"; (eo, e) = SELF ->
2212 (None, <:expr< fun [ $p$ $opt:eo$ -> $e$ ] >>)
2213 | eo = OPT [ "when"; e = expr -> e ]; "->"; e = expr ->
2214 (eo, <:expr< $e$ >>) ] ]
2218 [ i = V LIDENT -> <:expr< $_lid:i$ >>
2219 | i = V UIDENT -> <:expr< $_uid:i$ >>
2220 | i = V UIDENT; "."; j = SELF ->
2223 [ <:expr< $x$ . $y$ >> -> loop <:expr< $m$ . $x$ >> y
2224 | e -> <:expr< $m$ . $e$ >> ]
2226 loop <:expr< $_uid:i$ >> j
2227 | i = V UIDENT; "."; "("; j = operator_rparen ->
2228 <:expr< $_uid:i$ . $lid:j$ >> ] ]
2233 [ p1 = SELF; "as"; i = LIDENT -> <:patt< ($p1$ as $lid:i$) >> ]
2235 [ p1 = SELF; "|"; p2 = SELF -> <:patt< $p1$ | $p2$ >> ]
2236 | [ p = SELF; ","; pl = LIST1 NEXT SEP "," ->
2237 <:patt< ( $list:[p :: pl]$) >> ]
2239 [ p1 = SELF; ".."; p2 = SELF -> <:patt< $p1$ .. $p2$ >> ]
2241 [ p1 = SELF; "::"; p2 = SELF -> <:patt< [$p1$ :: $p2$] >> ]
2243 [ p1 = SELF; p2 = SELF ->
2246 [ <:patt< $p11$ $p12$ >> -> (p11, <:patt< $p12$ $p2$ >>)
2249 match constr_patt_arity loc p1 with
2250 [ 1 -> <:patt< $p1$ $p2$ >>
2254 [ <:patt< _ >> when n > 1 ->
2256 loop n where rec loop n =
2257 if n = 0 then [] else [<:patt< _ >> :: loop (n - 1)]
2259 <:patt< ( $list:pl$ ) >>
2263 [ <:patt< ( $list:pl$ ) >> ->
2264 List.fold_left (fun p1 p2 -> <:patt< $p1$ $p2$ >>) p1 pl
2265 | _ -> <:patt< $p1$ $p2$ >> ] ]
2266 | "lazy"; p = SELF -> <:patt< lazy $p$ >> ]
2268 [ p1 = SELF; "."; p2 = SELF -> <:patt< $p1$ . $p2$ >> ]
2270 [ s = V LIDENT -> <:patt< $_lid:s$ >>
2271 | s = V UIDENT -> <:patt< $_uid:s$ >>
2272 | s = V INT -> <:patt< $_int:s$ >>
2273 | s = V INT_l -> <:patt< $_int32:s$ >>
2274 | s = V INT_L -> <:patt< $_int64:s$ >>
2275 | s = V INT_n -> <:patt< $_nativeint:s$ >>
2276 | "-"; s = INT -> <:patt< $int:"-" ^ s$ >>
2277 | "-"; s = FLOAT -> <:patt< $flo:"-" ^ s$ >>
2278 | s = V FLOAT -> <:patt< $_flo:s$ >>
2279 | s = V STRING -> <:patt< $_str:s$ >>
2280 | s = V CHAR -> <:patt< $_chr:s$ >>
2281 | UIDENT "True" -> <:patt< True_ >>
2282 | UIDENT "False" -> <:patt< False_ >>
2283 | "false" -> <:patt< False >>
2284 | "true" -> <:patt< True >>
2285 | "["; "]" -> <:patt< [] >>
2286 | "["; pl = patt_semi_list; "]" -> <:patt< $mklistpat loc None pl$ >>
2287 | "[|"; "|]" -> <:patt< [| |] >>
2288 | "[|"; pl = V patt_semi_list "list"; "|]" ->
2289 <:patt< [| $_list:pl$ |] >>
2290 | "{"; lpl = V lbl_patt_list "list"; "}" ->
2291 <:patt< { $_list:lpl$ } >>
2292 | "("; ")" -> <:patt< () >>
2293 | "("; op = operator_rparen -> <:patt< $lid:op$ >>
2294 | "("; pl = V p_phony "list"; ")" -> <:patt< ($_list:pl$) >>
2295 | "("; p = SELF; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >>
2296 | "("; p = SELF; ")" -> <:patt< $p$ >>
2297 | "("; "type"; s = V LIDENT; ")" -> <:patt< (type $_lid:s$) >>
2298 | "("; "module"; s = V UIDENT; ":"; mt = module_type; ")" ->
2299 <:patt< (module $_uid:s$ : $mt$) >>
2300 | "("; "module"; s = V UIDENT; ")" ->
2301 <:patt< (module $_uid:s$) >>
2302 | "_" -> <:patt< _ >>
2304 let con = quotation_content x in
2305 Pcaml.handle_patt_quotation loc con ] ]
2308 [ [ p = patt; ";"; pl = SELF -> [p :: pl]
2309 | p = patt; ";" -> [p]
2310 | p = patt -> [p] ] ]
2313 [ [ le = lbl_patt; ";"; lel = SELF -> [le :: lel]
2314 | le = lbl_patt; ";" -> [le]
2315 | le = lbl_patt -> [le] ] ]
2318 [ [ i = patt_label_ident; "="; p = patt -> (i, p) ] ]
2322 [ p1 = SELF; "."; p2 = SELF -> <:patt< $p1$ . $p2$ >> ]
2324 [ i = UIDENT -> <:patt< $uid:i$ >>
2325 | i = LIDENT -> <:patt< $lid:i$ >> ] ]
2327 (* Type declaration *)
2329 [ [ tpl = type_parameters; n = V type_patt; "="; pf = V (FLAG "private");
2330 tk = type_kind; cl = V (LIST0 constrain) ->
2331 <:type_decl< $_tp:n$ $list:tpl$ = $_priv:pf$ $tk$ $_list:cl$ >>
2332 | tpl = type_parameters; n = V type_patt; cl = V (LIST0 constrain) ->
2333 let tk = <:ctyp< '$choose_tvar tpl$ >> in
2334 <:type_decl< $_tp:n$ $list:tpl$ = $tk$ $_list:cl$ >> ] ]
2337 [ [ n = V LIDENT -> (loc, n) ] ]
2340 [ [ "constraint"; t1 = ctyp; "="; t2 = ctyp -> (t1, t2) ] ]
2343 [ [ test_constr_decl; OPT "|";
2344 cdl = LIST1 constructor_declaration SEP "|" ->
2345 <:ctyp< [ $list:cdl$ ] >>
2348 | t = ctyp; "="; pf = FLAG "private"; "{";
2349 ldl = V label_declarations "list"; "}" ->
2350 <:ctyp< $t$ == $priv:pf$ { $_list:ldl$ } >>
2351 | t = ctyp; "="; pf = FLAG "private"; OPT "|";
2352 cdl = LIST1 constructor_declaration SEP "|" ->
2353 <:ctyp< $t$ == $priv:pf$ [ $list:cdl$ ] >>
2354 | "{"; ldl = V label_declarations "list"; "}" ->
2355 <:ctyp< { $_list:ldl$ } >> ] ]
2358 [ [ -> (* empty *) []
2359 | tp = type_parameter -> [tp]
2360 | "("; tpl = LIST1 type_parameter SEP ","; ")" -> tpl ] ]
2363 [ [ "+"; p = V simple_type_parameter -> (p, Some True)
2364 | "-"; p = V simple_type_parameter -> (p, Some False)
2365 | p = V simple_type_parameter -> (p, None) ] ]
2367 simple_type_parameter:
2368 [ [ "'"; i = ident -> Some i
2371 constructor_declaration:
2372 [ [ ci = cons_ident; "of"; cal = V (LIST1 (ctyp LEVEL "apply") SEP "*") ->
2373 (loc, ci, cal, None)
2374 | ci = cons_ident; ":"; cal = V (LIST1 (ctyp LEVEL "apply") SEP "*");
2376 (loc, ci, cal, Some t)
2377 | ci = cons_ident; ":"; cal = V (LIST1 (ctyp LEVEL "apply") SEP "*") ->
2380 [ <:vala< [t] >> -> t
2381 | <:vala< [t :: tl] >> -> <:ctyp< ($list:[t :: tl]$) >>
2382 | _ -> assert False ]
2384 (loc, ci, <:vala< [] >>, Some t)
2385 | ci = cons_ident -> (loc, ci, <:vala< [] >>, None) ] ]
2388 [ [ i = V UIDENT "uid" "" -> i
2389 | UIDENT "True" -> <:vala< "True_" >>
2390 | UIDENT "False" -> <:vala< "False_" >> ] ]
2393 [ [ ld = label_declaration; ";"; ldl = SELF -> [ld :: ldl]
2394 | ld = label_declaration; ";" -> [ld]
2395 | ld = label_declaration -> [ld] ] ]
2398 [ [ i = LIDENT; ":"; t = poly_type -> (loc, i, False, t)
2399 | "mutable"; i = LIDENT; ":"; t = poly_type -> (loc, i, True, t) ] ]
2403 [ [ t1 = SELF; "as"; "'"; i = ident -> <:ctyp< $t1$ as '$i$ >> ]
2405 [ t1 = SELF; "->"; t2 = SELF -> <:ctyp< $t1$ -> $t2$ >> ]
2407 [ t = SELF; "*"; tl = LIST1 (ctyp LEVEL "apply") SEP "*" ->
2408 <:ctyp< ( $list:[t :: tl]$ ) >> ]
2410 [ t1 = SELF; t2 = SELF -> <:ctyp< $t2$ $t1$ >> ]
2412 [ t1 = SELF; "."; t2 = SELF -> <:ctyp< $t1$ . $t2$ >>
2413 | t1 = SELF; "("; t2 = SELF; ")" -> <:ctyp< $t1$ $t2$ >> ]
2415 [ "'"; i = V ident "" -> <:ctyp< '$_:i$ >>
2416 | "_" -> <:ctyp< _ >>
2417 | i = V LIDENT -> <:ctyp< $_lid:i$ >>
2418 | i = V UIDENT -> <:ctyp< $_uid:i$ >>
2419 | "("; "module"; mt = module_type; ")" -> <:ctyp< module $mt$ >>
2420 | "("; t = SELF; ","; tl = LIST1 ctyp SEP ","; ")";
2421 i = ctyp LEVEL "ctyp2" ->
2422 List.fold_left (fun c a -> <:ctyp< $c$ $a$ >>) i [t :: tl]
2423 | "("; t = SELF; ")" -> <:ctyp< $t$ >> ] ]
2428 | i = UIDENT -> i ] ]
2434 | i = UIDENT; "."; j = SELF -> [i :: j] ] ]
2439 | "downto" -> False ] ]
2441 (* Objects and Classes *)
2443 [ [ "class"; cd = V (LIST1 class_declaration SEP "and") ->
2444 <:str_item< class $_list:cd$ >>
2445 | "class"; "type"; ctd = V (LIST1 class_type_declaration SEP "and") ->
2446 <:str_item< class type $_list:ctd$ >> ] ]
2449 [ [ "class"; cd = V (LIST1 class_description SEP "and") ->
2450 <:sig_item< class $_list:cd$ >>
2451 | "class"; "type"; ctd = V (LIST1 class_type_declaration SEP "and") ->
2452 <:sig_item< class type $_list:ctd$ >> ] ]
2454 (* Class expressions *)
2456 [ [ vf = V (FLAG "virtual"); ctp = class_type_parameters; i = V LIDENT;
2457 cfb = class_fun_binding ->
2458 {MLast.ciLoc = loc; MLast.ciVir = vf; MLast.ciPrm = ctp;
2459 MLast.ciNam = i; MLast.ciExp = cfb} ] ]
2462 [ [ "="; ce = class_expr -> ce
2463 | ":"; ct = class_type; "="; ce = class_expr ->
2464 <:class_expr< ($ce$ : $ct$) >>
2465 | p = patt LEVEL "simple"; cfb = SELF ->
2466 <:class_expr< fun $p$ -> $cfb$ >> ] ]
2468 class_type_parameters:
2469 [ [ -> (loc, <:vala< [] >>)
2470 | "["; tpl = V (LIST1 type_parameter SEP ","); "]" -> (loc, tpl) ] ]
2473 [ [ p = patt LEVEL "simple"; "->"; ce = class_expr ->
2474 <:class_expr< fun $p$ -> $ce$ >>
2475 | p = patt LEVEL "simple"; cfd = SELF ->
2476 <:class_expr< fun $p$ -> $cfd$ >> ] ]
2480 [ "fun"; cfd = class_fun_def -> cfd
2481 | "let"; rf = V (FLAG "rec"); lb = V (LIST1 let_binding SEP "and");
2483 <:class_expr< let $_flag:rf$ $_list:lb$ in $ce$ >> ]
2485 [ ce = SELF; e = expr LEVEL "label" ->
2486 <:class_expr< $ce$ $e$ >> ]
2488 [ "["; ct = ctyp; ","; ctcl = LIST1 ctyp SEP ","; "]";
2489 ci = class_longident ->
2490 <:class_expr< [ $list:[ct :: ctcl]$ ] $list:ci$ >>
2491 | "["; ct = ctyp; "]"; ci = class_longident ->
2492 <:class_expr< [ $ct$ ] $list:ci$ >>
2493 | ci = class_longident -> <:class_expr< $list:ci$ >>
2494 | "object"; cspo = V (OPT class_self_patt);
2495 cf = V class_structure "list"; "end" ->
2496 <:class_expr< object $_opt:cspo$ $_list:cf$ end >>
2497 | "("; ce = SELF; ":"; ct = class_type; ")" ->
2498 <:class_expr< ($ce$ : $ct$) >>
2499 | "("; ce = SELF; ")" -> ce ] ]
2502 [ [ cf = LIST0 class_str_item -> cf ] ]
2505 [ [ "("; p = patt; ")" -> p
2506 | "("; p = patt; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >> ] ]
2509 [ [ "inherit"; ce = class_expr; pb = V (OPT [ "as"; i = LIDENT -> i ]) ->
2510 <:class_str_item< inherit $ce$ $_opt:pb$ >>
2511 | "val"; ov = V (FLAG "!") "!"; mf = V (FLAG "mutable");
2512 lab = V LIDENT "lid" ""; e = cvalue_binding ->
2513 <:class_str_item< value $_!:ov$ $_flag:mf$ $_lid:lab$ = $e$ >>
2514 | "val"; ov = V (FLAG "!") "!"; mf = V (FLAG "mutable");
2515 "virtual"; lab = V LIDENT "lid" ""; ":"; t = ctyp ->
2516 if Pcaml.unvala ov then
2517 Ploc.raise loc (Stream.Error "virtual value cannot override")
2519 <:class_str_item< value virtual $_flag:mf$ $_lid:lab$ : $t$ >>
2520 | "val"; "virtual"; mf = V (FLAG "mutable"); lab = V LIDENT "lid" "";
2522 <:class_str_item< value virtual $_flag:mf$ $_lid:lab$ : $t$ >>
2523 | "method"; "private"; "virtual"; l = V LIDENT "lid" ""; ":";
2525 <:class_str_item< method virtual private $_lid:l$ : $t$ >>
2526 | "method"; "virtual"; "private"; l = V LIDENT "lid" ""; ":";
2528 <:class_str_item< method virtual private $_lid:l$ : $t$ >>
2529 | "method"; "virtual"; l = V LIDENT "lid" ""; ":"; t = poly_type ->
2530 <:class_str_item< method virtual $_lid:l$ : $t$ >>
2531 | "method"; ov = V (FLAG "!") "!"; "private"; l = V LIDENT "lid" "";
2532 ":"; t = poly_type; "="; e = expr ->
2533 <:class_str_item< method $_!:ov$ private $_lid:l$ : $t$ = $e$ >>
2534 | "method"; ov = V (FLAG "!") "!"; "private"; l = V LIDENT "lid" "";
2536 <:class_str_item< method $_!:ov$ private $_lid:l$ = $sb$ >>
2537 | "method"; ov = V (FLAG "!") "!"; l = V LIDENT "lid" ""; ":";
2538 t = poly_type; "="; e = expr ->
2539 <:class_str_item< method $_!:ov$ $_lid:l$ : $t$ = $e$ >>
2540 | "method"; ov = V (FLAG "!") "!"; l = V LIDENT "lid" "";
2542 <:class_str_item< method $_!:ov$ $_lid:l$ = $sb$ >>
2543 | "constraint"; t1 = ctyp; "="; t2 = ctyp ->
2544 <:class_str_item< type $t1$ = $t2$ >>
2545 | "initializer"; se = expr -> <:class_str_item< initializer $se$ >> ] ]
2548 [ [ "="; e = expr -> e
2549 | ":"; t = ctyp; "="; e = expr -> <:expr< ($e$ : $t$) >>
2550 | ":"; t = ctyp; ":>"; t2 = ctyp; "="; e = expr ->
2551 <:expr< ($e$ : $t$ :> $t2$) >>
2552 | ":>"; t = ctyp; "="; e = expr ->
2553 <:expr< ($e$ :> $t$) >> ] ]
2556 [ [ i = LIDENT -> i ] ]
2560 [ [ test_ctyp_minusgreater; t = ctyp LEVEL "star"; "->"; ct = SELF ->
2561 <:class_type< [ $t$ ] -> $ct$ >>
2562 | cs = class_signature -> cs ] ]
2565 [ [ "["; tl = LIST1 ctyp SEP ","; "]"; id = SELF ->
2566 <:class_type< $id$ [ $list:tl$ ] >>
2567 | "object"; cst = V (OPT class_self_type);
2568 csf = V (LIST0 class_sig_item); "end" ->
2569 <:class_type< object $_opt:cst$ $_list:csf$ end >> ]
2570 | [ ct1 = SELF; "."; ct2 = SELF -> <:class_type< $ct1$ . $ct2$ >>
2571 | ct1 = SELF; "("; ct2 = SELF; ")" -> <:class_type< $ct1$ $ct2$ >> ]
2572 | [ i = V LIDENT -> <:class_type< $_id: i$ >>
2573 | i = V UIDENT -> <:class_type< $_id: i$ >> ] ]
2576 [ [ "("; t = ctyp; ")" -> t ] ]
2579 [ [ "inherit"; cs = class_signature ->
2580 <:class_sig_item< inherit $cs$ >>
2581 | "val"; mf = V (FLAG "mutable"); l = V LIDENT "lid" ""; ":"; t = ctyp ->
2582 <:class_sig_item< value $_flag:mf$ $_lid:l$ : $t$ >>
2583 | "method"; "private"; "virtual"; l = V LIDENT "lid" ""; ":";
2585 <:class_sig_item< method virtual private $_lid:l$ : $t$ >>
2586 | "method"; "virtual"; "private"; l = V LIDENT "lid" ""; ":";
2588 <:class_sig_item< method virtual private $_lid:l$ : $t$ >>
2589 | "method"; "virtual"; l = V LIDENT "lid" ""; ":"; t = poly_type ->
2590 <:class_sig_item< method virtual $_lid:l$ : $t$ >>
2591 | "method"; "private"; l = V LIDENT "lid" ""; ":"; t = poly_type ->
2592 <:class_sig_item< method private $_lid:l$ : $t$ >>
2593 | "method"; l = V LIDENT "lid" ""; ":"; t = poly_type ->
2594 <:class_sig_item< method $_lid:l$ : $t$ >>
2595 | "constraint"; t1 = ctyp; "="; t2 = ctyp ->
2596 <:class_sig_item< type $t1$ = $t2$ >> ] ]
2599 [ [ vf = V (FLAG "virtual"); ctp = class_type_parameters; n = V LIDENT;
2600 ":"; ct = class_type ->
2601 {MLast.ciLoc = loc; MLast.ciVir = vf; MLast.ciPrm = ctp;
2602 MLast.ciNam = n; MLast.ciExp = ct} ] ]
2604 class_type_declaration:
2605 [ [ vf = V (FLAG "virtual"); ctp = class_type_parameters; n = V LIDENT;
2606 "="; cs = class_signature ->
2607 {MLast.ciLoc = loc; MLast.ciVir = vf; MLast.ciPrm = ctp;
2608 MLast.ciNam = n; MLast.ciExp = cs} ] ]
2611 expr: LEVEL "simple"
2613 [ "new"; i = V class_longident "list" -> <:expr< new $_list:i$ >>
2614 | "object"; cspo = V (OPT class_self_patt);
2615 cf = V class_structure "list"; "end" ->
2616 <:expr< object $_opt:cspo$ $_list:cf$ end >> ] ]
2619 [ [ e = SELF; "#"; lab = V LIDENT "lid" -> <:expr< $e$ # $_lid:lab$ >> ] ]
2621 expr: LEVEL "simple"
2622 [ [ "("; e = SELF; ":"; t = ctyp; ":>"; t2 = ctyp; ")" ->
2623 <:expr< ($e$ : $t$ :> $t2$) >>
2624 | "("; e = SELF; ":>"; t = ctyp; ")" -> <:expr< ($e$ :> $t$) >>
2625 | "{<"; ">}" -> <:expr< {< >} >>
2626 | "{<"; fel = V field_expr_list "list"; ">}" ->
2627 <:expr< {< $_list:fel$ >} >> ] ]
2630 [ [ l = label; "="; e = expr LEVEL "expr1"; ";"; fel = SELF ->
2632 | l = label; "="; e = expr LEVEL "expr1"; ";" -> [(l, e)]
2633 | l = label; "="; e = expr LEVEL "expr1" -> [(l, e)] ] ]
2636 ctyp: LEVEL "simple"
2637 [ [ "#"; id = V class_longident "list" ->
2638 <:ctyp< # $_list:id$ >>
2639 | "<"; ml = V meth_list "list"; v = V (FLAG ".."); ">" ->
2640 <:ctyp< < $_list:ml$ $_flag:v$ > >>
2647 [ [ f = field; ";"; ml = SELF -> [f :: ml]
2648 | f = field; ";" -> [f]
2649 | f = field -> [f] ] ]
2652 [ [ lab = LIDENT; ":"; t = poly_type -> (lab, t) ] ]
2654 (* Polymorphic types *)
2656 [ [ "'"; i = ident -> i ] ]
2659 [ [ "type"; nt = LIST1 LIDENT; "."; ct = ctyp ->
2660 <:ctyp< type $list:nt$ . $ct$ >>
2661 | test_typevar_list_dot; tpl = LIST1 typevar; "."; t2 = ctyp ->
2662 <:ctyp< ! $list:tpl$ . $t2$ >>
2667 [ [ m = UIDENT; "."; l = SELF -> [m :: l]
2668 | i = LIDENT -> [i] ] ]
2673 [ i = V LIDENT; ":"; t = SELF -> <:ctyp< ~$_:i$: $t$ >>
2674 | i = V QUESTIONIDENTCOLON; t = SELF -> <:ctyp< ?$_:i$: $t$ >>
2675 | i = V QUESTIONIDENT; ":"; t = SELF -> <:ctyp< ?$_:i$: $t$ >> ] ]
2677 ctyp: LEVEL "simple"
2678 [ [ "["; OPT "|"; rfl = V (LIST1 poly_variant SEP "|"); "]" ->
2679 <:ctyp< [ = $_list:rfl$ ] >>
2680 | "["; ">"; "]" -> <:ctyp< [ > $list:[]$ ] >>
2681 | "["; ">"; OPT "|"; rfl = V (LIST1 poly_variant SEP "|"); "]" ->
2682 <:ctyp< [ > $_list:rfl$ ] >>
2683 | "[<"; OPT "|"; rfl = V (LIST1 poly_variant SEP "|"); "]" ->
2684 <:ctyp< [ < $_list:rfl$ ] >>
2685 | "[<"; OPT "|"; rfl = V (LIST1 poly_variant SEP "|"); ">";
2686 ntl = V (LIST1 name_tag); "]" ->
2687 <:ctyp< [ < $_list:rfl$ > $_list:ntl$ ] >> ] ]
2690 [ [ "`"; i = V ident "" -> <:poly_variant< ` $_:i$ >>
2691 | "`"; i = V ident ""; "of"; ao = V (FLAG "&");
2692 l = V (LIST1 ctyp SEP "&") ->
2693 <:poly_variant< `$_:i$ of $_flag:ao$ $_list:l$ >>
2694 | t = ctyp -> <:poly_variant< $t$ >> ] ]
2697 [ [ "`"; i = ident -> i ] ]
2700 [ [ "fun"; p = labeled_patt; (eo, e) = fun_def ->
2701 <:expr< fun [ $p$ $opt:eo$ -> $e$ ] >> ] ]
2705 [ i = V TILDEIDENTCOLON; e = SELF -> <:expr< ~{$_:i$ = $e$} >>
2706 | i = V TILDEIDENT -> <:expr< ~{$_:i$} >>
2707 | i = V QUESTIONIDENTCOLON; e = SELF -> <:expr< ?{$_:i$ = $e$} >>
2708 | i = V QUESTIONIDENT -> <:expr< ?{$_:i$} >> ] ]
2710 expr: LEVEL "simple"
2711 [ [ "`"; s = V ident "" -> <:expr< ` $_:s$ >> ] ]
2714 [ [ p = labeled_patt; (eo, e) = SELF ->
2715 (None, <:expr< fun [ $p$ $opt:eo$ -> $e$ ] >>) ] ]
2718 [ [ p = labeled_patt; e = SELF -> <:expr< fun $p$ -> $e$ >> ] ]
2720 patt: LEVEL "simple"
2721 [ [ "`"; s = V ident "" -> <:patt< ` $_:s$ >>
2722 | "#"; t = V mod_ident "list" "" -> <:patt< # $_list:t$ >>
2723 | p = labeled_patt -> p ] ]
2726 [ [ i = V TILDEIDENTCOLON; p = patt LEVEL "simple" ->
2727 <:patt< ~{$_:i$ = $p$} >>
2728 | i = V TILDEIDENT ->
2730 | "~"; "("; i = LIDENT; ")" ->
2731 <:patt< ~{$lid:i$} >>
2732 | "~"; "("; i = LIDENT; ":"; t = ctyp; ")" ->
2733 <:patt< ~{$lid:i$ : $t$} >>
2734 | i = V QUESTIONIDENTCOLON; j = LIDENT ->
2735 <:patt< ?{$_:i$ = ?{$lid:j$}} >>
2736 | i = V QUESTIONIDENTCOLON; "_" ->
2738 | i = V QUESTIONIDENTCOLON; "("; p = patt; "="; e = expr; ")" ->
2739 <:patt< ?{$_:i$ = ?{$p$ = $e$}} >>
2740 | i = V QUESTIONIDENTCOLON; "("; p = patt; ":"; t = ctyp; ")" ->
2741 <:patt< ?{$_:i$ = ?{$p$ : $t$}} >>
2742 | i = V QUESTIONIDENTCOLON; "("; p = patt; ":"; t = ctyp; "=";
2744 <:patt< ?{$_:i$ = ?{$p$ : $t$ = $e$}} >>
2745 | i = V QUESTIONIDENTCOLON; "("; p = patt; ")" ->
2746 <:patt< ?{$_:i$ = ?{$p$}} >>
2747 | i = V QUESTIONIDENT -> <:patt< ?{$_:i$} >>
2748 | "?"; "("; i = LIDENT; "="; e = expr; ")" ->
2749 <:patt< ?{$lid:i$ = $e$} >>
2750 | "?"; "("; i = LIDENT; ":"; t = ctyp; "="; e = expr; ")" ->
2751 <:patt< ?{$lid:i$ : $t$ = $e$} >>
2752 | "?"; "("; i = LIDENT; ")" ->
2753 <:patt< ?{$lid:i$} >>
2754 | "?"; "("; i = LIDENT; ":"; t = ctyp; ")" ->
2755 <:patt< ?{$lid:i$ : $t$} >> ] ]
2758 [ [ i = LIDENT; ":"; t = ctyp LEVEL "apply"; "->"; ct = SELF ->
2759 <:class_type< [ ~$i$: $t$ ] -> $ct$ >>
2760 | i = V QUESTIONIDENTCOLON; t = ctyp LEVEL "apply"; "->"; ct = SELF ->
2761 <:class_type< [ ?$_:i$: $t$ ] -> $ct$ >>
2762 | i = V QUESTIONIDENT; ":"; t = ctyp LEVEL "apply"; "->"; ct = SELF ->
2763 <:class_type< [ ?$_:i$: $t$ ] -> $ct$ >> ] ]
2766 [ [ p = labeled_patt; cfb = SELF -> <:class_expr< fun $p$ -> $cfb$ >> ] ]
2769 [ [ p = labeled_patt; "->"; ce = class_expr ->
2770 <:class_expr< fun $p$ -> $ce$ >>
2771 | p = labeled_patt; cfd = SELF ->
2772 <:class_expr< fun $p$ -> $cfd$ >> ] ]
2776 (* Main entry points *)
2779 GLOBAL: interf implem use_file top_phrase expr patt;
2781 [ [ si = sig_item_semi; (sil, stopped) = SELF -> ([si :: sil], stopped)
2782 | "#"; n = LIDENT; dp = OPT expr; ";;" ->
2783 ([(<:sig_item< # $lid:n$ $opt:dp$ >>, loc)], None)
2784 | EOI -> ([], Some loc) ] ]
2787 [ [ si = sig_item; OPT ";;" -> (si, loc) ] ]
2790 [ [ si = str_item_semi; (sil, stopped) = SELF -> ([si :: sil], stopped)
2791 | "#"; n = LIDENT; dp = OPT expr; ";;" ->
2792 ([(<:str_item< # $lid:n$ $opt:dp$ >>, loc)], None)
2793 | EOI -> ([], Some loc) ] ]
2796 [ [ si = str_item; OPT ";;" -> (si, loc) ] ]
2799 [ [ ph = phrase; ";;" -> Some ph
2803 [ [ si = str_item; OPT ";;"; (sil, stopped) = SELF ->
2804 ([si :: sil], stopped)
2805 | "#"; n = LIDENT; dp = OPT expr; ";;" ->
2806 ([<:str_item< # $lid:n$ $opt:dp$ >>], True)
2807 | EOI -> ([], False) ] ]
2810 [ [ sti = str_item -> sti
2811 | "#"; n = LIDENT; dp = OPT expr ->
2812 <:str_item< # $lid:n$ $opt:dp$ >> ] ]
2816 Pcaml.add_option "-no_quot" (Arg.Set no_quotations)
2817 "Don't parse quotations, allowing to use, e.g. \"<:>\" as token";
2819 (* ------------------------------------------------------------------------- *)
2820 (* Added by JRH *** *)
2821 (* ------------------------------------------------------------------------- *)
2825 [[ f = expr; "o"; g = expr -> <:expr< ((o $f$) $g$) >>
2826 | f = expr; "upto"; g = expr -> <:expr< ((upto $f$) $g$) >>
2827 | f = expr; "F_F"; g = expr -> <:expr< ((f_f_ $f$) $g$) >>
2828 | f = expr; "THENC"; g = expr -> <:expr< ((thenc_ $f$) $g$) >>
2829 | f = expr; "THEN"; g = expr -> <:expr< ((then_ $f$) $g$) >>
2830 | f = expr; "THENL"; g = expr -> <:expr< ((thenl_ $f$) $g$) >>
2831 | f = expr; "ORELSE"; g = expr -> <:expr< ((orelse_ $f$) $g$) >>
2832 | f = expr; "ORELSEC"; g = expr -> <:expr< ((orelsec_ $f$) $g$) >>
2833 | f = expr; "THEN_TCL"; g = expr -> <:expr< ((then_tcl_ $f$) $g$) >>
2834 | f = expr; "ORELSE_TCL"; g = expr -> <:expr< ((orelse_tcl_ $f$) $g$) >>
2840 [ [ sti = str_item; ";;" ->
2842 [ <:str_item< $exp:e$ >> -> Some <:str_item< value it = $e$ >>