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 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712
|
unit BuildParseTree;
{(*}
(*------------------------------------------------------------------------------
Delphi Code formatter source code
The Original Code is BuildParseTree, released May 2003.
The Initial Developer of the Original Code is Anthony Steele.
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved.
Contributor(s): Anthony Steele, Adem Baba
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations
under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL")
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}
{ AFS 27 October
This unit turns a token stream into a full parse tree
using the Recursive Descent method
The tokens are then the leaves of a tree structure
The grammer is 'Appendix A Object Pascal grammar'
As found on the borland Web site.
It is much extended via test cases as that is woefully incomplete
}
{$I JcfGlobal.inc}
interface
uses
{ delphi }
{$IFNDEF FPC}Windows,{$ENDIF} Contnrs,
{ local }
ParseTreeNode,
ParseTreeNodeType,
ParseError,
SourceToken,
SourceTokenList,
Tokens,
TokenUtils;
type
TBuildParseTree = class(TObject)
Private
fbMadeTree: boolean;
fiTokenIndex: integer;
fcRoot: TParseTreeNode;
fcStack: TStack;
fcTokenList: TSourceTokenList;
fiTokenCount: integer;
procedure RecogniseTypeHelper;
procedure SplitGreaterThanOrEqual;
procedure RecogniseGoal;
procedure RecogniseUnit;
procedure RecogniseProgram;
procedure RecognisePackage;
procedure RecogniseLibrary;
procedure RecogniseFileEnd;
procedure RecogniseProgramBlock;
procedure RecogniseUsesClause(const pbInFiles: boolean);
procedure RecogniseUsesItem(const pbInFiles: boolean);
procedure RecogniseDottedName;
procedure RecogniseDottedNameElement;
procedure RecogniseInterfaceSection;
procedure RecogniseInterfaceDecls;
procedure RecogniseInterfaceDecl;
procedure RecogniseExportedHeading;
procedure RecogniseIdentifier(const pbCanHaveUnitQualifier: boolean; const peStrictness: TIdentifierStrictness);
procedure RecognisePossiblyAmpdIdentifier;
procedure RecogniseImplementationSection;
procedure RecogniseDeclSections;
procedure RecogniseDeclSection;
procedure RecogniseInitSection;
procedure RecogniseBlock(const CanBeJustEnd: boolean = false);
procedure RecogniseIdentList(const pbCanHaveUnitQualifier: boolean);
procedure RecogniseIdentValue;
procedure RecogniseAsCast;
procedure RecogniseLabelDeclSection;
procedure RecogniseLabel;
procedure RecogniseConstSection(const pbNestedInClass: Boolean);
procedure RecogniseConstantDecl;
procedure CheckLabelPrefix;
procedure RecogniseTypeSection(const pbNestedInCLass: Boolean);
procedure RecogniseVarSection(const pbClassVars: boolean);
procedure RecogniseClassVars;
procedure RecogniseProcedureDeclSection;
procedure RecogniseClassOperator(const pbHasBody: boolean);
procedure RecogniseOperator(const pbHasBody: boolean);
procedure RecogniseOperatorSymbol;
// set pbAnon = true if the proc has no name
procedure RecogniseProcedureHeading(const pbAnon, pbCanInterfaceMap: boolean);
procedure RecogniseFunctionHeading(const pbAnon, pbCanInterfaceMap: boolean);
procedure RecogniseCompoundStmnt;
procedure RecogniseStatementList(const peEndTokens: TTokenTypeSet);
procedure RecogniseStatement;
procedure RecogniseTypeId;
procedure RecogniseTypedConstant;
procedure RecogniseArrayConstant;
procedure RecogniseRecordConstant;
procedure RecogniseRecordFieldConstant;
procedure RecogniseTypeDecl;
procedure RecogniseArrayType;
procedure RecogniseClassRefType;
procedure RecogniseEnumeratedType;
procedure RecogniseFieldDecl;
procedure RecogniseFieldList;
procedure RecogniseRecordStaticItem;
procedure RecogniseMethodReferenceType;
procedure RecogniseFileType;
procedure RecogniseOrdIdent;
procedure RecogniseOrdinalType;
procedure RecognisePointerType;
procedure RecogniseProcedureType;
procedure RecogniseRealType;
procedure RecogniseRecordType;
procedure RecogniseRecordBody;
procedure RecogniseRecVariant;
procedure RecogniseRestrictedType;
procedure RecogniseSpecializeType;
procedure RecogniseSetType;
procedure RecogniseSimpleType;
procedure RecogniseStringType;
procedure RecogniseStrucType;
procedure RecogniseSubrangeType;
procedure RecogniseType;
procedure RecogniseVariantType;
procedure RecogniseClassType;
procedure RecogniseClassBody;
procedure RecogniseClassDeclarations(const pbInterface: boolean);
procedure RecogniseInterfaceType;
procedure RecogniseObjectType;
procedure RecogniseVariantSection;
procedure RecogniseVarDecl;
procedure RecogniseAddOp;
procedure RecogniseDesignator;
procedure RecogniseDesignatorTail;
procedure RecogniseExpr(const pbAllowRelop: boolean);
procedure RecogniseExprList;
procedure RecogniseFactor;
procedure RecogniseUnarySymbolFactor;
procedure RecogniseTerm;
procedure RecogniseMulOp;
procedure RecogniseRelOp;
procedure RecogniseSetConstructor;
procedure RecogniseSetElement;
procedure RecogniseQualId;
procedure RecogniseConstantExpression;
procedure RecogniseLiteralString;
procedure RecogniseBracketedStatement;
procedure RecognisePossibleAssign;
procedure RecogniseSimpleExpression;
procedure RecogniseSimpleStmnt;
procedure RecogniseCaseLabel;
procedure RecogniseCaseSelector;
procedure RecogniseCaseStmnt;
procedure RecogniseForStmnt;
procedure RecogniseIfStmnt;
procedure RecogniseRepeatStmnt;
procedure RecogniseStructStmnt;
procedure RecogniseWhileStmnt;
procedure RecogniseWithStmnt;
procedure RecogniseTryStatement;
procedure RecogniseExceptionHandlerBlock;
procedure RecogniseExceptionHandler;
procedure RecogniseRaise;
procedure RecogniseInline;
procedure RecogniseInlineItem;
procedure RecogniseFunctionDecl(const pbAnon: boolean);
procedure RecogniseProcedureDecl(const pbAnon: boolean);
procedure RecogniseConstructorDecl;
procedure RecogniseDestructorDecl;
procedure RecogniseFormalParameters;
procedure RecogniseFormalParam;
procedure RecogniseParameter;
procedure RecogniseActualParams;
procedure RecogniseActualParam;
procedure RecogniseProcedureDirectives;
procedure RecogniseExportsSection;
procedure RecogniseExportedProc;
// set pbDeclaration to false if the method body is to be recognised
procedure RecogniseConstructorHeading(const pbDeclaration: boolean);
procedure RecogniseDestructorHeading(const pbDeclaration: boolean);
procedure RecogniseObjHeritage;
procedure RecogniseContainsClause;
procedure RecogniseInterfaceHeritage;
procedure RecogniseProperty;
procedure RecognisePropertyInterface;
procedure RecognisePropertyParameterList;
procedure RecognisePropertySpecifiers;
procedure RecognisePropertyAccess;
procedure RecogniseRequiresClause;
procedure RecogniseInterfaceGuid;
procedure RecogniseClassHeritage;
procedure RecogniseClassVisibility;
procedure RecogniseMethodName(const pbClassNameCompulsory: boolean);
procedure RecogniseAsmBlock;
procedure RecogniseAsmParam;
procedure RecogniseAsmStatement;
procedure RecogniseAsmExpr;
procedure RecogniseAsmOperator;
procedure RecogniseAsmFactor;
procedure RecogniseAsmIdent;
procedure RecogniseAsmOpcode;
procedure RecogniseAsmLabel(const pbColon: boolean);
procedure RecogniseWhiteSpace;
procedure RecogniseNotSolidTokens;
procedure RecogniseHintDirectives;
procedure RecognisePropertyDirectives;
procedure RecogniseExternalProcDirective;
function RecognisePublicProcDirective: boolean;
procedure RecogniseAttributes;
function GenericAhead: boolean;
procedure RecogniseGenericType;
procedure Recognise(const peTokenTypes: TTokenTypeSet; const pbKeepTrailingWhiteSpace: Boolean = False); overload;
procedure Recognise(const peTokenType: TTokenType; const pbKeepTrailingWhiteSpace: Boolean = False); overload;
function PushNode(const peNodeType: TParseTreeNodeType): TParseTreeNode;
function PopNode: TParseTreeNode;
function TopNode: TParseTreeNode;
function IdentifierNext(const peStrictness: TIdentifierStrictness): boolean;
function ArrayConstantNext: boolean;
function SubrangeTypeNext: boolean;
function TypePastAttribute: boolean;
procedure RecogniseGenericConstraints;
procedure RecogniseGenericConstraint;
procedure RecogniseHeritageList;
procedure RecogniseAnonymousMethod;
function AnonymousMethodNext: boolean;
Protected
Public
constructor Create;
destructor Destroy; override;
procedure BuildParseTree;
procedure Clear;
property Root: TParseTreeNode Read fcRoot;
property TokenList: TSourceTokenList Read fcTokenList Write fcTokenList;
end;
implementation
uses
{ delphi }
SysUtils, Forms,
{ local }
JcfStringUtils;
const
UPDATE_INTERVAL = 512;
{------------------------------------------------------------------------------
standard overrides }
constructor TBuildParseTree.Create;
begin
inherited;
fcStack := TStack.Create;
fcRoot := nil;
fiTokenCount := 0;
end;
destructor TBuildParseTree.Destroy;
begin
Clear;
FreeAndNil(fcStack);
inherited;
end;
procedure TBuildParseTree.Clear;
begin
while fcStack.Count > 0 do
fcStack.Pop;
FreeAndNil(fcRoot);
end;
procedure TBuildParseTree.RecogniseHeritageList;
var
lbMore: boolean;
begin
{ heritage of a class or interface
}
lbMore := true;
while lbMore do
begin
if fcTokenList.FirstSolidTokenType = ttSpecialize then
Recognise(ttSpecialize);
RecogniseDottedName;
if fcTokenList.FirstSolidTokenType = ttLessThan then
begin
RecogniseGenericType;
end;
lbMore := fcTokenList.FirstSolidTokenType = ttComma;
if lbMore then
Recognise(ttComma);
end;
end;
procedure TBuildParseTree.BuildParseTree;
begin
Assert(fcTokenList <> nil);
Clear;
{ read to end of file necessary?
liIndex := 0;
while BufferTokens(liIndex).TokenType <> ttEOF do
begin
BufferTokens(liIndex);
inc(liIndex);
end; }
fiTokenIndex := 0;
RecogniseGoal;
{ should not have any sections started but not finished }
Assert(fcStack.Count = 0);
{ all tokens should have been processed }
Assert(fcTokenList.Count = fcTokenList.CurrentTokenIndex);
fcTokenList.Clear;
fbMadeTree := True;
end;
{-------------------------------------------------------------------------------
recogniser support }
procedure TBuildParseTree.Recognise(const peTokenTypes: TTokenTypeSet;
const pbKeepTrailingWhiteSpace: Boolean);
function DescribeTarget: string;
begin
Result := '"';
if peTokenTypes <> [] then
Result := Result + TokenTypesToString(peTokenTypes);
Result := Result + '"';
end;
var
lcCurrentToken: TSourceToken;
begin
// must accept something
Assert(peTokenTypes <> []);
{ read tokens up to and including the specified one.
Add them to the parse tree at the current growing point }
while not fcTokenList.EOF do
begin
lcCurrentToken := fcTokenList.Extract;
Assert(lcCurrentToken <> nil);
TopNode.AddChild(lcCurrentToken);
// the the match must be the first solid token
if lcCurrentToken.TokenType in peTokenTypes then
begin
// found it
Break;
end
// accept any white space until we find it
else if not (lcCurrentToken.TokenType in NotSolidTokens) then
raise TEParseError.Create('Unexpected token, expected ' +
DescribeTarget, lcCurrentToken);
end;
Inc(fiTokenCount);
{$IFNDEF COMMAND_LINE}
if (fiTokenCount mod UPDATE_INTERVAL) = 0 then
Application.ProcessMessages;
{$ENDIF}
{ add trailing white space
fixes some problems, causes others
problem is that comments are not well-attached }
// add trailing white space
if pbKeepTrailingWhiteSpace then
RecogniseNotSolidTokens;
end;
procedure TBuildParseTree.Recognise(const peTokenType: TTokenType; const pbKeepTrailingWhiteSpace: Boolean = False);
begin
Recognise([peTokenType], pbKeepTrailingWhiteSpace);
end;
function TBuildParseTree.PushNode(const peNodeType: TParseTreeNodeType): TParseTreeNode;
begin
Result := TParseTreeNode.Create;
Result.NodeType := peNodeType;
if fcStack.Count > 0 then
begin
TopNode.AddChild(Result);
Result.Parent := TopNode;
end
else
fcRoot := Result;
fcStack.Push(Result);
end;
function TBuildParseTree.PopNode: TParseTreeNode;
begin
Result := fcStack.Pop;
end;
function TBuildParseTree.TopNode: TParseTreeNode;
begin
Result := fcStack.Peek;
end;
{a unit / type/var name }
function TBuildParseTree.IdentifierNext(const peStrictness: TIdentifierStrictness): boolean;
var
lc: TSourceToken;
begin
lc := fcTokenList.FirstSolidToken;
{ We have to admit directives and type names as identifiers. see TestBogusDirectives.pas for the reasons why }
Result := IsIdentifierToken(lc, peStrictness);
end;
{-------------------------------------------------------------------------------
recognisers for the parse tree top to bottom
These procs are based on the "Appendix A Object Pascal grammar"
Found on the Borland Web site
All the productions should be here, in the same order
}
procedure TBuildParseTree.RecogniseGoal;
var
lc: TSourceToken;
s: string;
begin
// Goal -> (Program | Package | Library | Unit)
if fcTokenList.Count < 1 then
raise TEParseError.Create('No source to parse', nil);
lc := fcTokenList.FirstSolidToken;
Assert(lc <> nil);
WriteStr(s, lc.TokenType);
case lc.TokenType of
ttProgram:
RecogniseProgram;
ttPackage:
RecognisePackage;
ttLibrary:
RecogniseLibrary;
ttUnit:
RecogniseUnit;
else
raise TEParseError.Create('Expected program, package, library, unit, got "'
+ s + '" ', lc);
end
end;
procedure TBuildParseTree.RecogniseProgram;
begin
// Program -> [PROGRAM Ident ['(' IdentList ')'] ';'] ProgramBlock '.'
PushNode(nProgram);
PushNode(nUnitHeader);
Recognise(ttProgram);
PushNode(nUnitName);
RecogniseIdentifier(False, idStrict);
PopNode;
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
begin
Recognise(ttOpenBracket);
RecogniseIdentList(False);
Recognise(ttCloseBracket);
end;
if fcTokenList.FirstSolidTokenType = ttSemiColon then
Recognise(ttSemicolon);
PopNode;
RecogniseProgramBlock;
RecogniseFileEnd;
PopNode;
end;
procedure TBuildParseTree.RecogniseUnit;
begin
// Unit -> UNIT Ident ';' InterfaceSection ImplementationSection InitSection '.'
PushNode(nUnit);
PushNode(nUnitHeader);
Recognise(ttUnit);
PushNode(nUnitName);
RecogniseDottedName;
PopNode;
{ unit can be "deprecated platform library" }
if fcTokenList.FirstSolidTokenType in HintDirectives then
begin
PushNode(nHintDirectives);
while fcTokenList.FirstSolidTokenType in HintDirectives do
Recognise(HintDirectives);
PopNode;
end;
{ or platform }
if fcTokenList.FirstSolidTokenType = ttPlatform then
Recognise(ttPlatform);
Recognise(ttSemicolon);
PopNode;
RecogniseInterfaceSection;
RecogniseImplementationSection;
RecogniseInitSection;
RecogniseFileEnd;
PopNode;
end;
procedure TBuildParseTree.RecognisePackage;
begin
// Package -> PACKAGE Ident ';' [RequiresClause] [ContainsClause] END '.'
PushNode(nPackage);
PushNode(nUnitHeader);
Recognise(ttPackage);
PushNode(nUnitName);
RecogniseIdentifier(False, idStrict);
PopNode;
Recognise(ttSemicolon);
PopNode;
if fcTokenList.FirstSolidTokenType = ttRequires then
RecogniseRequiresClause;
if fcTokenList.FirstSolidTokenType = ttContains then
RecogniseContainsClause;
Recognise(ttEnd);
RecogniseFileEnd;
PopNode;
end;
procedure TBuildParseTree.RecogniseLibrary;
begin
// Library -> LIBRARY Ident ';' ProgramBlock '.'
PushNode(nLibrary);
PushNode(nUnitHeader);
Recognise(ttLibrary);
PushNode(nUnitName);
RecogniseIdentifier(False, idStrict);
PopNode;
Recognise(ttSemicolon);
PopNode;
RecogniseProgramBlock;
RecogniseFileEnd;
PopNode;
end;
procedure TBuildParseTree.RecogniseFileEnd;
var
lcCurrentToken: TSourceToken;
begin
Recognise(ttDot);
{ delphi accepts anything after the final end }
while not fcTokenList.EOF do
begin
lcCurrentToken := fcTokenList.Extract;
TopNode.AddChild(lcCurrentToken);
end;
end;
procedure TBuildParseTree.RecogniseProgramBlock;
var
lc: TSourceToken;
begin
// ProgramBlock -> [UsesClause] Block
// also it seems that the block is optional, can just be the "end" for the file
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttUses then
RecogniseUsesClause(True);
if fcTokenList.FirstSolidTokenType = ttOpenSquareBracket then
RecogniseAttributes;
RecogniseBlock(True);
end;
procedure TBuildParseTree.RecogniseUsesClause(const pbInFiles: boolean);
begin
// recognise comments etc before the uses clause
RecogniseNotSolidTokens;
// UsesClause -> USES IdentList ';'
PushNode(nUses);
Recognise(ttUses);
// IdentList -> Ident/','...
PushNode(nIdentList);
RecogniseNotSolidTokens;
RecogniseUsesItem(pbInFiles);
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseNotSolidTokens;
RecogniseUsesItem(pbInFiles);
end;
PopNode;
Recognise(ttSemicolon);
PopNode;
RecogniseNotSolidTokens;
end;
procedure TBuildParseTree.RecogniseUsesItem(const pbInFiles: boolean);
begin
PushNode(nUsesItem);
RecogniseDottedName;
if pbInFiles and (fcTokenList.FirstSolidTokenType = ttIn) then
begin
Recognise(ttIn);
Recognise(ttQuotedLiteralString);
end;
RecogniseNotSolidTokens;
PopNode;
end;
{ elements in a dotted name are usually just identifiers
but occasionally are reserved words - e.g. "object" and "type"
as in "var MyType: System.Type; " or "var pElement: System.Object; "
}
procedure TBuildParseTree.RecogniseDottedNameElement;
var
lcNext: TSourceToken;
begin
lcNext := fcTokenList.FirstSolidToken;
if lcNext = nil then
exit;
case lcNext.TokenType of
ttObject:
Recognise(ttObject);
ttType:
Recognise(ttType);
ttAmpersand:
RecognisePossiblyAmpdIdentifier;
else
// "Label" is valid here as an identifier even though it is a reserved word
RecogniseIdentifier(False, idAny);
end;
end;
procedure TBuildParseTree.RecogniseDottedName;
begin
RecogniseIdentifier(False, idStrict);
while fcTokenList.FirstSolidTokenType = ttDot do
begin
Recognise(ttDot);
RecogniseDottedNameElement;
end;
end;
procedure TBuildParseTree.RecogniseInterfaceSection;
begin
// InterfaceSection -> INTERFACE [UsesClause] [InterfaceDecl]...
PushNode(nInterfaceSection);
Recognise(ttInterface, True);
if fcTokenList.FirstSolidTokenType = ttUses then
RecogniseUsesClause(True);
RecogniseInterfaceDecls;
PopNode;
end;
procedure TBuildParseTree.RecogniseInterfaceDecls;
begin
{ a list of InterfaceDecl sections
e.g.
var a,b: integer;
const b = 3;
type foo = integer;
procedure fred;
NB also threadvar
}
while fcTokenList.FirstSolidTokenType in [ttConst, ttResourceString,
ttType, ttVar, ttThreadVar, ttOpenSquareBracket, ttExports, ttOperator] + ProcedureWords do
RecogniseInterfaceDecl;
end;
procedure TBuildParseTree.RecogniseInterfaceDecl;
var
lc: TSourceToken;
lt: Tokens.TTokenType;
begin
{
InterfaceDecl
-> ConstSection
-> TypeSection
-> VarSection
-> ExportedHeading
}
PushNode(nDeclSection);
lc := fcTokenList.FirstSolidToken;
lt := fcTokenList.FirstSolidTokenType;
case lt of
ttConst, ttResourceString:
RecogniseConstSection(false);
ttType:
RecogniseTypeSection(false);
ttVar, ttThreadvar:
RecogniseVarSection(false);
ttProcedure, ttFunction, ttOperator:
RecogniseExportedHeading;
ttOpenSquareBracket:
RecogniseAttributes;
ttExports:
RecogniseExportsSection;
else
raise TEParseError.Create('Expected const, type, var, procedure or function', lc);
end;
PopNode;
RecogniseNotSolidTokens;
end;
procedure TBuildParseTree.RecogniseExportedHeading;
var
lc: TSourceToken;
lt: TTokenType;
begin
{ ExportedHeading
-> ProcedureHeading ';' [Directive]
-> FunctionHeading ';' [Directive] }
lc := fcTokenList.FirstSolidToken;
lt := lc.TokenType;
case lt of
ttProcedure:
begin
RecogniseProcedureHeading(False, False);
end;
ttFunction:
begin
RecogniseFunctionHeading(False, False);
end;
ttOperator:
begin
RecogniseOperator(false);
end
else
raise TEParseError.Create('Expected function or procedure', lc);
end;
{ the ';' is ommited by lazy programmers in some rare occasions}
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon);
end;
procedure TBuildParseTree.RecogniseImplementationSection;
begin
{
ImplementationSection -> IMPLEMENTATION
[UsesClause]
[DeclSection]...
}
PushNode(nImplementationSection);
Recognise(ttImplementation, True);
if fcTokenList.FirstSolidTokenType = ttUses then
RecogniseUsesClause(True);
RecogniseDeclSections;
PopNode;
end;
procedure TBuildParseTree.RecogniseBlock(const CanBeJustEnd: boolean = false);
var
lc: TSourceToken;
lt: TTokenType;
begin
{ Block -> [DeclSection] CompoundStmt }
lc := fcTokenList.FirstSolidToken;
lt := lc.TokenType;
PushNode(nBlock);
// [DeclSection]
if lt in (Declarations + ProcedureWords) then
RecogniseDeclSections;
lc := fcTokenList.FirstSolidToken;
lt := lc.TokenType;
if lt = ttAsm then
RecogniseAsmBlock
else if CanBeJustEnd and (lt = ttEnd) then
Recognise(ttEnd)
else
RecogniseCompoundStmnt;
PopNode;
end;
procedure TBuildParseTree.RecogniseDeclSections;
begin
{ a list of Decl sections
e.g.
label b;
var a: integer;
const b = 3;
type foo = integer;
procedure fred;
class procedure TFoo.bar;
}
while fcTokenList.FirstSolidTokenType in
[ttClass] + Declarations + ProcedureWords do
RecogniseDeclSection;
end;
procedure TBuildParseTree.RecogniseDeclSection;
var
lc: TSourceToken;
lt: TTokenType;
begin
PushNode(nDeclSection);
{
DeclSection
-> LabelDeclSection
-> ConstSection
-> TypeSection
-> VarSection
-> ProcedureDeclSection
}
lc := fcTokenList.FirstSolidToken;
lt := fcTokenList.FirstSolidTokenType;
case lt of
ttLabel:
RecogniseLabelDeclSection;
ttConst, ttResourceString:
RecogniseConstSection(false);
ttType:
RecogniseTypeSection(false);
ttVar, ttThreadvar:
RecogniseVarSection(false);
ttProcedure, ttFunction, ttConstructor, ttDestructor, ttClass, ttOperator:
RecogniseProcedureDeclSection;
ttExports:
RecogniseExportsSection;
else
raise TEParseError.Create(
'Expected label, const, type, var, procedure or function', lc);
end;
PopNode;
RecogniseNotSolidTokens;
end;
procedure TBuildParseTree.RecogniseLabelDeclSection;
begin
{
LabelDeclSection -> LABEL LabelId
this grammer can't be right. Can be mutiple labels and must have semicolon
e.g.
Label foo, bar, fish;
code below is more flexible
}
PushNode(nLabelDeclSection);
Recognise(ttLabel);
// almost a RecogniseIdentList, but not quite. also numbers allowed
PushNode(nIdentList);
RecogniseLabel;
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseLabel;
end;
PopNode;
Recognise(ttSemicolon);
PopNode;
end;
procedure TBuildParseTree.RecogniseLabel;
begin
if fcTokenList.FirstSolidTokenType = ttNumber then
Recognise(ttNumber)
else
// no unit qualifier
RecogniseIdentifier(False, idAllowDirectives);
end;
procedure TBuildParseTree.RecogniseConstSection(const pbNestedInClass: Boolean);
var
leFirstTokenType: TTokenType;
begin
{
ConstSection -> CONST (ConstantDecl ';')...
}
PushNode(nConstSection);
Recognise([ttConst, ttResourceString]);
while (fcTokenList.FirstSolidWordType in IdentifierTypes) do
begin
RecogniseConstantDecl;
Recognise(ttSemicolon);
// #Trident# If const is nested inside a class, a visibility designator
// ("private" for exemple) can be written after.
// So, inside a class, no wtReservedWordDirective allowed
leFirstTokenType := fcTokenList.FirstSolidTokenType;
if pbNestedInClass and (leFirstTokenType in ClassVisibility) then
break;
// can be followed by an operator decl in FreePascal
if leFirstTokenType = ttOperator then
break;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseConstantDecl;
var
lc: TSourceToken;
begin
{
ConstantDecl
-> Ident '=' ConstExpr
-> Ident ':' TypeId '=' TypedConstant
TypeID is too simplistic -
can be, for e.g.
"const foo: array[1..3] of integer = (1,2,3);"
or "const recs: array[1..3] of TSomeRecord = ( (... "
}
PushNode(nConstDecl);
RecogniseIdentifier(False, idAllowDirectives);
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttEquals then
begin
Recognise(ttEquals);
RecogniseConstantExpression;
end
else if lc.TokenType = ttColon then
begin
Recognise(ttColon);
//RecogniseTypeId;
RecogniseType;
Recognise(ttEquals);
RecogniseTypedConstant;
end
else
raise TEParseError.Create('Expected equals or colon', lc);
{ can be deprecated library platform }
RecogniseHintDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseTypeSection(const pbNestedInCLass: Boolean);
var
lc: TSourceToken;
begin
{
TypeSection -> TYPE (TypeDecl ';')...
}
PushNode(nTypeSection);
Recognise(ttType);
{ In Delphi.Net, the type can be preceeded by an attribute in '[ ]' }
lc := fcTokenList.FirstSolidToken;
while (lc <> nil) and ((lc.WordType in IdentifierTypes) or TypePastAttribute) do
begin
RecogniseTypeDecl;
if pbNestedInClass and (fcTokenList.FirstSolidTokenType in ClassVisibility) then
break;
lc := fcTokenList.FirstSolidToken;
end;
PopNode;
end;
// is there an attribute followed by a type name?
function TBuildParseTree.TypePastAttribute: boolean;
var
lc: TSourceToken;
i: integer;
procedure AdvanceToSolid;
begin
while (lc <> nil) and (not lc.IsSolid) do
begin
inc(i);
lc := fcTokenList.SourceTokens[i];
end;
end;
begin
i := fcTokenList.CurrentTokenIndex;
lc := fcTokenList.SourceTokens[i];
AdvanceToSolid;
if (lc = nil) or (lc.TokenType <> ttOpenSquareBracket) then
begin
Result := False;
exit;
end;
while (lc <> nil) and (lc.TokenType <> ttCloseSquareBracket) do
begin
inc(i);
lc := fcTokenList.SourceTokens[i];
end;
inc(i);
lc := fcTokenList.SourceTokens[i];
if lc = nil then
begin
Result := False;
exit;
end;
AdvanceToSolid;
Result := (lc <> nil) and (lc.WordType in IdentifierTypes);
end;
procedure TBuildParseTree.RecogniseTypeHelper;
begin
PushNode(nClassType);
Recognise([ttType,ttRecord]);
Recognise(ttHelper);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then begin
Recognise(ttOpenBracket);
RecogniseIdentifier(False, idStrict);
Recognise(ttCloseBracket);
end;
Recognise(ttFor);
RecogniseIdentifier(False, idStrict);
RecogniseClassBody;
Recognise(ttEnd);
RecogniseHintDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseTypeDecl;
begin
{
TypeDecl -> Ident '=' Type
-> Ident '=' RestrictedType
Need a semicolon
}
PushNode(nTypeDecl);
//Recognise generic keyword (for fpc)
if (fcTokenList.FirstSolidTokenType = ttGeneric) then
begin
Recognise(ttGeneric);
end;
// Delph.Net Attribute?
if (fcTokenList.FirstSolidTokenType = ttOpenSquareBracket) then
RecogniseAttributes;
RecogniseIdentifier(False, idAllowDirectives);
if fcTokenList.FirstSolidTokenType = ttLessThan then
begin
// generic type decl
RecogniseGenericType;
end;
Recognise(ttEquals);
//Recognise type helper (for fpc)
if (fcTokenList.FirstSolidTokenType in [ttType,ttRecord]) and
(fcTokenList.SolidToken(2).TokenType=ttHelper) then
begin
RecogniseTypeHelper;
end else
// type or restricted type
if (fcTokenList.FirstSolidTokenType in [ttObject, ttClass, ttInterface,
ttDispInterface]) then
RecogniseRestrictedType
else
RecogniseType;
if fcTokenList.FirstSolidTokenType = ttLessThan then
begin
RecogniseGenericType;
end;
if fcTokenList.FirstSolidTokenType = ttIs then
begin
Recognise(ttIs);
Recognise(ttNested);
end;
// the type can be deprecated
if fcTokenList.FirstSolidTokenType = ttDeprecated then
Recognise(ttDeprecated);
Recognise(ttSemicolon);
PopNode;
end;
function TBuildParseTree.GenericAhead: boolean;
var
liTokenIndex: integer;
lcToken: TSourceToken;
begin
Result := false;
// generics follow the pattern "< typeid >" or "< typeid, typeid >"
if fcTokenList.FirstSolidTokenType <> ttLessThan then
begin
exit;
end;
liTokenIndex := 2;
while True do
begin
lcToken := fcTokenList.SolidToken(liTokenIndex);
if lcToken = nil then
begin
exit;
end;
// alternating id and comma
if liTokenIndex mod 2 = 0 then
begin
// should be id
if (lcToken.WordType <> wtBuiltInType) and (not IsIdentifierToken(lcToken, idAny)) then
begin
break;
end;
end
else
begin
// should be comma or end with ">"
if lcToken.TokenType = ttGreaterThan then
begin
Result := true;
break;
end
else if lcToken.TokenType = ttLessThan then
begin
// looks like a nested generic
Result := true;
break;
end
else if lcToken.TokenType <> ttComma then
begin
break;
end;
end;
inc(liTokenIndex);
end; // while
end;
const
ConstraintTokens = [ttClass, ttRecord, ttConstructor];
procedure TBuildParseTree.RecogniseGenericType;
begin
PushNode(nGeneric);
// angle brackets
Recognise(ttLessThan);
RecogniseType;
if fcTokenList.FirstSolidTokenType = ttColon then
begin
RecogniseGenericConstraints;
end;
// more types after commas
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseType;
end;
if fcTokenList.FirstSolidTokenType = ttGreaterThanOrEqual then
begin
// the tokenizer got it wrong - e.g "TTestNullable<T:Record>=Class"
// this is the same as TTestNullable<T:Record> =Class
RecogniseWhiteSpace;
SplitGreaterThanOrEqual;
end;
Recognise(ttGreaterThan);
PopNode;
end;
procedure TBuildParseTree.RecogniseGenericConstraints;
begin
// restriction on the generic type. Colon followed by the constraint
Recognise(ttColon);
RecogniseGenericConstraint;
// optionally more constraints seperated by commas
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseGenericConstraint;
end;
end;
procedure TBuildParseTree.RecogniseGenericConstraint;
begin
// one of a small set of constraints - class, record, constructor
if fcTokenList.FirstSolidTokenType in ConstraintTokens then
begin
Recognise(ConstraintTokens);
end
else
begin
// can be a class name
RecogniseIdentifier(true, idAny);
// and the class can be generic
if fcTokenList.FirstSolidTokenType = ttLessThan then
begin
RecogniseGenericType;
end;
end;
end;
procedure TBuildParseTree.SplitGreaterThanOrEqual;
var
liIndex: integer;
lcNewToken: TSourceToken;
fsFileName: string;
begin
if fcTokenList.FirstTokenType = ttGreaterThanOrEqual then
begin
liIndex := fcTokenList.CurrentTokenIndex;
fsFileName := fcTokenList.SourceTokens[liIndex].FileName;
fcTokenList.Delete(liIndex);
lcNewToken := TSourceToken.Create();
lcNewToken.FileName := fsFileName;
lcNewToken.SourceCode := '>';
lcNewToken.TokenType := ttGreaterThan;
fcTokenList.Insert(liIndex, lcNewToken);
lcNewToken := TSourceToken.Create();
lcNewToken.FileName := fsFileName;
lcNewToken.SourceCode := '=';
lcNewToken.TokenType := ttEquals;
fcTokenList.Insert(liIndex + 1 , lcNewToken);
end;
end;
{ helper proc for RecogniseTypedConstant
need to distinguish
"expr" from "(expr, expr)"
note that expr can -> (expr)
so we need to notice the comma
is there a semicolon first or a comma
Array of records can be "((f: 1), (f: 2))"
and if it is an array with one element then it is "((f: x))"
Is more deeply nested comma valid in non-array expressions?
}
function TBuildParseTree.ArrayConstantNext: boolean;
var
liIndex: integer;
liBracketLevel: integer;
tt: TTokenType;
begin
Result := False;
if fcTokenList.FirstSolidTokenType <> ttOpenBracket then
exit;
liBracketLevel := 0;
liIndex := fcTokenList.CurrentTokenIndex;
// scan past the open bracket
while fcTokenList.SourceTokens[liIndex].TokenType <> ttOpenBracket do
Inc(liIndex);
if fcTokenList.SourceTokens[liIndex].TokenType = ttOpenBracket then
begin
inc(liBracketLevel);
Inc(liIndex);
end;
// look forward to find the first comma or semicolon
while True do
begin
if liIndex >= fcTokenList.Count then
break;
tt := fcTokenList.SourceTokens[liIndex].TokenType;
if tt = ttOpenBracket then
Inc(liBracketLevel)
else if tt = ttCloseBracket then
Dec(liBracketLevel)
else if (tt = ttComma) then // and (liBracketLevel = 1) then
begin
Result := True;
break;
end
else if (tt = ttSemicolon) and (liBracketLevel = 0) then
begin
Result := False;
break;
end
{ if we get an semicolon at bracket level 2, it means an array of records
e.g.
Const MyFooRecArray = ((x: 2; y:3), (x: 5; y: 6)); }
else if (tt = ttSemicolon) and (liBracketLevel = 2) then
begin
Result := True;
break;
end;
Inc(liIndex);
if (liBracketLevel = 0) then
begin
Result := False;
break;
end;
end;
end;
procedure TBuildParseTree.RecogniseTypedConstant;
begin
{ TypedConstant -> (ConstExpr | ArrayConstant | RecordConstant)
How to tell these apart?
The record constant must start with open brackets, a field name followed by a colon,
e.g. "AREC: TMap = (s1: 'Foo'; i1: 1; i2: 4);"
No complexity is permitted here. All that can vary is the names
Array and normal constants are trickier, as both can start with an
arbitrary number of open brackets
a normal constant is an expression, and an array constant is a
bracketed comma-sperated list of them
You can't look for the word 'array' in the just-parsed text
as an alias type could be used
}
if (fcTokenList.FirstSolidTokenType = ttOpenBracket) and
(fcTokenList.SolidWordType(2) in IdentifierTypes) and
(fcTokenList.SolidTokenType(3) = ttColon) then
begin
RecogniseRecordConstant;
end
else if (ArrayConstantNext) then
begin
RecogniseArrayConstant
end
else
RecogniseConstantExpression;
end;
procedure TBuildParseTree.RecogniseArrayConstant;
begin
// ArrayConstant -> '(' TypedConstant/','... ')'
PushNode(nArrayConstant);
Recognise(ttOpenBracket);
RecogniseTypedConstant;
while (fcTokenList.FirstSolidTokenType = ttComma) do
begin
Recognise(ttComma);
RecogniseTypedConstant;
end;
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseRecordConstant;
begin
// RecordConstant -> '(' RecordFieldConstant/';'... ')'
PushNode(nRecordConstant);
Recognise(ttOpenBracket);
RecogniseRecordFieldConstant;
while (fcTokenList.FirstSolidTokenType = ttSemicolon) do
begin
Recognise(ttSemicolon);
if fcTokenList.FirstSolidTokenType = ttCloseBracket then
break;
RecogniseRecordFieldConstant;
end;
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseRecordFieldConstant;
begin
// RecordFieldConstant -> Ident ':' TypedConstant
PushNode(nRecordFieldConstant);
RecogniseIdentifier(False, idAllowDirectives);
Recognise(ttColon);
RecogniseTypedConstant;
PopNode;
end;
procedure TBuildParseTree.RecogniseType;
var
lc, lc2: TSourceToken;
begin
{
Type
-> TypeId
-> SimpleType
-> StrucType
-> PointerType
-> StringType
-> ProcedureType
-> VariantType
-> ClassRefType
NB: const can be a psuedo-type in params
e.g. "procedure fred(foo: const);"
}
PushNode(nType);
lc := fcTokenList.FirstSolidToken;
lc2 := fcTokenList.SolidToken(2);
if (lc.TokenType = ttType) then
begin
{ this can be a prefix. See help under "Declaring types".
an e.g. is in TestDeclarations.pas }
Recognise(ttType);
end;
{ Adem Baba - used case for speed
not sure this is faster. But it does avoid mixing tokentypes in the conditionals}
case lc.TokenType of
ttConst: Recognise(ttConst);
ttReal48, ttReal, ttSingle, ttDouble, ttExtended, ttCurrency, ttComp,
ttShortInt, ttSmallInt, ttInteger, ttByte, ttLongInt, ttInt64, ttWord,
ttBoolean, ttByteBool, ttWordBool, ttLongBool,
ttChar, ttWideChar, ttLongWord, ttPChar:
RecogniseSimpleType; {RealTypes + OrdTypes}
ttOpenBracket:
RecogniseSimpleType; {enumerated types}
ttPacked:
begin
// packed can be applied to class types and to structured types (e.g. records)
if lc2.TokenType = ttClass then
begin
RecogniseClassType;
end
else if lc2.TokenType = ttObject then
begin
RecogniseObjectType;
end
else
begin
RecogniseStrucType;
end;
end;
ttArray, ttSet, ttFile, ttRecord:
RecogniseStrucType;
ttSpecialize:
RecogniseSpecializeType;
ttHat:
RecognisePointerType;
ttString, ttAnsiString, ttWideString:
RecogniseStringType; {StringWords}
ttProcedure, ttFunction:
RecogniseProcedureType;
ttVariant, ttOleVariant:
RecogniseVariantType; {VariantTypes}
else
if (lc.TokenType = ttClass) and (lc2.TokenType = ttOf) then
begin
RecogniseClassRefType;
end else
if (lc.TokenType = ttReference) and (lc2.TokenType = ttTo) then
begin
RecogniseMethodReferenceType;
end
else if (lc.WordType in IdentifierTypes) or (lc.TokenType = ttAmpersand) then
begin
{ could be a subrange on an enum,
e.g. "clBlue .. clBlack".
NB: this can also be Low(Integer) .. High(Integer)
or <expr> .. <expr>
}
if SubrangeTypeNext then
RecogniseSubRangeType
else
// some previously declared type that this simple prog does not know of
RecogniseTypeId;
end
else
RecogniseSimpleType;
end;
PopNode;
end;
function TBuildParseTree.SubrangeTypeNext: boolean;
var
lc: TSourceToken;
begin
lc := fcTokenList.FirstSolidToken;
result :=
AnsiSameText(lc.SourceCode, 'Low') or
(fcTokenList.SolidTokenType(2) = ttDoubleDot);
{
- not needed
var
liIndex: integer;
leType: TTokenType;
begin
liIndex := fcTokenList.CurrentTokenIndex;
// which comes first, a ".." or a ";"
Result := False;
while True do
begin
if liIndex >= fcTokenList.Count then
break;
leType := fcTokenList.SourceTokens[liIndex].TokenType;
if leType = ttSemicolon then
break;
if leType = ttDoubleDot then
begin
Result := True;
break;
end;
inc(liIndex);
end;
}
end;
procedure TBuildParseTree.RecogniseRestrictedType;
var
lc: TSourceToken;
begin
{
RestrictedType
-> ObjectType
-> ClassType
-> InterfaceType
}
PushNode(nRestrictedType);
lc := fcTokenList.FirstSolidToken;
case lc.TokenType of
ttObject:
RecogniseObjectType;
ttClass:
RecogniseClassType;
ttInterface, ttDispInterface:
RecogniseInterfaceType;
else
raise TEParseError.Create('Expected object, class or interface', lc);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseClassRefType;
begin
// ClassRefType -> CLASS OF TypeId
Recognise(ttClass);
Recognise(ttOf);
RecogniseTypeId;
end;
procedure TBuildParseTree.RecogniseSimpleType;
var
lc: TSourceToken;
begin
// SimpleType -> (OrdinalType | RealType)
lc := fcTokenList.FirstSolidToken;
if lc.TokenType in RealTypes then
RecogniseRealType
else
RecogniseOrdinalType;
end;
procedure TBuildParseTree.RecogniseRealType;
begin
{ RealType
-> REAL48
-> REAL
-> SINGLE
-> DOUBLE
-> EXTENDED
-> CURRENCY
-> COMP
}
Recognise(RealTypes);
end;
procedure TBuildParseTree.RecogniseOrdinalType;
var
lc: TSourceToken;
begin
// OrdinalType -> (SubrangeType | EnumeratedType | OrdIdent)
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttOpenBracket then
RecogniseEnumeratedType
else if lc.TokenType in OrdTypes then
RecogniseOrdIdent
else
RecogniseSubRangeType;
end;
procedure TBuildParseTree.RecogniseOrdIdent;
begin
{
OrdIdent
-> SHORTINT
-> SMALLINT
-> INTEGER
-> BYTE
-> LONGINT
-> INT64
-> WORD
-> BOOLEAN
-> CHAR
-> WIDECHAR
-> LONGWORD
-> PCHAR
}
Recognise(OrdTypes);
end;
procedure TBuildParseTree.RecogniseVariantType;
begin
{
VariantType
-> VARIANT
-> OLEVARIANT
}
Recognise(VariantTypes);
end;
procedure TBuildParseTree.RecogniseSubrangeType;
begin
{ SubrangeType -> ConstExpr '..' ConstExpr
this fails when an array is indexed on an entire type, eg
'BoolArray: array[Boolean] of Boolean;'
}
PushNode(nSubrangeType);
RecogniseConstantExpression;
if fcTokenList.FirstSolidTokenType = ttDoubleDot then
begin
Recognise(ttDoubleDot);
{ recognising any expr is a bad idea here, as "a = 3" is an expression
and we want this to end with a '='
this could be "const ValidCharSet: set of 'A'..'z' = ['A'..'Z','a'..'z'];"
}
RecogniseExpr(False);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseEnumeratedType;
begin
// EnumeratedType -> '(' IdentList ')'
PushNode(nEnumeratedType);
Recognise(ttOpenBracket);
RecogniseIdentList(False);
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseStringType;
begin
{
StringType
-> STRING
-> ANSISTRING
-> WIDESTRING
-> STRING '[' ConstExpr ']'
}
if fcTokenList.FirstSolidTokenType = ttString then
begin
Recognise(ttString);
if fcTokenList.FirstSolidTokenType = ttOpenSquareBracket then
begin
// e.g. var f = String[30];
Recognise(ttOpenSquareBracket);
RecogniseConstantExpression;
Recognise(ttCloseSquareBracket);
end;
end
else
Recognise([ttAnsiString, ttWideString]);
end;
//Recognise specialize keyword in type definition (for fpc)
procedure TBuildParseTree.RecogniseSpecializeType;
begin
Recognise(ttSpecialize);
RecogniseType;
end;
procedure TBuildParseTree.RecogniseStrucType;
var
lc: TSourceToken;
begin
// StrucType -> [PACKED] (ArrayType | SetType | FileType | RecType)
if fcTokenList.FirstSolidTokenType = ttPacked then
Recognise(ttPacked);
lc := fcTokenList.FirstSolidToken;
case lc.TokenType of
ttArray:
RecogniseArrayType;
ttSet:
RecogniseSetType;
ttFile:
RecogniseFileType;
ttRecord:
RecogniseRecordType;
else
raise TEParseError.Create('Expected array, set, file or record type', lc);
end;
end;
procedure TBuildParseTree.RecogniseArrayType;
var
lcType: TTokenType;
begin
// ArrayType -> ARRAY ['[' OrdinalType/','... ']'] OF Type
PushNode(nArrayType);
Recognise(ttArray);
if fcTokenList.FirstSolidTokenType = ttOpenSquarebracket then
begin
Recognise(ttOpenSquareBracket);
{ Maybe just empty bracket with comma inside
Possible syntaxes for dotNET dynamic array :
-> array[]
-> array[,]
-> array[x,e]
}
while fcTokenList.FirstSolidTokenType = ttComma do
Recognise(ttComma);
lcType := fcTokenList.FirstSolidTokenType;
if lcType = ttCloseSquareBracket then
begin
// Delphi.net can have dynamic arrays
end
else
begin
RecogniseOrdinalType;
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseOrdinalType;
end;
end;
Recognise(ttCloseSquareBracket);
end;
Recognise(ttOf);
RecogniseType;
PopNode;
end;
procedure TBuildParseTree.RecogniseRecordType;
var
lcType: TTokenType;
begin
{
RecType -> RECORD [FieldList] END
Also in Delphi.net it can be a forward declaration e.g.
"TRecord1 = record;"
}
PushNode(nRecordType);
Recognise(ttRecord);
lcType := fcTokenList.FirstSolidTokenType;
if lcType = ttSemiColon then
begin
end
else
begin
RecogniseRecordBody;
Recognise(ttEnd);
end;
RecogniseHintDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseRecordBody;
var
lcNextToken: TSourceToken;
begin
lcNextToken := fcTokenList.FirstSolidToken;
if lcNextToken.TokenType = ttEnd then
exit;
RecogniseFieldList;
lcNextToken := fcTokenList.FirstSolidToken;
{ delphi.net records can have public and private parts }
while lcNextToken.TokenType in ClassVisibility + [ttStrict, ttClass] do
begin
PushNode(nClassVisibility);
RecogniseClassVisibility;
RecogniseFieldList;
PopNode;
lcNextToken := fcTokenList.FirstSolidToken;
end;
end;
{ recognise the fields of a record }
procedure TBuildParseTree.RecogniseFieldList;
var
lcNextToken: TSourceToken;
begin
// FieldList -> FieldDecl/';'... [VariantSection] [';']
lcNextToken := fcTokenList.FirstSolidToken;
while not (lcNextToken.TokenType in [ttEnd, ttCase, ttCloseBracket, ttStrict] + ClassVisibility) do
begin
case lcNextToken.TokenType of
ttProcedure:
RecogniseProcedureHeading(False, False);
ttFunction:
RecogniseFunctionHeading(False, False);
ttConstructor:
RecogniseConstructorHeading(True);
ttClass:
RecogniseRecordStaticItem;
ttProperty:
RecogniseProperty;
else
RecogniseFieldDecl;
end;
lcNextToken := fcTokenList.FirstSolidToken;
if lcNextToken.TokenType = ttSemicolon then
begin
Recognise(ttSemicolon);
lcNextToken := fcTokenList.FirstSolidToken;
end
else
Break;
end;
if lcNextToken.TokenType = ttCase then
begin
RecogniseVariantSection;
lcNextToken := fcTokenList.FirstSolidToken;
end;
if lcNextToken.TokenType = ttSemicolon then
Recognise(ttSemicolon);
end;
procedure TBuildParseTree.RecogniseRecordStaticItem;
var
lcNextItem: TSourceToken;
begin
lcNextItem := fcTokenList.SolidToken(2);
case lcNextItem.TokenType of
ttOperator:
RecogniseClassOperator(False);
ttProcedure:
begin
PushNode(nFunctionDecl);
RecogniseProcedureHeading(false, false);
PopNode;
end;
ttFunction:
begin
PushNode(nFunctionDecl);
RecogniseFunctionHeading(false, false);
PopNode;
end;
else
RecogniseClassVars;
end;
end;
procedure TBuildParseTree.RecogniseFieldDecl;
begin
// FieldDecl -> IdentList ':' Type
PushNode(nFieldDeclaration);
RecogniseIdentList(False);
Recognise(ttColon);
RecogniseType;
RecogniseHintDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseVariantSection;
begin
PushNode(nRecordVariantSection);
// VariantSection -> CASE [Ident ':'] TypeId OF RecVariant/';'...
Recognise(ttCase);
// is there an 'of' 2 tokens hence? If not, must be 'ident:' first
if not (fcTokenList.SolidTokenType(2) = ttOf) then
begin
RecogniseIdentifier(False, idAllowDirectives);
Recognise(ttColon);
end;
RecogniseTypeId;
Recognise(ttOf);
// I have tested and that there must be at least 1 case in a var section
repeat
RecogniseRecVariant;
// semicolon is optional on the last one
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon)
else
break;
until (fcTokenList.FirstSolidTokenType in [ttEnd, ttCloseBracket]);
PopNode;
end;
procedure TBuildParseTree.RecogniseRecVariant;
begin
// RecVariant -> ConstExpr/','... ':' '(' [FieldList] ')'
PushNode(nRecordVariant);
RecogniseConstantExpression;
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseConstantExpression;
end;
Recognise(ttColon);
Recognise(ttOpenBracket);
if fcTokenList.FirstSolidTokenType <> ttCloseBracket then
RecogniseFieldList;
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseSetType;
begin
{ SetType -> SET OF OrdinalType
cannot limit it to ord types, as this will not parse the below:
e.g.
type
TFoo = 1..20;
TBars = (monkey, williamshatnir, soy);
TFooSet = set of TFoo;
TBarSet = set of TBar;
}
PushNode(nSetType);
Recognise(ttSet);
Recognise(ttOf);
//RecogniseOrdinalType;
RecogniseType;
PopNode;
end;
procedure TBuildParseTree.RecogniseFileType;
begin
{
FileType -> FILE OF TypeId
also just plain 'file'
}
Recognise(ttFile);
if fcTokenList.FirstSolidTokenType = ttOf then
begin
Recognise(ttOf);
RecogniseTypeId;
end;
end;
procedure TBuildParseTree.RecognisePointerType;
begin
// PointerType -> '^' TypeId
Recognise(ttHat);
RecogniseTypeId;
end;
procedure TBuildParseTree.RecogniseProcedureType;
begin
PushNode(nProcedureType);
// ProcedureType -> (ProcedureHeading | FunctionHeading) [OF OBJECT]
if fcTokenList.FirstSolidTokenType = ttProcedure then
RecogniseProcedureHeading(True, False)
else if fcTokenList.FirstSolidTokenType = ttFunction then
RecogniseFunctionHeading(True, False)
else
raise TEParseError.Create('Expected procedure or function type',
fcTokenList.FirstSolidToken);
if fcTokenList.FirstSolidTokenType = ttOf then
begin
Recognise(ttOf);
Recognise(ttObject);
end;
RecogniseProcedureDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseVarSection(const pbClassVars: boolean);
const
END_VAR_SECTION: TTokenTypeSet =
[ttVar, ttThreadVar, ttConst, ttLabel, ttResourceString, ttType,
ttBegin, ttEnd, ttImplementation, ttInitialization,
ttProcedure, ttFunction, ttOperator, ttConstructor, ttDestructor, ttClass, ttAsm];
var
leEndVarSection: TTokenTypeSet;
begin
leEndVarSection := END_VAR_SECTION;
if pbClassVars then
leEndVarSection := leEndVarSection + ClassVisibility;
PushNode(nVarSection);
// VarSection -> VAR (VarDecl ';')...
Recognise([ttVar, ttThreadvar]);
// can be empty
while not (fcTokenList.FirstSolidTokenType in leEndVarSection) do
begin
RecogniseVarDecl;
Recognise(ttSemicolon);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseClassVars;
var
lbHasVars: Boolean;
begin
PushNode(nClassVars);
Recognise(ttClass);
Recognise(ttVar);
// can be an empty section
lbHasVars := True;
if fcTokenList.FirstSolidTokenType in ClassVisibility + [ttEnd] then
begin
lbHasVars := False;
end;
if lbHasVars then
begin
RecogniseVarDecl;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseClassOperator(const pbHasBody: boolean);
begin
PushNode(nFunctionDecl);
PushNode(nFunctionHeading);
Recognise(ttClass);
Recognise(ttOperator);
RecogniseMethodName(False);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseFormalParameters;
Recognise(ttColon);
PushNode(nFunctionReturnType);
RecogniseType;
PopNode;
RecogniseProcedureDirectives;
PopNode;
if pbHasBody then
begin
Recognise(ttSemiColon);
RecogniseBlock;
Recognise(ttSemiColon);
end;
PopNode;
end;
{
This is a free-pascal style operator
}
procedure TBuildParseTree.RecogniseOperator(const pbHasBody: boolean);
begin
PushNode(nFunctionDecl);
PushNode(nFunctionHeading);
Recognise(ttOperator);
RecogniseOperatorSymbol();
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseFormalParameters;
// FreePascal can give a name to "result" here
if fcTokenList.FirstSolidTokenType <> ttColon then
begin
RecogniseIdentifier(false, idAny);
end;
Recognise(ttColon);
PushNode(nFunctionReturnType);
RecogniseType;
PopNode;
RecogniseProcedureDirectives;
PopNode;
if pbHasBody then
begin
Recognise(ttSemiColon);
RecogniseBlock;
Recognise(ttSemiColon);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseOperatorSymbol;
const
OperatorTokens: TTokenTypeSet = [ttPlus, ttMinus, ttTimes, ttFloatDiv, ttExponent,
ttEquals, ttGreaterThan, ttLessThan, ttGreaterThanOrEqual, ttLessThanOrEqual,
ttAssign, ttPlusAssign, ttMinusAssign, ttTimesAssign, ttFloatDivAssign, ttXor,
ttAnd, ttOr, ttEnumerator];
begin
Recognise(OperatorTokens);
end;
procedure TBuildParseTree.RecogniseVarDecl;
var
lc: TSourceToken;
begin
// VarDecl -> IdentList ':' Type [(ABSOLUTE (Ident | ConstExpr)) | '=' ConstExpr]
PushNode(nVarDecl);
RecogniseIdentList(False);
Recognise(ttColon);
RecogniseType;
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttAbsolute then
begin
PushNode(nAbsoluteVar);
Recognise(ttAbsolute);
if (fcTokenList.FirstSolidWordType in IdentifierTypes) then
begin
// can be a dotted name
RecogniseIdentifier(True, idAllowDirectives);
while fcTokenList.FirstSolidTokenType = ttDot do
begin
Recognise(ttDot);
RecogniseIdentifier(false, idAllowDirectives);
end;
end
else
RecogniseConstantExpression;
PopNode;
end
else
begin
RecogniseHintDirectives;
if fcTokenList.FirstSolidTokenType = ttEquals then
begin
PushNode(nVariableInit);
Recognise(ttEquals);
{ not just an expr - can be an array, record or the like
reuse the code from typed constant declaration as it works the same
}
RecogniseTypedConstant;
PopNode;
end;
end;
{ yes, they can occur here too }
RecogniseHintDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseExpr(const pbAllowRelop: boolean);
begin
{ Expression -> SimpleExpression [RelOp SimpleExpression]...
nb this doesn't parse
lb := foo.Owner;
}
PushNode(nExpression);
RecogniseSimpleExpression;
if pbAllowRelop then
begin
while fcTokenList.FirstSolidTokenType in RelationalOperators do
begin
RecogniseRelop;
RecogniseSimpleExpression;
end;
end;
// added this to cope with real usage - see TestCastSimple
if fcTokenList.FirstSolidTokenType = ttDot then
begin
Recognise(ttDot);
RecogniseExpr(True);
end;
//likewise need to cope with pchar(foo)^
if fcTokenList.FirstSolidTokenType = ttHat then
begin
Recognise(ttHat);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseSimpleExpression;
{var
lc: TSourceToken;}
begin
{ SimpleExpression -> ['+' | '-'] Term [AddOp Term]...
the plus/minus prefix is a red herring
RecogniseFactor does that with a unary operator
}
{
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = wMinus then
Recognise(wMinus)
else if lc.TokenType = wPlus then
Recognise(wPlus);
}
RecogniseTerm;
while fcTokenList.FirstSolidTokenType in AddOperators do
begin
RecogniseAddOp;
RecogniseTerm;
end;
end;
procedure TBuildParseTree.RecogniseTerm;
begin
// Term -> Factor [MulOp Factor]...
PushNode(nTerm);
RecogniseFactor;
while fcTokenList.FirstSolidTokenType in MulOperators do
begin
RecogniseMulOp;
RecogniseFactor;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseFactor;
var
lc: TSourceToken;
begin
{
Factor
-> Designator ['(' ExprList ')']
-> '' Designator
-> Number
-> String
-> NIL
-> '(' Expression ')'
-> NOT Factor
-> SetConstructor
-> TypeId '(' Expression ')'
What is that second line??
What about unary operators other than not,
e.g. b := b * -2;
PossiblyUnarySymbolOperators
Can also be fn call with no params but with the optional braces,
e.g. "Foo();"
or a call to an inherited fucntion, e.g. "inherited foo();
Note that the function name can be omitted "
}
lc := fcTokenList.FirstSolidToken;
if AnonymousMethodNext then
begin
RecogniseAnonymousMethod;
end
else if lc.TokenType = ttInherited then
begin
Recognise(ttInherited);
if not (fcTokenList.FirstSolidTokenType in Operators + [ttSemicolon]) then
begin
RecogniseDesignator;
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
begin
RecogniseActualParams;
end;
end;
end
else if (lc.TokenType = ttNumber) then
begin
Recognise(ttNumber);
end
else if (lc.TokenType in LiteralStringStarters) then
begin
RecogniseLiteralString;
end
else if (lc.TokenType in BuiltInConstants) then
begin
// nil, true, false
Recognise(BuiltInConstants);
end
else if (lc.TokenType = ttOpenBracket) then
begin
Recognise(ttOpenBracket);
while fcTokenList.FirstSolidTokenType = ttComma do
Recognise(ttComma);
{ can be empty brackets }
if fcTokenList.FirstSolidTokenType <> ttCloseBracket then
begin
RecogniseExpr(True);
{ Delphi dotNET : or bracket with initilizer separated by comma
Example : the New method parameters to initialize a dynamic array}
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseExpr(True);
end;
end;
Recognise(ttCloseBracket);
end
else if (lc.TokenType = ttNot) then
begin
Recognise(ttNot);
RecogniseFactor;
end
else if lc.TokenType in PossiblyUnarySymbolOperators then
begin
RecogniseUnarySymbolFactor;
end
else if (lc.TokenType = ttOpenSquareBracket) then
begin
RecogniseSetConstructor;
end
// try identifiers last, since liberal identifiers may match text tokens above
// can prefix with an '&' to force it to be an identifier not a keyword
else if (lc.TokenType = ttAmpersand) or IsIdentifierToken(lc, idAny) then
begin
if lc.TokenType = ttAmpersand then
Recognise(ttAmpersand);
RecogniseDesignator;
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
begin
RecogniseActualParams;
end
else if fcTokenList.FirstSolidTokenType = ttLessThan then
begin
// check for a generic type
if GenericAhead then
begin
// a type constructor - specifying types for the generic
RecogniseGenericType();
end;
end;
end
else
raise TEParseError.Create('unexpected token in factor', lc);
{ can't use lc for FirstSolidToken any more, have moved on }
if fcTokenList.FirstSolidTokenType in [ttHat, ttDot, ttOpenSquareBracket] then
begin
RecogniseDesignatorTail;
end
else if fcTokenList.FirstSolidTokenType = ttOpenBracket then
begin
// following an anonymous method
RecogniseActualParams;
end;
end;
procedure TBuildParseTree.RecogniseUnarySymbolFactor;
var
lc2: TSourceToken;
lbOldStyleCharEscape: boolean;
begin
{!!! special undocumented syntax held from Turbopascal
A char constant can be represented by '^G' for a ctrl-g char etc
This caused problems when it is the likes of '^@' or '^]'
see Sourceforge bugs #888862, #913439
and test case code in TestCharLiterals.pas
}
lbOldStyleCharEscape := False;
if fcTokenList.FirstSolidTokenType = ttHat then
begin
lc2 := fcTokenList.SolidToken(2);
lbOldStyleCharEscape := (lc2 <> nil) and (Length(lc2.Sourcecode) = 1) and
not (CharIsAlpha(lc2.Sourcecode[1]));
end
else
lc2 := nil;
if lbOldStyleCharEscape then
begin
{ bizarre char constant }
Recognise(ttHat);
Recognise(lc2.TokenType);
end
else
begin
{ normal path }
PushNode(nUnaryOp);
Recognise(PossiblyUnarySymbolOperators);
RecogniseFactor;
PopNode;
end;
end;
procedure TBuildParseTree.RecogniseRelOp;
var
lc: TSourceToken;
begin
{RelOp
-> '>'
-> '<'
-> '<='
-> '>='
-> '<>'
-> IN
-> IS
-> AS
}
lc := fcTokenList.FirstSolidToken;
if lc.TokenType in RelationalOperators then
Recognise(RelationalOperators)
else
raise TEParseError.Create('unexpected token in rel op', lc);
end;
procedure TBuildParseTree.RecogniseAddOp;
var
lc: TSourceToken;
begin
lc := fcTokenList.FirstSolidToken;
if lc.TokenType in AddOperators then
Recognise(AddOperators)
else
raise TEParseError.Create('unexpected token in add op', lc);
end;
procedure TBuildParseTree.RecogniseAnonymousMethod;
var
lc: TSourceToken;
begin
lc := fcTokenList.FirstSolidToken;
PushNode(nAnonymousMethod);
case lc.TokenType of
ttProcedure:
RecogniseProcedureDecl(true);
ttFunction:
RecogniseFunctionDecl(true);
else
raise TEParseError.Create('unexpected token in RecogniseAnonymousMethod', lc);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseMulOp;
var
lc: TSourceToken;
begin
{
MulOp
-> '*'
-> '/'
-> DIV
-> MOD
-> AND
-> SHL
-> SHR
}
lc := fcTokenList.FirstSolidToken;
if lc.TokenType in MulOperators then
Recognise(MulOperators)
else
raise TEParseError.Create('unexpected token in mul op', lc);
end;
procedure TBuildParseTree.RecogniseDesignator;
var
lc: TSourceToken;
begin
{ Designator -> QualId ['.' Ident | '[' ExprList ']' | '^']...
Need brackets here too for hard typecasts like
pointer(foo)
And can be an anonymous function/procedure
}
PushNode(nDesignator);
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttAtSign then
begin
Recognise(ttAtSign);
end;
RecogniseQualId;
lc := fcTokenList.FirstSolidToken;
if (lc.TokenType = ttLessThan) and GenericAhead then
begin
RecogniseGenericType;
end;
RecogniseDesignatorTail;
PopNode;
end;
{ Delphi.Net uses '&' to signal that the next token
is not a reserved word,
but is a CLR method of the same name
}
procedure TBuildParseTree.RecognisePossiblyAmpdIdentifier;
begin
if fcTokenList.FirstSolidTokenType = ttAmpersand then
begin
Recognise(ttAmpersand);
RecogniseIdentifier(False, idAny);
end
else
begin
RecogniseIdentifier(False, idAny);
end;
end;
procedure TBuildParseTree.RecogniseDesignatorTail;
const
DESIGNATOR_TAIL_TOKENS = [ttDot, ttOpenBracket, ttOpenSquareBracket, ttHat,
ttPlus, ttMinus, ttAs];
begin
while (fcTokenList.FirstSolidTokenType in DESIGNATOR_TAIL_TOKENS) do
begin
case fcTokenList.FirstSolidTokenType of
ttDot:
begin
Recognise(ttDot);
RecognisePossiblyAmpdIdentifier;
if GenericAhead then
begin
RecogniseGenericType;
end;
end;
ttHat:
begin
Recognise(ttHat);
// and after the deref operator ?
end;
ttOpenSquareBracket:
begin
Recognise(ttOpenSquareBracket);
RecogniseExprList;
Recognise(ttCloseSquareBracket);
end;
ttOpenBracket:
begin
RecogniseActualParams;
end;
ttPlus, ttMinus:
begin
Recognise([ttPlus, ttMinus]);
RecogniseExpr(True);
end;
ttAs:
begin
RecogniseAsCast;
end;
else
Assert(False, 'Should not be here - bad token type');
end;
end;
end;
procedure TBuildParseTree.RecogniseSetConstructor;
begin
// SetConstructor -> '[' [SetElement/','...] ']'
Recognise(ttOpenSquareBracket);
while fcTokenList.FirstSolidTokenType <> ttCloseSquareBracket do
begin
RecogniseSetElement;
if fcTokenList.FirstSolidTokenType = ttComma then
Recognise(ttComma)
else
break; // no comma -> no more items
end;
Recognise(ttCloseSquareBracket);
end;
procedure TBuildParseTree.RecogniseSetElement;
begin
// SetElement -> Expression ['..' Expression]
RecogniseExpr(True);
if fcTokenList.FirstSolidTokenType = ttDoubleDot then
begin
Recognise(ttDoubleDot);
RecogniseExpr(False);
end;
end;
procedure TBuildParseTree.RecogniseExprList;
begin
// ExprList -> Expression/','...
RecogniseExpr(True);
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseExpr(True);
end;
end;
procedure TBuildParseTree.RecogniseStatement;
const
BLOCK_END: TTokenTypeSet = [ttEnd, ttFinally, ttExcept, ttUntil];
var
lc: TSourceToken;
lct: TTokenType;
begin
RecogniseNotSolidTokens;
// Statement -> [LabelId ':'] [SimpleStatement | StructStmt]
PushNode(nStatement);
lct := fcTokenList.FirstSolidTokenType;
if lct = ttSemicolon then
begin
// empty statement
PopNode;
Exit;
end
else if lct = ttEnd then
begin
PopNode;
Exit;
end;
CheckLabelPrefix;
lc := fcTokenList.FirstSolidToken;
{ anything more? can just be a label at the end of the proc/block }
if not (lc.TokenType in BLOCK_END) then
begin
if lc.TokenType in StructStatementWords then
RecogniseStructStmnt
else
RecogniseSimpleStmnt;
end;
PopNode;
end;
procedure TBuildParseTree.CheckLabelPrefix;
var
lc2: TSourceToken;
lbColonSecond: boolean;
begin
lc2 := fcTokenList.SolidToken(2);
lbColonSecond := (lc2.TokenType = ttColon);
if lbColonSecond then
begin
PushNode(nStatementLabel);
RecogniseLabel;
Recognise(ttColon);
PopNode;
{ can be followed by another label }
CheckLabelPrefix
end
end;
procedure TBuildParseTree.RecogniseStatementList(const peEndTokens: TTokenTypeSet);
begin
// StmtList -> Statement/';'...
PushNode(nStatementList);
while not (fcTokenList.FirstSolidTokenType in peEndTokens) do
begin
RecogniseStatement;
// last semicolon is optional
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon)
else
break;
RecogniseNotSolidTokens;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseSimpleStmnt;
var
lc: TSourceToken;
begin
{
SimpleStatement
-> Designator ['(' ExprList ')']
-> Designator ':=' Expression
-> INHERITED
-> GOTO LabelId
-> inline ()
argh this doesn't take brackets into account
as far as I can tell, typecasts like "(lcFoo as TComponent)" is a designator
so is "Pointer(lcFoo)" so that you can do
" Pointer(lcFoo) := Pointer(lcFoo) + 1;
Niether does it take into account using property on returned object, e.g.
qry.fieldbyname('line').AsInteger := 1;
These can be chained indefinitely, as in
foo.GetBar(1).Stuff['fish'].MyFudgeFactor.Default(2).Name := 'Jiim';
you can also bracket the whole expression, as in
"(CheckBox1.Checked := not CheckBox1.Checked);"
}
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttOpenBracket then
begin
RecogniseBracketedStatement;
RecogniseDesignatorTail;
if fcTokenList.FirstSolidTokenType in AssignmentDirectives then
begin
PushNode(nAssignment);
Recognise(fcTokenList.FirstSolidTokenType);
RecogniseExpr(True);
PopNode;
end;
end
else if (IdentifierNext(idAllowDirectives)) or (lc.TokenType = ttAtSign) then
begin
RecognisePossibleAssign;
// else nothing at all is also ok. i.e. procedure call with no params
end
else if lc.TokenType = ttInherited then
begin
{ can be one of
"inherited;
inherited Foo;
inherited Foo(bar);
inherited FooProp := bar;
inherited FooProp[Bar] := Fish;
bar := inherited FooProp[Bar];
}
Recognise(ttInherited);
if IdentifierNext(idAllowDirectives) then
RecogniseSimpleStmnt;
end
else if lc.TokenType = ttGoto then
begin
Recognise(ttGoto);
RecogniseLabel;
end
else if lc.TokenType = ttRaise then
begin
RecogniseRaise;
end
else if lc.TokenType = ttInline then
begin
RecogniseInline;
end
else if lc.TokenType = ttSemicolon then
begin
// empty statement
// this gets doen later in common code Recognise(ttSemicolon);
end
else
raise TEParseError.Create('expected simple statement', lc);
end;
procedure TBuildParseTree.RecogniseBracketedStatement;
begin
Recognise(ttOpenBracket);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseBracketedStatement
else
RecognisePossibleAssign;
Recognise(ttCloseBracket);
RecogniseDesignatorTail;
end;
procedure TBuildParseTree.RecognisePossibleAssign;
begin
// should be fullblown expression?
RecogniseDesignator;
RecogniseDesignatorTail;
if TokenList.FirstSolidTokenType in AssignmentDirectives then
begin
PushNode(nAssignment);
Recognise(TokenList.FirstSolidTokenType);
RecogniseExpr(True);
PopNode;
end;
if (fcTokenList.FirstSolidTokenType = ttAs) then
RecogniseAsCast;
end;
procedure TBuildParseTree.RecogniseRaise;
begin
// another omission - raise expr or just raise (in except block)
Recognise(ttRaise);
if not (fcTokenList.FirstSolidTokenType in [ttSemicolon, ttEnd, ttElse]) then
RecogniseExpr(True);
// can be at addr
if fcTokenList.FirstSolidTokenType = ttAt then
begin
Recognise(ttAt);
RecogniseExpr(True);
end;
end;
procedure TBuildParseTree.RecogniseInline;
begin
{ inline is not supported in Delphi,
but occurs in some Turbo Pascal code.
It is a primitive way to do inline machine code,
by wedging in some literal bytes into the exe
}
PushNode(nInline);
Recognise(ttInline);
Recognise(ttOpenBracket);
// inline body is some inline constants separated by '/'
while fcTokenList.FirstSolidTokenType <> ttCloseBracket do
begin
RecogniseInlineItem;
// floatdiv is the '/' char here
if fcTokenList.FirstSolidTokenType = ttFloatDiv then
Recognise(ttFloatDiv);
end;
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseInlineItem;
begin
PushNode(nInlineItem);
// for not, accept anything up to the '/' or ')'
while not (fcTokenList.FirstSolidTokenType in [ttFloatDiv, ttCloseBracket]) do
Recognise(fcTokenList.FirstSolidTokenType);
PopNode;
end;
procedure TBuildParseTree.RecogniseStructStmnt;
var
lc: TSourceToken;
begin
{
StructStmt
-> CompoundStmt
-> ConditionalStmt
-> LoopStmt
-> WithStmt
}
{ ConditionalStmt
-> IfStmt
-> CaseStmt
}
{
LoopStmt
-> RepeatStmt
-> WhileStmt
-> ForStmt
}
{ they completely left out try blocks !}
lc := fcTokenList.FirstSolidToken;
case lc.TokenType of
ttBegin:
RecogniseCompoundStmnt;
ttAsm:
RecogniseAsmBlock;
ttIf:
RecogniseIfStmnt;
ttCase:
RecogniseCaseStmnt;
ttRepeat:
RecogniseRepeatStmnt;
ttWhile:
RecogniseWhileStmnt;
ttFor:
RecogniseForStmnt;
ttWith:
RecogniseWithStmnt;
ttTry:
RecogniseTryStatement;
else
raise TEParseError.Create('expected structured statement', lc);
end;
end;
procedure TBuildParseTree.RecogniseCompoundStmnt;
begin
{ CompoundStmt -> BEGIN StmtList END }
PushNode(nCompoundStatement);
Recognise(ttBegin);
RecogniseStatementList([ttEnd]);
Recognise(ttEnd);
PopNode;
end;
procedure TBuildParseTree.RecogniseIfStmnt;
begin
// IfStmt -> IF Expression THEN Statement [ELSE Statement]
Recognise(ttIf);
PushNode(nIfCondition);
RecogniseExpr(True);
PopNode;
Recognise(ttThen);
PushNode(nIfBlock);
{ if body can be completely missing - go straight to else }
if fcTokenList.FirstSolidTokenType <> ttElse then
RecogniseStatement;
PopNode;
if fcTokenList.FirstSolidTokenType = ttElse then
begin
Recognise(ttElse);
PushNode(nElseBlock);
if not (fcTokenList.FirstSolidTokenType in [ttElse, ttEnd]) then
RecogniseStatement;
PopNode;
end;
end;
procedure TBuildParseTree.RecogniseCaseStmnt;
begin
// CaseStmt -> CASE Expression OF CaseSelector/';'... [ELSE Statement] [';'] END
PushNode(nCaseStatement);
Recognise(ttCase);
PushNode(nBlockHeaderExpr);
RecogniseExpr(True);
PopNode;
Recognise(ttOf);
while not (fcTokenList.FirstSolidTokenType in [ttElse, ttEnd]) do
RecogniseCaseSelector;
if fcTokenList.FirstSolidTokenType = ttElse then
begin
PushNode(nElseCase);
Recognise(ttElse);
RecogniseStatementList([ttEnd]);
PopNode;
end;
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon);
Recognise(ttEnd);
PopNode;
end;
procedure TBuildParseTree.RecogniseCaseSelector;
begin
// CaseSelector -> CaseLabel/','... ':' Statement ';'
PushNode(nCaseSelector);
PushNode(nCaseLabels);
RecogniseCaseLabel;
while (fcTokenList.FirstSolidTokenType = ttComma) do
begin
Recognise(ttComma);
RecogniseCaseLabel;
end;
Recognise(ttColon);
PopNode;
{ semicolon is optional in the last case before the else }
if not (fcTokenList.FirstSolidTokenType in [ttElse, ttEnd]) then
begin
RecogniseStatement;
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseCaseLabel;
begin
// CaseLabel -> ConstExpr ['..' ConstExpr]
PushNode(nCaseLabel);
RecogniseConstantExpression;
if (fcTokenList.FirstSolidTokenType = ttDoubleDot) then
begin
Recognise(ttDoubleDot);
RecogniseConstantExpression;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseRepeatStmnt;
begin
{ RepeatStmt -> REPEAT Statement UNTIL Expression
Incorect - it is a statement list
}
PushNode(nRepeatStatement);
Recognise(ttRepeat);
RecogniseStatementList([ttUntil]);
Recognise(ttUntil);
PushNode(nLoopHeaderExpr);
RecogniseExpr(True);
PopNode;
PopNode;
end;
procedure TBuildParseTree.RecogniseWhileStmnt;
begin
// WhileStmt -> WHILE Expression DO Statement
PushNode(nWhileStatement);
Recognise(ttWhile);
PushNode(nLoopHeaderExpr);
RecogniseExpr(True);
PopNode;
Recognise(ttDo);
RecogniseStatement;
PopNode;
end;
procedure TBuildParseTree.RecogniseForStmnt;
var
lc: TSourceToken;
begin
{ ForStmt -> FOR QualId ':=' Expression (TO | DOWNTO) Expression DO Statement
or Delphi 2005 syntax:
ForStmt -> FOR QualId 'in' Expression DO Statement
}
PushNode(nForStatement);
Recognise(ttFor);
RecogniseQualId;
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttIn then
begin
// Delphi 2005 syntax
Recognise(ttIn);
RecogniseExpr(True);
end
else
begin
Recognise(ttAssign);
PushNode(nLoopHeaderExpr);
RecogniseExpr(True);
PopNode;
Recognise([ttTo, ttDownto]);
PushNode(nLoopHeaderExpr);
RecogniseExpr(True);
PopNode;
end;
Recognise(ttDo);
RecogniseStatement;
PopNode;
end;
procedure TBuildParseTree.RecogniseWithStmnt;
begin
{ WithStmt -> WITH IdentList DO Statement
it's not an identlist, but an expression list
}
PushNode(nWithStatement);
Recognise(ttWith);
//RecogniseIdentList;
PushNode(nBlockHeaderExpr);
RecogniseExprList;
PopNode;
Recognise(ttDo);
RecogniseStatement;
PopNode;
end;
procedure TBuildParseTree.RecogniseTryStatement;
var
lc: TSourceToken;
begin
{ um. right, I'll have to wing this one
as borland neglected to mention it at all
TryStatement -> 'try' StatementList TryEnd
TryEnd
-> 'finally' StatementList 'end'
-> except ExceptionHandlers 'end'
}
PushNode(nTryAndHandlerBlock);
PushNode(nTryBlock);
Recognise(ttTry);
RecogniseStatementList([ttEnd, ttFinally, ttExcept]);
PopNode;
lc := fcTokenList.FirstSolidToken;
case lc.TokenType of
ttFinally:
begin
PushNode(nFinallyBlock);
Recognise(ttFinally);
RecogniseStatementList([ttEnd]);
Recognise(ttEnd);
PopNode;
end;
ttExcept:
begin
PushNode(nExceptBlock);
Recognise(ttExcept);
RecogniseExceptionHandlerBlock;
// can be statements here - see SF bug 1314607
if fcTokenList.FirstSolidTokenType <> ttEnd then
RecogniseStatementList([ttEnd]);
Recognise(ttEnd);
PopNode;
end
else
raise TEParseError.Create('expected except or finally', lc);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseExceptionHandlerBlock;
begin
{ um. Double-um
can be a statement list
or those 'on Excepttype' thingies
ie
try
...
except
ShowMessage('Foo');
end
or
try
...
except
on TFooException do
ShowMessage('Foo');
on E: TBarException do
ShowMessage('Bar');
else
ShowMessage('Else');
end;
here's the grammar
ExceptionHandlers -> Statement
ExceptionHandlers -> ExceptionSpecifier
}
RecogniseNotSolidTokens;
PushNode(nExceptionHandlers);
if fcTokenList.FirstSolidTokenType in [ttOn, ttElse] then
begin
while fcTokenList.FirstSolidTokenType in [ttOn, ttElse] do
RecogniseExceptionHandler;
end
else
begin
// can be 0 or more statements
RecogniseStatementList([ttEnd]);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseExceptionHandler;
begin
{
ExceptionSpecifier
-> 'on' [ident ':'] ExceptType 'do' Statement
-> 'else' Statement
}
PushNode(nOnExceptionHandler);
if fcTokenList.FirstSolidTokenType = ttElse then
begin
Recognise(ttElse);
RecogniseStatement;
end
else if fcTokenList.FirstSolidTokenType = ttOn then
begin
Recognise(ttOn);
if fcTokenList.SolidTokenType(2) = ttColon then
begin
RecogniseIdentifier(False, idAllowDirectives);
Recognise(ttColon);
end;
RecogniseDottedName;
Recognise(ttDo);
RecogniseNotSolidTokens;
{ special case - empty statement block, go straight on to the else }
if fcTokenList.FirstSolidTokenType <> ttElse then
RecogniseStatement;
end
else
RecogniseStatement;
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon);
PopNode;
RecogniseNotSolidTokens;
end;
procedure TBuildParseTree.RecogniseProcedureDeclSection;
var
lc: TSourceToken;
begin
{
ProcedureDeclSection
-> ProcedureDecl
-> FunctionDecl
}
lc := fcTokenList.FirstSolidToken;
case lc.TokenType of
ttProcedure:
RecogniseProcedureDecl(false);
ttFunction:
RecogniseFunctionDecl(false);
ttConstructor:
RecogniseConstructorDecl;
ttDestructor:
RecogniseDestructorDecl;
ttOperator:
RecogniseOperator(True);
ttClass:
begin
{ class proc or class function
or in delphi.net
class constructor or operator }
case fcTokenList.SolidTokenType(2) of
ttProcedure:
RecogniseProcedureDecl(false);
ttFunction:
RecogniseFunctionDecl(false);
ttConstructor:
RecogniseConstructorDecl;
ttDestructor:
RecogniseDestructorDecl;
ttOperator:
RecogniseClassOperator(True);
else
raise TEParseError.Create('expected class procedure or class function', lc);
end;
end;
else
raise TEParseError.Create('expected procedure or function', lc);
end;
end;
{ the proc/function is forward or extern (ie has no body)
if the word 'forward' or 'extern' is in the directives
these are also valid param names }
function IsForwardExtern(pt: TParseTreeNode): boolean;
var
lcDirectives: TParseTreeNode;
begin
Assert(pt <> nil);
if pt.NodeType in ProcedureNodes then
pt := pt.GetImmediateChild(ProcedureHeadings);
Assert(pt <> nil);
lcDirectives := pt.GetImmediateChild(nProcedureDirectives);
Result := (lcDirectives <> nil) and lcDirectives.HasChildNode([ttExternal, ttForward])
end;
procedure TBuildParseTree.RecogniseProcedureDecl(const pbAnon: boolean);
var
lcTop: TParseTreeNode;
begin
{ ProcedureDecl -> ProcedureHeading ';' [Directive] Block ';'
NB: the block is omitted if there is a 'forward' or external' directive
}
PushNode(nProcedureDecl);
RecogniseProcedureHeading(pbAnon, False);
{ the ';' is ommited by lazy programmers in some rare occasions}
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon);
RecogniseNotSolidTokens;
{ if the proc declaration has the directive external or forward,
it will not have a body
note that though 'forward' is a spectacularly unfortunate variable name,
it has happened, e.g. in ActnMenus.pas }
lcTop := TParseTreeNode(fcStack.Peek);
if not IsForwardExtern(lcTop) then
begin
RecogniseBlock;
if (not pbAnon) and (fcTokenList.FirstSolidTokenType = ttSemiColon) then
begin
Recognise(ttSemicolon);
end;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseFunctionDecl(const pbAnon: boolean);
var
lcTop: TParseTreeNode;
begin
// ProcedureDecl -> FunctionHeading ';' [Directive] Block ';'
PushNode(nFunctionDecl);
RecogniseFunctionHeading(pbAnon, False);
{ the ';' is ommited by lazy programmers in some rare occasions}
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon);
//opt
if fcTokenList.FirstSolidTokenType in ProcedureDirectives then
RecogniseProcedureDirectives;
{ if the proc declaration has the directive external or forward,
it will not have a body }
lcTop := TParseTreeNode(fcStack.Peek);
if not IsForwardExtern(lcTop) then
begin
RecogniseBlock;
if (not pbAnon) and (fcTokenList.FirstSolidTokenType = ttSemiColon) then
begin
Recognise(ttSemicolon);
end;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseConstructorDecl;
begin
// ProcedureDecl -> ProcedureHeading ';' [Directive] Block ';'
PushNode(nConstructorDecl);
RecogniseConstructorHeading(False);
Recognise(ttSemicolon);
if fcTokenList.FirstSolidTokenType in ProcedureDirectives then
RecogniseProcedureDirectives;
RecogniseBlock;
Recognise(ttSemicolon);
PopNode;
end;
procedure TBuildParseTree.RecogniseDestructorDecl;
begin
// ProcedureDecl -> ProcedureHeading ';' [Directive] Block ';'
PushNode(nDestructorDecl);
RecogniseDestructorHeading(False);
Recognise(ttSemicolon);
if fcTokenList.FirstSolidTokenType in ProcedureDirectives then
RecogniseProcedureDirectives;
RecogniseBlock;
Recognise(ttSemicolon);
PopNode;
end;
procedure TBuildParseTree.RecogniseFunctionHeading(
const pbAnon, pbCanInterfaceMap: boolean);
begin
// FunctionHeading -> FUNCTION Ident [FormalParameters] ':' (SimpleType | STRING)
PushNode(nFunctionHeading);
// class procs
if fcTokenList.FirstSolidTokenType = ttClass then
Recognise(ttClass);
Recognise(ttFunction);
if not pbAnon then
RecogniseMethodName(False);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseFormalParameters;
{ the colon and type is in fact optional in
- external fns
- when making good on a forward }
if fcTokenList.FirstSolidTokenType = ttColon then
begin
Recognise(ttColon);
PushNode(nFunctionReturnType);
RecogniseType;
PopNode;
end;
RecogniseProcedureDirectives;
if pbCanInterfaceMap and (fcTokenList.FirstSolidTokenType = ttEquals) then
begin
Recognise(ttEquals);
RecogniseIdentifier(False, idAllowDirectives);
end;
PopNode;
RecogniseNotSolidTokens;
end;
procedure TBuildParseTree.RecogniseProcedureHeading(
const pbAnon, pbCanInterfaceMap: boolean);
begin
{ ProcedureHeading -> PROCEDURE Ident [FormalParameters]
can also map to an interface name
e.g.
type
TFoo = class(TObject, IFoo)
public
procedure IFoo.P1 = MyP1;
Procedure MyP1;
end;
Or a constant
}
PushNode(nProcedureHeading);
if fcTokenList.FirstSolidTokenType = ttClass then
Recognise(ttClass);
Recognise(ttProcedure);
if not pbAnon then
RecogniseMethodName(False);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseFormalParameters;
RecogniseProcedureDirectives;
if pbCanInterfaceMap and (fcTokenList.FirstSolidTokenType = ttEquals) then
begin
Recognise(ttEquals);
RecogniseIdentifier(False, idAllowDirectives);
end;
PopNode;
RecogniseNotSolidTokens;
end;
procedure TBuildParseTree.RecogniseFormalParameters;
begin
// FormalParameters -> '(' FormalParm/';'... ')'
PushNode(nFormalParams);
Recognise(ttOpenBracket);
{ funciton Foo(); is accepted so must allow empty brackets }
if fcTokenList.FirstSolidTokenType <> ttCloseBracket then
begin
RecogniseFormalParam;
while fcTokenList.FirstSolidTokenType = ttSemicolon do
begin
Recognise(ttSemicolon);
RecogniseFormalParam;
end;
end;
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseFormalParam;
const
PARAM_PREFIXES: TTokenTypeSet = [ttVar, ttConst, ttConstRef];
begin
PushNode(nFormalParam);
if (fcTokenList.FirstSolidTokenType = ttOpenSquareBracket) then
RecogniseAttributes;
{ FormalParm -> [VAR | CONST | CONSTREF | OUT] Parameter
'out' is different as it is also a param name so this is legal
procedure Foo(out out: integer);
'out' with a comma, colon or ')' directly after is not a prefix, it is a param name
if another name follows it is a prefix
}
if fcTokenList.FirstSolidTokenType in PARAM_PREFIXES then
Recognise(PARAM_PREFIXES)
else if fcTokenList.FirstSolidTokenType = ttOut then
begin
if IsIdentifierToken(fcTokenList.SolidToken(2), idAllowDirectives) then
Recognise(ttOut);
end;
RecogniseParameter;
PopNode;
end;
procedure TBuildParseTree.RecogniseParameter;
var
lbArray: boolean;
begin
{ Parameter
-> IdentList [':' ([ARRAY OF] SimpleType | STRING | FILE)]
-> Ident ':' SimpleType '=' ConstExpr
hard to distinguish these two productions
will go for the superset
-> IdentList [':' ([ARRAY OF] Type) ['=' ConstExpr] ]
Also I think that's broken as the following are legal:
procedure foo(bar: array of file);
procedure foo(bar: array of TMyRecord);
}
lbArray := False;
RecogniseIdentList(False);
if fcTokenList.FirstSolidTokenType = ttColon then
begin
Recognise(ttColon);
if fcTokenList.FirstSolidTokenType = ttArray then
begin
Recognise(ttArray);
Recognise(ttOf);
lbArray := True;
end;
// type is optional in params ie procedure foo(var pp);
if (lbArray) or ( not (fcTokenList.FirstSolidTokenType in
[ttSemicolon, ttCloseBracket])) then
RecogniseType;
if fcTokenList.FirstSolidTokenType = ttEquals then
begin
Recognise(ttEquals);
RecogniseConstantExpression;
end;
end;
end;
procedure TBuildParseTree.RecogniseProcedureDirectives;
var
lbFirstPass: boolean;
begin
{ these are semi-colon separated
want to leave 'Function foo;' as is,
but strip off the '; safecall' off 'Function bar; safecall;'
external is more complex
}
if (fcTokenList.FirstSolidTokenType in ProcedureDirectives) or
((fcTokenList.FirstSolidTokenType = ttSemicolon) and
(fcTokenList.SolidTokenType(2) in ProcedureDirectives)) then
begin
PushNode(nProcedureDirectives);
if fcTokenList.FirstSolidTokenType = ttSemiColon then
Recognise(ttSemiColon);
lbFirstPass := True;
while (fcTokenList.FirstSolidTokenType in ProcedureDirectives) or
((fcTokenList.FirstSolidTokenType = ttSemicolon) and
(fcTokenList.SolidTokenType(2) in ProcedureDirectives)) do
begin
if ( not lbFirstPass) and (fcTokenList.FirstSolidTokenType = ttSemiColon) then
Recognise(ttSemiColon);
case fcTokenList.FirstSolidTokenType of
ttExternal:
begin
RecogniseExternalProcDirective;
end;
ttPublic:
begin
{ Break the loop if we have found a class visibility "public" }
if not RecognisePublicProcDirective then
break;
end;
ttDispId:
begin
Recognise(ttDispId);
RecogniseConstantExpression;
end;
ttMessage:
begin
Recognise(ttMessage);
RecogniseConstantExpression;
end;
ttEnumerator:
begin
Recognise(ttEnumerator);
RecogniseIdentifier(False, idStrict);
end
else
Recognise(ProcedureDirectives);
end;
lbFirstPass := False;
end;
PopNode;
end;
end;
procedure TBuildParseTree.RecogniseExternalProcDirective;
begin
{ right, i'll fake this one
ExternalProcDirective ->
External ["'" libname "'"] ["name" "'" procname "'"]
also allow "index expr"
}
PushNode(nExternalDirective);
Recognise(ttExternal);
if fcTokenList.FirstSolidTokenType = ttName then
begin
Recognise(ttName);
RecogniseConstantExpression;
end
else if fcTokenList.FirstSolidTokenType in (IdentiferTokens + [ttQuotedLiteralString]) then
begin
Recognise((IdentiferTokens + [ttQuotedLiteralString]));
if fcTokenList.FirstSolidTokenType = ttName then
begin
Recognise(ttName);
RecogniseConstantExpression;
end;
end;
if fcTokenList.FirstSolidTokenType = ttIndex then
begin
Recognise(ttIndex);
RecogniseConstantExpression;
end;
PopNode;
end;
function TBuildParseTree.RecognisePublicProcDirective: boolean;
begin
{
PublicProcDirective ->
Public ["name" "'" symname "'"]
}
result:=false;
if TopNode.HasParentNode([nClassBody, nObjectType]) then
exit;
Recognise(ttPublic);
if fcTokenList.FirstSolidTokenType = ttName then
begin
Recognise(ttName);
RecogniseConstantExpression;
end;
result:=true;
end;
procedure TBuildParseTree.RecogniseObjectType;
begin
{ ObjectType -> OBJECT [ObjHeritage] [ObjFieldList] [MethodList] END
arg this is badly broken, need to
}
PushNode(nObjectType);
// optional "packed" on the oject
if fcTokenList.FirstSolidTokenType = ttPacked then
Recognise(ttPacked);
Recognise(ttObject);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseObjHeritage;
// swiped this from the delphi object defs
RecogniseClassBody;
Recognise(ttEnd);
PopNode;
end;
procedure TBuildParseTree.RecogniseObjHeritage;
begin
// ObjHeritage -> '(' QualId ')'
Recognise(ttOpenBracket);
RecogniseQualId;
Recognise(ttCloseBracket);
end;
procedure TBuildParseTree.RecogniseConstructorHeading(const pbDeclaration: boolean);
begin
//ConstructorHeading -> CONSTRUCTOR Ident [FormalParameters]
PushNode(nConstructorHeading);
if fcTokenList.FirstSolidTokenType = ttClass then
Recognise(ttClass);
Recognise(ttConstructor);
RecogniseMethodName( not pbDeclaration);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseFormalParameters;
RecogniseProcedureDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseDestructorHeading(const pbDeclaration: boolean);
begin
//DestructorHeading -> DESTRUCTOR Ident [FormalParameters]
PushNode(nDestructorHeading);
if fcTokenList.FirstSolidTokenType = ttClass then
Recognise(ttClass);
Recognise(ttDestructor);
RecogniseMethodName( not pbDeclaration);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseFormalParameters;
RecogniseProcedureDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseInitSection;
var
lc: TSourceToken;
begin
{
InitSection
-> INITIALIZATION StmtList [FINALIZATION StmtList] END
-> BEGIN StmtList END
-> END
}
lc := fcTokenList.FirstSolidToken;
if lc = nil then
exit;
PushNode(nInitSection);
case lc.TokenType of
ttInitialization:
begin
Recognise(ttInitialization, True);
RecogniseStatementList([ttEnd, ttFinalization]);
if fcTokenList.FirstSolidTokenType = ttFinalization then
begin
Recognise(ttFinalization, True);
RecogniseStatementList([ttEnd]);
end;
Recognise(ttEnd);
end;
ttFinalization:
begin
Recognise(ttFinalization, True);
RecogniseStatementList([ttEnd]);
Recognise(ttEnd);
end;
ttBegin:
begin
Recognise(ttBegin);
RecogniseStatementList([ttEnd]);
Recognise(ttEnd);
end;
ttEnd:
begin
Recognise(ttEnd);
end
else
raise TEParseError.Create('expected initialisation, begin or end', lc);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseClassType;
begin
{
ClassType -> CLASS [ClassHeritage]
[ClassFieldList]
[ClassMethodList]
[ClassPropertyList]
END
This is not right - these can repeat
My own take on this is as follows:
class -> ident '=' 'class' [Classheritage] classbody 'end'
classbody -> clasdeclarations (ClassVisibility clasdeclarations) ...
ClassVisibility -> 'private' | 'protected' | 'public' | 'published' | 'automated'
classdeclarations -> (procheader|fnheader|constructor|destructor|vars|property|) [';'] ...
can also be a forward declaration, e.g.
TFred = class;
or a class ref type
TFoo = class of TBar;
or in delphi.net
TMyClassHelper = class helper for TMyClass
TMyClassHelper2 = class helper(TMyClassHelper) for TMyClass
TSealedClass = class sealed (TMaClass)
TAbstractClass = class abstract (TObject)
}
PushNode(nClassType);
// the class can be prefixed with "packed"
if fcTokenList.FirstSolidTokenType = ttPacked then
Recognise(ttPacked);
Recognise(ttClass);
if fcTokenList.FirstSolidTokenType = ttHelper then
begin
Recognise(ttHelper);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseClassHeritage;
Recognise(ttFor);
RecogniseIdentifier(False, idStrict);
end
else
begin
// delphi.net sealed class
if fcTokenList.FirstSolidTokenType = ttSealed then
Recognise(ttSealed);
// abstract class
if fcTokenList.FirstSolidTokenType = ttAbstract then
Recognise(ttAbstract);
if fcTokenList.FirstSolidTokenType = ttSemicolon then
begin
PopNode;
exit;
end;
if fcTokenList.FirstSolidTokenType = ttOf then
begin
Recognise(ttOf);
RecogniseIdentifier(True, idStrict);
PopNode;
exit;
end;
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseClassHeritage;
end;
// can end here
if fcTokenList.FirstSolidTokenType = ttSemicolon then
begin
PopNode;
exit;
end;
RecogniseClassBody;
Recognise(ttEnd);
RecogniseHintDirectives;
PopNode;
end;
procedure TBuildParseTree.RecogniseClassHeritage;
begin
PushNode(nClassHeritage);
// ClassHeritage -> '(' IdentList ')'
Recognise(ttOpenBracket);
RecogniseHeritageList;
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseClassVisibility;
begin
// ClassVisibility -> [PUBLIC | PROTECTED | PRIVATE | PUBLISHED]
if fcTokenList.FirstSolidTokenType = ttStrict then
begin
// Delphi.net allows "strict private" and "strict protected"
Recognise(ttStrict);
Recognise([ttPrivate, ttProtected]);
end
else
Recognise(ClassVisibility);
end;
procedure TBuildParseTree.RecogniseClassBody;
begin
//ClassBody -> classdeclarations (access classdeclarations) ...
PushNode(nClassBody);
RecogniseClassDeclarations(False);
while (fcTokenList.FirstSolidTokenType in ClassVisibility + [ttStrict, ttClass]) do
begin
PushNode(nClassVisibility);
RecogniseClassVisibility;
RecogniseClassDeclarations(False);
PopNode;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseClassDeclarations(const pbInterface: boolean);
const
// can declare thse things in a class
CLASS_DECL_WORDS = [ttProcedure, ttFunction,
ttConstructor, ttDestructor, ttProperty, ttClass, ttConst, ttType, ttVar];
var
lc: TSourceToken;
lbStarted: boolean;
lbHasTrailingSemicolon: Boolean;
begin
{ this is a superset of delphi.
in dcc these must be ordered vars, then fns then properties
nb this can be empty as in
class TFoo(Tobject)
private
public
end;
or even
class TBar(TObject) end;
classdeclarations -> (procheader|fnheader|constructor|destructor|vars|property|) [';'] ...
This is all the stuff in a class def between different visibility sections
could a procedure, fuction, constructor, destructor, or property
all of which start with the requite word
or it could just be a varaible declaration, which starts with a new var name
addition: must also do class fns and procs,
eg
" class function ClassName: ShortString; "
Delphi .net allows class types to be declared inside other class types
also "var" to introduce variables
}
lbStarted := False;
while (fcTokenList.FirstSolidTokenType in (CLASS_DECL_WORDS + [ttOpenSquareBracket])) or
(fcTokenList.FirstSolidWordType in IdentifierTypes) do
begin
// only make this node if it will have children
if not lbStarted then
PushNode(nClassDeclarations);
lbStarted := True;
lc := fcTokenList.FirstSolidToken;
lbHasTrailingSemicolon := True;
// these end the visibility section
if lc.TokenType in (ClassVisibility + [ttEnd, ttStrict]) then
break;
{ delphi.net attribute applied to the procedure, property or vars }
if lc.TokenType = ttOpenSquareBracket then
begin
RecogniseAttributes();
lc := fcTokenList.FirstSolidToken;
end;
case lc.TokenType of
ttProcedure:
RecogniseProcedureHeading(False, True);
ttFunction:
RecogniseFunctionHeading(False, True);
ttConst:
begin
{ constant in a class are legal in Delphi.net }
RecogniseConstSection(true);
lbHasTrailingSemicolon := False;
end;
ttClass:
begin
{ 'class' must be followed by 'procedure' or 'function'
or in Delphi.Net: "var", "property", "constructor" or "operator"
}
case fcTokenList.SolidTokenType(2) of
ttProcedure:
RecogniseProcedureHeading(False, True);
ttFunction:
RecogniseFunctionHeading(False, True);
ttVar:
RecogniseClassVars;
ttProperty:
begin
RecogniseProperty;
end;
ttConstructor:
RecogniseConstructorHeading(True);
ttDestructor:
RecogniseDestructorHeading(True);
ttOperator:
RecogniseClassOperator(False);
else
raise TEParseError.Create('Expected class procedure or class function', lc);
end;
end;
ttConstructor:
begin
// no constructor on interface
if pbInterface then
raise TEParseError.Create('unexpected token', lc);
RecogniseConstructorHeading(True);
end;
ttDestructor:
begin
// no constructor on interface
if pbInterface then
raise TEParseError.Create('unexpected token', lc);
RecogniseDestructorHeading(True);
end;
ttProperty:
RecogniseProperty;
ttType:
begin
RecogniseTypeSection(true);
lbHasTrailingSemicolon := False;
end;
ttVar:
begin
RecogniseVarSection(True);
lbHasTrailingSemicolon := False;
end;
else
begin
// end of this list with next visibility section or class end?
if lc.TokenType in CLASS_DECL_WORDS + [ttEnd] then
begin
break;
end
// vars start with an identifier
else if lc.TokenType in IdentiferTokens then
begin
// no vars on interface
if pbInterface then
raise TEParseError.Create('unexpected token', lc);
RecogniseVarDecl;
end
else
raise TEParseError.Create('unexpected token', lc);
end;
end;
// semicolon after each def.
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon)
else if lbHasTrailingSemicolon then
{ expect a semicolon on all except the last, or a const or type (already parsed therein ) }
Break;
end;
if lbStarted then
PopNode;
end;
procedure TBuildParseTree.RecogniseProperty;
begin
{PropertyList -> PROPERTY Ident [PropertyInterface] PropertySpecifiers
There is also the syntax of reclaring properties to raise visibility
-> Property Ident;
}
PushNode(nProperty);
// class property
if fcTokenList.FirstSolidTokenType = ttClass then
Recognise(ttClass);
Recognise(ttProperty);
RecogniseIdentifier(False, idAllowDirectives);
{ this is omitted if it is a property redeclaration for visibility raising
in that case it may still have directives and hints }
if fcTokenList.FirstSolidTokenType in [ttColon, ttOpenSquareBracket] then
begin
RecognisePropertyInterface;
end;
RecognisePropertySpecifiers;
RecognisePropertyDirectives;
RecogniseHintDirectives;
PopNode;
end;
procedure TBuildParseTree.RecognisePropertyInterface;
begin
// PropertyInterface -> [PropertyParameterList] ':' Ident
if fcTokenList.FirstSolidTokenType <> ttColon then
RecognisePropertyParameterList;
Recognise(ttColon);
// recongising any type is overkill but hey
RecogniseType;
end;
procedure TBuildParseTree.RecognisePropertyParameterList;
begin
{ PropertyParameterList -> '[' (IdentList ':' TypeId)/';'... ']'
this forgets const and var, e.g.
property ComplexArrayProp[const piIndex: integer; var pcsString: string]: boolean read GetComplexArrayProp ;
}
PushNode(nPropertyParameterList);
Recognise(ttOpenSquareBracket);
repeat
if (fcTokenList.FirstSolidTokenType in [ttConst, ttConstref, ttVar, ttOut]) then
Recognise([ttConst, ttConstref, ttVar, ttOut]);
RecogniseIdentList(False);
Recognise(ttColon);
RecogniseTypeId;
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon)
else
break;
until fcTokenList.FirstSolidTokenType = ttCloseSquareBracket;
Recognise(ttCloseSquareBracket);
PopNode;
end;
procedure TBuildParseTree.RecognisePropertySpecifiers;
var
lc: TSourceToken;
const
PROPERTY_SPECIFIERS: TTokenTypeSet = [ttIndex, ttRead, ttWrite,
ttAdd, ttRemove,
ttStored, ttDefault, ttNoDefault,
ttImplements, ttDispId, ttReadOnly, ttWriteOnly];
begin
{
PropertySpecifiers ->
[INDEX ConstExpr]
[READ Ident]
[WRITE Ident]
[STORED (Ident | Constant)]
[(DEFAULT ConstExpr) | NODEFAULT]
[IMPLEMENTS TypeId]
This is broken in that
- can be more than one of them (and usually are for read and write)
- left out dispid
- left out readonly
- Add and remove for Delphi.net
}
lc := fcTokenList.FirstSolidToken;
while lc.TokenType in PROPERTY_SPECIFIERS do
begin
PushNode(nPropertySpecifier);
case lc.TokenType of
ttIndex:
begin
Recognise(ttIndex);
RecogniseConstantExpression;
end;
ttRead, ttWrite, ttAdd, ttRemove:
begin
Recognise(lc.TokenType);
RecognisePropertyAccess;
end;
ttStored:
begin
Recognise(ttStored);
RecogniseConstantExpression;
end;
ttDefault:
begin
Recognise(ttDefault);
RecogniseConstantExpression;
end;
ttNoDefault:
begin
Recognise(ttNoDefault);
end;
ttImplements:
begin
Recognise(ttImplements);
RecogniseTypeId;
{ can be a lost of them, e.g. "implements foo, bar" }
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseTypeId;
end;
end;
ttDispId:
begin
Recognise(ttDispId);
RecogniseConstantExpression;
end;
ttReadOnly:
begin
Recognise(ttReadOnly);
end;
ttWriteOnly:
begin
Recognise(ttWriteOnly);
end;
else
raise TEParseError.Create('expected proeprty specifier',
fcTokenList.FirstSolidToken);
end;
PopNode;
lc := fcTokenList.FirstSolidToken;
end;
end;
procedure TBuildParseTree.RecognisePropertyAccess;
begin
{ property access is the bit after the "read" or "write" in a property declaration
This is usually just a procedure, function or simple var
but sometimes it is a record or array field, .. or both e.g. "FDummy[0].ERX" }
RecogniseIdentifier(False, idAllowDirectives);
{ array access }
if fcTokenList.FirstSolidTokenType = ttOpenSquareBracket then
begin
Recognise(ttOpenSquareBracket);
// this is evaluated at compile-time, so we expect a constant subscript, e.g. "FDummy[0]"
RecogniseConstantExpression;
Recognise(ttCloseSquareBracket);
end;
{ record field }
if fcTokenList.FirstSolidTokenType = ttDot then
begin
Recognise(ttDot);
// after the dot can be more structure, so recurse
RecognisePropertyAccess;
end
end;
procedure TBuildParseTree.RecogniseInterfaceType;
begin
{
InterfaceType -> INTERFACE [InterfaceHeritage]
[ClassMethodList]
[ClassPropertyList]
END
This is broken
- left out Dispinterface
- left out possible guid
- left out forward declaration e.g. "IFoo = interface; "
}
PushNode(nInterfaceType);
Recognise(InterfaceWords);
if fcTokenList.FirstSolidTokenType = ttSemicolon then
begin
PopNode;
exit;
end;
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseInterfaceHeritage;
if fcTokenList.FirstSolidTokenType = ttOpenSquareBracket then
RecogniseInterfaceGuid;
if fcTokenList.FirstSolidTokenType <> ttEnd then
begin
PushNode(nInterfaceBody);
RecogniseClassDeclarations(True);
PopNode;
end;
Recognise(ttEnd);
PopNode;
end;
procedure TBuildParseTree.RecogniseInterfaceGuid;
begin
// interface guid can be a litteral string, or occasionally a string constant
PushNode(nInterfaceTypeGuid);
Recognise(ttOpenSquareBracket);
if fcTokenList.FirstSolidTokenType = ttQuotedLiteralString then
Recognise(ttQuotedLiteralString)
else
RecogniseIdentifier(False, idStrict);
Recognise(ttCloseSquareBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseInterfaceHeritage;
begin
// InterfaceHeritage -> '(' IdentList ')'
PushNode(nInterfaceHeritage);
Recognise(ttOpenBracket);
RecogniseHeritageList;
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseRequiresClause;
begin
// RequiresClause -> REQUIRES IdentList... ';'
PushNode(nRequires);
Recognise(ttRequires);
RecogniseIdentList(False);
Recognise(ttSemicolon);
PopNode;
end;
procedure TBuildParseTree.RecogniseContainsClause;
begin
// ContainsClause -> CONTAINS IdentList... ';'
{ it's not an ident list it's a unit list can be
"ident1, indent2" etc
or more usually
"ident1 in 'file1.pas',
ident2 in 'file2.pas' " etc}
PushNode(nContains);
Recognise(ttContains);
PushNode(nIdentList);
RecogniseUsesItem(True);
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseUsesItem(True);
end;
PopNode;
Recognise(ttSemicolon);
PopNode;
end;
{ worker for RecogniseIdentList }
procedure TBuildParseTree.RecogniseIdentValue;
begin
if fcTokenList.FirstSolidTokenType = ttEquals then
begin
Recognise(ttEquals);
RecogniseExpr(True);
end;
end;
procedure TBuildParseTree.RecogniseIdentList(const pbCanHaveUnitQualifier: boolean);
begin
{ IdentList -> Ident/','...
now in D6 enum types can have numeric values
e.g. (foo, bar = 3, baz)
}
PushNode(nIdentList);
RecogniseIdentifier(pbCanHaveUnitQualifier, idAllowDirectives);
RecogniseIdentValue;
while fcTokenList.FirstSolidTokenType = ttComma do
begin
Recognise(ttComma);
RecogniseIdentifier(pbCanHaveUnitQualifier, idAllowDirectives);
RecogniseIdentValue;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseConstantExpression;
begin
RecogniseExpr(True);
end;
procedure TBuildParseTree.RecogniseQualId;
begin
{ typecast, e.g. "(x as Ty)"
or just bracketed, as in (x).y();
See TestCastSimple.pas for the heinous examples
QualID ->
-> (Designator)
-> (Designator as type)
-> ident
->(pointervar + expr)
}
if (fcTokenList.FirstSolidTokenType = ttOpenBracket) then
begin
PushNode(nBracketedQual);
Recognise(ttOpenBracket);
RecogniseDesignator;
if (fcTokenList.FirstSolidTokenType = ttAs) then
RecogniseAsCast;
Recognise(ttCloseBracket);
PopNode;
end
else
// a simple ident - e.g. "x"
RecogniseIdentifier(True, idAny);
end;
procedure TBuildParseTree.RecogniseIdentifier(const pbCanHaveUnitQualifier: boolean;
const peStrictness: TIdentifierStrictness);
var
lc: TSourceToken;
begin
lc := fcTokenList.FirstSolidToken;
if not IdentifierNext(peStrictness) then
raise TEParseError.Create('Expected identifier', lc);
PushNode(nIdentifier);
Recognise(IdentiferTokens);
{ tokens can be qualified by a unit name }
if pbCanHaveUnitQualifier and (fcTokenList.FirstSolidTokenType = ttDot) then
begin
Recognise(ttDot);
{ delphi.net can preface the identifier with an '&'
in order to do something obscure with it - make it a literal or something
e.g. "WebRequest.&Create" is not a constructor,
but a C# method called "Create", which is not a reserved word in C#
}
RecognisePossiblyAmpdIdentifier;
end;
PopNode;
end;
{ the name of a procedure/function/constructor can be
a plain name or classname.methodname
or class<generic>.typename }
procedure TBuildParseTree.RecogniseMethodName(const pbClassNameCompulsory: boolean);
var
lbMore: boolean;
begin
if IsSymbolOperator(fcTokenList.FirstSolidToken) then begin
PushNode(nIdentifier);
Recognise(Operators);
PopNode;
exit;
end;
if not (IdentifierNext(idAllowDirectives)) then
raise TEParseError.Create('Expected identifier', fcTokenList.FirstSolidToken);
// a method name is an identifier
PushNode(nIdentifier);
Recognise(IdentiferTokens);
if fcTokenList.FirstSolidTokenType = ttLessThan then
begin
// a generic decl on the method or class
RecogniseGenericType;
end;
if (fcTokenList.FirstSolidTokenType = ttDot) or pbClassNameCompulsory then
begin
lbMore := true;
while lbMore do
begin
Recognise(ttDot);
Recognise(IdentiferTokens + Operators);
if fcTokenList.FirstSolidTokenType = ttLessThan then
begin
// a generic decl on the method in a class
RecogniseGenericType;
end;
{ delphi.net nested types have more than one dot }
lbMore := (fcTokenList.FirstSolidTokenType = ttDot);
end;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseMethodReferenceType;
var
lc: TSourceToken;
begin
PushNode(nMethodReferenceType);
Recognise(ttReference);
Recognise(ttTo);
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttFunction then
begin
RecogniseFunctionHeading(true, false);
end
else if lc.TokenType = ttProcedure then
begin
RecogniseProcedureHeading(true, false);
end
else
begin
raise TEParseError.Create('expected procedure or function', lc);
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseTypeId;
var
lc: TSourceToken;
begin
lc := fcTokenList.FirstSolidToken;
{ a type is an identifier. Or a file or other Reserved word }
if lc.TokenType in BuiltInTypes then
begin
Recognise(BuiltInTypes);
end
else if lc.TokenType = ttFile then
begin
Recognise(ttFile);
end
else if lc.TokenType = ttAmpersand then
begin
RecognisePossiblyAmpdIdentifier;
end
else
begin
{ type can be prefixed with a unit name, e.g. Classes.TList;
or it could be .NET style, e.g. System.Windows.Forms.TextBox }
RecogniseDottedName;
end;
if fcTokenList.FirstSolidTokenType = ttLessThan then
begin
// a use not a decl
RecogniseGenericType;
end;
end;
procedure TBuildParseTree.RecogniseAsmBlock;
begin
PushNode(nAsm);
Recognise(ttAsm);
while fcTokenList.FirstSolidTokenType <> ttEnd do
RecogniseAsmStatement;
Recognise(ttEnd);
PopNode;
end;
procedure TBuildParseTree.RecogniseAsmStatement;
begin
{ um.
AsmStatement
-> [AsmLabel]
-> Opcode [AsmParam] [',' AsmParam]...
NB whitespace is significant, i.e. returns can separate statement
Help says ' semicolons, end-of-line characters, or Delphi comments.'
I know that the help claims that a label is a prefix on a statement,
but a label can be the last thing in an asm block
so that would require a complete statement to consist of
an optional label followed by an optional opcode
Anyway labels are usually placed on a separate line
RET is opcode with no params
}
PushNode(nAsmStatement);
if fcTokenList.FirstSolidTokenType = ttAtSign then
begin
RecogniseAsmLabel(True);
end
else
begin
// apparently you can have a regular colon label in here
CheckLabelPrefix;
RecogniseAsmOpcode;
RecogniseWhiteSpace;
if fcTokenList.FirstSolidTokenType = ttSemiColon then
begin
Recognise(ttSemiColon);
end
else
begin
while not (fcTokenList.FirstTokenType in [ttSemicolon, ttReturn, ttComment, ttEnd]) do
begin
if fcTokenList.FirstSolidTokenType = ttComma then
begin
Recognise(ttComma);
end;
RecogniseAsmParam;
RecogniseWhiteSpace;
if fcTokenList.FirstSolidTokenType = ttEnd then
begin
Break;
end;
if fcTokenList.FirstSolidTokenType = ttSemiColon then
begin
Recognise(ttSemiColon);
break;
end;
end;
end;
end;
PopNode;
end;
{ purpose: to consume white space
make sure that buffertokens(0)
contains a return, comment or solid token }
procedure TBuildParseTree.RecogniseWhiteSpace;
begin
while fcTokenList.FirstTokenType = ttWhiteSpace do
Recognise(ttWhiteSpace);
end;
procedure TBuildParseTree.RecogniseNotSolidTokens;
begin
while (fcTokenList.CurrentTokenIndex < fcTokenList.Count) and
(fcTokenList.FirstTokenType in NotSolidTokens) do
begin
TopNode.AddChild(fcTokenList.Extract);
end;
end;
procedure TBuildParseTree.RecogniseAsmIdent;
var
lc: TSourceToken;
begin
PushNode(nAsmIdent);
{ can contain '@' signs }
lc := fcTokenList.FirstSolidToken;
if not (lc.TokenType in IdentiferTokens + [ttAtSign]) then
raise TEParseError.Create('Expected asm identifier', lc);
while (lc.TokenType in IdentiferTokens + [ttAtSign]) do
begin
Recognise(IdentiferTokens + [ttAtSign]);
{ whitespace ends this so no fcTokenList.FirstSolidToken }
lc := fcTokenList.First;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseAsmOpcode;
begin
{ these are all short (3 chars? 4 chars)
but it's too large a cast and varies from CPU to CPU
so I will not enumerate them all
some overlap with Delphi reserved words
e.g. SHL
}
PushNode(nAsmOpcode);
if IdentifierNext(idStrict) then
RecogniseIdentifier(False, idStrict)
else if WordTypeOfToken(fcTokenList.FirstSolidTokenType) in TextualWordTypes then
// match anything
Recognise(fcTokenList.FirstSolidTokenType)
else
raise TEParseError.Create('Expected asm opcode', fcTokenList.FirstSolidToken);
PopNode;
end;
function IsAsmLabel(const pt: TSourceToken): boolean;
begin
Result := False;
if pt = nil then
exit;
Result := (pt.TokenType in [ttNumber, ttIdentifier, ttAtSign]) or
(pt.WordType in [wtReservedWord, wtReservedWordDirective,
wtBuiltInConstant, wtBuiltInType]);
end;
procedure TBuildParseTree.RecogniseAsmLabel(const pbColon: boolean);
begin
PushNode(nAsmLabel);
Recognise(ttAtSign);
if fcTokenList.FirstSolidTokenType = ttAtSign then
Recognise(ttAtSign);
{ label can be a number, eg "@@1:"
or an identifier that starts with a number, eg "@@2a"
can also be a delphi keyword, e.g. "@@repeat:"
}
while IsAsmLabel(fcTokenList.First) do
begin
Recognise(fcTokenList.FirstTokenType);
end;
if pbColon then
Recognise(ttColon);
PopNode;
end;
procedure TBuildParseTree.RecogniseAsmParam;
const
ASM_EXPRESSION_START = [ttOpenBracket, ttOpenSquareBracket, ttNumber,
ttNot, ttQuotedLiteralString,
ttTrue, ttFalse, ttPlus, ttMinus, ttType, ttOffset,
ttVmtOffset, ttDmtOffset];
var
lc, lcNext: TSourceToken;
lbHasLabel: boolean;
begin
{ um. No formal grammar for these
AsmParam
-> Ident
-> Ident(AsmExpr)
-> '@' Ident
-> '&' Ident
-> '[' AsmExpr ']'
}
lbHasLabel := False;
PushNode(nAsmParam);
lc := fcTokenList.FirstSolidToken;
if lc.TokenType = ttAtSign then
begin
RecogniseAsmLabel(False);
lbHasLabel := True;
if fcTokenList.FirstSolidTokenType = ttDot then
Recognise(ttDot);
end;
if lc.TokenType = ttAmpersand then
begin
Recognise(ttAmpersand);
end;
{ only parse trailing expressions if it is on the same line
Asm is not completely white-space-independant }
lcNext := fcTokenList.FirstTokenWithExclusion([ttWhiteSpace]);
if (lcNext <> nil) and (lcNext.TokenType <> ttReturn) then
begin
if IdentifierNext(idAllowDirectives) or (lc.TokenType in ASM_EXPRESSION_START) then
begin
RecogniseAsmExpr;
end
else
begin
if not lbHasLabel then
raise TEParseError.Create('Expected asm param', lc);
end;
end;
PopNode;
end;
const
ASM_OPERATORS = [ttPlus, ttMinus, ttAnd, ttOr, ttTimes, ttFloatDiv, ttPtr, ttColon];
{ having to wing this one. it is like expressions, but different }
procedure TBuildParseTree.RecogniseAsmExpr;
var
lc: TSourceToken;
begin
RecogniseAsmFactor;
{ can't go past returns }
lc := fcTokenList.FirstTokenWithExclusion([ttWhiteSpace]);
while lc.TokenType in ASM_OPERATORS do
begin
RecogniseAsmOperator;
RecogniseAsmFactor;
lc := fcTokenList.FirstTokenWithExclusion([ttWhiteSpace]);
end;
end;
procedure TBuildParseTree.RecogniseAsmOperator;
begin
Recognise(ASM_OPERATORS);
end;
procedure TBuildParseTree.RecogniseAsmFactor;
var
lcNext: TSourceToken;
lcLastChar: Char;
begin
if fcTokenList.FirstSolidTokenType = ttNot then
Recognise(ttNot);
if fcTokenList.FirstSolidTokenType = ttMinus then
Recognise(ttMinus);
if fcTokenList.FirstSolidTokenType = ttAt then
Recognise(ttAt);
if fcTokenList.FirstSolidTokenType = ttType then
Recognise(ttType);
if fcTokenList.FirstSolidTokenType = ttOffset then
Recognise(ttOffset);
if fcTokenList.FirstSolidTokenType in AsmOffsets then
Recognise(AsmOffsets);
case fcTokenList.FirstSolidTokenType of
ttNumber:
begin
Recognise(ttNumber);
// numbers in Asm blocks can be suffixed with 'h' for hex
// there could be unanounced hex digits before the 'h'
lcNext := fcTokenList.FirstSolidToken;
if (lcNext.TokenType = ttIdentifier) then
begin
lcLastChar := lcNext.SourceCode[Length(lcNext.SourceCode)];
if (lcLastChar = 'h') then
begin
Recognise(ttIdentifier);
end;
end;
end;
ttQuotedLiteralString:
Recognise(ttQuotedLiteralString);
ttTrue:
Recognise(ttTrue);
ttFalse:
Recognise(ttFalse);
ttOpenBracket:
begin
Recognise(ttOpenBracket);
RecogniseAsmExpr;
Recognise(ttCloseBracket);
end;
ttOpenSquareBracket:
begin
Recognise(ttOpenSquareBracket);
RecogniseAsmExpr;
Recognise(ttCloseSquareBracket);
end;
ttComma, ttSemicolon:
begin
// expression over, go home
// can be caused by bug 1933836 - the unary operator was actually a var name
end
else
begin
RecogniseAsmIdent;
end
end;
while fcTokenList.FirstSolidTokenType in [ttDot, ttOpenBracket, ttOpenSquareBracket] do
begin
if fcTokenList.FirstSolidTokenType = ttDot then
begin
Recognise(ttDot);
if fcTokenList.FirstSolidTokenType = ttAtSign then
Recognise(ttAtSign);
RecogniseAsmIdent;
end;
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
begin
Recognise(ttOpenBracket);
RecogniseAsmFactor;
Recognise(ttCloseBracket);
end;
if fcTokenList.FirstSolidTokenType = ttOpenSquareBracket then
begin
Recognise(ttOpenSquareBracket);
RecogniseAsmExpr;
Recognise(ttCloseSquareBracket);
end;
end;
end;
procedure TBuildParseTree.RecogniseHintDirectives;
begin
if ((fcTokenList.FirstSolidTokenType = ttSemicolon) and
(fcTokenList.SolidTokenType(2) in HintDirectives)) or
(fcTokenList.FirstSolidTokenType in HintDirectives) then
begin
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon);
PushNode(nHintDirectives);
while (fcTokenList.FirstSolidTokenType in HintDirectives) do
begin
Recognise(HintDirectives);
end;
PopNode;
end;
end;
procedure TBuildParseTree.RecognisePropertyDirectives;
const
{ this can be specified at the end after a semicolon
so it's not just in the specifiers
the default directive works differently for array and not-array properties
for non-array properties it is followed by an identifier
}
PropertyDirectives = [ttDefault, ttNoDefault, ttStored, ttEnumerator];
begin
if ((fcTokenList.FirstSolidTokenType = ttSemicolon) and
(fcTokenList.SolidTokenType(2) in PropertyDirectives)) or
(fcTokenList.FirstSolidTokenType in PropertyDirectives) then
begin
if fcTokenList.FirstSolidTokenType = ttSemicolon then
Recognise(ttSemicolon);
while fcTokenList.FirstSolidTokenType in PropertyDirectives do
begin
PushNode(nPropertyDirective);
case fcTokenList.FirstSolidTokenType of
ttDefault:
begin
Recognise(ttDefault);
if fcTokenList.FirstSolidTokenType <> ttSemicolon then
RecogniseConstantExpression;
end;
ttNoDefault:
begin
Recognise(ttNoDefault);
end;
ttStored:
begin
Recognise(ttStored);
if fcTokenList.FirstSolidTokenType <> ttSemicolon then
RecogniseConstantExpression;
end;
ttEnumerator:
begin
Recognise(ttEnumerator);
RecogniseIdentifier(False, idStrict);
end;
end;
PopNode;
end;
end;
end;
procedure TBuildParseTree.RecogniseExportsSection;
begin
PushNode(nExports);
Recognise(ttExports);
RecogniseExportedProc;
// more to come?
while fcTokenList.FirstSolidTokenType <> ttSemicolon do
begin
Recognise(ttComma);
RecogniseExportedProc;
end;
Recognise(ttSemicolon);
PopNode;
end;
procedure TBuildParseTree.RecogniseExportedProc;
const
ExportedDirectives: TTokenTypeSet = [ttName, ttIndex, ttResident];
var
lc: TSourceToken;
begin
PushNode(nExportedProc);
RecogniseIdentifier(True, idAllowDirectives);
if fcTokenList.FirstSolidTokenType = ttOpenBracket then
RecogniseFormalParameters;
while fcTokenList.FirstSolidTokenType in ExportedDirectives do
begin
lc := fcTokenList.FirstSolidToken;
case lc.TokenType of
ttName:
begin
Recognise(ttName);
Recognise(IdentiferTokens + [ttQuotedLiteralString]);
end;
ttIndex:
begin
Recognise(ttIndex);
Recognise(ttNumber);
end;
ttResident:
Recognise(ttResident);
else
raise TEParseError.Create('Expected export directive', lc);
end;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseActualParams;
const
SKIP_PARAM: TTokenTypeSet = [ttComma, ttCloseBracket];
var
lbMore: boolean;
liParamsRecognised: integer;
begin
PushNode(nActualParams);
Recognise(ttOpenBracket);
liParamsRecognised := 0;
if fcTokenList.FirstSolidTokenType <> ttCloseBracket then
begin
//RecogniseExprList;
repeat
{ SF Bug 1311753
- end param can be empty, as in "GetBitmap(1, bitmap, );"
this is the case when
- not first param
- next solid token is comma or close brackets
}
if (liParamsRecognised = 0) or (not (fcTokenList.FirstSolidTokenType in SKIP_PARAM)) then
RecogniseActualParam;
inc(liParamsRecognised);
lbMore := fcTokenList.FirstSolidTokenType = ttComma;
if lbMore then
Recognise(ttComma);
until not lbMore;
end;
Recognise(ttCloseBracket);
PopNode;
end;
procedure TBuildParseTree.RecogniseActualParam;
const
EXPR_TYPES = [ttNumber, ttIdentifier, ttQuotedLiteralString,
ttPlus, ttMinus, ttOpenBracket, ttOpenSquareBracket, ttNot, ttInherited];
var
lc: TSourceToken;
begin
lc := fcTokenList.FirstSolidToken;
{ all kinds of reserved words can sometimes be param names
thanks to COM and named params
See LittleTest43.pas }
if ( not (lc.TokenType in EXPR_TYPES)) and StrIsAlphaNum(lc.SourceCode) and
( not IsIdentifierToken(lc, idAllowDirectives)) then
begin
{ TridenT - test if token is the Reserved word ARRAY
Sample Delphi2005 syntax :
TbObj:= New(array[] of TObject, (S1, I, D1, D2, Etat, S2));
}
if lc.TokenType = ttArray then
begin
RecogniseArrayType;
end
else if AnonymousMethodNext then
begin
RecogniseAnonymousMethod;
end
else
begin
{ quick surgery. Perhaps even a hack -
reclasify the token, as it isn't what it thinks it is
e.g. if this word is 'then', then
we don't want a linbreak after it like in if statements }
lc.TokenType := ttIdentifier;
Recognise(ttIdentifier);
{ this must be a named value, e.g. "end = 3". See LittleTest43.pas for e.g.s }
Recognise(ttAssign);
RecogniseExpr(True);
end;
end
else if lc.TokenType = ttComma then
begin
{ See TestOleParam: "WordApp.Documents.Open('foo',,,,'bar');"
params can be skipped
I guess a missing param has a default value
}
end
else
begin
RecogniseExpr(True);
{ ole named param syntax, e.g.
" MSWord.TextToTable(ConvertFrom := 2, NumColumns := 3);"
}
if fcTokenList.FirstSolidTokenType = ttAssign then
begin
Recognise(ttAssign);
RecogniseExpr(True);
end
{ str width specifiers e.g. " Str(val:0, S);" this is an odd wart on the syntax }
else if fcTokenList.FirstSolidTokenType = ttColon then
begin
{ can be more than one of them }
while fcTokenList.FirstSolidTokenType = ttColon do
begin
Recognise(ttColon);
RecogniseExpr(True);
end;
end;
end;
end;
function TBuildParseTree.AnonymousMethodNext: boolean;
var
lc, lcNext: TSourceToken;
begin
Result := False;
lc := fcTokenList.FirstSolidToken;
if lc.TokenType in [ttProcedure, ttFunction] then
begin
lcNext := fcTokenList.SolidToken(2);
if lcNext <> nil then
Result := (lcNext.TokenType in [ttOpenBracket, ttColon]);
end;
end;
procedure TBuildParseTree.RecogniseLiteralString;
begin
RecogniseNotSolidTokens;
PushNode(nLiteralString);
while fcTokenList.FirstTokenType in LiteralStringStarters do
begin
case fcTokenList.FirstTokenType of
ttQuotedLiteralString:
begin
Recognise(ttQuotedLiteralString);
end;
ttHat:
begin
Recognise(ttHat);
// followed by any single char token
if fcTokenList.FirstTokenLength = 1 then
Recognise(fcTokenList.FirstTokenType)
else
raise TEParseError.Create('Unexpected token, expected single char after ^', fcTokenList.FirstSolidToken);
end;
ttHash:
begin
Recognise(ttHash);
Recognise(ttNumber);
end;
end;
end;
PopNode;
end;
procedure TBuildParseTree.RecogniseAsCast;
begin
Recognise(ttAs);
RecogniseIdentifier(True, idStrict);
end;
procedure TBuildParseTree.RecogniseAttributes;
begin
repeat
PushNode(nAttribute);
{ Delphi.Net syntax for metadata in square brackets }
Recognise(ttOpenSquareBracket);
while fcTokenList.FirstTokenType <> ttCloseSquareBracket do
Recognise(fcTokenList.FirstTokenType);
Recognise(ttCloseSquareBracket);
PopNode;
RecogniseNotSolidTokens;
until fcTokenList.FirstTokenType <> ttOpenSquareBracket;
end;
end.
|