-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathparser.mly
4767 lines (4368 loc) · 148 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. */
%{
open Asttypes
open Jane_asttypes
open Longident
open Parsetree
open Ast_helper
open Docstrings
open Docstrings.WithMenhir
module N_ary = Jane_syntax.N_ary_functions
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 ?attrs d = Pat.mk ~loc:(make_loc loc) ?attrs d
let mkexp ~loc ?attrs d = Exp.mk ~loc:(make_loc loc) ?attrs 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 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_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 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))
(* See commentary about ghost locations at the declaration of Location.t *)
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 ?attrs d = Typ.mk ~loc:(ghost_loc loc) ?attrs 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
let mkuminus ~oploc name arg =
match name, arg.pexp_desc with
| "-", Pexp_constant(Pconst_integer (n,m)) ->
Pexp_constant(Pconst_integer(neg_string n,m)), arg.pexp_attributes
| ("-" | "-."), Pexp_constant(Pconst_float (f, m)) ->
Pexp_constant(Pconst_float(neg_string f, m)), arg.pexp_attributes
| _ ->
Pexp_apply(mkoperator ~loc:oploc ("~" ^ name), [Nolabel, arg]), []
let mkuplus ~oploc name arg =
let desc = arg.pexp_desc in
match name, desc with
| "+", Pexp_constant(Pconst_integer _)
| ("+" | "+."), Pexp_constant(Pconst_float _) -> desc, arg.pexp_attributes
| _ ->
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
let local_ext_loc loc = mkloc "extension.local" loc
let unique_ext_loc loc = mkloc "extension.unique" loc
let once_ext_loc loc = mkloc "extension.once" loc
let local_attr loc =
mk_attr ~loc (local_ext_loc loc) (PStr [])
let unique_attr loc =
mk_attr ~loc (unique_ext_loc loc) (PStr [])
let once_attr loc =
mk_attr ~loc (once_ext_loc loc) (PStr [])
let local_extension loc =
Exp.mk (Pexp_extension(local_ext_loc loc, PStr []))
let unique_extension loc =
Exp.mk (Pexp_extension(unique_ext_loc loc, PStr []))
let once_extension loc =
Exp.mk (Pexp_extension(once_ext_loc loc, PStr []))
let mkexp_stack ~loc ~kwd_loc exp =
Exp.mk ~loc (Pexp_apply(local_extension kwd_loc, [Nolabel, exp]))
let mkexp_unique ~loc ~kwd_loc exp =
Exp.mk ~loc (Pexp_apply(unique_extension kwd_loc, [Nolabel, exp]))
let mkexp_once ~loc ~kwd_loc exp =
Exp.mk ~loc (Pexp_apply(once_extension kwd_loc, [Nolabel, exp]))
let mkpat_stack pat loc =
{pat with
ppat_attributes = local_attr (make_loc loc) :: pat.ppat_attributes}
let mkpat_unique pat loc =
{pat with
ppat_attributes = unique_attr (make_loc loc) :: pat.ppat_attributes}
let mkpat_once pat loc =
{pat with
ppat_attributes = once_attr (make_loc loc) :: pat.ppat_attributes}
let mktyp_stack typ loc =
{typ with
ptyp_attributes = local_attr (make_loc loc) :: typ.ptyp_attributes}
let mktyp_unique typ loc =
{typ with
ptyp_attributes = unique_attr (make_loc loc) :: typ.ptyp_attributes}
let mktyp_once typ loc =
{typ with
ptyp_attributes = once_attr (make_loc loc) :: typ.ptyp_attributes}
type mode_annotation = N_ary.mode_annotation =
| Local
| Unique
| Once
(** [loc] is the location to be used for the whole expression including the
extension node. The extension node will always have the location [kwd_loc]. *)
let exp_with_mode ~loc ~kwd_loc flag exp =
match flag with
| Local -> mkexp_stack exp ~loc ~kwd_loc
| Unique -> mkexp_unique exp ~loc ~kwd_loc
| Once -> mkexp_once exp ~loc ~kwd_loc
let exp_with_modes loc modes exp =
List.fold_left
(fun exp mode -> exp_with_mode mode.txt exp ~loc ~kwd_loc:mode.loc)
exp modes
let mkexp_with_mode loc (flag, kwd_loc) exp =
let loc = make_loc loc in
exp_with_mode ~loc ~kwd_loc:(make_loc kwd_loc) flag exp
(** [loc] is a location covering all the modes and the expression, and will be
used as for all the nested expressions. It is imprecise and taken as ghost. *)
let ghexp_with_modes loc modes exp =
let loc = ghost_loc loc in
let modes = List.map (fun (mode, loc) -> mkloc mode (make_loc loc)) modes in
exp_with_modes loc modes exp
let mkpat_with_mode = function
| Local -> mkpat_stack
| Unique -> mkpat_unique
| Once -> mkpat_once
let mkpat_with_modes flags pat =
List.fold_left (fun pat (flag, loc) -> mkpat_with_mode flag pat loc) pat flags
let mktyp_with_mode = function
| Local -> mktyp_stack
| Unique -> mktyp_unique
| Once -> mktyp_once
let mktyp_with_modes flags typ =
List.fold_left (fun typ (flag, loc) -> mktyp_with_mode flag typ loc) typ flags
let let_binding_mode_attrs mode_annots =
List.map
(fun (annot, loc) ->
let mk_attr =
match annot with
| Local -> local_attr
| Unique -> unique_attr
| Once -> once_attr
in
mk_attr (make_loc loc))
mode_annots
let exclave_ext_loc loc = mkloc "extension.exclave" loc
let exclave_extension loc =
Exp.mk ~loc:Location.none
(Pexp_extension(exclave_ext_loc loc, PStr []))
let mkexp_exclave ~loc ~kwd_loc exp =
ghexp ~loc (Pexp_apply(exclave_extension (make_loc kwd_loc), [Nolabel, exp]))
let curry_attr loc =
mk_attr ~loc:Location.none (mkloc "extension.curry" loc) (PStr [])
let is_curry_attr attr =
attr.attr_name.txt = "extension.curry"
let mktyp_curry typ loc =
{typ with ptyp_attributes = curry_attr loc :: typ.ptyp_attributes}
let maybe_curry_typ typ loc =
match typ.ptyp_desc with
| Ptyp_arrow _ ->
if List.exists is_curry_attr typ.ptyp_attributes then typ
else mktyp_curry typ (make_loc loc)
| _ -> typ
let global_loc loc = mkloc "extension.global" loc
let global_attr loc =
mk_attr ~loc:loc (global_loc loc) (PStr [])
let mkld_global ld loc =
{ ld with pld_attributes = global_attr loc :: ld.pld_attributes }
let mkld_global_maybe gbl ld loc =
match gbl with
| Global -> mkld_global ld loc
| Nothing -> ld
let mkcty_global cty loc =
{ cty with ptyp_attributes = global_attr loc :: cty.ptyp_attributes }
let mkcty_global_maybe gbl cty loc =
match gbl with
| Global -> mkcty_global cty loc
| Nothing -> cty
(* 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
| N_ary.Pconstraint t -> Pexp_constraint(e, t)
| N_ary.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)))
(* Normal mutable arrays and immutable arrays are parsed identically, just with
different delimiters. The parsing is done by the [array_exprs] rule, and the
[Generic_array] module provides (1) a type representing the possible results,
and (2) a function for going from that type to an AST fragment representing
an array. *)
module Generic_array = struct
(** The possible ways of parsing an array (writing [[? ... ?]] for either
[[| ... |]] or [[: ... :]]). The set of available constructs differs
between expressions and patterns.
*)
module Simple = struct
type 'a t =
| Literal of 'a list
(** A plain array literal/pattern, [[? x; y; z ?]] *)
| Unclosed of (Lexing.position * Lexing.position) *
(Lexing.position * Lexing.position)
(** Parse error: an unclosed array literal, [\[? x; y; z] with no closing
[?\]]. *)
let to_ast (open_ : string) (close : string) array t =
match t with
| Literal elts -> array elts
| Unclosed (startpos, endpos) -> unclosed open_ startpos close endpos
end
module Expression = struct
type t =
| Simple of expression Simple.t
| Opened_literal of open_declaration *
Lexing.position *
Lexing.position *
expression list
(** An array literal with a local open, [Module.[? x; y; z ?]] (only valid
in expressions) *)
let to_desc (open_ : string) (close : string) array t =
match t with
| Simple x -> Simple.to_ast open_ close array x
| Opened_literal (od, startpos, endpos, elts) ->
Pexp_open (od, mkexp ~loc:(startpos, endpos) (array elts))
let to_expression (open_ : string) (close : string) array ~loc t =
match t with
| Simple x -> Simple.to_ast open_ close (array ~loc) x
| Opened_literal (od, startpos, endpos, elts) ->
mkexp ~loc (Pexp_open (od, array ~loc:(startpos, endpos) elts))
end
module Pattern = struct
type t = pattern Simple.t
let to_ast open_ close array (t : t) =
Simple.to_ast open_ close array t
end
end
let ppat_iarray loc elts =
Jane_syntax.Immutable_arrays.pat_of
~loc:(make_loc loc)
(Iapat_immutable_array elts)
let expecting_loc (loc : Location.t) (nonterm : string) =
raise Syntaxerr.(Error(Expecting(loc, nonterm)))
let expecting (loc : Lexing.position * Lexing.position) nonterm =
expecting_loc (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 exp =
match Jane_syntax.Expression.of_ast exp with
| Some _ -> [exp]
| None -> match exp with
{ pexp_desc = Pexp_tuple explist; pexp_loc = _ } -> explist
| exp -> [exp]
(* Immutable array indexing is a regular operator, so it doesn't need a special
case here *)
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)))
let make_ghost x =
if x.loc.loc_ghost
then x (* Save an allocation *)
else { x with loc = Location.ghostify x.loc }
let loc_last (id : Longident.t Location.loc) : string Location.loc =
Location.map Longident.last id
let loc_lident (id : string Location.loc) : Longident.t Location.loc =
Location.map (fun x -> Lident x) id
let exp_of_longident lid =
let lid = Location.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 mk_one (name, jkind) exp =
match jkind with
| None -> mkexp ~loc (Pexp_newtype (name, exp))
| Some jkind ->
Jane_syntax.Layouts.expr_of ~loc:(make_loc loc)
(Lexp_newtype (name, jkind, exp))
in
List.fold_right mk_one newtypes exp
(* The [typloc] argument is used to adjust a location for something we're
parsing a bit differently than upstream. See comment about [Pvc_constraint]
in [let_binding_body_no_punning]. *)
let wrap_type_annotation ~loc ?(typloc=loc) newtypes core_type body =
let mk_newtypes = mk_newtypes ~loc in
let exp = mkexp ~loc (Pexp_constraint(body,core_type)) in
let exp = mk_newtypes newtypes exp in
let inner_type = Typ.varify_constructors (List.map fst newtypes) core_type in
let ltyp =
Jane_syntax.Layouts.Ltyp_poly { bound_vars = newtypes; inner_type }
in
(exp,
Jane_syntax.Layouts.type_of
~loc:(Location.ghostify (make_loc typloc)) ltyp)
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 ext_attrs =
wrap_exp_attrs ~loc (mkexp ~loc d) ext_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 e = ghexp ~loc (Pexp_constant (Pconst_string (str, strloc, delim))) 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 open N_ary in
let is_newtype { pparam_desc; _ } =
match pparam_desc with
| Pparam_newtype _ -> true
| Pparam_val _ -> false
in
let as_newtype { pparam_desc; _ } =
match pparam_desc with
| Pparam_newtype (x, jkind) -> Some (x, jkind)
| 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 ~loc =
let wrapped_body =
match body_constraint with
| None -> body
| Some { N_ary.type_constraint; mode_annotations } ->
let loc = { body.pexp_loc with loc_ghost = true } in
let body = Exp.mk (mkexp_desc_constraint body type_constraint) ~loc in
exp_with_modes loc mode_annotations body
in
mk_newtypes ~loc newtypes wrapped_body
let n_ary_function expr ~attrs ~loc =
wrap_exp_attrs ~loc (N_ary.expr_of expr ~loc:(make_loc loc)) attrs
let mkfunction ~loc ~attrs params body_constraint body =
match body with
| N_ary.Pfunction_cases _ ->
n_ary_function (params, body_constraint, body) ~loc ~attrs
| N_ary.Pfunction_body body_exp -> begin
(* If all the params are newtypes, then we don't create a function node;
we create a newtype node. *)
match all_params_as_newtypes params with
| None -> n_ary_function (params, body_constraint, body) ~loc ~attrs
| Some newtypes ->
wrap_exp_attrs
~loc
(mkghost_newtype_function_body newtypes body_constraint body_exp
~loc)
attrs
end
(* 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 "parametrized types are not supported";
if ptyp.ptype_cstrs <> [] then
err loc "constrained types are not supported";
if ptyp.ptype_private <> Public then
err loc "private types are not supported";
(* 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 "only 'with type t =' constraints are supported"
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
"only module type identifier and 'with type' constraints are supported"
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;
}
let check_jkind ~loc id : const_jkind =
match id with
| "any" -> Any
| "value" -> Value
| "void" -> Void
| "immediate64" -> Immediate64
| "immediate" -> Immediate
| "float64" -> Float64
| _ -> expecting_loc loc "layout"
(* Unboxed literals *)
(* CR layouts v2.5: The [unboxed_*] functions will both be improved and lose
their explicit assert once we have real unboxed literals in Jane syntax; they
may also get re-inlined at that point *)
let unboxed_literals_extension = Language_extension.Layouts
module Constant : sig
type t = private
| Value of constant
| Unboxed of Jane_syntax.Layouts.constant
type loc := Lexing.position * Lexing.position
val value : Parsetree.constant -> t
val unboxed : loc:loc -> Jane_syntax.Layouts.constant -> t
val to_expression : loc:loc -> t -> expression
val to_pattern : loc:loc -> t -> pattern
end = struct
type t =
| Value of constant
| Unboxed of Jane_syntax.Layouts.constant
let value x = Value x
let assert_unboxed_literals ~loc =
Language_extension.(
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Beta)
let unboxed ~loc x =
assert_unboxed_literals ~loc:(make_loc loc);
Unboxed x
let to_expression ~loc : t -> expression = function
| Value const_value ->
mkexp ~loc (Pexp_constant const_value)
| Unboxed const_unboxed ->
Jane_syntax.Layouts.expr_of ~loc:(make_loc loc)
(Lexp_constant const_unboxed)
let to_pattern ~loc : t -> pattern = function
| Value const_value ->
mkpat ~loc (Ppat_constant const_value)
| Unboxed const_unboxed ->
Jane_syntax.Layouts.pat_of
~loc:(make_loc loc) (Lpat_constant const_unboxed)
end
type sign = Positive | Negative
let with_sign sign num =
match sign with
| Positive -> num
| Negative -> "-" ^ num
let unboxed_int sloc int_loc sign (n, m) =
match m with
| Some m ->
Constant.unboxed ~loc:int_loc (Integer (with_sign sign n, m))
| None ->
if Language_extension.is_enabled unboxed_literals_extension then
expecting int_loc "unboxed integer literal with type-specifying suffix"
else
not_expecting sloc "line number directive"
let unboxed_float sloc sign (f, m) =
Constant.unboxed ~loc:sloc (Float (with_sign sign f, m))
(* Unboxed float type *)
let assert_unboxed_float_type ~loc =
Language_extension.(
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Beta)