forked from ocaml/ocaml
/
Camlp4OCamlRevisedParser.ml
1861 lines (1825 loc) · 73.7 KB
/
Camlp4OCamlRevisedParser.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
open Camlp4; (* -*- camlp4r -*- *)
(****************************************************************************)
(* *)
(* Objective Caml *)
(* *)
(* INRIA Rocquencourt *)
(* *)
(* Copyright 2002-2006 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed under *)
(* the terms of the GNU Library General Public License, with the special *)
(* exception on linking described in LICENSE at the top of the Objective *)
(* Caml source tree. *)
(* *)
(****************************************************************************)
(* Authors:
* - Daniel de Rauglaudre: initial version
* - Nicolas Pouillard: refactoring
*)
module Id = struct
value name = "Camlp4OCamlRevisedParser";
value version = Sys.ocaml_version;
end;
module Make (Syntax : Sig.Camlp4Syntax) = struct
open Sig;
include Syntax;
(* Camlp4_config.constructors_arity.val := True; *)
Camlp4_config.constructors_arity.val := False;
value help_sequences () =
do {
Printf.eprintf "\
New syntax:\n\
(e1; e2; ... ; en) OR begin e1; e2; ... ; en end\n\
while e do e1; e2; ... ; en done\n\
for v = v1 to/downto v2 do e1; e2; ... ; en done\n\
Old syntax (still supported):\n\
do {e1; e2; ... ; en}\n\
while e do {e1; e2; ... ; en}\n\
for v = v1 to/downto v2 do {e1; e2; ... ; en}\n\
Very old (no more supported) syntax:\n\
do e1; e2; ... ; en-1; return en\n\
while e do e1; e2; ... ; en; done\n\
for v = v1 to/downto v2 do e1; e2; ... ; en; done\n\
";
flush stderr;
exit 1
}
;
Options.add "-help_seq" (Arg.Unit help_sequences)
"Print explanations about new sequences and exit.";
Gram.Entry.clear a_CHAR;
Gram.Entry.clear a_FLOAT;
Gram.Entry.clear a_INT;
Gram.Entry.clear a_INT32;
Gram.Entry.clear a_INT64;
Gram.Entry.clear a_LABEL;
Gram.Entry.clear a_LIDENT;
Gram.Entry.clear a_NATIVEINT;
Gram.Entry.clear a_OPTLABEL;
Gram.Entry.clear a_STRING;
Gram.Entry.clear a_UIDENT;
Gram.Entry.clear a_ident;
Gram.Entry.clear amp_ctyp;
Gram.Entry.clear and_ctyp;
Gram.Entry.clear match_case;
Gram.Entry.clear match_case0;
Gram.Entry.clear match_case_quot;
Gram.Entry.clear binding;
Gram.Entry.clear binding_quot;
Gram.Entry.clear rec_binding_quot;
Gram.Entry.clear class_declaration;
Gram.Entry.clear class_description;
Gram.Entry.clear class_expr;
Gram.Entry.clear class_expr_quot;
Gram.Entry.clear class_fun_binding;
Gram.Entry.clear class_fun_def;
Gram.Entry.clear class_info_for_class_expr;
Gram.Entry.clear class_info_for_class_type;
Gram.Entry.clear class_longident;
Gram.Entry.clear class_longident_and_param;
Gram.Entry.clear class_name_and_param;
Gram.Entry.clear class_sig_item;
Gram.Entry.clear class_sig_item_quot;
Gram.Entry.clear class_signature;
Gram.Entry.clear class_str_item;
Gram.Entry.clear class_str_item_quot;
Gram.Entry.clear class_structure;
Gram.Entry.clear class_type;
Gram.Entry.clear class_type_declaration;
Gram.Entry.clear class_type_longident;
Gram.Entry.clear class_type_longident_and_param;
Gram.Entry.clear class_type_plus;
Gram.Entry.clear class_type_quot;
Gram.Entry.clear comma_ctyp;
Gram.Entry.clear comma_expr;
Gram.Entry.clear comma_ipatt;
Gram.Entry.clear comma_patt;
Gram.Entry.clear comma_type_parameter;
Gram.Entry.clear constrain;
Gram.Entry.clear constructor_arg_list;
Gram.Entry.clear constructor_declaration;
Gram.Entry.clear constructor_declarations;
Gram.Entry.clear ctyp;
Gram.Entry.clear ctyp_quot;
Gram.Entry.clear cvalue_binding;
Gram.Entry.clear direction_flag;
Gram.Entry.clear dummy;
Gram.Entry.clear eq_expr;
Gram.Entry.clear expr;
Gram.Entry.clear expr_eoi;
Gram.Entry.clear expr_quot;
Gram.Entry.clear field_expr;
Gram.Entry.clear field_expr_list;
Gram.Entry.clear fun_binding;
Gram.Entry.clear fun_def;
Gram.Entry.clear ident;
Gram.Entry.clear ident_quot;
Gram.Entry.clear implem;
Gram.Entry.clear interf;
Gram.Entry.clear ipatt;
Gram.Entry.clear ipatt_tcon;
Gram.Entry.clear label;
Gram.Entry.clear label_declaration;
Gram.Entry.clear label_declaration_list;
Gram.Entry.clear label_expr_list;
Gram.Entry.clear label_expr;
Gram.Entry.clear label_ipatt;
Gram.Entry.clear label_ipatt_list;
Gram.Entry.clear label_longident;
Gram.Entry.clear label_patt;
Gram.Entry.clear label_patt_list;
Gram.Entry.clear labeled_ipatt;
Gram.Entry.clear let_binding;
Gram.Entry.clear meth_list;
Gram.Entry.clear meth_decl;
Gram.Entry.clear module_binding;
Gram.Entry.clear module_binding0;
Gram.Entry.clear module_binding_quot;
Gram.Entry.clear module_declaration;
Gram.Entry.clear module_expr;
Gram.Entry.clear module_expr_quot;
Gram.Entry.clear module_longident;
Gram.Entry.clear module_longident_with_app;
Gram.Entry.clear module_rec_declaration;
Gram.Entry.clear module_type;
Gram.Entry.clear module_type_quot;
Gram.Entry.clear more_ctyp;
Gram.Entry.clear name_tags;
Gram.Entry.clear opt_as_lident;
Gram.Entry.clear opt_class_self_patt;
Gram.Entry.clear opt_class_self_type;
Gram.Entry.clear opt_comma_ctyp;
Gram.Entry.clear opt_dot_dot;
Gram.Entry.clear opt_eq_ctyp;
Gram.Entry.clear opt_expr;
Gram.Entry.clear opt_meth_list;
Gram.Entry.clear opt_mutable;
Gram.Entry.clear opt_polyt;
Gram.Entry.clear opt_private;
Gram.Entry.clear opt_rec;
Gram.Entry.clear opt_virtual;
Gram.Entry.clear opt_when_expr;
Gram.Entry.clear patt;
Gram.Entry.clear patt_as_patt_opt;
Gram.Entry.clear patt_eoi;
Gram.Entry.clear patt_quot;
Gram.Entry.clear patt_tcon;
Gram.Entry.clear phrase;
Gram.Entry.clear poly_type;
Gram.Entry.clear row_field;
Gram.Entry.clear sem_expr;
Gram.Entry.clear sem_expr_for_list;
Gram.Entry.clear sem_patt;
Gram.Entry.clear sem_patt_for_list;
Gram.Entry.clear semi;
Gram.Entry.clear sequence;
Gram.Entry.clear sig_item;
Gram.Entry.clear sig_item_quot;
Gram.Entry.clear sig_items;
Gram.Entry.clear star_ctyp;
Gram.Entry.clear str_item;
Gram.Entry.clear str_item_quot;
Gram.Entry.clear str_items;
Gram.Entry.clear top_phrase;
Gram.Entry.clear type_constraint;
Gram.Entry.clear type_declaration;
Gram.Entry.clear type_ident_and_parameters;
Gram.Entry.clear type_kind;
Gram.Entry.clear type_longident;
Gram.Entry.clear type_longident_and_parameters;
Gram.Entry.clear type_parameter;
Gram.Entry.clear type_parameters;
Gram.Entry.clear typevars;
Gram.Entry.clear use_file;
Gram.Entry.clear val_longident;
Gram.Entry.clear value_let;
Gram.Entry.clear value_val;
Gram.Entry.clear with_constr;
Gram.Entry.clear with_constr_quot;
value neg_string n =
let len = String.length n in
if len > 0 && n.[0] = '-' then String.sub n 1 (len - 1)
else "-" ^ n
;
value mkumin _loc f arg =
match arg with
[ <:expr< $int:n$ >> -> <:expr< $int:neg_string n$ >>
| <:expr< $int32:n$ >> -> <:expr< $int32:neg_string n$ >>
| <:expr< $int64:n$ >> -> <:expr< $int64:neg_string n$ >>
| <:expr< $nativeint:n$ >> -> <:expr< $nativeint:neg_string n$ >>
| <:expr< $flo:n$ >> -> <:expr< $flo:neg_string n$ >>
| _ -> <:expr< $lid:"~" ^ f$ $arg$ >> ];
value mklistexp _loc last =
loop True where rec loop top =
fun
[ [] ->
match last with
[ Some e -> e
| None -> <:expr< [] >> ]
| [e1 :: el] ->
let _loc =
if top then _loc else Loc.merge (Ast.loc_of_expr e1) _loc
in
<:expr< [$e1$ :: $loop False el$] >> ]
;
value mkassert _loc =
fun
[ <:expr< False >> ->
<:expr< assert False >> (* this case takes care about
the special assert false node *)
| e -> <:expr< assert $e$ >> ]
;
value append_eLem el e = el @ [e];
value mk_anti ?(c = "") n s = "\\$"^n^c^":"^s;
value mksequence _loc =
fun
[ <:expr< $_$; $_$ >> | <:expr< $anti:_$ >> as e -> <:expr< do { $e$ } >>
| e -> e ]
;
value mksequence' _loc =
fun
[ <:expr< $_$; $_$ >> as e -> <:expr< do { $e$ } >>
| e -> e ]
;
value rec lid_of_ident =
fun
[ <:ident< $_$ . $i$ >> -> lid_of_ident i
| <:ident< $lid:lid$ >> -> lid
| _ -> assert False ];
value module_type_app mt1 mt2 =
match (mt1, mt2) with
[ (<:module_type@_loc< $id:i1$ >>, <:module_type< $id:i2$ >>) ->
<:module_type< $id:<:ident< $i1$ $i2$ >>$ >>
| _ -> raise Stream.Failure ];
value module_type_acc mt1 mt2 =
match (mt1, mt2) with
[ (<:module_type@_loc< $id:i1$ >>, <:module_type< $id:i2$ >>) ->
<:module_type< $id:<:ident< $i1$.$i2$ >>$ >>
| _ -> raise Stream.Failure ];
value bigarray_get _loc arr arg =
let coords =
match arg with
[ <:expr< ($e1$, $e2$) >> | <:expr< $e1$, $e2$ >> ->
Ast.list_of_expr e1 (Ast.list_of_expr e2 [])
| _ -> [arg] ]
in
match coords with
[ [c1] -> <:expr< Bigarray.Array1.get $arr$ $c1$ >>
| [c1; c2] -> <:expr< Bigarray.Array2.get $arr$ $c1$ $c2$ >>
| [c1; c2; c3] -> <:expr< Bigarray.Array3.get $arr$ $c1$ $c2$ $c3$ >>
(* | coords -> <:expr< Bigarray.Genarray.get $arr$ [| $list:coords$ |] >> ] *)
| coords ->
<:expr< Bigarray.Genarray.get $arr$ [| $Ast.exSem_of_list coords$ |] >> ];
value bigarray_set _loc var newval =
match var with
[ <:expr< Bigarray.Array1.get $arr$ $c1$ >> ->
Some <:expr< Bigarray.Array1.set $arr$ $c1$ $newval$ >>
| <:expr< Bigarray.Array2.get $arr$ $c1$ $c2$ >> ->
Some <:expr< Bigarray.Array2.set $arr$ $c1$ $c2$ $newval$ >>
| <:expr< Bigarray.Array3.get $arr$ $c1$ $c2$ $c3$ >> ->
Some <:expr< Bigarray.Array3.set $arr$ $c1$ $c2$ $c3$ $newval$ >>
| <:expr< Bigarray.Genarray.get $arr$ [| $coords$ |] >> ->
Some <:expr< Bigarray.Genarray.set $arr$ [| $coords$ |] $newval$ >>
| _ -> None ];
value stopped_at _loc =
Some (Loc.move_line 1 _loc) (* FIXME be more precise *);
value symbolchar =
let list =
['$'; '!'; '%'; '&'; '*'; '+'; '-'; '.'; '/'; ':'; '<'; '='; '>'; '?';
'@'; '^'; '|'; '~'; '\\']
in
let rec loop s i =
if i == String.length s then True
else if List.mem s.[i] list then loop s (i + 1)
else False
in
loop
;
value setup_op_parser entry p =
Gram.Entry.setup_parser entry
(parser
[: `(KEYWORD x | SYMBOL x, ti) when p x :] ->
let _loc = Gram.token_location ti in
<:expr< $lid:x$ >>);
let list = ['!'; '?'; '~'] in
let excl = ["!="; "??"] in
setup_op_parser prefixop
(fun x -> not (List.mem x excl) && String.length x >= 2 &&
List.mem x.[0] list && symbolchar x 1);
let list_ok = ["<"; ">"; "<="; ">="; "="; "<>"; "=="; "!="; "$"] in
let list_first_char_ok = ['='; '<'; '>'; '|'; '&'; '$'; '!'] in
let excl = ["<-"; "||"; "&&"] in
setup_op_parser infixop0
(fun x -> (List.mem x list_ok) ||
(not (List.mem x excl) && String.length x >= 2 &&
List.mem x.[0] list_first_char_ok && symbolchar x 1));
let list = ['@'; '^'] in
setup_op_parser infixop1
(fun x -> String.length x >= 1 && List.mem x.[0] list &&
symbolchar x 1);
let list = ['+'; '-'] in
setup_op_parser infixop2
(fun x -> x <> "->" && String.length x >= 1 && List.mem x.[0] list &&
symbolchar x 1);
let list = ['*'; '/'; '%'; '\\'] in
setup_op_parser infixop3
(fun x -> String.length x >= 1 && List.mem x.[0] list &&
(x.[0] <> '*' || String.length x < 2 || x.[1] <> '*') &&
symbolchar x 1);
setup_op_parser infixop4
(fun x -> String.length x >= 2 && x.[0] == '*' && x.[1] == '*' &&
symbolchar x 2);
value rec infix_kwds_filter =
parser
[ [: `((KEYWORD "(", _) as tok); xs :] ->
match xs with parser
[ [: `(KEYWORD ("mod"|"land"|"lor"|"lxor"|"lsl"|"lsr"|"asr" as i), _loc);
`(KEYWORD ")", _); xs :] ->
[: `(LIDENT i, _loc); infix_kwds_filter xs :]
| [: xs :] ->
[: `tok; infix_kwds_filter xs :] ]
| [: `x; xs :] -> [: `x; infix_kwds_filter xs :] ];
Token.Filter.define_filter (Gram.get_filter ())
(fun f strm -> infix_kwds_filter (f strm));
Gram.Entry.setup_parser sem_expr begin
let symb1 = Gram.parse_tokens_after_filter expr in
let symb =
parser
[ [: `(ANTIQUOT ("list" as n) s, ti) :] ->
let _loc = Gram.token_location ti in
<:expr< $anti:mk_anti ~c:"expr;" n s$ >>
| [: a = symb1 :] -> a ]
in
let rec kont al =
parser
[ [: `(KEYWORD ";", _); a = symb; s :] ->
let _loc = Loc.merge (Ast.loc_of_expr al)
(Ast.loc_of_expr a) in
kont <:expr< $al$; $a$ >> s
| [: :] -> al ]
in
parser [: a = symb; s :] -> kont a s
end;
EXTEND Gram
GLOBAL:
a_CHAR a_FLOAT a_INT a_INT32 a_INT64 a_LABEL a_LIDENT rec_binding_quot
a_NATIVEINT a_OPTLABEL a_STRING a_UIDENT a_ident
amp_ctyp and_ctyp match_case match_case0 match_case_quot binding binding_quot
class_declaration class_description class_expr class_expr_quot
class_fun_binding class_fun_def class_info_for_class_expr
class_info_for_class_type class_longident class_longident_and_param
class_name_and_param class_sig_item class_sig_item_quot class_signature
class_str_item class_str_item_quot class_structure class_type
class_type_declaration class_type_longident
class_type_longident_and_param class_type_plus class_type_quot
comma_ctyp comma_expr comma_ipatt comma_patt comma_type_parameter
constrain constructor_arg_list constructor_declaration
constructor_declarations ctyp ctyp_quot cvalue_binding direction_flag
dummy eq_expr expr expr_eoi expr_quot field_expr field_expr_list fun_binding
fun_def ident ident_quot implem interf ipatt ipatt_tcon label
label_declaration label_declaration_list label_expr label_expr_list
label_ipatt label_ipatt_list label_longident label_patt label_patt_list
labeled_ipatt let_binding meth_list meth_decl module_binding module_binding0
module_binding_quot module_declaration module_expr module_expr_quot
module_longident module_longident_with_app module_rec_declaration
module_type module_type_quot more_ctyp name_tags opt_as_lident
opt_class_self_patt opt_class_self_type opt_comma_ctyp opt_dot_dot opt_eq_ctyp opt_expr
opt_meth_list opt_mutable opt_polyt opt_private opt_rec
opt_virtual opt_when_expr patt patt_as_patt_opt patt_eoi
patt_quot patt_tcon phrase poly_type row_field
sem_expr sem_expr_for_list sem_patt sem_patt_for_list semi sequence
sig_item sig_item_quot sig_items star_ctyp str_item str_item_quot
str_items top_phrase type_constraint type_declaration
type_ident_and_parameters type_kind type_longident
type_longident_and_parameters type_parameter type_parameters typevars
use_file val_longident value_let value_val with_constr with_constr_quot
infixop0 infixop1 infixop2 infixop3 infixop4 do_sequence package_type
rec_flag_quot direction_flag_quot mutable_flag_quot private_flag_quot
virtual_flag_quot row_var_flag_quot override_flag_quot;
module_expr:
[ "top"
[ "functor"; "("; i = a_UIDENT; ":"; t = module_type; ")"; "->";
me = SELF ->
<:module_expr< functor ( $i$ : $t$ ) -> $me$ >>
| "struct"; st = str_items; "end" ->
<:module_expr< struct $st$ end >> ]
| "apply"
[ me1 = SELF; me2 = SELF -> <:module_expr< $me1$ $me2$ >> ]
| "simple"
[ `ANTIQUOT (""|"mexp"|"anti"|"list" as n) s ->
<:module_expr< $anti:mk_anti ~c:"module_expr" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.module_expr_tag
| i = module_longident -> <:module_expr< $id:i$ >>
| "("; me = SELF; ":"; mt = module_type; ")" ->
<:module_expr< ( $me$ : $mt$ ) >>
| "("; me = SELF; ")" -> <:module_expr< $me$ >>
| "("; value_val; e = expr; ")" ->
<:module_expr< (value $e$) >>
| "("; value_val; e = expr; ":"; p = package_type; ")" ->
<:module_expr< (value $e$ : $p$) >> ] ]
;
str_item:
[ "top"
[ "exception"; t = constructor_declaration ->
<:str_item< exception $t$ >>
| "exception"; t = constructor_declaration; "="; i = type_longident ->
<:str_item< exception $t$ = $i$ >>
| "external"; i = a_LIDENT; ":"; t = ctyp; "="; sl = string_list ->
<:str_item< external $i$ : $t$ = $sl$ >>
| "include"; me = module_expr -> <:str_item< include $me$ >>
| "module"; i = a_UIDENT; mb = module_binding0 ->
<:str_item< module $i$ = $mb$ >>
| "module"; "rec"; mb = module_binding ->
<:str_item< module rec $mb$ >>
| "module"; "type"; i = a_UIDENT; "="; mt = module_type ->
<:str_item< module type $i$ = $mt$ >>
| "open"; i = module_longident -> <:str_item< open $i$ >>
| "type"; td = type_declaration ->
<:str_item< type $td$ >>
| value_let; r = opt_rec; bi = binding ->
<:str_item< value $rec:r$ $bi$ >>
| "class"; cd = class_declaration ->
<:str_item< class $cd$ >>
| "class"; "type"; ctd = class_type_declaration ->
<:str_item< class type $ctd$ >>
| `ANTIQUOT (""|"stri"|"anti"|"list" as n) s ->
<:str_item< $anti:mk_anti ~c:"str_item" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.str_item_tag
| e = expr -> <:str_item< $exp:e$ >> ] ]
;
module_binding0:
[ RIGHTA
[ "("; m = a_UIDENT; ":"; mt = module_type; ")"; mb = SELF ->
<:module_expr< functor ( $m$ : $mt$ ) -> $mb$ >>
| ":"; mt = module_type; "="; me = module_expr ->
<:module_expr< ( $me$ : $mt$ ) >>
| "="; me = module_expr -> <:module_expr< $me$ >> ] ]
;
module_binding:
[ LEFTA
[ b1 = SELF; "and"; b2 = SELF ->
<:module_binding< $b1$ and $b2$ >>
| `ANTIQUOT ("module_binding"|"anti"|"list" as n) s ->
<:module_binding< $anti:mk_anti ~c:"module_binding" n s$ >>
| `ANTIQUOT ("" as n) s ->
<:module_binding< $anti:mk_anti ~c:"module_binding" n s$ >>
| `ANTIQUOT ("" as n) m; ":"; mt = module_type; "="; me = module_expr ->
<:module_binding< $mk_anti n m$ : $mt$ = $me$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.module_binding_tag
| m = a_UIDENT; ":"; mt = module_type; "="; me = module_expr ->
<:module_binding< $m$ : $mt$ = $me$ >> ] ]
;
module_type:
[ "top"
[ "functor"; "("; i = a_UIDENT; ":"; t = SELF; ")"; "->"; mt = SELF ->
<:module_type< functor ( $i$ : $t$ ) -> $mt$ >> ]
| "with"
[ mt = SELF; "with"; wc = with_constr ->
<:module_type< $mt$ with $wc$ >> ]
| "apply"
[ mt1 = SELF; mt2 = SELF; dummy -> module_type_app mt1 mt2 ]
| "."
[ mt1 = SELF; "."; mt2 = SELF -> module_type_acc mt1 mt2 ]
| "sig"
[ "sig"; sg = sig_items; "end" ->
<:module_type< sig $sg$ end >> ]
| "simple"
[ `ANTIQUOT (""|"mtyp"|"anti"|"list" as n) s ->
<:module_type< $anti:mk_anti ~c:"module_type" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.module_type_tag
| i = module_longident_with_app -> <:module_type< $id:i$ >>
| "'"; i = a_ident -> <:module_type< ' $i$ >>
| "("; mt = SELF; ")" -> <:module_type< $mt$ >> ] ]
;
sig_item:
[ "top"
[ `ANTIQUOT (""|"sigi"|"anti"|"list" as n) s ->
<:sig_item< $anti:mk_anti ~c:"sig_item" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.sig_item_tag
| "exception"; t = constructor_declaration ->
<:sig_item< exception $t$ >>
| "external"; i = a_LIDENT; ":"; t = ctyp; "="; sl = string_list ->
<:sig_item< external $i$ : $t$ = $sl$ >>
| "include"; mt = module_type -> <:sig_item< include $mt$ >>
| "module"; i = a_UIDENT; mt = module_declaration ->
<:sig_item< module $i$ : $mt$ >>
| "module"; "rec"; mb = module_rec_declaration ->
<:sig_item< module rec $mb$ >>
| "module"; "type"; i = a_UIDENT; "="; mt = module_type ->
<:sig_item< module type $i$ = $mt$ >>
| "module"; "type"; i = a_UIDENT ->
<:sig_item< module type $i$ >>
| "open"; i = module_longident -> <:sig_item< open $i$ >>
| "type"; t = type_declaration ->
<:sig_item< type $t$ >>
| value_val; i = a_LIDENT; ":"; t = ctyp ->
<:sig_item< value $i$ : $t$ >>
| "class"; cd = class_description ->
<:sig_item< class $cd$ >>
| "class"; "type"; ctd = class_type_declaration ->
<:sig_item< class type $ctd$ >> ] ]
;
module_declaration:
[ RIGHTA
[ ":"; mt = module_type -> <:module_type< $mt$ >>
| "("; i = a_UIDENT; ":"; t = module_type; ")"; mt = SELF ->
<:module_type< functor ( $i$ : $t$ ) -> $mt$ >> ] ]
;
module_rec_declaration:
[ LEFTA
[ m1 = SELF; "and"; m2 = SELF -> <:module_binding< $m1$ and $m2$ >>
| `ANTIQUOT (""|"module_binding"|"anti"|"list" as n) s ->
<:module_binding< $anti:mk_anti ~c:"module_binding" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.module_binding_tag
| m = a_UIDENT; ":"; mt = module_type -> <:module_binding< $m$ : $mt$ >>
] ]
;
with_constr:
[ LEFTA
[ wc1 = SELF; "and"; wc2 = SELF -> <:with_constr< $wc1$ and $wc2$ >>
| `ANTIQUOT (""|"with_constr"|"anti"|"list" as n) s ->
<:with_constr< $anti:mk_anti ~c:"with_constr" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.with_constr_tag
| "type"; `ANTIQUOT (""|"typ"|"anti" as n) s; "="; t = ctyp ->
<:with_constr< type $anti:mk_anti ~c:"ctyp" n s$ = $t$ >>
| "type"; t1 = type_longident_and_parameters; "="; t2 = ctyp ->
<:with_constr< type $t1$ = $t2$ >>
| "module"; i1 = module_longident; "="; i2 = module_longident_with_app ->
<:with_constr< module $i1$ = $i2$ >>
| "type"; `ANTIQUOT (""|"typ"|"anti" as n) s; ":="; t = ctyp ->
<:with_constr< type $anti:mk_anti ~c:"ctyp" n s$ := $t$ >>
| "type"; t1 = type_longident_and_parameters; ":="; t2 = ctyp ->
<:with_constr< type $t1$ := $t2$ >>
| "module"; i1 = module_longident; ":="; i2 = module_longident_with_app ->
<:with_constr< module $i1$ := $i2$ >> ] ]
;
expr:
[ "top" RIGHTA
[ "let"; r = opt_rec; bi = binding; "in"; x = SELF ->
<:expr< let $rec:r$ $bi$ in $x$ >>
| "let"; "module"; m = a_UIDENT; mb = module_binding0; "in"; e = SELF ->
<:expr< let module $m$ = $mb$ in $e$ >>
| "let"; "open"; i = module_longident; "in"; e = SELF ->
<:expr< let open $id:i$ in $e$ >>
| "fun"; "["; a = LIST0 match_case0 SEP "|"; "]" ->
<:expr< fun [ $list:a$ ] >>
| "fun"; e = fun_def -> e
| "match"; e = sequence; "with"; a = match_case ->
<:expr< match $mksequence' _loc e$ with [ $a$ ] >>
| "try"; e = sequence; "with"; a = match_case ->
<:expr< try $mksequence' _loc e$ with [ $a$ ] >>
| "if"; e1 = SELF; "then"; e2 = SELF; "else"; e3 = SELF ->
<:expr< if $e1$ then $e2$ else $e3$ >>
| "do"; seq = do_sequence -> mksequence _loc seq
| "for"; i = a_LIDENT; "="; e1 = sequence; df = direction_flag;
e2 = sequence; "do"; seq = do_sequence ->
<:expr< for $i$ = $mksequence' _loc e1$ $to:df$ $mksequence' _loc e2$ do { $seq$ } >>
| "while"; e = sequence; "do"; seq = do_sequence ->
<:expr< while $mksequence' _loc e$ do { $seq$ } >>
| "object"; csp = opt_class_self_patt; cst = class_structure; "end" ->
<:expr< object ($csp$) $cst$ end >> ]
| "where"
[ e = SELF; "where"; rf = opt_rec; lb = let_binding ->
<:expr< let $rec:rf$ $lb$ in $e$ >> ]
| ":=" NONA
[ e1 = SELF; ":="; e2 = SELF; dummy ->
match bigarray_set _loc e1 e2 with
[ Some e -> e
| None -> <:expr< $e1$ := $e2$ >> ] ]
| "||" RIGHTA
[ e1 = SELF; op = infixop6; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
| "&&" RIGHTA
[ e1 = SELF; op = infixop5; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
| "<" LEFTA
[ e1 = SELF; op = infixop0; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
| "^" RIGHTA
[ e1 = SELF; op = infixop1; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
| "+" LEFTA
[ e1 = SELF; op = infixop2; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
| "*" LEFTA
[ e1 = SELF; "land"; e2 = SELF -> <:expr< $e1$ land $e2$ >>
| e1 = SELF; "lor"; e2 = SELF -> <:expr< $e1$ lor $e2$ >>
| e1 = SELF; "lxor"; e2 = SELF -> <:expr< $e1$ lxor $e2$ >>
| e1 = SELF; "mod"; e2 = SELF -> <:expr< $e1$ mod $e2$ >>
| e1 = SELF; op = infixop3; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
| "**" RIGHTA
[ e1 = SELF; "asr"; e2 = SELF -> <:expr< $e1$ asr $e2$ >>
| e1 = SELF; "lsl"; e2 = SELF -> <:expr< $e1$ lsl $e2$ >>
| e1 = SELF; "lsr"; e2 = SELF -> <:expr< $e1$ lsr $e2$ >>
| e1 = SELF; op = infixop4; e2 = SELF -> <:expr< $op$ $e1$ $e2$ >> ]
| "unary minus" NONA
[ "-"; e = SELF -> mkumin _loc "-" e
| "-."; e = SELF -> mkumin _loc "-." e ]
| "apply" LEFTA
[ e1 = SELF; e2 = SELF -> <:expr< $e1$ $e2$ >>
| "assert"; e = SELF -> mkassert _loc e
| "new"; i = class_longident -> <:expr< new $i$ >>
| "lazy"; e = SELF -> <:expr< lazy $e$ >> ]
| "label" NONA
[ "~"; i = a_LIDENT; ":"; e = SELF -> <:expr< ~ $i$ : $e$ >>
| "~"; i = a_LIDENT -> <:expr< ~ $i$ >>
(* Here it's LABEL and not tilde_label since ~a:b is different than ~a : b *)
| `LABEL i; e = SELF -> <:expr< ~ $i$ : $e$ >>
(* Same remark for ?a:b *)
| `OPTLABEL i; e = SELF -> <:expr< ? $i$ : $e$ >>
| "?"; i = a_LIDENT; ":"; e = SELF -> <:expr< ? $i$ : $e$ >>
| "?"; i = a_LIDENT -> <:expr< ? $i$ >> ]
| "." LEFTA
[ e1 = SELF; "."; "("; e2 = SELF; ")" -> <:expr< $e1$ .( $e2$ ) >>
| e1 = SELF; "."; "["; e2 = SELF; "]" -> <:expr< $e1$ .[ $e2$ ] >>
| e1 = SELF; "."; "{"; e2 = comma_expr; "}" -> bigarray_get _loc e1 e2
| e1 = SELF; "."; e2 = SELF -> <:expr< $e1$ . $e2$ >>
| e = SELF; "#"; lab = label -> <:expr< $e$ # $lab$ >> ]
| "~-" NONA
[ "!"; e = SELF -> <:expr< $e$.val >>
| f = prefixop; e = SELF -> <:expr< $f$ $e$ >> ]
| "simple"
[ `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.expr_tag
| `ANTIQUOT ("exp"|""|"anti" as n) s ->
<:expr< $anti:mk_anti ~c:"expr" n s$ >>
| `ANTIQUOT ("`bool" as n) s ->
<:expr< $id:<:ident< $anti:mk_anti n s$ >>$ >>
| `ANTIQUOT ("tup" as n) s ->
<:expr< $tup: <:expr< $anti:mk_anti ~c:"expr" n s$ >>$ >>
| `ANTIQUOT ("seq" as n) s ->
<:expr< do $anti:mk_anti ~c:"expr" n s$ done >>
| s = a_INT -> <:expr< $int:s$ >>
| s = a_INT32 -> <:expr< $int32:s$ >>
| s = a_INT64 -> <:expr< $int64:s$ >>
| s = a_NATIVEINT -> <:expr< $nativeint:s$ >>
| s = a_FLOAT -> <:expr< $flo:s$ >>
| s = a_STRING -> <:expr< $str:s$ >>
| s = a_CHAR -> <:expr< $chr:s$ >>
| i = TRY module_longident_dot_lparen; e = sequence; ")" ->
<:expr< let open $i$ in $e$ >>
| i = TRY val_longident -> <:expr< $id:i$ >>
| "`"; s = a_ident -> <:expr< ` $s$ >>
| "["; "]" -> <:expr< [] >>
| "["; mk_list = sem_expr_for_list; "::"; last = expr; "]" ->
mk_list last
| "["; mk_list = sem_expr_for_list; "]" ->
mk_list <:expr< [] >>
| "[|"; "|]" -> <:expr< [| $<:expr<>>$ |] >>
| "[|"; el = sem_expr; "|]" -> <:expr< [| $el$ |] >>
| "{"; el = label_expr_list; "}" -> <:expr< { $el$ } >>
| "{"; "("; e = SELF; ")"; "with"; el = label_expr_list; "}" ->
<:expr< { ($e$) with $el$ } >>
| "{<"; ">}" -> <:expr< {<>} >>
| "{<"; fel = field_expr_list; ">}" -> <:expr< {< $fel$ >} >>
| "("; ")" -> <:expr< () >>
| "("; e = SELF; ":"; t = ctyp; ")" -> <:expr< ($e$ : $t$) >>
| "("; e = SELF; ","; el = comma_expr; ")" -> <:expr< ( $e$, $el$ ) >>
| "("; e = SELF; ";"; seq = sequence; ")" -> mksequence _loc <:expr< $e$; $seq$ >>
| "("; e = SELF; ";"; ")" -> mksequence _loc e
| "("; e = SELF; ":"; t = ctyp; ":>"; t2 = ctyp; ")" ->
<:expr< ($e$ : $t$ :> $t2$ ) >>
| "("; e = SELF; ":>"; t = ctyp; ")" -> <:expr< ($e$ :> $t$) >>
| "("; e = SELF; ")" -> e
| "begin"; seq = sequence; "end" -> mksequence _loc seq
| "begin"; "end" -> <:expr< () >>
| "("; "module"; me = module_expr; ")" ->
<:expr< (module $me$) >>
| "("; "module"; me = module_expr; ":"; pt = package_type; ")" ->
<:expr< (module $me$ : $pt$) >>
] ]
;
do_sequence:
[ [ seq = TRY ["{"; seq = sequence; "}" -> seq] -> seq
| TRY ["{"; "}"] -> <:expr< () >>
| seq = TRY [seq = sequence; "done" -> seq] -> seq
| "done" -> <:expr< () >>
] ]
;
infixop5:
[ [ x = [ "&" | "&&" ] -> <:expr< $lid:x$ >> ] ]
;
infixop6:
[ [ x = [ "or" | "||" ] -> <:expr< $lid:x$ >> ] ]
;
sem_expr_for_list:
[ [ e = expr; ";"; el = SELF -> fun acc -> <:expr< [ $e$ :: $el acc$ ] >>
| e = expr -> fun acc -> <:expr< [ $e$ :: $acc$ ] >>
] ]
;
comma_expr:
[ [ e1 = SELF; ","; e2 = SELF -> <:expr< $e1$, $e2$ >>
| `ANTIQUOT ("list" as n) s -> <:expr< $anti:mk_anti ~c:"expr," n s$ >>
| e = expr LEVEL "top" -> e ] ]
;
dummy:
[ [ -> () ] ]
;
sequence':
[ [ -> fun e -> e
| ";" -> fun e -> e
| ";"; el = sequence -> fun e -> <:expr< $e$; $el$ >> ] ]
;
sequence:
[ [ "let"; rf = opt_rec; bi = binding; "in"; e = expr; k = sequence' ->
k <:expr< let $rec:rf$ $bi$ in $e$ >>
| "let"; rf = opt_rec; bi = binding; ";"; el = SELF ->
<:expr< let $rec:rf$ $bi$ in $mksequence _loc el$ >>
| "let"; "module"; m = a_UIDENT; mb = module_binding0; "in"; e = expr; k = sequence' ->
k <:expr< let module $m$ = $mb$ in $e$ >>
| "let"; "module"; m = a_UIDENT; mb = module_binding0; ";"; el = SELF ->
<:expr< let module $m$ = $mb$ in $mksequence _loc el$ >>
| "let"; "open"; i = module_longident; "in"; e = SELF ->
<:expr< let open $id:i$ in $e$ >>
| `ANTIQUOT ("list" as n) s -> <:expr< $anti:mk_anti ~c:"expr;" n s$ >>
| e = expr; k = sequence' -> k e ] ]
;
binding:
[ LEFTA
[ `ANTIQUOT ("binding"|"list" as n) s ->
<:binding< $anti:mk_anti ~c:"binding" n s$ >>
| `ANTIQUOT (""|"anti" as n) s; "="; e = expr ->
<:binding< $anti:mk_anti ~c:"patt" n s$ = $e$ >>
| `ANTIQUOT (""|"anti" as n) s -> <:binding< $anti:mk_anti ~c:"binding" n s$ >>
| b1 = SELF; "and"; b2 = SELF -> <:binding< $b1$ and $b2$ >>
| b = let_binding -> b
] ]
;
let_binding:
[ [ p = ipatt; e = fun_binding -> <:binding< $p$ = $e$ >> ] ]
;
fun_binding:
[ RIGHTA
[ TRY ["("; "type"]; i = a_LIDENT; ")"; e = SELF ->
<:expr< fun (type $i$) -> $e$ >>
| p = TRY labeled_ipatt; e = SELF ->
<:expr< fun $p$ -> $e$ >>
| bi = cvalue_binding -> bi
] ]
;
match_case:
[ [ "["; l = LIST0 match_case0 SEP "|"; "]" -> Ast.mcOr_of_list l
| p = ipatt; "->"; e = expr -> <:match_case< $p$ -> $e$ >> ] ]
;
match_case0:
[ [ `ANTIQUOT ("match_case"|"list" as n) s ->
<:match_case< $anti:mk_anti ~c:"match_case" n s$ >>
| `ANTIQUOT (""|"anti" as n) s ->
<:match_case< $anti:mk_anti ~c:"match_case" n s$ >>
| `ANTIQUOT (""|"anti" as n) s; "->"; e = expr ->
<:match_case< $anti:mk_anti ~c:"patt" n s$ -> $e$ >>
| `ANTIQUOT (""|"anti" as n) s; "when"; w = expr; "->"; e = expr ->
<:match_case< $anti:mk_anti ~c:"patt" n s$ when $w$ -> $e$ >>
| p = patt_as_patt_opt; w = opt_when_expr; "->"; e = expr -> <:match_case< $p$ when $w$ -> $e$ >>
] ]
;
opt_when_expr:
[ [ "when"; w = expr -> w
| -> <:expr<>>
] ]
;
patt_as_patt_opt:
[ [ p1 = patt; "as"; p2 = patt -> <:patt< ($p1$ as $p2$) >>
| p = patt -> p
] ]
;
label_expr_list:
[ [ b1 = label_expr; ";"; b2 = SELF -> <:rec_binding< $b1$ ; $b2$ >>
| b1 = label_expr; ";" -> b1
| b1 = label_expr -> b1
] ];
label_expr:
[ [ `ANTIQUOT ("rec_binding" as n) s ->
<:rec_binding< $anti:mk_anti ~c:"rec_binding" n s$ >>
| `ANTIQUOT (""|"anti" as n) s ->
<:rec_binding< $anti:mk_anti ~c:"rec_binding" n s$ >>
| `ANTIQUOT (""|"anti" as n) s; "="; e = expr ->
<:rec_binding< $anti:mk_anti ~c:"ident" n s$ = $e$ >>
| `ANTIQUOT ("list" as n) s ->
<:rec_binding< $anti:mk_anti ~c:"rec_binding" n s$ >>
| i = label_longident; e = fun_binding -> <:rec_binding< $i$ = $e$ >>
| i = label_longident ->
<:rec_binding< $i$ = $lid:lid_of_ident i$ >> ] ]
;
fun_def:
[ [ TRY ["("; "type"]; i = a_LIDENT; ")";
e = fun_def_cont_no_when ->
<:expr< fun (type $i$) -> $e$ >>
| p = TRY labeled_ipatt; (w, e) = fun_def_cont ->
<:expr< fun [ $p$ when $w$ -> $e$ ] >> ] ]
;
fun_def_cont:
[ RIGHTA
[ TRY ["("; "type"]; i = a_LIDENT; ")";
e = fun_def_cont_no_when ->
(<:expr<>>, <:expr< fun (type $i$) -> $e$ >>)
| p = TRY labeled_ipatt; (w,e) = SELF ->
(<:expr<>>, <:expr< fun [ $p$ when $w$ -> $e$ ] >>)
| "when"; w = expr; "->"; e = expr -> (w, e)
| "->"; e = expr -> (<:expr<>>, e) ] ]
;
fun_def_cont_no_when:
[ RIGHTA
[ TRY ["("; "type"]; i = a_LIDENT; ")";
e = fun_def_cont_no_when -> <:expr< fun (type $i$) -> $e$ >>
| p = TRY labeled_ipatt; (w,e) = fun_def_cont ->
<:expr< fun [ $p$ when $w$ -> $e$ ] >>
| "->"; e = expr -> e ] ]
;
patt:
[ "|" LEFTA
[ p1 = SELF; "|"; p2 = SELF -> <:patt< $p1$ | $p2$ >> ]
| ".." NONA
[ p1 = SELF; ".."; p2 = SELF -> <:patt< $p1$ .. $p2$ >> ]
| "apply" LEFTA
[ p1 = SELF; p2 = SELF -> <:patt< $p1$ $p2$ >>
| "lazy"; p = SELF -> <:patt< lazy $p$ >> ]
| "simple"
[ `ANTIQUOT (""|"pat"|"anti" as n) s ->
<:patt< $anti:mk_anti ~c:"patt" n s$ >>
| `ANTIQUOT ("tup" as n) s -> <:patt< ($tup:<:patt< $anti:mk_anti ~c:"patt" n s$ >>$) >>
| `ANTIQUOT ("`bool" as n) s -> <:patt< $id:<:ident< $anti:mk_anti n s$ >>$ >>
| i = ident -> <:patt< $id:i$ >>
| s = a_INT -> <:patt< $int:s$ >>
| s = a_INT32 -> <:patt< $int32:s$ >>
| s = a_INT64 -> <:patt< $int64:s$ >>
| s = a_NATIVEINT -> <:patt< $nativeint:s$ >>
| s = a_FLOAT -> <:patt< $flo:s$ >>
| s = a_STRING -> <:patt< $str:s$ >>
| s = a_CHAR -> <:patt< $chr:s$ >>
| "-"; s = a_INT -> <:patt< $int:neg_string s$ >>
| "-"; s = a_INT32 -> <:patt< $int32:neg_string s$ >>
| "-"; s = a_INT64 -> <:patt< $int64:neg_string s$ >>
| "-"; s = a_NATIVEINT -> <:patt< $nativeint:neg_string s$ >>
| "-"; s = a_FLOAT -> <:patt< $flo:neg_string s$ >>
| "["; "]" -> <:patt< [] >>
| "["; mk_list = sem_patt_for_list; "::"; last = patt; "]" ->
mk_list last
| "["; mk_list = sem_patt_for_list; "]" ->
mk_list <:patt< [] >>
| "[|"; "|]" -> <:patt< [| $<:patt<>>$ |] >>
| "[|"; pl = sem_patt; "|]" -> <:patt< [| $pl$ |] >>
| "{"; pl = label_patt_list; "}" -> <:patt< { $pl$ } >>
| "("; ")" -> <:patt< () >>
| "("; p = SELF; ")" -> p
| "("; p = SELF; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >>
| "("; p = SELF; "as"; p2 = SELF; ")" -> <:patt< ($p$ as $p2$) >>
| "("; p = SELF; ","; pl = comma_patt; ")" -> <:patt< ($p$, $pl$) >>
| "_" -> <:patt< _ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.patt_tag
| "`"; s = a_ident -> <:patt< ` $s$ >>
| "#"; i = type_longident -> <:patt< # $i$ >>
| `LABEL i; p = SELF -> <:patt< ~ $i$ : $p$ >>
| "~"; `ANTIQUOT (""|"lid" as n) i; ":"; p = SELF ->
<:patt< ~ $mk_anti n i$ : $p$ >>
| "~"; `ANTIQUOT (""|"lid" as n) i -> <:patt< ~ $mk_anti n i$ >>
| "~"; `LIDENT i -> <:patt< ~ $i$ >>
(* | i = opt_label; "("; p = patt_tcon; ")" -> *)
(* <:patt< ? $i$ : ($p$) >> *)
| `OPTLABEL i; "("; p = patt_tcon; f = eq_expr; ")" -> f i p
| "?"; `ANTIQUOT (""|"lid" as n) i; ":"; "("; p = patt_tcon; f = eq_expr; ")" ->
f (mk_anti n i) p
| "?"; `LIDENT i -> <:patt< ? $i$ >>
| "?"; `ANTIQUOT (""|"lid" as n) i -> <:patt< ? $mk_anti n i$ >>
| "?"; "("; p = patt_tcon; ")" ->
<:patt< ? ($p$) >>
| "?"; "("; p = patt_tcon; "="; e = expr; ")" ->
<:patt< ? ($p$ = $e$) >> ] ]
;
comma_patt:
[ [ p1 = SELF; ","; p2 = SELF -> <:patt< $p1$, $p2$ >>
| `ANTIQUOT ("list" as n) s -> <:patt< $anti:mk_anti ~c:"patt," n s$ >>
| p = patt -> p ] ]
;
sem_patt:
[ LEFTA
[ p1 = SELF; ";"; p2 = SELF -> <:patt< $p1$; $p2$ >>
| `ANTIQUOT ("list" as n) s -> <:patt< $anti:mk_anti ~c:"patt;" n s$ >>
| p = patt -> p ] ]
;
sem_patt_for_list:
[ [ p = patt; ";"; pl = SELF -> fun acc -> <:patt< [ $p$ :: $pl acc$ ] >>
| p = patt -> fun acc -> <:patt< [ $p$ :: $acc$ ] >>
] ]
;
label_patt_list:
[ [ p1 = label_patt; ";"; p2 = SELF -> <:patt< $p1$ ; $p2$ >>
| p1 = label_patt; ";"; "_" -> <:patt< $p1$ ; _ >>
| p1 = label_patt; ";"; "_"; ";" -> <:patt< $p1$ ; _ >>
| p1 = label_patt; ";" -> p1
| p1 = label_patt -> p1
] ];
label_patt:
[ [ `ANTIQUOT (""|"pat"|"anti" as n) s ->
<:patt< $anti:mk_anti ~c:"patt" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.patt_tag
| `ANTIQUOT ("list" as n) s ->
<:patt< $anti:mk_anti ~c:"patt;" n s$ >>
| i = label_longident; "="; p = patt -> <:patt< $i$ = $p$ >>
| i = label_longident -> <:patt< $i$ = $lid:lid_of_ident i$ >>
] ]
;
ipatt:
[ [ "{"; pl = label_ipatt_list; "}" -> <:patt< { $pl$ } >>
| `ANTIQUOT (""|"pat"|"anti" as n) s ->
<:patt< $anti:mk_anti ~c:"patt" n s$ >>
| `ANTIQUOT ("tup" as n) s ->
<:patt< ($tup:<:patt< $anti:mk_anti ~c:"patt" n s$ >>$) >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.patt_tag
| "("; ")" -> <:patt< () >>
| "("; p = SELF; ")" -> p
| "("; p = SELF; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >>
| "("; p = SELF; "as"; p2 = SELF; ")" -> <:patt< ($p$ as $p2$) >>
| "("; p = SELF; ","; pl = comma_ipatt; ")" -> <:patt< ($p$, $pl$) >>
| s = a_LIDENT -> <:patt< $lid:s$ >>
| "_" -> <:patt< _ >> ] ]
;
labeled_ipatt:
[ [ p = ipatt -> p ] ]
;
comma_ipatt:
[ LEFTA
[ p1 = SELF; ","; p2 = SELF -> <:patt< $p1$, $p2$ >>
| `ANTIQUOT ("list" as n) s -> <:patt< $anti:mk_anti ~c:"patt," n s$ >>
| p = ipatt -> p ] ]
;
label_ipatt_list:
[ [ p1 = label_ipatt; ";"; p2 = SELF -> <:patt< $p1$ ; $p2$ >>
| p1 = label_ipatt; ";" -> p1
| p1 = label_ipatt -> p1
] ];
label_ipatt:
[ [ `ANTIQUOT (""|"pat"|"anti" as n) s ->
<:patt< $anti:mk_anti ~c:"patt" n s$ >>
| `ANTIQUOT ("list" as n) s -> <:patt< $anti:mk_anti ~c:"patt;" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.patt_tag
| i = label_longident; "="; p = ipatt -> <:patt< $i$ = $p$ >>
] ]
;
type_declaration:
[ LEFTA
[ `ANTIQUOT (""|"typ"|"anti" as n) s ->
<:ctyp< $anti:mk_anti ~c:"ctyp" n s$ >>
| `ANTIQUOT ("list" as n) s ->
<:ctyp< $anti:mk_anti ~c:"ctypand" n s$ >>
| `QUOTATION x -> Quotation.expand _loc x Quotation.DynAst.ctyp_tag
| t1 = SELF; "and"; t2 = SELF -> <:ctyp< $t1$ and $t2$ >>
| (n, tpl) = type_ident_and_parameters; tk = opt_eq_ctyp;
cl = LIST0 constrain -> Ast.TyDcl _loc n tpl tk cl ] ]
;
constrain:
[ [ "constraint"; t1 = ctyp; "="; t2 = ctyp -> (t1, t2) ] ]
;