1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890
|
#!/usr/bin/env python
"""
Fortran 2003 Syntax Rules.
-----
Permission to use, modify, and distribute this software is given under the
terms of the NumPy License. See http://scipy.org.
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
Author: Pearu Peterson <pearu@cens.ioc.ee>
Created: Oct 2006
-----
"""
import re
from splitline import string_replace_map
import pattern_tools as pattern
from readfortran import FortranReaderBase
###############################################################################
############################## BASE CLASSES ###################################
###############################################################################
class NoMatchError(Exception):
pass
class ParseError(Exception):
pass
class Base(object):
""" Base class for Fortran 2003 syntax rules.
All Base classes have the following attributes:
.string - original argument to construct a class instance, it's type
is either str or FortranReaderBase.
.item - Line instance (holds label) or None.
"""
subclasses = {}
def __new__(cls, string, parent_cls = None):
"""
"""
if parent_cls is None:
parent_cls = [cls]
elif cls not in parent_cls:
parent_cls.append(cls)
#print '__new__:',cls.__name__,`string`
match = cls.__dict__.get('match', None)
if isinstance(string, FortranReaderBase) and not issubclass(cls, BlockBase) \
and match is not None:
reader = string
item = reader.get_item()
if item is None: return
try:
obj = cls(item.line, parent_cls = parent_cls)
except NoMatchError:
obj = None
if obj is None:
reader.put_item(item)
return
obj.item = item
return obj
errmsg = '%s: %r' % (cls.__name__, string)
if match is not None:
try:
result = cls.match(string)
except NoMatchError, msg:
if str(msg)==errmsg: # avoid recursion 1.
raise
result = None
else:
result = None
#print '__new__:result:',cls.__name__,`string,result`
if isinstance(result, tuple):
obj = object.__new__(cls)
obj.string = string
obj.item = None
if hasattr(cls, 'init'): obj.init(*result)
return obj
elif isinstance(result, Base):
return result
elif result is None:
for subcls in Base.subclasses.get(cls.__name__,[]):
if subcls in parent_cls: # avoid recursion 2.
continue
#print '%s:%s: %r' % (cls.__name__,subcls.__name__,string)
try:
obj = subcls(string, parent_cls = parent_cls)
except NoMatchError, msg:
obj = None
if obj is not None:
return obj
else:
raise AssertionError,`result`
raise NoMatchError,errmsg
## def restore_reader(self):
## self._item.reader.put_item(self._item)
## return
def init(self, *items):
self.items = items
return
def torepr(self):
return '%s(%s)' % (self.__class__.__name__, ', '.join(map(repr,self.items)))
def compare(self, other):
return cmp(self.items,other.items)
def __str__(self): return self.tostr()
def __repr__(self): return self.torepr()
def __cmp__(self, other):
if self is other: return 0
if not isinstance(other, self.__class__): return cmp(self.__class__, other.__class__)
return self.compare(other)
def tofortran(self, tab='', isfix=None):
return tab + str(self)
class BlockBase(Base):
"""
<block-base> = [ <startcls> ]
[ <subcls> ]...
...
[ <subcls> ]...
[ <endcls> ]
"""
def match(startcls, subclasses, endcls, reader):
assert isinstance(reader,FortranReaderBase),`reader`
content = []
if startcls is not None:
try:
obj = startcls(reader)
except NoMatchError:
obj = None
if obj is None: return
content.append(obj)
if endcls is not None:
classes = subclasses + [endcls]
else:
classes = subclasses[:]
i = 0
while 1:
cls = classes[i]
try:
obj = cls(reader)
except NoMatchError:
obj = None
if obj is None:
j = i
for cls in classes[i+1:]:
j += 1
try:
obj = cls(reader)
except NoMatchError:
obj = None
if obj is not None:
break
if obj is not None:
i = j
if obj is not None:
content.append(obj)
if endcls is not None and isinstance(obj, endcls): break
continue
if endcls is not None:
item = reader.get_item()
if item is not None:
reader.error('failed to parse with %s, skipping.' % ('|'.join([c.__name__ for c in classes[i:]])), item)
continue
if hasattr(content[0],'name'):
reader.error('unexpected eof file while looking line for <%s> of %s.'\
% (classes[-1].__name__.lower().replace('_','-'), content[0].name))
else:
reader.error('unexpected eof file while looking line for <%s>.'\
% (classes[-1].__name__.lower().replace('_','-')))
break
if not content: return
if startcls is not None and endcls is not None:
# check names of start and end statements:
start_stmt = content[0]
end_stmt = content[-1]
if isinstance(end_stmt, endcls) and hasattr(end_stmt, 'get_name') and hasattr(start_stmt, 'get_name'):
if end_stmt.get_name() is not None:
if start_stmt.get_name() != end_stmt.get_name():
end_stmt._item.reader.error('expected <%s-name> is %s but got %s. Ignoring.'\
% (end_stmt.get_type().lower(), start_stmt.get_name(), end_stmt.get_name()))
else:
end_stmt.set_name(start_stmt.get_name())
return content,
match = staticmethod(match)
def init(self, content):
self.content = content
return
def compare(self, other):
return cmp(self.content,other.content)
def tostr(self):
return self.tofortran()
def torepr(self):
return '%s(%s)' % (self.__class__.__name__,', '.join(map(repr, self.content)))
def tofortran(self, tab='', isfix=None):
l = []
start = self.content[0]
end = self.content[-1]
extra_tab = ''
if isinstance(end, EndStmtBase):
extra_tab = ' '
l.append(start.tofortran(tab=tab,isfix=isfix))
for item in self.content[1:-1]:
l.append(item.tofortran(tab=tab+extra_tab,isfix=isfix))
if len(self.content)>1:
l.append(end.tofortran(tab=tab,isfix=isfix))
return '\n'.join(l)
## def restore_reader(self):
## content = self.content[:]
## content.reverse()
## for obj in content:
## obj.restore_reader()
## return
class SequenceBase(Base):
"""
<sequence-base> = <obj>, <obj> [ , <obj> ]...
"""
def match(separator, subcls, string):
line, repmap = string_replace_map(string)
if isinstance(separator, str):
splitted = line.split(separator)
else:
splitted = separator[1].split(line)
separator = separator[0]
if len(splitted)<=1: return
lst = []
for p in splitted:
lst.append(subcls(repmap(p.strip())))
return separator, tuple(lst)
match = staticmethod(match)
def init(self, separator, items):
self.separator = separator
self.items = items
return
def tostr(self):
s = self.separator
if s==',': s = s + ' '
elif s==' ': pass
else: s = ' ' + s + ' '
return s.join(map(str, self.items))
def torepr(self): return '%s(%r, %r)' % (self.__class__.__name__, self.separator, self.items)
def compare(self, other):
return cmp((self.separator,self.items),(other.separator,self.items))
class UnaryOpBase(Base):
"""
<unary-op-base> = <unary-op> <rhs>
"""
def tostr(self):
return '%s %s' % tuple(self.items)
def match(op_pattern, rhs_cls, string):
m = op_pattern.match(string)
if not m: return
#if not m: return rhs_cls(string)
rhs = string[m.end():].lstrip()
if not rhs: return
op = string[:m.end()].rstrip().upper()
return op, rhs_cls(rhs)
match = staticmethod(match)
class BinaryOpBase(Base):
"""
<binary-op-base> = <lhs> <op> <rhs>
<op> is searched from right by default.
"""
def match(lhs_cls, op_pattern, rhs_cls, string, right=True):
line, repmap = string_replace_map(string)
if isinstance(op_pattern, str):
if right:
t = line.rsplit(op_pattern,1)
else:
t = line.split(op_pattern,1)
if len(t)!=2: return
lhs, rhs = t[0].rstrip(), t[1].lstrip()
op = op_pattern
else:
if right:
t = op_pattern.rsplit(line)
else:
t = op_pattern.lsplit(line)
if t is None or len(t)!=3: return
lhs, op, rhs = t
lhs = lhs.rstrip()
rhs = rhs.lstrip()
op = op.upper()
if not lhs: return
if not rhs: return
lhs_obj = lhs_cls(repmap(lhs))
rhs_obj = rhs_cls(repmap(rhs))
return lhs_obj, op, rhs_obj
match = staticmethod(match)
def tostr(self):
return '%s %s %s' % tuple(self.items)
class SeparatorBase(Base):
"""
<separator-base> = [ <lhs> ] : [ <rhs> ]
"""
def match(lhs_cls, rhs_cls, string, require_lhs=False, require_rhs=False):
line, repmap = string_replace_map(string)
if ':' not in line: return
lhs,rhs = line.split(':',1)
lhs = lhs.rstrip()
rhs = rhs.lstrip()
lhs_obj, rhs_obj = None, None
if lhs:
if lhs_cls is None: return
lhs_obj = lhs_cls(repmap(lhs))
elif require_lhs:
return
if rhs:
if rhs_cls is None: return
rhs_obj = rhs_cls(repmap(rhs))
elif require_rhs:
return
return lhs_obj, rhs_obj
match = staticmethod(match)
def tostr(self):
s = ''
if self.items[0] is not None:
s += '%s :' % (self.items[0])
else:
s += ':'
if self.items[1] is not None:
s += ' %s' % (self.items[1])
return s
class KeywordValueBase(Base):
"""
<keyword-value-base> = [ <lhs> = ] <rhs>
"""
def match(lhs_cls, rhs_cls, string, require_lhs = True, upper_lhs = False):
if require_lhs and '=' not in string: return
if isinstance(lhs_cls, (list, tuple)):
for s in lhs_cls:
try:
obj = KeywordValueBase.match(s, rhs_cls, string, require_lhs=require_lhs, upper_lhs=upper_lhs)
except NoMatchError:
obj = None
if obj is not None: return obj
return obj
lhs,rhs = string.split('=',1)
lhs = lhs.rstrip()
rhs = rhs.lstrip()
if not rhs: return
if not lhs:
if require_lhs: return
return None, rhs_cls(rhs)
if isinstance(lhs_cls, str):
if upper_lhs:
lhs = lhs.upper()
if lhs_cls!=lhs: return
return lhs, rhs_cls(rhs)
return lhs_cls(lhs),rhs_cls(rhs)
match = staticmethod(match)
def tostr(self):
if self.items[0] is None: return str(self.items[1])
return '%s = %s' % tuple(self.items)
class BracketBase(Base):
"""
<bracket-base> = <left-bracket-base> <something> <right-bracket>
"""
def match(brackets, cls, string, require_cls=True):
i = len(brackets)/2
left = brackets[:i]
right = brackets[-i:]
if string.startswith(left) and string.endswith(right):
line = string[i:-i].strip()
if not line:
if require_cls:
return
return left,None,right
return left,cls(line),right
return
match = staticmethod(match)
def tostr(self):
if self.items[1] is None:
return '%s%s' % (self.items[0], self.items[2])
return '%s%s%s' % tuple(self.items)
class NumberBase(Base):
"""
<number-base> = <number> [ _ <kind-param> ]
"""
def match(number_pattern, string):
m = number_pattern.match(string)
if m is None: return
return m.group('value').upper(),m.group('kind_param')
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return str(self.items[0])
return '%s_%s' % tuple(self.items)
def compare(self, other):
return cmp(self.items[0], other.items[0])
class CallBase(Base):
"""
<call-base> = <lhs> ( [ <rhs> ] )
"""
def match(lhs_cls, rhs_cls, string, upper_lhs = False, require_rhs=False):
if not string.endswith(')'): return
line, repmap = string_replace_map(string)
i = line.find('(')
if i==-1: return
lhs = line[:i].rstrip()
if not lhs: return
rhs = line[i+1:-1].strip()
lhs = repmap(lhs)
if upper_lhs:
lhs = lhs.upper()
rhs = repmap(rhs)
if isinstance(lhs_cls, str):
if lhs_cls!=lhs: return
else:
lhs = lhs_cls(lhs)
if rhs:
if isinstance(rhs_cls, str):
if rhs_cls!=rhs: return
else:
rhs = rhs_cls(rhs)
return lhs, rhs
elif require_rhs:
return
return lhs, None
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return '%s()' % (self.items[0])
return '%s(%s)' % (self.items[0], self.items[1])
class CALLBase(CallBase):
"""
<CALL-base> = <LHS> ( [ <rhs> ] )
"""
def match(lhs_cls, rhs_cls, string, require_rhs = False):
return CallBase.match(lhs_cls, rhs_cls, string, upper_lhs=True, require_rhs = require_rhs)
match = staticmethod(match)
class StringBase(Base):
"""
<string-base> = <xyz>
"""
def match(pattern, string):
if isinstance(pattern, (list,tuple)):
for p in pattern:
obj = StringBase.match(p, string)
if obj is not None: return obj
return
if isinstance(pattern, str):
if len(pattern)==len(string) and pattern==string: return string,
return
if pattern.match(string): return string,
return
match = staticmethod(match)
def init(self, string):
self.string = string
return
def tostr(self): return str(self.string)
def torepr(self): return '%s(%r)' % (self.__class__.__name__, self.string)
def compare(self, other):
return cmp(self.string,other.string)
class STRINGBase(StringBase):
"""
<STRING-base> = <XYZ>
"""
match = staticmethod(StringBase.match)
def match(pattern, string):
if isinstance(pattern, (list,tuple)):
for p in pattern:
obj = STRINGBase.match(p, string)
if obj is not None: return obj
return
STRING = string.upper()
if isinstance(pattern, str):
if len(pattern)==len(string) and pattern==STRING: return STRING,
return
if pattern.match(STRING): return STRING,
return
match = staticmethod(match)
class StmtBase(Base):
"""
[ <label> ] <stmt>
"""
def tofortran(self, tab='', isfix=None):
label = None
if self.item is not None: label = self.item.label
if isfix:
colon = ''
c = ' '
else:
colon = ':'
c = ''
if label:
t = c + label + colon
if isfix:
while len(t)<6: t += ' '
else:
tab = tab[len(t):] or ' '
else:
t = ''
return t + tab + str(self)
class EndStmtBase(StmtBase):
"""
<end-stmt-base> = END [ <stmt> [ <stmt-name>] ]
"""
def match(stmt_type, stmt_name, string, require_stmt_type=False):
start = string[:3].upper()
if start != 'END': return
line = string[3:].lstrip()
start = line[:len(stmt_type)].upper()
if start:
if start.replace(' ','') != stmt_type.replace(' ',''): return
line = line[len(stmt_type):].lstrip()
else:
if require_stmt_type: return
line = ''
if line:
if stmt_name is None: return
return stmt_type, stmt_name(line)
return stmt_type, None
match = staticmethod(match)
def init(self, stmt_type, stmt_name):
self.items = [stmt_type, stmt_name]
self.type, self.name = stmt_type, stmt_name
return
def get_name(self): return self.items[1]
def get_type(self): return self.items[0]
def set_name(self, name):
self.items[1] = name
def tostr(self):
if self.items[1] is not None:
return 'END %s %s' % tuple(self.items)
return 'END %s' % (self.items[0])
def torepr(self):
return '%s(%r, %r)' % (self.__class__.__name__, self.type, self.name)
def isalnum(c): return c.isalnum() or c=='_'
class WORDClsBase(Base):
"""
<WORD-cls> = <WORD> [ [ :: ] <cls> ]
"""
def match(pattern, cls, string, check_colons=False, require_cls=False):
if isinstance(pattern, (tuple,list)):
for p in pattern:
try:
obj = WORDClsBase.match(p, cls, string, check_colons=check_colons, require_cls=require_cls)
except NoMatchError:
obj = None
if obj is not None: return obj
return
if isinstance(pattern, str):
if string[:len(pattern)].upper()!=pattern: return
line = string[len(pattern):]
if not line: return pattern, None
if isalnum(line[0]): return
line = line.lstrip()
if check_colons and line.startswith('::'):
line = line[2:].lstrip()
if not line:
if require_cls: return
return pattern, None
if cls is None: return
return pattern, cls(line)
m = pattern.match(string)
if m is None: return
line = string[len(m.group()):]
if pattern.value is not None:
pattern_value = pattern.value
else:
pattern_value = m.group().upper()
if not line: return pattern_value, None
if isalnum(line[0]): return
line = line.lstrip()
if check_colons and line.startswith('::'):
line = line[2:].lstrip()
if not line:
if require_cls: return
return pattern_value, None
if cls is None: return
return pattern_value, cls(line)
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return str(self.items[0])
s = str(self.items[1])
if s and s[0] in '(*':
return '%s%s' % (self.items[0], s)
return '%s %s' % (self.items[0], s)
def tostr_a(self): # colons version of tostr
if self.items[1] is None: return str(self.items[0])
return '%s :: %s' % (self.items[0], self.items[1])
###############################################################################
############################### SECTION 1 ####################################
###############################################################################
#R101: <xyz-list> = <xyz> [ , <xyz> ]...
#R102: <xyz-name> = <name>
#R103: <scalar-xyz> = <xyz>
###############################################################################
############################### SECTION 2 ####################################
###############################################################################
class Program(BlockBase): # R201
"""
<program> = <program-unit>
[ <program-unit> ] ...
"""
subclass_names = []
use_names = ['Program_Unit']
def match(reader):
return BlockBase.match(Program_Unit, [Program_Unit], None, reader)
match = staticmethod(match)
class Program_Unit(Base): # R202
"""
<program-unit> = <main-program>
| <external-subprogram>
| <module>
| <block-data>
"""
subclass_names = ['Main_Program', 'External_Subprogram', 'Module', 'Block_Data']
class External_Subprogram(Base): # R203
"""
<external-subprogram> = <function-subprogram>
| <subroutine-subprogram>
"""
subclass_names = ['Function_Subprogram', 'Subroutine_Subprogram']
class Specification_Part(BlockBase): # R204
"""
<specification-part> = [ <use-stmt> ]...
[ <import-stmt> ]...
[ <implicit-part> ]
[ <declaration-construct> ]...
"""
subclass_names = []
use_names = ['Use_Stmt', 'Import_Stmt', 'Implicit_Part', 'Declaration_Construct']
def match(reader):
return BlockBase.match(None, [Use_Stmt, Import_Stmt, Implicit_Part, Declaration_Construct], None, reader)
match = staticmethod(match)
class Implicit_Part(Base): # R205
"""
<implicit-part> = [ <implicit-part-stmt> ]...
<implicit-stmt>
"""
subclass_names = []
use_names = ['Implicit_Part_Stmt', 'Implicit_Stmt']
class Implicit_Part_Stmt(Base): # R206
"""
<implicit-part-stmt> = <implicit-stmt>
| <parameter-stmt>
| <format-stmt>
| <entry-stmt>
"""
subclass_names = ['Implicit_Stmt', 'Parameter_Stmt', 'Format_Stmt', 'Entry_Stmt']
class Declaration_Construct(Base): # R207
"""
<declaration-construct> = <derived-type-def>
| <entry-stmt>
| <enum-def>
| <format-stmt>
| <interface-block>
| <parameter-stmt>
| <procedure-declaration-stmt>
| <specification-stmt>
| <type-declaration-stmt>
| <stmt-function-stmt>
"""
subclass_names = ['Derived_Type_Def', 'Entry_Stmt', 'Enum_Def', 'Format_Stmt',
'Interface_Block', 'Parameter_Stmt', 'Procedure_Declaration_Stmt',
'Specification_Stmt', 'Type_Declaration_Stmt', 'Stmt_Function_Stmt']
class Execution_Part(BlockBase): # R208
"""
<execution-part> = <executable-construct>
| [ <execution-part-construct> ]...
<execution-part> shall not contain <end-function-stmt>, <end-program-stmt>, <end-subroutine-stmt>
"""
subclass_names = []
use_names = ['Executable_Construct_C201', 'Execution_Part_Construct_C201']
def match(string): return BlockBase.match(Executable_Construct_C201, [Execution_Part_Construct_C201], None, string)
match = staticmethod(match)
class Execution_Part_Construct(Base): # R209
"""
<execution-part-construct> = <executable-construct>
| <format-stmt>
| <entry-stmt>
| <data-stmt>
"""
subclass_names = ['Executable_Construct', 'Format_Stmt', 'Entry_Stmt', 'Data_Stmt']
class Execution_Part_Construct_C201(Base):
subclass_names = ['Executable_Construct_C201', 'Format_Stmt', 'Entry_Stmt', 'Data_Stmt']
class Internal_Subprogram_Part(Base): # R210
"""
<internal-subprogram-part> = <contains-stmt>
<internal-subprogram>
[ <internal-subprogram> ]...
"""
subclass_names = []
use_names = ['Contains_Stmt', 'Internal_Subprogram']
class Internal_Subprogram(Base): # R211
"""
<internal-subprogram> = <function-subprogram>
| <subroutine-subprogram>
"""
subclass_names = ['Function_Subprogram', 'Subroutine_Subprogram']
class Specification_Stmt(Base):# R212
"""
<specification-stmt> = <access-stmt>
| <allocatable-stmt>
| <asynchronous-stmt>
| <bind-stmt>
| <common-stmt>
| <data-stmt>
| <dimension-stmt>
| <equivalence-stmt>
| <external-stmt>
| <intent-stmt>
| <intrinsic-stmt>
| <namelist-stmt>
| <optional-stmt>
| <pointer-stmt>
| <protected-stmt>
| <save-stmt>
| <target-stmt>
| <volatile-stmt>
| <value-stmt>
"""
subclass_names = ['Access_Stmt', 'Allocatable_Stmt', 'Asynchronous_Stmt','Bind_Stmt',
'Common_Stmt', 'Data_Stmt', 'Dimension_Stmt', 'Equivalence_Stmt',
'External_Stmt', 'Intent_Stmt', 'Intrinsic_Stmt', 'Namelist_Stmt',
'Optional_Stmt','Pointer_Stmt','Protected_Stmt','Save_Stmt',
'Target_Stmt','Volatile_Stmt', 'Value_Stmt']
class Executable_Construct(Base):# R213
"""
<executable-construct> = <action-stmt>
| <associate-stmt>
| <case-construct>
| <do-construct>
| <forall-construct>
| <if-construct>
| <select-type-construct>
| <where-construct>
"""
subclass_names = ['Action_Stmt', 'Associate_Stmt', 'Case_Construct', 'Do_Construct',
'Forall_Construct', 'If_Construct', 'Select_Type_Construct', 'Where_Construct']
class Executable_Construct_C201(Base):
subclass_names = Executable_Construct.subclass_names[:]
subclass_names[subclass_names.index('Action_Stmt')] = 'Action_Stmt_C201'
class Action_Stmt(Base):# R214
"""
<action-stmt> = <allocate-stmt>
| <assignment-stmt>
| <backspace-stmt>
| <call-stmt>
| <close-stmt>
| <continue-stmt>
| <cycle-stmt>
| <deallocate-stmt>
| <endfile-stmt>
| <end-function-stmt>
| <end-program-stmt>
| <end-subroutine-stmt>
| <exit-stmt>
| <flush-stmt>
| <forall-stmt>
| <goto-stmt>
| <if-stmt>
| <inquire-stmt>
| <nullify-stmt>
| <open-stmt>
| <pointer-assignment-stmt>
| <print-stmt>
| <read-stmt>
| <return-stmt>
| <rewind-stmt>
| <stop-stmt>
| <wait-stmt>
| <where-stmt>
| <write-stmt>
| <arithmetic-if-stmt>
| <computed-goto-stmt>
"""
subclass_names = ['Allocate_Stmt', 'Assignment_Stmt', 'Backspace_Stmt', 'Call_Stmt',
'Close_Stmt', 'Continue_Stmt', 'Cycle_Stmt', 'Deallocate_Stmt',
'Endfile_Stmt', 'End_Function_Stmt', 'End_Subroutine_Stmt', 'Exit_Stmt',
'Flush_Stmt', 'Forall_Stmt', 'Goto_Stmt', 'If_Stmt', 'Inquire_Stmt',
'Nullify_Stmt', 'Open_Stmt', 'Pointer_Assignment_Stmt', 'Print_Stmt',
'Read_Stmt', 'Return_Stmt', 'Rewind_Stmt', 'Stop_Stmt', 'Wait_Stmt',
'Where_Stmt', 'Write_Stmt', 'Arithmetic_If_Stmt', 'Computed_Goto_Stmt']
class Action_Stmt_C201(Base):
"""
<action-stmt-c201> = <action-stmt>
C201 is applied.
"""
subclass_names = Action_Stmt.subclass_names[:]
subclass_names.remove('End_Function_Stmt')
subclass_names.remove('End_Subroutine_Stmt')
#subclass_names.remove('End_Program_Stmt')
class Action_Stmt_C802(Base):
"""
<action-stmt-c802> = <action-stmt>
C802 is applied.
"""
subclass_names = Action_Stmt.subclass_names[:]
subclass_names.remove('End_Function_Stmt')
subclass_names.remove('End_Subroutine_Stmt')
subclass_names.remove('If_Stmt')
class Action_Stmt_C824(Base):
"""
<action-stmt-c824> = <action-stmt>
C824 is applied.
"""
subclass_names = Action_Stmt.subclass_names[:]
subclass_names.remove('End_Function_Stmt')
subclass_names.remove('End_Subroutine_Stmt')
subclass_names.remove('Continue_Stmt')
subclass_names.remove('Goto_Stmt')
subclass_names.remove('Return_Stmt')
subclass_names.remove('Stop_Stmt')
subclass_names.remove('Exit_Stmt')
subclass_names.remove('Cycle_Stmt')
subclass_names.remove('Arithmetic_If_Stmt')
class Keyword(Base): # R215
"""
<keyword> = <name>
"""
subclass_names = ['Name']
###############################################################################
############################### SECTION 3 ####################################
###############################################################################
#R301: <character> = <alphanumeric-character> | <special-character>
#R302: <alphanumeric-character> = <letter> | <digit> | <underscore>
#R303: <underscore> = _
class Name(StringBase): # R304
"""
<name> = <letter> [ <alphanumeric_character> ]...
"""
subclass_names = []
def match(string): return StringBase.match(pattern.abs_name, string)
match = staticmethod(match)
class Constant(Base): # R305
"""
<constant> = <literal-constant>
| <named-constant>
"""
subclass_names = ['Literal_Constant','Named_Constant']
class Literal_Constant(Base): # R306
"""
<literal-constant> = <int-literal-constant>
| <real-literal-constant>
| <complex-literal-constant>
| <logical-literal-constant>
| <char-literal-constant>
| <boz-literal-constant>
"""
subclass_names = ['Int_Literal_Constant', 'Real_Literal_Constant','Complex_Literal_Constant',
'Logical_Literal_Constant','Char_Literal_Constant','Boz_Literal_Constant']
class Named_Constant(Base): # R307
"""
<named-constant> = <name>
"""
subclass_names = ['Name']
class Int_Constant(Base): # R308
"""
<int-constant> = <constant>
"""
subclass_names = ['Constant']
class Char_Constant(Base): # R309
"""
<char-constant> = <constant>
"""
subclass_names = ['Constant']
#R310: <intrinsic-operator> = <power-op> | <mult-op> | <add-op> | <concat-op> | <rel-op> | <not-op> | <and-op> | <or-op> | <equiv-op>
#R311: <defined-operator> = <defined-unary-op> | <defined-binary-op> | <extended-intrinsic-op>
#R312: <extended-intrinsic-op> = <intrinsic-op>
class Label(StringBase): # R313
"""
<label> = <digit> [ <digit> [ <digit> [ <digit> [ <digit> ] ] ] ]
"""
subclass_names = []
def match(string): return StringBase.match(pattern.abs_label, string)
match = staticmethod(match)
###############################################################################
############################### SECTION 4 ####################################
###############################################################################
class Type_Spec(Base): # R401
"""
<type-spec> = <intrinsic-type-spec>
| <derived-type-spec>
"""
subclass_names = ['Intrinsic_Type_Spec', 'Derived_Type_Spec']
class Type_Param_Value(StringBase): # R402
"""
<type-param-value> = <scalar-int-expr>
| *
| :
"""
subclass_names = ['Scalar_Int_Expr']
use_names = []
def match(string): return StringBase.match(['*',':'], string)
match = staticmethod(match)
class Intrinsic_Type_Spec(WORDClsBase): # R403
"""
<intrinsic-type-spec> = INTEGER [ <kind-selector> ]
| REAL [ <kind-selector> ]
| DOUBLE COMPLEX
| COMPLEX [ <kind-selector> ]
| CHARACTER [ <char-selector> ]
| LOGICAL [ <kind-selector> ]
Extensions:
| DOUBLE PRECISION
| BYTE
"""
subclass_names = []
use_names = ['Kind_Selector','Char_Selector']
def match(string):
for w,cls in [('INTEGER',Kind_Selector),
('REAL',Kind_Selector),
('COMPLEX',Kind_Selector),
('LOGICAL',Kind_Selector),
('CHARACTER',Char_Selector),
(pattern.abs_double_complex_name, None),
(pattern.abs_double_precision_name, None),
('BYTE', None),
]:
try:
obj = WORDClsBase.match(w,cls,string)
except NoMatchError:
obj = None
if obj is not None: return obj
return
match = staticmethod(match)
class Kind_Selector(Base): # R404
"""
<kind-selector> = ( [ KIND = ] <scalar-int-initialization-expr> )
Extensions:
| * <char-length>
"""
subclass_names = []
use_names = ['Char_Length','Scalar_Int_Initialization_Expr']
def match(string):
if string[0]+string[-1] != '()':
if not string.startswith('*'): return
return '*',Char_Length(string[1:].lstrip())
line = string[1:-1].strip()
if line[:4].upper()=='KIND':
line = line[4:].lstrip()
if not line.startswith('='): return
line = line[1:].lstrip()
return '(',Scalar_Int_Initialization_Expr(line),')'
match = staticmethod(match)
def tostr(self):
if len(self.items)==2: return '%s%s' % tuple(self.items)
return '%sKIND = %s%s' % tuple(self.items)
class Signed_Int_Literal_Constant(NumberBase): # R405
"""
<signed-int-literal-constant> = [ <sign> ] <int-literal-constant>
"""
subclass_names = ['Int_Literal_Constant'] # never used because sign is included in pattern
def match(string):
return NumberBase.match(pattern.abs_signed_int_literal_constant_named, string)
match = staticmethod(match)
class Int_Literal_Constant(NumberBase): # R406
"""
<int-literal-constant> = <digit-string> [ _ <kind-param> ]
"""
subclass_names = []
def match(string):
return NumberBase.match(pattern.abs_int_literal_constant_named, string)
match = staticmethod(match)
#R407: <kind-param> = <digit-string> | <scalar-int-constant-name>
#R408: <signed-digit-string> = [ <sign> ] <digit-string>
#R409: <digit-string> = <digit> [ <digit> ]...
#R410: <sign> = + | -
class Boz_Literal_Constant(Base): # R411
"""
<boz-literal-constant> = <binary-constant>
| <octal-constant>
| <hex-constant>
"""
subclass_names = ['Binary_Constant','Octal_Constant','Hex_Constant']
class Binary_Constant(STRINGBase): # R412
"""
<binary-constant> = B ' <digit> [ <digit> ]... '
| B \" <digit> [ <digit> ]... \"
"""
subclass_names = []
def match(string): return STRINGBase.match(pattern.abs_binary_constant, string)
match = staticmethod(match)
class Octal_Constant(STRINGBase): # R413
"""
<octal-constant> = O ' <digit> [ <digit> ]... '
| O \" <digit> [ <digit> ]... \"
"""
subclass_names = []
def match(string): return STRINGBase.match(pattern.abs_octal_constant, string)
match = staticmethod(match)
class Hex_Constant(STRINGBase): # R414
"""
<hex-constant> = Z ' <digit> [ <digit> ]... '
| Z \" <digit> [ <digit> ]... \"
"""
subclass_names = []
def match(string): return STRINGBase.match(pattern.abs_hex_constant, string)
match = staticmethod(match)
#R415: <hex-digit> = <digit> | A | B | C | D | E | F
class Signed_Real_Literal_Constant(NumberBase): # R416
"""
<signed-real-literal-constant> = [ <sign> ] <real-literal-constant>
"""
subclass_names = ['Real_Literal_Constant'] # never used
def match(string):
return NumberBase.match(pattern.abs_signed_real_literal_constant_named, string)
match = staticmethod(match)
class Real_Literal_Constant(NumberBase): # R417
"""
"""
subclass_names = []
def match(string):
return NumberBase.match(pattern.abs_real_literal_constant_named, string)
match = staticmethod(match)
#R418: <significand> = <digit-string> . [ <digit-string> ] | . <digit-string>
#R419: <exponent-letter> = E | D
#R420: <exponent> = <signed-digit-string>
class Complex_Literal_Constant(Base): # R421
"""
<complex-literal-constant> = ( <real-part>, <imag-part> )
"""
subclass_names = []
use_names = ['Real_Part','Imag_Part']
def match(string):
if not string or string[0]+string[-1]!='()': return
if not pattern.abs_complex_literal_constant.match(string):
return
r,i = string[1:-1].split(',')
return Real_Part(r.strip()), Imag_Part(i.strip())
match = staticmethod(match)
def tostr(self): return '(%s, %s)' % tuple(self.items)
class Real_Part(Base): # R422
"""
<real-part> = <signed-int-literal-constant>
| <signed-real-literal-constant>
| <named-constant>
"""
subclass_names = ['Signed_Int_Literal_Constant','Signed_Real_Literal_Constant','Named_Constant']
class Imag_Part(Base): # R423
"""
<imag-part> = <real-part>
"""
subclass_names = ['Signed_Int_Literal_Constant','Signed_Real_Literal_Constant','Named_Constant']
class Char_Selector(Base): # R424
"""
<char-selector> = <length-selector>
| ( LEN = <type-param-value> , KIND = <scalar-int-initialization-expr> )
| ( <type-param-value> , [ KIND = ] <scalar-int-initialization-expr> )
| ( KIND = <scalar-int-initialization-expr> [ , LEN = <type-param-value> ] )
"""
subclass_names = ['Length_Selector']
use_names = ['Type_Param_Value','Scalar_Int_Initialization_Expr']
def match(string):
if string[0]+string[-1] != '()': return
line, repmap = string_replace_map(string[1:-1].strip())
if line[:3].upper()=='LEN':
line = line[3:].lstrip()
if not line.startswith('='): return
line = line[1:].lstrip()
i = line.find(',')
if i==-1: return
v = line[:i].rstrip()
line = line[i+1:].lstrip()
if line[:4].upper()!='KIND': return
line = line[4:].lstrip()
if not line.startswith('='): return
line = line[1:].lstrip()
v = repmap(v)
line = repmap(line)
return Type_Param_Value(v), Scalar_Int_Initialization_Expr(line)
elif line[:4].upper()=='KIND':
line = line[4:].lstrip()
if not line.startswith('='): return
line = line[1:].lstrip()
i = line.find(',')
if i==-1: return None,Scalar_Int_Initialization_Expr(line)
v = line[i+1:].lstrip()
line = line[:i].rstrip()
if v[:3].upper()!='LEN': return
v = v[3:].lstrip()
if not v.startswith('='): return
v = v[1:].lstrip()
return Type_Param_Value(v), Scalar_Int_Initialization_Expr(line)
else:
i = line.find(',')
if i==-1: return
v = line[:i].rstrip()
line = line[i+1:].lstrip()
if line[:4].upper()=='KIND':
line = line[4:].lstrip()
if not line.startswith('='): return
line = line[1:].lstrip()
return Type_Param_Value(v), Scalar_Int_Initialization_Expr(line)
return
match = staticmethod(match)
def tostr(self):
if self.items[0] is None:
return '(KIND = %s)' % (self.items[1])
return '(LEN = %s, KIND = %s)' % (self.items[0],self.items[1])
class Length_Selector(Base): # R425
"""
<length -selector> = ( [ LEN = ] <type-param-value> )
| * <char-length> [ , ]
"""
subclass_names = []
use_names = ['Type_Param_Value','Char_Length']
def match(string):
if string[0]+string[-1] == '()':
line = string[1:-1].strip()
if line[:3].upper()=='LEN':
line = line[3:].lstrip()
if not line.startswith('='): return
line = line[1:].lstrip()
return '(',Type_Param_Value(line),')'
if not string.startswith('*'): return
line = string[1:].lstrip()
if string[-1]==',': line = line[:-1].rstrip()
return '*',Char_Length(line)
match = staticmethod(match)
def tostr(self):
if len(self.items)==2: return '%s%s' % tuple(self.items)
return '%sLEN = %s%s' % tuple(self.items)
class Char_Length(BracketBase): # R426
"""
<char-length> = ( <type-param-value> )
| <scalar-int-literal-constant>
"""
subclass_names = ['Scalar_Int_Literal_Constant']
use_names = ['Type_Param_Value']
def match(string): return BracketBase.match('()',Type_Param_Value, string)
match = staticmethod(match)
class Char_Literal_Constant(Base): # R427
"""
<char-literal-constant> = [ <kind-param> _ ] ' <rep-char> '
| [ <kind-param> _ ] \" <rep-char> \"
"""
subclass_names = []
rep = pattern.char_literal_constant
def match(string):
if string[-1] not in '"\'': return
if string[-1]=='"':
abs_a_n_char_literal_constant_named = pattern.abs_a_n_char_literal_constant_named2
else:
abs_a_n_char_literal_constant_named = pattern.abs_a_n_char_literal_constant_named1
line, repmap = string_replace_map(string)
m = abs_a_n_char_literal_constant_named.match(line)
if not m: return
kind_param = m.group('kind_param')
line = m.group('value')
line = repmap(line)
return line, kind_param
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return str(self.items[0])
return '%s_%s' % (self.items[1], self.items[0])
class Logical_Literal_Constant(NumberBase): # R428
"""
<logical-literal-constant> = .TRUE. [ _ <kind-param> ]
| .FALSE. [ _ <kind-param> ]
"""
subclass_names = []
def match(string):
return NumberBase.match(pattern.abs_logical_literal_constant_named, string)
match = staticmethod(match)
class Derived_Type_Def(Base): # R429
"""
<derived-type-def> = <derived-type-stmt>
[ <type-param-def-stmt> ]...
[ <private-or-sequence> ]...
[ <component-part> ]
[ <type-bound-procedure-part> ]
<end-type-stmt>
"""
subclass_names = []
use_names = ['Derived_Type_Stmt', 'Type_Param_Def_Stmt', 'Private_Or_Sequence',
'Component_Part', 'Type_Bound_Procedure_Part', 'End_Type_Stmt']
class Derived_Type_Stmt(StmtBase): # R430
"""
<derived-type-stmt> = TYPE [ [ , <type-attr-spec-list> ] :: ] <type-name> [ ( <type-param-name-list> ) ]
"""
subclass_names = []
use_names = ['Type_Attr_Spec_List', 'Type_Name', 'Type_Param_Name_List']
def match(string):
if string[:4].upper()!='TYPE': return
line = string[4:].lstrip()
i = line.find('::')
attr_specs = None
if i!=-1:
if line.startswith(','):
l = line[1:i].strip()
if not l: return
attr_specs = Type_Attr_Spec_List(l)
line = line[i+2:].lstrip()
m = pattern.name.match(line)
if m is None: return
name = Type_Name(m.group())
line = line[m.end():].lstrip()
if not line: return attr_specs, name, None
if line[0]+line[-1]!='()': return
return attr_specs, name, Type_Param_Name_List(line[1:-1].strip())
match = staticmethod(match)
def tostr(self):
s = 'TYPE'
if self.items[0] is not None:
s += ', %s :: %s' % (self.items[0], self.items[1])
else:
s += ' :: %s' % (self.items[1])
if self.items[2] is not None:
s += '(%s)' % (self.items[2])
return s
class Type_Name(Name): # C424
"""
<type-name> = <name>
<type-name> shall not be DOUBLEPRECISION or the name of intrinsic type
"""
subclass_names = []
use_names = []
def match(string):
if pattern.abs_intrinsic_type_name.match(string): return
return Name.match(string)
match = staticmethod(match)
class Type_EXTENDS_Parent_Type_Name(CALLBase):
"""
<..> = EXTENDS ( <parent-type-name> )
"""
subclass_names = []
use_names = ['Parent_Type_Name']
def match(string): return CALLBase.match('EXTENDS', Parent_Type_Name, string)
match = staticmethod(match)
class Type_Attr_Spec(STRINGBase): # R431
"""
<type-attr-spec> = <access-spec>
| EXTENDS ( <parent-type-name> )
| ABSTRACT
| BIND (C)
"""
subclass_names = ['Access_Spec', 'Type_EXTENDS_Parent_Type_Name', 'Language_Binding_Spec']
def match(string): return STRINGBase.match('ABSTRACT', string)
match = staticmethod(match)
class Private_Or_Sequence(Base): # R432
"""
<private-or-sequence> = <private-components-stmt>
| <sequence-stmt>
"""
subclass_names = ['Private_Components_Stmt', 'Sequence_Stmt']
class End_Type_Stmt(EndStmtBase): # R433
"""
<end-type-stmt> = END TYPE [ <type-name> ]
"""
subclass_names = []
use_names = ['Type_Name']
def match(string): return EndStmtBase.match('TYPE',Type_Name, string, require_stmt_type=True)
match = staticmethod(match)
class Sequence_Stmt(STRINGBase): # R434
"""
<sequence-stmt> = SEQUENCE
"""
subclass_names = []
def match(string): return STRINGBase.match('SEQUENCE', string)
match = staticmethod(match)
class Type_Param_Def_Stmt(StmtBase): # R435
"""
<type-param-def-stmt> = INTEGER [ <kind-selector> ] , <type-param-attr-spec> :: <type-param-decl-list>
"""
subclass_names = []
use_names = ['Kind_Selector', 'Type_Param_Attr_Spec', 'Type_Param_Decl_List']
def match(string):
if string[:7].upper()!='INTEGER': return
line, repmap = string_replace_map(string[7:].lstrip())
if not line: return
i = line.find(',')
if i==-1: return
kind_selector = repmap(line[:i].rstrip()) or None
line = repmap(line[i+1:].lstrip())
i = line.find('::')
if i==-1: return
l1 = line[:i].rstrip()
l2 = line[i+2:].lstrip()
if not l1 or not l2: return
if kind_selector: kind_selector = Kind_Selector(kind_selector)
return kind_selector, Type_Param_Attr_Spec(l1), Type_Param_Decl_List(l2)
match = staticmethod(match)
def tostr(self):
s = 'INTEGER'
if self.items[0] is not None:
s += '%s, %s :: %s' % tuple(self.items)
else:
s += ', %s :: %s' % tuple(self.items[1:])
return s
class Type_Param_Decl(BinaryOpBase): # R436
"""
<type-param-decl> = <type-param-name> [ = <scalar-int-initialization-expr> ]
"""
subclass_names = ['Type_Param_Name']
use_names = ['Scalar_Int_Initialization_Expr']
def match(string):
if '=' not in string: return
lhs,rhs = string.split('=',1)
lhs = lhs.rstrip()
rhs = rhs.lstrip()
if not lhs or not rhs: return
return Type_Param_Name(lhs),'=',Scalar_Int_Initialization_Expr(rhs)
match = staticmethod(match)
class Type_Param_Attr_Spec(STRINGBase): # R437
"""
<type-param-attr-spec> = KIND
| LEN
"""
subclass_names = []
def match(string): return STRINGBase.match(['KIND', 'LEN'], string)
match = staticmethod(match)
class Component_Part(BlockBase): # R438
"""
<component-part> = [ <component-def-stmt> ]...
"""
subclass_names = []
use_names = ['Component_Def_Stmt']
def match(reader):
content = []
while 1:
try:
obj = Component_Def_Stmt(reader)
except NoMatchError:
obj = None
if obj is None:
break
content.append(obj)
if content:
return content,
return
match = staticmethod(match)
def tofortran(self, tab='', isfix=None):
l = []
for item in self.content:
l.append(item.tofortran(tab=tab,isfix=isfix))
return '\n'.join(l)
class Component_Def_Stmt(Base): # R439
"""
<component-def-stmt> = <data-component-def-stmt>
| <proc-component-def-stmt>
"""
subclass_names = ['Data_Component_Def_Stmt', 'Proc_Component_Def_Stmt']
class Data_Component_Def_Stmt(StmtBase): # R440
"""
<data-component-def-stmt> = <declaration-type-spec> [ [ , <component-attr-spec-list> ] :: ] <component-decl-list>
"""
subclass_names = []
use_names = ['Declaration_Type_Spec', 'Component_Attr_Spec_List', 'Component_Decl_List']
class Dimension_Component_Attr_Spec(CALLBase):
"""
<dimension-component-attr-spec> = DIMENSION ( <component-array-spec> )
"""
subclass_names = []
use_names = ['Component_Array_Spec']
def match(string): return CALLBase.match('DIMENSION', Component_Array_Spec, string)
match = staticmethod(match)
class Component_Attr_Spec(STRINGBase): # R441
"""
<component-attr-spec> = POINTER
| DIMENSION ( <component-array-spec> )
| ALLOCATABLE
| <access-spec>
"""
subclass_names = ['Access_Spec', 'Dimension_Component_Attr_Spec']
use_names = []
def match(string): return STRINGBase.match(['POINTER', 'ALLOCATABLE'], string)
match = staticmethod(match)
class Component_Decl(Base): # R442
"""
<component-decl> = <component-name> [ ( <component-array-spec> ) ] [ * <char-length> ] [ <component-initialization> ]
"""
subclass_names = []
use_names = ['Component_Name', 'Component_Array_Spec', 'Char_Length', 'Component_Initialization']
def match(string):
m = pattern.name.match(string)
if m is None: return
name = Component_Name(m.group())
newline = string[m.end():].lstrip()
if not newline: return name, None, None, None
array_spec = None
char_length = None
init = None
if newline.startswith('('):
line, repmap = string_replace_map(newline)
i = line.find(')')
if i==-1: return
array_spec = Component_Array_Spec(repmap(line[1:i].strip()))
newline = repmap(line[i+1:].lstrip())
if newline.startswith('*'):
line, repmap = string_replace_map(newline)
i = line.find('=')
if i!=-1:
char_length = repmap(line[1:i].strip())
newline = repmap(newline[i:].lstrip())
else:
char_length = repmap(newline[1:].strip())
newline = ''
char_length = Char_Length(char_length)
if newline.startswith('='):
init = Component_Initialization(newline)
else:
assert newline=='',`newline`
return name, array_spec, char_length, init
match = staticmethod(match)
def tostr(self):
s = str(self.items[0])
if self.items[1] is not None:
s += '(' + str(self.items[1]) + ')'
if self.items[2] is not None:
s += '*' + str(self.items[2])
if self.items[3] is not None:
s += ' ' + str(self.items[3])
return s
class Component_Array_Spec(Base): # R443
"""
<component-array-spec> = <explicit-shape-spec-list>
| <deferred-shape-spec-list>
"""
subclass_names = ['Explicit_Shape_Spec_List', 'Deferred_Shape_Spec_List']
class Component_Initialization(Base): # R444
"""
<component-initialization> = = <initialization-expr>
| => <null-init>
"""
subclass_names = []
use_names = ['Initialization_Expr', 'Null_Init']
def match(string):
if string.startswith('=>'):
return '=>', Null_Init(string[2:].lstrip())
if string.startswith('='):
return '=', Initialization_Expr(string[2:].lstrip())
return
match = staticmethod(match)
def tostr(self): return '%s %s' % tuple(self.items)
class Proc_Component_Def_Stmt(StmtBase): # R445
"""
<proc-component-def-stmt> = PROCEDURE ( [ <proc-interface> ] ) , <proc-component-attr-spec-list> :: <proc-decl-list>
"""
subclass_names = []
use_names = ['Proc_Interface', 'Proc_Component_Attr_Spec_List', 'Proc_Decl_List']
class Proc_Component_PASS_Arg_Name(CALLBase):
"""
<proc-component-PASS-arg-name> = PASS ( <arg-name> )
"""
subclass_names = []
use_names = ['Arg_Name']
def match(string): return CALLBase.match('PASS', Arg_Name, string)
match = staticmethod(match)
class Proc_Component_Attr_Spec(STRINGBase): # R446
"""
<proc-component-attr-spec> = POINTER
| PASS [ ( <arg-name> ) ]
| NOPASS
| <access-spec>
"""
subclass_names = ['Access_Spec', 'Proc_Component_PASS_Arg_Name']
def match(string): return STRINGBase.match(['POINTER','PASS','NOPASS'], string)
match = staticmethod(match)
class Private_Components_Stmt(StmtBase): # R447
"""
<private-components-stmt> = PRIVATE
"""
subclass_names = []
def match(string): return StringBase.match('PRIVATE', string)
match = staticmethod(match)
class Type_Bound_Procedure_Part(Base): # R448
"""
<type-bound-procedure-part> = <contains-stmt>
[ <binding-private-stmt> ]
<proc-binding-stmt>
[ <proc-binding-stmt> ]...
"""
subclass_names = []
use_names = ['Contains_Stmt', 'Binding_Private_Stmt', 'Proc_Binding_Stmt']
class Binding_Private_Stmt(StmtBase, STRINGBase): # R449
"""
<binding-private-stmt> = PRIVATE
"""
subclass_names = []
def match(string): return StringBase.match('PRIVATE', string)
match = staticmethod(match)
class Proc_Binding_Stmt(Base): # R450
"""
<proc-binding-stmt> = <specific-binding>
| <generic-binding>
| <final-binding>
"""
subclass_names = ['Specific_Binding', 'Generic_Binding', 'Final_Binding']
class Specific_Binding(StmtBase): # R451
"""
<specific-binding> = PROCEDURE [ ( <interface-name> ) ] [ [ , <binding-attr-list> ] :: ] <binding-name> [ => <procedure-name> ]
"""
subclass_names = []
use_names = ['Interface_Name', 'Binding_Attr_List', 'Binding_Name', 'Procedure_Name']
class Generic_Binding(StmtBase): # R452
"""
<generic-binding> = GENERIC [ , <access-spec> ] :: <generic-spec> => <binding-name-list>
"""
subclass_names = []
use_names = ['Access_Spec', 'Generic_Spec', 'Binding_Name_List']
class Binding_PASS_Arg_Name(CALLBase):
"""
<binding-PASS-arg-name> = PASS ( <arg-name> )
"""
subclass_names = []
use_names = ['Arg_Name']
def match(string): return CALLBase.match('PASS', Arg_Name, string)
match = staticmethod(match)
class Binding_Attr(STRINGBase): # R453
"""
<binding-attr> = PASS [ ( <arg-name> ) ]
| NOPASS
| NON_OVERRIDABLE
| <access-spec>
"""
subclass_names = ['Access_Spec', 'Binding_PASS_Arg_Name']
def match(string): return STRINGBase.match(['PASS', 'NOPASS', 'NON_OVERRIDABLE'], string)
match = staticmethod(match)
class Final_Binding(StmtBase, WORDClsBase): # R454
"""
<final-binding> = FINAL [ :: ] <final-subroutine-name-list>
"""
subclass_names = []
use_names = ['Final_Subroutine_Name_List']
def match(string): return WORDClsBase.match('FINAL',Final_Subroutine_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Derived_Type_Spec(CallBase): # R455
"""
<derived-type-spec> = <type-name> [ ( <type-param-spec-list> ) ]
"""
subclass_names = ['Type_Name']
use_names = ['Type_Param_Spec_List']
def match(string): return CallBase.match(Type_Name, Type_Param_Spec_List, string)
match = staticmethod(match)
class Type_Param_Spec(KeywordValueBase): # R456
"""
<type-param-spec> = [ <keyword> = ] <type-param-value>
"""
subclass_names = ['Type_Param_Value']
use_names = ['Keyword']
def match(string): return KeywordValueBase.match(Keyword, Type_Param_Value, string)
match = staticmethod(match)
class Structure_Constructor_2(KeywordValueBase): # R457.b
"""
<structure-constructor-2> = [ <keyword> = ] <component-data-source>
"""
subclass_names = ['Component_Data_Source']
use_names = ['Keyword']
def match(string): return KeywordValueBase.match(Keyword, Component_Data_Source, string)
match = staticmethod(match)
class Structure_Constructor(CallBase): # R457
"""
<structure-constructor> = <derived-type-spec> ( [ <component-spec-list> ] )
| <structure-constructor-2>
"""
subclass_names = ['Structure_Constructor_2']
use_names = ['Derived_Type_Spec', 'Component_Spec_List']
def match(string): return CallBase.match(Derived_Type_Spec, Component_Spec_List, string)
match = staticmethod(match)
class Component_Spec(KeywordValueBase): # R458
"""
<component-spec> = [ <keyword> = ] <component-data-source>
"""
subclass_names = ['Component_Data_Source']
use_names = ['Keyword']
def match(string): return KeywordValueBase.match(Keyword, Component_Data_Source, string)
match = staticmethod(match)
class Component_Data_Source(Base): # R459
"""
<component-data-source> = <expr>
| <data-target>
| <proc-target>
"""
subclass_names = ['Proc_Target', 'Data_Target', 'Expr']
class Enum_Def(Base): # R460
"""
<enum-def> = <enum-def-stmt>
<enumerator-def-stmt>
[ <enumerator-def-stmt> ]...
<end-enum-stmt>
"""
subclass_names = []
use_names = ['Enum_Def_Stmt', 'Enumerator_Def_Stmt', 'End_Enum_Stmt']
class Enum_Def_Stmt(STRINGBase): # R461
"""
<enum-def-stmt> = ENUM, BIND(C)
"""
subclass_names = []
def match(string):
if string[:4].upper()!='ENUM': return
line = string[4:].lstrip()
if not line.startswith(','): return
line = line[1:].lstrip()
if line[:4].upper()!='BIND': return
line = line[4:].lstrip()
if not line or line[0]+line[-1]!='()': return
line = line[1:-1].strip()
if line!='C' or line!='c': return
return 'ENUM, BIND(C)',
match = staticmethod(match)
class Enumerator_Def_Stmt(StmtBase, WORDClsBase): # R462
"""
<enumerator-def-stmt> = ENUMERATOR [ :: ] <enumerator-list>
"""
subclass_names = []
use_names = ['Enumerator_List']
def match(string): return WORDClsBase.match('ENUMERATOR',Enumerator_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Enumerator(BinaryOpBase): # R463
"""
<enumerator> = <named-constant> [ = <scalar-int-initialization-expr> ]
"""
subclass_names = ['Named_Constant']
use_names = ['Scalar_Int_Initialization_Expr']
def match(string):
if '=' not in string: return
lhs,rhs = string.split('=',1)
return Named_Constant(lhs.rstrip()),'=',Scalar_Int_Initialization_Expr(rhs.lstrip())
match = staticmethod(match)
class End_Enum_Stmt(EndStmtBase): # R464
"""
<end-enum-stmt> = END ENUM
"""
subclass_names = []
def match(string): return EndStmtBase.match('ENUM',None, string, requite_stmt_type=True)
match = staticmethod(match)
class Array_Constructor(BracketBase): # R465
"""
<array-constructor> = (/ <ac-spec> /)
| <left-square-bracket> <ac-spec> <right-square-bracket>
"""
subclass_names = []
use_names = ['Ac_Spec']
def match(string):
try:
obj = BracketBase.match('(//)', Ac_Spec, string)
except NoMatchError:
obj = None
if obj is None:
obj = BracketBase.match('[]', Ac_Spec, string)
return obj
match = staticmethod(match)
class Ac_Spec(Base): # R466
"""
<ac-spec> = <type-spec> ::
| [ <type-spec> :: ] <ac-value-list>
"""
subclass_names = ['Ac_Value_List']
use_names = ['Type_Spec']
def match(string):
if string.endswith('::'):
return Type_Spec(string[:-2].rstrip()),None
line, repmap = string_replace_map(string)
i = line.find('::')
if i==-1: return
ts = line[:i].rstrip()
line = line[i+2:].lstrip()
ts = repmap(ts)
line = repmap(line)
return Type_Spec(ts),Ac_Value_List(line)
match = staticmethod(match)
def tostr(self):
if self.items[0] is None:
return str(self.items[1])
if self.items[1] is None:
return str(self.items[0]) + ' ::'
return '%s :: %s' % self.items
# R467: <left-square-bracket> = [
# R468: <right-square-bracket> = ]
class Ac_Value(Base): # R469
"""
<ac-value> = <expr>
| <ac-implied-do>
"""
subclass_names = ['Ac_Implied_Do','Expr']
class Ac_Implied_Do(Base): # R470
"""
<ac-implied-do> = ( <ac-value-list> , <ac-implied-do-control> )
"""
subclass_names = []
use_names = ['Ac_Value_List','Ac_Implied_Do_Control']
def match(string):
if string[0]+string[-1] != '()': return
line, repmap = string_replace_map(string[1:-1].strip())
i = line.rfind('=')
if i==-1: return
j = line[:i].rfind(',')
assert j!=-1
s1 = repmap(line[:j].rstrip())
s2 = repmap(line[j+1:].lstrip())
return Ac_Value_List(s1),Ac_Implied_Do_Control(s2)
match = staticmethod(match)
def tostr(self): return '(%s, %s)' % tuple(self.items)
class Ac_Implied_Do_Control(Base): # R471
"""
<ac-implied-do-control> = <ac-do-variable> = <scalar-int-expr> , <scalar-int-expr> [ , <scalar-int-expr> ]
"""
subclass_names = []
use_names = ['Ac_Do_Variable','Scalar_Int_Expr']
def match(string):
i = string.find('=')
if i==-1: return
s1 = string[:i].rstrip()
line, repmap = string_replace_map(string[i+1:].lstrip())
t = line.split(',')
if not (2<=len(t)<=3): return
t = [Scalar_Int_Expr(s.strip()) for s in t]
return Ac_Do_Variable(s1), t
match = staticmethod(match)
def tostr(self): return '%s = %s' % (self.items[0], ', '.join(map(str,self.items[1])))
class Ac_Do_Variable(Base): # R472
"""
<ac-do-variable> = <scalar-int-variable>
<ac-do-variable> shall be a named variable
"""
subclass_names = ['Scalar_Int_Variable']
###############################################################################
############################### SECTION 5 ####################################
###############################################################################
class Type_Declaration_Stmt(Base): # R501
"""
<type-declaration-stmt> = <declaration-type-spec> [ [ , <attr-spec> ]... :: ] <entity-decl-list>
"""
subclass_names = []
use_names = ['Declaration_Type_Spec', 'Attr_Spec_List', 'Entity_Decl_List']
def match(string):
line, repmap = string_replace_map(string)
i = line.find('::')
if i!=-1:
j = line[:i].find(',')
if j!=-1:
i = j
else:
if line[:6].upper()=='DOUBLE':
m = re.search(r'\s[a-z_]',line[6:].lstrip(),re.I)
if m is None: return
i = m.start() + len(line)-len(line[6:].lstrip())
else:
m = re.search(r'\s[a-z_]',line,re.I)
if m is None: return
i = m.start()
type_spec = Declaration_Type_Spec(repmap(line[:i].rstrip()))
if type_spec is None: return
line = line[i:].lstrip()
if line.startswith(','):
i = line.find('::')
if i==-1: return
attr_specs = Attr_Spec_List(repmap(line[1:i].strip()))
if attr_specs is None: return
line = line[i:]
else:
attr_specs = None
if line.startswith('::'):
line = line[2:].lstrip()
entity_decls = Entity_Decl_List(repmap(line))
if entity_decls is None: return
return type_spec, attr_specs, entity_decls
match = staticmethod(match)
def tostr(self):
if self.items[1] is None:
return '%s :: %s' % (self.items[0], self.items[2])
else:
return '%s, %s :: %s' % self.items
class Declaration_Type_Spec(Base): # R502
"""
<declaration-type-spec> = <intrinsic-type-spec>
| TYPE ( <derived-type-spec> )
| CLASS ( <derived-type-spec> )
| CLASS ( * )
"""
subclass_names = ['Intrinsic_Type_Spec']
use_names = ['Derived_Type_Spec']
def match(string):
if string[-1] != ')': return
start = string[:4].upper()
if start == 'TYPE':
line = string[4:].lstrip()
if not line.startswith('('): return
return 'TYPE',Derived_Type_Spec(line[1:-1].strip())
start = string[:5].upper()
if start == 'CLASS':
line = string[5:].lstrip()
if not line.startswith('('): return
line = line[1:-1].strip()
if line=='*': return 'CLASS','*'
return 'CLASS', Derived_Type_Spec(line)
return
match = staticmethod(match)
def tostr(self): return '%s(%s)' % self.items
class Dimension_Attr_Spec(CALLBase): # R503.d
"""
<dimension-attr-spec> = DIMENSION ( <array-spec> )
"""
subclass_names = []
use_names = ['Array_Spec']
def match(string): return CALLBase.match('DIMENSION', Array_Spec, string)
match = staticmethod(match)
class Intent_Attr_Spec(CALLBase): # R503.f
"""
<intent-attr-spec> = INTENT ( <intent-spec> )
"""
subclass_names = []
use_names = ['Intent_Spec']
def match(string): return CALLBase.match('INTENT', Intent_Spec, string)
match = staticmethod(match)
class Attr_Spec(STRINGBase): # R503
"""
<attr-spec> = <access-spec>
| ALLOCATABLE
| ASYNCHRONOUS
| DIMENSION ( <array-spec> )
| EXTERNAL
| INTENT ( <intent-spec> )
| INTRINSIC
| <language-binding-spec>
| OPTIONAL
| PARAMETER
| POINTER
| PROTECTED
| SAVE
| TARGET
| VALUE
| VOLATILE
"""
subclass_names = ['Access_Spec', 'Language_Binding_Spec',
'Dimension_Attr_Spec', 'Intent_Attr_Spec']
use_names = []
def match(string): return STRINGBase.match(pattern.abs_attr_spec, string)
match = staticmethod(match)
class Entity_Decl(Base): # R504
"""
<entity-decl> = <object-name> [ ( <array-spec> ) ] [ * <char-length> ] [ <initialization> ]
| <function-name> [ * <char-length> ]
"""
subclass_names = []
use_names = ['Object_Name', 'Array_Spec', 'Char_Length', 'Initialization', 'Function_Name']
def match(string):
m = pattern.name.match(string)
if m is None: return
name = Name(m.group())
newline = string[m.end():].lstrip()
if not newline: return name, None, None, None
array_spec = None
char_length = None
init = None
if newline.startswith('('):
line, repmap = string_replace_map(newline)
i = line.find(')')
if i==-1: return
array_spec = Array_Spec(repmap(line[1:i].strip()))
newline = repmap(line[i+1:].lstrip())
if newline.startswith('*'):
line, repmap = string_replace_map(newline)
i = line.find('=')
if i!=-1:
char_length = repmap(line[1:i].strip())
newline = repmap(newline[i:].lstrip())
else:
char_length = repmap(newline[1:].strip())
newline = ''
char_length = Char_Length(char_length)
if newline.startswith('='):
init = Initialization(newline)
else:
assert newline=='',`newline`
return name, array_spec, char_length, init
match = staticmethod(match)
def tostr(self):
s = str(self.items[0])
if self.items[1] is not None:
s += '(' + str(self.items[1]) + ')'
if self.items[2] is not None:
s += '*' + str(self.items[2])
if self.items[3] is not None:
s += ' ' + str(self.items[3])
return s
class Object_Name(Base): # R505
"""
<object-name> = <name>
"""
subclass_names = ['Name']
class Initialization(Base): # R506
"""
<initialization> = = <initialization-expr>
| => <null-init>
"""
subclass_names = []
use_names = ['Initialization_Expr', 'Null_Init']
def match(string):
if string.startswith('=>'):
return '=>', Null_Init(string[2:].lstrip())
if string.startswith('='):
return '=', Initialization_Expr(string[2:].lstrip())
return
match = staticmethod(match)
def tostr(self): return '%s %s' % self.items
class Null_Init(STRINGBase): # R507
"""
<null-init> = <function-reference>
<function-reference> shall be a reference to the NULL intrinsic function with no arguments.
"""
subclass_names = ['Function_Reference']
def match(string): return STRINGBase.match('NULL', string)
match = staticmethod(match)
class Access_Spec(STRINGBase): # R508
"""
<access-spec> = PUBLIC
| PRIVATE
"""
subclass_names = []
def match(string): return STRINGBase.match(['PUBLIC','PRIVATE'], string)
match = staticmethod(match)
class Language_Binding_Spec(Base): # R509
"""
<language-binding-spec> = BIND ( C [ , NAME = <scalar-char-initialization-expr> ] )
"""
subclass_names = []
use_names = ['Scalar_Char_Initialization_Expr']
def match(string):
start = string[:4].upper()
if start != 'BIND': return
line = string[4:].lstrip()
if not line or line[0]+line[-1]!='()': return
line = line[1:-1].strip()
if not line: return
start = line[0].upper()
if start!='C': return
line = line[1:].lstrip()
if not line: return None,
if not line.startswith(','): return
line = line[1:].lstrip()
start = line[:4].upper()
if start!='NAME': return
line=line[4:].lstrip()
if not line.startswith('='): return
return Scalar_Char_Initialization_Expr(line[1:].lstrip()),
match = staticmethod(match)
def tostr(self):
if self.items[0] is None: return 'BIND(C)'
return 'BIND(C, NAME = %s)' % (self.items[0])
class Array_Spec(Base): # R510
"""
<array-spec> = <explicit-shape-spec-list>
| <assumed-shape-spec-list>
| <deferred-shape-spec-list>
| <assumed-size-spec>
"""
subclass_names = ['Assumed_Size_Spec', 'Explicit_Shape_Spec_List', 'Assumed_Shape_Spec_List',
'Deferred_Shape_Spec_List']
class Explicit_Shape_Spec(SeparatorBase): # R511
"""
<explicit-shape-spec> = [ <lower-bound> : ] <upper-bound>
"""
subclass_names = []
use_names = ['Lower_Bound', 'Upper_Bound']
def match(string):
line, repmap = string_replace_map(string)
if ':' not in line:
return None, Upper_Bound(string)
lower,upper = line.split(':',1)
lower = lower.rstrip()
upper = upper.lstrip()
if not upper: return
if not lower: return
return Lower_Bound(repmap(lower)), Upper_Bound(repmap(upper))
match = staticmethod(match)
def tostr(self):
if self.items[0] is None: return str(self.items[1])
return SeparatorBase.tostr(self)
class Lower_Bound(Base): # R512
"""
<lower-bound> = <specification-expr>
"""
subclass_names = ['Specification_Expr']
class Upper_Bound(Base): # R513
"""
<upper-bound> = <specification-expr>
"""
subclass_names = ['Specification_Expr']
class Assumed_Shape_Spec(SeparatorBase): # R514
"""
<assumed-shape-spec> = [ <lower-bound> ] :
"""
subclass_names = []
use_names = ['Lower_Bound']
def match(string): return SeparatorBase.match(Lower_Bound, None, string)
match = staticmethod(match)
class Deferred_Shape_Spec(SeparatorBase): # R515
"""
<deferred_shape_spec> = :
"""
subclass_names = []
def match(string):
if string==':': return None,None
return
match = staticmethod(match)
class Assumed_Size_Spec(Base): # R516
"""
<assumed-size-spec> = [ <explicit-shape-spec-list> , ] [ <lower-bound> : ] *
"""
subclass_names = []
use_names = ['Explicit_Shape_Spec_List', 'Lower_Bound']
def match(string):
if not string.endswith('*'): return
line = string[:-1].rstrip()
if not line: return None,None
if line.endswith(':'):
line, repmap = string_replace_map(line[:-1].rstrip())
i = line.rfind(',')
if i==-1:
return None, Lower_Bound(repmap(line))
return Explicit_Shape_Spec_List(repmap(line[:i].rstrip())), Lower_Bound(repmap(line[i+1:].lstrip()))
if not line.endswith(','): return
line = line[:-1].rstrip()
return Explicit_Shape_Spec_List(line), None
match = staticmethod(match)
def tostr(self):
s = ''
if self.items[0] is not None:
s += str(self.items[0]) + ', '
if self.items[1] is not None:
s += str(self.items[1]) + ' : '
s += '*'
return s
class Intent_Spec(STRINGBase): # R517
"""
<intent-spec> = IN
| OUT
| INOUT
"""
subclass_names = []
def match(string): return STRINGBase.match(pattern.abs_intent_spec, string)
match = staticmethod(match)
class Access_Stmt(StmtBase, WORDClsBase): # R518
"""
<access-stmt> = <access-spec> [ [ :: ] <access-id-list> ]
"""
subclass_names = []
use_names = ['Access_Spec', 'Access_Id_List']
def match(string): return WORDClsBase.match(['PUBLIC', 'PRIVATE'],Access_Id_List,string,check_colons=True, require_cls=False)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Access_Id(Base): # R519
"""
<access-id> = <use-name>
| <generic-spec>
"""
subclass_names = ['Use_Name', 'Generic_Spec']
class Object_Name_Deferred_Shape_Spec_List_Item(CallBase):
"""
<..> = <object-name> [ ( <deferred-shape-spec-list> ) ]
"""
subclass_names = ['Object_Name']
use_names = ['Deferred_Shape_Spec_List']
def match(string): return CallBase.match(Object_Name, Deferred_Shape_Spec_List, string, require_rhs=True)
match = staticmethod(match)
class Allocatable_Stmt(StmtBase, WORDClsBase): # R520
"""
<allocateble-stmt> = ALLOCATABLE [ :: ] <object-name> [ ( <deferred-shape-spec-list> ) ] [ , <object-name> [ ( <deferred-shape-spec-list> ) ] ]...
"""
subclass_names = []
use_names = ['Object_Name_Deferred_Shape_Spec_List_Item_List']
def match(string):
return WORDClsBase.match('ALLOCATABLE', Object_Name_Deferred_Shape_Spec_List_Item_List, string,
check_colons=True, require_cls=True)
match = staticmethod(match)
class Asynchronous_Stmt(StmtBase, WORDClsBase): # R521
"""
<asynchronous-stmt> = ASYNCHRONOUS [ :: ] <object-name-list>
"""
subclass_names = []
use_names = ['Object_Name_List']
def match(string): return WORDClsBase.match('ASYNCHRONOUS',Object_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
class Bind_Stmt(StmtBase): # R522
"""
<bind-stmt> = <language-binding-spec> [ :: ] <bind-entity-list>
"""
subclass_names = []
use_names = ['Language_Binding_Spec', 'Bind_Entity_List']
def match(string):
i = string.find('::')
if i==-1:
i = string.find(')')
if i==-1: return
lhs. rhs = string[:i], string[i+1:]
else:
lhs, rhs = string.split('::',1)
lhs = lhs.rstrip()
rhs = rhs.lstrip()
if not lhs or not rhs: return
return Language_Binding_Spec(lhs), Bind_Entity_List(rhs)
match = staticmethod(match)
def tostr(self):
return '%s :: %s' % self.items
class Bind_Entity(BracketBase): # R523
"""
<bind-entity> = <entity-name>
| / <common-block-name> /
"""
subclass_names = ['Entity_Name']
use_names = ['Common_Block_Name']
def match(string): return BracketBase.match('//',Common_Block_Name, string)
match = staticmethod(match)
class Data_Stmt(StmtBase): # R524
"""
<data-stmt> = DATA <data-stmt-set> [ [ , ] <data-stmt-set> ]...
"""
subclass_names = []
use_names = ['Data_Stmt_Set']
class Data_Stmt_Set(Base): # R525
"""
<data-stmt-set> = <data-stmt-object-list> / <data-stmt-value-list> /
"""
subclass_names = []
use_names = ['Data_Stmt_Object_List', 'Data_Stmt_Value_List']
class Data_Stmt_Object(Base): # R526
"""
<data-stmt-object> = <variable>
| <data-implied-do>
"""
subclass_names = ['Variable', 'Data_Implied_Do']
class Data_Implied_Do(Base): # R527
"""
<data-implied-do> = ( <data-i-do-object-list> , <data-i-do-variable> = <scalar-int-expr > , <scalar-int-expr> [ , <scalar-int-expr> ] )
"""
subclass_names = []
use_names = ['Data_I_Do_Object_List', 'Data_I_Do_Variable', 'Scalar_Int_Expr']
class Data_I_Do_Object(Base): # R528
"""
<data-i-do-object> = <array-element>
| <scalar-structure-component>
| <data-implied-do>
"""
subclass_names = ['Array_Element', 'Scalar_Structure_Component', 'Data_Implied_Do']
class Data_I_Do_Variable(Base): # R529
"""
<data-i-do-variable> = <scalar-int-variable>
"""
subclass_names = ['Scalar_Int_Variable']
class Data_Stmt_Value(Base): # R530
"""
<data-stmt-value> = [ <data-stmt-repeat> * ] <data-stmt-constant>
"""
subclass_names = ['Data_Stmt_Constant']
use_names = ['Data_Stmt_Repeat']
def match(string):
line, repmap = string_replace_map(string)
s = line.split('*')
if len(s)!=2: return
lhs = repmap(s[0].rstrip())
rhs = repmap(s[1].lstrip())
if not lhs or not rhs: return
return Data_Stmt_Repeat(lhs), Data_Stmt_Constant(rhs)
match = staticmethod(match)
def tostr(self):
return '%s * %s' % self.items
class Data_Stmt_Repeat(Base): # R531
"""
<data-stmt-repeat> = <scalar-int-constant>
| <scalar-int-constant-subobject>
"""
subclass_names = ['Scalar_Int_Constant', 'Scalar_Int_Constant_Subobject']
class Data_Stmt_Constant(Base): # R532
"""
<data-stmt-constant> = <scalar-constant>
| <scalar-constant-subobject>
| <signed-int-literal-constant>
| <signed-real-literal-constant>
| <null-init>
| <structure-constructor>
"""
subclass_names = ['Scalar_Constant', 'Scalar_Constant_Subobject',
'Signed_Int_Literal_Constant', 'Signed_Real_Literal_Constant',
'Null_Init', 'Structure_Constructor']
class Int_Constant_Subobject(Base): # R533
"""
<int-constant-subobject> = <constant-subobject>
"""
subclass_names = ['Constant_Subobject']
class Constant_Subobject(Base): # R534
"""
<constant-subobject> = <designator>
"""
subclass_names = ['Designator']
class Dimension_Stmt(StmtBase): # R535
"""
<dimension-stmt> = DIMENSION [ :: ] <array-name> ( <array-spec> ) [ , <array-name> ( <array-spec> ) ]...
"""
subclass_names = []
use_names = ['Array_Name', 'Array_Spec']
def match(string):
if string[:9].upper()!='DIMENSION': return
line, repmap = string_replace_map(string[9:].lstrip())
if line.startswith('::'): line = line[2:].lstrip()
decls = []
for s in line.split(','):
s = s.strip()
if not s.endswith(')'): return
i = s.find('(')
if i==-1: return
decls.append((Array_Name(repmap(s[:i].rstrip())), Array_Spec(repmap(s[i+1:-1].strip()))))
if not decls: return
return decls,
match = staticmethod(match)
def tostr(self):
return 'DIMENSION :: ' + ', '.join(['%s(%s)' % ns for ns in self.items[0]])
class Intent_Stmt(StmtBase): # R536
"""
<intent-stmt> = INTENT ( <intent-spec> ) [ :: ] <dummy-arg-name-list>
"""
subclass_names = []
use_names = ['Intent_Spec', 'Dummy_Arg_Name_List']
def match(string):
if string[:6].upper()!='INTENT': return
line = string[6:].lstrip()
if not line or not line.startswith('('): return
i = line.rfind(')')
if i==-1: return
spec = line[1:i].strip()
if not spec: return
line = line[i+1:].lstrip()
if line.startswith('::'):
line = line[2:].lstrip()
if not line: return
return Intent_Spec(spec), Dummy_Arg_Name_List(line)
match = staticmethod(match)
def tostr(self):
return 'INTENT(%s) :: %s' % self.items
class Optional_Stmt(StmtBase, WORDClsBase): # R537
"""
<optional-stmt> = OPTIONAL [ :: ] <dummy-arg-name-list>
"""
subclass_names = []
use_names = ['Dummy_Arg_Name_List']
def match(string): return WORDClsBase.match('OPTIONAL',Dummy_Arg_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Parameter_Stmt(StmtBase, CALLBase): # R538
"""
<parameter-stmt> = PARAMETER ( <named-constant-def-list> )
"""
subclass_names = []
use_names = ['Named_Constant_Def_List']
def match(string): return CALLBase.match('PARAMETER', Named_Constant_Def_List, string, require_rhs=True)
match = staticmethod(match)
class Named_Constant_Def(KeywordValueBase): # R539
"""
<named-constant-def> = <named-constant> = <initialization-expr>
"""
subclass_names = []
use_names = ['Named_Constant', 'Initialization_Expr']
def match(string): return KeywordValueBase.match(Named_Constant, Initialization_Expr, string)
match = staticmethod(match)
class Pointer_Stmt(StmtBase, WORDClsBase): # R540
"""
<pointer-stmt> = POINTER [ :: ] <pointer-decl-list>
"""
subclass_names = []
use_names = ['Pointer_Decl_List']
def match(string): return WORDClsBase.match('POINTER',Pointer_Decl_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Pointer_Decl(CallBase): # R541
"""
<pointer-decl> = <object-name> [ ( <deferred-shape-spec-list> ) ]
| <proc-entity-name>
"""
subclass_names = ['Proc_Entity_Name', 'Object_Name']
use_names = ['Deferred_Shape_Spec_List']
def match(string): return CallBase.match(Object_Name, Deferred_Shape_Spec_List, string, require_rhs=True)
match = staticmethod(match)
class Protected_Stmt(StmtBase, WORDClsBase): # R542
"""
<protected-stmt> = PROTECTED [ :: ] <entity-name-list>
"""
subclass_names = []
use_names = ['Entity_Name_List']
def match(string): return WORDClsBase.match('PROTECTED',Entity_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Save_Stmt(StmtBase, WORDClsBase): # R543
"""
<save-stmt> = SAVE [ [ :: ] <saved-entity-list> ]
"""
subclass_names = []
use_names = ['Saved_Entity_List']
def match(string): return WORDClsBase.match('SAVE',Saved_Entity_List,string,check_colons=True, require_cls=False)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Saved_Entity(BracketBase): # R544
"""
<saved-entity> = <object-name>
| <proc-pointer-name>
| / <common-block-name> /
"""
subclass_names = ['Object_Name', 'Proc_Pointer_Name']
use_names = ['Common_Block_Name']
def match(string): return BracketBase.match('//',CommonBlockName, string)
match = staticmethod(match)
class Proc_Pointer_Name(Base): # R545
"""
<proc-pointer-name> = <name>
"""
subclass_names = ['Name']
class Target_Stmt(StmtBase): # R546
"""
<target-stmt> = TARGET [ :: ] <object-name> [ ( <array-spec> ) ] [ , <object-name> [ ( <array-spec> ) ] ]...
"""
subclass_names = []
use_names = ['Object_Name', 'Array_Spec']
class Value_Stmt(StmtBase, WORDClsBase): # R547
"""
<value-stmt> = VALUE [ :: ] <dummy-arg-name-list>
"""
subclass_names = []
use_names = ['Dummy_Arg_Name_List']
def match(string): return WORDClsBase.match('VALUE',Dummy_Arg_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Volatile_Stmt(StmtBase, WORDClsBase): # R548
"""
<volatile-stmt> = VOLATILE [ :: ] <object-name-list>
"""
subclass_names = []
use_names = ['Object_Name_List']
def match(string): return WORDClsBase.match('VOLATILE',Object_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Implicit_Stmt(StmtBase, WORDClsBase): # R549
"""
<implicit-stmt> = IMPLICIT <implicit-spec-list>
| IMPLICIT NONE
"""
subclass_names = []
use_names = ['Implicit_Spec_List']
def match(string):
for w,cls in [(pattern.abs_implicit_none, None),
('IMPLICIT', Implicit_Spec_List)]:
try:
obj = WORDClsBase.match(w, cls, string)
except NoMatchError:
obj = None
if obj is not None: return obj
return
match = staticmethod(match)
class Implicit_Spec(CallBase): # R550
"""
<implicit-spec> = <declaration-type-spec> ( <letter-spec-list> )
"""
subclass_names = []
use_names = ['Declaration_Type_Spec', 'Letter_Spec_List']
def match(string):
if not string.endswith(')'): return
i = string.rfind('(')
if i==-1: return
s1 = string[:i].rstrip()
s2 = string[i+1:-1].strip()
if not s1 or not s2: return
return Declaration_Type_Spec(s1), Letter_Spec_List(s2)
match = staticmethod(match)
class Letter_Spec(Base): # R551
"""
<letter-spec> = <letter> [ - <letter> ]
"""
subclass_names = []
def match(string):
if len(string)==1:
lhs = string.upper()
if 'A'<=lhs<='Z': return lhs, None
return
if '-' not in string: return
lhs,rhs = string.split('-',1)
lhs = lhs.strip().upper()
rhs = rhs.strip().upper()
if not len(lhs)==len(rhs)==1: return
if not ('A'<=lhs<=rhs<='Z'): return
return lhs,rhs
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return str(self.items[0])
return '%s - %s' % tuple(self.items)
class Namelist_Stmt(StmtBase): # R552
"""
<namelist-stmt> = NAMELIST / <namelist-group-name> / <namelist-group-object-list> [ [ , ] / <namelist-group-name> / <namelist-group-object-list> ]
"""
subclass_names = []
use_names = ['Namelist_Group_Name', 'Namelist_Group_Object_List']
class Namelist_Group_Object(Base): # R553
"""
<namelist-group-object> = <variable-name>
"""
subclass_names = ['Variable_Name']
class Equivalence_Stmt(StmtBase, WORDClsBase): # R554
"""
<equivalence-stmt> = EQUIVALENCE <equivalence-set-list>
"""
subclass_names = []
use_names = ['Equivalence_Set_List']
def match(string): return WORDClsBase.match('EQUIVALENCE', Equivalence_Set_List, string)
match = staticmethod(match)
class Equivalence_Set(Base): # R555
"""
<equivalence-set> = ( <equivalence-object> , <equivalence-object-list> )
"""
subclass_names = []
use_names = ['Equivalence_Object', 'Equivalence_Object_List']
def match(string):
if not string or string[0]+string[-1]!='()': return
line = string[1:-1].strip()
if not line: return
l = Equivalence_Object_List(line)
obj = l.items[0]
l.items = l.items[1:]
if not l.items: return
return obj, l
match = staticmethod(match)
def tostr(self): return '(%s, %s)' % tuple(self.items)
class Equivalence_Object(Base): # R556
"""
<equivalence-object> = <variable-name>
| <array-element>
| <substring>
"""
subclass_names = ['Variable_Name', 'Array_Element', 'Substring']
class Common_Stmt(StmtBase): # R557
"""
<common-stmt> = COMMON [ / [ <common-block-name> ] / ] <common-block-object-list> [ [ , ] / [ <common-block-name> ] / <common-block-object-list> ]...
"""
subclass_names = []
use_names = ['Common_Block_Name', 'Common_Block_Object_List']
def match(string):
if string[:6].upper()!='COMMON': return
line = string[6:]
if not line or 'A'<=line[0].upper()<='Z' or line[0]=='_': return
line, repmap = string_replace_map(line.lstrip())
items = []
if line.startswith('/'):
i = line.find('/',1)
if i==-1: return
name = line[1:i].strip() or None
if name is not None: name = Common_Block_Name(name)
line = line[i+1:].lstrip()
i = line.find('/')
if i==-1:
lst = Common_Block_Object_List(repmap(line))
line = ''
else:
l = line[:i].rstrip()
if l.endswith(','): l = l[:-1].rstrip()
if not l: return
lst = Common_Block_Object_List(repmap(l))
line = line[i:].lstrip()
else:
name = None
i = line.find('/')
if i==-1:
lst = Common_Block_Object_List(repmap(line))
line = ''
else:
l = line[:i].rstrip()
if l.endswith(','): l = l[:-1].rstrip()
if not l: return
lst = Common_Block_Object_List(repmap(l))
line = line[i:].lstrip()
items.append((name, lst))
while line:
if line.startswith(','): line = line[1:].lstrip()
if not line.startswith('/'): return
i = line.find('/',1)
name = line[1:i].strip() or None
if name is not None: name = Common_Block_Name(name)
line = line[i+1:].lstrip()
i = line.find('/')
if i==-1:
lst = Common_Block_Object_List(repmap(line))
line = ''
else:
l = line[:i].rstrip()
if l.endswith(','): l = l[:-1].rstrip()
if not l: return
lst = Common_Block_Object_List(repmap(l))
line = line[i:].lstrip()
items.append((name, lst))
return items,
match = staticmethod(match)
def tostr(self):
s = 'COMMON'
for (name, lst) in self.items[0]:
if name is not None:
s += ' /%s/ %s' % (name, lst)
else:
s += ' // %s' % (lst)
return s
class Common_Block_Object(CallBase): # R558
"""
<common-block-object> = <variable-name> [ ( <explicit-shape-spec-list> ) ]
| <proc-pointer-name>
"""
subclass_names = ['Proc_Pointer_Name','Variable_Name']
use_names = ['Variable_Name', 'Explicit_Shape_Spec_List']
def match(string): return CallBase.match(Variable_Name, Explicit_Shape_Spec_List, string, require_rhs=True)
match = staticmethod(match)
###############################################################################
############################### SECTION 6 ####################################
###############################################################################
class Variable(Base): # R601
"""
<variable> = <designator>
"""
subclass_names = ['Designator']
class Variable_Name(Base): # R602
"""
<variable-name> = <name>
"""
subclass_names = ['Name']
class Designator(Base): # R603
"""
<designator> = <object-name>
| <array-element>
| <array-section>
| <structure-component>
| <substring>
<substring-range> = [ <scalar-int-expr> ] : [ <scalar-int-expr> ]
<structure-component> = <data-ref>
"""
subclass_names = ['Object_Name','Array_Section','Array_Element','Structure_Component',
'Substring'
]
class Logical_Variable(Base): # R604
"""
<logical-variable> = <variable>
"""
subclass_names = ['Variable']
class Default_Logical_Variable(Base): # R605
"""
<default-logical-variable> = <variable>
"""
subclass_names = ['Variable']
class Char_Variable(Base): # R606
"""
<char-variable> = <variable>
"""
subclass_names = ['Variable']
class Default_Char_Variable(Base): # R607
"""
<default-char-variable> = <variable>
"""
subclass_names = ['Variable']
class Int_Variable(Base): # R608
"""
<int-variable> = <variable>
"""
subclass_names = ['Variable']
class Substring(CallBase): # R609
"""
<substring> = <parent-string> ( <substring-range> )
"""
subclass_names = []
use_names = ['Parent_String','Substring_Range']
def match(string): return CallBase.match(Parent_String, Substring_Range, string, require_rhs=True)
match = staticmethod(match)
class Parent_String(Base): # R610
"""
<parent-string> = <scalar-variable-name>
| <array-element>
| <scalar-structure-component>
| <scalar-constant>
"""
subclass_names = ['Scalar_Variable_Name', 'Array_Element', 'Scalar_Structure_Component', 'Scalar_Constant']
class Substring_Range(SeparatorBase): # R611
"""
<substring-range> = [ <scalar-int-expr> ] : [ <scalar-int-expr> ]
"""
subclass_names = []
use_names = ['Scalar_Int_Expr']
def match(string):
return SeparatorBase.match(Scalar_Int_Expr, Scalar_Int_Expr, string)
match = staticmethod(match)
class Data_Ref(SequenceBase): # R612
"""
<data-ref> = <part-ref> [ % <part-ref> ]...
"""
subclass_names = ['Part_Ref']
use_names = []
def match(string): return SequenceBase.match(r'%', Part_Ref, string)
match = staticmethod(match)
class Part_Ref(CallBase): # R613
"""
<part-ref> = <part-name> [ ( <section-subscript-list> ) ]
"""
subclass_names = ['Part_Name']
use_names = ['Section_Subscript_List']
def match(string):
return CallBase.match(Part_Name, Section_Subscript_List, string, require_rhs=True)
match = staticmethod(match)
class Structure_Component(Base): # R614
"""
<structure-component> = <data-ref>
"""
subclass_names = ['Data_Ref']
class Type_Param_Inquiry(BinaryOpBase): # R615
"""
<type-param-inquiry> = <designator> % <type-param-name>
"""
subclass_names = []
use_names = ['Designator','Type_Param_Name']
def match(string):
return BinaryOpBase.match(\
Designator, pattern.percent_op.named(), Type_Param_Name, string)
match = staticmethod(match)
class Array_Element(Base): # R616
"""
<array-element> = <data-ref>
"""
subclass_names = ['Data_Ref']
class Array_Section(CallBase): # R617
"""
<array-section> = <data-ref> [ ( <substring-range> ) ]
"""
subclass_names = ['Data_Ref']
use_names = ['Substring_Range']
def match(string): return CallBase.match(Data_Ref, Substring_Range, string, require_rhs=True)
match = staticmethod(match)
class Subscript(Base): # R618
"""
<subscript> = <scalar-int-expr>
"""
subclass_names = ['Scalar_Int_Expr']
class Section_Subscript(Base): # R619
"""
<section-subscript> = <subscript>
| <subscript-triplet>
| <vector-subscript>
"""
subclass_names = ['Subscript_Triplet', 'Vector_Subscript', 'Subscript']
class Subscript_Triplet(Base): # R620
"""
<subscript-triplet> = [ <subscript> ] : [ <subscript> ] [ : <stride> ]
"""
subclass_names = []
use_names = ['Subscript','Stride']
def match(string):
line, repmap = string_replace_map(string)
t = line.split(':')
if len(t)<=1 or len(t)>3: return
lhs_obj,rhs_obj, stride_obj = None, None, None
if len(t)==2:
lhs,rhs = t[0].rstrip(),t[1].lstrip()
else:
lhs,rhs,stride = t[0].rstrip(),t[1].strip(),t[2].lstrip()
if stride:
stride_obj = Stride(repmap(stride))
if lhs:
lhs_obj = Subscript(repmap(lhs))
if rhs:
rhs_obj = Subscript(repmap(rhs))
return lhs_obj, rhs_obj, stride_obj
match = staticmethod(match)
def tostr(self):
s = ''
if self.items[0] is not None:
s += str(self.items[0]) + ' :'
else:
s += ':'
if self.items[1] is not None:
s += ' ' + str(self.items[1])
if self.items[2] is not None:
s += ' : ' + str(self.items[2])
return s
class Stride(Base): # R621
"""
<stride> = <scalar-int-expr>
"""
subclass_names = ['Scalar_Int_Expr']
class Vector_Subscript(Base): # R622
"""
<vector-subscript> = <int-expr>
"""
subclass_names = ['Int_Expr']
class Allocate_Stmt(StmtBase): # R623
"""
<allocate-stmt> = ALLOCATE ( [ <type-spec> :: ] <allocation-list> [ , <alloc-opt-list> ] )
"""
subclass_names = []
use_names = ['Type_Spec', 'Allocation_List', 'Alloc_Opt_List']
class Alloc_Opt(KeywordValueBase):# R624
"""
<alloc-opt> = STAT = <stat-variable>
| ERRMSG = <errmsg-variable>
| SOURCE = <source-expr>
"""
subclass_names = []
use_names = ['Stat_Variable', 'Errmsg_Variable', 'Source_Expr']
def match(string):
for (k,v) in [('STAT', Stat_Variable),
('ERRMSG', Errmsg_Variable),
('SOURCE', Source_Expr)
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return
match = staticmethod(match)
class Stat_Variable(Base):# R625
"""
<stat-variable> = <scalar-int-variable>
"""
subclass_names = ['Scalar_Int_Variable']
class Errmsg_Variable(Base):# R626
"""
<errmsg-variable> = <scalar-default-char-variable>
"""
subclass_names = ['Scalar_Default_Char_Variable']
class Source_Expr(Base):# R627
"""
<source-expr> = <expr>
"""
subclass_names = ['Expr']
class Allocation(CallBase):# R628
"""
<allocation> = <allocate-object> [ ( <allocate-shape-spec-list> ) ]
| <variable-name>
"""
subclass_names = ['Variable_Name', 'Allocate_Object']
use_names = ['Allocate_Shape_Spec_List']
def match(string):
return CallBase.match(Allocate_Object, Allocate_Shape_Spec_List, string, require_rhs = True)
match = staticmethod(match)
class Allocate_Object(Base): # R629
"""
<allocate-object> = <variable-name>
| <structure-component>
"""
subclass_names = ['Variable_Name', 'Structure_Component']
class Allocate_Shape_Spec(SeparatorBase): # R630
"""
<allocate-shape-spec> = [ <lower-bound-expr> : ] <upper-bound-expr>
"""
subclass_names = []
use_names = ['Lower_Bound_Expr', 'Upper_Bound_Expr']
def match(string):
line, repmap = string_replace_map(string)
if ':' not in line: return None, Upper_Bound_Expr(string)
lower,upper = line.split(':',1)
lower = lower.rstrip()
upper = upper.lstrip()
if not upper: return
if not lower: return
return Lower_Bound_Expr(repmap(lower)), Upper_Bound_Expr(repmap(upper))
match = staticmethod(match)
def tostr(self):
if self.items[0] is None: return str(self.items[1])
return SeparatorBase.tostr(self)
class Lower_Bound_Expr(Base): # R631
"""
<lower-bound-expr> = <scalar-int-expr>
"""
subclass_names = ['Scalar_Int_Expr']
class Upper_Bound_Expr(Base): # R632
"""
<upper-bound-expr> = <scalar-int-expr>
"""
subclass_names = ['Scalar_Int_Expr']
class Nullify_Stmt(StmtBase, CALLBase): # R633
"""
<nullify-stmt> = NULLIFY ( <pointer-object-list> )
"""
subclass_names = []
use_names = ['Pointer_Object_List']
def match(string): return CALLBase.match('NULLIFY', Pointer_Object_List, string, require_rhs=True)
match = staticmethod(match)
class Pointer_Object(Base): # R634
"""
<pointer-object> = <variable-name>
| <structure-component>
| <proc-pointer-name>
"""
subclass_names = ['Variable_Name', 'Structure_Component', 'Proc_Pointer_Name']
class Deallocate_Stmt(StmtBase): # R635
"""
<deallocate-stmt> = DEALLOCATE ( <allocate-object-list> [ , <dealloc-opt-list> ] )
"""
subclass_names = []
use_names = ['Allocate_Object_List', 'Dealloc_Opt_List']
class Dealloc_Opt(KeywordValueBase): # R636
"""
<dealloc-opt> = STAT = <stat-variable>
| ERRMSG = <errmsg-variable>
"""
subclass_names = []
use_names = ['Stat_Variable', 'Errmsg_Variable']
def match(string):
for (k,v) in [('STAT', Stat_Variable),
('ERRMSG', Errmsg_Variable),
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return
match = staticmethod(match)
class Scalar_Char_Initialization_Expr(Base):
subclass_names = ['Char_Initialization_Expr']
###############################################################################
############################### SECTION 7 ####################################
###############################################################################
class Primary(Base): # R701
"""
<primary> = <constant>
| <designator>
| <array-constructor>
| <structure-constructor>
| <function-reference>
| <type-param-inquiry>
| <type-param-name>
| ( <expr> )
"""
subclass_names = ['Constant', 'Parenthesis', 'Designator','Array_Constructor',
'Structure_Constructor',
'Function_Reference', 'Type_Param_Inquiry', 'Type_Param_Name',
]
class Parenthesis(BracketBase): # R701.h
"""
<parenthesis> = ( <expr> )
"""
subclass_names = []
use_names = ['Expr']
def match(string): return BracketBase.match('()', Expr, string)
match = staticmethod(match)
class Level_1_Expr(UnaryOpBase): # R702
"""
<level-1-expr> = [ <defined-unary-op> ] <primary>
<defined-unary-op> = . <letter> [ <letter> ]... .
"""
subclass_names = ['Primary']
use_names = []
def match(string):
if pattern.non_defined_binary_op.match(string):
raise NoMatchError,'%s: %r' % (Level_1_Expr.__name__, string)
return UnaryOpBase.match(\
pattern.defined_unary_op.named(),Primary,string)
match = staticmethod(match)
class Defined_Unary_Op(STRINGBase): # R703
"""
<defined-unary-op> = . <letter> [ <letter> ]... .
"""
subclass_names = ['Defined_Op']
class Defined_Op(STRINGBase): # R703, 723
"""
<defined-op> = . <letter> [ <letter> ]... .
"""
subclass_names = []
def match(string):
if pattern.non_defined_binary_op.match(string):
raise NoMatchError,'%s: %r' % (Defined_Unary_Op.__name__, string)
return STRINGBase.match(pattern.abs_defined_op, string)
match = staticmethod(match)
class Mult_Operand(BinaryOpBase): # R704
"""
<mult-operand> = <level-1-expr> [ <power-op> <mult-operand> ]
<power-op> = **
"""
subclass_names = ['Level_1_Expr']
use_names = ['Mult_Operand']
def match(string):
return BinaryOpBase.match(\
Level_1_Expr,pattern.power_op.named(),Mult_Operand,string,right=False)
match = staticmethod(match)
class Add_Operand(BinaryOpBase): # R705
"""
<add-operand> = [ <add-operand> <mult-op> ] <mult-operand>
<mult-op> = *
| /
"""
subclass_names = ['Mult_Operand']
use_names = ['Add_Operand','Mult_Operand']
def match(string):
return BinaryOpBase.match(Add_Operand,pattern.mult_op.named(),Mult_Operand,string)
match = staticmethod(match)
class Level_2_Expr(BinaryOpBase): # R706
"""
<level-2-expr> = [ [ <level-2-expr> ] <add-op> ] <add-operand>
<level-2-expr> = [ <level-2-expr> <add-op> ] <add-operand>
| <level-2-unary-expr>
<add-op> = +
| -
"""
subclass_names = ['Level_2_Unary_Expr']
use_names = ['Level_2_Expr']
def match(string):
return BinaryOpBase.match(\
Level_2_Expr,pattern.add_op.named(),Add_Operand,string)
match = staticmethod(match)
class Level_2_Unary_Expr(UnaryOpBase): # R706.c
"""
<level-2-unary-expr> = [ <add-op> ] <add-operand>
"""
subclass_names = ['Add_Operand']
use_names = []
def match(string): return UnaryOpBase.match(pattern.add_op.named(),Add_Operand,string)
match = staticmethod(match)
#R707: <power-op> = **
#R708: <mult-op> = * | /
#R709: <add-op> = + | -
class Level_3_Expr(BinaryOpBase): # R710
"""
<level-3-expr> = [ <level-3-expr> <concat-op> ] <level-2-expr>
<concat-op> = //
"""
subclass_names = ['Level_2_Expr']
use_names =['Level_3_Expr']
def match(string):
return BinaryOpBase.match(\
Level_3_Expr,pattern.concat_op.named(),Level_2_Expr,string)
match = staticmethod(match)
#R711: <concat-op> = //
class Level_4_Expr(BinaryOpBase): # R712
"""
<level-4-expr> = [ <level-3-expr> <rel-op> ] <level-3-expr>
<rel-op> = .EQ. | .NE. | .LT. | .LE. | .GT. | .GE. | == | /= | < | <= | > | >=
"""
subclass_names = ['Level_3_Expr']
use_names = []
def match(string):
return BinaryOpBase.match(\
Level_3_Expr,pattern.rel_op.named(),Level_3_Expr,string)
match = staticmethod(match)
#R713: <rel-op> = .EQ. | .NE. | .LT. | .LE. | .GT. | .GE. | == | /= | < | <= | > | >=
class And_Operand(UnaryOpBase): # R714
"""
<and-operand> = [ <not-op> ] <level-4-expr>
<not-op> = .NOT.
"""
subclass_names = ['Level_4_Expr']
use_names = []
def match(string):
return UnaryOpBase.match(\
pattern.not_op.named(),Level_4_Expr,string)
match = staticmethod(match)
class Or_Operand(BinaryOpBase): # R715
"""
<or-operand> = [ <or-operand> <and-op> ] <and-operand>
<and-op> = .AND.
"""
subclass_names = ['And_Operand']
use_names = ['Or_Operand','And_Operand']
def match(string):
return BinaryOpBase.match(\
Or_Operand,pattern.and_op.named(),And_Operand,string)
match = staticmethod(match)
class Equiv_Operand(BinaryOpBase): # R716
"""
<equiv-operand> = [ <equiv-operand> <or-op> ] <or-operand>
<or-op> = .OR.
"""
subclass_names = ['Or_Operand']
use_names = ['Equiv_Operand']
def match(string):
return BinaryOpBase.match(\
Equiv_Operand,pattern.or_op.named(),Or_Operand,string)
match = staticmethod(match)
class Level_5_Expr(BinaryOpBase): # R717
"""
<level-5-expr> = [ <level-5-expr> <equiv-op> ] <equiv-operand>
<equiv-op> = .EQV.
| .NEQV.
"""
subclass_names = ['Equiv_Operand']
use_names = ['Level_5_Expr']
def match(string):
return BinaryOpBase.match(\
Level_5_Expr,pattern.equiv_op.named(),Equiv_Operand,string)
match = staticmethod(match)
#R718: <not-op> = .NOT.
#R719: <and-op> = .AND.
#R720: <or-op> = .OR.
#R721: <equiv-op> = .EQV. | .NEQV.
class Expr(BinaryOpBase): # R722
"""
<expr> = [ <expr> <defined-binary-op> ] <level-5-expr>
<defined-binary-op> = . <letter> [ <letter> ]... .
TODO: defined_binary_op must not be intrinsic_binary_op!!
"""
subclass_names = ['Level_5_Expr']
use_names = ['Expr']
def match(string):
return BinaryOpBase.match(Expr, pattern.defined_binary_op.named(), Level_5_Expr,
string)
match = staticmethod(match)
class Defined_Unary_Op(STRINGBase): # R723
"""
<defined-unary-op> = . <letter> [ <letter> ]... .
"""
subclass_names = ['Defined_Op']
class Logical_Expr(Base): # R724
"""
<logical-expr> = <expr>
"""
subclass_names = ['Expr']
class Char_Expr(Base): # R725
"""
<char-expr> = <expr>
"""
subclass_names = ['Expr']
class Default_Char_Expr(Base): # R726
"""
<default-char-expr> = <expr>
"""
subclass_names = ['Expr']
class Int_Expr(Base): # R727
"""
<int-expr> = <expr>
"""
subclass_names = ['Expr']
class Numeric_Expr(Base): # R728
"""
<numeric-expr> = <expr>
"""
subclass_names = ['Expr']
class Specification_Expr(Base): # R729
"""
<specification-expr> = <scalar-int-expr>
"""
subclass_names = ['Scalar_Int_Expr']
class Initialization_Expr(Base): # R730
"""
<initialization-expr> = <expr>
"""
subclass_names = ['Expr']
class Char_Initialization_Expr(Base): # R731
"""
<char-initialization-expr> = <char-expr>
"""
subclass_names = ['Char_Expr']
class Int_Initialization_Expr(Base): # R732
"""
<int-initialization-expr> = <int-expr>
"""
subclass_names = ['Int_Expr']
class Logical_Initialization_Expr(Base): # R733
"""
<logical-initialization-expr> = <logical-expr>
"""
subclass_names = ['Logical_Expr']
class Assignment_Stmt(StmtBase, BinaryOpBase): # R734
"""
<assignment-stmt> = <variable> = <expr>
"""
subclass_names = []
use_names = ['Variable', 'Expr']
def match(string):
return BinaryOpBase.match(Variable, '=', Expr, string, right=False)
match = staticmethod(match)
class Pointer_Assignment_Stmt(StmtBase): # R735
"""
<pointer-assignment-stmt> = <data-pointer-object> [ ( <bounds-spec-list> ) ] => <data-target>
| <data-pointer-object> ( <bounds-remapping-list> ) => <data-target>
| <proc-pointer-object> => <proc-target>
"""
subclass_names = []
use_names = ['Data_Pointer_Object', 'Bounds_Spec_List', 'Data_Target', 'Bounds_Remapping_List',
'Proc_Pointer_Object', 'Proc_Target']
class Data_Pointer_Object(BinaryOpBase): # R736
"""
<data-pointer-object> = <variable-name>
| <variable> % <data-pointer-component-name>
"""
subclass_names = ['Variable_Name']
use_names = ['Variable', 'Data_Pointer_Component_Name']
def match(string):
return BinaryOpBase.match(Variable, r'%', Data_Pointer_Component_Name, string)
match = staticmethod(match)
class Bounds_Spec(SeparatorBase): # R737
"""
<bounds-spec> = <lower-bound-expr> :
"""
subclass_names = []
use_names = ['Lower_Bound_Expr']
def match(string): return SeparatorBase.match(Lower_Bound_Expr, None, string, require_lhs=True)
match = staticmethod(match)
class Bounds_Remapping(SeparatorBase): # R738
"""
<bounds-remapping> = <lower-bound-expr> : <upper-bound-expr>
"""
subclass_names = []
use_classes = ['Lower_Bound_Expr', 'Upper_Bound_Expr']
def match(string): return SeparatorBase.match(Lower_Bound_Expr, Upper_Bound_Expr, string, require_lhs=True, require_rhs=True)
match = staticmethod(match)
class Data_Target(Base): # R739
"""
<data-target> = <variable>
| <expr>
"""
subclass_names = ['Variable','Expr']
class Proc_Pointer_Object(Base): # R740
"""
<proc-pointer-object> = <proc-pointer-name>
| <proc-component-ref>
"""
subclass_names = ['Proc_Pointer_Name', 'Proc_Component_Ref']
class Proc_Component_Ref(BinaryOpBase): # R741
"""
<proc-component-ref> = <variable> % <procedure-component-name>
"""
subclass_names = []
use_names = ['Variable','Procedure_Component_Name']
def match(string):
return BinaryOpBase.match(Variable, r'%', Procedure_Component_Name, string)
match = staticmethod(match)
class Proc_Target(Base): # R742
"""
<proc-target> = <expr>
| <procedure-name>
| <proc-component-ref>
"""
subclass_names = ['Proc_Component_Ref', 'Procedure_Name', 'Expr']
class Where_Stmt(StmtBase): # R743
"""
<where-stmt> = WHERE ( <mask-expr> ) <where-assignment-stmt>
"""
subclass_names = []
use_names = ['Mask_Expr', 'Where_Assignment_Stmt']
def match(string):
if string[:5].upper()!='WHERE': return
line, repmap = string_replace_map(string[5:].lstrip())
if not line.startswith('('): return
i = line.find(')')
if i==-1: return
stmt = repmap(line[i+1:].lstrip())
if not stmt: return
expr = repmap(line[1:i].strip())
if not expr: return
return Mask_Expr(expr), Where_Assignment_Stmt(stmt)
match = staticmethod(match)
def tostr(self): return 'WHERE (%s) %s' % tuple(self.items)
class Where_Construct(Base): # R744
"""
<where-construct> = <where-construct-stmt>
[ <where-body-construct> ]...
[ <masked-elsewhere-stmt>
[ <where-body-construct> ]...
]...
[ <elsewhere-stmt>
[ <where-body-construct> ]... ]
<end-where-stmt>
"""
subclass_names = []
use_names = ['Where_Construct_Stmt', 'Where_Body_Construct',
'Elsewhere_Stmt', 'End_Where_Stmt'
]
class Where_Construct_Stmt(StmtBase): # R745
"""
<where-construct-stmt> = [ <where-construct-name> : ] WHERE ( <mask-expr> )
"""
subclass_names = []
use_names = ['Where_Construct_Name', 'Mask_Expr']
def match(string):
if string[:5].upper()!='WHERE': return
line = string[5:].lstrip()
if not line: return
if line[0]+line[-1] != '()': return
line = line[1:-1].strip()
if not line: return
return Mask_Expr(line),
match = staticmethod(match)
def tostr(self): return 'WHERE (%s)' % tuple(self.items)
class Where_Body_Construct(Base): # R746
"""
<where-body-construct> = <where-assignment-stmt>
| <where-stmt>
| <where-construct>
"""
subclass_names = ['Where_Assignment_Stmt', 'Where_Stmt', 'Where_Construct']
class Where_Assignment_Stmt(Base): # R747
"""
<where-assignment-stmt> = <assignment-stmt>
"""
subclass_names = ['Assignment_Stmt']
class Mask_Expr(Base): # R748
"""
<mask-expr> = <logical-expr>
"""
subclass_names = ['Logical_Expr']
class Masked_Elsewhere_Stmt(StmtBase): # R749
"""
<masked-elsewhere-stmt> = ELSEWHERE ( <mask-expr> ) [ <where-construct-name> ]
"""
subclass_names = []
use_names = ['Mask_Expr', 'Where_Construct_Name']
def match(string):
if string[:9].upper()!='ELSEWHERE': return
line = string[9:].lstrip()
if not line.startswith('('): return
i = line.rfind(')')
if i==-1: return
expr = line[1:i].strip()
if not expr: return
line = line[i+1:].rstrip()
if line:
return Mask_Expr(expr), Where_Construct_Name(line)
return Mask_Expr(expr), None
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return 'ELSEWHERE(%s)' % (self.items[0])
return 'ELSEWHERE(%s) %s' % self.items
class Elsewhere_Stmt(StmtBase, WORDClsBase): # R750
"""
<elsewhere-stmt> = ELSEWHERE [ <where-construct-name> ]
"""
subclass_names = []
use_names = ['Where_Construct_Name']
def match(string): return WORDClsBase.match('ELSEWHERE', Where_Construct_Name, string)
match = staticmethod(match)
class End_Where_Stmt(EndStmtBase): # R751
"""
<end-where-stmt> = END WHERE [ <where-construct-name> ]
"""
subclass_names = []
use_names = ['Where_Construct_Name']
def match(string): return EndStmtBase.match('WHERE',Where_Construct_Name, string, require_stmt_type=True)
match = staticmethod(match)
class Forall_Construct(Base): # R752
"""
<forall-construct> = <forall-construct-stmt>
[ <forall-body-construct> ]...
<end-forall-stmt>
"""
subclass_names = []
use_names = ['Forall_Construct_Stmt', 'Forall_Body_Construct', 'End_Forall_Stmt']
class Forall_Construct_Stmt(StmtBase, WORDClsBase): # R753
"""
<forall-construct-stmt> = [ <forall-construct-name> : ] FORALL <forall-header>
"""
subclass_names = []
use_names = ['Forall_Construct_Name', 'Forall_Header']
def match(string): return WORDClsBase.match('FORALL', Forall_Header, string, require_cls = True)
match = staticmethod(match)
class Forall_Header(Base): # R754
"""
<forall-header> = ( <forall-triplet-spec-list> [ , <scalar-mask-expr> ] )
"""
subclass_names = []
use_names = ['Forall_Triplet_Spec_List', 'Scalar_Mask_Expr']
class Forall_Triplet_Spec(Base): # R755
"""
<forall-triplet-spec> = <index-name> = <subscript> : <subscript> [ : <stride> ]
"""
subclass_names = []
use_names = ['Index_Name', 'Subscript', 'Stride']
class Forall_Body_Construct(Base): # R756
"""
<forall-body-construct> = <forall-assignment-stmt>
| <where-stmt>
| <where-construct>
| <forall-construct>
| <forall-stmt>
"""
subclass_names = ['Forall_Assignment_Stmt', 'Where_Stmt', 'Where_Construct',
'Forall_Construct', 'Forall_Stmt']
class Forall_Assignment_Stmt(Base): # R757
"""
<forall-assignment-stmt> = <assignment-stmt>
| <pointer-assignment-stmt>
"""
subclass_names = ['Assignment_Stmt', 'Pointer_Assignment_Stmt']
class End_Forall_Stmt(EndStmtBase): # R758
"""
<end-forall-stmt> = END FORALL [ <forall-construct-name> ]
"""
subclass_names = []
use_names = ['Forall_Construct_Name']
def match(string): return EndStmtBase.match('FORALL',Forall_Construct_Name, string, require_stmt_type=True)
match = staticmethod(match)
class Forall_Stmt(StmtBase): # R759
"""
<forall-stmt> = FORALL <forall-header> <forall-assignment-stmt>
"""
subclass_names = []
use_names = ['Forall_Header', 'Forall_Assignment_Stmt']
def match(string):
if string[:6].upper()!='FORALL': return
line, repmap = string_replace_map(string[6:].lstrip())
if not line.startswith(')'): return
i = line.find(')')
if i==-1: return
header = repmap(line[1:i].strip())
if not header: return
line = repmap(line[i+1:].lstrip())
if not line: return
return Forall_Header(header), Forall_Assignment_Stmt(line)
match = staticmethod(match)
def tostr(self): return 'FORALL %s %s' % self.items
###############################################################################
############################### SECTION 8 ####################################
###############################################################################
class Block(BlockBase): # R801
"""
block = [ <execution-part-construct> ]...
"""
subclass_names = []
use_names = ['Execution_Part_Construct']
def match(string): return BlockBase.match(None, [Execution_Part_Construct], None, string)
match = staticmethod(match)
class If_Construct(BlockBase): # R802
"""
<if-construct> = <if-then-stmt>
<block>
[ <else-if-stmt>
<block>
]...
[ <else-stmt>
<block>
]
<end-if-stmt>
"""
subclass_names = []
use_names = ['If_Then_Stmt', 'Block', 'Else_If_Stmt', 'Else_Stmt', 'End_If_Stmt']
def match(reader):
content = []
try:
obj = If_Then_Stmt(reader)
except NoMatchError:
obj = None
if obj is None: return
content.append(obj)
obj = Block(reader)
if obj is None: return # todo: restore reader
content.append(obj)
while 1:
try:
obj = Else_If_Stmt(reader)
except NoMatchError:
obj = None
if obj is not None:
content.append(obj)
obj = Block(reader)
if obj is None: return # todo: restore reader
content.append(obj)
continue
try:
obj = Else_Stmt(reader)
except NoMatchError:
obj = None
if obj is not None:
content.append(obj)
obj = Block(reader)
if obj is None: return # todo: restore reader
content.append(obj)
break
try:
obj = End_If_Stmt(reader)
except NoMatchError:
obj = None
if obj is None: return # todo: restore reader
content.append(obj)
return content,
match = staticmethod(match)
def tofortran(self, tab='', isfix=None):
l = []
start = self.content[0]
end = self.content[-1]
l.append(start.tofortran(tab=tab,isfix=isfix))
for item in self.content[1:-1]:
if isinstance(item, (Else_If_Stmt, Else_Stmt)):
l.append(item.tofortran(tab=tab,isfix=isfix))
else:
l.append(item.tofortran(tab=tab+' ',isfix=isfix))
l.append(end.tofortran(tab=tab,isfix=isfix))
return '\n'.join(l)
class If_Then_Stmt(StmtBase): # R803
"""
<if-then-stmt> = [ <if-construct-name> : ] IF ( <scalar-logical-expr> ) THEN
"""
subclass_names = []
use_names = ['If_Construct_Name', 'Scalar_Logical_Expr']
def match(string):
if string[:2].upper()!='IF': return
if string[-4:].upper()!='THEN': return
line = string[2:-4].strip()
if not line: return
if line[0]+line[-1]!='()': return
return Scalar_Logical_Expr(line[1:-1].strip()),
match = staticmethod(match)
def tostr(self): return 'IF (%s) THEN' % self.items
class Else_If_Stmt(StmtBase): # R804
"""
<else-if-stmt> = ELSE IF ( <scalar-logical-expr> ) THEN [ <if-construct-name> ]
"""
subclass_names = []
use_names = ['Scalar_Logical_Expr', 'If_Construct_Name']
def match(string):
if string[:4].upper()!='ELSE': return
line = string[4:].lstrip()
if line[:2].upper()!='IF': return
line = line[2:].lstrip()
if not line.startswith('('): return
i = line.rfind(')')
if i==-1: return
expr = line[1:i].strip()
line = line[i+1:].lstrip()
if line[:4].upper()!='THEN': return
line = line[4:].lstrip()
if line: return Scalar_Logical_Expr(expr), If_Construct_Name(line)
return Scalar_Logical_Expr(expr), None
match = staticmethod(match)
def tostr(self):
if self.items[1] is None:
return 'ELSE IF (%s) THEN' % (self.items[0])
return 'ELSE IF (%s) THEN %s' % self.items
class Else_Stmt(StmtBase): # R805
"""
<else-stmt> = ELSE [ <if-construct-name> ]
"""
subclass_names = []
use_names = ['If_Construct_Name']
def match(string):
if string[:4].upper()!='ELSE': return
line = string[4:].lstrip()
if line: return If_Construct_Name(line),
return None,
match = staticmethod(match)
def tostr(self):
if self.items[0] is None:
return 'ELSE'
return 'ELSE %s' % self.items
class End_If_Stmt(EndStmtBase): # R806
"""
<end-if-stmt> = END IF [ <if-construct-name> ]
"""
subclass_names = []
use_names = ['If_Construct_Name']
def match(string): return EndStmtBase.match('IF',If_Construct_Name, string, require_stmt_type=True)
match = staticmethod(match)
class If_Stmt(StmtBase): # R807
"""
<if-stmt> = IF ( <scalar-logical-expr> ) <action-stmt>
"""
subclass_names = []
use_names = ['Scalar_Logical_Expr', 'Action_Stmt_C802']
def match(string):
if string[:2].upper() != 'IF': return
line, repmap = string_replace_map(string)
line = line[2:].lstrip()
if not line.startswith('('): return
i = line.find(')')
if i==-1: return
expr = repmap(line[1:i].strip())
stmt = repmap(line[i+1:].lstrip())
return Scalar_Logical_Expr(expr), Action_Stmt_C802(stmt)
match = staticmethod(match)
def tostr(self): return 'IF (%s) %s' % self.items
class Case_Construct(Base): # R808
"""
<case-construct> = <select-case-stmt>
[ <case-stmt>
<block>
]..
<end-select-stmt>
"""
subclass_names = []
use_names = ['Select_Case_Stmt', 'Case_Stmt', 'End_Select_Stmt']
class Select_Case_Stmt(StmtBase, CALLBase): # R809
"""
<select-case-stmt> = [ <case-construct-name> : ] SELECT CASE ( <case-expr> )
"""
subclass_names = []
use_names = ['Case_Construct_Name', 'Case_Expr']
def match(string): return CALLBase.match(pattter.abs_select_case, Case_Expr, string)
match = staticmethod(match)
class Case_Stmt(StmtBase): # R810
"""
<case-stmt> = CASE <case-selector> [ <case-construct-name> ]
"""
subclass_names = []
use_names = ['Case_Selector', 'Case_Construct_Name']
class End_Select_Stmt(EndStmtBase): # R811
"""
<end-select-stmt> = END SELECT [ <case-construct-name> ]
"""
subclass_names = []
use_names = ['Case_Construct_Name']
def match(string): return EndStmtBase.match('SELECT',Case_Construct_Name, string, require_stmt_type=True)
match = staticmethod(match)
class Case_Expr(Base): # R812
"""
<case-expr> = <scalar-int-expr>
| <scalar-char-expr>
| <scalar-logical-expr>
"""
subclass_names = []
subclass_names = ['Scalar_Int_Expr', 'Scalar_Char_Expr', 'Scalar_Logical_Expr']
class Case_Selector(Base): # R813
"""
<case-selector> = ( <case-value-range-list> )
| DEFAULT
"""
subclass_names = []
use_names = ['Case_Value_Range_List']
class Case_Value_Range(SeparatorBase): # R814
"""
<case-value-range> = <case-value>
| <case-value> :
| : <case-value>
| <case-value> : <case-value>
"""
subclass_names = ['Case_Value']
def match(string): return SeparatorBase.match(Case_Value, Case_Value, string)
match = staticmethod(match)
class Case_Value(Base): # R815
"""
<case-value> = <scalar-int-initialization-expr>
| <scalar-char-initialization-expr>
| <scalar-logical-initialization-expr>
"""
subclass_names = ['Scalar_Int_Initialization_Expr', 'Scalar_Char_Initialization_Expr', 'Scalar_Logical_Initialization_Expr']
class Associate_Construct(Base): # R816
"""
<associate-construct> = <associate-stmt>
<block>
<end-associate-stmt>
"""
subclass_names = []
use_names = ['Associate_Stmt', 'Block', 'End_Associate_Stmt']
class Associate_Stmt(StmtBase, CALLBase): # R817
"""
<associate-stmt> = [ <associate-construct-name> : ] ASSOCIATE ( <association-list> )
"""
subclass_names = []
use_names = ['Associate_Construct_Name', 'Association_List']
def match(string): return CALLBase.match('ASSOCIATE', Association_List, string)
match = staticmethod(match)
class Association(BinaryOpBase): # R818
"""
<association> = <associate-name> => <selector>
"""
subclass_names = []
use_names = ['Associate_Name', 'Selector']
def match(string): return BinaryOpBase.match(Assiciate_Name, '=>', Selector, string)
match = staticmethod(match)
class Selector(Base): # R819
"""
<selector> = <expr>
| <variable>
"""
subclass_names = ['Expr', 'Variable']
class End_Associate_Stmt(EndStmtBase): # R820
"""
<end-associate-stmt> = END ASSOCIATE [ <associate-construct-name> ]
"""
subclass_names = []
use_names = ['Associate_Construct_Name']
def match(string): return EndStmtBase.match('ASSOCIATE',Associate_Construct_Name, string, require_stmt_type=True)
match = staticmethod(match)
class Select_Type_Construct(Base): # R821
"""
<select-type-construct> = <select-type-stmt>
[ <type-guard-stmt>
<block>
]...
<end-select-type-stmt>
"""
subclass_names = []
use_names = ['Select_Type_Stmt', 'Type_Guard_Stmt', 'Block', 'End_Select_Type_Stmt']
class Select_Type_Stmt(StmtBase): # R822
"""
<select-type-stmt> = [ <select-construct-name> : ] SELECT TYPE ( [ <associate-name> => ] <selector> )
"""
subclass_names = []
use_names = ['Select_Construct_Name', 'Associate_Name', 'Selector']
class Type_Guard_Stmt(StmtBase): # R823
"""
<type-guard-stmt> = TYPE IS ( <type-spec> ) [ <select-construct-name> ]
| CLASS IS ( <type-spec> ) [ <select-construct-name> ]
| CLASS DEFAULT [ <select-construct-name> ]
"""
subclass_names = []
use_names = ['Type_Spec', 'Select_Construct_Name']
def match(string):
if string[:4].upper()=='TYPE':
line = string[4:].lstrip()
if not line[:2].upper()=='IS': return
line = line[2:].lstrip()
kind = 'TYPE IS'
elif string[:5].upper()=='CLASS':
line = string[5:].lstrip()
if line[:2].upper()=='IS':
line = line[2:].lstrip()
kind = 'CLASS IS'
elif line[:7].upper()=='DEFAULT':
line = line[7:].lstrip()
if line:
if isalnum(line[0]): return
return 'CLASS DEFAULT', None, Select_Construct_Name(line)
return 'CLASS DEFAULT', None, None
else:
return
else:
return
if not line.startswith('('): return
i = line.rfind(')')
if i==-1: return
l = line[1:i].strip()
if not l: return
line = line[i+1:].lstrip()
if line:
return kind, Type_Spec(l), Select_Construct_Name(line)
return kind, Type_Spec(l), None
match = staticmethod(match)
def tostr(self):
s = str(self.items[0])
if self.items[1] is not None:
s += ' (%s)' % (self.items[0])
if self.items[2] is not None:
s += ' %s' % (self.items[2])
return s
class End_Select_Type_Stmt(EndStmtBase): # R824
"""
<end-select-type-stmt> = END SELECT [ <select-construct-name> ]
"""
subclass_names = []
use_names = ['Select_Construct_Name']
def match(string): return EndStmtBase.match('SELECT',Select_Construct_Name, string, require_stmt_type=True)
match = staticmethod(match)
class Do_Construct(Base): # R825
"""
<do-construct> = <block-do-construct>
| <nonblock-do-construct>
"""
subclass_names = ['Block_Do_Construct', 'Nonblock_Do_Construct']
class Block_Do_Construct(BlockBase): # R826
"""
<block-do-construct> = <do-stmt>
<do-block>
<end-do>
"""
subclass_names = []
use_names = ['Do_Stmt', 'Do_Block', 'End_Do']
def match(reader):
assert isinstance(reader,FortranReaderBase),`reader`
content = []
try:
obj = Do_Stmt(reader)
except NoMatchError:
obj = None
if obj is None: return
content.append(obj)
if isinstance(obj, Label_Do_Stmt):
label = str(obj.dolabel)
while 1:
try:
obj = Execution_Part_Construct(reader)
except NoMatchError:
obj = None
if obj is None: break
content.append(obj)
if isinstance(obj, Continue_Stmt) and obj.item.label==label:
return content,
return
raise RuntimeError,'Expected continue stmt with specified label'
else:
obj = End_Do(reader)
content.append(obj)
raise NotImplementedError
return content,
match = staticmethod(match)
def tofortran(self, tab='', isfix=None):
if not isinstance(self.content[0], Label_Do_Stmt):
return BlockBase.tofortran(tab, isfix)
l = []
start = self.content[0]
end = self.content[-1]
extra_tab = ' '
l.append(start.tofortran(tab=tab,isfix=isfix))
for item in self.content[1:-1]:
l.append(item.tofortran(tab=tab+extra_tab,isfix=isfix))
if len(self.content)>1:
l.append(end.tofortran(tab=tab,isfix=isfix))
return '\n'.join(l)
class Do_Stmt(Base): # R827
"""
<do-stmt> = <label-do-stmt>
| <nonlabel-do-stmt>
"""
subclass_names = ['Label_Do_Stmt', 'Nonlabel_Do_Stmt']
class Label_Do_Stmt(StmtBase): # R828
"""
<label-do-stmt> = [ <do-construct-name> : ] DO <label> [ <loop-control> ]
"""
subclass_names = []
use_names = ['Do_Construct_Name', 'Label', 'Loop_Control']
def match(string):
if string[:2].upper()!='DO': return
line = string[2:].lstrip()
m = pattern.label.match(line)
if m is None: return
label = m.group()
line = line[m.end():].lstrip()
if line: return Label(label), Loop_Control(line)
return Label(label), None
match = staticmethod(match)
def tostr(self):
if self.itens[1] is None: return 'DO %s' % (self.items[0])
return 'DO %s %s' % self.items
class Nonlabel_Do_Stmt(StmtBase, WORDClsBase): # R829
"""
<nonlabel-do-stmt> = [ <do-construct-name> : ] DO [ <loop-control> ]
"""
subclass_names = []
use_names = ['Do_Construct_Name', 'Loop_Control']
def match(string): return WORDClsBase.match('DO', Loop_Control, string)
match = staticmethod(match)
class Loop_Control(Base): # R830
"""
<loop-control> = [ , ] <do-variable> = <scalar-int-expr> , <scalar-int-expr> [ , <scalar-int-expr> ]
| [ , ] WHILE ( <scalar-logical-expr> )
"""
subclass_names = []
use_names = ['Do_Variable', 'Scalar_Int_Expr', 'Scalar_Logical_Expr']
def match(string):
if string.startswith(','):
line, repmap = string_replace_map(string[1:].lstrip())
else:
line, repmap = string_replace_map(string)
if line[:5].upper()=='WHILE' and line[5:].lstrip().startswith('('):
l = line[5:].lstrip()
i = l.find(')')
if i!=-1 and i==len(l)-1:
return Scalar_Logical_Expr(repmap(l[1:i].strip())),
if line.count('=')!=1: return
var,rhs = line.split('=')
rhs = [s.strip() for s in rhs.lstrip().split(',')]
if not 2<=len(rhs)<=3: return
return Variable(repmap(var.rstrip())),map(Scalar_Int_Expr, map(repmap,rhs))
match = staticmethod(match)
def tostr(self):
if len(self.items)==1: return ', WHILE (%s)' % (self.items[0])
return ', %s = %s' % (self.items[0], ', '.join(map(str,self.items[1])))
class Do_Variable(Base): # R831
"""
<do-variable> = <scalar-int-variable>
"""
subclass_names = ['Scalar_Int_Variable']
class Do_Block(Base): # R832
"""
<do-block> = <block>
"""
subclass_names = ['Block']
class End_Do(Base): # R833
"""
<end-do> = <end-do-stmt>
| <continue-stmt>
"""
subclass_names = ['End_Do_Stmt', 'Continue_Stmt']
class End_Do_Stmt(EndStmtBase): # R834
"""
<end-do-stmt> = END DO [ <do-construct-name> ]
"""
subclass_names = []
use_names = ['Do_Construct_Name']
def match(string): return EndStmtBase.match('DO',Do_Construct_Name, string, require_stmt_type=True)
match = staticmethod(match)
class Nonblock_Do_Construct(Base): # R835
"""
<nonblock-do-stmt> = <action-term-do-construct>
| <outer-shared-do-construct>
"""
subclass_names = ['Action_Term_Do_Construct', 'Outer_Shared_Do_Construct']
class Action_Term_Do_Construct(BlockBase): # R836
"""
<action-term-do-construct> = <label-do-stmt>
<do-body>
<do-term-action-stmt>
"""
subclass_names = []
use_names = ['Label_Do_Stmt', 'Do_Body', 'Do_Term_Action_Stmt']
def match(reader):
content = []
for cls in [Label_Do_Stmt, Do_Body, Do_Term_Action_Stmt]:
obj = cls(reader)
if obj is None: # todo: restore reader
return
content.append(obj)
return content,
match = staticmethod(match)
class Do_Body(BlockBase): # R837
"""
<do-body> = [ <execution-part-construct> ]...
"""
subclass_names = []
use_names = ['Execution_Part_Construct']
def match(string): return BlockBase.match(None, [Execution_Part_Construct], None, string)
match = staticmethod(match)
class Do_Term_Action_Stmt(StmtBase): # R838
"""
<do-term-action-stmt> = <action-stmt>
C824: <do-term-action-stmt> shall not be <continue-stmt>, <goto-stmt>, <return-stmt>, <stop-stmt>,
<exit-stmt>, <cycle-stmt>, <end-function-stmt>, <end-subroutine-stmt>,
<end-program-stmt>, <arithmetic-if-stmt>
"""
subclass_names = ['Action_Stmt_C824']
class Outer_Shared_Do_Construct(BlockBase): # R839
"""
<outer-shared-do-construct> = <label-do-stmt>
<do-body>
<shared-term-do-construct>
"""
subclass_names = []
use_names = ['Label_Do_Stmt', 'Do_Body', 'Shared_Term_Do_Construct']
def match(reader):
content = []
for cls in [Label_Do_Stmt, Do_Body, Shared_Term_Do_Construct]:
obj = cls(reader)
if obj is None: # todo: restore reader
return
content.append(obj)
return content,
match = staticmethod(match)
class Shared_Term_Do_Construct(Base): # R840
"""
<shared-term-do-construct> = <outer-shared-do-construct>
| <inner-shared-do-construct>
"""
subclass_names = ['Outer_Shared_Do_Construct', 'Inner_Shared_Do_Construct']
class Inner_Shared_Do_Construct(BlockBase): # R841
"""
<inner-shared-do-construct> = <label-do-stmt>
<do-body>
<do-term-shared-stmt>
"""
subclass_names = []
use_names = ['Label_Do_Stmt', 'Do_Body', 'Do_Term_Shared_Stmt']
def match(reader):
content = []
for cls in [Label_Do_Stmt, Do_Body, Do_Term_Shared_Stmt]:
obj = cls(reader)
if obj is None: # todo: restore reader
return
content.append(obj)
return content,
match = staticmethod(match)
class Do_Term_Shared_Stmt(StmtBase): # R842
"""
<do-term-shared-stmt> = <action-stmt>
C826: see C824 above.
"""
subclass_names = ['Action_Stmt']
class Cycle_Stmt(StmtBase, WORDClsBase): # R843
"""
<cycle-stmt> = CYCLE [ <do-construct-name> ]
"""
subclass_names = []
use_names = ['Do_Construct_Name']
def match(string): return WORDClsBase.match('CYCLE', Do_Construct_Name, string)
match = staticmethod(match)
class Exit_Stmt(StmtBase, WORDClsBase): # R844
"""
<exit-stmt> = EXIT [ <do-construct-name> ]
"""
subclass_names = []
use_names = ['Do_Construct_Name']
def match(string): return WORDClsBase.match('EXIT', Do_Construct_Name, string)
match = staticmethod(match)
class Goto_Stmt(StmtBase): # R845
"""
<goto-stmt> = GO TO <label>
"""
subclass_names = []
use_names = ['Label']
def match(string):
if string[:2].upper() != 'GO': return
line = string[2:].lstrip()
if line[:2].upper() != 'TO': return
return Label(line[2:].lstrip()),
match = staticmethod(match)
def tostr(self): return 'GO TO %s' % (self.items[0])
class Computed_Goto_Stmt(StmtBase): # R846
"""
<computed-goto-stmt> = GO TO ( <label-list> ) [ , ] <scalar-int-expr>
"""
subclass_names = []
use_names = ['Label_List', 'Scalar_Int_Expr']
def match(string):
if string[:2].upper()!='GO': return
line = string[2:].lstrip()
if line[:2].upper()!='TO': return
line = line[2:].lstrip()
if not line.startswith('('): return
i = line.find(')')
if i==-1: return
lst = line[1:i].strip()
if not lst: return
line = line[i+1:].lstrip()
if line.startswith(','):
line = line[1:].lstrip()
if not line: return
return Label_List(lst), Scalar_Int_Expr(line)
match = staticmethod(match)
def tostr(self): return 'GO TO (%s), %s' % self.items
class Arithmetic_If_Stmt(StmtBase): # R847
"""
<arithmetic-if-stmt> = IF ( <scalar-numeric-expr> ) <label> , <label> , <label>
"""
subclass_names = []
use_names = ['Scalar_Numeric_Expr', 'Label']
def match(string):
if string[:2].upper() != 'IF': return
line = string[2:].lstrip()
if not line.startswith('('): return
i = line.rfind(')')
if i==-1: return
labels = line[i+1:].lstrip().split(',')
if len(labels) != 3: return
labels = [Label(l.strip()) for l in labels]
return (Scalar_Numeric_Expr(line[1:i].strip()),) + tuple(labels)
match = staticmethod(match)
def tostr(self): return 'IF (%s) %s, %s, %s' % self.items
class Continue_Stmt(StmtBase, STRINGBase): # R848
"""
<continue-stmt> = CONTINUE
"""
subclass_names = []
def match(string): return STRINGBase.match('CONTINUE', string)
match = staticmethod(match)
class Stop_Stmt(StmtBase, WORDClsBase): # R849
"""
<stop-stmt> = STOP [ <stop-code> ]
"""
subclass_names = []
use_names = ['Stop_Code']
def match(string): return WORDClsBase.match('STOP', Stop_Code, string)
match = staticmethod(match)
class Stop_Code(StringBase): # R850
"""
<stop-code> = <scalar-char-constant>
| <digit> [ <digit> [ <digit> [ <digit> [ <digit> ] ] ] ]
"""
subclass_names = ['Scalar_Char_Constant']
def match(string): return StringBase.match(pattern.abs_label, string)
match = staticmethod(match)
###############################################################################
############################### SECTION 9 ####################################
###############################################################################
class Io_Unit(StringBase): # R901
"""
<io-unit> = <file-unit-number>
| *
| <internal-file-variable>
"""
subclass_names = ['File_Unit_Number', 'Internal_File_Variable']
def match(string): return StringBase.match('*', string)
match = staticmethod(match)
class File_Unit_Number(Base): # R902
"""
<file-unit-number> = <scalar-int-expr>
"""
subclass_names = ['Scalar_Int_Expr']
class Internal_File_Variable(Base): # R903
"""
<internal-file-variable> = <char-variable>
C901: <char-variable> shall not be an array section with a vector subscript.
"""
subclass_names = ['Char_Variable']
class Open_Stmt(StmtBase, CALLBase): # R904
"""
<open-stmt> = OPEN ( <connect-spec-list> )
"""
subclass_names = []
use_names = ['Connect_Spec_List']
def match(string): CALLBase.match('OPEN', Connect_Spec_List, string, require_rhs=True)
match = staticmethod(match)
class Connect_Spec(KeywordValueBase): # R905
"""
<connect-spec> = [ UNIT = ] <file-unit-number>
| ACCESS = <scalar-default-char-expr>
| ACTION = <scalar-default-char-expr>
| ASYNCHRONOUS = <scalar-default-char-expr>
| BLANK = <scalar-default-char-expr>
| DECIMAL = <scalar-default-char-expr>
| DELIM = <scalar-default-char-expr>
| ENCODING = <scalar-default-char-expr>
| ERR = <label>
| FILE = <file-name-expr>
| FORM = <scalar-default-char-expr>
| IOMSG = <iomsg-variable>
| IOSTAT = <scalar-int-variable>
| PAD = <scalar-default-char-expr>
| POSITION = <scalar-default-char-expr>
| RECL = <scalar-int-expr>
| ROUND = <scalar-default-char-expr>
| SIGN = <scalar-default-char-expr>
| STATUS = <scalar-default-char-expr>
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Scalar_Default_Char_Expr', 'Label', 'File_Name_Expr', 'Iomsg_Variable',
'Scalar_Int_Expr', 'Scalar_Int_Variable']
def match(string):
for (k,v) in [\
(['ACCESS','ACTION','ASYNCHRONOUS','BLANK','DECIMAL','DELIM','ENCODING',
'FORM','PAD','POSITION','ROUND','SIGN','STATUS'], Scalar_Default_Char_Expr),
('ERR', Label),
('FILE',File_Name_Expr),
('IOSTAT', Scalar_Int_Variable),
('IOMSG', Iomsg_Variable),
('RECL', Scalar_Int_Expr),
('UNIT', File_Unit_Number),
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return 'UNIT', File_Unit_Number
match = staticmethod(match)
class File_Name_Expr(Base): # R906
"""
<file-name-expr> = <scalar-default-char-expr>
"""
subclass_names = ['Scalar_Default_Char_Expr']
class Iomsg_Variable(Base): # R907
"""
<iomsg-variable> = <scalar-default-char-variable>
"""
subclass_names = ['Scalar_Default_Char_Variable']
class Close_Stmt(StmtBase, CALLBase): # R908
"""
<close-stmt> = CLOSE ( <close-spec-list> )
"""
subclass_names = []
use_names = ['Close_Spec_List']
def match(string): CALLBase.match('CLOSE', Close_Spec_List, string, require_rhs=True)
match = staticmethod(match)
class Close_Spec(KeywordValueBase): # R909
"""
<close-spec> = [ UNIT = ] <file-unit-number>
| IOSTAT = <scalar-int-variable>
| IOMSG = <iomsg-variable>
| ERR = <label>
| STATUS = <scalar-default-char-expr>
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Scalar_Default_Char_Expr', 'Label', 'Iomsg_Variable',
'Scalar_Int_Variable']
def match(string):
for (k,v) in [\
('ERR', Label),
('IOSTAT', Scalar_Int_Variable),
('IOMSG', Iomsg_Variable),
('STATUS', Scalar_Default_Char_Expr),
('UNIT', File_Unit_Number),
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return 'UNIT', File_Unit_Number(string)
match = staticmethod(match)
class Read_Stmt(StmtBase): # R910
"""
<read-stmt> = READ ( <io-control-spec-list> ) [ <input-item-list> ]
| READ <format> [ , <input-item-list> ]
"""
subclass_names = []
use_names = ['Io_Control_Spec_List', 'Input_Item_List', 'Format']
class Write_Stmt(StmtBase): # R911
"""
<write-stmt> = WRITE ( <io-control-spec-list> ) [ <output-item-list> ]
"""
subclass_names = []
use_names = ['Io_Control_Spec_List', 'Output_Item_List']
def match(string):
if string[:5].upper()!='WRITE': return
line = string[5:].lstrip()
if not line.startswith('('): return
line, repmap = string_replace_map(line)
i = line.find(')')
if i==-1: return
l = line[1:i].strip()
if not l: return
l = repmap(l)
if i==len(line)-1:
return Io_Control_Spec_List(l),None
return Io_Control_Spec_List(l), Output_Item_List(repmap(line[i+1:].lstrip()))
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return 'WRITE(%s)' % (self.items[0])
return 'WRITE(%s) %s' % tuple(self.items)
class Print_Stmt(StmtBase): # R912
"""
<print-stmt> = PRINT <format> [ , <output-item-list> ]
"""
subclass_names = []
use_names = ['Format', 'Output_Item_List']
def match(string):
if string[:5].upper()!='PRINT': return
line = string[5:]
if not line: return
c = line[0].upper()
if 'A'<=c<='Z' or c=='_' or '0'<=c<='9': return
line, repmap = string_replace_map(line.lstrip())
i = line.find(',')
if i==-1: return Format(repmap(line)), None
l = repmap(line[i+1:].lstrip())
if not l: return
return Format(repmap(line[:i].rstrip())), Output_Item_List(l)
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return 'PRINT %s' % (self.items[0])
return 'PRINT %s, %s' % tuple(self.items)
class Io_Control_Spec_List(SequenceBase): # R913-list
"""
<io-control-spec-list> is a list taking into account C910, C917, C918
"""
subclass_names = []
use_names = ['Io_Control_Spec']
def match(string):
line, repmap = string_replace_map(string)
splitted = line.split(',')
if not splitted: return
lst = []
for i in range(len(splitted)):
p = splitted[i].strip()
if i==0:
if '=' not in p: p = 'UNIT=%s' % (repmap(p))
else: p = repmap(p)
elif i==1:
if '=' not in p:
p = repmap(p)
try:
f = Format(p)
# todo: make sure that f is char-expr, if not, raise NoMatchError
p = 'FMT=%s' % (Format(p))
except NoMatchError:
p = 'NML=%s' % (Namelist_Group_Name(p))
else:
p = repmap(p)
else:
p = repmap(p)
lst.append(Io_Control_Spec(p))
return ',', tuple(lst)
match = staticmethod(match)
class Io_Control_Spec(KeywordValueBase): # R913
"""
<io-control-spec> = [ UNIT = ] <io-unit>
| [ FMT = ] <format>
| [ NML = ] <namelist-group-name>
| ADVANCE = <scalar-default-char-expr>
| ASYNCHRONOUS = <scalar-char-initialization-expr>
| BLANK = <scalar-default-char-expr>
| DECIMAL = <scalar-default-char-expr>
| DELIM = <scalar-default-char-expr>
| END = <label>
| EOR = <label>
| ERR = <label>
| ID = <scalar-int-variable>
| IOMSG = <iomsg-variable>
| IOSTAT = <scalar-int-variable>
| PAD = <scalar-default-char-expr>
| POS = <scalar-int-expr>
| REC = <scalar-int-expr>
| ROUND = <scalar-default-char-expr>
| SIGN = <scalar-default-char-expr>
| SIZE = <scalar-int-variable>
"""
subclass_names = []
use_names = ['Io_Unit', 'Format', 'Namelist_Group_Name', 'Scalar_Default_Char_Expr',
'Scalar_Char_Initialization_Expr', 'Label', 'Scalar_Int_Variable',
'Iomsg_Variable', 'Scalar_Int_Expr']
def match(string):
for (k,v) in [\
(['ADVANCE', 'BLANK', 'DECIMAL', 'DELIM', 'PAD', 'ROUND', 'SIGN'], Scalar_Default_Char_Expr),
('ASYNCHRONOUS', Scalar_Char_Initialization_Expr),
(['END','EOR','ERR'], Label),
(['ID','IOSTAT','SIZE'], Scalar_Int_Variable),
('IOMSG', Iomsg_Variable),
(['POS', 'REC'], Scalar_Int_Expr),
('UNIT', Io_Unit),
('FMT', Format),
('NML', Namelist_Group_Name)
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return
match = staticmethod(match)
class Format(StringBase): # R914
"""
<format> = <default-char-expr>
| <label>
| *
"""
subclass_names = ['Label', 'Default_Char_Expr']
def match(string): return StringBase.match('*', string)
match = staticmethod(match)
class Input_Item(Base): # R915
"""
<input-item> = <variable>
| <io-implied-do>
"""
subclass_names = ['Variable', 'Io_Implied_Do']
class Output_Item(Base): # R916
"""
<output-item> = <expr>
| <io-implied-do>
"""
subclass_names = ['Expr', 'Io_Implied_Do']
class Io_Implied_Do(Base): # R917
"""
<io-implied-do> = ( <io-implied-do-object-list> , <io-implied-do-control> )
"""
subclass_names = []
use_names = ['Io_Implied_Do_Object_List', 'Io_Implied_Do_Control']
class Io_Implied_Do_Object(Base): # R918
"""
<io-implied-do-object> = <input-item>
| <output-item>
"""
subclass_names = ['Input_Item', 'Output_Item']
class Io_Implied_Do_Control(Base): # R919
"""
<io-implied-do-control> = <do-variable> = <scalar-int-expr> , <scalar-int-expr> [ , <scalar-int-expr> ]
"""
subclass_names = []
use_names = ['Do_Variable', 'Scalar_Int_Expr']
class Dtv_Type_Spec(CALLBase): # R920
"""
<dtv-type-spec> = TYPE ( <derived-type-spec> )
| CLASS ( <derived-type-spec> )
"""
subclass_names = []
use_names = ['Derived_Type_Spec']
def match(string): CALLStmt.match(['TYPE', 'CLASS'], Derived_Type_Spec, string, require_rhs=True)
match = staticmethod(match)
class Wait_Stmt(StmtBase, CALLBase): # R921
"""
<wait-stmt> = WAIT ( <wait-spec-list> )
"""
subclass_names = []
use_names = ['Wait_Spec_List']
def match(string): return CALLBase.match('WAIT', Wait_Spec_List, string, require_rhs=True)
match = staticmethod(match)
class Wait_Spec(KeywordValueBase): # R922
"""
<wait-spec> = [ UNIT = ] <file-unit-number>
| END = <label>
| EOR = <label>
| ERR = <label>
| ID = <scalar-int-expr>
| IOMSG = <iomsg-variable>
| IOSTAT = <scalar-int-variable>
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Label', 'Scalar_Int_Expr', 'Iomsg_Variable', 'Scalar_Int_Variable']
def match(string):
for (k,v) in [\
(['END','EOR','ERR'], Label),
('IOSTAT', Scalar_Int_Variable),
('IOMSG', Iomsg_Variable),
('ID', Scalar_Int_Expr),
('UNIT', File_Unit_Number),
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return 'UNIT', File_Unit_Number(string)
match = staticmethod(match)
class Backspace_Stmt(StmtBase): # R923
"""
<backspace-stmt> = BACKSPACE <file-unit-number>
| BACKSPACE ( <position-spec-list> )
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Position_Spec_List']
class Endfile_Stmt(StmtBase): # R924
"""
<endfile-stmt> = ENDFILE <file-unit-number>
| ENDFILE ( <position-spec-list> )
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Position_Spec_List']
class Rewind_Stmt(StmtBase): # R925
"""
<rewind-stmt> = REWIND <file-unit-number>
| REWIND ( <position-spec-list> )
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Position_Spec_List']
class Position_Spec(KeywordValueBase): # R926
"""
<position-spec> = [ UNIT = ] <file-unit-number>
| IOMSG = <iomsg-variable>
| IOSTAT = <scalar-int-variable>
| ERR = <label>
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Iomsg_Variable', 'Scalar_Int_Variable', 'Label']
def match(string):
for (k,v) in [\
('ERR', Label),
('IOSTAT', Scalar_Int_Variable),
('IOMSG', Iomsg_Variable),
('UNIT', File_Unit_Number),
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return 'UNIT', File_Unit_Number(string)
match = staticmethod(match)
class Flush_Stmt(StmtBase): # R927
"""
<flush-stmt> = FLUSH <file-unit-number>
| FLUSH ( <position-spec-list> )
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Position_Spec_List']
class Flush_Spec(KeywordValueBase): # R928
"""
<flush-spec> = [ UNIT = ] <file-unit-number>
| IOMSG = <iomsg-variable>
| IOSTAT = <scalar-int-variable>
| ERR = <label>
"""
subclass_names = []
use_names = ['File_Unit_Number', 'Iomsg_Variable', 'Scalar_Int_Variable', 'Label']
def match(string):
for (k,v) in [\
('ERR', Label),
('IOSTAT', Scalar_Int_Variable),
('IOMSG', Iomsg_Variable),
('UNIT', File_Unit_Number),
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return 'UNIT', File_Unit_Number(string)
match = staticmethod(match)
class Inquire_Stmt(StmtBase): # R929
"""
<inquire-stmt> = INQUIRE ( <inquire-spec-list> )
| INQUIRE ( IOLENGTH = <scalar-int-variable> ) <output-item-list>
"""
subclass_names = []
use_names = ['Inquire_Spec_List', 'Scalar_Int_Variable', 'Output_Item_List']
class Inquire_Spec(KeywordValueBase): # R930
"""
<inquire-spec> = [ UNIT = ] <file-unit-number>
| FILE = <file-name-expr>
| ACCESS = <scalar-default-char-variable>
| ACTION = <scalar-default-char-variable>
| ASYNCHRONOUS = <scalar-default-char-variable>
| BLANK = <scalar-default-char-variable>
| DECIMAL = <scalar-default-char-variable>
| DELIM = <scalar-default-char-variable>
| DIRECT = <scalar-default-char-variable>
| ENCODING = <scalar-default-char-variable>
| ERR = <label>
| EXIST = <scalar-default-logical-variable>
| FORM = <scalar-default-char-variable>
| FORMATTED = <scalar-default-char-variable>
| ID = <scalar-int-expr>
| IOMSG = <iomsg-variable>
| IOSTAT = <scalar-int-variable>
| NAME = <scalar-default-char-variable>
| NAMED = <scalar-default-logical-variable>
| NEXTREC = <scalar-int-variable>
| NUMBER = <scalar-int-variable>
| OPENED = <scalar-default-logical-variable>
| PAD = <scalar-default-char-variable>
| PENDING = <scalar-default-logical-variable>
| POS = <scalar-int-variable>
| POSITION = <scalar-default-char-variable>
| READ = <scalar-default-char-variable>
| READWRITE = <scalar-default-char-variable>
| RECL = <scalar-int-variable>
| ROUND = <scalar-default-char-variable>
| SEQUENTIAL = <scalar-default-char-variable>
| SIGN = <scalar-default-char-variable>
| SIZE = <scalar-int-variable>
| STREAM = <scalar-default-char-variable>
| UNFORMATTED = <scalar-default-char-variable>
| WRITE = <scalar-default-char-variable>
"""
subclass_names = []
use_names = ['File_Unit_Number', 'File_Name_Expr', 'Scalar_Default_Char_Variable',
'Scalar_Default_Logical_Variable', 'Scalar_Int_Variable', 'Scalar_Int_Expr',
'Label', 'Iomsg_Variable']
def match(string):
for (k,v) in [\
(['ACCESS','ACTION','ASYNCHRONOUS', 'BLANK', 'DECIMAL', 'DELIM',
'DIRECT','ENCODING','FORM','NAME','PAD', 'POSITION','READ','READWRITE',
'ROUND', 'SEQUENTIAL', 'SIGN','STREAM','UNFORMATTED','WRITE'],
Scalar_Default_Char_Variable),
('ERR', Label),
(['EXIST','NAMED','PENDING'], Scalar_Default_Logical_Variable),
('ID', Scalar_Int_Expr),
(['IOSTAT','NEXTREC','NUMBER','POS','RECL','SIZE'], Scalar_Int_Variable),
('IOMSG', Iomsg_Variable),
('FILE', File_Name_Expr),
('UNIT', File_Unit_Number),
]:
try:
obj = KeywordValueBase.match(k, v, string, upper_lhs = True)
except NoMatchError:
obj = None
if obj is not None: return obj
return 'UNIT', File_Unit_Number(string)
return
match = staticmethod(match)
###############################################################################
############################### SECTION 10 ####################################
###############################################################################
class Format_Stmt(StmtBase, WORDClsBase): # R1001
"""
<format-stmt> = FORMAT <format-specification>
"""
subclass_names = []
use_names = ['Format_Specification']
def match(string): WORDClsBase.match('FORMAT', Format_Specification, string, require_cls=True)
match = staticmethod(match)
class Format_Specification(BracketBase): # R1002
"""
<format-specification> = ( [ <format-item-list> ] )
"""
subclass_names = []
use_names = ['Format_Item_List']
def match(string): return BracketBase.match('()', Format_Item_List, string, require_cls=False)
match = staticmethod(match)
class Format_Item(Base): # R1003
"""
<format-item> = [ <r> ] <data-edit-desc>
| <control-edit-desc>
| <char-string-edit-desc>
| [ <r> ] ( <format-item-list> )
"""
subclass_names = ['Control_Edit_Desc', 'Char_String_Edit_Desc']
use_names = ['R', 'Format_Item_List']
class R(Base): # R1004
"""
<r> = <int-literal-constant>
<r> shall be positive and without kind parameter specified.
"""
subclass_names = ['Int_Literal_Constant']
class Data_Edit_Desc(Base): # R1005
"""
<data-edit-desc> = I <w> [ . <m> ]
| B <w> [ . <m> ]
| O <w> [ . <m> ]
| Z <w> [ . <m> ]
| F <w> . <d>
| E <w> . <d> [ E <e> ]
| EN <w> . <d> [ E <e> ]
| ES <w> . <d> [ E <e>]
| G <w> . <d> [ E <e> ]
| L <w>
| A [ <w> ]
| D <w> . <d>
| DT [ <char-literal-constant> ] [ ( <v-list> ) ]
"""
subclass_names = []
use_names = ['W', 'M', 'D', 'E', 'Char_Literal_Constant', 'V_List']
def match(string):
c = string[0].upper()
if c in ['I','B','O','Z','D']:
line = string[1:].lstrip()
if '.' in line:
i1,i2 = line.split('.',1)
i1 = i1.rstrip()
i2 = i2.lstrip()
return c, W(i1), M(i2), None
return c,W(line), None, None
if c in ['E','G']:
line = string[1:].lstrip()
if line.count('.')==1:
i1,i2 = line.split('.',1)
i1 = i1.rstrip()
i2 = i2.lstrip()
return c, W(i1), D(i2), None
elif line.count('.')==2:
i1,i2,i3 = line.split('.',2)
i1 = i1.rstrip()
i2 = i2.lstrip()
i3 = i3.lstrip()
return c, W(i1), D(i2), E(i3)
else:
return
if c=='L':
line = string[1:].lstrip()
if not line: return
return c, W(line), None, None
if c=='A':
line = string[1:].lstrip()
if not line:
return c, None, None, None
return c, W(line), None, None
c = string[:2].upper()
if len(c)!=2: return
if c in ['EN','ES']:
line = string[2:].lstrip()
if line.count('.')==1:
i1,i2 = line.split('.',1)
i1 = i1.rstrip()
i2 = i2.lstrip()
return c, W(i1), D(i2), None
elif line.count('.')==2:
i1,i2,i3 = line.split('.',2)
i1 = i1.rstrip()
i2 = i2.lstrip()
i3 = i3.lstrip()
return c, W(i1), D(i2), E(i3)
else:
return
if c=='DT':
line = string[2:].lstrip()
if not line:
return c, None, None, None
lst = None
if line.endswith(')'):
i = line.rfind('(')
if i==-1: return
l = line[i+1:-1].strip()
if not l: return
lst = V_List(l)
line = line[:i].rstrip()
if not line:
return c, None, lst, None
return c, Char_Literal_Constant(line), lst, None
return
match = staticmethod(match)
def tostr(self):
c = selt.items[0]
if c in ['I', 'B', 'O', 'Z', 'F', 'D', 'A', 'L']:
if self.items[2] is None:
return '%s%s' % (c, self.items[1])
return '%s%s.%s' % (c, self.items[1], self.items[2])
if c in ['E', 'EN', 'ES', 'G']:
if self.items[3] is None:
return '%s%s.%s' % (c, self.items[1], self.items[2])
return '%s%s.%sE%s' % (c, self.items[1], self.items[2], self.items[3])
if c=='DT':
if self.items[1] is None:
if self.items[2] is None:
return c
else:
return '%s(%s)' % (c, self.items[2])
else:
if self.items[2] is None:
return '%s%s' % (c, self.items[1])
else:
return '%s%s(%s)' % (c, self.items[1], self.items[2])
raise NotImpletenetedError,`c`
class W(Base): # R1006
"""
<w> = <int-literal-constant>
"""
subclass_names = ['Int_Literal_Constant']
class M(Base): # R1007
"""
<m> = <int-literal-constant>
"""
subclass_names = ['Int_Literal_Constant']
class D(Base): # R1008
"""
<d> = <int-literal-constant>
"""
subclass_names = ['Int_Literal_Constant']
class E(Base): # R1009
"""
<e> = <int-literal-constant>
"""
subclass_names = ['Int_Literal_Constant']
class V(Base): # R1010
"""
<v> = <signed-int-literal-constant>
"""
subclass_names = ['Signed_Int_Literal_Constant']
class Control_Edit_Desc(Base): # R1011
"""
<control-edit-desc> = <position-edit-desc>
| [ <r> ] /
| :
| <sign-edit-desc>
| <k> P
| <blank-interp-edit-desc>
| <round-edit-desc>
| <decimal-edit-desc>
"""
subclass_names = ['Position_Edit_Desc', 'Sign_Edit_Desc', 'Blank_Interp_Edit_Desc', 'Round_Edit_Desc',
'Decimal_Edit_Desc']
use_names = ['R', 'K']
class K(Base): # R1012
"""
<k> = <signed-int-literal-constant>
"""
subclass_names = ['Signed_Int_Literal_Constant']
class Position_Edit_Desc(Base): # R1013
"""
<position-edit-desc> = T <n>
| TL <n>
| TR <n>
| <n> X
"""
subclass_names = []
use_names = ['N']
class N(Base): # R1014
"""
<n> = <int-literal-constant>
"""
subclass_names = ['Int_Literal_Constant']
class Sign_Edit_Desc(STRINGBase): # R1015
"""
<sign-edit-desc> = SS
| SP
| S
"""
subclass_names = []
def match(string): return STRINGBase.match(['SS','SP','S'], string)
match = staticmethod(match)
class Blank_Interp_Edit_Desc(STRINGBase): # R1016
"""
<blank-interp-edit-desc> = BN
| BZ
"""
subclass_names = []
def match(string): return STRINGBase.match(['BN','BZ',], string)
match = staticmethod(match)
class Round_Edit_Desc(STRINGBase): # R1017
"""
<round-edit-desc> = RU
| RD
| RZ
| RN
| RC
| RP
"""
subclass_names = []
def match(string): return STRINGBase.match(['RU','RD','RZ','RN','RC','RP'], string)
match = staticmethod(match)
class Decimal_Edit_Desc(STRINGBase): # R1018
"""
<decimal-edit-desc> = DC
| DP
"""
subclass_names = []
def match(string): return STRINGBase.match(['DC','DP'], string)
match = staticmethod(match)
class Char_String_Edit_Desc(Base): # R1019
"""
<char-string-edit-desc> = <char-literal-constant>
"""
subclass_names = ['Char_Literal_Constant']
###############################################################################
############################### SECTION 11 ####################################
###############################################################################
class Main_Program(Base): # R1101
"""
<main-program> = [ <program-stmt> ]
[ <specification-part> ]
[ <execution-part> ]
[ <internal-subprogram-part> ]
<end-program-stmt>
"""
subclass_names = []
use_names = ['Program_Stmt', 'Specification_Part', 'Execution_Part', 'Internal_Subprogram_Part',
'End_Program_Stmt']
class Program_Stmt(StmtBase, WORDClsBase): # R1102
"""
<program-stmt> = PROGRAM <program-name>
"""
subclass_names = []
use_names = ['Program_Name']
def match(string): return WORDClsBase.match('PROGRAM',Program_Name, string, require_cls = True)
match = staticmethod(match)
class End_Program_Stmt(EndStmtBase): # R1103
"""
<end-program-stmt> = END [ PROGRAM [ <program-name> ] ]
"""
subclass_names = []
use_names = ['Program_Name']
def match(string): return EndStmtBase.match('PROGRAM',Program_Name, string)
match = staticmethod(match)
class Module(Base): # R1104
"""
<module> = <module-stmt>
[ <specification-part> ]
[ <module-subprogram-part> ]
<end-module-stmt>
"""
subclass_names = []
use_names = ['Module_Stmt', 'Specification_Part', 'Module_Subprogram_Part', 'End_Module_Stmt']
class Module_Stmt(StmtBase, WORDClsBase): # R1105
"""
<module-stmt> = MODULE <module-name>
"""
subclass_names = []
use_names = ['Module_Name']
def match(string): return WORDClsBase.match('MODULE',Module_Name, string, require_cls = True)
match = staticmethod(match)
class End_Module_Stmt(EndStmtBase): # R1106
"""
<end-module-stmt> = END [ MODULE [ <module-name> ] ]
"""
subclass_names = []
use_names = ['Module_Name']
def match(string): return EndStmtBase.match('MODULE',Module_Name, string, require_stmt_type=True)
match = staticmethod(match)
class Module_Subprogram_Part(Base): # R1107
"""
<module-subprogram-part> = <contains-stmt>
<module-subprogram>
[ <module-subprogram> ]...
"""
subclass_names = []
use_names = ['Contains_Stmt', 'Module_Subprogram']
class Module_Subprogram(Base): # R1108
"""
<module-subprogram> = <function-subprogram>
| <subroutine-subprogram>
"""
subclass_names = ['Function_Subprogram', 'Subroutine_Subprogram']
class Use_Stmt(StmtBase): # R1109
"""
<use-stmt> = USE [ [ , <module-nature> ] :: ] <module-name> [ , <rename-list> ]
| USE [ [ , <module-nature> ] :: ] <module-name> , ONLY: [ <only-list> ]
"""
subclass_names = []
use_names = ['Module_Nature', 'Module_Name', 'Rename_List', 'Only_List']
def match(string):
if string[:3].upper() != 'USE': return
line = string[3:]
if not line: return
if isalnum(line[0]): return
line = line.lstrip()
i = line.find('::')
nature = None
if i!=-1:
if line.startswith(','):
l = line[1:i].strip()
if not l: return
nature = Module_Nature(l)
line = line[i+2:].lstrip()
if not line: return
i = line.find(',')
if i==-1: return nature, Module_Name(line), '', None
name = line[:i].rstrip()
if not name: return
name = Module_Name(name)
line = line[i+1:].lstrip()
if not line: return
if line[:5].upper()=='ONLY:':
line = line[5:].lstrip()
if not line:
return nature, name, ', ONLY:', None
return nature, name, ', ONLY:', Only_List(line)
return nature, name, ',', Rename_List(line)
match = staticmethod(match)
def tostr(self):
s = 'USE'
if self.items[0] is not None:
s += ', %s' % (self.items[0])
s += ' :: %s%s' % (self.items[1], self.items[2])
if self.items[3] is not None:
s += ' %s' % (self.items[3])
return s
class Module_Nature(STRINGBase): # R1110
"""
<module-nature> = INTRINSIC
| NON_INTRINSIC
"""
subclass_names = []
def match(string): return STRINGBase.match(['INTRINSIC','NON_INTRINSIC'], string)
match = staticmethod(match)
class Rename(Base): # R1111
"""
<rename> = <local-name> => <use-name>
| OPERATOR(<local-defined-operator>) => OPERATOR(<use-defined-operator>)
"""
subclass_names = []
use_names = ['Local_Name', 'Use_Name', 'Local_Defined_Operator', 'Use_Defined_Operator']
def match(string):
s = string.split('=>', 1)
if len(s) != 2: return
lhs, rhs = s[0].rstrip(), s[1].lstrip()
if not lhs or not rhs: return
if lhs[:8].upper()=='OPERATOR' and rhs[:8].upper()=='OPERATOR':
l = lhs[8:].lstrip()
r = rhs[8:].lstrip()
if l and r and l[0]+l[-1]=='()':
if r[0]+r[-1] != '()': return
l = l[1:-1].strip()
r = r[1:-1].strip()
if not l or not r: return
return 'OPERATOR', Local_Defined_Operator(l), Use_Defined_Operator(r)
return None, Local_Name(lhs), Use_Name(rhs)
match = staticmethod(match)
def tostr(self):
if not self.items[0]:
return '%s => %s' % self.items[1:]
return '%s(%s) => %s(%s)' % (self.items[0], self.items[1],self.items[0], self.items[2])
class Only(Base): # R1112
"""
<only> = <generic-spec>
| <only-use-name>
| <rename>
"""
subclass_names = ['Generic_Spec', 'Only_Use_Name', 'Rename']
class Only_Use_Name(Base): # R1113
"""
<only-use-name> = <name>
"""
subclass_names = ['Name']
class Local_Defined_Operator(Base): # R1114
"""
<local-defined-operator> = <defined-unary-op>
| <defined-binary-op>
"""
subclass_names = ['Defined_Unary_Op', 'Defined_Binary_Op']
class Use_Defined_Operator(Base): # R1115
"""
<use-defined-operator> = <defined-unary-op>
| <defined-binary-op>
"""
subclass_names = ['Defined_Unary_Op', 'Defined_Binary_Op']
class Block_Data(Base): # R1116
"""
<block-data> = <block-data-stmt>
[ <specification-part> ]
<end-block-data-stmt>
"""
subclass_names = []
use_names = ['Block_Data_Stmt', 'Specification_Part', 'End_Block_Data_Stmt']
class Block_Data_Stmt(StmtBase): # R1117
"""
<block-data-stmt> = BLOCK DATA [ <block-data-name> ]
"""
subclass_names = []
use_names = ['Block_Data_Name']
def match(string):
if string[:5].upper()!='BLOCK': return
line = string[5:].lstrip()
if line[:4].upper()!='DATA': return
line = line[4:].lstrip()
if not line: return None,
return Block_Data_Name(line),
match = staticmethod(match)
def tostr(self):
if self.items[0] is None: return 'BLOCK DATA'
return 'BLOCK DATA %s' % self.items
class End_Block_Data_Stmt(EndStmtBase): # R1118
"""
<end-block-data-stmt> = END [ BLOCK DATA [ <block-data-name> ] ]
"""
subclass_names = []
use_names = ['Block_Data_Name']
def match(string): return EndStmtBase.match('BLOCK DATA',Block_Data_Name, string)
match = staticmethod(match)
###############################################################################
############################### SECTION 12 ####################################
###############################################################################
class Interface_Block(Base): # R1201
"""
<interface-block> = <interface-stmt>
[ <interface-specification> ]...
<end-interface-stmt>
"""
subclass_names = []
use_names = ['Interface_Stmt', 'Interface_Specification', 'End_Interface_Stmt']
class Interface_Specification(Base): # R1202
"""
<interface-specification> = <interface-body>
| <procedure-stmt>
"""
subclass_names = ['Interface_Body', 'Procedure_Stmt']
class Interface_Stmt(StmtBase): # R1203
"""
<interface-stmt> = INTERFACE [ <generic-spec> ]
| ABSTRACT INTERFACE
"""
subclass_names = []
use_names = ['Generic_Spec']
class End_Interface_Stmt(EndStmtBase): # R1204
"""
<end-interface-stmt> = END INTERFACE [ <generic-spec> ]
"""
subclass_names = []
use_names = ['Generic_Spec']
def match(string): return EndStmtBase.match('INTERFACE',Generic_Spec, string, require_stmt_type=True)
match = staticmethod(match)
class Interface_Body(Base): # R1205
"""
<interface-body> = <function-stmt>
[ <specification-part> ]
<end-function-stmt>
| <subroutine-stmt>
[ <specification-part> ]
<end-subroutine-stmt>
"""
subclass_names = []
use_names = ['Function_Stmt', 'Specification_Part', 'Subroutine_Stmt', 'End_Function_Stmt', 'End_Subroutine_Stmt']
class Procedure_Stmt(StmtBase): # R1206
"""
<procedure-stmt> = [ MODULE ] PROCEDURE <procedure-name-list>
"""
subclass_names = []
use_names = ['Procedure_Name_List']
class Generic_Spec(Base): # R1207
"""
<generic-spec> = <generic-name>
| OPERATOR ( <defined-operator> )
| ASSIGNMENT ( = )
| <dtio-generic-spec>
"""
subclass_names = ['Generic_Name', 'Dtio_Generic_Spec']
use_names = ['Defined_Operator']
class Dtio_Generic_Spec(Base): # R1208
"""
<dtio-generic-spec> = READ ( FORMATTED )
| READ ( UNFORMATTED )
| WRITE ( FORMATTED )
| WRITE ( UNFORMATTED )
"""
subclass_names = []
class Import_Stmt(StmtBase, WORDClsBase): # R1209
"""
<import-stmt> = IMPORT [ :: ] <import-name-list>
"""
subclass_names = []
use_names = ['Import_Name_List']
def match(string): return WORDClsBase.match('IMPORT',Import_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class External_Stmt(StmtBase, WORDClsBase): # R1210
"""
<external-stmt> = EXTERNAL [ :: ] <external-name-list>
"""
subclass_names = []
use_names = ['External_Name_List']
def match(string): return WORDClsBase.match('EXTERNAL',External_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Procedure_Declaration_Stmt(StmtBase): # R1211
"""
<procedure-declaration-stmt> = PROCEDURE ( [ <proc-interface> ] ) [ [ , <proc-attr-spec> ]... :: ] <proc-decl-list>
"""
subclass_names = []
use_names = ['Proc_Interface', 'Proc_Attr_Spec', 'Proc_Decl_List']
class Proc_Interface(Base): # R1212
"""
<proc-interface> = <interface-name>
| <declaration-type-spec>
"""
subclass_names = ['Interface_Name', 'Declaration_Type_Spec']
class Proc_Attr_Spec(Base): # R1213
"""
<proc-attr-spec> = <access-spec>
| <proc-language-binding-spec>
| INTENT ( <intent-spec> )
| OPTIONAL
| SAVE
"""
subclass_names = ['Access_Spec', 'Proc_Language_Binding_Spec']
use_names = ['Intent_Spec']
class Proc_Decl(BinaryOpBase): # R1214
"""
<proc-decl> = <procedure-entity-name> [ => <null-init> ]
"""
subclass_names = ['Procedure_Entity_Name']
use_names = ['Null_Init']
def match(string): return BinaryOpBase.match(Procedure_Entity_Name,'=>', Null_Init, string)
match = staticmethod(match)
class Interface_Name(Base): # R1215
"""
<interface-name> = <name>
"""
subclass_names = ['Name']
class Intrinsic_Stmt(StmtBase, WORDClsBase): # R1216
"""
<intrinsic-stmt> = INTRINSIC [ :: ] <intrinsic-procedure-name-list>
"""
subclass_names = []
use_names = ['Intrinsic_Procedure_Name_List']
def match(string): return WORDClsBase.match('INTRINSIC',Intrinsic_Procedure_Name_List,string,check_colons=True, require_cls=True)
match = staticmethod(match)
tostr = WORDClsBase.tostr_a
class Function_Reference(CallBase): # R1217
"""
<function-reference> = <procedure-designator> ( [ <actual-arg-spec-list> ] )
"""
subclass_names = []
use_names = ['Procedure_Designator','Actual_Arg_Spec_List']
def match(string):
return CallBase.match(Procedure_Designator, Actual_Arg_Spec_List, string)
match = staticmethod(match)
class Call_Stmt(StmtBase): # R1218
"""
<call-stmt> = CALL <procedure-designator> [ ( [ <actual-arg-spec-list> ] ) ]
"""
subclass_names = []
use_names = ['Procedure_Designator', 'Actual_Arg_Spec_List']
def match(string):
if string[:4].upper()!='CALL': return
line, repmap = string_replace_map(string[4:].lstrip())
if line.endswith(')'):
i = line.rfind('(')
if i==-1: return
args = repmap(line[i+1:-1].strip())
if args:
return Procedure_Designator(repmap(line[:i].rstrip())),Actual_Arg_Spec_List(args)
return Procedure_Designator(repmap(line[:i].rstrip())),None
return Procedure_Designator(string[4:].lstrip()),None
match = staticmethod(match)
def tostr(self):
if self.items[1] is None: return 'CALL %s' % (self.items[0])
return 'CALL %s(%s)' % self.items
class Procedure_Designator(BinaryOpBase): # R1219
"""
<procedure-designator> = <procedure-name>
| <proc-component-ref>
| <data-ref> % <binding-name>
"""
subclass_names = ['Procedure_Name','Proc_Component_Ref']
use_names = ['Data_Ref','Binding_Name']
def match(string):
return BinaryOpBase.match(\
Data_Ref, pattern.percent_op.named(), Binding_Name, string)
match = staticmethod(match)
class Actual_Arg_Spec(KeywordValueBase): # R1220
"""
<actual-arg-spec> = [ <keyword> = ] <actual-arg>
"""
subclass_names = ['Actual_Arg']
use_names = ['Keyword']
def match(string): return KeywordValueBase.match(Keyword, Actual_Arg, string)
match = staticmethod(match)
class Actual_Arg(Base): # R1221
"""
<actual-arg> = <expr>
| <variable>
| <procedure-name>
| <proc-component-ref>
| <alt-return-spec>
"""
subclass_names = ['Procedure_Name','Proc_Component_Ref','Alt_Return_Spec', 'Variable', 'Expr']
class Alt_Return_Spec(Base): # R1222
"""
<alt-return-spec> = * <label>
"""
subclass_names = []
use_names = ['Label']
def match(string):
if not string.startswith('*'): return
line = string[1:].lstrip()
if not line: return
return Label(line),
match = staticmethod(match)
def tostr(self): return '*%s' % (self.items[0])
class Function_Subprogram(BlockBase): # R1223
"""
<function-subprogram> = <function-stmt>
[ <specification-part> ]
[ <execution-part> ]
[ <internal-subprogram-part> ]
<end-function-stmt>
"""
subclass_names = []
use_names = ['Function_Stmt', 'Specification_Part', 'Execution_Part',
'Internal_Subprogram_Part', 'End_Function_Stmt']
def match(reader):
return BlockBase.match(Function_Stmt, [Specification_Part, Execution_Part, Internal_Subprogram_Part], End_Function_Stmt, reader)
match = staticmethod(match)
class Function_Stmt(StmtBase): # R1224
"""
<function-stmt> = [ <prefix> ] FUNCTION <function-name> ( [ <dummy-arg-name-list> ] ) [ <suffix> ]
"""
subclass_names = []
use_names = ['Prefix','Function_Name','Dummy_Arg_Name_List', 'Suffix']
class Proc_Language_Binding_Spec(Base): #1225
"""
<proc-language-binding-spec> = <language-binding-spec>
"""
subclass_names = ['Language_Binding_Spec']
class Dummy_Arg_Name(Base): # R1226
"""
<dummy-arg-name> = <name>
"""
subclass_names = ['Name']
class Prefix(SequenceBase): # R1227
"""
<prefix> = <prefix-spec> [ <prefix-spec> ]..
"""
subclass_names = ['Prefix_Spec']
_separator = (' ',re.compile(r'\s+(?=[a-z_])',re.I))
def match(string): return SequenceBase.match(Prefix._separator, Prefix_Spec, string)
match = staticmethod(match)
class Prefix_Spec(STRINGBase): # R1228
"""
<prefix-spec> = <declaration-type-spec>
| RECURSIVE
| PURE
| ELEMENTAL
"""
subclass_names = ['Declaration_Type_Spec']
def match(string):
return STRINGBase.match(['RECURSIVE', 'PURE', 'ELEMENTAL'], string)
match = staticmethod(match)
class Suffix(Base): # R1229
"""
<suffix> = <proc-language-binding-spec> [ RESULT ( <result-name> ) ]
| RESULT ( <result-name> ) [ <proc-language-binding-spec> ]
"""
subclass_names = ['Proc_Language_Binding_Spec']
use_names = ['Result_Name']
def match(string):
if string[:6].upper()=='RESULT':
line = string[6:].lstrip()
if not line.startswith('('): return
i = line.find(')')
if i==-1: return
name = line[1:i].strip()
if not name: return
line = line[i+1:].lstrip()
if line: return Result_Name(name), Proc_Language_Binding_Spec(line)
return Result_Name(name), None
if not string.endswith(')'): return
i = string.rfind('(')
if i==-1: return
name = string[i+1:-1].strip()
if not name: return
line = string[:i].rstrip()
if line[-6:].upper()!='RESULT': return
line = line[:-6].rstrip()
if not line: return
return Result_Name(name), Proc_Language_Binding_Spec(line)
match = staticmethod(match)
def tostr(self):
if self.items[1] is None:
return 'RESULT(%s)' % (self.items[0])
return 'RESULT(%s) %s' % self.items
class End_Function_Stmt(EndStmtBase): # R1230
"""
<end-function-stmt> = END [ FUNCTION [ <function-name> ] ]
"""
subclass_names = []
use_names = ['Function_Name']
def match(string): return EndStmtBase.match('FUNCTION',Function_Name, string)
match = staticmethod(match)
class Subroutine_Subprogram(BlockBase): # R1231
"""
<subroutine-subprogram> = <subroutine-stmt>
[ <specification-part> ]
[ <execution-part> ]
[ <internal-subprogram-part> ]
<end-subroutine-stmt>
"""
subclass_names = []
use_names = ['Subroutine_Stmt', 'Specification_Part', 'Execution_Part',
'Internal_Subprogram_Part', 'End_Subroutine_Stmt']
def match(reader):
return BlockBase.match(Subroutine_Stmt, [Specification_Part, Execution_Part, Internal_Subprogram_Part], End_Subroutine_Stmt, reader)
match = staticmethod(match)
class Subroutine_Stmt(StmtBase): # R1232
"""
<subroutine-stmt> = [ <prefix> ] SUBROUTINE <subroutine-name> [ ( [ <dummy-arg-list> ] ) [ <proc-language-binding-spec> ] ]
"""
subclass_names = []
use_names = ['Prefix', 'Subroutine_Name', 'Dummy_Arg_List', 'Proc_Language_Binding_Spec']
def match(string):
line, repmap = string_replace_map(string)
m = pattern.subroutine.search(line)
if m is None: return
prefix = line[:m.start()].rstrip() or None
if prefix is not None:
prefix = Prefix(repmap(prefix))
line = line[m.end():].lstrip()
m = pattern.name.match(line)
if m is None: return
name = Subroutine_Name(m.group())
line = line[m.end():].lstrip()
dummy_args = None
if line.startswith('('):
i = line.find(')')
if i==-1: return
dummy_args = line[1:i].strip() or None
if dummy_args is not None:
dummy_args = Dummy_Arg_List(repmap(dummy_args))
line = line[i+1:].lstrip()
binding_spec = None
if line:
binding_spec = Proc_Language_Binding_Spec(repmap(line))
return prefix, name, dummy_args, binding_spec
match = staticmethod(match)
def get_name(self): return self.items[1]
def tostr(self):
if self.items[0] is not None:
s = '%s SUBROUTINE %s' % (self.items[0], self.items[1])
else:
s = 'SUBROUTINE %s' % (self.items[1])
if self.items[2] is not None:
s += '(%s)' % (self.items[2])
if self.items[3] is not None:
s += ' %s' % (self.items[3])
return s
class Dummy_Arg(StringBase): # R1233
"""
<dummy-arg> = <dummy-arg-name>
| *
"""
subclass_names = ['Dummy_Arg_Name']
def match(string): return StringBase.match('*', string)
match = staticmethod(match)
class End_Subroutine_Stmt(EndStmtBase): # R1234
"""
<end-subroutine-stmt> = END [ SUBROUTINE [ <subroutine-name> ] ]
"""
subclass_names = []
use_names = ['Subroutine_Name']
def match(string): return EndStmtBase.match('SUBROUTINE', Subroutine_Name, string)
match = staticmethod(match)
class Entry_Stmt(StmtBase): # R1235
"""
<entry-stmt> = ENTRY <entry-name> [ ( [ <dummy-arg-list> ] ) [ <suffix> ] ]
"""
subclass_names = []
use_names = ['Entry_Name', 'Dummy_Arg_List', 'Suffix']
class Return_Stmt(StmtBase): # R1236
"""
<return-stmt> = RETURN [ <scalar-int-expr> ]
"""
subclass_names = []
use_names = ['Scalar_Int_Expr']
def match(string):
start = string[:6].upper()
if start!='RETURN': return
if len(string)==6: return None,
return Scalar_Int_Expr(string[6:].lstrip()),
match = staticmethod(match)
def tostr(self):
if self.items[0] is None: return 'RETURN'
return 'RETURN %s' % self.items
class Contains_Stmt(StmtBase, STRINGBase): # R1237
"""
<contains-stmt> = CONTAINS
"""
subclass_names = []
def match(string): return STRINGBase.match('CONTAINS',string)
match = staticmethod(match)
class Stmt_Function_Stmt(StmtBase): # R1238
"""
<stmt-function-stmt> = <function-name> ( [ <dummy-arg-name-list> ] ) = Scalar_Expr
"""
subclass_names = []
use_names = ['Function_Name', 'Dummy_Arg_Name_List', 'Scalar_Expr']
def match(string):
i = string.find('=')
if i==-1: return
expr = string[i+1:].lstrip()
if not expr: return
line = string[:i].rstrip()
if not line or not line.endswith(')'): return
i = line.find('(')
if i==-1: return
name = line[:i].rstrip()
if not name: return
args = line[i+1:-1].strip()
if args:
return Function_Name(name), Dummy_Arg_Name_List(args), Scalar_Expr(expr)
return Function_Name(name), None, Scalar_Expr(expr)
match = staticmethod(match)
def tostr(self):
if self.items[1] is None:
return '%s () = %s' % (self.items[0], self.items[2])
return '%s (%s) = %s' % self.items
###############################################################################
################ GENERATE Scalar_, _List, _Name CLASSES #######################
###############################################################################
ClassType = type(Base)
_names = dir()
for clsname in _names:
cls = eval(clsname)
if not (isinstance(cls, ClassType) and issubclass(cls, Base) and not cls.__name__.endswith('Base')): continue
names = getattr(cls, 'subclass_names', []) + getattr(cls, 'use_names', [])
for n in names:
if n in _names: continue
if n.endswith('_List'):
_names.append(n)
n = n[:-5]
#print 'Generating %s_List' % (n)
exec '''\
class %s_List(SequenceBase):
subclass_names = [\'%s\']
use_names = []
def match(string): return SequenceBase.match(r\',\', %s, string)
match = staticmethod(match)
''' % (n, n, n)
elif n.endswith('_Name'):
_names.append(n)
n = n[:-5]
#print 'Generating %s_Name' % (n)
exec '''\
class %s_Name(Base):
subclass_names = [\'Name\']
''' % (n)
elif n.startswith('Scalar_'):
_names.append(n)
n = n[7:]
#print 'Generating Scalar_%s' % (n)
exec '''\
class Scalar_%s(Base):
subclass_names = [\'%s\']
''' % (n,n)
Base_classes = {}
for clsname in dir():
cls = eval(clsname)
if isinstance(cls, ClassType) and issubclass(cls, Base) and not cls.__name__.endswith('Base'):
Base_classes[cls.__name__] = cls
###############################################################################
##################### OPTIMIZE subclass_names tree ############################
###############################################################################
if 1: # Optimize subclass tree:
def _rpl_list(clsname):
if clsname not in Base_classes:
print 'Not implemented:',clsname
return [] # remove this code when all classes are implemented
cls = Base_classes[clsname]
if 'match' in cls.__dict__:
return [clsname]
l = []
for n in getattr(cls,'subclass_names',[]):
l1 = _rpl_list(n)
for n1 in l1:
if n1 not in l:
l.append(n1)
return l
for cls in Base_classes.values():
if not hasattr(cls, 'subclass_names'): continue
opt_subclass_names = []
for n in cls.subclass_names:
for n1 in _rpl_list(n):
if n1 not in opt_subclass_names: opt_subclass_names.append(n1)
if not opt_subclass_names==cls.subclass_names:
#print cls.__name__,':',', '.join(cls.subclass_names),'->',', '.join(opt_subclass_names)
cls.subclass_names[:] = opt_subclass_names
#else:
# print cls.__name__,':',opt_subclass_names
# Initialize Base.subclasses dictionary:
for clsname, cls in Base_classes.items():
subclass_names = getattr(cls, 'subclass_names', None)
if subclass_names is None:
print '%s class is missing subclass_names list' % (clsname)
continue
try:
l = Base.subclasses[clsname]
except KeyError:
Base.subclasses[clsname] = l = []
for n in subclass_names:
if n in Base_classes:
l.append(Base_classes[n])
else:
print '%s not implemented needed by %s' % (n,clsname)
if 1:
for cls in Base_classes.values():
subclasses = Base.subclasses.get(cls.__name__,[])
subclasses_names = [c.__name__ for c in subclasses]
subclass_names = getattr(cls,'subclass_names', [])
use_names = getattr(cls,'use_names',[])
for n in subclasses_names:
break
if n not in subclass_names:
print '%s needs to be added to %s subclasses_name list' % (n,cls.__name__)
for n in subclass_names:
break
if n not in subclasses_names:
print '%s needs to be added to %s subclass_name list' % (n,cls.__name__)
for n in use_names + subclass_names:
if n not in Base_classes:
print '%s not defined used by %s' % (n, cls.__name__)
#EOF
|