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 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
|
(* camlp4r pa_extend.cmo q_MLast.cmo *)
(***********************************************************************)
(* *)
(* Camlp4 *)
(* *)
(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 2002 Institut National de Recherche en Informatique et *)
(* Automatique. Distributed only by permission. *)
(* *)
(***********************************************************************)
(* $Id: pa_o.ml,v 1.58.2.1 2004/08/18 11:17:37 mauny Exp $ *)
open Stdpp;
open Pcaml;
Pcaml.syntax_name.val := "OCaml";
Pcaml.no_constructors_arity.val := True;
(* ------------------------------------------------------------------------- *)
(* Hacked version of the lexer. *)
(* ------------------------------------------------------------------------- *)
open Token;
value jrh_lexer = ref False;
value no_quotations = ref False;
(* The string buffering machinery *)
value buff = ref (String.create 80);
value store len x =
do {
if len >= String.length buff.val then
buff.val := buff.val ^ String.create (String.length buff.val)
else ();
buff.val.[len] := x;
succ len
}
;
value mstore len s =
add_rec len 0 where rec add_rec len i =
if i == String.length s then len else add_rec (store len s.[i]) (succ i)
;
value get_buff len = String.sub buff.val 0 len;
(* The lexer *)
value stream_peek_nth n strm =
loop n (Stream.npeek n strm) where rec loop n =
fun
[ [] -> None
| [x] -> if n == 1 then Some x else None
| [_ :: l] -> loop (n - 1) l ]
;
value rec ident len =
parser
[ [: `('A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
'\248'..'\255' | '0'..'9' | '_' | ''' as
c)
;
s :] ->
ident (store len c) s
| [: :] -> len ]
and ident2 len =
parser
[ [: `('!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
'%' | '.' | ':' | '<' | '>' | '|' | '$' as
c)
;
s :] ->
ident2 (store len c) s
| [: :] -> len ]
and ident3 len =
parser
[ [: `('0'..'9' | 'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
'\248'..'\255' | '_' | '!' | '%' | '&' | '*' | '+' | '-' | '.' |
'/' | ':' | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' | ''' |
'$' as
c)
;
s :] ->
ident3 (store len c) s
| [: :] -> len ]
and base_number len =
parser
[ [: `'o' | 'O'; s :] -> digits octal (store len 'o') s
| [: `'x' | 'X'; s :] -> digits hexa (store len 'x') s
| [: `'b' | 'B'; s :] -> digits binary (store len 'b') s
| [: a = number len :] -> a ]
and digits kind len =
parser
[ [: d = kind; s :] -> digits_under kind (store len d) s
| [: :] -> raise (Stream.Error "ill-formed integer constant") ]
and digits_under kind len =
parser
[ [: d = kind; s :] -> digits_under kind (store len d) s
| [: `'_'; s :] -> digits_under kind len s
| [: `'l' :] -> ("INT32", get_buff len)
| [: `'L' :] -> ("INT64", get_buff len)
| [: `'n' :] -> ("NATIVEINT", get_buff len)
| [: :] -> ("INT", get_buff len) ]
and octal = parser [ [: `('0'..'7' as d) :] -> d ]
and hexa = parser [ [: `('0'..'9' | 'a'..'f' | 'A'..'F' as d) :] -> d ]
and binary = parser [ [: `('0'..'1' as d) :] -> d ]
and number len =
parser
[ [: `('0'..'9' as c); s :] -> number (store len c) s
| [: `'_'; s :] -> number len s
| [: `'.'; s :] -> decimal_part (store len '.') s
| [: `'e' | 'E'; s :] -> exponent_part (store len 'E') s
| [: `'l' :] -> ("INT32", get_buff len)
| [: `'L' :] -> ("INT64", get_buff len)
| [: `'n' :] -> ("NATIVEINT", get_buff len)
| [: :] -> ("INT", get_buff len) ]
and decimal_part len =
parser
[ [: `('0'..'9' as c); s :] -> decimal_part (store len c) s
| [: `'_'; s :] -> decimal_part len s
| [: `'e' | 'E'; s :] -> exponent_part (store len 'E') s
| [: :] -> ("FLOAT", get_buff len) ]
and exponent_part len =
parser
[ [: `('+' | '-' as c); s :] -> end_exponent_part (store len c) s
| [: a = end_exponent_part len :] -> a ]
and end_exponent_part len =
parser
[ [: `('0'..'9' as c); s :] -> end_exponent_part_under (store len c) s
| [: :] -> raise (Stream.Error "ill-formed floating-point constant") ]
and end_exponent_part_under len =
parser
[ [: `('0'..'9' as c); s :] -> end_exponent_part_under (store len c) s
| [: `'_'; s :] -> end_exponent_part_under len s
| [: :] -> ("FLOAT", get_buff len) ]
;
value error_on_unknown_keywords = ref False;
value err loc msg = raise_with_loc loc (Token.Error msg);
(* ------------------------------------------------------------------------- *)
(* JRH's hack to make the case distinction "unmixed" versus "mixed" *)
(* ------------------------------------------------------------------------- *)
value is_uppercase s = String.uppercase s = s;
value is_only_lowercase s = String.lowercase s = s && not(is_uppercase s);
value jrh_identifier find_kwd id =
let jflag = jrh_lexer.val in
if id = "set_jrh_lexer" then
(let _ = jrh_lexer.val := True in ("",find_kwd "true"))
else if id = "unset_jrh_lexer" then
(let _ = jrh_lexer.val := False in ("",find_kwd "false"))
else
try ("", find_kwd id) with
[ Not_found ->
if not(jflag) then
if is_uppercase (String.sub id 0 1) then ("UIDENT", id)
else ("LIDENT", id)
else if is_uppercase (String.sub id 0 1) &&
is_only_lowercase (String.sub id 1 (String.length id - 1))
(***** Carl's alternative version
then ("UIDENT", id) else if is_uppercase (String.sub id 0 1) then ("LIDENT", "__uc_"^id) else ("LIDENT", id)];
*****)
then ("UIDENT", id) else ("LIDENT", id)];
(* ------------------------------------------------------------------------- *)
(* Back to original file with the mod of using the above. *)
(* ------------------------------------------------------------------------- *)
(* Debugging positions and locations *)
value eprint_pos msg p =
Printf.eprintf "%s: fname=%s; lnum=%d; bol=%d; cnum=%d\n%!"
msg p.Lexing.pos_fname p.Lexing.pos_lnum p.Lexing.pos_bol p.Lexing.pos_cnum
;
value eprint_loc (bp, ep) =
do { eprint_pos "P1" bp; eprint_pos "P2" ep }
;
value check_location msg ((bp, ep) as loc) =
let ok =
if (bp.Lexing.pos_lnum > ep.Lexing.pos_lnum ||
bp.Lexing.pos_bol > ep.Lexing.pos_bol ||
bp.Lexing.pos_cnum > ep.Lexing.pos_cnum ||
bp.Lexing.pos_lnum < 0 || ep.Lexing.pos_lnum < 0 ||
bp.Lexing.pos_bol < 0 || ep.Lexing.pos_bol < 0 ||
bp.Lexing.pos_cnum < 0 || ep.Lexing.pos_cnum < 0)
(* Here, we don't check
bp.Lexing.pos_cnum < bp.Lexing.pos_bol || ep.Lexing.pos_cnum < bp.Lexing.pos_bol
since the lexer is called on antiquotations, with cnum=0, but lnum and bolpos
have "correct" values *)
then
do {
Printf.eprintf "*** Warning: (%s) strange positions ***\n" msg;
eprint_loc loc;
False
}
else
True in
(ok, loc)
;
value next_token_fun dfa ssd find_kwd fname lnum bolpos glexr =
let make_pos p =
{Lexing.pos_fname = fname.val; Lexing.pos_lnum = lnum.val;
Lexing.pos_bol = bolpos.val; Lexing.pos_cnum = p} in
let mkloc (bp, ep) = (make_pos bp, make_pos ep) in
let keyword_or_error (bp,ep) s =
let loc = mkloc (bp, ep) in
try (("", find_kwd s), loc) with
[ Not_found ->
if error_on_unknown_keywords.val then err loc ("illegal token: " ^ s)
else (("", s), loc) ] in
let error_if_keyword ( ((_,id) as a), bep) =
let loc = mkloc bep in
try do {
ignore(find_kwd id);
err loc ("illegal use of a keyword as a label: " ^ id) }
with [ Not_found -> (a, loc) ]
in
let rec next_token after_space =
parser bp
[ [: `'\010'; s :] ep ->
do { bolpos.val := ep; incr lnum; next_token True s }
| [: `'\013'; s :] ep ->
let ep =
match Stream.peek s with
[ Some '\010' -> do { Stream.junk s; ep+1 }
| _ -> ep ] in
do { bolpos.val := ep; incr lnum; next_token True s }
| [: `' ' | '\t' | '\026' | '\012'; s :] -> next_token True s
| [: `'#' when bp = bolpos.val; s :] ->
if linedir 1 s then do { line_directive s; next_token True s }
else keyword_or_error (bp, bp + 1) "#"
| [: `'('; s :] -> left_paren bp s
| [: `('A'..'Z' | '\192'..'\214' | '\216'..'\222' as c); s :] ->
let id = get_buff (ident (store 0 c) s) in
let loc = mkloc (bp, (Stream.count s)) in
(jrh_identifier find_kwd id, loc)
(********** original
(try ("", find_kwd id) with [ Not_found -> ("UIDENT", id) ], loc)
***********)
| [: `('a'..'z' | '\223'..'\246' | '\248'..'\255' | '_' as c); s :] ->
let id = get_buff (ident (store 0 c) s) in
let loc = mkloc (bp, (Stream.count s)) in
(jrh_identifier find_kwd id, loc)
(********** original
(try ("", find_kwd id) with [ Not_found -> ("LIDENT", id) ], loc)
**********)
| [: `('1'..'9' as c); s :] ->
let tok = number (store 0 c) s in
let loc = mkloc (bp, (Stream.count s)) in
(tok, loc)
| [: `'0'; s :] ->
let tok = base_number (store 0 '0') s in
let loc = mkloc (bp, (Stream.count s)) in
(tok, loc)
| [: `'''; s :] ->
match Stream.npeek 2 s with
[ [_; '''] | ['\\'; _] ->
let tok = ("CHAR", get_buff (char bp 0 s)) in
let loc = mkloc (bp, (Stream.count s)) in
(tok, loc)
| _ -> keyword_or_error (bp, Stream.count s) "'" ]
| [: `'"'; s :] ->
let tok = ("STRING", get_buff (string bp 0 s)) in
let loc = mkloc (bp, Stream.count s) in
(tok, loc)
| [: `'`'; s :] ->
let tok = ("QUOTATION", "tot:"^(qstring bp 0 s)) in
let loc = mkloc (bp, Stream.count s) in
(tok, loc)
| [: `'$'; s :] ->
let tok = dollar bp 0 s in
let loc = mkloc (bp, Stream.count s) in
(tok, loc)
| [: `('!' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' as c);
s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id
| [: `('~' as c);
a =
parser
[ [: `('a'..'z' as c); len = ident (store 0 c); s :] ep ->
let id = get_buff len in
match s with parser
[ [: `':' :] eb -> error_if_keyword (("LABEL", id), (bp, ep))
| [: :] -> error_if_keyword (("TILDEIDENT", id), (bp, ep)) ]
| [: s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id ] :] ->
a
| [: `('?' as c);
a =
parser
[ [: `('a'..'z' as c); len = ident (store 0 c); s :] ep ->
let id = get_buff len in
match s with parser
[ [: `':' :] eb -> error_if_keyword (("OPTLABEL", id), (bp,ep))
| [: :] -> error_if_keyword (("QUESTIONIDENT", id), (bp, ep)) ]
| [: s :] ->
let id = get_buff (ident2 (store 0 c) s) in
keyword_or_error (bp, Stream.count s) id ] :] ->
a
| [: `'<'; s :] -> less bp s
| [: `(':' as c1);
len =
parser
[ [: `(']' | ':' | '=' | '>' as c2) :] -> store (store 0 c1) c2
| [: :] -> store 0 c1 ] :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `('>' | '|' as c1);
len =
parser
[ [: `(']' | '}' as c2) :] -> store (store 0 c1) c2
| [: a = ident2 (store 0 c1) :] -> a ] :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `('[' | '{' as c1); s :] ->
let len =
match Stream.npeek 2 s with
[ ['<'; '<' | ':'] -> store 0 c1
| _ ->
match s with parser
[ [: `('|' | '<' | ':' as c2) :] -> store (store 0 c1) c2
| [: :] -> store 0 c1 ] ]
in
let ep = Stream.count s in
let id = get_buff len in
keyword_or_error (bp, ep) id
| [: `'.';
id =
parser
[ [: `'.' :] -> ".."
| [: :] -> if ssd && after_space then " ." else "." ] :] ep ->
keyword_or_error (bp, ep) id
| [: `';';
id =
parser
[ [: `';' :] -> ";;"
| [: :] -> ";" ] :] ep ->
keyword_or_error (bp, ep) id
| [: `'\\'; s :] ep -> (("LIDENT", get_buff (ident3 0 s)), mkloc (bp, ep))
| [: `c :] ep -> keyword_or_error (bp, ep) (String.make 1 c)
| [: _ = Stream.empty :] -> (("EOI", ""), mkloc (bp, succ bp)) ]
and less bp strm =
if no_quotations.val then
match strm with parser
[ [: len = ident2 (store 0 '<') :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id ]
else
match strm with parser
[ [: `'<'; len = quotation bp 0 :] ep ->
(("QUOTATION", ":" ^ get_buff len), mkloc (bp, ep))
| [: `':'; i = parser [: len = ident 0 :] -> get_buff len;
`'<' ? "character '<' expected"; len = quotation bp 0 :] ep ->
(("QUOTATION", i ^ ":" ^ get_buff len), mkloc (bp, ep))
| [: len = ident2 (store 0 '<') :] ep ->
let id = get_buff len in
keyword_or_error (bp, ep) id ]
and string bp len =
parser
[ [: `'"' :] -> len
| [: `'\\'; `c; s :] ep -> string bp (store (store len '\\') c) s
| [: `'\010'; s :] ep -> do { bolpos.val := ep; incr lnum; string bp (store len '\010') s }
| [: `'\013'; s :] ep ->
let (len, ep) =
match Stream.peek s with
[ Some '\010' -> do { Stream.junk s; (store (store len '\013') '\010', ep+1) }
| _ -> (store len '\013', ep) ] in
do { bolpos.val := ep; incr lnum; string bp len s }
| [: `c; s :] -> string bp (store len c) s
| [: :] ep -> err (mkloc (bp, ep)) "string not terminated" ]
and qstring bp len =
parser
[ [: `'`' :] -> get_buff len
| [: `c; s :] -> qstring bp (store len c) s
| [: :] ep -> err (mkloc (bp, ep)) "quotation not terminated" ]
and char bp len =
parser
[ [: `'''; s :] -> if len = 0 then char bp (store len ''') s else len
| [: `'\\'; `c; s :] -> char bp (store (store len '\\') c) s
| [: `'\010'; s :] -> do {bolpos.val := bp+1; incr lnum; char bp (store len '\010') s}
| [: `'\013'; s :] ->
let bol =
match Stream.peek s with
[ Some '\010' -> do { Stream.junk s; bp+2 }
| _ -> bp+1 ] in
do { bolpos.val := bol; incr lnum; char bp (store len '\013') s}
| [: `c; s :] -> char bp (store len c) s
| [: :] ep -> err (mkloc (bp, ep)) "char not terminated" ]
and dollar bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('a'..'z' | 'A'..'Z' as c); s :] -> antiquot bp (store len c) s
| [: `('0'..'9' as c); s :] -> maybe_locate bp (store len c) s
| [: `':'; s :] ->
let k = get_buff len in
("ANTIQUOT", k ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: s :] ->
if dfa then
match s with parser
[ [: `c :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (mkloc (bp, ep)) "antiquotation not terminated" ]
else ("", get_buff (ident2 (store 0 '$') s)) ]
and maybe_locate bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('0'..'9' as c); s :] -> maybe_locate bp (store len c) s
| [: `':'; s :] ->
("LOCATE", get_buff len ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (mkloc (bp, ep)) "antiquotation not terminated" ]
and antiquot bp len =
parser
[ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
| [: `('a'..'z' | 'A'..'Z' | '0'..'9' as c); s :] ->
antiquot bp (store len c) s
| [: `':'; s :] ->
let k = get_buff len in
("ANTIQUOT", k ^ ":" ^ locate_or_antiquot_rest bp 0 s)
| [: `'\\'; `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: `c; s :] ->
("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
| [: :] ep -> err (mkloc (bp, ep)) "antiquotation not terminated" ]
and locate_or_antiquot_rest bp len =
parser
[ [: `'$' :] -> get_buff len
| [: `'\\'; `c; s :] -> locate_or_antiquot_rest bp (store len c) s
| [: `c; s :] -> locate_or_antiquot_rest bp (store len c) s
| [: :] ep -> err (mkloc (bp, ep)) "antiquotation not terminated" ]
and quotation bp len =
parser
[ [: `'>'; s :] -> maybe_end_quotation bp len s
| [: `'<'; s :] ->
quotation bp (maybe_nested_quotation bp (store len '<') s) s
| [: `'\\';
len =
parser
[ [: `('>' | '<' | '\\' as c) :] -> store len c
| [: :] -> store len '\\' ];
s :] ->
quotation bp len s
| [: `'\010'; s :] -> do {bolpos.val := bp+1; incr lnum; quotation bp (store len '\010') s}
| [: `'\013'; s :] ->
let bol =
match Stream.peek s with
[ Some '\010' -> do { Stream.junk s; bp+2 }
| _ -> bp+1 ] in
do { bolpos.val := bol; incr lnum; quotation bp (store len '\013') s}
| [: `c; s :] -> quotation bp (store len c) s
| [: :] ep -> err (mkloc (bp, ep)) "quotation not terminated" ]
and maybe_nested_quotation bp len =
parser
[ [: `'<'; s :] -> mstore (quotation bp (store len '<') s) ">>"
| [: `':'; len = ident (store len ':');
a =
parser
[ [: `'<'; s :] -> mstore (quotation bp (store len '<') s) ">>"
| [: :] -> len ] :] ->
a
| [: :] -> len ]
and maybe_end_quotation bp len =
parser
[ [: `'>' :] -> len
| [: a = quotation bp (store len '>') :] -> a ]
and left_paren bp =
parser
[ [: `'*'; _ = comment bp; a = next_token True :] -> a
| [: :] ep -> keyword_or_error (bp, ep) "(" ]
and comment bp =
parser
[ [: `'('; s :] -> left_paren_in_comment bp s
| [: `'*'; s :] -> star_in_comment bp s
| [: `'"'; _ = string bp 0; s :] -> comment bp s
| [: `'''; s :] -> quote_in_comment bp s
| [: `'\010'; s :] ep -> do { bolpos.val := ep; incr lnum; comment bp s }
| [: `'\013'; s :] ep ->
let ep =
match Stream.peek s with
[ Some '\010' -> do { Stream.junk s; ep+1 }
| _ -> ep ] in
do { bolpos.val := ep; incr lnum; comment bp s }
| [: `c; s :] -> comment bp s
| [: :] ep -> err (mkloc (bp, ep)) "comment not terminated" ]
and quote_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: `'\\'; s :] -> quote_antislash_in_comment bp 0 s
| [: s :] ->
do {
match Stream.npeek 2 s with
[ [ ( '\013' | '\010' ); '''] ->
do { bolpos.val := bp + 1; incr lnum;
Stream.junk s; Stream.junk s }
| [ '\013'; '\010' ] ->
match Stream.npeek 3 s with
[ [_; _; '''] -> do { bolpos.val := bp + 2; incr lnum;
Stream.junk s; Stream.junk s; Stream.junk s }
| _ -> () ]
| [_; '''] -> do { Stream.junk s; Stream.junk s }
| _ -> () ];
comment bp s
} ]
and quote_any_in_comment bp =
parser
[ [: `'''; s :] -> comment bp s
| [: a = comment bp :] -> a ]
and quote_antislash_in_comment bp len =
parser
[ [: `'''; s :] -> comment bp s
| [: `'\\' | '"' | 'n' | 't' | 'b' | 'r'; s :] ->
quote_any_in_comment bp s
| [: `'0'..'9'; s :] -> quote_antislash_digit_in_comment bp s
| [: a = comment bp :] -> a ]
and quote_antislash_digit_in_comment bp =
parser
[ [: `'0'..'9'; s :] -> quote_antislash_digit2_in_comment bp s
| [: a = comment bp :] -> a ]
and quote_antislash_digit2_in_comment bp =
parser
[ [: `'0'..'9'; s :] -> quote_any_in_comment bp s
| [: a = comment bp :] -> a ]
and left_paren_in_comment bp =
parser
[ [: `'*'; s :] -> do { comment bp s; comment bp s }
| [: a = comment bp :] -> a ]
and star_in_comment bp =
parser
[ [: `')' :] -> ()
| [: a = comment bp :] -> a ]
and linedir n s =
match stream_peek_nth n s with
[ Some (' ' | '\t') -> linedir (n + 1) s
| Some ('0'..'9') -> True
| _ -> False ]
and any_to_nl =
parser
[ [: `'\010'; s :] ep ->
do { bolpos.val := ep; incr lnum }
| [: `'\013'; s :] ep ->
let ep =
match Stream.peek s with
[ Some '\010' -> do { Stream.junk s; ep+1 }
| _ -> ep ] in
do { bolpos.val := ep; incr lnum }
| [: `_; s :] -> any_to_nl s
| [: :] -> () ]
and line_directive = parser (* we are sure that there is a line directive here *)
[ [: _ = skip_spaces; n = line_directive_number 0;
_ = skip_spaces; _ = line_directive_string;
_ = any_to_nl :] ep
-> do { bolpos.val := ep; lnum.val := n }
]
and skip_spaces = parser
[ [: `' ' | '\t'; s :] -> skip_spaces s
| [: :] -> () ]
and line_directive_number n = parser
[ [: `('0'..'9' as c) ; s :]
-> line_directive_number (10*n + (Char.code c - Char.code '0')) s
| [: :] -> n ]
and line_directive_string = parser
[ [: ` '"' ; _ = line_directive_string_contents 0 :] -> ()
| [: :] -> ()
]
and line_directive_string_contents len = parser
[ [: ` '\010' | '\013' :] -> ()
| [: ` '"' :] -> fname.val := get_buff len
| [: `c; s :] -> line_directive_string_contents (store len c) s
]
in
fun cstrm ->
try
let glex = glexr.val in
let comm_bp = Stream.count cstrm in
let r = next_token False cstrm in
do {
match glex.tok_comm with
[ Some list ->
let next_bp = (fst (snd r)).Lexing.pos_cnum in
if next_bp > comm_bp then
let comm_loc = mkloc (comm_bp, next_bp) in
glex.tok_comm := Some [comm_loc :: list]
else ()
| None -> () ];
r
}
with
[ Stream.Error str ->
err (mkloc (Stream.count cstrm, Stream.count cstrm + 1)) str ]
;
value dollar_for_antiquotation = ref True;
value specific_space_dot = ref False;
value func kwd_table glexr =
let bolpos = ref 0 in
let lnum = ref 1 in
let fname = ref "" in
let find = Hashtbl.find kwd_table in
let dfa = dollar_for_antiquotation.val in
let ssd = specific_space_dot.val in
Token.lexer_func_of_parser (next_token_fun dfa ssd find fname lnum bolpos glexr)
;
value rec check_keyword_stream =
parser [: _ = check; _ = Stream.empty :] -> True
and check =
parser
[ [: `'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' | '\248'..'\255'
;
s :] ->
check_ident s
| [: `'!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
'%' | '.'
;
s :] ->
check_ident2 s
| [: `'<'; s :] ->
match Stream.npeek 1 s with
[ [':' | '<'] -> ()
| _ -> check_ident2 s ]
| [: `':';
_ =
parser
[ [: `']' | ':' | '=' | '>' :] -> ()
| [: :] -> () ] :] ep ->
()
| [: `'>' | '|';
_ =
parser
[ [: `']' | '}' :] -> ()
| [: a = check_ident2 :] -> a ] :] ->
()
| [: `'[' | '{'; s :] ->
match Stream.npeek 2 s with
[ ['<'; '<' | ':'] -> ()
| _ ->
match s with parser
[ [: `'|' | '<' | ':' :] -> ()
| [: :] -> () ] ]
| [: `';';
_ =
parser
[ [: `';' :] -> ()
| [: :] -> () ] :] ->
()
| [: `_ :] -> () ]
and check_ident =
parser
[ [: `'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
'\248'..'\255' | '0'..'9' | '_' | '''
;
s :] ->
check_ident s
| [: :] -> () ]
and check_ident2 =
parser
[ [: `'!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
'%' | '.' | ':' | '<' | '>' | '|'
;
s :] ->
check_ident2 s
| [: :] -> () ]
;
value check_keyword s =
try check_keyword_stream (Stream.of_string s) with _ -> False
;
value error_no_respect_rules p_con p_prm =
raise
(Token.Error
("the token " ^
(if p_con = "" then "\"" ^ p_prm ^ "\""
else if p_prm = "" then p_con
else p_con ^ " \"" ^ p_prm ^ "\"") ^
" does not respect Plexer rules"))
;
value error_ident_and_keyword p_con p_prm =
raise
(Token.Error
("the token \"" ^ p_prm ^ "\" is used as " ^ p_con ^
" and as keyword"))
;
value using_token kwd_table ident_table (p_con, p_prm) =
match p_con with
[ "" ->
if not (Hashtbl.mem kwd_table p_prm) then
if check_keyword p_prm then
if Hashtbl.mem ident_table p_prm then
error_ident_and_keyword (Hashtbl.find ident_table p_prm) p_prm
else Hashtbl.add kwd_table p_prm p_prm
else error_no_respect_rules p_con p_prm
else ()
| "LIDENT" ->
if p_prm = "" then ()
else
match p_prm.[0] with
[ 'A'..'Z' -> error_no_respect_rules p_con p_prm
| _ ->
if Hashtbl.mem kwd_table p_prm then
error_ident_and_keyword p_con p_prm
else Hashtbl.add ident_table p_prm p_con ]
| "UIDENT" ->
if p_prm = "" then ()
else
match p_prm.[0] with
[ 'a'..'z' -> error_no_respect_rules p_con p_prm
| _ ->
if Hashtbl.mem kwd_table p_prm then
error_ident_and_keyword p_con p_prm
else Hashtbl.add ident_table p_prm p_con ]
| "INT" | "INT32" | "INT64" | "NATIVEINT"
| "FLOAT" | "CHAR" | "STRING"
| "TILDEIDENT" | "QUESTIONIDENT" | "LABEL" | "OPTLABEL"
| "QUOTATION" | "ANTIQUOT" | "LOCATE" | "EOI" ->
()
| _ ->
raise
(Token.Error
("the constructor \"" ^ p_con ^
"\" is not recognized by Plexer")) ]
;
value removing_token kwd_table ident_table (p_con, p_prm) =
match p_con with
[ "" -> Hashtbl.remove kwd_table p_prm
| "LIDENT" | "UIDENT" ->
if p_prm <> "" then Hashtbl.remove ident_table p_prm else ()
| _ -> () ]
;
value text =
fun
[ ("", t) -> "'" ^ t ^ "'"
| ("LIDENT", "") -> "lowercase identifier"
| ("LIDENT", t) -> "'" ^ t ^ "'"
| ("UIDENT", "") -> "uppercase identifier"
| ("UIDENT", t) -> "'" ^ t ^ "'"
| ("INT", "") -> "integer"
| ("INT32", "") -> "32 bits integer"
| ("INT64", "") -> "64 bits integer"
| ("NATIVEINT", "") -> "native integer"
| (("INT" | "INT32" | "NATIVEINT"), s) -> "'" ^ s ^ "'"
| ("FLOAT", "") -> "float"
| ("STRING", "") -> "string"
| ("CHAR", "") -> "char"
| ("QUOTATION", "") -> "quotation"
| ("ANTIQUOT", k) -> "antiquot \"" ^ k ^ "\""
| ("LOCATE", "") -> "locate"
| ("EOI", "") -> "end of input"
| (con, "") -> con
| (con, prm) -> con ^ " \"" ^ prm ^ "\"" ]
;
value eq_before_colon p e =
loop 0 where rec loop i =
if i == String.length e then
failwith "Internal error in Plexer: incorrect ANTIQUOT"
else if i == String.length p then e.[i] == ':'
else if p.[i] == e.[i] then loop (i + 1)
else False
;
value after_colon e =
try
let i = String.index e ':' in
String.sub e (i + 1) (String.length e - i - 1)
with
[ Not_found -> "" ]
;
value tok_match =
fun
[ ("ANTIQUOT", p_prm) ->
fun
[ ("ANTIQUOT", prm) when eq_before_colon p_prm prm -> after_colon prm
| _ -> raise Stream.Failure ]
| tok -> Token.default_match tok ]
;
value gmake () =
let kwd_table = Hashtbl.create 301 in
let id_table = Hashtbl.create 301 in
let glexr =
ref
{tok_func = fun []; tok_using = fun []; tok_removing = fun [];
tok_match = fun []; tok_text = fun []; tok_comm = None}
in
let glex =
{tok_func = func kwd_table glexr;
tok_using = using_token kwd_table id_table;
tok_removing = removing_token kwd_table id_table; tok_match = tok_match;
tok_text = text; tok_comm = None}
in
do { glexr.val := glex; glex }
;
value tparse =
fun
[ ("ANTIQUOT", p_prm) ->
let p =
parser
[: `("ANTIQUOT", prm) when eq_before_colon p_prm prm :] ->
after_colon prm
in
Some p
| _ -> None ]
;
value make () =
let kwd_table = Hashtbl.create 301 in
let id_table = Hashtbl.create 301 in
let glexr =
ref
{tok_func = fun []; tok_using = fun []; tok_removing = fun [];
tok_match = fun []; tok_text = fun []; tok_comm = None}
in
{func = func kwd_table glexr; using = using_token kwd_table id_table;
removing = removing_token kwd_table id_table; tparse = tparse; text = text}
;
(* ------------------------------------------------------------------------- *)
(* Resume the main file. *)
(* ------------------------------------------------------------------------- *)
do {
let odfa = dollar_for_antiquotation.val in
dollar_for_antiquotation.val := False;
Grammar.Unsafe.gram_reinit gram (gmake ());
dollar_for_antiquotation.val := odfa;
Grammar.Unsafe.clear_entry interf;
Grammar.Unsafe.clear_entry implem;
Grammar.Unsafe.clear_entry top_phrase;
Grammar.Unsafe.clear_entry use_file;
Grammar.Unsafe.clear_entry module_type;
Grammar.Unsafe.clear_entry module_expr;
Grammar.Unsafe.clear_entry sig_item;
Grammar.Unsafe.clear_entry str_item;
Grammar.Unsafe.clear_entry expr;
Grammar.Unsafe.clear_entry patt;
Grammar.Unsafe.clear_entry ctyp;
Grammar.Unsafe.clear_entry let_binding;
Grammar.Unsafe.clear_entry type_declaration;
Grammar.Unsafe.clear_entry class_type;
Grammar.Unsafe.clear_entry class_expr;
Grammar.Unsafe.clear_entry class_sig_item;
Grammar.Unsafe.clear_entry class_str_item
};
Pcaml.parse_interf.val := Grammar.Entry.parse interf;
Pcaml.parse_implem.val := Grammar.Entry.parse implem;
value o2b =
fun
[ Some _ -> True
| None -> False ]
;
value mkumin loc f arg =
match (f, arg) with
[ ("-", <:expr< $int:n$ >>) when int_of_string n > 0 ->
let n = "-" ^ n in
<:expr< $int:n$ >>
| ("-", MLast.ExInt32 loc n) when (Int32.of_string n) > 0l ->
MLast.ExInt32 loc ("-" ^ n)
| ("-", MLast.ExInt64 loc n) when (Int64.of_string n) > 0L ->
MLast.ExInt64 loc ("-" ^ n)
| ("-", MLast.ExNativeInt loc n) when (Nativeint.of_string n) > 0n ->
MLast.ExNativeInt loc ("-" ^ n)
| (_, <:expr< $flo:n$ >>) when float_of_string n > 0.0 ->
let n = "-" ^ n in
<:expr< $flo:n$ >>
| _ ->
let f = "~" ^ f in
<:expr< $lid:f$ $arg$ >> ]
;
value mklistexp loc last =
loop True where rec loop top =
fun
[ [] ->
match last with
[ Some e -> e
| None -> <:expr< [] >> ]
| [e1 :: el] ->
let loc = if top then loc else (fst (MLast.loc_of_expr e1), snd loc) in
<:expr< [$e1$ :: $loop False el$] >> ]
;
value mklistpat loc last =
loop True where rec loop top =
fun
[ [] ->
match last with
[ Some p -> p
| None -> <:patt< [] >> ]
| [p1 :: pl] ->
let loc = if top then loc else (fst (MLast.loc_of_patt p1), snd loc) in
<:patt< [$p1$ :: $loop False pl$] >> ]
;
(*** JRH pulled this outside so user can add new infixes here too ***)
value ht = Hashtbl.create 73;
(*** And JRH added all the new HOL Light infixes here already ***)
value is_operator =
let ct = Hashtbl.create 73 in
do {
List.iter (fun x -> Hashtbl.add ht x True)
["asr"; "land"; "lor"; "lsl"; "lsr"; "lxor"; "mod"; "or"; "o"; "upto";
"F_F"; "THENC"; "THEN"; "THENL"; "ORELSE"; "ORELSEC";
"THEN_TCL"; "ORELSE_TCL"];
List.iter (fun x -> Hashtbl.add ct x True)
['!'; '&'; '*'; '+'; '-'; '/'; ':'; '<'; '='; '>'; '@'; '^'; '|'; '~';
'?'; '%'; '.'; '$'];
fun x ->
try Hashtbl.find ht x with
[ Not_found -> try Hashtbl.find ct x.[0] with _ -> False ]
}
;
(*** JRH added this so parenthesised operators undergo same mapping ***)
value translate_operator =
fun s ->
match s with
[ "THEN" -> "then_"
| "THENC" -> "thenc_"
| "THENL" -> "thenl_"
| "ORELSE" -> "orelse_"
| "ORELSEC" -> "orelsec_"
| "THEN_TCL" -> "then_tcl_"
| "ORELSE_TCL" -> "orelse_tcl_"
| "F_F" -> "f_f_"
| _ -> s];
(*** And JRH inserted it in here ***)
value operator_rparen =
Grammar.Entry.of_parser gram "operator_rparen"
(fun strm ->
match Stream.npeek 2 strm with
[ [("", s); ("", ")")] when is_operator s ->
do { Stream.junk strm; Stream.junk strm; translate_operator s }
| _ -> raise Stream.Failure ])
;
value lident_colon =
Grammar.Entry.of_parser gram "lident_colon"
(fun strm ->
match Stream.npeek 2 strm with
[ [("LIDENT", i); ("", ":")] ->
do { Stream.junk strm; Stream.junk strm; i }
| _ -> raise Stream.Failure ])
;
value symbolchar =
let list =
['!'; '$'; '%'; '&'; '*'; '+'; '-'; '.'; '/'; ':'; '<'; '='; '>'; '?';
'@'; '^'; '|'; '~']
in
let rec loop s i =
if i == String.length s then True
else if List.mem s.[i] list then loop s (i + 1)
else False
in
loop
;
value prefixop =
let list = ['!'; '?'; '~'] in
let excl = ["!="; "??"] in
Grammar.Entry.of_parser gram "prefixop"
(parser
[: `("", x)
when
not (List.mem x excl) && String.length x >= 2 &&
List.mem x.[0] list && symbolchar x 1 :] ->
x)
;
value infixop0 =
let list = ['='; '<'; '>'; '|'; '&'; '$'] in
let excl = ["<-"; "||"; "&&"] in
Grammar.Entry.of_parser gram "infixop0"
(parser
[: `("", x)
when
not (List.mem x excl) && String.length x >= 2 &&
List.mem x.[0] list && symbolchar x 1 :] ->
x)
;
value infixop1 =
let list = ['@'; '^'] in
Grammar.Entry.of_parser gram "infixop1"
(parser
[: `("", x)
when
String.length x >= 2 && List.mem x.[0] list &&
symbolchar x 1 :] ->
x)
;
value infixop2 =
let list = ['+'; '-'] in
Grammar.Entry.of_parser gram "infixop2"
(parser
[: `("", x)
when
x <> "->" && String.length x >= 2 && List.mem x.[0] list &&
symbolchar x 1 :] ->
x)
;
value infixop3 =
let list = ['*'; '/'; '%'] in
Grammar.Entry.of_parser gram "infixop3"
(parser
[: `("", x)
when
String.length x >= 2 && List.mem x.[0] list &&
symbolchar x 1 :] ->
x)
;
value infixop4 =
Grammar.Entry.of_parser gram "infixop4"
(parser
[: `("", x)
when
String.length x >= 3 && x.[0] == '*' && x.[1] == '*' &&
symbolchar x 2 :] ->
x)
;
value test_constr_decl =
Grammar.Entry.of_parser gram "test_constr_decl"
(fun strm ->
match Stream.npeek 1 strm with
[ [("UIDENT", _)] ->
match Stream.npeek 2 strm with
[ [_; ("", ".")] -> raise Stream.Failure
| [_; ("", "(")] -> raise Stream.Failure
| [_ :: _] -> ()
| _ -> raise Stream.Failure ]
| [("", "|")] -> ()
| _ -> raise Stream.Failure ])
;
value stream_peek_nth n strm =
loop n (Stream.npeek n strm) where rec loop n =
fun
[ [] -> None
| [x] -> if n == 1 then Some x else None
| [_ :: l] -> loop (n - 1) l ]
;
(* horrible hack to be able to parse class_types *)
value test_ctyp_minusgreater =
Grammar.Entry.of_parser gram "test_ctyp_minusgreater"
(fun strm ->
let rec skip_simple_ctyp n =
match stream_peek_nth n strm with
[ Some ("", "->") -> n
| Some ("", "[" | "[<") ->
skip_simple_ctyp (ignore_upto "]" (n + 1) + 1)
| Some ("", "(") -> skip_simple_ctyp (ignore_upto ")" (n + 1) + 1)
| Some
("",
"as" | "'" | ":" | "*" | "." | "#" | "<" | ">" | ".." | ";" |
"_") ->
skip_simple_ctyp (n + 1)
| Some ("QUESTIONIDENT" | "LIDENT" | "UIDENT", _) ->
skip_simple_ctyp (n + 1)
| Some _ | None -> raise Stream.Failure ]
and ignore_upto end_kwd n =
match stream_peek_nth n strm with
[ Some ("", prm) when prm = end_kwd -> n
| Some ("", "[" | "[<") ->
ignore_upto end_kwd (ignore_upto "]" (n + 1) + 1)
| Some ("", "(") -> ignore_upto end_kwd (ignore_upto ")" (n + 1) + 1)
| Some _ -> ignore_upto end_kwd (n + 1)
| None -> raise Stream.Failure ]
in
match Stream.peek strm with
[ Some (("", "[") | ("LIDENT" | "UIDENT", _)) -> skip_simple_ctyp 1
| Some ("", "object") -> raise Stream.Failure
| _ -> 1 ])
;
value test_label_eq =
Grammar.Entry.of_parser gram "test_label_eq"
(test 1 where rec test lev strm =
match stream_peek_nth lev strm with
[ Some (("UIDENT", _) | ("LIDENT", _) | ("", ".")) ->
test (lev + 1) strm
| Some ("", "=") -> ()
| _ -> raise Stream.Failure ])
;
value test_typevar_list_dot =
Grammar.Entry.of_parser gram "test_typevar_list_dot"
(let rec test lev strm =
match stream_peek_nth lev strm with
[ Some ("", "'") -> test2 (lev + 1) strm
| Some ("", ".") -> ()
| _ -> raise Stream.Failure ]
and test2 lev strm =
match stream_peek_nth lev strm with
[ Some ("UIDENT" | "LIDENT", _) -> test (lev + 1) strm
| _ -> raise Stream.Failure ]
in
test 1)
;
value constr_arity = ref [("Some", 1); ("Match_Failure", 1)];
value rec is_expr_constr_call =
fun
[ <:expr< $uid:_$ >> -> True
| <:expr< $uid:_$.$e$ >> -> is_expr_constr_call e
| <:expr< $e$ $_$ >> -> is_expr_constr_call e
| _ -> False ]
;
value rec constr_expr_arity loc =
fun
[ <:expr< $uid:c$ >> ->
try List.assoc c constr_arity.val with [ Not_found -> 0 ]
| <:expr< $uid:_$.$e$ >> -> constr_expr_arity loc e
| <:expr< $e$ $_$ >> ->
if is_expr_constr_call e then
Stdpp.raise_with_loc loc (Stream.Error "currified constructor")
else 1
| _ -> 1 ]
;
value rec is_patt_constr_call =
fun
[ <:patt< $uid:_$ >> -> True
| <:patt< $uid:_$.$p$ >> -> is_patt_constr_call p
| <:patt< $p$ $_$ >> -> is_patt_constr_call p
| _ -> False ]
;
value rec constr_patt_arity loc =
fun
[ <:patt< $uid:c$ >> ->
try List.assoc c constr_arity.val with [ Not_found -> 0 ]
| <:patt< $uid:_$.$p$ >> -> constr_patt_arity loc p
| <:patt< $p$ $_$ >> ->
if is_patt_constr_call p then
Stdpp.raise_with_loc loc (Stream.Error "currified constructor")
else 1
| _ -> 1 ]
;
value get_seq =
fun
[ <:expr< do { $list:el$ } >> -> el
| e -> [e] ]
;
value choose_tvar tpl =
let rec find_alpha v =
let s = String.make 1 v in
if List.mem_assoc s tpl then
if v = 'z' then None else find_alpha (Char.chr (Char.code v + 1))
else Some (String.make 1 v)
in
let rec make_n n =
let v = "a" ^ string_of_int n in
if List.mem_assoc v tpl then make_n (succ n) else v
in
match find_alpha 'a' with
[ Some x -> x
| None -> make_n 1 ]
;
value rec patt_lid =
fun
[ <:patt< $p1$ $p2$ >> ->
match p1 with
[ <:patt< $lid:i$ >> -> Some (MLast.loc_of_patt p1, i, [p2])
| _ ->
match patt_lid p1 with
[ Some (loc, i, pl) -> Some (loc, i, [p2 :: pl])
| None -> None ] ]
| _ -> None ]
;
value bigarray_get loc arr arg =
let coords =
match arg with
[ <:expr< ($list:el$) >> -> el
| _ -> [arg] ]
in
match coords with
[ [c1] -> <:expr< Bigarray.Array1.get $arr$ $c1$ >>
| [c1; c2] -> <:expr< Bigarray.Array2.get $arr$ $c1$ $c2$ >>
| [c1; c2; c3] -> <:expr< Bigarray.Array3.get $arr$ $c1$ $c2$ $c3$ >>
| coords -> <:expr< Bigarray.Genarray.get $arr$ [| $list:coords$ |] >> ]
;
value bigarray_set loc var newval =
match var with
[ <:expr< Bigarray.Array1.get $arr$ $c1$ >> ->
Some <:expr< Bigarray.Array1.set $arr$ $c1$ $newval$ >>
| <:expr< Bigarray.Array2.get $arr$ $c1$ $c2$ >> ->
Some <:expr< Bigarray.Array2.set $arr$ $c1$ $c2$ $newval$ >>
| <:expr< Bigarray.Array3.get $arr$ $c1$ $c2$ $c3$ >> ->
Some <:expr< Bigarray.Array3.set $arr$ $c1$ $c2$ $c3$ $newval$ >>
| <:expr< Bigarray.Genarray.get $arr$ [| $list:coords$ |] >> ->
Some <:expr< Bigarray.Genarray.set $arr$ [| $list:coords$ |] $newval$ >>
| _ -> None ]
;
(* ...works bad...
value rec sync cs =
match cs with parser
[ [: `';' :] -> sync_semi cs
| [: `_ :] -> sync cs ]
and sync_semi cs =
match cs with parser
[ [: `';' :] -> sync_semisemi cs
| [: :] -> sync cs ]
and sync_semisemi cs =
match Stream.peek cs with
[ Some ('\010' | '\013') -> ()
| _ -> sync_semi cs ]
;
Pcaml.sync.val := sync;
*)
EXTEND
GLOBAL: sig_item str_item ctyp patt expr module_type module_expr class_type
class_expr class_sig_item class_str_item let_binding type_declaration;
module_expr:
[ [ "functor"; "("; i = UIDENT; ":"; t = module_type; ")"; "->";
me = SELF ->
<:module_expr< functor ( $i$ : $t$ ) -> $me$ >>
| "struct"; st = LIST0 [ s = str_item; OPT ";;" -> s ]; "end" ->
<:module_expr< struct $list:st$ end >> ]
| [ me1 = SELF; me2 = SELF -> <:module_expr< $me1$ $me2$ >> ]
| [ i = mod_expr_ident -> i
| "("; me = SELF; ":"; mt = module_type; ")" ->
<:module_expr< ( $me$ : $mt$ ) >>
| "("; me = SELF; ")" -> <:module_expr< $me$ >> ] ]
;
mod_expr_ident:
[ LEFTA
[ i = SELF; "."; j = SELF -> <:module_expr< $i$ . $j$ >> ]
| [ i = UIDENT -> <:module_expr< $uid:i$ >> ] ]
;
str_item:
[ "top"
[ "exception"; (_, c, tl) = constructor_declaration; b = rebind_exn ->
<:str_item< exception $c$ of $list:tl$ = $b$ >>
| "external"; i = LIDENT; ":"; t = ctyp; "="; pd = LIST1 STRING ->
<:str_item< external $i$ : $t$ = $list:pd$ >>
| "external"; "("; i = operator_rparen; ":"; t = ctyp; "=";
pd = LIST1 STRING ->
<:str_item< external $i$ : $t$ = $list:pd$ >>
| "include"; me = module_expr -> <:str_item< include $me$ >>
| "module"; i = UIDENT; mb = module_binding ->
<:str_item< module $i$ = $mb$ >>
| "module"; "rec"; nmtmes = LIST1 module_rec_binding SEP "and" ->
MLast.StRecMod loc nmtmes
| "module"; "type"; i = UIDENT; "="; mt = module_type ->
<:str_item< module type $i$ = $mt$ >>
| "open"; i = mod_ident -> <:str_item< open $i$ >>
| "type"; tdl = LIST1 type_declaration SEP "and" ->
<:str_item< type $list:tdl$ >>
| "let"; r = OPT "rec"; l = LIST1 let_binding SEP "and"; "in";
x = expr ->
let e = <:expr< let $opt:o2b r$ $list:l$ in $x$ >> in
<:str_item< $exp:e$ >>
| "let"; r = OPT "rec"; l = LIST1 let_binding SEP "and" ->
match l with
[ [(<:patt< _ >>, e)] -> <:str_item< $exp:e$ >>
| _ -> <:str_item< value $opt:o2b r$ $list:l$ >> ]
| "let"; "module"; m = UIDENT; mb = module_binding; "in"; e = expr ->
<:str_item< let module $m$ = $mb$ in $e$ >>
| e = expr -> <:str_item< $exp:e$ >> ] ]
;
rebind_exn:
[ [ "="; sl = mod_ident -> sl
| -> [] ] ]
;
module_binding:
[ RIGHTA
[ "("; m = UIDENT; ":"; mt = module_type; ")"; mb = SELF ->
<:module_expr< functor ( $m$ : $mt$ ) -> $mb$ >>
| ":"; mt = module_type; "="; me = module_expr ->
<:module_expr< ( $me$ : $mt$ ) >>
| "="; me = module_expr -> <:module_expr< $me$ >> ] ]
;
module_rec_binding:
[ [ m = UIDENT; ":"; mt = module_type; "="; me = module_expr ->
(m, mt, me) ] ]
;
(* Module types *)
module_type:
[ [ "functor"; "("; i = UIDENT; ":"; t = SELF; ")"; "->"; mt = SELF ->
<:module_type< functor ( $i$ : $t$ ) -> $mt$ >> ]
| [ mt = SELF; "with"; wcl = LIST1 with_constr SEP "and" ->
<:module_type< $mt$ with $list:wcl$ >> ]
| [ "sig"; sg = LIST0 [ s = sig_item; OPT ";;" -> s ]; "end" ->
<:module_type< sig $list:sg$ end >>
| i = mod_type_ident -> i
| "("; mt = SELF; ")" -> <:module_type< $mt$ >> ] ]
;
mod_type_ident:
[ LEFTA
[ m1 = SELF; "."; m2 = SELF -> <:module_type< $m1$ . $m2$ >>
| m1 = SELF; "("; m2 = SELF; ")" -> <:module_type< $m1$ $m2$ >> ]
| [ m = UIDENT -> <:module_type< $uid:m$ >>
| m = LIDENT -> <:module_type< $lid:m$ >> ] ]
;
sig_item:
[ "top"
[ "exception"; (_, c, tl) = constructor_declaration ->
<:sig_item< exception $c$ of $list:tl$ >>
| "external"; i = LIDENT; ":"; t = ctyp; "="; pd = LIST1 STRING ->
<:sig_item< external $i$ : $t$ = $list:pd$ >>
| "external"; "("; i = operator_rparen; ":"; t = ctyp; "=";
pd = LIST1 STRING ->
<:sig_item< external $i$ : $t$ = $list:pd$ >>
| "include"; mt = module_type -> <:sig_item< include $mt$ >>
| "module"; i = UIDENT; mt = module_declaration ->
<:sig_item< module $i$ : $mt$ >>
| "module"; "rec"; mds = LIST1 module_rec_declaration SEP "and" ->
MLast.SgRecMod loc mds
| "module"; "type"; i = UIDENT; "="; mt = module_type ->
<:sig_item< module type $i$ = $mt$ >>
| "module"; "type"; i = UIDENT ->
<:sig_item< module type $i$ = 'abstract >>
| "open"; i = mod_ident -> <:sig_item< open $i$ >>
| "type"; tdl = LIST1 type_declaration SEP "and" ->
<:sig_item< type $list:tdl$ >>
| "val"; i = LIDENT; ":"; t = ctyp -> <:sig_item< value $i$ : $t$ >>
| "val"; "("; i = operator_rparen; ":"; t = ctyp ->
<:sig_item< value $i$ : $t$ >> ] ]
;
module_declaration:
[ RIGHTA
[ ":"; mt = module_type -> <:module_type< $mt$ >>
| "("; i = UIDENT; ":"; t = module_type; ")"; mt = SELF ->
<:module_type< functor ( $i$ : $t$ ) -> $mt$ >> ] ]
;
module_rec_declaration:
[ [ m = UIDENT; ":"; mt = module_type -> (m, mt)] ]
;
(* "with" constraints (additional type equations over signature
components) *)
with_constr:
[ [ "type"; tpl = type_parameters; i = mod_ident; "="; t = ctyp ->
MLast.WcTyp loc i tpl t
| "module"; i = mod_ident; "="; me = module_expr ->
MLast.WcMod loc i me ] ]
;
(* Core expressions *)
expr:
[ "top" RIGHTA
[ e1 = SELF; ";"; e2 = SELF ->
<:expr< do { $list:[e1 :: get_seq e2]$ } >>
| e1 = SELF; ";" -> e1 ]
| "expr1"
[ "let"; o = OPT "rec"; l = LIST1 let_binding SEP "and"; "in";
x = expr LEVEL "top" ->
<:expr< let $opt:o2b o$ $list:l$ in $x$ >>
| "let"; "module"; m = UIDENT; mb = module_binding; "in";
e = expr LEVEL "top" ->
<:expr< let module $m$ = $mb$ in $e$ >>
| "function"; OPT "|"; l = LIST1 match_case SEP "|" ->
<:expr< fun [ $list:l$ ] >>
| "fun"; p = simple_patt; e = fun_def ->
<:expr< fun [$p$ -> $e$] >>
| "match"; e = SELF; "with"; OPT "|"; l = LIST1 match_case SEP "|" ->
<:expr< match $e$ with [ $list:l$ ] >>
| "try"; e = SELF; "with"; OPT "|"; l = LIST1 match_case SEP "|" ->
<:expr< try $e$ with [ $list:l$ ] >>
| "if"; e1 = SELF; "then"; e2 = expr LEVEL "expr1";
"else"; e3 = expr LEVEL "expr1" ->
<:expr< if $e1$ then $e2$ else $e3$ >>
| "if"; e1 = SELF; "then"; e2 = expr LEVEL "expr1" ->
<:expr< if $e1$ then $e2$ else () >>
| "for"; i = LIDENT; "="; e1 = SELF; df = direction_flag; e2 = SELF;
"do"; e = SELF; "done" ->
<:expr< for $i$ = $e1$ $to:df$ $e2$ do { $list:get_seq e$ } >>
| "while"; e1 = SELF; "do"; e2 = SELF; "done" ->
<:expr< while $e1$ do { $list:get_seq e2$ } >>
| "object"; cspo = OPT class_self_patt; cf = class_structure; "end" ->
(* <:expr< object $opt:cspo$ $list:cf$ end >> *)
MLast.ExObj loc cspo cf ]
| [ e = SELF; ","; el = LIST1 NEXT SEP "," ->
<:expr< ( $list:[e :: el]$ ) >> ]
| ":=" NONA
[ e1 = SELF; ":="; e2 = expr LEVEL "expr1" ->
<:expr< $e1$.val := $e2$ >>
| e1 = SELF; "<-"; e2 = expr LEVEL "expr1" ->
match bigarray_set loc e1 e2 with
[ Some e -> e
| None -> <:expr< $e1$ := $e2$ >> ] ]
| "||" RIGHTA
[ e1 = SELF; "or"; e2 = SELF -> <:expr< $lid:"or"$ $e1$ $e2$ >>
| e1 = SELF; "||"; e2 = SELF -> <:expr< $e1$ || $e2$ >> ]
| "&&" RIGHTA
[ e1 = SELF; "&"; e2 = SELF -> <:expr< $lid:"&"$ $e1$ $e2$ >>
| e1 = SELF; "&&"; e2 = SELF -> <:expr< $e1$ && $e2$ >> ]
| "<" LEFTA
[ e1 = SELF; "<"; e2 = SELF -> <:expr< $e1$ < $e2$ >>
| e1 = SELF; ">"; e2 = SELF -> <:expr< $e1$ > $e2$ >>
| e1 = SELF; "<="; e2 = SELF -> <:expr< $e1$ <= $e2$ >>
| e1 = SELF; ">="; e2 = SELF -> <:expr< $e1$ >= $e2$ >>
| e1 = SELF; "="; e2 = SELF -> <:expr< $e1$ = $e2$ >>
| e1 = SELF; "<>"; e2 = SELF -> <:expr< $e1$ <> $e2$ >>
| e1 = SELF; "=="; e2 = SELF -> <:expr< $e1$ == $e2$ >>
| e1 = SELF; "!="; e2 = SELF -> <:expr< $e1$ != $e2$ >>
| e1 = SELF; "$"; e2 = SELF -> <:expr< $lid:"\$"$ $e1$ $e2$ >>
| e1 = SELF; op = infixop0; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
| "^" RIGHTA
[ e1 = SELF; "^"; e2 = SELF -> <:expr< $e1$ ^ $e2$ >>
| e1 = SELF; "@"; e2 = SELF -> <:expr< $e1$ @ $e2$ >>
| e1 = SELF; op = infixop1; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
| RIGHTA
[ e1 = SELF; "::"; e2 = SELF -> <:expr< [$e1$ :: $e2$] >> ]
| "+" LEFTA
[ e1 = SELF; "+"; e2 = SELF -> <:expr< $e1$ + $e2$ >>
| e1 = SELF; "-"; e2 = SELF -> <:expr< $e1$ - $e2$ >>
| e1 = SELF; op = infixop2; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
| "*" LEFTA
[ e1 = SELF; "*"; e2 = SELF -> <:expr< $e1$ * $e2$ >>
| e1 = SELF; "/"; e2 = SELF -> <:expr< $e1$ / $e2$ >>
| e1 = SELF; "%"; e2 = SELF -> <:expr< $lid:"%"$ $e1$ $e2$ >>
| e1 = SELF; "land"; e2 = SELF -> <:expr< $e1$ land $e2$ >>
| e1 = SELF; "lor"; e2 = SELF -> <:expr< $e1$ lor $e2$ >>
| e1 = SELF; "lxor"; e2 = SELF -> <:expr< $e1$ lxor $e2$ >>
| e1 = SELF; "mod"; e2 = SELF -> <:expr< $e1$ mod $e2$ >>
| e1 = SELF; op = infixop3; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
| "**" RIGHTA
[ e1 = SELF; "**"; e2 = SELF -> <:expr< $e1$ ** $e2$ >>
| e1 = SELF; "asr"; e2 = SELF -> <:expr< $e1$ asr $e2$ >>
| e1 = SELF; "lsl"; e2 = SELF -> <:expr< $e1$ lsl $e2$ >>
| e1 = SELF; "lsr"; e2 = SELF -> <:expr< $e1$ lsr $e2$ >>
| e1 = SELF; op = infixop4; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
| "unary minus" NONA
[ "-"; e = SELF -> <:expr< $mkumin loc "-" e$ >>
| "-."; e = SELF -> <:expr< $mkumin loc "-." e$ >> ]
| "apply" LEFTA
[ e1 = SELF; e2 = SELF ->
match constr_expr_arity loc e1 with
[ 1 -> <:expr< $e1$ $e2$ >>
| _ ->
match e2 with
[ <:expr< ( $list:el$ ) >> ->
List.fold_left (fun e1 e2 -> <:expr< $e1$ $e2$ >>) e1 el
| _ -> <:expr< $e1$ $e2$ >> ] ]
| "assert"; e = SELF ->
match e with
[ <:expr< False >> -> <:expr< assert False >>
| _ -> <:expr< assert ($e$) >> ]
| "lazy"; e = SELF ->
<:expr< lazy ($e$) >> ]
| "." LEFTA
[ e1 = SELF; "."; "("; e2 = SELF; ")" -> <:expr< $e1$ .( $e2$ ) >>
| e1 = SELF; "."; "["; e2 = SELF; "]" -> <:expr< $e1$ .[ $e2$ ] >>
| e1 = SELF; "."; "{"; e2 = SELF; "}" -> bigarray_get loc e1 e2
| e1 = SELF; "."; e2 = SELF -> <:expr< $e1$ . $e2$ >> ]
| "~-" NONA
[ "!"; e = SELF -> <:expr< $e$ . val>>
| "~-"; e = SELF -> <:expr< ~- $e$ >>
| "~-."; e = SELF -> <:expr< ~-. $e$ >>
| f = prefixop; e = SELF -> <:expr< $lid:f$ $e$ >> ]
| "simple" LEFTA
[ s = INT -> <:expr< $int:s$ >>
| s = INT32 -> MLast.ExInt32 loc s
| s = INT64 -> MLast.ExInt64 loc s
| s = NATIVEINT -> MLast.ExNativeInt loc s
| s = FLOAT -> <:expr< $flo:s$ >>
| s = STRING -> <:expr< $str:s$ >>
| c = CHAR -> <:expr< $chr:c$ >>
| UIDENT "True" -> <:expr< $uid:" True"$ >>
| UIDENT "False" -> <:expr< $uid:" False"$ >>
| i = expr_ident -> i
| s = "false" -> <:expr< False >>
| s = "true" -> <:expr< True >>
| "["; "]" -> <:expr< [] >>
| "["; el = expr1_semi_list; "]" -> <:expr< $mklistexp loc None el$ >>
| "[|"; "|]" -> <:expr< [| |] >>
| "[|"; el = expr1_semi_list; "|]" -> <:expr< [| $list:el$ |] >>
| "{"; test_label_eq; lel = lbl_expr_list; "}" ->
<:expr< { $list:lel$ } >>
| "{"; e = expr LEVEL "."; "with"; lel = lbl_expr_list; "}" ->
<:expr< { ($e$) with $list:lel$ } >>
| "("; ")" -> <:expr< () >>
| "("; op = operator_rparen -> <:expr< $lid:op$ >>
| "("; e = SELF; ":"; t = ctyp; ")" -> <:expr< ($e$ : $t$) >>
| "("; e = SELF; ")" -> <:expr< $e$ >>
| "begin"; e = SELF; "end" -> <:expr< $e$ >>
| "begin"; "end" -> <:expr< () >>
| x = LOCATE ->
let x =
try
let i = String.index x ':' in
({Lexing.pos_fname = "";
Lexing.pos_lnum = 0;
Lexing.pos_bol = 0;
Lexing.pos_cnum = int_of_string (String.sub x 0 i)},
String.sub x (i + 1) (String.length x - i - 1))
with
[ Not_found | Failure _ -> (Token.nowhere, x) ]
in
Pcaml.handle_expr_locate loc x
| x = QUOTATION ->
let x =
try
let i = String.index x ':' in
(String.sub x 0 i,
String.sub x (i + 1) (String.length x - i - 1))
with
[ Not_found -> ("", x) ]
in
Pcaml.handle_expr_quotation loc x ] ]
;
let_binding:
[ [ p = patt; e = fun_binding ->
match patt_lid p with
[ Some (loc, i, pl) ->
let e =
List.fold_left (fun e p -> <:expr< fun $p$ -> $e$ >>) e pl
in
(<:patt< $lid:i$ >>, e)
| None -> (p, e) ] ] ]
;
fun_binding:
[ RIGHTA
[ p = simple_patt; e = SELF -> <:expr< fun $p$ -> $e$ >>
| "="; e = expr -> <:expr< $e$ >>
| ":"; t = ctyp; "="; e = expr -> <:expr< ($e$ : $t$) >> ] ]
;
match_case:
[ [ x1 = patt; w = OPT [ "when"; e = expr -> e ]; "->"; x2 = expr ->
(x1, w, x2) ] ]
;
lbl_expr_list:
[ [ le = lbl_expr; ";"; lel = SELF -> [le :: lel]
| le = lbl_expr; ";" -> [le]
| le = lbl_expr -> [le] ] ]
;
lbl_expr:
[ [ i = patt_label_ident; "="; e = expr LEVEL "expr1" -> (i, e) ] ]
;
expr1_semi_list:
[ [ e = expr LEVEL "expr1"; ";"; el = SELF -> [e :: el]
| e = expr LEVEL "expr1"; ";" -> [e]
| e = expr LEVEL "expr1" -> [e] ] ]
;
fun_def:
[ RIGHTA
[ p = simple_patt; e = SELF -> <:expr< fun $p$ -> $e$ >>
| "->"; e = expr -> <:expr< $e$ >> ] ]
;
expr_ident:
[ RIGHTA
[ i = LIDENT -> <:expr< $lid:i$ >>
| i = UIDENT -> <:expr< $uid:i$ >>
| i = UIDENT; "."; j = SELF ->
let rec loop m =
fun
[ <:expr< $x$ . $y$ >> -> loop <:expr< $m$ . $x$ >> y
| e -> <:expr< $m$ . $e$ >> ]
in
loop <:expr< $uid:i$ >> j
| i = UIDENT; "."; "("; j = operator_rparen ->
<:expr< $uid:i$ . $lid:j$ >> ] ]
;
(* Patterns *)
patt:
[ LEFTA
[ p1 = SELF; "as"; i = LIDENT -> <:patt< ($p1$ as $lid:i$) >> ]
| LEFTA
[ p1 = SELF; "|"; p2 = SELF -> <:patt< $p1$ | $p2$ >> ]
| [ p = SELF; ","; pl = LIST1 NEXT SEP "," ->
<:patt< ( $list:[p :: pl]$) >> ]
| NONA
[ p1 = SELF; ".."; p2 = SELF -> <:patt< $p1$ .. $p2$ >> ]
| RIGHTA
[ p1 = SELF; "::"; p2 = SELF -> <:patt< [$p1$ :: $p2$] >> ]
| LEFTA
[ p1 = SELF; p2 = SELF ->
match constr_patt_arity loc p1 with
[ 1 -> <:patt< $p1$ $p2$ >>
| n ->
let p2 =
match p2 with
[ <:patt< _ >> when n > 1 ->
let pl =
loop n where rec loop n =
if n = 0 then [] else [<:patt< _ >> :: loop (n - 1)]
in
<:patt< ( $list:pl$ ) >>
| _ -> p2 ]
in
match p2 with
[ <:patt< ( $list:pl$ ) >> ->
List.fold_left (fun p1 p2 -> <:patt< $p1$ $p2$ >>) p1 pl
| _ -> <:patt< $p1$ $p2$ >> ] ] ]
| LEFTA
[ p1 = SELF; "."; p2 = SELF -> <:patt< $p1$ . $p2$ >> ]
| "simple"
[ p = simple_patt -> p ] ]
;
simple_patt:
[ [ s = LIDENT -> <:patt< $lid:s$ >>
| s = UIDENT -> <:patt< $uid:s$ >>
| s = INT -> <:patt< $int:s$ >>
| s = INT32 -> MLast.PaInt32 loc s
| s = INT64 -> MLast.PaInt64 loc s
| s = NATIVEINT -> MLast.PaNativeInt loc s
| "-"; s = INT -> <:patt< $int:"-" ^ s$ >>
| "-"; s = INT32 -> MLast.PaInt32 loc ("-" ^ s)
| "-"; s = INT64 -> MLast.PaInt64 loc ("-" ^ s)
| "-"; s = NATIVEINT -> MLast.PaNativeInt loc ("-" ^ s)
| "-"; s = FLOAT -> <:patt< $flo:"-" ^ s$ >>
| s = FLOAT -> <:patt< $flo:s$ >>
| s = STRING -> <:patt< $str:s$ >>
| s = CHAR -> <:patt< $chr:s$ >>
| UIDENT "True" -> <:patt< $uid:" True"$ >>
| UIDENT "False" -> <:patt< $uid:" False"$ >>
| s = "false" -> <:patt< False >>
| s = "true" -> <:patt< True >>
| "["; "]" -> <:patt< [] >>
| "["; pl = patt_semi_list; "]" -> <:patt< $mklistpat loc None pl$ >>
| "[|"; "|]" -> <:patt< [| |] >>
| "[|"; pl = patt_semi_list; "|]" -> <:patt< [| $list:pl$ |] >>
| "{"; lpl = lbl_patt_list; "}" -> <:patt< { $list:lpl$ } >>
| "("; ")" -> <:patt< () >>
| "("; op = operator_rparen -> <:patt< $lid:op$ >>
| "("; p = patt; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >>
| "("; p = patt; ")" -> <:patt< $p$ >>
| "_" -> <:patt< _ >>
| "`"; s = ident -> <:patt< ` $s$ >>
| "#"; t = mod_ident -> <:patt< # $list:t$ >>
| x = LOCATE ->
let x =
try
let i = String.index x ':' in
({Lexing.pos_fname = "";
Lexing.pos_lnum = 0;
Lexing.pos_bol = 0;
Lexing.pos_cnum = int_of_string (String.sub x 0 i)},
String.sub x (i + 1) (String.length x - i - 1))
with
[ Not_found | Failure _ -> (Token.nowhere, x) ]
in
Pcaml.handle_patt_locate loc x
| x = QUOTATION ->
let x =
try
let i = String.index x ':' in
(String.sub x 0 i,
String.sub x (i + 1) (String.length x - i - 1))
with
[ Not_found -> ("", x) ]
in
Pcaml.handle_patt_quotation loc x ] ]
;
patt_semi_list:
[ [ p = patt; ";"; pl = SELF -> [p :: pl]
| p = patt; ";" -> [p]
| p = patt -> [p] ] ]
;
lbl_patt_list:
[ [ le = lbl_patt; ";"; lel = SELF -> [le :: lel]
| le = lbl_patt; ";" -> [le]
| le = lbl_patt -> [le] ] ]
;
lbl_patt:
[ [ i = patt_label_ident; "="; p = patt -> (i, p) ] ]
;
patt_label_ident:
[ LEFTA
[ p1 = SELF; "."; p2 = SELF -> <:patt< $p1$ . $p2$ >> ]
| RIGHTA
[ i = UIDENT -> <:patt< $uid:i$ >>
| i = LIDENT -> <:patt< $lid:i$ >> ] ]
;
(* Type declaration *)
type_declaration:
[ [ tpl = type_parameters; n = type_patt; "="; tk = type_kind;
cl = LIST0 constrain ->
(n, tpl, tk, cl)
| tpl = type_parameters; n = type_patt; cl = LIST0 constrain ->
(n, tpl, <:ctyp< '$choose_tvar tpl$ >>, cl) ] ]
;
type_patt:
[ [ n = LIDENT -> (loc, n) ] ]
;
constrain:
[ [ "constraint"; t1 = ctyp; "="; t2 = ctyp -> (t1, t2) ] ]
;
type_kind:
[ [ "private"; "{"; ldl = label_declarations; "}" ->
<:ctyp< private { $list:ldl$ } >>
| "private"; OPT "|";
cdl = LIST1 constructor_declaration SEP "|" -> <:ctyp< private [ $list:cdl$ ] >>
| test_constr_decl; OPT "|";
cdl = LIST1 constructor_declaration SEP "|" -> <:ctyp< [ $list:cdl$ ] >>
| t = ctyp -> <:ctyp< $t$ >>
| t = ctyp; "="; "private"; "{"; ldl = label_declarations; "}" ->
<:ctyp< $t$ == private { $list:ldl$ } >>
| t = ctyp; "="; "{"; ldl = label_declarations; "}" ->
<:ctyp< $t$ == { $list:ldl$ } >>
| t = ctyp; "="; "private"; OPT "|"; cdl = LIST1 constructor_declaration SEP "|" ->
<:ctyp< $t$ == private [ $list:cdl$ ] >>
| t = ctyp; "="; OPT "|"; cdl = LIST1 constructor_declaration SEP "|" ->
<:ctyp< $t$ == [ $list:cdl$ ] >>
| "{"; ldl = label_declarations; "}" ->
<:ctyp< { $list:ldl$ } >> ] ]
;
type_parameters:
[ [ -> (* empty *) []
| tp = type_parameter -> [tp]
| "("; tpl = LIST1 type_parameter SEP ","; ")" -> tpl ] ]
;
type_parameter:
[ [ "'"; i = ident -> (i, (False, False))
| "+"; "'"; i = ident -> (i, (True, False))
| "-"; "'"; i = ident -> (i, (False, True)) ] ]
;
constructor_declaration:
[ [ ci = UIDENT; "of"; cal = LIST1 ctyp LEVEL "ctyp1" SEP "*" ->
(loc, ci, cal)
| ci = UIDENT -> (loc, ci, []) ] ]
;
label_declarations:
[ [ ld = label_declaration; ";"; ldl = SELF -> [ld :: ldl]
| ld = label_declaration; ";" -> [ld]
| ld = label_declaration -> [ld] ] ]
;
label_declaration:
[ [ i = LIDENT; ":"; t = poly_type -> (loc, i, False, t)
| "mutable"; i = LIDENT; ":"; t = poly_type -> (loc, i, True, t) ] ]
;
(* Core types *)
ctyp:
[ [ t1 = SELF; "as"; "'"; i = ident -> <:ctyp< $t1$ as '$i$ >> ]
| "arrow" RIGHTA
[ t1 = SELF; "->"; t2 = SELF -> <:ctyp< $t1$ -> $t2$ >> ]
| "star"
[ t = SELF; "*"; tl = LIST1 (ctyp LEVEL "ctyp1") SEP "*" ->
<:ctyp< ( $list:[t :: tl]$ ) >> ]
| "ctyp1"
[ t1 = SELF; t2 = SELF -> <:ctyp< $t2$ $t1$ >> ]
| "ctyp2"
[ t1 = SELF; "."; t2 = SELF -> <:ctyp< $t1$ . $t2$ >>
| t1 = SELF; "("; t2 = SELF; ")" -> <:ctyp< $t1$ $t2$ >> ]
| "simple"
[ "'"; i = ident -> <:ctyp< '$i$ >>
| "_" -> <:ctyp< _ >>
| i = LIDENT -> <:ctyp< $lid:i$ >>
| i = UIDENT -> <:ctyp< $uid:i$ >>
| "("; t = SELF; ","; tl = LIST1 ctyp SEP ","; ")";
i = ctyp LEVEL "ctyp2" ->
List.fold_left (fun c a -> <:ctyp< $c$ $a$ >>) i [t :: tl]
| "("; t = SELF; ")" -> <:ctyp< $t$ >> ] ]
;
(* Identifiers *)
ident:
[ [ i = LIDENT -> i
| i = UIDENT -> i ] ]
;
mod_ident:
[ RIGHTA
[ i = UIDENT -> [i]
| i = LIDENT -> [i]
| i = UIDENT; "."; j = SELF -> [i :: j] ] ]
;
(* Miscellaneous *)
direction_flag:
[ [ "to" -> True
| "downto" -> False ] ]
;
(* Objects and Classes *)
str_item:
[ [ "class"; cd = LIST1 class_declaration SEP "and" ->
<:str_item< class $list:cd$ >>
| "class"; "type"; ctd = LIST1 class_type_declaration SEP "and" ->
<:str_item< class type $list:ctd$ >> ] ]
;
sig_item:
[ [ "class"; cd = LIST1 class_description SEP "and" ->
<:sig_item< class $list:cd$ >>
| "class"; "type"; ctd = LIST1 class_type_declaration SEP "and" ->
<:sig_item< class type $list:ctd$ >> ] ]
;
(* Class expressions *)
class_declaration:
[ [ vf = OPT "virtual"; ctp = class_type_parameters; i = LIDENT;
cfb = class_fun_binding ->
{MLast.ciLoc = loc; MLast.ciVir = o2b vf; MLast.ciPrm = ctp;
MLast.ciNam = i; MLast.ciExp = cfb} ] ]
;
class_fun_binding:
[ [ "="; ce = class_expr -> ce
| ":"; ct = class_type; "="; ce = class_expr ->
<:class_expr< ($ce$ : $ct$) >>
| p = simple_patt; cfb = SELF ->
<:class_expr< fun $p$ -> $cfb$ >> ] ]
;
class_type_parameters:
[ [ -> (loc, [])
| "["; tpl = LIST1 type_parameter SEP ","; "]" -> (loc, tpl) ] ]
;
class_fun_def:
[ [ p = simple_patt; "->"; ce = class_expr ->
<:class_expr< fun $p$ -> $ce$ >>
| p = labeled_patt; "->"; ce = class_expr ->
<:class_expr< fun $p$ -> $ce$ >>
| p = simple_patt; cfd = SELF ->
<:class_expr< fun $p$ -> $cfd$ >>
| p = labeled_patt; cfd = SELF ->
<:class_expr< fun $p$ -> $cfd$ >> ] ]
;
class_expr:
[ "top"
[ "fun"; cfd = class_fun_def -> cfd
| "let"; rf = OPT "rec"; lb = LIST1 let_binding SEP "and"; "in";
ce = SELF ->
<:class_expr< let $opt:o2b rf$ $list:lb$ in $ce$ >> ]
| "apply" LEFTA
[ ce = SELF; e = expr LEVEL "label" ->
<:class_expr< $ce$ $e$ >> ]
| "simple"
[ "["; ct = ctyp; ","; ctcl = LIST1 ctyp SEP ","; "]";
ci = class_longident ->
<:class_expr< $list:ci$ [ $list:[ct :: ctcl]$ ] >>
| "["; ct = ctyp; "]"; ci = class_longident ->
<:class_expr< $list:ci$ [ $ct$ ] >>
| ci = class_longident -> <:class_expr< $list:ci$ >>
| "object"; cspo = OPT class_self_patt; cf = class_structure; "end" ->
<:class_expr< object $opt:cspo$ $list:cf$ end >>
| "("; ce = SELF; ":"; ct = class_type; ")" ->
<:class_expr< ($ce$ : $ct$) >>
| "("; ce = SELF; ")" -> ce ] ]
;
class_structure:
[ [ cf = LIST0 class_str_item -> cf ] ]
;
class_self_patt:
[ [ "("; p = patt; ")" -> p
| "("; p = patt; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >> ] ]
;
class_str_item:
[ [ "inherit"; ce = class_expr; pb = OPT [ "as"; i = LIDENT -> i ] ->
<:class_str_item< inherit $ce$ $opt:pb$ >>
| "val"; mf = OPT "mutable"; lab = label; e = cvalue_binding ->
<:class_str_item< value $opt:o2b mf$ $lab$ = $e$ >>
| "method"; "private"; "virtual"; l = label; ":"; t = poly_type ->
<:class_str_item< method virtual private $l$ : $t$ >>
| "method"; "virtual"; "private"; l = label; ":"; t = poly_type ->
<:class_str_item< method virtual private $l$ : $t$ >>
| "method"; "virtual"; l = label; ":"; t = poly_type ->
<:class_str_item< method virtual $l$ : $t$ >>
| "method"; "private"; l = label; ":"; t = poly_type; "="; e = expr ->
MLast.CrMth loc l True e (Some t)
| "method"; "private"; l = label; sb = fun_binding ->
MLast.CrMth loc l True sb None
| "method"; l = label; ":"; t = poly_type; "="; e = expr ->
MLast.CrMth loc l False e (Some t)
| "method"; l = label; sb = fun_binding ->
MLast.CrMth loc l False sb None
| "constraint"; t1 = ctyp; "="; t2 = ctyp ->
<:class_str_item< type $t1$ = $t2$ >>
| "initializer"; se = expr -> <:class_str_item< initializer $se$ >> ] ]
;
cvalue_binding:
[ [ "="; e = expr -> e
| ":"; t = ctyp; "="; e = expr -> <:expr< ($e$ : $t$) >>
| ":"; t = ctyp; ":>"; t2 = ctyp; "="; e = expr ->
<:expr< ($e$ : $t$ :> $t2$) >>
| ":>"; t = ctyp; "="; e = expr ->
<:expr< ($e$ :> $t$) >> ] ]
;
label:
[ [ i = LIDENT -> i ] ]
;
(* Class types *)
class_type:
[ [ test_ctyp_minusgreater; t = ctyp LEVEL "star"; "->"; ct = SELF ->
<:class_type< [ $t$ ] -> $ct$ >>
| cs = class_signature -> cs ] ]
;
class_signature:
[ [ "["; tl = LIST1 ctyp SEP ","; "]"; id = clty_longident ->
<:class_type< $list:id$ [ $list:tl$ ] >>
| id = clty_longident -> <:class_type< $list:id$ >>
| "object"; cst = OPT class_self_type; csf = LIST0 class_sig_item;
"end" ->
<:class_type< object $opt:cst$ $list:csf$ end >> ] ]
;
class_self_type:
[ [ "("; t = ctyp; ")" -> t ] ]
;
class_sig_item:
[ [ "inherit"; cs = class_signature -> <:class_sig_item< inherit $cs$ >>
| "val"; mf = OPT "mutable"; l = label; ":"; t = ctyp ->
<:class_sig_item< value $opt:o2b mf$ $l$ : $t$ >>
| "method"; "private"; "virtual"; l = label; ":"; t = poly_type ->
<:class_sig_item< method virtual private $l$ : $t$ >>
| "method"; "virtual"; "private"; l = label; ":"; t = poly_type ->
<:class_sig_item< method virtual private $l$ : $t$ >>
| "method"; "virtual"; l = label; ":"; t = poly_type ->
<:class_sig_item< method virtual $l$ : $t$ >>
| "method"; "private"; l = label; ":"; t = poly_type ->
<:class_sig_item< method private $l$ : $t$ >>
| "method"; l = label; ":"; t = poly_type ->
<:class_sig_item< method $l$ : $t$ >>
| "constraint"; t1 = ctyp; "="; t2 = ctyp ->
<:class_sig_item< type $t1$ = $t2$ >> ] ]
;
class_description:
[ [ vf = OPT "virtual"; ctp = class_type_parameters; n = LIDENT; ":";
ct = class_type ->
{MLast.ciLoc = loc; MLast.ciVir = o2b vf; MLast.ciPrm = ctp;
MLast.ciNam = n; MLast.ciExp = ct} ] ]
;
class_type_declaration:
[ [ vf = OPT "virtual"; ctp = class_type_parameters; n = LIDENT; "=";
cs = class_signature ->
{MLast.ciLoc = loc; MLast.ciVir = o2b vf; MLast.ciPrm = ctp;
MLast.ciNam = n; MLast.ciExp = cs} ] ]
;
(* Expressions *)
expr: LEVEL "simple"
[ LEFTA
[ "new"; i = class_longident -> <:expr< new $list:i$ >> ] ]
;
expr: LEVEL "."
[ [ e = SELF; "#"; lab = label -> <:expr< $e$ # $lab$ >> ] ]
;
expr: LEVEL "simple"
[ [ "("; e = SELF; ":"; t = ctyp; ":>"; t2 = ctyp; ")" ->
<:expr< ($e$ : $t$ :> $t2$) >>
| "("; e = SELF; ":>"; t = ctyp; ")" -> <:expr< ($e$ :> $t$) >>
| "{<"; ">}" -> <:expr< {< >} >>
| "{<"; fel = field_expr_list; ">}" -> <:expr< {< $list:fel$ >} >> ] ]
;
field_expr_list:
[ [ l = label; "="; e = expr LEVEL "expr1"; ";"; fel = SELF ->
[(l, e) :: fel]
| l = label; "="; e = expr LEVEL "expr1"; ";" -> [(l, e)]
| l = label; "="; e = expr LEVEL "expr1" -> [(l, e)] ] ]
;
(* Core types *)
ctyp: LEVEL "simple"
[ [ "#"; id = class_longident -> <:ctyp< # $list:id$ >>
| "<"; (ml, v) = meth_list; ">" -> <:ctyp< < $list:ml$ $opt:v$ > >>
| "<"; ">" -> <:ctyp< < > >> ] ]
;
meth_list:
[ [ f = field; ";"; (ml, v) = SELF -> ([f :: ml], v)
| f = field; ";" -> ([f], False)
| f = field -> ([f], False)
| ".." -> ([], True) ] ]
;
field:
[ [ lab = LIDENT; ":"; t = poly_type -> (lab, t) ] ]
;
(* Polymorphic types *)
typevar:
[ [ "'"; i = ident -> i ] ]
;
poly_type:
[ [ test_typevar_list_dot; tpl = LIST1 typevar; "."; t2 = ctyp ->
<:ctyp< ! $list:tpl$ . $t2$ >>
| t = ctyp -> t ] ]
;
(* Identifiers *)
clty_longident:
[ [ m = UIDENT; "."; l = SELF -> [m :: l]
| i = LIDENT -> [i] ] ]
;
class_longident:
[ [ m = UIDENT; "."; l = SELF -> [m :: l]
| i = LIDENT -> [i] ] ]
;
(* Labels *)
ctyp: LEVEL "arrow"
[ RIGHTA
[ i = lident_colon; t1 = ctyp LEVEL "star"; "->"; t2 = SELF ->
<:ctyp< ( ~ $i$ : $t1$ ) -> $t2$ >>
| i = OPTLABEL; t1 = ctyp LEVEL "star"; "->"; t2 = SELF ->
<:ctyp< ( ? $i$ : $t1$ ) -> $t2$ >>
| i = QUESTIONIDENT; ":"; t1 = ctyp LEVEL "star"; "->"; t2 = SELF ->
<:ctyp< ( ? $i$ : $t1$ ) -> $t2$ >>
| "?"; i=lident_colon;t1 = ctyp LEVEL "star"; "->"; t2 = SELF ->
<:ctyp< ( ? $i$ : $t1$ ) -> $t2$ >> ] ]
;
ctyp: LEVEL "simple"
[ [ "["; OPT "|"; rfl = LIST1 row_field SEP "|"; "]" ->
<:ctyp< [ = $list:rfl$ ] >>
| "["; ">"; "]" -> <:ctyp< [ > $list:[]$ ] >>
| "["; ">"; OPT "|"; rfl = LIST1 row_field SEP "|"; "]" ->
<:ctyp< [ > $list:rfl$ ] >>
| "[<"; OPT "|"; rfl = LIST1 row_field SEP "|"; "]" ->
<:ctyp< [ < $list:rfl$ ] >>
| "[<"; OPT "|"; rfl = LIST1 row_field SEP "|"; ">";
ntl = LIST1 name_tag; "]" ->
<:ctyp< [ < $list:rfl$ > $list:ntl$ ] >> ] ]
;
row_field:
[ [ "`"; i = ident -> MLast.RfTag i True []
| "`"; i = ident; "of"; ao = OPT "&"; l = LIST1 ctyp SEP "&" ->
MLast.RfTag i (o2b ao) l
| t = ctyp -> MLast.RfInh t ] ]
;
name_tag:
[ [ "`"; i = ident -> i ] ]
;
expr: LEVEL "expr1"
[ [ "fun"; p = labeled_patt; e = fun_def -> <:expr< fun $p$ -> $e$ >> ] ]
;
expr: AFTER "apply"
[ "label"
[ i = LABEL; e = SELF -> <:expr< ~ $i$ : $e$ >>
| i = TILDEIDENT -> <:expr< ~ $i$ >>
| "~"; i = LIDENT -> <:expr< ~ $i$ >>
| i = OPTLABEL; e = SELF -> <:expr< ? $i$ : $e$ >>
| i = QUESTIONIDENT -> <:expr< ? $i$ >>
| "?"; i = LIDENT -> <:expr< ? $i$ >> ] ]
;
expr: LEVEL "simple"
[ [ "`"; s = ident -> <:expr< ` $s$ >> ] ]
;
fun_def:
[ [ p = labeled_patt; e = SELF -> <:expr< fun $p$ -> $e$ >> ] ]
;
fun_binding:
[ [ p = labeled_patt; e = SELF -> <:expr< fun $p$ -> $e$ >> ] ]
;
labeled_patt:
[ [ i = LABEL; p = simple_patt ->
<:patt< ~ $i$ : $p$ >>
| i = TILDEIDENT ->
<:patt< ~ $i$ >>
| "~"; i=LIDENT -> <:patt< ~ $i$ >>
| "~"; "("; i = LIDENT; ")" ->
<:patt< ~ $i$ >>
| "~"; "("; i = LIDENT; ":"; t = ctyp; ")" ->
<:patt< ~ $i$ : ($lid:i$ : $t$) >>
| i = OPTLABEL; j = LIDENT ->
<:patt< ? $i$ : ($lid:j$) >>
| i = OPTLABEL; "("; p = patt; "="; e = expr; ")" ->
<:patt< ? $i$ : ( $p$ = $e$ ) >>
| i = OPTLABEL; "("; p = patt; ":"; t = ctyp; ")" ->
<:patt< ? $i$ : ( $p$ : $t$ ) >>
| i = OPTLABEL; "("; p = patt; ":"; t = ctyp; "=";
e = expr; ")" ->
<:patt< ? $i$ : ( $p$ : $t$ = $e$ ) >>
| i = QUESTIONIDENT -> <:patt< ? $i$ >>
| "?"; i = LIDENT -> <:patt< ? $i$ >>
| "?"; "("; i = LIDENT; "="; e = expr; ")" ->
<:patt< ? ( $lid:i$ = $e$ ) >>
| "?"; "("; i = LIDENT; ":"; t = ctyp; "="; e = expr; ")" ->
<:patt< ? ( $lid:i$ : $t$ = $e$ ) >>
| "?"; "("; i = LIDENT; ")" ->
<:patt< ? $i$ >>
| "?"; "("; i = LIDENT; ":"; t = ctyp; ")" ->
<:patt< ? ( $lid:i$ : $t$ ) >> ] ]
;
class_type:
[ [ i = lident_colon; t = ctyp LEVEL "star"; "->"; ct = SELF ->
<:class_type< [ ~ $i$ : $t$ ] -> $ct$ >>
| i = OPTLABEL; t = ctyp LEVEL "star"; "->"; ct = SELF ->
<:class_type< [ ? $i$ : $t$ ] -> $ct$ >>
| i = QUESTIONIDENT; ":"; t = ctyp LEVEL "star"; "->"; ct = SELF ->
<:class_type< [ ? $i$ : $t$ ] -> $ct$ >>
| "?"; i = LIDENT; ":"; t = ctyp LEVEL "star"; "->"; ct = SELF ->
<:class_type< [ ? $i$ : $t$ ] -> $ct$ >> ] ]
;
class_fun_binding:
[ [ p = labeled_patt; cfb = SELF -> <:class_expr< fun $p$ -> $cfb$ >> ] ]
;
END;
(* Main entry points *)
EXTEND
GLOBAL: interf implem use_file top_phrase expr patt;
interf:
[ [ si = sig_item_semi; (sil, stopped) = SELF -> ([si :: sil], stopped)
| "#"; n = LIDENT; dp = OPT expr; ";;" ->
([(<:sig_item< # $n$ $opt:dp$ >>, loc)], True)
| EOI -> ([], False) ] ]
;
sig_item_semi:
[ [ si = sig_item; OPT ";;" -> (si, loc) ] ]
;
implem:
[ [ si = str_item_semi; (sil, stopped) = SELF -> ([si :: sil], stopped)
| "#"; n = LIDENT; dp = OPT expr; ";;" ->
([(<:str_item< # $n$ $opt:dp$ >>, loc)], True)
| EOI -> ([], False) ] ]
;
str_item_semi:
[ [ si = str_item; OPT ";;" -> (si, loc) ] ]
;
top_phrase:
[ [ ph = phrase; ";;" -> Some ph
| EOI -> None ] ]
;
use_file:
[ [ si = str_item; OPT ";;"; (sil, stopped) = SELF ->
([si :: sil], stopped)
| "#"; n = LIDENT; dp = OPT expr; ";;" ->
([<:str_item< # $n$ $opt:dp$ >>], True)
| EOI -> ([], False) ] ]
;
phrase:
[ [ sti = str_item -> sti
| "#"; n = LIDENT; dp = OPT expr -> <:str_item< # $n$ $opt:dp$ >> ] ]
;
END;
Pcaml.add_option "-no_quot" (Arg.Set Plexer.no_quotations)
"Don't parse quotations, allowing to use, e.g. \"<:>\" as token";
EXTEND
expr: AFTER "<"
[[ f = expr; "o"; g = expr -> <:expr< ((o $f$) $g$) >>
| f = expr; "upto"; g = expr -> <:expr< ((upto $f$) $g$) >>
| f = expr; "F_F"; g = expr -> <:expr< ((f_f_ $f$) $g$) >>
| f = expr; "THENC"; g = expr -> <:expr< ((thenc_ $f$) $g$) >>
| f = expr; "THEN"; g = expr -> <:expr< ((then_ $f$) $g$) >>
| f = expr; "THENL"; g = expr -> <:expr< ((thenl_ $f$) $g$) >>
| f = expr; "ORELSE"; g = expr -> <:expr< ((orelse_ $f$) $g$) >>
| f = expr; "ORELSEC"; g = expr -> <:expr< ((orelsec_ $f$) $g$) >>
| f = expr; "THEN_TCL"; g = expr -> <:expr< ((then_tcl_ $f$) $g$) >>
| f = expr; "ORELSE_TCL"; g = expr -> <:expr< ((orelse_tcl_ $f$) $g$) >>
]];
END;
EXTEND
top_phrase:
[ [ sti = str_item; ";;" ->
match sti with
[ <:str_item< $exp:e$ >> -> Some <:str_item< value it = $e$ >>
| x -> Some x ] ] ]
;
END;
|