forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
4124 lines (3805 loc) · 127 KB
/
parser.mly
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
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
/* The parser definition */
/* The commands [make list-parse-errors] and [make generate-parse-errors]
run Menhir on a modified copy of the parser where every block of
text comprised between the markers [BEGIN AVOID] and -----------
[END AVOID] has been removed. This file should be formatted in
such a way that this results in a clean removal of certain
symbols, productions, or declarations. */
%{
[@@@ocaml.warning "-60"] module Str = Ast_helper.Str (* For ocamldep *)
[@@@ocaml.warning "+60"]
open Asttypes
open Longident
open Parsetree
open Ast_helper
open Docstrings
open Docstrings.WithMenhir
let mkloc = Location.mkloc
let mknoloc = Location.mknoloc
let make_loc (startpos, endpos) = {
Location.loc_start = startpos;
Location.loc_end = endpos;
Location.loc_ghost = false;
}
let ghost_loc (startpos, endpos) = {
Location.loc_start = startpos;
Location.loc_end = endpos;
Location.loc_ghost = true;
}
let mktyp ~loc ?attrs d = Typ.mk ~loc:(make_loc loc) ?attrs d
let mkpat ~loc d = Pat.mk ~loc:(make_loc loc) d
let mkexp ~loc d = Exp.mk ~loc:(make_loc loc) d
let mkmty ~loc ?attrs d = Mty.mk ~loc:(make_loc loc) ?attrs d
let mksig ~loc d = Sig.mk ~loc:(make_loc loc) d
let mkmod ~loc ?attrs d = Mod.mk ~loc:(make_loc loc) ?attrs d
let mkstr ~loc d = Str.mk ~loc:(make_loc loc) d
let mkclass ~loc ?attrs d = Cl.mk ~loc:(make_loc loc) ?attrs d
let mkcty ~loc ?attrs d = Cty.mk ~loc:(make_loc loc) ?attrs d
let mkconst ~loc c = Const.mk ~loc:(make_loc loc) c
let pstr_typext (te, ext) =
(Pstr_typext te, ext)
let pstr_primitive (vd, ext) =
(Pstr_primitive vd, ext)
let pstr_type ((nr, ext), tys) =
(Pstr_type (nr, tys), ext)
let pstr_exception (te, ext) =
(Pstr_exception te, ext)
let pstr_include (body, ext) =
(Pstr_include body, ext)
let pstr_recmodule (ext, bindings) =
(Pstr_recmodule bindings, ext)
let psig_typext (te, ext) =
(Psig_typext te, ext)
let psig_value (vd, ext) =
(Psig_value vd, ext)
let psig_type ((nr, ext), tys) =
(Psig_type (nr, tys), ext)
let psig_typesubst ((nr, ext), tys) =
assert (nr = Recursive); (* see [no_nonrec_flag] *)
(Psig_typesubst tys, ext)
let psig_exception (te, ext) =
(Psig_exception te, ext)
let psig_include (body, ext) =
(Psig_include body, ext)
let mkctf ~loc ?attrs ?docs d =
Ctf.mk ~loc:(make_loc loc) ?attrs ?docs d
let mkcf ~loc ?attrs ?docs d =
Cf.mk ~loc:(make_loc loc) ?attrs ?docs d
let mkrhs rhs loc = mkloc rhs (make_loc loc)
let ghrhs rhs loc = mkloc rhs (ghost_loc loc)
let push_loc x acc =
if x.Location.loc_ghost
then acc
else x :: acc
let reloc_pat ~loc x =
{ x with ppat_loc = make_loc loc;
ppat_loc_stack = push_loc x.ppat_loc x.ppat_loc_stack }
let reloc_exp ~loc x =
{ x with pexp_loc = make_loc loc;
pexp_loc_stack = push_loc x.pexp_loc x.pexp_loc_stack }
let reloc_typ ~loc x =
{ x with ptyp_loc = make_loc loc;
ptyp_loc_stack = push_loc x.ptyp_loc x.ptyp_loc_stack }
let mkexpvar ~loc (name : string) =
mkexp ~loc (Pexp_ident(mkrhs (Lident name) loc))
let mkoperator =
mkexpvar
let mkpatvar ~loc name =
mkpat ~loc (Ppat_var (mkrhs name loc))
(*
Ghost expressions and patterns:
expressions and patterns that do not appear explicitly in the
source file they have the loc_ghost flag set to true.
Then the profiler will not try to instrument them and the
-annot option will not try to display their type.
Every grammar rule that generates an element with a location must
make at most one non-ghost element, the topmost one.
How to tell whether your location must be ghost:
A location corresponds to a range of characters in the source file.
If the location contains a piece of code that is syntactically
valid (according to the documentation), and corresponds to the
AST node, then the location must be real; in all other cases,
it must be ghost.
*)
let ghexp ~loc d = Exp.mk ~loc:(ghost_loc loc) d
let ghpat ~loc d = Pat.mk ~loc:(ghost_loc loc) d
let ghtyp ~loc d = Typ.mk ~loc:(ghost_loc loc) d
let ghloc ~loc d = { txt = d; loc = ghost_loc loc }
let ghstr ~loc d = Str.mk ~loc:(ghost_loc loc) d
let ghsig ~loc d = Sig.mk ~loc:(ghost_loc loc) d
let mkinfix arg1 op arg2 =
Pexp_apply(op, [Nolabel, arg1; Nolabel, arg2])
let neg_string f =
if String.length f > 0 && f.[0] = '-'
then String.sub f 1 (String.length f - 1)
else "-" ^ f
(* Pre-apply the special [-], [-.], [+] and [+.] prefix operators into
constants if possible, otherwise turn them into the corresponding prefix
operators [~-], [~-.], etc.. *)
let mkuminus ~sloc ~oploc name arg =
match name, arg.pexp_desc, arg.pexp_attributes with
| "-",
Pexp_constant({pconst_desc = Pconst_integer (n,m); pconst_loc=_}),
[] ->
Pexp_constant(mkconst ~loc:sloc (Pconst_integer(neg_string n, m)))
| ("-" | "-."),
Pexp_constant({pconst_desc = Pconst_float (f, m); pconst_loc=_}), [] ->
Pexp_constant(mkconst ~loc:sloc (Pconst_float(neg_string f, m)))
| _ ->
Pexp_apply(mkoperator ~loc:oploc ("~" ^ name), [Nolabel, arg])
let mkuplus ~sloc ~oploc name arg =
let desc = arg.pexp_desc in
match name, desc, arg.pexp_attributes with
| "+",
Pexp_constant({pconst_desc = Pconst_integer _ as desc; pconst_loc=_}),
[]
| ("+" | "+."),
Pexp_constant({pconst_desc = Pconst_float _ as desc; pconst_loc=_}),
[] ->
Pexp_constant(mkconst ~loc:sloc desc)
| _ ->
Pexp_apply(mkoperator ~loc:oploc ("~" ^ name), [Nolabel, arg])
let mk_attr ~loc name payload =
Builtin_attributes.(register_attr Parser name);
Attr.mk ~loc name payload
(* TODO define an abstraction boundary between locations-as-pairs
and locations-as-Location.t; it should be clear when we move from
one world to the other *)
let mkexp_cons_desc consloc args =
Pexp_construct(mkrhs (Lident "::") consloc, Some args)
let mkexp_cons ~loc consloc args =
mkexp ~loc (mkexp_cons_desc consloc args)
let mkpat_cons_desc consloc args =
Ppat_construct(mkrhs (Lident "::") consloc, Some ([], args))
let mkpat_cons ~loc consloc args =
mkpat ~loc (mkpat_cons_desc consloc args)
let ghexp_cons_desc consloc args =
Pexp_construct(ghrhs (Lident "::") consloc, Some args)
let ghpat_cons_desc consloc args =
Ppat_construct(ghrhs (Lident "::") consloc, Some ([], args))
let rec mktailexp nilloc = let open Location in function
[] ->
let nil = ghloc ~loc:nilloc (Lident "[]") in
Pexp_construct (nil, None), nilloc
| e1 :: el ->
let exp_el, el_loc = mktailexp nilloc el in
let loc = (e1.pexp_loc.loc_start, snd el_loc) in
let arg = ghexp ~loc (Pexp_tuple [e1; ghexp ~loc:el_loc exp_el]) in
ghexp_cons_desc loc arg, loc
let rec mktailpat nilloc = let open Location in function
[] ->
let nil = ghloc ~loc:nilloc (Lident "[]") in
Ppat_construct (nil, None), nilloc
| p1 :: pl ->
let pat_pl, el_loc = mktailpat nilloc pl in
let loc = (p1.ppat_loc.loc_start, snd el_loc) in
let arg = ghpat ~loc (Ppat_tuple [p1; ghpat ~loc:el_loc pat_pl]) in
ghpat_cons_desc loc arg, loc
let mkstrexp e attrs =
{ pstr_desc = Pstr_eval (e, attrs); pstr_loc = e.pexp_loc }
let mkexp_desc_constraint e t =
match t with
| Pconstraint t -> Pexp_constraint(e, t)
| Pcoerce(t1, t2) -> Pexp_coerce(e, t1, t2)
let mkexp_constraint ~loc e t =
mkexp ~loc (mkexp_desc_constraint e t)
let mkexp_opt_constraint ~loc e = function
| None -> e
| Some constraint_ -> mkexp_constraint ~loc e constraint_
let mkpat_opt_constraint ~loc p = function
| None -> p
| Some typ -> mkpat ~loc (Ppat_constraint(p, typ))
let syntax_error () =
raise Syntaxerr.Escape_error
let unclosed opening_name opening_loc closing_name closing_loc =
raise(Syntaxerr.Error(Syntaxerr.Unclosed(make_loc opening_loc, opening_name,
make_loc closing_loc, closing_name)))
let expecting loc nonterm =
raise Syntaxerr.(Error(Expecting(make_loc loc, nonterm)))
let removed_string_set loc =
raise(Syntaxerr.Error(Syntaxerr.Removed_string_set(make_loc loc)))
(* Using the function [not_expecting] in a semantic action means that this
syntactic form is recognized by the parser but is in fact incorrect. This
idiom is used in a few places to produce ad hoc syntax error messages. *)
(* This idiom should be used as little as possible, because it confuses the
analyses performed by Menhir. Because Menhir views the semantic action as
opaque, it believes that this syntactic form is correct. This can lead
[make generate-parse-errors] to produce sentences that cause an early
(unexpected) syntax error and do not achieve the desired effect. This could
also lead a completion system to propose completions which in fact are
incorrect. In order to avoid these problems, the productions that use
[not_expecting] should be marked with AVOID. *)
let not_expecting loc nonterm =
raise Syntaxerr.(Error(Not_expecting(make_loc loc, nonterm)))
(* Helper functions for desugaring array indexing operators *)
type paren_kind = Paren | Brace | Bracket
(* We classify the dimension of indices: Bigarray distinguishes
indices of dimension 1,2,3, or more. Similarly, user-defined
indexing operator behave differently for indices of dimension 1
or more.
*)
type index_dim =
| One
| Two
| Three
| Many
type ('dot,'index) array_family = {
name:
Lexing.position * Lexing.position -> 'dot -> assign:bool -> paren_kind
-> index_dim -> Longident.t Location.loc
(*
This functions computes the name of the explicit indexing operator
associated with a sugared array indexing expression.
For instance, for builtin arrays, if Clflags.unsafe is set,
* [ a.[index] ] => [String.unsafe_get]
* [ a.{x,y} <- 1 ] => [ Bigarray.Array2.unsafe_set]
User-defined indexing operator follows a more local convention:
* [ a .%(index)] => [ (.%()) ]
* [ a.![1;2] <- 0 ] => [(.![;..]<-)]
* [ a.My.Map.?(0) => [My.Map.(.?())]
*);
index:
Lexing.position * Lexing.position -> paren_kind -> 'index
-> index_dim * (arg_label * expression) list
(*
[index (start,stop) paren index] computes the dimension of the
index argument and how it should be desugared when transformed
to a list of arguments for the indexing operator.
In particular, in both the Bigarray case and the user-defined case,
beyond a certain dimension, multiple indices are packed into a single
array argument:
* [ a.(x) ] => [ [One, [Nolabel, <<x>>] ]
* [ a.{1,2} ] => [ [Two, [Nolabel, <<1>>; Nolabel, <<2>>] ]
* [ a.{1,2,3,4} ] => [ [Many, [Nolabel, <<[|1;2;3;4|]>>] ] ]
*);
}
let bigarray_untuplify = function
{ pexp_desc = Pexp_tuple explist; pexp_loc = _ } -> explist
| exp -> [exp]
let builtin_arraylike_name loc _ ~assign paren_kind n =
let opname = if assign then "set" else "get" in
let opname = if !Clflags.unsafe then "unsafe_" ^ opname else opname in
let prefix = match paren_kind with
| Paren -> Lident "Array"
| Bracket ->
if assign then removed_string_set loc
else Lident "String"
| Brace ->
let submodule_name = match n with
| One -> "Array1"
| Two -> "Array2"
| Three -> "Array3"
| Many -> "Genarray" in
Ldot(Lident "Bigarray", submodule_name) in
ghloc ~loc (Ldot(prefix,opname))
let builtin_arraylike_index loc paren_kind index = match paren_kind with
| Paren | Bracket -> One, [Nolabel, index]
| Brace ->
(* Multi-indices for bigarray are comma-separated ([a.{1,2,3,4}]) *)
match bigarray_untuplify index with
| [x] -> One, [Nolabel, x]
| [x;y] -> Two, [Nolabel, x; Nolabel, y]
| [x;y;z] -> Three, [Nolabel, x; Nolabel, y; Nolabel, z]
| coords -> Many, [Nolabel, ghexp ~loc (Pexp_array coords)]
let builtin_indexing_operators : (unit, expression) array_family =
{ index = builtin_arraylike_index; name = builtin_arraylike_name }
let paren_to_strings = function
| Paren -> "(", ")"
| Bracket -> "[", "]"
| Brace -> "{", "}"
let user_indexing_operator_name loc (prefix,ext) ~assign paren_kind n =
let name =
let assign = if assign then "<-" else "" in
let mid = match n with
| Many | Three | Two -> ";.."
| One -> "" in
let left, right = paren_to_strings paren_kind in
String.concat "" ["."; ext; left; mid; right; assign] in
let lid = match prefix with
| None -> Lident name
| Some p -> Ldot(p,name) in
ghloc ~loc lid
let user_index loc _ index =
(* Multi-indices for user-defined operators are semicolon-separated
([a.%[1;2;3;4]]) *)
match index with
| [a] -> One, [Nolabel, a]
| l -> Many, [Nolabel, mkexp ~loc (Pexp_array l)]
let user_indexing_operators:
(Longident.t option * string, expression list) array_family
= { index = user_index; name = user_indexing_operator_name }
let mk_indexop_expr array_indexing_operator ~loc
(array,dot,paren,index,set_expr) =
let assign = match set_expr with None -> false | Some _ -> true in
let n, index = array_indexing_operator.index loc paren index in
let fn = array_indexing_operator.name loc dot ~assign paren n in
let set_arg = match set_expr with
| None -> []
| Some expr -> [Nolabel, expr] in
let args = (Nolabel,array) :: index @ set_arg in
mkexp ~loc (Pexp_apply(ghexp ~loc (Pexp_ident fn), args))
let indexop_unclosed_error loc_s s loc_e =
let left, right = paren_to_strings s in
unclosed left loc_s right loc_e
let lapply ~loc p1 p2 =
if !Clflags.applicative_functors
then Lapply(p1, p2)
else raise (Syntaxerr.Error(
Syntaxerr.Applicative_path (make_loc loc)))
(* [loc_map] could be [Location.map]. *)
let loc_map (f : 'a -> 'b) (x : 'a Location.loc) : 'b Location.loc =
{ x with txt = f x.txt }
let make_ghost x = { x with loc = { x.loc with loc_ghost = true }}
let loc_last (id : Longident.t Location.loc) : string Location.loc =
loc_map Longident.last id
let loc_lident (id : string Location.loc) : Longident.t Location.loc =
loc_map (fun x -> Lident x) id
let exp_of_longident lid =
let lid = loc_map (fun id -> Lident (Longident.last id)) lid in
Exp.mk ~loc:lid.loc (Pexp_ident lid)
let exp_of_label lbl =
Exp.mk ~loc:lbl.loc (Pexp_ident (loc_lident lbl))
let pat_of_label lbl =
Pat.mk ~loc:lbl.loc (Ppat_var (loc_last lbl))
let mk_newtypes ~loc newtypes exp =
let mkexp = mkexp ~loc in
List.fold_right (fun newtype exp -> mkexp (Pexp_newtype (newtype, exp)))
newtypes exp
let wrap_type_annotation ~loc newtypes core_type body =
let mkexp, ghtyp = mkexp ~loc, ghtyp ~loc in
let mk_newtypes = mk_newtypes ~loc in
let exp = mkexp(Pexp_constraint(body,core_type)) in
let exp = mk_newtypes newtypes exp in
(exp, ghtyp(Ptyp_poly(newtypes, Typ.varify_constructors newtypes core_type)))
let wrap_exp_attrs ~loc body (ext, attrs) =
let ghexp = ghexp ~loc in
(* todo: keep exact location for the entire attribute *)
let body = {body with pexp_attributes = attrs @ body.pexp_attributes} in
match ext with
| None -> body
| Some id -> ghexp(Pexp_extension (id, PStr [mkstrexp body []]))
let mkexp_attrs ~loc d attrs =
wrap_exp_attrs ~loc (mkexp ~loc d) attrs
let wrap_typ_attrs ~loc typ (ext, attrs) =
(* todo: keep exact location for the entire attribute *)
let typ = {typ with ptyp_attributes = attrs @ typ.ptyp_attributes} in
match ext with
| None -> typ
| Some id -> ghtyp ~loc (Ptyp_extension (id, PTyp typ))
let wrap_pat_attrs ~loc pat (ext, attrs) =
(* todo: keep exact location for the entire attribute *)
let pat = {pat with ppat_attributes = attrs @ pat.ppat_attributes} in
match ext with
| None -> pat
| Some id -> ghpat ~loc (Ppat_extension (id, PPat (pat, None)))
let mkpat_attrs ~loc d attrs =
wrap_pat_attrs ~loc (mkpat ~loc d) attrs
let wrap_class_attrs ~loc:_ body attrs =
{body with pcl_attributes = attrs @ body.pcl_attributes}
let wrap_mod_attrs ~loc:_ attrs body =
{body with pmod_attributes = attrs @ body.pmod_attributes}
let wrap_mty_attrs ~loc:_ attrs body =
{body with pmty_attributes = attrs @ body.pmty_attributes}
let wrap_str_ext ~loc body ext =
match ext with
| None -> body
| Some id -> ghstr ~loc (Pstr_extension ((id, PStr [body]), []))
let wrap_mkstr_ext ~loc (item, ext) =
wrap_str_ext ~loc (mkstr ~loc item) ext
let wrap_sig_ext ~loc body ext =
match ext with
| None -> body
| Some id -> ghsig ~loc (Psig_extension ((id, PSig [body]), []))
let wrap_mksig_ext ~loc (item, ext) =
wrap_sig_ext ~loc (mksig ~loc item) ext
let mk_quotedext ~loc (id, idloc, str, strloc, delim) =
let exp_id = mkloc id idloc in
let const = Const.mk ~loc:strloc (Pconst_string (str, strloc, delim)) in
let e = ghexp ~loc (Pexp_constant const) in
(exp_id, PStr [mkstrexp e []])
let text_str pos = Str.text (rhs_text pos)
let text_sig pos = Sig.text (rhs_text pos)
let text_cstr pos = Cf.text (rhs_text pos)
let text_csig pos = Ctf.text (rhs_text pos)
let text_def pos =
List.map (fun def -> Ptop_def [def]) (Str.text (rhs_text pos))
let extra_text startpos endpos text items =
match items with
| [] ->
let post = rhs_post_text endpos in
let post_extras = rhs_post_extra_text endpos in
text post @ text post_extras
| _ :: _ ->
let pre_extras = rhs_pre_extra_text startpos in
let post_extras = rhs_post_extra_text endpos in
text pre_extras @ items @ text post_extras
let extra_str p1 p2 items = extra_text p1 p2 Str.text items
let extra_sig p1 p2 items = extra_text p1 p2 Sig.text items
let extra_cstr p1 p2 items = extra_text p1 p2 Cf.text items
let extra_csig p1 p2 items = extra_text p1 p2 Ctf.text items
let extra_def p1 p2 items =
extra_text p1 p2
(fun txt -> List.map (fun def -> Ptop_def [def]) (Str.text txt))
items
let extra_rhs_core_type ct ~pos =
let docs = rhs_info pos in
{ ct with ptyp_attributes = add_info_attrs docs ct.ptyp_attributes }
type let_binding =
{ lb_pattern: pattern;
lb_expression: expression;
lb_constraint: value_constraint option;
lb_is_pun: bool;
lb_attributes: attributes;
lb_docs: docs Lazy.t;
lb_text: text Lazy.t;
lb_loc: Location.t; }
type let_bindings =
{ lbs_bindings: let_binding list;
lbs_rec: rec_flag;
lbs_extension: string Asttypes.loc option }
let mklb first ~loc (p, e, typ, is_pun) attrs =
{
lb_pattern = p;
lb_expression = e;
lb_constraint=typ;
lb_is_pun = is_pun;
lb_attributes = attrs;
lb_docs = symbol_docs_lazy loc;
lb_text = (if first then empty_text_lazy
else symbol_text_lazy (fst loc));
lb_loc = make_loc loc;
}
let addlb lbs lb =
if lb.lb_is_pun && lbs.lbs_extension = None then syntax_error ();
{ lbs with lbs_bindings = lb :: lbs.lbs_bindings }
let mklbs ext rf lb =
let lbs = {
lbs_bindings = [];
lbs_rec = rf;
lbs_extension = ext;
} in
addlb lbs lb
let val_of_let_bindings ~loc lbs =
let bindings =
List.map
(fun lb ->
Vb.mk ~loc:lb.lb_loc ~attrs:lb.lb_attributes
~docs:(Lazy.force lb.lb_docs)
~text:(Lazy.force lb.lb_text)
?value_constraint:lb.lb_constraint lb.lb_pattern lb.lb_expression)
lbs.lbs_bindings
in
let str = mkstr ~loc (Pstr_value(lbs.lbs_rec, List.rev bindings)) in
match lbs.lbs_extension with
| None -> str
| Some id -> ghstr ~loc (Pstr_extension((id, PStr [str]), []))
let expr_of_let_bindings ~loc lbs body =
let bindings =
List.map
(fun lb ->
Vb.mk ~loc:lb.lb_loc ~attrs:lb.lb_attributes
?value_constraint:lb.lb_constraint lb.lb_pattern lb.lb_expression)
lbs.lbs_bindings
in
mkexp_attrs ~loc (Pexp_let(lbs.lbs_rec, List.rev bindings, body))
(lbs.lbs_extension, [])
let class_of_let_bindings ~loc lbs body =
let bindings =
List.map
(fun lb ->
Vb.mk ~loc:lb.lb_loc ~attrs:lb.lb_attributes
?value_constraint:lb.lb_constraint lb.lb_pattern lb.lb_expression)
lbs.lbs_bindings
in
(* Our use of let_bindings(no_ext) guarantees the following: *)
assert (lbs.lbs_extension = None);
mkclass ~loc (Pcl_let (lbs.lbs_rec, List.rev bindings, body))
(* If all the parameters are [Pparam_newtype x], then return [Some xs] where
[xs] is the corresponding list of values [x]. This function is optimized for
the common case, where a list of parameters contains at least one value
parameter.
*)
let all_params_as_newtypes =
let is_newtype { pparam_desc; _ } =
match pparam_desc with
| Pparam_newtype _ -> true
| Pparam_val _ -> false
in
let as_newtype { pparam_desc; pparam_loc } =
match pparam_desc with
| Pparam_newtype x -> Some (x, pparam_loc)
| Pparam_val _ -> None
in
fun params ->
if List.for_all is_newtype params
then Some (List.filter_map as_newtype params)
else None
(* Given a construct [fun (type a b c) : t -> e], we construct
[Pexp_newtype(a, Pexp_newtype(b, Pexp_newtype(c, Pexp_constraint(e, t))))]
rather than a [Pexp_function].
*)
let mkghost_newtype_function_body newtypes body_constraint body =
let wrapped_body =
match body_constraint with
| None -> body
| Some body_constraint ->
let loc = { body.pexp_loc with loc_ghost = true } in
Exp.mk (mkexp_desc_constraint body body_constraint) ~loc
in
let expr =
List.fold_right
(fun (newtype, newtype_loc) e ->
(* Mints a ghost location that approximates the newtype's "extent" as
being from the start of the newtype param until the end of the
function body.
*)
let loc = (newtype_loc.Location.loc_start, body.pexp_loc.loc_end) in
ghexp (Pexp_newtype (newtype, e)) ~loc)
newtypes
wrapped_body
in
expr.pexp_desc
let mkfunction params body_constraint body =
match body with
| Pfunction_cases _ -> Pexp_function (params, body_constraint, body)
| Pfunction_body body_exp ->
(* If all the params are newtypes, then we don't create a function node;
we create nested newtype nodes. *)
match all_params_as_newtypes params with
| None -> Pexp_function (params, body_constraint, body)
| Some newtypes ->
mkghost_newtype_function_body newtypes body_constraint body_exp
(* Alternatively, we could keep the generic module type in the Parsetree
and extract the package type during type-checking. In that case,
the assertions below should be turned into explicit checks. *)
let package_type_of_module_type pmty =
let err loc s =
raise (Syntaxerr.Error (Syntaxerr.Invalid_package_type (loc, s)))
in
let map_cstr = function
| Pwith_type (lid, ptyp) ->
let loc = ptyp.ptype_loc in
if ptyp.ptype_params <> [] then
err loc Syntaxerr.Parameterized_types;
if ptyp.ptype_cstrs <> [] then
err loc Syntaxerr.Constrained_types;
if ptyp.ptype_private <> Public then
err loc Syntaxerr.Private_types;
(* restrictions below are checked by the 'with_constraint' rule *)
assert (ptyp.ptype_kind = Ptype_abstract);
assert (ptyp.ptype_attributes = []);
let ty =
match ptyp.ptype_manifest with
| Some ty -> ty
| None -> assert false
in
(lid, ty)
| _ ->
err pmty.pmty_loc Not_with_type
in
match pmty with
| {pmty_desc = Pmty_ident lid} -> (lid, [], pmty.pmty_attributes)
| {pmty_desc = Pmty_with({pmty_desc = Pmty_ident lid}, cstrs)} ->
(lid, List.map map_cstr cstrs, pmty.pmty_attributes)
| _ ->
err pmty.pmty_loc Neither_identifier_nor_with_type
let mk_directive_arg ~loc k =
{ pdira_desc = k;
pdira_loc = make_loc loc;
}
let mk_directive ~loc name arg =
Ptop_dir {
pdir_name = name;
pdir_arg = arg;
pdir_loc = make_loc loc;
}
%}
/* Tokens */
/* The alias that follows each token is used by Menhir when it needs to
produce a sentence (that is, a sequence of tokens) in concrete syntax. */
/* Some tokens represent multiple concrete strings. In most cases, an
arbitrary concrete string can be chosen. In a few cases, one must
be careful: e.g., in PREFIXOP and INFIXOP2, one must choose a concrete
string that will not trigger a syntax error; see how [not_expecting]
is used in the definition of [type_variance]. */
%token AMPERAMPER "&&"
%token AMPERSAND "&"
%token AND "and"
%token AS "as"
%token ASSERT "assert"
%token BACKQUOTE "`"
%token BANG "!"
%token BAR "|"
%token BARBAR "||"
%token BARRBRACKET "|]"
%token BEGIN "begin"
%token <char> CHAR "'a'" (* just an example *)
%token CLASS "class"
%token COLON ":"
%token COLONCOLON "::"
%token COLONEQUAL ":="
%token COLONGREATER ":>"
%token COMMA ","
%token CONSTRAINT "constraint"
%token DO "do"
%token DONE "done"
%token DOT "."
%token DOTDOT ".."
%token DOWNTO "downto"
%token EFFECT "effect"
%token ELSE "else"
%token END "end"
%token EOF ""
%token EQUAL "="
%token EXCEPTION "exception"
%token EXTERNAL "external"
%token FALSE "false"
%token <string * char option> FLOAT "42.0" (* just an example *)
%token FOR "for"
%token FUN "fun"
%token FUNCTION "function"
%token FUNCTOR "functor"
%token GREATER ">"
%token GREATERRBRACE ">}"
%token GREATERRBRACKET ">]"
%token IF "if"
%token IN "in"
%token INCLUDE "include"
%token <string> INFIXOP0 "!=" (* just an example *)
%token <string> INFIXOP1 "@" (* just an example *)
%token <string> INFIXOP2 "+!" (* chosen with care; see above *)
%token <string> INFIXOP3 "land" (* just an example *)
%token <string> INFIXOP4 "**" (* just an example *)
%token <string> DOTOP ".+"
%token <string> LETOP "let*" (* just an example *)
%token <string> ANDOP "and*" (* just an example *)
%token INHERIT "inherit"
%token INITIALIZER "initializer"
%token <string * char option> INT "42" (* just an example *)
%token <string> LABEL "~label:" (* just an example *)
%token LAZY "lazy"
%token LBRACE "{"
%token LBRACELESS "{<"
%token LBRACKET "["
%token LBRACKETBAR "[|"
%token LBRACKETLESS "[<"
%token LBRACKETGREATER "[>"
%token LBRACKETPERCENT "[%"
%token LBRACKETPERCENTPERCENT "[%%"
%token LESS "<"
%token LESSMINUS "<-"
%token LET "let"
%token <string> LIDENT "lident" (* just an example *)
%token LPAREN "("
%token LBRACKETAT "[@"
%token LBRACKETATAT "[@@"
%token LBRACKETATATAT "[@@@"
%token MATCH "match"
%token METHOD "method"
%token MINUS "-"
%token MINUSDOT "-."
%token MINUSGREATER "->"
%token MODULE "module"
%token MUTABLE "mutable"
%token NEW "new"
%token NONREC "nonrec"
%token OBJECT "object"
%token OF "of"
%token OPEN "open"
%token <string> OPTLABEL "?label:" (* just an example *)
%token OR "or"
/* %token PARSER "parser" */
%token PERCENT "%"
%token PLUS "+"
%token PLUSDOT "+."
%token PLUSEQ "+="
%token <string> PREFIXOP "!+" (* chosen with care; see above *)
%token PRIVATE "private"
%token QUESTION "?"
%token QUOTE "'"
%token RBRACE "}"
%token RBRACKET "]"
%token REC "rec"
%token RPAREN ")"
%token SEMI ";"
%token SEMISEMI ";;"
%token HASH "#"
%token <string> HASHOP "##" (* just an example *)
%token SIG "sig"
%token STAR "*"
%token <string * Location.t * string option>
STRING "\"hello\"" (* just an example *)
%token <string * Location.t * string * Location.t * string option>
QUOTED_STRING_EXPR "{%hello|world|}" (* just an example *)
%token <string * Location.t * string * Location.t * string option>
QUOTED_STRING_ITEM "{%%hello|world|}" (* just an example *)
%token STRUCT "struct"
%token THEN "then"
%token TILDE "~"
%token TO "to"
%token TRUE "true"
%token TRY "try"
%token TYPE "type"
%token <string> UIDENT "UIdent" (* just an example *)
%token UNDERSCORE "_"
%token VAL "val"
%token VIRTUAL "virtual"
%token WHEN "when"
%token WHILE "while"
%token WITH "with"
%token <string * Location.t> COMMENT "(* comment *)"
%token <Docstrings.docstring> DOCSTRING "(** documentation *)"
%token EOL "\\n" (* not great, but EOL is unused *)
/* Precedences and associativities.
Tokens and rules have precedences. A reduce/reduce conflict is resolved
in favor of the first rule (in source file order). A shift/reduce conflict
is resolved by comparing the precedence and associativity of the token to
be shifted with those of the rule to be reduced.
By default, a rule has the precedence of its rightmost terminal (if any).
When there is a shift/reduce conflict between a rule and a token that
have the same precedence, it is resolved using the associativity:
if the token is left-associative, the parser will reduce; if
right-associative, the parser will shift; if non-associative,
the parser will declare a syntax error.
We will only use associativities with operators of the kind x * x -> x
for example, in the rules of the form expr: expr BINOP expr
in all other cases, we define two precedences if needed to resolve
conflicts.
The precedences must be listed from low to high.
*/
%nonassoc IN
%nonassoc below_SEMI
%nonassoc SEMI /* below EQUAL ({lbl=...; lbl=...}) */
%nonassoc LET /* above SEMI ( ...; let ... in ...) */
%nonassoc below_WITH
%nonassoc FUNCTION WITH /* below BAR (match ... with ...) */
%nonassoc AND /* above WITH (module rec A: SIG with ... and ...) */
%nonassoc THEN /* below ELSE (if ... then ...) */
%nonassoc ELSE /* (if ... then ... else ...) */
%nonassoc LESSMINUS /* below COLONEQUAL (lbl <- x := e) */
%right COLONEQUAL /* expr (e := e := e) */
%nonassoc AS
%left BAR /* pattern (p|p|p) */
%nonassoc below_COMMA
%left COMMA /* expr/expr_comma_list (e,e,e) */
%right MINUSGREATER /* function_type (t -> t -> t) */
%right OR BARBAR /* expr (e || e || e) */
%right AMPERSAND AMPERAMPER /* expr (e && e && e) */
%nonassoc below_EQUAL
%left INFIXOP0 EQUAL LESS GREATER /* expr (e OP e OP e) */
%right INFIXOP1 /* expr (e OP e OP e) */
%nonassoc below_LBRACKETAT
%nonassoc LBRACKETAT
%right COLONCOLON /* expr (e :: e :: e) */
%left INFIXOP2 PLUS PLUSDOT MINUS MINUSDOT PLUSEQ /* expr (e OP e OP e) */
%left PERCENT INFIXOP3 STAR /* expr (e OP e OP e) */
%right INFIXOP4 /* expr (e OP e OP e) */
%nonassoc prec_unary_minus prec_unary_plus /* unary - */
%nonassoc prec_constant_constructor /* cf. simple_expr (C versus C x) */
%nonassoc prec_constr_appl /* above AS BAR COLONCOLON COMMA */
%nonassoc below_HASH
%nonassoc HASH /* simple_expr/toplevel_directive */
%left HASHOP
%nonassoc below_DOT
%nonassoc DOT DOTOP
/* Finally, the first tokens of simple_expr are above everything else. */
%nonassoc BACKQUOTE BANG BEGIN CHAR FALSE FLOAT INT OBJECT
LBRACE LBRACELESS LBRACKET LBRACKETBAR LIDENT LPAREN
NEW PREFIXOP STRING TRUE UIDENT
LBRACKETPERCENT QUOTED_STRING_EXPR
/* Entry points */
/* Several start symbols are marked with AVOID so that they are not used by
[make generate-parse-errors]. The three start symbols that we keep are
[implementation], [use_file], and [toplevel_phrase]. The latter two are
of marginal importance; only [implementation] really matters, since most
states in the automaton are reachable from it. */
%start implementation /* for implementation files */
%type <Parsetree.structure> implementation
/* BEGIN AVOID */
%start interface /* for interface files */
%type <Parsetree.signature> interface
/* END AVOID */
%start toplevel_phrase /* for interactive use */
%type <Parsetree.toplevel_phrase> toplevel_phrase
%start use_file /* for the #use directive */
%type <Parsetree.toplevel_phrase list> use_file
/* BEGIN AVOID */
%start parse_module_type
%type <Parsetree.module_type> parse_module_type
%start parse_module_expr
%type <Parsetree.module_expr> parse_module_expr
%start parse_core_type
%type <Parsetree.core_type> parse_core_type
%start parse_expression
%type <Parsetree.expression> parse_expression
%start parse_pattern
%type <Parsetree.pattern> parse_pattern
%start parse_constr_longident
%type <Longident.t> parse_constr_longident
%start parse_val_longident
%type <Longident.t> parse_val_longident
%start parse_mty_longident
%type <Longident.t> parse_mty_longident
%start parse_mod_ext_longident
%type <Longident.t> parse_mod_ext_longident
%start parse_mod_longident
%type <Longident.t> parse_mod_longident
%start parse_any_longident
%type <Longident.t> parse_any_longident
/* END AVOID */
%%
/* macros */
%inline extra_str(symb): symb { extra_str $startpos $endpos $1 };
%inline extra_sig(symb): symb { extra_sig $startpos $endpos $1 };
%inline extra_cstr(symb): symb { extra_cstr $startpos $endpos $1 };
%inline extra_csig(symb): symb { extra_csig $startpos $endpos $1 };
%inline extra_def(symb): symb { extra_def $startpos $endpos $1 };
%inline extra_text(symb): symb { extra_text $startpos $endpos $1 };
%inline extra_rhs(symb): symb { extra_rhs_core_type $1 ~pos:$endpos($1) };
%inline mkrhs(symb): symb
{ mkrhs $1 $sloc }
;
%inline text_str(symb): symb
{ text_str $startpos @ [$1] }
%inline text_str_SEMISEMI: SEMISEMI
{ text_str $startpos }
%inline text_sig(symb): symb
{ text_sig $startpos @ [$1] }
%inline text_sig_SEMISEMI: SEMISEMI
{ text_sig $startpos }
%inline text_def(symb): symb
{ text_def $startpos @ [$1] }
%inline top_def(symb): symb
{ Ptop_def [$1] }
%inline text_cstr(symb): symb
{ text_cstr $startpos @ [$1] }
%inline text_csig(symb): symb
{ text_csig $startpos @ [$1] }
(* Using this %inline definition means that we do not control precisely
when [mark_rhs_docs] is called, but I don't think this matters. *)
%inline mark_rhs_docs(symb): symb
{ mark_rhs_docs $startpos $endpos;
$1 }
%inline op(symb): symb