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
|
/*=============================================================================
Boost.Wave: A Standard compliant C++ preprocessor library
Spirit based lexer
http://www.boost.org/
Copyright (c) 2001, Daniel C. Nuffer.
Copyright (c) 2001-2008 Hartmut Kaiser.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
TODO List:
X callback objects (called when a match is made.)
X callback passed first & last iterator, and
a reference to a lexercontrol object that supports some
operations on the lexer.
set state
terminate
state stack (push, pop, top)
set new token return value
ignore the current token
yymore
get length of matched token
get current lexer state
X DFA serialization to save recomputing the DFA.
lexer states.
organize the file into hpp and ipp. arrange the classes in a logical order.
use buffering - iterator only needs be an input iterator,
lexer & callback are not templatized on iterator type, but char type.
next_token is templatized on iterator type.
beginning/ending contexts.
^ and $
DFA minimization.
DFA table compression.
=============================================================================*/
#ifndef BOOST_SPIRIT_LEXER_HPP
#define BOOST_SPIRIT_LEXER_HPP
///////////////////////////////////////////////////////////////////////////////
#include <boost/throw_exception.hpp>
#include <boost/spirit/core.hpp>
#include <boost/spirit/symbols/symbols.hpp>
#include <boost/spirit/utility/chset.hpp>
#include <boost/spirit/utility/escape_char.hpp>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <utility> // for pair
#include <iostream>
#include <fstream>
#include <boost/assert.hpp>
#include <boost/limits.hpp>
#if SPIRIT_VERSION < 0x1700 && defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
#include <boost/spirit/core/impl/msvc.hpp>
#endif
#if defined(BOOST_NO_STD_ITERATOR_TRAITS)
#define BOOST_SPIRIT_IT_NS impl
#else
#define BOOST_SPIRIT_IT_NS std
#endif
///////////////////////////////////////////////////////////////////////////////
namespace boost {
namespace spirit {
typedef unsigned char uchar;
typedef unsigned int node_id_t;
const node_id_t invalid_node = node_id_t(-1);
typedef std::set<node_id_t> node_set;
typedef std::vector<uchar> uchar_vector;
typedef std::map<node_id_t, node_set> followpos_t;
typedef std::vector<uchar_vector> state_match_t;
template <typename TokenT>
class lexer_control;
class bad_regex : public std::exception
{
};
namespace lexerimpl
{
class node
{
public:
virtual ~node() {}
virtual node* clone() const = 0;
virtual bool nullable() const = 0;
virtual node_set firstpos() const = 0;
virtual node_set lastpos() const = 0;
virtual void compute_followpos(followpos_t& followpos) const = 0;
virtual void compute_state_match(state_match_t& state_match) const = 0;
virtual void get_eof_ids(node_set& eof_set) const = 0;
virtual void assign_node_ids(node_id_t& node_count) = 0;
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
virtual void dump(std::ostream& out) const = 0;
#endif
};
class char_node : public node
{
public:
char_node(const uchar c);
char_node(const char_node& x);
virtual ~char_node(){}
virtual node* clone() const;
virtual bool nullable() const;
virtual node_set firstpos() const;
virtual node_set lastpos() const;
virtual void compute_followpos(followpos_t& followpos) const;
virtual void compute_state_match(state_match_t& state_match ) const;
virtual void get_eof_ids(node_set& eof_set) const;
virtual void assign_node_ids(node_id_t& node_count);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
virtual void dump(std::ostream& out) const;
#endif
private:
uchar m_char;
node_id_t m_node_num;
};
inline
char_node::char_node(const uchar c)
: node()
, m_char(c)
, m_node_num(0)
{
}
inline
char_node::char_node(const char_node& x)
: node(x)
, m_char(x.m_char)
, m_node_num(x.m_node_num)
{
}
inline node *
char_node::clone() const
{
return new char_node(*this);
}
inline bool
char_node::nullable() const
{
return false;
}
inline node_set
char_node::firstpos() const
{
node_set rval;
rval.insert(m_node_num);
return rval;
}
inline node_set
char_node::lastpos() const
{
return firstpos();
}
inline void
char_node::compute_followpos(followpos_t&) const
{
return;
}
inline void
char_node::compute_state_match(state_match_t& state_match) const
{
if (state_match.size() < m_node_num + 1)
state_match.resize(m_node_num + 1);
state_match[m_node_num].resize(256);
state_match[m_node_num][m_char] = 1;
}
inline void
char_node::get_eof_ids(node_set&) const
{
return;
}
inline void
char_node::assign_node_ids(node_id_t& node_count)
{
m_node_num = node_count++;
}
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
inline void
char_node::dump(std::ostream& out) const
{
out << "\nchar_node m_char = " << m_char;
out << " m_node_num = " << m_node_num;
out << " nullable() = " << (nullable() ? "true" : "false");
out << " firstpos() = ";
node_set fp = firstpos();
std::copy(fp.begin(), fp.end(),
std::ostream_iterator<node_id_t>(out, ","));
out << " lastpos() = ";
node_set lp = lastpos();
std::copy(lp.begin(), lp.end(),
std::ostream_iterator<node_id_t>(out, ","));
}
#endif
class epsilon_node : public node
{
public:
epsilon_node();
epsilon_node(const epsilon_node& x);
virtual ~epsilon_node(){}
virtual node* clone() const;
virtual bool nullable() const;
virtual node_set firstpos() const;
virtual node_set lastpos() const;
virtual void compute_followpos(followpos_t& followpos) const;
virtual void compute_state_match(state_match_t& state_match ) const;
virtual void get_eof_ids(node_set& eof_set) const;
virtual void assign_node_ids(node_id_t& node_count);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
virtual void dump(std::ostream& out) const;
#endif
private:
node_id_t m_node_num;
};
inline
epsilon_node::epsilon_node()
: node()
, m_node_num(0)
{
}
inline
epsilon_node::epsilon_node(const epsilon_node& x)
: node(x)
, m_node_num(x.m_node_num)
{
}
inline node *
epsilon_node::clone() const
{
return new epsilon_node(*this);
}
inline bool
epsilon_node::nullable() const
{
return true;
}
inline node_set
epsilon_node::firstpos() const
{
return node_set();
}
inline node_set
epsilon_node::lastpos() const
{
return node_set();
}
inline void
epsilon_node::compute_followpos(followpos_t&) const
{
return;
}
inline void
epsilon_node::compute_state_match(state_match_t& state_match) const
{
if (state_match.size() < m_node_num + 1)
state_match.resize(m_node_num + 1);
state_match[m_node_num].resize(256, 1);
}
inline void
epsilon_node::get_eof_ids(node_set&) const
{
return;
}
inline void
epsilon_node::assign_node_ids(node_id_t& node_count)
{
m_node_num = node_count++;
}
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
inline void
epsilon_node::dump(std::ostream& out) const
{
out << "\nepsilon_node";
out << " m_node_num = " << m_node_num;
out << " nullable() = " << (nullable() ? "true" : "false");
out << " firstpos() = ";
node_set fp = firstpos();
std::copy(fp.begin(), fp.end(),
std::ostream_iterator<node_id_t>(out, ","));
out << " lastpos() = ";
node_set lp = lastpos();
std::copy(lp.begin(), lp.end(),
std::ostream_iterator<node_id_t>(out, ","));
}
#endif
class or_node : public node
{
public:
or_node(node* left, node* right);
or_node(const or_node& x);
virtual ~or_node(){}
virtual node* clone() const;
virtual bool nullable() const;
virtual node_set firstpos() const;
virtual node_set lastpos() const;
virtual void compute_followpos(followpos_t& followpos) const;
virtual void compute_state_match(state_match_t& state_match ) const;
virtual void get_eof_ids(node_set& eof_set) const;
virtual void assign_node_ids(node_id_t& node_count);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
virtual void dump(std::ostream& out) const;
#endif
private:
std::auto_ptr<node> m_left;
std::auto_ptr<node> m_right;
};
inline
or_node::or_node(node* left, node* right)
: node()
, m_left(left)
, m_right(right)
{
}
inline
or_node::or_node(const or_node& x)
: node(x)
, m_left(x.m_left->clone())
, m_right(x.m_right->clone())
{
}
inline node *
or_node::clone() const
{
return new or_node(m_left->clone(), m_right->clone());
}
inline bool
or_node::nullable() const
{
return m_left->nullable() || m_right->nullable();
}
inline node_set
or_node::firstpos() const
{
node_set rval;
node_set l = m_left->firstpos();
node_set r = m_right->firstpos();
std::set_union(l.begin(), l.end(), r.begin(), r.end(),
std::inserter(rval, rval.begin()));
return rval;
}
inline node_set
or_node::lastpos() const
{
node_set rval;
node_set l = m_left->lastpos();
node_set r = m_right->lastpos();
std::set_union(l.begin(), l.end(), r.begin(), r.end(),
std::inserter(rval, rval.begin()));
return rval;
}
inline void
or_node::compute_followpos(followpos_t& followpos) const
{
m_left->compute_followpos(followpos);
m_right->compute_followpos(followpos);
}
inline void
or_node::compute_state_match(state_match_t& state_match) const
{
m_left->compute_state_match(state_match);
m_right->compute_state_match(state_match);
}
inline void
or_node::get_eof_ids(node_set& eof_nodes) const
{
m_left->get_eof_ids(eof_nodes);
m_right->get_eof_ids(eof_nodes);
}
inline void
or_node::assign_node_ids(node_id_t& node_count)
{
m_left->assign_node_ids(node_count);
m_right->assign_node_ids(node_count);
}
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
inline void
or_node::dump(std::ostream& out) const
{
m_left->dump(out);
out << "\nor_node";
out << " nullable() = " << (nullable() ? "true" : "false");
out << " firstpos() = ";
node_set fp = firstpos();
std::copy(fp.begin(), fp.end(),
std::ostream_iterator<node_id_t>(out, ","));
out << " lastpos() = ";
node_set lp = lastpos();
std::copy(lp.begin(), lp.end(),
std::ostream_iterator<node_id_t>(out, ","));
m_right->dump(out);
}
#endif
class cat_node : public node
{
public:
cat_node(node* left, node* right);
cat_node(const cat_node& x);
virtual ~cat_node(){}
virtual node* clone() const;
virtual bool nullable() const;
virtual node_set firstpos() const;
virtual node_set lastpos() const;
virtual void compute_followpos(followpos_t& followpos) const;
virtual void compute_state_match(state_match_t& state_match ) const;
virtual void get_eof_ids(node_set& eof_set) const;
virtual void assign_node_ids(node_id_t& node_count);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
virtual void dump(std::ostream& out) const;
#endif
private:
std::auto_ptr<node> m_left;
std::auto_ptr<node> m_right;
};
inline
cat_node::cat_node(node* left, node* right)
: node()
, m_left(left)
, m_right(right)
{
}
inline
cat_node::cat_node(const cat_node& x)
: node(x)
, m_left(x.m_left->clone())
, m_right(x.m_right->clone())
{
}
inline node *
cat_node::clone() const
{
return new cat_node(m_left->clone(), m_right->clone());
}
inline bool
cat_node::nullable() const
{
return m_left->nullable() && m_right->nullable();
}
inline node_set
cat_node::firstpos() const
{
if (m_left->nullable())
{
node_set rval;
node_set l = m_left->firstpos();
node_set r = m_right->firstpos();
std::set_union(l.begin(), l.end(), r.begin(), r.end(),
std::inserter(rval, rval.begin()));
return rval;
}
else
{
return m_left->firstpos();
}
}
inline node_set
cat_node::lastpos() const
{
if (m_right->nullable())
{
node_set rval;
node_set l = m_left->lastpos();
node_set r = m_right->lastpos();
std::set_union(l.begin(), l.end(), r.begin(), r.end(),
std::inserter(rval, rval.begin()));
return rval;
}
else
{
return m_right->lastpos();
}
}
inline void
cat_node::compute_followpos(followpos_t& followpos) const
{
node_set l = m_left->lastpos();
for (node_set::iterator i = l.begin();
i != l.end();
++i)
{
node_set rf = m_right->firstpos();
followpos[*i].insert(rf.begin(), rf.end());
}
m_left->compute_followpos(followpos);
m_right->compute_followpos(followpos);
}
inline void
cat_node::compute_state_match(state_match_t& state_match) const
{
m_left->compute_state_match(state_match);
m_right->compute_state_match(state_match);
}
inline void
cat_node::get_eof_ids(node_set& eof_nodes) const
{
m_left->get_eof_ids(eof_nodes);
m_right->get_eof_ids(eof_nodes);
}
inline void
cat_node::assign_node_ids(node_id_t& node_count)
{
m_left->assign_node_ids(node_count);
m_right->assign_node_ids(node_count);
}
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
inline void
cat_node::dump(std::ostream& out) const
{
m_left->dump(out);
out << "\ncat_node";
out << " nullable() = " << (nullable() ? "true" : "false");
out << " firstpos() = ";
node_set fp = firstpos();
std::copy(fp.begin(), fp.end(),
std::ostream_iterator<node_id_t>(out, ","));
out << " lastpos() = ";
node_set lp = lastpos();
std::copy(lp.begin(), lp.end(),
std::ostream_iterator<node_id_t>(out, ","));
m_right->dump(out);
}
#endif
class star_node : public node
{
public:
star_node(node* left);
star_node(const star_node& x);
virtual ~star_node(){}
virtual node* clone() const;
virtual bool nullable() const;
virtual node_set firstpos() const;
virtual node_set lastpos() const;
virtual void compute_followpos(followpos_t& followpos) const;
virtual void compute_state_match(state_match_t& state_match ) const;
virtual void get_eof_ids(node_set& eof_set) const;
virtual void assign_node_ids(node_id_t& node_count);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
virtual void dump(std::ostream& out) const;
#endif
private:
std::auto_ptr<node> m_left;
};
inline
star_node::star_node(node* left)
: node()
, m_left(left)
{
}
inline
star_node::star_node(const star_node& x)
: node(x)
, m_left(x.m_left->clone())
{
}
inline node *
star_node::clone() const
{
return new star_node(m_left->clone());
}
inline bool
star_node::nullable() const
{
return true;
}
inline node_set
star_node::firstpos() const
{
return m_left->firstpos();
}
inline node_set
star_node::lastpos() const
{
return m_left->lastpos();
}
inline void
star_node::compute_followpos(followpos_t& followpos) const
{
node_set lp = this->lastpos();
for (node_set::iterator i = lp.begin();
i != lp.end();
++i)
{
node_set fp = this->firstpos();
followpos[*i].insert(fp.begin(), fp.end());
}
m_left->compute_followpos(followpos);
}
inline void
star_node::compute_state_match(state_match_t& state_match) const
{
m_left->compute_state_match(state_match);
}
inline void
star_node::get_eof_ids(node_set& eof_nodes) const
{
m_left->get_eof_ids(eof_nodes);
}
inline void
star_node::assign_node_ids(node_id_t& node_count)
{
m_left->assign_node_ids(node_count);
}
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
inline void
star_node::dump(std::ostream& out) const
{
m_left->dump(out);
out << "\nstar_node";
out << " nullable() = " << (nullable() ? "true" : "false");
out << " firstpos() = ";
node_set fp = firstpos();
std::copy(fp.begin(), fp.end(),
std::ostream_iterator<node_id_t>(out, ","));
out << " lastpos() = ";
node_set lp = lastpos();
std::copy(lp.begin(), lp.end(),
std::ostream_iterator<node_id_t>(out, ","));
}
#endif
class eof_node : public node
{
public:
eof_node();
eof_node(const eof_node& x);
virtual ~eof_node(){}
virtual node* clone() const;
virtual bool nullable() const;
virtual node_set firstpos() const;
virtual node_set lastpos() const;
virtual void compute_followpos(followpos_t& followpos) const;
virtual void compute_state_match(state_match_t& state_match ) const;
virtual void get_eof_ids(node_set& eof_set) const;
virtual void assign_node_ids(node_id_t& node_count);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
virtual void dump(std::ostream& out) const;
#endif
private:
node_id_t m_node_num;
};
inline
eof_node::eof_node()
: node()
, m_node_num(0)
{
}
inline
eof_node::eof_node(const eof_node& x)
: node(x)
, m_node_num(x.m_node_num)
{
}
inline node *
eof_node::clone() const
{
return new eof_node(*this);
}
inline bool
eof_node::nullable() const
{
return false;
}
inline node_set
eof_node::firstpos() const
{
node_set rval;
rval.insert(m_node_num);
return rval;
}
inline node_set
eof_node::lastpos() const
{
node_set rval;
rval.insert(m_node_num);
return rval;
}
inline void
eof_node::compute_followpos(followpos_t&) const
{
return;
}
inline void
eof_node::compute_state_match(state_match_t& state_match) const
{
if (state_match.size() < m_node_num + 1)
state_match.resize(m_node_num + 1);
state_match[m_node_num].resize(256, 0);
}
inline void
eof_node::get_eof_ids(node_set& eof_nodes) const
{
eof_nodes.insert(m_node_num);
}
inline void
eof_node::assign_node_ids(node_id_t& node_count)
{
m_node_num = node_count++;
}
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
inline void
eof_node::dump(std::ostream& out) const
{
out << "\neof_node";
out << " m_node_num = " << m_node_num;
out << " nullable() = " << (nullable() ? "true" : "false");
out << " firstpos() = ";
node_set fp = firstpos();
std::copy(fp.begin(), fp.end(),
std::ostream_iterator<node_id_t>(out, ","));
out << " lastpos() = ";
node_set lp = lastpos();
std::copy(lp.begin(), lp.end(),
std::ostream_iterator<node_id_t>(out, ","));
}
#endif
class ccl_node : public node
{
public:
ccl_node(const std::vector<uchar>& v);
ccl_node(const uchar c1, const uchar c2);
ccl_node(const ccl_node& x);
virtual ~ccl_node(){}
virtual node* clone() const;
virtual bool nullable() const;
virtual node_set firstpos() const;
virtual node_set lastpos() const;
virtual void compute_followpos(followpos_t& followpos) const;
virtual void compute_state_match(state_match_t& state_match ) const;
virtual void get_eof_ids(node_set& eof_set) const;
virtual void assign_node_ids(node_id_t& node_count);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
virtual void dump(std::ostream& out) const;
#endif
private:
std::vector<uchar> m_match;
node_id_t m_node_num;
};
inline
ccl_node::ccl_node(const std::vector<uchar>& v)
: node()
, m_match(v)
, m_node_num(0)
{
m_match.resize(256); // make sure it's the right size
}
inline
ccl_node::ccl_node(const uchar c1, const uchar c2)
: node()
, m_match(256, uchar(0))
, m_node_num(0)
{
BOOST_ASSERT(c1 < c2);
for (std::size_t i = c1; i <= std::size_t(c2); ++i)
{
m_match[i] = 1;
}
}
inline
ccl_node::ccl_node(const ccl_node& x)
: node(x)
, m_match(x.m_match)
, m_node_num(x.m_node_num)
{
}
inline node *
ccl_node::clone() const
{
return new ccl_node(*this);
}
inline bool
ccl_node::nullable() const
{
return false;
}
inline node_set
ccl_node::firstpos() const
{
node_set rval;
rval.insert(m_node_num);
return rval;
}
inline node_set
ccl_node::lastpos() const
{
return firstpos();
}
inline void
ccl_node::compute_followpos(followpos_t&) const
{
return;
}
inline void
ccl_node::compute_state_match(state_match_t& state_match) const
{
if (state_match.size() < m_node_num + 1)
state_match.resize(m_node_num + 1);
state_match[m_node_num] = m_match;
}
inline void
ccl_node::get_eof_ids(node_set&) const
{
return;
}
inline void
ccl_node::assign_node_ids(node_id_t& node_count)
{
m_node_num = node_count++;
}
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
inline void
ccl_node::dump(std::ostream& out) const
{
out << "\nccl_node m_match = ";
for (std::size_t i = 0; i < m_match.size(); ++i)
{
if (m_match[i])
out << i << ", ";
}
out << " m_node_num = " << m_node_num;
out << " nullable() = " << (nullable() ? "true" : "false");
out << " firstpos() = ";
node_set fp = firstpos();
std::copy(fp.begin(), fp.end(),
std::ostream_iterator<node_id_t>(out, ","));
out << " lastpos() = ";
node_set lp = lastpos();
std::copy(lp.begin(), lp.end(),
std::ostream_iterator<node_id_t>(out, ","));
}
#endif
template <typename ScannerT>
class make_concat
{
typedef typename ScannerT::iterator_t iterator_type;
public:
make_concat(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(iterator_type const &, iterator_type const &) const
{
node* right = m_stack.top();
m_stack.pop();
node* left = m_stack.top();
m_stack.pop();
node* newnode = new cat_node(left, right);
m_stack.push(newnode);
}
std::stack<node*>& m_stack;
};
template <int CharTSize>
struct get_byte_aux;
template<>
struct get_byte_aux<1>
{
template <typename CharT>
unsigned char operator()(CharT c, unsigned int byte)
{
BOOST_ASSERT(byte == 0);
return c;
}
};
template<>
struct get_byte_aux<2>
{
template <typename CharT>
unsigned char operator()(CharT c, unsigned int byte)
{
static unsigned long mask[] =
{
0xFF00,
0x00FF
};
BOOST_ASSERT(byte < 2);
return (c & mask[byte]) >> ((sizeof(c) - 1 - byte) * 8);
}
};
template<>
struct get_byte_aux<4>
{
template <typename CharT>
unsigned char operator()(CharT c, unsigned int byte)
{
static unsigned long mask[] =
{
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
};
BOOST_ASSERT(byte < 4);
return (c & mask[byte]) >> ((sizeof(c) - 1 - byte) * 8);
}
};
template <typename CharT>
inline unsigned char
get_byte(CharT c, unsigned int byte)
{
return get_byte_aux<sizeof(c)>()(c, byte);
}
template <typename ScannerT>
class make_star
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
make_star(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(const char_t) const
{
node* left = m_stack.top();
m_stack.pop();
node* newnode = new star_node(left);
m_stack.push(newnode);
}
std::stack<node*>& m_stack;
};
template <typename ScannerT>
class make_or
{
typedef typename ScannerT::iterator_t iterator_type;
public:
make_or(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(iterator_type const&, iterator_type const&) const
{
node* right = m_stack.top();
m_stack.pop();
node* left = m_stack.top();
m_stack.pop();
node* newnode = new or_node(left, right);
m_stack.push(newnode);
}
std::stack<node*>& m_stack;
};
template <typename ScannerT>
class make_plus
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
make_plus(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(const char_t) const
{
node* left = m_stack.top();
m_stack.pop();
node* copy = left->clone();
node* new_star = new star_node(copy);
node* new_cat = new cat_node(left, new_star);
m_stack.push(new_cat);
}
std::stack<node*>& m_stack;
};
template <typename ScannerT>
class make_optional
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
make_optional(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(const char_t) const
{
node* left = m_stack.top();
m_stack.pop();
node* new_or = new or_node(left, new epsilon_node());
m_stack.push(new_or);
}
std::stack<node*>& m_stack;
};
///////////////////////////////////////////////////////////////////////////////
// utility function
template <typename CharT>
inline utility::impl::range<CharT> const&
full_range()
{
static utility::impl::range<CharT> full((std::numeric_limits<CharT>::min)(),
(std::numeric_limits<CharT>::max)());
return full;
}
namespace ccl_utils
{
template <typename char_t>
inline utility::impl::range_run<char_t>
negate_range_run(
const utility::impl::range_run<char_t>& rr)
{
utility::impl::range_run<char_t> newrr;
newrr.set(full_range<char_t>());
for (typename utility::impl::range_run<char_t>::const_iterator iter = rr.begin();
iter != rr.end(); ++iter)
newrr.clear(*iter);
return newrr;
}
template <typename char_t>
inline node*
create_mb_node_seq(char_t c)
{
node* newnode = new char_node(get_byte(c, 0));
for (unsigned int i = 1; i < sizeof(c); ++i)
{
node* cnode = new char_node(get_byte(c, i));
node* top_node = new cat_node(newnode, cnode);
newnode = top_node;
}
return newnode;
}
template <typename char_t>
inline void
handle_mb_char(char_t c, bool first_time,
std::stack<node*>& stack)
{
node* newnode = create_mb_node_seq(c);
if (first_time)
{
stack.push(newnode);
}
else
{
node* top = stack.top();
stack.pop();
node* newtop = new or_node(top, newnode);
stack.push(newtop);
}
}
// forward decl only
template <typename char_t>
inline void
handle_mb_range(char_t c1, char_t c2, bool first_time,
std::stack<node*>& stack);
template <typename char_t>
inline void
create_nodes(const utility::impl::range_run<char_t>& rr,
std::stack<node*>& stack)
{
if (sizeof(char_t) == 1)
{
std::vector<uchar> ccl;
ccl.resize(256);
for (typename utility::impl::range_run<char_t>::const_iterator iter = rr.begin();
iter != rr.end(); ++iter)
{
for (int i = iter->first; i <= iter->last; ++i)
{
// this is always true because of the limited datatype
// BOOST_ASSERT(uchar(i) < 256 && ccl.size() == 256);
ccl[uchar(i)] = 1;
}
}
node* new_ccl = new ccl_node(ccl);
stack.push(new_ccl);
}
else
{
bool mb_first_time = true;
for (typename utility::impl::range_run<char_t>::const_iterator iter = rr.begin();
iter != rr.end(); ++iter)
{
if (iter->first == iter->last)
{
handle_mb_char(iter->first, mb_first_time, stack);
}
else
{
handle_mb_range(iter->first, iter->last, mb_first_time, stack);
}
mb_first_time = false;
}
}
}
template <typename char_t>
inline std::size_t
compute_differing_byte(char_t c1, char_t c2)
{
std::size_t rval = 0;
while (rval < sizeof(c1) &&
get_byte(c1, (unsigned int)rval) == get_byte(c2, (unsigned int)rval))
{
++rval;
}
return rval;
}
template <typename char_t>
inline node*
create_mb_node_type1(std::size_t j, char_t c1, char_t c2)
{
std::size_t diff = get_byte(c2, (unsigned int)j) -
get_byte(c1, (unsigned int)j);
if (diff == 1) {
return 0;
}
else if (diff == 2) {
return new char_node(get_byte(c1, (unsigned int)j)+1);
}
else {
return new ccl_node(get_byte(c1, (unsigned int)j)+1,
get_byte(c2, (unsigned int)j)-1);
}
}
template <typename char_t>
inline node *
create_mb_node_for_byte(std::size_t i, std::size_t j, std::size_t sizem1,
std::size_t differing_byte, char_t c1, char_t c2, node* newnode)
{
node* cnode;
if (i == sizem1 && j == differing_byte && j != sizem1)
{
node* tmp = create_mb_node_type1(j, c1, c2);
if (tmp == 0)
{
delete newnode;
return 0;
}
else
cnode = tmp;
}
else if (i == differing_byte && j == sizem1)
{
if (i != sizem1) {
cnode = new ccl_node(get_byte(c1, (unsigned int)j), 0xFF);
}
else {
cnode = new ccl_node(get_byte(c1, (unsigned int)j),
get_byte(c2, (unsigned int)j));
}
}
else if (i != differing_byte && i != sizem1 &&
j == (sizem1 - i + differing_byte))
{
cnode = new ccl_node(get_byte(c1, (unsigned int)j)+1, 0xFF);
}
else if (i + j - differing_byte > sizem1) {
cnode = new ccl_node(0, 0xFF);
}
else {//if (is plain)
cnode = new char_node(get_byte(c1, (unsigned int)j));
}
node* top_node = new cat_node(newnode, cnode);
return top_node;
}
// On platforms, where wchar_t is a typedef for unsigned short, the
// comparision for a negative value is pointless
template <bool is_signed>
struct correct_char_aux {
};
template <>
struct correct_char_aux<true> {
template <typename char_t>
static char_t correct(char_t c) { if (c < 0) c = 0; return c; }
};
template <>
struct correct_char_aux<false> {
template <typename char_t>
static char_t correct(char_t c) { return c; }
};
template <typename char_t>
struct correct_char
{
static char_t correct(char_t c)
{
return correct_char_aux<std::numeric_limits<char_t>::is_signed >::
correct(c);
}
};
template <typename char_t>
inline void
handle_mb_range(char_t c1, char_t c2, bool first_time,
std::stack<node*>& stack)
{
// The algorithm can't handle negative value chars, which don't make
// much sense anyway. This comparision is pointless for wchar_t's on
// platforms, where wchar_t is a typedef for unsigned short
c1 = correct_char<char_t>::correct(c1);
//if (c1 < 0)
// c1 = 0;
BOOST_ASSERT(c1 < c2);
node* newnode = 0;
node* savednode = 0;
const std::size_t differing_byte = compute_differing_byte(c1, c2);
const std::size_t sizem1 = sizeof(c1) - 1;
const std::size_t ndb = sizem1 - differing_byte;
for (std::size_t i = differing_byte; i < sizeof(c1); ++i)
{
// generate node for the first byte
if (differing_byte == 0 && i == ndb)
{
node* tmp = create_mb_node_type1(0, c1, c2);
if (tmp == 0)
continue;
else
newnode = tmp;
}
else
{
newnode = new char_node(get_byte(c1, 0));
}
for (std::size_t j = 1; j < sizeof(c1); ++j)
{
newnode = create_mb_node_for_byte(i, j, sizem1, differing_byte,
c1, c2, newnode);
if (newnode == 0)
goto end_outer_for;
}
// or together the various parts
if (savednode)
{
node* top_node = new or_node(savednode, newnode);
savednode = top_node;
}
else
{
savednode = newnode;
}
end_outer_for:
continue;
}
for (std::size_t k = 0; k < ndb; ++k)
{
newnode = new char_node(get_byte(c2, 0));
for (std::size_t j = 1; j < sizeof(c2); ++j)
{
node* cnode;
if (k == differing_byte && j == sizem1 && k != sizem1)
cnode = new ccl_node(0, get_byte(c2, (unsigned int)j));
else if (k != differing_byte && k != sizem1 &&
j == (sizem1 - k + differing_byte))
cnode = new ccl_node(0, get_byte(c2, (unsigned int)j)-1);
else if (k + j - differing_byte > sizem1)
cnode = new ccl_node(0, 0xFF);
else //if (is plain)
cnode = new char_node(get_byte(c2, (unsigned int)j));
node* top_node = new cat_node(newnode, cnode);
newnode = top_node;
}
// or together the various parts
if (savednode)
{
node* top_node = new or_node(savednode, newnode);
savednode = top_node;
}
else
{
savednode = newnode;
}
}
if (first_time)
{
stack.push(savednode);
}
else
{
node* top = stack.top();
stack.pop();
node* newtop = new or_node(top, savednode);
stack.push(newtop);
}
}
} // namespace ccl_utils
template <typename ScannerT>
class make_char
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
make_char(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(iterator_type const& first, iterator_type const& last) const
{
const escape_char_parser<lex_escapes, char_t> lex_escape_ch =
escape_char_parser<lex_escapes, char_t>();
char_t the_char;
iterator_type first_ = first;
ScannerT scan(first_, last);
lex_escape_ch[assign(the_char)].parse(scan);
node* newnode = ccl_utils::create_mb_node_seq(the_char);
m_stack.push(newnode);
}
std::stack<node*>& m_stack;
};
template <typename ScannerT>
class make_ccl
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
make_ccl(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
static bool is_equal_to_string(iterator_type first,
iterator_type const & last, const char* str)
{
while (first != last &&*str &&*first ==*str)
{
++first;
++str;
}
return*str == 0;
}
template <typename ParserT>
static void fill_ccl(utility::impl::range_run<char_t>& rr, const ParserT& parser)
{
for (int i = 0; i < 256; ++i)
{
if (parser.test(static_cast<char_t>(uchar(i))))
rr.set(utility::impl::range<char_t>(char_t(i), char_t(i)));
}
}
void operator()(iterator_type const& first_, iterator_type const& last) const
{
BOOST_ASSERT(*first_ == '[');
iterator_type first = first_;
++first; // skip over '['
bool negated_ccl = false;
if (*first == '^')
{
negated_ccl = true;
++first;
}
utility::impl::range_run<char_t> rr;
while (first != last &&*first != ']')
{
if (*first == '[') // it's a ccl_expr like [:space:]
{
// check for [:space:], etc.
if (is_equal_to_string(first, last, "[:alnum:]"))
{
fill_ccl(rr, alnum_p);
}
else if (is_equal_to_string(first, last, "[:alpha:]"))
{
fill_ccl(rr, alpha_p);
}
else if (is_equal_to_string(first, last, "[:blank:]"))
{
fill_ccl(rr, blank_p);
}
else if (is_equal_to_string(first, last, "[:cntrl:]"))
{
fill_ccl(rr, cntrl_p);
}
else if (is_equal_to_string(first, last, "[:digit:]"))
{
fill_ccl(rr, digit_p);
}
else if (is_equal_to_string(first, last, "[:graph:]"))
{
fill_ccl(rr, graph_p);
}
else if (is_equal_to_string(first, last, "[:lower:]"))
{
fill_ccl(rr, lower_p);
}
else if (is_equal_to_string(first, last, "[:print:]"))
{
fill_ccl(rr, print_p);
}
else if (is_equal_to_string(first, last, "[:punct:]"))
{
fill_ccl(rr, punct_p);
}
else if (is_equal_to_string(first, last, "[:space:]"))
{
fill_ccl(rr, space_p);
}
else if (is_equal_to_string(first, last, "[:upper:]"))
{
fill_ccl(rr, upper_p);
}
else if (is_equal_to_string(first, last, "[:xdigit:]"))
{
fill_ccl(rr, xdigit_p);
}
// this can't happen, because it's parsed before we get here.
//else
// throw bad_regex();
// Advance past the character class expression
while (first != last &&*first != ']')
++first;
BOOST_ASSERT(*first == ']');
++first;
}
else {
const escape_char_parser<lex_escapes, char_t> lex_escape_ch =
escape_char_parser<lex_escapes, char_t>();
char_t c1;
ScannerT scan(first, last);
lex_escape_ch[assign(c1)].parse(scan);
if (*scan.first == '-') // insert a range
{
++scan.first;
char_t c2;
lex_escape_ch[assign(c2)].parse(scan);
BOOST_ASSERT(c1 < c2); // Throw exception?
rr.set(utility::impl::range<char_t>(c1, c2));
}
else // insert 1 char
{
rr.set(utility::impl::range<char_t>(c1, c1));
}
}
}
if (negated_ccl)
{
rr = ccl_utils::negate_range_run(rr);
}
ccl_utils::create_nodes(rr, m_stack);
}
std::stack<node*>& m_stack;
};
template <typename ScannerT>
class make_any_char
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
std::stack<node*>& m_stack;
make_any_char(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(const char_t c) const
{
BOOST_ASSERT(c == '.');
do_any_char();
}
void do_any_char() const
{
static utility::impl::range_run<char_t> rr;
rr.set(full_range<char_t>());
char_t newline = '\n';
rr.clear(utility::impl::range<char_t>(newline, newline));
ccl_utils::create_nodes(rr, m_stack);
}
};
template <typename ScannerT>
class make_string
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
std::stack<node*>& m_stack;
make_string(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(iterator_type const& first, iterator_type const& last) const
{
BOOST_ASSERT(*first == '"');
iterator_type first_ = first;
ScannerT scan(first_, last);
++scan.first; // skip over '"'
// empty string not allowed
if (*scan.first == '"')
{
boost::throw_exception(bad_regex());
}
const escape_char_parser<lex_escapes, char_t> lex_escape_ch =
escape_char_parser<lex_escapes, char_t>();
char_t c;
lex_escape_ch[assign(c)].parse(scan);
node* top_node = ccl_utils::create_mb_node_seq(c);
while (*scan.first != '"' && scan.first != scan.last)
{
lex_escape_ch[assign(c)].parse(scan);
node* cur_node = ccl_utils::create_mb_node_seq(c);
top_node = new cat_node(top_node, cur_node);
}
m_stack.push(top_node);
}
};
inline
node* repeat_node(node* n, int num)
{
node* list_of_nodes = n;
for (int i = 1; i < num; ++i)
{
list_of_nodes = new cat_node(list_of_nodes, n->clone());
}
return list_of_nodes;
}
inline
node* optional_node(node* n)
{
return new or_node(n, new epsilon_node());
}
template <typename ScannerT>
class make_rep1
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
std::stack<node*>& m_stack;
make_rep1(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(iterator_type const& first, iterator_type const& last) const
{
BOOST_ASSERT(*first == '{');
iterator_type first_ = first;
ScannerT scan(first_, last);
++scan.first; // skip over '{'
unsigned int count;
uint_p[assign(count)].parse(scan);
if (count == 0)
boost::throw_exception(bad_regex());
node* top_node = m_stack.top();
m_stack.pop();
top_node = repeat_node(top_node, count);
m_stack.push(top_node);
}
};
template <typename ScannerT>
class make_rep2
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
std::stack<node*>& m_stack;
make_rep2(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(iterator_type const& first, iterator_type const& last) const
{
BOOST_ASSERT(*first == '{');
iterator_type first_ = first;
ScannerT scan (first_, last);
++scan.first; // skip over '{'
unsigned int count;
uint_p[assign(count)].parse(scan);
if (count == 0)
boost::throw_exception(bad_regex());
node* top_node = m_stack.top();
m_stack.pop();
top_node = new cat_node(repeat_node(top_node, count),
new star_node(top_node->clone()));
m_stack.push(top_node);
}
};
template <typename ScannerT>
class make_rep3
{
typedef typename ScannerT::iterator_t iterator_type;
public:
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
std::stack<node*>& m_stack;
make_rep3(std::stack<node*>& the_stack)
: m_stack(the_stack)
{}
void operator()(iterator_type const& first, iterator_type const& last) const
{
BOOST_ASSERT(*first == '{');
iterator_type first_ = first;
ScannerT scan(first_, last);
++scan.first; // skip over '{'
unsigned int count1, count2;
uint_p[assign(count1)].parse(scan);
if (count1 == 0)
boost::throw_exception(bad_regex());
++scan.first; // skip over ','
uint_p[assign(count2)].parse(scan);
if (count2 <= count1)
boost::throw_exception(bad_regex());
node* top_node = m_stack.top();
m_stack.pop();
node* repeats = repeat_node(top_node, count1);
top_node = new cat_node(repeats,
repeat_node(optional_node(top_node->clone()),
count2 - count1));
m_stack.push(top_node);
}
};
///////////////////////////////////////////////////////////////////////////////
//
// Lexer grammar
//
// Defines the grammar, which mandates the syntax of the understood
// lexeme definitions passed to lexer::register_regex.
//
///////////////////////////////////////////////////////////////////////////////
class lexer_grammar : public boost::spirit::grammar<lexer_grammar>
{
public:
lexer_grammar(std::stack<node*> &node_stack_)
: node_stack(node_stack_) {}
template <typename ScannerT>
struct definition
{
typedef rule<ScannerT> rule_t;
typedef typename ScannerT::iterator_t iterator_type;
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<iterator_type>::value_type
char_t;
rule_t regex, re, series, singleton, singleton2, fullccl, ccl, string,
escseq, ccl_char;
symbols<> ccl_expr;
definition(lexer_grammar const &self)
{
regex =
re >> !('/' >> re) >> !ch_p('$')
;
re =
series
>>*( ('|' >> series)[make_or<ScannerT>(self.node_stack)] )
;
series =
singleton
>>*( singleton[make_concat<ScannerT>(self.node_stack)] )
;
singleton =
ch_p('.')[make_any_char<ScannerT>(self.node_stack)]
>> singleton2
| fullccl
>> singleton2
| ('"' >> string >> '"')
[
make_string<ScannerT>(self.node_stack)
]
>> singleton2
| '(' >> re >> ')'
>> singleton2
| ((anychar_p - chset<>("/|*+?.(){}\\")) | escseq)
[
make_char<ScannerT>(self.node_stack)
]
>> singleton2
;
singleton2 =
ch_p('*')[make_star<ScannerT>(self.node_stack)]
>> singleton2
| ch_p('+')[make_plus<ScannerT>(self.node_stack)]
>> singleton2
| ch_p('?')[make_optional<ScannerT>(self.node_stack)]
>> singleton2
| ('{' >> uint_p >> '}')
[
make_rep1<ScannerT>(self.node_stack)
]
>> singleton2
| ('{' >> uint_p >> ',' >> '}')
[
make_rep2<ScannerT>(self.node_stack)
]
>> singleton2
| ('{' >> uint_p >> ',' >> uint_p >> '}')
[
make_rep3<ScannerT>(self.node_stack)
]
>> singleton2
| epsilon_p
;
fullccl =
('[' >> !ch_p('^') >> ccl >> ']')
[
make_ccl<ScannerT>(self.node_stack)
]
;
ccl =
*(ccl_expr | (ccl_char >> !('-' >> ccl_char)))
;
ccl_char =
( (anychar_p - chset<>("\\\n]")) | escseq )
;
ccl_expr =
"[:alnum:]",
"[:alpha:]",
"[:blank:]",
"[:cntrl:]",
"[:digit:]",
"[:graph:]",
"[:lower:]",
"[:print:]",
"[:punct:]",
"[:space:]",
"[:upper:]",
"[:xdigit:]"
;
string =
+( (anychar_p - chset<>("\"\\")) | escseq )
;
typedef
uint_parser<char_t, 8, 1,
std::numeric_limits<char_t>::digits / 3 + 1
> oct_parser_t;
typedef
uint_parser<char_t, 16, 1,
std::numeric_limits<char_t>::digits / 4 + 1
> hex_parser_t;
escseq =
ch_p('\\')
>> (
oct_parser_t()
| as_lower_d['x'] >> hex_parser_t()
| (anychar_p - chset<>('\n'))
)
;
#define BOOST_SLEX_DEBUG (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
BOOST_SPIRIT_DEBUG_TRACE_RULE(regex, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(re, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(series, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(singleton, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(singleton2, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(fullccl, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(ccl, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(string, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(escseq, BOOST_SLEX_DEBUG);
BOOST_SPIRIT_DEBUG_TRACE_RULE(ccl_char, BOOST_SLEX_DEBUG);
#undef BOOST_SLEX_DEBUG
}
rule<ScannerT> const&
start() const { return regex; }
};
std::stack<node*> &node_stack;
}; // class lexer_grammar
template <typename StringT>
inline node *
parse(lexer_grammar& g, StringT const& str)
{
typedef
scanner<typename StringT::const_iterator, scanner_policies<> >
scanner_t;
typedef typename rule<scanner_t>::template result<scanner_t>::type
result_t;
typename StringT::const_iterator first = str.begin();
typename StringT::const_iterator last = str.end();
scanner_t scan(first, last);
// typename rule<scanner_t>::result_t hit = g.parse(scan);
result_t hit = g.parse(scan);
if (!hit || !scan.at_end())
{
while (g.node_stack.size())
{
delete g.node_stack.top();
g.node_stack.pop();
}
return 0;
}
BOOST_ASSERT(g.node_stack.size() == 1);
node* rval = g.node_stack.top();
g.node_stack.pop();
node* an_eof_node = new eof_node();
rval = new cat_node(rval, an_eof_node);
return rval;
}
inline
void make_case_insensitive(state_match_t& state_match)
{
// TODO: Fix this.
// This doesn't take into account foreign languages, figure out how to
// do that. Also this approach is broken for this implementation of
// wide chars.
for (state_match_t::iterator iter = state_match.begin();
iter != state_match.end(); ++iter)
{
int i, j;
for (i = 'A', j = 'a'; i <= 'Z'; ++i, ++j)
{
// if either is set, turn them both on
(*iter)[i] = (*iter)[j] = uchar((*iter)[i] | (*iter)[j]);
}
}
}
template<bool wide_char>
struct regex_match_helper;
template<>
struct regex_match_helper<false> // single byte char
{
template <typename DfaT, typename IteratorT>
static bool
do_match(DfaT const& dfa, IteratorT &first, IteratorT const& last,
int& regex_index,
std::basic_string<
typename BOOST_SPIRIT_IT_NS::iterator_traits<IteratorT>::value_type
> *token)
{
typedef std::basic_string<
typename BOOST_SPIRIT_IT_NS::iterator_traits<IteratorT>::value_type
> string_type;
typedef typename string_type::size_type size_type;
node_id_t s = 0;
node_id_t last_accepting_index = invalid_node;
IteratorT p = first;
IteratorT last_accepting_cpos = first;
while (p != last)
{
s = dfa.transition_table[s][(uchar)*p];
if (s == invalid_node)
break;
if (token) token->append((size_type)1, *p);
++p;
if (dfa.acceptance_index[s] != invalid_node)
{
last_accepting_index = s;
last_accepting_cpos = p;
}
}
if (last_accepting_index != invalid_node)
{
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
std::cout << "dfa.acceptance_index[" << last_accepting_index << "] = " <<
dfa.acceptance_index[last_accepting_index] << '\n';
#endif
first = last_accepting_cpos;
regex_index = dfa.acceptance_index[last_accepting_index];
return true;
}
else
return false;
}
};
template<>
struct regex_match_helper<true> // wide char
{
template <typename DfaT, typename IteratorT>
static bool
do_match(DfaT const& dfa, IteratorT &first, IteratorT const& last,
int& regex_index,
std::basic_string<
typename BOOST_SPIRIT_IT_NS::iterator_traits<IteratorT>::value_type
> *token)
{
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<IteratorT>::value_type
char_t;
typedef std::basic_string<char_t> string_type;
typedef typename string_type::size_type size_type;
node_id_t s = 0;
node_id_t last_accepting_index = invalid_node;
IteratorT wp = first;
IteratorT last_accepting_cpos = first;
while (wp != last)
{
for (unsigned int i = 0; i < sizeof(char_t); ++i)
{
s = dfa.transition_table[s][get_byte(*wp,i)];
if (s == invalid_node)
{
goto break_while;
}
}
if (token) token->append((size_type)1, *wp);
++wp;
if (dfa.acceptance_index[s] != invalid_node)
{
last_accepting_index = s;
last_accepting_cpos = wp;
}
}
break_while:
if (last_accepting_index != invalid_node)
{
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
std::cout << "dfa.acceptance_index[" << last_accepting_index << "] = " <<
dfa.acceptance_index[last_accepting_index] << '\n';
#endif
first = last_accepting_cpos;
regex_index = dfa.acceptance_index[last_accepting_index];
return true;
}
else
return false;
}
};
template <typename DfaT, typename IteratorT, bool wide_char>
struct regex_match
{
static bool
do_match(DfaT const& dfa, IteratorT &first, IteratorT const& last,
int& regex_index,
std::basic_string<
typename BOOST_SPIRIT_IT_NS::iterator_traits<IteratorT>::value_type
> *token)
{
return regex_match_helper<wide_char>::do_match(
dfa, first, last, regex_index, token);
}
};
} // namespace lexerimpl
///////////////////////////////////////////////////////////////////////////////
//
template <typename IteratorT = char const*, typename TokenT = int,
typename CallbackT = void(*)(IteratorT const &,
IteratorT &,
IteratorT const&,
TokenT const&,
lexer_control<TokenT> &)>
class lexer
{
public:
typedef CallbackT callback_t;
typedef
typename BOOST_SPIRIT_IT_NS::iterator_traits<IteratorT>::value_type
char_t;
struct regex_info
{
std::basic_string<char_t> str;
TokenT token;
CallbackT callback;
regex_info(const std::basic_string<char_t>& _str,
const TokenT& _token,
const CallbackT& _callback)
: str(_str)
, token(_token)
, callback(_callback)
{}
};
struct dfa_table
{
std::vector<std::vector<node_id_t> > transition_table;
std::vector<node_id_t> acceptance_index;
};
typedef std::vector<node_id_t> node_table_t;
typedef std::vector<node_table_t> transition_table_t;
typedef std::vector<dfa_table> dfa_t;
lexer(unsigned int states = 1);
void register_regex(const std::basic_string<char_t>& regex,
const TokenT& id, const CallbackT& cb = CallbackT(),
unsigned int state = 0);
TokenT next_token(IteratorT &first, IteratorT const &last,
std::basic_string<char_t> *token = 0);
void create_dfa();
bool has_compiled_dfa() { return m_compiled_dfa; }
void set_case_insensitive(bool insensitive);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
void dump(std::ostream& out);
#endif
typedef std::vector<std::vector<regex_info> > regex_list_t;
bool load (std::ifstream &in, long unique_id = 0);
bool save (std::ofstream &out, long unique_id = 0);
enum {
SLEX_SIGNATURE = 0x58454C53, // "SLEX"
SLEX_VERSION_100 = 0x0100, // persistance version
SLEX_LAST_KNOWN_VERSION = SLEX_VERSION_100,
SLEX_MINOR_VERSION_MASK = 0xFF
};
private:
void create_dfa_for_state(int state);
static bool regex_match(const dfa_t& dfa, IteratorT& first,
IteratorT& last, int& regex_index);
mutable std::stack<lexerimpl::node*> node_stack;
lexerimpl::lexer_grammar g;
mutable bool m_compiled_dfa;
mutable dfa_t m_dfa;
regex_list_t m_regex_list;
bool m_case_insensitive;
unsigned int m_state;
std::stack<unsigned int> m_state_stack;
unsigned int m_num_states;
};
template <typename IteratorT, typename TokenT, typename CallbackT>
inline
lexer<IteratorT, TokenT, CallbackT>::lexer(unsigned int states)
: g(node_stack)
, m_compiled_dfa(false)
, m_regex_list(states)
, m_case_insensitive(false)
, m_state(0)
, m_state_stack()
, m_num_states(states)
{
BOOST_SPIRIT_DEBUG_TRACE_NODE_NAME(g, "slex::lexer",
BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX);
}
template <typename IteratorT, typename TokenT, typename CallbackT>
inline void
lexer<IteratorT, TokenT, CallbackT>::register_regex(
const std::basic_string<char_t>& regex, const TokenT& id,
const CallbackT& callback, unsigned int state)
{
if (state > m_num_states) {
m_regex_list.resize(state);
m_num_states = state;
}
m_regex_list[state].push_back(regex_info(regex, id, callback));
}
template <typename IteratorT, typename TokenT, typename CallbackT>
inline TokenT
lexer<IteratorT, TokenT, CallbackT>::next_token(
IteratorT &first, IteratorT const& last,
std::basic_string<
typename BOOST_SPIRIT_IT_NS::iterator_traits<IteratorT>::value_type
> *token)
{
if (!m_compiled_dfa)
{
create_dfa();
}
IteratorT saved = first;
int regex_index;
if (!lexerimpl::regex_match<dfa_table, IteratorT, (sizeof(char_t) > 1)>::
do_match(m_dfa[m_state], first, last, regex_index, token))
return -1; // TODO: can't return -1, need to return some invalid token.
// how to figure this out? We can use traits I guess.
else
{
regex_info regex = m_regex_list[m_state][regex_index];
TokenT rval = regex.token;
if (regex.callback)
{
// execute corresponding callback
lexer_control<TokenT> controller(rval, m_state, m_state_stack);
regex.callback(saved, first, last, regex.token, controller);
if (controller.ignore_current_token_set()) {
if (token)
token->erase();
return next_token(first, last, token);
}
}
return rval;
}
}
namespace lexerimpl
{
inline
bool find_acceptance_state(const node_set& eof_node_ids,
const node_set& current_set,
node_id_t& acceptance_node_id)
{
for(node_set::const_iterator nsi = eof_node_ids.begin();
nsi != eof_node_ids.end(); ++nsi)
{
node_id_t eof_node_id =*nsi;
if (current_set.end() != current_set.find(eof_node_id))
{
// store the first one we come to as the
// matched pattern
acceptance_node_id = eof_node_id;
// don't bother searching for more
return true;
}
}
return false;
}
template <typename RegexListT, typename GrammarT>
inline std::auto_ptr<node>
parse_regexes(const RegexListT& regex_list, GrammarT& g)
{
// parse the expressions into a tree
if (regex_list.begin() == regex_list.end())
boost::throw_exception(bad_regex());
typename RegexListT::const_iterator ri = regex_list.begin();
std::auto_ptr<node> tree(lexerimpl::parse(g, (*ri).str));
if (tree.get() == 0)
boost::throw_exception(bad_regex());
++ri;
for (/**/; ri != regex_list.end(); ++ri)
{
std::auto_ptr<node> next_tree(lexerimpl::parse(g, (*ri).str));
if (next_tree.get() == 0)
boost::throw_exception(bad_regex());
std::auto_ptr<node> newnode(new or_node(tree.release(), next_tree.release()));
tree = newnode;
}
return tree;
}
} //namespace lexerimpl
template <typename IteratorT, typename TokenT, typename CallbackT>
inline void
lexer<IteratorT, TokenT, CallbackT>::create_dfa()
{
m_dfa.resize(m_num_states);
for (unsigned int i = 0; i < m_num_states; ++i)
create_dfa_for_state(i);
}
// Algorithm from Compilers: Principles, Techniques, and Tools p. 141
template <typename IteratorT, typename TokenT, typename CallbackT>
inline void
lexer<IteratorT, TokenT, CallbackT>::create_dfa_for_state(int state)
{
using lexerimpl::node;
std::auto_ptr<node> tree = lexerimpl::parse_regexes(m_regex_list[state], g);
node_id_t dummy = 0;
tree->assign_node_ids(dummy);
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
tree->dump(std::cout);
#endif
// compute followpos(root)
followpos_t followpos;
tree->compute_followpos(followpos);
// the dfa states <-> nfa state groups
std::map<node_set, node_id_t> dstates1;
std::map<node_id_t, node_set> dstates2;
// the dfa transitions
m_dfa[state].transition_table.push_back(
std::vector<node_id_t>(256, invalid_node));
m_dfa[state].acceptance_index.push_back(invalid_node);
// whether the dfa state has been processed yet
std::vector<node_id_t> marked;
// used to give a unique id to each dfa state
node_id_t num_states = 0;
// initially, the only unmarked state in Dstates is firstpos(root).
marked.push_back(0);
node_set fpr = tree->firstpos();
dstates1[fpr] = 0;
dstates2[0] = fpr;
state_match_t state_match;
tree->compute_state_match(state_match);
if (m_case_insensitive)
lexerimpl::make_case_insensitive(state_match);
node_set eof_node_ids;
tree->get_eof_ids(eof_node_ids);
// translate the eof_node_ids into a 0-based index
std::map<node_id_t, node_id_t> eof_node_id_map;
unsigned int x = 0;
for (node_set::iterator node_id_it = eof_node_ids.begin();
node_id_it != eof_node_ids.end();
++node_id_it)
{
eof_node_id_map[*node_id_it] = x++;
}
// figure out if this is an acceptance state
node_id_t eof_node_id;
if (lexerimpl::find_acceptance_state(eof_node_ids, fpr, eof_node_id))
{
m_dfa[state].acceptance_index[0] = eof_node_id_map[eof_node_id];
}
std::vector<node_id_t>::iterator i = std::find(marked.begin(), marked.end(),
node_id_t(0));
while (marked.end() != i)
{
*i = 1;
node_id_t T = node_id_t(std::distance(marked.begin(), i));
BOOST_ASSERT(T < dstates2.size());
node_set Tstates = dstates2[T];
for (node_id_t j = 0; j < 256; ++j)
{
node_set U;
for (node_set::iterator k = Tstates.begin();
k != Tstates.end(); ++k)
{
node_id_t p =*k;
BOOST_ASSERT(p < state_match.size());
BOOST_ASSERT(j < state_match[p].size());
if (state_match[p][j])
{
node_set fpp = followpos[p];
U.insert(fpp.begin(), fpp.end());
}
}
if (U.size() > 0)
{
std::map<node_set, node_id_t>::iterator l = dstates1.find(U);
node_id_t target_state;
if (l == dstates1.end()) // not in the states yet
{
++num_states;
dstates1[U] = target_state = num_states;
dstates2[target_state] = U;
marked.push_back(0);
m_dfa[state].transition_table.push_back(
std::vector<node_id_t>(256, invalid_node));
m_dfa[state].acceptance_index.push_back(invalid_node);
// figure out if this is an acceptance state
node_id_t eof_node_id;
if (lexerimpl::find_acceptance_state(eof_node_ids, U, eof_node_id))
{
m_dfa[state].acceptance_index[target_state] =
eof_node_id_map[eof_node_id];
}
}
else
{
target_state = dstates1[U];
}
BOOST_ASSERT(T < m_dfa[state].transition_table.size());
BOOST_ASSERT(j < m_dfa[state].transition_table[T].size());
m_dfa[state].transition_table[T][j] = target_state;
}
}
i = std::find(marked.begin(), marked.end(), node_id_t(0));
}
m_compiled_dfa = true;
}
template <typename IteratorT, typename TokenT, typename CallbackT>
inline void
lexer<IteratorT, TokenT, CallbackT>::set_case_insensitive(bool insensitive)
{
m_case_insensitive = insensitive;
}
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_SLEX)
template <typename IteratorT, typename TokenT, typename CallbackT>
inline void
lexer<IteratorT, TokenT, CallbackT>::dump(std::ostream& out)
{
for (unsigned x = 0; x < m_dfa.size(); ++x)
{
out << "\nm_dfa[" << x << "] has " << m_dfa[x].transition_table.size() << " states\n";
for (node_id_t i = 0; i < m_dfa[x].transition_table.size(); ++i)
{
out << "state " << i << ":";
for (node_id_t j = 0; j < m_dfa[x].transition_table[i].size(); ++j)
{
if (m_dfa[x].transition_table[i][j] != invalid_node)
out << j << "->" << m_dfa[x].transition_table[i][j] << " ";
}
out << "\n";
}
out << "acceptance states: ";
for(unsigned int k = 0; k < m_dfa[x].acceptance_index.size(); ++k)
{
if (m_dfa[x].acceptance_index[k] != invalid_node)
out << '<' << k << ',' << m_dfa[x].acceptance_index[k] << "> ";
}
out << endl;
}
}
#endif
///////////////////////////////////////////////////////////////////////////////
// load the lexer tables
#define slex_in(strm, val) \
strm.read((char*)&val, sizeof(val)); \
if (std::ios::goodbit != strm.rdstate()) return false
template <typename IteratorT, typename TokenT, typename CallbackT>
inline bool
lexer<IteratorT, TokenT, CallbackT>::load (std::ifstream &in, long unique_id)
{
// ensure correct signature and version
long signature = 0;
slex_in (in, signature);
if (signature != SLEX_SIGNATURE)
return false; // not for us
long version = 0;
slex_in (in, version);
if ((version & ~SLEX_MINOR_VERSION_MASK) > SLEX_LAST_KNOWN_VERSION)
return false; // to new for us
long uid = 0;
slex_in (in, uid);
if (uid != unique_id)
return false; // not saved by us
// load auxiliary members
int num_states = 0;
slex_in (in, num_states);
// load the dfa tables
dfa_t in_dfa;
std::size_t dfa_size = 0;
slex_in (in, dfa_size);
in_dfa.resize(dfa_size);
for (std::size_t dfa = 0; dfa < dfa_size; ++dfa)
{
// load the transition tables
std::size_t tt_size = 0;
transition_table_t &tt_table = in_dfa[dfa].transition_table;
slex_in (in, tt_size);
tt_table.resize(tt_size);
for (std::size_t tt = 0; tt < tt_size; ++tt)
{
std::size_t nt_size = 0;
node_table_t &nt_table = tt_table[tt];
slex_in (in, nt_size);
nt_table.resize(nt_size);
for (std::size_t nt = 0; nt < nt_size; ++nt)
{
slex_in (in, nt_table[nt]);
}
}
// load the acceptance index table
std::size_t ai_size = 0;
node_table_t &ai_table = in_dfa[dfa].acceptance_index;
slex_in (in, ai_size);
ai_table.resize(ai_size);
for (std::size_t ai = 0; ai < ai_size; ++ai)
{
slex_in (in, ai_table[ai]);
}
}
m_dfa.swap(in_dfa); // success, swap in the read values
m_num_states = num_states;
m_compiled_dfa = true;
return true;
}
#undef slex_in
///////////////////////////////////////////////////////////////////////////////
// save the lexer tables
#define slex_out(strm, val) \
strm.write((char*)&val, sizeof(val)); \
if (std::ios::goodbit != strm.rdstate()) return false
template <typename IteratorT, typename TokenT, typename CallbackT>
inline bool
lexer<IteratorT, TokenT, CallbackT>::save (std::ofstream &out, long unique_id)
{
// save signature and version information
long out_long = SLEX_SIGNATURE;
slex_out(out, out_long);
out_long = SLEX_VERSION_100;
slex_out(out, out_long);
slex_out(out, unique_id);
// save auxiliary members
slex_out(out, m_num_states);
// save the dfa tables
typedef typename dfa_t::const_iterator dfa_iter_t;
typedef transition_table_t::const_iterator transition_table_iter_t;
typedef node_table_t::const_iterator node_table_iter_t;
std::size_t out_size_t = m_dfa.size();
slex_out(out, out_size_t);
dfa_iter_t end = m_dfa.end();
for (dfa_iter_t it = m_dfa.begin(); it != end; ++it)
{
// save the transition table
out_size_t = (*it).transition_table.size();
slex_out(out, out_size_t);
transition_table_iter_t tt_end = (*it).transition_table.end();
for (transition_table_iter_t tt_it = (*it).transition_table.begin();
tt_it != tt_end;
++tt_it)
{
out_size_t = (*tt_it).size();
slex_out(out, out_size_t);
node_table_iter_t nt_end = (*tt_it).end();
for (node_table_iter_t nt_it = (*tt_it).begin();
nt_it != nt_end;
++nt_it)
{
slex_out(out, (*nt_it));
}
}
// save the acceptance index table
out_size_t = (*it).acceptance_index.size();
slex_out(out, out_size_t);
node_table_iter_t nt_end = (*it).acceptance_index.end();
for (node_table_iter_t nt_it = (*it).acceptance_index.begin();
nt_it != nt_end;
++nt_it)
{
slex_out(out, (*nt_it));
}
}
return true;
}
#undef slex_out
/*
a lexer_control object supports some operations on the lexer.
get current lexer state
set state
terminate
state stack (push, pop, top)
set new token return value
ignore the current token
yymore
get length of matched token
*/
template <typename TokenT>
class lexer_control
{
public:
lexer_control(TokenT& token, unsigned int& current_state,
std::stack<unsigned int>& state_stack);
// functions dealing with the lexer state
// set the state to state
void set_state(unsigned int state);
// get the current state
unsigned int get_state();
// pushes the current state onto the top of the state stack and
// switches to new_state
void push_state(unsigned int new_state);
// pops the top of the state stack and switches to it.
void pop_state();
// returns the top of the stack without altering the stack's contents.
unsigned int top_state();
// functions dealing with the token returned.
// set a new TokenT return value, overriding that one that was
// registered via. register_regex()
void set_token(const TokenT& token);
// tell the lexer to return an invalid token, signifying termination.
void terminate();
// ignore the current token, and move on to the next one. The current
// token will NOT be returned from next_token()
void ignore_current_token();
// returns true if ignore_current_token() has been called,
// false otherwise.
bool ignore_current_token_set();
private:
TokenT& m_token;
bool m_ignore_current_token;
unsigned int& m_current_state;
std::stack<unsigned int>& m_state_stack;
};
template <typename TokenT>
inline
lexer_control<TokenT>::lexer_control(TokenT& token, unsigned int& current_state,
std::stack<unsigned int>& state_stack)
: m_token(token)
, m_ignore_current_token(false)
, m_current_state(current_state)
, m_state_stack(state_stack)
{
}
template <typename TokenT>
inline void
lexer_control<TokenT>::set_state(unsigned int state)
{
m_current_state = state;
}
template <typename TokenT>
inline unsigned int
lexer_control<TokenT>::get_state()
{
return m_current_state;
}
template <typename TokenT>
inline void
lexer_control<TokenT>::push_state(unsigned int new_state)
{
m_state_stack.push(m_current_state);
m_current_state = new_state;
}
template <typename TokenT>
inline void
lexer_control<TokenT>::pop_state()
{
m_current_state = m_state_stack.top();
m_state_stack.pop();
}
template <typename TokenT>
inline unsigned int
lexer_control<TokenT>::top_state()
{
return m_state_stack.top();
}
template <typename TokenT>
inline void
lexer_control<TokenT>::set_token(const TokenT& token)
{
m_token = token;
}
template <typename TokenT>
inline void
lexer_control<TokenT>::terminate()
{
m_token = -1; // TOOD: fix this, need to be determined by traits
}
template <typename TokenT>
inline void
lexer_control<TokenT>::ignore_current_token()
{
m_ignore_current_token = true;
}
template <typename TokenT>
inline bool
lexer_control<TokenT>::ignore_current_token_set()
{
return m_ignore_current_token;
}
} // namespace spirit
} // namespace boost
#undef BOOST_SPIRIT_IT_NS
#endif
|