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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 The CodeLite Team
// file name : ASFormatter.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* ASFormatter.cpp
*
* Copyright (C) 2006-2011 by Jim Pattee <jimp03@email.com>
* Copyright (C) 1998-2002 by Tal Davidson
* <http://www.gnu.org/licenses/lgpl-3.0.html>
*
* This file is a part of Artistic Style - an indentation and
* reformatting tool for C, C++, C# and Java source files.
* <http://astyle.sourceforge.net>
*
* Artistic Style is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Artistic Style 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Artistic Style. If not, see <http://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
#include "astyle.h"
#include <algorithm>
#include <fstream>
#include <iostream>
namespace astyle
{
/**
* Constructor of ASFormatter
*/
ASFormatter::ASFormatter()
{
sourceIterator = NULL;
enhancer = new ASEnhancer;
preBracketHeaderStack = NULL;
bracketTypeStack = NULL;
parenStack = NULL;
structStack = NULL;
lineCommentNoIndent = false;
formattingStyle = STYLE_NONE;
bracketFormatMode = NONE_MODE;
pointerAlignment = PTR_ALIGN_NONE;
referenceAlignment = REF_SAME_AS_PTR;
lineEnd = LINEEND_DEFAULT;
maxCodeLength = string::npos;
shouldPadOperators = false;
shouldPadParensOutside = false;
shouldPadFirstParen = false;
shouldPadParensInside = false;
shouldPadHeader = false;
shouldUnPadParens = false;
shouldAttachClosingBracket = false;
shouldBreakOneLineBlocks = true;
shouldBreakOneLineStatements = true;
shouldConvertTabs = false;
shouldIndentCol1Comments = false;
shouldCloseTemplates = false;
shouldBreakBlocks = false;
shouldBreakClosingHeaderBlocks = false;
shouldBreakClosingHeaderBrackets = false;
shouldDeleteEmptyLines = false;
shouldBreakElseIfs = false;
shouldBreakLineAfterLogical = false;
shouldAddBrackets = false;
shouldAddOneLineBrackets = false;
// initialize ASFormatter member vectors
formatterFileType = 9; // reset to an invalid type
headers = new vector<const string*>;
nonParenHeaders = new vector<const string*>;
preDefinitionHeaders = new vector<const string*>;
preCommandHeaders = new vector<const string*>;
operators = new vector<const string*>;
assignmentOperators = new vector<const string*>;
castOperators = new vector<const string*>;
}
/**
* Destructor of ASFormatter
*/
ASFormatter::~ASFormatter()
{
// delete ASFormatter stack vectors
deleteContainer(preBracketHeaderStack);
deleteContainer(bracketTypeStack);
deleteContainer(parenStack);
deleteContainer(structStack);
// delete ASFormatter member vectors
formatterFileType = 9; // reset to an invalid type
delete headers;
delete nonParenHeaders;
delete preDefinitionHeaders;
delete preCommandHeaders;
delete operators;
delete assignmentOperators;
delete castOperators;
// delete ASBeautifier member vectors
// must be done when the ASFormatter object is deleted (not ASBeautifier)
ASBeautifier::deleteBeautifierVectors();
delete enhancer;
}
/**
* initialize the ASFormatter.
*
* init() should be called every time a ASFormatter object is to start
* formatting a NEW source file.
* init() recieves a pointer to a ASSourceIterator object that will be
* used to iterate through the source code.
*
* @param sourceIterator a pointer to the ASSourceIterator or ASStreamIterator object.
*/
void ASFormatter::init(ASSourceIterator* si)
{
buildLanguageVectors();
fixOptionVariableConflicts();
ASBeautifier::init(si);
enhancer->init(getFileType(),
getIndentLength(),
getTabLength(),
getIndentString() == "\t" ? true : false,
getForceTabIndentation(),
getCaseIndent(),
getPreprocessorIndent(),
getEmptyLineFill());
sourceIterator = si;
initContainer(preBracketHeaderStack, new vector<const string*>);
initContainer(parenStack, new vector<int>);
initContainer(structStack, new vector<bool>);
parenStack->push_back(0); // parenStack must contain this default entry
initContainer(bracketTypeStack, new vector<BracketType>);
bracketTypeStack->push_back(NULL_TYPE); // bracketTypeStack must contain this default entry
clearFormattedLineSplitPoints();
currentHeader = NULL;
currentLine = "";
readyFormattedLine = "";
formattedLine = "";
currentChar = ' ';
previousChar = ' ';
previousCommandChar = ' ';
previousNonWSChar = ' ';
quoteChar = '"';
charNum = 0;
checksumIn = 0;
checksumOut = 0;
currentLineFirstBracketNum = string::npos;
formattedLineCommentNum = 0;
leadingSpaces = 0;
previousReadyFormattedLineLength = string::npos;
preprocBracketTypeStackSize = 0;
spacePadNum = 0;
nextLineSpacePadNum = 0;
templateDepth = 0;
traceLineNumber = 0;
horstmannIndentChars = 0;
tabIncrementIn = 0;
previousBracketType = NULL_TYPE;
previousOperator = NULL;
isVirgin = true;
isInLineComment = false;
isInComment = false;
isInCommentStartLine = false;
noTrimCommentContinuation = false;
isInPreprocessor = false;
isInPreprocessorBeautify = false;
doesLineStartComment = false;
lineEndsInCommentOnly = false;
lineIsLineCommentOnly = false;
lineIsEmpty = false;
isImmediatelyPostCommentOnly = false;
isImmediatelyPostEmptyLine = false;
isInQuote = false;
isInVerbatimQuote = false;
haveLineContinuationChar = false;
isInQuoteContinuation = false;
isInBlParen = false;
isSpecialChar = false;
isNonParenHeader = false;
foundNamespaceHeader = false;
foundClassHeader = false;
foundStructHeader = false;
foundInterfaceHeader = false;
foundPreDefinitionHeader = false;
foundPreCommandHeader = false;
foundCastOperator = false;
foundQuestionMark = false;
isInLineBreak = false;
endOfAsmReached = false;
endOfCodeReached = false;
isInEnum = false;
isInExecSQL = false;
isInAsm = false;
isInAsmOneLine = false;
isInAsmBlock = false;
isLineReady = false;
isPreviousBracketBlockRelated = false;
isInPotentialCalculation = false;
shouldReparseCurrentChar = false;
needHeaderOpeningBracket = false;
shouldBreakLineAtNextChar = false;
shouldKeepLineUnbroken = false;
passedSemicolon = false;
passedColon = false;
isImmediatelyPostNonInStmt = false;
isCharImmediatelyPostNonInStmt = false;
isInTemplate = false;
isImmediatelyPostComment = false;
isImmediatelyPostLineComment = false;
isImmediatelyPostEmptyBlock = false;
isImmediatelyPostPreprocessor = false;
isImmediatelyPostReturn = false;
isImmediatelyPostThrow = false;
isImmediatelyPostOperator = false;
isImmediatelyPostTemplate = false;
isImmediatelyPostPointerOrReference = false;
isCharImmediatelyPostReturn = false;
isCharImmediatelyPostThrow = false;
isCharImmediatelyPostOperator = false;
isCharImmediatelyPostComment = false;
isPreviousCharPostComment = false;
isCharImmediatelyPostLineComment = false;
isCharImmediatelyPostOpenBlock = false;
isCharImmediatelyPostCloseBlock = false;
isCharImmediatelyPostTemplate = false;
isCharImmediatelyPostPointerOrReference = false;
breakCurrentOneLineBlock = false;
isInHorstmannRunIn = false;
currentLineBeginsWithBracket = false;
isPrependPostBlockEmptyLineRequested = false;
isAppendPostBlockEmptyLineRequested = false;
prependEmptyLine = false;
appendOpeningBracket = false;
foundClosingHeader = false;
isImmediatelyPostHeader = false;
isInHeader = false;
isInCase = false;
isJavaStaticConstructor = false;
}
/**
* build vectors for each programing language
* depending on the file extension.
*/
void ASFormatter::buildLanguageVectors()
{
if (getFileType() == formatterFileType) // don't build unless necessary
return;
formatterFileType = getFileType();
headers->clear();
nonParenHeaders->clear();
preDefinitionHeaders->clear();
preCommandHeaders->clear();
operators->clear();
assignmentOperators->clear();
castOperators->clear();
ASResource::buildHeaders(headers, getFileType());
ASResource::buildNonParenHeaders(nonParenHeaders, getFileType());
ASResource::buildPreDefinitionHeaders(preDefinitionHeaders, getFileType());
ASResource::buildPreCommandHeaders(preCommandHeaders, getFileType());
if (operators->empty())
ASResource::buildOperators(operators, getFileType());
if (assignmentOperators->empty())
ASResource::buildAssignmentOperators(assignmentOperators);
if (castOperators->empty())
ASResource::buildCastOperators(castOperators);
}
/**
* set the variables for each preefined style.
* this will override any previous settings.
*/
void ASFormatter::fixOptionVariableConflicts()
{
if (formattingStyle == STYLE_ALLMAN)
{
setBracketFormatMode(BREAK_MODE);
}
else if (formattingStyle == STYLE_JAVA)
{
setBracketFormatMode(ATTACH_MODE);
}
else if (formattingStyle == STYLE_KR)
{
setBracketFormatMode(LINUX_MODE);
}
else if (formattingStyle == STYLE_STROUSTRUP)
{
setBracketFormatMode(STROUSTRUP_MODE);
}
else if (formattingStyle == STYLE_WHITESMITH)
{
setBracketFormatMode(BREAK_MODE);
setBracketIndent(true);
setClassIndent(true);
setSwitchIndent(true);
}
else if (formattingStyle == STYLE_BANNER)
{
setBracketFormatMode(ATTACH_MODE);
setBracketIndent(true);
setClassIndent(true);
setSwitchIndent(true);
}
else if (formattingStyle == STYLE_GNU)
{
setBracketFormatMode(BREAK_MODE);
setBlockIndent(true);
}
else if (formattingStyle == STYLE_LINUX)
{
setBracketFormatMode(LINUX_MODE);
// always for Linux style
setMinConditionalIndentOption(MINCOND_ONEHALF);
}
else if (formattingStyle == STYLE_HORSTMANN)
{
setBracketFormatMode(RUN_IN_MODE);
setSwitchIndent(true);
}
else if (formattingStyle == STYLE_1TBS)
{
setBracketFormatMode(LINUX_MODE);
setAddBracketsMode(true);
}
else if (formattingStyle == STYLE_PICO)
{
setBracketFormatMode(RUN_IN_MODE);
setAttachClosingBracket(true);
setSwitchIndent(true);
setBreakOneLineBlocksMode(false);
setSingleStatementsMode(false);
// add-brackets won't work for pico, but it could be fixed if necessary
// both options should be set to true
if (shouldAddBrackets)
shouldAddOneLineBrackets = true;
}
else if (formattingStyle == STYLE_LISP)
{
setBracketFormatMode(ATTACH_MODE);
setAttachClosingBracket(true);
setSingleStatementsMode(false);
// add-one-line-brackets won't work for lisp
// only shouldAddBrackets should be set to true
if (shouldAddOneLineBrackets)
{
shouldAddBrackets = true;
shouldAddOneLineBrackets = false;
}
}
setMinConditionalIndentLength();
// if not set by indent=force-tab-x set equal to indentLength
if (!getTabLength())
setDefaultTabLength();
// add-one-line-brackets implies keep-one-line-blocks
if (shouldAddOneLineBrackets)
setBreakOneLineBlocksMode(false);
}
/**
* get the next formatted line.
*
* @return formatted line.
*/
string ASFormatter::nextLine()
{
const string* newHeader;
bool isInVirginLine = isVirgin;
isCharImmediatelyPostComment = false;
isPreviousCharPostComment = false;
isCharImmediatelyPostLineComment = false;
isCharImmediatelyPostOpenBlock = false;
isCharImmediatelyPostCloseBlock = false;
isCharImmediatelyPostTemplate = false;
traceLineNumber++;
while (!isLineReady)
{
if (shouldReparseCurrentChar)
shouldReparseCurrentChar = false;
else if (!getNextChar())
{
breakLine();
continue;
}
else // stuff to do when reading a new character...
{
// make sure that a virgin '{' at the begining of the file will be treated as a block...
if (isInVirginLine && currentChar == '{'
&& currentLineBeginsWithBracket // lineBeginsWith('{')
&& previousCommandChar == ' ')
previousCommandChar = '{';
if (isInHorstmannRunIn)
isInLineBreak = false;
if (!isWhiteSpace(currentChar))
isInHorstmannRunIn = false;
isPreviousCharPostComment = isCharImmediatelyPostComment;
isCharImmediatelyPostComment = false;
isCharImmediatelyPostTemplate = false;
isCharImmediatelyPostReturn = false;
isCharImmediatelyPostThrow = false;
isCharImmediatelyPostOperator = false;
isCharImmediatelyPostPointerOrReference = false;
isCharImmediatelyPostOpenBlock = false;
isCharImmediatelyPostCloseBlock = false;
}
if (shouldBreakLineAtNextChar)
{
if (isWhiteSpace(currentChar) && !lineIsEmpty)
continue;
isInLineBreak = true;
shouldBreakLineAtNextChar = false;
}
if (isInExecSQL && !passedSemicolon)
{
if (currentChar == ';')
passedSemicolon = true;
appendCurrentChar();
continue;
}
if (isInLineComment)
{
formatLineCommentBody();
continue;
}
else if (isInComment)
{
formatCommentBody();
continue;
}
// not in line comment or comment
else if (isInQuote)
{
formatQuoteBody();
continue;
}
if (isSequenceReached("//"))
{
formatLineCommentOpener();
testForTimeToSplitFormattedLine();
continue;
}
else if (isSequenceReached("/*"))
{
formatCommentOpener();
testForTimeToSplitFormattedLine();
continue;
}
else if (currentChar == '"' || currentChar == '\'')
{
formatQuoteOpener();
testForTimeToSplitFormattedLine();
continue;
}
// treat these preprocessor statements as a line comment
else if (currentChar =='#')
{
string preproc = trim(currentLine.c_str() + charNum + 1);
if (preproc.compare(0, 6, "region") == 0
|| preproc.compare(0, 9, "endregion") == 0
|| preproc.compare(0, 5, "error") == 0
|| preproc.compare(0, 7, "warning") == 0)
{
// check for horstmann run-in
if (formattedLine.length() > 0 && formattedLine[0] == '{')
{
isInLineBreak = true;
isInHorstmannRunIn = false;
}
isInLineComment = true;
appendCurrentChar();
continue;
}
}
if (isInPreprocessor)
{
appendCurrentChar();
continue;
}
if (isInTemplate && shouldCloseTemplates)
{
if (previousCommandChar == '<' && isWhiteSpace(currentChar))
continue;
if (isWhiteSpace(currentChar) && peekNextChar() == '>')
continue;
}
// handle white space - needed to simplify the rest.
if (isWhiteSpace(currentChar))
{
appendCurrentChar();
continue;
}
/* not in MIDDLE of quote or comment or SQL or white-space of any type ... */
// check if in preprocessor
// ** isInPreprocessor will be automatically reset at the begining
// of a new line in getnextChar()
if (currentChar == '#')
{
isInPreprocessor = true;
// check for horstmann run-in
if (formattedLine.length() > 0 && formattedLine[0] == '{')
{
isInLineBreak = true;
isInHorstmannRunIn = false;
}
processPreprocessor();
// need to fall thru here to reset the variables
}
/* not in preprocessor ... */
if (isImmediatelyPostComment)
{
isImmediatelyPostComment = false;
isCharImmediatelyPostComment = true;
}
if (isImmediatelyPostLineComment)
{
isImmediatelyPostLineComment = false;
isCharImmediatelyPostLineComment = true;
}
if (isImmediatelyPostReturn)
{
isImmediatelyPostReturn = false;
isCharImmediatelyPostReturn = true;
}
if (isImmediatelyPostThrow)
{
isImmediatelyPostThrow = false;
isCharImmediatelyPostThrow = true;
}
if (isImmediatelyPostOperator)
{
isImmediatelyPostOperator = false;
isCharImmediatelyPostOperator = true;
}
if (isImmediatelyPostTemplate)
{
isImmediatelyPostTemplate = false;
isCharImmediatelyPostTemplate = true;
}
if (isImmediatelyPostPointerOrReference)
{
isImmediatelyPostPointerOrReference = false;
isCharImmediatelyPostPointerOrReference = true;
}
// reset isImmediatelyPostHeader information
if (isImmediatelyPostHeader)
{
// should brackets be added
if (currentChar != '{' && shouldAddBrackets)
{
bool bracketsAdded = addBracketsToStatement();
if (bracketsAdded && !shouldAddOneLineBrackets)
{
size_t firstText = currentLine.find_first_not_of(" \t");
assert(firstText != string::npos);
if ((int) firstText == charNum)
breakCurrentOneLineBlock = true;
}
}
// Make sure headers are broken from their succeeding blocks
// (e.g.
// if (isFoo) DoBar();
// should become
// if (isFoo)
// DoBar;
// )
// But treat else if() as a special case which should not be broken!
if (shouldBreakOneLineStatements
&& isOkToBreakBlock(bracketTypeStack->back()))
{
// if may break 'else if()'s, then simply break the line
if (shouldBreakElseIfs)
isInLineBreak = true;
}
isImmediatelyPostHeader = false;
}
if (passedSemicolon) // need to break the formattedLine
{
passedSemicolon = false;
if (parenStack->back() == 0 && !isCharImmediatelyPostComment && currentChar != ';') // allow ;;
{
// does a one-line statement have ending comments?
if (isBracketType(bracketTypeStack->back(), SINGLE_LINE_TYPE))
{
size_t blockEnd = currentLine.rfind(AS_CLOSE_BRACKET);
assert(blockEnd != string::npos);
// move ending comments to this formattedLine
if (isBeforeAnyLineEndComment(blockEnd))
{
size_t commentStart = currentLine.find_first_not_of(" \t", blockEnd + 1);
assert(commentStart != string::npos);
assert((currentLine.compare(commentStart, 2, "//") == 0)
|| (currentLine.compare(commentStart, 2, "/*") == 0));
size_t commentLength = currentLine.length() - commentStart;
formattedLine.append(getIndentLength() - 1, ' ');
formattedLine.append(currentLine, commentStart, commentLength);
currentLine.erase(commentStart, commentLength);
testForTimeToSplitFormattedLine();
}
}
isInExecSQL = false;
shouldReparseCurrentChar = true;
isInLineBreak = true;
if (needHeaderOpeningBracket)
{
isCharImmediatelyPostCloseBlock = true;
needHeaderOpeningBracket = false;
}
continue;
}
}
if (passedColon)
{
passedColon = false;
if (parenStack->back() == 0 && !isBeforeAnyComment())
{
shouldReparseCurrentChar = true;
isInLineBreak = true;
continue;
}
}
// Check if in template declaration, e.g. foo<bar> or foo<bar,fig>
if (!isInTemplate && currentChar == '<')
{
checkIfTemplateOpener();
}
// handle parenthesies
if (currentChar == '(' || currentChar == '[' || (isInTemplate && currentChar == '<'))
{
parenStack->back()++;
if (currentChar == '[')
isInBlParen = true;
}
else if (currentChar == ')' || currentChar == ']' || (isInTemplate && currentChar == '>'))
{
foundPreCommandHeader = false;
parenStack->back()--;
if (isInTemplate && currentChar == '>')
{
templateDepth--;
if (templateDepth == 0)
{
isInTemplate = false;
isImmediatelyPostTemplate = true;
}
}
// check if this parenthesis closes a header, e.g. if (...), while (...)
if (isInHeader && parenStack->back() == 0)
{
isInHeader = false;
isImmediatelyPostHeader = true;
foundQuestionMark = false;
}
if (currentChar == ']')
isInBlParen = false;
if (currentChar == ')')
{
foundCastOperator = false;
if (parenStack->back() == 0)
endOfAsmReached = true;
}
}
// handle brackets
if (currentChar == '{' || currentChar == '}')
{
// if appendOpeningBracket this was already done for the original bracket
if (currentChar == '{' && !appendOpeningBracket)
{
BracketType newBracketType = getBracketType();
foundNamespaceHeader = false;
foundClassHeader = false;
foundStructHeader = false;
foundInterfaceHeader = false;
foundPreDefinitionHeader = false;
foundPreCommandHeader = false;
isInPotentialCalculation = false;
isInEnum = false;
isJavaStaticConstructor = false;
isCharImmediatelyPostNonInStmt = false;
needHeaderOpeningBracket = false;
isPreviousBracketBlockRelated = !isBracketType(newBracketType, ARRAY_TYPE);
bracketTypeStack->push_back(newBracketType);
preBracketHeaderStack->push_back(currentHeader);
currentHeader = NULL;
structStack->push_back(isInIndentableStruct);
if (isBracketType(newBracketType, STRUCT_TYPE) && isCStyle())
isInIndentableStruct = isStructAccessModified(currentLine, charNum);
else
isInIndentableStruct = false;
}
// this must be done before the bracketTypeStack is popped
BracketType bracketType = bracketTypeStack->back();
bool isOpeningArrayBracket = (isBracketType(bracketType, ARRAY_TYPE)
&& bracketTypeStack->size() >= 2
&& !isBracketType((*bracketTypeStack)[bracketTypeStack->size()-2], ARRAY_TYPE)
);
if (currentChar == '}')
{
// if a request has been made to append a post block empty line,
// but the block exists immediately before a closing bracket,
// then there is no need for the post block empty line.
isAppendPostBlockEmptyLineRequested = false;
breakCurrentOneLineBlock = false;
isInAsmBlock = false;
// added for release 1.24
// TODO: remove at the appropriate time
assert(isInAsm == false || endOfAsmReached == true);
assert(isInAsmOneLine == false);
assert(isInQuote == false);
isInAsm = isInAsmOneLine = isInQuote = false;
// end remove
if (bracketTypeStack->size() > 1)
{
previousBracketType = bracketTypeStack->back();
bracketTypeStack->pop_back();
isPreviousBracketBlockRelated = !isBracketType(bracketType, ARRAY_TYPE);
}
else
{
previousBracketType = NULL_TYPE;
isPreviousBracketBlockRelated = false;
}
if (!preBracketHeaderStack->empty())
{
currentHeader = preBracketHeaderStack->back();
preBracketHeaderStack->pop_back();
}
else
currentHeader = NULL;
if (!structStack->empty())
{
isInIndentableStruct = structStack->back();
structStack->pop_back();
}
else
isInIndentableStruct = false;
if (isNonInStatementArray
&& (!isBracketType(bracketTypeStack->back(), ARRAY_TYPE) // check previous bracket
|| peekNextChar() == ';')) // check for "};" added V2.01
isImmediatelyPostNonInStmt = true;
}
// format brackets
appendOpeningBracket = false;
if (isBracketType(bracketType, ARRAY_TYPE))
{
formatArrayBrackets(bracketType, isOpeningArrayBracket);
}
else
{
if (currentChar == '{')
formatOpeningBracket(bracketType);
else
formatClosingBracket(bracketType);
}
continue;
}
if ((((previousCommandChar == '{' && isPreviousBracketBlockRelated)
|| ((previousCommandChar == '}'
&& !isImmediatelyPostEmptyBlock
&& isPreviousBracketBlockRelated
&& !isPreviousCharPostComment // Fixes wrongly appended newlines after '}' immediately after comments
&& peekNextChar() != ' '
&& !isBracketType(previousBracketType, DEFINITION_TYPE))
&& !isBracketType(bracketTypeStack->back(), DEFINITION_TYPE)))
&& isOkToBreakBlock(bracketTypeStack->back()))
// check for array
|| (previousCommandChar == '{' // added 9/30/2010
&& isBracketType(bracketTypeStack->back(), ARRAY_TYPE)
&& !isBracketType(bracketTypeStack->back(), SINGLE_LINE_TYPE)
&& isNonInStatementArray))
{
isCharImmediatelyPostOpenBlock = (previousCommandChar == '{');
isCharImmediatelyPostCloseBlock = (previousCommandChar == '}');
if (isCharImmediatelyPostOpenBlock
&& !isCharImmediatelyPostComment
&& !isCharImmediatelyPostLineComment)
{
previousCommandChar = ' ';
if (bracketFormatMode == NONE_MODE)
{
if (shouldBreakOneLineBlocks
&& isBracketType(bracketTypeStack->back(), SINGLE_LINE_TYPE))
isInLineBreak = true;
else if (currentLineBeginsWithBracket)
formatRunIn();
else
breakLine();
}
else if (bracketFormatMode == RUN_IN_MODE
&& currentChar != '#')
formatRunIn();
else
isInLineBreak = true;
}
else if (isCharImmediatelyPostCloseBlock
&& shouldBreakOneLineStatements
&& (isLegalNameChar(currentChar) && currentChar != '.')
&& !isCharImmediatelyPostComment)
{
previousCommandChar = ' ';
isInLineBreak = true;
}
}
// reset block handling flags
isImmediatelyPostEmptyBlock = false;
// look for headers
bool isPotentialHeader = isCharPotentialHeader(currentLine, charNum);
if (isPotentialHeader && !isInTemplate)
{
isNonParenHeader = false;
foundClosingHeader = false;
newHeader = findHeader(headers);
if (newHeader != NULL)
{
const string* previousHeader;
// recognize closing headers of do..while, if..else, try..catch..finally
if ((newHeader == &AS_ELSE && currentHeader == &AS_IF)
|| (newHeader == &AS_WHILE && currentHeader == &AS_DO)
|| (newHeader == &AS_CATCH && currentHeader == &AS_TRY)
|| (newHeader == &AS_CATCH && currentHeader == &AS_CATCH)
|| (newHeader == &AS_FINALLY && currentHeader == &AS_TRY)
|| (newHeader == &AS_FINALLY && currentHeader == &AS_CATCH)
|| (newHeader == &_AS_FINALLY && currentHeader == &_AS_TRY)
|| (newHeader == &_AS_EXCEPT && currentHeader == &_AS_TRY)
|| (newHeader == &AS_SET && currentHeader == &AS_GET)
|| (newHeader == &AS_REMOVE && currentHeader == &AS_ADD))
foundClosingHeader = true;
previousHeader = currentHeader;
currentHeader = newHeader;
needHeaderOpeningBracket = true;
if (foundClosingHeader && previousNonWSChar == '}')
{
if (isOkToBreakBlock(bracketTypeStack->back()))
isLineBreakBeforeClosingHeader();
// get the adjustment for a comment following the closing header
if (isInLineBreak)
nextLineSpacePadNum = getNextLineCommentAdjustment();
else
spacePadNum = getCurrentLineCommentAdjustment();
}
// check if the found header is non-paren header
isNonParenHeader = findHeader(nonParenHeaders) != NULL;
// join 'else if' statements
if (currentHeader == &AS_IF && previousHeader == &AS_ELSE && isInLineBreak
&& !shouldBreakElseIfs && !isCharImmediatelyPostLineComment)
{
// 'else' must be last thing on the line, but must not be #else
size_t start = formattedLine.length() >= 6 ? formattedLine.length()-6 : 0;
if (formattedLine.find("else", start) != string::npos
&& formattedLine.find("#else", start) == string::npos)
{
appendSpacePad();
isInLineBreak = false;
}
}
appendSequence(*currentHeader);
goForward(currentHeader->length() - 1);
// if a paren-header is found add a space after it, if needed
// this checks currentLine, appendSpacePad() checks formattedLine
// in 'case' and C# 'catch' can be either a paren or non-paren header
if (shouldPadHeader
&& (!isNonParenHeader
|| (currentHeader == &AS_CASE && peekNextChar() == '(')
|| (currentHeader == &AS_CATCH && peekNextChar() == '('))
&& charNum < (int) currentLine.length() - 1 && !isWhiteSpace(currentLine[charNum+1]))
appendSpacePad();
// Signal that a header has been reached
// *** But treat a closing while() (as in do...while)
// as if it were NOT a header since a closing while()
// should never have a block after it!
if (currentHeader != &AS_CASE
&& !(foundClosingHeader && currentHeader == &AS_WHILE))
{
isInHeader = true;
// in C# 'catch' and 'delegate' can be a paren or non-paren header
if (isNonParenHeader && !isSharpStyleWithParen(currentHeader))
{
isImmediatelyPostHeader = true;
isInHeader = false;
}
}
if (shouldBreakBlocks
&& isOkToBreakBlock(bracketTypeStack->back()))
{
if (previousHeader == NULL
&& !foundClosingHeader
&& !isCharImmediatelyPostOpenBlock
&& !isImmediatelyPostCommentOnly)
{
isPrependPostBlockEmptyLineRequested = true;
}
if (currentHeader == &AS_ELSE
|| currentHeader == &AS_CATCH
|| currentHeader == &AS_FINALLY
|| foundClosingHeader)
{
isPrependPostBlockEmptyLineRequested = false;
}
if (shouldBreakClosingHeaderBlocks
&& isCharImmediatelyPostCloseBlock
&& !isImmediatelyPostCommentOnly
&& currentHeader != &AS_WHILE) // closing do-while block
{
isPrependPostBlockEmptyLineRequested = true;
}
}
if (currentHeader == &AS_CASE
|| currentHeader == &AS_DEFAULT)
isInCase = true;
continue;
}
else if ((newHeader = findHeader(preDefinitionHeaders)) != NULL
&& parenStack->back() == 0)
{
if (newHeader == &AS_NAMESPACE)
foundNamespaceHeader = true;
if (newHeader == &AS_CLASS)
foundClassHeader = true;
if (newHeader == &AS_STRUCT)
foundStructHeader = true;
if (newHeader == &AS_INTERFACE)
foundInterfaceHeader = true;
foundPreDefinitionHeader = true;
appendSequence(*newHeader);
goForward(newHeader->length() - 1);
continue;
}
else if ((newHeader = findHeader(preCommandHeaders)) != NULL)
{
foundPreCommandHeader = true;
// fall thru here for a 'const' that is not a precommand header
}
else if ((newHeader = findHeader(castOperators)) != NULL)
{
foundCastOperator = true;
appendSequence(*newHeader);
goForward(newHeader->length() - 1);
continue;
}
} // (isPotentialHeader && !isInTemplate)
if (isInLineBreak) // OK to break line here
{
breakLine();
if (isInVirginLine) // adjust for the first line
{
lineCommentNoBeautify = lineCommentNoIndent;
lineCommentNoIndent = false;
}
}
if (previousNonWSChar == '}' || currentChar == ';')
{
if (currentChar == ';')
{
if (((shouldBreakOneLineStatements
|| isBracketType(bracketTypeStack->back(), SINGLE_LINE_TYPE))
&& isOkToBreakBlock(bracketTypeStack->back()))
&& !(shouldAttachClosingBracket && peekNextChar() == '}'))
{
passedSemicolon = true;
}
// append post block empty line for unbracketed header
if (shouldBreakBlocks
&& currentHeader != NULL
&& currentHeader != &AS_CASE
&& currentHeader != &AS_DEFAULT
&& parenStack->back() == 0)
{
isAppendPostBlockEmptyLineRequested = true;
}
}
// end of block if a closing bracket was found
// or an opening bracket was not found (';' closes)
if (currentChar != ';'
|| (needHeaderOpeningBracket && parenStack->back() == 0))
currentHeader = NULL;
foundQuestionMark = false;
foundNamespaceHeader = false;
foundClassHeader = false;
foundStructHeader = false;
foundInterfaceHeader = false;
foundPreDefinitionHeader = false;
foundPreCommandHeader = false;
foundCastOperator = false;
isInPotentialCalculation = false;
isSharpAccessor = false;
isSharpDelegate = false;
isInEnum = false;
isInExtern = false;
nonInStatementBracket = 0;
}
if (currentChar == ':')
{
if (isInCase
&& previousChar != ':' // not part of '::'
&& peekNextChar() != ':') // not part of '::'
{
isInCase = false;
if (shouldBreakOneLineStatements)
passedColon = true;
}
else if (isCStyle() // for C/C++ only
&& shouldBreakOneLineStatements
&& !foundQuestionMark // not in a ... ? ... : ... sequence
&& !foundPreDefinitionHeader // not in a definition block (e.g. class foo : public bar
&& previousCommandChar != ')' // not immediately after closing paren of a method header, e.g. ASFormatter::ASFormatter(...) : ASBeautifier(...)
&& previousChar != ':' // not part of '::'
&& peekNextChar() != ':' // not part of '::'
&& !isDigit(peekNextChar()) // not a bit field
&& !isInEnum // not an enum with a base type
&& !isInAsm // not in extended assembler
&& !isInAsmOneLine // not in extended assembler
&& !isInAsmBlock) // not in extended assembler
{
passedColon = true;
}
}
if (currentChar == '?')
foundQuestionMark = true;
if (isPotentialHeader && !isInTemplate)
{
if (findKeyword(currentLine, charNum, AS_NEW))
isInPotentialCalculation = false;
if (findKeyword(currentLine, charNum, AS_RETURN))
{
isInPotentialCalculation = true; // return is the same as an = sign
isImmediatelyPostReturn = true;
}
if (isCStyle()
&& findKeyword(currentLine, charNum, AS_THROW)
&& previousCommandChar != ')'
&& !foundPreCommandHeader) // 'const' throw()
isImmediatelyPostThrow = true;
if (findKeyword(currentLine, charNum, AS_OPERATOR))
isImmediatelyPostOperator = true;
if (isCStyle() && findKeyword(currentLine, charNum, AS_ENUM))
isInEnum = true;
if (isCStyle() && findKeyword(currentLine, charNum, AS_EXTERN))
isInExtern = true;
if (isCStyle() && isExecSQL(currentLine, charNum))
isInExecSQL = true;
if (isCStyle())
{
if (findKeyword(currentLine, charNum, AS_ASM)
|| findKeyword(currentLine, charNum, AS__ASM__))
{
isInAsm = true;
}
else if (findKeyword(currentLine, charNum, AS_MS_ASM) // microsoft specific
|| findKeyword(currentLine, charNum, AS_MS__ASM))
{
int index = 4;
if (peekNextChar() == '_') // check for __asm
index = 5;
char peekedChar = ASBase::peekNextChar(currentLine, charNum + index);
if (peekedChar == '{' || peekedChar == ' ')
isInAsmBlock = true;
else
isInAsmOneLine = true;
}
}
if (isJavaStyle()
&& (findKeyword(currentLine, charNum, AS_STATIC)
&& isNextCharOpeningBracket(charNum + 6)))
isJavaStaticConstructor = true;
if (isSharpStyle()
&& (findKeyword(currentLine, charNum, AS_DELEGATE)
|| findKeyword(currentLine, charNum, AS_UNCHECKED)))
isSharpDelegate = true;
// append the entire name
string name = getCurrentWord(currentLine, charNum);
// must pad the 'and' and 'or' operators if required
if (shouldPadOperators
&& (name == "and" || name == "or"))
{
appendSpacePad();
appendSequence(name);
goForward(name.length() - 1);
if (!isBeforeAnyComment()
&& !(currentLine.compare(charNum + 1, 1, ";") == 0)
&& !(currentLine.compare(charNum + 1, 2, "::") == 0))
appendSpaceAfter();
}
else
{
appendSequence(name);
goForward(name.length() - 1);
}
continue;
} // (isPotentialHeader && !isInTemplate)
// determine if this is a potential calculation
bool isPotentialOperator = isCharPotentialOperator(currentChar);
newHeader = NULL;
if (isPotentialOperator)
{
newHeader = findOperator(operators);
if (newHeader != NULL)
{
// correct mistake of two >> closing a template
if (isInTemplate && (newHeader == &AS_GR_GR || newHeader == &AS_GR_GR_GR))
newHeader = &AS_GR;
if (!isInPotentialCalculation)
{
// must determine if newHeader is an assignment operator
// do NOT use findOperator!!!
if (find(assignmentOperators->begin(), assignmentOperators->end(), newHeader)
!= assignmentOperators->end())
{
foundPreCommandHeader = false;
char peekedChar = peekNextChar();
isInPotentialCalculation = (!(newHeader == &AS_EQUAL && peekedChar == '*')
&& !(newHeader == &AS_EQUAL && peekedChar == '&'));
}
}
}
}
// process pointers and references
// check newHeader to elimnate things like '&&' sequence
if (!isJavaStyle()
&& (newHeader == &AS_MULT || newHeader == &AS_BIT_AND || newHeader == &AS_BIT_XOR)
&& isPointerOrReference()
&& !isDereferenceOrAddressOf())
{
formatPointerOrReference();
isImmediatelyPostPointerOrReference = true;
continue;
}
if (shouldPadOperators && newHeader != NULL)
{
padOperators(newHeader);
continue;
}
// pad commas and semi-colons
if (currentChar == ';'
|| (currentChar == ',' && shouldPadOperators))
{
char nextChar = ' ';
if (charNum + 1 < (int) currentLine.length())
nextChar = currentLine[charNum+1];
if (!isWhiteSpace(nextChar)
&& nextChar != '}'
&& nextChar != ')'
&& nextChar != ']'
&& nextChar != '>'
&& nextChar != ';'
&& !isBeforeAnyComment()
/* && !(isBracketType(bracketTypeStack->back(), ARRAY_TYPE)) */
)
{
appendCurrentChar();
appendSpaceAfter();
continue;
}
}
// do NOT use 'continue' after this, it must do padParens if necessary
if (currentChar == '('
&& shouldPadHeader
&& (isCharImmediatelyPostReturn || isCharImmediatelyPostThrow))
appendSpacePad();
if ((currentChar == '(' || currentChar == ')')
&& (shouldPadParensOutside || shouldPadParensInside || shouldUnPadParens || shouldPadFirstParen))
{
padParens();
continue;
}
// bypass the entire operator
if (newHeader != NULL && newHeader->length() > 1)
{
appendSequence(*newHeader);
goForward(newHeader->length() - 1);
continue;
}
appendCurrentChar();
} // end of while loop * end of while loop * end of while loop * end of while loop
// return a beautified (i.e. correctly indented) line.
string beautifiedLine;
size_t readyFormattedLineLength = trim(readyFormattedLine).length();
if (prependEmptyLine // prepend a blank line before this formatted line
&& readyFormattedLineLength > 0
&& previousReadyFormattedLineLength > 0)
{
isLineReady = true; // signal a waiting readyFormattedLine
beautifiedLine = beautify("");
previousReadyFormattedLineLength = 0;
// call the enhancer for new empty lines
enhancer->enhance(beautifiedLine, isInPreprocessorBeautify, isInBeautifySQL);
}
else // format the current formatted line
{
isLineReady = false;
horstmannIndentInStatement = horstmannIndentChars;
beautifiedLine = beautify(readyFormattedLine);
previousReadyFormattedLineLength = readyFormattedLineLength;
// the enhancer is not called for no-indent line comments
if (!lineCommentNoBeautify)
enhancer->enhance(beautifiedLine, isInPreprocessorBeautify, isInBeautifySQL);
horstmannIndentChars = 0;
lineCommentNoBeautify = lineCommentNoIndent;
lineCommentNoIndent = false;
if (isCharImmediatelyPostNonInStmt)
{
isNonInStatementArray = false;
isCharImmediatelyPostNonInStmt = false;
}
isInPreprocessorBeautify = isInPreprocessor; // used by ASEnhancer
isInBeautifySQL = isInExecSQL; // used by ASEnhancer
}
prependEmptyLine = false;
assert(computeChecksumOut(beautifiedLine));
return beautifiedLine;
}
/**
* check if there are any indented lines ready to be read by nextLine()
*
* @return are there any indented lines ready?
*/
bool ASFormatter::hasMoreLines() const
{
return !endOfCodeReached;
}
/**
* comparison function for BracketType enum
*/
bool ASFormatter::isBracketType(BracketType a, BracketType b) const
{
return ((a & b) == b);
}
/**
* set the formatting style.
*
* @param mode the formatting style.
*/
void ASFormatter::setFormattingStyle(FormatStyle style)
{
formattingStyle = style;
}
/**
* set the add brackets mode.
* options:
* true brackets added to headers for single line statements.
* false brackets NOT added to headers for single line statements.
*
* @param mode the bracket formatting mode.
*/
void ASFormatter::setAddBracketsMode(bool state)
{
shouldAddBrackets = state;
}
/**
* set the add one line brackets mode.
* options:
* true one line brackets added to headers for single line statements.
* false one line brackets NOT added to headers for single line statements.
*
* @param mode the bracket formatting mode.
*/
void ASFormatter::setAddOneLineBracketsMode(bool state)
{
shouldAddBrackets = state;
shouldAddOneLineBrackets = state;
}
/**
* set the bracket formatting mode.
* options:
*
* @param mode the bracket formatting mode.
*/
void ASFormatter::setBracketFormatMode(BracketMode mode)
{
bracketFormatMode = mode;
}
/**
* set 'break after' mode for maximum code length
*
* @param state the 'break after' mode.
*/
void ASFormatter::setBreakAfterMode(bool state)
{
shouldBreakLineAfterLogical = state;
}
/**
* set closing header bracket breaking mode
* options:
* true brackets just before closing headers (e.g. 'else', 'catch')
* will be broken, even if standard brackets are attached.
* false closing header brackets will be treated as standard brackets.
*
* @param state the closing header bracket breaking mode.
*/
void ASFormatter::setBreakClosingHeaderBracketsMode(bool state)
{
shouldBreakClosingHeaderBrackets = state;
}
/**
* set 'else if()' breaking mode
* options:
* true 'else' headers will be broken from their succeeding 'if' headers.
* false 'else' headers will be attached to their succeeding 'if' headers.
*
* @param state the 'else if()' breaking mode.
*/
void ASFormatter::setBreakElseIfsMode(bool state)
{
shouldBreakElseIfs = state;
}
/**
* set maximum code length
*
* @param max the maximum code length.
*/
void ASFormatter::setMaxCodeLength(int max)
{
maxCodeLength = max;
}
/**
* set operator padding mode.
* options:
* true statement operators will be padded with spaces around them.
* false statement operators will not be padded.
*
* @param state the padding mode.
*/
void ASFormatter::setOperatorPaddingMode(bool state)
{
shouldPadOperators = state;
}
/**
* set parenthesis outside padding mode.
* options:
* true statement parenthesiss will be padded with spaces around them.
* false statement parenthesiss will not be padded.
*
* @param state the padding mode.
*/
void ASFormatter::setParensOutsidePaddingMode(bool state)
{
shouldPadParensOutside = state;
}
/**
* set parenthesis inside padding mode.
* options:
* true statement parenthesis will be padded with spaces around them.
* false statement parenthesis will not be padded.
*
* @param state the padding mode.
*/
void ASFormatter::setParensInsidePaddingMode(bool state)
{
shouldPadParensInside = state;
}
/**
* set padding mode before one or more open parentheses.
* options:
* true first open parenthesis will be padded with a space before.
* false first open parenthesis will not be padded.
*
* @param state the padding mode.
*/
void ASFormatter::setParensFirstPaddingMode(bool state)
{
shouldPadFirstParen = state;
}
/**
* set header padding mode.
* options:
* true headers will be padded with spaces around them.
* false headers will not be padded.
*
* @param state the padding mode.
*/
void ASFormatter::setParensHeaderPaddingMode(bool state)
{
shouldPadHeader = state;
}
/**
* set parenthesis unpadding mode.
* options:
* true statement parenthesis will be unpadded with spaces removed around them.
* false statement parenthesis will not be unpadded.
*
* @param state the padding mode.
*/
void ASFormatter::setParensUnPaddingMode(bool state)
{
shouldUnPadParens = state;
}
/**
* set option to attach closing brackets
*
* @param state true = attach, false = don't attach.
*/
void ASFormatter::setAttachClosingBracket(bool state)
{
shouldAttachClosingBracket = state;
}
/**
* set option to break/not break one-line blocks
*
* @param state true = break, false = don't break.
*/
void ASFormatter::setBreakOneLineBlocksMode(bool state)
{
shouldBreakOneLineBlocks = state;
}
void ASFormatter::setCloseTemplatesMode(bool state)
{
shouldCloseTemplates = state;
}
/**
* set option to break/not break lines consisting of multiple statements.
*
* @param state true = break, false = don't break.
*/
void ASFormatter::setSingleStatementsMode(bool state)
{
shouldBreakOneLineStatements = state;
}
/**
* set option to convert tabs to spaces.
*
* @param state true = convert, false = don't convert.
*/
void ASFormatter::setTabSpaceConversionMode(bool state)
{
shouldConvertTabs = state;
}
/**
* set option to indent comments in column 1.
*
* @param state true = indent, false = don't indent.
*/
void ASFormatter::setIndentCol1CommentsMode(bool state)
{
shouldIndentCol1Comments = state;
}
/**
* set option to force all line ends to a particular style.
*
* @param fmt format enum value
*/
void ASFormatter::setLineEndFormat(LineEndFormat fmt)
{
lineEnd = fmt;
}
/**
* set option to break unrelated blocks of code with empty lines.
*
* @param state true = convert, false = don't convert.
*/
void ASFormatter::setBreakBlocksMode(bool state)
{
shouldBreakBlocks = state;
}
/**
* set option to break closing header blocks of code (such as 'else', 'catch', ...) with empty lines.
*
* @param state true = convert, false = don't convert.
*/
void ASFormatter::setBreakClosingHeaderBlocksMode(bool state)
{
shouldBreakClosingHeaderBlocks = state;
}
/**
* set option to delete empty lines.
*
* @param state true = delete, false = don't delete.
*/
void ASFormatter::setDeleteEmptyLinesMode(bool state)
{
shouldDeleteEmptyLines = state;
}
/**
* set the pointer alignment.
* options:
*
* @param alignment the pointer alignment.
*/
void ASFormatter::setPointerAlignment(PointerAlign alignment)
{
pointerAlignment = alignment;
}
void ASFormatter::setReferenceAlignment(ReferenceAlign alignment)
{
referenceAlignment = alignment;
}
/**
* jump over several characters.
*
* @param i the number of characters to jump over.
*/
void ASFormatter::goForward(int i)
{
while (--i >= 0)
getNextChar();
}
/**
* peek at the next unread character.
*
* @return the next unread character.
*/
char ASFormatter::peekNextChar() const
{
char ch = ' ';
size_t peekNum = currentLine.find_first_not_of(" \t", charNum + 1);
if (peekNum == string::npos)
return ch;
ch = currentLine[peekNum];
return ch;
}
/**
* check if current placement is before a comment
*
* @return is before a comment.
*/
bool ASFormatter::isBeforeComment() const
{
bool foundComment = false;
size_t peekNum = currentLine.find_first_not_of(" \t", charNum + 1);
if (peekNum == string::npos)
return foundComment;
foundComment = (currentLine.compare(peekNum, 2, "/*") == 0);
return foundComment;
}
/**
* check if current placement is before a comment or line-comment
*
* @return is before a comment or line-comment.
*/
bool ASFormatter::isBeforeAnyComment() const
{
bool foundComment = false;
size_t peekNum = currentLine.find_first_not_of(" \t", charNum + 1);
if (peekNum == string::npos)
return foundComment;
foundComment = (currentLine.compare(peekNum, 2, "/*") == 0
|| currentLine.compare(peekNum, 2, "//") == 0);
return foundComment;
}
/**
* check if current placement is before a comment or line-comment
* if a block comment it must be at the end of the line
*
* @return is before a comment or line-comment.
*/
bool ASFormatter::isBeforeAnyLineEndComment(int startPos) const
{
bool foundLineEndComment = false;
size_t peekNum = currentLine.find_first_not_of(" \t", startPos + 1);
if (peekNum != string::npos)
{
if (currentLine.compare(peekNum, 2, "//") == 0)
foundLineEndComment = true;
else if (currentLine.compare(peekNum, 2, "/*") == 0)
{
// comment must be closed on this line with nothing after it
size_t endNum = currentLine.find("*/", peekNum + 2);
if (endNum != string::npos)
{
size_t nextChar = currentLine.find_first_not_of(" \t", endNum + 2);
if (nextChar == string::npos)
foundLineEndComment = true;
}
}
}
return foundLineEndComment;
}
/**
* check if current placement is before a comment followed by a line-comment
*
* @return is before a multiple line-end comment.
*/
bool ASFormatter::isBeforeMultipleLineEndComments(int startPos) const
{
bool foundMultipleLineEndComment = false;
size_t peekNum = currentLine.find_first_not_of(" \t", startPos + 1);
if (peekNum != string::npos)
{
if (currentLine.compare(peekNum, 2, "/*") == 0)
{
// comment must be closed on this line with nothing after it
size_t endNum = currentLine.find("*/", peekNum + 2);
if (endNum != string::npos)
{
size_t nextChar = currentLine.find_first_not_of(" \t", endNum + 2);
if (nextChar != string::npos
&& currentLine.compare(nextChar, 2, "//") == 0)
foundMultipleLineEndComment = true;
}
}
}
return foundMultipleLineEndComment;
}
/**
* get the next character, increasing the current placement in the process.
* the new character is inserted into the variable currentChar.
*
* @return whether succeded to recieve the new character.
*/
bool ASFormatter::getNextChar()
{
isInLineBreak = false;
previousChar = currentChar;
if (!isWhiteSpace(currentChar))
{
previousNonWSChar = currentChar;
if (!isInComment && !isInLineComment && !isInQuote
&& !isImmediatelyPostComment
&& !isImmediatelyPostLineComment
&& !isInPreprocessor
&& !isSequenceReached("/*")
&& !isSequenceReached("//"))
previousCommandChar = currentChar;
}
if (charNum + 1 < (int) currentLine.length()
&& (!isWhiteSpace(peekNextChar()) || isInComment || isInLineComment))
{
currentChar = currentLine[++charNum];
if (shouldConvertTabs && currentChar == '\t')
convertTabToSpaces();
return true;
}
// end of line has been reached
return getNextLine();
}
/**
* get the next line of input, increasing the current placement in the process.
*
* @param sequence the sequence to append.
* @return whether succeded in reading the next line.
*/
bool ASFormatter::getNextLine(bool emptyLineWasDeleted /*false*/)
{
if (sourceIterator->hasMoreLines())
{
if (appendOpeningBracket)
currentLine = "{"; // append bracket that was removed from the previous line
else
{
currentLine = sourceIterator->nextLine(emptyLineWasDeleted);
assert(computeChecksumIn(currentLine));
}
// reset variables for new line
inLineNumber++;
if (endOfAsmReached)
endOfAsmReached = isInAsm = false;
shouldKeepLineUnbroken = false;
isInCommentStartLine = false;
isInCase = false;
isInAsmOneLine = false;
isInQuoteContinuation = isInVerbatimQuote | haveLineContinuationChar;
haveLineContinuationChar= false;
isImmediatelyPostEmptyLine = lineIsEmpty;
previousChar = ' ';
if (currentLine.length() == 0)
currentLine = string(" "); // a null is inserted if this is not done
// unless reading in the first line of the file, break a new line.
if (!isVirgin)
isInLineBreak = true;
else
isVirgin = false;
// TODO: FIX FOR BROKEN CASE STATEMANTS - RELEASE 2.02.1
// REMOVE AT AN APPROPRIATE TIME
if (currentHeader == &AS_CASE
&& isInLineBreak
&& !isImmediatelyPostLineComment)
{
// check for split line
if ((formattedLine.length() >= 4
&& formattedLine.substr(formattedLine.length() - 4, 4) == "case")
|| (formattedLine[formattedLine.length() - 1] == '\''
&& findNextChar(currentLine, ':') != string::npos)
)
{
isInLineBreak = false;
isInCase = true;
if (formattedLine.substr(formattedLine.length() - 4, 4) == "case")
appendSpacePad();
}
}
// END OF FIX
if (isImmediatelyPostNonInStmt)
{
isCharImmediatelyPostNonInStmt = true;
isImmediatelyPostNonInStmt = false;
}
// check if is in preprocessor before line trimming
// a blank line after a \ will remove the flag
isImmediatelyPostPreprocessor = isInPreprocessor;
if (!isInComment
&& (previousNonWSChar != '\\'
|| isEmptyLine(currentLine)))
isInPreprocessor = false;
if (passedSemicolon)
isInExecSQL = false;
initNewLine();
currentChar = currentLine[charNum];
if (isInHorstmannRunIn && previousNonWSChar == '{' && !isInComment)
isInLineBreak = false;
isInHorstmannRunIn = false;
if (shouldConvertTabs && currentChar == '\t')
convertTabToSpaces();
// check for an empty line inside a command bracket.
// if yes then read the next line (calls getNextLine recursively).
// must be after initNewLine.
if (shouldDeleteEmptyLines
&& lineIsEmpty
&& isBracketType((*bracketTypeStack)[bracketTypeStack->size()-1], COMMAND_TYPE))
{
if (!shouldBreakBlocks || previousNonWSChar == '{' || !commentAndHeaderFollows())
{
isInPreprocessor = isImmediatelyPostPreprocessor; // restore
lineIsEmpty = false;
return getNextLine(true);
}
}
return true;
}
else
{
endOfCodeReached = true;
return false;
}
}
/**
* jump over the leading white space in the current line,
* IF the line does not begin a comment or is in a preprocessor definition.
*/
void ASFormatter::initNewLine()
{
assert(getTabLength() > 0);
size_t len = currentLine.length();
size_t tabSize = getTabLength();
charNum = 0;
// don't trim these
if (isInQuoteContinuation
|| (isInPreprocessor && !getPreprocessorIndent()))
return;
// SQL continuation lines must be adjusted so the leading spaces
// is equivalent to the opening EXEC SQL
if (isInExecSQL)
{
// replace leading tabs with spaces
// so that continuation indent will be spaces
size_t tabCount_ = 0;
size_t i;
for (i = 0; i < currentLine.length(); i++)
{
if (!isWhiteSpace(currentLine[i])) // stop at first text
break;
if (currentLine[i] == '\t')
{
size_t numSpaces = tabSize - ((tabCount_ + i) % tabSize);
currentLine.replace(i, 1, numSpaces, ' ');
tabCount_++;
i += tabSize - 1;
}
}
// this will correct the format if EXEC SQL is not a hanging indent
trimContinuationLine();
return;
}
// comment continuation lines must be adjusted so the leading spaces
// is equivalent to the opening comment
if (isInComment)
{
if (noTrimCommentContinuation)
leadingSpaces = tabIncrementIn = 0;
trimContinuationLine();
return;
}
// compute leading spaces
isImmediatelyPostCommentOnly = lineIsLineCommentOnly || lineEndsInCommentOnly;
lineIsLineCommentOnly = false;
lineEndsInCommentOnly = false;
doesLineStartComment = false;
currentLineBeginsWithBracket = false;
lineIsEmpty = false;
currentLineFirstBracketNum = string::npos;
tabIncrementIn = 0;
// bypass whitespace at the start of a line
// preprocessor tabs are replaced later in the program
for (charNum = 0; isWhiteSpace(currentLine[charNum]) && charNum + 1 < (int) len; charNum++)
{
if (currentLine[charNum] == '\t' && !isInPreprocessor)
tabIncrementIn += tabSize - 1 - ((tabIncrementIn + charNum) % tabSize);
}
leadingSpaces = charNum + tabIncrementIn;
if (isSequenceReached("/*"))
{
doesLineStartComment = true;
}
else if (isSequenceReached("//"))
{
lineIsLineCommentOnly = true;
}
else if (isSequenceReached("{"))
{
currentLineBeginsWithBracket = true;
currentLineFirstBracketNum = charNum;
size_t firstText = currentLine.find_first_not_of(" \t", charNum + 1);
if (firstText != string::npos)
{
if (currentLine.compare(firstText, 2, "//") == 0)
lineIsLineCommentOnly = true;
else if (currentLine.compare(firstText, 2, "/*") == 0
|| isExecSQL(currentLine, firstText))
{
// get the extra adjustment
size_t j;
for (j = charNum + 1; isWhiteSpace(currentLine[j]) && j < firstText; j++)
{
if (currentLine[j] == '\t')
tabIncrementIn += tabSize - 1 - ((tabIncrementIn + j) % tabSize);
}
leadingSpaces = j + tabIncrementIn;
if (currentLine.compare(firstText, 2, "/*") == 0)
doesLineStartComment = true;
}
}
}
else if (isWhiteSpace(currentLine[charNum]) && !(charNum + 1 < (int) currentLine.length()))
{
lineIsEmpty = true;
}
// do not trim indented preprocessor define (except for comment continuation lines)
if (isInPreprocessor)
{
if (!doesLineStartComment)
leadingSpaces = 0;
charNum = 0;
}
}
/**
* Append a character to the current formatted line.
*
* @param char the character to append.
* @param canBreakLine if true, a registered line-break
*/
void ASFormatter::appendChar(char ch, bool canBreakLine)
{
if (canBreakLine && isInLineBreak)
breakLine();
formattedLine.append(1, ch);
isImmediatelyPostCommentOnly = false;
if (maxCodeLength != string::npos)
{
updateFormattedLineSplitPoints(ch);
testForTimeToSplitFormattedLine(1);
}
}
/**
* Append a string sequence to the current formatted line.
*
* @param sequence the sequence to append.
* @param canBreakLine if true, a registered line-break
*/
void ASFormatter::appendSequence(const string &sequence, bool canBreakLine)
{
if (canBreakLine && isInLineBreak)
breakLine();
formattedLine.append(sequence);
if (maxCodeLength != string::npos)
{
updateFormattedLineSplitPointSequence(sequence);
testForTimeToSplitFormattedLine(sequence.length());
}
}
/**
* append a space to the current formattedline, UNLESS the
* last character is already a white-space character.
*/
void ASFormatter::appendSpacePad()
{
int len = formattedLine.length();
if (len > 0 && !isWhiteSpace(formattedLine[len-1]))
{
formattedLine.append(1, ' ');
spacePadNum++;
if (maxCodeLength != string::npos)
{
updateFormattedLineSplitPoints(' ');
testForTimeToSplitFormattedLine(1);
}
}
}
/**
* append a space to the current formattedline, UNLESS the
* next character is already a white-space character.
*/
void ASFormatter::appendSpaceAfter()
{
int len = currentLine.length();
if (charNum + 1 < len && !isWhiteSpace(currentLine[charNum+1]))
{
formattedLine.append(1, ' ');
spacePadNum++;
if (maxCodeLength != string::npos)
{
updateFormattedLineSplitPoints(' ');
testForTimeToSplitFormattedLine(1);
}
}
}
/**
* register a line break for the formatted line.
*/
void ASFormatter::breakLine(bool isSplitLine /*false*/)
{
isLineReady = true;
isInLineBreak = false;
spacePadNum = nextLineSpacePadNum;
nextLineSpacePadNum = 0;
readyFormattedLine = formattedLine;
formattedLine = "";
if (!isSplitLine)
{
formattedLineCommentNum = string::npos;
clearFormattedLineSplitPoints();
}
// queue an empty line prepend request if one exists
prependEmptyLine = isPrependPostBlockEmptyLineRequested;
if (!isSplitLine && isAppendPostBlockEmptyLineRequested)
{
isAppendPostBlockEmptyLineRequested = false;
isPrependPostBlockEmptyLineRequested = true;
}
else
isPrependPostBlockEmptyLineRequested = false;
}
/**
* check if the currently reached open-bracket (i.e. '{')
* opens a:
* - a definition type block (such as a class or namespace),
* - a command block (such as a method block)
* - a static array
* this method takes for granted that the current character
* is an opening bracket.
*
* @return the type of the opened block.
*/
BracketType ASFormatter::getBracketType()
{
assert(currentChar == '{');
BracketType returnVal;
if ((previousNonWSChar == '='
|| isBracketType(bracketTypeStack->back(), ARRAY_TYPE))
&& previousCommandChar != ')')
returnVal = ARRAY_TYPE;
else if (foundPreDefinitionHeader && previousCommandChar != ')')
{
returnVal = DEFINITION_TYPE;
if (foundNamespaceHeader)
returnVal = (BracketType)(returnVal | NAMESPACE_TYPE);
else if (foundClassHeader)
returnVal = (BracketType)(returnVal | CLASS_TYPE);
else if (foundStructHeader)
returnVal = (BracketType)(returnVal | STRUCT_TYPE);
else if (foundInterfaceHeader)
returnVal = (BracketType)(returnVal | INTERFACE_TYPE);
}
else
{
bool isCommandType = (foundPreCommandHeader
|| (currentHeader != NULL && isNonParenHeader)
|| (previousCommandChar == ')')
|| (previousCommandChar == ':' && !foundQuestionMark)
|| (previousCommandChar == ';')
|| ((previousCommandChar == '{' || previousCommandChar == '}')
&& isPreviousBracketBlockRelated)
|| isJavaStaticConstructor
|| isSharpDelegate);
// C# methods containing 'get', 'set', 'add', and 'remove' do NOT end with parens
if (!isCommandType && isSharpStyle() && isNextWordSharpNonParenHeader(charNum + 1))
{
isCommandType = true;
isSharpAccessor = true;
}
if (!isCommandType && isInExtern)
returnVal = EXTERN_TYPE;
else
returnVal = (isCommandType ? COMMAND_TYPE : ARRAY_TYPE);
}
int foundOneLineBlock = isOneLineBlockReached(currentLine, charNum);
// this assumes each array definition is on a single line
// (foundOneLineBlock == 2) is a one line block followed by a comma
if (foundOneLineBlock == 2 && returnVal == COMMAND_TYPE)
returnVal = ARRAY_TYPE;
if (foundOneLineBlock > 0) // found one line block
returnVal = (BracketType)(returnVal | SINGLE_LINE_TYPE);
if (isBracketType(returnVal, ARRAY_TYPE) && isNonInStatementArrayBracket())
{
returnVal = (BracketType)(returnVal | ARRAY_NIS_TYPE);
isNonInStatementArray = true;
nonInStatementBracket = formattedLine.length() - 1;
}
return returnVal;
}
/**
* check if a line is empty
*
* @return whether line is empty
*/
bool ASFormatter::isEmptyLine(const string &line) const
{
return line.find_first_not_of(" \t") == string::npos;
}
/**
* Check if the currently reached '*', '&' or '^' character is
* a pointer-or-reference symbol, or another operator.
* A pointer dereference (*) or an "address of" character (&)
* counts as a pointer or reference because it is not an
* arithmetic operator.
*
* @return whether current character is a reference-or-pointer
*/
bool ASFormatter::isPointerOrReference() const
{
assert(currentChar == '*' || currentChar == '&' || currentChar == '^');
if (isJavaStyle())
return false;
if ((currentChar == '&' && previousChar == '&')
|| isCharImmediatelyPostOperator)
return false;
if (previousNonWSChar == '='
|| previousNonWSChar == '('
|| previousNonWSChar == '['
|| isCharImmediatelyPostReturn
|| isInTemplate
|| isCharImmediatelyPostTemplate
|| currentHeader == &AS_CATCH)
return true;
// get the last legal word (may be a number)
string lastWord = getPreviousWord(currentLine, charNum);
if (lastWord.empty())
lastWord = " ";
char nextChar = peekNextChar();
// check for preceding or following numeric values
if (isDigit(lastWord[0])
|| isDigit(nextChar)
|| nextChar == '!')
return false;
if (isBracketType(bracketTypeStack->back(), ARRAY_TYPE)
&& isLegalNameChar(lastWord[0])
&& isLegalNameChar(nextChar)
&& previousNonWSChar != ')')
{
if (isArrayOperator())
return false;
}
// checks on operators in parens
if (parenStack->back() > 0
&& isLegalNameChar(lastWord[0])
&& isLegalNameChar(nextChar))
{
// if followed by an assignment it is a pointer or reference
const string* followingOperator = getFollowingOperator();
if (followingOperator
&& followingOperator != &AS_MULT
&& followingOperator != &AS_BIT_AND)
{
if (followingOperator == &AS_ASSIGN)
return true;
else
return false;
}
if (!isBracketType(bracketTypeStack->back(), COMMAND_TYPE))
return true;
else
return false;
}
// checks on operators in parens with following '('
if (parenStack->back() > 0
&& nextChar == '('
&& previousNonWSChar != ','
&& previousNonWSChar != '('
&& previousNonWSChar != '!'
&& previousNonWSChar != '&'
&& previousNonWSChar != '*'
&& previousNonWSChar != '|')
return false;
if (nextChar == '-'
|| nextChar == '+')
{
size_t nextNum = currentLine.find_first_not_of(" \t", charNum + 1);
if (nextNum != string::npos)
{
if (currentLine.compare(nextNum, 2, "++") != 0
&& currentLine.compare(nextNum, 2, "--") != 0)
return false;
}
}
bool isPR = (!isInPotentialCalculation
|| isBracketType(bracketTypeStack->back(), DEFINITION_TYPE)
|| (!isLegalNameChar(previousNonWSChar)
&& !(previousNonWSChar == ')' && nextChar == '(')
&& !(previousNonWSChar == ')' && currentChar == '*' && !isImmediatelyPostCast())
&& previousNonWSChar != ']')
);
if (!isPR)
{
isPR |= (!isWhiteSpace(nextChar)
&& nextChar != '-'
&& nextChar != '('
&& nextChar != '['
&& !isLegalNameChar(nextChar));
}
return isPR;
}
/**
* Check if the currently reached '*' or '&' character is
* a dereferenced pointer or "address of" symbol.
* NOTE: this MUST be a pointer or reference as determined by
* the function isPointerOrReference().
*
* @return whether current character is a dereference or address of
*/
bool ASFormatter::isDereferenceOrAddressOf() const
{
assert(currentChar == '*' || currentChar == '&' || currentChar == '^');
if (isCharImmediatelyPostTemplate)
return false;
if (previousNonWSChar == '='
|| previousNonWSChar == ','
|| previousNonWSChar == '.'
|| previousNonWSChar == '{'
|| previousNonWSChar == '>'
|| previousNonWSChar == '<'
|| isCharImmediatelyPostLineComment
|| isCharImmediatelyPostComment
|| isCharImmediatelyPostReturn)
return true;
// check for **
if (currentChar == '*'
&& (int) currentLine.length() > charNum
&& currentLine[charNum+1] == '*')
{
if (previousNonWSChar == '(')
return true;
if ((int) currentLine.length() < charNum + 2)
return true;
return false;
}
// check first char on the line
if (charNum == (int) currentLine.find_first_not_of(" \t"))
return true;
char nextChar = peekNextChar();
if (nextChar == ')' || nextChar == '>' || nextChar == ',')
return false;
// check for reference to a pointer *& (cannot have &*)
if (( currentChar == '*' && nextChar == '&')
|| (previousNonWSChar == '*' && currentChar == '&'))
return false;
if (!isBracketType(bracketTypeStack->back(), COMMAND_TYPE)
&& parenStack->back() == 0)
return false;
string lastWord = getPreviousWord(currentLine, charNum);
if (lastWord == "else" || lastWord == "delete")
return true;
bool isDA = (!(isLegalNameChar(previousNonWSChar) || previousNonWSChar == '>')
|| (!isLegalNameChar(nextChar) && nextChar != '/')
|| (ispunct((unsigned char)previousNonWSChar) && previousNonWSChar != '.')
|| isCharImmediatelyPostReturn);
return isDA;
}
/**
* Check if the currently reached '*' or '&' character is
* centered with one space on each side.
* Only spaces are checked, not tabs.
* If true then a space will be deleted on the output.
*
* @return whether current character is centered.
*/
bool ASFormatter::isPointerOrReferenceCentered() const
{
assert(currentLine[charNum] == '*' || currentLine[charNum] == '&' || currentLine[charNum] == '^');
int prNum = charNum;
int lineLength = (int) currentLine.length();
// check for end of line
if (peekNextChar() == ' ')
return false;
// check space before
if (prNum < 1
|| currentLine[prNum-1] != ' ')
return false;
// check no space before that
if (prNum < 2
|| currentLine[prNum-2] == ' ')
return false;
// check for **
if (prNum + 1 < lineLength
&& currentLine[prNum+1] == '*')
prNum++;
// check space after
if (prNum + 1 <= lineLength
&& currentLine[prNum+1] != ' ')
return false;
// check no space after that
if (prNum + 2 < lineLength
&& currentLine[prNum+2] == ' ')
return false;
return true;
}
/**
* check if the currently reached '+' or '-' character is a unary operator
* this method takes for granted that the current character
* is a '+' or '-'.
*
* @return whether the current '+' or '-' is a unary operator.
*/
bool ASFormatter::isUnaryOperator() const
{
assert(currentChar == '+' || currentChar == '-');
return ((isCharImmediatelyPostReturn || !isLegalNameChar(previousCommandChar))
&& previousCommandChar != '.'
&& previousCommandChar != '\"'
&& previousCommandChar != '\''
&& previousCommandChar != ')'
&& previousCommandChar != ']');
}
/**
* check if the currently reached '+' or '-' character is
* part of an exponent, i.e. 0.2E-5.
*
* this method takes for granted that the current character
* is a '+' or '-'.
*
* @return whether the current '+' or '-' is in an exponent.
*/
bool ASFormatter::isInExponent() const
{
assert(currentChar == '+' || currentChar == '-');
int formattedLineLength = formattedLine.length();
if (formattedLineLength >= 2)
{
char prevPrevFormattedChar = formattedLine[formattedLineLength - 2];
char prevFormattedChar = formattedLine[formattedLineLength - 1];
return ((prevFormattedChar == 'e' || prevFormattedChar == 'E')
&& (prevPrevFormattedChar == '.' || isDigit(prevPrevFormattedChar)));
}
else
return false;
}
/**
* check if an array bracket should NOT have an in-statement indent
*
* @return the array is non in-statement
*/
bool ASFormatter::isNonInStatementArrayBracket() const
{
bool returnVal = false;
char nextChar = peekNextChar();
// if this opening bracket begins the line there will be no inStatement indent
if (currentLineBeginsWithBracket
&& charNum == (int) currentLineFirstBracketNum
&& nextChar != '}')
returnVal = true;
// if an opening bracket ends the line there will be no inStatement indent
if (isWhiteSpace(nextChar)
|| isBeforeAnyLineEndComment(charNum)
|| nextChar == '{')
returnVal = true;
// Java "new Type [] {...}" IS an inStatement indent
if (isJavaStyle() && previousNonWSChar == ']')
returnVal = false;
// trace
//if (isNonInStatementArray)
// cout << traceLineNumber << " " << 'x' << endl;
//else
// cout << traceLineNumber << " " << ' ' << endl
return returnVal;
}
/**
* check if a one-line bracket has been reached,
* i.e. if the currently reached '{' character is closed
* with a complimentry '}' elsewhere on the current line,
*.
* @return 0 = one-line bracket has not been reached.
* 1 = one-line bracket has been reached.
* 2 = one-line bracket has been reached and is followed by a comma.
*/
int ASFormatter::isOneLineBlockReached(string &line, int startChar) const
{
assert(line[startChar] == '{');
bool isInComment_ = false;
bool isInQuote_ = false;
int bracketCount = 1;
int lineLength = line.length();
char quoteChar_ = ' ';
char ch = ' ';
char prevCh = ' ';
for (int i = startChar + 1; i < lineLength; ++i)
{
ch = line[i];
if (isInComment_)
{
if (line.compare(i, 2, "*/") == 0)
{
isInComment_ = false;
++i;
}
continue;
}
if (ch == '\\')
{
++i;
continue;
}
if (isInQuote_)
{
if (ch == quoteChar_)
isInQuote_ = false;
continue;
}
if (ch == '"' || ch == '\'')
{
isInQuote_ = true;
quoteChar_ = ch;
continue;
}
if (line.compare(i, 2, "//") == 0)
break;
if (line.compare(i, 2, "/*") == 0)
{
isInComment_ = true;
++i;
continue;
}
if (ch == '{')
++bracketCount;
else if (ch == '}')
--bracketCount;
if (bracketCount == 0)
{
// is this an array?
if (parenStack->back() == 0 && prevCh != '}')
{
size_t peekNum = line.find_first_not_of(" \t", i + 1);
if (peekNum != string::npos && line[peekNum] == ',')
return 2;
}
return 1;
}
if (!isWhiteSpace(ch))
prevCh = ch;
}
return 0;
}
/**
* peek at the next word to determine if it is a C# non-paren header.
* will look ahead in the input file if necessary.
*
* @param char position on currentLine to start the search
* @return true if the next word is get or set.
*/
bool ASFormatter::isNextWordSharpNonParenHeader(int startChar) const
{
// look ahead to find the next non-comment text
string nextText = peekNextText(currentLine.substr(startChar));
if (nextText.length() == 0)
return false;
if (nextText[0] == '[')
return true;
if (!isCharPotentialHeader(nextText, 0))
return false;
if (findKeyword(nextText, 0, AS_GET) || findKeyword(nextText, 0, AS_SET)
|| findKeyword(nextText, 0, AS_ADD) || findKeyword(nextText, 0, AS_REMOVE))
return true;
return false;
}
/**
* peek at the next char to determine if it is an opening bracket.
* will look ahead in the input file if necessary.
* this determines a java static constructor.
*
* @param char position on currentLine to start the search
* @return true if the next word is an opening bracket.
*/
bool ASFormatter::isNextCharOpeningBracket(int startChar) const
{
bool retVal = false;
string nextText = peekNextText(currentLine.substr(startChar));
if (nextText.compare(0, 1, "{") == 0)
retVal = true;
return retVal;
}
/**
* get the next non-whitespace substring on following lines, bypassing all comments.
*
* @param the first line to check
* @return the next non-whitespace substring.
*/
string ASFormatter::peekNextText(const string &firstLine, bool endOnEmptyLine /*false*/, bool shouldReset /*false*/) const
{
bool isFirstLine = true;
bool needReset = shouldReset;
string nextLine_ = firstLine;
size_t firstChar= string::npos;
// find the first non-blank text, bypassing all comments.
bool isInComment_ = false;
while (sourceIterator->hasMoreLines())
{
if (isFirstLine)
isFirstLine = false;
else
{
nextLine_ = sourceIterator->peekNextLine();
needReset = true;
}
firstChar = nextLine_.find_first_not_of(" \t");
if (firstChar == string::npos)
{
if (endOnEmptyLine && !isInComment_)
break;
continue;
}
if (nextLine_.compare(firstChar, 2, "/*") == 0)
{
firstChar += 2;
isInComment_ = true;
}
if (isInComment_)
{
firstChar = nextLine_.find("*/", firstChar);
if (firstChar == string::npos)
continue;
firstChar += 2;
isInComment_ = false;
firstChar = nextLine_.find_first_not_of(" \t", firstChar);
if (firstChar == string::npos)
continue;
}
if (nextLine_.compare(firstChar, 2, "//") == 0)
continue;
// found the next text
break;
}
if (needReset)
sourceIterator->peekReset();
if (firstChar == string::npos)
nextLine_ = "";
else
nextLine_ = nextLine_.substr(firstChar);
return nextLine_;
}
/**
* adjust comment position because of adding or deleting spaces
* the spaces are added or deleted to formattedLine
* spacePadNum contains the adjustment
*/
void ASFormatter::adjustComments(void)
{
assert(spacePadNum != 0);
assert(currentLine.compare(charNum, 2, "//") == 0
|| currentLine.compare(charNum, 2, "/*") == 0);
// block comment must be closed on this line with nothing after it
if (currentLine.compare(charNum, 2, "/*") == 0)
{
size_t endNum = currentLine.find("*/", charNum + 2);
if (endNum == string::npos)
return;
if (currentLine.find_first_not_of(" \t", endNum + 2) != string::npos)
return;
}
size_t len = formattedLine.length();
// don't adjust a tab
if (formattedLine[len-1] == '\t')
return;
// if spaces were removed, need to add spaces before the comment
if (spacePadNum < 0)
{
int adjust = -spacePadNum; // make the number positive
formattedLine.append(adjust, ' ');
}
// if spaces were added, need to delete extra spaces before the comment
// if cannot be done put the comment one space after the last text
else if (spacePadNum > 0)
{
int adjust = spacePadNum;
size_t lastText = formattedLine.find_last_not_of(' ');
if (lastText != string::npos
&& lastText < len - adjust - 1)
formattedLine.resize(len - adjust);
else if (len > lastText + 2)
formattedLine.resize(lastText + 2);
else if (len < lastText + 2)
formattedLine.append(len - lastText, ' ');
}
}
/**
* append the current bracket inside the end of line comments
* currentChar contains the bracket, it will be appended to formattedLine
* formattedLineCommentNum is the comment location on formattedLine
*/
void ASFormatter::appendCharInsideComments(void)
{
if (formattedLineCommentNum == string::npos) // does the comment start on the previous line?
{
appendCurrentChar(); // don't attach
return;
}
assert(formattedLine.compare(formattedLineCommentNum, 2, "//") == 0
|| formattedLine.compare(formattedLineCommentNum, 2, "/*") == 0);
// find the previous non space char
size_t end = formattedLineCommentNum;
size_t beg = formattedLine.find_last_not_of(" \t", end-1);
if (beg == string::npos)
{
appendCurrentChar(); // don't attach
return;
}
beg++;
// insert the bracket
if (end - beg < 3) // is there room to insert?
formattedLine.insert(beg, 3 - end+beg, ' ');
if (formattedLine[beg] == '\t') // don't pad with a tab
formattedLine.insert(beg, 1, ' ');
formattedLine[beg+1] = currentChar;
testForTimeToSplitFormattedLine();
if (isBeforeComment())
breakLine();
else if (isCharImmediatelyPostLineComment)
shouldBreakLineAtNextChar = true;
return;
}
/**
* add or remove space padding to operators
* currentChar contains the paren
* the operators and necessary padding will be appended to formattedLine
* the calling function should have a continue statement after calling this method
*
* @param *newOperator the operator to be padded
*/
void ASFormatter::padOperators(const string* newOperator)
{
assert(newOperator != NULL);
bool shouldPad = (newOperator != &AS_COLON_COLON
&& newOperator != &AS_PLUS_PLUS
&& newOperator != &AS_MINUS_MINUS
&& newOperator != &AS_NOT
&& newOperator != &AS_BIT_NOT
&& newOperator != &AS_ARROW
&& !(newOperator == &AS_MINUS && isInExponent())
&& !((newOperator == &AS_PLUS || newOperator == &AS_MINUS) // check for unary plus or minus
&& (previousNonWSChar == '('
|| previousNonWSChar == '['
|| previousNonWSChar == '='
|| previousNonWSChar == ','))
&& !(newOperator == &AS_PLUS && isInExponent())
&& !isCharImmediatelyPostOperator
&& !((newOperator == &AS_MULT || newOperator == &AS_BIT_AND)
&& isPointerOrReference())
&& !(newOperator == &AS_MULT
&& (previousNonWSChar == '.'
|| previousNonWSChar == '>')) // check for ->
&& !((isInTemplate || isImmediatelyPostTemplate)
&& (newOperator == &AS_LS || newOperator == &AS_GR))
&& !(newOperator == &AS_GCC_MIN_ASSIGN
&& ASBase::peekNextChar(currentLine, charNum+1) == '>')
&& !(newOperator == &AS_GR && previousNonWSChar == '?')
&& !(newOperator == &AS_QUESTION // check for Java wildcard
&& (previousNonWSChar == '<'
|| ASBase::peekNextChar(currentLine, charNum) == '>'
|| ASBase::peekNextChar(currentLine, charNum) == '.'))
&& !isInCase
&& !isInAsm
&& !isInAsmOneLine
&& !isInAsmBlock
);
// pad before operator
if (shouldPad
&& !(newOperator == &AS_COLON
&& (!foundQuestionMark && !isInEnum) && currentHeader != &AS_FOR)
&& !(newOperator == &AS_QUESTION && isSharpStyle() // check for C# nullable type (e.g. int?)
&& currentLine.find(':', charNum+1) == string::npos)
)
appendSpacePad();
appendSequence(*newOperator);
goForward(newOperator->length() - 1);
currentChar = (*newOperator)[newOperator->length() - 1];
// pad after operator
// but do not pad after a '-' that is a unary-minus.
if (shouldPad
&& !isBeforeAnyComment()
&& !(newOperator == &AS_PLUS && isUnaryOperator())
&& !(newOperator == &AS_MINUS && isUnaryOperator())
&& !(currentLine.compare(charNum + 1, 1, ";") == 0)
&& !(currentLine.compare(charNum + 1, 2, "::") == 0)
&& !(newOperator == &AS_QUESTION && isSharpStyle() // check for C# nullable type (e.g. int?)
&& peekNextChar() == '[')
)
appendSpaceAfter();
previousOperator = newOperator;
return;
}
/**
* format pointer or reference
* currentChar contains the pointer or reference
* the symbol and necessary padding will be appended to formattedLine
* the calling function should have a continue statement after calling this method
*
* NOTE: Do NOT use appendCurrentChar() in this method. The line should not be
* broken once the calculation starts.
*/
void ASFormatter::formatPointerOrReference(void)
{
assert(currentChar == '*' || currentChar == '&' || currentChar == '^');
assert(!isJavaStyle());
int pa = pointerAlignment;
int ra = referenceAlignment;
int itemAlignment = (currentChar == '*' || currentChar == '^') ? pa : ((ra == REF_SAME_AS_PTR) ? pa : ra);
// check for cast
char peekedChar = peekNextChar();
if (currentChar == '*'
&& (int) currentLine.length() > charNum + 1
&& currentLine[charNum+1] == '*')
{
size_t nextChar = currentLine.find_first_not_of(" \t", charNum+2);
if (nextChar == string::npos)
peekedChar = ' ';
else
peekedChar = currentLine[nextChar];
}
if (peekedChar == ')' || peekedChar =='>' || peekedChar ==',')
{
formatPointerOrReferenceCast();
return;
}
// check for a padded space and remove it
if (charNum > 0
&& !isWhiteSpace(currentLine[charNum-1])
&& formattedLine.length() > 0
&& isWhiteSpace(formattedLine[formattedLine.length()-1]))
formattedLine.erase(formattedLine.length()-1);
// do this before bumping charNum
bool isOldPRCentered = isPointerOrReferenceCentered();
if (itemAlignment == PTR_ALIGN_TYPE)
{
size_t prevCh = formattedLine.find_last_not_of(" \t");
if (prevCh == string::npos)
prevCh = 0;
if (formattedLine.length() == 0 || prevCh == formattedLine.length() - 1)
formattedLine.append(1, currentChar);
else
{
// exchange * or & with character following the type
// this may not work every time with a tab character
string charSave = formattedLine.substr(prevCh+1, 1);
formattedLine[prevCh+1] = currentChar;
formattedLine.append(charSave);
}
if (isSequenceReached("**"))
{
if (formattedLine.length() == 1)
formattedLine.append("*");
else
formattedLine.insert(prevCh+2, "*");
goForward(1);
}
// if no space after * then add one
if (charNum < (int) currentLine.length() - 1
&& !isWhiteSpace(currentLine[charNum+1])
&& currentLine[charNum+1] != ')')
appendSpacePad();
// if old pointer or reference is centered, remove a space
if (isOldPRCentered
&& isWhiteSpace(formattedLine[formattedLine.length()-1]))
{
formattedLine.erase(formattedLine.length()-1, 1);
spacePadNum--;
}
}
else if (itemAlignment == PTR_ALIGN_MIDDLE)
{
// compute current whitespace before
size_t wsBefore = currentLine.find_last_not_of(" \t", charNum - 1);
if (wsBefore == string::npos)
wsBefore = 0;
else
wsBefore = charNum - wsBefore - 1;
string sequenceToInsert(1, currentChar);
if (isSequenceReached("**"))
{
sequenceToInsert = "**";
goForward(1);
}
// if reference to a pointer check for conflicting alignment
else if (currentChar == '*' && peekedChar == '&'
&& (referenceAlignment == REF_ALIGN_TYPE
|| referenceAlignment == REF_ALIGN_MIDDLE
|| referenceAlignment == REF_SAME_AS_PTR))
{
sequenceToInsert = "*&";
goForward(1);
for (size_t i = charNum; i < currentLine.length() - 1 && isWhiteSpace(currentLine[i]); i++)
goForward(1);
}
bool isAfterScopeResolution = previousNonWSChar == ':'; // check for ::
size_t charNumSave = charNum;
// if a comment follows don't align, just space pad
if (isBeforeAnyComment())
{
appendSpacePad();
formattedLine.append(sequenceToInsert);
appendSpaceAfter();
return;
}
// if this is not the last thing on the line
if ((int) currentLine.find_first_not_of(" \t", charNum + 1) > charNum)
{
// goForward() to convert tabs to spaces, if necessary,
// and move following characters to preceding characters
// this may not work every time with tab characters
for (size_t i = charNum+1; i < currentLine.length() && isWhiteSpace(currentLine[i]); i++)
{
goForward(1);
formattedLine.append(1, currentLine[i]);
}
}
// find space padding after
size_t wsAfter = currentLine.find_first_not_of(" \t", charNumSave + 1);
if (wsAfter == string::npos || isBeforeAnyComment())
wsAfter = 0;
else
wsAfter = wsAfter - charNumSave - 1;
// don't pad before scope resolution operator, but pad after
if (isAfterScopeResolution)
{
size_t lastText = formattedLine.find_last_not_of(" \t");
formattedLine.insert(lastText + 1, sequenceToInsert);
appendSpacePad();
}
// whitespace should be at least 2 chars to center
else
{
if (wsBefore + wsAfter < 2)
{
size_t charsToAppend = (2 - (wsBefore + wsAfter));
formattedLine.append(charsToAppend, ' ');
spacePadNum += charsToAppend;
if (wsBefore == 0) wsBefore++;
if (wsAfter == 0) wsAfter++;
}
// insert the pointer or reference char
size_t padAfter = (wsBefore + wsAfter) / 2;
formattedLine.insert(formattedLine.length() - padAfter, sequenceToInsert);
}
}
else if (itemAlignment == PTR_ALIGN_NAME)
{
size_t startNum = formattedLine.find_last_not_of(" \t");
string sequenceToInsert(1, currentChar);
if (isSequenceReached("**"))
{
sequenceToInsert = "**";
goForward(1);
}
// if reference to a pointer align both to type
else if (currentChar == '*' && peekedChar == '&')
{
sequenceToInsert = "*&";
goForward(1);
for (size_t i = charNum; i < currentLine.length() - 1 && isWhiteSpace(currentLine[i]); i++)
goForward(1);
}
bool isAfterScopeResolution = previousNonWSChar == ':'; // check for ::
// if this is not the last thing on the line
if (!isBeforeAnyComment()
&& (int) currentLine.find_first_not_of(" \t", charNum + 1) > charNum)
{
// goForward() to convert tabs to spaces, if necessary,
// and move following characters to preceding characters
// this may not work every time with tab characters
for (size_t i = charNum+1; i < currentLine.length() && isWhiteSpace(currentLine[i]); i++)
{
// if a padded paren follows don't move
if (shouldPadParensOutside && peekedChar == '(' && !isOldPRCentered)
break;
goForward(1);
formattedLine.append(1, currentLine[i]);
}
}
// don't pad before scope resolution operator
if (startNum != string::npos && isAfterScopeResolution)
{
size_t lastText = formattedLine.find_last_not_of(" \t");
if (lastText + 1 < formattedLine.length())
formattedLine.erase(lastText + 1);
}
// if no space before * then add one
else if (formattedLine.length() <= startNum + 1
|| !isWhiteSpace(formattedLine[startNum+1]))
{
formattedLine.insert(startNum+1 , 1, ' ');
spacePadNum++;
}
appendSequence(sequenceToInsert, false);
// if old pointer or reference is centered, remove a space
if (isOldPRCentered
&& formattedLine.length() > startNum+1
&& isWhiteSpace(formattedLine[startNum+1])
&& !isBeforeAnyComment())
{
formattedLine.erase(startNum+1, 1);
spacePadNum--;
}
}
else // pointerAlignment == PTR_ALIGN_NONE
{
formattedLine.append(1, currentChar);
}
return;
}
/**
* format pointer or reference cast
* currentChar contains the pointer or reference
* NOTE: the pointers and references in function definitions
* are processed as a cast (e.g. void foo(void*, void*))
* is processed here.
*/
void ASFormatter::formatPointerOrReferenceCast(void)
{
assert(currentChar == '*' || currentChar == '&' || currentChar == '^');
assert(!isJavaStyle());
int pa = pointerAlignment;
int ra = referenceAlignment;
int itemAlignment = (currentChar == '*' || currentChar == '^') ? pa : ((ra == REF_SAME_AS_PTR) ? pa : ra);
string sequenceToInsert(1, currentChar);
if (isSequenceReached("**"))
{
sequenceToInsert = "**";
goForward(1);
}
if (itemAlignment == PTR_ALIGN_NONE)
{
appendSequence(sequenceToInsert, false);
return;
}
// remove trailing whitespace
size_t prevCh = formattedLine.find_last_not_of(" \t");
if (prevCh == string::npos)
prevCh = 0;
if (prevCh + 1 < formattedLine.length()
&& isWhiteSpace(formattedLine[prevCh+1]))
{
spacePadNum -= (formattedLine.length() - 1 - prevCh);
formattedLine.erase(prevCh+1);
}
if (itemAlignment == PTR_ALIGN_MIDDLE
|| itemAlignment == PTR_ALIGN_NAME)
{
appendSpacePad();
appendSequence(sequenceToInsert, false);
}
else
appendSequence(sequenceToInsert, false);
}
/**
* add or remove space padding to parens
* currentChar contains the paren
* the parens and necessary padding will be appended to formattedLine
* the calling function should have a continue statement after calling this method
*/
void ASFormatter::padParens(void)
{
assert(currentChar == '(' || currentChar == ')');
int spacesOutsideToDelete = 0;
int spacesInsideToDelete = 0;
if (currentChar == '(')
{
spacesOutsideToDelete = formattedLine.length() - 1;
spacesInsideToDelete = 0;
// compute spaces outside the opening paren to delete
if (shouldUnPadParens)
{
char lastChar = ' ';
bool prevIsParenHeader = false;
size_t i = formattedLine.find_last_not_of(" \t");
if (i != string::npos)
{
// if last char is a bracket the previous whitespace is an indent
if (formattedLine[i] == '{')
spacesOutsideToDelete = 0;
else if (isCharImmediatelyPostPointerOrReference)
spacesOutsideToDelete = 0;
else
{
spacesOutsideToDelete -= i;
lastChar = formattedLine[i];
// if previous word is a header, it will be a paren header
string prevWord = getPreviousWord(formattedLine, formattedLine.length());
const string* prevWordH = NULL;
if (shouldPadHeader
&& prevWord.length() > 0
&& isCharPotentialHeader(prevWord, 0))
prevWordH = ASBeautifier::findHeader(prevWord, 0, headers);
if (prevWordH != NULL)
prevIsParenHeader = true;
else if (prevWord == "return") // don't unpad
prevIsParenHeader = true;
else if (isCStyle() && prevWord == "throw" && shouldPadHeader) // don't unpad
prevIsParenHeader = true;
else if (prevWord == "and" || prevWord == "or") // don't unpad
prevIsParenHeader = true;
// don't unpad variables
else if (prevWord == "bool"
|| prevWord == "int"
|| prevWord == "void"
|| prevWord == "void*"
|| (prevWord.length() >= 6 // check end of word for _t
&& prevWord.compare(prevWord.length()-2, 2, "_t") == 0)
|| prevWord == "BOOL"
|| prevWord == "DWORD"
|| prevWord == "HWND"
|| prevWord == "INT"
|| prevWord == "LPSTR"
|| prevWord == "VOID"
|| prevWord == "LPVOID"
)
{
prevIsParenHeader = true;
// trace
//cout << traceLineNumber << " " << prevWord << endl;
}
}
}
// do not unpad operators, but leave them if already padded
if (shouldPadParensOutside || prevIsParenHeader)
spacesOutsideToDelete--;
else if (lastChar == '|' // check for ||
|| lastChar == '&' // check for &&
|| lastChar == ','
|| (lastChar == '(' && shouldPadParensInside)
|| (lastChar == '>' && !foundCastOperator)
|| lastChar == '<'
|| lastChar == '?'
|| lastChar == ':'
|| lastChar == ';'
|| lastChar == '='
|| lastChar == '+'
|| lastChar == '-'
|| lastChar == '*'
|| lastChar == '/'
|| lastChar == '%'
|| lastChar == '^'
)
spacesOutsideToDelete--;
if (spacesOutsideToDelete > 0)
{
formattedLine.erase(i + 1, spacesOutsideToDelete);
spacePadNum -= spacesOutsideToDelete;
}
}
// pad open paren outside
if (shouldPadFirstParen && previousChar != '(')
appendSpacePad();
else if (shouldPadParensOutside)
{
char peekedCharOutside = peekNextChar();
if (!(currentChar == '(' && peekedCharOutside == ')'))
appendSpacePad();
}
appendCurrentChar();
// unpad open paren inside
if (shouldUnPadParens)
{
size_t j = currentLine.find_first_not_of(" \t", charNum + 1);
if (j != string::npos)
spacesInsideToDelete = j - charNum - 1;
if (shouldPadParensInside)
spacesInsideToDelete--;
if (spacesInsideToDelete > 0)
{
currentLine.erase(charNum + 1, spacesInsideToDelete);
spacePadNum -= spacesInsideToDelete;
}
// convert tab to space if requested
if (shouldConvertTabs
&& (int)currentLine.length() > charNum + 1
&& currentLine[charNum+1] == '\t')
currentLine[charNum+1] = ' ';
}
// pad open paren inside
char peekedCharInside = peekNextChar();
if (shouldPadParensInside)
if (!(currentChar == '(' && peekedCharInside == ')'))
appendSpaceAfter();
// trace
//if(spacesOutsideToDelete > 0 || spacesInsideToDelete > 0)
// cout << traceLineNumber << " " << spacesOutsideToDelete << '(' << spacesInsideToDelete << endl;
}
else if (currentChar == ')')
{
spacesOutsideToDelete = 0;
spacesInsideToDelete = formattedLine.length();
// unpad close paren inside
if (shouldUnPadParens)
{
size_t i = formattedLine.find_last_not_of(" \t");
if (i != string::npos)
spacesInsideToDelete = formattedLine.length() - 1 - i;
if (shouldPadParensInside)
spacesInsideToDelete--;
if (spacesInsideToDelete > 0)
{
formattedLine.erase(i + 1, spacesInsideToDelete);
spacePadNum -= spacesInsideToDelete;
}
}
// pad close paren inside
if (shouldPadParensInside)
if (!(previousChar == '(' && currentChar == ')'))
appendSpacePad();
appendCurrentChar();
// unpad close paren outside
// close parens outside are left unchanged
if (shouldUnPadParens)
{
//size_t j = currentLine.find_first_not_of(" \t", charNum + 1);
//if (j != string::npos)
// spacesOutsideToDelete = j - charNum - 1;
//if (shouldPadParensOutside)
// spacesOutsideToDelete--;
//if (spacesOutsideToDelete > 0)
//{
// currentLine.erase(charNum + 1, spacesOutsideToDelete);
// spacePadNum -= spacesOutsideToDelete;
//}
}
// pad close paren outside
char peekedCharOutside = peekNextChar();
if (shouldPadParensOutside)
if (peekedCharOutside != ';'
&& peekedCharOutside != ','
&& peekedCharOutside != '.'
&& peekedCharOutside != '-' // check for ->
&& peekedCharOutside != ']')
appendSpaceAfter();
// trace
//if(spacesInsideToDelete > 0)
// cout << traceLineNumber << " " << spacesInsideToDelete << ')' << 0 << endl;
}
return;
}
/**
* format opening bracket as attached or broken
* currentChar contains the bracket
* the brackets will be appended to the current formattedLine or a new formattedLine as necessary
* the calling function should have a continue statement after calling this method
*
* @param bracketType the type of bracket to be formatted.
*/
void ASFormatter::formatOpeningBracket(BracketType bracketType)
{
assert(!isBracketType(bracketType, ARRAY_TYPE));
assert(currentChar == '{');
parenStack->push_back(0);
bool breakBracket = isCurrentBracketBroken();
if (breakBracket)
{
if (isBeforeAnyComment() && isOkToBreakBlock(bracketType))
{
// if comment is at line end leave the comment on this line
if (isBeforeAnyLineEndComment(charNum) && !currentLineBeginsWithBracket) // lineBeginsWith('{')
{
currentChar = ' '; // remove bracket from current line
if (parenStack->size() > 1)
parenStack->pop_back();
currentLine[charNum] = currentChar;
appendOpeningBracket = true; // append bracket to following line
}
// else put comment after the bracket
else if (!isBeforeMultipleLineEndComments(charNum))
breakLine();
}
else if (!isBracketType(bracketType, SINGLE_LINE_TYPE))
breakLine();
else if (shouldBreakOneLineBlocks && peekNextChar() != '}')
breakLine();
else if (!isInLineBreak)
appendSpacePad();
appendCurrentChar();
// should a following comment break from the bracket?
// must break the line AFTER the bracket
if (isBeforeComment()
&& formattedLine.length() > 0
&& formattedLine[0] == '{'
&& isOkToBreakBlock(bracketType)
&& (bracketFormatMode == BREAK_MODE
|| bracketFormatMode == LINUX_MODE
|| bracketFormatMode == STROUSTRUP_MODE))
{
shouldBreakLineAtNextChar = true;
}
}
else // attach bracket
{
// are there comments before the bracket?
if (isCharImmediatelyPostComment || isCharImmediatelyPostLineComment)
{
if (isOkToBreakBlock(bracketType)
&& !(isCharImmediatelyPostComment && isCharImmediatelyPostLineComment) // don't attach if two comments on the line
&& !isImmediatelyPostPreprocessor
&& peekNextChar() != '}' // don't attach { }
&& previousCommandChar != '{' // don't attach { {
&& previousCommandChar != '}' // don't attach } {
&& previousCommandChar != ';') // don't attach ; {
{
appendCharInsideComments();
}
else
{
appendCurrentChar(); // don't attach
}
}
else if (previousCommandChar == '{'
|| previousCommandChar == '}'
|| previousCommandChar == ';') // '}' , ';' chars added for proper handling of '{' immediately after a '}' or ';'
{
appendCurrentChar(); // don't attach
}
else
{
// if a blank line preceeds this don't attach
if (isEmptyLine(formattedLine))
appendCurrentChar(); // don't attach
else if (isOkToBreakBlock(bracketType)
&& !(isImmediatelyPostPreprocessor
&& currentLineBeginsWithBracket)) // lineBeginsWith('{')
{
if (peekNextChar() != '}')
{
appendSpacePad();
appendCurrentChar(false); // OK to attach
testForTimeToSplitFormattedLine(); // line length will have changed
// should a following comment attach with the bracket?
// insert spaces to reposition the comment
if (isBeforeComment()
&& !isBeforeMultipleLineEndComments(charNum)
&& (!isBeforeAnyLineEndComment(charNum) || currentLineBeginsWithBracket)) // lineBeginsWith('{')
{
shouldBreakLineAtNextChar = true;
currentLine.insert(charNum+1, charNum+1, ' ');
}
}
else
{
appendSpacePad();
appendCurrentChar();
}
}
else
{
if (!isInLineBreak)
appendSpacePad();
appendCurrentChar(); // don't attach
}
}
}
}
/**
* format closing bracket
* currentChar contains the bracket
* the calling function should have a continue statement after calling this method
*
* @param bracketType the type of the opening bracket for this closing bracket.
*/
void ASFormatter::formatClosingBracket(BracketType bracketType)
{
assert(!isBracketType(bracketType, ARRAY_TYPE));
assert(currentChar == '}');
// parenStack must contain one entry
if (parenStack->size() > 1)
parenStack->pop_back();
// mark state of immediately after empty block
// this state will be used for locating brackets that appear immedately AFTER an empty block (e.g. '{} \n}').
if (previousCommandChar == '{')
isImmediatelyPostEmptyBlock = true;
if (shouldAttachClosingBracket)
{
// for now, namespaces and classes will be attached. Uncomment the lines below to break.
if ((isEmptyLine(formattedLine) // if a blank line preceeds this
|| isCharImmediatelyPostLineComment
|| isCharImmediatelyPostComment
|| (isImmediatelyPostPreprocessor && (int) currentLine.find_first_not_of(" \t") == charNum)
// || (isBracketType(bracketType, CLASS_TYPE) && isOkToBreakBlock(bracketType) && previousNonWSChar != '{')
// || (isBracketType(bracketType, NAMESPACE_TYPE) && isOkToBreakBlock(bracketType) && previousNonWSChar != '{')
)
&& (!isBracketType(bracketType, SINGLE_LINE_TYPE) || isOkToBreakBlock(bracketType)))
{
breakLine();
appendCurrentChar(); // don't attach
}
else
{
if (previousNonWSChar != '{'
&& (!isBracketType(bracketType, SINGLE_LINE_TYPE) || isOkToBreakBlock(bracketType)))
appendSpacePad();
appendCurrentChar(false); // attach
}
}
else if ((!(previousCommandChar == '{' && isPreviousBracketBlockRelated)) // this '{' does not close an empty block
&& isOkToBreakBlock(bracketType)) // astyle is allowed to break one line blocks
// && !isImmediatelyPostEmptyBlock) /* removed 9/5/10 */ // this '}' does not immediately follow an empty block
{
breakLine();
appendCurrentChar();
}
else
{
appendCurrentChar();
}
// if a declaration follows a definition, space pad
if (isLegalNameChar(peekNextChar()))
appendSpaceAfter();
if (shouldBreakBlocks && currentHeader != NULL && parenStack->back() == 0)
{
if (currentHeader == &AS_CASE || currentHeader == &AS_DEFAULT)
{
// do not yet insert a line if "break" statement is outside the brackets
string nextText = peekNextText(currentLine.substr(charNum+1));
if (nextText.substr(0, 5) != "break")
isAppendPostBlockEmptyLineRequested = true;
}
else
isAppendPostBlockEmptyLineRequested = true;
}
}
/**
* format array brackets as attached or broken
* determine if the brackets can have an inStatement indent
* currentChar contains the bracket
* the brackets will be appended to the current formattedLine or a new formattedLine as necessary
* the calling function should have a continue statement after calling this method
*
* @param bracketType the type of bracket to be formatted, must be an ARRAY_TYPE.
* @param isOpeningArrayBracket indicates if this is the opening bracket for the array block.
*/
void ASFormatter::formatArrayBrackets(BracketType bracketType, bool isOpeningArrayBracket)
{
assert(isBracketType(bracketType, ARRAY_TYPE));
assert(currentChar == '{' || currentChar == '}');
if (currentChar == '{')
{
// is this the first opening bracket in the array?
if (isOpeningArrayBracket)
{
if (bracketFormatMode == ATTACH_MODE
|| bracketFormatMode == LINUX_MODE
|| bracketFormatMode == STROUSTRUP_MODE)
{
// don't attach to a preprocessor directive
if (isImmediatelyPostPreprocessor && currentLineBeginsWithBracket) // lineBeginsWith('{')
{
isInLineBreak = true;
appendCurrentChar(); // don't attach
}
else if (isCharImmediatelyPostComment)
{
// TODO: attach bracket to line-end comment
appendCurrentChar(); // don't attach
}
else if (isCharImmediatelyPostLineComment && !isBracketType(bracketType, SINGLE_LINE_TYPE))
{
appendCharInsideComments();
}
else
{
// if a blank line preceeds this don't attach
if (isEmptyLine(formattedLine))
appendCurrentChar(); // don't attach
else
{
// if bracket is broken or not an assignment
if (currentLineBeginsWithBracket // lineBeginsWith('{')
&& !isBracketType(bracketType, SINGLE_LINE_TYPE))
{
appendSpacePad();
appendCurrentChar(false); // OK to attach
// TODO: debug the following line
testForTimeToSplitFormattedLine(); // line length will have changed
if (currentLineBeginsWithBracket
&& (int)currentLineFirstBracketNum == charNum)
shouldBreakLineAtNextChar = true;
}
else
{
appendSpacePad();
appendCurrentChar();
}
}
}
}
else if (bracketFormatMode == BREAK_MODE)
{
if (isWhiteSpace(peekNextChar()))
breakLine();
else if (isBeforeAnyComment())
{
// do not break unless comment is at line end
if (isBeforeAnyLineEndComment(charNum) && !currentLineBeginsWithBracket)
{
currentChar = ' '; // remove bracket from current line
appendOpeningBracket = true; // append bracket to following line
}
}
if (!isInLineBreak)
appendSpacePad();
appendCurrentChar();
if (currentLineBeginsWithBracket
&& (int)currentLineFirstBracketNum == charNum
&& !isBracketType(bracketType, SINGLE_LINE_TYPE))
shouldBreakLineAtNextChar = true;
}
else if (bracketFormatMode == RUN_IN_MODE)
{
if (isWhiteSpace(peekNextChar()))
breakLine();
else if (isBeforeAnyComment())
{
// do not break unless comment is at line end
if (isBeforeAnyLineEndComment(charNum) && !currentLineBeginsWithBracket) // lineBeginsWith('{')
{
currentChar = ' '; // remove bracket from current line
appendOpeningBracket = true; // append bracket to following line
}
}
if (!isInLineBreak)
appendSpacePad();
appendCurrentChar();
}
else if (bracketFormatMode == NONE_MODE)
{
if (currentLineBeginsWithBracket) // lineBeginsWith('{')
{
appendCurrentChar(); // don't attach
}
else
{
appendSpacePad();
appendCurrentChar(false); // OK to attach
}
}
}
else // not the first opening bracket
{
if (bracketFormatMode == RUN_IN_MODE)
{
if (previousNonWSChar == '{'
&& bracketTypeStack->size() > 2
&& !isBracketType((*bracketTypeStack)[bracketTypeStack->size()-2], SINGLE_LINE_TYPE))
formatArrayRunIn();
}
else if (!isInLineBreak
&& !isWhiteSpace(peekNextChar())
&& previousNonWSChar == '{'
&& bracketTypeStack->size() > 2
&& !isBracketType((*bracketTypeStack)[bracketTypeStack->size()-2], SINGLE_LINE_TYPE))
formatArrayRunIn();
appendCurrentChar();
}
}
else if (currentChar == '}')
{
if (shouldAttachClosingBracket)
{
if (isEmptyLine(formattedLine) // if a blank line preceeds this
|| isImmediatelyPostPreprocessor
|| isCharImmediatelyPostLineComment
|| isCharImmediatelyPostComment)
appendCurrentChar(); // don't attach
else
{
appendSpacePad();
appendCurrentChar(false); // attach
}
}
else
{
// does this close the first opening bracket in the array?
// must check if the block is still a single line because of anonymous statements
if (!isBracketType(bracketType, SINGLE_LINE_TYPE)
|| formattedLine.find('{') == string::npos)
breakLine();
appendCurrentChar();
}
// if a declaration follows an enum definition, space pad
char peekedChar = peekNextChar();
if (isLegalNameChar(peekedChar)
|| peekedChar == '[')
appendSpaceAfter();
}
}
/**
* determine if a run-in can be attached.
* if it can insert the indents in formattedLine and reset the current line break.
*/
void ASFormatter::formatRunIn()
{
assert(bracketFormatMode == RUN_IN_MODE || bracketFormatMode == NONE_MODE);
// keep one line blocks returns true without indenting the run-in
if (!isOkToBreakBlock(bracketTypeStack->back()))
return; // true;
// make sure the line begins with a bracket
size_t lastText = formattedLine.find_last_not_of(" \t");
if (lastText == string::npos || formattedLine[lastText] != '{')
return; // false;
// make sure the bracket is broken
if (formattedLine.find_first_not_of(" \t{") != string::npos)
return; // false;
if (isBracketType(bracketTypeStack->back(), NAMESPACE_TYPE))
return; // false;
bool extraIndent = false;
isInLineBreak = true;
// cannot attach a class modifier without indent-classes
if (isCStyle()
&& isCharPotentialHeader(currentLine, charNum)
&& (isBracketType(bracketTypeStack->back(), CLASS_TYPE)
|| (isBracketType(bracketTypeStack->back(), STRUCT_TYPE)
&& isInIndentableStruct)))
{
if (findKeyword(currentLine, charNum, AS_PUBLIC)
|| findKeyword(currentLine, charNum, AS_PRIVATE)
|| findKeyword(currentLine, charNum, AS_PROTECTED))
{
if (!getClassIndent())
return; // false;
}
else if (getClassIndent())
extraIndent = true;
}
// cannot attach a 'case' statement without indent-switches
if (!getSwitchIndent()
&& isCharPotentialHeader(currentLine, charNum)
&& (findKeyword(currentLine, charNum, AS_CASE)
|| findKeyword(currentLine, charNum, AS_DEFAULT)))
return; // false;
// extra indent for switch statements
if (getSwitchIndent()
&& !preBracketHeaderStack->empty()
&& preBracketHeaderStack->back() == &AS_SWITCH
&& ((isLegalNameChar(currentChar)
&& !findKeyword(currentLine, charNum, AS_CASE))
|| isSequenceReached("//")
|| isSequenceReached("/*")))
extraIndent = true;
isInLineBreak = false;
// remove for extra whitespace
if (formattedLine.length() > lastText+1
&& formattedLine.find_first_not_of(" \t", lastText+1) == string::npos)
formattedLine.erase(lastText+1);
if (getForceTabIndentation() && getIndentLength() != getTabLength())
{
// insert the space indents
string indent;
int indentLength_ = getIndentLength();
int tabLength_ = getTabLength();
indent.append(indentLength_, ' ');
if (extraIndent)
indent.append(indentLength_, ' ');
// replace spaces indents with tab indents
size_t tabCount = indent.length() / tabLength_; // truncate extra spaces
indent.erase(0U, tabCount * tabLength_);
indent.insert(0U, tabCount, '\t');
horstmannIndentChars = indentLength_;
if (indent[0] == ' ') // allow for bracket
indent.erase(0, 1);
formattedLine.append(indent);
}
else if (getIndentString() == "\t")
{
appendChar('\t', false);
horstmannIndentChars = 2; // one for { and one for tab
if (extraIndent)
{
appendChar('\t', false);
horstmannIndentChars++;
}
}
else // spaces
{
int indentLength_ = getIndentLength();
formattedLine.append(indentLength_ - 1, ' ');
horstmannIndentChars = indentLength_;
if (extraIndent)
{
formattedLine.append(indentLength_, ' ');
horstmannIndentChars += indentLength_;
}
}
isInHorstmannRunIn = true;
}
/**
* remove whitepace and add indentation for an array run-in.
*/
void ASFormatter::formatArrayRunIn()
{
assert(isBracketType(bracketTypeStack->back(), ARRAY_TYPE));
// make sure the bracket is broken
if (formattedLine.find_first_not_of(" \t{") != string::npos)
return;
size_t lastText = formattedLine.find_last_not_of(" \t");
if (lastText == string::npos || formattedLine[lastText] != '{')
return;
// check for extra whitespace
if (formattedLine.length() > lastText+1
&& formattedLine.find_first_not_of(" \t", lastText+1) == string::npos)
formattedLine.erase(lastText+1);
if (getIndentString() == "\t")
{
appendChar('\t', false);
horstmannIndentChars = 2; // one for { and one for tab
}
else
{
int indent = getIndentLength();
formattedLine.append(indent-1, ' ');
horstmannIndentChars = indent;
}
isInHorstmannRunIn = true;
isInLineBreak = false;
}
/**
* delete a bracketTypeStack vector object
* BracketTypeStack did not work with the DeleteContainer template
*/
void ASFormatter::deleteContainer(vector<BracketType>* &container)
{
if (container != NULL)
{
container->clear();
delete (container);
container = NULL;
}
}
/**
* delete a vector object
* T is the type of vector
* used for all vectors except bracketTypeStack
*/
template<typename T>
void ASFormatter::deleteContainer(T &container)
{
if (container != NULL)
{
container->clear();
delete (container);
container = NULL;
}
}
/**
* initialize a BracketType vector object
* BracketType did not work with the DeleteContainer template
*/
void ASFormatter::initContainer(vector<BracketType>* &container, vector<BracketType>* value)
{
if (container != NULL)
deleteContainer(container);
container = value;
}
/**
* initialize a vector object
* T is the type of vector
* used for all vectors except bracketTypeStack
*/
template<typename T>
void ASFormatter::initContainer(T &container, T value)
{
// since the ASFormatter object is never deleted,
// the existing vectors must be deleted before creating new ones
if (container != NULL)
deleteContainer(container);
container = value;
}
/**
* convert a tab to spaces.
* charNum points to the current character to convert to spaces.
* tabIncrementIn is the increment that must be added for tab indent characters
* to get the correct column for the current tab.
* replaces the tab in currentLine with the required number of spaces.
* replaces the value of currentChar.
*/
void ASFormatter::convertTabToSpaces()
{
assert(currentLine[charNum] == '\t');
assert(getTabLength() > 0);
// do NOT replace if in quotes
if (isInQuote || isInQuoteContinuation)
return;
size_t tabSize = getTabLength();
size_t numSpaces = tabSize - ((tabIncrementIn + charNum) % tabSize);
currentLine.replace(charNum, 1, numSpaces, ' ');
currentChar = currentLine[charNum];
}
/**
* is it ok to break this block?
*/
bool ASFormatter::isOkToBreakBlock(BracketType bracketType) const
{
// Actually, there should not be an ARRAY_TYPE bracket here.
// But this will avoid breaking a one line block when there is.
// Otherwise they will be formatted differently on consecutive runs.
if (isBracketType(bracketType, ARRAY_TYPE)
&& isBracketType(bracketType, SINGLE_LINE_TYPE))
return false;
if (!isBracketType(bracketType, SINGLE_LINE_TYPE)
|| shouldBreakOneLineBlocks
|| breakCurrentOneLineBlock)
return true;
return false;
}
/**
* check if a sharp header is a paren or nonparen header
*/
bool ASFormatter::isSharpStyleWithParen(const string* header) const
{
if (isSharpStyle() && peekNextChar() == '('
&& (header == &AS_CATCH
|| header == &AS_DELEGATE))
return true;
return false;
}
/**
* check for a following header when a comment is reached.
* if a header follows, the comments are kept as part of the header block.
* firstLine must contain the start of the comment.
*/
void ASFormatter::checkForHeaderFollowingComment(const string &firstLine)
{
assert(isInComment || isInLineComment);
// this is called ONLY IF shouldBreakBlocks is TRUE.
assert(shouldBreakBlocks);
// look ahead to find the next non-comment text
bool endOnEmptyLine = (currentHeader == NULL);
string nextText = peekNextText(firstLine, endOnEmptyLine);
if (nextText.length() == 0 || !isCharPotentialHeader(nextText, 0))
return;
const string* newHeader = ASBeautifier::findHeader(nextText, 0, headers);
if (newHeader == NULL)
return;
// if a closing header, reset break unless break is requested
if (isClosingHeader(newHeader))
{
if (!shouldBreakClosingHeaderBlocks)
isPrependPostBlockEmptyLineRequested = false;
}
// if an opening header, break before the comment
else
{
isPrependPostBlockEmptyLineRequested = true;
}
}
/**
* process preprocessor statements.
* charNum should be the index of the #.
*
* delete bracketTypeStack entries added by #if if a #else is found.
* prevents double entries in the bracketTypeStack.
*/
void ASFormatter::processPreprocessor()
{
assert(currentChar == '#');
const size_t preproc = currentLine.find_first_not_of(" \t", charNum + 1);
if (preproc == string::npos)
return;
if (currentLine.compare(preproc, 2, "if") == 0)
{
preprocBracketTypeStackSize = bracketTypeStack->size();
}
else if (currentLine.compare(preproc, 4, "else") == 0)
{
// delete stack entries added in #if
// should be replaced by #else
if (preprocBracketTypeStackSize > 0)
{
int addedPreproc = bracketTypeStack->size() - preprocBracketTypeStackSize;
for (int i=0; i < addedPreproc; i++)
bracketTypeStack->pop_back();
}
}
}
/**
* determine if the next line starts a comment
* and a header follows the comment or comments.
*/
bool ASFormatter::commentAndHeaderFollows()
{
// called ONLY IF shouldDeleteEmptyLines and shouldBreakBlocks are TRUE.
assert(shouldDeleteEmptyLines && shouldBreakBlocks);
// is the next line a comment
if (!sourceIterator->hasMoreLines())
return false;
string nextLine_ = sourceIterator->peekNextLine();
size_t firstChar = nextLine_.find_first_not_of(" \t");
if (firstChar == string::npos
|| !(nextLine_.compare(firstChar, 2, "//") == 0
|| nextLine_.compare(firstChar, 2, "/*") == 0))
{
sourceIterator->peekReset();
return false;
}
// find the next non-comment text, and reset
string nextText = peekNextText(nextLine_, false, true);
if (nextText.length() == 0 || !isCharPotentialHeader(nextText, 0))
return false;
const string* newHeader = ASBeautifier::findHeader(nextText, 0, headers);
if (newHeader == NULL)
return false;
// if a closing header, reset break unless break is requested
if (isClosingHeader(newHeader) && !shouldBreakClosingHeaderBlocks)
{
isAppendPostBlockEmptyLineRequested = false;
return false;
}
return true;
}
/**
* determine if a bracket should be attached or broken
* uses brackets in the bracketTypeStack
* the last bracket in the bracketTypeStack is the one being formatted
* returns true if the bracket should be broken
*/
bool ASFormatter::isCurrentBracketBroken() const
{
assert(bracketTypeStack->size() > 1);
bool breakBracket = false;
size_t bracketTypeStackEnd = bracketTypeStack->size()-1;
if (isBracketType((*bracketTypeStack)[bracketTypeStackEnd], EXTERN_TYPE))
{
if (currentLineBeginsWithBracket
|| bracketFormatMode == RUN_IN_MODE)
breakBracket = true;
}
else if (bracketFormatMode == NONE_MODE)
{
if (currentLineBeginsWithBracket
&& (int)currentLineFirstBracketNum == charNum) // lineBeginsWith('{')
breakBracket = true;
}
else if (bracketFormatMode == BREAK_MODE || bracketFormatMode == RUN_IN_MODE)
{
breakBracket = true;
}
else if (bracketFormatMode == LINUX_MODE || bracketFormatMode == STROUSTRUP_MODE)
{
// break a class if Linux
if (isBracketType((*bracketTypeStack)[bracketTypeStackEnd], CLASS_TYPE))
{
if (bracketFormatMode == LINUX_MODE)
breakBracket = true;
}
// break a namespace or interface if Linux
else if (isBracketType((*bracketTypeStack)[bracketTypeStackEnd], NAMESPACE_TYPE)
|| isBracketType((*bracketTypeStack)[bracketTypeStackEnd], INTERFACE_TYPE))
{
if (bracketFormatMode == LINUX_MODE)
breakBracket = true;
}
// break the first bracket if a function
else if (bracketTypeStackEnd == 1
&& isBracketType((*bracketTypeStack)[bracketTypeStackEnd], COMMAND_TYPE))
{
breakBracket = true;
}
else if (bracketTypeStackEnd > 1)
{
// break the first bracket after a namespace or extern if a function
if (isBracketType((*bracketTypeStack)[bracketTypeStackEnd-1], NAMESPACE_TYPE)
|| isBracketType((*bracketTypeStack)[bracketTypeStackEnd-1], EXTERN_TYPE))
{
if (isBracketType((*bracketTypeStack)[bracketTypeStackEnd], COMMAND_TYPE))
breakBracket = true;
}
// if not C style then break the first bracket after a class if a function
else if (!isCStyle())
{
if ((isBracketType((*bracketTypeStack)[bracketTypeStackEnd-1], CLASS_TYPE)
|| isBracketType((*bracketTypeStack)[bracketTypeStackEnd-1], ARRAY_TYPE)
|| isBracketType((*bracketTypeStack)[bracketTypeStackEnd-1], STRUCT_TYPE))
&& isBracketType((*bracketTypeStack)[bracketTypeStackEnd], COMMAND_TYPE))
breakBracket = true;
}
}
}
return breakBracket;
}
/**
* format comment body
* the calling function should have a continue statement after calling this method
*/
void ASFormatter::formatCommentBody()
{
assert(isInComment);
if (isSequenceReached("*/"))
{
isInComment = false;
noTrimCommentContinuation = false;
isImmediatelyPostComment = true;
appendSequence(AS_CLOSE_COMMENT);
goForward(1);
if (doesLineStartComment
&& (currentLine.find_first_not_of(" \t", charNum+1) == string::npos))
lineEndsInCommentOnly = true;
if (peekNextChar() == '}'
&& previousCommandChar != ';'
&& !isBracketType(bracketTypeStack->back(), ARRAY_TYPE)
&& !isInPreprocessor
&& isOkToBreakBlock(bracketTypeStack->back()))
{
isInLineBreak = true;
shouldBreakLineAtNextChar = true;
}
}
else
{
appendCurrentChar();
// append the comment up to the next tab or comment end
// tabs must be checked for convert-tabs before appending
while (charNum + 1 < (int) currentLine.length()
&& !isLineReady
&& currentLine[charNum+1] != '\t'
&& currentLine.compare(charNum+1, 2, "*/") != 0)
{
currentChar = currentLine[++charNum];
appendCurrentChar();
}
}
}
/**
* format a comment opener
* the comment opener will be appended to the current formattedLine or a new formattedLine as necessary
* the calling function should have a continue statement after calling this method
*/
void ASFormatter::formatCommentOpener()
{
assert(isSequenceReached("/*"));
isInComment = isInCommentStartLine = true;
isImmediatelyPostLineComment = false;
if (spacePadNum != 0 && !isInLineBreak)
adjustComments();
formattedLineCommentNum = formattedLine.length();
// must be done BEFORE appendSequence
if (previousCommandChar == '{'
&& !isImmediatelyPostComment
&& !isImmediatelyPostLineComment)
{
if (bracketFormatMode == NONE_MODE)
{
// should a run-in statement be attached?
if (currentLineBeginsWithBracket)
formatRunIn();
}
else if (bracketFormatMode == ATTACH_MODE)
{
// if the bracket was not attached?
if (formattedLine.length() > 0 && formattedLine[0] == '{'
&& !isBracketType(bracketTypeStack->back(), SINGLE_LINE_TYPE))
isInLineBreak = true;
}
else if (bracketFormatMode == RUN_IN_MODE)
{
// should a run-in statement be attached?
if (formattedLine.length() > 0 && formattedLine[0] == '{')
formatRunIn();
}
}
else if (!doesLineStartComment)
noTrimCommentContinuation = true;
// appendSequence will write the previous line
appendSequence(AS_OPEN_COMMENT);
goForward(1);
// must be done AFTER appendSequence
if (shouldBreakBlocks)
{
// break before the comment if a header follows the comment
// for speed, do not check if previous line is empty,
// if previous line is a line comment or if previous line is '{'
if (doesLineStartComment
&& !isImmediatelyPostEmptyLine
&& !isImmediatelyPostCommentOnly
&& previousCommandChar != '{')
{
checkForHeaderFollowingComment(currentLine.substr(charNum-1));
}
}
if (previousCommandChar == '}')
currentHeader = NULL;
}
/**
* format a line comment body
* the calling function should have a continue statement after calling this method
*/
void ASFormatter::formatLineCommentBody()
{
assert(isInLineComment);
appendCurrentChar();
// append the comment up to the next tab
// tabs must be checked for convert-tabs before appending
while (charNum + 1 < (int) currentLine.length()
&& !isLineReady
&& currentLine[charNum+1] != '\t')
{
currentChar = currentLine[++charNum];
appendCurrentChar();
}
// explicitely break a line when a line comment's end is found.
if (charNum + 1 == (int) currentLine.length())
{
isInLineBreak = true;
isInLineComment = false;
isImmediatelyPostLineComment = true;
currentChar = 0; //make sure it is a neutral char.
}
}
/**
* format a line comment opener
* the line comment opener will be appended to the current formattedLine or a new formattedLine as necessary
* the calling function should have a continue statement after calling this method
*/
void ASFormatter::formatLineCommentOpener()
{
assert(isSequenceReached("//"));
if ((int)currentLine.length() > charNum + 2
&& currentLine[charNum+2] == '\xf2') // check for windows line marker
isAppendPostBlockEmptyLineRequested = false;
isInLineComment = true;
isCharImmediatelyPostComment = false;
// do not indent if in column 1 or 2
if (!shouldIndentCol1Comments && !lineCommentNoIndent)
{
if (charNum == 0)
lineCommentNoIndent = true;
else if (charNum == 1 && currentLine[0] == ' ')
lineCommentNoIndent = true;
}
// move comment if spaces were added or deleted
if (lineCommentNoIndent == false && spacePadNum != 0 && !isInLineBreak)
adjustComments();
formattedLineCommentNum = formattedLine.length();
// must be done BEFORE appendSequence
// check for run-in statement
if (previousCommandChar == '{'
&& !isImmediatelyPostComment
&& !isImmediatelyPostLineComment)
{
if (bracketFormatMode == NONE_MODE)
{
if (currentLineBeginsWithBracket)
formatRunIn();
}
else if (bracketFormatMode == RUN_IN_MODE)
{
if (!lineCommentNoIndent)
formatRunIn();
else
isInLineBreak = true;
}
else if (bracketFormatMode == BREAK_MODE)
{
if (formattedLine.length() > 0 && formattedLine[0] == '{')
isInLineBreak = true;
}
else
{
if (currentLineBeginsWithBracket)
isInLineBreak = true;
}
}
// appendSequence will write the previous line
appendSequence(AS_OPEN_LINE_COMMENT);
goForward(1);
if (formattedLine.compare(0, 2, "//") == 0)
lineIsLineCommentOnly = true;
// must be done AFTER appendSequence
if (shouldBreakBlocks)
{
// break before the comment if a header follows the line comment
// for speed, do not check if previous line is empty,
// if previous line is a comment or if previous line is '{'
if (lineIsLineCommentOnly
&& previousCommandChar != '{'
&& !isImmediatelyPostEmptyLine
&& !isImmediatelyPostCommentOnly)
{
checkForHeaderFollowingComment(currentLine.substr(charNum-1));
}
}
if (previousCommandChar == '}')
currentHeader = NULL;
// if tabbed input don't convert the immediately following tabs to spaces
if (getIndentString() == "\t" && lineCommentNoIndent)
{
while (charNum + 1 < (int) currentLine.length()
&& currentLine[charNum+1] == '\t')
{
currentChar = currentLine[++charNum];
appendCurrentChar();
}
}
// explicitely break a line when a line comment's end is found.
if (charNum + 1 == (int) currentLine.length())
{
isInLineBreak = true;
isInLineComment = false;
isImmediatelyPostLineComment = true;
currentChar = 0; //make sure it is a neutral char.
}
}
/**
* format quote body
* the calling function should have a continue statement after calling this method
*/
void ASFormatter::formatQuoteBody()
{
assert(isInQuote);
if (isSpecialChar)
{
isSpecialChar = false;
}
else if (currentChar == '\\' && !isInVerbatimQuote)
{
if (peekNextChar() == ' ') // is this '\' at end of line
haveLineContinuationChar = true;
else
isSpecialChar = true;
}
else if (isInVerbatimQuote && currentChar == '"')
{
if (peekNextChar() == '"') // check consecutive quotes
{
appendSequence("\"\"");
goForward(1);
return;
}
else
{
isInQuote = false;
isInVerbatimQuote = false;
}
}
else if (quoteChar == currentChar)
{
isInQuote = false;
}
appendCurrentChar();
// append the text to the ending quoteChar or an escape sequence
// tabs in quotes are NOT changed by convert-tabs
if (isInQuote && currentChar != '\\')
{
while (charNum + 1 < (int) currentLine.length()
&& currentLine[charNum+1] != quoteChar
&& currentLine[charNum+1] != '\\')
{
currentChar = currentLine[++charNum];
appendCurrentChar();
}
}
}
/**
* format a quote opener
* the quote opener will be appended to the current formattedLine or a new formattedLine as necessary
* the calling function should have a continue statement after calling this method
*/
void ASFormatter::formatQuoteOpener()
{
assert(currentChar == '"' || currentChar == '\'');
isInQuote = true;
quoteChar = currentChar;
if (isSharpStyle() && previousChar == '@')
isInVerbatimQuote = true;
// a quote following a bracket is an array
if (previousCommandChar == '{'
&& !isImmediatelyPostComment
&& !isImmediatelyPostLineComment
&& isNonInStatementArray
&& !isBracketType(bracketTypeStack->back(), SINGLE_LINE_TYPE)
&& !isWhiteSpace(peekNextChar()))
{
if (bracketFormatMode == NONE_MODE)
{
if (currentLineBeginsWithBracket)
formatRunIn();
}
else if (bracketFormatMode == RUN_IN_MODE)
{
formatRunIn();
}
else if (bracketFormatMode == BREAK_MODE)
{
if (formattedLine.length() > 0 && formattedLine[0] == '{')
isInLineBreak = true;
}
else
{
if (currentLineBeginsWithBracket)
isInLineBreak = true;
}
}
previousCommandChar = ' ';
appendCurrentChar();
}
/**
* get the next line comment adjustment that results from breaking a closing bracket.
* the bracket must be on the same line as the closing header.
* i.e "} else" changed to "} \n else".
*/
int ASFormatter::getNextLineCommentAdjustment()
{
assert(foundClosingHeader && previousNonWSChar == '}');
if (charNum < 1) // "else" is in column 1
return 0;
size_t lastBracket = currentLine.rfind('}', charNum - 1);
if (lastBracket != string::npos)
return (lastBracket - charNum); // return a negative number
return 0;
}
// for console build only
LineEndFormat ASFormatter::getLineEndFormat() const
{
return lineEnd;
}
/**
* get the current line comment adjustment that results from attaching
* a closing header to a closing bracket.
* the bracket must be on the line previous to the closing header.
* the adjustment is 2 chars, one for the bracket and one for the space.
* i.e "} \n else" changed to "} else".
*/
int ASFormatter::getCurrentLineCommentAdjustment()
{
assert(foundClosingHeader && previousNonWSChar == '}');
if (charNum < 1)
return 2;
size_t lastBracket = currentLine.rfind('}', charNum - 1);
if (lastBracket == string::npos)
return 2;
return 0;
}
/**
* get the previous word on a line
* the argument 'currPos' must point to the current position.
*
* @return is the previous word or an empty string if none found.
*/
string ASFormatter::getPreviousWord(const string &line, int currPos) const
{
// get the last legal word (may be a number)
if (currPos == 0)
return string();
size_t end = line.find_last_not_of(" \t", currPos-1);
if (end == string::npos || !isLegalNameChar(line[end]))
return string();
int start; // start of the previous word
for (start = end; start > -1; start--)
{
if (!isLegalNameChar(line[start]) || line[start] == '.')
break;
}
start++;
return (line.substr(start, end-start+1));
}
/**
* check if a line break is needed when a closing bracket
* is followed by a closing header.
* the break depends on the bracketFormatMode and other factors.
*/
void ASFormatter::isLineBreakBeforeClosingHeader()
{
assert(foundClosingHeader && previousNonWSChar == '}');
if (bracketFormatMode == BREAK_MODE
|| bracketFormatMode == RUN_IN_MODE
|| shouldAttachClosingBracket)
{
isInLineBreak = true;
}
else if (bracketFormatMode == NONE_MODE)
{
if (shouldBreakClosingHeaderBrackets
|| getBracketIndent() || getBlockIndent())
{
isInLineBreak = true;
}
else
{
appendSpacePad();
// is closing bracket broken?
size_t i = currentLine.find_first_not_of(" \t");
if (i != string::npos && currentLine[i] == '}')
isInLineBreak = false;
if (shouldBreakBlocks)
isAppendPostBlockEmptyLineRequested = false;
}
}
// bracketFormatMode == ATTACH_MODE, LINUX_MODE, STROUSTRUP_MODE
else
{
if (shouldBreakClosingHeaderBrackets
|| getBracketIndent() || getBlockIndent())
{
isInLineBreak = true;
}
else
{
// if a blank line does not preceed this
// or last line is not a one line block, attach header
bool previousLineIsEmpty = isEmptyLine(formattedLine);
int previousLineIsOneLineBlock = 0;
size_t firstBracket = findNextChar(formattedLine, '{');
if (firstBracket != string::npos)
previousLineIsOneLineBlock = isOneLineBlockReached(formattedLine, firstBracket);
if (!previousLineIsEmpty
&& previousLineIsOneLineBlock == 0)
{
isInLineBreak = false;
appendSpacePad();
spacePadNum = 0; // don't count as comment padding
}
if (shouldBreakBlocks)
isAppendPostBlockEmptyLineRequested = false;
}
}
}
/**
* Add brackets to a single line statement following a header.
* Brackets are not added if the proper conditions are not met.
* Brackets are added to the currentLine.
*/
bool ASFormatter::addBracketsToStatement()
{
assert(isImmediatelyPostHeader);
if (currentHeader != &AS_IF
&& currentHeader != &AS_ELSE
&& currentHeader != &AS_FOR
&& currentHeader != &AS_WHILE
&& currentHeader != &AS_DO
&& currentHeader != &AS_FOREACH)
return false;
if (currentHeader == &AS_WHILE && foundClosingHeader) // do-while
return false;
// do not bracket an empty statement
if (currentChar == ';')
return false;
// do not add if a header follows (i.e. else if)
if (isCharPotentialHeader(currentLine, charNum))
if (findHeader(headers) != NULL)
return false;
// find the next semi-colon
size_t nextSemiColon = charNum;
if (currentChar != ';')
nextSemiColon = findNextChar(currentLine, ';', charNum+1);
if (nextSemiColon == string::npos)
return false;
// add closing bracket before changing the line length
if (nextSemiColon == currentLine.length() - 1)
currentLine.append(" }");
else
currentLine.insert(nextSemiColon + 1, " }");
// add opening bracket
currentLine.insert(charNum, "{ ");
assert(computeChecksumIn("{}"));
currentChar = '{';
// remove extra spaces
if (!shouldAddOneLineBrackets)
{
size_t lastText = formattedLine.find_last_not_of(" \t");
if ((formattedLine.length() - 1) - lastText > 1)
formattedLine.erase(lastText + 1);
}
return true;
}
/**
* Find the next character that is not in quotes or a comment.
*
* @param line the line to be searched.
* @param searchChar the char to find.
* @param searchStart the start position on the line (default is 0).
* @return the position on the line or string::npos if not found.
*/
size_t ASFormatter::findNextChar(string &line, char searchChar, int searchStart /*0*/)
{
// find the next searchChar
size_t i;
for (i = searchStart; i < line.length(); i++)
{
if (line.compare(i, 2, "//") == 0)
return string::npos;
if (line.compare(i, 2, "/*") == 0)
{
size_t endComment = line.find("*/", i+2);
if (endComment == string::npos)
return string::npos;
i = endComment + 2;
if (i >= line.length())
return string::npos;
}
if (line[i] == '\'' || line[i] == '\"')
{
char quote = line[i];
while (i < line.length())
{
size_t endQuote = line.find(quote, i+1);
if (endQuote == string::npos)
return string::npos;
i = endQuote;
if (line[endQuote-1] != '\\') // check for '\"'
break;
if (line[endQuote-2] == '\\') // check for '\\'
break;
}
}
if (line[i] == searchChar)
break;
// for now don't process C# 'delegate' brackets
// do this last in case the search char is a '{'
if (line[i] == '{')
return string::npos;
}
if (i >= line.length()) // didn't find searchChar
return string::npos;
return i;
}
/**
* Look ahead in the file to see if a struct has access modifiers.
*
* @param line a reference to the line to indent.
* @param index the current line index.
* @return true if the struct has access modifiers.
*/
bool ASFormatter::isStructAccessModified(string &firstLine, size_t index) const
{
assert(firstLine[index] == '{');
assert(isCStyle());
bool isFirstLine = true;
bool needReset = false;
size_t bracketCount = 1;
string nextLine_ = firstLine.substr(index + 1);
// find the first non-blank text, bypassing all comments and quotes.
bool isInComment_ = false;
bool isInQuote_ = false;
char quoteChar_ = ' ';
while (sourceIterator->hasMoreLines())
{
if (isFirstLine)
isFirstLine = false;
else
{
nextLine_ = sourceIterator->peekNextLine();
needReset = true;
}
// parse the line
for (size_t i = 0; i < nextLine_.length(); i++)
{
if (isWhiteSpace(nextLine_[i]))
continue;
if (nextLine_.compare(i, 2, "/*") == 0)
isInComment_ = true;
if (isInComment_)
{
if (nextLine_.compare(i, 2, "*/") == 0)
{
isInComment_ = false;
++i;
}
continue;
}
if (nextLine_[i] == '\\')
{
++i;
continue;
}
if (isInQuote_)
{
if (nextLine_[i] == quoteChar_)
isInQuote_ = false;
continue;
}
if (nextLine_[i] == '"' || nextLine_[i] == '\'')
{
isInQuote_ = true;
quoteChar_ = nextLine_[i];
continue;
}
if (nextLine_.compare(i, 2, "//") == 0)
{
i = nextLine_.length();
continue;
}
// handle brackets
if (nextLine_[i] == '{')
++bracketCount;
if (nextLine_[i] == '}')
--bracketCount;
if (bracketCount == 0)
{
if (needReset)
sourceIterator->peekReset();
return false;
}
// check for access modifiers
if (isCharPotentialHeader(nextLine_, i))
{
if (findKeyword(nextLine_, i, AS_PUBLIC)
|| findKeyword(nextLine_, i, AS_PRIVATE)
|| findKeyword(nextLine_, i, AS_PROTECTED))
{
if (needReset)
sourceIterator->peekReset();
return true;
}
string name = getCurrentWord(nextLine_, i);
i += name.length() - 1;
}
} // end of for loop
} // end of while loop
if (needReset)
sourceIterator->peekReset();
return false;
}
/**
* Check to see if this is an EXEC SQL statement.
*
* @param line a reference to the line to indent.
* @param index the current line index.
* @return true if the statement is EXEC SQL.
*/
bool ASFormatter::isExecSQL(string &line, size_t index) const
{
if (line[index] != 'e' && line[index] != 'E') // quick check to reject most
return false;
string word;
if (isCharPotentialHeader(line, index))
word = getCurrentWord(line, index);
for (size_t i = 0; i < word.length(); i++)
word[i] = (char) toupper(word[i]);
if (word != "EXEC")
return false;
size_t index2 = index + word.length();
index2 = line.find_first_not_of(" \t", index2);
if (index2 == string::npos)
return false;
word.erase();
if (isCharPotentialHeader(line, index2))
word = getCurrentWord(line, index2);
for (size_t i = 0; i < word.length(); i++)
word[i] = (char) toupper(word[i]);
if (word != "SQL")
return false;
return true;
}
/**
* The continuation lines must be adjusted so the leading spaces
* is equivalent to the text on the opening line.
*
* Updates currentLine and charNum.
*/
void ASFormatter::trimContinuationLine()
{
assert(getTabLength() > 0);
size_t len = currentLine.length();
size_t tabSize = getTabLength();
charNum = 0;
if (leadingSpaces > 0 && len > 0)
{
size_t i;
size_t continuationIncrementIn = 0;
for (i = 0; (i < len) && (i + continuationIncrementIn < leadingSpaces); i++)
{
if (!isWhiteSpace(currentLine[i])) // don't delete any text
{
if (i < continuationIncrementIn)
leadingSpaces = i + tabIncrementIn;
continuationIncrementIn = tabIncrementIn;
break;
}
if (currentLine[i] == '\t')
continuationIncrementIn += tabSize - 1 - ((continuationIncrementIn + i) % tabSize);
}
if ((int) continuationIncrementIn == tabIncrementIn)
charNum = i;
else
{
// build a new line with the equivalent leading chars
string newLine;
int leadingChars = 0;
if ((int) leadingSpaces > tabIncrementIn)
leadingChars = leadingSpaces - tabIncrementIn;
newLine.append(leadingChars, ' ');
newLine.append(currentLine, i, len-i);
currentLine = newLine;
charNum = leadingChars;
if (currentLine.length() == 0)
currentLine = string(" "); // a null is inserted if this is not done
}
if (i >= len)
charNum = 0;
}
return;
}
/**
* Determine if a header is a closing header
*
* @return true if the header is a closing header.
*/
bool ASFormatter::isClosingHeader(const string* header) const
{
return (header == &AS_ELSE
|| header == &AS_CATCH
|| header == &AS_FINALLY);
}
/**
* Determine if a * following a closing paren is immediately.
* after a cast. If so it is a dereference and not a multiply.
* e.g. "(int*) *ptr" is a dereference.
*/
bool ASFormatter::isImmediatelyPostCast() const
{
assert(previousNonWSChar == ')' && currentChar == '*');
// find preceeding closing paren
size_t paren = currentLine.rfind(")", charNum);
if (paren == string::npos || paren == 0)
return false;
// find character preceeding the closing paren
size_t lastChar = currentLine.find_last_not_of(" \t", paren-1);
if (lastChar == string::npos)
return false;
// check for pointer cast
if (currentLine[lastChar] == '*')
return true;
return false;
}
/**
* Determine if a < is a template definition or instantiation.
* Sets the class variables isInTemplate and templateDepth.
*/
void ASFormatter::checkIfTemplateOpener()
{
assert(!isInTemplate && currentChar == '<');
int parenDepth_ = 0;
int maxTemplateDepth = 0;
templateDepth = 0;
for (size_t i = charNum; i < currentLine.length(); i++)
{
char currentChar_ = currentLine[i];
if (isWhiteSpace(currentChar_))
continue;
if (currentChar_ == '<')
{
templateDepth++;
maxTemplateDepth++;
}
else if (currentChar_ == '>')
{
templateDepth--;
if (templateDepth == 0)
{
if (parenDepth_ == 0)
{
// this is a template!
isInTemplate = true;
templateDepth = maxTemplateDepth;
}
return;
}
}
else if (currentChar_ == '(' || currentChar_ == ')')
{
if (currentChar_ == '(')
parenDepth_++;
else
parenDepth_--;
continue;
}
else if (currentLine.compare(i, 2, "&&") == 0
|| currentLine.compare(i, 2, "||") == 0)
{
// this is not a template -> leave...
isInTemplate = false;
return;
}
else if (currentChar_ == ',' // comma, e.g. A<int, char>
|| currentChar_ == '&' // reference, e.g. A<int&>
|| currentChar_ == '*' // pointer, e.g. A<int*>
|| currentChar_ == '^' // C++/CLI managed pointer, e.g. A<int^>
|| currentChar_ == ':' // ::, e.g. std::string
|| currentChar_ == '=' // assign e.g. default parameter
|| currentChar_ == '[' // [] e.g. string[]
|| currentChar_ == ']' // [] e.g. string[]
|| currentChar_ == '(' // (...) e.g. function definition
|| currentChar_ == ')') // (...) e.g. function definition
{
continue;
}
else if (!isLegalNameChar(currentChar_) && currentChar_ != '?')
{
// this is not a template -> leave...
isInTemplate = false;
return;
}
}
}
void ASFormatter::updateFormattedLineSplitPoints(char appendedChar)
{
assert(formattedLine.length() > 0);
if (!isOkToSplitFormattedLine())
return;
char nextChar = peekNextChar();
// don't split before or after a bracket
if (appendedChar == '{' || appendedChar == '}'
|| previousNonWSChar == '{' || previousNonWSChar == '}'
|| nextChar == '{' || nextChar == '}'
|| currentChar == '{' || currentChar == '}') // currentChar tests for an appended bracket
return;
// don't split before or after a block paren
if (appendedChar == '[' || appendedChar == ']'
|| previousNonWSChar == '['
|| nextChar == '[' || nextChar == ']')
return;
// don't split before an end of line comment
if (nextChar == '/')
return;
if (isWhiteSpace(appendedChar))
{
if (nextChar != ')' // empty parens
&& currentChar != ')' // appended space preceeding a paren
&& nextChar != '/' // following comment
&& currentChar != '(' // appended space after a paren
&& previousNonWSChar != '(' // decided at the '('
&& !(nextChar == '*'
&& !isCharPotentialOperator(previousNonWSChar)
&& pointerAlignment == PTR_ALIGN_TYPE)
&& !(nextChar == '&'
&& !isCharPotentialOperator(previousNonWSChar)
&& (referenceAlignment == REF_ALIGN_TYPE
|| (referenceAlignment == REF_SAME_AS_PTR && pointerAlignment == PTR_ALIGN_TYPE)))
&& !(nextChar == '('
&& !isCharPotentialOperator(previousNonWSChar)) // not an operator followed by a paren
&& !(currentChar == '('
&& !isCharPotentialOperator(previousNonWSChar)) // appended space between a non operator followed by a paren
// NO && !(previousNonWSChar == '(' && nextChar == '(') // space between opening parens
// NO && !(currentChar == '(' && nextChar == '(') // appended space between opening parens
// NO && !(previousNonWSChar == '(' && nextChar == '"') // space after a paren followed by a quote
// NO && !(currentChar == '(' && nextChar == '"') // appended space after a paren followed by a quote
)
{
if (maxWhiteSpace == 0 || formattedLine.length() < maxCodeLength)
maxWhiteSpace = formattedLine.length() - 1;
else
maxWhiteSpacePending = formattedLine.length() -1;
}
}
// unpadded operators may split before the operator (counts as whitespace)
else if (isSplittableOperator(appendedChar))
{
if (charNum > 0
&& (isLegalNameChar(currentLine[charNum - 1]) || currentLine[charNum - 1] == ')'))
{
if (formattedLine.length() + 1 < maxCodeLength)
maxWhiteSpace = formattedLine.length();
else if (maxWhiteSpace == 0 || formattedLine.length() < maxCodeLength)
maxWhiteSpace = formattedLine.length() - 1;
else
maxWhiteSpacePending = formattedLine.length() - 1;
}
}
// unpadded closing parens may split after the paren (counts as whitespace)
else if (appendedChar == ')')
{
char adjacentChar = ' ';
if (charNum + 1 < (int) currentLine.length())
adjacentChar = currentLine[charNum + 1];
if (previousNonWSChar != '(' // empty parens
&& adjacentChar != ' '
&& adjacentChar != ';'
&& adjacentChar != ','
&& adjacentChar != '.')
{
if (maxWhiteSpace == 0 || formattedLine.length() < maxCodeLength)
maxWhiteSpace = formattedLine.length();
else
maxWhiteSpacePending = formattedLine.length();
}
}
else if (appendedChar == ',')
{
if (maxComma == 0 || formattedLine.length() < maxCodeLength)
maxComma = formattedLine.length();
else
maxCommaPending = formattedLine.length();
}
else if (appendedChar == '(')
{
// a following quote is something like wxT("..."), do not break
if (nextChar != ')' && nextChar != '(' && nextChar != '"' && nextChar != '\'')
{
// if follows an operator break before
size_t parenNum;
if (isCharPotentialOperator(previousNonWSChar))
parenNum = formattedLine.length() - 1 ;
else
parenNum = formattedLine.length();
if (maxParen == 0 || formattedLine.length() < maxCodeLength)
maxParen = parenNum;
else
maxParenPending = parenNum;
}
}
else if (appendedChar == ';')
{
if (nextChar != ' ' && nextChar != '}' && nextChar != '/') // check for following comment
{
if (maxSemi == 0 || formattedLine.length() < maxCodeLength)
maxSemi = formattedLine.length();
else
maxSemiPending = formattedLine.length();
}
}
}
void ASFormatter::updateFormattedLineSplitPointSequence(const string &sequence)
{
assert(formattedLine.length() > 0);
if (!isOkToSplitFormattedLine())
return;
// check for logical conditional
if (sequence == "||" || sequence == "&&"|| sequence == "or"|| sequence == "and")
{
if (shouldBreakLineAfterLogical)
maxAndOr = formattedLine.length();
else
maxAndOr = formattedLine.length() - sequence.length();
}
// unpadded comparison operators will split after the operator (counts as whitespace)
else if (sequence == "==" || sequence == "!="|| sequence == ">="|| sequence == "<=")
{
if (maxWhiteSpace == 0 || formattedLine.length() < maxCodeLength)
maxWhiteSpace = formattedLine.length();
else
maxWhiteSpacePending = formattedLine.length();
}
}
bool ASFormatter::isSplittableOperator(char appendedChar) const
{
return (appendedChar == '+' || appendedChar == '-' || appendedChar == '='
|| appendedChar == ':' || appendedChar == '?');
}
bool ASFormatter::isOkToSplitFormattedLine()
{
// Is it OK to split the line?
if (shouldKeepLineUnbroken
|| isInLineComment
|| isInComment
|| isInQuote
|| isInBlParen
|| isInPreprocessor
|| isInExecSQL
|| isInAsm || isInAsmOneLine || isInAsmBlock
|| isInTemplate)
return false;
if (!isOkToBreakBlock(bracketTypeStack->back())
|| isBracketType(bracketTypeStack->back(), ARRAY_TYPE))
{
shouldKeepLineUnbroken = true;
clearFormattedLineSplitPoints();
return false;
}
return true;
}
/* This is called if the option maxCodeLength is set.
This can be called either before the characters are attached to the formattedLine
or after. If attached before, the sequenceLength of the attached characters must
be sent as a parameter. If attached after, the sequenceLength is NOT sent.
*/
void ASFormatter::testForTimeToSplitFormattedLine(int sequenceLength /* 0 */)
{
// should the line be split
if (formattedLine.length() > maxCodeLength && !isLineReady)
{
//if (charNum + sequenceLength == (int) currentLine.length())
//{
// isInLineBreak = true; // break when this sequence is attached
// return;
//}
size_t splitPoint = findFormattedLineSplitPoint(sequenceLength);
if (splitPoint > 0)
{
string splitLine = formattedLine.substr(splitPoint);
formattedLine = formattedLine.substr(0, splitPoint);
breakLine(true);
formattedLine = splitLine;
// adjust max split points
maxAndOr = (maxAndOr > splitPoint) ? (maxAndOr - splitPoint) : 0;
maxSemi = (maxSemi > splitPoint) ? (maxSemi - splitPoint) : 0;
maxComma = (maxComma > splitPoint) ? (maxComma - splitPoint) : 0;
maxParen = (maxParen > splitPoint) ? (maxParen - splitPoint) : 0;
maxWhiteSpace = (maxWhiteSpace > splitPoint) ? (maxWhiteSpace - splitPoint) : 0;
if (maxSemiPending > 0)
{
maxSemi = (maxSemiPending > splitPoint) ? (maxSemiPending - splitPoint) : 0;
maxSemiPending = 0;
}
if (maxCommaPending > 0)
{
maxComma = (maxCommaPending > splitPoint) ? (maxCommaPending - splitPoint) : 0;
maxCommaPending = 0;
}
if (maxParenPending > 0)
{
maxParen = (maxParenPending > splitPoint) ? (maxParenPending - splitPoint) : 0;
maxParenPending = 0;
}
if (maxWhiteSpacePending > 0)
{
maxWhiteSpace = (maxWhiteSpacePending > splitPoint) ? (maxWhiteSpacePending - splitPoint) : 0;
maxWhiteSpacePending = 0;
}
// don't allow an empty formatted line
size_t firstText = formattedLine.find_first_not_of(" \t");
if (firstText == string::npos && formattedLine.length() > 0)
{
formattedLine.erase();
clearFormattedLineSplitPoints();
if (isWhiteSpace(currentChar))
for (size_t i = charNum+1; i < currentLine.length() && isWhiteSpace(currentLine[i]); i++)
goForward(1);
}
else if (firstText > 0)
{
formattedLine.erase(0, firstText);
maxSemi = (maxSemi > firstText) ? (maxSemi - firstText) : 0;
maxAndOr = (maxAndOr > firstText) ? (maxAndOr - firstText) : 0;
maxComma = (maxComma > firstText) ? (maxComma - firstText) : 0;
maxParen = (maxParen > firstText) ? (maxParen - firstText) : 0;
maxWhiteSpace = (maxWhiteSpace > firstText) ? (maxWhiteSpace - firstText) : 0;
}
// reset formattedLineCommentNum
if (formattedLineCommentNum != string::npos)
{
formattedLineCommentNum = formattedLine.find("//");
if (formattedLineCommentNum == string::npos)
formattedLineCommentNum = formattedLine.find("/*");
}
}
}
}
size_t ASFormatter::findFormattedLineSplitPoint(int sequenceLength) const
{
// don't split if the last char is a semi-colon or space
if (formattedLine.length() == maxCodeLength + 1
&& (currentChar == ';' || currentChar == ' '))
return 0;
// determine where to split
size_t indentLength_ = static_cast<size_t>(getIndentLength());
size_t splitPoint = 0;
if (maxSemi > 0)
splitPoint = maxSemi;
else if (maxAndOr > 0)
splitPoint = maxAndOr;
else if (maxComma > 0)
splitPoint = maxComma;
size_t minCodeLength = (indentLength_ * 2) + 2;
if (splitPoint < minCodeLength)
splitPoint = 0;
// use maxParen instead of whitespace if it is long enough
if (splitPoint == 0
&& maxParen > minCodeLength
&& (maxParen > maxWhiteSpace
|| maxParen > (maxCodeLength * .7)
|| maxWhiteSpace > maxCodeLength))
splitPoint = maxParen;
// use whitespace or paren if available
if (splitPoint == 0)
splitPoint = maxWhiteSpace;
if (splitPoint == 0
&& maxParen > 0)
splitPoint = maxParen;
// replace split point with first available break point
if (splitPoint < minCodeLength)
{
splitPoint = string::npos;
if (maxSemiPending > 0 && maxSemiPending < splitPoint)
splitPoint = maxSemiPending;
// if (maxAndOrPending > 0 && maxAndOrPending < splitPoint) // TODO: Fix This
// splitPoint = maxAndOrPending;
if (maxCommaPending > 0 && maxCommaPending < splitPoint)
splitPoint = maxCommaPending;
if (maxParenPending > 0 && maxParenPending < splitPoint)
splitPoint = maxParenPending;
if (maxWhiteSpacePending > 0 && maxWhiteSpacePending < splitPoint)
splitPoint = maxWhiteSpacePending;
if (splitPoint == string::npos)
splitPoint = 0;
}
// Don't split if near the end.
int remainingCurrent = currentLine.length() - (charNum + sequenceLength);
if (remainingCurrent == 0)
{
if (formattedLine.length() <= maxCodeLength
|| formattedLine.length() <= splitPoint
|| (splitPoint >= maxCodeLength
&& formattedLine.length() <= maxCodeLength + 2))
splitPoint = 0;
}
return splitPoint;
}
//int ASFormatter::findRemainingPadding() const
//{
// // find remaining padding on the current line
// // err on the side of accumulating too much so the line will be broken
// int remainingPadding = 0;
// if ((int) currentLine.length() <= charNum + 1)
// return remainingPadding;
// string searchLine = currentLine.substr(charNum);
// char prevChar = searchLine[0];
// char currChar;
// char nextChar;
// for (size_t i = 1; i < searchLine.length(); i++)
// {
// currChar = searchLine[i];
// nextChar = (i+1 >= searchLine.length()) ? ' ' : searchLine[i+1];
// if (currChar == ',' && nextChar != ' ')
// ++remainingPadding;
// if (currChar == '(')
// {
// if (shouldPadParensOutside && prevChar != ' ')
// ++remainingPadding;
// if (shouldPadParensInside && nextChar != ' '
// && (prevChar != '(' && shouldPadParensInside)) // to avoid double counting '(('
// ++remainingPadding;
// }
// if (currChar == ')')
// {
// if (shouldPadParensInside && prevChar != ' '
// && (prevChar != ')' && shouldPadParensOutside)) // to avoid double counting '))'
// ++remainingPadding;
// if (shouldPadParensOutside && nextChar != ' ' && nextChar != ';')
// ++remainingPadding;
// }
// prevChar = currChar;
// }
// return remainingPadding;
//}
void ASFormatter::clearFormattedLineSplitPoints()
{
maxSemi = 0;
maxAndOr = 0;
maxComma = 0;
maxParen = 0;
maxWhiteSpace = 0;
maxSemiPending = 0;
maxCommaPending = 0;
maxParenPending = 0;
maxWhiteSpacePending = 0;
}
/**
* Compute the input checksum.
* This is called as an assert so it for is debug config only
*/
bool ASFormatter::computeChecksumIn(const string ¤tLine_)
{
for (size_t i = 0; i < currentLine_.length(); i++)
if (!isWhiteSpace(currentLine_[i]))
checksumIn += currentLine_[i];
return true;
}
/**
* get the value of checksumIn for unit testing
*
* @return checksumIn.
*/
size_t ASFormatter::getChecksumIn() const
{
return checksumIn;
}
/**
* Compute the output checksum.
* This is called as an assert so it is for debug config only
*/
bool ASFormatter::computeChecksumOut(const string &beautifiedLine)
{
for (size_t i = 0; i < beautifiedLine.length(); i++)
if (!isWhiteSpace(beautifiedLine[i]))
checksumOut += beautifiedLine[i];
return true;
}
/**
* Return isLineReady for the final check at end of file.
*/
bool ASFormatter::getIsLineReady() const
{
return isLineReady;
}
/**
* get the value of checksumOut for unit testing
*
* @return checksumOut.
*/
size_t ASFormatter::getChecksumOut() const
{
return checksumOut;
}
/**
* Return the difference in checksums.
* If zero all is okay.
*/
int ASFormatter::getChecksumDiff() const
{
return checksumOut - checksumIn;
}
// for unit testing
int ASFormatter::getFormatterFileType() const
{ return formatterFileType; }
// Check if an operator follows the next word.
// The next word must be a legal name.
const string* ASFormatter::getFollowingOperator() const
{
// find next word
size_t nextNum = currentLine.find_first_not_of(" \t", charNum + 1);
if (nextNum == string::npos)
return NULL;
if (!isLegalNameChar(currentLine[nextNum]))
return NULL;
// bypass next word and following spaces
while (nextNum < currentLine.length())
{
if (!isLegalNameChar(currentLine[nextNum])
&& !isWhiteSpace(currentLine[nextNum]))
break;
nextNum++;
}
if (nextNum >= currentLine.length()
|| !isCharPotentialOperator(currentLine[nextNum])
|| currentLine[nextNum] == '/') // comment
return NULL;
const string* newOperator = ASBeautifier::findOperator(currentLine, nextNum, operators);
return newOperator;
}
// Check following data to determine if the current character is an array operator.
bool ASFormatter::isArrayOperator() const
{
assert(currentChar == '*' || currentChar == '&');
assert(isBracketType(bracketTypeStack->back(), ARRAY_TYPE));
// find next word
size_t nextNum = currentLine.find_first_not_of(" \t", charNum + 1);
if (nextNum == string::npos)
return NULL;
if (!isLegalNameChar(currentLine[nextNum]))
return NULL;
// bypass next word and following spaces
while (nextNum < currentLine.length())
{
if (!isLegalNameChar(currentLine[nextNum])
&& !isWhiteSpace(currentLine[nextNum]))
break;
nextNum++;
}
// check for characters that indicate an operator
if (currentLine[nextNum] == ','
|| currentLine[nextNum] == '}'
|| currentLine[nextNum] == ')'
|| currentLine[nextNum] == '(')
return true;
return false;
}
} // end namespace astyle
|