parser.pre.mly 27.7 KB
Newer Older
1 2
/**************************************************************************/
/*                                                                        */
Jean-Christophe Filliâtre's avatar
headers  
Jean-Christophe Filliâtre committed
3
/*  Copyright (C) 2010-                                                   */
MARCHE Claude's avatar
MARCHE Claude committed
4 5 6
/*    François Bobot                                                     */
/*    Jean-Christophe Filliâtre                                          */
/*    Claude Marché                                                      */
Jean-Christophe Filliâtre's avatar
headers  
Jean-Christophe Filliâtre committed
7
/*    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 39 40 41 42 43 44 45 46 47 48 49 50 51
  open Theory

  let env_ref  = ref []
  let lenv_ref = ref []
  let uc_ref   = ref []

  let ref_get  ref = List.hd !ref
  let ref_tail ref = List.tl !ref
  let ref_drop ref = ref := ref_tail ref
  let ref_pop  ref = let v = ref_get ref in ref_drop ref; v

  let ref_push ref v = ref := v :: !ref
  let ref_set  ref v = ref := v :: ref_tail ref

  let inside_env env rule lexer lexbuf =
    ref_push env_ref env;
      ref_push lenv_ref Mnm.empty;
    let res = rule lexer lexbuf in
      ref_drop lenv_ref;
    ref_drop env_ref;
    res

  let inside_uc env lenv uc rule lexer lexbuf =
    ref_push env_ref env;
      ref_push lenv_ref lenv;
        ref_push uc_ref uc;
    let res = rule lexer lexbuf in
        ref_drop uc_ref;
      ref_drop lenv_ref;
    ref_drop env_ref;
    res
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
52 53 54 55 56 57 58 59 60 61 62

  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
63

Andrei Paskevich's avatar
Andrei Paskevich committed
64 65
  let mk_pat p = { pat_loc = loc (); pat_desc = p }

66
  let infix_ppl loc a i b = mk_ppl loc (PPbinop (a, i, b))
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
67 68
  let infix_pp a i b = infix_ppl (loc ()) a i b

69
  let prefix_ppl loc p a = mk_ppl loc (PPunop (p, a))
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
70 71
  let prefix_pp p a = prefix_ppl (loc ()) p a

72 73 74
  let infix s = "infix " ^ s
  let prefix s = "prefix " ^ s

75 76 77 78
  let mk_id id loc = { id = id; id_lab = []; id_loc = loc }

  let add_lab id l = { id with id_lab = l }

79 80 81 82 83 84 85 86 87
  let user_loc fname lnum bol cnum1 cnum2 =
    let pos = {
      Lexing.pos_fname = fname;
      Lexing.pos_lnum = lnum;
      Lexing.pos_bol = bol;
      Lexing.pos_cnum = cnum1 }
    in
    pos, { pos with Lexing.pos_cnum = cnum2 }

88 89 90 91 92
  let () = Exn_printer.register
    (fun fmt exn -> match exn with
      | Parsing.Parse_error -> Format.fprintf fmt "syntax error"
      | _ -> raise exn
    )
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

  let mk_expr d = { expr_loc = loc (); expr_desc = d }
  let mk_expr_i i d = { expr_loc = loc_i i; expr_desc = d }

  let cast_body c ((p,e,q) as t) = match c with
    | None -> t
    | Some pt -> p, { e with expr_desc = Ecast (e, pt) }, q

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

  let rec mk_apply f = function
    | [] ->
	assert false
    | [a] ->
	Eapply (f, a)
    | a :: l ->
	let loc = join f.expr_loc a.expr_loc in
	mk_apply { expr_loc = loc; expr_desc = Eapply (f, a) } l

  let mk_apply_id id =
    let e =
      { expr_desc = Eident (Qident id); expr_loc = id.id_loc }
    in
    mk_apply e

  let mk_infix e1 op e2 =
    let id = { id = infix op; id_lab = []; id_loc = loc_i 2 } in
    mk_expr (mk_apply_id id [e1; e2])

  let mk_prefix op e1 =
    let id = { id = prefix op; id_lab = []; id_loc = loc_i 1 } in
    mk_expr (mk_apply_id id [e1])

  let exit_exn () = Qident { id = "%Exit"; id_lab = []; id_loc = loc () }
  let id_anonymous () = { id = "_"; id_lab = []; id_loc = loc () }
  let id_unit () = { id = "unit"; id_lab = []; id_loc = loc () }
  let ty_unit () = Tpure (PPTtyapp ([], Qident (id_unit ())))

  let id_lt_nat () = Qident { id = "lt_nat"; id_lab = []; id_loc = loc () }

  let empty_effect = { pe_reads = []; pe_writes = []; pe_raises = [] }

  let type_c p ty ef q =
    { pc_result_type = ty;
      pc_effect      = ef;
      pc_pre         = p;
      pc_post        = q; }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
140 141
%}

142
/* Tokens */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
143

144
%token <string> LIDENT UIDENT
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
145
%token <string> INTEGER
146
%token <string> OP1 OP2 OP3 OP4 OPPREF
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
147 148
%token <Ptree.real_constant> FLOAT
%token <string> STRING
149 150 151

/* keywords */

152 153
%token AND AS AXIOM CLONE
%token ELSE END EPSILON EXISTS EXPORT FALSE FORALL
154
%token GOAL IF IMPORT IN INDUCTIVE LEMMA
155
%token LET LOGIC MATCH META NAMESPACE NOT PROP OR
Andrei Paskevich's avatar
Andrei Paskevich committed
156
%token THEN THEORY TRUE TYPE USE WITH
157

158 159 160 161 162 163
/* program keywords */

%token ABSURD ANY ASSERT ASSUME BEGIN CHECK DO DONE DOWNTO EXCEPTION FOR 
%token FUN GHOST INVARIANT LABEL MODEL MODULE MUTABLE PARAMETER RAISE 
%token RAISES READS REC TO TRY VARIANT WHILE WRITES

164 165
/* symbols */

166
%token ARROW 
167
%token BACKQUOTE BAR
168
%token COLON COMMA
169
%token DOT EQUAL FUNC LAMBDA LTGT
170
%token LEFTPAR LEFTPAR_STAR_RIGHTPAR LEFTSQ
171
%token LRARROW
172
%token PRED QUOTE
173
%token RIGHTPAR RIGHTSQ
174 175 176 177
%token UNDERSCORE

%token EOF

178 179
/* program symbols */

180
%token AMPAMP BARBAR LEFTBRC RIGHTBRC SEMICOLON
181

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
182 183
/* Precedences */

184 185 186 187 188 189 190 191 192 193
%nonassoc prec_post
%nonassoc BAR

%nonassoc prec_triple
%nonassoc prec_simple

%nonassoc IN
%right SEMICOLON
%nonassoc prec_no_else
%nonassoc DOT ELSE
194
%nonassoc prec_named
195
%nonassoc COLON
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
196 197

%right ARROW LRARROW
198 199
%right OR BARBAR
%right AND AMPAMP
Andrei Paskevich's avatar
Andrei Paskevich committed
200
%nonassoc NOT
201
%left EQUAL LTGT OP1
202
%left OP2
203
%left OP3
204
%left OP4
Andrei Paskevich's avatar
Andrei Paskevich committed
205
%nonassoc prefix_op
206
%left prec_app
207

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
208 209
/* Entry points */

210 211 212 213 214 215
%type <Ptree.lexpr> lexpr_eof
%start lexpr_eof
%type <Theory.theory_uc> list0_decl_eof
%start list0_decl_eof
%type <Theory.theory Theory.Mnm.t> logic_file_eof
%start logic_file_eof
216 217
%type <Ptree.program_file> program_file
%start program_file
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
218 219
%%

220 221 222 223 224 225 226 227 228 229 230 231 232
logic_file_eof:
| list0_theory EOF  { ref_get lenv_ref }
;

list0_decl_eof:
| list0_decl EOF  { ref_get uc_ref }
;

lexpr_eof:
| lexpr EOF  { $1 }
;

/* File, theory, namespace */
233

234 235 236
list0_theory:
| /* epsilon */         { () }
| theory list0_theory   { () }
237 238
;

239
theory_head:
240 241 242
| THEORY uident labels
   { let id = add_lab $2 $3 and labels = $3 in
     let name = Ident.id_user ~labels id.id id.id_loc in
243
     ref_push uc_ref (create_theory name); id }
244 245
;

246
theory:
247 248 249
| theory_head list0_decl END
   { let uc = ref_pop uc_ref in
     ref_set lenv_ref (Typing.close_theory (ref_get lenv_ref) $1 uc) }
250 251
;

252
list0_decl:
253 254 255 256 257 258 259 260 261 262
| /* epsilon */        { () }
| new_decl list0_decl  { () }
;

new_decl:
| decl
   { let env = ref_get env_ref in let lenv = ref_get lenv_ref in
     ref_set uc_ref (Typing.add_decl env lenv (ref_get uc_ref) $1) }
| namespace_head namespace_import namespace_name list0_decl END
   { ref_set uc_ref (Typing.close_namespace (loc ()) $2 $3 (ref_get uc_ref)) }
263 264
;

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
namespace_head:
| NAMESPACE
   { ref_set uc_ref (open_namespace (ref_get uc_ref)) }
;

namespace_import:
| /* epsilon */  { false }
| IMPORT         { true }
;

namespace_name:
| uident      { Some $1 }
| UNDERSCORE  { None }
;

/* Declaration */

282
decl:
283 284 285 286 287 288
| TYPE list1_type_decl
    { TypeDecl $2 }
| LOGIC list1_logic_decl
    { LogicDecl $2 }
| INDUCTIVE list1_inductive_decl
    { IndDecl $2 }
289
| AXIOM ident labels COLON lexpr
290
    { PropDecl (loc (), Kaxiom, add_lab $2 $3, $5) }
291
| LEMMA ident labels COLON lexpr
292
    { PropDecl (loc (), Klemma, add_lab $2 $3, $5) }
293
| GOAL ident labels COLON lexpr
294
    { PropDecl (loc (), Kgoal, add_lab $2 $3, $5) }
295 296 297 298
| USE use
    { UseClone (loc (), $2, None) }
| CLONE use clone_subst
    { UseClone (loc (), $2, Some $3) }
299 300
| META ident list1_meta_arg_sep_comma
    { Meta (loc (), $2, $3) }
301
| META STRING list1_meta_arg_sep_comma
302
    { Meta (loc (), mk_id $2 (loc_i 2), $3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
303 304
;

305 306 307 308 309 310 311 312 313
/* Use and clone */

use:
| imp_exp tqualid
    { { use_theory = $2; use_as = None; use_imp_exp = $1 } }
| imp_exp tqualid AS uident
    { { use_theory = $2; use_as = Some (Some $4); use_imp_exp = $1 } }
| imp_exp tqualid AS UNDERSCORE
    { { use_theory = $2; use_as = Some None; use_imp_exp = $1 } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
314 315
;

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
imp_exp:
| IMPORT        { Import }
| EXPORT        { Export }
| /* epsilon */ { Nothing }
;

clone_subst:
| /* epsilon */          { [] }
| WITH list1_comma_subst { $2 }
;

list1_comma_subst:
| subst                         { [$1] }
| subst COMMA list1_comma_subst { $1 :: $3 }
;

subst:
333
| NAMESPACE ns EQUAL ns     { CSns   ($2, $4) }
334 335 336 337 338 339
| TYPE  qualid EQUAL qualid { CStsym ($2, $4) }
| LOGIC qualid EQUAL qualid { CSlsym ($2, $4) }
| LEMMA qualid              { CSlemma $2 }
| GOAL  qualid              { CSgoal  $2 }
;

340 341 342 343 344
ns:
| uqualid { Some $1 }
| DOT     { None }
;

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
/* Meta args */

list1_meta_arg_sep_comma:
| meta_arg                                { [$1] }
| meta_arg COMMA list1_meta_arg_sep_comma { $1 :: $3 }
;

meta_arg:
| TYPE  qualid { PMAts  $2 }
| LOGIC qualid { PMAls  $2 }
| PROP  qualid { PMApr  $2 }
| STRING       { PMAstr $1 }
| INTEGER      { PMAint $1 }
;

360 361 362
/* Type declarations */

list1_type_decl:
363 364
| type_decl                       { [$1] }
| type_decl WITH list1_type_decl  { $1 :: $3 }
365 366 367
;

type_decl:
368 369 370
| lident labels type_args typedefn
  { { td_loc = loc (); td_ident = add_lab $1 $2;
      td_params = $3; td_def = $4 } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
371 372
;

373
type_args:
374 375
| /* epsilon */             { [] }
| type_var labels type_args { add_lab $1 $2 :: $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
376 377 378
;

typedefn:
379 380 381 382
| /* epsilon */           { TDabstract }
| EQUAL primitive_type    { TDalias $2 }
| EQUAL typecases         { TDalgebraic $2 }
| EQUAL BAR typecases     { TDalgebraic $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
383 384 385 386 387 388 389 390
;

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

typecase:
391
| uident labels params   { (loc (), add_lab $1 $2, $3) }
392 393 394 395 396
;

/* Logic declarations */

list1_logic_decl:
397 398
| logic_decl                        { [$1] }
| logic_decl WITH list1_logic_decl  { $1 :: $3 }
399 400 401
;

logic_decl:
402 403 404
| lident_rich labels params logic_type_option logic_def_option
  { { ld_loc = loc (); ld_ident = add_lab $1 $2;
      ld_params = $3; ld_type = $4; ld_def = $5 } }
405 406 407 408 409 410 411 412 413 414
;

logic_type_option:
| /* epsilon */        { None }
| COLON primitive_type { Some $2 }
;

logic_def_option:
| /* epsilon */ { None }
| EQUAL lexpr   { Some $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
415 416
;

417 418 419
/* Inductive declarations */

list1_inductive_decl:
420 421
| inductive_decl                            { [$1] }
| inductive_decl WITH list1_inductive_decl  { $1 :: $3 }
422 423 424
;

inductive_decl:
425 426 427
| lident_rich labels params inddefn
  { { in_loc = loc (); in_ident = add_lab $1 $2;
      in_params = $3; in_def = $4 } }
Andrei Paskevich's avatar
Andrei Paskevich committed
428
;
429

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
430 431 432 433 434 435 436 437 438 439 440
inddefn:
| /* epsilon */       { [] }
| EQUAL bar_ indcases { $3 }
;

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

indcase:
441
| ident labels COLON lexpr { (loc (), add_lab $1 $2, $4) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
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
/* Type expressions */

primitive_type:
| primitive_type_arg           { $1 }
| lqualid primitive_type_args  { PPTtyapp ($2, $1) }
;

primitive_type_non_lident:
| primitive_type_arg_non_lident  { $1 }
| lq_uident primitive_type_args  { PPTtyapp ($2, $1) }
;

primitive_type_args:
| primitive_type_arg                      { [$1] }
| primitive_type_arg primitive_type_args  { $1 :: $2 }
;

primitive_type_arg:
| lq_lident                      { PPTtyapp ([], $1) }
| primitive_type_arg_non_lident  { $1 }
;

primitive_type_arg_non_lident:
| lq_uident
468
   { PPTtyapp ([], $1) }
469 470 471 472
| type_var
   { PPTtyvar $1 }
| LEFTPAR primitive_type COMMA list1_primitive_type_sep_comma RIGHTPAR
   { PPTtuple ($2 :: $4) }
473 474 475 476 477 478
| LEFTPAR RIGHTPAR
   { PPTtuple [] }
| LEFTPAR primitive_type RIGHTPAR
   { $2 }
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
479 480 481 482 483
list1_primitive_type_sep_comma:
| primitive_type                                      { [$1] }
| primitive_type COMMA list1_primitive_type_sep_comma { $1 :: $3 }
;

484 485
type_var:
| QUOTE ident { $2 }
486 487
;

488 489
/* Logic expressions */

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
490
lexpr:
491
| lexpr ARROW lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
492
   { infix_pp $1 PPimplies $3 }
493
| lexpr LRARROW lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
494
   { infix_pp $1 PPiff $3 }
495
| lexpr OR lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
496
   { infix_pp $1 PPor $3 }
497
| lexpr BARBAR lexpr
498
   { mk_pp (PPnamed (Ident.label "asym_split", infix_pp $1 PPor $3)) }
499
| lexpr AND lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
500
   { infix_pp $1 PPand $3 }
501
| lexpr AMPAMP lexpr
502
   { mk_pp (PPnamed (Ident.label "asym_split", infix_pp $1 PPand $3)) }
503
| NOT lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
504
   { prefix_pp PPnot $2 }
505
| lexpr EQUAL lexpr
506
   { mk_pp (PPinfix ($1, mk_id (infix "=") (loc_i 2), $3)) }
507
| lexpr LTGT lexpr
508
   { prefix_pp PPnot (mk_pp (PPinfix ($1, mk_id (infix "=") (loc_i 2), $3))) }
509
| lexpr OP1 lexpr
510
   { mk_pp (PPinfix ($1, mk_id (infix $2) (loc_i 2), $3)) }
511
| lexpr OP2 lexpr
512
   { mk_pp (PPinfix ($1, mk_id (infix $2) (loc_i 2), $3)) }
513
| lexpr OP3 lexpr
514
   { mk_pp (PPinfix ($1, mk_id (infix $2) (loc_i 2), $3)) }
515
| lexpr OP4 lexpr
516
   { mk_pp (PPinfix ($1, mk_id (infix $2) (loc_i 2), $3)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
517
| any_op lexpr %prec prefix_op
518
   { mk_pp (PPapp (Qident (mk_id (prefix $1) (loc_i 2)), [$2])) }
519 520
| qualid list1_lexpr_arg
   { mk_pp (PPapp ($1, $2)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
521
| IF lexpr THEN lexpr ELSE lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
522
   { mk_pp (PPif ($2, $4, $6)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
523 524
| quant list1_param_var_sep_comma triggers DOT lexpr
   { mk_pp (PPquant ($1, $2, $3, $5)) }
525 526
| label lexpr %prec prec_named
   { mk_pp (PPnamed ($1, $2)) }
527
| LET pattern EQUAL lexpr IN lexpr
528 529
   { match $2.pat_desc with
       | PPpvar id -> mk_pp (PPlet (id, $4, $6))
Andrei Paskevich's avatar
Andrei Paskevich committed
530 531
       | _ -> mk_pp (PPmatch ($4, [$2, $6])) }
| MATCH lexpr WITH bar_ match_cases END
532
   { mk_pp (PPmatch ($2, $5)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
533 534
| MATCH lexpr COMMA list1_lexpr_sep_comma WITH bar_ match_cases END
   { mk_pp (PPmatch (mk_pp (PPtuple ($2::$4)), $7)) }
535 536
| EPSILON lident labels COLON primitive_type DOT lexpr
   { mk_pp (PPeps (add_lab $2 $3, $5, $7)) }
537
| lexpr COLON primitive_type
538
   { mk_pp (PPcast ($1, $3)) }
539
| lexpr_arg
540 541 542
   { $1 }
;

543 544 545
list1_lexpr_arg:
| lexpr_arg                 { [$1] }
| lexpr_arg list1_lexpr_arg { $1::$2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
546
;
547

548 549 550 551 552 553 554
constant:
| INTEGER
   { Term.ConstInt $1 }
| FLOAT
   { Term.ConstReal $1 }
;

555 556 557
lexpr_arg:
| qualid
   { mk_pp (PPvar $1) }
558 559
| constant
   { mk_pp (PPconst $1) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
560 561 562
| TRUE
   { mk_pp PPtrue }
| FALSE
563
   { mk_pp PPfalse }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
564 565
| LEFTPAR lexpr RIGHTPAR
   { $2 }
566 567 568 569
| LEFTPAR RIGHTPAR
   { mk_pp (PPtuple []) }
| LEFTPAR lexpr COMMA list1_lexpr_sep_comma RIGHTPAR
   { mk_pp (PPtuple ($2 :: $4)) }
570
| OPPREF lexpr_arg
571
   { mk_pp (PPapp (Qident (mk_id (prefix $1) (loc_i 2)), [$2])) }
Andrei Paskevich's avatar
Andrei Paskevich committed
572
;
573

Andrei Paskevich's avatar
Andrei Paskevich committed
574 575 576 577
quant:
| FORALL  { PPforall }
| EXISTS  { PPexists }
| LAMBDA  { PPlambda }
578 579
| FUNC    { PPfunc }
| PRED    { PPpred }
Andrei Paskevich's avatar
Andrei Paskevich committed
580 581
;

582
/* Triggers */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
583

584 585 586 587
triggers:
| /* epsilon */                         { [] }
| LEFTSQ list1_trigger_sep_bar RIGHTSQ  { $2 }
;
588

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
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 }
;

/* Match expressions */
604

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
605 606 607 608 609 610
match_cases:
| match_case                  { [$1] }
| match_case BAR match_cases  { $1::$3 }
;

match_case:
Andrei Paskevich's avatar
Andrei Paskevich committed
611
| pattern ARROW lexpr   { ($1,$3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
612 613 614
;

pattern:
Andrei Paskevich's avatar
Andrei Paskevich committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
| pat_conj              { $1 }
| pat_conj BAR pattern  { mk_pat (PPpor ($1, $3)) }
;

pat_conj:
| pat_uni                      { $1 }
| pat_uni COMMA list1_pat_uni  { mk_pat (PPptuple ($1::$3)) }
;

list1_pat_uni:
| pat_uni                      { [$1] }
| pat_uni COMMA list1_pat_uni  { $1::$3 }
;

pat_uni:
630 631 632
| pat_arg                   { $1 }
| uqualid list1_pat_arg     { mk_pat (PPpapp ($1, $2)) }
| pat_uni AS lident labels  { mk_pat (PPpas ($1, add_lab $3 $4)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
633
;
634

635
list1_pat_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
636 637 638
| pat_arg                { [$1] }
| pat_arg list1_pat_arg  { $1::$2 }
;
639

640
pat_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
641
| UNDERSCORE                { mk_pat (PPpwild) }
642
| lident labels             { mk_pat (PPpvar (add_lab $1 $2)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
643 644 645
| uqualid                   { mk_pat (PPpapp ($1, [])) }
| LEFTPAR RIGHTPAR          { mk_pat (PPptuple []) }
| LEFTPAR pattern RIGHTPAR  { $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
646 647
;

648
/* Parameters */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
649

650 651 652
params:
| /* epsilon */   { [] }
| param params    { $1 @ $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
653 654
;

655 656 657 658 659 660 661 662 663 664 665 666 667
param:
| LEFTPAR param_var RIGHTPAR
   { $2 }
| LEFTPAR param_type RIGHTPAR
   { [None, $2] }
| LEFTPAR param_type COMMA list1_primitive_type_sep_comma RIGHTPAR
   { [None, PPTtuple ($2::$4)] }
| LEFTPAR RIGHTPAR
   { [None, PPTtuple []] }
| type_var
   { [None, PPTtyvar $1] }
| lqualid
   { [None, PPTtyapp ([], $1)] }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
668 669
;

670 671 672 673 674 675 676 677
param_type:
| lident param_type_cont
   { PPTtyapp ($2, Qident $1) }
| lident list1_lident param_type_cont
   { let id2ty i = PPTtyapp ([], Qident i) in
     PPTtyapp (List.map id2ty $2 @ $3, Qident $1) }
| primitive_type_non_lident
   { $1 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
678 679
;

680 681 682 683
param_type_cont:
| /* epsilon */                                      { [] }
| primitive_type_arg_non_lident                      { [$1] }
| primitive_type_arg_non_lident primitive_type_args  { $1 :: $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
684 685
;

686 687 688
list1_param_var_sep_comma:
| param_var                                  { $1 }
| param_var COMMA list1_param_var_sep_comma  { $1 @ $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
689 690
;

691 692 693
param_var:
| list1_lident COLON primitive_type
   { List.map (fun id -> (Some id, $3)) $1 }
694 695 696 697 698 699
| list1_lident label labels list0_lident_labels COLON primitive_type
   { let l = match List.rev $1 with
       | i :: l -> add_lab i ($2 :: $3) :: l
       | [] -> assert false
     in
     List.map (fun id -> (Some id, $6)) (List.rev_append l $4) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
700 701
;

702
list1_lident:
703 704
| lident               { [$1] }
| lident list1_lident  { $1 :: $2 }
705 706
;

707 708 709 710 711
list0_lident_labels:
| /* epsilon */                      { [] }
| lident labels list0_lident_labels  { add_lab $1 $2 :: $3 }
;

712 713 714 715 716
/* Idents */

ident:
| uident { $1 }
| lident { $1 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
717 718
;

719 720 721
ident_rich:
| uident      { $1 }
| lident_rich { $1 }
722 723
;

724 725 726 727
lident_rich:
| lident
    { $1 }
| LEFTPAR lident_op RIGHTPAR
728
    { mk_id (infix $2) (loc ()) }
729
| LEFTPAR_STAR_RIGHTPAR
730
    { mk_id (infix "*") (loc ()) }
731
| LEFTPAR lident_op UNDERSCORE RIGHTPAR
732
    { mk_id (prefix $2) (loc ()) }
733
| LEFTPAR OPPREF RIGHTPAR
734
    { mk_id (prefix $2) (loc ()) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
735 736
;

737
lident:
738
| LIDENT { mk_id $1 (loc ()) }
739
| MODEL  { mk_id "model" (loc ()) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
740 741
;

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
lident_op:
| OP1   { $1 }
| OP2   { $1 }
| OP3   { $1 }
| OP4   { $1 }
| EQUAL { "=" }
;

any_op:
| OP1   { $1 }
| OP2   { $1 }
| OP3   { $1 }
| OP4   { $1 }
;

uident:
758
| UIDENT { mk_id $1 (loc ()) }
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
;

lq_lident:
| lident             { Qident $1 }
;

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

lqualid:
| lq_lident { $1 }
| lq_uident { $1 }
;

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

any_qualid:
| ident                { Qident $1 }
| any_qualid DOT ident { Qdot ($1, $3) }
;

tqualid:
| uident                { Qident $1 }
| any_qualid DOT uident { Qdot ($1, $3) }
Andrei Paskevich's avatar
Andrei Paskevich committed
787
;
788 789 790 791

qualid:
| ident_rich             { Qident $1 }
| uqualid DOT ident_rich { Qdot ($1, $3) }
Andrei Paskevich's avatar
Andrei Paskevich committed
792
;
793 794 795

/* Misc */

796 797 798 799 800 801 802 803 804 805
label:
| STRING
   { Ident.label $1 }
| STRING BACKQUOTE INTEGER BACKQUOTE INTEGER
         BACKQUOTE INTEGER BACKQUOTE INTEGER BACKQUOTE STRING
   { let loc = user_loc $11 (int_of_string $3) (int_of_string $5)
                            (int_of_string $7) (int_of_string $9) in
     Ident.label ~loc $1 }
;

806 807 808 809 810
labels:
| /* epsilon */ { [] }
| label labels  { $1 :: $2 }
;

811 812 813
bar_:
| /* epsilon */ { () }
| BAR           { () }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
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
/****************************************************************************/

program_file:
| list0_module_ EOF { $1 }
;

list0_module_:
| /* epsilon */
   { [] }
| list1_module_
   { $1 }
;

list1_module_:
| module_
   { [$1] }
| module_ list1_module_
   { $1 :: $2 }
;

module_:
| MODULE uident labels list0_program_decl END
   { { mod_name = $2; mod_labels = $3; mod_decl = $4 } }
;

list0_program_decl:
| /* epsilon */
   { [] }
| list1_program_decl
   { $1 }
;

list1_program_decl:
| program_decl
   { [$1] }
| program_decl list1_program_decl
   { $1 :: $2 }
;

program_decl:
| decl
    { Dlogic $1 }
858
| LET lident_rich labels list1_type_v_binder opt_cast EQUAL triple
859
    { Dlet (add_lab $2 $3, mk_expr_i 7 (Efun ($4, cast_body $5 $7))) }
860
| LET lident_rich labels EQUAL FUN list1_type_v_binder ARROW triple
861 862 863
    { Dlet (add_lab $2 $3, mk_expr_i 8 (Efun ($6, $8))) }
| LET REC list1_recfun_sep_and
    { Dletrec $3 }
864
| PARAMETER lident_rich labels COLON type_v
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
    { Dparam (add_lab $2 $3, $5) }
| EXCEPTION uident labels
    { Dexn (add_lab $2 $3, None) }
| EXCEPTION uident labels pure_type
    { Dexn (add_lab $2 $3, Some $4) }
| USE use_module
    { $2 }
| NAMESPACE namespace_import uident list0_program_decl END
    { Dnamespace ($3, $2, $4) }
| MUTABLE TYPE lident type_args model
    { Dmutable_type ($3, $4, $5) }
;

use_module:
| imp_exp MODULE tqualid
    { Duse ($3, $1, None) }
| imp_exp MODULE tqualid AS uident
    { Duse ($3, $1, Some $5) }
;

model:
| /* epsilon */   { None }
| MODEL pure_type { Some $2 }
;

list1_recfun_sep_and:
| recfun                           { [ $1 ] }
| recfun WITH list1_recfun_sep_and { $1 :: $3 }
;

recfun:
896
| lident_rich labels list1_type_v_binder opt_cast opt_variant EQUAL triple
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
   { add_lab $1 $2, $3, $5, cast_body $4 $7 }
;

expr:
| simple_expr %prec prec_simple
   { $1 }
| expr EQUAL expr
   { mk_infix $1 "=" $3 }
| expr LTGT expr
   { let t = mk_infix $1 "=" $3 in
     mk_expr (mk_apply_id { id = "notb"; id_lab = []; id_loc = loc () } [t]) }
| expr OP1 expr
   { mk_infix $1 $2 $3 }
| expr OP2 expr
   { mk_infix $1 $2 $3 }
| expr OP3 expr
   { mk_infix $1 $2 $3 }
| expr OP4 expr
   { mk_infix $1 $2 $3 }
| NOT expr %prec prefix_op
   { mk_expr (mk_apply_id { id = "notb"; id_lab = []; id_loc = loc () } [$2]) }
| any_op expr %prec prefix_op
   { mk_prefix $1 $2 }
/*
| expr COLONEQUAL expr
   { mk_infix $1 ":=" $3 }
*/
| simple_expr list1_simple_expr %prec prec_app
   { mk_expr (mk_apply $1 $2) }
| IF expr THEN expr ELSE expr
   { mk_expr (Eif ($2, $4, $6)) }
| IF expr THEN expr %prec prec_no_else
   { mk_expr (Eif ($2, $4, mk_expr Eskip)) }
| expr SEMICOLON expr
   { mk_expr (Esequence ($1, $3)) }
| assertion_kind annotation
   { mk_expr (Eassert ($1, $2)) }
| expr AMPAMP expr
   { mk_expr (Elazy (LazyAnd, $1, $3)) }
| expr BARBAR expr
   { mk_expr (Elazy (LazyOr, $1, $3)) }
| LET pattern EQUAL expr IN expr
   { match $2.pat_desc with
       | PPpvar id -> mk_expr (Elet (id, $4, $6))
       | _ -> mk_expr (Ematch ($4, [$2, $6])) }
| LET lident labels list1_type_v_binder EQUAL triple IN expr
   { mk_expr (Elet (add_lab $2 $3, mk_expr_i 6 (Efun ($4, $6)), $8)) }
| LET REC list1_recfun_sep_and IN expr
   { mk_expr (Eletrec ($3, $5)) }
| FUN list1_type_v_binder ARROW triple
   { mk_expr (Efun ($2, $4)) }
| MATCH expr WITH bar_ program_match_cases END
   { mk_expr (Ematch ($2, $5)) }
| MATCH expr COMMA list1_expr_sep_comma WITH bar_ program_match_cases END
   { mk_expr (Ematch (mk_expr (Etuple ($2::$4)), $7)) }
| LABEL uident COLON expr
   { mk_expr (Elabel ($2, $4)) }
| WHILE expr DO loop_annotation expr DONE
   { mk_expr
       (Etry
	  (mk_expr
	     (Eloop ($4,
		     mk_expr (Eif ($2, $5,
				   mk_expr (Eraise (exit_exn (), None)))))),
	   [exit_exn (), None, mk_expr Eskip])) }
| FOR lident EQUAL expr for_direction expr DO loop_invariant expr DONE
   { mk_expr (Efor ($2, $4, $5, $6, $8, $9)) }
| ABSURD
   { mk_expr Eabsurd }
| expr COLON pure_type
   { mk_expr (Ecast ($1, $3)) }
| RAISE uqualid
   { mk_expr (Eraise ($2, None)) }
| RAISE LEFTPAR uqualid expr RIGHTPAR
   { mk_expr (Eraise ($3, Some $4)) }
| TRY expr WITH bar_ list1_handler_sep_bar END
   { mk_expr (Etry ($2, $5)) }
| ANY simple_type_c
   { mk_expr (Eany $2) }
| LEFTPAR expr COMMA list1_expr_sep_comma RIGHTPAR
   { mk_expr (Etuple ($2 :: $4)) }
;

triple:
| pre expr post
  { $1, $2, $3 }
| expr %prec prec_triple
  { mk_pp PPtrue, $1, (mk_pp PPtrue, []) }
;

simple_expr:
| constant
    { mk_expr (Econstant $1) }
| qualid
    { mk_expr (Eident $1) }
| LEFTPAR expr RIGHTPAR
    { $2 }
| BEGIN expr END
    { $2 }
| LEFTPAR RIGHTPAR
    { mk_expr Eskip }
| BEGIN END
    { mk_expr Eskip }
| OPPREF simple_expr
    { mk_prefix $1 $2 }
;

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

list1_expr_sep_comma:
| expr                            { [$1] }
| expr COMMA list1_expr_sep_comma { $1 :: $3 }
;

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

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

program_match_cases:
| program_match_case                          { [$1] }
| program_match_case BAR program_match_cases  { $1::$3 }
;

program_match_case:
| pattern ARROW expr   { ($1,$3) }
;

assertion_kind:
| ASSERT { Aassert }
| ASSUME { Aassume }
| CHECK  { Acheck  }
;

for_direction:
| TO     { To }
| DOWNTO { Downto }
;

loop_annotation:
| loop_invariant opt_variant { { loop_invariant = $1; loop_variant = $2 } }
;

loop_invariant:
| INVARIANT annotation { Some $2 }
| /* epsilon */        { None    }
;

/* TODO: We should be able to reuse primitive_type for pure_type
pure_type:
| primitive_type { $1 }
;
*/

pure_type:
| pure_type_arg_no_paren { $1 }
| lqualid pure_type_args { PPTtyapp ($2, $1) }
;

pure_type_args:
| pure_type_arg                { [$1] }
| pure_type_arg pure_type_args { $1 :: $2 }
;

pure_type_arg:
| LEFTPAR pure_type RIGHTPAR { $2 }
| pure_type_arg_no_paren     { $1 }
;

pure_type_arg_no_paren:
| type_var
   { PPTtyvar $1 }
| lqualid
   { PPTtyapp ([], $1) }
| LEFTPAR pure_type COMMA list1_pure_type_sep_comma RIGHTPAR
   { PPTtuple ($2 :: $4) }
| LEFTPAR RIGHTPAR
   { PPTtuple ([]) }
;

list1_pure_type_sep_comma:
| pure_type                                 { [$1] }
| pure_type COMMA list1_pure_type_sep_comma { $1 :: $3 }
;

list1_type_v_binder:
| type_v_binder                     { $1 }
| type_v_binder list1_type_v_binder { $1 @ $2 }
;

type_v_binder:
| lident labels
   { [add_lab $1 $2, None] }
| LEFTPAR RIGHTPAR
   { [id_anonymous (), Some (ty_unit ())] }
| LEFTPAR lidents_lab COLON type_v RIGHTPAR
   { List.map (fun i -> (i, Some $4)) $2 }
;

lidents_lab:
| lident labels list0_lident_labels { add_lab $1 $2 :: $3 }
;

type_v:
| simple_type_v
   { $1 }
| simple_type_v ARROW type_c
   { Tarrow ([id_anonymous (), Some $1], $3) }
| lident labels COLON simple_type_v ARROW type_c
   { Tarrow ([add_lab $1 $2, Some $4], $6) }
   /* TODO: we could alllow lidents instead, e.g. x y : int -> ... */
   /*{ Tarrow (List.map (fun x -> x, Some $3) $1, $5) }*/
;

simple_type_v:
| pure_type
    { Tpure $1 }
| LEFTPAR type_v RIGHTPAR
    { $2 }
;

type_c:
| type_v
    { type_c (mk_pp PPtrue) $1 empty_effect (mk_pp PPtrue, []) }
| pre type_v effects post
    { type_c $1 $2 $3 $4 }
;

/* for ANY */
simple_type_c:
| pure_type
    { type_c (mk_pp PPtrue) (Tpure $1) empty_effect (mk_pp PPtrue, []) }
| LEFTPAR type_v RIGHTPAR
    { type_c (mk_pp PPtrue) $2 empty_effect (mk_pp PPtrue, []) }
| pre type_v effects post
    { type_c $1 $2 $3 $4 }
;

annotation:
1145
| LEFTBRC RIGHTBRC       { mk_pp PPtrue }
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
| LEFTBRC lexpr RIGHTBRC { $2 }
;

pre:
| annotation { $1 }
;

post:
| annotation list0_post_exn { $1, $2 }
;

list0_post_exn:
| /* epsilon */  %prec prec_post { [] }
| list1_post_exn                 { $1 }
;

list1_post_exn:
| post_exn                %prec prec_post { [$1] }
| post_exn list1_post_exn                 { $1 :: $2 }
;

post_exn:
| BAR uqualid ARROW annotation { $2, $4 }
;

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

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

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

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

opt_variant:
| /* epsilon */                   { None }
1193 1194
| VARIANT annotation              { Some ($2, None) }
| VARIANT annotation WITH lqualid { Some ($2, Some $4) }
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
;

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

list0_lqualid:
| /* epsilon */ { [] }
| list1_lqualid { $1 }
;

list1_lqualid:
| lqualid               { [$1] }
| lqualid list1_lqualid { $1 :: $2 }
;

list0_uqualid:
| /* epsilon */ { [] }
| list1_uqualid { $1 }
;

list1_uqualid:
| uqualid               { [$1] }
| uqualid list1_uqualid { $1 :: $2 }
;

/*
Local Variables:
compile-command: "unset LANG; make -C ../.."
End:
*/