-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxParser.mly
1177 lines (1089 loc) · 39.4 KB
/
xParser.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
/* CR jfuruse: This file is already diverted from the original enough.
We need clean-up
*/
/***********************************************************************/
/* */
/* 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 Q Public License version 1.0. */
/* */
/***********************************************************************/
/* The parser definition */
%{
open Location
open Asttypes
open Longident
open Parsetree
let mktyp d =
{ ptyp_desc = d; ptyp_loc = symbol_rloc() }
let mkpat d =
{ ppat_desc = d; ppat_loc = symbol_rloc() }
let mkmty d =
{ pmty_desc = d; pmty_loc = symbol_rloc() }
let mksig d =
{ psig_desc = d; psig_loc = symbol_rloc() }
let mkfield d =
{ pfield_desc = d; pfield_loc = symbol_rloc() }
let mkcty d =
{ pcty_desc = d; pcty_loc = symbol_rloc() }
let mkctf d =
{ pctf_desc = d; pctf_loc = symbol_rloc () }
let mkrhs rhs pos = mkloc rhs (rhs_loc pos)
let mkoption d =
let loc = {d.ptyp_loc with loc_ghost = true} in
{ ptyp_desc = Ptyp_constr(mkloc (Ldot (Lident "*predef*", "option")) loc,[d]);
ptyp_loc = loc}
let reloc_pat x = { x with ppat_loc = symbol_rloc () };;
(*
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 ghpat d = { ppat_desc = d; ppat_loc = symbol_gloc () };;
let ghtyp d = { ptyp_desc = d; ptyp_loc = symbol_gloc () };;
let mkpat_cons consloc args loc =
{ppat_desc = Ppat_construct(mkloc (Lident "::") consloc, Some args, false);
ppat_loc = loc}
let rec mktailpat nilloc = function
[] ->
let loc = { nilloc with loc_ghost = true } in
let nil = { txt = Lident "[]"; loc = loc } in
{ ppat_desc = Ppat_construct (nil, None, false); ppat_loc = loc }
| p1 :: pl ->
let pat_pl = mktailpat nilloc pl in
let l = {loc_start = p1.ppat_loc.loc_start;
loc_end = pat_pl.ppat_loc.loc_end;
loc_ghost = true}
in
let arg = {ppat_desc = Ppat_tuple [p1; pat_pl]; ppat_loc = l} in
mkpat_cons {l with loc_ghost = true} arg l
let rec deep_mkrangepat c1 c2 =
if c1 = c2 then ghpat(Ppat_constant(Const_char c1)) else
ghpat(Ppat_or(ghpat(Ppat_constant(Const_char c1)),
deep_mkrangepat (Char.chr(Char.code c1 + 1)) c2))
let rec mkrangepat c1 c2 =
if c1 > c2 then mkrangepat c2 c1 else
if c1 = c2 then mkpat(Ppat_constant(Const_char c1)) else
reloc_pat (deep_mkrangepat c1 c2)
let syntax_error () =
raise Syntaxerr.Escape_error
let unclosed opening_name opening_num closing_name closing_num =
raise(Syntaxerr.Error(Syntaxerr.Unclosed(rhs_loc opening_num, opening_name,
rhs_loc closing_num, closing_name)))
let expecting pos nonterm =
raise Syntaxerr.(Error(Expecting(rhs_loc pos, nonterm)))
let lapply p1 p2 =
if !Clflags.applicative_functors
then Lapply(p1, p2)
else raise (Syntaxerr.Error(Syntaxerr.Applicative_path (symbol_rloc())))
let pat_of_label lbl pos =
mkpat (Ppat_var (mkrhs (Longident.last lbl) pos))
%}
/* Tokens */
%token AMPERAMPER
%token AMPERSAND
%token AND
%token AS
%token ASSERT
%token BACKQUOTE
%token BANG
%token BAR
%token BARBAR
%token BARRBRACKET
%token BEGIN
%token <char> CHAR
%token CLASS
%token COLON
%token COLONCOLON
%token COLONEQUAL
%token COLONGREATER
%token COMMA
%token CONSTRAINT
%token DO
%token DONE
%token DOT
%token DOTDOT
%token DOWNTO
%token ELSE
%token END
%token EOF
%token EQUAL
%token EXCEPTION
%token EXTERNAL
%token FALSE
%token <string> FLOAT
%token FOR
%token FUN
%token FUNCTION
%token FUNCTOR
%token GREATER
%token GREATERRBRACE
%token GREATERRBRACKET
%token IF
%token IN
%token INCLUDE
%token <string> INFIXOP0
%token <string> INFIXOP1
%token <string> INFIXOP2
%token <string> INFIXOP3
%token <string> INFIXOP4
%token INHERIT
%token INITIALIZER
%token <int> INT
%token <int32> INT32
%token <int64> INT64
%token <string> LABEL
%token LAZY
%token LBRACE
%token LBRACELESS
%token LBRACKET
%token LBRACKETBAR
%token LBRACKETLESS
%token LBRACKETGREATER
%token LESS
%token LESSMINUS
%token LET
%token <string> LIDENT
%token LPAREN
%token MATCH
%token METHOD
%token MINUS
%token MINUSDOT
%token MINUSGREATER
%token MODULE
%token MUTABLE
%token <nativeint> NATIVEINT
%token NEW
%token OBJECT
%token OF
%token OPEN
%token <string> OPTLABEL
%token OR
/* %token PARSER */
%token PLUS
%token PLUSDOT
%token <string> PREFIXOP
%token PRIVATE
%token QUESTION
%token QUOTE
%token RBRACE
%token RBRACKET
%token REC
%token RPAREN
%token SEMI
%token SEMISEMI
%token SHARP
%token SIG
%token STAR
%token <string> STRING
%token STRUCT
%token THEN
%token TILDE
%token TO
%token TRUE
%token TRY
%token TYPE
%token <string> UIDENT
%token UNDERSCORE
%token VAL
%token VIRTUAL
%token WHEN
%token WHILE
%token WITH
%token <string * Location.t> COMMENT
/* 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 /* core_type2 (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) */
%right COLONCOLON /* expr (e :: e :: e) */
%left INFIXOP2 PLUS PLUSDOT MINUS MINUSDOT /* expr (e OP e OP e) */
%left 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_SHARP
%nonassoc SHARP /* simple_expr/toplevel_directive */
%nonassoc below_DOT
%nonassoc DOT
/* Finally, the first tokens of simple_expr are above everything else. */
%nonassoc BACKQUOTE BANG BEGIN CHAR FALSE FLOAT INT INT32 INT64
LBRACE LBRACELESS LBRACKET LBRACKETBAR LIDENT LPAREN
NEW NATIVEINT PREFIXOP STRING TRUE UIDENT
/* Entry points */
%start interface /* for interface files */
%type <Parsetree.signature> interface
%start any_longident
%type <Longident.t> any_longident
%start pattern_longident
%type <Longident.t> pattern_longident
%start poly_type
%type <Parsetree.core_type> poly_type
%%
/* Entry points */
interface:
signature EOF { List.rev $1 }
;
/* Module types */
module_type:
mty_longident
{ mkmty(Pmty_ident (mkrhs $1 1)) }
| SIG signature END
{ mkmty(Pmty_signature(List.rev $2)) }
| SIG signature error
{ unclosed "sig" 1 "end" 3 }
| FUNCTOR LPAREN UIDENT COLON module_type RPAREN MINUSGREATER module_type
%prec below_WITH
{ mkmty(Pmty_functor(mkrhs $3 3, $5, $8)) }
| module_type WITH with_constraints
{ mkmty(Pmty_with($1, List.rev $3)) }
/*
| MODULE TYPE OF module_expr
{ mkmty(Pmty_typeof $4) }
*/
| LPAREN module_type RPAREN
{ $2 }
| LPAREN module_type error
{ unclosed "(" 1 ")" 3 }
;
signature:
/* empty */ { [] }
| signature signature_item { $2 :: $1 }
| signature signature_item SEMISEMI { $2 :: $1 }
;
signature_item:
VAL val_ident COLON core_type
{ mksig(Psig_value(mkrhs $2 2, {pval_type = $4; pval_prim = [];
pval_loc = symbol_rloc()})) }
/*
| EXTERNAL val_ident COLON core_type EQUAL primitive_declaration
{ mksig(Psig_value(mkrhs $2 2, {pval_type = $4; pval_prim = $6;
pval_loc = symbol_rloc()})) }
| TYPE type_declarations
{ mksig(Psig_type(List.rev $2)) }
| EXCEPTION UIDENT constructor_arguments
{ mksig(Psig_exception(mkrhs $2 2, $3)) }
| MODULE UIDENT module_declaration
{ mksig(Psig_module(mkrhs $2 2, $3)) }
| MODULE REC module_rec_declarations
{ mksig(Psig_recmodule(List.rev $3)) }
| MODULE TYPE ident
{ mksig(Psig_modtype(mkrhs $3 3, Pmodtype_abstract)) }
| MODULE TYPE ident EQUAL module_type
{ mksig(Psig_modtype(mkrhs $3 3, Pmodtype_manifest $5)) }
| OPEN override_flag mod_longident
{ mksig(Psig_open ($2, mkrhs $3 3)) }
| INCLUDE module_type
{ mksig(Psig_include $2) }
| CLASS class_descriptions
{ mksig(Psig_class (List.rev $2)) }
| CLASS TYPE class_type_declarations
{ mksig(Psig_class_type (List.rev $3)) }
*/
;
module_declaration:
COLON module_type
{ $2 }
| LPAREN UIDENT COLON module_type RPAREN module_declaration
{ mkmty(Pmty_functor(mkrhs $2 2, $4, $6)) }
;
module_rec_declarations:
module_rec_declaration { [$1] }
| module_rec_declarations AND module_rec_declaration { $3 :: $1 }
;
module_rec_declaration:
UIDENT COLON module_type { (mkrhs $1 1, $3) }
;
/* Class expressions */
class_type_parameters:
/*empty*/ { [], symbol_gloc () }
| LBRACKET type_parameter_list RBRACKET { List.rev $2, symbol_rloc () }
;
parent_binder:
AS LIDENT
{ Some $2 }
| /* empty */
{ None }
;
virtual_value:
override_flag MUTABLE VIRTUAL label COLON core_type
{ if $1 = Override then syntax_error ();
mkloc $4 (rhs_loc 4), Mutable, $6 }
| VIRTUAL mutable_flag label COLON core_type
{ mkrhs $3 3, $2, $5 }
;
virtual_method:
METHOD override_flag PRIVATE VIRTUAL label COLON poly_type
{ if $2 = Override then syntax_error ();
mkloc $5 (rhs_loc 5), Private, $7 }
| METHOD override_flag VIRTUAL private_flag label COLON poly_type
{ if $2 = Override then syntax_error ();
mkloc $5 (rhs_loc 5), $4, $7 }
;
/* Class types */
class_type:
class_signature
{ $1 }
| QUESTION LIDENT COLON simple_core_type_or_tuple MINUSGREATER class_type
{ mkcty(Pcty_fun("?" ^ $2 , mkoption $4, $6)) }
| OPTLABEL simple_core_type_or_tuple MINUSGREATER class_type
{ mkcty(Pcty_fun("?" ^ $1, mkoption $2, $4)) }
| LIDENT COLON simple_core_type_or_tuple MINUSGREATER class_type
{ mkcty(Pcty_fun($1, $3, $5)) }
| simple_core_type_or_tuple MINUSGREATER class_type
{ mkcty(Pcty_fun("", $1, $3)) }
;
class_signature:
LBRACKET core_type_comma_list RBRACKET clty_longident
{ mkcty(Pcty_constr (mkloc $4 (rhs_loc 4), List.rev $2)) }
| clty_longident
{ mkcty(Pcty_constr (mkrhs $1 1, [])) }
| OBJECT class_sig_body END
{ mkcty(Pcty_signature $2) }
| OBJECT class_sig_body error
{ unclosed "object" 1 "end" 3 }
;
class_sig_body:
class_self_type class_sig_fields
{ { pcsig_self = $1; pcsig_fields = List.rev $2;
pcsig_loc = symbol_rloc(); } }
;
class_self_type:
LPAREN core_type RPAREN
{ $2 }
| /* empty */
{ mktyp(Ptyp_any) }
;
class_sig_fields:
/* empty */ { [] }
| class_sig_fields class_sig_field { $2 :: $1 }
;
class_sig_field:
INHERIT class_signature { mkctf (Pctf_inher $2) }
| VAL value_type { mkctf (Pctf_val $2) }
| virtual_method_type { mkctf (Pctf_virt $1) }
| method_type { mkctf (Pctf_meth $1) }
| CONSTRAINT constrain_field { mkctf (Pctf_cstr $2) }
;
value_type:
VIRTUAL mutable_flag label COLON core_type
{ $3, $2, Virtual, $5 }
| MUTABLE virtual_flag label COLON core_type
{ $3, Mutable, $2, $5 }
| label COLON core_type
{ $1, Immutable, Concrete, $3 }
;
method_type:
METHOD private_flag label COLON poly_type
{ $3, $2, $5 }
;
virtual_method_type:
METHOD PRIVATE VIRTUAL label COLON poly_type
{ $4, Private, $6 }
| METHOD VIRTUAL private_flag label COLON poly_type
{ $4, $3, $6 }
;
constrain:
core_type EQUAL core_type { $1, $3, symbol_rloc() }
;
constrain_field:
core_type EQUAL core_type { $1, $3 }
;
class_descriptions:
class_descriptions AND class_description { $3 :: $1 }
| class_description { [$1] }
;
class_description:
virtual_flag class_type_parameters LIDENT COLON class_type
{ let params, variance = List.split (fst $2) in
{pci_virt = $1; pci_params = params, snd $2;
pci_name = mkrhs $3 3; pci_expr = $5; pci_variance = variance;
pci_loc = symbol_rloc ()} }
;
class_type_declarations:
class_type_declarations AND class_type_declaration { $3 :: $1 }
| class_type_declaration { [$1] }
;
class_type_declaration:
virtual_flag class_type_parameters LIDENT EQUAL class_signature
{ let params, variance = List.split (fst $2) in
{pci_virt = $1; pci_params = params, snd $2;
pci_name = mkrhs $3 3; pci_expr = $5; pci_variance = variance;
pci_loc = symbol_rloc ()} }
;
/* Core expressions */
pattern_var:
LIDENT { mkpat(Ppat_var (mkrhs $1 1)) }
| UNDERSCORE { mkpat Ppat_any }
;
label_let_pattern:
label_var
{ $1 }
| label_var COLON core_type
{ let (lab, pat) = $1 in (lab, mkpat(Ppat_constraint(pat, $3))) }
;
label_var:
LIDENT { ($1, mkpat(Ppat_var (mkrhs $1 1))) }
;
let_pattern:
pattern
{ $1 }
| pattern COLON core_type
{ mkpat(Ppat_constraint($1, $3)) }
;
lident_list:
LIDENT { [$1] }
| LIDENT lident_list { $1 :: $2 }
;
type_constraint:
COLON core_type { (Some $2, None) }
| COLON core_type COLONGREATER core_type { (Some $2, Some $4) }
| COLONGREATER core_type { (None, Some $2) }
| COLON error { syntax_error() }
| COLONGREATER error { syntax_error() }
;
/* Patterns */
pattern:
simple_pattern
{ $1 }
| pattern AS val_ident
{ mkpat(Ppat_alias($1, mkrhs $3 3)) }
| pattern AS error
{ expecting 3 "identifier" }
| pattern_comma_list %prec below_COMMA
{ mkpat(Ppat_tuple(List.rev $1)) }
| constr_longident pattern %prec prec_constr_appl
{ mkpat(Ppat_construct(mkrhs $1 1, Some $2, false)) }
| name_tag pattern %prec prec_constr_appl
{ mkpat(Ppat_variant($1, Some $2)) }
| pattern COLONCOLON pattern
{ mkpat_cons (rhs_loc 2) (ghpat(Ppat_tuple[$1;$3])) (symbol_rloc()) }
| pattern COLONCOLON error
{ expecting 3 "pattern" }
| LPAREN COLONCOLON RPAREN LPAREN pattern COMMA pattern RPAREN
{ mkpat_cons (rhs_loc 2) (ghpat(Ppat_tuple[$5;$7])) (symbol_rloc()) }
| LPAREN COLONCOLON RPAREN LPAREN pattern COMMA pattern error
{ unclosed "(" 4 ")" 8 }
| pattern BAR pattern
{ mkpat(Ppat_or($1, $3)) }
| pattern BAR error
{ expecting 3 "pattern" }
| LAZY simple_pattern
{ mkpat(Ppat_lazy $2) }
;
simple_pattern:
val_ident %prec below_EQUAL
{ mkpat(Ppat_var (mkrhs $1 1)) }
| UNDERSCORE
{ mkpat(Ppat_any) }
| signed_constant
{ mkpat(Ppat_constant $1) }
| CHAR DOTDOT CHAR
{ mkrangepat $1 $3 }
| constr_longident
{ mkpat(Ppat_construct(mkrhs $1 1, None, false)) }
| name_tag
{ mkpat(Ppat_variant($1, None)) }
| SHARP type_longident
{ mkpat(Ppat_type (mkrhs $2 2)) }
| LBRACE lbl_pattern_list RBRACE
{ let (fields, closed) = $2 in mkpat(Ppat_record(fields, closed)) }
| LBRACE lbl_pattern_list error
{ unclosed "{" 1 "}" 3 }
| LBRACKET pattern_semi_list opt_semi RBRACKET
{ reloc_pat (mktailpat (rhs_loc 4) (List.rev $2)) }
| LBRACKET pattern_semi_list opt_semi error
{ unclosed "[" 1 "]" 4 }
| LBRACKETBAR pattern_semi_list opt_semi BARRBRACKET
{ mkpat(Ppat_array(List.rev $2)) }
| LBRACKETBAR BARRBRACKET
{ mkpat(Ppat_array []) }
| LBRACKETBAR pattern_semi_list opt_semi error
{ unclosed "[|" 1 "|]" 4 }
| LPAREN pattern RPAREN
{ reloc_pat $2 }
| LPAREN pattern error
{ unclosed "(" 1 ")" 3 }
| LPAREN pattern COLON core_type RPAREN
{ mkpat(Ppat_constraint($2, $4)) }
| LPAREN pattern COLON core_type error
{ unclosed "(" 1 ")" 5 }
| LPAREN pattern COLON error
{ expecting 4 "type" }
| LPAREN MODULE UIDENT RPAREN
{ mkpat(Ppat_unpack (mkrhs $3 3)) }
| LPAREN MODULE UIDENT COLON package_type RPAREN
{ mkpat(Ppat_constraint(mkpat(Ppat_unpack (mkrhs $3 3)),
ghtyp(Ptyp_package $5))) }
| LPAREN MODULE UIDENT COLON package_type error
{ unclosed "(" 1 ")" 6 }
;
pattern_comma_list:
pattern_comma_list COMMA pattern { $3 :: $1 }
| pattern COMMA pattern { [$3; $1] }
| pattern COMMA error { expecting 3 "pattern" }
;
pattern_semi_list:
pattern { [$1] }
| pattern_semi_list SEMI pattern { $3 :: $1 }
;
lbl_pattern_list:
lbl_pattern { [$1], Closed }
| lbl_pattern SEMI { [$1], Closed }
| lbl_pattern SEMI UNDERSCORE opt_semi { [$1], Open }
| lbl_pattern SEMI lbl_pattern_list
{ let (fields, closed) = $3 in $1 :: fields, closed }
;
lbl_pattern:
label_longident EQUAL pattern
{ (mkrhs $1 1,$3) }
| label_longident
{ (mkrhs $1 1, pat_of_label $1 1) }
;
/* Primitive declarations */
primitive_declaration:
STRING { [$1] }
| STRING primitive_declaration { $1 :: $2 }
;
/* Type declarations */
type_declarations:
type_declaration { [$1] }
| type_declarations AND type_declaration { $3 :: $1 }
;
type_declaration:
optional_type_parameters LIDENT type_kind constraints
{ let (params, variance) = List.split $1 in
let (kind, private_flag, manifest) = $3 in
(mkrhs $2 2, {ptype_params = params;
ptype_cstrs = List.rev $4;
ptype_kind = kind;
ptype_private = private_flag;
ptype_manifest = manifest;
ptype_variance = variance;
ptype_loc = symbol_rloc() }) }
;
constraints:
constraints CONSTRAINT constrain { $3 :: $1 }
| /* empty */ { [] }
;
type_kind:
/*empty*/
{ (Ptype_abstract, Public, None) }
| EQUAL core_type
{ (Ptype_abstract, Public, Some $2) }
| EQUAL PRIVATE core_type
{ (Ptype_abstract, Private, Some $3) }
| EQUAL constructor_declarations
{ (Ptype_variant(List.rev $2), Public, None) }
| EQUAL PRIVATE constructor_declarations
{ (Ptype_variant(List.rev $3), Private, None) }
| EQUAL private_flag BAR constructor_declarations
{ (Ptype_variant(List.rev $4), $2, None) }
| EQUAL private_flag LBRACE label_declarations opt_semi RBRACE
{ (Ptype_record(List.rev $4), $2, None) }
| EQUAL core_type EQUAL private_flag opt_bar constructor_declarations
{ (Ptype_variant(List.rev $6), $4, Some $2) }
| EQUAL core_type EQUAL private_flag LBRACE label_declarations opt_semi RBRACE
{ (Ptype_record(List.rev $6), $4, Some $2) }
;
optional_type_parameters:
/*empty*/ { [] }
| optional_type_parameter { [$1] }
| LPAREN optional_type_parameter_list RPAREN { List.rev $2 }
;
optional_type_parameter:
type_variance QUOTE ident { Some (mkrhs $3 3), $1 }
| type_variance UNDERSCORE { None, $1 }
;
optional_type_parameter_list:
optional_type_parameter { [$1] }
| optional_type_parameter_list COMMA optional_type_parameter { $3 :: $1 }
;
type_parameters:
/*empty*/ { [] }
| type_parameter { [$1] }
| LPAREN type_parameter_list RPAREN { List.rev $2 }
;
type_parameter:
type_variance QUOTE ident { mkrhs $3 3, $1 }
;
type_variance:
/* empty */ { false, false }
| PLUS { true, false }
| MINUS { false, true }
;
type_parameter_list:
type_parameter { [$1] }
| type_parameter_list COMMA type_parameter { $3 :: $1 }
;
constructor_declarations:
constructor_declaration { [$1] }
| constructor_declarations BAR constructor_declaration { $3 :: $1 }
;
constructor_declaration:
| constr_ident generalized_constructor_arguments
{ let arg_types,ret_type = $2 in
(mkrhs $1 1, arg_types,ret_type, symbol_rloc()) }
;
constructor_arguments:
/*empty*/ { [] }
| OF core_type_list { List.rev $2 }
;
generalized_constructor_arguments:
/*empty*/ { ([],None) }
| OF core_type_list { (List.rev $2,None) }
| COLON core_type_list MINUSGREATER simple_core_type
{ (List.rev $2,Some $4) }
| COLON simple_core_type { ([],Some $2) }
;
label_declarations:
label_declaration { [$1] }
| label_declarations SEMI label_declaration { $3 :: $1 }
;
label_declaration:
mutable_flag label COLON poly_type
{ (mkrhs $2 2, $1, $4, symbol_rloc()) }
;
/* "with" constraints (additional type equations over signature components) */
with_constraints:
with_constraint { [$1] }
| with_constraints AND with_constraint { $3 :: $1 }
;
with_constraint:
TYPE type_parameters label_longident with_type_binder core_type constraints
{ let params, variance = List.split $2 in
(mkrhs $3 3,
Pwith_type {ptype_params = List.map (fun x -> Some x) params;
ptype_cstrs = List.rev $6;
ptype_kind = Ptype_abstract;
ptype_manifest = Some $5;
ptype_private = $4;
ptype_variance = variance;
ptype_loc = symbol_rloc()}) }
/* used label_longident instead of type_longident to disallow
functor applications in type path */
| TYPE type_parameters label COLONEQUAL core_type
{ let params, variance = List.split $2 in
(mkrhs (Lident $3) 3,
Pwith_typesubst { ptype_params = List.map (fun x -> Some x) params;
ptype_cstrs = [];
ptype_kind = Ptype_abstract;
ptype_manifest = Some $5;
ptype_private = Public;
ptype_variance = variance;
ptype_loc = symbol_rloc()}) }
| MODULE mod_longident EQUAL mod_ext_longident
{ (mkrhs $2 2, Pwith_module (mkrhs $4 4)) }
| MODULE UIDENT COLONEQUAL mod_ext_longident
{ (mkrhs (Lident $2) 2, Pwith_modsubst (mkrhs $4 4)) }
;
with_type_binder:
EQUAL { Public }
| EQUAL PRIVATE { Private }
;
/* Polymorphic types */
typevar_list:
QUOTE ident { [$2] }
| typevar_list QUOTE ident { $3 :: $1 }
;
poly_type:
core_type
{ mktyp(Ptyp_poly([], $1)) }
| typevar_list DOT core_type
{ mktyp(Ptyp_poly(List.rev $1, $3)) }
;
/* Core types */
core_type:
core_type2
{ $1 }
| core_type2 AS QUOTE ident
{ mktyp(Ptyp_alias($1, $4)) }
;
core_type2:
simple_core_type_or_tuple
{ $1 }
| QUESTION LIDENT COLON core_type2 MINUSGREATER core_type2
{ mktyp(Ptyp_arrow("?" ^ $2 , mkoption $4, $6)) }
| OPTLABEL core_type2 MINUSGREATER core_type2
{ mktyp(Ptyp_arrow("?" ^ $1 , mkoption $2, $4)) }
| LIDENT COLON core_type2 MINUSGREATER core_type2
{ mktyp(Ptyp_arrow($1, $3, $5)) }
| core_type2 MINUSGREATER core_type2
{ mktyp(Ptyp_arrow("", $1, $3)) }
;
simple_core_type:
simple_core_type2 %prec below_SHARP
{ $1 }
| LPAREN core_type_comma_list RPAREN %prec below_SHARP
{ match $2 with [sty] -> sty | _ -> raise Parse_error }
;
simple_core_type2:
QUOTE ident
{ mktyp(Ptyp_var $2) }
| UNDERSCORE
{ mktyp(Ptyp_any) }
| type_longident
{ mktyp(Ptyp_constr(mkrhs $1 1, [])) }
| simple_core_type2 type_longident
{ mktyp(Ptyp_constr(mkrhs $2 2, [$1])) }
| LPAREN core_type_comma_list RPAREN type_longident
{ mktyp(Ptyp_constr(mkrhs $4 4, List.rev $2)) }
| LESS meth_list GREATER
{ mktyp(Ptyp_object $2) }
| LESS GREATER
{ mktyp(Ptyp_object []) }
| SHARP class_longident opt_present
{ mktyp(Ptyp_class(mkrhs $2 2, [], $3)) }
| simple_core_type2 SHARP class_longident opt_present
{ mktyp(Ptyp_class(mkrhs $3 3, [$1], $4)) }
| LPAREN core_type_comma_list RPAREN SHARP class_longident opt_present
{ mktyp(Ptyp_class(mkrhs $5 5, List.rev $2, $6)) }
| LBRACKET tag_field RBRACKET
{ mktyp(Ptyp_variant([$2], true, None)) }
/* PR#3835: this is not LR(1), would need lookahead=2
| LBRACKET simple_core_type RBRACKET
{ mktyp(Ptyp_variant([$2], true, None)) }
*/
| LBRACKET BAR row_field_list RBRACKET
{ mktyp(Ptyp_variant(List.rev $3, true, None)) }
| LBRACKET row_field BAR row_field_list RBRACKET
{ mktyp(Ptyp_variant($2 :: List.rev $4, true, None)) }
| LBRACKETGREATER opt_bar row_field_list RBRACKET
{ mktyp(Ptyp_variant(List.rev $3, false, None)) }
| LBRACKETGREATER RBRACKET
{ mktyp(Ptyp_variant([], false, None)) }
| LBRACKETLESS opt_bar row_field_list RBRACKET
{ mktyp(Ptyp_variant(List.rev $3, true, Some [])) }
| LBRACKETLESS opt_bar row_field_list GREATER name_tag_list RBRACKET
{ mktyp(Ptyp_variant(List.rev $3, true, Some (List.rev $5))) }
| LPAREN MODULE package_type RPAREN
{ mktyp(Ptyp_package $3) }
;
package_type:
mty_longident { (mkrhs $1 1, []) }
| mty_longident WITH package_type_cstrs { (mkrhs $1 1, $3) }
;
package_type_cstr:
TYPE label_longident EQUAL core_type { (mkrhs $2 2, $4) }
;
package_type_cstrs:
package_type_cstr { [$1] }
| package_type_cstr AND package_type_cstrs { $1::$3 }
;
row_field_list:
row_field { [$1] }
| row_field_list BAR row_field { $3 :: $1 }
;
row_field:
tag_field { $1 }
| simple_core_type { Rinherit $1 }
;
tag_field:
name_tag OF opt_ampersand amper_type_list
{ Rtag ($1, $3, List.rev $4) }
| name_tag
{ Rtag ($1, true, []) }
;
opt_ampersand:
AMPERSAND { true }
| /* empty */ { false }
;
amper_type_list:
core_type { [$1] }
| amper_type_list AMPERSAND core_type { $3 :: $1 }
;
opt_present:
LBRACKETGREATER name_tag_list RBRACKET { List.rev $2 }
| /* empty */ { [] }
;
name_tag_list:
name_tag { [$1] }
| name_tag_list name_tag { $2 :: $1 }
;
simple_core_type_or_tuple:
simple_core_type { $1 }
| simple_core_type STAR core_type_list
{ mktyp(Ptyp_tuple($1 :: List.rev $3)) }
;
core_type_comma_list:
core_type { [$1] }
| core_type_comma_list COMMA core_type { $3 :: $1 }
;
core_type_list:
simple_core_type { [$1] }
| core_type_list STAR simple_core_type { $3 :: $1 }
;
meth_list:
field SEMI meth_list { $1 :: $3 }
| field opt_semi { [$1] }
| DOTDOT { [mkfield Pfield_var] }
;
field:
label COLON poly_type { mkfield(Pfield($1, $3)) }
;
label:
LIDENT { $1 }
;
/* Constants */
constant:
INT { Const_int $1 }
| CHAR { Const_char $1 }
| STRING { Const_string $1 }
| FLOAT { Const_float $1 }
| INT32 { Const_int32 $1 }
| INT64 { Const_int64 $1 }
| NATIVEINT { Const_nativeint $1 }
;
signed_constant:
constant { $1 }
| MINUS INT { Const_int(- $2) }
| MINUS FLOAT { Const_float("-" ^ $2) }
| MINUS INT32 { Const_int32(Int32.neg $2) }
| MINUS INT64 { Const_int64(Int64.neg $2) }
| MINUS NATIVEINT { Const_nativeint(Nativeint.neg $2) }
| PLUS INT { Const_int $2 }
| PLUS FLOAT { Const_float $2 }
| PLUS INT32 { Const_int32 $2 }
| PLUS INT64 { Const_int64 $2 }
| PLUS NATIVEINT { Const_nativeint $2 }
;
/* Identifiers and long identifiers */
ident:
UIDENT { $1 }
| LIDENT { $1 }
;
val_ident:
LIDENT { $1 }
| LPAREN operator RPAREN { $2 }
| LPAREN operator error { unclosed "(" 1 ")" 3 }
| LPAREN error { expecting 2 "operator" }
| LPAREN MODULE error { expecting 3 "module-expr" }
;
operator:
PREFIXOP { $1 }
| INFIXOP0 { $1 }
| INFIXOP1 { $1 }
| INFIXOP2 { $1 }
| INFIXOP3 { $1 }
| INFIXOP4 { $1 }
| BANG { "!" }
| PLUS { "+" }
| PLUSDOT { "+." }
| MINUS { "-" }
| MINUSDOT { "-." }
| STAR { "*" }
| EQUAL { "=" }
| LESS { "<" }
| GREATER { ">" }
| OR { "or" }