-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathparser.mly
5262 lines (4826 loc) · 164 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
open Parser_types
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 ghexpvar ~loc name =
ghexp ~loc (Pexp_ident (ghrhs (Lident name) loc))
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 =
let result =
match arg.pexp_desc with
| Pexp_constant const -> begin
match name, const with
| "-", Pconst_integer (n, m) ->
Some (Pconst_integer (neg_string n, m))
| "-", Pconst_unboxed_integer (n, m) ->
Some (Pconst_unboxed_integer (neg_string n, m))
| ("-" | "-."), Pconst_float (f, m) ->
Some (Pconst_float (neg_string f, m))
| ("-" | "-."), Pconst_unboxed_float (f, m) ->
Some (Pconst_unboxed_float (neg_string f, m))
| _, _ -> None
end
| _ -> None
in
match result with
| Some desc -> Pexp_constant desc, arg.pexp_attributes
| None ->
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 _ | Pconst_unboxed_integer _)
| ("+" | "+."), Pexp_constant (Pconst_float _ | Pconst_unboxed_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 mkpat_with_modes ~loc ~pat ~cty ~modes =
match pat.ppat_desc with
| Ppat_constraint (pat', cty', modes') ->
begin match cty, cty' with
| Some _, None ->
{ pat with
ppat_desc = Ppat_constraint (pat', cty, modes @ modes');
ppat_loc = make_loc loc
}
| None, _ ->
{ pat with
ppat_desc = Ppat_constraint (pat', cty', modes @ modes');
ppat_loc = make_loc loc
}
| _ ->
mkpat ~loc (Ppat_constraint (pat, cty, modes))
end
| _ ->
begin match cty, modes with
| None, [] -> pat
| cty, modes -> mkpat ~loc (Ppat_constraint (pat, cty, modes))
end
let ghpat_with_modes ~loc ~pat ~cty ~modes =
let pat = mkpat_with_modes ~loc ~pat ~cty ~modes in
{ pat with ppat_loc = { pat.ppat_loc with loc_ghost = true }}
let mkexp_constraint ~loc ~exp ~cty ~modes =
match exp.pexp_desc with
| Pexp_constraint (exp', cty', modes') ->
begin match cty, cty' with
| cty, None | None, cty ->
{ exp with
pexp_desc = Pexp_constraint (exp', cty, modes @ modes');
pexp_loc = make_loc loc
}
| _ ->
mkexp ~loc (Pexp_constraint (exp, cty, modes))
end
| _ ->
begin match cty, modes with
| None, [] -> exp
| cty, modes -> mkexp ~loc (Pexp_constraint (exp, cty, modes))
end
let ghexp_constraint ~loc ~exp ~cty ~modes =
let exp = mkexp_constraint ~loc ~exp ~cty ~modes in
{ exp with pexp_loc = { exp.pexp_loc with loc_ghost = true }}
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 mktyp_curry typ loc =
{typ with ptyp_attributes =
Builtin_attributes.curry_attr loc :: typ.ptyp_attributes}
let maybe_curry_typ typ loc =
match typ.ptyp_desc with
| Ptyp_arrow _ ->
if Builtin_attributes.has_curry typ.ptyp_attributes then typ
else mktyp_curry typ (make_loc loc)
| _ -> typ
(* 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 [None, e1; None, 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 ([None, p1; None, ghpat ~loc:el_loc pat_pl], Closed)) in
ghpat_cons_desc loc arg, loc
let mkstrexp e attrs =
{ pstr_desc = Pstr_eval (e, attrs); pstr_loc = e.pexp_loc }
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) mut t =
let array elts = Pexp_array (mut, elts) in
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) mut ~loc t =
let array ~loc elts = mkexp ~loc (Pexp_array (mut, elts)) in
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 mut (t : t) =
Simple.to_ast open_ close (fun elts -> Ppat_array (mut, elts)) t
end
end
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)))
let mkexp_type_constraint_with_modes ?(ghost=false) ~loc ~modes e t =
match t with
| Pconstraint t ->
let mk = if ghost then ghexp_constraint else mkexp_constraint in
mk ~loc ~exp:e ~cty:(Some t) ~modes
| Pcoerce(t1, t2) ->
match modes with
| [] ->
let mk = if ghost then ghexp else mkexp ?attrs:None in
mk ~loc (Pexp_coerce(e, t1, t2))
| _ :: _ -> not_expecting loc "mode annotations"
let mkexp_opt_type_constraint_with_modes ?ghost ~loc ~modes e = function
| None -> e
| Some c -> mkexp_type_constraint_with_modes ?ghost ~loc ~modes e c
(* 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 exp.pexp_desc with
| Pexp_tuple explist when
List.for_all (function None, _ -> true | _ -> false) explist ->
List.map (fun (_, e) -> e) explist
| _ -> [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 (Mutable, 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 (Mutable, 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 =
ghexp ~loc (Pexp_newtype (name, jkind, exp))
in
let exp = List.fold_right mk_one newtypes exp in
(* outermost expression should have non-ghost location *)
{ exp with pexp_loc = make_loc loc }
(* 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) ~modes newtypes core_type body =
let mk_newtypes = mk_newtypes ~loc in
let exp = mkexp_constraint ~loc ~exp:body ~cty:(Some core_type) ~modes in
let exp = mk_newtypes newtypes exp in
let inner_type = Typ.varify_constructors (List.map fst newtypes) core_type in
(exp, ghtyp ~loc:typloc (Ptyp_poly (newtypes, inner_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 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 {psg_items=[body];
psg_modalities=[]; psg_loc=make_loc loc}), []))
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 }
let mklb first ~loc (p, e, typ, modes, is_pun) attrs =
{
lb_pattern = p;
lb_expression = e;
lb_constraint=typ;
lb_is_pun = is_pun;
lb_modes = modes;
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
~modes:lb.lb_modes
~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
~modes:lb.lb_modes
?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
~modes:lb.lb_modes
?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; _ } =
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
let empty_body_constraint =
{ ret_type_constraint = None; mode_annotations = []; ret_mode_annotations = []}
(* 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 =
let { ret_type_constraint; mode_annotations; ret_mode_annotations } =
body_constraint
in
let modes = mode_annotations @ ret_mode_annotations in
let {Location.loc_start; loc_end} = body.pexp_loc in
let loc = loc_start, loc_end in
mkexp_opt_type_constraint_with_modes ~ghost:true ~loc ~modes body ret_type_constraint
in
mk_newtypes ~loc newtypes wrapped_body
let mkfunction ~loc ~attrs params body_constraint body =
match body with
| Pfunction_cases _ ->
mkexp_attrs (Pexp_function (params, body_constraint, body)) attrs ~loc
| Pfunction_body body_exp -> begin
(* 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 ->
mkexp_attrs (Pexp_function (params, body_constraint, body)) attrs ~loc
| Some newtypes ->
wrap_exp_attrs
~loc
(mkghost_newtype_function_body newtypes body_constraint body_exp
~loc)
attrs
end
let mk_functor_typ args mty_mm =
let mty, _ =
List.fold_left (fun (mty, mm) (startpos, arg) ->
let mty =
mkmty ~loc:(startpos, mty.pmty_loc.loc_end) (Pmty_functor (arg, mty, mm))
in
let mm = [] in
mty, mm)
mty_mm args
in
mty
(* 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; pmty_attributes = inner_attributes}, cstrs)} ->
begin match inner_attributes with
| [] -> ()
| attr :: _ ->
err attr.attr_loc Syntaxerr.Misplaced_attribute
end;
(lid, List.map map_cstr cstrs, pmty.pmty_attributes)
| _ ->
err pmty.pmty_loc Neither_identifier_nor_with_type
(* There's no dedicated syntax for module instances. Functor application
syntax is translated into a module instance expression.
*)
let pmod_instance : module_expr -> module_expr_desc =
let raise_malformed_instance loc =
raise (Syntaxerr.Error (Malformed_instance_identifier loc))
in
let head_of_ident (lid : Longident.t Location.loc) =
match lid with
| { txt = Lident s; loc = _ } -> s
| { txt = _; loc } -> raise_malformed_instance loc
in
let gather_args mexpr =
let rec loop mexpr acc =
match mexpr with
| { pmod_desc = Pmod_apply (f, v); _ } ->
(match f.pmod_desc with
| Pmod_apply (f, n) -> loop f ((n, v) :: acc)
| _ -> raise_malformed_instance f.pmod_loc)
| head -> head, acc
in
loop mexpr []
in
let string_of_module_expr mexpr =
match mexpr.pmod_desc with
| Pmod_ident i -> head_of_ident i
| _ -> raise_malformed_instance mexpr.pmod_loc
in
let rec instance_of_module_expr mexpr =
match gather_args mexpr with
| { pmod_desc = Pmod_ident i; _ }, args ->
let head = head_of_ident i in
let args = List.map instances_of_arg_pair args in
{ pmod_instance_head = head; pmod_instance_args = args }
| { pmod_loc; _ }, _ -> raise_malformed_instance pmod_loc
and instances_of_arg_pair (n, v) =
string_of_module_expr n, instance_of_module_expr v
in
fun mexpr -> Pmod_instance (instance_of_module_expr mexpr)
;;
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;
}
(* 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
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 -> Pconst_unboxed_integer (with_sign sign n, m)
| None ->
if Language_extension.is_enabled unboxed_literals_extension then
raise
Syntaxerr.(Error(Missing_unboxed_literal_suffix (make_loc int_loc)))
else
not_expecting sloc "line number directive"
let unboxed_float sign (f, m) = Pconst_unboxed_float (with_sign sign f, m)
(* Invariant: [lident] must end with an [Lident] that ends with a ["#"]. *)
let unboxed_type sloc lident tys =
let loc = make_loc sloc in
Ptyp_constr (mkloc lident loc, tys)
let maybe_pmod_constraint mode expr =
match mode with
| [] -> expr
| _ :: _ -> Mod.constraint_ None mode expr
%}
/* 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 COLONRBRACKET ":]"
%token COMMA ","
%token CONSTRAINT "constraint"
%token DO "do"
%token DONE "done"
%token DOT "."
%token DOTDOT ".."
%token DOTHASH ".#"
%token DOWNTO "downto"
%token ELSE "else"
%token END "end"
%token EOF ""
%token EQUAL "="
%token EXCEPTION "exception"
%token EXCLAVE "exclave_"
%token EXTERNAL "external"
%token FALSE "false"
%token <string * char option> FLOAT "42.0" (* just an example *)
%token <string * char option> HASH_FLOAT "#42.0" (* just an example *)
%token FOR "for"
%token FUN "fun"
%token FUNCTION "function"
%token FUNCTOR "functor"
%token GLOBAL "global_"
%token GREATER ">"
%token GREATERRBRACE ">}"
%token GREATERRBRACKET ">]"
%token HASHLPAREN "#("
%token HASHLBRACE "#{"
%token IF "if"
%token IN "in"
%token INCLUDE "include"
%token <string> INFIXOP0 "!=" (* just an example *)
%token AT "@" (* mode expression *)
%token ATAT "@@" (* mode expression *)
%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 * char option> HASH_INT "#42l" (* just an example *)
%token KIND_ABBREV "kind_abbrev_"
%token KIND_OF "kind_of_"
%token <string> LABEL "~label:" (* just an example *)
%token LAZY "lazy"