parser.mly 33 KB
Newer Older
1 2
/**************************************************************************/
/*                                                                        */
3
/*  Copyright (C) 2010-2011                                               */
4 5 6
/*    François Bobot                                                      */
/*    Jean-Christophe Filliâtre                                           */
/*    Claude Marché                                                       */
Jean-Christophe Filliâtre's avatar
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
module Incremental = struct
22 23 24
  let env_ref  = ref []
  let lenv_ref = ref []
  let uc_ref   = ref []
25
  let path_ref = ref []
26 27 28 29 30 31 32 33 34

  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

35 36 37 38
  let open_logic_file env path =
    ref_push env_ref env;
    ref_push path_ref path;
    ref_push lenv_ref Util.Mstr.empty
39 40

  let close_logic_file () =
41 42 43
    ref_drop path_ref;
    ref_drop env_ref;
    ref_pop lenv_ref
44 45

  let open_theory id =
46 47
    let path = ref_get path_ref in
    ref_push uc_ref (Theory.create_theory ~path (Denv.create_user_id id))
48 49 50 51 52 53 54 55 56 57 58 59

  let close_theory loc =
    let uc = ref_pop uc_ref in
    ref_set lenv_ref (Typing.close_theory loc (ref_get lenv_ref) uc)

  let open_namespace () =
    ref_set uc_ref (Theory.open_namespace (ref_get uc_ref))

  let close_namespace loc import name =
    ref_set uc_ref (Typing.close_namespace loc import name (ref_get uc_ref))

  let new_decl d =
60 61 62
    ref_set uc_ref (Typing.add_decl (ref_get uc_ref) d)

  let new_use_clone d =
63
    let env = ref_get env_ref in let lenv = ref_get lenv_ref in
64
    ref_set uc_ref (Typing.add_use_clone env lenv (ref_get uc_ref) d)
65

66
end
67

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
68 69 70 71
  open Ptree
  open Parsing

  let loc () = (symbol_start_pos (), symbol_end_pos ())
72 73
  let floc () = Loc.extract (loc ())

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
74
  let loc_i i = (rhs_start_pos i, rhs_end_pos i)
75
  let floc_i i = Loc.extract (loc_i i)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
76
  let loc_ij i j = (rhs_start_pos i, rhs_end_pos j)
77
  let floc_ij i j = Loc.extract (loc_ij i j)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
78 79

  let mk_ppl loc d = { pp_loc = loc; pp_desc = d }
80 81
  let mk_pp d = mk_ppl (floc ()) d
  let mk_pp_i i d = mk_ppl (floc_i i) d
82

83
  let mk_pat p = { pat_loc = floc (); pat_desc = p }
Andrei Paskevich's avatar
Andrei Paskevich committed
84

85
  let infix_ppl loc a i b = mk_ppl loc (PPbinop (a, i, b))
86
  let infix_pp a i b = infix_ppl (floc ()) a i b
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
87

88
  let prefix_ppl loc p a = mk_ppl loc (PPunop (p, a))
89
  let prefix_pp p a = prefix_ppl (floc ()) p a
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
90

91
  let infix  s = "infix "  ^ s
92
  let prefix s = "prefix " ^ s
93
  let mixfix s = "mixfix " ^ s
94

95 96
  let quote id = { id with id = "'" ^ id.id }

97 98 99 100
  let mk_id id loc = { id = id; id_lab = []; id_loc = loc }

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

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  let mk_l_prefix op e1 =
    let id = mk_id (prefix op) (floc_i 1) in
    mk_pp (PPapp (Qident id, [e1]))

  let mk_l_infix e1 op e2 =
    let id = mk_id (infix op) (floc_i 2) in
    mk_pp (PPinfix (e1, id, e2))

  let mk_l_mixfix2 op e1 e2 =
    let id = mk_id (mixfix op) (floc_i 2) in
    mk_pp (PPapp (Qident id, [e1;e2]))

  let mk_l_mixfix3 op e1 e2 e3 =
    let id = mk_id (mixfix op) (floc_i 2) in
    mk_pp (PPapp (Qident id, [e1;e2;e3]))

117 118 119 120 121
  let () = Exn_printer.register
    (fun fmt exn -> match exn with
      | Parsing.Parse_error -> Format.fprintf fmt "syntax error"
      | _ -> raise exn
    )
122

123 124
  let mk_expr d = { expr_loc = floc (); expr_desc = d }
  let mk_expr_i i d = { expr_loc = floc_i i; expr_desc = d }
125 126 127 128 129 130 131

  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 rec mk_apply f = function
    | [] ->
132
        assert false
133
    | [a] ->
134
        Eapply (f, a)
135
    | a :: l ->
136 137
        let loc = Loc.join f.expr_loc a.expr_loc in
        mk_apply { expr_loc = loc; expr_desc = Eapply (f, a) } l
138 139 140 141 142 143 144

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

145 146
  let mk_mixfix2 op e1 e2 =
    let id = mk_id (mixfix op) (floc_i 2) in
147 148
    mk_expr (mk_apply_id id [e1; e2])

149 150
  let mk_mixfix3 op e1 e2 e3 =
    let id = mk_id (mixfix op) (floc_i 2) in
151 152
    mk_expr (mk_apply_id id [e1; e2; e3])

153
  let mk_infix e1 op e2 =
154
    let id = mk_id (infix op) (floc_i 2) in
155 156 157
    mk_expr (mk_apply_id id [e1; e2])

  let mk_prefix op e1 =
158
    let id = mk_id (prefix op) (floc_i 1) in
159 160
    mk_expr (mk_apply_id id [e1])

161 162
  let exit_exn () = Qident (mk_id "%Exit" (floc ()))
  let id_anonymous () = mk_id "_" (floc ())
163
  let ty_unit () = Tpure (PPTtuple [])
164

165
  let id_lt_nat () = Qident (mk_id "lt_nat" (floc ()))
166 167 168 169 170 171 172 173

  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; }
174

175
  let add_init_mark e =
176
    let init = { id = "Init"; id_lab = []; id_loc = e.expr_loc } in
177
    { e with expr_desc = Emark (init, e) }
178

179 180 181 182 183 184 185 186 187
  let small_integer i =
    try
      match i with
      | Term.IConstDecimal s -> int_of_string s
      | Term.IConstHexa    s -> int_of_string ("0x"^s)
      | Term.IConstOctal   s -> int_of_string ("0o"^s)
      | Term.IConstBinary  s -> int_of_string ("0b"^s)
    with Failure _ -> raise Parsing.Parse_error

188 189 190
  let qualid_last = function
    | Qident x | Qdot (_, x) -> x.id

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
191 192
%}

193
/* Tokens */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
194

195
%token <string> LIDENT UIDENT
196
%token <Ptree.integer_constant> INTEGER
197
%token <string> OP1 OP2 OP3 OP4 OPPREF
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
198 199
%token <Ptree.real_constant> FLOAT
%token <string> STRING
200
%token <Loc.position> POSITION
201 202 203

/* keywords */

204
%token AS AXIOM CLONE CONSTANT
Andrei Paskevich's avatar
Andrei Paskevich committed
205 206 207
%token ELSE END EPSILON EXISTS EXPORT FALSE FORALL FUNCTION
%token GOAL IF IMPORT IN INDUCTIVE LEMMA
%token LET MATCH META NAMESPACE NOT PROP PREDICATE
208
%token THEN THEORY TRUE TYPE USE WITH
209

210 211
/* program keywords */

212 213
%token ABSTRACT ABSURD ANY ASSERT ASSUME BEGIN CHECK DO DONE DOWNTO
%token EXCEPTION FOR
214
%token FUN INVARIANT LOOP MODEL MODULE MUTABLE RAISE
215
%token RAISES READS REC TO TRY VAL VARIANT WHILE WRITES
216

217 218
/* symbols */

Andrei Paskevich's avatar
Andrei Paskevich committed
219
%token AND ARROW
220
%token BAR
221
%token COLON COMMA
222
%token DOT EQUAL FUNC LAMBDA LTGT
223
%token LEFTPAR LEFTPAR_STAR_RIGHTPAR LEFTREC LEFTSQ
224
%token LARROW LRARROW
Andrei Paskevich's avatar
Andrei Paskevich committed
225
%token OR PRED QUOTE
226
%token RIGHTPAR RIGHTREC RIGHTSQ
Andrei Paskevich's avatar
Andrei Paskevich committed
227
%token UNDERSCORE
228 229 230

%token EOF

231 232
/* program symbols */

233
%token AMPAMP BARBAR LEFTBRC RIGHTBRC SEMICOLON
234

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
235 236
/* Precedences */

237
%nonassoc prec_mark
238 239 240 241 242 243 244 245 246 247
%nonassoc prec_post
%nonassoc BAR

%nonassoc prec_triple
%nonassoc prec_simple

%nonassoc IN
%right SEMICOLON
%nonassoc prec_no_else
%nonassoc DOT ELSE
248
%nonassoc prec_named
249
%nonassoc COLON
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
250

Andrei Paskevich's avatar
Andrei Paskevich committed
251
%right ARROW LRARROW
252 253
%right OR BARBAR
%right AND AMPAMP
Andrei Paskevich's avatar
Andrei Paskevich committed
254
%nonassoc NOT
255
%left EQUAL LTGT OP1
256 257
%nonassoc LARROW
%nonassoc RIGHTSQ    /* stronger than <- for e1[e2 <- e3] */
258
%left OP2
259
%left OP3
260
%left OP4
261
%nonassoc prec_prefix_op
262
%left prec_app
263 264
%nonassoc LEFTSQ
%nonassoc OPPREF
265

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
266 267
/* Entry points */

268
%type <unit Env.library -> string list -> unit> pre_logic_file
269
%start pre_logic_file
270
%type <Theory.theory Util.Mstr.t> logic_file
271
%start logic_file
272 273
%type <Ptree.program_file> program_file
%start program_file
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
274 275
%%

276 277 278 279
pre_logic_file:
| /* epsilon */  { Incremental.open_logic_file }
;

280
logic_file:
281
| list0_theory EOF  { Incremental.close_logic_file () }
282 283 284
;

/* File, theory, namespace */
285

286 287 288
list0_theory:
| /* epsilon */         { () }
| theory list0_theory   { () }
289 290
;

291
theory_head:
292
| THEORY uident labels  { Incremental.open_theory (add_lab $2 $3) }
293 294
;

295
theory:
296
| theory_head list0_decl END  { Incremental.close_theory (floc_i 1) }
297 298
;

299
list0_decl:
300 301 302 303 304 305
| /* epsilon */        { () }
| new_decl list0_decl  { () }
;

new_decl:
| decl
306
   { Incremental.new_decl $1 }
307 308
| use_clone
   { Incremental.new_use_clone $1 }
309
| namespace_head namespace_import namespace_name list0_decl END
310
   { Incremental.close_namespace (floc_i 3) $2 $3 }
311 312
;

313
namespace_head:
314
| NAMESPACE  { Incremental.open_namespace () }
315 316 317 318 319 320 321 322
;

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

namespace_name:
323
| uident      { Some $1.id }
324 325 326 327 328
| UNDERSCORE  { None }
;

/* Declaration */

329
decl:
330 331
| TYPE list1_type_decl
    { TypeDecl $2 }
332 333
| CONSTANT logic_decl_constant
    { LogicDecl [$2] }
Andrei Paskevich's avatar
Andrei Paskevich committed
334 335 336
| FUNCTION list1_logic_decl_function
    { LogicDecl $2 }
| PREDICATE list1_logic_decl_predicate
337 338 339
    { LogicDecl $2 }
| INDUCTIVE list1_inductive_decl
    { IndDecl $2 }
340
| AXIOM ident labels COLON lexpr
341
    { PropDecl (floc (), Kaxiom, add_lab $2 $3, $5) }
342
| LEMMA ident labels COLON lexpr
343
    { PropDecl (floc (), Klemma, add_lab $2 $3, $5) }
344
| GOAL ident labels COLON lexpr
345
    { PropDecl (floc (), Kgoal, add_lab $2 $3, $5) }
346
| META sident list1_meta_arg_sep_comma
347
    { Meta (floc (), $2, $3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
348 349
;

350 351
/* Use and clone */

352 353 354 355 356 357 358
use_clone:
| USE use
    { (floc (), $2, None) }
| CLONE use clone_subst
    { (floc (), $2, Some $3) }
;

359 360
use:
| imp_exp tqualid
361
    { { use_theory = $2; use_as = Some (qualid_last $2); use_imp_exp = $1 } }
362
| imp_exp tqualid AS uident
363
    { { use_theory = $2; use_as = Some $4.id; use_imp_exp = $1 } }
364
| imp_exp tqualid AS UNDERSCORE
365
    { { use_theory = $2; use_as = None; use_imp_exp = $1 } }
366 367
;

368
imp_exp:
369 370 371
| IMPORT        { Some true }
| EXPORT        { None }
| /* epsilon */ { Some false }
372 373 374 375 376 377 378 379 380 381 382 383 384
;

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

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

subst:
385 386 387 388 389 390 391
| NAMESPACE ns     EQUAL ns     { CSns   (floc (), $2, $4) }
| TYPE      qualid EQUAL qualid { CStsym (floc (), $2, $4) }
| CONSTANT  qualid EQUAL qualid { CSfsym (floc (), $2, $4) }
| FUNCTION  qualid EQUAL qualid { CSfsym (floc (), $2, $4) }
| PREDICATE qualid EQUAL qualid { CSpsym (floc (), $2, $4) }
| LEMMA     qualid              { CSlemma (floc (), $2) }
| GOAL      qualid              { CSgoal  (floc (), $2) }
392 393
;

394 395 396 397 398
ns:
| uqualid { Some $1 }
| DOT     { None }
;

399 400 401 402 403 404 405 406
/* Meta args */

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

meta_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
407
| TYPE      qualid { PMAts  $2 }
408 409
| FUNCTION  qualid { PMAfs  $2 }
| PREDICATE qualid { PMAps  $2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
410 411
| PROP      qualid { PMApr  $2 }
| STRING           { PMAstr $1 }
412
| INTEGER          { PMAint (small_integer $1) }
413 414
;

415 416 417
/* Type declarations */

list1_type_decl:
418 419
| type_decl                       { [$1] }
| type_decl WITH list1_type_decl  { $1 :: $3 }
420 421 422
;

type_decl:
423
| lident labels type_args typedefn
424 425 426
  { let model, def = $4 in
    { td_loc = floc (); td_ident = add_lab $1 $2;
      td_params = $3; td_model = model; td_def = def } }
427 428
;

429
type_args:
430 431
| /* epsilon */             { [] }
| type_var labels type_args { add_lab $1 $2 :: $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
432 433 434
;

typedefn:
435 436 437 438 439 440 441 442 443 444
| /* epsilon */                 { false, TDabstract }
| equal_model primitive_type    { $1, TDalias $2 }
| equal_model typecases         { $1, TDalgebraic $2 }
| equal_model BAR typecases     { $1, TDalgebraic $3 }
| equal_model record_type       { $1, TDrecord $2 }
;

equal_model:
| EQUAL { false }
| MODEL { true }
445 446 447
;

record_type:
Andrei Paskevich's avatar
Andrei Paskevich committed
448
| LEFTREC list1_record_field opt_semicolon RIGHTREC { List.rev $2 }
449 450 451 452
;

list1_record_field:
| record_field                              { [$1] }
453
| list1_record_field SEMICOLON record_field { $3 :: $1 }
454 455 456
;

record_field:
457
| opt_mutable lident labels COLON primitive_type
458
   { floc (), $1, add_lab $2 $3, $5 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
459 460 461 462 463 464 465 466
;

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

typecase:
467
| uident labels params   { (floc (), add_lab $1 $2, $3) }
468 469 470 471
;

/* Logic declarations */

Andrei Paskevich's avatar
Andrei Paskevich committed
472 473 474 475 476 477 478 479 480 481
list1_logic_decl_function:
| logic_decl_function                        { [$1] }
| logic_decl_function WITH list1_logic_decl  { $1 :: $3 }
;

list1_logic_decl_predicate:
| logic_decl_predicate                        { [$1] }
| logic_decl_predicate WITH list1_logic_decl  { $1 :: $3 }
;

482
list1_logic_decl:
483 484
| logic_decl                        { [$1] }
| logic_decl WITH list1_logic_decl  { $1 :: $3 }
485 486
;

487 488 489 490 491 492
logic_decl_constant:
| lident_rich labels COLON primitive_type logic_def_option
  { { ld_loc = floc (); ld_ident = add_lab $1 $2;
      ld_params = []; ld_type = Some $4; ld_def = $5 } }
;

Andrei Paskevich's avatar
Andrei Paskevich committed
493 494 495 496 497 498 499 500 501 502 503 504
logic_decl_function:
| lident_rich labels params COLON primitive_type logic_def_option
  { { ld_loc = floc (); ld_ident = add_lab $1 $2;
      ld_params = $3; ld_type = Some $5; ld_def = $6 } }
;

logic_decl_predicate:
| lident_rich labels params logic_def_option
  { { ld_loc = floc (); ld_ident = add_lab $1 $2;
      ld_params = $3; ld_type = None; ld_def = $4 } }
;

505
logic_decl:
506
| lident_rich labels params logic_type_option logic_def_option
507
  { { ld_loc = floc (); ld_ident = add_lab $1 $2;
508
      ld_params = $3; ld_type = $4; ld_def = $5 } }
509 510 511 512 513 514 515 516 517 518
;

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
519 520
;

521 522 523
/* Inductive declarations */

list1_inductive_decl:
524 525
| inductive_decl                            { [$1] }
| inductive_decl WITH list1_inductive_decl  { $1 :: $3 }
526 527 528
;

inductive_decl:
529
| lident_rich labels params inddefn
530
  { { in_loc = floc (); in_ident = add_lab $1 $2;
531
      in_params = $3; in_def = $4 } }
Andrei Paskevich's avatar
Andrei Paskevich committed
532
;
533

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
534 535 536 537 538 539 540 541 542 543 544
inddefn:
| /* epsilon */       { [] }
| EQUAL bar_ indcases { $3 }
;

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

indcase:
545
| ident labels COLON lexpr { (floc (), add_lab $1 $2, $4) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
546 547
;

548 549 550 551 552 553 554 555
/* Type expressions */

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

primitive_type_non_lident:
556 557
| primitive_type_arg_non_lident           { $1 }
| uqualid DOT lident primitive_type_args  { PPTtyapp ($4, Qdot ($1, $3)) }
558 559 560 561 562 563 564 565
;

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

primitive_type_arg:
566
| lident                         { PPTtyapp ([], Qident $1) }
567 568 569 570
| primitive_type_arg_non_lident  { $1 }
;

primitive_type_arg_non_lident:
571 572
| uqualid DOT lident
   { PPTtyapp ([], Qdot ($1, $3)) }
573 574 575 576
| type_var
   { PPTtyvar $1 }
| LEFTPAR primitive_type COMMA list1_primitive_type_sep_comma RIGHTPAR
   { PPTtuple ($2 :: $4) }
577 578 579 580 581 582
| LEFTPAR RIGHTPAR
   { PPTtuple [] }
| LEFTPAR primitive_type RIGHTPAR
   { $2 }
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
583 584 585 586 587
list1_primitive_type_sep_comma:
| primitive_type                                      { [$1] }
| primitive_type COMMA list1_primitive_type_sep_comma { $1 :: $3 }
;

588
type_var:
589
| QUOTE lident { $2 }
590 591
;

592 593
/* Logic expressions */

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
594
lexpr:
595
| lexpr ARROW lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
596
   { infix_pp $1 PPimplies $3 }
597
| lexpr LRARROW lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
598
   { infix_pp $1 PPiff $3 }
599
| lexpr OR lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
600
   { infix_pp $1 PPor $3 }
601
| lexpr BARBAR lexpr
602
   { infix_pp (mk_pp (PPnamed (Lstr Term.asym_label, $1))) PPor $3 }
603
| lexpr AND lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
604
   { infix_pp $1 PPand $3 }
605
| lexpr AMPAMP lexpr
606
   { infix_pp (mk_pp (PPnamed (Lstr Term.asym_label, $1))) PPand $3 }
607
| NOT lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
608
   { prefix_pp PPnot $2 }
609
| lexpr EQUAL lexpr
610
   { mk_l_infix $1 "=" $3 }
611
| lexpr LTGT lexpr
612
   { prefix_pp PPnot (mk_l_infix $1 "=" $3) }
613
| lexpr OP1 lexpr
614
   { mk_l_infix $1 $2 $3 }
615
| lexpr OP2 lexpr
616
   { mk_l_infix $1 $2 $3 }
617
| lexpr OP3 lexpr
618
   { mk_l_infix $1 $2 $3 }
619
| lexpr OP4 lexpr
620
   { mk_l_infix $1 $2 $3 }
621
| prefix_op lexpr %prec prec_prefix_op
622
   { mk_l_prefix $1 $2 }
623 624
| qualid list1_lexpr_arg
   { mk_pp (PPapp ($1, $2)) }
625
| IF lexpr THEN lexpr ELSE lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
626
   { mk_pp (PPif ($2, $4, $6)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
627 628
| quant list1_param_var_sep_comma triggers DOT lexpr
   { mk_pp (PPquant ($1, $2, $3, $5)) }
629 630
| label lexpr %prec prec_named
   { mk_pp (PPnamed ($1, $2)) }
631
| LET pattern EQUAL lexpr IN lexpr
632 633
   { match $2.pat_desc with
       | PPpvar id -> mk_pp (PPlet (id, $4, $6))
Andrei Paskevich's avatar
Andrei Paskevich committed
634 635
       | _ -> mk_pp (PPmatch ($4, [$2, $6])) }
| MATCH lexpr WITH bar_ match_cases END
636
   { mk_pp (PPmatch ($2, $5)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
637 638
| MATCH lexpr COMMA list1_lexpr_sep_comma WITH bar_ match_cases END
   { mk_pp (PPmatch (mk_pp (PPtuple ($2::$4)), $7)) }
639 640
| EPSILON lident labels COLON primitive_type DOT lexpr
   { mk_pp (PPeps (add_lab $2 $3, $5, $7)) }
641
| lexpr COLON primitive_type
642
   { mk_pp (PPcast ($1, $3)) }
643
| lexpr_arg
644 645 646
   { $1 }
;

647 648 649 650 651 652 653 654 655
list1_field_value:
| field_value                             { [$1] }
| list1_field_value SEMICOLON field_value { $3 :: $1 }
;

field_value:
| lqualid EQUAL lexpr { $1, $3 }
;

656 657 658
list1_lexpr_arg:
| lexpr_arg                 { [$1] }
| lexpr_arg list1_lexpr_arg { $1::$2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
659
;
660

661
constant:
662 663
| INTEGER   { Term.ConstInt $1 }
| FLOAT     { Term.ConstReal $1 }
664 665
;

666
lexpr_arg:
667 668 669 670 671 672
| qualid            { mk_pp (PPvar $1) }
| constant          { mk_pp (PPconst $1) }
| TRUE              { mk_pp PPtrue }
| FALSE             { mk_pp PPfalse }
| OPPREF lexpr_arg  { mk_l_prefix $1 $2 }
| lexpr_sub         { $1 }
673
| QUOTE uident      { mk_pp (PPvar (Qident (quote $2))) }
674 675 676
;

lexpr_dot:
677 678 679
| lqualid_copy      { mk_pp (PPvar $1) }
| OPPREF lexpr_dot  { mk_l_prefix $1 $2 }
| lexpr_sub         { $1 }
680 681 682
;

lexpr_sub:
683
| lexpr_dot DOT lqualid_rich
684
   { mk_pp (PPapp ($3, [$1])) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
685 686
| LEFTPAR lexpr RIGHTPAR
   { $2 }
687 688 689 690
| LEFTPAR RIGHTPAR
   { mk_pp (PPtuple []) }
| LEFTPAR lexpr COMMA list1_lexpr_sep_comma RIGHTPAR
   { mk_pp (PPtuple ($2 :: $4)) }
691 692
| LEFTREC list1_field_value opt_semicolon RIGHTREC
   { mk_pp (PPrecord (List.rev $2)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
693 694
| LEFTREC lexpr_arg WITH list1_field_value opt_semicolon RIGHTREC
   { mk_pp (PPupdate ($2, List.rev $4)) }
695
| lexpr_arg LEFTSQ lexpr RIGHTSQ
696
   { mk_l_mixfix2 "[]" $1 $3 }
697
| lexpr_arg LEFTSQ lexpr LARROW lexpr RIGHTSQ
698
   { mk_l_mixfix3 "[<-]" $1 $3 $5 }
Andrei Paskevich's avatar
Andrei Paskevich committed
699
;
700

Andrei Paskevich's avatar
Andrei Paskevich committed
701 702 703 704
quant:
| FORALL  { PPforall }
| EXISTS  { PPexists }
| LAMBDA  { PPlambda }
705 706
| FUNC    { PPfunc }
| PRED    { PPpred }
Andrei Paskevich's avatar
Andrei Paskevich committed
707 708
;

709
/* Triggers */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
710

711 712 713 714
triggers:
| /* epsilon */                         { [] }
| LEFTSQ list1_trigger_sep_bar RIGHTSQ  { $2 }
;
715

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
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 */
731

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
732 733 734 735 736 737
match_cases:
| match_case                  { [$1] }
| match_case BAR match_cases  { $1::$3 }
;

match_case:
Andrei Paskevich's avatar
Andrei Paskevich committed
738
| pattern ARROW lexpr   { ($1,$3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
739 740 741
;

pattern:
Andrei Paskevich's avatar
Andrei Paskevich committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
| 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:
757 758 759
| 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
760
;
761

762
list1_pat_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
763 764 765
| pat_arg                { [$1] }
| pat_arg list1_pat_arg  { $1::$2 }
;
766

767
pat_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
768
| UNDERSCORE                { mk_pat (PPpwild) }
769
| lident labels             { mk_pat (PPpvar (add_lab $1 $2)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
770 771 772
| uqualid                   { mk_pat (PPpapp ($1, [])) }
| LEFTPAR RIGHTPAR          { mk_pat (PPptuple []) }
| LEFTPAR pattern RIGHTPAR  { $2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
773 774 775 776 777 778 779 780 781 782
| LEFTREC pfields RIGHTREC  { mk_pat (PPprec $2) }
;

pfields:
| pat_field opt_semicolon       { [$1] }
| pat_field SEMICOLON pfields   { $1::$3 }
;

pat_field:
| lqualid EQUAL pattern   { ($1, $3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
783 784
;

785
/* Parameters */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
786

787 788 789
params:
| /* epsilon */   { [] }
| param params    { $1 @ $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
790 791
;

792 793 794 795 796 797 798 799 800 801 802 803 804
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
805 806
;

807 808 809 810 811 812 813 814
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
815 816
;

817 818 819 820
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
821 822
;

823 824 825
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
826 827
;

828 829 830
param_var:
| list1_lident COLON primitive_type
   { List.map (fun id -> (Some id, $3)) $1 }
831 832 833 834 835 836
| 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
837 838
;

839
list1_lident:
840 841
| lident               { [$1] }
| lident list1_lident  { $1 :: $2 }
842 843
;

844 845 846 847 848
list0_lident_labels:
| /* epsilon */                      { [] }
| lident labels list0_lident_labels  { add_lab $1 $2 :: $3 }
;

849 850 851 852 853
/* Idents */

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

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
uident:
| UIDENT          { mk_id $1 (floc ()) }
;

lident:
| LIDENT          { mk_id $1 (floc ()) }
| lident_keyword  { mk_id $1 (floc ()) }
;

lident_keyword:
| MODEL           { "model" }
;

/* Idents + symbolic operations' names */

871 872 873
ident_rich:
| uident      { $1 }
| lident_rich { $1 }
874 875
;

876
lident_rich:
877 878 879
| lident                      { $1 }
| LEFTPAR lident_op RIGHTPAR  { mk_id $2 (floc ()) }
| LEFTPAR_STAR_RIGHTPAR       { mk_id (infix "*") (floc ()) }
880 881
;

882
lident_op:
883 884 885 886 887 888
| prefix_op             { infix $1 }
| prefix_op UNDERSCORE  { prefix $1 }
| EQUAL                 { infix "=" }
| OPPREF                { prefix $1 }
| LEFTSQ RIGHTSQ        { mixfix "[]" }
| LEFTSQ LARROW RIGHTSQ { mixfix "[<-]" }
889 890
;

891
prefix_op:
892 893 894 895 896 897
| OP1   { $1 }
| OP2   { $1 }
| OP3   { $1 }
| OP4   { $1 }
;

898
/* Qualified idents */
899

900 901 902
qualid:
| ident_rich              { Qident $1 }
| uqualid DOT ident_rich  { Qdot ($1, $3) }
903 904
;

905 906 907
lqualid_rich:
| lident_rich             { Qident $1 }
| uqualid DOT lident_rich { Qdot ($1, $3) }
908 909 910
;

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

915 916 917 918
/* copy of lqualid to avoid yacc conflicts */
lqualid_copy:
| lident              { Qident $1 }
| uqualid DOT lident  { Qdot ($1, $3) }
919 920
;

921 922 923
uqualid:
| uident              { Qident $1 }
| uqualid DOT uident  { Qdot ($1, $3) }
924 925
;

926 927
/* Theory/Module names */

928 929 930
tqualid:
| uident                { Qident $1 }
| any_qualid DOT uident { Qdot ($1, $3) }
Andrei Paskevich's avatar
Andrei Paskevich committed
931
;
932

933
any_qualid:
934 935 936 937 938 939 940
| sident                { Qident $1 }
| any_qualid DOT sident { Qdot ($1, $3) }
;

sident:
| ident   { $1 }
| STRING  { mk_id $1 (floc ()) }
Andrei Paskevich's avatar
Andrei Paskevich committed
941
;
942 943 944

/* Misc */

945
label:
Andrei Paskevich's avatar
Andrei Paskevich committed
946
| STRING    { Lstr (Ident.create_label $1) }
947
| POSITION  { Lpos $1 }
948 949
;

950 951 952 953 954
labels:
| /* epsilon */ { [] }
| label labels  { $1 :: $2 }
;

955 956 957
bar_:
| /* epsilon */ { () }
| BAR           { () }
958 959
;

960 961 962
/****************************************************************************/

program_file:
963
| list0_theory_or_module_ EOF { $1 }
964 965
;

966
list0_theory_or_module_:
967 968
| /* epsilon */
   { [] }
969
| list1_theory_or_module_
970 971 972
   { $1 }
;

973 974
list1_theory_or_module_:
| theory_or_module_
975
   { [$1] }
976
| theory_or_module_ list1_theory_or_module_
977 978 979
   { $1 :: $2 }
;

980 981
theory_or_module_:
| THEORY uident labels list0_full_decl END
982
   { Ptheory { pth_name = add_lab $2 $3; pth_decl = $4; } }
983
| MODULE uident labels list0_program_decl END
984
   { Pmodule { mod_name = add_lab $2 $3; mod_decl = $4; } }
985 986
;

987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
list0_full_decl:
| /* epsilon */
   { [] }
| list1_full_decl
   { $1 }
;

list1_full_decl:
| full_decl
   { [$1] }
| full_decl list1_full_decl
   { $1 :: $2 }
;

full_decl:
| decl
   { Dlogic $1 }
1004 1005 1006 1007
| use_clone
   { Duseclone $1 }
| NAMESPACE namespace_import namespace_name list0_full_decl END
   { Dnamespace (floc_i 3, $3, $2, $4) }
1008 1009
;

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
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 }
1027 1028
| use_clone
    { Duseclone $1 }
1029
| LET lident_rich_pgm labels list1_type_v_binder opt_cast EQUAL triple
1030
    { Dlet (add_lab $2 $3, mk_expr_i 7 (Efun ($4, cast_body $5 $7))) }
1031
| LET lident_rich_pgm labels EQUAL FUN list1_type_v_binder ARROW triple
1032 1033 1034
    { Dlet (add_lab $2 $3, mk_expr_i 8 (Efun ($6, $8))) }
| LET REC list1_recfun_sep_and
    { Dletrec $3 }
1035
| VAL lident_rich_pgm labels COLON type_v
1036
    { Dparam (add_lab $2 $3, $5) }
1037
| VAL lident_rich_pgm labels list1_type_v_param COLON type_c
1038 1039
    { let tv = Tarrow ($4, $6) in
      Dparam (add_lab $2 $3, tv) }
1040 1041
| EXCEPTION uident labels
    { Dexn (add_lab $2 $3, None) }
1042
| EXCEPTION uident labels primitive_type
1043 1044 1045
    { Dexn (add_lab $2 $3, Some $4) }
| USE use_module
    { $2 }
1046
| NAMESPACE namespace_import namespace_name list0_program_decl END
1047
    { Dnamespace (floc_i 3, $3, $2, $4) }
1048 1049
;

1050 1051 1052 1053
lident_rich_pgm:
| lident_rich
    { $1 }
| LEFTPAR LEFTSQ RIGHTSQ LARROW RIGHTPAR
1054
    { mk_id (mixfix "[]<-") (floc ()) }
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
;

opt_mutable:
| /* epsilon */ { false }
| MUTABLE       { true  }
;

opt_semicolon:
| /* epsilon */ {}
| SEMICOLON     {}
1065 1066 1067 1068
;

use_module:
| imp_exp MODULE tqualid
1069
    { Duse ($3, $1, Some (qualid_last $3)) }
1070
| imp_exp MODULE tqualid AS uident
1071
    { Duse ($3, $1, Some $5.id) }
Jean-Christophe Filliatre's avatar