jocaml.mly 47.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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 1145 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 1193 1194 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 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
/***********************************************************************/
/*                                                                     */
/*                           Objective Caml                            */
/*                                                                     */
/*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
/*                                                                     */
/*  Copyright 1996 Institut National de Recherche en Informatique et   */
/*  Automatique.  Distributed only by permission.                      */
/*                                                                     */
/***********************************************************************/

/* $Id: jocaml.mly,v 1.1 2005/08/23 11:15:13 fpottier Exp $ */

/* The parser definition */

%{
open Location
open Asttypes
open Longident
open Parsetree

let mktyp d =
  { ptyp_desc = d; ptyp_loc = symbol_loc() }
let mkpat d =
  { ppat_desc = d; ppat_loc = symbol_loc() }
let mkexp d =
  { pexp_desc = d; pexp_loc = symbol_loc() }
let mkmty d =
  { pmty_desc = d; pmty_loc = symbol_loc() }
let mksig d =
  { psig_desc = d; psig_loc = symbol_loc() }
let mkmod d =
  { pmod_desc = d; pmod_loc = symbol_loc() }
let mkstr d =
  { pstr_desc = d; pstr_loc = symbol_loc() }
let mkfield d =
  { pfield_desc = d; pfield_loc = symbol_loc() }

let mkoperator name pos =
  { pexp_desc = Pexp_ident(Lident name); pexp_loc = rhs_loc pos }

let void () = mkexp (Pexp_construct (Lident "()", None, false))
let void_pat () = mkpat (Ppat_construct (Lident "()", None, false))

let mkassert e =
  let {loc_start = st; loc_end = en} = symbol_loc () in
  let triple = mkexp (Pexp_tuple
                       [mkexp (Pexp_constant (Const_string !input_name));
                        mkexp (Pexp_constant (Const_int st));
                        mkexp (Pexp_constant (Const_int en))]) in
  let ex = Ldot (Lident "Pervasives", "Assert_failure") in
  let bucket = mkexp (Pexp_construct (ex, Some triple, false)) in
  let ra = Ldot (Lident "Pervasives", "raise") in
  let raiser = mkexp (Pexp_apply (mkexp (Pexp_ident ra), [bucket])) in
  let un = mkexp (Pexp_construct (Lident "()", None, false)) in
  match e with
  | {pexp_desc = Pexp_construct (Lident "false", None, false) } -> raiser
  | _ -> if !Clflags.noassert
         then un
         else mkexp (Pexp_ifthenelse (e, un, Some raiser))
;;
let mklazy e =
  let void_pat = mkpat (Ppat_construct (Lident "()", None, false)) in
  let f = mkexp (Pexp_function ([void_pat, e])) in
  let delayed = Ldot (Lident "Lazy", "Delayed") in
  let df = mkexp (Pexp_construct (delayed, Some f, false)) in
  let r = mkexp (Pexp_ident (Ldot (Lident "Pervasives", "ref"))) in
  mkexp (Pexp_apply (r, [df]))
;;

let mkinfix arg1 name arg2 =
  mkexp(Pexp_apply(mkoperator name 2, [arg1; arg2]))

let mkuminus name arg =
  match arg.pexp_desc with
    Pexp_constant(Const_int n) ->
      mkexp(Pexp_constant(Const_int(-n)))
  | Pexp_constant(Const_float f) ->
      mkexp(Pexp_constant(Const_float("-" ^ f)))
  | _ ->
      mkexp(Pexp_apply(mkoperator ("~" ^ name) 1, [arg]))

let rec mklistexp = function
    [] ->
      mkexp(Pexp_construct(Lident "[]", None, false))
  | e1 :: el ->
      mkexp(Pexp_construct(Lident "::",
                           Some(mkexp(Pexp_tuple[e1; mklistexp el])),
                           false))
let rec mklistpat = function
    [] ->
      mkpat(Ppat_construct(Lident "[]", None, false))
  | p1 :: pl ->
      mkpat(Ppat_construct(Lident "::",
                           Some(mkpat(Ppat_tuple[p1; mklistpat pl])),
                           false))

let mkstrexp e =
  { pstr_desc = Pstr_eval e; pstr_loc = e.pexp_loc }

let array_function str name =
  Ldot(Lident str, (if !Clflags.fast then "unsafe_" ^ name else name))

let rec mkrangepat c1 c2 =
  if c1 > c2 then mkrangepat c2 c1 else
  if c1 = c2 then mkpat(Ppat_constant(Const_char c1)) else
  mkpat(Ppat_or(mkpat(Ppat_constant(Const_char c1)),
                mkrangepat (Char.chr(Char.code c1 + 1)) c2))

let syntax_error () =
  raise Syntaxerr.Escape_error

let unclosed opening_name opening_num closing_name closing_num =
  raise(Syntaxerr.Error(Syntaxerr.Unclosed(rhs_loc opening_num, opening_name,
                                           rhs_loc closing_num, closing_name)))

let join_def ()=
  mkexp (Pexp_apply 
           (mkexp (Pexp_ident (Ldot (Lident "Join", "definition"))),
            [void ()]))
    
let mkloc l = 
  let r = mkexp (Pexp_ident (Ldot (Lident "Join", "create_location"))) in 
  mkexp (Pexp_apply (r, [mkexp Pexp_thismodule;  
                                mkexp (Pexp_function [void_pat (),l])]))

let pexp_add list = function
    { pexp_desc= Pexp_par l } -> l@list
  | { pexp_desc= Pexp_null } -> list
  | p -> p::list


let deconstr_record exp f =
 match exp.pexp_desc with
   Pexp_construct(lid,Some arg,_) ->
              (* un deconstructeur *)
     mkexp (Pexp_let
              (Nonrecursive,
               [mkpat(Ppat_construct
                        (lid,
                         Some (mkpat(Ppat_var "*match*")),
                         false)),
                 arg],
               f (mkexp(Pexp_ident(Lident "*match*")))))
          | _ -> f exp
          

%}

/* Tokens */

%token AMPERAMPER
%token AMPERSAND
%token AND
%token OR
%token AS
%token ASSERT
%token BAR
%token BARBAR
%token BARRBRACKET
%token BEGIN
%token <char> CHAR
%token CLASS
%token CLOSED
%token COLON
%token COLONCOLON
%token COLONEQUAL
%token COLONGREATER
%token COMMA
%token CONSTRAINT
%token DO
%token DONE
%token DOT
%token DOTDOT
%token DOWNTO
%token ELSE
%token END
%token EOF
%token EQUAL
%token EXCEPTION
%token EXTERNAL
%token FALSE
%token <string> FLOAT
%token FOR
%token FUN
%token FUNCTION
%token FUNCTOR
%token GREATER
%token GREATERRBRACE
%token GREATERRBRACKET
%token IF
%token IN
%token INCLUDE
%token <string> INFIXOP0
%token <string> INFIXOP1
%token <string> INFIXOP2
%token <string> INFIXOP3
%token <string> INFIXOP4
%token INHERIT
%token <int> INT
%token LAZY
%token LBRACE
%token LBRACELESS
%token LBRACKET
%token LBRACKETBAR
%token LBRACKETLESS
%token LESS
%token LESSMINUS
%token LET
%token <string> LIDENT
%token LPAREN
%token MATCH
%token METHOD
%token MINUSGREATER
%token MODULE
%token MUTABLE
%token NEW
%token OF
%token OPEN
%token PARSER
%token <string> PREFIXOP
%token PRIVATE
%token PROTECTED
%token QUESTION
%token QUOTE
%token RBRACE
%token RBRACKET
%token REC
%token RPAREN
%token SEMI
%token SEMISEMI
%token SHARP
%token SIG
%token STAR
%token <string> STRING
%token STRUCT
%token <string> SUBTRACTIVE
%token THEN
%token TO
%token TRUE
%token TRY
%token TYPE
%token <string> UIDENT
%token UNDERSCORE
%token VAL
%token VIRTUAL
%token WHEN
%token WHILE
%token RELOCINFO
%token WITH
%token VARTYPE
%token LOC
%token REPLY
%token LESSLESS
%token GREATERGREATER
%token LBRACEBAR
%token BARRBRACE
%token LESSLESSGREATERGREATER 
%token LESSGREATER
%token LBRACERBRACE
%token SPAWN
%token JOINDEF

/* Precedences and associativities. Lower precedences come first. */

%right prec_let                         /* let ... in ... */
%right prec_type_def                    /* = in type definitions */
%right SEMI                             /* e1; e2 (sequence) */
%right prec_fun prec_match prec_try     /* match ... with ... */
%right prec_list                        /* e1; e2 (list, array, record) */
%right prec_if                          /* if ... then ... else ... */
%right COLONEQUAL LESSMINUS             /* assignments */
%left  AS                               /* as in patterns */
%left  BAR                              /* | in patterns */
%left  COMMA                            /* , in expressions, patterns, types */
%right prec_type_arrow                  /* -> in type expressions */
%right BARBAR                        /* or */
%right AMPERSAND AMPERAMPER             /* & */
%left  INFIXOP0 EQUAL LESS GREATER   LESSGREATER   /* = < > etc */
%right INFIXOP1                         /* @ ^ etc */
%right COLONCOLON                       /* :: */
%left  INFIXOP2 SUBTRACTIVE             /* + - */
%left  INFIXOP3 STAR                    /* * / */
%right INFIXOP4                         /* ** */
%right prec_unary_minus                 /* - unary */
%left  prec_appl                        /* function application */
%right prec_constr_appl                 /* constructor application */
%left  SHARP                            /* method call */
%left  DOT                              /* record access, array access */
%right PREFIXOP                         /* ! */
%left prec_expr
%left prec_proc                        /* { process */

/* Entry points */

%start implementation                   /* for implementation files */
%type <Parsetree.structure> implementation
%start interface                        /* for interface files */
%type <Parsetree.signature> interface
%start toplevel_phrase                  /* for interactive use */
%type <Parsetree.toplevel_phrase> toplevel_phrase
%start use_file                         /* for the #use directive */
%type <Parsetree.toplevel_phrase list> use_file

%%

/* Entry points */

implementation:
    structure EOF                        { $1 }
;
interface:
    signature EOF                        { List.rev $1 }
;
toplevel_phrase:
    top_structure SEMISEMI               { Ptop_def $1 }
  | seq_expr SEMISEMI                    { Ptop_def[mkstrexp $1] }
  | toplevel_directive SEMISEMI          { $1 }
  | EOF                                  { raise End_of_file }
;
top_structure:
    structure_item                       { [$1] }
  | structure_item top_structure         { $1 :: $2 }
  | LET LOC loc_bindings                 { [mkstr (Pstr_location $3)] }
;
use_file:
    use_file_tail                        { $1 }
  | seq_expr use_file_tail               { Ptop_def[mkstrexp $1] :: $2 }
;
use_file_tail:
    EOF                                         { [] }
  | SEMISEMI EOF                                { [] }
  | SEMISEMI seq_expr use_file_tail             { Ptop_def[mkstrexp $2] :: $3 }
  | SEMISEMI structure_item use_file_tail       { Ptop_def[$2] :: $3 }
  | SEMISEMI toplevel_directive use_file_tail   { $2 :: $3 }
  | structure_item use_file_tail                { Ptop_def[$1] :: $2 }
  | toplevel_directive use_file_tail            { $1 :: $2 }
;

/* Module expressions */

module_expr:
    mod_longident
      { mkmod(Pmod_ident $1) }
  | STRUCT structure END
      { mkmod(Pmod_structure($2)) }
  | STRUCT structure error
      { unclosed "struct" 1 "end" 3 }
  | FUNCTOR LPAREN UIDENT COLON module_type RPAREN MINUSGREATER module_expr
    %prec prec_fun
      { mkmod(Pmod_functor($3, $5, $8)) }
  | module_expr LPAREN module_expr RPAREN
      { mkmod(Pmod_apply($1, $3)) }
  | module_expr LPAREN module_expr error
      { unclosed "(" 2 ")" 4 }
  | LPAREN module_expr COLON module_type RPAREN
      { mkmod(Pmod_constraint($2, $4)) }
  | LPAREN module_expr COLON module_type error
      { unclosed "(" 1 ")" 5 }
  | LPAREN module_expr RPAREN
      { $2 }
  | LPAREN module_expr error
      { unclosed "(" 1 ")" 3 }
;

lident_comma_list:
    LIDENT {[$1]}
  | LIDENT COMMA lident_comma_list { $1::$3 }
;

structure:
    structure_tail                              { $1 }
  | seq_expr structure_tail                     { mkstrexp $1 :: $2 }
;
structure_tail:
    /* empty */                                 { [] }
  | SEMISEMI                                    { [] }
  | SEMISEMI seq_expr structure_tail            { mkstrexp $2 :: $3 }
  | SEMISEMI structure_item structure_tail      { $2 :: $3 }
  | structure_item structure_tail               { $1 :: $2 }
  | LET LOC loc_bindings structure_tail 
      { (mkstr (Pstr_location $3)):: $4 }
  | SEMISEMI LET LOC loc_bindings structure_tail 
      { (mkstr (Pstr_location $4)):: $5 }
;

structure_item:
    LET JOINDEF def_bindings
      {  mkstr(Pstr_def(List.rev $3)) }
  | LET rec_flag let_bindings
      { match $3 with
          [{ppat_desc = Ppat_any}, exp] -> mkstr(Pstr_eval exp)
        | _ -> mkstr(Pstr_value($2, List.rev $3)) }
  | EXTERNAL val_ident COLON core_type EQUAL primitive_declaration
      { mkstr(Pstr_primitive($2, {pval_type = $4; pval_prim = $6})) }
  | TYPE type_declarations
      { mkstr(Pstr_type(List.rev $2)) }
  | EXCEPTION UIDENT constructor_arguments
      { mkstr(Pstr_exception($2, $3)) }
  | MODULE UIDENT module_binding
      { mkstr(Pstr_module($2, $3)) }
  | MODULE TYPE ident EQUAL module_type
      { mkstr(Pstr_modtype($3, $5)) }
  | OPEN mod_longident
      { mkstr(Pstr_open $2) }
  | CLASS class_list END
      { mkstr(Pstr_class (List.rev $2)) }
  | CLASS class_list error
      { unclosed "class" 1 "end" 3 }
;
module_binding:
    EQUAL module_expr
      { $2 }
  | COLON module_type EQUAL module_expr
      { mkmod(Pmod_constraint($4, $2)) }
  | LPAREN UIDENT COLON module_type RPAREN module_binding
      { mkmod(Pmod_functor($2, $4, $6)) }
;

/* Module types */

module_type:
    mty_longident
      { mkmty(Pmty_ident $1) }
  | SIG signature END
      { mkmty(Pmty_signature(List.rev $2)) }
  | SIG signature error
      { unclosed "sig" 1 "end" 3 }
  | FUNCTOR LPAREN UIDENT COLON module_type RPAREN MINUSGREATER module_type
    %prec prec_fun
      { mkmty(Pmty_functor($3, $5, $8)) }
  | module_type WITH with_constraints
      { mkmty(Pmty_with($1, List.rev $3)) }
  | LPAREN module_type RPAREN
      { $2 }
  | LPAREN module_type error
      { unclosed "(" 1 ")" 3 }
;
signature:
    /* empty */                                 { [] }
  | signature signature_item                    { $2 :: $1 }
  | signature signature_item SEMISEMI           { $2 :: $1 }
;
signature_item:
    VAL val_ident COLON core_type
      { mksig(Psig_value($2, {pval_type = $4; pval_prim = []})) }
  | EXTERNAL val_ident COLON core_type EQUAL primitive_declaration
      { mksig(Psig_value($2, {pval_type = $4; pval_prim = $6})) }
  | TYPE type_declarations
      { mksig(Psig_type(List.rev $2)) }
  | EXCEPTION UIDENT constructor_arguments
      { mksig(Psig_exception($2, $3)) }
  | MODULE UIDENT module_declaration
      { mksig(Psig_module($2, $3)) }
  | MODULE TYPE ident
      { mksig(Psig_modtype($3, Pmodtype_abstract)) }
  | MODULE TYPE ident EQUAL module_type
      { mksig(Psig_modtype($3, Pmodtype_manifest $5)) }
  | OPEN mod_longident
      { mksig(Psig_open $2) }
  | INCLUDE module_type
      { mksig(Psig_include $2) }
  | CLASS class_type_list END
      { mksig(Psig_class (List.rev $2)) }
;

module_declaration:
    COLON module_type
      { $2 }
  | LPAREN UIDENT COLON module_type RPAREN module_declaration
      { mkmty(Pmty_functor($2, $4, $6)) }
;

/* Core expressions */

seq_expr:
  | expr                          { $1 }
  | expr SEMI                     { $1 }
  | expr SEMI seq_expr            { mkexp(Pexp_sequence($1, $3)) }
;

expr:
    expr0  { $1 }
  | expr_comma_list
      { mkexp(Pexp_tuple(List.rev $1)) }
;

expr0:
    simple_expr
      { $1 }
  | simple_expr simple_expr_list %prec prec_appl
      { mkexp(Pexp_apply($1, List.rev $2)) }
  | LET JOINDEF def_bindings IN error %prec prec_let
      { syntax_error() }
  | LET JOINDEF def_bindings error %prec prec_let
      { unclosed "def" 1 "in" 4  }
  | LET JOINDEF def_bindings IN seq_expr %prec prec_let
      { mkexp(Pexp_def(List.rev $3, $5)) }
  | LET LOC loc_bindings  IN error %prec prec_let
      { syntax_error() }
  | LET LOC loc_bindings  error %prec prec_let
      { unclosed "let" 1 "in" 4  }
  | LET LOC loc_bindings  IN seq_expr %prec prec_let
      { mkexp (Pexp_location ($3,$5)) }
  | LET rec_flag let_bindings IN seq_expr %prec prec_let
      { mkexp(Pexp_let($2, List.rev $3, $5)) }
  | LET rec_flag let_bindings IN error %prec prec_let
      { syntax_error() }
  | LET rec_flag let_bindings error %prec prec_let
      { unclosed "let" 1 "in" 4  }
  | SPAWN LBRACE error
      { syntax_error () }
  | SPAWN LBRACE process RBRACE
      { mkexp(Pexp_process $3) }
  | LBRACE BAR process RBRACE
      { mkexp(Pexp_process $3) }
  | SPAWN LBRACE process error
      { unclosed "{" 2 "}" 4 }
  | PARSER opt_pat opt_bar parser_cases %prec prec_fun
      { Pstream.cparser ($2, List.rev $4) }
  | FUNCTION opt_bar match_cases %prec prec_fun
      { mkexp(Pexp_function(List.rev $3)) }
  | FUN simple_pattern fun_def %prec prec_fun
      { mkexp(Pexp_function([$2, $3])) }
  | MATCH seq_expr WITH opt_bar match_cases %prec prec_match
      { mkexp(Pexp_match($2, List.rev $5)) }
  | MATCH seq_expr WITH PARSER opt_pat opt_bar parser_cases %prec prec_match
      { mkexp(Pexp_apply(Pstream.cparser ($5, List.rev $7), [$2])) }
  | TRY seq_expr WITH opt_bar match_cases %prec prec_try
      { mkexp(Pexp_try($2, List.rev $5)) }
  | TRY seq_expr WITH error %prec prec_try
      { syntax_error() }
  | TRY seq_expr error %prec prec_try
      { unclosed "try" 1 "with" 3 }
  | constr_longident simple_expr %prec prec_constr_appl
      { mkexp(Pexp_construct($1, Some $2, false)) }
  | IF seq_expr THEN expr ELSE expr %prec prec_if
      { mkexp(Pexp_ifthenelse($2, $4, Some $6)) }
  | IF seq_expr THEN expr %prec prec_if
      { mkexp(Pexp_ifthenelse($2, $4, None)) }
  | WHILE seq_expr DO seq_expr DONE
      { mkexp(Pexp_while($2, $4)) }
  | WHILE seq_expr DO seq_expr error
      { unclosed "while" 1 "done" 5 }
  | FOR val_ident EQUAL seq_expr direction_flag seq_expr DO seq_expr DONE
      { mkexp(Pexp_for($2, $4, $6, $5, $8)) }
  | FOR val_ident EQUAL seq_expr direction_flag seq_expr DO seq_expr error
      { unclosed "for" 1 "done" 9 }
  | expr0 COLONCOLON expr0
      { mkexp(Pexp_construct(Lident "::", Some(mkexp(Pexp_tuple[$1;$3])), false)) }
  | expr0 INFIXOP0 expr0
      { mkinfix $1 $2 $3 }
  | expr0 LESSGREATER expr0
      { mkinfix $1 "<>" $3 }
  | expr0 INFIXOP1 expr0
      { mkinfix $1 $2 $3 }
  | expr0 INFIXOP2 expr0
      { mkinfix $1 $2 $3 }
  | expr0 INFIXOP3 expr0
      { mkinfix $1 $2 $3 }
  | expr0 INFIXOP4 expr0
      { mkinfix $1 $2 $3 }
  | expr0 SUBTRACTIVE expr0
      { mkinfix $1 $2 $3 } 
  | expr0 STAR expr0
      { mkinfix $1 "*" $3 } 
  | expr0 EQUAL expr0
      { mkinfix $1 "=" $3 } 
  | expr0 LESS expr0
      { mkinfix $1 "<" $3 } 
  | expr0 GREATER expr0
      { mkinfix $1 ">" $3 } 
  | expr0 BARBAR expr0
      { mkinfix $1 "||" $3 }
  | expr0 AMPERSAND expr0
      { mkinfix $1 "&" $3 }
  | expr0 AMPERAMPER expr0
      { mkinfix $1 "&&" $3 }
  | expr0 COLONEQUAL expr0
      { mkinfix $1 ":=" $3 }
  | SUBTRACTIVE expr0 %prec prec_unary_minus
      { mkuminus $1 $2 }
  | simple_expr DOT label_longident LESSMINUS expr
      { deconstr_record $1 (fun exp -> mkexp(Pexp_setfield(exp, $3, $5))) }
  | simple_expr DOT LPAREN seq_expr RPAREN LESSMINUS expr
      { mkexp(Pexp_apply(mkexp(Pexp_ident(array_function "Array" "set")),
                         [$1; $4; $7])) }
  | simple_expr DOT LBRACKET seq_expr RBRACKET LESSMINUS expr
      { mkexp(Pexp_apply(mkexp(Pexp_ident(array_function "String" "set")),
                         [$1; $4; $7])) }
  | label LESSMINUS expr
      { mkexp(Pexp_setinstvar($1, $3)) }
  | ASSERT simple_expr %prec prec_appl
      { mkassert $2 }
  | LAZY simple_expr %prec prec_appl
      { mklazy $2 }
;

simple_expr:
    val_longident
      { mkexp(Pexp_ident $1) }
  | constant
      { mkexp(Pexp_constant $1) }
  | constr_longident
      { mkexp(Pexp_construct($1, None, false)) }
  | VARTYPE 
      { mkexp Pexp_vartype }
  | RELOCINFO
      { mkexp(Pexp_thismodule) }
  | LPAREN seq_expr RPAREN
      { $2 }
  | LPAREN seq_expr error
      { unclosed "(" 1 ")" 3 }
  | BEGIN seq_expr END
      { $2 }
  | BEGIN seq_expr error
      { unclosed "begin" 1 "end" 3 }
  | LPAREN seq_expr type_constraint RPAREN
      { let (t, t') = $3 in mkexp(Pexp_constraint($2, t, t')) }
  | simple_expr DOT label_longident
      { deconstr_record $1 (fun exp -> mkexp(Pexp_field(exp, $3))) }
  | simple_expr DOT LPAREN seq_expr RPAREN
      { mkexp(Pexp_apply(mkexp(Pexp_ident(array_function "Array" "get")),
                         [$1; $4])) }
  | simple_expr DOT LPAREN seq_expr error
      { unclosed "(" 3 ")" 5 }
  | simple_expr DOT LBRACKET seq_expr RBRACKET
      { mkexp(Pexp_apply(mkexp(Pexp_ident(array_function "String" "get")),
                         [$1; $4])) }
  | simple_expr DOT LBRACKET seq_expr error
      { unclosed "[" 3 "]" 5 }
  | LBRACE lbl_expr_list opt_semi RBRACE
      { mkexp(Pexp_record(0,List.rev $2)) }
  | LBRACE LBRACKET LIDENT RBRACKET lbl_expr_list opt_semi RBRACE
      { 
       let tag = 
         try
           List.assoc $3 Tag.list
         with
           Not_found -> syntax_error()
       in
       mkexp(Pexp_record(tag,List.rev $5)) }
  | LBRACE lbl_expr_list opt_semi error
      { unclosed "{" 1 "}" 4 }
  | LBRACKETLESS stream_expr opt_semi GREATERRBRACKET
      { Pstream.cstream (List.rev $2) }
  | LBRACKETLESS stream_expr opt_semi error
      { unclosed "[<" 1 ">]" 4 }
  | LBRACKETLESS GREATERRBRACKET
      { Pstream.cstream [] }
  | LBRACKETBAR expr_semi_list opt_semi BARRBRACKET
      { mkexp(Pexp_array(List.rev $2)) }
  | LBRACKETBAR expr_semi_list opt_semi error
       { unclosed "[|" 1 "|]" 4 }
  | LBRACKETBAR BARRBRACKET
      { mkexp(Pexp_array []) }
  | LBRACKET expr_semi_list opt_semi RBRACKET
      { mklistexp(List.rev $2) }
  | LBRACKET expr_semi_list opt_semi error
      { unclosed "[" 1 "]" 4 }
  | PREFIXOP simple_expr
      { mkexp(Pexp_apply(mkoperator $1 1, [$2])) }
  | simple_expr SHARP label
      { mkexp(Pexp_send($1, $3)) }
  | NEW class_longident
      { mkexp(Pexp_new($2)) }
  | LBRACELESS label_expr_list opt_semi GREATERRBRACE
      { mkexp(Pexp_override(List.rev $2)) }
  | LBRACELESS label_expr_list opt_semi error
      { unclosed "{<" 1 ">}" 4 }
  | LBRACELESS GREATERRBRACE
      { mkexp(Pexp_override []) }
  | LPAREN SHARP label RPAREN
      { mkexp(Pexp_function [mkpat(Ppat_var "x"),
                mkexp(Pexp_send(mkexp(Pexp_ident (Lident"x")), $3))]) }
     
;

simple_expr_list:
    simple_expr
      { [$1] }
  | simple_expr_list simple_expr
      { $2 :: $1 }
;

def_bindings:
    def_binding                                    { [$1] }
  | def_bindings AND def_binding                { $3 :: $1 }
;

def_binding:
  LIDENT LBRACKET lident_comma_list
  RBRACKET LBRACE simple_def_binding loc_init RBRACE  
    { (Some ($1, $7, Some $3)), List.rev $6 }
|  LIDENT LBRACE simple_def_binding loc_init RBRACE  
    { (Some ($1, $4, None)), List.rev $3 }
| simple_def_binding                        { None, List.rev $1 }
  ;
  
simple_def_binding:
    def_pattern            { [$1] }
  | simple_def_binding OR def_pattern            { $3 :: $1 }
  ;
  
def_pattern:
    join_pattern EQUAL process      { ($1,$3) }
;
join_pattern:
    LIDENT simple_pattern      { mkpat(Ppat_channel ($1,$2,Sync)) }
  | LIDENT PREFIXOP simple_pattern 
           { match $2 with
             "!" -> mkpat(Ppat_channel ($1,$3,Async))
           | _ -> syntax_error () }
  | UIDENT simple_pattern   { mkpat(Ppat_construct(Lident $1,Some $2,false))}
  | join_pattern BAR join_pattern      { mkpat(Ppat_or($1,$3)) }
;

def_parameters:
        /* nothing */       {[]}
    | LPAREN lident_comma_list RPAREN { $2 }
;

loc_bindings:
      loc_binding                   { [$1] }
    | loc_binding AND loc_bindings { $1::$3 }
;

loc_init:
       DO process0 { $2 }
    | /* nothing */ { mkexp(Pexp_null) }

loc_binding:
    LIDENT loc_modules loc_defs loc_init { $1, mklistexp $2, $3, $4 }
;

list_of_modules:
       UIDENT   { [ mkexp(Pexp_module $1) ] }
    |  UIDENT SEMI list_of_modules { (mkexp(Pexp_module $1))::$3 }
;

loc_modules:
    /* nothing */ { [] }
  | LBRACKET list_of_modules RBRACKET { $2 }
  | LBRACKET RBRACKET { [] }
;

loc_defs:
    JOINDEF def_bindings { $2 }        
  | /* nothing */        { [] }
;

to_ident:
   TO LIDENT    { Some $2 }
  | /* nothing */ { None }
;

process:
                                             { mkexp(Pexp_null)}
  | process0                                 { $1 }
  | process0 BAR process                      
     { mkexp(Pexp_par(pexp_add (pexp_add [] $1) $3)) }
  | LET JOINDEF def_bindings IN process %prec prec_let
      { mkexp(Pexp_def(List.rev $3, $5)) }
  | LET LOC loc_bindings  IN process %prec prec_let
      { mkexp (Pexp_location ($3,$5)) }
  | LET rec_flag let_bindings IN process %prec prec_let
      { mkexp(Pexp_let($2, List.rev $3, $5)) }
  | IF expr THEN  process0  %prec prec_if
      { mkexp(Pexp_ifthenelse($2,$4,None)) }
  | SPAWN LBRACE process RBRACE SEMI process
      { mkexp(Pexp_par(pexp_add (pexp_add [] $3) $6)) }
  | LBRACE BAR process RBRACE SEMI process
      { mkexp(Pexp_par(pexp_add (pexp_add [] $3) $6)) }
  | proc_expr SEMI process     
      { 
    match ($3).pexp_desc with
      Pexp_reply( { pexp_desc = Pexp_construct(Lident "()",_,_) },channel) ->
        mkexp( Pexp_reply($1,channel) )
    | _ ->
        mkexp( Pexp_sequence($1,$3) ) 
  } 
 /* mkexp(Pexp_let(Nonrecursive,[mkpat(Ppat_any),$1],$3))  */
;

join_match_cases:
    pattern join_match_action                        { [$1, $2] }
  | join_match_cases BAR pattern join_match_action        { ($3, $4) :: $1 }
;
join_match_action:
    MINUSGREATER process0                       { $2 }
  | WHEN seq_expr MINUSGREATER process0         { mkexp(Pexp_when($2, $4)) }
;

proc_expr:
    simple_chan_expr
      { $1 }
  | simple_chan_expr simple_expr_list %prec prec_appl
      { mkexp(Pexp_apply($1, List.rev $2)) }
  | WHILE seq_expr DO seq_expr DONE
      { mkexp(Pexp_while($2, $4)) }
  | WHILE seq_expr DO seq_expr error
      { unclosed "while" 1 "done" 5 }
  | IF seq_expr THEN proc_expr ELSE proc_expr %prec prec_if
      { mkexp(Pexp_ifthenelse($2, $4, Some $6)) }
  | IF seq_expr THEN proc_expr %prec prec_if
      { mkexp(Pexp_ifthenelse($2, $4, None)) }
  | FOR val_ident EQUAL seq_expr direction_flag seq_expr DO seq_expr DONE
      { mkexp(Pexp_for($2, $4, $6, $5, $8)) }
  | FOR val_ident EQUAL seq_expr direction_flag seq_expr DO seq_expr error
      { unclosed "for" 1 "done" 9 }
  | simple_chan_expr DOT label_longident LESSMINUS expr
      { deconstr_record $1 (fun exp -> mkexp(Pexp_setfield(exp, $3, $5))) }
  | simple_chan_expr DOT LPAREN seq_expr RPAREN LESSMINUS expr
      { mkexp(Pexp_apply(mkexp(Pexp_ident(array_function "Array" "set")),
                         [$1; $4; $7])) }
  | simple_chan_expr DOT LBRACKET seq_expr RBRACKET LESSMINUS expr
      { mkexp(Pexp_apply(mkexp(Pexp_ident(array_function "String" "set")),
        [$1; $4; $7])) }
  | simple_chan_expr SHARP label simple_expr
      { mkexp(Pexp_apply(mkexp(Pexp_send($1, $3)), [$4])) }
  | proc_expr COLONEQUAL expr
      { mkinfix $1 ":=" $3 }
  | label LESSMINUS expr
      { mkexp(Pexp_setinstvar($1, $3)) }
  | ASSERT simple_expr %prec prec_appl
      { mkassert $2 }
  | LAZY simple_expr %prec prec_appl
      { mklazy $2 }
;

simple_chan_expr:
    val_longident
      { mkexp(Pexp_ident $1) }
  | constant
      { mkexp(Pexp_constant $1) }
  | LPAREN seq_expr RPAREN
      { $2 }
  | LPAREN seq_expr error
      { unclosed "(" 1 ")" 3 }
  | BEGIN seq_expr END
      { $2 }
  | BEGIN seq_expr error
      { unclosed "begin" 1 "end" 3 }
  | LPAREN seq_expr type_constraint RPAREN
      { let (t, t') = $3 in mkexp(Pexp_constraint($2, t, t')) }
  | simple_chan_expr DOT label_longident
      { deconstr_record $1 (fun exp -> mkexp(Pexp_field(exp, $3))) }
;

process0:
    REPLY expr to_ident
      { mkexp(Pexp_reply($2,$3))}
  | REPLY to_ident
      { mkexp(Pexp_reply(void (),$2))}
  | simple_chan_expr simple_expr
      { mkexp(Pexp_call($1,$2,false)) }
  | simple_chan_expr SHARP label simple_expr
    { mkexp(Pexp_call(mkexp(Pexp_send($1, $3)), $4, false)) }
  | IF expr THEN  process0 ELSE process0 %prec prec_if
      { mkexp(Pexp_ifthenelse($2,$4,Some $6)) }    
  | MATCH seq_expr WITH opt_bar join_match_cases %prec prec_match
      { mkexp(Pexp_match($2, List.rev $5)) }
  | LBRACERBRACE            { mkexp(Pexp_null) }
  | LBRACE process RBRACE { $2 }
  | LBRACE process error { unclosed "{" 1 "}" 3 }
  | LBRACE error { syntax_error () }
  | LOC loc_bindings { mkexp(Pexp_location($2, mkexp(Pexp_null))) }
;

let_bindings:
    let_binding                                 { [$1] }
  | let_bindings AND let_binding                { $3 :: $1 }
;

let_binding:
    val_ident fun_binding
      { ({ppat_desc = Ppat_var $1; ppat_loc = rhs_loc 1}, $2) }
  | pattern EQUAL seq_expr %prec prec_let
      { ($1,$3) }
  | pattern COLONEQUAL seq_expr %prec prec_let
      {  let r = mkexp (Pexp_ident (Ldot (Lident "Pervasives", "ref"))) in
	    ($1, mkexp (Pexp_apply (r, [$3]))) }
;

fun_binding:
    EQUAL seq_expr %prec prec_let
      { $2 }
  | type_constraint EQUAL seq_expr %prec prec_let
      { let (t, t') = $1 in mkexp(Pexp_constraint($3, t, t')) }
  | simple_pattern fun_binding
      { mkexp(Pexp_function[$1,$2]) }
;



parser_cases:
    parser_case                                 { [$1] }
  | parser_cases BAR parser_case                { $3 :: $1 }
;
parser_case:
    LBRACKETLESS stream_pattern opt_semi GREATERRBRACKET opt_pat
    MINUSGREATER seq_expr
      { (List.rev $2, $5, $7) }
  | LBRACKETLESS stream_pattern opt_semi error
      { unclosed "[<" 1 ">]" 4 }
  | LBRACKETLESS GREATERRBRACKET opt_pat MINUSGREATER seq_expr
      { ([], $3, $5) }
;
stream_pattern:
    stream_pattern_component opt_err                     { [($1, $2)] }
  | stream_pattern SEMI stream_pattern_component opt_err { ($3, $4) :: $1 }
;
stream_pattern_component:
    QUOTE pattern
      { Pstream.Spat_term ($2, None) }
  | QUOTE pattern WHEN expr %prec prec_list
      { Pstream.Spat_term ($2, Some $4) }
  | pattern EQUAL expr
      { Pstream.Spat_nterm ($1, $3) }
  | pattern
      { Pstream.Spat_sterm $1 }
;
opt_pat:
    /* empty */                                 { None }
  | simple_pattern                              { Some $1 }
;
opt_err:
    /* empty */                                 { None }
  | QUESTION expr %prec prec_list               { Some $2 }
;
stream_expr:
    stream_expr_component                       { [$1] }
  | stream_expr SEMI stream_expr_component      { $3 :: $1 }
;
stream_expr_component:
    QUOTE expr %prec prec_list                  { Pstream.Sexp_term $2 }
  | expr %prec prec_list                        { Pstream.Sexp_nterm $1 }
;
match_cases:
    pattern match_action                        { [$1, $2] }
  | match_cases BAR pattern match_action        { ($3, $4) :: $1 }
;
fun_def:
    match_action                                { $1 }
  | simple_pattern fun_def                      { mkexp(Pexp_function[$1,$2]) }
;
match_action:
    MINUSGREATER seq_expr                       { $2 }
  | WHEN seq_expr MINUSGREATER seq_expr         { mkexp(Pexp_when($2, $4)) }
;
expr_comma_list:
    expr_comma_list COMMA expr                  { $3 :: $1 }
  | expr COMMA expr                             { [$3; $1] }
;
lbl_expr_list:
    label_longident EQUAL expr %prec prec_list
      { [$1,$3] }
  | lbl_expr_list SEMI label_longident EQUAL expr %prec prec_list
      { ($3, $5) :: $1 }
;
label_expr_list:
    label EQUAL expr %prec prec_list
      { [$1,$3] }
  | label_expr_list SEMI label EQUAL expr %prec prec_list
      { ($3, $5) :: $1 }
;
expr_semi_list:
    expr %prec prec_list                        { [$1] }
  | expr_semi_list SEMI expr %prec prec_list    { $3 :: $1 }
;
type_constraint:
    COLON core_type                             { (Some $2, None) }
  | COLON core_type COLONGREATER core_type      { (Some $2, Some $4) }
  | COLONGREATER core_type                      { (None, Some $2) }
  | COLON error                                 { syntax_error() }
  | COLONGREATER error                          { syntax_error() }
;

/* Patterns */

pattern:
    simple_pattern
      { $1 }
  | pattern AS val_ident
      { mkpat(Ppat_alias($1, $3)) }
  | pattern_comma_list
      { mkpat(Ppat_tuple(List.rev $1)) }
  | constr_longident pattern %prec prec_constr_appl
      { mkpat(Ppat_construct($1, Some $2, false)) }
  | pattern COLONCOLON pattern
      { mkpat(Ppat_construct(Lident "::", Some(mkpat(Ppat_tuple[$1;$3])),
                             false)) }
  | pattern BAR pattern
      { mkpat(Ppat_or($1, $3)) }
;
simple_pattern:
    val_ident
      { mkpat(Ppat_var $1) }
  | UNDERSCORE
      { mkpat(Ppat_any) }
  | signed_constant
      { mkpat(Ppat_constant $1) }
  | CHAR DOTDOT CHAR
      { mkrangepat $1 $3 }
  | constr_longident
      { mkpat(Ppat_construct($1, None, false)) }
  | LBRACE lbl_pattern_list opt_semi RBRACE
      { mkpat(Ppat_record(List.rev $2)) }
  | LBRACE lbl_pattern_list opt_semi error
      { unclosed "{" 1 "}" 4 }
  | LBRACKET pattern_semi_list opt_semi RBRACKET
      { mklistpat(List.rev $2) }
  | LBRACKET pattern_semi_list opt_semi error
      { unclosed "{" 1 "}" 4 }
  | LPAREN pattern RPAREN
      { $2 }
  | LPAREN pattern error
      { unclosed "(" 1 ")" 3 }
  | LPAREN pattern COLON core_type RPAREN
      { mkpat(Ppat_constraint($2, $4)) }
  | LPAREN pattern COLON core_type error
      { unclosed "(" 1 ")" 5 }
;

pattern_comma_list:
    pattern_comma_list COMMA pattern            { $3 :: $1 }
  | pattern COMMA pattern                       { [$3; $1] }
;
pattern_semi_list:
    pattern                                     { [$1] }
  | pattern_semi_list SEMI pattern              { $3 :: $1 }
;
lbl_pattern_list:
    label_longident EQUAL pattern               { [($1, $3)] }
  | lbl_pattern_list SEMI label_longident EQUAL pattern { ($3, $5) :: $1 }
;

/* Primitive declarations */

primitive_declaration:
    STRING                                      { [$1] }
  | STRING primitive_declaration                { $1 :: $2 }
;

/* Class definitions */

class_list:
        class_list AND class_def                { $3 :: $1 }
      | class_def                               { [$1] }
;
class_def:
        virtual_flag closed_flag
        class_type_parameters LIDENT simple_pattern_list self self_type EQUAL
        constraints class_fields 
          { { pcl_name = $4; pcl_param = $3; pcl_args = List.rev $5;
              pcl_self = $6; pcl_self_ty = $7; pcl_cstr = List.rev $9;
              pcl_field = List.rev $10;
              pcl_kind = $1; pcl_closed = $2;
              pcl_loc = symbol_loc () } }
;
class_type_parameters:
        type_parameters                         { $1, symbol_loc () }
;
simple_pattern_list:
        simple_pattern                          { [$1] }
      | simple_pattern_list simple_pattern      { $2::$1 }
;
self:
        AS LIDENT                               { Some $2 }
      | /* empty */                             { None }
;
class_fields:
        /* empty */                             { [] }
      | class_fields INHERIT ancestor           { Pcf_inher $3 :: $1 }
      | class_fields VAL value                  { Pcf_val $3 :: $1 }
      | class_fields virtual_method             { Pcf_virt $2 :: $1 }
      | class_fields method_def                 { Pcf_meth $2 :: $1 }
;
ancestor:
        LPAREN core_type_comma_list RPAREN class_longident simple_expr_list
        self
          { $4, List.rev $2, List.rev $5, $6, symbol_loc () }
      | LPAREN core_type RPAREN class_longident simple_expr_list self
          { $4, [$2], List.rev $5, $6, symbol_loc () }
      | class_longident simple_expr_list self
          { $1, [], List.rev $2, $3, symbol_loc () }
;
value:
        private_flag mutable_flag label EQUAL seq_expr
          { $3, $1, $2, Some $5, symbol_loc () }
      | private_flag mutable_flag label
          { $3, $1, $2, None, symbol_loc () }
;
virtual_method:
        VIRTUAL protected_flag label COLON core_type
          { $3, $2, $5, symbol_loc () }

meth_binding:
     EQUAL seq_expr %prec prec_let
       { ($2,0) }
   | type_constraint EQUAL seq_expr %prec prec_let
       { let (t, t') = $1 in (mkexp(Pexp_constraint($3, t, t')),0) }
   | simple_pattern meth_binding
       { 
       let (s2,nparams) = $2
         in
         (mkexp(Pexp_function[$1,s2]),nparams+1) }
;

method_def :
        METHOD protected_flag label meth_binding
          { let (s4,np)= $4 in
	    $3, np , $2, s4, symbol_loc () }
;

/* Class type definitions */

class_type_list:
        class_type_list AND class_type          { $3 :: $1 }
      | class_type                              { [$1] }
;
class_type:
        virtual_flag closed_flag class_type_parameters LIDENT type_list
        self_type
        EQUAL
        constraints class_type_fields
          { { pcty_name = $4; pcty_param = $3; pcty_args = $5;
              pcty_self = $6; pcty_cstr = List.rev $8;
              pcty_field = List.rev $9;
              pcty_kind = $1; pcty_closed = $2;
              pcty_loc = symbol_loc () } }
;
type_list:
        LPAREN core_type RPAREN type_list       { $2 :: $4 }
      | LPAREN core_type RPAREN                 { [$2] }
;
self_type:
        COLON type_parameter                    { Some $2 }
      | /* empty */                             { None }
;
constraints:
        constraints CONSTRAINT constrain        { $3 :: $1 }
      | /* empty */                             { [] }
;
constrain:
        type_parameter EQUAL core_type          { $1, $3, symbol_loc () }
;
class_type_fields:
        /* empty */                             { [] }
      | class_type_fields INHERIT ancestor_type { Pctf_inher $3 :: $1 }
      | class_type_fields VAL value_type        { Pctf_val $3 :: $1 }
      | class_type_fields virtual_method        { Pctf_virt $2 :: $1 }
      | class_type_fields method_type           { Pctf_meth $2 :: $1 }
;
ancestor_type:
        LPAREN core_type_comma_list RPAREN class_longident
          { $4, List.rev $2, symbol_loc () }
      | LPAREN core_type RPAREN class_longident
          { $4, [$2], symbol_loc () }
      | class_longident
          { $1, [], symbol_loc () }
;
value_type :
        private_flag mutable_flag label COLON core_type
          { $3, $1, $2, Some $5, symbol_loc () }
      | private_flag mutable_flag label
          { $3, $1, $2, None, symbol_loc () }
;
method_type :
        METHOD protected_flag label COLON core_type
          { $3, $2, $5, symbol_loc () }
      | METHOD protected_flag label
          { $3, $2, mktyp(Ptyp_any), symbol_loc () }
;

/* Type declarations */

type_declarations:
    type_declaration                            { [$1] }
  | type_declarations AND type_declaration      { $3 :: $1 }
;
type_declaration:
    type_parameters LIDENT type_kind constraints
      { let (kind, manifest) = $3 in
        ($2, {ptype_params = $1;
              ptype_cstrs = List.rev $4;
              ptype_kind = kind;
              ptype_manifest = manifest;
              ptype_loc = symbol_loc()}) }
;
type_kind:
    /*empty*/
      { (Ptype_abstract, None) }
  | EQUAL core_type %prec prec_type_def
      { (Ptype_abstract, Some $2) }
  | EQUAL constructor_declarations_ext
      { (Ptype_variant(List.rev $2), None) }
  | EQUAL BAR constructor_declarations_ext
      { (Ptype_variant(List.rev $3), None) }
  | EQUAL LBRACE label_declarations opt_semi RBRACE
      { (Ptype_record(List.rev $3), None) }
  | EQUAL core_type EQUAL opt_bar constructor_declarations_ext %prec prec_type_def
      { (Ptype_variant(List.rev $5), Some $2) }
  | EQUAL core_type EQUAL LBRACE label_declarations opt_semi RBRACE
    %prec prec_type_def
      { (Ptype_record(List.rev $5), Some $2) }
;
type_parameters:
    /*empty*/                                   { [] }
  | type_parameter                              { [$1] }
  | LPAREN type_parameter_list RPAREN           { List.rev $2 }
;
type_parameter:
    QUOTE ident                                 { $2 }
;
type_parameter_list:
    type_parameter                              { [$1] }
  | type_parameter_list COMMA type_parameter    { $3 :: $1 }
;
constructor_declarations:
    constructor_declaration                     { [$1] }
  | constructor_declarations BAR constructor_declaration { $3 :: $1 }
;
constructor_declarations_ext:
     constructor_declarations                    { $1 }
   | tag_constructor_declarations                { $1 }
;
tag_constructor_declarations:
    tag_constructor_declaration                     { [$1] }
  | tag_constructor_declarations BAR tag_constructor_declaration { $3 :: $1 }
;

/* Ne pas permettre de creer des types concrets multiples si un tag
    est specifie.  */

tag_constructor_declaration:
  constr_ident LBRACKET INT RBRACKET ext_constructor_arguments
                                             { ($1, Some $3,$5) }
;
constructor_declaration:
     constr_ident ext_constructor_arguments   { ($1, None, $2) }
;
constructor_arguments:
    /*empty*/                                   { [] }
  | OF core_type_list                           { List.rev $2 }
;

ext_constructor_arguments:
    /*empty*/                                    { Pconstr_tuple[]}
  | OF core_type_list                            { Pconstr_tuple(List.rev $2)}
  | OF LBRACE label_declarations opt_semi RBRACE { Pconstr_record(List.rev $3)}
;


label_declarations:
    label_declaration                           { [$1] }
  | label_declarations SEMI label_declaration   { $3 :: $1 }
;
label_declaration:
    mutable_flag LIDENT COLON core_type         { ($2, $1, $4) }
;

/* "with" constraints (additional type equations over signature components) */

with_constraints:
    with_constraint                             { [$1] }
  | with_constraints AND with_constraint        { $3 :: $1 }
;
with_constraint:
    TYPE type_parameters label_longident EQUAL core_type constraints
      { ($3, Pwith_type {ptype_params = $2;
                         ptype_cstrs = List.rev $6;
                         ptype_kind = Ptype_abstract;
                         ptype_manifest = Some $5;
                         ptype_loc = symbol_loc()}) }
    /* used label_longident instead of type_longident to disallow
       functor applications in type path */
  | MODULE mod_longident EQUAL mod_ext_longident
      { ($2, Pwith_module $4) }
;

/* Core types */

core_type:
    simple_core_type
      { $1 }
  | core_type MINUSGREATER core_type %prec prec_type_arrow
      { mktyp(Ptyp_arrow($1, $3)) }
  | core_type_tuple
      { mktyp(Ptyp_tuple(List.rev $1)) }
  | core_type AS type_parameter
      { mktyp(Ptyp_alias($1, $3)) }
;

simple_core_type:
    QUOTE ident
      { mktyp(Ptyp_var $2) }
  | LESSLESS core_type GREATERGREATER
      { mktyp(Ptyp_channel($2,Pasync)) }
  | UNDERSCORE
      { mktyp(Ptyp_any) }
  | type_longident
      { mktyp(Ptyp_constr($1, [])) }
  | simple_core_type type_longident %prec prec_constr_appl
      { mktyp(Ptyp_constr($2, [$1])) }
  | LPAREN core_type_comma_list RPAREN type_longident
      %prec prec_constr_appl
      { mktyp(Ptyp_constr($4, List.rev $2)) }
  | LPAREN core_type RPAREN
      { $2 }
  | LESS meth_list GREATER
      { mktyp(Ptyp_object $2) }
  | LESS GREATER
      { mktyp(Ptyp_object []) }
  | SHARP class_longident
      { mktyp(Ptyp_class($2, [])) }
  | simple_core_type SHARP class_longident %prec prec_constr_appl
      { mktyp(Ptyp_class($3, [$1])) }
  | LPAREN core_type_comma_list RPAREN SHARP class_longident
      %prec prec_constr_appl
      { mktyp(Ptyp_class($5, List.rev $2)) }
;
core_type_tuple:
    simple_core_type STAR simple_core_type      { [$3; $1] }
  | core_type_tuple STAR simple_core_type       { $3 :: $1 }
;
core_type_comma_list:
    core_type COMMA core_type                   { [$3; $1] }
  | core_type_comma_list COMMA core_type        { $3 :: $1 }
;
core_type_list:
    simple_core_type                            { [$1] }
  | core_type_list STAR simple_core_type        { $3 :: $1 }
;
meth_list:
    field SEMI meth_list                        { $1 :: $3 }
  | field opt_semi                              { [$1] }
  | DOTDOT                                      { [mkfield Pfield_var] }
;
field:
    label COLON core_type                       { mkfield(Pfield($1, $3)) }
;
label:
    LIDENT                                      { $1 }
;

/* Constants */

constant:
    INT                                         { Const_int $1 }
  | CHAR                                        { Const_char $1 }
  | STRING                                      { Const_string $1 }
  | FLOAT                                       { Const_float $1 }
;
signed_constant:
    constant                                    { $1 }
  | SUBTRACTIVE INT                             { Const_int(- $2) }
  | SUBTRACTIVE FLOAT                           { Const_float("-" ^ $2) }
;
/* Identifiers and long identifiers */

ident:
    UIDENT                                      { $1 }
  | LIDENT                                      { $1 }
;
val_ident:
    LIDENT                                      { $1 }
  | LPAREN operator RPAREN                      { $2 }
;
operator:
    PREFIXOP                                    { $1 }
  | INFIXOP0                                    { $1 }
  | LESSGREATER                                 { "<>" }
  | INFIXOP1                                    { $1 }
  | INFIXOP2                                    { $1 }
  | INFIXOP3                                    { $1 }
  | INFIXOP4                                    { $1 }
  | SUBTRACTIVE                                 { $1 }
  | STAR                                        { "*" }
  | EQUAL                                       { "=" }
  | LESS                                        { "<" }
  | GREATER                                     { ">" }
/*
  | OR                                          { "or" }
*/
  | BARBAR                                      { "||" }
  | AMPERSAND                                   { "&" }
  | AMPERAMPER                                  { "&&" }
  | COLONEQUAL                                  { ":=" }
;
constr_ident:
    UIDENT                                      { $1 }
  | LBRACKET RBRACKET                           { "[]" }
  | LPAREN RPAREN                               { "()" }
  | COLONCOLON                                  { "::" }
  | FALSE                                       { "false" }
  | TRUE                                        { "true" }
;
    
val_longident:
    val_ident                                   { Lident $1 }
  | mod_longident DOT val_ident                 { Ldot($1, $3) }
;
constr_longident:
    mod_longident                               { $1 }
  | LBRACKET RBRACKET                           { Lident "[]" }
  | LPAREN RPAREN                               { Lident "()" }
  | FALSE                                       { Lident "false" }
  | TRUE                                        { Lident "true" }
;
label_longident:
    LIDENT                                      { Lident $1 }
  | mod_longident DOT LIDENT                    { Ldot($1, $3) }
;
type_longident:
    LIDENT                                      { Lident $1 }
  | mod_ext_longident DOT LIDENT                { Ldot($1, $3) }
;
mod_longident:
    UIDENT                                      { Lident $1 }
  | mod_longident DOT UIDENT                    { Ldot($1, $3) }
;
mod_ext_longident:
    UIDENT                                      { Lident $1 }
  | mod_ext_longident DOT UIDENT                { Ldot($1, $3) }
  | mod_ext_longident LPAREN mod_ext_longident RPAREN { Lapply($1, $3) }
;
mty_longident:
    ident                                       { Lident $1 }
  | mod_ext_longident DOT ident                 { Ldot($1, $3) }
;
class_longident:
    LIDENT                                      { Lident $1 }
  | mod_longident DOT LIDENT                    { Ldot($1, $3) }
;

/* Toplevel directives */

toplevel_directive:
    SHARP ident                 { Ptop_dir($2, Pdir_none) }
  | SHARP ident STRING          { Ptop_dir($2, Pdir_string $3) }
  | SHARP ident INT             { Ptop_dir($2, Pdir_int $3) }
  | SHARP ident val_longident   { Ptop_dir($2, Pdir_ident $3) }
;

/* Miscellaneous */

rec_flag:
    /* empty */                                 { Nonrecursive }
  | REC                                         { Recursive }
;

direction_flag:
    TO                                          { Upto }
  | DOWNTO                                      { Downto }
;
private_flag:
    /* empty */                                 { Public }
  | PRIVATE                                     { Private }
;
protected_flag:
    /* empty */                                 { Public }
  | PROTECTED                                   { Private }
;
mutable_flag:
    /* empty */                                 { Immutable }
  | MUTABLE                                     { Mutable }
;
virtual_flag:
    /* empty */                                 { Concrete }
  | VIRTUAL                                     { Virtual }
;
closed_flag:
    /* empty */                                 { Open }
  | CLOSED                                      { Closed }
;
opt_bar:
    /* empty */                                 { () }
  | BAR                                         { () }
;
opt_semi:
  | /* empty */                                 { () }
  | SEMI                                        { () }
;
%%