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
|
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.
Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.
File: ParserTreeWalker.cpp
Responsibility: Sharon Correll
Last reviewed: Not yet.
Description:
Methods to implement the parser, that is, those that call the ANTLR parser and then
walk the syntax tree to create the objects (Gdl and Grc).
-------------------------------------------------------------------------------*//*:End Ignore*/
/***********************************************************************************************
Include files
***********************************************************************************************/
#include "Grp.h"
#include "main.h"
#ifdef _WIN32
#include <io.h> // needed for _dup & _dup2
#else
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#endif
#ifdef _MSC_VER
#pragma hdrstop
#endif
#undef THIS_FILE
DEFINE_THIS_FILE
/***********************************************************************************************
Forward declarations
***********************************************************************************************/
using std::cout;
using std::endl;
/***********************************************************************************************
Local Constants and static variables
***********************************************************************************************/
/***********************************************************************************************
Methods
***********************************************************************************************/
/*----------------------------------------------------------------------------------------------
Run the parser over the file with the given name. Record an error if the file does not
exist.
----------------------------------------------------------------------------------------------*/
bool GrcManager::Parse(std::string staFileName, std::string staGdlppFile,
std::string staOutputPath)
{
std::string staFilePreProc;
if (!RunPreProcessor(staFileName, &staFilePreProc, staGdlppFile, staOutputPath))
{
return false;
}
std::ifstream strmIn;
strmIn.open(staFilePreProc.c_str());
if (strmIn.fail())
{
g_errorList.AddError(1105, NULL,
"File ",
staFileName,
" does not exist--compilation aborted");
return false;
}
return ParseFile(strmIn, staFilePreProc);
}
/*----------------------------------------------------------------------------------------------
Run the C pre-processor over the GDL file. Return the name of the resulting file.
----------------------------------------------------------------------------------------------*/
bool GrcManager::RunPreProcessor(std::string staFileName, std::string * pstaFilePreProc,
#ifdef _WIN32
std::string & staGdlppFile, std::string & staOutputPath)
#else
std::string & staGdlppFile, std::string &)
#endif
{
#ifdef _WIN32
STARTUPINFO sui = {sizeof(sui)};
//Apparently not needed for DOS programs:
//sui.wShowWindow = SW_HIDE;
//sui.dwFlags = STARTF_USESHOWWINDOW;
// create temporary file and redirect stderr into it
// ENHANCE AlanW: do this properly? - learn about NT child process inheritance, pipe creation,
// and security
// Search MSDN for "Creating a Child Process with Redirected Input and Output" for
// more proper way. Using the C RTL is much easier than the proper way with the Win API.
auto cchOutputPath = strlen(staOutputPath.data());
auto cchPreProcErr = cchOutputPath + strlen(_T("\\$_gdlpp_stderr.txt")) + 1;
char * pszPreProcErr = new char[cchPreProcErr];
memset(pszPreProcErr, 0, cchPreProcErr);
if (!pszPreProcErr)
{
g_errorList.AddError(1106, NULL, "Out of memory");
return false;
}
strcpy(pszPreProcErr, staOutputPath.c_str());
if (cchOutputPath > 0)
{strcpy(pszPreProcErr + cchOutputPath, _T("\\$_gdlpp_stderr.txt"));}
else
{strcpy(pszPreProcErr + cchOutputPath, _T("$_gdlpp_stderr.txt"));}
FILE * pFilePreProcErr = fopen(pszPreProcErr, "w+");
if (!pFilePreProcErr)
{
g_errorList.AddError(1107, NULL, "Could not create temporary file to run pre-processor: ", pszPreProcErr);
delete[] pszPreProcErr;
return false;
}
int nOrigStderr = _dup(2); // save original stderr, 2 is stderr file handle
if (-1 == _dup2(_fileno(pFilePreProcErr), 2)) //stderr now refers to tmp file opened above
{
g_errorList.AddError(1108, NULL, "Could not redirect stderr.");
}
PROCESS_INFORMATION procinfo = {0};
achar rgchErrorCode[20];
// switch backslash path separators to forward slash for gdlpp
std::replace(staFileName.begin(), staFileName.end(), '\\', '/');
std::string strCommandLine(_T("\""));
strCommandLine += staGdlppFile;
if (m_fVerbose)
strCommandLine += _T("\" -V \"");
else
strCommandLine += _T("\" \"");
strCommandLine += staFileName;
strCommandLine += _T("\"");
strCommandLine += _T(" $_temp.gdl"); // output file
// needs to be changed to achar but we then need to make sure that this works with
// memset and memcpy. This has the possibility of creating a very nasty bug
achar rgchCommandLine[200];
_tcscpy(rgchCommandLine, strCommandLine.data());
//memset(rgchCommandLine, 0, 200);
//memcpy(rgchCommandLine, staCommandLine.Chars(), staCommandLine.Length());
BOOL f = CreateProcess(NULL,
rgchCommandLine,
NULL, NULL, TRUE, 0, NULL, NULL,
&sui, &procinfo);
DWORD d = GetLastError();
if (f == FALSE)
{
_itot((int)d, rgchErrorCode, 10);
g_errorList.AddWarning(1502, NULL,
"Could not create process to run pre-processor gdlpp.exe (error = ",
rgchErrorCode,
"); compiling ",
staFileName);
*pstaFilePreProc = staFileName;
return true;
}
HANDLE h = procinfo.hProcess;
WaitForSingleObject(h, INFINITE);
// read any errors from gdlpp
fflush(pFilePreProcErr);
if (fseek(pFilePreProcErr, 0, SEEK_SET)) // returns 0 on success
{
g_errorList.AddError(1109, NULL, "Error in pre-processor temporary file");
return false;
}
RecordPreProcessorErrors(pFilePreProcErr);
// clean up stderr file & delete tmp file used to catch errors
if (-1 == _dup2(nOrigStderr, 2)) // restore stderr
{
g_errorList.AddError(1110, NULL, "Could not restore stderr from being redirected.");
}
fclose(pFilePreProcErr);
unlink(pszPreProcErr);
delete [] pszPreProcErr;
ULONG exitCode = 0;
GetExitCodeProcess(h, &exitCode);
if (exitCode != 0)
{
GrpLineAndFile lnf(0, kMaxFileLineNumber, ""); // make this message come last
_itot((int)exitCode, rgchErrorCode, 10);
g_errorList.AddError(1111, NULL,
"Fatal error in pre-processor gdlpp.exe--compilation aborted",
lnf);
g_errorList.SetLastMsgIncludesFatality(true);
return false;
}
*pstaFilePreProc = "$_temp.gdl";
#else
char tmpgdl[15] = "/tmp/gdlXXXXXX";
if (mkstemp(tmpgdl)==-1)
{
g_errorList.AddError(1112, NULL,
"Could not create temp file to run pre-processor: ", staGdlppFile,
" compiling ",
staFileName);
return false;
}
pid_t pid;
int status, died, testexec;
switch (pid = fork())
{
case -1:
cout << "Can't fork pre-processor: " << strerror(errno) << "\n";
exit(-1);
case 0 :
// In child process
if (m_fVerbose)
testexec = execlp(staGdlppFile.c_str(), staGdlppFile.c_str(),
"-V", staFileName.c_str(), tmpgdl, NULL);
else
testexec = execlp(staGdlppFile.c_str(), staGdlppFile.c_str(),
staFileName.c_str(), tmpgdl, NULL);
cout << "exec failed - retval: " << testexec
<< ", errno: " << strerror(errno) << " (" << errno << ")\n";
cout << "tmpfile " << tmpgdl << endl;
cout << "file " << staFileName.c_str() << endl;
exit(-2);
default:
// In parent process
died = waitpid(pid, &status, 0); // this is the code the parent runs
if (WIFSIGNALED(status))
cout << "Pre-processor died with signal " << WTERMSIG(status) << "\n";
else if (WIFSTOPPED(status))
cout << "Pre-processor stopped with signal " << WSTOPSIG(status) << "\n";
else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
cout << "Pre-processor exited with result " << WEXITSTATUS(status) << "\n";
else
break;
g_errorList.AddError(1113, NULL,
"Failed to run pre-processor: ", staGdlppFile,
" compiling ",
staFileName);
return false;
}
*pstaFilePreProc = tmpgdl;
#endif
return true;
}
/*----------------------------------------------------------------------------------------------
Process the output of the pre-processor--parse any error messages and record them in
the standard way.
----------------------------------------------------------------------------------------------*/
void GrcManager::RecordPreProcessorErrors(FILE * pFilePreProcErr)
{
schar rgch[4096];
size_t cbRead;
cbRead = fread(rgch, 1, 4096, pFilePreProcErr);
schar * pch = rgch;
// Skip the first 4 lines--copyright info, etc.
unsigned int nLinesSkipped = 0;
while (nLinesSkipped < 2 && (unsigned int)(pch - rgch) < cbRead)
{
if (*pch == 0x0A)
{
nLinesSkipped++;
// pch++;
}
pch++;
}
// example of line to parse:
// cpp: "PigLatinInput.gdl", line 17: Error: #endif must be in an #if
schar * pchFileMin;
schar * pchFileLim;
schar * pchLineNoMin;
schar * pchLineNoLim;
schar * pchMsgMin;
schar * pchMsgLim;
int nLineNo;
//std::string staMsg;
GrpLineAndFile lnf;
lnf.SetPreProcessedLine(0);
while ((unsigned int)(pch - rgch) < cbRead)
{
// pchFileMin = pch + strlen(_T("cpp: \"")); _T is undefined on Linux
pchFileMin = pch + 6;
// Assert(strcmp(pch, _T("cpp: \"")));
Assert((*pch == 'c' && *(pch + 1) == 'p' && *(pch + 2) == 'p' &&
*(pch + 3) == ':' && *(pch + 4) == ' ' && *(pch + 5) == '\"'));
pch = pchFileMin;
while (*pch != '\"')
{
pch++;
if ((unsigned int)(pch - rgch) >= cbRead)
{
Assert(false);
goto LNextLine;
}
}
pchFileLim = pch;
pch++; // skip the '"'
// pchLineNoMin = pch + strlen(_T(", line "));
pchLineNoMin = pch + 7;
while (*pch != ':')
{
pch++;
if ((unsigned int)(pch - rgch) >= cbRead)
{
Assert(false);
goto LNextLine;
}
}
pchLineNoLim = pch;
pch++; // skip the ':'
Assert(*pch == ' ');
while (*pch == ' ')
pch++;
bool fFatal;
if (*pch == 'W')
{
Assert(*(pch+1) == 'a');
Assert(*(pch+2) == 'r');
fFatal = false;
}
else
{
Assert((*pch == 'E' && *(pch+1) == 'r' && *(pch+2) == 'r') || // Error
(*pch == 'F' && *(pch+1) == 'a' && *(pch+2) == 't')); // Fatal error
fFatal = true;
}
pchMsgMin = pch;
while (*pch != 0x0A && (unsigned int)(pch - rgch) < cbRead)
pch++;
pchMsgLim = pch;
// Record error message.
nLineNo = atoi((const char *)pchLineNoMin);
lnf.SetFile(std::string(pchFileMin, pchFileLim - pchFileMin));
lnf.SetOriginalLine(nLineNo);
if (fFatal)
g_errorList.AddError(1114, NULL,
"Gdlpp.exe ",
std::string(pchMsgMin, pchMsgLim - pchMsgMin),
lnf);
else
g_errorList.AddWarning(1503, NULL,
"Gdlpp.exe ",
std::string(pchMsgMin, pchMsgLim - pchMsgMin),
lnf);
g_errorList.SetLastMsgIncludesFatality(true);
LNextLine:
while (*pch != 0x0A && (unsigned int)(pch - rgch) < cbRead)
pch++;
pch++;
}
}
/*----------------------------------------------------------------------------------------------
Given the name of a file (possibly including path) answer the name of the corresponding
file output by the pre-processor (not including path). If the name of the file is
'abc.gdl', the name of outfile file will be 'abc.i'.
OBSOLETE now that we are not using CL.EXE.
----------------------------------------------------------------------------------------------*/
std::string GrcManager::PreProcName(std::string sta)
{
size_t ichMin;
size_t ichLim = sta.length();
for ( ; ichLim >= 0 && sta[ichLim] != '\\' && sta[ichLim] != '.'; ichLim--)
;
if (sta[ichLim] == '.')
{
for (ichMin = ichLim; ichMin >= 0 && sta[ichMin] != '\\'; ichMin--)
;
ichMin++;
}
else
{
ichMin = ichLim;
ichLim = sta.length();
}
char rgch[100];
memset(rgch, 0, 100);
memcpy(rgch, sta.data() + ichMin, (ichLim - ichMin));
std::string staRet(rgch);
staRet += ".i";
return staRet;
}
/*----------------------------------------------------------------------------------------------
Run the parser over the input file, generating an ANTLR tree, then walk the tree to
extract the associated data.
Assumes the input file exists and has been successfully opened.
----------------------------------------------------------------------------------------------*/
bool GrcManager::ParseFile(std::ifstream & strmIn, std::string staFileName)
{
try
{
GrpLexer lexer(strmIn);
GrpTokenStreamFilter tsf(lexer);
lexer.init(tsf);
tsf.init(staFileName);
GrpParser parser(tsf);
parser.init(tsf);
parser.renderDescription();
RefAST ast = parser.getAST();
if (g_errorList.AnyFatalErrors())
{
if (ast != NULL)
ast->iterativeRemoveChildren(true); // to avoid stack overflows
return false;
}
InitPreDefined();
WalkParseTree(ast);
ast->iterativeRemoveChildren(true); // to avoid stack overflows
}
catch(ANTLRException & e)
{
// Handle exceptions that happen during parsing.
g_errorList.AddError(1115, NULL,
e.getMessage().c_str());
//e.getLine());
}
return true;
}
/*----------------------------------------------------------------------------------------------
Initialize the data structures with pre-defined stuff. Specifically the ANY class.
----------------------------------------------------------------------------------------------*/
void GrcManager::InitPreDefined()
{
AddGlyphClass(GrpLineAndFile(), "ANY");
}
/*----------------------------------------------------------------------------------------------
Walk the parse tree to extract the data and fill in the GdlRenderer.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkParseTree(RefAST ast)
{
if (ast == NULL)
{
g_errorList.AddError(1116, NULL,
"Invalid input file--compilation aborted");
return;
}
if (OutputDebugFiles())
DebugParseTree(ast);
Assert(ast->getType() == Ztop);
if (ast->getType() != Ztop)
return;
WalkTopTree(ast);
Assert(m_venv.size() == 1);
}
/*----------------------------------------------------------------------------------------------
Process the entire parse tree.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkTopTree(RefAST ast)
{
Assert(ast->getType() == Ztop);
Assert(ast->getNextSibling() == NULL);
RefAST astChild = ast->getFirstChild();
while (astChild)
{
int nodetyp = astChild->getType();
switch (nodetyp)
{
case LITERAL_environment:
WalkEnvTree(astChild, ktbltNone, NULL, NULL);
break;
case LITERAL_table:
WalkTableTree(astChild);
break;
case OP_EQ:
case OP_PLUSEQUAL:
ProcessGlobalSetting(astChild);
break;
default:
Assert(false);
}
astChild = astChild->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Process a global setting statement.
Review: should we also allow directives?
----------------------------------------------------------------------------------------------*/
void GrcManager::ProcessGlobalSetting(RefAST ast)
{
Assert(ast->getType() == OP_EQ || ast->getType() == OP_PLUSEQUAL);
RefAST astName = ast->getFirstChild();
Assert(astName);
std::string staName = astName->getText();
Symbol psym = SymbolTable()->FindSymbol(staName);
if (!psym || !psym->FitsSymbolType(ksymtGlobal))
{
g_errorList.AddError(1117, NULL,
"Invalid global: ",
staName,
LineAndFile(ast));
return;
}
RefAST astValue = astName->getNextSibling();
Assert(astValue);
if (staName == "AutoPseudo")
{
if (ast->getType() == OP_PLUSEQUAL)
{
g_errorList.AddError(1118, NULL,
"Inappropriate use of += operator",
LineAndFile(ast));
return;
}
if (astValue->getType() == LITERAL_true)
m_prndr->SetAutoPseudo(true);
else if (astValue->getType() == LITERAL_false)
m_prndr->SetAutoPseudo(false);
else if (astValue->getType() == LIT_INT)
{
int nValue;
bool fM;
nValue = NumericValue(astValue, &fM);
if (fM)
g_errorList.AddError(1119, NULL,
"The AutoPseudo global does not expect a scaled number",
LineAndFile(astValue));
else
m_prndr->SetAutoPseudo(nValue != 0);
}
if (astValue->getNextSibling())
g_errorList.AddError(1120, NULL,
"The AutoPseudo global cannot take multiple values",
LineAndFile(ast));
}
else if (staName == "Bidi")
{
if (ast->getType() == OP_PLUSEQUAL)
{
g_errorList.AddError(1121, NULL,
"Inappropriate use of += operator",
LineAndFile(ast));
return;
}
if (astValue->getType() == LITERAL_true)
m_prndr->SetBidi(1);
else if (astValue->getType() == LITERAL_false)
m_prndr->SetBidi(0);
else if (astValue->getType() == LIT_INT)
{
int nValue;
bool fM;
nValue = NumericValue(astValue, &fM);
if (fM)
g_errorList.AddError(1122, NULL,
"The Bidi global does not expect a scaled number",
LineAndFile(astValue));
else
{
if (nValue != 0 && nValue != 1 && nValue != 2)
g_errorList.AddWarning(1512, NULL,
"Non-boolean value for the Bidi global; will be set to true",
LineAndFile(astValue));
m_prndr->SetBidi(nValue);
}
}
if (astValue->getNextSibling())
g_errorList.AddError(1123, NULL,
"The Bidi global cannot take multiple values",
LineAndFile(ast));
}
else if (staName == "ExtraAscent" || staName == "ExtraDescent")
{
bool fDescent = (staName == "ExtraDescent");
GdlNumericExpression * pexpOld
= (fDescent) ? m_prndr->ExtraDescent() : m_prndr->ExtraAscent();
if (ast->getType() == OP_EQ)
{
if (pexpOld)
{
g_errorList.AddWarning(1504, NULL,
"The ", staName, " global setting overrode a previous value",
LineAndFile(ast));
delete pexpOld;
}
(fDescent) ? m_prndr->SetExtraDescent(NULL) : m_prndr->SetExtraAscent(NULL);
pexpOld = NULL;
}
if (astValue->getType() != LIT_INT)
{
g_errorList.AddError(1124, NULL,
"Invalid value for ", staName, " global",
LineAndFile(ast));
}
else
{
int nValue;
bool fM;
nValue = NumericValue(astValue, &fM);
if (!fM)
g_errorList.AddWarning(1505, NULL,
"The ", staName, " global setting expects a scaled number",
LineAndFile(astValue));
if (pexpOld && (pexpOld->Units() != MUnits()))
{
g_errorList.AddError(1125, NULL,
"Cannot combine the new value of the ", staName,
"global with the previous because the units are different",
LineAndFile(ast));
}
else
{
GdlNumericExpression * pexpNew;
if (fM)
pexpNew = new GdlNumericExpression(nValue, MUnits());
else
pexpNew = new GdlNumericExpression(nValue);
(fDescent) ? m_prndr->SetExtraDescent(pexpNew) : m_prndr->SetExtraAscent(pexpNew);
if (pexpOld)
delete pexpOld;
}
}
if (astValue->getNextSibling())
g_errorList.AddError(1126, NULL,
"The ", staName, " global cannot take multiple values",
LineAndFile(ast));
}
else if (staName == "ScriptDirection" || staName == "ScriptDirections")
{
if (ast->getType() == OP_EQ)
{
bool fNotEmpty = m_prndr->ClearScriptDirections();
if (fNotEmpty)
g_errorList.AddWarning(1506, NULL,
staName, " global setting overrode a previous value",
LineAndFile(ast));
}
while (astValue)
{
if (astValue->getType() == LIT_INT || astValue->getType() == OP_PLUS)
{
int nValue;
GdlExpression * pexp = WalkExpressionTree(astValue);
if (!pexp->ResolveToInteger(&nValue, false))
{
g_errorList.AddError(1128, pexp,
"Invalid value for ScriptDirection");
}
else
m_prndr->AddScriptDirection(nValue);
delete pexp;
}
else
g_errorList.AddError(1129, NULL,
"Invalid value for ScriptDirection",
LineAndFile(ast));
astValue = astValue->getNextSibling();
}
}
else if (staName == "ScriptTags" || staName == "ScriptTag")
{
if (ast->getType() == OP_EQ)
{
bool fNotEmpty = m_prndr->ClearScriptTags();
if (fNotEmpty)
g_errorList.AddWarning(1507, NULL,
staName, " global setting overrode a previous value",
LineAndFile(ast));
}
while (astValue)
{
if (astValue->getType() != LIT_STRING)
{
g_errorList.AddError(1130, NULL,
"The ScriptTags global expects a string value",
LineAndFile(astValue));
return;
}
std::string sta = astValue->getText();
auto cb = sta.length();
if (cb > 4)
{
g_errorList.AddError(1131, NULL,
"Invalid script tag value--must be a 4-byte string",
LineAndFile(astValue));
return;
}
else if (cb < 4)
g_errorList.AddWarning(1508, NULL,
"Unexpected script tag value--should be a 4-byte string",
LineAndFile(astValue));
gr::byte b1, b2, b3, b4;
b1 = (cb > 0) ? sta[0] : 0;
b2 = (cb > 1) ? sta[1] : 0;
b3 = (cb > 2) ? sta[2] : 0;
b4 = (cb > 3) ? sta[3] : 0;
int nValue = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
m_prndr->AddScriptTag(nValue);
astValue = astValue->getNextSibling();
}
if (m_prndr->NumScriptTags() > kMaxScriptTags)
{
g_errorList.AddError(1132, NULL,
"Number of script tags (",
std::to_string(int(m_prndr->NumScriptTags())),
") exceeds maximum of ",
std::to_string(kMaxScriptTags));
}
}
else
{
Assert(false);
}
}
/*----------------------------------------------------------------------------------------------
Process an "environment" statement.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkEnvTree(RefAST ast, TableType tblt,
GdlRuleTable * prultbl, GdlPass * ppass)
{
Assert(ast->getType() == LITERAL_environment);
GrpLineAndFile lnf = LineAndFile(ast);
PushGeneralEnv(lnf);
RefAST astDirectives = ast->getFirstChild();
RefAST astContents = astDirectives;
if (astDirectives && astDirectives->getType() == Zdirectives)
{
WalkDirectivesTree(astDirectives);
astContents = astDirectives->getNextSibling();
}
while (astContents)
{
int nodetyp = astContents->getType();
switch (nodetyp)
{
case OP_EQ:
case OP_PLUSEQUAL:
ProcessGlobalSetting(astContents);
break;
default:
WalkTableElement(astContents, tblt, prultbl, ppass);
}
astContents = astContents->getNextSibling();
}
PopEnv(lnf, "environment");
}
/*----------------------------------------------------------------------------------------------
Process a list of environment directives.
Assumes that parser does not permit a directive to take a list of values.
pnCollisionFix - set when processing a pass(X) statement; if NULL, FixCollisions is invalid
pnAutoKern
pnCollisionThreshold
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkDirectivesTree(RefAST ast, int * pnCollisionFix, int * pnAutoKern,
int * pnCollisionThreshold, int * pnDir)
{
Assert(ast->getType() == Zdirectives);
RefAST astDirective = ast->getFirstChild();
while (astDirective)
{
std::string staName = astDirective->getFirstChild()->getText();
// For now, all directives have numeric or boolean values.
RefAST astValue = astDirective->getFirstChild()->getNextSibling();
Assert(astValue);
int nValue = -1;
bool fM = false;
if (astValue->getType() == LIT_INT)
nValue = NumericValue(astValue, &fM);
else if (astValue->getType() == LITERAL_false)
nValue = 0;
else if (astValue->getType() == LITERAL_true)
nValue = 1;
else
{
g_errorList.AddError(1195, NULL,
"Invalid value for directive ", staName,
LineAndFile(ast));
}
Symbol psym = SymbolTable()->FindSymbol(staName);
if (!psym || !psym->FitsSymbolType(ksymtDirective))
g_errorList.AddError(1133, NULL,
"Invalid directive: ",
staName,
LineAndFile(ast));
else
{
if (fM && psym->ExpType() != kexptMeas)
g_errorList.AddError(1134, NULL,
"The ",
staName,
" directive does not expect a scaled value",
LineAndFile(ast));
if (staName == "AttributeOverride")
SetAttrOverride(nValue != 0);
else if (staName == "CodePage")
SetCodePage(nValue);
else if (staName == "MaxRuleLoop")
SetMaxRuleLoop(nValue);
else if (staName == "MaxBackup")
SetMaxBackup(nValue);
else if (staName == "MUnits")
SetMUnits(nValue);
else if (staName == "PointRadius")
{
Assert(psym->ExpType() == kexptMeas);
if (!fM)
g_errorList.AddError(1135, NULL,
"The PointRadius directive requires a scaled value",
LineAndFile(ast));
SetPointRadius(nValue, MUnits());
}
else if (staName == "CollisionFix")
{
if (pnCollisionFix == NULL)
g_errorList.AddError(1185, NULL,
"The CollisionFix directive must be indicated directly on a pass",
LineAndFile(ast));
else
{
if (astValue->getType() == LITERAL_true)
*pnCollisionFix = 3; // default; should this be 1??
else if (nValue > kMaxColIterations || nValue < 0)
{
g_errorList.AddError(1186, NULL,
"The CollisionFix value must be between 0 and ",
std::to_string(kMaxColIterations),
LineAndFile(ast));
*pnCollisionFix = kMaxColIterations;
}
else
*pnCollisionFix = nValue;
}
}
else if (staName == "AutoKern")
{
if (pnAutoKern == NULL)
g_errorList.AddError(1189, NULL,
"The AutoKern directive must be indicated directly on a pass",
LineAndFile(ast));
else if (nValue != kakNone && nValue != kakFull && nValue != kakNoSpace)
g_errorList.AddWarning(1516, NULL,
"The AutoKern value should be 0 (NONE), 1 (FULL), or 2 (NOSPACE)",
LineAndFile(ast));
else
*pnAutoKern = nValue;
}
else if (staName == "CollisionThreshold")
{
if (pnCollisionThreshold == NULL)
g_errorList.AddError(1190, NULL,
"The CollisionThreshold directive must be indicated directly on a pass",
LineAndFile(ast));
else if (nValue < 1 || nValue > 255)
g_errorList.AddError(1191, NULL,
"The CollisionThreshold value must be between 1 and 255",
LineAndFile(ast));
else
*pnCollisionThreshold = nValue;
}
else if (staName == "Direction")
{
if (pnDir == NULL)
g_errorList.AddError(1194, NULL,
"The Direction directive must be indicated directly on a pass",
LineAndFile(ast));
else
*pnDir = nValue;
}
else
{
Assert(false);
}
}
astDirective = astDirective->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Process a "table" statement.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkTableTree(RefAST ast)
{
Assert(ast->getType() == LITERAL_table);
RefAST astTableType = ast->getFirstChild();
Assert(astTableType);
int nodetyp = astTableType->getType();
switch (nodetyp)
{
case LITERAL_glyph:
WalkGlyphTableTree(ast);
break;
case LITERAL_feature:
WalkFeatureTableTree(ast);
break;
case LITERAL_language:
WalkLanguageTableTree(ast);
break;
case LITERAL_name:
WalkNameTableTree(ast);
break;
case LITERAL_substitution:
case LITERAL_linebreak:
case LITERAL_position:
case LITERAL_positioning:
case LITERAL_justification:
WalkRuleTableTree(ast, nodetyp);
break;
case IDENT:
g_errorList.AddWarning(1509, NULL,
"Skipping '",
astTableType->getText().c_str(),
"' table--unrecognized table name",
LineAndFile(ast));
break;
default:
Assert(false);
}
}
/*----------------------------------------------------------------------------------------------
Process an element of a table: a -pass-, -table-, -if-. or -environment- statement,
assignment, or rule.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkTableElement(RefAST ast, TableType tblt,
GdlRuleTable * prultbl, GdlPass * ppass)
{
int nodetyp = ast->getType();
switch (nodetyp)
{
case LITERAL_environment:
WalkEnvTree(ast, tblt, prultbl, ppass);
break;
case LITERAL_table:
WalkTableTree(ast);
break;
case Zdirectives:
WalkDirectivesTree(ast);
break;
case LITERAL_pass:
Assert(prultbl);
Assert(ktbltRule == tblt);
WalkPassTree(ast, prultbl, ppass);
break;
case ZifStruct:
Assert(prultbl);
Assert(ktbltRule == tblt);
WalkIfTree(ast, prultbl, ppass);
break;
case Zrule:
Assert(prultbl);
Assert(ktbltRule == tblt);
WalkRuleTree(ast, prultbl, ppass);
break;
default:
// Assignment of some sort.
switch (tblt)
{
case ktbltFeature:
WalkFeatureTableElement(ast);
break;
case ktbltLanguage:
WalkLanguageTableElement(ast);
break;
case ktbltName:
WalkNameTableElement(ast);
break;
case ktbltGlyph:
WalkGlyphTableElement(ast);
break;
case ktbltRule:
default:
Assert(false);
}
}
}
/*----------------------------------------------------------------------------------------------
Process the glyph table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkGlyphTableTree(RefAST ast)
{
Assert(ast->getType() == LITERAL_table);
Assert(ast->getFirstChild()->getType() == LITERAL_glyph);
RefAST astContents = ast->getFirstChild()->getNextSibling();
while (astContents)
{
int nodetyp = astContents->getType();
switch (nodetyp)
{
case OP_EQ:
case OP_PLUSEQUAL:
case OP_ANDEQUAL:
case OP_MINUSEQUAL:
WalkGlyphTableElement(astContents);
break;
default:
WalkTableElement(astContents, ktbltGlyph, NULL, NULL);
}
astContents = astContents->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Process a top-level glyph table element.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkGlyphTableElement(RefAST ast)
{
int nodetyp = ast->getType();
Assert(nodetyp == OP_EQ || nodetyp == OP_PLUSEQUAL
|| nodetyp == OP_ANDEQUAL || nodetyp == OP_MINUSEQUAL);
GlyphClassType glfct;
switch (nodetyp)
{
case OP_ANDEQUAL: glfct = kglfctIntersect; break;
case OP_MINUSEQUAL: glfct = kglfctDifference; break;
default: glfct = kglfctUnion; break;
}
std::vector<std::string> vsta;
GdlGlyphClassDefn * pglfc;
std::string staClassName = ast->getFirstChild()->getText();
Symbol psymClass = SymbolTable()->FindSymbol(staClassName);
if (!psymClass)
{
// Create the class.
psymClass = SymbolTable()->AddClassSymbol(GrcStructName(staClassName), LineAndFile(ast),
glfct);
pglfc = psymClass->GlyphClassDefnData();
Assert(pglfc);
pglfc->SetName(staClassName);
m_prndr->AddGlyphClass(pglfc);
if (nodetyp == OP_ANDEQUAL || nodetyp == OP_MINUSEQUAL) // union or intersection
{
g_errorList.AddError(1184, NULL,
"Cannot perform set operation on nonexistent class ",
staClassName,
LineAndFile(ast));
return;
}
else if (nodetyp == OP_PLUSEQUAL) // appending
{
g_errorList.AddWarning(1515, NULL,
"Appending to non-existent class '", staClassName, "'; class will be created",
LineAndFile(ast));
}
}
else
{
if (!psymClass->FitsSymbolType(ksymtClass))
{
g_errorList.AddError(1136, NULL,
"Name conflict: '",
staClassName,
"' cannot be used as a glyph class name",
LineAndFile(ast));
return;
}
pglfc = psymClass->GlyphClassDefnData();
Assert(pglfc);
if (nodetyp == OP_EQ)
{
g_errorList.AddError(1137, NULL,
"Duplicate definition of class '",
staClassName, "'",
LineAndFile(ast));
return;
}
else if (nodetyp == OP_ANDEQUAL)
{
GrpLineAndFile lnf = LineAndFile(ast); // separate line makes Linux build happy
pglfc = ConvertClassToIntersection(psymClass, pglfc, lnf);
}
else if (nodetyp == OP_MINUSEQUAL)
{
GrpLineAndFile lnf = LineAndFile(ast); // separate line makes Linux build happy
pglfc = ConvertClassToDifference(psymClass, pglfc, lnf);
}
}
WalkGlyphClassTree(ast, pglfc, glfct);
}
/*----------------------------------------------------------------------------------------------
Process a single glyph class definition.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkGlyphClassTree(RefAST ast, GdlGlyphClassDefn * pglfc, GlyphClassType glfct)
{
// Skip the class name.
RefAST astContents = ast->getFirstChild()->getNextSibling();
while (astContents)
{
if (astContents->getType() == Zattrs)
{
// Attributes.
std::vector<std::string> vsta;
vsta.push_back(pglfc->Name());
RefAST astT = astContents->getFirstChild();
while (astT)
{
WalkGlyphAttrTree(astT, vsta);
Assert(vsta.size() == 1);
astT = astT->getNextSibling();
}
}
else
// Class member
ProcessGlyphClassMember(astContents, pglfc, glfct, NULL);
astContents = astContents->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Traverse the glyph attribute assignment tree, adding the assignments to the symbol table
and master glyph attribute table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkGlyphAttrTree(RefAST ast, std::vector<std::string> & vsta)
{
if (!ast)
return;
RefAST astNextID = ast->getFirstChild();
vsta.push_back(astNextID->getText());
int nodetyp = ast->getType();
if (nodetyp == OP_EQ)
{
// Assignment.
Symbol psymBase = SymbolTable()->FindSymbol(vsta[1]);
if (psymBase && !psymBase->FitsSymbolType(ksymtGlyphAttr) &&
!psymBase->FitsSymbolType(ksymtInvalid))
{
g_errorList.AddError(1138, NULL,
"Invalid glyph attribute name: ",
psymBase->FullName(),
LineAndFile(ast));
}
else
{
RefAST astValue = astNextID->getNextSibling();
if (astValue->getType() == Zfunction)
ProcessFunction(astValue, vsta, false);
else
{
GdlExpression * pexpValue = WalkExpressionTree(astValue);
GdlExpression * pexpX = NULL;
GdlExpression * pexpY = NULL;
GdlExpression * pexpGpoint = NULL;
GdlExpression * pexpXoffset = NULL;
GdlExpression * pexpYoffset = NULL;
if (pexpValue->PointFieldEquivalents(this, &pexpX, &pexpY, &pexpGpoint, &pexpXoffset, &pexpYoffset)
&& (pexpX || pexpY))
{
if (pexpX)
{
vsta.push_back("x");
AddGlyphAttr(ast, vsta, pexpX);
vsta.pop_back();
}
if (pexpY)
{
vsta.push_back("y");
AddGlyphAttr(ast, vsta, pexpY);
vsta.pop_back();
}
if (OffsetAttrs())
{
if (pexpGpoint)
{
vsta.push_back("gpoint");
AddGlyphAttr(ast, vsta, pexpGpoint);
vsta.pop_back();
}
if (pexpXoffset)
{
vsta.push_back("xoffset");
AddGlyphAttr(ast, vsta, pexpY);
vsta.pop_back();
}
if (pexpYoffset)
{
vsta.push_back("yoffset");
AddGlyphAttr(ast, vsta, pexpY);
vsta.pop_back();
}
}
else
{
if (pexpGpoint)
delete pexpGpoint;
if (pexpXoffset)
delete pexpXoffset;
if (pexpYoffset)
delete pexpYoffset;
}
delete pexpValue;
}
else
{
AddGlyphAttr(ast, vsta, pexpValue);
}
}
}
}
else if (nodetyp == OP_PLUSEQUAL || nodetyp == OP_MINUSEQUAL
|| nodetyp == OP_MULTEQUAL || nodetyp == OP_DIVEQUAL)
// || nodetyp == OP_ANDEQUAL || nodetyp == OP_OREQUAL) - not implemented
{
std::string staOp = ast->getText();
g_errorList.AddError(1139, NULL,
"Cannot assign a glyph attribute with ",
staOp,
LineAndFile(ast));
}
else
{
// Dot or brace.
Assert(ast->getType() == OP_DOT || ast->getType() == ZdotStruct);
RefAST astT = astNextID->getNextSibling();
while (astT)
{
WalkGlyphAttrTree(astT, vsta);
astT = astT->getNextSibling();
}
}
vsta.pop_back();
}
/*----------------------------------------------------------------------------------------------
Add the expression as the value of the glyph attribute indicated by the strings.
----------------------------------------------------------------------------------------------*/
void GrcManager::AddGlyphAttr(RefAST ast, std::vector<std::string> & vsta, GdlExpression * pexpValue)
{
Symbol psym = SymbolTable()->AddGlyphAttrSymbol(GrcStructName(vsta),
LineAndFile(ast), pexpValue->ExpType());
if (psym)
m_mtbGlyphAttrs->AddItem(psym, pexpValue,
PointRadius(), PointRadiusUnits(), AttrOverride(), LineAndFile(ast),
"glyph attr assignment");
}
/*----------------------------------------------------------------------------------------------
Process an attribute whose value is a function: box, point, gpoint, gpath.
Arguments:
fSlotAttr - true if this is a slot attribute, false if it is a glyph attr
prit, psymOp - only used for slot attributes
----------------------------------------------------------------------------------------------*/
void GrcManager::ProcessFunction(RefAST ast, std::vector<std::string> & vsta,
bool fSlotAttr, GdlRuleItem * prit, Symbol psymOp)
{
Assert(ast->getType() == Zfunction);
Assert(!fSlotAttr || (prit && psymOp));
RefAST astName = ast->getFirstChild();
std::string staName = astName->getText();
int nPR = PointRadius();
int mPRUnits = PointRadiusUnits();
bool fOverride = AttrOverride();
GrpLineAndFile lnf = LineAndFile(ast);
RefAST astX1;
RefAST astX2;
RefAST astX3;
RefAST astX4;
GdlExpression * pexp1 = NULL;
GdlExpression * pexp2 = NULL;
GdlExpression * pexp3 = NULL;
GdlExpression * pexp4 = NULL;
std::string sta1;
std::string sta2;
std::string sta3;
std::string sta4;
ExpressionType expt1, expt2, expt3, expt4;
if (staName == "box")
{
sta1 = "left";
sta2 = "bottom";
sta3 = "right";
sta4 = "top";
expt1 = kexptMeas;
expt2 = kexptMeas;
expt3 = kexptMeas;
expt4 = kexptMeas;
astX1 = astName->getNextSibling();
if (astX1)
{
pexp1 = WalkExpressionTree(astX1); // left
astX2 = astX1->getNextSibling();
if (astX2)
{
pexp2 = WalkExpressionTree(astX2); // bottom
astX3 = astX2->getNextSibling();
if (astX3)
{
pexp3 = WalkExpressionTree(astX3); // right
astX4 = astX3->getNextSibling();
if (astX4)
{
pexp4 = WalkExpressionTree(astX4); // top
if (astX4->getNextSibling())
BadFunctionError(lnf, staName, "4");
}
else
BadFunctionError(lnf, staName, "4");
}
else
BadFunctionError(lnf, staName, "4");
}
else
BadFunctionError(lnf, staName, "4");
}
else
BadFunctionError(lnf, staName, "4");
}
else if (staName == "point")
{
sta1 = "x";
sta2 = "y";
sta3 = "xoffset";
sta4 = "yoffset";
expt1 = kexptMeas;
expt2 = kexptMeas;
expt3 = kexptMeas;
expt4 = kexptMeas;
astX1 = astName->getNextSibling();
if (astX1)
{
pexp1 = WalkExpressionTree(astX1); // x
astX2 = astX1->getNextSibling();
if (astX2)
{
pexp2 = WalkExpressionTree(astX2); // y
astX3 = astX2->getNextSibling();
if (astX3)
{
pexp3 = WalkExpressionTree(astX3); // xoffset
astX4 = astX3->getNextSibling();
if (astX4)
{
pexp4 = WalkExpressionTree(astX4); // yoffset
if (astX4->getNextSibling())
BadFunctionError(lnf, staName, "2 or 4");
}
else
BadFunctionError(lnf, staName, "2 or 4");
}
else
{
pexp3 = new GdlNumericExpression(0); // xoffset
pexp4 = new GdlNumericExpression(0); // yoffset
}
}
else
BadFunctionError(lnf, staName, "2 or 4");
}
else
BadFunctionError(lnf, staName, "2 or 4");
}
else if (staName == "gpoint" || staName == "gpath")
{
sta1 = staName;
sta2 = "xoffset";
sta3 = "yoffset";
expt1 = kexptNumber;
expt2 = kexptMeas;
expt3 = kexptMeas;
astX1 = astName->getNextSibling();
if (astX1)
{
pexp1 = WalkExpressionTree(astX1); // gpath or gpoint
astX2 = astX1->getNextSibling();
if (astX2)
{
pexp2 = WalkExpressionTree(astX2); // xoffset
astX3 = astX2->getNextSibling();
if (astX3)
{
pexp3 = WalkExpressionTree(astX3); // yoffset
if (astX3->getNextSibling())
BadFunctionError(lnf, staName, "1 or 3");
}
else
BadFunctionError(lnf, staName, "1 or 3");
}
else
{
pexp2 = new GdlNumericExpression(0); // xoffset
pexp3 = new GdlNumericExpression(0); // yoffset
}
}
else
BadFunctionError(lnf, staName, "1 or 3");
}
// glyphattr(attrName) function is not implemented - instead use glyph.attrName
//else if (staName == "glyphattr")
//{
// if (!fSlotAttr)
// g_errorList.AddError(9999, NULL,
// "glyphattr() function should only be used within a rule",
// LineAndFile(ast));
// astX1 = astName->getNextSibling();
// if (astX1)
// {
// pexp1 = WalkExpressionTree(astX1);
// astX2 = astX1->getNextSibling();
// if (astX2)
// BadFunctionError(lnf, staName, "1");
// else
// {
// ProcessFunctionArg(false, vsta, nPR, mPRUnits, fOverride, lnf,
// kexptUnknown, prit, psymOp, pexp1);
// }
// pexp1 = NULL; // don't do processing below
// }
// else
// BadFunctionError(lnf, staName, "1");
//}
else
{
g_errorList.AddError(1140, NULL,
"Undefined glyph attribute function: ",
staName,
LineAndFile(ast));
return;
}
if (pexp1)
{
vsta.push_back(sta1);
ProcessFunctionArg(fSlotAttr, GrcStructName(vsta),
nPR, mPRUnits, fOverride, lnf,
expt1, prit, psymOp, pexp1);
vsta.pop_back();
}
if (pexp2)
{
vsta.push_back(sta2);
ProcessFunctionArg(fSlotAttr, GrcStructName(vsta),
nPR, mPRUnits, fOverride, lnf,
expt2, prit, psymOp, pexp2);
vsta.pop_back();
}
if (pexp3)
{
vsta.push_back(sta3);
ProcessFunctionArg(fSlotAttr, GrcStructName(vsta),
nPR, mPRUnits, fOverride, lnf,
expt3, prit, psymOp, pexp3);
vsta.pop_back();
}
if (pexp4)
{
vsta.push_back(sta4);
ProcessFunctionArg(fSlotAttr, GrcStructName(vsta),
nPR, mPRUnits, fOverride, lnf,
expt4, prit, psymOp, pexp4);
vsta.pop_back();
}
}
/*----------------------------------------------------------------------------------------------
Process a single argument of a function (point, box, gpath, gpoint) either as a glyph
attribute or a slot attribute.
----------------------------------------------------------------------------------------------*/
void GrcManager::ProcessFunctionArg(bool fSlotAttr, GrcStructName const& xns,
int nPR, int mPRUnits, bool fOverride, GrpLineAndFile const& lnf,
ExpressionType expt, GdlRuleItem * prit, Symbol psymOp, GdlExpression * pexpValue)
{
if (fSlotAttr)
{
Symbol psymSlotAttr = SymbolTable()->FindSlotAttr(xns, lnf);
if (!psymSlotAttr)
g_errorList.AddError(1141 ,NULL,
"Invalid slot attribute: ",
xns.FullString(),
lnf);
else
{
GdlAttrValueSpec * pavs = new GdlAttrValueSpec(psymSlotAttr, psymOp, pexpValue);
prit->AddAttrValueSpec(pavs);
}
}
else
{
Symbol psymGlyphAttr = SymbolTable()->AddGlyphAttrSymbol(xns, lnf, expt);
m_mtbGlyphAttrs->AddItem(psymGlyphAttr, pexpValue, nPR, mPRUnits, fOverride, lnf,
"glyph attr assignment");
}
}
/*----------------------------------------------------------------------------------------------
Record an error indicating that a function has the wrong number of arguments.
----------------------------------------------------------------------------------------------*/
void GrcManager::BadFunctionError(GrpLineAndFile & lnf, std::string staFunction,
std::string staArgsExpected)
{
g_errorList.AddError(1142, NULL,
"Invalid number of arguments for '",
staFunction,
"'; ",
staArgsExpected,
" expected",
lnf);
}
/*----------------------------------------------------------------------------------------------
Process a member of a glyph class; either add the member to the class or return it
to be used in creating a pseudo-glyph.
Arguments:
pglfc - class to add to
glfct - union, intersection, or difference
pglfRet - value to return to embed inside of pseudo;
one or the other of these arguments will be NULL
----------------------------------------------------------------------------------------------*/
void GrcManager::ProcessGlyphClassMember(RefAST ast, GdlGlyphClassDefn * pglfc,
GlyphClassType glfct, GdlGlyphDefn ** ppglfRet)
{
Assert(!pglfc || !ppglfRet);
Assert(pglfc || ppglfRet);
RefAST astItem;
int nCodePage;
int nPseudoInput;
GlyphType glft = kglftUnknown;
std::string staSubClassName;
Symbol psymSubClass;
GdlGlyphDefn * pglfT;
GdlGlyphClassDefn * pglfcSub;
GdlGlyphDefn * pglfForPseudo;
int nodetyp = ast->getType();
switch (nodetyp)
{
case LITERAL_unicode: glft = kglftUnicode; break;
case ZuHex: glft = kglftUnicode; break;
case LITERAL_glyphid: glft = kglftGlyphID; break;
case LITERAL_codepoint: glft = kglftCodepoint; break;
case LITERAL_postscript: glft = kglftPostscript; break;
case LITERAL_pseudo: glft = kglftPseudo; break;
case IDENT: break;
default:
Assert(false);
}
switch (nodetyp)
{
case LITERAL_unicode:
case ZuHex:
case LITERAL_glyphid:
case LITERAL_postscript:
astItem = ast->getFirstChild();
Assert(astItem->getType() != Zcodepage);
while (astItem)
{
pglfT = ProcessGlyph(astItem, glft);
if (pglfc)
pglfc->AddElement(pglfT, LineAndFile(ast), glfct, this);
else
*ppglfRet = pglfT; // return for pseudo
astItem = astItem->getNextSibling();
if (astItem && !pglfc)
{
// Can't put more than one output glyph in a pseudo.
g_errorList.AddError(1143, NULL,
"Pseudo-glyph -> glyph ID mapping contains more than one glyph",
LineAndFile(astItem));
break;
}
}
break;
case LITERAL_codepoint:
astItem = ast->getFirstChild();
if (astItem->getType() == Zcodepage)
{
Assert(astItem->getFirstChild()->getType() == LIT_INT);
nCodePage = NumericValue(astItem->getFirstChild());
astItem = astItem->getNextSibling();
}
else
nCodePage = CodePage();
while (astItem)
{
pglfT = ProcessGlyph(astItem, kglftCodepoint, nCodePage);
if (pglfc)
pglfc->AddElement(pglfT, LineAndFile(ast), glfct, this);
else
*ppglfRet = pglfT; // return for pseudo
astItem = astItem->getNextSibling();
if (astItem && !pglfc)
{
// Can't put more than one output glyph in a pseudo.
g_errorList.AddError(1144, NULL,
"Pseudo-glyph -> glyph ID mapping contains more than one glyph",
LineAndFile(astItem));
break;
}
}
break;
case LITERAL_pseudo:
Assert(pglfc && !ppglfRet); // can't put a pseudo inside a pseudo
astItem = ast->getFirstChild();
ProcessGlyphClassMember(astItem, NULL, kglfctUnion, &pglfForPseudo);
if (astItem->getNextSibling())
{
RefAST astInput = astItem->getNextSibling();
nPseudoInput = NumericValue(astInput);
pglfT = new GdlGlyphDefn(kglftPseudo, pglfForPseudo, nPseudoInput);
}
else
pglfT = new GdlGlyphDefn(kglftPseudo, pglfForPseudo);
pglfT->SetLineAndFile(LineAndFile(ast));
pglfc->AddElement(pglfT, LineAndFile(ast), glfct, this);
break;
case IDENT:
Assert(!ast->getFirstChild());
Assert(pglfc && !ppglfRet); // can't put a subclass identifer inside a pseudo
staSubClassName = ast->getText();
psymSubClass = SymbolTable()->FindSymbol(staSubClassName);
if (!psymSubClass)
{
g_errorList.AddError(1145, NULL,
"Undefined glyph class: ",
staSubClassName,
LineAndFile(ast));
return;
}
if (!psymSubClass->FitsSymbolType(ksymtClass))
{
g_errorList.AddError(1146, NULL,
"Name conflict: '",
staSubClassName,
"' cannot be used as a glyph class name",
LineAndFile(ast));
return;
}
pglfcSub = psymSubClass->GlyphClassDefnData();
Assert(pglfcSub);
pglfc->AddElement(pglfcSub, LineAndFile(ast), glfct, this);
break;
default:
Assert(false);
}
}
/*----------------------------------------------------------------------------------------------
Process the contents of a glyph function ("unicode", "glyphid", etc); return the
resulting glyph.
----------------------------------------------------------------------------------------------*/
GdlGlyphDefn * GrcManager::ProcessGlyph(RefAST astGlyph, GlyphType glft, int nCodePage)
{
RefAST ast1;
RefAST ast2;
int n1 = 0, n2 = 0;
utf16 w1;
std::string sta;
utf16 wCodePage = (utf16)nCodePage;
GdlGlyphDefn * pglfRet = NULL;
int nodetyp = astGlyph->getType();
switch (nodetyp)
{
case OP_DOTDOT:
// Range: 0x1111..0x2222, 'a'..'z', U+89ab..U+89ff
ast1 = astGlyph->getFirstChild();
Assert(ast1);
ast2 = ast1->getNextSibling();
Assert(ast2);
Assert(!ast2->getNextSibling());
if (ast1->getType() == LIT_INT || ast1->getType() == LIT_UHEX)
n1 = NumericValue(ast1);
else if (ast1->getType() == LIT_CHAR)
n1 = *(ast1->getText().c_str());
else
{
Assert(false);
}
if (ast2->getType() == LIT_INT || ast1->getType() == LIT_UHEX)
n2 = NumericValue(ast2);
else if (ast2->getType() == LIT_CHAR)
n2 = *(ast2->getText().c_str());
else
{
Assert(false);
}
pglfRet = (nCodePage == -1) ?
new GdlGlyphDefn(glft, n1, n2) :
new GdlGlyphDefn(glft, n1, n2, wCodePage);
break;
case LIT_INT:
case LIT_UHEX:
n1 = NumericValue(astGlyph);
pglfRet = (nCodePage == -1) ?
new GdlGlyphDefn(glft, n1) :
new GdlGlyphDefn(glft, n1, nCodePage);
break;
case LIT_CHAR:
w1 = (astGlyph->getText())[0];
pglfRet = (nCodePage == -1) ?
new GdlGlyphDefn(glft, w1) :
new GdlGlyphDefn(glft, w1, nCodePage);
break;
case LIT_STRING:
sta = astGlyph->getText();
pglfRet = (nCodePage == -1) ?
new GdlGlyphDefn(glft, sta) :
new GdlGlyphDefn(glft, sta, wCodePage);
break;
default:
Assert(false);
}
pglfRet->SetLineAndFile(LineAndFile(astGlyph));
return pglfRet;
}
/*----------------------------------------------------------------------------------------------
Process the feature table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkFeatureTableTree(RefAST ast)
{
Assert(ast->getType() == LITERAL_table);
Assert(ast->getFirstChild()->getType() == LITERAL_feature);
RefAST astContents = ast->getFirstChild()->getNextSibling();
while (astContents)
{
int nodetyp = astContents->getType();
switch (nodetyp)
{
case OP_DOT:
case ZdotStruct:
case OP_EQ:
WalkFeatureTableElement(astContents);
break;
default:
WalkTableElement(astContents, ktbltFeature, NULL, NULL);
}
astContents = astContents->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Process a top level element in the feature table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkFeatureTableElement(RefAST ast)
{
std::vector<std::string> vsta;
std::string staFeatureName = ast->getFirstChild()->getText();
Symbol psymFeat = SymbolTable()->FindSymbol(staFeatureName);
if (!psymFeat)
{
if (staFeatureName == "lang")
{
g_errorList.AddError(1147, NULL,
"Attempt to redefine reserved feature: 'lang'",
LineAndFile(ast));
return;
}
psymFeat = SymbolTable()->AddFeatureSymbol(GrcStructName(staFeatureName),
LineAndFile(ast));
GdlFeatureDefn * pfeat = psymFeat->FeatureDefnData();
Assert(pfeat);
pfeat->SetName(std::string(staFeatureName));
m_prndr->AddFeature(pfeat);
}
else if (!psymFeat->FitsSymbolType(ksymtFeature))
{
g_errorList.AddError(1148, NULL,
"Identifier conflict: '",
staFeatureName,
"' cannot be used as a feature name",
LineAndFile(ast));
return;
}
vsta.clear();
WalkFeatureSettingsTree(ast, vsta);
}
/*----------------------------------------------------------------------------------------------
Traverse the features identifier tree, adding the assignments to the symbol table
and master features table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkFeatureSettingsTree(RefAST ast, std::vector<std::string> & vsta)
{
RefAST astNextID = ast->getFirstChild();
vsta.push_back(astNextID->getText());
if (ast->getType() == OP_EQ)
{
if (vsta.size() < 2)
{
g_errorList.AddError(1149, NULL,
"Invalid feature statement",
LineAndFile(ast));
return;
}
Symbol psym = SymbolTable()->AddSymbol(GrcStructName(vsta), ksymtFeatSetting,
LineAndFile(ast));
RefAST astValue = astNextID->getNextSibling();
if (!astValue)
return;
GdlExpression *pexpValue;
if (astValue->getType() == IDENT)
{
// A kludge, because this isn't a slot-ref expression, but that is the
// most convenient thing to hold a simple identifier until we can process it
// further (see the master table function that processes the features).
pexpValue = new GdlSlotRefExpression(astValue->getText());
pexpValue->SetLineAndFile(LineAndFile(ast));
}
else
{
pexpValue = WalkExpressionTree(astValue);
}
m_mtbFeatures->AddItem(psym, pexpValue,
PointRadius(), PointRadiusUnits(), AttrOverride(), LineAndFile(ast),
"feature setting");
}
else
{
Assert(ast->getType() == OP_DOT || ast->getType() == ZdotStruct);
RefAST astT = astNextID->getNextSibling();
while (astT)
{
WalkFeatureSettingsTree(astT, vsta);
astT = astT->getNextSibling();
}
}
vsta.pop_back();
}
/*----------------------------------------------------------------------------------------------
Process the language table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkLanguageTableTree(RefAST ast)
{
Assert(ast->getType() == LITERAL_table);
Assert(ast->getFirstChild()->getType() == LITERAL_language);
RefAST astContents = ast->getFirstChild()->getNextSibling();
while (astContents)
{
int nodetyp = astContents->getType();
switch (nodetyp)
{
case OP_DOT:
case ZdotStruct:
WalkLanguageTableElement(astContents);
break;
default:
WalkTableElement(astContents, ktbltLanguage, NULL, NULL);
}
astContents = astContents->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Process a top level element in the language table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkLanguageTableElement(RefAST ast)
{
RefAST astLabel = ast->getFirstChild();
std::string staLabel(astLabel->getText().c_str());
RefAST astItem = astLabel->getNextSibling();
// Find or create the language class with that name:
GdlLangClass * plcls;
size_t ilcls;
for (ilcls = 0; ilcls < m_vplcls.size(); ilcls++)
{
if (strcmp(m_vplcls[ilcls]->m_staLabel.c_str(), staLabel.c_str()) == 0)
{
plcls = m_vplcls[ilcls];
break;
}
}
if (ilcls >= m_vplcls.size())
{
plcls = new GdlLangClass(staLabel);
m_vplcls.push_back(plcls);
}
WalkLanguageItem(astItem, plcls);
}
/*----------------------------------------------------------------------------------------------
Process one assignment in the language table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkLanguageItem(RefAST ast, GdlLangClass * plcls)
{
RefAST astItem = ast;
while (astItem)
{
Assert(astItem->getType() == OP_EQ);
RefAST astLhs = astItem->getFirstChild();
std::string staLhs = astLhs->getText();
if (staLhs == "language" || staLhs == "languages")
{
if (plcls->m_vplang.size() > 0)
{
g_errorList.AddError(1150, NULL,
"Redefining list of languages for language group '", plcls->m_staLabel, "'",
LineAndFile(astLhs));
}
RefAST astLangList = astLhs->getNextSibling();
WalkLanguageCodeList(astLangList, plcls);
}
else // feature setting
{
RefAST astValue = astLhs->getNextSibling();
std::string staValue = astValue->getText();
GdlExpression * pexpVal = NULL;
if (astValue->getType() != IDENT)
pexpVal = WalkExpressionTree(astValue);
plcls->AddFeatureValue(staLhs, staValue, pexpVal, LineAndFile(astValue));
}
astItem = astItem->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Process one line in the language table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkLanguageCodeList(RefAST astList, GdlLangClass * plcls)
{
RefAST astNext = astList;
while (astNext && astNext->getType() == LIT_STRING)
{
std::string staLang = astNext->getText();
if (staLang.length() > 4)
{
g_errorList.AddError(1151, NULL,
"Language codes may contain a maximum of 4 characters",
LineAndFile(astNext));
}
// Create a language.
GrcStructName xns(staLang);
GrpLineAndFile lnf;
Symbol psymLang = SymbolTable()->AddLanguageSymbol(xns, lnf);
GdlLanguageDefn * plang = psymLang->LanguageDefnData();
Assert(plang);
plang->SetCode(std::string(staLang));
m_prndr->AddLanguage(plang);
plcls->AddLanguage(plang);
astNext = astNext->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Process the name table.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkNameTableTree(RefAST ast)
{
Assert(ast->getType() == LITERAL_table);
Assert(ast->getFirstChild()->getType() == LITERAL_name);
RefAST astContents = ast->getFirstChild()->getNextSibling();
while (astContents)
{
int nodetyp = astContents->getType();
switch (nodetyp)
{
case OP_DOT:
case ZdotStruct:
case OP_EQ:
case OP_PLUSEQUAL:
WalkNameTableElement(astContents);
break;
default:
WalkTableElement(astContents, ktbltName, NULL, NULL);
}
astContents = astContents->getNextSibling();
}
}
/*----------------------------------------------------------------------------------------------
Process a top-level name table entry.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkNameTableElement(RefAST ast)
{
// Nothing special to do.
std::vector<std::string> vsta;
WalkNameIDTree(ast, vsta);
Assert(vsta.size() == 0);
}
/*----------------------------------------------------------------------------------------------
Process a name assignment.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkNameIDTree(RefAST ast, std::vector<std::string> & vsta)
{
RefAST astNextID = ast->getFirstChild();
vsta.push_back(astNextID->getText());
if (ast->getType() == OP_EQ || ast->getType() == OP_PLUSEQUAL)
{
Symbol psym = SymbolTable()->AddSymbol(GrcStructName(vsta), ksymtNameID,
LineAndFile(ast));
RefAST astValue = astNextID->getNextSibling();
GdlExpression *pexpValue;
pexpValue = WalkExpressionTree(astValue);
m_mvlNameStrings->AddItem(psym, pexpValue,
PointRadius(), PointRadiusUnits(), AttrOverride(), LineAndFile(ast),
"name assignment");
}
else
{
Assert(ast->getType() == OP_DOT || ast->getType() == ZdotStruct);
RefAST astT = astNextID->getNextSibling();
while (astT)
{
WalkNameIDTree(astT, vsta);
astT = astT->getNextSibling();
}
}
vsta.pop_back();
}
/*----------------------------------------------------------------------------------------------
Process a table that includes rules (substitution, positioning, linebreak).
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkRuleTableTree(RefAST ast, int nodetyp)
{
GrpLineAndFile lnf = LineAndFile(ast);
if (nodetyp == LITERAL_substitution)
PushTableEnv(lnf, "substitution");
else if (nodetyp == LITERAL_linebreak)
PushTableEnv(lnf, "linebreak");
else if (nodetyp == LITERAL_position)
PushTableEnv(lnf, "positioning");
else if (nodetyp == LITERAL_positioning)
PushTableEnv(lnf, "positioning");
else if (nodetyp == LITERAL_justification)
PushTableEnv(lnf, "justification");
else
{
Assert(false);
}
RefAST astDirectives = ast->getFirstChild()->getNextSibling();
RefAST astContents = astDirectives;
if (astDirectives && astDirectives->getType() == Zdirectives)
{
WalkDirectivesTree(astDirectives);
astContents = astDirectives->getNextSibling();
}
GdlRuleTable * prultbl = RuleTable(lnf);
if (nodetyp == LITERAL_substitution || nodetyp == LITERAL_justification)
prultbl->SetSubstitution(true);
GdlPass * ppass = prultbl->GetPass(lnf, Pass(), MaxRuleLoop(), MaxBackup());
while (astContents)
{
WalkTableElement(astContents, ktbltRule, prultbl, ppass);
astContents = astContents->getNextSibling();
}
PopEnv(lnf, "table");
}
/*----------------------------------------------------------------------------------------------
Process a "pass" statement and its contents.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkPassTree(RefAST ast, GdlRuleTable * prultbl, GdlPass * /*ppassPrev*/)
{
RefAST astPassNumber = ast->getFirstChild();
Assert(astPassNumber->getType() == LIT_INT);
bool fM;
int nPassNumber = NumericValue(astPassNumber, &fM);
if (fM)
g_errorList.AddError(1152, NULL,
"Pass number should not be a scaled number",
LineAndFile(ast));
GrpLineAndFile lnf = LineAndFile(ast);
PushPassEnv(lnf, nPassNumber);
RefAST astDirectives = ast->getFirstChild()->getNextSibling();
RefAST astContents = astDirectives;
int nCollisionFix = 0;
int nAutoKern = 0;
int nCollisionThreshold = 0;
int nDir = 0;
if (astDirectives && astDirectives->getType() == Zdirectives)
{
WalkDirectivesTree(astDirectives, &nCollisionFix, &nAutoKern, &nCollisionThreshold, &nDir);
astContents = astDirectives->getNextSibling();
}
GdlPass * ppass = prultbl->GetPass(lnf, Pass(), MaxRuleLoop(), MaxBackup());
auto const staPass = std::to_string(Pass());
if (nCollisionFix > 0 && prultbl->Substitution())
{
g_errorList.AddError(1187, NULL,
"CollisionFix cannot be set on substitution passes, only positioning passes",
LineAndFile(ast));
}
else if (!prultbl->Substitution())
{
if (nCollisionFix == 0 && ppass->CollisionFix() > 0)
g_errorList.AddWarning(1514, NULL,
"Clearing previous value of CollisionFix for pass ", staPass,
LineAndFile(ast));
ppass->SetCollisionFix(nCollisionFix);
}
if (nAutoKern > 0 && prultbl->Substitution())
{
g_errorList.AddError(1192, NULL,
"AutoKern cannot be set on substitution passes, only positioning passes",
LineAndFile(ast));
}
else if (!prultbl->Substitution())
{
if (nAutoKern == 0 && ppass->AutoKern())
g_errorList.AddWarning(1517, NULL,
"Clearing previous value of AutoKern for pass ", staPass,
LineAndFile(ast));
ppass->SetAutoKern(nAutoKern);
}
if (nCollisionThreshold != 0 && prultbl->Substitution())
{
g_errorList.AddError(1193, NULL,
"CollisionThreshold cannot be set on substitution passes, only positioning passes",
LineAndFile(ast));
}
else if (prultbl->Substitution())
{
ppass->SetCollisionThreshold(0);
}
else
{
if (nCollisionThreshold == 0 && ppass->CollisionThreshold() != 0
&& ppass->CollisionThreshold() != kCollisionThresholdDefault )
g_errorList.AddWarning(1518, NULL,
"Clearing previous value of CollisionThreshold for pass ", staPass,
LineAndFile(ast));
else if (nCollisionThreshold > 0 && nCollisionFix == 0 && nAutoKern == 0)
g_errorList.AddWarning(1519, NULL,
"CollisionThreshold has no effect without collision-fixing and/or auto-kerning",
LineAndFile(ast));
if (nCollisionThreshold == 0)
nCollisionThreshold = kCollisionThresholdDefault;
ppass->SetCollisionThreshold(nCollisionThreshold);
}
if (ppass->Direction() != 0 && ppass->Direction() != nDir)
{
g_errorList.AddWarning(1520, NULL,
"Inconsistent Direction directive specifications on pass ", staPass,
LineAndFile(ast));
}
ppass->SetDirection(nDir);
// Copy the conditional(s) from the -if- statement currently in effect as a pass-level
// constraint.
for (size_t ipexp = 0; ipexp < m_vpexpPassConstraints.size(); ipexp++)
ppass->AddConstraint(m_vpexpPassConstraints[ipexp]->Clone());
while (astContents)
{
WalkTableElement(astContents, ktbltRule, prultbl, ppass);
astContents = astContents->getNextSibling();
}
PopEnv(lnf, "pass");
}
/*----------------------------------------------------------------------------------------------
Process an "if" statement.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkIfTree(RefAST ast, GdlRuleTable * prultbl, GdlPass * ppass)
{
Assert(ast->getType() == ZifStruct);
int cConds = 0;
bool fPassConstraint = AllContentsArePasses(ast);
// Only the most immediate pass constraints apply to the embedded passes, so
// temporarily remove any that are hanging around at this point.
std::vector<GdlExpression *> vpexpSavePassConstr;
size_t ipexp;
for (ipexp = 0; ipexp < m_vpexpPassConstraints.size(); ipexp++)
vpexpSavePassConstr.push_back(m_vpexpPassConstraints[ipexp]);
m_vpexpPassConstraints.clear();
Symbol psymNot = SymbolTable()->FindSymbol("!");
Assert(psymNot);
RefAST astCond;
GdlExpression * pexpCond = NULL;
RefAST astNext = ast->getFirstChild();
while (astNext)
{
if (pexpCond)
{
// The else branch: push the negation of the previous condition on the stack.
GdlExpression * pexpNot = new GdlUnaryExpression(psymNot, pexpCond);
if (fPassConstraint)
{
m_vpexpPassConstraints.pop_back();
m_vpexpPassConstraints.push_back(pexpNot);
}
else
{
m_vpexpConditionals.pop_back();
m_vpexpConditionals.push_back(pexpNot);
}
}
RefAST astContents = astNext->getFirstChild();
int nodetyp = astNext->getType();
switch (nodetyp)
{
case LITERAL_if:
case LITERAL_elseif:
case Zelseif:
astCond = astContents;
astContents = astContents->getNextSibling();
break;
case LITERAL_else:
Assert(!astNext->getNextSibling());
astCond = RefAST(NULL);
break;
default:
Assert(false);
}
if (astCond)
{
// Push the current condition on the stack.
pexpCond = WalkExpressionTree(astCond);
if (fPassConstraint)
m_vpexpPassConstraints.push_back(pexpCond);
else
m_vpexpConditionals.push_back(pexpCond);
cConds++;
}
while (astContents)
{
WalkTableElement(astContents, ktbltRule, prultbl, ppass);
astContents = astContents->getNextSibling();
}
astNext = astNext->getNextSibling();
}
// Delete all the conditions that were pushed (copies of them have been stored in
// the rules or passes themselves).
for (int i = 0; i < cConds; i++)
{
if (fPassConstraint)
{
delete m_vpexpPassConstraints.back();
m_vpexpPassConstraints.pop_back();
}
else
{
delete m_vpexpConditionals.back();
m_vpexpConditionals.pop_back();
}
}
// Put the pass constraint list back the way it was.
Assert(m_vpexpPassConstraints.size() == 0);
for (ipexp = 0; ipexp < vpexpSavePassConstr.size(); ipexp++)
m_vpexpPassConstraints.push_back(vpexpSavePassConstr[ipexp]);
}
/*----------------------------------------------------------------------------------------------
Return true if all the direct children of the -if- structure are pass structures.
If there is a mixture, return false and give a warning.
----------------------------------------------------------------------------------------------*/
bool GrcManager::AllContentsArePasses(RefAST ast)
{
bool fOne = false;
bool fAll = true;
RefAST astNext = ast->getFirstChild();
while (astNext)
{
RefAST astContents = astNext->getFirstChild();
int nodetyp = astNext->getType();
switch (nodetyp)
{
case LITERAL_if:
case LITERAL_elseif:
case Zelseif:
// First item in conditional.
astContents = astContents->getNextSibling();
break;
case LITERAL_else:
break;
default:
Assert(false);
}
while (astContents)
{
int nodetyp = astContents->getType();
if (nodetyp == LITERAL_pass)
fOne = true;
else
fAll = false;
astContents = astContents->getNextSibling();
}
astNext = astNext->getNextSibling();
}
if (fOne && !fAll)
{
g_errorList.AddWarning(1501, NULL,
"Not all elements of if statement are passes; test will be applied at rule level.",
LineAndFile(ast));
}
return fAll;
}
/*----------------------------------------------------------------------------------------------
Process a rule.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkRuleTree(RefAST ast, GdlRuleTable * prultbl, GdlPass * ppass)
{
GrpLineAndFile lnf = LineAndFile(ast);
GdlRule * prule = ppass->NewRule(lnf);
RefAST astLhs = ast->getFirstChild();
Assert(astLhs);
if (!astLhs)
return;
RefAST astRhs;
RefAST astContext;
if (astLhs->getType() == Zrhs)
{
astRhs = astLhs;
astLhs = RefAST(NULL);
}
else
{
Assert(astLhs->getType() == Zlhs);
astRhs = astLhs->getNextSibling();
if (!astRhs || astRhs->getType() == Zcontext)
{
// Substitution rule with no lhs: make what is labeled Zlhs be the rhs.
astRhs = astLhs;
astLhs = RefAST(NULL);
}
else
{
Assert(astRhs);
Assert(astRhs->getType() == Zrhs);
}
}
astContext = astRhs->getNextSibling(); // may be NULL
// Process the context first, then the RHS, then the LHS.
RefAST astItem;
int irit = 0;
if (astContext)
{
Assert(astContext->getType() == Zcontext);
astItem = astContext->getFirstChild();
while (astItem)
{
ProcessItemRange(astItem, prultbl, ppass, prule, &irit, 0, false);
astItem = astItem->getNextSibling();
}
}
astItem = astRhs->getFirstChild();
irit = 0;
while (astItem)
{
ProcessItemRange(astItem, prultbl, ppass, prule, &irit, 1, (astLhs != NULL));
astItem = astItem->getNextSibling();
}
if (astLhs)
{
astItem = astLhs->getFirstChild();
int critRhs = irit;
irit = 0;
while (astItem)
{
ProcessItemRange(astItem, prultbl, ppass, prule, &irit, 2, false);
astItem = astItem->getNextSibling();
}
if (irit > 0 && irit != critRhs)
g_errorList.AddError(1188, NULL,
"Number of items in left- and right-hand-sides do not match",
LineAndFile(ast));
}
// Copy the conditionals from the -if- statements currently in effect.
for (size_t ipexp = 0; ipexp < m_vpexpConditionals.size(); ipexp++)
{
prule->AddConstraint(m_vpexpConditionals[ipexp]->Clone());
}
// Last step:
prule->InitialChecks();
}
/*----------------------------------------------------------------------------------------------
Process a rule item or optional range.
----------------------------------------------------------------------------------------------*/
void GrcManager::ProcessItemRange(RefAST astItem, GdlRuleTable * prultbl, GdlPass * ppass,
GdlRule * prule, int * pirit, int lrc, bool fHasLhs)
{
if (astItem->getType() == OP_QUESTION)
{
// Optional range.
int iritStart = *pirit;
RefAST astSubItem = astItem->getFirstChild();
while (astSubItem)
{
ProcessItemRange(astSubItem, prultbl, ppass, prule, pirit, lrc, fHasLhs);
astSubItem = astSubItem->getNextSibling();
}
prule->AddOptionalRange(iritStart, (*pirit - iritStart), (lrc == 0));
}
else if (astItem->getType() == OP_CARET)
{
if (prule->ScanAdvance() == -1)
prule->SetScanAdvance(*pirit);
else
g_errorList.AddError(1153, NULL,
"Cannot have more than one scan advance marker per rule",
LineAndFile(astItem));
}
else
{
// Single item
ProcessRuleItem(astItem, prultbl, ppass, prule, pirit, lrc, fHasLhs);
(*pirit)++;
}
}
/*----------------------------------------------------------------------------------------------
Process a single rule item.
lrc - 0 = context, 1 = rhs, 2 = lhs
fHasLhs - if we are in the rhs, and there is in fact a lhs (irrelevant
if we are not in the rhs)
----------------------------------------------------------------------------------------------*/
void GrcManager::ProcessRuleItem(RefAST astItem, GdlRuleTable * prultbl, GdlPass * /*ppass*/,
GdlRule * prule, int * pirit, int lrc, bool fHasLhs)
{
GrpLineAndFile lnf = LineAndFile(astItem);
Assert(astItem->getType() == ZruleItem);
RefAST astNext = astItem->getFirstChild();
RefAST astSel = RefAST(NULL);
RefAST astSelValue = RefAST(NULL);
RefAST astClass = RefAST(NULL);
bool fSel = false;
// bool fAssocs = false;
GdlAlias aliasSel;
std::string staAlias;
std::string staClass;
GdlRuleItem * prit = NULL;
if (astNext->getType() == OP_AT)
{
fSel = true;
staClass = "@";
astSel = astNext->getNextSibling();
if (lrc == 2)
{
g_errorList.AddError(1154, NULL,
"@ not permitted in lhs",
lnf);
staClass = "_";
}
if (astSel && astSel->getType() == Zselector)
{
if (lrc == 2)
g_errorList.AddError(1155, NULL,
"Cannot specify a selector in the lhs",
lnf);
else if (lrc == 1 && !fHasLhs)
g_errorList.AddError(1156, NULL,
"Selectors not permitted in a rule with no lhs",
lnf);
else
{
astSelValue = astSel->getFirstChild();
Assert(astSelValue);
ProcessSlotIndicator(astSelValue, &aliasSel);
astNext = astSel->getNextSibling();
}
}
else
{
aliasSel.SetIndex(0);
astNext = astNext->getNextSibling();
}
}
else
{
astClass = astNext; // class name, glyph spec, _, #
if (// astClass->getType() == IDENT ||
astClass->getType() == OP_HASH ||
astClass->getType() == OP_UNDER)
{
staClass = astClass->getText();
astNext = astClass->getNextSibling();
}
else
{
// Class name, list, or anonymous class
staClass = ProcessClassList(astClass, &astNext);
}
}
if (astNext && astNext->getType() == Zselector)
{
if (lrc == 2)
g_errorList.AddError(1157, NULL,
"Cannot specify a selector in the lhs",
lnf);
else if (lrc == 1 && !fHasLhs)
g_errorList.AddError(1158, NULL,
"Selectors not permitted in a rule with no lhs",
lnf);
else
{
fSel = true;
astSelValue = astNext->getFirstChild();
ProcessSlotIndicator(astSelValue, &aliasSel);
}
astNext = astNext->getNextSibling();
}
if (astNext && astNext->getType() == Zalias)
{
staAlias = astNext->getFirstChild()->getText();
astNext = astNext->getNextSibling();
}
// If we are in the RHS and there is a LHS, make substitution items.
bool fMakeSubItems = ((lrc == 1) && fHasLhs); //// && prultbl->Substitution();
switch (lrc)
{
case 0: // context
if (fSel)
{
prit = (aliasSel.Index() == -1) ?
prule->ContextSelectorItemAt(lnf, *pirit, staClass, aliasSel.Name(), staAlias) :
prule->ContextSelectorItemAt(lnf, *pirit, staClass, aliasSel.Index(), staAlias);
}
else
prit = prule->ContextItemAt(lnf, *pirit, staClass, staAlias);
break;
case 1: // rhs
if (fSel)
{
prit = (aliasSel.Index() == -1) ?
prule->RhsSelectorItemAt(lnf, *pirit, staClass, aliasSel.Name(), staAlias) :
prule->RhsSelectorItemAt(lnf, *pirit, staClass, aliasSel.Index(), staAlias);
}
else
prit = prule->RhsItemAt(lnf, *pirit, staClass, staAlias, fMakeSubItems);
break;
case 2: // lhs
if (fSel)
{
prit = (aliasSel.Index() == -1) ?
prule->LhsSelectorItemAt(lnf, *pirit, staClass, aliasSel.Name(), staAlias) :
prule->LhsSelectorItemAt(lnf, *pirit, staClass, aliasSel.Index(), staAlias);
}
else
prit = prule->LhsItemAt(lnf, *pirit, staClass, staAlias);
break;
default:
Assert(false);
}
if (prit)
{
if (astNext && astNext->getType() == Zassocs)
{
ProcessAssociations(astNext, prultbl, prit, lrc);
astNext = astNext->getNextSibling();
}
if (astNext)
{
if (lrc == 0) // context--constraint
{
Assert(astNext->getType() == Zconstraint);
RefAST astConstraint = astNext->getFirstChild();
if (astConstraint)
{
Assert(!astConstraint->getNextSibling());
GdlExpression * pexpConstraint = WalkExpressionTree(astConstraint);
prit->SetConstraint(pexpConstraint);
}
}
else if (lrc == 2)
g_errorList.AddError(1159, NULL,
"Cannot set attributes in the lhs",
lnf);
else
{
// attributes
Assert(lrc == 1);
Assert(astNext->getType() == Zattrs);
std::vector<std::string> vsta;
RefAST astAttr = astNext->getFirstChild();
while (astAttr)
{
WalkSlotAttrTree(astAttr, prit, vsta);
Assert(vsta.size() == 0);
astAttr = astAttr->getNextSibling();
}
}
Assert(!astNext->getNextSibling());
}
}
}
/*----------------------------------------------------------------------------------------------
Create a new class corresponding to an anonymous class in a rule. Return its name.
----------------------------------------------------------------------------------------------*/
std::string GrcManager::ProcessClassList(RefAST ast, RefAST * pastNext)
{
if (ast->getType() == IDENT)
{
*pastNext = ast->getNextSibling();
// If only one class in the list, just return its name.
if (!*pastNext)
return ast->getText();
int nextNodeTyp = ast->getNextSibling()->getType();
if (nextNodeTyp != IDENT && nextNodeTyp != LITERAL_glyphid &&
nextNodeTyp != LITERAL_unicode && nextNodeTyp != LITERAL_codepoint &&
nextNodeTyp != LITERAL_postscript && nextNodeTyp != LITERAL_pseudo)
{
return ast->getText().c_str();
}
}
Symbol psymClass = SymbolTable()->AddAnonymousClassSymbol(LineAndFile(ast));
std::string staClassName = psymClass->FullName();
GdlGlyphClassDefn * pglfc = psymClass->GlyphClassDefnData();
Assert(pglfc);
pglfc->SetName(staClassName);
m_prndr->AddGlyphClass(pglfc);
int nodetyp;
RefAST astGlyph = ast;
while (astGlyph &&
((nodetyp = astGlyph->getType()) == IDENT ||
nodetyp == LITERAL_glyphid ||
nodetyp == LITERAL_unicode || nodetyp == LITERAL_codepoint || nodetyp == ZuHex ||
nodetyp == LITERAL_postscript || nodetyp == LITERAL_pseudo))
{
ProcessGlyphClassMember(astGlyph, pglfc, kglfctUnion, NULL);
astGlyph = astGlyph->getNextSibling();
}
*pastNext = astGlyph;
return staClassName;
}
/*----------------------------------------------------------------------------------------------
Create a new class corresponding to an anonymous class in a rule. Return its name.
----------------------------------------------------------------------------------------------*/
std::string GrcManager::ProcessAnonymousClass(RefAST ast, RefAST * pastNext)
{
Symbol psymClass = SymbolTable()->AddAnonymousClassSymbol(LineAndFile(ast));
std::string staClassName = psymClass->FullName();
GdlGlyphClassDefn * pglfc = psymClass->GlyphClassDefnData();
Assert(pglfc);
pglfc->SetName(staClassName);
m_prndr->AddGlyphClass(pglfc);
// Note that there might possibly be a chain of glyphs, not just a single one.
int nodetyp;
RefAST astGlyph = ast;
while (astGlyph &&
((nodetyp = astGlyph->getType()) == LITERAL_glyphid ||
nodetyp == LITERAL_unicode || nodetyp == LITERAL_codepoint || nodetyp == ZuHex ||
nodetyp == LITERAL_postscript || nodetyp == LITERAL_pseudo))
{
ProcessGlyphClassMember(astGlyph, pglfc, kglfctUnion, NULL);
astGlyph = astGlyph->getNextSibling();
}
*pastNext = astGlyph;
return staClassName;
}
/*----------------------------------------------------------------------------------------------
Convert an existing class to an intersection class if necessary.
----------------------------------------------------------------------------------------------*/
GdlGlyphClassDefn * GrcManager::ConvertClassToIntersection(Symbol psymClass,
GdlGlyphClassDefn * pglfc, GrpLineAndFile & lnf)
{
GdlGlyphClassDefn * pglfcRet;
GdlGlyphIntersectionClassDefn * pglfci
= dynamic_cast<GdlGlyphIntersectionClassDefn *>(pglfc);
if (pglfci)
{
// Already an intersection class.
pglfcRet = pglfc;
}
else
{
GdlGlyphIntersectionClassDefn * pglfciNew = new GdlGlyphIntersectionClassDefn();
pglfciNew->SetLineAndFile(lnf);
std::string staName = pglfc->Name();
pglfciNew->SetName(staName);
Symbol psymAnonClass = SymbolTable()->AddAnonymousClassSymbol(pglfc->LineAndFile());
std::string staAnonClassName = psymAnonClass->FullName();
pglfc->SetName(staAnonClassName);
psymAnonClass->ReplaceClassData(pglfc);
// Make the original class one of the set elements of the intersection.
pglfciNew->AddSet(pglfc, lnf);
psymClass->SetData(pglfciNew);
pglfcRet = dynamic_cast<GdlGlyphClassDefn *>(pglfciNew);
m_prndr->AddGlyphClass(pglfcRet);
}
return pglfcRet;
}
/*----------------------------------------------------------------------------------------------
Convert an existing class to a difference class with the original class contents as
its minuend.
----------------------------------------------------------------------------------------------*/
GdlGlyphClassDefn * GrcManager::ConvertClassToDifference(Symbol psymClass,
GdlGlyphClassDefn * pglfc, GrpLineAndFile & lnf)
{
GdlGlyphDifferenceClassDefn * pglfcd = new GdlGlyphDifferenceClassDefn();
pglfcd->SetLineAndFile(lnf);
pglfcd->SetName(pglfc->Name());
Symbol psymAnonClass = SymbolTable()->AddAnonymousClassSymbol(pglfc->LineAndFile());
std::string staAnonClassName = psymAnonClass->FullName();
pglfc->SetName(staAnonClassName);
psymAnonClass->ReplaceClassData(pglfc);
// Make the original class the minuend of the new difference.
// I.e, "clsA -= clsB" means that the original contents of clsA becomes the minuend
// of the difference class, but is now anonymous.
pglfcd->SetMinuend(pglfc, lnf);
psymClass->SetData(pglfcd);
GdlGlyphClassDefn * pglfcRet = dynamic_cast<GdlGlyphClassDefn *>(pglfcd);
m_prndr->AddGlyphClass(pglfcRet);
return pglfcRet;
}
/*----------------------------------------------------------------------------------------------
Process a node that is a slot reference, either an identifer or a number. Fill in the
given GdlAlias with the result.
----------------------------------------------------------------------------------------------*/
void GrcManager::ProcessSlotIndicator(RefAST ast, GdlAlias * palias)
{
if (ast->getType() == LIT_INT)
{
bool fM;
int sr = NumericValue(ast, &fM);
if (fM)
g_errorList.AddError(1160, NULL,
"Slot indicators should not be scaled numbers",
LineAndFile(ast));
palias->SetIndex(sr);
}
else if (ast->getType() == IDENT || ast->getType() == Qalias)
{
palias->SetName(ast->getText().c_str());
}
else
{
Assert(false);
}
}
/*----------------------------------------------------------------------------------------------
Process a list of associations.
----------------------------------------------------------------------------------------------*/
void GrcManager::ProcessAssociations(RefAST ast, GdlRuleTable *prultbl, GdlRuleItem * prit,
int lrc)
{
GrpLineAndFile lnf = LineAndFile(ast);
if (lrc == 0) // context
g_errorList.AddError(1161, NULL,
"Cannot set associations in the context",
lnf);
else if (lrc == 2) // lhs
g_errorList.AddError(1162, NULL,
"Cannot set associations in the lhs",
lnf);
else if (!prultbl->Substitution())
g_errorList.AddError(1163, NULL,
"Cannot set associations in the ",
prultbl->NameSymbol()->FullName(),
" table",
lnf);
else
{
GdlSubstitutionItem * pritsub = dynamic_cast<GdlSubstitutionItem *>(prit);
if (pritsub)
{
RefAST astAssoc = ast->getFirstChild();
while (astAssoc)
{
GdlAlias aliasAssoc;
ProcessSlotIndicator(astAssoc, &aliasAssoc);
(aliasAssoc.Index() == -1)?
pritsub->AddAssociation(lnf, aliasAssoc.Name()) :
pritsub->AddAssociation(lnf, aliasAssoc.Index());
astAssoc = astAssoc->getNextSibling();
}
}
else
{
g_errorList.AddError(1164, NULL,
"Cannot set associations in a rule with no lhs",
lnf);
}
}
}
/*----------------------------------------------------------------------------------------------
Traverse the slot attribute assignment tree, adding the assignments to the rule item.
This will also handle feature assignments.
----------------------------------------------------------------------------------------------*/
void GrcManager::WalkSlotAttrTree(RefAST ast, GdlRuleItem * prit, std::vector<std::string> & vsta)
{
if (!ast)
return;
RefAST astNextID = ast->getFirstChild();
vsta.push_back(astNextID->getText().c_str());
int nodetyp = ast->getType();
if (nodetyp == OP_EQ || nodetyp == OP_PLUSEQUAL || nodetyp == OP_MINUSEQUAL
|| nodetyp == OP_MULTEQUAL || nodetyp == OP_DIVEQUAL)
// || nodetyp == OP_ANDEQUAL || nodetyp == OP_OREQUAL -- not implemented
{
Symbol psymOp = SymbolTable()->FindSymbol(ast->getText().c_str());
Assert(psymOp);
Symbol psymSlotAttr = SymbolTable()->FindSlotAttr(GrcStructName(vsta), LineAndFile(ast));
Symbol psymFeature = SymbolTable()->FindFeature(GrcStructName(vsta), LineAndFile(ast));
if (psymSlotAttr && psymFeature)
{
g_errorList.AddError(1165, NULL,
GrcStructName(vsta).FullString(),
" is ambiguous with regards to being a feature or slot attribute",
LineAndFile(ast));
}
else if (!psymSlotAttr && !psymFeature)
{
g_errorList.AddError(1165, NULL,
"Invalid slot attribute: ",
GrcStructName(vsta).FullString(),
LineAndFile(ast));
}
else
{
RefAST astValue = astNextID->getNextSibling();
if (astValue->getType() == Zfunction)
{
ProcessFunction(astValue, vsta, true, prit, psymOp);
}
else
{
GdlExpression * pexpValue = WalkExpressionTree(astValue);
GdlAttrValueSpec * pavs = (psymSlotAttr)
? new GdlAttrValueSpec(psymSlotAttr, psymOp, pexpValue)
: new GdlAttrValueSpec(psymFeature, psymOp, pexpValue);
prit->AddAttrValueSpec(pavs);
}
}
}
else
{
Assert(ast->getType() == OP_DOT || ast->getType() == ZdotStruct);
RefAST astT = astNextID->getNextSibling();
while (astT)
{
WalkSlotAttrTree(astT, prit, vsta);
astT = astT->getNextSibling();
}
}
vsta.pop_back();
}
/*----------------------------------------------------------------------------------------------
Process an expression.
----------------------------------------------------------------------------------------------*/
GdlExpression * GrcManager::WalkExpressionTree(RefAST ast)
{
GdlExpression * pexpRet = NULL;
GdlExpression * pexp1 = NULL;
GdlExpression * pexp2 = NULL;
GdlExpression * pexpCond = NULL;
GdlExpression * pexpSel = NULL;
GdlSlotRefExpression * pexpsrSel = NULL;
Symbol psymOp;
Symbol psymName;
RefAST astOperand1;
RefAST astOperand2;
RefAST astOperand3;
RefAST astT;
RefAST astCond;
RefAST astSel;
RefAST astName;
RefAST astCluster;
RefAST astText;
RefAST astCodePage;
int nValue;
int nCluster;
bool fM;
bool fGlyphAttr = false;
std::vector<std::string> vsta;
GdlLookupExpression * pexplook;
int nodetyp = ast->getType();
switch (nodetyp)
{
case OP_EQ:
g_errorList.AddError(1166, NULL,
"'=' cannot be used in an expression (use '==')",
LineAndFile(ast));
// fall through and treat as if it were ==
case OP_EQUALEQUAL:
case OP_NE:
case OP_GE:
case OP_GT:
case OP_LT:
case OP_LE:
case OP_PLUS:
case OP_MULT:
case OP_DIV:
case OP_AND:
case OP_OR:
case OP_BITAND:
case OP_BITOR:
if (nodetyp == OP_EQ)
psymOp = SymbolTable()->FindSymbol("==");
else
psymOp = SymbolTable()->FindSymbol(ast->getText().c_str());
Assert(psymOp);
astOperand1 = ast->getFirstChild();
Assert(astOperand1);
astOperand2 = astOperand1->getNextSibling();
Assert(astOperand2);
Assert(!astOperand2->getNextSibling());
pexp1 = WalkExpressionTree(astOperand1);
pexp2 = WalkExpressionTree(astOperand2);
pexpRet = new GdlBinaryExpression(psymOp, pexp1, pexp2);
break;
case LITERAL_min:
case LITERAL_max:
psymOp = SymbolTable()->FindSymbol(ast->getText().c_str());
Assert(psymOp);
astOperand1 = ast->getFirstChild();
if (!astOperand1)
{
g_errorList.AddError(1167, NULL,
"No arguments for ",
ast->getText().c_str(),
" function",
LineAndFile(ast));
return NULL;
}
pexp1 = WalkExpressionTree(astOperand1);
astOperand2 = astOperand1->getNextSibling();
while (astOperand2)
{
pexp2 = WalkExpressionTree(astOperand2);
pexp1 = new GdlBinaryExpression(psymOp, pexp1, pexp2);
astOperand2 = astOperand2->getNextSibling();
}
pexpRet = pexp1;
break;
case OP_MINUS:
psymOp = SymbolTable()->FindSymbol(ast->getText().c_str());
Assert(psymOp);
astOperand1 = ast->getFirstChild();
Assert(astOperand1);
astOperand2 = astOperand1->getNextSibling();
if (astOperand2)
{
Assert(!astOperand2->getNextSibling());
pexp1 = WalkExpressionTree(astOperand1);
pexp2 = WalkExpressionTree(astOperand2);
pexpRet = new GdlBinaryExpression(psymOp, pexp1, pexp2);
break;
}
else
{ // fall through to make a unary expression
}
case OP_NOT:
case OP_BITNOT:
psymOp = SymbolTable()->FindSymbol(ast->getText().c_str());
Assert(psymOp);
astOperand1 = ast->getFirstChild();
Assert(astOperand1);
Assert(!astOperand1->getNextSibling());
pexp1 = WalkExpressionTree(astOperand1);
pexpRet = new GdlUnaryExpression(psymOp, pexp1);
break;
case OP_QUESTION:
astCond = ast->getFirstChild();
Assert(astCond);
astOperand1 = astCond->getNextSibling();
Assert(astOperand1);
Assert(astOperand1->getNextSibling()->getType() == OP_COLON);
astOperand2 = astOperand1->getNextSibling()->getNextSibling();
Assert(astOperand2);
pexpCond = WalkExpressionTree(astCond);
pexp1 = WalkExpressionTree(astOperand1);
pexp2 = WalkExpressionTree(astOperand2);
pexpRet = new GdlCondExpression(pexpCond, pexp1, pexp2);
break;
case Zlookup:
astT = ast->getFirstChild();
astSel = nullAST;
pexpSel = NULL;
if (astT->getType() == OP_AT)
{
astSel = astT;
astT = astT->getNextSibling();
pexpSel = WalkExpressionTree(astSel);
pexpsrSel = dynamic_cast<GdlSlotRefExpression *>(pexpSel);
Assert(pexpsrSel);
}
astName = astT;
vsta.clear();
psymName = IdentifierSymbol(astName, vsta, &fGlyphAttr);
Assert(psymName);
// GdlLookupExpression::LookupType lookType;
// if (psymName->FitsSymbolType(ksymtFeature))
// lookType = GdlLookupExpression::klookFeature;
// else if (psymName->FitsSymbolType(ksymtGlyphAttr))
// lookType = GdlLookupExpression::klookGlyph;
// else if (psymName->FitsSymbolType(ksymtGlyphMetric))
// lookType = GdlLookupExpression::klookGlyph;
// else if (psymName->FitsSymbolType(ksymtSlotAttr))
// lookType = GdlLookupExpression::klookSlot;
// else
// lookType = GdlLookupExpression::klookUnknown;
astCluster = astName->getNextSibling();
nCluster = 0; // default
if (astCluster)
{
Assert(astCluster->getType() == Zcluster);
Assert(astCluster->getFirstChild());
nCluster = NumericValue(astCluster->getFirstChild(), &fM);
Assert(!fM);
}
if (pexpSel)
pexpRet = new GdlLookupExpression(psymName, pexpsrSel, nCluster);
else if (psymName->FitsSymbolType(ksymtClass))
pexpRet = new GdlClassMemberExpression(psymName);
else
pexpRet = new GdlLookupExpression(psymName, -1, nCluster);
pexplook = dynamic_cast<GdlLookupExpression*>(pexpRet);
pexplook->SetGlyphAttr(fGlyphAttr);
break;
case OP_AT:
astT = ast->getFirstChild();
Assert(!astT->getNextSibling());
if (astT->getType() == IDENT || astT->getType() == Qalias)
pexpRet = new GdlSlotRefExpression(astT->getText().c_str());
else if (astT->getType() == LITERAL_true || astT->getType() == LITERAL_false)
{
Assert(false); // grammar should not allow this
g_errorList.AddError(1168, NULL,
"Slot reference cannot contain a boolean",
LineAndFile(astT));
pexpRet = new GdlSlotRefExpression(ast->getType() == LITERAL_true);
}
else
{
if (astT->getType() == OP_MINUS)
{
Assert(false); // grammar should not allow this
nValue = NumericValue(astT->getFirstChild(), &fM);
nValue *= -1;
}
else
nValue = NumericValue(astT, &fM);
if (fM)
{
Assert(false); // grammar should not allow this
g_errorList.AddError(1169, NULL,
"Slot reference cannot contain a scaled value",
LineAndFile(astT));
}
pexpRet = new GdlSlotRefExpression(nValue);
}
break;
case LIT_INT:
nValue = NumericValue(ast, &fM);
if (fM)
pexpRet = new GdlNumericExpression(nValue, MUnits());
else
pexpRet = new GdlNumericExpression(nValue, false);
break;
case LITERAL_true:
pexpRet = new GdlNumericExpression(1, true);
break;
case LITERAL_false:
pexpRet = new GdlNumericExpression(0, true);
break;
case LIT_STRING:
pexpRet = new GdlStringExpression(ast->getText().c_str(), CodePage());
break;
case LITERAL_string:
// string function
astText = ast->getFirstChild();
astCodePage = astText->getNextSibling();
if (astCodePage)
{
Assert(astCodePage->getType() == LIT_INT);
nValue = NumericValue(astCodePage, &fM);
pexpRet = new GdlStringExpression(astText->getText().c_str(), nValue);
}
else
pexpRet = new GdlStringExpression(astText->getText().c_str(), CodePage());
break;
default:
Assert(false);
}
pexpRet->SetLineAndFile(LineAndFile(ast));
return pexpRet;
} // end of WalkExpressionTree
/*----------------------------------------------------------------------------------------------
Return the line information the tree node is associated with.
----------------------------------------------------------------------------------------------*/
GrpLineAndFile GrcManager::LineAndFile(RefAST ast)
{
GrpASTNode * wrNode = dynamic_cast<GrpASTNode *>(ast->getNode());
Assert(wrNode);
if (!wrNode)
return GrpLineAndFile(0, 0, "");
GrpLineAndFile lnf = wrNode->LineAndFile();
if (!lnf.NotSet())
return lnf;
if (ast->getFirstChild())
return LineAndFile(ast->getFirstChild());
if (ast->getNextSibling())
return LineAndFile(ast->getNextSibling());
return GrpLineAndFile(0, 0, "");
}
/*----------------------------------------------------------------------------------------------
Return the numeric value that is the equivalent of the text in the node. Return a
flag indicating if there is an "m" on the end. The following are legal strings:
123
0x89AB
0x89abcde
123m
123M
U+89ab
Return 0 if the value is not a legal numeric string.
----------------------------------------------------------------------------------------------*/
int GrcManager::NumericValue(RefAST ast, bool * pfM)
{
Assert(ast->getType() == LIT_INT || ast->getType() == LIT_UHEX);
std::string str = ast->getText();
int nRet = 0;
unsigned int ich = 0;
int nBase = 10;
if (ast->getType() == LIT_UHEX)
{
if (str[0] == 'U' && str[1] == '+')
ich = 2;
nBase = 16;
}
else if (str[0] == '0' && str[1] == 'x')
{
ich = 2;
nBase = 16;
}
else if (str[0] == 'x')
{
ich = 1;
nBase = 16;
}
while (ich < str.length())
{
if (str[ich] >= 0 && str[ich] <= '9')
nRet = nRet * nBase + (str[ich] - '0');
else if (nBase == 16 && str[ich] >= 'A' && str[ich] <= 'F')
nRet = nRet * nBase + (str[ich] - 'A' + 10);
else if (nBase == 16 && str[ich] >= 'a' && str[ich] <= 'f')
nRet = nRet * nBase + (str[ich] - 'a' + 10);
else
break;
ich++;
}
*pfM = (str[ich] == 'm' || str[ich] == 'M');
Assert(!*pfM || ast->getType() != LIT_UHEX);
return nRet;
}
int GrcManager::NumericValue(RefAST ast)
{
bool fM;
int nRet = NumericValue(ast, &fM);
Assert(!fM);
return nRet;
}
/*----------------------------------------------------------------------------------------------
Return the symbol corresponding to the dotted identifier.
If the first element of the symbol is "glyph", that is stripped out, but its presence
is indicated by the boolean flag, which tells that we want to treat the attribute name
as a glyph attribute, not a slot attribute.
----------------------------------------------------------------------------------------------*/
Symbol GrcManager::IdentifierSymbol(RefAST ast, std::vector<std::string> & vsta,
bool * pfGlyphAttr)
{
if (ast->getType() == OP_DOT)
{
RefAST ast1 = ast->getFirstChild();
if (vsta.size() == 0 && strcmp(ast1->getText().c_str(), "glyph") == 0)
*pfGlyphAttr = true; // treat as glyph attribute
else
vsta.push_back(ast1->getText().c_str());
return IdentifierSymbol(ast1->getNextSibling(), vsta, pfGlyphAttr);
}
Assert(ast->getType() == IDENT || ast->getType() == LITERAL_glyph);
if (vsta.size() == 0 && strcmp(ast->getText().c_str(), "glyph") == 0)
*pfGlyphAttr = true; // treat as glyph attribute
else
vsta.push_back(ast->getText().c_str());
Symbol psymRet = SymbolTable()->FindSymbol(GrcStructName(vsta));
if (!psymRet)
{
// If the symbol is of the form <class>.<predefined-glyph-attr>, add the symbol
// as a glyph attribute.
ExpressionType expt;
SymbolType symt;
GrcStructName xns(vsta);
if (ClassPredefGlyphAttr(vsta, &expt, &symt))
psymRet = SymbolTable()->AddGlyphAttrSymbol(xns, LineAndFile(ast), expt,
(symt == ksymtGlyphMetric));
else
psymRet = SymbolTable()->AddSymbol(xns, ksymtInvalid, LineAndFile(ast));
}
return psymRet;
}
/*----------------------------------------------------------------------------------------------
Return true if the given array of symbol names is of the form
<class>.<predefined-glyph-attr>.
----------------------------------------------------------------------------------------------*/
bool GrcManager::ClassPredefGlyphAttr(std::vector<std::string> & vsta,
ExpressionType * pexpt, SymbolType * psymt)
{
std::string sta1 = vsta[0];
Symbol psymClass = SymbolTable()->FindSymbol(sta1);
if (!psymClass || !psymClass->FitsSymbolType(ksymtClass))
return false;
std::vector<std::string> vstaMinusClass;
for (size_t ista = 1; ista < vsta.size(); ista++)
vstaMinusClass.push_back(vsta[ista]);
Symbol psymGlyphAttr = SymbolTable()->FindSymbol(GrcStructName(vstaMinusClass));
if (!psymGlyphAttr)
return false;
if (psymGlyphAttr->FitsSymbolType(ksymtGlyphAttr) && !psymGlyphAttr->IsUserDefined())
{
*pexpt = psymGlyphAttr->ExpType();
*psymt = ksymtGlyphAttr;
return true;
}
else if (psymGlyphAttr->FitsSymbolType(ksymtGlyphMetric))
{
*pexpt = psymGlyphAttr->ExpType();
*psymt = ksymtGlyphMetric;
return true;
}
return false;
}
/**********************************************************************************************/
/*----------------------------------------------------------------------------------------------
Perform initial checking and clean-up on a newly added rule.
----------------------------------------------------------------------------------------------*/
void GdlRule::InitialChecks()
{
CheckInputClass();
ConvertLhsOptRangesToContext();
}
/*----------------------------------------------------------------------------------------------
Check that there are no unused underscores in the context or rhs, or that an item didn't
show up as an underscore in both the lhs and rhs. In other words, check that all items
have either an input class or an output class.
----------------------------------------------------------------------------------------------*/
void GdlRule::CheckInputClass()
{
for (size_t iritT = 0; iritT < m_vprit.size(); iritT++)
{
Symbol psymInput = m_vprit[iritT]->m_psymInput;
if (!psymInput)
{
g_errorList.AddError(1170, this,
"Mismatched items between lhs and rhs");
psymInput = g_cman.SymbolTable()->FindSymbol("_");
m_vprit[iritT]->m_psymInput = psymInput;
}
if (!psymInput->FitsSymbolType(ksymtClass) &&
!psymInput->FitsSymbolType(ksymtSpecialLb))
{
// The only other thing it can be:
Assert(psymInput->FitsSymbolType(ksymtSpecialUnderscore));
GdlSubstitutionItem * pritsub = dynamic_cast<GdlSubstitutionItem*>(m_vprit[iritT]);
if (pritsub)
{
Symbol psymOutput = pritsub->OutputSymbol();
if (!psymOutput ||
(!psymOutput->FitsSymbolType(ksymtClass) &&
(!psymOutput->FitsSymbolType(ksymtSpecialAt))))
g_errorList.AddError(1171, this,
"Item ",
pritsub->PosString(),
": no input or output class specified");
}
else
g_errorList.AddError(1172, this,
"Mismatched items among context, lhs and/or rhs");
}
}
}
/*----------------------------------------------------------------------------------------------
Convert the optional ranges that are relative to the lhs to be relative to the
context.
If there is a conflict between the lhs ranges and the context ranges, record an
error and clear the optional ranges.
Note that Lhs and Rhs items are always GdlSetAttrItems or GdlSubstitutionItems.
----------------------------------------------------------------------------------------------*/
void GdlRule::ConvertLhsOptRangesToContext()
{
Assert(m_viritOptRangeStart.size() == m_viritOptRangeEnd.size());
Assert(m_viritOptRangeStart.size() == m_vfOptRangeContext.size());
if (m_viritOptRangeStart.size() == 0)
return;
// Make a mapping from lhs/rhs items to corresponding indices in the context.
std::vector<int> viritToContext;
for (auto iritT = 0U; iritT < m_vprit.size(); ++iritT)
{
GdlSetAttrItem * pritset = dynamic_cast<GdlSetAttrItem*>(m_vprit[iritT]);
if (pritset)
{
// Left-hand-side item.
viritToContext.push_back(iritT);
}
}
// Check that there is no overlap or interference between lhs ranges and
// context ranges.
for (auto iirit = 0U; iirit < m_viritOptRangeStart.size(); ++iirit)
{
if (!m_vfOptRangeContext[iirit])
{
int iiritLhs = iirit;
int iritLhs1 = m_viritOptRangeStart[iiritLhs];
int iritLhs2 = m_viritOptRangeEnd[iiritLhs];
if (iritLhs1 < signed(viritToContext.size()) && iritLhs2 < signed(viritToContext.size()))
{
iritLhs1 = viritToContext[iritLhs1];
iritLhs2 = viritToContext[iritLhs2];
for (size_t iiritC = 0; iiritC < m_viritOptRangeStart.size(); iiritC++)
{
if (m_vfOptRangeContext[iiritC])
{
int iritC1 = m_viritOptRangeStart[iiritC];
int iritC2 = m_viritOptRangeEnd[iiritC];
if ((iritLhs1 >= iritC1 && iritLhs1 <= iritC2) ||
(iritLhs2 >= iritC1 && iritLhs2 <= iritC2))
{
g_errorList.AddError(1127, this,
"Conflict between optional ranges in lhs and context");
m_viritOptRangeStart.clear();
m_viritOptRangeEnd.clear();
m_vfOptRangeContext.clear();
return;
}
}
}
}
else
{
// Error condition previous handled--ignore this optional range.
m_vfOptRangeContext.erase(m_vfOptRangeContext.begin() + iirit);
m_viritOptRangeStart.erase(m_viritOptRangeStart.begin() + iirit);
m_viritOptRangeEnd.erase(m_viritOptRangeEnd.begin() + iirit);
--iirit;
}
}
}
// Now do the conversion.
for (auto iirit = 0U; iirit < m_viritOptRangeStart.size(); ++iirit)
{
if (!m_vfOptRangeContext[iirit])
{
int irit1 = m_viritOptRangeStart[iirit];
int irit2 = m_viritOptRangeEnd[iirit];
Assert(irit1 < static_cast<int>(viritToContext.size()));
Assert(irit2 < static_cast<int>(viritToContext.size()));
m_viritOptRangeStart[iirit] = viritToContext[irit1];
m_viritOptRangeEnd[iirit] = viritToContext[irit2];
// Just to keep things tidy:
m_vfOptRangeContext[iirit] = true;
}
}
}
/***********************************************************************************************
Debuggers
***********************************************************************************************/
/*----------------------------------------------------------------------------------------------
Write a text version of the parse tree to a file.
----------------------------------------------------------------------------------------------*/
void GrcManager::DebugParseTree(RefAST ast)
{
std::ofstream strmOut;
strmOut.open("dbg_parsetree.txt");
if (strmOut.fail())
{
g_errorList.AddWarning(6506, NULL,
"Error in writing to file ",
"dbg_parsetree.txt");
return;
}
strmOut << "PARSE TREE\n\n";
ast->Trace(strmOut, nullptr, 0);
strmOut.close();
}
|