parser.mly 32.9 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
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
module Incremental = struct
22 23 24 25 26 27 28 29 30 31 32 33
  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

34
  let open_logic_file env =
35
    ref_push env_ref env; ref_push lenv_ref Util.Mstr.empty
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

  let close_logic_file () =
    ref_drop env_ref; ref_pop lenv_ref

  let open_theory id =
    ref_push uc_ref (Theory.create_theory (Denv.create_user_id id))

  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 =
    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) d)
end
57

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
58 59 60 61
  open Ptree
  open Parsing

  let loc () = (symbol_start_pos (), symbol_end_pos ())
62 63
  let floc () = Loc.extract (loc ())

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
64
  let loc_i i = (rhs_start_pos i, rhs_end_pos i)
65
  let floc_i i = Loc.extract (loc_i i)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
66
  let loc_ij i j = (rhs_start_pos i, rhs_end_pos j)
67
  let floc_ij i j = Loc.extract (loc_ij i j)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
68 69

  let mk_ppl loc d = { pp_loc = loc; pp_desc = d }
70 71
  let mk_pp d = mk_ppl (floc ()) d
  let mk_pp_i i d = mk_ppl (floc_i i) d
72

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

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

78
  let prefix_ppl loc p a = mk_ppl loc (PPunop (p, a))
79
  let prefix_pp p a = prefix_ppl (floc ()) p a
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
80

81
  let infix  s = "infix "  ^ s
82
  let prefix s = "prefix " ^ s
Andrei Paskevich's avatar
Andrei Paskevich committed
83
  let mixfix s = "mixfix " ^ s
84

85 86
  let quote id = { id with id = "'" ^ id.id }

87 88 89 90
  let mk_id id loc = { id = id; id_lab = []; id_loc = loc }

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

Andrei Paskevich's avatar
Andrei Paskevich committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  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]))

107 108 109 110 111
  let () = Exn_printer.register
    (fun fmt exn -> match exn with
      | Parsing.Parse_error -> Format.fprintf fmt "syntax error"
      | _ -> raise exn
    )
112

113 114
  let mk_expr d = { expr_loc = floc (); expr_desc = d }
  let mk_expr_i i d = { expr_loc = floc_i i; expr_desc = d }
115 116 117 118 119 120 121

  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
    | [] ->
122
        assert false
123
    | [a] ->
124
        Eapply (f, a)
125
    | a :: l ->
126 127
        let loc = Loc.join f.expr_loc a.expr_loc in
        mk_apply { expr_loc = loc; expr_desc = Eapply (f, a) } l
128 129 130 131 132 133 134

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

Andrei Paskevich's avatar
Andrei Paskevich committed
135 136
  let mk_mixfix2 op e1 e2 =
    let id = mk_id (mixfix op) (floc_i 2) in
137 138
    mk_expr (mk_apply_id id [e1; e2])

Andrei Paskevich's avatar
Andrei Paskevich committed
139 140
  let mk_mixfix3 op e1 e2 e3 =
    let id = mk_id (mixfix op) (floc_i 2) in
141 142
    mk_expr (mk_apply_id id [e1; e2; e3])

143
  let mk_infix e1 op e2 =
Andrei Paskevich's avatar
Andrei Paskevich committed
144
    let id = mk_id (infix op) (floc_i 2) in
145 146 147
    mk_expr (mk_apply_id id [e1; e2])

  let mk_prefix op e1 =
Andrei Paskevich's avatar
Andrei Paskevich committed
148
    let id = mk_id (prefix op) (floc_i 1) in
149 150
    mk_expr (mk_apply_id id [e1])

Andrei Paskevich's avatar
Andrei Paskevich committed
151 152
  let exit_exn () = Qident (mk_id "%Exit" (floc ()))
  let id_anonymous () = mk_id "_" (floc ())
153
  let ty_unit () = Tpure (PPTtuple [])
154

Andrei Paskevich's avatar
Andrei Paskevich committed
155
  let id_lt_nat () = Qident (mk_id "lt_nat" (floc ()))
156 157 158 159 160 161 162 163

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

165
  let add_init_mark e =
166
    let init = { id = "Init"; id_lab = []; id_loc = e.expr_loc } in
167
    { e with expr_desc = Emark (init, e) }
168

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
169 170
%}

171
/* Tokens */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
172

173
%token <string> LIDENT UIDENT
174
%token <Ptree.integer_constant> INTEGER
175
%token <string> OP1 OP2 OP3 OP4 OPPREF
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
176 177
%token <Ptree.real_constant> FLOAT
%token <string> STRING
178
%token <Loc.position> POSITION
179 180 181

/* keywords */

Andrei Paskevich's avatar
Andrei Paskevich committed
182 183 184 185
%token AS AXIOM CLONE
%token ELSE END EPSILON EXISTS EXPORT FALSE FORALL FUNCTION
%token GOAL IF IMPORT IN INDUCTIVE LEMMA
%token LET MATCH META NAMESPACE NOT PROP PREDICATE
Andrei Paskevich's avatar
Andrei Paskevich committed
186
%token THEN THEORY TRUE TYPE USE WITH
187

188 189
/* program keywords */

190 191
%token ABSTRACT ABSURD ANY ASSERT ASSUME BEGIN CHECK DO DONE DOWNTO
%token EXCEPTION FOR
192
%token FUN INVARIANT LOOP MODEL MODULE MUTABLE RAISE
Andrei Paskevich's avatar
Andrei Paskevich committed
193
%token RAISES READS REC TO TRY VAL VARIANT WHILE WRITES
194

195 196
/* symbols */

Andrei Paskevich's avatar
Andrei Paskevich committed
197
%token AND ARROW
198
%token BAR
199
%token COLON COMMA
200
%token DOT EQUAL FUNC LAMBDA LTGT
201
%token LEFTPAR LEFTPAR_STAR_RIGHTPAR LEFTREC LEFTSQ
202
%token LARROW LRARROW
Andrei Paskevich's avatar
Andrei Paskevich committed
203
%token OR PRED QUOTE
204
%token RIGHTPAR RIGHTREC RIGHTSQ
Andrei Paskevich's avatar
Andrei Paskevich committed
205
%token UNDERSCORE
206 207 208

%token EOF

209 210
/* program symbols */

211
%token AMPAMP BARBAR LEFTBRC RIGHTBRC SEMICOLON
212

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
213 214
/* Precedences */

215
%nonassoc prec_mark
216 217 218 219 220 221 222 223 224 225
%nonassoc prec_post
%nonassoc BAR

%nonassoc prec_triple
%nonassoc prec_simple

%nonassoc IN
%right SEMICOLON
%nonassoc prec_no_else
%nonassoc DOT ELSE
226
%nonassoc prec_named
227
%nonassoc COLON
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
228

Andrei Paskevich's avatar
Andrei Paskevich committed
229
%right ARROW LRARROW
230 231
%right OR BARBAR
%right AND AMPAMP
Andrei Paskevich's avatar
Andrei Paskevich committed
232
%nonassoc NOT
233
%left EQUAL LTGT OP1
234 235
%nonassoc LARROW
%nonassoc RIGHTSQ    /* stronger than <- for e1[e2 <- e3] */
236
%left OP2
237
%left OP3
238
%left OP4
239
%nonassoc prec_prefix_op
240
%left prec_app
241 242
%nonassoc LEFTSQ
%nonassoc OPPREF
243

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
244 245
/* Entry points */

246 247
%type <Env.env -> unit> pre_logic_file
%start pre_logic_file
248
%type <Theory.theory Util.Mstr.t> logic_file
Andrei Paskevich's avatar
Andrei Paskevich committed
249
%start logic_file
250 251
%type <Ptree.program_file> program_file
%start program_file
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
252 253
%%

254 255 256 257
pre_logic_file:
| /* epsilon */  { Incremental.open_logic_file }
;

Andrei Paskevich's avatar
Andrei Paskevich committed
258
logic_file:
259
| list0_theory EOF  { Incremental.close_logic_file () }
260 261 262
;

/* File, theory, namespace */
263

264 265 266
list0_theory:
| /* epsilon */         { () }
| theory list0_theory   { () }
267 268
;

269
theory_head:
270
| THEORY uident labels  { Incremental.open_theory (add_lab $2 $3) }
271 272
;

273
theory:
274
| theory_head list0_decl END  { Incremental.close_theory (floc_i 1) }
275 276
;

277
list0_decl:
278 279 280 281 282 283
| /* epsilon */        { () }
| new_decl list0_decl  { () }
;

new_decl:
| decl
284
   { Incremental.new_decl $1 }
285
| namespace_head namespace_import namespace_name list0_decl END
286
   { Incremental.close_namespace (floc_i 3) $2 $3 }
287 288
;

289
namespace_head:
290
| NAMESPACE  { Incremental.open_namespace () }
291 292 293 294 295 296 297 298 299 300 301 302 303 304
;

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

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

/* Declaration */

305
decl:
306 307
| TYPE list1_type_decl
    { TypeDecl $2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
308 309 310
| FUNCTION list1_logic_decl_function
    { LogicDecl $2 }
| PREDICATE list1_logic_decl_predicate
311 312 313
    { LogicDecl $2 }
| INDUCTIVE list1_inductive_decl
    { IndDecl $2 }
314
| AXIOM ident labels COLON lexpr
315
    { PropDecl (floc (), Kaxiom, add_lab $2 $3, $5) }
316
| LEMMA ident labels COLON lexpr
317
    { PropDecl (floc (), Klemma, add_lab $2 $3, $5) }
318
| GOAL ident labels COLON lexpr
319
    { PropDecl (floc (), Kgoal, add_lab $2 $3, $5) }
320
| USE use
321
    { UseClone (floc (), $2, None) }
322
| CLONE use clone_subst
323
    { UseClone (floc (), $2, Some $3) }
324
| META ident list1_meta_arg_sep_comma
325
    { Meta (floc (), $2, $3) }
326
| META STRING list1_meta_arg_sep_comma
327
    { Meta (floc (), mk_id $2 (floc_i 2), $3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
328 329
;

330 331 332 333 334 335 336 337 338
/* 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
339 340
;

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
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:
Andrei Paskevich's avatar
Andrei Paskevich committed
358 359
| NAMESPACE ns     EQUAL ns     { CSns   ($2, $4) }
| TYPE      qualid EQUAL qualid { CStsym ($2, $4) }
360 361
| FUNCTION  qualid EQUAL qualid { CSfsym ($2, $4) }
| PREDICATE qualid EQUAL qualid { CSpsym ($2, $4) }
Andrei Paskevich's avatar
Andrei Paskevich committed
362 363
| LEMMA     qualid              { CSlemma $2 }
| GOAL      qualid              { CSgoal  $2 }
364 365
;

366 367 368 369 370
ns:
| uqualid { Some $1 }
| DOT     { None }
;

371 372 373 374 375 376 377 378
/* 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
379
| TYPE      qualid { PMAts  $2 }
380 381
| FUNCTION  qualid { PMAfs  $2 }
| PREDICATE qualid { PMAps  $2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
382 383
| PROP      qualid { PMApr  $2 }
| STRING           { PMAstr $1 }
384
| INTEGER          { PMAint (match $1 with Term.IConstDecimal s -> int_of_string s) }
385 386
;

387 388 389
/* Type declarations */

list1_type_decl:
390 391
| type_decl                       { [$1] }
| type_decl WITH list1_type_decl  { $1 :: $3 }
392 393 394
;

type_decl:
395
| lident labels type_args typedefn
396 397 398
  { let model, def = $4 in
    { td_loc = floc (); td_ident = add_lab $1 $2;
      td_params = $3; td_model = model; td_def = def } }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
399 400
;

401
type_args:
402 403
| /* epsilon */             { [] }
| type_var labels type_args { add_lab $1 $2 :: $3 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
404 405 406
;

typedefn:
407 408 409 410 411 412 413 414 415 416
| /* 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 }
417 418 419
;

record_type:
Andrei Paskevich's avatar
Andrei Paskevich committed
420
| LEFTREC list1_record_field opt_semicolon RIGHTREC { List.rev $2 }
421 422 423 424
;

list1_record_field:
| record_field                              { [$1] }
425
| list1_record_field SEMICOLON record_field { $3 :: $1 }
426 427 428
;

record_field:
429
| opt_mutable lident labels COLON primitive_type
430
   { floc (), $1, add_lab $2 $3, $5 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
431 432 433 434 435 436 437 438
;

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

typecase:
439
| uident labels params   { (floc (), add_lab $1 $2, $3) }
440 441 442 443
;

/* Logic declarations */

Andrei Paskevich's avatar
Andrei Paskevich committed
444 445 446 447 448 449 450 451 452 453
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 }
;

454
list1_logic_decl:
455 456
| logic_decl                        { [$1] }
| logic_decl WITH list1_logic_decl  { $1 :: $3 }
457 458
;

Andrei Paskevich's avatar
Andrei Paskevich committed
459 460 461 462 463 464 465 466 467 468 469 470
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 } }
;

471
logic_decl:
472
| lident_rich labels params logic_type_option logic_def_option
473
  { { ld_loc = floc (); ld_ident = add_lab $1 $2;
474
      ld_params = $3; ld_type = $4; ld_def = $5 } }
475 476 477 478 479 480 481 482 483 484
;

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
485 486
;

487 488 489
/* Inductive declarations */

list1_inductive_decl:
490 491
| inductive_decl                            { [$1] }
| inductive_decl WITH list1_inductive_decl  { $1 :: $3 }
492 493 494
;

inductive_decl:
495
| lident_rich labels params inddefn
496
  { { in_loc = floc (); in_ident = add_lab $1 $2;
497
      in_params = $3; in_def = $4 } }
Andrei Paskevich's avatar
Andrei Paskevich committed
498
;
499

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
500 501 502 503 504 505 506 507 508 509 510
inddefn:
| /* epsilon */       { [] }
| EQUAL bar_ indcases { $3 }
;

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

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

514 515 516 517 518 519 520 521
/* Type expressions */

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

primitive_type_non_lident:
Andrei Paskevich's avatar
Andrei Paskevich committed
522 523
| primitive_type_arg_non_lident           { $1 }
| uqualid DOT lident primitive_type_args  { PPTtyapp ($4, Qdot ($1, $3)) }
524 525 526 527 528 529 530 531
;

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

primitive_type_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
532
| lident                         { PPTtyapp ([], Qident $1) }
533 534 535 536
| primitive_type_arg_non_lident  { $1 }
;

primitive_type_arg_non_lident:
Andrei Paskevich's avatar
Andrei Paskevich committed
537 538
| uqualid DOT lident
   { PPTtyapp ([], Qdot ($1, $3)) }
539 540 541 542
| type_var
   { PPTtyvar $1 }
| LEFTPAR primitive_type COMMA list1_primitive_type_sep_comma RIGHTPAR
   { PPTtuple ($2 :: $4) }
543 544 545 546 547 548
| LEFTPAR RIGHTPAR
   { PPTtuple [] }
| LEFTPAR primitive_type RIGHTPAR
   { $2 }
;

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
549 550 551 552 553
list1_primitive_type_sep_comma:
| primitive_type                                      { [$1] }
| primitive_type COMMA list1_primitive_type_sep_comma { $1 :: $3 }
;

554
type_var:
555
| QUOTE lident { $2 }
556 557
;

558 559
/* Logic expressions */

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
560
lexpr:
561
| lexpr ARROW lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
562
   { infix_pp $1 PPimplies $3 }
563
| lexpr LRARROW lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
564
   { infix_pp $1 PPiff $3 }
565
| lexpr OR lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
566
   { infix_pp $1 PPor $3 }
567
| lexpr BARBAR lexpr
568
   { mk_pp (PPnamed (Lstr Term.asym_label, infix_pp $1 PPor $3)) }
569
| lexpr AND lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
570
   { infix_pp $1 PPand $3 }
571
| lexpr AMPAMP lexpr
572
   { mk_pp (PPnamed (Lstr Term.asym_label, infix_pp $1 PPand $3)) }
573
| NOT lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
574
   { prefix_pp PPnot $2 }
575
| lexpr EQUAL lexpr
Andrei Paskevich's avatar
Andrei Paskevich committed
576
   { mk_l_infix $1 "=" $3 }
577
| lexpr LTGT lexpr
Andrei Paskevich's avatar
Andrei Paskevich committed
578
   { prefix_pp PPnot (mk_l_infix $1 "=" $3) }
579
| lexpr OP1 lexpr
Andrei Paskevich's avatar
Andrei Paskevich committed
580
   { mk_l_infix $1 $2 $3 }
581
| lexpr OP2 lexpr
Andrei Paskevich's avatar
Andrei Paskevich committed
582
   { mk_l_infix $1 $2 $3 }
583
| lexpr OP3 lexpr
Andrei Paskevich's avatar
Andrei Paskevich committed
584
   { mk_l_infix $1 $2 $3 }
585
| lexpr OP4 lexpr
Andrei Paskevich's avatar
Andrei Paskevich committed
586
   { mk_l_infix $1 $2 $3 }
587
| prefix_op lexpr %prec prec_prefix_op
Andrei Paskevich's avatar
Andrei Paskevich committed
588
   { mk_l_prefix $1 $2 }
589 590
| qualid list1_lexpr_arg
   { mk_pp (PPapp ($1, $2)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
591
| IF lexpr THEN lexpr ELSE lexpr
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
592
   { mk_pp (PPif ($2, $4, $6)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
593 594
| quant list1_param_var_sep_comma triggers DOT lexpr
   { mk_pp (PPquant ($1, $2, $3, $5)) }
595 596
| label lexpr %prec prec_named
   { mk_pp (PPnamed ($1, $2)) }
597
| LET pattern EQUAL lexpr IN lexpr
598 599
   { match $2.pat_desc with
       | PPpvar id -> mk_pp (PPlet (id, $4, $6))
Andrei Paskevich's avatar
Andrei Paskevich committed
600 601
       | _ -> mk_pp (PPmatch ($4, [$2, $6])) }
| MATCH lexpr WITH bar_ match_cases END
602
   { mk_pp (PPmatch ($2, $5)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
603 604
| MATCH lexpr COMMA list1_lexpr_sep_comma WITH bar_ match_cases END
   { mk_pp (PPmatch (mk_pp (PPtuple ($2::$4)), $7)) }
605 606
| EPSILON lident labels COLON primitive_type DOT lexpr
   { mk_pp (PPeps (add_lab $2 $3, $5, $7)) }
607
| lexpr COLON primitive_type
608
   { mk_pp (PPcast ($1, $3)) }
609
| lexpr_arg
610 611 612
   { $1 }
;

613 614 615 616 617 618 619 620 621
list1_field_value:
| field_value                             { [$1] }
| list1_field_value SEMICOLON field_value { $3 :: $1 }
;

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

622 623 624
list1_lexpr_arg:
| lexpr_arg                 { [$1] }
| lexpr_arg list1_lexpr_arg { $1::$2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
625
;
626

627
constant:
Andrei Paskevich's avatar
Andrei Paskevich committed
628 629
| INTEGER   { Term.ConstInt $1 }
| FLOAT     { Term.ConstReal $1 }
630 631
;

632
lexpr_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
633 634 635 636 637 638
| 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 }
639
| QUOTE uident      { mk_pp (PPvar (Qident (quote $2))) }
Andrei Paskevich's avatar
Andrei Paskevich committed
640 641 642
;

lexpr_dot:
Andrei Paskevich's avatar
Andrei Paskevich committed
643 644 645
| lqualid_copy      { mk_pp (PPvar $1) }
| OPPREF lexpr_dot  { mk_l_prefix $1 $2 }
| lexpr_sub         { $1 }
Andrei Paskevich's avatar
Andrei Paskevich committed
646 647 648
;

lexpr_sub:
649
| lexpr_dot DOT lqualid_rich
Andrei Paskevich's avatar
Andrei Paskevich committed
650
   { mk_pp (PPapp ($3, [$1])) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
651 652
| LEFTPAR lexpr RIGHTPAR
   { $2 }
653 654 655 656
| LEFTPAR RIGHTPAR
   { mk_pp (PPtuple []) }
| LEFTPAR lexpr COMMA list1_lexpr_sep_comma RIGHTPAR
   { mk_pp (PPtuple ($2 :: $4)) }
657 658
| LEFTREC list1_field_value opt_semicolon RIGHTREC
   { mk_pp (PPrecord (List.rev $2)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
659 660
| LEFTREC lexpr_arg WITH list1_field_value opt_semicolon RIGHTREC
   { mk_pp (PPupdate ($2, List.rev $4)) }
661
| lexpr_arg LEFTSQ lexpr RIGHTSQ
Andrei Paskevich's avatar
Andrei Paskevich committed
662
   { mk_l_mixfix2 "[]" $1 $3 }
663
| lexpr_arg LEFTSQ lexpr LARROW lexpr RIGHTSQ
Andrei Paskevich's avatar
Andrei Paskevich committed
664
   { mk_l_mixfix3 "[<-]" $1 $3 $5 }
Andrei Paskevich's avatar
Andrei Paskevich committed
665
;
666

Andrei Paskevich's avatar
Andrei Paskevich committed
667 668 669 670
quant:
| FORALL  { PPforall }
| EXISTS  { PPexists }
| LAMBDA  { PPlambda }
671 672
| FUNC    { PPfunc }
| PRED    { PPpred }
Andrei Paskevich's avatar
Andrei Paskevich committed
673 674
;

675
/* Triggers */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
676

677 678 679 680
triggers:
| /* epsilon */                         { [] }
| LEFTSQ list1_trigger_sep_bar RIGHTSQ  { $2 }
;
681

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
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 */
697

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
698 699 700 701 702 703
match_cases:
| match_case                  { [$1] }
| match_case BAR match_cases  { $1::$3 }
;

match_case:
Andrei Paskevich's avatar
Andrei Paskevich committed
704
| pattern ARROW lexpr   { ($1,$3) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
705 706 707
;

pattern:
Andrei Paskevich's avatar
Andrei Paskevich committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
| 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:
723 724 725
| 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
726
;
727

728
list1_pat_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
729 730 731
| pat_arg                { [$1] }
| pat_arg list1_pat_arg  { $1::$2 }
;
732

733
pat_arg:
Andrei Paskevich's avatar
Andrei Paskevich committed
734
| UNDERSCORE                { mk_pat (PPpwild) }
735
| lident labels             { mk_pat (PPpvar (add_lab $1 $2)) }
Andrei Paskevich's avatar
Andrei Paskevich committed
736 737 738
| uqualid                   { mk_pat (PPpapp ($1, [])) }
| LEFTPAR RIGHTPAR          { mk_pat (PPptuple []) }
| LEFTPAR pattern RIGHTPAR  { $2 }
Andrei Paskevich's avatar
Andrei Paskevich committed
739 740 741 742 743 744 745 746 747 748
| 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
749 750
;

751
/* Parameters */
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
752

753 754 755
params:
| /* epsilon */   { [] }
| param params    { $1 @ $2 }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
756 757
;

758 759 760 761 762 763 764 765 766 767 768 769 770
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
771 772
;

773 774 775 776 777 778 779 780
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
781 782
;

783 784 785 786
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
787 788
;

789 790 791
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
792 793
;

794 795 796
param_var:
| list1_lident COLON primitive_type
   { List.map (fun id -> (Some id, $3)) $1 }
797 798 799 800 801 802
| 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
803 804
;

805
list1_lident:
806 807
| lident               { [$1] }
| lident list1_lident  { $1 :: $2 }
808 809
;

810 811 812 813 814
list0_lident_labels:
| /* epsilon */                      { [] }
| lident labels list0_lident_labels  { add_lab $1 $2 :: $3 }
;

815 816 817 818 819
/* Idents */

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

Andrei Paskevich's avatar
Andrei Paskevich committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
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 */

837 838 839
ident_rich:
| uident      { $1 }
| lident_rich { $1 }
840 841
;

842
lident_rich:
Andrei Paskevich's avatar
Andrei Paskevich committed
843 844 845
| lident                      { $1 }
| LEFTPAR lident_op RIGHTPAR  { mk_id $2 (floc ()) }
| LEFTPAR_STAR_RIGHTPAR       { mk_id (infix "*") (floc ()) }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
846 847
;

848
lident_op:
Andrei Paskevich's avatar
Andrei Paskevich committed
849 850 851 852 853 854
| prefix_op             { infix $1 }
| prefix_op UNDERSCORE  { prefix $1 }
| EQUAL                 { infix "=" }
| OPPREF                { prefix $1 }
| LEFTSQ RIGHTSQ        { mixfix "[]" }
| LEFTSQ LARROW RIGHTSQ { mixfix "[<-]" }
855 856
;

857
prefix_op:
858 859 860 861 862 863
| OP1   { $1 }
| OP2   { $1 }
| OP3   { $1 }
| OP4   { $1 }
;

Andrei Paskevich's avatar
Andrei Paskevich committed
864
/* Qualified idents */
865

Andrei Paskevich's avatar
Andrei Paskevich committed
866 867 868
qualid:
| ident_rich              { Qident $1 }
| uqualid DOT ident_rich  { Qdot ($1, $3) }
869 870
;

Andrei Paskevich's avatar
Andrei Paskevich committed
871 872 873
lqualid_rich:
| lident_rich             { Qident $1 }
| uqualid DOT lident_rich { Qdot ($1, $3) }
874 875 876
;

lqualid:
Andrei Paskevich's avatar
Andrei Paskevich committed
877 878
| lident              { Qident $1 }
| uqualid DOT lident  { Qdot ($1, $3) }
879 880
;

Andrei Paskevich's avatar
Andrei Paskevich committed
881 882 883 884
/* copy of lqualid to avoid yacc conflicts */
lqualid_copy:
| lident              { Qident $1 }
| uqualid DOT lident  { Qdot ($1, $3) }
885 886
;

Andrei Paskevich's avatar
Andrei Paskevich committed
887 888 889
uqualid:
| uident              { Qident $1 }
| uqualid DOT uident  { Qdot ($1, $3) }
890 891
;

Andrei Paskevich's avatar
Andrei Paskevich committed
892 893
/* Theory/Module names */

894 895 896
tqualid:
| uident                { Qident $1 }
| any_qualid DOT uident { Qdot ($1, $3) }
Andrei Paskevich's avatar
Andrei Paskevich committed
897
;
898

Andrei Paskevich's avatar
Andrei Paskevich committed
899 900 901
any_qualid:
| ident                 { Qident $1 }
| any_qualid DOT ident  { Qdot ($1, $3) }
Andrei Paskevich's avatar
Andrei Paskevich committed
902
;
903 904 905

/* Misc */

906
label:
Andrei Paskevich's avatar
Andrei Paskevich committed
907 908
| STRING    { Lstr $1 }
| POSITION  { Lpos $1 }
909 910
;

911 912 913 914 915
labels:
| /* epsilon */ { [] }
| label labels  { $1 :: $2 }
;

916 917 918
bar_:
| /* epsilon */ { () }
| BAR           { () }
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
919 920
;

921 922 923
/****************************************************************************/

program_file:
924
| list0_theory_or_module_ EOF { $1 }
925 926
;

927
list0_theory_or_module_:
928 929
| /* epsilon */
   { [] }
930
| list1_theory_or_module_
931 932 933
   { $1 }
;

934 935
list1_theory_or_module_:
| theory_or_module_
936
   { [$1] }
937
| theory_or_module_ list1_theory_or_module_
938 939 940
   { $1 :: $2 }
;

941 942
theory_or_module_:
| THEORY uident labels list0_full_decl END
943
   { Ptheory { pth_name = add_lab $2 $3; pth_decl = $4; } }
944
| MODULE uident labels list0_program_decl END
945
   { Pmodule { mod_name = add_lab $2 $3; mod_decl = $4; } }
946 947
;

948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
list0_full_decl:
| /* epsilon */
   { [] }
| list1_full_decl
   { $1 }
;

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

full_decl:
| NAMESPACE namespace_import namespace_name list0_full_decl END
964
   { Dnamespace (floc_i 3, $3, $2, $4) }
965 966 967 968
| decl
   { Dlogic $1 }
;

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
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 }