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
|
------------------------------------------------------------------------------
-- --
-- GNAT LIBRARY COMPONENTS --
-- --
-- G N A T . R E G P A T --
-- --
-- B o d y --
-- --
-- --
-- Copyright (C) 1986 by University of Toronto. --
-- Copyright (C) 1996-2002 Ada Core Technologies, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. You should have received a copy of the GNU General --
-- Public License distributed with GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
-- MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from this --
-- unit, or you link this unit with other files to produce an executable, --
-- this unit does not by itself cause the resulting executable to be --
-- covered by the GNU General Public License. This exception does not --
-- however invalidate any other reasons why the executable file might be --
-- covered by the GNU Public License. --
-- --
-- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
-- --
------------------------------------------------------------------------------
-- This is an altered Ada 95 version of the original V8 style regular
-- expression library written in C by Henry Spencer. Apart from the
-- translation to Ada, the interface has been considerably changed to
-- use the Ada String type instead of C-style nul-terminated strings.
-- Beware that some of this code is subtly aware of the way operator
-- precedence is structured in regular expressions. Serious changes in
-- regular-expression syntax might require a total rethink.
with System.IO; use System.IO;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Unchecked_Conversion;
package body GNAT.Regpat is
MAGIC : constant Character := Character'Val (10#0234#);
-- The first byte of the regexp internal "program" is actually
-- this magic number; the start node begins in the second byte.
--
-- This is used to make sure that a regular expression was correctly
-- compiled.
----------------------------
-- Implementation details --
----------------------------
-- This is essentially a linear encoding of a nondeterministic
-- finite-state machine, also known as syntax charts or
-- "railroad normal form" in parsing technology.
-- Each node is an opcode plus a "next" pointer, possibly plus an
-- operand. "Next" pointers of all nodes except BRANCH implement
-- concatenation; a "next" pointer with a BRANCH on both ends of it
-- is connecting two alternatives.
-- The operand of some types of node is a literal string; for others,
-- it is a node leading into a sub-FSM. In particular, the operand of
-- a BRANCH node is the first node of the branch.
-- (NB this is *not* a tree structure: the tail of the branch connects
-- to the thing following the set of BRANCHes).
-- You can see the exact byte-compiled version by using the Dump
-- subprogram. However, here are a few examples:
-- (a|b): 1 : MAGIC
-- 2 : BRANCH (next at 10)
-- 5 : EXACT (next at 18) operand=a
-- 10 : BRANCH (next at 18)
-- 13 : EXACT (next at 18) operand=b
-- 18 : EOP (next at 0)
--
-- (ab)*: 1 : MAGIC
-- 2 : CURLYX (next at 26) { 0, 32767}
-- 9 : OPEN 1 (next at 13)
-- 13 : EXACT (next at 19) operand=ab
-- 19 : CLOSE 1 (next at 23)
-- 23 : WHILEM (next at 0)
-- 26 : NOTHING (next at 29)
-- 29 : EOP (next at 0)
-- The opcodes are:
type Opcode is
-- Name Operand? Meaning
(EOP, -- no End of program
MINMOD, -- no Next operator is not greedy
-- Classes of characters
ANY, -- no Match any one character except newline
SANY, -- no Match any character, including new line
ANYOF, -- class Match any character in this class
EXACT, -- str Match this string exactly
EXACTF, -- str Match this string (case-folding is one)
NOTHING, -- no Match empty string
SPACE, -- no Match any whitespace character
NSPACE, -- no Match any non-whitespace character
DIGIT, -- no Match any numeric character
NDIGIT, -- no Match any non-numeric character
ALNUM, -- no Match any alphanumeric character
NALNUM, -- no Match any non-alphanumeric character
-- Branches
BRANCH, -- node Match this alternative, or the next
-- Simple loops (when the following node is one character in length)
STAR, -- node Match this simple thing 0 or more times
PLUS, -- node Match this simple thing 1 or more times
CURLY, -- 2num node Match this simple thing between n and m times.
-- Complex loops
CURLYX, -- 2num node Match this complex thing {n,m} times
-- The nums are coded on two characters each.
WHILEM, -- no Do curly processing and see if rest matches
-- Matches after or before a word
BOL, -- no Match "" at beginning of line
MBOL, -- no Same, assuming mutiline (match after \n)
SBOL, -- no Same, assuming single line (don't match at \n)
EOL, -- no Match "" at end of line
MEOL, -- no Same, assuming mutiline (match before \n)
SEOL, -- no Same, assuming single line (don't match at \n)
BOUND, -- no Match "" at any word boundary
NBOUND, -- no Match "" at any word non-boundary
-- Parenthesis groups handling
REFF, -- num Match some already matched string, folded
OPEN, -- num Mark this point in input as start of #n
CLOSE); -- num Analogous to OPEN
for Opcode'Size use 8;
-- Opcode notes:
-- BRANCH
-- The set of branches constituting a single choice are hooked
-- together with their "next" pointers, since precedence prevents
-- anything being concatenated to any individual branch. The
-- "next" pointer of the last BRANCH in a choice points to the
-- thing following the whole choice. This is also where the
-- final "next" pointer of each individual branch points; each
-- branch starts with the operand node of a BRANCH node.
-- STAR,PLUS
-- '?', and complex '*' and '+', are implemented with CURLYX.
-- branches. Simple cases (one character per match) are implemented with
-- STAR and PLUS for speed and to minimize recursive plunges.
-- OPEN,CLOSE
-- ...are numbered at compile time.
-- EXACT, EXACTF
-- There are in fact two arguments, the first one is the length (minus
-- one of the string argument), coded on one character, the second
-- argument is the string itself, coded on length + 1 characters.
-- A node is one char of opcode followed by two chars of "next" pointer.
-- "Next" pointers are stored as two 8-bit pieces, high order first. The
-- value is a positive offset from the opcode of the node containing it.
-- An operand, if any, simply follows the node. (Note that much of the
-- code generation knows about this implicit relationship.)
-- Using two bytes for the "next" pointer is vast overkill for most
-- things, but allows patterns to get big without disasters.
-----------------------
-- Character classes --
-----------------------
-- This is the implementation for character classes ([...]) in the
-- syntax for regular expressions. Each character (0..256) has an
-- entry into the table. This makes for a very fast matching
-- algorithm.
type Class_Byte is mod 256;
type Character_Class is array (Class_Byte range 0 .. 31) of Class_Byte;
type Bit_Conversion_Array is array (Class_Byte range 0 .. 7) of Class_Byte;
Bit_Conversion : constant Bit_Conversion_Array :=
(1, 2, 4, 8, 16, 32, 64, 128);
type Std_Class is (ANYOF_NONE,
ANYOF_ALNUM, -- Alphanumeric class [a-zA-Z0-9]
ANYOF_NALNUM,
ANYOF_SPACE, -- Space class [ \t\n\r\f]
ANYOF_NSPACE,
ANYOF_DIGIT, -- Digit class [0-9]
ANYOF_NDIGIT,
ANYOF_ALNUMC, -- Alphanumeric class [a-zA-Z0-9]
ANYOF_NALNUMC,
ANYOF_ALPHA, -- Alpha class [a-zA-Z]
ANYOF_NALPHA,
ANYOF_ASCII, -- Ascii class (7 bits) 0..127
ANYOF_NASCII,
ANYOF_CNTRL, -- Control class
ANYOF_NCNTRL,
ANYOF_GRAPH, -- Graphic class
ANYOF_NGRAPH,
ANYOF_LOWER, -- Lower case class [a-z]
ANYOF_NLOWER,
ANYOF_PRINT, -- printable class
ANYOF_NPRINT,
ANYOF_PUNCT, --
ANYOF_NPUNCT,
ANYOF_UPPER, -- Upper case class [A-Z]
ANYOF_NUPPER,
ANYOF_XDIGIT, -- Hexadecimal digit
ANYOF_NXDIGIT
);
procedure Set_In_Class
(Bitmap : in out Character_Class;
C : Character);
-- Set the entry to True for C in the class Bitmap.
function Get_From_Class
(Bitmap : Character_Class;
C : Character)
return Boolean;
-- Return True if the entry is set for C in the class Bitmap.
procedure Reset_Class (Bitmap : in out Character_Class);
-- Clear all the entries in the class Bitmap.
pragma Inline (Set_In_Class);
pragma Inline (Get_From_Class);
pragma Inline (Reset_Class);
-----------------------
-- Local Subprograms --
-----------------------
function "=" (Left : Character; Right : Opcode) return Boolean;
function Is_Alnum (C : Character) return Boolean;
-- Return True if C is an alphanum character or an underscore ('_')
function Is_Space (C : Character) return Boolean;
-- Return True if C is a whitespace character
function Is_Printable (C : Character) return Boolean;
-- Return True if C is a printable character
function Operand (P : Pointer) return Pointer;
-- Return a pointer to the first operand of the node at P
function String_Length
(Program : Program_Data;
P : Pointer)
return Program_Size;
-- Return the length of the string argument of the node at P
function String_Operand (P : Pointer) return Pointer;
-- Return a pointer to the string argument of the node at P
procedure Bitmap_Operand
(Program : Program_Data;
P : Pointer;
Op : out Character_Class);
-- Return a pointer to the string argument of the node at P
function Get_Next_Offset
(Program : Program_Data;
IP : Pointer)
return Pointer;
-- Get the offset field of a node. Used by Get_Next.
function Get_Next
(Program : Program_Data;
IP : Pointer)
return Pointer;
-- Dig the next instruction pointer out of a node
procedure Optimize (Self : in out Pattern_Matcher);
-- Optimize a Pattern_Matcher by noting certain special cases
function Read_Natural
(Program : Program_Data;
IP : Pointer)
return Natural;
-- Return the 2-byte natural coded at position IP.
-- All of the subprograms above are tiny and should be inlined
pragma Inline ("=");
pragma Inline (Is_Alnum);
pragma Inline (Is_Space);
pragma Inline (Get_Next);
pragma Inline (Get_Next_Offset);
pragma Inline (Operand);
pragma Inline (Read_Natural);
pragma Inline (String_Length);
pragma Inline (String_Operand);
type Expression_Flags is record
Has_Width, -- Known never to match null string
Simple, -- Simple enough to be STAR/PLUS operand
SP_Start : Boolean; -- Starts with * or +
end record;
Worst_Expression : constant Expression_Flags := (others => False);
-- Worst case
---------
-- "=" --
---------
function "=" (Left : Character; Right : Opcode) return Boolean is
begin
return Character'Pos (Left) = Opcode'Pos (Right);
end "=";
--------------------
-- Bitmap_Operand --
--------------------
procedure Bitmap_Operand
(Program : Program_Data;
P : Pointer;
Op : out Character_Class)
is
function Convert is new Unchecked_Conversion
(Program_Data, Character_Class);
begin
Op (0 .. 31) := Convert (Program (P + 3 .. P + 34));
end Bitmap_Operand;
-------------
-- Compile --
-------------
procedure Compile
(Matcher : out Pattern_Matcher;
Expression : String;
Final_Code_Size : out Program_Size;
Flags : Regexp_Flags := No_Flags)
is
-- We can't allocate space until we know how big the compiled form
-- will be, but we can't compile it (and thus know how big it is)
-- until we've got a place to put the code. So we cheat: we compile
-- it twice, once with code generation turned off and size counting
-- turned on, and once "for real".
-- This also means that we don't allocate space until we are sure
-- that the thing really will compile successfully, and we never
-- have to move the code and thus invalidate pointers into it.
-- Beware that the optimization-preparation code in here knows
-- about some of the structure of the compiled regexp.
PM : Pattern_Matcher renames Matcher;
Program : Program_Data renames PM.Program;
Emit_Code : constant Boolean := PM.Size > 0;
Emit_Ptr : Pointer := Program_First;
Parse_Pos : Natural := Expression'First; -- Input-scan pointer
Parse_End : Natural := Expression'Last;
----------------------------
-- Subprograms for Create --
----------------------------
procedure Emit (B : Character);
-- Output the Character to the Program.
-- If code-generation is disables, simply increments the program
-- counter.
function Emit_Node (Op : Opcode) return Pointer;
-- If code-generation is enabled, Emit_Node outputs the
-- opcode and reserves space for a pointer to the next node.
-- Return value is the location of new opcode, ie old Emit_Ptr.
procedure Emit_Natural (IP : Pointer; N : Natural);
-- Split N on two characters at position IP.
procedure Emit_Class (Bitmap : Character_Class);
-- Emits a character class.
procedure Case_Emit (C : Character);
-- Emit C, after converting is to lower-case if the regular
-- expression is case insensitive.
procedure Parse
(Parenthesized : Boolean;
Flags : in out Expression_Flags;
IP : out Pointer);
-- Parse regular expression, i.e. main body or parenthesized thing
-- Caller must absorb opening parenthesis.
procedure Parse_Branch
(Flags : in out Expression_Flags;
First : Boolean;
IP : out Pointer);
-- Implements the concatenation operator and handles '|'
-- First should be true if this is the first item of the alternative.
procedure Parse_Piece
(Expr_Flags : in out Expression_Flags; IP : out Pointer);
-- Parse something followed by possible [*+?]
procedure Parse_Atom
(Expr_Flags : in out Expression_Flags; IP : out Pointer);
-- Parse_Atom is the lowest level parse procedure.
-- Optimization: gobbles an entire sequence of ordinary characters
-- so that it can turn them into a single node, which is smaller to
-- store and faster to run. Backslashed characters are exceptions,
-- each becoming a separate node; the code is simpler that way and
-- it's not worth fixing.
procedure Insert_Operator
(Op : Opcode;
Operand : Pointer;
Greedy : Boolean := True);
-- Insert_Operator inserts an operator in front of an
-- already-emitted operand and relocates the operand.
-- This applies to PLUS and STAR.
-- If Minmod is True, then the operator is non-greedy.
procedure Insert_Curly_Operator
(Op : Opcode;
Min : Natural;
Max : Natural;
Operand : Pointer;
Greedy : Boolean := True);
-- Insert an operator for CURLY ({Min}, {Min,} or {Min,Max}).
-- If Minmod is True, then the operator is non-greedy.
procedure Link_Tail (P, Val : Pointer);
-- Link_Tail sets the next-pointer at the end of a node chain
procedure Link_Operand_Tail (P, Val : Pointer);
-- Link_Tail on operand of first argument; nop if operandless
function Next_Instruction (P : Pointer) return Pointer;
-- Dig the "next" pointer out of a node
procedure Fail (M : in String);
pragma No_Return (Fail);
-- Fail with a diagnostic message, if possible
function Is_Curly_Operator (IP : Natural) return Boolean;
-- Return True if IP is looking at a '{' that is the beginning
-- of a curly operator, ie it matches {\d+,?\d*}
function Is_Mult (IP : Natural) return Boolean;
-- Return True if C is a regexp multiplier: '+', '*' or '?'
procedure Get_Curly_Arguments
(IP : Natural;
Min : out Natural;
Max : out Natural;
Greedy : out Boolean);
-- Parse the argument list for a curly operator.
-- It is assumed that IP is indeed pointing at a valid operator.
procedure Parse_Character_Class (IP : out Pointer);
-- Parse a character class.
-- The calling subprogram should consume the opening '[' before.
procedure Parse_Literal (Expr_Flags : in out Expression_Flags;
IP : out Pointer);
-- Parse_Literal encodes a string of characters
-- to be matched exactly.
function Parse_Posix_Character_Class return Std_Class;
-- Parse a posic character class, like [:alpha:] or [:^alpha:].
-- The called is suppoed to absorbe the opening [.
pragma Inline (Is_Mult);
pragma Inline (Emit_Natural);
pragma Inline (Parse_Character_Class); -- since used only once
---------------
-- Case_Emit --
---------------
procedure Case_Emit (C : Character) is
begin
if (Flags and Case_Insensitive) /= 0 then
Emit (To_Lower (C));
else
-- Dump current character
Emit (C);
end if;
end Case_Emit;
----------
-- Emit --
----------
procedure Emit (B : Character) is
begin
if Emit_Code then
Program (Emit_Ptr) := B;
end if;
Emit_Ptr := Emit_Ptr + 1;
end Emit;
----------------
-- Emit_Class --
----------------
procedure Emit_Class (Bitmap : Character_Class) is
subtype Program31 is Program_Data (0 .. 31);
function Convert is new Unchecked_Conversion
(Character_Class, Program31);
begin
if Emit_Code then
Program (Emit_Ptr .. Emit_Ptr + 31) := Convert (Bitmap);
end if;
Emit_Ptr := Emit_Ptr + 32;
end Emit_Class;
------------------
-- Emit_Natural --
------------------
procedure Emit_Natural (IP : Pointer; N : Natural) is
begin
if Emit_Code then
Program (IP + 1) := Character'Val (N / 256);
Program (IP) := Character'Val (N mod 256);
end if;
end Emit_Natural;
---------------
-- Emit_Node --
---------------
function Emit_Node (Op : Opcode) return Pointer is
Result : constant Pointer := Emit_Ptr;
begin
if Emit_Code then
Program (Emit_Ptr) := Character'Val (Opcode'Pos (Op));
Program (Emit_Ptr + 1) := ASCII.NUL;
Program (Emit_Ptr + 2) := ASCII.NUL;
end if;
Emit_Ptr := Emit_Ptr + 3;
return Result;
end Emit_Node;
----------
-- Fail --
----------
procedure Fail (M : in String) is
begin
raise Expression_Error;
end Fail;
-------------------------
-- Get_Curly_Arguments --
-------------------------
procedure Get_Curly_Arguments
(IP : Natural;
Min : out Natural;
Max : out Natural;
Greedy : out Boolean)
is
pragma Warnings (Off, IP);
Save_Pos : Natural := Parse_Pos + 1;
begin
Min := 0;
Max := Max_Curly_Repeat;
while Expression (Parse_Pos) /= '}'
and then Expression (Parse_Pos) /= ','
loop
Parse_Pos := Parse_Pos + 1;
end loop;
Min := Natural'Value (Expression (Save_Pos .. Parse_Pos - 1));
if Expression (Parse_Pos) = ',' then
Save_Pos := Parse_Pos + 1;
while Expression (Parse_Pos) /= '}' loop
Parse_Pos := Parse_Pos + 1;
end loop;
if Save_Pos /= Parse_Pos then
Max := Natural'Value (Expression (Save_Pos .. Parse_Pos - 1));
end if;
else
Max := Min;
end if;
if Parse_Pos < Expression'Last
and then Expression (Parse_Pos + 1) = '?'
then
Greedy := False;
Parse_Pos := Parse_Pos + 1;
else
Greedy := True;
end if;
end Get_Curly_Arguments;
---------------------------
-- Insert_Curly_Operator --
---------------------------
procedure Insert_Curly_Operator
(Op : Opcode;
Min : Natural;
Max : Natural;
Operand : Pointer;
Greedy : Boolean := True)
is
Dest : constant Pointer := Emit_Ptr;
Old : Pointer;
Size : Pointer := 7;
begin
-- If the operand is not greedy, insert an extra operand before it
if not Greedy then
Size := Size + 3;
end if;
-- Move the operand in the byte-compilation, so that we can insert
-- the operator before it.
if Emit_Code then
Program (Operand + Size .. Emit_Ptr + Size) :=
Program (Operand .. Emit_Ptr);
end if;
-- Insert the operator at the position previously occupied by the
-- operand.
Emit_Ptr := Operand;
if not Greedy then
Old := Emit_Node (MINMOD);
Link_Tail (Old, Old + 3);
end if;
Old := Emit_Node (Op);
Emit_Natural (Old + 3, Min);
Emit_Natural (Old + 5, Max);
Emit_Ptr := Dest + Size;
end Insert_Curly_Operator;
---------------------
-- Insert_Operator --
---------------------
procedure Insert_Operator
(Op : Opcode;
Operand : Pointer;
Greedy : Boolean := True)
is
Dest : constant Pointer := Emit_Ptr;
Old : Pointer;
Size : Pointer := 3;
begin
-- If not greedy, we have to emit another opcode first
if not Greedy then
Size := Size + 3;
end if;
-- Move the operand in the byte-compilation, so that we can insert
-- the operator before it.
if Emit_Code then
Program (Operand + Size .. Emit_Ptr + Size)
:= Program (Operand .. Emit_Ptr);
end if;
-- Insert the operator at the position previously occupied by the
-- operand.
Emit_Ptr := Operand;
if not Greedy then
Old := Emit_Node (MINMOD);
Link_Tail (Old, Old + 3);
end if;
Old := Emit_Node (Op);
Emit_Ptr := Dest + Size;
end Insert_Operator;
-----------------------
-- Is_Curly_Operator --
-----------------------
function Is_Curly_Operator (IP : Natural) return Boolean is
Scan : Natural := IP;
begin
if Expression (Scan) /= '{'
or else Scan + 2 > Expression'Last
or else not Is_Digit (Expression (Scan + 1))
then
return False;
end if;
Scan := Scan + 1;
-- The first digit
loop
Scan := Scan + 1;
if Scan > Expression'Last then
return False;
end if;
exit when not Is_Digit (Expression (Scan));
end loop;
if Expression (Scan) = ',' then
loop
Scan := Scan + 1;
if Scan > Expression'Last then
return False;
end if;
exit when not Is_Digit (Expression (Scan));
end loop;
end if;
return Expression (Scan) = '}';
end Is_Curly_Operator;
-------------
-- Is_Mult --
-------------
function Is_Mult (IP : Natural) return Boolean is
C : constant Character := Expression (IP);
begin
return C = '*'
or else C = '+'
or else C = '?'
or else (C = '{' and then Is_Curly_Operator (IP));
end Is_Mult;
-----------------------
-- Link_Operand_Tail --
-----------------------
procedure Link_Operand_Tail (P, Val : Pointer) is
begin
if Emit_Code and then Program (P) = BRANCH then
Link_Tail (Operand (P), Val);
end if;
end Link_Operand_Tail;
---------------
-- Link_Tail --
---------------
procedure Link_Tail (P, Val : Pointer) is
Scan : Pointer;
Temp : Pointer;
Offset : Pointer;
begin
if not Emit_Code then
return;
end if;
-- Find last node
Scan := P;
loop
Temp := Next_Instruction (Scan);
exit when Temp = 0;
Scan := Temp;
end loop;
Offset := Val - Scan;
Emit_Natural (Scan + 1, Natural (Offset));
end Link_Tail;
----------------------
-- Next_Instruction --
----------------------
function Next_Instruction (P : Pointer) return Pointer is
Offset : Pointer;
begin
if not Emit_Code then
return 0;
end if;
Offset := Get_Next_Offset (Program, P);
if Offset = 0 then
return 0;
end if;
return P + Offset;
end Next_Instruction;
-----------
-- Parse --
-----------
-- Combining parenthesis handling with the base level
-- of regular expression is a trifle forced, but the
-- need to tie the tails of the branches to what follows
-- makes it hard to avoid.
procedure Parse
(Parenthesized : in Boolean;
Flags : in out Expression_Flags;
IP : out Pointer)
is
E : String renames Expression;
Br : Pointer;
Ender : Pointer;
Par_No : Natural;
New_Flags : Expression_Flags;
Have_Branch : Boolean := False;
begin
Flags := (Has_Width => True, others => False); -- Tentatively
-- Make an OPEN node, if parenthesized
if Parenthesized then
if Matcher.Paren_Count > Max_Paren_Count then
Fail ("too many ()");
end if;
Par_No := Matcher.Paren_Count + 1;
Matcher.Paren_Count := Matcher.Paren_Count + 1;
IP := Emit_Node (OPEN);
Emit (Character'Val (Par_No));
else
IP := 0;
Par_No := 0;
end if;
-- Pick up the branches, linking them together
Parse_Branch (New_Flags, True, Br);
if Br = 0 then
IP := 0;
return;
end if;
if Parse_Pos <= Parse_End
and then E (Parse_Pos) = '|'
then
Insert_Operator (BRANCH, Br);
Have_Branch := True;
end if;
if IP /= 0 then
Link_Tail (IP, Br); -- OPEN -> first
else
IP := Br;
end if;
if not New_Flags.Has_Width then
Flags.Has_Width := False;
end if;
Flags.SP_Start := Flags.SP_Start or New_Flags.SP_Start;
while Parse_Pos <= Parse_End
and then (E (Parse_Pos) = '|')
loop
Parse_Pos := Parse_Pos + 1;
Parse_Branch (New_Flags, False, Br);
if Br = 0 then
IP := 0;
return;
end if;
Link_Tail (IP, Br); -- BRANCH -> BRANCH
if not New_Flags.Has_Width then
Flags.Has_Width := False;
end if;
Flags.SP_Start := Flags.SP_Start or New_Flags.SP_Start;
end loop;
-- Make a closing node, and hook it on the end
if Parenthesized then
Ender := Emit_Node (CLOSE);
Emit (Character'Val (Par_No));
else
Ender := Emit_Node (EOP);
end if;
Link_Tail (IP, Ender);
if Have_Branch then
-- Hook the tails of the branches to the closing node
Br := IP;
loop
exit when Br = 0;
Link_Operand_Tail (Br, Ender);
Br := Next_Instruction (Br);
end loop;
end if;
-- Check for proper termination
if Parenthesized then
if Parse_Pos > Parse_End or else E (Parse_Pos) /= ')' then
Fail ("unmatched ()");
end if;
Parse_Pos := Parse_Pos + 1;
elsif Parse_Pos <= Parse_End then
if E (Parse_Pos) = ')' then
Fail ("unmatched ()");
else
Fail ("junk on end"); -- "Can't happen"
end if;
end if;
end Parse;
----------------
-- Parse_Atom --
----------------
procedure Parse_Atom
(Expr_Flags : in out Expression_Flags;
IP : out Pointer)
is
C : Character;
begin
-- Tentatively set worst expression case
Expr_Flags := Worst_Expression;
C := Expression (Parse_Pos);
Parse_Pos := Parse_Pos + 1;
case (C) is
when '^' =>
if (Flags and Multiple_Lines) /= 0 then
IP := Emit_Node (MBOL);
elsif (Flags and Single_Line) /= 0 then
IP := Emit_Node (SBOL);
else
IP := Emit_Node (BOL);
end if;
when '$' =>
if (Flags and Multiple_Lines) /= 0 then
IP := Emit_Node (MEOL);
elsif (Flags and Single_Line) /= 0 then
IP := Emit_Node (SEOL);
else
IP := Emit_Node (EOL);
end if;
when '.' =>
if (Flags and Single_Line) /= 0 then
IP := Emit_Node (SANY);
else
IP := Emit_Node (ANY);
end if;
Expr_Flags.Has_Width := True;
Expr_Flags.Simple := True;
when '[' =>
Parse_Character_Class (IP);
Expr_Flags.Has_Width := True;
Expr_Flags.Simple := True;
when '(' =>
declare
New_Flags : Expression_Flags;
begin
Parse (True, New_Flags, IP);
if IP = 0 then
return;
end if;
Expr_Flags.Has_Width :=
Expr_Flags.Has_Width or New_Flags.Has_Width;
Expr_Flags.SP_Start :=
Expr_Flags.SP_Start or New_Flags.SP_Start;
end;
when '|' | ASCII.LF | ')' =>
Fail ("internal urp"); -- Supposed to be caught earlier
when '?' | '+' | '*' | '{' =>
Fail ("?+*{ follows nothing");
when '\' =>
if Parse_Pos > Parse_End then
Fail ("trailing \");
end if;
Parse_Pos := Parse_Pos + 1;
case Expression (Parse_Pos - 1) is
when 'b' =>
IP := Emit_Node (BOUND);
when 'B' =>
IP := Emit_Node (NBOUND);
when 's' =>
IP := Emit_Node (SPACE);
Expr_Flags.Simple := True;
Expr_Flags.Has_Width := True;
when 'S' =>
IP := Emit_Node (NSPACE);
Expr_Flags.Simple := True;
Expr_Flags.Has_Width := True;
when 'd' =>
IP := Emit_Node (DIGIT);
Expr_Flags.Simple := True;
Expr_Flags.Has_Width := True;
when 'D' =>
IP := Emit_Node (NDIGIT);
Expr_Flags.Simple := True;
Expr_Flags.Has_Width := True;
when 'w' =>
IP := Emit_Node (ALNUM);
Expr_Flags.Simple := True;
Expr_Flags.Has_Width := True;
when 'W' =>
IP := Emit_Node (NALNUM);
Expr_Flags.Simple := True;
Expr_Flags.Has_Width := True;
when 'A' =>
IP := Emit_Node (SBOL);
when 'G' =>
IP := Emit_Node (SEOL);
when '0' .. '9' =>
IP := Emit_Node (REFF);
declare
Save : Natural := Parse_Pos - 1;
begin
while Parse_Pos <= Expression'Last
and then Is_Digit (Expression (Parse_Pos))
loop
Parse_Pos := Parse_Pos + 1;
end loop;
Emit (Character'Val (Natural'Value
(Expression (Save .. Parse_Pos - 1))));
end;
when others =>
Parse_Pos := Parse_Pos - 1;
Parse_Literal (Expr_Flags, IP);
end case;
when others =>
Parse_Literal (Expr_Flags, IP);
end case;
end Parse_Atom;
------------------
-- Parse_Branch --
------------------
procedure Parse_Branch
(Flags : in out Expression_Flags;
First : Boolean;
IP : out Pointer)
is
E : String renames Expression;
Chain : Pointer;
Last : Pointer;
New_Flags : Expression_Flags;
Dummy : Pointer;
begin
Flags := Worst_Expression; -- Tentatively
if First then
IP := Emit_Ptr;
else
IP := Emit_Node (BRANCH);
end if;
Chain := 0;
while Parse_Pos <= Parse_End
and then E (Parse_Pos) /= ')'
and then E (Parse_Pos) /= ASCII.LF
and then E (Parse_Pos) /= '|'
loop
Parse_Piece (New_Flags, Last);
if Last = 0 then
IP := 0;
return;
end if;
Flags.Has_Width := Flags.Has_Width or New_Flags.Has_Width;
if Chain = 0 then -- First piece
Flags.SP_Start := Flags.SP_Start or New_Flags.SP_Start;
else
Link_Tail (Chain, Last);
end if;
Chain := Last;
end loop;
if Chain = 0 then -- Loop ran zero CURLY
Dummy := Emit_Node (NOTHING);
end if;
end Parse_Branch;
---------------------------
-- Parse_Character_Class --
---------------------------
procedure Parse_Character_Class (IP : out Pointer) is
Bitmap : Character_Class;
Invert : Boolean := False;
In_Range : Boolean := False;
Named_Class : Std_Class := ANYOF_NONE;
Value : Character;
Last_Value : Character := ASCII.Nul;
begin
Reset_Class (Bitmap);
-- Do we have an invert character class ?
if Parse_Pos <= Parse_End
and then Expression (Parse_Pos) = '^'
then
Invert := True;
Parse_Pos := Parse_Pos + 1;
end if;
-- First character can be ] or -, without closing the class.
if Parse_Pos <= Parse_End
and then (Expression (Parse_Pos) = ']'
or else Expression (Parse_Pos) = '-')
then
Set_In_Class (Bitmap, Expression (Parse_Pos));
Parse_Pos := Parse_Pos + 1;
end if;
-- While we don't have the end of the class
while Parse_Pos <= Parse_End
and then Expression (Parse_Pos) /= ']'
loop
Named_Class := ANYOF_NONE;
Value := Expression (Parse_Pos);
Parse_Pos := Parse_Pos + 1;
-- Do we have a Posix character class
if Value = '[' then
Named_Class := Parse_Posix_Character_Class;
elsif Value = '\' then
if Parse_Pos = Parse_End then
Fail ("Trailing \");
end if;
Value := Expression (Parse_Pos);
Parse_Pos := Parse_Pos + 1;
case Value is
when 'w' => Named_Class := ANYOF_ALNUM;
when 'W' => Named_Class := ANYOF_NALNUM;
when 's' => Named_Class := ANYOF_SPACE;
when 'S' => Named_Class := ANYOF_NSPACE;
when 'd' => Named_Class := ANYOF_DIGIT;
when 'D' => Named_Class := ANYOF_NDIGIT;
when 'n' => Value := ASCII.LF;
when 'r' => Value := ASCII.CR;
when 't' => Value := ASCII.HT;
when 'f' => Value := ASCII.FF;
when 'e' => Value := ASCII.ESC;
when 'a' => Value := ASCII.BEL;
-- when 'x' => ??? hexadecimal value
-- when 'c' => ??? control character
-- when '0'..'9' => ??? octal character
when others => null;
end case;
end if;
-- Do we have a character class?
if Named_Class /= ANYOF_NONE then
-- A range like 'a-\d' or 'a-[:digit:] is not a range
if In_Range then
Set_In_Class (Bitmap, Last_Value);
Set_In_Class (Bitmap, '-');
In_Range := False;
end if;
-- Expand the range
case Named_Class is
when ANYOF_NONE => null;
when ANYOF_ALNUM | ANYOF_ALNUMC =>
for Value in Class_Byte'Range loop
if Is_Alnum (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NALNUM | ANYOF_NALNUMC =>
for Value in Class_Byte'Range loop
if not Is_Alnum (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_SPACE =>
for Value in Class_Byte'Range loop
if Is_Space (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NSPACE =>
for Value in Class_Byte'Range loop
if not Is_Space (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_DIGIT =>
for Value in Class_Byte'Range loop
if Is_Digit (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NDIGIT =>
for Value in Class_Byte'Range loop
if not Is_Digit (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_ALPHA =>
for Value in Class_Byte'Range loop
if Is_Letter (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NALPHA =>
for Value in Class_Byte'Range loop
if not Is_Letter (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_ASCII =>
for Value in 0 .. 127 loop
Set_In_Class (Bitmap, Character'Val (Value));
end loop;
when ANYOF_NASCII =>
for Value in 128 .. 255 loop
Set_In_Class (Bitmap, Character'Val (Value));
end loop;
when ANYOF_CNTRL =>
for Value in Class_Byte'Range loop
if Is_Control (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NCNTRL =>
for Value in Class_Byte'Range loop
if not Is_Control (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_GRAPH =>
for Value in Class_Byte'Range loop
if Is_Graphic (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NGRAPH =>
for Value in Class_Byte'Range loop
if not Is_Graphic (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_LOWER =>
for Value in Class_Byte'Range loop
if Is_Lower (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NLOWER =>
for Value in Class_Byte'Range loop
if not Is_Lower (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_PRINT =>
for Value in Class_Byte'Range loop
if Is_Printable (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NPRINT =>
for Value in Class_Byte'Range loop
if not Is_Printable (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_PUNCT =>
for Value in Class_Byte'Range loop
if Is_Printable (Character'Val (Value))
and then not Is_Space (Character'Val (Value))
and then not Is_Alnum (Character'Val (Value))
then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NPUNCT =>
for Value in Class_Byte'Range loop
if not Is_Printable (Character'Val (Value))
or else Is_Space (Character'Val (Value))
or else Is_Alnum (Character'Val (Value))
then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_UPPER =>
for Value in Class_Byte'Range loop
if Is_Upper (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NUPPER =>
for Value in Class_Byte'Range loop
if not Is_Upper (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_XDIGIT =>
for Value in Class_Byte'Range loop
if Is_Hexadecimal_Digit (Character'Val (Value)) then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
when ANYOF_NXDIGIT =>
for Value in Class_Byte'Range loop
if not Is_Hexadecimal_Digit
(Character'Val (Value))
then
Set_In_Class (Bitmap, Character'Val (Value));
end if;
end loop;
end case;
-- Not a character range
elsif not In_Range then
Last_Value := Value;
if Expression (Parse_Pos) = '-'
and then Parse_Pos < Parse_End
and then Expression (Parse_Pos + 1) /= ']'
then
Parse_Pos := Parse_Pos + 1;
-- Do we have a range like '\d-a' and '[:space:]-a'
-- which is not a real range
if Named_Class /= ANYOF_NONE then
Set_In_Class (Bitmap, '-');
else
In_Range := True;
end if;
else
Set_In_Class (Bitmap, Value);
end if;
-- Else in a character range
else
if Last_Value > Value then
Fail ("Invalid Range [" & Last_Value'Img
& "-" & Value'Img & "]");
end if;
while Last_Value <= Value loop
Set_In_Class (Bitmap, Last_Value);
Last_Value := Character'Succ (Last_Value);
end loop;
In_Range := False;
end if;
end loop;
-- Optimize case-insensitive ranges (put the upper case or lower
-- case character into the bitmap)
if (Flags and Case_Insensitive) /= 0 then
for C in Character'Range loop
if Get_From_Class (Bitmap, C) then
Set_In_Class (Bitmap, To_Lower (C));
Set_In_Class (Bitmap, To_Upper (C));
end if;
end loop;
end if;
-- Optimize inverted classes
if Invert then
for J in Bitmap'Range loop
Bitmap (J) := not Bitmap (J);
end loop;
end if;
Parse_Pos := Parse_Pos + 1;
-- Emit the class
IP := Emit_Node (ANYOF);
Emit_Class (Bitmap);
end Parse_Character_Class;
-------------------
-- Parse_Literal --
-------------------
-- This is a bit tricky due to quoted chars and due to
-- the multiplier characters '*', '+', and '?' that
-- take the SINGLE char previous as their operand.
--
-- On entry, the character at Parse_Pos - 1 is going to go
-- into the string, no matter what it is. It could be
-- following a \ if Parse_Atom was entered from the '\' case.
--
-- Basic idea is to pick up a good char in C and examine
-- the next char. If Is_Mult (C) then twiddle, if it's a \
-- then frozzle and if it's another magic char then push C and
-- terminate the string. If none of the above, push C on the
-- string and go around again.
--
-- Start_Pos is used to remember where "the current character"
-- starts in the string, if due to an Is_Mult we need to back
-- up and put the current char in a separate 1-character string.
-- When Start_Pos is 0, C is the only char in the string;
-- this is used in Is_Mult handling, and in setting the SIMPLE
-- flag at the end.
procedure Parse_Literal
(Expr_Flags : in out Expression_Flags;
IP : out Pointer)
is
Start_Pos : Natural := 0;
C : Character;
Length_Ptr : Pointer;
Has_Special_Operator : Boolean := False;
begin
Parse_Pos := Parse_Pos - 1; -- Look at current character
if (Flags and Case_Insensitive) /= 0 then
IP := Emit_Node (EXACTF);
else
IP := Emit_Node (EXACT);
end if;
Length_Ptr := Emit_Ptr;
Emit_Ptr := String_Operand (IP);
Parse_Loop :
loop
C := Expression (Parse_Pos); -- Get current character
case C is
when '.' | '[' | '(' | ')' | '|' | ASCII.LF | '$' | '^' =>
if Start_Pos = 0 then
Start_Pos := Parse_Pos;
Emit (C); -- First character is always emitted
else
exit Parse_Loop; -- Else we are done
end if;
when '?' | '+' | '*' | '{' =>
if Start_Pos = 0 then
Start_Pos := Parse_Pos;
Emit (C); -- First character is always emitted
-- Are we looking at an operator, or is this
-- simply a normal character ?
elsif not Is_Mult (Parse_Pos) then
Start_Pos := Parse_Pos;
Case_Emit (C);
else
-- We've got something like "abc?d". Mark this as a
-- special case. What we want to emit is a first
-- constant string for "ab", then one for "c" that will
-- ultimately be transformed with a CURLY operator, A
-- special case has to be handled for "a?", since there
-- is no initial string to emit.
Has_Special_Operator := True;
exit Parse_Loop;
end if;
when '\' =>
Start_Pos := Parse_Pos;
if Parse_Pos = Parse_End then
Fail ("Trailing \");
else
case Expression (Parse_Pos + 1) is
when 'b' | 'B' | 's' | 'S' | 'd' | 'D'
| 'w' | 'W' | '0' .. '9' | 'G' | 'A'
=> exit Parse_Loop;
when 'n' => Emit (ASCII.LF);
when 't' => Emit (ASCII.HT);
when 'r' => Emit (ASCII.CR);
when 'f' => Emit (ASCII.FF);
when 'e' => Emit (ASCII.ESC);
when 'a' => Emit (ASCII.BEL);
when others => Emit (Expression (Parse_Pos + 1));
end case;
Parse_Pos := Parse_Pos + 1;
end if;
when others =>
Start_Pos := Parse_Pos;
Case_Emit (C);
end case;
exit Parse_Loop when Emit_Ptr - Length_Ptr = 254;
Parse_Pos := Parse_Pos + 1;
exit Parse_Loop when Parse_Pos > Parse_End;
end loop Parse_Loop;
-- Is the string followed by a '*+?{' operator ? If yes, and if there
-- is an initial string to emit, do it now.
if Has_Special_Operator
and then Emit_Ptr >= Length_Ptr + 3
then
Emit_Ptr := Emit_Ptr - 1;
Parse_Pos := Start_Pos;
end if;
if Emit_Code then
Program (Length_Ptr) := Character'Val (Emit_Ptr - Length_Ptr - 2);
end if;
Expr_Flags.Has_Width := True;
-- Slight optimization when there is a single character
if Emit_Ptr = Length_Ptr + 2 then
Expr_Flags.Simple := True;
end if;
end Parse_Literal;
-----------------
-- Parse_Piece --
-----------------
-- Note that the branching code sequences used for '?' and the
-- general cases of '*' and + are somewhat optimized: they use
-- the same NOTHING node as both the endmarker for their branch
-- list and the body of the last branch. It might seem that
-- this node could be dispensed with entirely, but the endmarker
-- role is not redundant.
procedure Parse_Piece
(Expr_Flags : in out Expression_Flags;
IP : out Pointer)
is
Op : Character;
New_Flags : Expression_Flags;
Greedy : Boolean := True;
begin
Parse_Atom (New_Flags, IP);
if IP = 0 then
return;
end if;
if Parse_Pos > Parse_End
or else not Is_Mult (Parse_Pos)
then
Expr_Flags := New_Flags;
return;
end if;
Op := Expression (Parse_Pos);
if Op /= '+' then
Expr_Flags := (SP_Start => True, others => False);
else
Expr_Flags := (Has_Width => True, others => False);
end if;
-- Detect non greedy operators in the easy cases
if Op /= '{'
and then Parse_Pos + 1 <= Parse_End
and then Expression (Parse_Pos + 1) = '?'
then
Greedy := False;
Parse_Pos := Parse_Pos + 1;
end if;
-- Generate the byte code
case Op is
when '*' =>
if New_Flags.Simple then
Insert_Operator (STAR, IP, Greedy);
else
Link_Tail (IP, Emit_Node (WHILEM));
Insert_Curly_Operator
(CURLYX, 0, Max_Curly_Repeat, IP, Greedy);
Link_Tail (IP, Emit_Node (NOTHING));
end if;
when '+' =>
if New_Flags.Simple then
Insert_Operator (PLUS, IP, Greedy);
else
Link_Tail (IP, Emit_Node (WHILEM));
Insert_Curly_Operator
(CURLYX, 1, Max_Curly_Repeat, IP, Greedy);
Link_Tail (IP, Emit_Node (NOTHING));
end if;
when '?' =>
if New_Flags.Simple then
Insert_Curly_Operator (CURLY, 0, 1, IP, Greedy);
else
Link_Tail (IP, Emit_Node (WHILEM));
Insert_Curly_Operator (CURLYX, 0, 1, IP, Greedy);
Link_Tail (IP, Emit_Node (NOTHING));
end if;
when '{' =>
declare
Min, Max : Natural;
begin
Get_Curly_Arguments (Parse_Pos, Min, Max, Greedy);
if New_Flags.Simple then
Insert_Curly_Operator (CURLY, Min, Max, IP, Greedy);
else
Link_Tail (IP, Emit_Node (WHILEM));
Insert_Curly_Operator (CURLYX, Min, Max, IP, Greedy);
Link_Tail (IP, Emit_Node (NOTHING));
end if;
end;
when others =>
null;
end case;
Parse_Pos := Parse_Pos + 1;
if Parse_Pos <= Parse_End
and then Is_Mult (Parse_Pos)
then
Fail ("nested *+{");
end if;
end Parse_Piece;
---------------------------------
-- Parse_Posix_Character_Class --
---------------------------------
function Parse_Posix_Character_Class return Std_Class is
Invert : Boolean := False;
Class : Std_Class := ANYOF_NONE;
E : String renames Expression;
begin
if Parse_Pos <= Parse_End
and then Expression (Parse_Pos) = ':'
then
Parse_Pos := Parse_Pos + 1;
-- Do we have something like: [[:^alpha:]]
if Parse_Pos <= Parse_End
and then Expression (Parse_Pos) = '^'
then
Invert := True;
Parse_Pos := Parse_Pos + 1;
end if;
-- All classes have 6 characters at least
-- ??? magid constant 6 should have a name!
if Parse_Pos + 6 <= Parse_End then
case Expression (Parse_Pos) is
when 'a' =>
if E (Parse_Pos .. Parse_Pos + 4) = "alnum:]" then
if Invert then
Class := ANYOF_NALNUMC;
else
Class := ANYOF_ALNUMC;
end if;
elsif E (Parse_Pos .. Parse_Pos + 6) = "alpha:]" then
if Invert then
Class := ANYOF_NALPHA;
else
Class := ANYOF_ALPHA;
end if;
elsif E (Parse_Pos .. Parse_Pos + 6) = "ascii:]" then
if Invert then
Class := ANYOF_NASCII;
else
Class := ANYOF_ASCII;
end if;
end if;
when 'c' =>
if E (Parse_Pos .. Parse_Pos + 6) = "cntrl:]" then
if Invert then
Class := ANYOF_NCNTRL;
else
Class := ANYOF_CNTRL;
end if;
end if;
when 'd' =>
if E (Parse_Pos .. Parse_Pos + 6) = "digit:]" then
if Invert then
Class := ANYOF_NDIGIT;
else
Class := ANYOF_DIGIT;
end if;
end if;
when 'g' =>
if E (Parse_Pos .. Parse_Pos + 6) = "graph:]" then
if Invert then
Class := ANYOF_NGRAPH;
else
Class := ANYOF_GRAPH;
end if;
end if;
when 'l' =>
if E (Parse_Pos .. Parse_Pos + 6) = "lower:]" then
if Invert then
Class := ANYOF_NLOWER;
else
Class := ANYOF_LOWER;
end if;
end if;
when 'p' =>
if E (Parse_Pos .. Parse_Pos + 6) = "print:]" then
if Invert then
Class := ANYOF_NPRINT;
else
Class := ANYOF_PRINT;
end if;
elsif E (Parse_Pos .. Parse_Pos + 6) = "punct:]" then
if Invert then
Class := ANYOF_NPUNCT;
else
Class := ANYOF_PUNCT;
end if;
end if;
when 's' =>
if E (Parse_Pos .. Parse_Pos + 6) = "space:]" then
if Invert then
Class := ANYOF_NSPACE;
else
Class := ANYOF_SPACE;
end if;
end if;
when 'u' =>
if E (Parse_Pos .. Parse_Pos + 6) = "upper:]" then
if Invert then
Class := ANYOF_NUPPER;
else
Class := ANYOF_UPPER;
end if;
end if;
when 'w' =>
if E (Parse_Pos .. Parse_Pos + 5) = "word:]" then
if Invert then
Class := ANYOF_NALNUM;
else
Class := ANYOF_ALNUM;
end if;
Parse_Pos := Parse_Pos - 1;
end if;
when 'x' =>
if Parse_Pos + 7 <= Parse_End
and then E (Parse_Pos .. Parse_Pos + 7) = "xdigit:]"
then
if Invert then
Class := ANYOF_NXDIGIT;
else
Class := ANYOF_XDIGIT;
end if;
Parse_Pos := Parse_Pos + 1;
end if;
when others =>
Class := ANYOF_NONE;
end case;
if Class /= ANYOF_NONE then
Parse_Pos := Parse_Pos + 7;
end if;
else
Fail ("Invalid character class");
end if;
else
return ANYOF_NONE;
end if;
return Class;
end Parse_Posix_Character_Class;
Expr_Flags : Expression_Flags;
Result : Pointer;
-- Start of processing for Compile
begin
Emit (MAGIC);
Parse (False, Expr_Flags, Result);
if Result = 0 then
Fail ("Couldn't compile expression");
end if;
Final_Code_Size := Emit_Ptr - 1;
-- Do we want to actually compile the expression, or simply get the
-- code size ???
if Emit_Code then
Optimize (PM);
end if;
PM.Flags := Flags;
end Compile;
function Compile
(Expression : String;
Flags : Regexp_Flags := No_Flags)
return Pattern_Matcher
is
Size : Program_Size;
Dummy : Pattern_Matcher (0);
begin
Compile (Dummy, Expression, Size, Flags);
declare
Result : Pattern_Matcher (Size);
begin
Compile (Result, Expression, Size, Flags);
return Result;
end;
end Compile;
procedure Compile
(Matcher : out Pattern_Matcher;
Expression : String;
Flags : Regexp_Flags := No_Flags)
is
Size : Program_Size;
begin
Compile (Matcher, Expression, Size, Flags);
end Compile;
----------
-- Dump --
----------
procedure Dump (Self : Pattern_Matcher) is
-- Index : Pointer := Program_First + 1;
-- What is the above line for ???
Op : Opcode;
Program : Program_Data renames Self.Program;
procedure Dump_Until
(Start : Pointer;
Till : Pointer;
Indent : Natural := 0);
-- Dump the program until the node Till (not included) is met.
-- Every line is indented with Index spaces at the beginning
-- Dumps till the end if Till is 0.
----------------
-- Dump_Until --
----------------
procedure Dump_Until
(Start : Pointer;
Till : Pointer;
Indent : Natural := 0)
is
Next : Pointer;
Index : Pointer := Start;
Local_Indent : Natural := Indent;
Length : Pointer;
begin
while Index < Till loop
Op := Opcode'Val (Character'Pos ((Self.Program (Index))));
if Op = CLOSE then
Local_Indent := Local_Indent - 3;
end if;
declare
Point : String := Pointer'Image (Index);
begin
for J in 1 .. 6 - Point'Length loop
Put (' ');
end loop;
Put (Point
& " : "
& (1 .. Local_Indent => ' ')
& Opcode'Image (Op));
end;
-- Print the parenthesis number
if Op = OPEN or else Op = CLOSE or else Op = REFF then
Put (Natural'Image (Character'Pos (Program (Index + 3))));
end if;
Next := Index + Get_Next_Offset (Program, Index);
if Next = Index then
Put (" (next at 0)");
else
Put (" (next at " & Pointer'Image (Next) & ")");
end if;
case Op is
-- Character class operand
when ANYOF => null;
declare
Bitmap : Character_Class;
Last : Character := ASCII.Nul;
Current : Natural := 0;
Current_Char : Character;
begin
Bitmap_Operand (Program, Index, Bitmap);
Put (" operand=");
while Current <= 255 loop
Current_Char := Character'Val (Current);
-- First item in a range
if Get_From_Class (Bitmap, Current_Char) then
Last := Current_Char;
-- Search for the last item in the range
loop
Current := Current + 1;
exit when Current > 255;
Current_Char := Character'Val (Current);
exit when
not Get_From_Class (Bitmap, Current_Char);
end loop;
if Last <= ' ' then
Put (Last'Img);
else
Put (Last);
end if;
if Character'Succ (Last) /= Current_Char then
Put ("-" & Character'Pred (Current_Char));
end if;
else
Current := Current + 1;
end if;
end loop;
New_Line;
Index := Index + 3 + Bitmap'Length;
end;
-- string operand
when EXACT | EXACTF =>
Length := String_Length (Program, Index);
Put (" operand (length:" & Program_Size'Image (Length + 1)
& ") ="
& String (Program (String_Operand (Index)
.. String_Operand (Index)
+ Length)));
Index := String_Operand (Index) + Length + 1;
New_Line;
-- Node operand
when BRANCH =>
New_Line;
Dump_Until (Index + 3, Next, Local_Indent + 3);
Index := Next;
when STAR | PLUS =>
New_Line;
-- Only one instruction
Dump_Until (Index + 3, Index + 4, Local_Indent + 3);
Index := Next;
when CURLY | CURLYX =>
Put (" {"
& Natural'Image (Read_Natural (Program, Index + 3))
& ","
& Natural'Image (Read_Natural (Program, Index + 5))
& "}");
New_Line;
Dump_Until (Index + 7, Next, Local_Indent + 3);
Index := Next;
when OPEN =>
New_Line;
Index := Index + 4;
Local_Indent := Local_Indent + 3;
when CLOSE | REFF =>
New_Line;
Index := Index + 4;
when EOP =>
Index := Index + 3;
New_Line;
exit;
-- No operand
when others =>
Index := Index + 3;
New_Line;
end case;
end loop;
end Dump_Until;
-- Start of processing for Dump
begin
pragma Assert (Self.Program (Program_First) = MAGIC,
"Corrupted Pattern_Matcher");
Put_Line ("Must start with (Self.First) = "
& Character'Image (Self.First));
if (Self.Flags and Case_Insensitive) /= 0 then
Put_Line (" Case_Insensitive mode");
end if;
if (Self.Flags and Single_Line) /= 0 then
Put_Line (" Single_Line mode");
end if;
if (Self.Flags and Multiple_Lines) /= 0 then
Put_Line (" Multiple_Lines mode");
end if;
Put_Line (" 1 : MAGIC");
Dump_Until (Program_First + 1, Self.Program'Last + 1);
end Dump;
--------------------
-- Get_From_Class --
--------------------
function Get_From_Class
(Bitmap : Character_Class;
C : Character)
return Boolean
is
Value : constant Class_Byte := Character'Pos (C);
begin
return (Bitmap (Value / 8)
and Bit_Conversion (Value mod 8)) /= 0;
end Get_From_Class;
--------------
-- Get_Next --
--------------
function Get_Next (Program : Program_Data; IP : Pointer) return Pointer is
Offset : constant Pointer := Get_Next_Offset (Program, IP);
begin
if Offset = 0 then
return 0;
else
return IP + Offset;
end if;
end Get_Next;
---------------------
-- Get_Next_Offset --
---------------------
function Get_Next_Offset
(Program : Program_Data;
IP : Pointer)
return Pointer
is
begin
return Pointer (Read_Natural (Program, IP + 1));
end Get_Next_Offset;
--------------
-- Is_Alnum --
--------------
function Is_Alnum (C : Character) return Boolean is
begin
return Is_Alphanumeric (C) or else C = '_';
end Is_Alnum;
------------------
-- Is_Printable --
------------------
function Is_Printable (C : Character) return Boolean is
Value : constant Natural := Character'Pos (C);
begin
return (Value > 32 and then Value < 127)
or else Is_Space (C);
end Is_Printable;
--------------
-- Is_Space --
--------------
function Is_Space (C : Character) return Boolean is
begin
return C = ' '
or else C = ASCII.HT
or else C = ASCII.CR
or else C = ASCII.LF
or else C = ASCII.VT
or else C = ASCII.FF;
end Is_Space;
-----------
-- Match --
-----------
procedure Match
(Self : Pattern_Matcher;
Data : String;
Matches : out Match_Array)
is
Program : Program_Data renames Self.Program; -- Shorter notation
-- Global work variables
Input_Pos : Natural; -- String-input pointer
BOL_Pos : Natural; -- Beginning of input, for ^ check
Matched : Boolean := False; -- Until proven True
Matches_Full : Match_Array (0 .. Natural'Max (Self.Paren_Count,
Matches'Last));
-- Stores the value of all the parenthesis pairs.
-- We do not use directly Matches, so that we can also use back
-- references (REFF) even if Matches is too small.
type Natural_Array is array (Match_Count range <>) of Natural;
Matches_Tmp : Natural_Array (Matches_Full'Range);
-- Save the opening position of parenthesis.
Last_Paren : Natural := 0;
-- Last parenthesis seen
Greedy : Boolean := True;
-- True if the next operator should be greedy
type Current_Curly_Record;
type Current_Curly_Access is access all Current_Curly_Record;
type Current_Curly_Record is record
Paren_Floor : Natural; -- How far back to strip parenthesis data
Cur : Integer; -- How many instances of scan we've matched
Min : Natural; -- Minimal number of scans to match
Max : Natural; -- Maximal number of scans to match
Greedy : Boolean; -- Whether to work our way up or down
Scan : Pointer; -- The thing to match
Next : Pointer; -- What has to match after it
Lastloc : Natural; -- Where we started matching this scan
Old_Cc : Current_Curly_Access; -- Before we started this one
end record;
-- Data used to handle the curly operator and the plus and star
-- operators for complex expressions.
Current_Curly : Current_Curly_Access := null;
-- The curly currently being processed.
-----------------------
-- Local Subprograms --
-----------------------
function Index (Start : Positive; C : Character) return Natural;
-- Find character C in Data starting at Start and return position
function Repeat
(IP : Pointer;
Max : Natural := Natural'Last)
return Natural;
-- Repeatedly match something simple, report how many
-- It only matches on things of length 1.
-- Starting from Input_Pos, it matches at most Max CURLY.
function Try (Pos : in Positive) return Boolean;
-- Try to match at specific point
function Match (IP : Pointer) return Boolean;
-- This is the main matching routine. Conceptually the strategy
-- is simple: check to see whether the current node matches,
-- call self recursively to see whether the rest matches,
-- and then act accordingly.
--
-- In practice Match makes some effort to avoid recursion, in
-- particular by going through "ordinary" nodes (that don't
-- need to know whether the rest of the match failed) by
-- using a loop instead of recursion.
function Match_Whilem (IP : Pointer) return Boolean;
-- Return True if a WHILEM matches
function Recurse_Match (IP : Pointer; From : Natural) return Boolean;
pragma Inline (Recurse_Match);
-- Calls Match recursively. It saves and restores the parenthesis
-- status and location in the input stream correctly, so that
-- backtracking is possible
function Match_Simple_Operator
(Op : Opcode;
Scan : Pointer;
Next : Pointer;
Greedy : Boolean)
return Boolean;
-- Return True it the simple operator (possibly non-greedy) matches
pragma Inline (Index);
pragma Inline (Repeat);
-- These are two complex functions, but used only once.
pragma Inline (Match_Whilem);
pragma Inline (Match_Simple_Operator);
-----------
-- Index --
-----------
function Index
(Start : Positive;
C : Character)
return Natural
is
begin
for J in Start .. Data'Last loop
if Data (J) = C then
return J;
end if;
end loop;
return 0;
end Index;
-------------------
-- Recurse_Match --
-------------------
function Recurse_Match (IP : Pointer; From : Natural) return Boolean is
L : constant Natural := Last_Paren;
Tmp_F : constant Match_Array :=
Matches_Full (From + 1 .. Matches_Full'Last);
Start : constant Natural_Array :=
Matches_Tmp (From + 1 .. Matches_Tmp'Last);
Input : constant Natural := Input_Pos;
begin
if Match (IP) then
return True;
end if;
Last_Paren := L;
Matches_Full (Tmp_F'Range) := Tmp_F;
Matches_Tmp (Start'Range) := Start;
Input_Pos := Input;
return False;
end Recurse_Match;
-----------
-- Match --
-----------
function Match (IP : Pointer) return Boolean is
Scan : Pointer := IP;
Next : Pointer;
Op : Opcode;
begin
State_Machine :
loop
pragma Assert (Scan /= 0);
-- Determine current opcode and count its usage in debug mode
Op := Opcode'Val (Character'Pos (Program (Scan)));
-- Calculate offset of next instruction.
-- Second character is most significant in Program_Data.
Next := Get_Next (Program, Scan);
case Op is
when EOP =>
return True; -- Success !
when BRANCH =>
if Program (Next) /= BRANCH then
Next := Operand (Scan); -- No choice, avoid recursion
else
loop
if Recurse_Match (Operand (Scan), 0) then
return True;
end if;
Scan := Get_Next (Program, Scan);
exit when Scan = 0 or Program (Scan) /= BRANCH;
end loop;
exit State_Machine;
end if;
when NOTHING =>
null;
when BOL =>
exit State_Machine when
Input_Pos /= BOL_Pos
and then ((Self.Flags and Multiple_Lines) = 0
or else Data (Input_Pos - 1) /= ASCII.LF);
when MBOL =>
exit State_Machine when
Input_Pos /= BOL_Pos
and then Data (Input_Pos - 1) /= ASCII.LF;
when SBOL =>
exit State_Machine when Input_Pos /= BOL_Pos;
when EOL =>
exit State_Machine when
Input_Pos <= Data'Last
and then ((Self.Flags and Multiple_Lines) = 0
or else Data (Input_Pos) /= ASCII.LF);
when MEOL =>
exit State_Machine when
Input_Pos <= Data'Last
and then Data (Input_Pos) /= ASCII.LF;
when SEOL =>
exit State_Machine when Input_Pos <= Data'Last;
when BOUND | NBOUND =>
-- Was last char in word ?
declare
N : Boolean := False;
Ln : Boolean := False;
begin
if Input_Pos /= Data'First then
N := Is_Alnum (Data (Input_Pos - 1));
end if;
if Input_Pos > Data'Last then
Ln := False;
else
Ln := Is_Alnum (Data (Input_Pos));
end if;
if Op = BOUND then
if N = Ln then
exit State_Machine;
end if;
else
if N /= Ln then
exit State_Machine;
end if;
end if;
end;
when SPACE =>
exit State_Machine when
Input_Pos > Data'Last
or else not Is_Space (Data (Input_Pos));
Input_Pos := Input_Pos + 1;
when NSPACE =>
exit State_Machine when
Input_Pos > Data'Last
or else Is_Space (Data (Input_Pos));
Input_Pos := Input_Pos + 1;
when DIGIT =>
exit State_Machine when
Input_Pos > Data'Last
or else not Is_Digit (Data (Input_Pos));
Input_Pos := Input_Pos + 1;
when NDIGIT =>
exit State_Machine when
Input_Pos > Data'Last
or else Is_Digit (Data (Input_Pos));
Input_Pos := Input_Pos + 1;
when ALNUM =>
exit State_Machine when
Input_Pos > Data'Last
or else not Is_Alnum (Data (Input_Pos));
Input_Pos := Input_Pos + 1;
when NALNUM =>
exit State_Machine when
Input_Pos > Data'Last
or else Is_Alnum (Data (Input_Pos));
Input_Pos := Input_Pos + 1;
when ANY =>
exit State_Machine when Input_Pos > Data'Last
or else Data (Input_Pos) = ASCII.LF;
Input_Pos := Input_Pos + 1;
when SANY =>
exit State_Machine when Input_Pos > Data'Last;
Input_Pos := Input_Pos + 1;
when EXACT =>
declare
Opnd : Pointer := String_Operand (Scan);
Current : Positive := Input_Pos;
Last : constant Pointer :=
Opnd + String_Length (Program, Scan);
begin
while Opnd <= Last loop
exit State_Machine when Current > Data'Last
or else Program (Opnd) /= Data (Current);
Current := Current + 1;
Opnd := Opnd + 1;
end loop;
Input_Pos := Current;
end;
when EXACTF =>
declare
Opnd : Pointer := String_Operand (Scan);
Current : Positive := Input_Pos;
Last : constant Pointer :=
Opnd + String_Length (Program, Scan);
begin
while Opnd <= Last loop
exit State_Machine when Current > Data'Last
or else Program (Opnd) /= To_Lower (Data (Current));
Current := Current + 1;
Opnd := Opnd + 1;
end loop;
Input_Pos := Current;
end;
when ANYOF =>
declare
Bitmap : Character_Class;
begin
Bitmap_Operand (Program, Scan, Bitmap);
exit State_Machine when
Input_Pos > Data'Last
or else not Get_From_Class (Bitmap, Data (Input_Pos));
Input_Pos := Input_Pos + 1;
end;
when OPEN =>
declare
No : constant Natural :=
Character'Pos (Program (Operand (Scan)));
begin
Matches_Tmp (No) := Input_Pos;
end;
when CLOSE =>
declare
No : constant Natural :=
Character'Pos (Program (Operand (Scan)));
begin
Matches_Full (No) := (Matches_Tmp (No), Input_Pos - 1);
if Last_Paren < No then
Last_Paren := No;
end if;
end;
when REFF =>
declare
No : constant Natural :=
Character'Pos (Program (Operand (Scan)));
Data_Pos : Natural;
begin
-- If we haven't seen that parenthesis yet
if Last_Paren < No then
return False;
end if;
Data_Pos := Matches_Full (No).First;
while Data_Pos <= Matches_Full (No).Last loop
if Input_Pos > Data'Last
or else Data (Input_Pos) /= Data (Data_Pos)
then
return False;
end if;
Input_Pos := Input_Pos + 1;
Data_Pos := Data_Pos + 1;
end loop;
end;
when MINMOD =>
Greedy := False;
when STAR | PLUS | CURLY =>
declare
Greed : constant Boolean := Greedy;
begin
Greedy := True;
return Match_Simple_Operator (Op, Scan, Next, Greed);
end;
when CURLYX =>
-- Looking at something like:
-- 1: CURLYX {n,m} (->4)
-- 2: code for complex thing (->3)
-- 3: WHILEM (->0)
-- 4: NOTHING
declare
Cc : aliased Current_Curly_Record;
Min : Natural := Read_Natural (Program, Scan + 3);
Max : Natural := Read_Natural (Program, Scan + 5);
Has_Match : Boolean;
begin
Cc := (Paren_Floor => Last_Paren,
Cur => -1,
Min => Min,
Max => Max,
Greedy => Greedy,
Scan => Scan + 7,
Next => Next,
Lastloc => 0,
Old_Cc => Current_Curly);
Current_Curly := Cc'Unchecked_Access;
Has_Match := Match (Next - 3);
-- Start on the WHILEM
Current_Curly := Cc.Old_Cc;
return Has_Match;
end;
when WHILEM =>
return Match_Whilem (IP);
when others =>
raise Expression_Error; -- Invalid instruction
end case;
Scan := Next;
end loop State_Machine;
-- If we get here, there is no match.
-- For successful matches when EOP is the terminating point.
return False;
end Match;
---------------------------
-- Match_Simple_Operator --
---------------------------
function Match_Simple_Operator
(Op : Opcode;
Scan : Pointer;
Next : Pointer;
Greedy : Boolean)
return Boolean
is
Next_Char : Character := ASCII.Nul;
Next_Char_Known : Boolean := False;
No : Integer; -- Can be negative
Min : Natural;
Max : Natural := Natural'Last;
Operand_Code : Pointer;
Old : Natural;
Last_Pos : Natural;
Save : Natural := Input_Pos;
begin
-- Lookahead to avoid useless match attempts
-- when we know what character comes next.
if Program (Next) = EXACT then
Next_Char := Program (String_Operand (Next));
Next_Char_Known := True;
end if;
-- Find the minimal and maximal values for the operator
case Op is
when STAR =>
Min := 0;
Operand_Code := Operand (Scan);
when PLUS =>
Min := 1;
Operand_Code := Operand (Scan);
when others =>
Min := Read_Natural (Program, Scan + 3);
Max := Read_Natural (Program, Scan + 5);
Operand_Code := Scan + 7;
end case;
-- Non greedy operators
if not Greedy then
-- Test the minimal repetitions
if Min /= 0
and then Repeat (Operand_Code, Min) < Min
then
return False;
end if;
Old := Input_Pos;
-- Find the place where 'next' could work
if Next_Char_Known then
-- Last position to check
Last_Pos := Input_Pos + Max;
if Last_Pos > Data'Last
or else Max = Natural'Last
then
Last_Pos := Data'Last;
end if;
-- Look for the first possible opportunity
loop
-- Find the next possible position
while Input_Pos <= Last_Pos
and then Data (Input_Pos) /= Next_Char
loop
Input_Pos := Input_Pos + 1;
end loop;
if Input_Pos > Last_Pos then
return False;
end if;
-- Check that we still match if we stop
-- at the position we just found.
declare
Num : constant Natural := Input_Pos - Old;
begin
Input_Pos := Old;
if Repeat (Operand_Code, Num) < Num then
return False;
end if;
end;
-- Input_Pos now points to the new position
if Match (Get_Next (Program, Scan)) then
return True;
end if;
Old := Input_Pos;
Input_Pos := Input_Pos + 1;
end loop;
-- We know what the next character is
else
while Max >= Min loop
-- If the next character matches
if Match (Next) then
return True;
end if;
Input_Pos := Save + Min;
-- Could not or did not match -- move forward
if Repeat (Operand_Code, 1) /= 0 then
Min := Min + 1;
else
return False;
end if;
end loop;
end if;
return False;
-- Greedy operators
else
No := Repeat (Operand_Code, Max);
-- ??? Perl has some special code here in case the
-- next instruction is of type EOL, since $ and \Z
-- can match before *and* after newline at the end.
-- ??? Perl has some special code here in case (paren)
-- is True.
-- Else, if we don't have any parenthesis
while No >= Min loop
if not Next_Char_Known
or else (Input_Pos <= Data'Last
and then Data (Input_Pos) = Next_Char)
then
if Match (Next) then
return True;
end if;
end if;
-- Could not or did not work, we back up
No := No - 1;
Input_Pos := Save + No;
end loop;
return False;
end if;
end Match_Simple_Operator;
------------------
-- Match_Whilem --
------------------
-- This is really hard to understand, because after we match what we're
-- trying to match, we must make sure the rest of the REx is going to
-- match for sure, and to do that we have to go back UP the parse tree
-- by recursing ever deeper. And if it fails, we have to reset our
-- parent's current state that we can try again after backing off.
function Match_Whilem (IP : Pointer) return Boolean is
pragma Warnings (Off, IP);
Cc : Current_Curly_Access := Current_Curly;
N : Natural := Cc.Cur + 1;
Ln : Natural := 0;
Lastloc : Natural := Cc.Lastloc;
-- Detection of 0-len.
begin
-- If degenerate scan matches "", assume scan done.
if Input_Pos = Cc.Lastloc
and then N >= Cc.Min
then
-- Temporarily restore the old context, and check that we
-- match was comes after CURLYX.
Current_Curly := Cc.Old_Cc;
if Current_Curly /= null then
Ln := Current_Curly.Cur;
end if;
if Match (Cc.Next) then
return True;
end if;
if Current_Curly /= null then
Current_Curly.Cur := Ln;
end if;
Current_Curly := Cc;
return False;
end if;
-- First, just match a string of min scans.
if N < Cc.Min then
Cc.Cur := N;
Cc.Lastloc := Input_Pos;
if Match (Cc.Scan) then
return True;
end if;
Cc.Cur := N - 1;
Cc.Lastloc := Lastloc;
return False;
end if;
-- Prefer next over scan for minimal matching.
if not Cc.Greedy then
Current_Curly := Cc.Old_Cc;
if Current_Curly /= null then
Ln := Current_Curly.Cur;
end if;
if Recurse_Match (Cc.Next, Cc.Paren_Floor) then
return True;
end if;
if Current_Curly /= null then
Current_Curly.Cur := Ln;
end if;
Current_Curly := Cc;
-- Maximum greed exceeded ?
if N >= Cc.Max then
return False;
end if;
-- Try scanning more and see if it helps
Cc.Cur := N;
Cc.Lastloc := Input_Pos;
if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
return True;
end if;
Cc.Cur := N - 1;
Cc.Lastloc := Lastloc;
return False;
end if;
-- Prefer scan over next for maximal matching
if N < Cc.Max then -- more greed allowed ?
Cc.Cur := N;
Cc.Lastloc := Input_Pos;
if Recurse_Match (Cc.Scan, Cc.Paren_Floor) then
return True;
end if;
end if;
-- Failed deeper matches of scan, so see if this one works
Current_Curly := Cc.Old_Cc;
if Current_Curly /= null then
Ln := Current_Curly.Cur;
end if;
if Match (Cc.Next) then
return True;
end if;
if Current_Curly /= null then
Current_Curly.Cur := Ln;
end if;
Current_Curly := Cc;
Cc.Cur := N - 1;
Cc.Lastloc := Lastloc;
return False;
end Match_Whilem;
------------
-- Repeat --
------------
function Repeat
(IP : Pointer;
Max : Natural := Natural'Last)
return Natural
is
Scan : Natural := Input_Pos;
Last : Natural;
Op : constant Opcode := Opcode'Val (Character'Pos (Program (IP)));
Count : Natural;
C : Character;
Is_First : Boolean := True;
Bitmap : Character_Class;
begin
if Max = Natural'Last or else Scan + Max - 1 > Data'Last then
Last := Data'Last;
else
Last := Scan + Max - 1;
end if;
case Op is
when ANY =>
while Scan <= Last
and then Data (Scan) /= ASCII.LF
loop
Scan := Scan + 1;
end loop;
when SANY =>
Scan := Last + 1;
when EXACT =>
-- The string has only one character if Repeat was called
C := Program (String_Operand (IP));
while Scan <= Last
and then C = Data (Scan)
loop
Scan := Scan + 1;
end loop;
when EXACTF =>
-- The string has only one character if Repeat was called
C := Program (String_Operand (IP));
while Scan <= Last
and then To_Lower (C) = Data (Scan)
loop
Scan := Scan + 1;
end loop;
when ANYOF =>
if Is_First then
Bitmap_Operand (Program, IP, Bitmap);
Is_First := False;
end if;
while Scan <= Last
and then Get_From_Class (Bitmap, Data (Scan))
loop
Scan := Scan + 1;
end loop;
when ALNUM =>
while Scan <= Last
and then Is_Alnum (Data (Scan))
loop
Scan := Scan + 1;
end loop;
when NALNUM =>
while Scan <= Last
and then not Is_Alnum (Data (Scan))
loop
Scan := Scan + 1;
end loop;
when SPACE =>
while Scan <= Last
and then Is_Space (Data (Scan))
loop
Scan := Scan + 1;
end loop;
when NSPACE =>
while Scan <= Last
and then not Is_Space (Data (Scan))
loop
Scan := Scan + 1;
end loop;
when DIGIT =>
while Scan <= Last
and then Is_Digit (Data (Scan))
loop
Scan := Scan + 1;
end loop;
when NDIGIT =>
while Scan <= Last
and then not Is_Digit (Data (Scan))
loop
Scan := Scan + 1;
end loop;
when others =>
raise Program_Error;
end case;
Count := Scan - Input_Pos;
Input_Pos := Scan;
return Count;
end Repeat;
---------
-- Try --
---------
function Try (Pos : in Positive) return Boolean is
begin
Input_Pos := Pos;
Last_Paren := 0;
Matches_Full := (others => No_Match);
if Match (Program_First + 1) then
Matches_Full (0) := (Pos, Input_Pos - 1);
return True;
end if;
return False;
end Try;
-- Start of processing for Match
begin
-- Do we have the regexp Never_Match?
if Self.Size = 0 then
Matches (0) := No_Match;
return;
end if;
-- Check validity of program
pragma Assert
(Program (Program_First) = MAGIC,
"Corrupted Pattern_Matcher");
-- If there is a "must appear" string, look for it
if Self.Must_Have_Length > 0 then
declare
First : constant Character := Program (Self.Must_Have);
Must_First : constant Pointer := Self.Must_Have;
Must_Last : constant Pointer :=
Must_First + Pointer (Self.Must_Have_Length - 1);
Next_Try : Natural := Index (Data'First, First);
begin
while Next_Try /= 0
and then Data (Next_Try .. Next_Try + Self.Must_Have_Length - 1)
= String (Program (Must_First .. Must_Last))
loop
Next_Try := Index (Next_Try + 1, First);
end loop;
if Next_Try = 0 then
Matches_Full := (others => No_Match);
return; -- Not present
end if;
end;
end if;
-- Mark beginning of line for ^
BOL_Pos := Data'First;
-- Simplest case first: an anchored match need be tried only once
if Self.Anchored and then (Self.Flags and Multiple_Lines) = 0 then
Matched := Try (Data'First);
elsif Self.Anchored then
declare
Next_Try : Natural := Data'First;
begin
-- Test the first position in the buffer
Matched := Try (Next_Try);
-- Else only test after newlines
if not Matched then
while Next_Try <= Data'Last loop
while Next_Try <= Data'Last
and then Data (Next_Try) /= ASCII.LF
loop
Next_Try := Next_Try + 1;
end loop;
Next_Try := Next_Try + 1;
if Next_Try <= Data'Last then
Matched := Try (Next_Try);
exit when Matched;
end if;
end loop;
end if;
end;
elsif Self.First /= ASCII.NUL then
-- We know what char it must start with
declare
Next_Try : Natural := Index (Data'First, Self.First);
begin
while Next_Try /= 0 loop
Matched := Try (Next_Try);
exit when Matched;
Next_Try := Index (Next_Try + 1, Self.First);
end loop;
end;
else
-- Messy cases: try all locations (including for the empty string)
Matched := Try (Data'First);
if not Matched then
for S in Data'First + 1 .. Data'Last loop
Matched := Try (S);
exit when Matched;
end loop;
end if;
end if;
-- Matched has its value
for J in Last_Paren + 1 .. Matches'Last loop
Matches_Full (J) := No_Match;
end loop;
Matches := Matches_Full (Matches'Range);
return;
end Match;
function Match
(Self : Pattern_Matcher;
Data : String)
return Natural
is
Matches : Match_Array (0 .. 0);
begin
Match (Self, Data, Matches);
if Matches (0) = No_Match then
return Data'First - 1;
else
return Matches (0).First;
end if;
end Match;
procedure Match
(Expression : String;
Data : String;
Matches : out Match_Array;
Size : Program_Size := 0)
is
PM : Pattern_Matcher (Size);
Finalize_Size : Program_Size;
begin
if Size = 0 then
Match (Compile (Expression), Data, Matches);
else
Compile (PM, Expression, Finalize_Size);
Match (PM, Data, Matches);
end if;
end Match;
function Match
(Expression : String;
Data : String;
Size : Program_Size := 0)
return Natural
is
PM : Pattern_Matcher (Size);
Final_Size : Program_Size; -- unused
begin
if Size = 0 then
return Match (Compile (Expression), Data);
else
Compile (PM, Expression, Final_Size);
return Match (PM, Data);
end if;
end Match;
function Match
(Expression : String;
Data : String;
Size : Program_Size := 0)
return Boolean
is
Matches : Match_Array (0 .. 0);
PM : Pattern_Matcher (Size);
Final_Size : Program_Size; -- unused
begin
if Size = 0 then
Match (Compile (Expression), Data, Matches);
else
Compile (PM, Expression, Final_Size);
Match (PM, Data, Matches);
end if;
return Matches (0).First >= Data'First;
end Match;
-------------
-- Operand --
-------------
function Operand (P : Pointer) return Pointer is
begin
return P + 3;
end Operand;
--------------
-- Optimize --
--------------
procedure Optimize (Self : in out Pattern_Matcher) is
Max_Length : Program_Size;
This_Length : Program_Size;
Longest : Pointer;
Scan : Pointer;
Program : Program_Data renames Self.Program;
begin
-- Start with safe defaults (no optimization):
-- * No known first character of match
-- * Does not necessarily start at beginning of line
-- * No string known that has to appear in data
Self.First := ASCII.NUL;
Self.Anchored := False;
Self.Must_Have := Program'Last + 1;
Self.Must_Have_Length := 0;
Scan := Program_First + 1; -- First instruction (can be anything)
if Program (Scan) = EXACT then
Self.First := Program (String_Operand (Scan));
elsif Program (Scan) = BOL
or else Program (Scan) = SBOL
or else Program (Scan) = MBOL
then
Self.Anchored := True;
end if;
-- If there's something expensive in the regexp, find the
-- longest literal string that must appear and make it the
-- regmust. Resolve ties in favor of later strings, since
-- the regstart check works with the beginning of the regexp.
-- and avoiding duplication strengthens checking. Not a
-- strong reason, but sufficient in the absence of others.
if False then -- if Flags.SP_Start then ???
Longest := 0;
Max_Length := 0;
while Scan /= 0 loop
if Program (Scan) = EXACT or else Program (Scan) = EXACTF then
This_Length := String_Length (Program, Scan);
if This_Length >= Max_Length then
Longest := String_Operand (Scan);
Max_Length := This_Length;
end if;
end if;
Scan := Get_Next (Program, Scan);
end loop;
Self.Must_Have := Longest;
Self.Must_Have_Length := Natural (Max_Length) + 1;
end if;
end Optimize;
-----------------
-- Paren_Count --
-----------------
function Paren_Count (Regexp : Pattern_Matcher) return Match_Count is
begin
return Regexp.Paren_Count;
end Paren_Count;
-----------
-- Quote --
-----------
function Quote (Str : String) return String is
S : String (1 .. Str'Length * 2);
Last : Natural := 0;
begin
for J in Str'Range loop
case Str (J) is
when '^' | '$' | '|' | '*' | '+' | '?' | '{'
| '}' | '[' | ']' | '(' | ')' | '\' =>
S (Last + 1) := '\';
S (Last + 2) := Str (J);
Last := Last + 2;
when others =>
S (Last + 1) := Str (J);
Last := Last + 1;
end case;
end loop;
return S (1 .. Last);
end Quote;
------------------
-- Read_Natural --
------------------
function Read_Natural
(Program : Program_Data;
IP : Pointer)
return Natural
is
begin
return Character'Pos (Program (IP)) +
256 * Character'Pos (Program (IP + 1));
end Read_Natural;
-----------------
-- Reset_Class --
-----------------
procedure Reset_Class (Bitmap : in out Character_Class) is
begin
Bitmap := (others => 0);
end Reset_Class;
------------------
-- Set_In_Class --
------------------
procedure Set_In_Class
(Bitmap : in out Character_Class;
C : Character)
is
Value : constant Class_Byte := Character'Pos (C);
begin
Bitmap (Value / 8) := Bitmap (Value / 8)
or Bit_Conversion (Value mod 8);
end Set_In_Class;
-------------------
-- String_Length --
-------------------
function String_Length
(Program : Program_Data;
P : Pointer)
return Program_Size
is
begin
pragma Assert (Program (P) = EXACT or else Program (P) = EXACTF);
return Character'Pos (Program (P + 3));
end String_Length;
--------------------
-- String_Operand --
--------------------
function String_Operand (P : Pointer) return Pointer is
begin
return P + 4;
end String_Operand;
end GNAT.Regpat;
|