MAJ terminée. Nous sommes passés en version 14.6.2 . Pour consulter les "releases notes" associées c'est ici :

https://about.gitlab.com/releases/2022/01/11/security-release-gitlab-14-6-2-released/
https://about.gitlab.com/releases/2022/01/04/gitlab-14-6-1-released/

parser.mly 20.9 KB
Newer Older
1
2
/**************************************************************************/
/*                                                                        */
Jean-Christophe Filliâtre's avatar
headers    
Jean-Christophe Filliâtre committed
3
4
5
6
7
/*  Copyright (C) 2010-                                                   */
/*    Francois Bobot                                                      */
/*    Jean-Christophe Filliatre                                           */
/*    Johannes Kanig                                                      */
/*    Andrei Paskevich                                                    */
8
9
10
11
12
13
14
15
16
17
18
/*                                                                        */
/*  This software is free software; you can redistribute it and/or        */
/*  modify it under the terms of the GNU Library General Public           */
/*  License version 2.1, with the special exception on linking            */
/*  described in file LICENSE.                                            */
/*                                                                        */
/*  This software is distributed in the hope that it will be useful,      */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  */
/*                                                                        */
/**************************************************************************/
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

%{

  open Ptree
  open Parsing

  let loc () = (symbol_start_pos (), symbol_end_pos ())
  let loc_i i = (rhs_start_pos i, rhs_end_pos i)
  let loc_ij i j = (rhs_start_pos i, rhs_end_pos j)

  let mk_ppl loc d = { pp_loc = loc; pp_desc = d }
  let mk_pp d = mk_ppl (loc ()) d
  let mk_pp_i i d = mk_ppl (loc_i i) d
		    
  let infix_ppl loc a i b = mk_ppl loc (PPinfix (a, i, b))
  let infix_pp a i b = infix_ppl (loc ()) a i b

  let prefix_ppl loc p a = mk_ppl loc (PPprefix (p, a))
  let prefix_pp p a = prefix_ppl (loc ()) p a

39
40
41
42
  let infix s = "infix " ^ s
  let prefix s = "prefix " ^ s
  let postfix s = "postfix " ^ s

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
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
(***
  let with_loc loc d = { pdesc = d; ploc = loc }
  let locate d = with_loc (loc ()) d
  let locate_i i d = with_loc (loc_i i) d

  let rec_name = function Srec (x,_,_,_,_,_) -> x | _ -> assert false

  let join (b,_) (_,e) = (b,e)

  let rec app f = function
    | [] -> 
	assert false
    | [a] -> 
	Sapp (f, a)
    | a :: l -> 
	let loc = join f.ploc a.ploc in 
	app (with_loc loc (Sapp (f, a))) l

  let bin_op (loc_op,op) e1 e2 =
    let f = with_loc loc_op (Svar op) in
    let f_e1 = with_loc (join e1.ploc loc_op) (Sapp (f, e1)) in
    locate (Sapp (f_e1, e2))
      
  let un_op (loc_op,op) e =
    locate (app (with_loc loc_op (Svar op)) [e])

  let ptype_c_of_v v =
    { pc_result_name = Ident.result;
      pc_result_type = v;
      pc_effect = { pe_reads = []; pe_writes = []; pe_raises = [] };
      pc_pre = []; 
      pc_post = None }

  let list_of_some = function None -> [] | Some x -> [x]

  (*s ensures a postcondition for a function body *)

  let force_function_post ?(warn=false) e = match e.pdesc with
    | Spost _ -> 
	e
    | _ -> 
       if warn then 
	 Format.eprintf 
	   "%ano postcondition for this function; true inserted@\n"
	   Loc.report_position e.ploc; 
       let q = 
	 { pa_name = Anonymous; pa_value = mk_pp PPtrue; pa_loc = loc () }
       in
       { e with pdesc = Spost (e, (q, []), Transparent) }
***)
%}

/* Tokens */ 

97
%token <string> LIDENT UIDENT
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
98
%token <string> INTEGER
99
%token <string> OP0 OP1 OP2 OP3
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
100
101
102
103
%token <Ptree.real_constant> FLOAT
%token <string> STRING
%token ABSURD AMPAMP AND ARRAY ARROW AS ASSERT AT AXIOM 
%token BANG BAR BARBAR BEGIN 
104
%token BIGARROW CHECK CLONE COLON COLONEQUAL COMMA DO 
105
106
%token DONE DOT ELSE END EOF EQUAL
%token EXCEPTION EXISTS EXPORT EXTERNAL FALSE FOR FORALL FPI 
107
%token FUN FUNCTION GOAL
108
%token IF IMPORT IN INCLUDE INDUCTIVE INVARIANT
109
%token LEFTB LEFTBLEFTB LEFTPAR LEFTSQ LEMMA 
110
%token LET LOGIC LRARROW MATCH 
111
112
%token NAMESPACE NOT OF OR PARAMETER  PREDICATE PROP 
%token QUOTE RAISE RAISES READS REC REF RETURNS RIGHTB RIGHTBRIGHTB
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
113
%token RIGHTPAR RIGHTSQ 
114
115
%token SEMICOLON 
%token THEN THEORY TRUE TRY TYPE UNDERSCORE
116
%token UNIT USE VARIANT VOID WHILE WITH WRITES
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
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

/* Precedences */

%nonassoc prec_recfun
%nonassoc prec_fun
%left LEFTB LEFTBLEFTB
%left prec_simple

%left COLON 

%left prec_letrec
%left IN

%right SEMICOLON

%left prec_no_else
%left ELSE

%right prec_named
%left COLONEQUAL
%right prec_forall prec_exists
%right ARROW LRARROW
%right OR BARBAR
%right AND AMPAMP
%right NOT
%right prec_if
143
144
145
146
147
%left EQUAL OP0
%left OP1
%left OP2
%left OP3
%right unary_op
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
148
149
150
151
%left prec_app
%left prec_ident
%left LEFTSQ

152
153
154
%nonassoc prec_logics prec_types
%nonassoc LOGIC TYPE

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
155
156
157
158
159
160
161
162
163
/* Entry points */

%type <Ptree.lexpr> lexpr
%start lexpr
%type <Ptree.logic_file> logic_file
%start logic_file
%%

logic_file:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
164
165
| list1_theory EOF
   { $1 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
166
167
168
169
170
171
172
173
174
175
176
| EOF 
   { [] }
;

list1_decl:
| decl 
   { [$1] }
| decl list1_decl 
   { $1 :: $2 }
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
177
178
179
180
181
182
183
list0_decl:
| /* epsilon */
   { [] }
| list1_decl 
   { $1 }
;

184
ident:
185
186
| lident_string { { id = $1; id_loc = loc () } }
| UIDENT        { { id = $1; id_loc = loc () } }
187
188
;

189
lident:
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
| lident_string
    { { id = $1; id_loc = loc () } }
;

lident_string:
| LIDENT                        
    { $1 }
| LEFTPAR UNDERSCORE lident_op UNDERSCORE RIGHTPAR 
    { infix $3 }
| LEFTPAR lident_op UNDERSCORE RIGHTPAR 
    { prefix $2 }
/*
| LEFTPAR UNDERSCORE lident_op RIGHTPAR 
    { postfix $3 }
*/
205
206
;

207
208
209
210
211
212
lident_op:
| OP0   { $1 }
| OP2   { $1 }
| OP3   { $1 }
| EQUAL { "=" }
;
213

214
215
216
217
218
any_op:
| OP0   { $1 }
| OP2   { $1 }
| OP3   { $1 }
;
219

220
221
222
223
224
225
226
227
228
229
230
231
uident:
| UIDENT { { id = $1; id_loc = loc () } }
;

lqualid:
| lident             { Qident $1 }
| uqualid DOT lident { Qdot ($1, $3) }
;

uqualid:
| uident             { Qident $1 }
| uqualid DOT uident { Qdot ($1, $3) }
232
233
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
234
235
236
any_qualid:
| ident                { Qident $1 }
| any_qualid DOT ident { Qdot ($1, $3) }
237
238
;

239
qualid:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
240
241
| ident             { Qident $1 }
| uqualid DOT ident { Qdot ($1, $3) }
242

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
params:
| /* epsilon */                          { [] }
| LEFTPAR list1_param_sep_comma RIGHTPAR { $2 }
;

param:
| primitive_type              { None, $1 }
| lident COLON primitive_type { Some $1, $3 }
;

list1_param_sep_comma:
| param                             { [$1] }
| param COMMA list1_param_sep_comma { $1 :: $3 }
;

258
259
260
261
primitive_types:
| /* epsilon */                                   { [] }
| LEFTPAR list1_primitive_type_sep_comma RIGHTPAR { $2 }

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
logic_type_option:
| /* epsilon */        { None }
| COLON primitive_type { Some $2 }
;

logic_def_option:
| /* epsilon */ { None }
| EQUAL lexpr   { Some $2 }
;

logic_decl:
| LOGIC lident params logic_type_option logic_def_option
    { { ld_loc = loc ();
	ld_ident = $2; ld_params = $3; ld_type = $4; ld_def = $5; } }
;

list1_logic_decl:
| logic_decl                  %prec prec_logics { [$1] }
| logic_decl list1_logic_decl                   { $1 :: $2 }
;

type_decl:
| TYPE typedecl typedefn
  { let _, pl, id = $2 in
    { td_loc = loc (); td_ident = id; td_params = pl; td_def = $3 } }
;

list1_type_decl:
| type_decl                  %prec prec_types { [$1] }
| type_decl list1_type_decl                   { $1 :: $2 }
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
294
decl:
295
296
297
298
| list1_type_decl
   { TypeDecl (loc (), $1) }
| list1_logic_decl
   { Logic (loc (), $1) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
299
| AXIOM uident COLON lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
300
301
302
303
304
   { Prop (loc (), Kaxiom, $2, $4) }
| GOAL uident COLON lexpr
   { Prop (loc (), Kgoal, $2, $4) }
| LEMMA uident COLON lexpr
   { Prop (loc (), Klemma, $2, $4) }
305
306
| INDUCTIVE lident primitive_types inddefn
   { Inductive_def (loc (), $2, $3, $4) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
307
308
| CLONE use clone_subst
   { UseClone (loc (), $2, Some $3) }
309
| USE use
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
310
   { UseClone (loc (), $2, None) }
311
312
| NAMESPACE uident list0_decl END
   { Namespace (loc (), $2, $3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
313
314
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
315
316
317
318
319
320
321
322
list1_theory:
| theory 
   { [$1] }
| theory list1_theory 
   { $1 :: $2 }
;

theory:
323
| THEORY uident list0_decl END 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
324
   { { pt_loc = loc (); pt_name = $2; pt_decl = $3 } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
325
326
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
327
typedecl:
328
| lident
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
329
    { (loc_i 1, [], $1) }
330
| type_var lident
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
331
    { (loc_i 2, [$1], $2) }
332
| LEFTPAR type_var COMMA list1_type_var_sep_comma RIGHTPAR lident
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
333
334
335
336
337
    { (loc_i 6, $2 :: $4, $6) }
;

typedefn:
| /* epsilon */
338
339
340
    { TDabstract }
| EQUAL primitive_type
    { TDalias $2 }
341
342
343
| EQUAL typecases
    { TDalgebraic $2 }
| EQUAL BAR typecases
344
    { TDalgebraic $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
345
346
347
348
349
350
351
352
;

typecases:
| typecase                { [$1] }
| typecase BAR typecases  { $1::$3 }
;

typecase:
353
| uident params { (loc_i 1,$1,$2) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
354
355
356
357
358
359
360
361
362
363
364
365
366
;

inddefn:
| /* epsilon */       { [] }
| EQUAL bar_ indcases { $3 }
;

indcases:
| indcase               { [$1] }
| indcase BAR indcases  { $1::$3 }
;

indcase:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
367
| uident COLON lexpr { (loc_i 1,$1,$3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
368
369
370
371
;

primitive_type:
| type_var 
372
   { PPTtyvar $1 }
373
| lqualid
374
   { PPTtyapp ([], $1) }
375
| primitive_type lqualid
376
   { PPTtyapp ([$1], $2) }
377
| LEFTPAR primitive_type COMMA list1_primitive_type_sep_comma RIGHTPAR lqualid
378
   { PPTtyapp ($2 :: $4, $6) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
;

list1_primitive_type_sep_comma:
| primitive_type                                      { [$1] }
| primitive_type COMMA list1_primitive_type_sep_comma { $1 :: $3 }
;

lexpr:
| lexpr ARROW lexpr 
   { infix_pp $1 PPimplies $3 }
| lexpr LRARROW lexpr 
   { infix_pp $1 PPiff $3 }
| lexpr OR lexpr 
   { infix_pp $1 PPor $3 }
| lexpr AND lexpr 
   { infix_pp $1 PPand $3 }
| NOT lexpr 
   { prefix_pp PPnot $2 }
397
| lexpr EQUAL lexpr 
398
   { let id = { id = infix "="; id_loc = loc_i 2 } in
399
     mk_pp (PPapp (Qident id, [$1; $3])) }
400
401
| lexpr OP0 lexpr 
   { let id = { id = infix $2; id_loc = loc_i 2 } in
402
     mk_pp (PPapp (Qident id, [$1; $3])) }
403
404
| lexpr OP1 lexpr 
   { let id = { id = infix $2; id_loc = loc_i 2 } in
405
     mk_pp (PPapp (Qident id, [$1; $3])) }
406
407
| lexpr OP2 lexpr 
   { let id = { id = infix $2; id_loc = loc_i 2 } in
408
     mk_pp (PPapp (Qident id, [$1; $3])) }
409
410
| lexpr OP3 lexpr 
   { let id = { id = infix $2; id_loc = loc_i 2 } in
411
     mk_pp (PPapp (Qident id, [$1; $3])) }
412
413
414
415
416
417
418
419
| any_op lexpr %prec unary_op
   { let id = { id = prefix $1; id_loc = loc_i 2 } in
     mk_pp (PPapp (Qident id, [$2])) }
/*
| lexpr any_op %prec unary_op
   { let id = { id = postfix $2; id_loc = loc_i 2 } in
     mk_pp (PPapp (Qident id, [$1])) }
*/
420
| qualid
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
421
   { mk_pp (PPvar $1) }
422
| qualid LEFTPAR list1_lexpr_sep_comma RIGHTPAR
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
423
424
425
   { mk_pp (PPapp ($1, $3)) }
| IF lexpr THEN lexpr ELSE lexpr %prec prec_if 
   { mk_pp (PPif ($2, $4, $6)) }
426
427
428
429
| FORALL list1_lident_sep_comma COLON primitive_type triggers DOT lexpr 
  %prec prec_forall
   { mk_pp (PPforall ($2, $4, $5, $7))
     (*let rec mk = function
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
430
431
432
433
       | [] -> assert false
       | [id] -> mk_pp (PPforall (id, $4, $5, $7))
       | id :: l -> mk_pp (PPforall (id, $4, [], mk l))
     in
434
     mk $2 *) }
435
| EXISTS lident COLON primitive_type DOT lexpr %prec prec_exists
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
436
437
   { mk_pp (PPexists ($2, $4, $6)) }
| INTEGER
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
438
   { mk_pp (PPconst (Term.ConstInt $1)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
439
| FLOAT
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
440
   { mk_pp (PPconst (Term.ConstReal $1)) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
441
442
443
444
445
446
447
448
| TRUE
   { mk_pp PPtrue }
| FALSE
   { mk_pp PPfalse }    
| LEFTPAR lexpr RIGHTPAR
   { $2 }
| ident_or_string COLON lexpr %prec prec_named
   { mk_pp (PPnamed ($1, $3)) }
449
| LET lident EQUAL lexpr IN lexpr 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
   { mk_pp (PPlet ($2, $4, $6)) }
| MATCH lexpr WITH bar_ match_cases END
   { mk_pp (PPmatch ($2, $5)) }
;

match_cases:
| match_case                  { [$1] }
| match_case BAR match_cases  { $1::$3 }
;

match_case:
| pattern ARROW lexpr { ($1,$3) }
;

pattern:
465
| uqualid                                         { ($1, [], loc ()) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
466
| uqualid LEFTPAR list1_lident_sep_comma RIGHTPAR  { ($1, $3, loc ()) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
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
;

triggers:
| /* epsilon */                         { [] }
| LEFTSQ list1_trigger_sep_bar RIGHTSQ  { $2 }
;

list1_trigger_sep_bar:
| trigger                           { [$1] }
| trigger BAR list1_trigger_sep_bar { $1 :: $3 }
;

trigger:
  list1_lexpr_sep_comma { $1 }
;

list1_lexpr_sep_comma:
| lexpr                             { [$1] }
| lexpr COMMA list1_lexpr_sep_comma { $1 :: $3 }
;

type_var:
| QUOTE ident { $2 }
;

list1_type_var_sep_comma:
| type_var                                { [$1] }
| type_var COMMA list1_type_var_sep_comma { $1 :: $3 }
;

ident_or_string:
498
| ident  { $1.id }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
499
500
501
502
503
504
505
506
| STRING { $1 }
;

bar_:
| /* epsilon */ { () }
| BAR           { () }
;

507
508
509
510
511
list1_lident_sep_comma:
| lident                              { [$1] }
| lident COMMA list1_lident_sep_comma { $1 :: $3 }
;

512
use:
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
513
| imp_exp any_qualid              
514
    { { use_theory = $2; use_as = None; use_imp_exp = $1 } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
515
516
| imp_exp any_qualid AS uident
    { { use_theory = $2; use_as = Some $4; use_imp_exp = $1 } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
517
518
;

519
520
521
522
imp_exp:
| IMPORT        { Import }
| EXPORT        { Export }
| /* epsilon */ { Nothing }
523
524
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
525
526
clone_subst:
| /* epsilon */ 
527
    { { ts_subst = []; ls_subst = [] } } 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
528
| WITH list1_comma_subst
529
530
    { let t,l = $2 in
      { ts_subst = t; ls_subst = l } } 
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
531
532
533
534
535
536
;

list1_comma_subst:
| subst                         
    { $1 }
| subst COMMA list1_comma_subst 
537
    { let t,l = $1 in let tl,ll = $3 in t@tl, l@ll }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
538
539
540
;

subst:
541
542
| TYPE  qualid EQUAL qualid { [$2, $4], [] }
| LOGIC qualid EQUAL qualid { [], [$2, $4] }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
543
544
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
545
546
/******* programs **************************************************

547
548
549
550
551
552
qualid_ident:
| IDENT          { $1, None }
| IDENT AT       { $1, Some "" }
| IDENT AT IDENT { $1, Some $3 }
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
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
list0_ident_sep_comma:
| /* epsilon * /         { [] }
| list1_ident_sep_comma { $1 }
;

decl:
| INCLUDE STRING
   { Include (loc_i 2,$2) }
| LET ident EQUAL expr
   { Program (loc_i 2,$2, $4) }
| LET ident binders EQUAL list0_bracket_assertion expr
   { Program (loc_i 2,$2, locate (Slam ($3, $5, force_function_post $6))) }
| LET REC recfun
   { let (loc,p) = $3 in Program (loc,rec_name p, locate p) }
| EXCEPTION ident
   { Exception (loc (), $2, None) }
| EXCEPTION ident OF primitive_type
   { Exception (loc (), $2, Some $4) }
| external_ PARAMETER list1_ident_sep_comma COLON type_v
   { Parameter (loc_i 3, $1, $3, $5) }

type_v:
| simple_type_v ARROW type_c
   { PVarrow ([Ident.anonymous, $1], $3) }
| ident COLON simple_type_v ARROW type_c
   { PVarrow ([($1, $3)], $5) }
| simple_type_v
   { $1 }
;

simple_type_v:
| primitive_type ARRAY    { PVref (PPTexternal ([$1], Ident.farray, loc_i 2)) }
| primitive_type REF      { PVref $1 }
| primitive_type          { PVpure $1 }
| LEFTPAR type_v RIGHTPAR { $2 }
;

type_c:
| LEFTB opt_assertion RIGHTB result effects LEFTB opt_post_condition RIGHTB
   { let id,v = $4 in
     { pc_result_name = id; pc_result_type = v;
       pc_effect = $5; pc_pre = list_of_some $2; pc_post = $7 } }
| type_v
   { ptype_c_of_v $1 }
;

result:
| RETURNS ident COLON type_v { $2, $4 }
| type_v                     { Ident.result, $1 }
;

effects:
| opt_reads opt_writes opt_raises
    { { pe_reads = $1; pe_writes = $2; pe_raises = $3 } }
;

opt_reads:
| /* epsilon * /               { [] }
| READS list0_ident_sep_comma { $2 }
;

opt_writes:
| /* epsilon * /                { [] }
| WRITES list0_ident_sep_comma { $2 }
;

opt_raises:
| /* epsilon * /                { [] }
| RAISES list0_ident_sep_comma { $2 }
;

opt_assertion:
| /* epsilon * /  { None }
| assertion      { Some $1 }
;

assertion:
| lexpr          
    { { pa_name = Anonymous; pa_value = $1; pa_loc = loc () } }
| lexpr AS ident 
    { { pa_name = Name $3; pa_value = $1; pa_loc = loc () } }
;

opt_post_condition:
| /* epsilon * /  { None }
| post_condition { Some $1 }
;

post_condition:
| assertion 
   { $1, [] }
| assertion BAR list1_exn_condition_sep_bar
   { $1, $3 }
| BAR list1_exn_condition_sep_bar
   { Format.eprintf "%awarning: no postcondition; false inserted@\n" 
       Loc.report_position (loc ());
     (* if Options.werror then exit 1; *)
     ({ pa_name = Anonymous; pa_value = mk_pp PPfalse; pa_loc = loc () }, $2) }
;

bracket_assertion:
| LEFTB assertion RIGHTB { $2 }
;

list1_bracket_assertion:
| bracket_assertion                         { [$1] }
| bracket_assertion list1_bracket_assertion { $1 :: $2 }
;

list0_bracket_assertion:
| /* epsilon * /           { [] }
| LEFTB RIGHTB            { [] }
| list1_bracket_assertion { $1 }
;

list1_exn_condition_sep_bar:
| exn_condition                                 { [$1] }
| exn_condition BAR list1_exn_condition_sep_bar { $1 :: $3 }
;

exn_condition:
| ident BIGARROW assertion { $1,$3 }
;

expr:
| simple_expr %prec prec_simple 
   { $1 }
| ident COLONEQUAL expr
   { locate 
       (Sapp (locate (Sapp (locate (Svar Ident.ref_set), 
			    locate_i 1 (Svar $1))),
	      $3)) }
| ident LEFTSQ expr RIGHTSQ COLONEQUAL expr
   { locate 
       (Sapp (locate 
		(Sapp (locate 
			 (Sapp (locate (Svar Ident.array_set), 
				locate_i 1 (Svar $1))),
			 $3)),
		$6)) }
| IF expr THEN expr ELSE expr
   { locate (Sif ($2, $4, $6)) }
| IF expr THEN expr %prec prec_no_else
   { locate (Sif ($2, $4, locate (Sconst ConstUnit))) }
| WHILE expr DO invariant_variant expr DONE
   { (* syntactic suget for
        try loop { invariant variant } if b then e else raise Exit
        with Exit -> void end *)
     let inv,var = $4 in
     locate 
       (Stry
	  (locate 
	     (Sloop (inv, var, 
		     locate 
		       (Sif ($2, $5,
			     locate (Sraise (exit_exn, None, None)))))),
	     [((exit_exn, None), locate (Sconst ConstUnit))])) }
| IDENT COLON expr
   { locate (Slabel ($1, $3)) }
| LET ident EQUAL expr IN expr
   { locate (Sletin ($2, $4, $6)) }
| LET ident EQUAL REF expr IN expr
   { locate (Sletref ($2, $5, $7)) }
| FUN binders ARROW list0_bracket_assertion expr %prec prec_fun
   { locate (Slam ($2, $4, force_function_post $5)) }
| LET ident binders EQUAL list0_bracket_assertion expr IN expr
   { let b =  force_function_post ~warn:true $6 in
     locate (Sletin ($2, locate (Slam ($3, $5, b)), $8)) }
| LET REC recfun %prec prec_letrec
   { let _loc,p = $3 in locate p }
| LET REC recfun IN expr
   { let _loc,p = $3 in locate (Sletin (rec_name p, locate p, $5)) }
| RAISE ident opt_cast
   { locate (Sraise ($2, None, $3)) }
| RAISE LEFTPAR ident expr RIGHTPAR opt_cast
   { locate (Sraise ($3, Some $4 , $6)) }
| TRY expr WITH bar_ list1_handler_sep_bar END
   { locate (Stry ($2, $5)) }
| ABSURD opt_cast
   { locate (Sabsurd $2) }
| simple_expr list1_simple_expr %prec prec_app
   { locate (app $1 $2) }
| expr BARBAR expr
   { locate (Slazy_or ($1, $3))
     (* let ptrue = locate (Sconst (ConstBool true)) in
     locate (Sif ($1, ptrue, $3)) *) }
| expr AMPAMP expr
   { locate (Slazy_and ($1, $3))
     (* let pf = locate (Sconst (ConstBool false)) in
     locate (Sif ($1, $3, pf)) *) }
| NOT expr
   { locate (Snot $2)
     (* let pf = locate (Sconst (ConstBool false)) in
     let pt = locate (Sconst (ConstBool true)) in
     locate (Sif ($2, pf, pt)) *) }
| expr relation_id expr %prec prec_relation
   { bin_op $2 $1 $3 }
| expr PLUS expr
   { bin_op (loc_i 2, Ident.t_add) $1 $3 }
| expr MINUS expr
   { bin_op (loc_i 2, Ident.t_sub) $1 $3 }
| expr TIMES expr
   { bin_op (loc_i 2, Ident.t_mul) $1 $3 }
| expr SLASH expr
   { bin_op (loc_i 2, Ident.t_div) $1 $3 }
| expr PERCENT expr
   { bin_op (loc_i 2, Ident.t_mod_int) $1 $3 }
| MINUS expr %prec uminus
   { un_op (loc_i 1, Ident.t_neg) $2 }
| expr SEMICOLON expr
   { locate (Sseq ($1, $3)) }
| ASSERT list1_bracket_assertion SEMICOLON expr 
   { locate (Sassert (`ASSERT,$2, $4)) }
| CHECK list1_bracket_assertion SEMICOLON expr 
   { locate (Sassert (`CHECK,$2, $4)) }
| expr LEFTB post_condition RIGHTB
   { locate (Spost ($1, $3, Transparent)) }
| expr LEFTBLEFTB post_condition RIGHTBRIGHTB
   { locate (Spost ($1, $3, Opaque)) }
;

simple_expr:
| ident %prec prec_ident
   { locate (Svar $1) }
| INTEGER
   { locate (Sconst (ConstInt $1)) }
| FLOAT
   { let f = $1 in locate (Sconst (ConstFloat f)) }
| VOID
   { locate (Sconst ConstUnit) }
| TRUE
   { locate (Sconst (ConstBool true)) }
| FALSE
   { locate (Sconst (ConstBool false)) }
| BANG ident
   { locate (Sderef $2) }
| ident LEFTSQ expr RIGHTSQ
   { locate 
       (Sapp (locate (Sapp (locate (Svar Ident.array_get), 
			    locate_i 1 (Svar $1))),
	      $3)) }
| LEFTSQ type_c RIGHTSQ
   { locate (Sany $2) }
| LEFTPAR expr RIGHTPAR
   { $2 }
| BEGIN expr END
   { $2 }
;

relation_id:
| LT    { loc (), Ident.t_lt }
| LE    { loc (), Ident.t_le }
| GT    { loc (), Ident.t_gt }
| GE    { loc (), Ident.t_ge }
| EQUAL { loc (), Ident.t_eq }
| NOTEQ { loc (), Ident.t_neq }
;

list1_simple_expr:
| simple_expr %prec prec_simple { [$1] }
| simple_expr list1_simple_expr { $1 :: $2 }
;

list1_handler_sep_bar:
| handler                           { [$1] }
| handler BAR list1_handler_sep_bar { $1 :: $3 }
;

handler:
| ident ARROW expr       { (($1, None), $3) }
| ident ident ARROW expr { (($1, Some $2), $4) }
;

opt_cast:
| /* epsilon * / { None }
| COLON type_v  { Some $2 }
;

invariant_variant:
| /* epsilon * / { None, None }
| LEFTB opt_invariant RIGHTB { $2, None }
| LEFTB opt_invariant VARIANT variant RIGHTB { $2, Some $4 }
;

opt_invariant:
| /* epsilon * /       { None }
| INVARIANT assertion { Some $2 }
;

recfun:
| ident binders COLON type_v opt_variant EQUAL 
  list0_bracket_assertion expr %prec prec_recfun
   { (loc_i 1),Srec ($1, $2, $4, $5, $7, force_function_post $8) }
;

opt_variant:
| LEFTB VARIANT variant RIGHTB { Some $3 } 
| /* epsilon * /                { None }
;

variant:
| lexpr FOR ident { ($1, $3) }
| lexpr           { ($1, Ident.t_zwf_zero) }
;

binders:
| list1_binder { List.flatten $1 }
;

list1_binder:
| binder              { [$1] }
| binder list1_binder { $1 :: $2 }
;

binder:
| LEFTPAR RIGHTPAR
   { [Ident.anonymous, PVpure PPTunit] }
| LEFTPAR list1_ident_sep_comma COLON type_v RIGHTPAR 
   { List.map (fun s -> (s, $4)) $2 }
;

****/