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 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1993-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
// Parser for Octave.
// C decarations.
%{
#define YYDEBUG 1
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include "Matrix.h"
#include "cmd-edit.h"
#include "cmd-hist.h"
#include "file-ops.h"
#include "file-stat.h"
#include "oct-env.h"
#include "oct-time.h"
#include "quit.h"
#include "Cell.h"
#include "anon-fcn-validator.h"
#include "builtin-defun-decls.h"
#include "defun.h"
#include "dynamic-ld.h"
#include "error.h"
#include "input.h"
#include "interpreter-private.h"
#include "interpreter.h"
#include "lex.h"
#include "load-path.h"
#include "lo-sysdep.h"
#include "oct-hist.h"
#include "oct-map.h"
#include "ov-classdef.h"
#include "ov-fcn-handle.h"
#include "ov-usr-fcn.h"
#include "ov-null-mat.h"
#include "pager.h"
#include "parse.h"
#include "pt-all.h"
#include "pt-eval.h"
#include "symtab.h"
#include "token.h"
#include "unwind-prot.h"
#include "utils.h"
#include "variables.h"
// oct-parse.h must be included after pt-all.h
#include "oct-parse.h"
extern int octave_lex (YYSTYPE *, void *);
// Forward declarations for some functions defined at the bottom of
// the file.
static void yyerror (octave::base_parser& parser, const char *s);
#define lexer (parser.get_lexer ())
#define scanner lexer.m_scanner
// Previous versions of Octave used Bison's YYUSE macro to avoid
// warnings about unused values in rules. But that Bison macro was
// apparently never intended to be public. So define our own. All we
// need to do is mention the symantic value somewhere in the rule. It
// doesn't actually need to be used to avoid the Bison warning, so just
// define this macro to discard its parameter.
#define OCTAVE_YYUSE(X)
#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
// Disable this warning for code that is generated by Bison,
// including grammar rules. Push the current state so we can
// restore the warning state prior to functions we define at
// the bottom of the file.
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
%}
// Bison declarations.
// The grammar currently has 9 shift/reduce conflicts. Ensure that
// we notice if that number changes.
%expect 9
// We are using the pure parser interface and the reentrant lexer
// interface but the Octave parser and lexer are NOT properly
// reentrant because both still use many global variables. It should be
// safe to create a parser object and call it while another parser
// object is active (to parse a callback function while the main
// interactive parser is waiting for input, for example) if you take
// care to properly save and restore (typically with an unwind_protect
// object) relevant global values before and after the nested call.
%define api.pure
// No spaces inside the braces for the prefix and push-pull definitions!
%define api.prefix {octave_}
%define api.push-pull both
%parse-param { octave::base_parser& parser }
%lex-param { void *lexer.scanner }
%union
{
int dummy_type;
// The type of the basic tokens returned by the lexer.
octave::token *tok_val;
// Comment strings that we need to deal with mid-rule.
octave::comment_list *comment_type;
// Types for the nonterminals we generate.
char punct_type;
octave::tree *tree_type;
octave::tree_matrix *tree_matrix_type;
octave::tree_cell *tree_cell_type;
octave::tree_expression *tree_expression_type;
octave::tree_constant *tree_constant_type;
octave::tree_fcn_handle *tree_fcn_handle_type;
octave::tree_superclass_ref *tree_superclass_ref_type;
octave::tree_metaclass_query *tree_metaclass_query_type;
octave::tree_function_def *tree_function_def_type;
octave::tree_anon_fcn_handle *tree_anon_fcn_handle_type;
octave::tree_identifier *tree_identifier_type;
octave::tree_index_expression *tree_index_expression_type;
octave::tree_colon_expression *tree_colon_expression_type;
octave::tree_argument_list *tree_argument_list_type;
octave::tree_parameter_list *tree_parameter_list_type;
octave::tree_command *tree_command_type;
octave::tree_if_command *tree_if_command_type;
octave::tree_if_clause *tree_if_clause_type;
octave::tree_if_command_list *tree_if_command_list_type;
octave::tree_switch_command *tree_switch_command_type;
octave::tree_switch_case *tree_switch_case_type;
octave::tree_switch_case_list *tree_switch_case_list_type;
octave::tree_decl_elt *tree_decl_elt_type;
octave::tree_decl_init_list *tree_decl_init_list_type;
octave::tree_decl_command *tree_decl_command_type;
octave::tree_statement *tree_statement_type;
octave::tree_statement_list *tree_statement_list_type;
octave_user_function *octave_user_function_type;
octave::tree_classdef *tree_classdef_type;
octave::tree_classdef_attribute* tree_classdef_attribute_type;
octave::tree_classdef_attribute_list* tree_classdef_attribute_list_type;
octave::tree_classdef_superclass* tree_classdef_superclass_type;
octave::tree_classdef_superclass_list* tree_classdef_superclass_list_type;
octave::tree_classdef_body* tree_classdef_body_type;
octave::tree_classdef_property* tree_classdef_property_type;
octave::tree_classdef_property_list* tree_classdef_property_list_type;
octave::tree_classdef_properties_block* tree_classdef_properties_block_type;
octave::tree_classdef_methods_list* tree_classdef_methods_list_type;
octave::tree_classdef_methods_block* tree_classdef_methods_block_type;
octave::tree_classdef_event* tree_classdef_event_type;
octave::tree_classdef_events_list* tree_classdef_events_list_type;
octave::tree_classdef_events_block* tree_classdef_events_block_type;
octave::tree_classdef_enum* tree_classdef_enum_type;
octave::tree_classdef_enum_list* tree_classdef_enum_list_type;
octave::tree_classdef_enum_block* tree_classdef_enum_block_type;
}
// Tokens with line and column information.
%token <tok_val> '=' ':' '-' '+' '*' '/'
%token <tok_val> '(' ')' '[' ']' '{' '}' '.' '@'
%token <tok_val> ',' ';' '\n'
%token <tok_val> ADD_EQ SUB_EQ MUL_EQ DIV_EQ LEFTDIV_EQ POW_EQ
%token <tok_val> EMUL_EQ EDIV_EQ ELEFTDIV_EQ EPOW_EQ AND_EQ OR_EQ
%token <tok_val> EXPR_AND_AND EXPR_OR_OR
%token <tok_val> EXPR_AND EXPR_OR EXPR_NOT
%token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT
%token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS
%token <tok_val> HERMITIAN TRANSPOSE
%token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW
%token <tok_val> NUM IMAG_NUM
%token <tok_val> STRUCT_ELT
%token <tok_val> NAME
%token <tok_val> END
%token <tok_val> DQ_STRING SQ_STRING
%token <tok_val> FOR PARFOR WHILE DO UNTIL
%token <tok_val> IF ELSEIF ELSE
%token <tok_val> SWITCH CASE OTHERWISE
%token <tok_val> BREAK CONTINUE FUNC_RET
%token <tok_val> UNWIND CLEANUP
%token <tok_val> TRY CATCH
%token <tok_val> GLOBAL PERSISTENT
%token <tok_val> FCN_HANDLE
%token <tok_val> CLASSDEF
%token <tok_val> PROPERTIES METHODS EVENTS ENUMERATION
%token <tok_val> METAQUERY
%token <tok_val> SUPERCLASSREF
%token <tok_val> FQ_IDENT
%token <tok_val> GET SET
%token <tok_val> FCN
%token <tok_val> LEXICAL_ERROR
%token <tok_val> END_OF_INPUT
// Other tokens.
%token <dummy_type> INPUT_FILE
// %token VARARGIN VARARGOUT
// Nonterminals we construct.
%type <dummy_type> indirect_ref_op decl_param_init
%type <dummy_type> push_fcn_symtab push_script_symtab begin_file
%type <dummy_type> param_list_beg param_list_end stmt_begin anon_fcn_begin
%type <dummy_type> parsing_local_fcns parse_error
%type <comment_type> stash_comment
%type <tok_val> function_beg classdef_beg
%type <punct_type> sep_no_nl opt_sep_no_nl nl opt_nl sep opt_sep
%type <tree_type> input
%type <tree_constant_type> string constant magic_colon
%type <tree_anon_fcn_handle_type> anon_fcn_handle
%type <tree_fcn_handle_type> fcn_handle
%type <tree_matrix_type> matrix_rows
%type <tree_cell_type> cell_rows
%type <tree_expression_type> matrix cell
%type <tree_expression_type> primary_expr oper_expr power_expr
%type <tree_expression_type> simple_expr colon_expr assign_expr expression
%type <tree_identifier_type> identifier fcn_name magic_tilde
%type <tree_superclass_ref_type> superclass_identifier
%type <tree_metaclass_query_type> meta_identifier
%type <tree_index_expression_type> word_list_cmd
%type <tree_argument_list_type> arg_list word_list assign_lhs
%type <tree_argument_list_type> cell_or_matrix_row
%type <tree_parameter_list_type> opt_param_list param_list
%type <tree_parameter_list_type> param_list1 param_list2
%type <tree_parameter_list_type> return_list return_list1
%type <tree_command_type> command select_command loop_command
%type <tree_command_type> jump_command except_command
%type <tree_function_def_type> function
%type <tree_classdef_type> classdef
%type <tree_command_type> file
%type <tree_if_command_type> if_command
%type <tree_if_clause_type> elseif_clause else_clause
%type <tree_if_command_list_type> if_cmd_list1 if_cmd_list
%type <tree_switch_command_type> switch_command
%type <tree_switch_case_type> switch_case default_case
%type <tree_switch_case_list_type> case_list1 case_list
%type <tree_decl_elt_type> decl2 param_list_elt
%type <tree_decl_init_list_type> decl1
%type <tree_decl_command_type> declaration
%type <tree_statement_type> statement function_end
%type <tree_statement_list_type> simple_list simple_list1 list list1
%type <tree_statement_list_type> opt_list
%type <tree_statement_list_type> opt_fcn_list fcn_list fcn_list1
%type <tree_classdef_attribute_type> attr
%type <tree_classdef_attribute_list_type> attr_list attr_list1
%type <tree_classdef_superclass_type> superclass
%type <tree_classdef_superclass_list_type> superclass_list superclass_list1
%type <tree_classdef_body_type> class_body class_body1
%type <tree_classdef_property_type> class_property
%type <tree_classdef_property_list_type> property_list property_list1
%type <tree_classdef_properties_block_type> properties_block
%type <tree_classdef_methods_list_type> methods_list methods_list1
%type <tree_classdef_methods_block_type> methods_block
%type <tree_classdef_event_type> class_event
%type <tree_classdef_events_list_type> events_list events_list1
%type <tree_classdef_events_block_type> events_block
%type <tree_classdef_enum_type> class_enum
%type <tree_classdef_enum_list_type> enum_list enum_list1
%type <tree_classdef_enum_block_type> enum_block
%type <tree_function_def_type> method_decl method
%type <octave_user_function_type> method_decl1
// Precedence and associativity.
%right '=' ADD_EQ SUB_EQ MUL_EQ DIV_EQ LEFTDIV_EQ POW_EQ EMUL_EQ EDIV_EQ ELEFTDIV_EQ EPOW_EQ OR_EQ AND_EQ
%left EXPR_OR_OR
%left EXPR_AND_AND
%left EXPR_OR
%left EXPR_AND
%left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT
%left ':'
%left '-' '+' EPLUS EMINUS
%left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV
%right UNARY EXPR_NOT
%left POW EPOW HERMITIAN TRANSPOSE
%right PLUS_PLUS MINUS_MINUS
%left '(' '.' '{'
// How to clean up if there is a parse error. We handle deleting tokens
// and comments separately and separators are just characters. The
// remaining items are dynamically allocated parse tree objects that
// must be deleted. Use the wildcard case (<*>) to detect unhandled
// cases (for example, a new semantic type is added but not handled
// here).
%destructor { } <tok_val>
%destructor { } <punct_type>
%destructor { } <comment_type>
%destructor { } <>
%destructor { delete $$; } <tree_type>
%destructor { delete $$; } <tree_matrix_type>
%destructor { delete $$; } <tree_cell_type>
%destructor { delete $$; } <tree_expression_type>
%destructor { delete $$; } <tree_constant_type>
%destructor { delete $$; } <tree_fcn_handle_type>
%destructor { delete $$; } <tree_superclass_ref_type>
%destructor { delete $$; } <tree_metaclass_query_type>
%destructor { delete $$; } <tree_function_def_type>
%destructor { delete $$; } <tree_anon_fcn_handle_type>
%destructor { delete $$; } <tree_identifier_type>
%destructor { delete $$; } <tree_index_expression_type>
%destructor { delete $$; } <tree_argument_list_type>
%destructor { delete $$; } <tree_parameter_list_type>
%destructor { delete $$; } <tree_command_type>
%destructor { delete $$; } <tree_if_command_type>
%destructor { delete $$; } <tree_if_clause_type>
%destructor { delete $$; } <tree_if_command_list_type>
%destructor { delete $$; } <tree_switch_command_type>
%destructor { delete $$; } <tree_switch_case_type>
%destructor { delete $$; } <tree_switch_case_list_type>
%destructor { delete $$; } <tree_decl_elt_type>
%destructor { delete $$; } <tree_decl_init_list_type>
%destructor { delete $$; } <tree_decl_command_type>
%destructor { delete $$; } <tree_statement_type>
%destructor { delete $$; } <tree_statement_list_type>
%destructor { delete $$; } <octave_user_function_type>
%destructor { delete $$; } <tree_classdef_type>
%destructor { delete $$; } <tree_classdef_attribute_type>
%destructor { delete $$; } <tree_classdef_attribute_list_type>
%destructor { delete $$; } <tree_classdef_superclass_type>
%destructor { delete $$; } <tree_classdef_superclass_list_type>
%destructor { delete $$; } <tree_classdef_body_type>
%destructor { delete $$; } <tree_classdef_property_type>
%destructor { delete $$; } <tree_classdef_property_list_type>
%destructor { delete $$; } <tree_classdef_properties_block_type>
%destructor { delete $$; } <tree_classdef_methods_list_type>
%destructor { delete $$; } <tree_classdef_methods_block_type>
%destructor { delete $$; } <tree_classdef_event_type>
%destructor { delete $$; } <tree_classdef_events_list_type>
%destructor { delete $$; } <tree_classdef_events_block_type>
%destructor { delete $$; } <tree_classdef_enum_type>
%destructor { delete $$; } <tree_classdef_enum_list_type>
%destructor { delete $$; } <tree_classdef_enum_block_type>
// Defining a generic destructor generates a warning if destructors are
// already explicitly declared for all types.
//
// %destructor {
// warning_with_id
// ("Octave:parser-destructor",
// "possible memory leak in cleanup following parse error");
// } <*>
// Where to start.
%start input
%%
// ==============================
// Statements and statement lists
// ==============================
input : simple_list '\n'
{
OCTAVE_YYUSE ($2);
$$ = nullptr;
std::shared_ptr<octave::tree_statement_list> tmp_lst ($1);
parser.statement_list (tmp_lst);
YYACCEPT;
}
| simple_list END_OF_INPUT
{
OCTAVE_YYUSE ($2);
$$ = nullptr;
lexer.m_end_of_input = true;
std::shared_ptr<octave::tree_statement_list> tmp_lst ($1);
parser.statement_list (tmp_lst);
YYACCEPT;
}
| parse_error
{
$$ = nullptr;
YYABORT;
}
;
simple_list : opt_sep_no_nl
{
OCTAVE_YYUSE ($1);
$$ = nullptr;
}
| simple_list1 opt_sep_no_nl
{ $$ = parser.set_stmt_print_flag ($1, $2, false); }
;
simple_list1 : statement
{ $$ = parser.make_statement_list ($1); }
| simple_list1 sep_no_nl statement
{ $$ = parser.append_statement_list ($1, $2, $3, false); }
;
opt_list : // empty
{ $$ = new octave::tree_statement_list (); }
| list
{ $$ = $1; }
;
list : list1 opt_sep
{ $$ = parser.set_stmt_print_flag ($1, $2, true); }
;
list1 : statement
{ $$ = parser.make_statement_list ($1); }
| list1 sep statement
{ $$ = parser.append_statement_list ($1, $2, $3, true); }
;
opt_fcn_list : // empty
{ $$ = new octave::tree_statement_list (); }
| fcn_list
{ $$ = $1; }
;
fcn_list : fcn_list1 opt_sep
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
fcn_list1 : function
{
octave::tree_statement *stmt = parser.make_statement ($1);
$$ = new octave::tree_statement_list (stmt);
}
| fcn_list1 opt_sep function
{
octave::tree_statement *stmt = parser.make_statement ($3);
$$ = parser.append_statement_list ($1, $2, stmt, false);
}
;
statement : expression
{ $$ = parser.make_statement ($1); }
| command
{ $$ = parser.make_statement ($1); }
| word_list_cmd
{ $$ = parser.make_statement ($1); }
;
// =================
// Word-list command
// =================
// These are not really like expressions since they can't appear on
// the RHS of an assignment. But they are also not like commands (IF,
// WHILE, etc.
word_list_cmd : identifier word_list
{
$$ = parser.make_index_expression ($1, $2, '(');
if (! $$)
{
// make_index_expression deleted $1 and $2.
YYABORT;
}
$$->mark_word_list_cmd ();
}
;
word_list : string
{ $$ = new octave::tree_argument_list ($1); }
| word_list string
{
$1->append ($2);
$$ = $1;
}
;
// ===========
// Expressions
// ===========
identifier : NAME
{
$$ = new octave::tree_identifier ($1->sym_rec (),
$1->line (),
$1->column ());
}
;
superclass_identifier
: SUPERCLASSREF
{
std::string meth = $1->superclass_method_name ();
std::string cls = $1->superclass_class_name ();
$$ = new octave::tree_superclass_ref (meth, cls,
$1->line (),
$1->column ());
}
;
meta_identifier : METAQUERY
{
std::string cls = $1->text ();
$$ = new octave::tree_metaclass_query (cls, $1->line (),
$1->column ());
}
;
string : DQ_STRING
{ $$ = parser.make_constant (DQ_STRING, $1); }
| SQ_STRING
{ $$ = parser.make_constant (SQ_STRING, $1); }
;
constant : NUM
{ $$ = parser.make_constant (NUM, $1); }
| IMAG_NUM
{ $$ = parser.make_constant (IMAG_NUM, $1); }
| string
{ $$ = $1; }
;
matrix : '[' matrix_rows ']'
{ $$ = parser.finish_matrix ($2, $1, $3); }
;
matrix_rows : cell_or_matrix_row
{ $$ = $1 ? new octave::tree_matrix ($1) : nullptr; }
| matrix_rows ';' cell_or_matrix_row
{
OCTAVE_YYUSE ($2);
if ($1)
{
if ($3)
$1->append ($3);
$$ = $1;
}
else
$$ = $3 ? new octave::tree_matrix ($3) : nullptr;
}
;
cell : '{' cell_rows '}'
{ $$ = parser.finish_cell ($2, $1, $3); }
;
cell_rows : cell_or_matrix_row
{ $$ = $1 ? new octave::tree_cell ($1) : nullptr; }
| cell_rows ';' cell_or_matrix_row
{
OCTAVE_YYUSE ($2);
if ($1)
{
if ($3)
$1->append ($3);
$$ = $1;
}
else
$$ = $3 ? new octave::tree_cell ($3) : nullptr;
}
;
// tree_argument_list objects can't be empty or have leading or trailing
// commas, but those are all allowed in matrix and cell array rows.
// FIXME: is tree_argument_list the best object for this purpose, or
// should we have a separate one intended specifically to represent the
// list of objects that make up elements in cell and matrix expressions?
cell_or_matrix_row
: // empty
{ $$ = nullptr; }
| ','
{
OCTAVE_YYUSE ($1);
$$ = nullptr;
}
| arg_list
{ $$ = $1; }
| arg_list ','
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
| ',' arg_list
{
OCTAVE_YYUSE ($1);
$$ = $2;
}
| ',' arg_list ','
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($3);
$$ = $2;
}
;
fcn_handle : FCN_HANDLE
{ $$ = parser.make_fcn_handle ($1); }
;
// Note that we are deliberately not setting the beginning of statement
// flag after recognizing the parameter list because we don't want to
// accept word list commands in anonymous function bodies.
anon_fcn_handle : '@' param_list anon_fcn_begin expression
{
$$ = parser.make_anon_fcn_handle ($2, $4, $1->beg_pos ());
if (! $$)
{
// make_anon_fcn_handle deleted $2 and $4.
YYABORT;
}
lexer.m_parsing_anon_fcn_body = false;
lexer.m_nesting_level.remove ();
}
| '@' param_list anon_fcn_begin error
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($2);
lexer.m_parsing_anon_fcn_body = false;
$$ = nullptr;
parser.bison_error ("anonymous function bodies must be single expressions");
YYABORT;
}
;
primary_expr : identifier
{ $$ = $1; }
| constant
{ $$ = $1; }
| fcn_handle
{ $$ = $1; }
| matrix
{
lexer.m_looking_at_matrix_or_assign_lhs = false;
$$ = $1;
}
| cell
{ $$ = $1; }
| meta_identifier
{ $$ = $1; }
| superclass_identifier
{ $$ = $1; }
| '(' expression ')'
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($3);
$$ = $2->mark_in_parens ();
}
;
magic_colon : ':'
{
OCTAVE_YYUSE ($1);
octave_value tmp (octave_value::magic_colon_t);
$$ = new octave::tree_constant (tmp);
}
;
magic_tilde : EXPR_NOT
{
OCTAVE_YYUSE ($1);
$$ = new octave::tree_black_hole ();
}
;
arg_list : expression
{ $$ = new octave::tree_argument_list ($1); }
| magic_colon
{ $$ = new octave::tree_argument_list ($1); }
| magic_tilde
{ $$ = new octave::tree_argument_list ($1); }
| arg_list ',' magic_colon
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
| arg_list ',' magic_tilde
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
| arg_list ',' expression
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
;
indirect_ref_op : '.'
{
OCTAVE_YYUSE ($1);
$$ = 0;
lexer.m_looking_at_indirect_ref = true;
}
;
oper_expr : primary_expr
{ $$ = $1; }
| oper_expr PLUS_PLUS
{ $$ = parser.make_postfix_op (PLUS_PLUS, $1, $2); }
| oper_expr MINUS_MINUS
{ $$ = parser.make_postfix_op (MINUS_MINUS, $1, $2); }
| oper_expr '(' ')'
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($3);
$$ = parser.make_index_expression ($1, nullptr, '(');
if (! $$)
{
// make_index_expression deleted $1.
YYABORT;
}
}
| oper_expr '(' arg_list ')'
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($4);
$$ = parser.make_index_expression ($1, $3, '(');
if (! $$)
{
// make_index_expression deleted $1 and $3.
YYABORT;
}
}
| oper_expr '{' '}'
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($3);
$$ = parser.make_index_expression ($1, nullptr, '{');
if (! $$)
{
// make_index_expression deleted $1.
YYABORT;
}
}
| oper_expr '{' arg_list '}'
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($4);
$$ = parser.make_index_expression ($1, $3, '{');
if (! $$)
{
// make_index_expression deleted $1 and $3.
YYABORT;
}
}
| oper_expr HERMITIAN
{ $$ = parser.make_postfix_op (HERMITIAN, $1, $2); }
| oper_expr TRANSPOSE
{ $$ = parser.make_postfix_op (TRANSPOSE, $1, $2); }
| oper_expr indirect_ref_op STRUCT_ELT
{ $$ = parser.make_indirect_ref ($1, $3->text ()); }
| oper_expr indirect_ref_op '(' expression ')'
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($5);
$$ = parser.make_indirect_ref ($1, $4);
}
| PLUS_PLUS oper_expr %prec UNARY
{ $$ = parser.make_prefix_op (PLUS_PLUS, $2, $1); }
| MINUS_MINUS oper_expr %prec UNARY
{ $$ = parser.make_prefix_op (MINUS_MINUS, $2, $1); }
| EXPR_NOT oper_expr %prec UNARY
{ $$ = parser.make_prefix_op (EXPR_NOT, $2, $1); }
| '+' oper_expr %prec UNARY
{ $$ = parser.make_prefix_op ('+', $2, $1); }
| '-' oper_expr %prec UNARY
{ $$ = parser.make_prefix_op ('-', $2, $1); }
| oper_expr POW power_expr
{ $$ = parser.make_binary_op (POW, $1, $2, $3); }
| oper_expr EPOW power_expr
{ $$ = parser.make_binary_op (EPOW, $1, $2, $3); }
| oper_expr '+' oper_expr
{ $$ = parser.make_binary_op ('+', $1, $2, $3); }
| oper_expr '-' oper_expr
{ $$ = parser.make_binary_op ('-', $1, $2, $3); }
| oper_expr '*' oper_expr
{ $$ = parser.make_binary_op ('*', $1, $2, $3); }
| oper_expr '/' oper_expr
{ $$ = parser.make_binary_op ('/', $1, $2, $3); }
| oper_expr EPLUS oper_expr
{ $$ = parser.make_binary_op ('+', $1, $2, $3); }
| oper_expr EMINUS oper_expr
{ $$ = parser.make_binary_op ('-', $1, $2, $3); }
| oper_expr EMUL oper_expr
{ $$ = parser.make_binary_op (EMUL, $1, $2, $3); }
| oper_expr EDIV oper_expr
{ $$ = parser.make_binary_op (EDIV, $1, $2, $3); }
| oper_expr LEFTDIV oper_expr
{ $$ = parser.make_binary_op (LEFTDIV, $1, $2, $3); }
| oper_expr ELEFTDIV oper_expr
{ $$ = parser.make_binary_op (ELEFTDIV, $1, $2, $3); }
;
power_expr : primary_expr
{ $$ = $1; }
| power_expr PLUS_PLUS
{ $$ = parser.make_postfix_op (PLUS_PLUS, $1, $2); }
| power_expr MINUS_MINUS
{ $$ = parser.make_postfix_op (MINUS_MINUS, $1, $2); }
| power_expr '(' ')'
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($3);
$$ = parser.make_index_expression ($1, nullptr, '(');
if (! $$)
{
// make_index_expression deleted $1.
YYABORT;
}
}
| power_expr '(' arg_list ')'
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($4);
$$ = parser.make_index_expression ($1, $3, '(');
if (! $$)
{
// make_index_expression deleted $1 and $3.
YYABORT;
}
}
| power_expr '{' '}'
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($3);
$$ = parser.make_index_expression ($1, nullptr, '{');
if (! $$)
{
// make_index_expression deleted $1.
YYABORT;
}
}
| power_expr '{' arg_list '}'
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($4);
$$ = parser.make_index_expression ($1, $3, '{');
if (! $$)
{
// make_index_expression deleted $1 and $3.
YYABORT;
}
}
| power_expr indirect_ref_op STRUCT_ELT
{ $$ = parser.make_indirect_ref ($1, $3->text ()); }
| power_expr indirect_ref_op '(' expression ')'
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($5);
$$ = parser.make_indirect_ref ($1, $4);
}
| PLUS_PLUS power_expr %prec POW
{ $$ = parser.make_prefix_op (PLUS_PLUS, $2, $1); }
| MINUS_MINUS power_expr %prec POW
{ $$ = parser.make_prefix_op (MINUS_MINUS, $2, $1); }
| EXPR_NOT power_expr %prec POW
{ $$ = parser.make_prefix_op (EXPR_NOT, $2, $1); }
| '+' power_expr %prec POW
{ $$ = parser.make_prefix_op ('+', $2, $1); }
| '-' power_expr %prec POW
{ $$ = parser.make_prefix_op ('-', $2, $1); }
;
colon_expr : oper_expr ':' oper_expr
{
OCTAVE_YYUSE ($2);
$$ = parser.make_colon_expression ($1, $3);
if (! $$)
{
// finish_colon_expression deleted $1 and $3.
YYABORT;
}
}
| oper_expr ':' oper_expr ':' oper_expr
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($4);
$$ = parser.make_colon_expression ($1, $5, $3);
if (! $$)
{
// finish_colon_expression deleted $1, $3, and $5.
YYABORT;
}
}
;
simple_expr : oper_expr
{ $$ = $1; }
| colon_expr
{ $$ = $1; }
| simple_expr EXPR_LT simple_expr
{ $$ = parser.make_binary_op (EXPR_LT, $1, $2, $3); }
| simple_expr EXPR_LE simple_expr
{ $$ = parser.make_binary_op (EXPR_LE, $1, $2, $3); }
| simple_expr EXPR_EQ simple_expr
{ $$ = parser.make_binary_op (EXPR_EQ, $1, $2, $3); }
| simple_expr EXPR_GE simple_expr
{ $$ = parser.make_binary_op (EXPR_GE, $1, $2, $3); }
| simple_expr EXPR_GT simple_expr
{ $$ = parser.make_binary_op (EXPR_GT, $1, $2, $3); }
| simple_expr EXPR_NE simple_expr
{ $$ = parser.make_binary_op (EXPR_NE, $1, $2, $3); }
| simple_expr EXPR_AND simple_expr
{ $$ = parser.make_binary_op (EXPR_AND, $1, $2, $3); }
| simple_expr EXPR_OR simple_expr
{ $$ = parser.make_binary_op (EXPR_OR, $1, $2, $3); }
| simple_expr EXPR_AND_AND simple_expr
{ $$ = parser.make_boolean_op (EXPR_AND_AND, $1, $2, $3); }
| simple_expr EXPR_OR_OR simple_expr
{ $$ = parser.make_boolean_op (EXPR_OR_OR, $1, $2, $3); }
;
assign_lhs : simple_expr
{
$$ = parser.validate_matrix_for_assignment ($1);
if ($$)
{ lexer.m_looking_at_matrix_or_assign_lhs = false; }
else
{
// validate_matrix_for_assignment deleted $1.
YYABORT;
}
}
;
assign_expr : assign_lhs '=' expression
{ $$ = parser.make_assign_op ('=', $1, $2, $3); }
| assign_lhs ADD_EQ expression
{ $$ = parser.make_assign_op (ADD_EQ, $1, $2, $3); }
| assign_lhs SUB_EQ expression
{ $$ = parser.make_assign_op (SUB_EQ, $1, $2, $3); }
| assign_lhs MUL_EQ expression
{ $$ = parser.make_assign_op (MUL_EQ, $1, $2, $3); }
| assign_lhs DIV_EQ expression
{ $$ = parser.make_assign_op (DIV_EQ, $1, $2, $3); }
| assign_lhs LEFTDIV_EQ expression
{ $$ = parser.make_assign_op (LEFTDIV_EQ, $1, $2, $3); }
| assign_lhs POW_EQ expression
{ $$ = parser.make_assign_op (POW_EQ, $1, $2, $3); }
| assign_lhs EMUL_EQ expression
{ $$ = parser.make_assign_op (EMUL_EQ, $1, $2, $3); }
| assign_lhs EDIV_EQ expression
{ $$ = parser.make_assign_op (EDIV_EQ, $1, $2, $3); }
| assign_lhs ELEFTDIV_EQ expression
{ $$ = parser.make_assign_op (ELEFTDIV_EQ, $1, $2, $3); }
| assign_lhs EPOW_EQ expression
{ $$ = parser.make_assign_op (EPOW_EQ, $1, $2, $3); }
| assign_lhs AND_EQ expression
{ $$ = parser.make_assign_op (AND_EQ, $1, $2, $3); }
| assign_lhs OR_EQ expression
{ $$ = parser.make_assign_op (OR_EQ, $1, $2, $3); }
;
expression : simple_expr
{
if ($1 && ($1->is_matrix () || $1->iscell ()))
{
if (parser.validate_array_list ($1))
$$ = $1;
else
{
delete $1;
YYABORT;
}
}
else
$$ = $1;
}
| assign_expr
{
if (! $1)
YYABORT;
$$ = $1;
}
| anon_fcn_handle
{ $$ = $1; }
;
// ================================================
// Commands, declarations, and function definitions
// ================================================
command : declaration
{ $$ = $1; }
| select_command
{ $$ = $1; }
| loop_command
{ $$ = $1; }
| jump_command
{ $$ = $1; }
| except_command
{ $$ = $1; }
| function
{ $$ = $1; }
| file
{ $$ = $1; }
;
// ======================
// Declaration statements
// ======================
declaration : GLOBAL decl1
{
$$ = parser.make_decl_command (GLOBAL, $1, $2);
lexer.m_looking_at_decl_list = false;
}
| PERSISTENT decl1
{
$$ = parser.make_decl_command (PERSISTENT, $1, $2);
lexer.m_looking_at_decl_list = false;
}
;
decl1 : decl2
{ $$ = new octave::tree_decl_init_list ($1); }
| decl1 decl2
{
$1->append ($2);
$$ = $1;
}
;
decl_param_init : // empty
{
$$ = 0;
lexer.m_looking_at_initializer_expression = true;
}
decl2 : identifier
{ $$ = new octave::tree_decl_elt ($1); }
| identifier '=' decl_param_init expression
{
OCTAVE_YYUSE ($2);
lexer.m_looking_at_initializer_expression = false;
$$ = new octave::tree_decl_elt ($1, $4);
}
;
// ====================
// Selection statements
// ====================
select_command : if_command
{ $$ = $1; }
| switch_command
{ $$ = $1; }
;
// ============
// If statement
// ============
if_command : IF stash_comment if_cmd_list END
{
if (! ($$ = parser.finish_if_command ($1, $3, $4, $2)))
{
// finish_if_command deleted $3.
YYABORT;
}
}
;
if_cmd_list : if_cmd_list1
{ $$ = $1; }
| if_cmd_list1 else_clause
{
$1->append ($2);
$$ = $1;
}
;
if_cmd_list1 : expression stmt_begin opt_sep opt_list
{
OCTAVE_YYUSE ($3);
$1->mark_braindead_shortcircuit ();
$$ = parser.start_if_command ($1, $4);
}
| if_cmd_list1 elseif_clause
{
$1->append ($2);
$$ = $1;
}
;
elseif_clause : ELSEIF stash_comment opt_sep expression stmt_begin opt_sep opt_list
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($6);
$4->mark_braindead_shortcircuit ();
$$ = parser.make_elseif_clause ($1, $4, $7, $2);
}
;
else_clause : ELSE stash_comment opt_sep opt_list
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($3);
$$ = new octave::tree_if_clause ($4, $2);
}
;
// ================
// Switch statement
// ================
switch_command : SWITCH stash_comment expression opt_sep case_list END
{
OCTAVE_YYUSE ($4);
if (! ($$ = parser.finish_switch_command ($1, $3, $5, $6, $2)))
{
// finish_switch_command deleted $3 adn $5.
YYABORT;
}
}
;
case_list : // empty
{ $$ = new octave::tree_switch_case_list (); }
| default_case
{ $$ = new octave::tree_switch_case_list ($1); }
| case_list1
{ $$ = $1; }
| case_list1 default_case
{
$1->append ($2);
$$ = $1;
}
;
case_list1 : switch_case
{ $$ = new octave::tree_switch_case_list ($1); }
| case_list1 switch_case
{
$1->append ($2);
$$ = $1;
}
;
switch_case : CASE stash_comment opt_sep expression stmt_begin opt_sep opt_list
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($6);
$$ = parser.make_switch_case ($1, $4, $7, $2);
}
;
default_case : OTHERWISE stash_comment opt_sep opt_list
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($3);
$$ = new octave::tree_switch_case ($4, $2);
}
;
// =======
// Looping
// =======
loop_command : WHILE stash_comment expression stmt_begin opt_sep opt_list END
{
OCTAVE_YYUSE ($5);
$3->mark_braindead_shortcircuit ();
if (! ($$ = parser.make_while_command ($1, $3, $6, $7, $2)))
{
// make_while_command deleted $3 and $6.
YYABORT;
}
}
| DO stash_comment opt_sep opt_list UNTIL expression
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($3);
$$ = parser.make_do_until_command ($5, $4, $6, $2);
}
| FOR stash_comment assign_lhs '=' expression stmt_begin opt_sep opt_list END
{
OCTAVE_YYUSE ($4);
OCTAVE_YYUSE ($7);
if (! ($$ = parser.make_for_command (FOR, $1, $3, $5,
nullptr, $8, $9, $2)))
{
// make_for_command deleted $3, $5, and $8.
YYABORT;
}
}
| FOR stash_comment '(' assign_lhs '=' expression ')' opt_sep opt_list END
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($5);
OCTAVE_YYUSE ($7);
OCTAVE_YYUSE ($8);
if (! ($$ = parser.make_for_command (FOR, $1, $4, $6,
nullptr, $9, $10, $2)))
{
// make_for_command deleted $4, $6, and $9.
YYABORT;
}
}
| PARFOR stash_comment assign_lhs '=' expression stmt_begin opt_sep opt_list END
{
OCTAVE_YYUSE ($4);
OCTAVE_YYUSE ($7);
if (! ($$ = parser.make_for_command (PARFOR, $1, $3, $5,
nullptr, $8, $9, $2)))
{
// make_for_command deleted $3, $5, and $8.
YYABORT;
}
}
| PARFOR stash_comment '(' assign_lhs '=' expression ',' expression ')' opt_sep opt_list END
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($5);
OCTAVE_YYUSE ($7);
OCTAVE_YYUSE ($9);
OCTAVE_YYUSE ($10);
if (! ($$ = parser.make_for_command (PARFOR, $1, $4, $6,
$8, $11, $12, $2)))
{
// make_for_command deleted $4, $6, $8, and $11.
YYABORT;
}
}
;
// =======
// Jumping
// =======
jump_command : BREAK
{
if (! ($$ = parser.make_break_command ($1)))
YYABORT;
}
| CONTINUE
{
if (! ($$ = parser.make_continue_command ($1)))
YYABORT;
}
| FUNC_RET
{ $$ = parser.make_return_command ($1); }
;
// ==========
// Exceptions
// ==========
except_command : UNWIND stash_comment opt_sep opt_list CLEANUP
stash_comment opt_sep opt_list END
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($5);
OCTAVE_YYUSE ($7);
if (! ($$ = parser.make_unwind_command ($1, $4, $8, $9, $2, $6)))
{
// make_unwind_command deleted $4 and $8.
YYABORT;
}
}
| TRY stash_comment opt_sep opt_list CATCH stash_comment
opt_sep opt_list END
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($5);
OCTAVE_YYUSE ($7);
if (! ($$ = parser.make_try_command ($1, $4, $7, $8, $9, $2, $6)))
{
// make_try_command deleted $4 and $8.
YYABORT;
}
}
| TRY stash_comment opt_sep opt_list END
{
OCTAVE_YYUSE ($3);
if (! ($$ = parser.make_try_command ($1, $4, 0, nullptr,
$5, $2, nullptr)))
{
// make_try_command deleted $4.
YYABORT;
}
}
;
// ===========================================
// Some 'subroutines' for function definitions
// ===========================================
push_fcn_symtab : // empty
{
if (! parser.push_fcn_symtab ())
YYABORT;
$$ = 0;
}
;
// ===========================
// List of function parameters
// ===========================
param_list_beg : '('
{
OCTAVE_YYUSE ($1);
$$ = 0;
lexer.m_looking_at_parameter_list = true;
if (lexer.m_looking_at_function_handle)
{
// Will get a real name later.
lexer.m_symtab_context.push (octave::symbol_scope ("parser:param_lsit_beg"));
lexer.m_looking_at_function_handle--;
lexer.m_looking_at_anon_fcn_args = true;
}
}
;
param_list_end : ')'
{
OCTAVE_YYUSE ($1);
$$ = 0;
lexer.m_looking_at_parameter_list = false;
lexer.m_looking_for_object_index = false;
}
;
opt_param_list : // empty
{ $$ = nullptr; }
| param_list
{ $$ = $1; }
;
param_list : param_list_beg param_list1 param_list_end
{
if ($2)
lexer.mark_as_variables ($2->variable_names ());
$$ = $2;
}
| param_list_beg error
{
$$ = nullptr;
parser.bison_error ("invalid parameter list");
YYABORT;
}
;
param_list1 : // empty
{ $$ = new octave::tree_parameter_list (octave::tree_parameter_list::in); }
| param_list2
{
$1->mark_as_formal_parameters ();
if (parser.validate_param_list ($1, octave::tree_parameter_list::in))
{
lexer.mark_as_variables ($1->variable_names ());
$$ = $1;
}
else
{
delete $1;
YYABORT;
}
}
;
param_list2 : param_list_elt
{ $$ = new octave::tree_parameter_list (octave::tree_parameter_list::in, $1); }
| param_list2 ',' param_list_elt
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
;
param_list_elt : decl2
{ $$ = $1; }
| magic_tilde
{ $$ = new octave::tree_decl_elt ($1); }
;
// ===================================
// List of function return value names
// ===================================
return_list : '[' ']'
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($2);
lexer.m_looking_at_return_list = false;
$$ = new octave::tree_parameter_list (octave::tree_parameter_list::out);
}
| identifier
{
lexer.m_looking_at_return_list = false;
octave::tree_parameter_list *tmp
= new octave::tree_parameter_list (octave::tree_parameter_list::out, $1);
// Even though this parameter list can contain only
// a single identifier, we still need to validate it
// to check for varargin or varargout.
if (parser.validate_param_list (tmp, octave::tree_parameter_list::out))
$$ = tmp;
else
{
delete tmp;
YYABORT;
}
}
| '[' return_list1 ']'
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($3);
lexer.m_looking_at_return_list = false;
// Check for duplicate parameter names, varargin,
// or varargout.
if (parser.validate_param_list ($2, octave::tree_parameter_list::out))
$$ = $2;
else
{
delete $2;
YYABORT;
}
}
;
return_list1 : identifier
{
$$ = new octave::tree_parameter_list (octave::tree_parameter_list::out, new octave::tree_decl_elt ($1));
}
| return_list1 ',' identifier
{
OCTAVE_YYUSE ($2);
$1->append (new octave::tree_decl_elt ($3));
$$ = $1;
}
;
// =======================
// Script or function file
// =======================
parsing_local_fcns
: // empty
{ parser.parsing_local_functions (true); }
;
push_script_symtab : // empty
{
$$ = 0;
// This scope may serve as the parent scope for local
// functions in classdef files..
lexer.m_symtab_context.push (octave::symbol_scope ("parser:push_script_symtab"));
}
;
begin_file : push_script_symtab INPUT_FILE
{ $$ = 0; }
;
file : begin_file opt_nl opt_list END_OF_INPUT
{
OCTAVE_YYUSE ($2);
if (lexer.m_reading_fcn_file)
{
// Delete the dummy statement_list we created
// after parsing the function. Any function
// definitions found in the file have already
// been stored in the symbol table or in
// base_parser::m_primary_fcn.
// Unused symbol table context.
lexer.m_symtab_context.pop ();
delete $3;
}
else
{
octave::tree_statement *end_of_script
= parser.make_end ("endscript", true,
$4->beg_pos (), $4->end_pos ());
parser.make_script ($3, end_of_script);
}
$$ = nullptr;
}
| begin_file opt_nl classdef parsing_local_fcns opt_sep opt_fcn_list END_OF_INPUT
{
OCTAVE_YYUSE ($2);
OCTAVE_YYUSE ($5);
OCTAVE_YYUSE ($7);
// Unused symbol table context.
lexer.m_symtab_context.pop ();
parser.finish_classdef_file ($3, $6);
$$ = nullptr;
}
;
// ===================
// Function definition
// ===================
function_beg : push_fcn_symtab FCN
{
$$ = $2;
if (lexer.m_reading_classdef_file
|| lexer.m_parsing_classdef)
lexer.m_maybe_classdef_get_set_method = true;
}
;
fcn_name : identifier
{
$$ = parser.make_fcn_name ($1);
if (! $$)
{
// make_fcn_name deleted $1.
YYABORT;
}
}
| GET '.' identifier
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($2);
lexer.m_parsed_function_name.top () = true;
lexer.m_maybe_classdef_get_set_method = false;
lexer.m_parsing_classdef_get_method = true;
$$ = $3;
}
| SET '.' identifier
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($2);
lexer.m_parsed_function_name.top () = true;
lexer.m_maybe_classdef_get_set_method = false;
lexer.m_parsing_classdef_set_method = true;
$$ = $3;
}
;
function_end : END
{
parser.endfunction_found (true);
if (parser.end_token_ok ($1, octave::token::function_end))
$$ = parser.make_end ("endfunction", false,
$1->beg_pos (), $1->end_pos ());
else
{
parser.end_token_error ($1, octave::token::function_end);
YYABORT;
}
}
| END_OF_INPUT
{
// A lot of tests are based on the assumption that this is OK
// if (lexer.m_reading_script_file)
// {
// parser.bison_error ("function body open at end of script");
// YYABORT;
// }
if (parser.endfunction_found ())
{
parser.bison_error ("inconsistent function endings -- "
"if one function is explicitly ended, "
"so must all the others");
YYABORT;
}
if (! (lexer.m_reading_fcn_file || lexer.m_reading_script_file
|| lexer.input_from_eval_string ()))
{
parser.bison_error ("function body open at end of input");
YYABORT;
}
if (lexer.m_reading_classdef_file)
{
parser.bison_error ("classdef body open at end of input");
YYABORT;
}
$$ = parser.make_end ("endfunction", true,
$1->beg_pos (), $1->end_pos ());
}
;
function : function_beg stash_comment fcn_name
opt_param_list opt_sep opt_list function_end
{
OCTAVE_YYUSE ($5);
$$ = parser.make_function ($1, nullptr, $3, $4, $6, $7, $2);
}
| function_beg stash_comment return_list '=' fcn_name
opt_param_list opt_sep opt_list function_end
{
OCTAVE_YYUSE ($4);
OCTAVE_YYUSE ($7);
$$ = parser.make_function ($1, $3, $5, $6, $8, $9, $2);
}
;
// ========
// Classdef
// ========
classdef_beg : CLASSDEF
{
if (! lexer.m_reading_classdef_file)
{
parser.bison_error ("classdef must appear inside a file containing only a class definition");
YYABORT;
}
// Create invalid parent scope.
lexer.m_symtab_context.push (octave::symbol_scope ());
lexer.m_parsing_classdef = true;
lexer.m_parsing_classdef_decl = true;
$$ = $1;
}
;
classdef : classdef_beg stash_comment attr_list identifier opt_sep superclass_list class_body END
{
OCTAVE_YYUSE ($5);
octave::comment_list *lc = $2;
octave::comment_list *tc = lexer.get_comment ();
lexer.m_parsing_classdef = false;
if (! ($$ = parser.make_classdef ($1, $3, $4, $6, $7, $8,
lc, tc)))
{
// make_classdef deleted $3, $4, $6, $7, LC, and
// TC.
YYABORT;
}
}
;
attr_list : // empty
{ $$ = nullptr; }
| '(' attr_list1 ')' opt_sep
{
OCTAVE_YYUSE ($1);
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($4);
$$ = $2;
}
;
attr_list1 : attr
{ $$ = new octave::tree_classdef_attribute_list ($1); }
| attr_list1 ',' attr
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
;
attr : identifier
{ $$ = new octave::tree_classdef_attribute ($1); }
| identifier '=' decl_param_init expression
{
OCTAVE_YYUSE ($2);
lexer.m_looking_at_initializer_expression = false;
$$ = new octave::tree_classdef_attribute ($1, $4);
}
| EXPR_NOT identifier
{
OCTAVE_YYUSE ($1);
$$ = new octave::tree_classdef_attribute ($2, false);
}
;
superclass_list : // empty
{
lexer.m_parsing_classdef_decl = false;
lexer.m_parsing_classdef_superclass = false;
$$ = nullptr;
}
| superclass_list1 opt_sep
{
OCTAVE_YYUSE ($2);
lexer.m_parsing_classdef_decl = false;
lexer.m_parsing_classdef_superclass = false;
$$ = $1;
}
;
superclass_list1
: EXPR_LT superclass
{
OCTAVE_YYUSE ($1);
$$ = new octave::tree_classdef_superclass_list ($2);
}
| superclass_list1 EXPR_AND superclass
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
;
superclass : FQ_IDENT
{ $$ = new octave::tree_classdef_superclass ($1->text ()); }
;
class_body : // empty
{ $$ = nullptr; }
| class_body1 opt_sep
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
class_body1 : properties_block
{ $$ = new octave::tree_classdef_body ($1); }
| methods_block
{ $$ = new octave::tree_classdef_body ($1); }
| events_block
{ $$ = new octave::tree_classdef_body ($1); }
| enum_block
{ $$ = new octave::tree_classdef_body ($1); }
| class_body1 opt_sep properties_block
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
| class_body1 opt_sep methods_block
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
| class_body1 opt_sep events_block
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
| class_body1 opt_sep enum_block
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
;
properties_block
: PROPERTIES stash_comment opt_sep attr_list property_list END
{
OCTAVE_YYUSE ($3);
octave::comment_list *lc = $2;
octave::comment_list *tc = lexer.get_comment ();
if (! ($$ = parser.make_classdef_properties_block
($1, $4, $5, $6, lc, tc)))
{
// make_classdef_properties_block deleted $4,
// $5, LC, and TC.
YYABORT;
}
}
;
property_list : // empty
{ $$ = nullptr; }
| property_list1 opt_sep
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
property_list1
: class_property
{ $$ = new octave::tree_classdef_property_list ($1); }
| property_list1 sep class_property
{
OCTAVE_YYUSE ($2);
// We don't look ahead to grab end-of-line comments.
// Instead, they are grabbed when we see the
// identifier that becomes the next element in the
// list. If the element at the end of the list
// doesn't have a doc string, see whether the
// element we are adding is stroing an end-of-line
// comment for us to use.
octave::tree_classdef_property *last_elt = $1->back ();
if (! last_elt->have_doc_string ())
{
octave::comment_list *cl = $3->comments ();
if (cl)
{
octave::comment_elt elt = cl->front ();
if (elt.is_end_of_line ())
last_elt->doc_string (elt.text ());
}
}
$1->append ($3);
$$ = $1;
}
;
class_property : stash_comment identifier
{
$$ = new octave::tree_classdef_property ($2, $1);
}
| stash_comment identifier '=' decl_param_init expression
{
OCTAVE_YYUSE ($3);
lexer.m_looking_at_initializer_expression = false;
$$ = new octave::tree_classdef_property ($2, $5, $1);
}
;
methods_block : METHODS stash_comment opt_sep attr_list methods_list END
{
OCTAVE_YYUSE ($3);
octave::comment_list *lc = $2;
octave::comment_list *tc = lexer.get_comment ();
if (! ($$ = parser.make_classdef_methods_block
($1, $4, $5, $6, lc, tc)))
{
// make_classdef_methods_block deleted $4, $5,
// LC, and TC.
YYABORT;
}
}
;
method_decl1 : identifier
{
if (! ($$ = parser.start_classdef_external_method ($1, nullptr)))
YYABORT;
}
| identifier param_list
{
if (! ($$ = parser.start_classdef_external_method ($1, $2)))
YYABORT;
}
;
method_decl : stash_comment method_decl1
{ $$ = parser.finish_classdef_external_method ($2, nullptr, $1); }
| stash_comment return_list '='
{
OCTAVE_YYUSE ($3);
lexer.m_defining_func++;
lexer.m_parsed_function_name.push (false);
}
method_decl1
{
lexer.m_defining_func--;
lexer.m_parsed_function_name.pop ();
$$ = parser.finish_classdef_external_method ($5, $2, $1);
}
;
method : method_decl
{ $$ = $1; }
| function
{ $$ = $1; }
;
methods_list : // empty
{ $$ = nullptr; }
| methods_list1 opt_sep
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
methods_list1 : method
{
octave_value fcn;
if ($1)
fcn = $1->function ();
delete $1;
$$ = new octave::tree_classdef_methods_list (fcn);
}
| methods_list1 opt_sep method
{
OCTAVE_YYUSE ($2);
octave_value fcn;
if ($3)
fcn = $3->function ();
delete $3;
$1->append (fcn);
$$ = $1;
}
;
events_block : EVENTS stash_comment opt_sep attr_list events_list END
{
OCTAVE_YYUSE ($3);
octave::comment_list *lc = $2;
octave::comment_list *tc = lexer.get_comment ();
if (! ($$ = parser.make_classdef_events_block
($1, $4, $5, $6, lc, tc)))
{
// make_classdef_events_block deleted $4, $5,
// LC, and TC.
YYABORT;
}
}
;
events_list : // empty
{ $$ = nullptr; }
| events_list1 opt_sep
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
events_list1 : class_event
{ $$ = new octave::tree_classdef_events_list ($1); }
| events_list1 opt_sep class_event
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
;
class_event : stash_comment identifier
{ $$ = new octave::tree_classdef_event ($2, $1); }
;
enum_block : ENUMERATION stash_comment opt_sep attr_list enum_list END
{
OCTAVE_YYUSE ($3);
octave::comment_list *lc = $2;
octave::comment_list *tc = lexer.get_comment ();
if (! ($$ = parser.make_classdef_enum_block
($1, $4, $5, $6, lc, tc)))
{
// make_classdef_enum_block deleted $4, $5, LC,
// and TC.
YYABORT;
}
}
;
enum_list : // empty
{ $$ = nullptr; }
| enum_list1 opt_sep
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
enum_list1 : class_enum
{ $$ = new octave::tree_classdef_enum_list ($1); }
| enum_list1 opt_sep class_enum
{
OCTAVE_YYUSE ($2);
$1->append ($3);
$$ = $1;
}
;
class_enum : stash_comment identifier '(' expression ')'
{
OCTAVE_YYUSE ($3);
OCTAVE_YYUSE ($5);
$$ = new octave::tree_classdef_enum ($2, $4, $1);
}
;
// =============
// Miscellaneous
// =============
stmt_begin : // empty
{
$$ = 0;
lexer.m_at_beginning_of_statement = true;
}
;
anon_fcn_begin : // empty
{
$$ = 0;
lexer.m_at_beginning_of_statement = true;
lexer.m_parsing_anon_fcn_body = true;
}
;
stash_comment : // empty
{ $$ = lexer.get_comment (); }
;
parse_error : LEXICAL_ERROR
{
$$ = 0;
std::string msg = $1->text ();
parser.bison_error (msg.c_str ());
}
| error
{ $$ = 0; }
;
sep_no_nl : ','
{
OCTAVE_YYUSE ($1);
$$ = ',';
}
| ';'
{
OCTAVE_YYUSE ($1);
$$ = ';';
}
| sep_no_nl ','
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
| sep_no_nl ';'
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
opt_sep_no_nl : // empty
{ $$ = 0; }
| sep_no_nl
{ $$ = $1; }
;
opt_nl : // empty
{ $$ = 0; }
| nl
{ $$ = $1; }
;
nl : '\n'
{
OCTAVE_YYUSE ($1);
$$ = '\n';
}
| nl '\n'
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
sep : ','
{
OCTAVE_YYUSE ($1);
$$ = ',';
}
| ';'
{
OCTAVE_YYUSE ($1);
$$ = ';';
}
| '\n'
{
OCTAVE_YYUSE ($1);
$$ = '\n';
}
| sep ','
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
| sep ';'
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
| sep '\n'
{
OCTAVE_YYUSE ($2);
$$ = $1;
}
;
opt_sep : // empty
{ $$ = 0; }
| sep
{ $$ = $1; }
;
%%
#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
// Restore prevailing warning state for remainder of the file.
# pragma GCC diagnostic pop
#endif
// Generic error messages.
#undef lexer
#undef scanner
static void
yyerror (octave::base_parser& parser, const char *s)
{
parser.bison_error (s);
}
namespace octave
{
size_t
base_parser::parent_scope_info::size (void) const
{
return m_info.size ();
}
void
base_parser::parent_scope_info::push (const value_type& elt)
{
m_info.push_back (elt);
}
void
base_parser::parent_scope_info::push (const symbol_scope& scope)
{
push (value_type (scope, ""));
}
void
base_parser::parent_scope_info::pop (void)
{
m_info.pop_back ();
}
bool
base_parser::parent_scope_info::name_ok (const std::string& name)
{
// Name can't be the same as any parent function or any other
// function we've already seen. We could maintain a complex
// tree structure of names, or we can just store the set of
// full names of all the functions, which must be unique.
std::string full_name;
for (size_t i = 0; i < size()-1; i++)
{
const value_type& elt = m_info[i];
if (name == elt.second)
return false;
full_name += elt.second + ">";
}
full_name += name;
if (m_all_names.find (full_name) != m_all_names.end ())
{
// Return false (failure) if we are parsing a subfunction, local
// function, or nested function. Otherwise, it is OK to have a
// duplicate name.
return ! (m_parser.parsing_subfunctions ()
|| m_parser.parsing_local_functions ()
|| m_parser.curr_fcn_depth () > 0);
}
m_all_names.insert (full_name);
return true;
}
bool
base_parser::parent_scope_info::name_current_scope (const std::string& name)
{
if (! name_ok (name))
return false;
if (size () > 0)
m_info.back().second = name;
return true;
}
symbol_scope
base_parser::parent_scope_info::parent_scope (void) const
{
return size () > 1 ? m_info[size()-2].first : symbol_scope ();
}
std::string
base_parser::parent_scope_info::parent_name (void) const
{
return m_info[size()-2].second;
}
void base_parser::parent_scope_info::clear (void)
{
m_info.clear ();
m_all_names.clear ();
}
base_parser::base_parser (base_lexer& lxr)
: m_endfunction_found (false), m_autoloading (false),
m_fcn_file_from_relative_lookup (false),
m_parsing_subfunctions (false), m_parsing_local_functions (false),
m_max_fcn_depth (-1), m_curr_fcn_depth (-1), m_primary_fcn_scope (),
m_curr_class_name (), m_curr_package_name (), m_function_scopes (*this),
m_primary_fcn (), m_subfunction_names (), m_classdef_object (),
m_stmt_list (), m_lexer (lxr), m_parser_state (yypstate_new ())
{ }
base_parser::~base_parser (void)
{
delete &m_lexer;
// FIXME: Deleting the internal Bison parser state structure does
// not clean up any partial parse trees in the event of an interrupt or
// error. It's not clear how to safely do that with the C language
// parser that Bison generates. The C++ language parser that Bison
// generates would do it for us automatically whenever an exception
// is thrown while parsing input, but there is currently no C++
// interface for a push parser.
yypstate_delete (static_cast<yypstate *> (m_parser_state));
}
void
base_parser::reset (void)
{
m_endfunction_found = false;
m_autoloading = false;
m_fcn_file_from_relative_lookup = false;
m_parsing_subfunctions = false;
m_parsing_local_functions = false;
m_max_fcn_depth = -1;
m_curr_fcn_depth = -1;
m_primary_fcn_scope = symbol_scope ();
m_curr_class_name = "";
m_curr_package_name = "";
m_function_scopes.clear ();
m_primary_fcn = octave_value ();
m_subfunction_names.clear ();
m_classdef_object.reset ();
m_stmt_list.reset ();
m_lexer.reset ();
yypstate_delete (static_cast<yypstate *> (m_parser_state));
m_parser_state = yypstate_new ();
}
// Error messages for mismatched end tokens.
static std::string
end_token_as_string (token::end_tok_type ettype)
{
std::string retval = "<unknown>";
switch (ettype)
{
case token::simple_end:
retval = "end";
break;
case token::classdef_end:
retval = "endclassdef";
break;
case token::enumeration_end:
retval = "endenumeration";
break;
case token::events_end:
retval = "endevents";
break;
case token::for_end:
retval = "endfor";
break;
case token::function_end:
retval = "endfunction";
break;
case token::if_end:
retval = "endif";
break;
case token::methods_end:
retval = "endmethods";
break;
case token::parfor_end:
retval = "endparfor";
break;
case token::properties_end:
retval = "endproperties";
break;
case token::switch_end:
retval = "endswitch";
break;
case token::try_catch_end:
retval = "end_try_catch";
break;
case token::unwind_protect_end:
retval = "end_unwind_protect";
break;
case token::while_end:
retval = "endwhile";
break;
default:
panic_impossible ();
break;
}
return retval;
}
void
base_parser::statement_list (std::shared_ptr<tree_statement_list>& lst)
{
if (! lst)
return;
if (m_stmt_list)
{
// Append additional code to existing statement list.
while (! lst->empty ())
{
m_stmt_list->push_back (lst->front ());
lst->pop_front ();
}
}
else
m_stmt_list = lst;
}
void
base_parser::end_token_error (token *tok, token::end_tok_type expected)
{
std::string msg = ("'" + end_token_as_string (expected)
+ "' command matched by '"
+ end_token_as_string (tok->ettype ()) + "'");
bison_error (msg, tok->beg_pos ());
}
// Check to see that end tokens are properly matched.
bool
base_parser::end_token_ok (token *tok, token::end_tok_type expected)
{
token::end_tok_type ettype = tok->ettype ();
return ettype == expected || ettype == token::simple_end;
}
bool
base_parser::push_fcn_symtab (void)
{
m_curr_fcn_depth++;
if (m_max_fcn_depth < m_curr_fcn_depth)
m_max_fcn_depth = m_curr_fcn_depth;
// Will get a real name later.
m_lexer.m_symtab_context.push (octave::symbol_scope ("parser:push_fcn_symtab"));
m_function_scopes.push (m_lexer.m_symtab_context.curr_scope ());
if (! m_lexer.m_reading_script_file && m_curr_fcn_depth == 0
&& ! m_parsing_subfunctions)
{
m_primary_fcn_scope = m_lexer.m_symtab_context.curr_scope ();
m_primary_fcn_scope.mark_primary_fcn_scope ();
}
if (m_lexer.m_reading_script_file && m_curr_fcn_depth > 0)
{
bison_error ("nested functions not implemented in this context");
return false;
}
return true;
}
// Make a constant.
tree_constant *
base_parser::make_constant (int op, token *tok_val)
{
int l = tok_val->line ();
int c = tok_val->column ();
tree_constant *retval = nullptr;
switch (op)
{
case NUM:
{
octave_value tmp (tok_val->number ());
retval = new tree_constant (tmp, l, c);
retval->stash_original_text (tok_val->text_rep ());
}
break;
case IMAG_NUM:
{
octave_value tmp (Complex (0.0, tok_val->number ()));
retval = new tree_constant (tmp, l, c);
retval->stash_original_text (tok_val->text_rep ());
}
break;
case DQ_STRING:
case SQ_STRING:
{
std::string txt = tok_val->text ();
char delim = op == DQ_STRING ? '"' : '\'';
octave_value tmp (txt, delim);
if (txt.empty ())
{
if (op == DQ_STRING)
tmp = octave_null_str::instance;
else
tmp = octave_null_sq_str::instance;
}
retval = new tree_constant (tmp, l, c);
if (op == DQ_STRING)
txt = undo_string_escapes (txt);
// FIXME: maybe this should also be handled by
// tok_val->text_rep () for character strings?
retval->stash_original_text (delim + txt + delim);
}
break;
default:
panic_impossible ();
break;
}
return retval;
}
// Make a function handle.
tree_fcn_handle *
base_parser::make_fcn_handle (token *tok_val)
{
int l = tok_val->line ();
int c = tok_val->column ();
tree_fcn_handle *retval = new tree_fcn_handle (tok_val->text (), l, c);
return retval;
}
// Make an anonymous function handle.
tree_anon_fcn_handle *
base_parser::make_anon_fcn_handle (tree_parameter_list *param_list,
tree_expression *expr,
const filepos& at_pos)
{
// FIXME: We need to examine EXPR and issue an error if any
// sub-expression contains an assignment, compound assignment,
// increment, or decrement operator.
anon_fcn_validator validator (param_list, expr);
if (! validator.ok ())
{
delete param_list;
delete expr;
bison_error (validator.message (), validator.line (),
validator.column ());
return nullptr;
}
symbol_scope fcn_scope = m_lexer.m_symtab_context.curr_scope ();
symbol_scope parent_scope = m_lexer.m_symtab_context.parent_scope ();
m_lexer.m_symtab_context.pop ();
expr->set_print_flag (false);
fcn_scope.mark_static ();
int at_line = at_pos.line ();
int at_column = at_pos.column ();
tree_anon_fcn_handle *retval
= new tree_anon_fcn_handle (param_list, expr, fcn_scope,
parent_scope, at_line, at_column);
std::ostringstream buf;
tree_print_code tpc (buf);
retval->accept (tpc);
std::string file = m_lexer.m_fcn_file_full_name;
if (! file.empty ())
buf << ": file: " << file;
else if (m_lexer.input_from_terminal ())
buf << ": *terminal input*";
else if (m_lexer.input_from_eval_string ())
buf << ": *eval string*";
buf << ": line: " << at_line << " column: " << at_column;
std::string scope_name = buf.str ();
fcn_scope.cache_name (scope_name);
// FIXME: Stash the filename. This does not work and produces
// errors when executed.
//retval->stash_file_name (m_lexer.m_fcn_file_name);
return retval;
}
// Build a colon expression.
tree_expression *
base_parser::make_colon_expression (tree_expression *base,
tree_expression *limit,
tree_expression *incr)
{
tree_expression *retval = nullptr;
if (! base || ! limit)
{
delete base;
delete limit;
delete incr;
return retval;
}
int l = base->line ();
int c = base->column ();
tree_colon_expression *expr
= new tree_colon_expression (base, limit, incr, l, c);
retval = expr;
if (base->is_constant () && limit->is_constant ()
&& (! incr || incr->is_constant ()))
{
interpreter& interp = __get_interpreter__ ("finish_colon_expression");
try
{
// If the evaluation generates a warning message, restore
// the previous value of last_warning_message and skip the
// conversion to a constant value.
unwind_protect frame;
error_system& es = interp.get_error_system ();
frame.add_method (es, &error_system::set_last_warning_message,
es.last_warning_message (""));
frame.add_method (es, &error_system::set_discard_warning_messages,
es.discard_warning_messages (true));
tree_evaluator& tw = interp.get_evaluator ();
octave_value tmp = expr->evaluate (tw);
std::string msg = es.last_warning_message ();
if (msg.empty ())
{
tree_constant *tc_retval
= new tree_constant (tmp, expr->line (), expr->column ());
std::ostringstream buf;
tree_print_code tpc (buf);
expr->accept (tpc);
tc_retval->stash_original_text (buf.str ());
delete expr;
retval = tc_retval;
}
}
catch (const execution_exception&)
{
interp.recover_from_exception ();
}
}
return retval;
}
// Build a binary expression.
tree_expression *
base_parser::make_binary_op (int op, tree_expression *op1,
token *tok_val, tree_expression *op2)
{
octave_value::binary_op t = octave_value::unknown_binary_op;
switch (op)
{
case POW:
t = octave_value::op_pow;
break;
case EPOW:
t = octave_value::op_el_pow;
break;
case '+':
t = octave_value::op_add;
break;
case '-':
t = octave_value::op_sub;
break;
case '*':
t = octave_value::op_mul;
break;
case '/':
t = octave_value::op_div;
break;
case EMUL:
t = octave_value::op_el_mul;
break;
case EDIV:
t = octave_value::op_el_div;
break;
case LEFTDIV:
t = octave_value::op_ldiv;
break;
case ELEFTDIV:
t = octave_value::op_el_ldiv;
break;
case EXPR_LT:
t = octave_value::op_lt;
break;
case EXPR_LE:
t = octave_value::op_le;
break;
case EXPR_EQ:
t = octave_value::op_eq;
break;
case EXPR_GE:
t = octave_value::op_ge;
break;
case EXPR_GT:
t = octave_value::op_gt;
break;
case EXPR_NE:
t = octave_value::op_ne;
break;
case EXPR_AND:
t = octave_value::op_el_and;
break;
case EXPR_OR:
t = octave_value::op_el_or;
break;
default:
panic_impossible ();
break;
}
int l = tok_val->line ();
int c = tok_val->column ();
return maybe_compound_binary_expression (op1, op2, l, c, t);
}
// Build a boolean expression.
tree_expression *
base_parser::make_boolean_op (int op, tree_expression *op1,
token *tok_val, tree_expression *op2)
{
tree_boolean_expression::type t;
switch (op)
{
case EXPR_AND_AND:
t = tree_boolean_expression::bool_and;
break;
case EXPR_OR_OR:
t = tree_boolean_expression::bool_or;
break;
default:
panic_impossible ();
break;
}
int l = tok_val->line ();
int c = tok_val->column ();
return new tree_boolean_expression (op1, op2, l, c, t);
}
// Build a prefix expression.
tree_expression *
base_parser::make_prefix_op (int op, tree_expression *op1, token *tok_val)
{
octave_value::unary_op t = octave_value::unknown_unary_op;
switch (op)
{
case EXPR_NOT:
t = octave_value::op_not;
break;
case '+':
t = octave_value::op_uplus;
break;
case '-':
t = octave_value::op_uminus;
break;
case PLUS_PLUS:
t = octave_value::op_incr;
break;
case MINUS_MINUS:
t = octave_value::op_decr;
break;
default:
panic_impossible ();
break;
}
int l = tok_val->line ();
int c = tok_val->column ();
return new tree_prefix_expression (op1, l, c, t);
}
// Build a postfix expression.
tree_expression *
base_parser::make_postfix_op (int op, tree_expression *op1, token *tok_val)
{
octave_value::unary_op t = octave_value::unknown_unary_op;
switch (op)
{
case HERMITIAN:
t = octave_value::op_hermitian;
break;
case TRANSPOSE:
t = octave_value::op_transpose;
break;
case PLUS_PLUS:
t = octave_value::op_incr;
break;
case MINUS_MINUS:
t = octave_value::op_decr;
break;
default:
panic_impossible ();
break;
}
int l = tok_val->line ();
int c = tok_val->column ();
return new tree_postfix_expression (op1, l, c, t);
}
// Build an unwind-protect command.
tree_command *
base_parser::make_unwind_command (token *unwind_tok,
tree_statement_list *body,
tree_statement_list *cleanup_stmts,
token *end_tok,
comment_list *lc,
comment_list *mc)
{
tree_command *retval = nullptr;
if (end_token_ok (end_tok, token::unwind_protect_end))
{
comment_list *tc = m_lexer.m_comment_buf.get_comment ();
int l = unwind_tok->line ();
int c = unwind_tok->column ();
retval = new tree_unwind_protect_command (body, cleanup_stmts,
lc, mc, tc, l, c);
}
else
{
delete body;
delete cleanup_stmts;
end_token_error (end_tok, token::unwind_protect_end);
}
return retval;
}
// Build a try-catch command.
tree_command *
base_parser::make_try_command (token *try_tok,
tree_statement_list *body,
char catch_sep,
tree_statement_list *cleanup_stmts,
token *end_tok,
comment_list *lc,
comment_list *mc)
{
tree_command *retval = nullptr;
if (end_token_ok (end_tok, token::try_catch_end))
{
comment_list *tc = m_lexer.m_comment_buf.get_comment ();
int l = try_tok->line ();
int c = try_tok->column ();
tree_identifier *id = nullptr;
if (! catch_sep && cleanup_stmts && ! cleanup_stmts->empty ())
{
tree_statement *stmt = cleanup_stmts->front ();
if (stmt)
{
tree_expression *expr = stmt->expression ();
if (expr && expr->is_identifier ())
{
id = dynamic_cast<tree_identifier *> (expr);
cleanup_stmts->pop_front ();
stmt->set_expression (nullptr);
delete stmt;
}
}
}
retval = new tree_try_catch_command (body, cleanup_stmts, id,
lc, mc, tc, l, c);
}
else
{
delete body;
delete cleanup_stmts;
end_token_error (end_tok, token::try_catch_end);
}
return retval;
}
// Build a while command.
tree_command *
base_parser::make_while_command (token *while_tok,
tree_expression *expr,
tree_statement_list *body,
token *end_tok,
comment_list *lc)
{
tree_command *retval = nullptr;
maybe_warn_assign_as_truth_value (expr);
if (end_token_ok (end_tok, token::while_end))
{
comment_list *tc = m_lexer.m_comment_buf.get_comment ();
m_lexer.m_looping--;
int l = while_tok->line ();
int c = while_tok->column ();
retval = new tree_while_command (expr, body, lc, tc, l, c);
}
else
{
delete expr;
delete body;
end_token_error (end_tok, token::while_end);
}
return retval;
}
// Build a do-until command.
tree_command *
base_parser::make_do_until_command (token *until_tok,
tree_statement_list *body,
tree_expression *expr,
comment_list *lc)
{
maybe_warn_assign_as_truth_value (expr);
comment_list *tc = m_lexer.m_comment_buf.get_comment ();
m_lexer.m_looping--;
int l = until_tok->line ();
int c = until_tok->column ();
return new tree_do_until_command (expr, body, lc, tc, l, c);
}
// Build a for command.
tree_command *
base_parser::make_for_command (int tok_id, token *for_tok,
tree_argument_list *lhs,
tree_expression *expr,
tree_expression *maxproc,
tree_statement_list *body,
token *end_tok,
comment_list *lc)
{
tree_command *retval = nullptr;
bool parfor = tok_id == PARFOR;
if (end_token_ok (end_tok, parfor ? token::parfor_end : token::for_end))
{
expr->mark_as_for_cmd_expr ();
comment_list *tc = m_lexer.m_comment_buf.get_comment ();
m_lexer.m_looping--;
int l = for_tok->line ();
int c = for_tok->column ();
if (lhs->length () == 1)
{
tree_expression *tmp = lhs->remove_front ();
retval = new tree_simple_for_command (parfor, tmp, expr, maxproc,
body, lc, tc, l, c);
delete lhs;
}
else
{
if (parfor)
{
delete lhs;
delete expr;
delete maxproc;
delete body;
bison_error ("invalid syntax for parfor statement");
}
else
retval = new tree_complex_for_command (lhs, expr, body,
lc, tc, l, c);
}
}
else
{
delete lhs;
delete expr;
delete maxproc;
delete body;
end_token_error (end_tok, parfor ? token::parfor_end : token::for_end);
}
return retval;
}
// Build a break command.
tree_command *
base_parser::make_break_command (token *break_tok)
{
int l = break_tok->line ();
int c = break_tok->column ();
if (! m_lexer.m_looping)
{
bison_error ("break must appear within a loop");
return nullptr;
}
else
return new tree_break_command (l, c);
}
// Build a continue command.
tree_command *
base_parser::make_continue_command (token *continue_tok)
{
int l = continue_tok->line ();
int c = continue_tok->column ();
if (! m_lexer.m_looping)
{
bison_error ("continue must appear within a loop");
return nullptr;
}
else
return new tree_continue_command (l, c);
}
// Build a return command.
tree_command *
base_parser::make_return_command (token *return_tok)
{
int l = return_tok->line ();
int c = return_tok->column ();
return new tree_return_command (l, c);
}
// Start an if command.
tree_if_command_list *
base_parser::start_if_command (tree_expression *expr,
tree_statement_list *list)
{
maybe_warn_assign_as_truth_value (expr);
tree_if_clause *t = new tree_if_clause (expr, list);
return new tree_if_command_list (t);
}
// Finish an if command.
tree_if_command *
base_parser::finish_if_command (token *if_tok,
tree_if_command_list *list,
token *end_tok,
comment_list *lc)
{
tree_if_command *retval = nullptr;
if (end_token_ok (end_tok, token::if_end))
{
comment_list *tc = m_lexer.m_comment_buf.get_comment ();
int l = if_tok->line ();
int c = if_tok->column ();
if (list && ! list->empty ())
{
tree_if_clause *elt = list->front ();
if (elt)
{
elt->line (l);
elt->column (c);
}
}
retval = new tree_if_command (list, lc, tc, l, c);
}
else
{
delete list;
end_token_error (end_tok, token::if_end);
}
return retval;
}
// Build an elseif clause.
tree_if_clause *
base_parser::make_elseif_clause (token *elseif_tok,
tree_expression *expr,
tree_statement_list *list,
comment_list *lc)
{
maybe_warn_assign_as_truth_value (expr);
int l = elseif_tok->line ();
int c = elseif_tok->column ();
return new tree_if_clause (expr, list, lc, l, c);
}
// Finish a switch command.
tree_switch_command *
base_parser::finish_switch_command (token *switch_tok,
tree_expression *expr,
tree_switch_case_list *list,
token *end_tok,
comment_list *lc)
{
tree_switch_command *retval = nullptr;
if (end_token_ok (end_tok, token::switch_end))
{
comment_list *tc = m_lexer.m_comment_buf.get_comment ();
int l = switch_tok->line ();
int c = switch_tok->column ();
if (list && ! list->empty ())
{
tree_switch_case *elt = list->front ();
if (elt)
{
elt->line (l);
elt->column (c);
}
}
retval = new tree_switch_command (expr, list, lc, tc, l, c);
}
else
{
delete expr;
delete list;
end_token_error (end_tok, token::switch_end);
}
return retval;
}
// Build a switch case.
tree_switch_case *
base_parser::make_switch_case (token *case_tok,
tree_expression *expr,
tree_statement_list *list,
comment_list *lc)
{
maybe_warn_variable_switch_label (expr);
int l = case_tok->line ();
int c = case_tok->column ();
return new tree_switch_case (expr, list, lc, l, c);
}
// Build an assignment to a variable.
tree_expression *
base_parser::make_assign_op (int op, tree_argument_list *lhs,
token *eq_tok, tree_expression *rhs)
{
octave_value::assign_op t = octave_value::unknown_assign_op;
switch (op)
{
case '=':
t = octave_value::op_asn_eq;
break;
case ADD_EQ:
t = octave_value::op_add_eq;
break;
case SUB_EQ:
t = octave_value::op_sub_eq;
break;
case MUL_EQ:
t = octave_value::op_mul_eq;
break;
case DIV_EQ:
t = octave_value::op_div_eq;
break;
case LEFTDIV_EQ:
t = octave_value::op_ldiv_eq;
break;
case POW_EQ:
t = octave_value::op_pow_eq;
break;
case EMUL_EQ:
t = octave_value::op_el_mul_eq;
break;
case EDIV_EQ:
t = octave_value::op_el_div_eq;
break;
case ELEFTDIV_EQ:
t = octave_value::op_el_ldiv_eq;
break;
case EPOW_EQ:
t = octave_value::op_el_pow_eq;
break;
case AND_EQ:
t = octave_value::op_el_and_eq;
break;
case OR_EQ:
t = octave_value::op_el_or_eq;
break;
default:
panic_impossible ();
break;
}
int l = eq_tok->line ();
int c = eq_tok->column ();
if (! lhs->is_simple_assign_lhs () && t != octave_value::op_asn_eq)
{
// Multiple assignments like [x,y] OP= rhs are only valid for
// '=', not '+=', etc.
delete lhs;
delete rhs;
bison_error ("computed multiple assignment not allowed",
eq_tok->beg_pos ());
return nullptr;
}
if (lhs->is_simple_assign_lhs ())
{
// We are looking at a simple assignment statement like x = rhs;
tree_expression *tmp = lhs->remove_front ();
if ((tmp->is_identifier () || tmp->is_index_expression ())
&& iskeyword (tmp->name ()))
{
std::string kw = tmp->name ();
delete tmp;
delete lhs;
delete rhs;
bison_error ("invalid assignment to keyword \"" + kw + "\"",
eq_tok->beg_pos ());
return nullptr;
}
delete lhs;
return new tree_simple_assignment (tmp, rhs, false, l, c, t);
}
else
{
std::list<std::string> names = lhs->variable_names ();
for (const auto& kw : names)
{
if (iskeyword (kw))
{
delete lhs;
delete rhs;
bison_error ("invalid assignment to keyword \"" + kw + "\"",
eq_tok->beg_pos ());
return nullptr;
}
}
return new tree_multi_assignment (lhs, rhs, false, l, c);
}
}
// Define a script.
void
base_parser::make_script (tree_statement_list *cmds,
tree_statement *end_script)
{
if (! cmds)
cmds = new tree_statement_list ();
cmds->append (end_script);
symbol_scope script_scope = m_lexer.m_symtab_context.curr_scope ();
script_scope.cache_name (m_lexer.m_fcn_file_full_name);
script_scope.cache_fcn_file_name (m_lexer.m_fcn_file_full_name);
script_scope.cache_dir_name (m_lexer.m_dir_name);
octave_user_script *script
= new octave_user_script (m_lexer.m_fcn_file_full_name,
m_lexer.m_fcn_file_name, script_scope,
cmds, m_lexer.m_help_text);
m_lexer.m_symtab_context.pop ();
m_lexer.m_help_text = "";
sys::time now;
script->stash_fcn_file_time (now);
script->stash_dir_name (m_lexer.m_dir_name);
m_primary_fcn = octave_value (script);
}
tree_identifier *
base_parser::make_fcn_name (tree_identifier *id)
{
std::string id_name = id->name ();
// Make classdef local functions unique from classdef methods.
if (m_parsing_local_functions && m_curr_fcn_depth == 0)
id_name = m_lexer.m_fcn_file_name + ">" + id_name;
if (! m_function_scopes.name_current_scope (id_name))
{
bison_error ("duplicate subfunction or nested function name",
id->line (), id->column ());
delete id;
return nullptr;
}
octave::symbol_scope curr_scope = m_lexer.m_symtab_context.curr_scope ();
curr_scope.cache_name (id_name);
m_lexer.m_parsed_function_name.top () = true;
m_lexer.m_maybe_classdef_get_set_method = false;
return id;
}
// Define a function.
// FIXME: combining start_function, finish_function, and
// recover_from_parsing_function should be possible, but it makes
// for a large mess. Maybe this could be a bit better organized?
tree_function_def *
base_parser::make_function (token *fcn_tok,
tree_parameter_list *ret_list,
tree_identifier *id,
tree_parameter_list *param_list,
tree_statement_list *body,
tree_statement *end_fcn_stmt,
comment_list *lc)
{
int l = fcn_tok->line ();
int c = fcn_tok->column ();
octave_user_function *tmp_fcn
= start_function (id, param_list, body, end_fcn_stmt);
tree_function_def *retval = finish_function (ret_list, tmp_fcn, lc, l, c);
recover_from_parsing_function ();
return retval;
}
// Begin defining a function.
octave_user_function *
base_parser::start_function (tree_identifier *id,
tree_parameter_list *param_list,
tree_statement_list *body,
tree_statement *end_fcn_stmt)
{
// We'll fill in the return list later.
std::string id_name = id->name ();
delete id;
if (m_lexer.m_parsing_classdef_get_method)
id_name.insert (0, "get.");
else if (m_lexer.m_parsing_classdef_set_method)
id_name.insert (0, "set.");
m_lexer.m_parsing_classdef_get_method = false;
m_lexer.m_parsing_classdef_set_method = false;
if (! body)
body = new tree_statement_list ();
body->append (end_fcn_stmt);
octave_user_function *fcn
= new octave_user_function (m_lexer.m_symtab_context.curr_scope (),
param_list, nullptr, body);
comment_list *tc = m_lexer.m_comment_buf.get_comment ();
fcn->stash_trailing_comment (tc);
fcn->stash_fcn_end_location (end_fcn_stmt->line (),
end_fcn_stmt->column ());
// If input is coming from a file, issue a warning if the name of
// the file does not match the name of the function stated in the
// file. Matlab doesn't provide a diagnostic (it ignores the stated
// name).
if (! m_autoloading && m_lexer.m_reading_fcn_file
&& m_curr_fcn_depth == 0 && ! m_parsing_subfunctions)
{
// FIXME: should m_lexer.m_fcn_file_name already be
// preprocessed when we get here? It seems to only be a
// problem with relative filenames.
std::string nm = m_lexer.m_fcn_file_name;
size_t pos = nm.find_last_of (sys::file_ops::dir_sep_chars ());
if (pos != std::string::npos)
nm = m_lexer.m_fcn_file_name.substr (pos+1);
if (nm != id_name)
{
warning_with_id
("Octave:function-name-clash",
"function name '%s' does not agree with function filename '%s'",
id_name.c_str (), m_lexer.m_fcn_file_full_name.c_str ());
id_name = nm;
}
}
sys::time now;
fcn->stash_fcn_file_name (m_lexer.m_fcn_file_full_name);
fcn->stash_fcn_file_time (now);
fcn->stash_dir_name (m_lexer.m_dir_name);
fcn->stash_package_name (m_lexer.m_package_name);
fcn->mark_as_system_fcn_file ();
fcn->stash_function_name (id_name);
if (m_lexer.m_reading_fcn_file || m_lexer.m_reading_classdef_file || m_autoloading)
{
if (m_fcn_file_from_relative_lookup)
fcn->mark_relative ();
if (m_lexer.m_parsing_class_method)
{
if (m_lexer.m_parsing_classdef)
{
if (m_curr_class_name == id_name)
fcn->mark_as_classdef_constructor ();
else
fcn->mark_as_classdef_method ();
}
else
{
if (m_curr_class_name == id_name)
fcn->mark_as_legacy_constructor ();
else
fcn->mark_as_legacy_method ();
}
fcn->stash_dispatch_class (m_curr_class_name);
}
std::string nm = fcn->fcn_file_name ();
sys::file_stat fs (nm);
if (fs && fs.is_newer (now))
warning_with_id ("Octave:future-time-stamp",
"time stamp for '%s' is in the future", nm.c_str ());
}
else if (! m_lexer.input_from_tmp_history_file ()
&& ! m_lexer.m_force_script
&& m_lexer.m_reading_script_file
&& m_lexer.m_fcn_file_name == id_name)
{
warning ("function '%s' defined within script file '%s'",
id_name.c_str (), m_lexer.m_fcn_file_full_name.c_str ());
}
// Record help text for functions other than nested functions.
// We cannot currently record help for nested functions (bug #46008)
// because the doc_string of the outermost function is read first,
// whereas this function is called for the innermost function first.
// We could have a stack of help_text in lexer.
if (! m_lexer.m_help_text.empty () && m_curr_fcn_depth == 0)
{
fcn->document (m_lexer.m_help_text);
m_lexer.m_help_text = "";
}
if (m_lexer.m_reading_fcn_file && m_curr_fcn_depth == 0
&& ! m_parsing_subfunctions)
m_primary_fcn = octave_value (fcn);
return fcn;
}
tree_statement *
base_parser::make_end (const std::string& type, bool eof,
const filepos& beg_pos, const filepos& /*end_pos*/)
{
int l = beg_pos.line ();
int c = beg_pos.column ();
return make_statement (new tree_no_op_command (type, eof, l, c));
}
tree_function_def *
base_parser::finish_function (tree_parameter_list *ret_list,
octave_user_function *fcn,
comment_list *lc,
int l, int c)
{
tree_function_def *retval = nullptr;
if (! ret_list)
ret_list = new tree_parameter_list (tree_parameter_list::out);
ret_list->mark_as_formal_parameters ();
if (fcn)
{
std::string fcn_nm = fcn->name ();
std::string file = fcn->fcn_file_name ();
std::string tmp = fcn_nm;
if (! file.empty ())
tmp += ": " + file;
symbol_scope fcn_scope = fcn->scope ();
fcn_scope.cache_name (tmp);
fcn_scope.cache_fcn_name (fcn_nm);
fcn_scope.cache_fcn_file_name (file);
fcn_scope.cache_dir_name (m_lexer.m_dir_name);
if (lc)
fcn->stash_leading_comment (lc);
fcn->define_ret_list (ret_list);
if (m_curr_fcn_depth > 0 || m_parsing_subfunctions)
{
fcn->stash_fcn_location (l, c);
fcn->stash_parent_fcn_name (m_lexer.m_fcn_file_name);
octave_value ov_fcn (fcn);
if (m_endfunction_found && m_function_scopes.size () > 1)
{
fcn->mark_as_nested_function ();
fcn_scope.set_nesting_depth (m_curr_fcn_depth);
symbol_scope pscope = m_function_scopes.parent_scope ();
fcn_scope.set_parent (pscope);
fcn_scope.set_primary_parent (m_primary_fcn_scope);
pscope.install_nestfunction (fcn_nm, ov_fcn, fcn_scope);
// For nested functions, the list of parent functions is
// set in symbol_scope::update_nest.
}
else
{
fcn->mark_as_subfunction ();
m_subfunction_names.push_back (fcn_nm);
fcn_scope.set_parent (m_primary_fcn_scope);
if (m_parsing_subfunctions)
fcn_scope.set_primary_parent (m_primary_fcn_scope);
m_primary_fcn_scope.install_subfunction (fcn_nm, ov_fcn);
// Prepend name of primary fucntion to list of parent
// functions (if any) for subfunction.
std::list<std::string> plst
= fcn_scope.parent_fcn_names ();
plst.push_front (m_primary_fcn_scope.fcn_name ());
fcn_scope.cache_parent_fcn_names (plst);
}
}
if (m_curr_fcn_depth == 0)
fcn_scope.update_nest ();
if (! m_lexer.m_reading_fcn_file && m_curr_fcn_depth == 0)
{
// We are either reading a script file or defining a function
// at the command line, so this definition creates a
// tree_function object that is placed in the parse tree.
// Otherwise, it is just inserted in the symbol table,
// either as a subfunction or nested function (see above),
// or as the primary function for the file, via
// m_primary_fcn (see also load_fcn_from_file,,
// parse_fcn_file, and
// fcn_info::fcn_info_rep::find_user_function).
if (m_lexer.m_buffer_function_text)
{
fcn->cache_function_text (m_lexer.m_function_text,
fcn->time_parsed ());
m_lexer.m_buffer_function_text = false;
}
retval = new tree_function_def (fcn);
}
}
return retval;
}
void
base_parser::recover_from_parsing_function (void)
{
m_lexer.m_symtab_context.pop ();
if (m_lexer.m_reading_fcn_file && m_curr_fcn_depth == 0
&& ! m_parsing_subfunctions)
m_parsing_subfunctions = true;
m_curr_fcn_depth--;
m_function_scopes.pop ();
m_lexer.m_defining_func--;
m_lexer.m_parsed_function_name.pop ();
m_lexer.m_looking_at_return_list = false;
m_lexer.m_looking_at_parameter_list = false;
}
// A CLASSDEF block defines a class that has a constructor and other
// methods, but it is not an executable command. Parsing the block
// makes some changes in the symbol table (inserting the constructor
// and methods, and adding to the list of known objects) and creates
// a parse tree containing meta information about the class.
// LC contains comments appearing before the classdef keyword.
// TC contains comments appearing between the classdef elements
// and the final end token for the classdef block.
tree_classdef *
base_parser::make_classdef (token *tok_val,
tree_classdef_attribute_list *a,
tree_identifier *id,
tree_classdef_superclass_list *sc,
tree_classdef_body *body, token *end_tok,
comment_list *lc, comment_list *tc)
{
tree_classdef *retval = nullptr;
m_lexer.m_symtab_context.pop ();
std::string cls_name = id->name ();
std::string nm = m_lexer.m_fcn_file_name;
size_t pos = nm.find_last_of (sys::file_ops::dir_sep_chars ());
if (pos != std::string::npos)
nm = m_lexer.m_fcn_file_name.substr (pos+1);
if (nm != cls_name)
{
int l = id->line ();
int c = id->column ();
delete a;
delete id;
delete sc;
delete body;
delete lc;
delete tc;
bison_error ("invalid classdef definition, the class name must match the filename", l, c);
}
else
{
if (end_token_ok (end_tok, token::classdef_end))
{
int l = tok_val->line ();
int c = tok_val->column ();
if (! body)
body = new tree_classdef_body ();
retval = new tree_classdef (m_lexer.m_symtab_context.curr_scope (),
a, id, sc, body, lc, tc,
m_curr_package_name, l, c);
}
else
{
delete a;
delete id;
delete sc;
delete body;
delete lc;
delete tc;
end_token_error (end_tok, token::switch_end);
}
}
return retval;
}
// LC contains comments appearing before the properties keyword.
// If this properties block appears first in the list of classdef
// elements, this comment list will be used for the help text for the
// classdef block.
// TC contains comments appearing between the list of properties
// and the final end token for the properties block and may be used to
// find the doc string for the final property in the list.
tree_classdef_properties_block *
base_parser::make_classdef_properties_block (token *tok_val,
tree_classdef_attribute_list *a,
tree_classdef_property_list *plist,
token *end_tok,
comment_list *lc,
comment_list *tc)
{
tree_classdef_properties_block *retval = nullptr;
if (end_token_ok (end_tok, token::properties_end))
{
int l = tok_val->line ();
int c = tok_val->column ();
if (plist)
{
// If the element at the end of the list doesn't have a doc
// string, see whether the first element of TC is an
// end-of-line comment for us to use.
if (tc)
{
tree_classdef_property *last_elt = plist->back ();
if (! last_elt->have_doc_string ())
{
comment_elt first_comment_elt = tc->front ();
if (first_comment_elt.is_end_of_line ())
{
std::string eol_comment = first_comment_elt.text ();
last_elt->doc_string (eol_comment);
}
}
}
}
else
plist = new tree_classdef_property_list ();
retval = new tree_classdef_properties_block (a, plist, lc, tc, l, c);
}
else
{
delete a;
delete plist;
delete lc;
delete tc;
end_token_error (end_tok, token::properties_end);
}
return retval;
}
// LC contains comments appearing before the methods keyword.
// If this methods block appears first in the list of classdef
// elements, this comment list will be used for the help text for the
// classdef block.
tree_classdef_methods_block *
base_parser::make_classdef_methods_block (token *tok_val,
tree_classdef_attribute_list *a,
tree_classdef_methods_list *mlist,
token *end_tok, comment_list *lc,
comment_list *tc)
{
tree_classdef_methods_block *retval = nullptr;
if (end_token_ok (end_tok, token::methods_end))
{
int l = tok_val->line ();
int c = tok_val->column ();
if (! mlist)
mlist = new tree_classdef_methods_list ();
retval = new tree_classdef_methods_block (a, mlist, lc, tc, l, c);
}
else
{
delete a;
delete mlist;
delete lc;
delete tc;
end_token_error (end_tok, token::methods_end);
}
return retval;
}
// LC contains comments appearing before the events keyword.
// If this events block appears first in the list of classdef
// elements, this comment list will be used for the help text for the
// classdef block.
// TC contains comments appearing between the list of events and
// the final end token for the events block and may be used to find
// the doc string for the final event in the list.
tree_classdef_events_block *
base_parser::make_classdef_events_block (token *tok_val,
tree_classdef_attribute_list *a,
tree_classdef_events_list *elist,
token *end_tok,
comment_list *lc,
comment_list *tc)
{
tree_classdef_events_block *retval = nullptr;
if (end_token_ok (end_tok, token::events_end))
{
int l = tok_val->line ();
int c = tok_val->column ();
if (! elist)
elist = new tree_classdef_events_list ();
retval = new tree_classdef_events_block (a, elist, lc, tc, l, c);
}
else
{
delete a;
delete elist;
delete lc;
delete tc;
end_token_error (end_tok, token::events_end);
}
return retval;
}
// LC contains comments appearing before the enumeration keyword.
// If this enumeration block appears first in the list of classdef
// elements, this comment list will be used for the help text for the
// classdef block.
// TC contains comments appearing between the list of
// enumerations and the final end token for the enumeration block and
// may be used to find the doc string for the final enumeration in the
// list.
tree_classdef_enum_block *
base_parser::make_classdef_enum_block (token *tok_val,
tree_classdef_attribute_list *a,
tree_classdef_enum_list *elist,
token *end_tok,
comment_list *lc,
comment_list *tc)
{
tree_classdef_enum_block *retval = nullptr;
if (end_token_ok (end_tok, token::enumeration_end))
{
int l = tok_val->line ();
int c = tok_val->column ();
if (! elist)
elist = new tree_classdef_enum_list ();
retval = new tree_classdef_enum_block (a, elist, lc, tc, l, c);
}
else
{
delete a;
delete elist;
delete lc;
delete tc;
end_token_error (end_tok, token::enumeration_end);
}
return retval;
}
octave_user_function*
base_parser::start_classdef_external_method (tree_identifier *id,
tree_parameter_list *pl)
{
octave_user_function* retval = nullptr;
// External methods are only allowed within @-folders. In this case,
// m_curr_class_name will be non-empty.
if (! m_curr_class_name.empty ())
{
std::string mname = id->name ();
// Methods that cannot be declared outside the classdef file:
// - methods with '.' character (e.g. property accessors)
// - class constructor
// - 'delete'
if (mname.find_first_of (".") == std::string::npos
&& mname != "delete"
&& mname != m_curr_class_name)
{
// Create a dummy function that is used until the real method
// is loaded.
retval = new octave_user_function (symbol_scope (), pl);
retval->stash_function_name (mname);
int l = id->line ();
int c = id->column ();
retval->stash_fcn_location (l, c);
}
else
bison_error ("invalid external method declaration, an external "
"method cannot be the class constructor, 'delete' "
"or have a dot (.) character in its name");
}
else
bison_error ("external methods are only allowed in @-folders");
if (! retval)
delete id;
return retval;
}
tree_function_def *
base_parser::finish_classdef_external_method (octave_user_function *fcn,
tree_parameter_list *ret_list,
comment_list *cl)
{
if (! ret_list)
ret_list = new tree_parameter_list (tree_parameter_list::out);
fcn->define_ret_list (ret_list);
if (cl)
fcn->stash_leading_comment (cl);
int l = fcn->beginning_line ();
int c = fcn->beginning_column ();
return new tree_function_def (fcn, l, c);
}
void
base_parser::finish_classdef_file (tree_classdef *cls,
tree_statement_list *local_fcns)
{
if (m_lexer.m_reading_classdef_file)
m_classdef_object = std::shared_ptr<tree_classdef> (cls);
if (local_fcns)
{
symbol_table& symtab
= __get_symbol_table__ ("base_parser::finish_classdef_file");
for (tree_statement *elt : *local_fcns)
{
tree_command *cmd = elt->command ();
tree_function_def *fcn_def
= dynamic_cast<tree_function_def *> (cmd);
octave_value ov_fcn = fcn_def->function ();
octave_function *fcn = ov_fcn.function_value ();
std::string nm = fcn->name ();
std::string file = fcn->fcn_file_name ();
symtab.install_local_function (nm, ov_fcn, file);
}
delete local_fcns;
}
}
// Make an index expression.
tree_index_expression *
base_parser::make_index_expression (tree_expression *expr,
tree_argument_list *args,
char type)
{
tree_index_expression *retval = nullptr;
if (args && args->has_magic_tilde ())
{
delete expr;
delete args;
bison_error ("invalid use of empty argument (~) in index expression");
}
else
{
int l = expr->line ();
int c = expr->column ();
if (! expr->is_postfix_indexed ())
expr->set_postfix_index (type);
if (expr->is_index_expression ())
{
tree_index_expression *tmp
= dynamic_cast<tree_index_expression *> (expr);
tmp->append (args, type);
retval = tmp;
}
else
retval = new tree_index_expression (expr, args, l, c, type);
}
return retval;
}
// Make an indirect reference expression.
tree_index_expression *
base_parser::make_indirect_ref (tree_expression *expr,
const std::string& elt)
{
tree_index_expression *retval = nullptr;
int l = expr->line ();
int c = expr->column ();
if (! expr->is_postfix_indexed ())
expr->set_postfix_index ('.');
if (expr->is_index_expression ())
{
tree_index_expression *tmp
= dynamic_cast<tree_index_expression *> (expr);
tmp->append (elt);
retval = tmp;
}
else
retval = new tree_index_expression (expr, elt, l, c);
m_lexer.m_looking_at_indirect_ref = false;
return retval;
}
// Make an indirect reference expression with dynamic field name.
tree_index_expression *
base_parser::make_indirect_ref (tree_expression *expr,
tree_expression *elt)
{
tree_index_expression *retval = nullptr;
int l = expr->line ();
int c = expr->column ();
if (! expr->is_postfix_indexed ())
expr->set_postfix_index ('.');
if (expr->is_index_expression ())
{
tree_index_expression *tmp
= dynamic_cast<tree_index_expression *> (expr);
tmp->append (elt);
retval = tmp;
}
else
retval = new tree_index_expression (expr, elt, l, c);
m_lexer.m_looking_at_indirect_ref = false;
return retval;
}
// Make a declaration command.
tree_decl_command *
base_parser::make_decl_command (int tok, token *tok_val,
tree_decl_init_list *lst)
{
tree_decl_command *retval = nullptr;
int l = tok_val->line ();
int c = tok_val->column ();
if (lst)
m_lexer.mark_as_variables (lst->variable_names ());
switch (tok)
{
case GLOBAL:
{
retval = new tree_decl_command ("global", lst, l, c);
retval->mark_global ();
}
break;
case PERSISTENT:
if (m_curr_fcn_depth >= 0)
{
retval = new tree_decl_command ("persistent", lst, l, c);
retval->mark_persistent ();
}
else
{
if (m_lexer.m_reading_script_file)
warning ("ignoring persistent declaration near line %d of file '%s'",
l, m_lexer.m_fcn_file_full_name.c_str ());
else
warning ("ignoring persistent declaration near line %d", l);
}
break;
default:
panic_impossible ();
break;
}
return retval;
}
bool
base_parser::validate_param_list (tree_parameter_list *lst,
tree_parameter_list::in_or_out type)
{
std::set<std::string> dict;
for (tree_decl_elt *elt : *lst)
{
tree_identifier *id = elt->ident ();
if (id)
{
std::string name = id->name ();
if (id->is_black_hole ())
{
if (type != tree_parameter_list::in)
{
bison_error ("invalid use of ~ in output list");
return false;
}
}
else if (dict.find (name) != dict.end ())
{
bison_error ("'" + name
+ "' appears more than once in parameter list");
return false;
}
else
dict.insert (name);
}
}
std::string va_type = (type == tree_parameter_list::in
? "varargin" : "varargout");
size_t len = lst->length ();
if (len > 0)
{
tree_decl_elt *elt = lst->back ();
tree_identifier *id = elt->ident ();
if (id && id->name () == va_type)
{
if (len == 1)
lst->mark_varargs_only ();
else
lst->mark_varargs ();
tree_parameter_list::iterator p = lst->end ();
--p;
delete *p;
lst->erase (p);
}
}
return true;
}
bool
base_parser::validate_array_list (tree_expression *e)
{
bool retval = true;
tree_array_list *al = dynamic_cast<tree_array_list *> (e);
for (tree_argument_list* row : *al)
{
if (row && row->has_magic_tilde ())
{
retval = false;
if (e->is_matrix ())
bison_error ("invalid use of tilde (~) in matrix expression");
else
bison_error ("invalid use of tilde (~) in cell expression");
break;
}
}
return retval;
}
tree_argument_list *
base_parser::validate_matrix_for_assignment (tree_expression *e)
{
tree_argument_list *retval = nullptr;
if (e->is_constant ())
{
tree_evaluator& tw
= __get_evaluator__ ("validate_matrix_for_assignment");
octave_value ov = e->evaluate (tw);
delete e;
if (ov.isempty ())
bison_error ("invalid empty left hand side of assignment");
else
bison_error ("invalid constant left hand side of assignment");
}
else
{
bool is_simple_assign = true;
tree_argument_list *tmp = nullptr;
if (e->is_matrix ())
{
tree_matrix *mat = dynamic_cast<tree_matrix *> (e);
if (mat && mat->size () == 1)
{
tmp = mat->front ();
mat->pop_front ();
delete e;
is_simple_assign = false;
}
}
else
tmp = new tree_argument_list (e);
if (tmp && tmp->is_valid_lvalue_list ())
{
m_lexer.mark_as_variables (tmp->variable_names ());
retval = tmp;
}
else
{
delete tmp;
bison_error ("invalid left hand side of assignment");
}
if (retval && is_simple_assign)
retval->mark_as_simple_assign_lhs ();
}
return retval;
}
// Finish building an array_list.
tree_expression *
base_parser::finish_array_list (tree_array_list *array_list,
token */*open_delim*/, token *close_delim)
{
tree_expression *retval = array_list;
array_list->set_location (close_delim->line (), close_delim->column ());
if (array_list->all_elements_are_constant ())
{
interpreter& interp = __get_interpreter__ ("finish_array_list");
try
{
// If the evaluation generates a warning message, restore
// the previous value of last_warning_message and skip the
// conversion to a constant value.
unwind_protect frame;
error_system& es = interp.get_error_system ();
frame.add_method (es, &error_system::set_last_warning_message,
es.last_warning_message (""));
frame.add_method (es, &error_system::set_discard_warning_messages,
es.discard_warning_messages (true));
tree_evaluator& tw = interp.get_evaluator ();
octave_value tmp = array_list->evaluate (tw);
std::string msg = es.last_warning_message ();
if (msg.empty ())
{
tree_constant *tc_retval
= new tree_constant (tmp, close_delim->line (),
close_delim->column ());
std::ostringstream buf;
tree_print_code tpc (buf);
array_list->accept (tpc);
tc_retval->stash_original_text (buf.str ());
delete array_list;
retval = tc_retval;
}
}
catch (const execution_exception&)
{
interp.recover_from_exception ();
}
}
return retval;
}
// Finish building a matrix list.
tree_expression *
base_parser::finish_matrix (tree_matrix *m, token *open_delim,
token *close_delim)
{
return (m
? finish_array_list (m, open_delim, close_delim)
: new tree_constant (octave_null_matrix::instance,
close_delim->line (), close_delim->column ()));
}
// Finish building a cell list.
tree_expression *
base_parser::finish_cell (tree_cell *c, token *open_delim,
token *close_delim)
{
return (c
? finish_array_list (c, open_delim, close_delim)
: new tree_constant (octave_value (Cell ()),
close_delim->line (), close_delim->column ()));
}
tree_statement_list *
base_parser::set_stmt_print_flag (tree_statement_list *list,
char sep, bool warn_missing_semi)
{
tree_statement *tmp = list->back ();
switch (sep)
{
case ';':
tmp->set_print_flag (false);
break;
case 0:
case ',':
case '\n':
tmp->set_print_flag (true);
if (warn_missing_semi)
maybe_warn_missing_semi (list);
break;
default:
warning ("unrecognized separator type!");
break;
}
// Even if a statement is null, we add it to the list then remove it
// here so that the print flag is applied to the correct statement.
if (tmp->is_null_statement ())
{
list->pop_back ();
delete tmp;
}
return list;
}
// Finish building a statement.
template <typename T>
tree_statement *
base_parser::make_statement (T *arg)
{
comment_list *comment = m_lexer.get_comment ();
return new tree_statement (arg, comment);
}
tree_statement_list *
base_parser::make_statement_list (tree_statement *stmt)
{
return new tree_statement_list (stmt);
}
tree_statement_list *
base_parser::append_statement_list (tree_statement_list *list,
char sep, tree_statement *stmt,
bool warn_missing_semi)
{
set_stmt_print_flag (list, sep, warn_missing_semi);
list->append (stmt);
return list;
}
void
base_parser::disallow_command_syntax (void)
{
m_lexer.m_allow_command_syntax = false;
}
// FIXME: this function partially duplicates do_dbtype in debug.cc.
static std::string
get_file_line (const std::string& name, int line)
{
// NAME should be an absolute file name and the file should exist.
std::ifstream fs = sys::ifstream (name.c_str (), std::ios::in);
std::string text;
if (fs)
{
int i = 1;
do
{
if (! std::getline (fs, text))
{
text = "";
break;
}
}
while (i++ < line);
}
return text;
}
void
base_parser::bison_error (const std::string& str)
{
bison_error (str, m_lexer.m_filepos);
}
void
base_parser::bison_error (const std::string& str, const filepos& pos)
{
bison_error (str, pos.line (), pos.column ());
}
void
base_parser::bison_error (const std::string& str, int err_line, int err_col)
{
std::ostringstream output_buf;
if (m_lexer.m_reading_fcn_file || m_lexer.m_reading_script_file
|| m_lexer.m_reading_classdef_file)
output_buf << "parse error near line " << err_line
<< " of file " << m_lexer.m_fcn_file_full_name;
else
output_buf << "parse error:";
if (str != "parse error")
output_buf << "\n\n " << str;
output_buf << "\n\n";
std::string curr_line;
if (m_lexer.m_reading_fcn_file || m_lexer.m_reading_script_file
|| m_lexer.m_reading_classdef_file)
curr_line = get_file_line (m_lexer.m_fcn_file_full_name, err_line);
else
curr_line = m_lexer.m_current_input_line;
// Adjust the error column for display because it is 1-based in the
// lexer for easier reporting.
err_col--;
if (! curr_line.empty ())
{
// FIXME: we could do better if we just cached lines from the
// input file in a list. See also functions for managing input
// buffers in lex.ll.
size_t len = curr_line.length ();
if (curr_line[len-1] == '\n')
curr_line.resize (len-1);
// Print the line, maybe with a pointer near the error token.
output_buf << ">>> " << curr_line << "\n";
if (err_col == 0)
err_col = len;
for (int i = 0; i < err_col + 3; i++)
output_buf << " ";
output_buf << "^";
}
output_buf << "\n";
m_parse_error_msg = output_buf.str ();
}
int
parser::run (void)
{
int status = -1;
yypstate *pstate = static_cast<yypstate *> (m_parser_state);
try
{
status = octave_pull_parse (pstate, *this);
}
catch (const execution_exception&)
{
// FIXME: In previous versions, we emitted a parse error here
// but that is not always correct because the error could have
// happened inside a GUI callback functions executing in the
// readline event_hook loop. Maybe we need a separate exception
// class for parse errors?
throw;
}
catch (const exit_exception&)
{
throw;
}
catch (const interrupt_exception&)
{
throw;
}
catch (...)
{
std::string file = m_lexer.m_fcn_file_full_name;
if (file.empty ())
error ("unexpected exception while parsing input");
else
error ("unexpected exception while parsing %s", file.c_str ());
}
if (status != 0)
parse_error ("%s", m_parse_error_msg.c_str ());
return status;
}
// Parse input from INPUT. Pass TRUE for EOF if the end of INPUT should
// finish the parse.
int
push_parser::run (const std::string& input, bool eof)
{
int status = -1;
dynamic_cast<push_lexer&> (m_lexer).append_input (input, eof);
do
{
YYSTYPE lval;
int token = octave_lex (&lval, m_lexer.m_scanner);
if (token < 0)
{
// TOKEN == -2 means that the lexer recognized a comment
// and we should be at the end of the buffer but not the
// end of the file so we should return 0 to indicate
// "complete input" instead of -1 to request more input.
status = (token == -2 ? 0 : -1);
if (! eof && m_lexer.at_end_of_buffer ())
return status;
break;
}
yypstate *pstate = static_cast<yypstate *> (m_parser_state);
try
{
status = octave_push_parse (pstate, token, &lval, *this);
}
catch (execution_exception& e)
{
std::string file = m_lexer.m_fcn_file_full_name;
if (file.empty ())
error (e, "parse error");
else
error (e, "parse error in %s", file.c_str ());
}
catch (const exit_exception&)
{
throw;
}
catch (interrupt_exception &)
{
throw;
}
catch (...)
{
std::string file = m_lexer.m_fcn_file_full_name;
if (file.empty ())
error ("unexpected exception while parsing input");
else
error ("unexpected exception while parsing %s", file.c_str ());
}
}
while (status == YYPUSH_MORE || ! m_lexer.at_end_of_buffer ());
if (status != 0)
parse_error ("%s", m_parse_error_msg.c_str ());
return status;
}
int
push_parser::run (void)
{
if (! m_reader)
error ("push_parser::run requires valid input_reader");
int exit_status = 0;
input_system& input_sys = m_interpreter.get_input_system ();
std::string prompt
= command_editor::decode_prompt_string (input_sys.PS1 ());
do
{
// Reset status each time through the read loop so that
// it won't be set to -1 and cause us to exit the outer
// loop early if there is an exception while reading
// input or parsing.
exit_status = 0;
bool eof = false;
std::string input_line = m_reader->get_input (prompt, eof);
if (eof)
{
exit_status = EOF;
break;
}
exit_status = run (input_line, false);
prompt = command_editor::decode_prompt_string (input_sys.PS2 ());
}
while (exit_status < 0);
return exit_status;
}
octave_value
parse_fcn_file (interpreter& interp, const std::string& full_file,
const std::string& file, const std::string& dir_name,
const std::string& dispatch_type,
const std::string& package_name, bool require_file,
bool force_script, bool autoload, bool relative_lookup)
{
octave_value retval;
FILE *ffile = nullptr;
if (! full_file.empty ())
ffile = sys::fopen (full_file, "rb");
if (! ffile)
{
if (require_file)
error ("no such file, '%s'", full_file.c_str ());
return octave_value ();
}
unwind_action act ([ffile] (void)
{
fclose (ffile);
});
parser parser (ffile, interp);
parser.m_curr_class_name = dispatch_type;
parser.m_curr_package_name = package_name;
parser.m_autoloading = autoload;
parser.m_fcn_file_from_relative_lookup = relative_lookup;
parser.m_lexer.m_force_script = force_script;
parser.m_lexer.prep_for_file ();
parser.m_lexer.m_parsing_class_method = ! dispatch_type.empty ();
parser.m_lexer.m_fcn_file_name = file;
parser.m_lexer.m_fcn_file_full_name = full_file;
parser.m_lexer.m_dir_name = dir_name;
parser.m_lexer.m_package_name = package_name;
int err = parser.run ();
if (err)
error ("parse error while reading file %s", full_file.c_str ());
octave_value ov_fcn = parser.m_primary_fcn;
if (parser.m_lexer.m_reading_classdef_file
&& parser.classdef_object ())
{
// Convert parse tree for classdef object to
// meta.class info (and stash it in the symbol
// table?). Return pointer to constructor?
if (ov_fcn.is_defined ())
panic_impossible ();
bool is_at_folder = ! dispatch_type.empty ();
std::shared_ptr<tree_classdef> cdef_obj
= parser.classdef_object();
return cdef_obj->make_meta_class (interp, is_at_folder);
}
else if (ov_fcn.is_defined ())
{
octave_function *fcn = ov_fcn.function_value ();
fcn->maybe_relocate_end ();
if (parser.m_parsing_subfunctions)
{
if (! parser.m_endfunction_found)
parser.m_subfunction_names.reverse ();
fcn->stash_subfunction_names (parser.m_subfunction_names);
}
return ov_fcn;
}
return octave_value ();
}
// Maybe print a warning if an assignment expression is used as the
// test in a logical expression.
void
base_parser::maybe_warn_assign_as_truth_value (tree_expression *expr)
{
if (expr->is_assignment_expression ()
&& expr->paren_count () < 2)
{
if (m_lexer.m_fcn_file_full_name.empty ())
warning_with_id
("Octave:assign-as-truth-value",
"suggest parenthesis around assignment used as truth value");
else
warning_with_id
("Octave:assign-as-truth-value",
"suggest parenthesis around assignment used as truth value near line %d, column %d in file '%s'",
expr->line (), expr->column (), m_lexer.m_fcn_file_full_name.c_str ());
}
}
// Maybe print a warning about switch labels that aren't constants.
void
base_parser::maybe_warn_variable_switch_label (tree_expression *expr)
{
if (! expr->is_constant ())
{
if (m_lexer.m_fcn_file_full_name.empty ())
warning_with_id ("Octave:variable-switch-label",
"variable switch label");
else
warning_with_id
("Octave:variable-switch-label",
"variable switch label near line %d, column %d in file '%s'",
expr->line (), expr->column (), m_lexer.m_fcn_file_full_name.c_str ());
}
}
void
base_parser::maybe_warn_missing_semi (tree_statement_list *t)
{
if (m_curr_fcn_depth >= 0)
{
tree_statement *tmp = t->back ();
if (tmp->is_expression ())
warning_with_id
("Octave:missing-semicolon",
"missing semicolon near line %d, column %d in file '%s'",
tmp->line (), tmp->column (), m_lexer.m_fcn_file_full_name.c_str ());
}
}
std::string
get_help_from_file (const std::string& nm, bool& symbol_found,
std::string& full_file)
{
std::string retval;
full_file = fcn_file_in_path (nm);
std::string file = full_file;
size_t file_len = file.length ();
if ((file_len > 4 && file.substr (file_len-4) == ".oct")
|| (file_len > 4 && file.substr (file_len-4) == ".mex")
|| (file_len > 2 && file.substr (file_len-2) == ".m"))
{
file = sys::env::base_pathname (file);
file = file.substr (0, file.find_last_of ('.'));
size_t pos = file.find_last_of (sys::file_ops::dir_sep_str ());
if (pos != std::string::npos)
file = file.substr (pos+1);
}
if (! file.empty ())
{
interpreter& interp = __get_interpreter__ ("get_help_from_file");
symbol_found = true;
octave_value ov_fcn
= parse_fcn_file (interp, full_file, file, "", "", "", true,
false, false, false);
if (ov_fcn.is_defined ())
{
octave_function *fcn = ov_fcn.function_value ();
if (fcn)
retval = fcn->doc_string ();
}
}
return retval;
}
std::string
get_help_from_file (const std::string& nm, bool& symbol_found)
{
std::string file;
return get_help_from_file (nm, symbol_found, file);
}
octave_value
load_fcn_from_file (const std::string& file_name,
const std::string& dir_name,
const std::string& dispatch_type,
const std::string& package_name,
const std::string& fcn_name, bool autoload)
{
octave_value retval;
unwind_protect frame;
std::string nm = file_name;
size_t nm_len = nm.length ();
std::string file;
bool relative_lookup = false;
file = nm;
if ((nm_len > 4 && nm.substr (nm_len-4) == ".oct")
|| (nm_len > 4 && nm.substr (nm_len-4) == ".mex")
|| (nm_len > 2 && nm.substr (nm_len-2) == ".m"))
{
nm = sys::env::base_pathname (file);
nm = nm.substr (0, nm.find_last_of ('.'));
size_t pos = nm.find_last_of (sys::file_ops::dir_sep_str ());
if (pos != std::string::npos)
nm = nm.substr (pos+1);
}
relative_lookup = ! sys::env::absolute_pathname (file);
file = sys::env::make_absolute (file);
int len = file.length ();
interpreter& interp = __get_interpreter__ ("load_fcn_from_file");
dynamic_loader& dyn_loader = interp.get_dynamic_loader ();
if (len > 4 && file.substr (len-4, len-1) == ".oct")
{
if (autoload && ! fcn_name.empty ())
nm = fcn_name;
octave_function *tmpfcn
= dyn_loader.load_oct (nm, file, relative_lookup);
if (tmpfcn)
{
tmpfcn->stash_package_name (package_name);
retval = octave_value (tmpfcn);
}
}
else if (len > 4 && file.substr (len-4, len-1) == ".mex")
{
// Temporarily load m-file version of mex-file, if it exists,
// to get the help-string to use.
std::string doc_string;
octave_value ov_fcn
= parse_fcn_file (interp, file.substr (0, len - 2), nm, dir_name,
dispatch_type, package_name, false,
autoload, autoload, relative_lookup);
if (ov_fcn.is_defined ())
{
octave_function *tmpfcn = ov_fcn.function_value ();
if (tmpfcn)
doc_string = tmpfcn->doc_string ();
}
octave_function *tmpfcn
= dyn_loader.load_mex (nm, file, relative_lookup);
if (tmpfcn)
{
tmpfcn->document (doc_string);
tmpfcn->stash_package_name (package_name);
retval = octave_value (tmpfcn);
}
}
else if (len > 2)
{
retval = parse_fcn_file (interp, file, nm, dir_name,
dispatch_type, package_name, true,
autoload, autoload, relative_lookup);
}
return retval;
}
}
DEFMETHOD (autoload, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{autoload_map} =} autoload ()
@deftypefnx {} {} autoload (@var{function}, @var{file})
@deftypefnx {} {} autoload (@dots{}, "remove")
Define @var{function} to autoload from @var{file}.
The second argument, @var{file}, should be an absolute filename or a file
name in the same directory as the function or script from which the autoload
command was run. @var{file} @emph{should not} depend on the Octave load
path.
Normally, calls to @code{autoload} appear in PKG_ADD script files that are
evaluated when a directory is added to Octave's load path. To avoid having
to hardcode directory names in @var{file}, if @var{file} is in the same
directory as the PKG_ADD script then
@example
autoload ("foo", "bar.oct");
@end example
@noindent
will load the function @code{foo} from the file @code{bar.oct}. The above
usage when @code{bar.oct} is not in the same directory, or usages such as
@example
autoload ("foo", file_in_loadpath ("bar.oct"))
@end example
@noindent
are strongly discouraged, as their behavior may be unpredictable.
With no arguments, return a structure containing the current autoload map.
If a third argument @qcode{"remove"} is given, the function is cleared and
not loaded anymore during the current Octave session.
@seealso{PKG_ADD}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin == 1 || nargin > 3)
print_usage ();
octave::tree_evaluator& tw = interp.get_evaluator ();
if (nargin == 0)
return octave_value (tw.get_autoload_map ());
else
{
string_vector argv = args.make_argv ("autoload");
if (nargin == 2)
tw.add_autoload (argv[1], argv[2]);
else if (nargin == 3)
{
if (argv[3] != "remove")
error_with_id ("Octave:invalid-input-arg",
"autoload: third argument can only be 'remove'");
tw.remove_autoload (argv[1], argv[2]);
}
}
return octave_value_list ();
}
DEFMETHOD (mfilename, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} mfilename ()
@deftypefnx {} {} mfilename ("fullpath")
@deftypefnx {} {} mfilename ("fullpathext")
Return the name of the currently executing file.
The base name of the currently executing script or function is returned without
any extension. If called from outside an m-file, such as the command line,
return the empty string.
Given the argument @qcode{"fullpath"}, include the directory part of the
filename, but not the extension.
Given the argument @qcode{"fullpathext"}, include the directory part of
the filename and the extension.
@seealso{inputname, dbstack}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin > 1)
print_usage ();
std::string opt;
if (nargin == 1)
opt = args(0).xstring_value ("mfilename: option argument must be a string");
return octave_value (interp.mfilename (opt));
}
namespace octave
{
// Execute the contents of a script file. For compatibility with
// Matlab, also execute a function file by calling the function it
// defines with no arguments and nargout = 0.
void
source_file (const std::string& file_name, const std::string& context,
bool verbose, bool require_file)
{
interpreter& interp = __get_interpreter__ ("source_file");
interp.source_file (file_name, context, verbose, require_file);
}
}
DEFMETHOD (source, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} source (@var{file})
@deftypefnx {} {} source (@var{file}, @var{context})
Parse and execute the contents of @var{file}.
Without specifying @var{context}, this is equivalent to executing commands
from a script file, but without requiring the file to be named
@file{@var{file}.m} or to be on the execution path.
Instead of the current context, the script may be executed in either the
context of the function that called the present function
(@qcode{"caller"}), or the top-level context (@qcode{"base"}).
@seealso{run}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
std::string file_name
= args(0).xstring_value ("source: FILE must be a string");
std::string context;
if (nargin == 2)
context = args(1).xstring_value ("source: CONTEXT must be a string");
interp.source_file (file_name, context);
return octave_value_list ();
}
namespace octave
{
//! Evaluate an Octave function (built-in or interpreted) and return
//! the list of result values.
//!
//! @param name The name of the function to call.
//! @param args The arguments to the function.
//! @param nargout The number of output arguments expected.
//! @return A list of output values. The length of the list is not
//! necessarily the same as @c nargout.
octave_value_list
feval (const char *name, const octave_value_list& args, int nargout)
{
interpreter& interp = __get_interpreter__ ("feval");
return interp.feval (name, args, nargout);
}
octave_value_list
feval (const std::string& name, const octave_value_list& args, int nargout)
{
interpreter& interp = __get_interpreter__ ("feval");
return interp.feval (name, args, nargout);
}
octave_value_list
feval (octave_function *fcn, const octave_value_list& args, int nargout)
{
interpreter& interp = __get_interpreter__ ("feval");
return interp.feval (fcn, args, nargout);
}
octave_value_list
feval (const octave_value& val, const octave_value_list& args, int nargout)
{
interpreter& interp = __get_interpreter__ ("feval");
return interp.feval (val, args, nargout);
}
octave_value_list
feval (const octave_value_list& args, int nargout)
{
interpreter& interp = __get_interpreter__ ("feval");
return interp.feval (args, nargout);
}
}
DEFMETHOD (feval, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {} feval (@var{name}, @dots{})
Evaluate the function named @var{name}.
Any arguments after the first are passed as inputs to the named function.
For example,
@example
@group
feval ("acos", -1)
@result{} 3.1416
@end group
@end example
@noindent
calls the function @code{acos} with the argument @samp{-1}.
The function @code{feval} can also be used with function handles of any sort
(@pxref{Function Handles}). Historically, @code{feval} was the only way to
call user-supplied functions in strings, but function handles are now
preferred due to the cleaner syntax they offer. For example,
@example
@group
@var{f} = @@exp;
feval (@var{f}, 1)
@result{} 2.7183
@var{f} (1)
@result{} 2.7183
@end group
@end example
@noindent
are equivalent ways to call the function referred to by @var{f}. If it
cannot be predicted beforehand whether @var{f} is a function handle,
function name in a string, or inline function then @code{feval} can be used
instead.
@end deftypefn */)
{
if (args.length () == 0)
print_usage ();
return interp.feval (args, nargout);
}
DEFMETHOD (builtin, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {[@dots{}] =} builtin (@var{f}, @dots{})
Call the base function @var{f} even if @var{f} is overloaded to another
function for the given type signature.
This is normally useful when doing object-oriented programming and there is
a requirement to call one of Octave's base functions rather than the
overloaded one of a new class.
A trivial example which redefines the @code{sin} function to be the
@code{cos} function shows how @code{builtin} works.
@example
@group
sin (0)
@result{} 0
function y = sin (x), y = cos (x); endfunction
sin (0)
@result{} 1
builtin ("sin", 0)
@result{} 0
@end group
@end example
@end deftypefn */)
{
octave_value_list retval;
if (args.length () == 0)
print_usage ();
const std::string name (args(0).xstring_value ("builtin: function name (F) must be a string"));
octave::symbol_table& symtab = interp.get_symbol_table ();
octave_value fcn = symtab.builtin_find (name);
if (fcn.is_defined ())
retval = interp.feval (fcn.function_value (), args.splice (0, 1), nargout);
else
error ("builtin: lookup for symbol '%s' failed", name.c_str ());
return retval;
}
namespace octave
{
octave_value_list
eval_string (const std::string& eval_str, bool silent,
int& parse_status, int nargout)
{
interpreter& interp = __get_interpreter__ ("eval_string");
return interp.eval_string (eval_str, silent, parse_status, nargout);
}
octave_value
eval_string (const std::string& eval_str, bool silent, int& parse_status)
{
interpreter& interp = __get_interpreter__ ("eval_string");
return interp.eval_string (eval_str, silent, parse_status);
}
void
cleanup_statement_list (tree_statement_list **lst)
{
if (*lst)
{
delete *lst;
*lst = nullptr;
}
}
}
DEFMETHOD (eval, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {} eval (@var{try})
@deftypefnx {} {} eval (@var{try}, @var{catch})
Parse the string @var{try} and evaluate it as if it were an Octave
program.
If execution fails, evaluate the optional string @var{catch}.
The string @var{try} is evaluated in the current context, so any results
remain available after @code{eval} returns.
The following example creates the variable @var{A} with the approximate
value of 3.1416 in the current workspace.
@example
eval ("A = acos(-1);");
@end example
If an error occurs during the evaluation of @var{try} then the @var{catch}
string is evaluated, as the following example shows:
@example
@group
eval ('error ("This is a bad example");',
'printf ("This error occurred:\n%s\n", lasterr ());');
@print{} This error occurred:
This is a bad example
@end group
@end example
Programming Note: if you are only using @code{eval} as an error-capturing
mechanism, rather than for the execution of arbitrary code strings,
Consider using try/catch blocks or unwind_protect/unwind_protect_cleanup
blocks instead. These techniques have higher performance and don't
introduce the security considerations that the evaluation of arbitrary code
does.
@seealso{evalin, evalc, assignin, feval}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
if (! args(0).is_string () || args(0).rows () > 1 || args(0).ndims () != 2)
error ("eval: TRY must be a string");
std::string try_code = args(0).string_value ();
if (nargin == 1)
return interp.eval (try_code, nargout);
else
{
if (! args(1).is_string () || args(1).rows () > 1
|| args(1).ndims () != 2)
error ("eval: CATCH must be a string");
std::string catch_code = args(1).string_value ();
return interp.eval (try_code, catch_code, nargout);
}
}
/*
%!shared x
%! x = 1;
%!assert (eval ("x"), 1)
%!assert (eval ("x;"))
%!assert (eval ("x;"), 1)
%!test
%! y = eval ("x");
%! assert (y, 1);
%!test
%! y = eval ("x;");
%! assert (y, 1);
%!test
%! eval ("x = 1;");
%! assert (x,1);
%!test
%! eval ("flipud = 2;");
%! assert (flipud, 2);
%!function y = __f ()
%! eval ("flipud = 2;");
%! y = flipud;
%!endfunction
%!assert (__f(), 2)
%!test <*35645>
%! [a,] = gcd (1,2);
%! [a,b,] = gcd (1, 2);
## Can't assign to a keyword
%!error eval ("switch = 13;")
%!shared str
%! str = "disp ('hello');";
%! str(:,:,2) = str(:,:,1);
%!error <TRY must be a string> eval (1)
%!error <TRY must be a string> eval (['a';'b'])
%!error <TRY must be a string> eval (str)
%!error <CATCH must be a string> eval (str(:,:,1), 1)
%!error <CATCH must be a string> eval (str(:,:,1), ['a';'b'])
%!error <CATCH must be a string> eval (str(:,:,1), str)
*/
DEFMETHOD (assignin, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} assignin (@var{context}, @var{varname}, @var{value})
Assign @var{value} to @var{varname} in context @var{context}, which
may be either @qcode{"base"} or @qcode{"caller"}.
@seealso{evalin}
@end deftypefn */)
{
if (args.length () != 3)
print_usage ();
std::string context
= args(0).xstring_value ("assignin: CONTEXT must be a string");
std::string varname
= args(1).xstring_value ("assignin: VARNAME must be a string");
interp.assignin (context, varname, args(2));
return octave_value_list ();
}
/*
%!error assignin ("base", "switch", "13")
*/
DEFMETHOD (evalin, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {} evalin (@var{context}, @var{try})
@deftypefnx {} {} evalin (@var{context}, @var{try}, @var{catch})
Like @code{eval}, except that the expressions are evaluated in the context
@var{context}, which may be either @qcode{"caller"} or @qcode{"base"}.
@seealso{eval, assignin}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin < 2 || nargin > 3)
print_usage ();
std::string context
= args(0).xstring_value ("evalin: CONTEXT must be a string");
std::string try_code
= args(1).xstring_value ("evalin: TRY must be a string");
if (nargin == 3)
{
std::string catch_code
= args(2).xstring_value ("evalin: CATCH must be a string");
return interp.evalin (context, try_code, catch_code, nargout);
}
return interp.evalin (context, try_code, nargout);
}
DEFMETHOD (evalc, interp, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{s} =} evalc (@var{try})
@deftypefnx {} {@var{s} =} evalc (@var{try}, @var{catch})
Parse and evaluate the string @var{try} as if it were an Octave program,
while capturing the output into the return variable @var{s}.
If execution fails, evaluate the optional string @var{catch}.
This function behaves like @code{eval}, but any output or warning messages
which would normally be written to the console are captured and returned in
the string @var{s}.
The @code{diary} is disabled during the execution of this function. When
@code{system} is used, any output produced by external programs is
@emph{not} captured, unless their output is captured by the @code{system}
function itself.
@example
@group
s = evalc ("t = 42"), t
@result{} s = t = 42
@result{} t = 42
@end group
@end example
@seealso{eval, diary}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin == 0 || nargin > 2)
print_usage ();
// Flush pending output and redirect stdout/stderr to capturing
// buffer.
octave_stdout.flush ();
std::cerr.flush ();
std::stringbuf buffer;
std::streambuf *old_out_buf = octave_stdout.rdbuf (&buffer);
std::streambuf *old_err_buf = std::cerr.rdbuf (&buffer);
// Restore previous output buffers no matter how control exits this
// function. There's no need to flush here. That has already
// happened for the normal execution path. If an error happens during
// the eval, then the message is stored in the exception object and we
// will display it later, after the buffers have been restored.
octave::unwind_action act ([old_out_buf, old_err_buf] (void)
{
octave_stdout.rdbuf (old_out_buf);
std::cerr.rdbuf (old_err_buf);
});
// Call standard eval function.
int eval_nargout = std::max (0, nargout - 1);
octave_value_list retval = Feval (interp, args, eval_nargout);
// Make sure we capture all pending output.
octave_stdout.flush ();
std::cerr.flush ();
retval.prepend (buffer.str ());
return retval;
}
/*
%!test
%! [old_fmt, old_spacing] = format ();
%! unwind_protect
%! format short;
%! str = evalc ("1");
%! assert (str, "ans = 1\n");
%! unwind_protect_cleanup
%! format (old_fmt);
%! format (old_spacing);
%! end_unwind_protect
%!assert (evalc ("1;"), "")
%!test
%! [s, y] = evalc ("1");
%! assert (s, "");
%! assert (y, 1);
%!test
%! [s, y] = evalc ("1;");
%! assert (s, "");
%! assert (y, 1);
%!test
%! [old_fmt, old_spacing] = format ();
%! unwind_protect
%! format short;
%! str = evalc ("y = 2");
%! assert (str, "y = 2\n");
%! assert (y, 2);
%! unwind_protect_cleanup
%! format (old_fmt);
%! format (old_spacing);
%! end_unwind_protect
%!test
%! assert (evalc ("y = 3;"), "");
%! assert (y, 3);
%!test
%! [s, a, b] = evalc ("deal (1, 2)");
%! assert (s, "");
%! assert (a, 1);
%! assert (b, 2);
%!function [a, b] = __f_evalc ()
%! printf ("foo");
%! fprintf (stdout, "bar ");
%! disp (pi);
%! a = 1;
%! b = 2;
%!endfunction
%!test
%! [old_fmt, old_spacing] = format ();
%! unwind_protect
%! format short;
%! [s, a, b] = evalc ("__f_evalc ()");
%! assert (s, "foobar 3.1416\n");
%! assert (a, 1);
%! assert (b, 2);
%! unwind_protect_cleanup
%! format (old_fmt);
%! format (old_spacing);
%! end_unwind_protect
%!error <foo> (evalc ("error ('foo')"))
%!error <bar> (evalc ("error ('foo')", "error ('bar')"))
%!test
%! warning ("off", "quiet", "local");
%! str = evalc ("warning ('foo')");
%! assert (str(1:13), "warning: foo\n");
%!test
%! warning ("off", "quiet", "local");
%! str = evalc ("error ('foo')", "warning ('bar')");
%! assert (str(1:13), "warning: bar\n");
%!error evalc ("switch = 13;")
*/
DEFUN (__parser_debug_flag__, args, nargout,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{val} =} __parser_debug_flag__ ()
@deftypefnx {} {@var{old_val} =} __parser_debug_flag__ (@var{new_val})
Query or set the internal flag that determines whether Octave's parser
prints debug information as it processes an expression.
@seealso{__lexer_debug_flag__}
@end deftypefn */)
{
octave_value retval;
bool debug_flag = octave_debug;
retval = set_internal_variable (debug_flag, args, nargout,
"__parser_debug_flag__");
octave_debug = debug_flag;
return retval;
}
DEFMETHOD (__parse_file__, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} __parse_file__ (@var{file}, @var{verbose})
Undocumented internal function.
@end deftypefn */)
{
octave_value retval;
int nargin = args.length ();
if (nargin < 1 || nargin > 2)
print_usage ();
std::string file = args(0).xstring_value ("__parse_file__: expecting filename as argument");
std::string full_file
= octave::sys::file_ops::tilde_expand (file);
full_file = octave::sys::env::make_absolute (full_file);
std::string dir_name;
size_t file_len = file.length ();
if ((file_len > 4 && file.substr (file_len-4) == ".oct")
|| (file_len > 4 && file.substr (file_len-4) == ".mex")
|| (file_len > 2 && file.substr (file_len-2) == ".m"))
{
file = octave::sys::env::base_pathname (file);
file = file.substr (0, file.find_last_of ('.'));
size_t pos = file.find_last_of (octave::sys::file_ops::dir_sep_str ());
if (pos != std::string::npos)
{
dir_name = file.substr (0, pos);
file = file.substr (pos+1);
}
}
if (nargin == 2)
octave_stdout << "parsing " << full_file << std::endl;
octave_value ov_fcn
= parse_fcn_file (interp, full_file, file, dir_name, "", "", true,
false, false, false);
return retval;
}
|