links.opp.exp 71.5 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 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
File "links.mly", line 115, characters 28-39:
Warning: the token MINUSLBRACE is unused.
%{

open Utility
open List
open Sugartypes

(* Generation of fresh type variables *)
      
let type_variable_counter = ref 0
  
let fresh_type_variable : subkind -> datatype =
  function subkind -> 
    incr type_variable_counter; TypeVar ("_" ^ string_of_int (!type_variable_counter), subkind)
  
let fresh_rigid_type_variable : subkind -> datatype =
  function subkind -> 
    incr type_variable_counter; RigidTypeVar ("_" ^ string_of_int (!type_variable_counter), subkind)

let fresh_row_variable : subkind -> row_var =
  function subkind -> 
    incr type_variable_counter; `Open ("_" ^ string_of_int (!type_variable_counter), subkind)

let fresh_rigid_row_variable : subkind -> row_var =
  function subkind -> 
    incr type_variable_counter; `OpenRigid ("_" ^ string_of_int (!type_variable_counter), subkind)

let fresh_presence_variable : unit -> fieldspec =
  function () -> 
    incr type_variable_counter; `Var ("_" ^ string_of_int (!type_variable_counter))
  
let fresh_rigid_presence_variable : unit -> fieldspec =
  function () -> 
    incr type_variable_counter; `RigidVar ("_" ^ string_of_int (!type_variable_counter))

let ensure_match (start, finish, _) (opening : string) (closing : string) = function
  | result when opening = closing -> result
  | _ -> raise (ConcreteSyntaxError ("Closing tag '" ^ closing ^ "' does not match start tag '" ^ opening ^ "'.",
                                     (start, finish, None)))

let pos () : Sugartypes.position = Parsing.symbol_start_pos (), Parsing.symbol_end_pos (), None

let default_fixity = Num.num_of_int 9

let annotate (signame, datatype) : _ -> binding = 
  let checksig (signame, _) name =
    if signame <> name then 
      raise (ConcreteSyntaxError
               ("Signature for `" ^ signame ^ "' should precede definition of `"
                ^ signame ^ "', not `"^ name ^"'.",
                pos ())) in
    function
      | `Fun ((name, bpos), phrase, location, dpos) ->
          let _ = checksig signame name in
            `Fun ((name, None, bpos), ([], phrase), location, Some datatype), dpos
      | `Var (((name, bpos), phrase, location), dpos) ->
          let _ = checksig signame name in
            `Val ([], (`Variable (name, None, bpos), dpos), phrase, location, Some datatype), dpos

let attach_kind pos (t, k) =
   match k with
     | `Type -> `TypeVar (t, `Any)
     | `BaseType -> `TypeVar (t, `Base)
     | `Row -> `RowVar (t, `Any)
     | `BaseRow -> `RowVar (t, `Base)
     | `Presence -> `PresenceVar t

let attach_subkind pos (t, k) =
   match k with
     | "Any" -> t
     | "Base" ->
         begin
           match t with
             | TypeVar (x, _) -> TypeVar (x, `Base)
             | RigidTypeVar (x, _) -> RigidTypeVar (x, `Base)
             | _ -> assert false
         end
     | s -> raise (ConcreteSyntaxError ("Unknown subkind", pos))

let attach_row_subkind pos (r, k) =
   match k with
     | "Any" -> r
     | "Base" ->
         begin
           match r with
             | `Open (x, _) -> `Open (x, `Base)
             | `OpenRigid (x, _) -> `OpenRigid (x, `Base)
             | _ -> assert false
         end
     | s -> raise (ConcreteSyntaxError ("Unknown subkind", pos))

let row_with field (fields, row_var) = field::fields, row_var

(* this preserves 1-tuples *)
let make_tuple pos =
  function
    | [e] -> `RecordLit ([("1", e)], None), pos
    | es -> `TupleLit es, pos

let labels = List.map fst

let parseRegexFlags f =
  let rec asList f i l = 
    if (i == String.length f) then
      List.rev l
    else
      asList f (i+1) ((String.get f i)::l) in
    List.map (function 'l' -> `RegexList | 'n' -> `RegexNative | 'g' -> `RegexGlobal) (asList f 0 [])

let datatype d = d, None

%}
%start file
%start interactive
%start just_datatype
%token YIELDS
%token WITH
%token WHERE
%token VBAR
%token <string> VARIABLE
%token VAR
%token VALUES
%token UPDATE
%token UNDERSCORE
%token <Num.num> UINTEGER
%token <float> UFLOAT
%token TYPENAME
%token TYPE
%token TRUE
%token TILDE
%token TABLEHANDLE
%token TABLE
%token SWITCH
%token <string> STRING
%token STAR
%token SSLASH
%token SQUIGRARROW
%token SPAWNWAIT
%token SPAWN
%token SLASHRXML
%token <string> SLASHFLAGS
%token SLASH
%token SIG
%token SET
%token SERVER
%token SEMICOLON
%token RXML
%token RQUOTE
%token RPAREN
%token ROW
%token RETURNING
%token <string> REGEXREPL
%token RECEIVE
%token READONLY
%token RBRACKET
%token RBRACE
%token RARROW
%token <char*char> RANGE
%token <string> QUOTEDMETA
%token <string> QUESTIONVAR
%token QUESTION
%token QUERY
%token PRESENCE
%token <string> PREFIXOP
%token <[`Left|`Right|`None|`Pre|`Post] -> int -> string -> unit> PREFIX
%token <string> POSTFIXOP
%token <[`Left|`Right|`None|`Pre|`Post] -> int -> string -> unit> POSTFIX
%token PLUS
%token PAGE
%token ORDERBY
%token OP
%token NATIVE
%token MU
%token MINUSLBRACE
%token MINUSDOT
%token MINUS
%token <string> LXML
%token LQUOTE
%token LPAREN
%token LLARROW
%token LBRACKETBAR
%token LBRACKET
%token LBRACEBAR
%token LBRACE
%token LARROW
%token <string> KEYWORD
%token INSERT
%token <string> INFIXR9
%token <string> INFIXR8
%token <string> INFIXR7
%token <string> INFIXR6
%token <string> INFIXR5
%token <string> INFIXR4
%token <string> INFIXR3
%token <string> INFIXR2
%token <string> INFIXR1
%token <string> INFIXR0
%token <[`Left|`Right|`None|`Pre|`Post] -> int -> string -> unit> INFIXR
%token <string> INFIXL9
%token <string> INFIXL8
%token <string> INFIXL7
%token <string> INFIXL6
%token <string> INFIXL5
%token <string> INFIXL4
%token <string> INFIXL3
%token <string> INFIXL2
%token <string> INFIXL1
%token <string> INFIXL0
%token <[`Left|`Right|`None|`Pre|`Post] -> int -> string -> unit> INFIXL
%token <string> INFIX9
%token <string> INFIX8
%token <string> INFIX7
%token <string> INFIX6
%token <string> INFIX5
%token <string> INFIX4
%token <string> INFIX3
%token <string> INFIX2
%token <string> INFIX1
%token <string> INFIX0
%token <[`Left|`Right|`None|`Pre|`Post] -> int -> string -> unit> INFIX
%token INCLUDE
%token IN
%token IF
%token FUN
%token FROM
%token FORMLET
%token FORALL
%token FOR
%token FATRARROW
%token FALSE
%token ESCAPE
%token EQUALSTILDE
%token EQ
%token <string> ENDTAG
%token END
%token ELSE
%token DOTDOT
%token DOT
%token DOLLAR
%token DELETE
%token DEFAULT
%token DATABASE
%token <string> CONSTRUCTOR
%token COMMA
%token COLONCOLON
%token COLON
%token CLIENT
%token <char> CHAR
%token <string> CDATA
%token CASE
%token CARET
%token BASETYPE
%token BASEROW
%token BASE
%token BARRBRACKET
%token BARRBRACE
%token BARBAR
%token AS
%token ANY
%token AMPAMP
%token ALTERNATE
%token ALIEN

%type <Sugartypes.phrase> atomic_expression
%type <(string * Sugartypes.phrase list) list> attr_list
%type <Sugartypes.phrase list> attr_val
%type <Sugartypes.binding> binding
%type <Sugartypes.constant * Sugartypes.position> constant
%type <Sugartypes.datatype> datatype
%type <Sugartypes.binding list * Sugartypes.phrase option> file
%type <Sugartypes.sentence> interactive
%type <Sugartypes.datatype> just_datatype
%type <Sugartypes.pattern> pattern
%type <Sugartypes.phrase> postfix_expression
%type <Sugartypes.phrase> primary_expression
%type <Sugartypes.regex> regex_pattern
%type <Sugartypes.regex> regex_pattern_alternate
%type <Sugartypes.regex list> regex_pattern_sequence
%type <(Sugartypes.name * Sugartypes.position) * Sugartypes.funlit * Sugartypes.location * Sugartypes.position> tlfunbinding
%%

interactive:
| _1 = preamble_declaration
    {                                                               ( `Definitions [_1] )}
| _1 = nofun_declaration
    {                                                               ( `Definitions [_1] )}
| _1 = fun_declarations _2 = SEMICOLON
    {                                                               ( `Definitions _1 )}
| _1 = SEMICOLON
    {                                                               ( `Definitions [] )}
| _1 = exp _2 = SEMICOLON
    {                                                               ( `Expression _1 )}
| _1 = directive
    {                                                               ( `Directive _1 )}
| _1 = END
    {                                                               ( `Directive ("quit", []) (* rather hackish *) )}

file:
| _1 = preamble _2 = declarations _3 = exp _4 = END
    {                                                               ( _1 @ _2, Some _3 )}
| _1 = preamble _2 = exp _3 = END
    {                                                               ( _1, Some _2 )}
| _1 = preamble _2 = declarations _3 = END
    {                                                               ( _1 @ _2, None )}

directive:
| _1 = KEYWORD _2 = args _3 = SEMICOLON
    {                                                               ( (_1, _2) )}

args:
| 
    {                                                               ( [] )}
| _1 = arg _2 = args
    {                                                               ( _1 :: _2 )}

arg:
| _1 = STRING
    {                                                               ( _1 )}
| _1 = VARIABLE
    {                                                               ( _1 )}
| _1 = CONSTRUCTOR
    {                                                               ( _1 )}
| _1 = UINTEGER
    {                                                               ( Num.string_of_num _1 )}
| _1 = UFLOAT
    {                                                               ( string_of_float _1 )}
| _1 = TRUE
    {                                                               ( "true" )}
| _1 = FALSE
    {                                                               ( "false" )}

var:
| _1 = VARIABLE
    {                                                               ( _1, pos() )}

preamble:
| _1 = preamble_declaration _2 = preamble
    {                                                               ( _1 :: _2 )}
| 
    {                                                               ( [] )}

declarations:
| _1 = declarations _2 = declaration
    {                                                               ( _1 @ [_2] )}
| _1 = declaration
    {                                                               ( [_1] )}

declaration:
| _1 = fun_declaration
    {                                                               ( _1 )}
| _1 = nofun_declaration
    {                                                               ( _1 )}

preamble_declaration:
| _1 = INCLUDE _2 = STRING
    {                                                               ( `Include _2, pos() )}

nofun_declaration:
| _1 = ALIEN _2 = VARIABLE _3 = var _4 = COLON _5 = datatype _6 = SEMICOLON
    {                                                               ( let (name, name_pos) = _3 in
                                                                   `Foreign ((name, None, name_pos), _2, datatype _5), pos() )}
| _1 = fixity _2 = perhaps_uinteger _3 = op _4 = SEMICOLON
    {                                                               ( let assoc, set = _1 in
                                                                   set assoc (Num.int_of_num (from_option default_fixity _2)) (fst _3); 
                                                                   (`Infix, pos()) )}
| _1 = tlvarbinding _2 = SEMICOLON
    {                                                               ( let ((d,dpos),p,l), pos = _1
                                                                 in `Val ([], (`Variable (d, None, dpos), pos),p,l,None), pos )}
| _1 = signature _2 = tlvarbinding _3 = SEMICOLON
    {                                                               ( annotate _1 (`Var _2) )}
| _1 = typedecl _2 = SEMICOLON
    {                                                               ( _1 )}

fun_declarations:
| _1 = fun_declarations _2 = fun_declaration
    {                                                               ( _1 @ [_2] )}
| _1 = fun_declaration
    {                                                               ( [_1] )}

fun_declaration:
| _1 = tlfunbinding
    {                                                               ( let ((d,dpos),p,l, pos) = _1
                                                                 in `Fun ((d, None, dpos),([],p),l,None), pos )}
| _1 = signature _2 = tlfunbinding
    {                                                               ( annotate _1 (`Fun _2) )}

perhaps_uinteger:
| 
    {                                                               ( None )}
| _1 = UINTEGER
    {                                                               ( Some _1 )}

prefixop:
| _1 = PREFIXOP
    {                                                               ( _1, pos() )}

postfixop:
| _1 = POSTFIXOP
    {                                                               ( _1, pos() )}

tlfunbinding:
| _1 = FUN _2 = var _3 = arg_lists _4 = perhaps_location _5 = block
    {                                                               ( (_2, (_3, (`Block _5, pos ())), _4, pos()) )}
| _1 = OP _2 = pattern _3 = op _4 = pattern _5 = perhaps_location _6 = block
    {                                                               ( (_3, ([[_2; _4]], (`Block _6, pos ())), _5, pos ()) )}
| _1 = OP _2 = prefixop _3 = pattern _4 = perhaps_location _5 = block
    {                                                               ( (_2, ([[_3]], (`Block _5, pos ())), _4, pos ()) )}
| _1 = OP _2 = pattern _3 = postfixop _4 = perhaps_location _5 = block
    {                                                               ( (_3, ([[_2]], (`Block _5, pos ())), _4, pos ()) )}

tlvarbinding:
| _1 = VAR _2 = var _3 = perhaps_location _4 = EQ _5 = exp
    {                                                               ( (_2, _5, _3), pos() )}

signature:
| _1 = SIG _2 = var _3 = COLON _4 = datatype
    {                                                               ( _2, datatype _4 )}
| _1 = SIG _2 = op _3 = COLON _4 = datatype
    {                                                               ( _2, datatype _4 )}

typedecl:
| _1 = TYPENAME _2 = CONSTRUCTOR _3 = typeargs_opt _4 = EQ _5 = datatype
    {                                                               ( `Type (_2, _3, datatype _5), pos()  )}

typeargs_opt:
| 
    {                                                               ( [] )}
| _1 = LPAREN _2 = varlist _3 = RPAREN
    {                                                               ( _2 )}

kind:
| _1 = TYPE
    {                                                               ( `Type )}
| _1 = BASETYPE
    {                                                               ( `BaseType )}
| _1 = ROW
    {                                                               ( `Row )}
| _1 = BASEROW
    {                                                               ( `BaseRow )}
| _1 = PRESENCE
    {                                                               ( `Presence )}

subkind:
| _1 = ANY
    {                                                               ( "Any" )}
| _1 = BASE
    {                                                               ( "Base" )}

typearg:
| _1 = VARIABLE
    {                                                               ( (`TypeVar (_1, `Any), None) )}
| _1 = VARIABLE _2 = kind
    {                                                               ( (attach_kind (pos()) (_1, _2), None) )}

varlist:
| _1 = typearg
    {                                                               ( [_1] )}
| _1 = typearg _2 = COMMA _3 = varlist
    {                                                               ( _1 :: _3 )}

fixity:
| _1 = INFIX
    {                                                               ( `None, _1 )}
| _1 = INFIXL
    {                                                               ( `Left, _1 )}
| _1 = INFIXR
    {                                                               ( `Right, _1 )}
| _1 = PREFIX
    {                                                               ( `Pre, _1 )}
| _1 = POSTFIX
    {                                                               ( `Post, _1 )}

perhaps_location:
| _1 = SERVER
    {                                                               ( `Server )}
| _1 = CLIENT
    {                                                               ( `Client )}
| _1 = NATIVE
    {                                                               ( `Native )}
| 
    {                                                               ( `Unknown )}

constant:
| _1 = UINTEGER
    {                                                               ( `Int _1    , pos() )}
| _1 = UFLOAT
    {                                                               ( `Float _1  , pos() )}
| _1 = STRING
    {                                                               ( `String _1 , pos() )}
| _1 = TRUE
    {                                                               ( `Bool true , pos() )}
| _1 = FALSE
    {                                                               ( `Bool false, pos() )}
| _1 = CHAR
    {                                                               ( `Char _1   , pos() )}

atomic_expression:
| _1 = VARIABLE
    {                                                               ( `Var _1, pos() )}
| _1 = constant
    {                                                               ( let c, p = _1 in `Constant c, p )}
| _1 = parenthesized_thing
    {                                                               ( _1 )}

primary_expression:
| _1 = atomic_expression
    {                                                               ( _1 )}
| _1 = LBRACKET _2 = RBRACKET
    {                                                               ( `ListLit ([], None), pos() )}
| _1 = LBRACKET _2 = exps _3 = RBRACKET
    {                                                               ( `ListLit (_2, None), pos() )}
| _1 = LBRACKET _2 = exp _3 = DOTDOT _4 = exp _5 = RBRACKET
    {                                                               ( `RangeLit(_2, _4), pos() )}
| _1 = xml
    {                                                               ( _1 )}
| _1 = FUN _2 = arg_lists _3 = block
    {                                                               ( `FunLit (None, (_2, (`Block _3, pos ()))), pos() )}

constructor_expression:
| _1 = CONSTRUCTOR
    {                                                               ( `ConstructorLit(_1, None, None), pos() )}
| _1 = CONSTRUCTOR _2 = parenthesized_thing
    {                                                               ( `ConstructorLit(_1, Some _2, None), pos() )}

parenthesized_thing:
| _1 = LPAREN _2 = binop _3 = RPAREN
    {                                                               ( `Section _2, pos() )}
| _1 = LPAREN _2 = DOT _3 = record_label _4 = RPAREN
    {                                                               ( `Section (`Project _3), pos() )}
| _1 = LPAREN _2 = RPAREN
    {                                                               ( `RecordLit ([], None), pos() )}
| _1 = LPAREN _2 = labeled_exps _3 = VBAR _4 = exp _5 = RPAREN
    {                                                               ( `RecordLit (_2, Some _4), pos() )}
| _1 = LPAREN _2 = labeled_exps _3 = RPAREN
    {                                                               ( `RecordLit (_2, None),               pos() )}
| _1 = LPAREN _2 = exps _3 = RPAREN
    {                                                               ( `TupleLit (_2), pos() )}
| _1 = LPAREN _2 = exp _3 = WITH _4 = labeled_exps _5 = RPAREN
    {                                                               ( `With (_2, _4), pos() )}

binop:
| _1 = MINUS
    {                                                               ( `Minus )}
| _1 = MINUSDOT
    {                                                               ( `FloatMinus )}
| _1 = op
    {                                                               ( `Name (fst _1) )}

op:
| _1 = INFIX0
    {                                                               ( _1, pos() )}
| _1 = INFIXL0
    {                                                               ( _1, pos() )}
| _1 = INFIXR0
    {                                                               ( _1, pos() )}
| _1 = INFIX1
    {                                                               ( _1, pos() )}
| _1 = INFIXL1
    {                                                               ( _1, pos() )}
| _1 = INFIXR1
    {                                                               ( _1, pos() )}
| _1 = INFIX2
    {                                                               ( _1, pos() )}
| _1 = INFIXL2
    {                                                               ( _1, pos() )}
| _1 = INFIXR2
    {                                                               ( _1, pos() )}
| _1 = INFIX3
    {                                                               ( _1, pos() )}
| _1 = INFIXL3
    {                                                               ( _1, pos() )}
| _1 = INFIXR3
    {                                                               ( _1, pos() )}
| _1 = INFIX4
    {                                                               ( _1, pos() )}
| _1 = INFIXL4
    {                                                               ( _1, pos() )}
| _1 = INFIXR4
    {                                                               ( _1, pos() )}
| _1 = INFIX5
    {                                                               ( _1, pos() )}
| _1 = INFIXL5
    {                                                               ( _1, pos() )}
| _1 = INFIXR5
    {                                                               ( _1, pos() )}
| _1 = INFIX6
    {                                                               ( _1, pos() )}
| _1 = INFIXL6
    {                                                               ( _1, pos() )}
| _1 = INFIXR6
    {                                                               ( _1, pos() )}
| _1 = INFIX7
    {                                                               ( _1, pos() )}
| _1 = INFIXL7
    {                                                               ( _1, pos() )}
| _1 = INFIXR7
    {                                                               ( _1, pos() )}
| _1 = INFIX8
    {                                                               ( _1, pos() )}
| _1 = INFIXL8
    {                                                               ( _1, pos() )}
| _1 = INFIXR8
    {                                                               ( _1, pos() )}
| _1 = INFIX9
    {                                                               ( _1, pos() )}
| _1 = INFIXL9
    {                                                               ( _1, pos() )}
| _1 = INFIXR9
    {                                                               ( _1, pos() )}

postfix_expression:
| _1 = primary_expression
    {                                                               ( _1 )}
| _1 = primary_expression _2 = POSTFIXOP
    {                                                               ( `UnaryAppl (([], `Name _2), _1), pos() )}
| _1 = block
    {                                                               ( `Block _1, pos () )}
| _1 = SPAWN _2 = block
    {                                                               ( `Spawn ((`Block _2, pos()), None), pos () )}
| _1 = SPAWNWAIT _2 = block
    {                                                               ( `SpawnWait ((`Block _2, pos()), None), pos () )}
| _1 = QUERY _2 = block
    {                                                               ( `Query (None, (`Block _2, pos ()), None), pos () )}
| _1 = QUERY _2 = LBRACKET _3 = exp _4 = RBRACKET _5 = block
    {                                                               ( `Query (Some (_3,
                                                                               (`Constant (`Int (Num.num_of_int 0)), pos ())),
                                                                         (`Block _5, pos ()), None), pos () )}
| _1 = QUERY _2 = LBRACKET _3 = exp _4 = COMMA _5 = exp _6 = RBRACKET _7 = block
    {                                                               ( `Query (Some (_3, _5), (`Block _7, pos ()), None), pos () )}
| _1 = postfix_expression _2 = arg_spec
    {                                                               ( `FnAppl (_1, _2), pos() )}
| _1 = postfix_expression _2 = DOT _3 = record_label
    {                                                               ( `Projection (_1, _3), pos() )}

arg_spec:
| _1 = LPAREN _2 = RPAREN
    {                                                               ( [] )}
| _1 = LPAREN _2 = exps _3 = RPAREN
    {                                                               ( _2 )}

exps:
| _1 = exp _2 = COMMA _3 = exps
    {                                                               ( _1 :: _3 )}
| _1 = exp
    {                                                               ( [_1] )}

unary_expression:
| _1 = MINUS _2 = unary_expression
    {                                                               ( `UnaryAppl (([], `Minus),      _2), pos() )}
| _1 = MINUSDOT _2 = unary_expression
    {                                                               ( `UnaryAppl (([], `FloatMinus), _2), pos() )}
| _1 = PREFIXOP _2 = unary_expression
    {                                                               ( `UnaryAppl (([], `Name _1), _2), pos() )}
| _1 = postfix_expression
    {                                                               ( _1 )}
| _1 = constructor_expression
    {                                                               ( _1 )}

infixr_9:
| _1 = unary_expression
    {                                                               ( _1 )}
| _1 = unary_expression _2 = INFIX9 _3 = unary_expression
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = unary_expression _2 = INFIXR9 _3 = infixr_9
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixl_9:
| _1 = infixr_9
    {                                                               ( _1 )}
| _1 = infixl_9 _2 = INFIXL9 _3 = infixr_9
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixr_8:
| _1 = infixl_9
    {                                                               ( _1 )}
| _1 = infixl_9 _2 = INFIX8 _3 = infixl_9
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_9 _2 = INFIXR8 _3 = infixr_8
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_9 _2 = COLONCOLON _3 = infixr_8
    {                                                               ( `InfixAppl (([], `Cons), _1, _3), pos() )}

infixl_8:
| _1 = infixr_8
    {                                                               ( _1 )}
| _1 = infixl_8 _2 = INFIXL8 _3 = infixr_8
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixr_7:
| _1 = infixl_8
    {                                                               ( _1 )}
| _1 = infixl_8 _2 = INFIX7 _3 = infixl_8
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_8 _2 = INFIXR7 _3 = infixr_7
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixl_7:
| _1 = infixr_7
    {                                                               ( _1 )}
| _1 = infixl_7 _2 = INFIXL7 _3 = infixr_7
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixr_6:
| _1 = infixl_7
    {                                                               ( _1 )}
| _1 = infixl_7 _2 = INFIX6 _3 = infixl_7
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_7 _2 = INFIXR6 _3 = infixr_6
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixl_6:
| _1 = infixr_6
    {                                                               ( _1 )}
| _1 = infixl_6 _2 = INFIXL6 _3 = infixr_6
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_6 _2 = MINUS _3 = infixr_6
    {                                                               ( `InfixAppl (([], `Minus), _1, _3), pos() )}
| _1 = infixl_6 _2 = MINUSDOT _3 = infixr_6
    {                                                               ( `InfixAppl (([], `FloatMinus), _1, _3), pos() )}

infixr_5:
| _1 = infixl_6
    {                                                               ( _1 )}
| _1 = infixl_6 _2 = INFIX5 _3 = infixl_6
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_6 _2 = INFIXR5 _3 = infixr_5
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixl_5:
| _1 = infixr_5
    {                                                               ( _1 )}
| _1 = infixl_5 _2 = INFIXL5 _3 = infixr_5
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixr_4:
| _1 = infixl_5
    {                                                               ( _1 )}
| _1 = infixl_5 _2 = INFIX4 _3 = infixl_5
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_5 _2 = INFIXR4 _3 = infixr_4
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixr_5 _2 = EQUALSTILDE _3 = regex
    {                                                               ( let r, flags = _3 in `InfixAppl (([], `RegexMatch flags), _1, r), pos() )}

infixl_4:
| _1 = infixr_4
    {                                                               ( _1 )}
| _1 = infixl_4 _2 = INFIXL4 _3 = infixr_4
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixr_3:
| _1 = infixl_4
    {                                                               ( _1 )}
| _1 = infixl_4 _2 = INFIX3 _3 = infixl_4
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_4 _2 = INFIXR3 _3 = infixr_3
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixl_3:
| _1 = infixr_3
    {                                                               ( _1 )}
| _1 = infixl_3 _2 = INFIXL3 _3 = infixr_3
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixr_2:
| _1 = infixl_3
    {                                                               ( _1 )}
| _1 = infixl_3 _2 = INFIX2 _3 = infixl_3
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_3 _2 = INFIXR2 _3 = infixr_2
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixl_2:
| _1 = infixr_2
    {                                                               ( _1 )}
| _1 = infixl_2 _2 = INFIXL2 _3 = infixr_2
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixr_1:
| _1 = infixl_2
    {                                                               ( _1 )}
| _1 = infixl_2 _2 = INFIX1 _3 = infixl_2
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_2 _2 = INFIXR1 _3 = infixr_1
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixl_1:
| _1 = infixr_1
    {                                                               ( _1 )}
| _1 = infixl_1 _2 = INFIXL1 _3 = infixr_1
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixr_0:
| _1 = infixl_1
    {                                                               ( _1 )}
| _1 = infixl_1 _2 = INFIX0 _3 = infixl_1
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}
| _1 = infixl_1 _2 = INFIXR0 _3 = infixr_0
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

infixl_0:
| _1 = infixr_0
    {                                                               ( _1 )}
| _1 = infixl_0 _2 = INFIXL0 _3 = infixr_0
    {                                                               ( `InfixAppl (([], `Name _2), _1, _3), pos() )}

logical_expression:
| _1 = infixl_0
    {                                                               ( _1 )}
| _1 = logical_expression _2 = BARBAR _3 = infixl_0
    {                                                               ( `InfixAppl (([], `Or), _1, _3), pos() )}
| _1 = logical_expression _2 = AMPAMP _3 = infixl_0
    {                                                               ( `InfixAppl (([], `And), _1, _3), pos() )}

typed_expression:
| _1 = logical_expression
    {                                                               ( _1 )}
| _1 = typed_expression _2 = COLON _3 = datatype
    {                                                               ( `TypeAnnotation (_1, datatype _3), pos() )}
| _1 = typed_expression _2 = COLON _3 = datatype _4 = LARROW _5 = datatype
    {                                                               ( `Upcast (_1, datatype _3, datatype _5), pos() )}

db_expression:
| _1 = typed_expression
    {                                                               ( _1 )}
| _1 = DELETE _2 = LPAREN _3 = table_generator _4 = RPAREN _5 = perhaps_where
    {                                                               ( let pat, phrase = _3 in `DBDelete (pat, phrase, _5), pos() )}
| _1 = UPDATE _2 = LPAREN _3 = table_generator _4 = RPAREN _5 = perhaps_where _6 = SET _7 = LPAREN _8 = labeled_exps _9 = RPAREN
    {                                                               ( let pat, phrase = _3 in `DBUpdate(pat, phrase, _5, _8), pos() )}

xml:
| _1 = xml_tree
    {                                                               ( _1 )}

xmlid:
| _1 = VARIABLE
    {                                                               ( _1 )}

attrs:
| _1 = block
    {                                                               ( [], Some (`Block _1, pos ()) )}
| _1 = attr_list
    {                                                               ( _1, None )}
| _1 = attr_list _2 = block
    {                                                               ( _1, Some (`Block _2, pos ()) )}

attr_list:
| _1 = attr
    {                                                               ( [_1] )}
| _1 = attr_list _2 = attr
    {                                                               ( _2 :: _1 )}

attr:
| _1 = xmlid _2 = EQ _3 = LQUOTE _4 = attr_val _5 = RQUOTE
    {                                                               ( (_1, _4) )}
| _1 = xmlid _2 = EQ _3 = LQUOTE _4 = RQUOTE
    {                                                               ( (_1, [(`Constant (`String ""), pos() : Sugartypes.phrase)]) )}

attr_val:
| _1 = block
    {                                                               ( [`Block _1, pos ()] )}
| _1 = STRING
    {                                                               ( [`Constant (`String _1), pos()] )}
| _1 = block _2 = attr_val
    {                                                               ( (`Block _1, pos ()) :: _2 )}
| _1 = STRING _2 = attr_val
    {                                                               ( (`Constant (`String _1), pos()) :: _2)}

xml_tree:
| _1 = LXML _2 = SLASHRXML
    {                                                               ( `Xml (_1, [], None, []), pos() )}
| _1 = LXML _2 = RXML _3 = ENDTAG
    {                                                               ( ensure_match (pos()) _1 _3 (`Xml (_1, [], None, []), pos()) )}
| _1 = LXML _2 = RXML _3 = xml_contents_list _4 = ENDTAG
    {                                                               ( ensure_match (pos()) _1 _4 (`Xml (_1, [], None, _3), pos()) )}
| _1 = LXML _2 = attrs _3 = RXML _4 = ENDTAG
    {                                                               ( ensure_match (pos()) _1 _4 (`Xml (_1, fst _2, snd _2, []), pos()) )}
| _1 = LXML _2 = attrs _3 = SLASHRXML
    {                                                               ( `Xml (_1, fst _2, snd _2, []), pos() )}
| _1 = LXML _2 = attrs _3 = RXML _4 = xml_contents_list _5 = ENDTAG
    {                                                               ( ensure_match (pos()) _1 _5 (`Xml (_1, fst _2, snd _2, _4), pos()) )}

xml_contents_list:
| _1 = xml_contents
    {                                                               ( [_1] )}
| _1 = xml_contents _2 = xml_contents_list
    {                                                               ( _1 :: _2 )}

xml_contents:
| _1 = block
    {                                                               ( `Block _1, pos () )}
| _1 = formlet_binding
    {                                                               ( _1 )}
| _1 = formlet_placement
    {                                                               ( _1 )}
| _1 = page_placement
    {                                                               ( _1 )}
| _1 = xml_tree
    {                                                               ( _1 )}
| _1 = CDATA
    {                                                               ( `TextNode (Utility.xml_unescape _1), pos() )}

formlet_binding:
| _1 = LBRACE _2 = logical_expression _3 = RARROW _4 = pattern _5 = RBRACE
    {                                                               ( `FormBinding(_2, _4), pos())}

formlet_placement:
| _1 = LBRACE _2 = logical_expression _3 = FATRARROW _4 = logical_expression _5 = RBRACE
    {                                                               ( `FormletPlacement (_2, _4, (`ListLit ([], None), pos())), pos () )}
| _1 = LBRACE _2 = logical_expression _3 = FATRARROW _4 = logical_expression _5 = WITH _6 = logical_expression _7 = RBRACE
    {                                                               ( `FormletPlacement (_2, _4, _6), pos () )}

page_placement:
| _1 = LBRACEBAR _2 = exp _3 = BARRBRACE
    {                                                               ( `PagePlacement _2, pos() )}

conditional_expression:
| _1 = db_expression
    {                                                               ( _1 )}
| _1 = IF _2 = LPAREN _3 = exp _4 = RPAREN _5 = exp _6 = ELSE _7 = exp
    {                                                               ( `Conditional (_3, _5, _7), pos() )}

cases:
| _1 = case
    {                                                               ( [_1] )}
| _1 = case _2 = cases
    {                                                               ( _1 :: _2 )}

case:
| _1 = CASE _2 = pattern _3 = RARROW _4 = block_contents
    {                                                               ( _2, (`Block (_4), pos()) )}

perhaps_cases:
| 
    {                                                               ( [] )}
| _1 = cases
    {                                                               ( _1 )}

case_expression:
| _1 = conditional_expression
    {                                                               ( _1 )}
| _1 = SWITCH _2 = LPAREN _3 = exp _4 = RPAREN _5 = LBRACE _6 = perhaps_cases _7 = RBRACE
    {                                                               ( `Switch (_3, _6, None), pos() )}
| _1 = RECEIVE _2 = LBRACE _3 = perhaps_cases _4 = RBRACE
    {                                                               ( `Receive (_3, None), pos() )}

iteration_expression:
| _1 = case_expression
    {                                                               ( _1 )}
| _1 = FOR _2 = LPAREN _3 = perhaps_generators _4 = RPAREN _5 = perhaps_where _6 = perhaps_orderby _7 = exp
    {                                                               ( `Iteration (_3, _7, _5, _6), pos() )}

perhaps_generators:
| 
    {                                                               ( [] )}
| _1 = generators
    {                                                               ( _1 )}

generators:
| _1 = generator
    {                                                               ( [_1] )}
| _1 = generator _2 = COMMA _3 = generators
    {                                                               ( _1 :: _3 )}

generator:
| _1 = list_generator
    {                                                               ( `List _1 )}
| _1 = table_generator
    {                                                               ( `Table _1 )}

list_generator:
| _1 = pattern _2 = LARROW _3 = exp
    {                                                               ( (_1, _3) )}

table_generator:
| _1 = pattern _2 = LLARROW _3 = exp
    {                                                               ( (_1, _3) )}

perhaps_where:
| 
    {                                                               ( None )}
| _1 = WHERE _2 = LPAREN _3 = exp _4 = RPAREN
    {                                                               ( Some _3 )}

perhaps_orderby:
| 
    {                                                               ( None )}
| _1 = ORDERBY _2 = LPAREN _3 = exps _4 = RPAREN
    {                                                               ( Some (make_tuple (pos()) _3) )}

escape_expression:
| _1 = iteration_expression
    {                                                               ( _1 )}
| _1 = ESCAPE _2 = var _3 = IN _4 = postfix_expression
    {                                                               ( `Escape ((fst _2, None, snd _2), _4), pos() )}

formlet_expression:
| _1 = escape_expression
    {                                                               ( _1 )}
| _1 = FORMLET _2 = xml _3 = YIELDS _4 = exp
    {                                                               ( `Formlet (_2, _4), pos() )}
| _1 = PAGE _2 = xml
    {                                                               ( `Page (_2), pos() )}

table_expression:
| _1 = formlet_expression
    {                                                               ( _1 )}
| _1 = TABLE _2 = exp _3 = WITH _4 = datatype _5 = perhaps_table_constraints _6 = FROM _7 = exp
    {                                                               ( `TableLit (_2, datatype _4, _5, _7), pos())}

perhaps_table_constraints:
| _1 = WHERE _2 = table_constraints
    {                                                               ( _2 )}
| 
    {                                                               ( [] )}

table_constraints:
| _1 = record_label _2 = field_constraints
    {                                                               ( [(_1, _2)] )}
| _1 = record_label _2 = field_constraints _3 = COMMA _4 = table_constraints
    {                                                               ( (_1, _2) :: _4 )}

field_constraints:
| _1 = field_constraint
    {                                                               ( [_1] )}
| _1 = field_constraint _2 = field_constraints
    {                                                               ( _1 :: _2 )}

field_constraint:
| _1 = READONLY
    {                                                               ( `Readonly )}
| _1 = DEFAULT
    {                                                               ( `Default )}

perhaps_db_args:
| _1 = atomic_expression
    {                                                               ( Some _1 )}
| 
    {                                                               ( None )}

perhaps_db_driver:
| _1 = atomic_expression _2 = perhaps_db_args
    {                                                               ( Some _1, _2 )}
| 
    {                                                               ( None, None )}

database_expression:
| _1 = table_expression
    {                                                               ( _1 )}
| _1 = INSERT _2 = exp _3 = VALUES _4 = LPAREN _5 = RPAREN _6 = exp
    {                                                               ( `DBInsert (_2, [], _6, None), pos() )}
| _1 = INSERT _2 = exp _3 = VALUES _4 = LPAREN _5 = record_labels _6 = RPAREN _7 = exp
    {                                                               ( `DBInsert (_2, _5, _7, None), pos() )}
| _1 = INSERT _2 = exp _3 = VALUES _4 = LBRACKET _5 = LPAREN _6 = labeled_exps _7 = RPAREN _8 = RBRACKET
    {                                                               ( `DBInsert (_2,
                                                                            labels _6,
                                                                            (`ListLit ([`RecordLit (_6, None), pos()], None), pos()),
                                                                            None),
                                                                 pos() )}
| _1 = INSERT _2 = exp _3 = VALUES _4 = LPAREN _5 = RPAREN _6 = db_expression _7 = RETURNING _8 = VARIABLE
    {                                                               ( `DBInsert (_2, [], _6, Some (`Constant (`String _8), pos())), pos() )}
| _1 = INSERT _2 = exp _3 = VALUES _4 = LPAREN _5 = record_labels _6 = RPAREN _7 = db_expression _8 = RETURNING _9 = VARIABLE
    {                                                               ( `DBInsert (_2, _5, _7, Some (`Constant (`String _9), pos())), pos() )}
| _1 = INSERT _2 = exp _3 = VALUES _4 = LBRACKET _5 = LPAREN _6 = RPAREN _7 = RBRACKET _8 = RETURNING _9 = VARIABLE
    {                                                               ( `DBInsert (_2,
                                                                            [],
                                                                            (`ListLit ([`RecordLit ([], None), pos()], None), pos()),
                                                                            Some (`Constant (`String _9), pos())),
                                                                 pos() )}
| _1 = INSERT _2 = exp _3 = VALUES _4 = LBRACKET _5 = LPAREN _6 = labeled_exps _7 = RPAREN _8 = RBRACKET _9 = RETURNING _10 = VARIABLE
    {                                                               ( `DBInsert (_2,
                                                                            labels _6,
                                                                            (`ListLit ([`RecordLit (_6, None), pos()], None), pos()),
                                                                            Some (`Constant (`String _10), pos())),
                                                                 pos() )}
| _1 = DATABASE _2 = atomic_expression _3 = perhaps_db_driver
    {                                                               ( `DatabaseLit (_2, _3), pos() )}

record_labels:
| _1 = record_label _2 = COMMA _3 = record_labels
    {                                                               ( _1 :: _3 )}
| _1 = record_label
    {                                                               ( [_1] )}

binding:
| _1 = VAR _2 = pattern _3 = EQ _4 = exp _5 = SEMICOLON
    {                                                               ( `Val ([], _2, _4, `Unknown, None), pos () )}
| _1 = exp _2 = SEMICOLON
    {                                                               ( `Exp _1, pos () )}
| _1 = FUN _2 = var _3 = arg_lists _4 = block
    {                                                               ( `Fun ((fst _2, None, snd _2), ([], (_3, (`Block _4, pos ()))), `Unknown, None), pos () )}
| _1 = typedecl _2 = SEMICOLON
    {                                                               ( _1 )}

bindings:
| _1 = binding
    {                                                               ( [_1] )}
| _1 = bindings _2 = binding
    {                                                               ( _1 @ [_2] )}

block:
| _1 = LBRACE _2 = block_contents _3 = RBRACE
    {                                                               ( _2 )}

block_contents:
| _1 = bindings _2 = exp _3 = SEMICOLON
    {                                                               ( (_1 @ [`Exp _2, pos ()], (`RecordLit ([], None), pos())) )}
| _1 = bindings _2 = exp
    {                                                               ( (_1, _2) )}
| _1 = exp _2 = SEMICOLON
    {                                                               ( ([`Exp _1, pos ()], (`RecordLit ([], None), pos())) )}
| _1 = exp
    {                                                               ( [], _1 )}
| _1 = perhaps_semi
    {                                                               ( ([], (`TupleLit [], pos())) )}

perhaps_semi:
| _1 = SEMICOLON
    {                                                               ()}
| 
    {                                                               ()}

exp:
| _1 = database_expression
    {                                                               ( _1 )}

labeled_exps:
| _1 = record_label _2 = EQ _3 = exp
    {                                                               ( [_1, _3] )}
| _1 = record_label _2 = EQ _3 = exp _4 = COMMA _5 = labeled_exps
    {                                                               ( (_1, _3) :: _5 )}

just_datatype:
| _1 = datatype _2 = END
    {                                                               ( _1 )}

datatype:
| _1 = mu_datatype
    {                                                               ( _1 )}
| _1 = straight_arrow
    {                                                               ( _1 )}
| _1 = squiggly_arrow
    {                                                               ( _1 )}

arrow_prefix:
| _1 = LBRACE _2 = RBRACE
    {                                                               ( ([], `Closed) )}
| _1 = LBRACE _2 = efields _3 = RBRACE
    {                                                               ( _2 )}

straight_arrow_prefix:
| _1 = arrow_prefix
    {                                                               ( _1 )}
| _1 = MINUS _2 = nonrec_row_var
    {                                                               ( ([], _2) )}
| _1 = MINUS _2 = kinded_nonrec_row_var
    {                                                               ( ([], _2) )}

squig_arrow_prefix:
| _1 = hear_arrow_prefix
    {                                                               ( _1 )}
| _1 = arrow_prefix
    {                                                               ( _1 )}
| _1 = TILDE _2 = nonrec_row_var
    {                                                               ( ([], _2) )}
| _1 = TILDE _2 = kinded_nonrec_row_var
    {                                                               ( ([], _2) )}

hear_arrow_prefix:
| _1 = LBRACE _2 = COLON _3 = datatype _4 = COMMA _5 = efields _6 = RBRACE
    {                                                               ( row_with
                                                                   ("wild", `Present UnitType)
                                                                   (row_with
                                                                      ("hear", `Present _3)
                                                                      _5) )}
| _1 = LBRACE _2 = COLON _3 = datatype _4 = RBRACE
    {                                                               ( ([("wild", `Present UnitType);
                                                                   ("hear", `Present _3)],
                                                                  `Closed) )}
| _1 = LBRACE _2 = COLON _3 = datatype _4 = VBAR _5 = nonrec_row_var _6 = RBRACE
    {                                                               ( ([("wild", `Present UnitType);
                                                                   ("hear", `Present _3)],
                                                                  _5) )}
| _1 = LBRACE _2 = COLON _3 = datatype _4 = VBAR _5 = kinded_nonrec_row_var _6 = RBRACE
    {                                                               ( ([("wild", `Present UnitType);
                                                                   ("hear", `Present _3)],
                                                                  _5) )}

straight_arrow:
| _1 = parenthesized_datatypes _2 = straight_arrow_prefix _3 = RARROW _4 = datatype
    {                                                               ( FunctionType (_1, _2, _4) )}
| _1 = parenthesized_datatypes _2 = RARROW _3 = datatype
    {                                                               ( FunctionType (_1,
                                                                               ([], fresh_rigid_row_variable `Any),
                                                                               _3) )}

squiggly_arrow:
| _1 = parenthesized_datatypes _2 = squig_arrow_prefix _3 = SQUIGRARROW _4 = datatype
    {                                                               ( FunctionType (_1,
                                                                               row_with
                                                                                 ("wild", `Present UnitType)
                                                                                 _2,
                                                                               _4) )}
| _1 = parenthesized_datatypes _2 = SQUIGRARROW _3 = datatype
    {                                                               ( FunctionType (_1,
                                                                               ([("wild", `Present UnitType)],
                                                                                 fresh_rigid_row_variable `Any),
                                                                                _3) )}

mu_datatype:
| _1 = MU _2 = VARIABLE _3 = DOT _4 = mu_datatype
    {                                                               ( MuType (_2, _4) )}
| _1 = forall_datatype
    {                                                               ( _1 )}

forall_datatype:
| _1 = FORALL _2 = varlist _3 = DOT _4 = datatype
    {                                                               ( ForallType (List.map fst _2, _4) )}
| _1 = primary_datatype
    {                                                               ( _1 )}

parenthesized_datatypes:
| _1 = LPAREN _2 = RPAREN
    {                                                               ( [] )}
| _1 = LPAREN _2 = datatypes _3 = RPAREN
    {                                                               ( _2 )}

primary_datatype:
| _1 = parenthesized_datatypes
    {                                                               ( match _1 with
                                                                   | [] -> UnitType
                                                                   | [t] -> t
                                                                   | ts  -> TupleType ts )}
| _1 = LPAREN _2 = rfields _3 = RPAREN
    {                                                               ( RecordType _2 )}
| _1 = TABLEHANDLE _2 = LPAREN _3 = datatype _4 = COMMA _5 = datatype _6 = COMMA _7 = datatype _8 = RPAREN
    {                                                               ( TableType (_3, _5, _7) )}
| _1 = LBRACKETBAR _2 = vrow _3 = BARRBRACKET
    {                                                               ( VariantType _2 )}
| _1 = LBRACKET _2 = datatype _3 = RBRACKET
    {                                                               ( ListType _2 )}
| _1 = type_var
    {                                                               ( _1 )}
| _1 = kinded_type_var
    {                                                               ( _1 )}
| _1 = CONSTRUCTOR
    {                                                               ( match _1 with 
                                                                   | "Bool"    -> PrimitiveType `Bool
                                                                   | "Int"     -> PrimitiveType `Int
                                                                   | "Char"    -> PrimitiveType `Char
                                                                   | "Float"   -> PrimitiveType `Float
                                                                   | "XmlItem" -> PrimitiveType `XmlItem
                                                                   | "String"  -> PrimitiveType `String
                                                                   | "Database"-> DBType
                                                                   | t         -> TypeApplication (t, [])
                                                               )}
| _1 = CONSTRUCTOR _2 = LPAREN _3 = type_arg_list _4 = RPAREN
    {                                                               ( TypeApplication (_1, _3) )}

type_var:
| _1 = VARIABLE
    {                                                               ( RigidTypeVar (_1, `Any) )}
| _1 = QUESTIONVAR
    {                                                               ( TypeVar (_1, `Any) )}
| _1 = UNDERSCORE
    {                                                               ( fresh_rigid_type_variable `Any )}
| _1 = QUESTION
    {                                                               ( fresh_type_variable `Any )}

kinded_type_var:
| _1 = type_var _2 = subkind
    {                                                               ( attach_subkind (pos()) (_1, _2) )}

type_arg_list:
| _1 = type_arg
    {                                                               ( [_1] )}
| _1 = type_arg _2 = COMMA _3 = type_arg_list
    {                                                               ( _1 :: _3 )}

type_arg:
| _1 = datatype
    {                                                               ( `Type _1 )}
| _1 = TYPE _2 = LPAREN _3 = datatype _4 = RPAREN
    {                                                               ( `Type _3 )}
| _1 = ROW _2 = LPAREN _3 = row _4 = RPAREN
    {                                                               ( `Row _3 )}
| _1 = PRESENCE _2 = LPAREN _3 = fieldspec _4 = RPAREN
    {                                                               ( `Presence _3 )}
| _1 = LBRACE _2 = row _3 = RBRACE
    {                                                               ( `Row _2 )}

vrow:
| _1 = vfields
    {                                                               ( _1 )}
| 
    {                                                               ( [], `Closed )}

datatypes:
| _1 = datatype
    {                                                               ( [_1] )}
| _1 = datatype _2 = COMMA _3 = datatypes
    {                                                               ( _1 :: _3 )}

row:
| _1 = fields
    {                                                               ( _1 )}
| 
    {                                                               ( [], `Closed )}

fields:
| _1 = field
    {                                                               ( [_1], `Closed )}
| _1 = field _2 = VBAR _3 = row_var
    {                                                               ( [_1], _3 )}
| _1 = field _2 = VBAR _3 = kinded_row_var
    {                                                               ( [_1], _3 )}
| _1 = VBAR _2 = row_var
    {                                                               ( [], _2 )}
| _1 = VBAR _2 = kinded_row_var
    {                                                               ( [], _2 )}
| _1 = field _2 = COMMA _3 = fields
    {                                                               ( _1 :: fst _3, snd _3 )}

field:
| _1 = field_label
    {                                                               ( _1, `Present UnitType )}
| _1 = field_label _2 = fieldspec
    {                                                               ( _1, _2 )}

field_label:
| _1 = CONSTRUCTOR
    {                                                               ( _1 )}
| _1 = VARIABLE
    {                                                               ( _1 )}
| _1 = STRING
    {                                                               ( _1 )}
| _1 = UINTEGER
    {                                                               ( Num.string_of_num _1 )}

rfields:
| _1 = rfield
    {                                                               ( [_1], `Closed )}
| _1 = rfield _2 = VBAR _3 = row_var
    {                                                               ( [_1], _3 )}
| _1 = rfield _2 = VBAR _3 = kinded_row_var
    {                                                               ( [_1], _3 )}
| _1 = VBAR _2 = row_var
    {                                                               ( [], _2 )}
| _1 = VBAR _2 = kinded_row_var
    {                                                               ( [], _2 )}
| _1 = rfield _2 = COMMA _3 = rfields
    {                                                               ( _1 :: fst _3, snd _3 )}

rfield:
| _1 = record_label
    {                                                               ( _1, `Present UnitType )}
| _1 = record_label _2 = fieldspec
    {                                                               ( _1, _2 )}

record_label:
| _1 = field_label
    {                                                               ( _1 )}

vfields:
| _1 = vfield
    {                                                               ( [_1], `Closed )}
| _1 = row_var
    {                                                               ( [], _1 )}
| _1 = kinded_row_var
    {                                                               ( [], _1 )}
| _1 = vfield _2 = VBAR _3 = vfields
    {                                                               ( _1 :: fst _3, snd _3 )}

vfield:
| _1 = CONSTRUCTOR
    {                                                               ( _1, `Present UnitType )}
| _1 = CONSTRUCTOR _2 = fieldspec
    {                                                               ( _1, _2 )}

efields:
| _1 = efield
    {                                                               ( [_1], `Closed )}
| _1 = efield _2 = VBAR _3 = nonrec_row_var
    {                                                               ( [_1], _3 )}
| _1 = efield _2 = VBAR _3 = kinded_nonrec_row_var
    {                                                               ( [_1], _3 )}
| _1 = VBAR _2 = nonrec_row_var
    {                                                               ( [], _2 )}
| _1 = VBAR _2 = kinded_nonrec_row_var
    {                                                               ( [], _2 )}
| _1 = efield _2 = COMMA _3 = efields
    {                                                               ( _1 :: fst _3, snd _3 )}

efield:
| _1 = effect_label
    {                                                               ( _1, `Present UnitType )}
| _1 = effect_label _2 = fieldspec
    {                                                               ( _1, _2 )}

effect_label:
| _1 = CONSTRUCTOR
    {                                                               ( _1 )}
| _1 = VARIABLE
    {                                                               ( _1 )}

fieldspec:
| _1 = COLON _2 = datatype
    {                                                               ( `Present _2 )}
| _1 = LBRACE _2 = COLON _3 = datatype _4 = RBRACE
    {                                                               ( `Present _3 )}
| _1 = MINUS
    {                                                               ( `Absent )}
| _1 = LBRACE _2 = MINUS _3 = RBRACE
    {                                                               ( `Absent )}
| _1 = LBRACE _2 = VARIABLE _3 = RBRACE
    {                                                               ( `RigidVar _2 )}
| _1 = LBRACE _2 = QUESTIONVAR _3 = RBRACE
    {                                                               ( `Var _2 )}
| _1 = LBRACE _2 = UNDERSCORE _3 = RBRACE
    {                                                               ( fresh_rigid_presence_variable () )}
| _1 = LBRACE _2 = QUESTION _3 = RBRACE
    {                                                               ( fresh_presence_variable () )}

nonrec_row_var:
| _1 = VARIABLE
    {                                                               ( `OpenRigid (_1, `Any) )}
| _1 = QUESTIONVAR
    {                                                               ( `Open (_1, `Any) )}
| _1 = UNDERSCORE
    {                                                               ( fresh_rigid_row_variable `Any )}
| _1 = QUESTION
    {                                                               ( fresh_row_variable `Any )}

row_var:
| _1 = nonrec_row_var
    {                                                               ( _1 )}
| _1 = LPAREN _2 = MU _3 = VARIABLE _4 = DOT _5 = vfields _6 = RPAREN
    {                                                               ( `Recursive (_3, _5) )}

kinded_nonrec_row_var:
| _1 = nonrec_row_var _2 = subkind
    {                                                               ( attach_row_subkind (pos()) (_1, _2) )}

kinded_row_var:
| _1 = row_var _2 = subkind
    {                                                               ( attach_row_subkind (pos()) (_1, _2) )}

regex:
| _1 = SLASH _2 = regex_pattern_alternate _3 = regex_flags_opt
    {                                                                                 ( (`Regex _2, pos()), _3 )}
| _1 = SLASH _2 = regex_flags_opt
    {                                                                                 ( (`Regex (`Simply ""), pos()), _2 )}
| _1 = SSLASH _2 = regex_pattern_alternate _3 = SLASH _4 = regex_replace _5 = regex_flags_opt
    {                                                                                 ( (`Regex (`Replace (_2, _4)), pos()), `RegexReplace :: _5 )}

regex_flags_opt:
| _1 = SLASH
    {                                                               ([])}
| _1 = SLASHFLAGS
    {                                                               (parseRegexFlags _1)}

regex_replace:
| 
    {                                                               ( `Literal "")}
| _1 = REGEXREPL
    {                                                               ( `Literal _1)}
| _1 = block
    {                                                               ( `Splice (`Block _1, pos ()) )}

regex_pattern:
| _1 = RANGE
    {                                                               ( `Range _1 )}
| _1 = STRING
    {                                                               ( `Simply _1 )}
| _1 = QUOTEDMETA
    {                                                               ( `Quote (`Simply _1) )}
| _1 = DOT
    {                                                               ( `Any )}
| _1 = CARET
    {                                                               ( `StartAnchor )}
| _1 = DOLLAR
    {                                                               ( `EndAnchor )}
| _1 = LPAREN _2 = regex_pattern_alternate _3 = RPAREN
    {                                                               ( `Group _2 )}
| _1 = regex_pattern _2 = STAR
    {                                                               ( `Repeat (Regex.Star, _1) )}
| _1 = regex_pattern _2 = PLUS
    {                                                               ( `Repeat (Regex.Plus, _1) )}
| _1 = regex_pattern _2 = QUESTION
    {                                                               ( `Repeat (Regex.Question, _1) )}
| _1 = block
    {                                                               ( `Splice (`Block _1, pos ()) )}

regex_pattern_alternate:
| _1 = regex_pattern_sequence
    {                                                               ( `Seq _1 )}
| _1 = regex_pattern_sequence _2 = ALTERNATE _3 = regex_pattern_alternate
    {                                                               ( `Alternate (`Seq _1, _3) )}

regex_pattern_sequence:
| _1 = regex_pattern
    {                                                               ( [_1] )}
| _1 = regex_pattern _2 = regex_pattern_sequence
    {                                                               ( _1 :: _2 )}

pattern:
| _1 = typed_pattern
    {                                                            ( _1 )}
| _1 = typed_pattern _2 = COLON _3 = primary_datatype
    {                                                            ( (`HasType (_1, datatype _3), pos()) )}

typed_pattern:
| _1 = cons_pattern
    {                                                            ( _1 )}
| _1 = cons_pattern _2 = AS _3 = var
    {                                                            ( `As ((fst _3, None, snd _3), _1), pos() )}

cons_pattern:
| _1 = constructor_pattern
    {                                                            ( _1 )}
| _1 = constructor_pattern _2 = COLONCOLON _3 = cons_pattern
    {                                                            ( `Cons (_1, _3), pos() )}

constructor_pattern:
| _1 = negative_pattern
    {                                                            ( _1 )}
| _1 = CONSTRUCTOR
    {                                                            ( `Variant (_1, None), pos() )}
| _1 = CONSTRUCTOR _2 = parenthesized_pattern
    {                                                            ( `Variant (_1, Some _2), pos() )}

constructors:
| _1 = CONSTRUCTOR
    {                                                            ( [_1] )}
| _1 = CONSTRUCTOR _2 = COMMA _3 = constructors
    {                                                            ( _1 :: _3 )}

negative_pattern:
| _1 = primary_pattern
    {                                                            ( _1 )}
| _1 = MINUS _2 = CONSTRUCTOR
    {                                                            ( `Negative [_2], pos() )}
| _1 = MINUS _2 = LPAREN _3 = constructors _4 = RPAREN
    {                                                            ( `Negative _3, pos() )}

parenthesized_pattern:
| _1 = LPAREN _2 = RPAREN
    {                                                            ( `Tuple [], pos() )}
| _1 = LPAREN _2 = pattern _3 = RPAREN
    {                                                            ( _2 )}
| _1 = LPAREN _2 = pattern _3 = COMMA _4 = patterns _5 = RPAREN
    {                                                            ( `Tuple (_2 :: _4), pos() )}
| _1 = LPAREN _2 = labeled_patterns _3 = VBAR _4 = pattern _5 = RPAREN
    {                                                            ( `Record (_2, Some _4), pos() )}
| _1 = LPAREN _2 = labeled_patterns _3 = RPAREN
    {                                                            ( `Record (_2, None), pos() )}

primary_pattern:
| _1 = VARIABLE
    {                                                            ( `Variable (_1, None, pos()), pos() )}
| _1 = UNDERSCORE
    {                                                            ( `Any, pos() )}
| _1 = constant
    {                                                            ( let c, p = _1 in `Constant c, p )}
| _1 = LBRACKET _2 = RBRACKET
    {                                                            ( `Nil, pos() )}
| _1 = LBRACKET _2 = patterns _3 = RBRACKET
    {                                                            ( `List _2, pos() )}
| _1 = parenthesized_pattern
    {                                                            ( _1 )}

patterns:
| _1 = pattern
    {                                                            ( [_1] )}
| _1 = pattern _2 = COMMA _3 = patterns
    {                                                            ( _1 :: _3 )}

labeled_patterns:
| _1 = record_label _2 = EQ _3 = pattern
    {                                                            ( [(_1, _3)] )}
| _1 = record_label _2 = EQ _3 = pattern _4 = COMMA _5 = labeled_patterns
    {                                                            ( (_1, _3) :: _5 )}

multi_args:
| _1 = LPAREN _2 = patterns _3 = RPAREN
    {                                                            ( _2 )}
| _1 = LPAREN _2 = RPAREN
    {                                                            ( [] )}

arg_lists:
| _1 = multi_args
    {                                                            ( [_1] )}
| _1 = multi_args _2 = arg_lists
    {                                                            ( _1 :: _2 )}

%%