summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/l3kernel/l3regex.dtx
blob: 99407b273fb81320b1459a0376917b0a182251b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
% \iffalse meta-comment
%
%% File: l3regex.dtx
%
% Copyright (C) 2011-2021 The LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "l3kernel bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver>
\documentclass[full,kernel]{l3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \textsf{l3regex} package: Regular expressions in \TeX{}^^A
% }
%
% \author{^^A
%  The \LaTeX{} Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
%
% \date{Released 2021-10-12}
%
% \maketitle
%
% \begin{documentation}
% \newenvironment{l3regex-syntax}
%   {\begin{itemize}\def\\{\char`\\}\def\makelabel##1{\hss\llap{\ttfamily##1}}}
%   {\end{itemize}}
%
% The \pkg{l3regex} package provides regular expression testing,
% extraction of submatches, splitting, and replacement, all acting
% on token lists. The syntax of regular expressions is mostly a subset
% of the \textsc{pcre} syntax (and very close to \textsc{posix}),
% with some additions
% due to the fact that \TeX{} manipulates tokens rather than characters.
% For performance reasons, only a limited set of features are implemented.
% Notably, back-references are not supported.
%
% Let us give a few examples. After
% \begin{verbatim}
%   \tl_set:Nn \l_my_tl { That~cat. }
%   \regex_replace_once:nnN { at } { is } \l_my_tl
% \end{verbatim}
% the token list variable \cs[no-index]{l_my_tl} holds the text
% \enquote{\texttt{This cat.}}, where the first
% occurrence of \enquote{\texttt{at}} was replaced
% by \enquote{\texttt{is}}. A more complicated example is
% a pattern to emphasize each word and add a comma after it:
% \begin{verbatim}
%   \regex_replace_all:nnN { \w+ } { \c{emph}\cB\{ \0 \cE\} , } \l_my_tl
% \end{verbatim}
% The |\w| sequence represents any \enquote{word} character, and |+|
% indicates that the |\w| sequence should be repeated as many times as
% possible (at least once), hence matching a word in the input token
% list. In the replacement text, |\0| denotes the full match (here, a
% word).  The command |\emph| is inserted using |\c{emph}|, and its
% argument |\0| is put between braces |\cB\{| and |\cE\}|.
%
% If a regular expression is to be used several times,
% it can be compiled once, and stored in a regex
% variable using \cs{regex_const:Nn}. For example,
% \begin{verbatim}
%   \regex_const:Nn \c_foo_regex { \c{begin} \cB. (\c[^BE].*) \cE. }
% \end{verbatim}
% stores in \cs[no-index]{c_foo_regex} a regular expression which matches the
% starting marker for an environment: \cs[no-index]{begin}, followed by a
% begin-group token (|\cB.|), then any number of tokens which are
% neither begin-group nor end-group character tokens (|\c[^BE].*|),
% ending with an end-group token (|\cE.|). As explained in the next
% section, the parentheses \enquote{capture} the result of |\c[^BE].*|,
% giving us access to the name of the environment when doing
% replacements.
%
% \section{Syntax of regular expressions}
%
% \subsection{Regex examples}
%
% We start with a few examples, and encourage the reader to apply
% \cs{regex_show:n} to these regular expressions.
% \begin{itemize}
% \item |Cat| matches the word \enquote{Cat} capitalized in this way,
%   but also matches the beginning of the word \enquote{Cattle}: use
%   |\bCat\b| to match a complete word only.
% \item |[abc]| matches one letter among \enquote{a}, \enquote{b},
%   \enquote{c}; the pattern \verb"(a|b|c)" matches the same three
%   possible letters (but see the discussion of submatches below).
% \item |[A-Za-z]*| matches any number (due to the quantifier
%   \verb"*") of Latin letters (not accented).
% \item |\c{[A-Za-z]*}| matches a control sequence made of Latin
%   letters.
% \item |\_[^\_]*\_| matches an underscore, any number of characters
%   other than underscore, and another underscore; it is equivalent to
%   |\_.*?\_| where |.| matches arbitrary characters and the
%   lazy quantifier |*?| means to match as few characters as
%   possible, thus avoiding matching underscores.
% \item |[\+\-]?\d+| matches an explicit integer with at most one
%   sign.
% \item \verb*"[\+\-\ ]*\d+\ *" matches an explicit integer with any
%   number of $+$ and $-$ signs, with spaces allowed except within the
%   mantissa, and surrounded by spaces.
% \item \verb*"[\+\-\ ]*(\d+|\d*\.\d+)\ *" matches an explicit integer or
%   decimal number; using \verb*"[.,]" instead of \verb*"\." would allow
%   the comma as a decimal marker.
% \item
%   \verb*"[\+\-\ ]*(\d+|\d*\.\d+)\ *((?i)pt|in|[cem]m|ex|[bs]p|[dn]d|[pcn]c)\ *"
%   \allowbreak matches an explicit dimension with any unit that \TeX{} knows, where
%   \verb*"(?i)" means to treat lowercase and uppercase letters
%   identically.
% \item \verb*"[\+\-\ ]*((?i)nan|inf|(\d+|\d*\.\d+)(\ *e[\+\-\ ]*\d+)?)\ *"
%   matches an explicit floating point number or the special values
%   \verb*"nan" and \verb*"inf" (with signs and spaces allowed).
% \item \verb*"[\+\-\ ]*(\d+|\cC.)\ *" matches an explicit integer or
%   control sequence (without checking whether it is an integer
%   variable).
% \item |\G.*?\K| at the beginning of a regular expression matches and
%   discards (due to |\K|) everything between the end of the previous
%   match (|\G|) and what is matched by the rest of the regular
%   expression; this is useful in \cs{regex_replace_all:nnN} when the
%   goal is to extract matches or submatches in a finer way than with
%   \cs{regex_extract_all:nnN}.
% \end{itemize}
% While it is impossible for a regular expression to match only integer
% expressions, \verb*"[\+\-\(]*\d+\)*([\+\-*/][\+\-\(]*\d+\)*)*" matches among
% other things all valid integer expressions (made only with explicit
% integers).  One should follow it with further testing.
%
% \subsection{Characters in regular expressions}
%
% Most characters match exactly themselves,
% with an arbitrary category code. Some characters are
% special and must be escaped with a backslash (\emph{e.g.}, |\*|
% matches a star character). Some escape sequences of
% the form backslash--letter also have a special meaning
% (for instance |\d| matches any digit). As a rule,
% \begin{itemize}
% \item every alphanumeric character (\texttt{A}--\texttt{Z},
%   \texttt{a}--\texttt{z}, \texttt{0}--\texttt{9}) matches
%   exactly itself, and should not be escaped, because
%   |\A|, |\B|, \ldots{} have special meanings;
% \item non-alphanumeric printable ascii characters can (and should)
%   always be escaped: many of them have special meanings (\emph{e.g.},
%   use |\(|, |\)|, |\?|, |\.|);
% \item spaces should always be escaped (even in character
%   classes);
% \item any other character may be escaped or not, without any
%   effect: both versions match exactly that character.
% \end{itemize}
% Note that these rules play nicely with the fact that many
% non-alphanumeric characters are difficult to input into \TeX{}
% under normal category codes. For instance, |\\abc\%|
% matches the characters |\abc%| (with arbitrary category codes),
% but does not match the control sequence |\abc| followed by a
% percent character. Matching control sequences can be done
% using the |\c|\Arg{regex} syntax (see below).
%
% Any special character which appears at a place where its special
% behaviour cannot apply matches itself instead (for instance, a
% quantifier appearing at the beginning of a string), after raising a
% warning.
%
% Characters.
% \begin{l3regex-syntax}
%   \item[\\x\{hh\ldots{}\}] Character with hex code \texttt{hh\ldots{}}
%   \item[\\xhh] Character with hex code \texttt{hh}.
%   \item[\\a] Alarm (hex 07).
%   \item[\\e] Escape (hex 1B).
%   \item[\\f] Form-feed (hex 0C).
%   \item[\\n] New line (hex 0A).
%   \item[\\r] Carriage return (hex 0D).
%   \item[\\t] Horizontal tab (hex 09).
% \end{l3regex-syntax}
%
% \subsection{Characters classes}
%
% Character types.
% \begin{l3regex-syntax}
%   \item[.] A single period matches any token.
%   \item[\\d] Any decimal digit.
%   \item[\\h] Any horizontal space character,
%     equivalent to |[\ \^^I]|: space and tab.
%   \item[\\s] Any space character,
%     equivalent to |[\ \^^I\^^J\^^L\^^M]|.
%   \item[\\v] Any vertical space character,
%     equivalent to |[\^^J\^^K\^^L\^^M]|. Note that |\^^K| is a vertical space,
%     but not a space, for compatibility with Perl.
%   \item[\\w] Any word character, \emph{i.e.},
%     alphanumerics and underscore, equivalent to the explicit
%     class |[A-Za-z0-9\_]|.
%   \item[\\D] Any token not matched by |\d|.
%   \item[\\H] Any token not matched by |\h|.
%   \item[\\N] Any token other than the |\n| character (hex 0A).
%   \item[\\S] Any token not matched by |\s|.
%   \item[\\V] Any token not matched by |\v|.
%   \item[\\W] Any token not matched by |\w|.
% \end{l3regex-syntax}
% Of those, |.|, |\D|, |\H|, |\N|, |\S|, |\V|, and |\W| match arbitrary
% control sequences.
%
% Character classes match exactly one token in the subject.
% \begin{l3regex-syntax}
%   \item[{[\ldots{}]}] Positive character class.
%     Matches any of the specified tokens.
%   \item[{[\char`\^\ldots{}]}] Negative character class.
%     Matches any token other than the specified characters.
%   \item[{x-y}] Within a character class, this denotes a range (can be
%     used with escaped characters).
%   \item[{[:\meta{name}:]}] Within a character class (one more set of
%     brackets), this denotes the \textsc{posix} character class
%     \meta{name}, which can be \texttt{alnum}, \texttt{alpha},
%     \texttt{ascii}, \texttt{blank}, \texttt{cntrl}, \texttt{digit},
%     \texttt{graph}, \texttt{lower}, \texttt{print}, \texttt{punct},
%     \texttt{space}, \texttt{upper}, \texttt{word}, or \texttt{xdigit}.
%   \item[{[:\char`\^\meta{name}:]}] Negative \textsc{posix} character class.
% \end{l3regex-syntax}
% For instance, |[a-oq-z\cC.]| matches any lowercase latin letter
% except |p|, as well as control sequences (see below for a description
% of |\c|).
%
% In character classes, only |[|, |^|, |-|, |]|, |\| and spaces are
% special, and should be escaped. Other non-alphanumeric characters can
% still be escaped without harm. Any escape sequence which matches a
% single character (|\d|, |\D|, \emph{etc.}) is supported in character
% classes.  If the first character is |^|, then
% the meaning of the character class is inverted; |^| appearing anywhere
% else in the range is not special.  If the first character (possibly
% following a leading |^|) is |]| then it does not need to be escaped
% since ending the range there would make it empty.
% Ranges of characters
% can be expressed using |-|, for instance, |[\D 0-5]| and |[^6-9]| are
% equivalent.
%
% \subsection{Structure: alternatives, groups, repetitions}
%
% Quantifiers (repetition).
% \begin{l3regex-syntax}
%   \item[?] $0$ or $1$, greedy.
%   \item[??] $0$ or $1$, lazy.
%   \item[*] $0$ or more, greedy.
%   \item[*?] $0$ or more, lazy.
%   \item[+] $1$ or more, greedy.
%   \item[+?] $1$ or more, lazy.
%   \item[\{$n$\}] Exactly $n$.
%   \item[\{$n,$\}] $n$ or more, greedy.
%   \item[\{$n,$\}?] $n$ or more, lazy.
%   \item[\{$n,m$\}] At least $n$, no more than $m$, greedy.
%   \item[\{$n,m$\}?] At least $n$, no more than $m$, lazy.
% \end{l3regex-syntax}
% For greedy quantifiers the regex code will first investigate matches
% that involve as many repetitions as possible, while for lazy
% quantifiers it investigates matches with as few repetitions as
% possible first.
%
% Alternation and capturing groups.
% \begin{l3regex-syntax}
%   \item[A\char`|B\char`|C] Either one of \texttt{A}, \texttt{B},
%     or \texttt{C}, investigating \texttt{A} first.
%   \item[(\ldots{})] Capturing group.
%   \item[(?:\ldots{})] Non-capturing group.
%   \item[(?\char`|\ldots{})] Non-capturing group which resets
%     the group number for capturing groups in each alternative.
%     The following group is numbered with the first unused
%     group number.
% \end{l3regex-syntax}
%
% Capturing groups are a means of extracting information about the
% match. Parenthesized groups are labelled in the order of their
% opening parenthesis, starting at $1$. The contents of those groups
% corresponding to the \enquote{best} match (leftmost longest)
% can be extracted and stored in a sequence of token lists using for
% instance \cs{regex_extract_once:nnNTF}.
%
% The |\K| escape sequence resets the beginning of the match to the
% current position in the token list. This only affects what is reported
% as the full match. For instance,
% \begin{verbatim}
%   \regex_extract_all:nnN { a \K . } { a123aaxyz } \l_foo_seq
% \end{verbatim}
% results in \cs[no-index]{l_foo_seq} containing the items |{1}| and |{a}|: the
% true matches are |{a1}| and |{aa}|, but they are trimmed by the use of
% |\K|. The |\K| command does not affect capturing groups: for instance,
% \begin{verbatim}
%   \regex_extract_once:nnN { (. \K c)+ \d } { acbc3 } \l_foo_seq
% \end{verbatim}
% results in \cs[no-index]{l_foo_seq} containing the items |{c3}| and |{bc}|: the
% true match is |{acbc3}|, with first submatch |{bc}|, but |\K| resets
% the beginning of the match to the last position where it appears.
%
% \subsection{Matching exact tokens}
%
% The |\c| escape sequence allows to test the category code of tokens,
% and match control sequences. Each character category is represented
% by a single uppercase letter:
% \begin{itemize}
% \item |C| for control sequences;
% \item |B| for begin-group tokens;
% \item |E| for end-group tokens;
% \item |M| for math shift;
% \item |T| for alignment tab tokens;
% \item |P| for macro parameter tokens;
% \item |U| for superscript tokens (up);
% \item |D| for subscript tokens (down);
% \item |S| for spaces;
% \item |L| for letters;
% \item |O| for others; and
% \item |A| for active characters.
% \end{itemize}
% The |\c| escape sequence is used as follows.
% \begin{l3regex-syntax}
%   \item[\\c\Arg{regex}] A control sequence whose csname matches the
%     \meta{regex}, anchored at the beginning and end, so that |\c{begin}|
%     matches exactly \cs[no-index]{begin}, and nothing else.
%   \item[\\cX] Applies to the next object, which can be a character,
%     character property, class, or group, and forces this object to
%     only match tokens with category |X| (any of |CBEMTPUDSLOA|. For
%     instance, |\cL[A-Z\d]| matches uppercase letters and digits of
%     category code letter, |\cC.| matches any control sequence, and
%     |\cO(abc)| matches |abc| where each character has category other.
%   \item[{\\c[XYZ]}] Applies to the next object, and forces it to only
%     match tokens with category |X|, |Y|, or |Z| (each being any of
%     |CBEMTPUDSLOA|). For instance, |\c[LSO](..)| matches two tokens of
%     category letter, space, or other.
%   \item[{\\c[\char`\^XYZ]}] Applies to the next object and prevents it
%     from matching any token with category |X|, |Y|, or |Z| (each being
%     any of |CBEMTPUDSLOA|). For instance, |\c[^O]\d| matches digits
%     which have any category different from other.
% \end{l3regex-syntax}
% The category code tests can be used inside classes; for instance,
% |[\cO\d \c[LO][A-F]]| matches what \TeX{} considers as hexadecimal
% digits, namely digits with category other, or uppercase letters from
% |A| to |F| with category either letter or other. Within a group
% affected by a category code test, the outer test can be overridden by
% a nested test: for instance, |\cL(ab\cO\*cd)| matches |ab*cd| where
% all characters are of category letter, except |*| which has category
% other.
%
% The |\u| escape sequence allows to insert the contents of a token list
% directly into a regular expression or a replacement, avoiding the need
% to escape special characters. Namely, |\u|\Arg{var~name} matches
% the exact contents (both character codes and category codes) of the
% variable \cs[no-index]{\meta{var~name}},
% which are obtained by applying \cs{exp_not:v} \Arg{var~name} at the
% time the regular expression is compiled. Within a |\c{...}|
% control sequence matching, the |\u| escape sequence only expands its
% argument once, in effect performing \cs{tl_to_str:v}.
% Quantifiers are supported.
%
% The |\ur| escape sequence allows to insert the contents of a |regex|
% variable into a larger regular expression.  For instance,
% |A\ur{l_tmpa_regex}D| matches the tokens |A| and |D| separated by
% something that matches the regular expression
% \cs[no-index]{l_tmpa_regex}.  This behaves as if a non-capturing group
% were surrounding \cs[no-index]{l_tmpa_regex}, and any group contained
% in \cs[no-index]{l_tmpa_regex} is converted to a non-capturing group.
% Quantifiers are supported.
%
% For instance, if \cs[no-index]{l_tmpa_regex} has value \verb"B|C",
% then |A\ur{l_tmpa_regex}D| is equivalent to \verb"A(?:B|C)D" (matching
% |ABD| or |ACD|) and not to \verb"AB|CD" (matching |AB| or |CD|).  To
% get the latter effect, it is simplest to use \TeX{}'s expansion
% machinery directly: if \cs[no-index]{l_mymodule_BC_tl} contains
% \verb"B|C" then the following two lines show the same result:
% \begin{quote}
%   \cs{regex_show:n} |{ A \u{l_mymodule_BC_tl} D }| \\
%   \cs{regex_show:n} \verb"{ A B | C D }"
% \end{quote}
%
% \subsection{Miscellaneous}
%
% Anchors and simple assertions.
% \begin{l3regex-syntax}
%   \item[\\b] Word boundary: either the previous token is matched by
%     |\w| and the next by |\W|, or the opposite. For this purpose,
%     the ends of the token list are considered as |\W|.
%   \item[\\B] Not a word boundary: between two |\w| tokens
%     or two |\W| tokens (including the boundary).
%   \item[\char`^ \textrm{or} \\A]
%     Start of the subject token list.
%   \item[\char`$\textrm{,} \\Z \textrm{or} \\z] ^^A $
%     End of the subject token list.
%   \item[\\G] Start of the current match. This is only different from |^|
%     in the case of multiple matches: for instance
%     |\regex_count:nnN { \G a } { aaba } \l_tmpa_int| yields $2$, but
%     replacing |\G| by |^| would result in \cs{l_tmpa_int} holding the
%     value $1$.
% \end{l3regex-syntax}
%
% The option |(?i)| makes the match case insensitive (identifying
% \texttt{A}--\texttt{Z} with \texttt{a}--\texttt{z}; no Unicode support
% yet). This applies until the end of the group in which it appears, and
% can be reverted using |(?-i)|. For instance, in
% \verb"(?i)(a(?-i)b|c)d", the letters |a| and |d| are affected by the
% |i| option. Characters within ranges and classes are affected
% individually: |(?i)[Y-\\]| is equivalent to |[YZ\[\\yz]|, and
% |(?i)[^aeiou]| matches any character which is not a vowel. Neither
% character properties, nor |\c{...}| nor |\u{...}| are affected by the
% |i| option.
% ^^A \]
%
% \section{Syntax of the replacement text}
%
% Most of the features described in regular expressions do not make
% sense within the replacement text.  Backslash introduces various
% special constructions, described further below:
% \begin{itemize}
%   \item |\0| is the whole match;
%   \item |\1| is the submatch that was matched by the first (capturing)
%     group |(...)|; similarly for |\2|, \ldots{}, |\9| and
%     |\g{|\meta{number}|}|;
%   \item \verb*|\ | inserts a space (spaces are ignored when not
%     escaped);
%   \item |\a|, |\e|, |\f|, |\n|, |\r|, |\t|, |\xhh|, |\x{hhh}|
%     correspond to single characters as in regular expressions;
%   \item |\c|\Arg{cs~name} inserts a control sequence;
%   \item |\c|\meta{category}\meta{character} (see below);
%   \item |\u|\Arg{tl~var~name} inserts the contents of the
%     \meta{tl~var} (see below).
% \end{itemize}
% Characters other than backslash and space are simply inserted in the
% result (but since the replacement text is first converted to a string,
% one should also escape characters that are special for \TeX{}, for
% instance use~|\#|).  Non-alphanumeric characters can always be safely
% escaped with a backslash.
%
% For instance,
% \begin{verbatim}
%   \tl_set:Nn \l_my_tl { Hello,~world! }
%   \regex_replace_all:nnN { ([er]?l|o) . } { (\0--\1) } \l_my_tl
% \end{verbatim}
% results in \cs[no-index]{l_my_tl} holding |H(ell--el)(o,--o) w(or--o)(ld--l)!|
%
% The submatches are numbered according to the order in which the
% opening parenthesis of capturing groups appear in the regular
% expression to match.  The $n$-th submatch is empty if there are fewer
% than $n$ capturing groups or for capturing groups that appear in
% alternatives that were not used for the match.  In case a capturing
% group matches several times during a match (due to quantifiers) only
% the last match is used in the replacement text. Submatches always keep
% the same category codes as in the original token list.
%
% By default, the category code of characters inserted by the
% replacement are determined by the prevailing category code regime at
% the time where the replacement is made, with two exceptions:
% \begin{itemize}
% \item space characters (with character code $32$) inserted with
%   \verb*|\ | or |\x20| or |\x{20}| have category code~$10$ regardless
%   of the prevailing category code regime;
% \item if the category code would be $0$~(escape), $5$~(newline),
%   $9$~(ignore), $14$~(comment) or $15$~(invalid), it is replaced by
%   $12$~(other) instead.
% \end{itemize}
% The escape sequence |\c| allows to insert characters
% with arbitrary category codes, as well as control sequences.
% \begin{l3regex-syntax}
% \item[\\cX(\ldots{})] Produces the characters \enquote{\ldots{}} with
%   category~|X|, which must be one of |CBEMTPUDSLOA| as in regular
%   expressions.  Parentheses are optional for a single character (which
%   can be an escape sequence).  When nested, the innermost category
%   code applies, for instance |\cL(Hello\cS\ world)!| gives this text
%   with standard category codes.
% \item[\\c\Arg{text}] Produces the control sequence with csname
%   \meta{text}.  The \meta{text} may contain references to the
%   submatches |\0|, |\1|, and so on, as in the example for |\u| below.
% \end{l3regex-syntax}
%
% The escape sequence |\u|\Arg{var~name} allows to insert the
% contents of the variable with name \meta{var~name} directly into
% the replacement, giving an easier control of category codes.  When
% nested in |\c{|\ldots{}|}| and |\u{|\ldots{}|}| constructions, the
% |\u| and |\c|~escape sequences perform \cs{tl_to_str:v}, namely
% extract the value of the control sequence and turn it into a string.
% Matches can also be used within the arguments of |\c| and |\u|.  For
% instance,
% \begin{verbatim}
%   \tl_set:Nn \l_my_one_tl { first }
%   \tl_set:Nn \l_my_two_tl { \emph{second} }
%   \tl_set:Nn \l_my_tl { one , two , one , one }
%   \regex_replace_all:nnN { [^,]+ } { \u{l_my_\0_tl} } \l_my_tl
% \end{verbatim}
% results in \cs[no-index]{l_my_tl} holding |first,\emph{second},first,first|.
%
% Regex replacement is also a convenient way to produce token lists
% with arbitrary category codes.  For instance
% \begin{verbatim}
% \tl_clear:N \l_tmpa_tl
% \regex_replace_all:nnN { } { \cU\% \cA\~ } \l_tmpa_tl
% \end{verbatim}
% results in \cs[no-index]{l_tmpa_tl} containing the percent character
% with category code~$7$ (superscript) and an active tilde character.
%
% \section{Pre-compiling regular expressions}
%
% If a regular expression is to be used several times,
% it is better to compile it once rather than doing it
% each time the regular expression is used. The compiled
% regular expression is stored in a variable. All
% of the \pkg{l3regex} module's functions can be given their
% regular expression argument either as an explicit string
% or as a compiled regular expression.
%
% \begin{function}[added = 2017-05-26]{\regex_new:N}
%   \begin{syntax}
%     \cs{regex_new:N} \meta{regex~var}
%   \end{syntax}
%   Creates a new \meta{regex~var} or raises an error if the
%   name is already taken. The declaration is global. The
%   \meta{regex~var} is initially such that it never matches.
% \end{function}
%
% \begin{function}[added = 2017-05-26]
%   {\regex_set:Nn, \regex_gset:Nn, \regex_const:Nn}
%   \begin{syntax}
%     \cs{regex_set:Nn} \meta{regex~var} \Arg{regex}
%   \end{syntax}
%   Stores a compiled version of the \meta{regular expression}
%   in the \meta{regex~var}. For instance, this function can be used
%   as
%   \begin{verbatim}
%     \regex_new:N \l_my_regex
%     \regex_set:Nn \l_my_regex { my\ (simple\ )? reg(ex|ular\ expression) }
%   \end{verbatim}
%   The assignment is local for \cs{regex_set:Nn} and global for
%   \cs{regex_gset:Nn}. Use \cs{regex_const:Nn} for compiled expressions
%   which never change.
% \end{function}
%
% \begin{function}[added = 2021-04-26, updated = 2021-04-29]
%   {\regex_show:N, \regex_show:n, \regex_log:N, \regex_log:n}
%   \begin{syntax}
%     \cs{regex_show:n} \Arg{regex}
%     \cs{regex_log:n} \Arg{regex}
%   \end{syntax}
%   Displays in the terminal or writes in the log file (respectively)
%   how \pkg{l3regex} interprets the \meta{regex}. For instance,
%   \cs{regex_show:n} \verb+{\A X|Y}+ shows
%   \begin{verbatim}
%     +-branch
%       anchor at start (\A)
%       char code 88 (X)
%     +-branch
%       char code 89 (Y)
%   \end{verbatim}
%   indicating that the anchor |\A| only applies to the first branch:
%   the second branch is not anchored to the beginning of the match.
% \end{function}
%
% \section{Matching}
%
% All regular expression functions are available in both |:n| and |:N|
% variants. The former require a \enquote{standard} regular expression,
% while the later require a compiled expression as generated by
% \cs{regex_set:Nn}.
%
% \begin{function}[TF, added = 2017-05-26]{\regex_match:nn, \regex_match:Nn}
%   \begin{syntax}
%     \cs{regex_match:nnTF} \Arg{regex} \Arg{token list} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether the \meta{regular expression} matches any part
%   of the \meta{token list}. For instance,
%   \begin{verbatim}
%     \regex_match:nnTF { b [cde]* } { abecdcx } { TRUE } { FALSE }
%     \regex_match:nnTF { [b-dq-w] } { example } { TRUE } { FALSE }
%   \end{verbatim}
%   leaves \texttt{TRUE} then \texttt{FALSE} in the input stream.
% \end{function}
%
% \begin{function}[added = 2017-05-26]{\regex_count:nnN, \regex_count:NnN}
%   \begin{syntax}
%     \cs{regex_count:nnN} \Arg{regex} \Arg{token list} \meta{int var}
%   \end{syntax}
%   Sets \meta{int var} within the current \TeX{} group level
%   equal to the number of times
%   \meta{regular expression} appears in \meta{token list}.
%   The search starts by finding the left-most longest match,
%   respecting greedy and lazy (non-greedy) operators. Then the search
%   starts again from the character following the last character
%   of the previous match, until reaching the end of the token list.
%   Infinite loops are prevented in the case where the regular expression
%   can match an empty token list: then we count one match between each
%   pair of characters.
%   For instance,
%   \begin{verbatim}
%     \int_new:N \l_foo_int
%     \regex_count:nnN { (b+|c) } { abbababcbb } \l_foo_int
%   \end{verbatim}
%   results in \cs[no-index]{l_foo_int} taking the value $5$.
% \end{function}
%
% \section{Submatch extraction}
%
% \begin{function}[noTF, added = 2017-05-26]
%   {\regex_extract_once:nnN, \regex_extract_once:NnN}
%   \begin{syntax}
%     \cs{regex_extract_once:nnN} \Arg{regex} \Arg{token list} \meta{seq~var}
%     \cs{regex_extract_once:nnNTF} \Arg{regex} \Arg{token list} \meta{seq~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Finds the first match of the \meta{regular expression} in the
%   \meta{token list}. If it exists, the match is stored as the first
%   item of the \meta{seq~var}, and further items are the contents of
%   capturing groups, in the order of their opening parenthesis. The
%   \meta{seq~var} is assigned locally. If there is no match, the
%   \meta{seq~var} is cleared.  The testing versions insert the
%   \meta{true code} into the input stream if a match was found, and the
%   \meta{false code} otherwise.
%
%   For instance, assume that you type
%   \begin{verbatim}
%     \regex_extract_once:nnNTF { \A(La)?TeX(!*)\Z } { LaTeX!!! } \l_foo_seq
%       { true } { false }
%   \end{verbatim}
%   Then the regular expression (anchored at the start with |\A| and
%   at the end with |\Z|) must match the whole token list. The first
%   capturing group, |(La)?|, matches |La|, and the second capturing
%   group, |(!*)|, matches |!!!|. Thus, \cs[no-index]{l_foo_seq} contains as a result
%   the items |{LaTeX!!!}|, |{La}|, and |{!!!}|, and the \texttt{true}
%   branch is left in the input stream.
%   Note that the $n$-th item of \cs[no-index]{l_foo_seq}, as obtained using
%   \cs{seq_item:Nn}, correspond to the submatch numbered $(n-1)$ in
%   functions such as \cs{regex_replace_once:nnN}.
% \end{function}
%
% \begin{function}[noTF, added = 2017-05-26]
%   {\regex_extract_all:nnN, \regex_extract_all:NnN}
%   \begin{syntax}
%     \cs{regex_extract_all:nnN} \Arg{regex} \Arg{token list} \meta{seq~var}
%     \cs{regex_extract_all:nnNTF} \Arg{regex} \Arg{token list} \meta{seq~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Finds all matches of the \meta{regular expression}
%   in the \meta{token list}, and stores all the submatch information
%   in a single sequence (concatenating the results of
%   multiple \cs{regex_extract_once:nnN} calls).
%   The \meta{seq~var} is assigned locally. If there is no match,
%   the \meta{seq~var} is cleared.
%   The testing versions insert the \meta{true code} into the input
%   stream if a match was found, and the \meta{false code} otherwise.
%   For instance, assume that you type
%   \begin{verbatim}
%     \regex_extract_all:nnNTF { \w+ } { Hello,~world! } \l_foo_seq
%       { true } { false }
%   \end{verbatim}
%   Then the regular expression matches twice, the resulting
%   sequence contains the two items |{Hello}| and |{world}|,
%   and the \texttt{true} branch is left in the input stream.
% \end{function}
%
% \begin{function}[noTF, added = 2017-05-26]{\regex_split:nnN, \regex_split:NnN}
%   \begin{syntax}
%     \cs{regex_split:nnN} \Arg{regular expression} \Arg{token list} \meta{seq~var}
%     \cs{regex_split:nnNTF} \Arg{regular expression} \Arg{token list} \meta{seq~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Splits the \meta{token list} into a sequence of parts, delimited by
%   matches of the \meta{regular expression}. If the \meta{regular expression}
%   has capturing groups, then the token lists that they match are stored as
%   items of the sequence as well. The assignment to \meta{seq~var} is local.
%   If no match is found the resulting \meta{seq~var} has the
%   \meta{token list} as its sole item. If the \meta{regular expression}
%   matches the empty token list, then the \meta{token list} is split
%   into single tokens.
%   The testing versions insert the \meta{true code} into the input
%   stream if a match was found, and the \meta{false code} otherwise.
%   For example, after
%   \begin{verbatim}
%     \seq_new:N \l_path_seq
%     \regex_split:nnNTF { / } { the/path/for/this/file.tex } \l_path_seq
%       { true } { false }
%   \end{verbatim}
%   the sequence |\l_path_seq| contains the items |{the}|, |{path}|,
%   |{for}|, |{this}|, and |{file.tex}|, and the \texttt{true} branch
%   is left in the input stream.
% \end{function}
%
% \section{Replacement}
%
% \begin{function}[noTF, added = 2017-05-26]
%   {\regex_replace_once:nnN,\regex_replace_once:NnN}
%   \begin{syntax}
%     \cs{regex_replace_once:nnN} \Arg{regular expression} \Arg{replacement} \meta{tl~var}
%     \cs{regex_replace_once:nnNTF} \Arg{regular expression} \Arg{replacement} \meta{tl~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Searches for the \meta{regular expression} in the \meta{token list}
%   and replaces the first match with the \meta{replacement}. The result
%   is assigned locally to \meta{tl~var}. In the \meta{replacement},
%   |\0| represents the full match, |\1| represent the contents of the
%   first capturing group, |\2| of the second, \emph{etc.}
% \end{function}
%
% \begin{function}[noTF, added = 2017-05-26]
%   {\regex_replace_all:nnN, \regex_replace_all:NnN}
%   \begin{syntax}
%     \cs{regex_replace_all:nnN} \Arg{regular expression} \Arg{replacement} \meta{tl~var}
%     \cs{regex_replace_all:nnNTF} \Arg{regular expression} \Arg{replacement} \meta{tl~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Replaces all occurrences of the \meta{regular expression} in the
%   \meta{token list} by the \meta{replacement}, where |\0| represents
%   the full match, |\1| represent the contents of the first capturing
%   group, |\2| of the second, \emph{etc.} Every match is treated
%   independently, and matches cannot overlap.  The result is assigned
%   locally to \meta{tl~var}.
% \end{function}
%
% \section{Constants and variables}
%
% \begin{variable}[added = 2017-12-11]{\l_tmpa_regex, \l_tmpb_regex}
%   Scratch regex for local assignment. These are never used by
%   the kernel code, and so are safe for use with any \LaTeX3-defined
%   function. However, they may be overwritten by other non-kernel
%   code and so should only be used for short-term storage.
% \end{variable}
%
% \begin{variable}[added = 2017-12-11]{\g_tmpa_regex, \g_tmpb_regex}
%   Scratch regex for global assignment. These are never used by
%   the kernel code, and so are safe for use with any \LaTeX3-defined
%   function. However, they may be overwritten by other non-kernel
%   code and so should only be used for short-term storage.
% \end{variable}
%
% \section{Bugs, misfeatures, future work, and other possibilities}
%
% The following need to be done now.
% \begin{itemize}
%   \item Rewrite the documentation in a more ordered way, perhaps add a
%     \textsc{bnf}?
% \end{itemize}
%
% Additional error-checking to come.
% \begin{itemize}
%   \item Clean up the use of messages.
%   \item Cleaner error reporting in the replacement phase.
%   \item Add tracing information.
%   \item Detect attempts to use back-references and other
%     non-implemented syntax.
%   \item Test for the maximum register \cs[no-index]{c_max_register_int}.
%   \item Find out whether the fact that |\W| and friends match the
%     end-marker leads to bugs. Possibly update \cs[no-index]{__regex_item_reverse:n}.
%   \item The empty cs should be matched by |\c{}|, not by
%     |\c{csname.?endcsname\s?}|.
% \end{itemize}
%
% Code improvements to come.
% \begin{itemize}
%   \item Shift arrays so that the useful information starts at
%     position~$1$.
%   \item Only build |\c{...}| once.
%   \item Use arrays for the left and right state stacks when
%     compiling a regex.
%   \item Should \cs[no-index]{__regex_action_free_group:n} only be used for greedy
%     |{n,}| quantifier? (I think not.)
%   \item Quantifiers for |\u| and assertions.
%   \item When matching, keep track of an explicit stack of
%     \texttt{curr_state} and \texttt{curr_submatches}.
%   \item If possible, when a state is reused by the same thread, kill
%     other subthreads.
%   \item Use an array rather than \cs[no-index]{l__regex_balance_tl}
%     to build the function \cs[no-index]{__regex_replacement_balance_one_match:n}.
%   \item Reduce the number of epsilon-transitions in alternatives.
%   \item Optimize simple strings: use less states (|abcade| should give
%     two states, for |abc| and |ade|). [Does that really make sense?]
%   \item Optimize groups with no alternative.
%   \item Optimize states with a single \cs[no-index]{__regex_action_free:n}.
%   \item Optimize the use of \cs[no-index]{__regex_action_success:} by inserting it
%     in state $2$ directly instead of having an extra transition.
%   \item Optimize the use of \cs{int_step_...} functions.
%   \item Groups don't capture within regexes for csnames; optimize and
%     document.
%   \item Better \enquote{show} for anchors, properties, and catcode tests.
%   \item Does |\K| really need a new state for itself?
%   \item When compiling, use a boolean \texttt{in_cs} and less magic
%     numbers.
%   \item Instead of checking whether the character is special or
%     alphanumeric using its character code, check if it is special in
%     regexes with \cs{cs_if_exist} tests.
% \end{itemize}
%
% The following features are likely to be implemented at some point
% in the future.
% \begin{itemize}
%   \item General look-ahead/behind assertions.
%   \item Regex matching on external files.
%   \item Conditional subpatterns with look ahead/behind: \enquote{if
%       what follows is [\ldots{}], then [\ldots{}]}.
%   \item |(*..)| and |(?..)| sequences to set some options.
%   \item UTF-8 mode for \pdfTeX{}.
%   \item Newline conventions are not done.
%     In particular, we should have an option for |.| not to match newlines.
%     Also, |\A| should differ from |^|, and |\Z|, |\z| and |$| should
%     differ.
%   \item Unicode properties: |\p{..}| and |\P{..}|;
%     |\X| which should match any \enquote{extended} Unicode sequence.
%     This requires to manipulate a lot of data, probably using tree-boxes.
% \end{itemize}
%
% The following features of \textsc{pcre} or Perl may or may not be
% implemented.
% \begin{itemize}
%   \item Callout with |(?C...)| or other syntax: some internal code
%     changes make that possible, and it can be useful for instance in
%     the replacement code to stop a regex replacement when some marker
%     has been found; this raises the question of a potential
%     |\regex_break:| and then of playing well with \cs{tl_map_break:}
%     called from within the code in a regex.  It also raises the
%     question of nested calls to the regex machinery, which is a
%     problem since \tn{fontdimen} are global.
%   \item Conditional subpatterns (other than with a look-ahead or
%     look-behind condition): this is non-regular, isn't it?
%   \item Named subpatterns: \TeX{} programmers have lived so far
%     without any need for named macro parameters.
% \end{itemize}
%
% The following features of \textsc{pcre} or Perl will definitely not be
% implemented.
% \begin{itemize}
%   \item Back-references: non-regular feature, this requires
%     backtracking, which is prohibitively slow.
%   \item Recursion: this is a non-regular feature.
%   \item Atomic grouping, possessive quantifiers: those tools, mostly
%     meant to fix catastrophic backtracking, are unnecessary in a
%     non-backtracking algorithm, and difficult to implement.
%   \item Subroutine calls: this syntactic sugar is difficult to include
%     in a non-backtracking algorithm, in particular because the
%     corresponding group should be treated as atomic.
%   \item Backtracking control verbs: intrinsically tied to
%     backtracking.
%   \item |\ddd|, matching the character with octal code \texttt{ddd}:
%     we already have |\x{...}| and the syntax is confusingly close to
%     what we could have used for backreferences (|\1|, |\2|, \ldots{}),
%     making it harder to produce useful error message.
%   \item |\cx|, similar to \TeX{}'s own |\^^x|.
%   \item Comments: \TeX{} already has its own system for comments.
%   \item |\Q...\E| escaping: this would require to read the argument
%     verbatim, which is not in the scope of this module.
%   \item |\C| single byte in UTF-8 mode: \XeTeX{} and \LuaTeX{} serve
%     us characters directly, and splitting those into bytes is tricky,
%     encoding dependent, and most likely not useful anyways.
% \end{itemize}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3regex} implementation}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=regex>
%    \end{macrocode}
%
% \subsection{Plan of attack}
%
% Most regex engines use backtracking. This allows to provide very
% powerful features (back-references come to mind first), but it is
% costly, and raises the problem of catastrophic backtracking. Since
% \TeX{} is not first and foremost a programming language, complicated
% code tends to run slowly, and we must use faster, albeit slightly more
% restrictive, techniques, coming from automata theory.
%
% Given a regular expression of $n$ characters, we do the following:
% \begin{itemize}
%   \item (Compiling.) Analyse the regex, finding invalid input, and
%     convert it to an internal representation.
%   \item (Building.) Convert the compiled regex to a non-deterministic
%     finite automaton (\textsc{nfa}) with $O(n)$ states which
%     accepts precisely token lists matching that regex.
%   \item (Matching.) Loop through the query token list one token (one
%     \enquote{position}) at a time, exploring in parallel every
%     possible path (\enquote{active thread}) through the \textsc{nfa},
%     considering active threads in an order determined by the
%     quantifiers' greediness.
% \end{itemize}
%
% We use the following vocabulary in the code comments (and in variable
% names).
% \begin{itemize}
%   \item \emph{Group}: index of the capturing group, $-1$ for
%     non-capturing groups. ^^A start/end index?
%   \item \emph{Position}: each token in the query is labelled by an
%     integer \meta{position}, with $\texttt{min_pos} - 1 \leq
%     \meta{position} \leq \texttt{max_pos}$. The lowest and highest
%     positions $\texttt{min_pos} - 1$ and $\texttt{max_pos}$
%     correspond to imaginary begin and end markers (with
%     non-existent category code and character code).
%     $\texttt{max_pos}$ is only set quite late in the processing.
%   \item \emph{Query}: the token list to which we apply the regular
%     expression.
%   \item \emph{State}: each state of the \textsc{nfa} is labelled by an
%     integer \meta{state} with $\texttt{min_state} \leq \meta{state} <
%     \texttt{max_state}$.
%   \item \emph{Active thread}: state of the \textsc{nfa} that is reached
%     when reading the query token list for the matching. Those threads
%     are ordered according to the greediness of quantifiers.
%   \item \emph{Step}: used when matching, starts at $0$, incremented
%     every time a character is read, and is not reset when searching
%     for repeated matches. The integer \cs{l_@@_step_int} is a
%     unique id for all the steps of the matching algorithm.
% \end{itemize}
%
% We use \pkg{l3intarray} to manipulate arrays of integers.
% We also abuse \TeX{}'s
% \tn{toks} registers, by accessing them directly by number rather than
% tying them to control sequence using the \tn{newtoks} allocation
% functions. Specifically, these arrays and \tn{toks} are used as
% follows.  When building,
% \tn{toks}\meta{state} holds the tests and actions to perform in the
% \meta{state} of the \textsc{nfa}.  When matching,
% \begin{itemize}
%   \item \cs{g_@@_state_active_intarray} holds the last \meta{step} in
%     which each \meta{state} was active.
%   \item \cs{g_@@_thread_info_intarray} consists of blocks for each
%     \meta{thread} (with $\texttt{min_thread} \leq \meta{thread} <
%     \texttt{max_thread}$).  Each block has
%     $1+2\cs{l_@@_capturing_group_int}$ entries: the \meta{state} in
%     which the \meta{thread} currently is, followed by the beginnings
%     of all submatches, and then the ends of all submatches. The
%     \meta{threads} are ordered starting from the best to the least
%     preferred.
%   \item \cs{g_@@_submatch_prev_intarray}, \cs{g_@@_submatch_begin_intarray}
%     and \cs{g_@@_submatch_end_intarray} hold, for each submatch (as would
%     be extracted by \cs{regex_extract_all:nnN}), the place where the
%     submatch started to be looked for and its two end-points.  For
%     historical reasons, the minimum index is twice \texttt{max_state},
%     and the used registers go up to \cs{l_@@_submatch_int}. They are
%     organized in blocks of \cs{l_@@_capturing_group_int} entries, each
%     block corresponding to one match with all its submatches stored in
%     consecutive entries.
% \end{itemize}
% When actually building the result,
% \begin{itemize}
%   \item \tn{toks}\meta{position} holds \meta{tokens} which \texttt{o}-
%     and \texttt{x}-expand to the \meta{position}-th token in the query.
%   \item \cs{g_@@_balance_intarray} holds the balance of begin-group and
%     end-group character tokens which appear before that point in the
%     token list.
% \end{itemize}
%
% The code is structured as follows. Variables are introduced in the
% relevant section. First we present some generic helper functions. Then
% comes the code for compiling a regular expression, and for showing the
% result of the compilation. The building phase converts a compiled
% regex to \textsc{nfa} states, and the automaton is run by the code in
% the following section. The only remaining brick is parsing the
% replacement text and performing the replacement. We are then ready for
% all the user functions. Finally, messages, and a little bit of tracing
% code.
%
% \subsection{Helpers}
%
% \begin{macro}{\@@_int_eval:w}
%   Access the primitive: performance is key here, so we do not use
%   the slower route \emph{via} \cs{int_eval:n}.
%    \begin{macrocode}
\cs_new_eq:NN \@@_int_eval:w \tex_numexpr:D
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_standard_escapechar:}
%   Make the \tn{escapechar} into the standard backslash.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_standard_escapechar:
  { \int_set:Nn \tex_escapechar:D { `\\ } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_toks_use:w}
%   Unpack a \tn{toks} given its number.
%    \begin{macrocode}
\cs_new:Npn \@@_toks_use:w { \tex_the:D \tex_toks:D }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_toks_clear:N, \@@_toks_set:Nn, \@@_toks_set:No}
%   Empty a \tn{toks} or set it to a value, given its number.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_toks_clear:N #1
  { \@@_toks_set:Nn #1 { } }
\cs_new_eq:NN \@@_toks_set:Nn \tex_toks:D
\cs_new_protected:Npn \@@_toks_set:No #1
  { \tex_toks:D #1 \exp_after:wN }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_toks_memcpy:NNn}
%   Copy |#3| \tn{toks} registers from |#2| onwards to |#1| onwards,
%   like |C|'s |memcpy|.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_toks_memcpy:NNn #1#2#3
  {
    \prg_replicate:nn {#3}
      {
        \tex_toks:D #1 = \tex_toks:D #2
        \int_incr:N #1
        \int_incr:N #2
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_toks_put_left:Nx}
% \begin{macro}{\@@_toks_put_right:Nx, \@@_toks_put_right:Nn}
%   During the building phase we wish to add \texttt{x}-expanded
%   material to \tn{toks}, either to the left or to the right. The
%   expansion is done \enquote{by hand} for optimization (these
%   operations are used quite a lot). The \texttt{Nn} version of
%   \cs{@@_toks_put_right:Nx} is provided because it is more
%   efficient than \texttt{x}-expanding with \cs{exp_not:n}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_toks_put_left:Nx #1#2
  {
    \cs_set_nopar:Npx \@@_tmp:w { #2 }
    \tex_toks:D #1 \exp_after:wN \exp_after:wN \exp_after:wN
      { \exp_after:wN \@@_tmp:w \tex_the:D \tex_toks:D #1 }
  }
\cs_new_protected:Npn \@@_toks_put_right:Nx #1#2
  {
    \cs_set_nopar:Npx \@@_tmp:w {#2}
    \tex_toks:D #1 \exp_after:wN
      { \tex_the:D \tex_toks:D \exp_after:wN #1 \@@_tmp:w }
  }
\cs_new_protected:Npn \@@_toks_put_right:Nn #1#2
  { \tex_toks:D #1 \exp_after:wN { \tex_the:D \tex_toks:D #1 #2 } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_curr_cs_to_str:}
%   Expands to the string representation of the token (known to be a
%   control sequence) at the current position \cs{l_@@_curr_pos_int}.
%   It should only be used in \texttt{x}-expansion to avoid losing a
%   leading space.
%    \begin{macrocode}
\cs_new:Npn \@@_curr_cs_to_str:
  {
    \exp_after:wN \exp_after:wN \exp_after:wN \cs_to_str:N
    \l_@@_curr_token_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_intarray_item:NnF, \@@_intarray_item_aux:nNF}
%   Item of intarray, with a default value.
%    \begin{macrocode}
\cs_new:Npn \@@_intarray_item:NnF #1#2
  { \exp_args:Nf \@@_intarray_item_aux:nNF { \int_eval:n {#2} } #1 }
\cs_new:Npn \@@_intarray_item_aux:nNF #1#2
  {
    \if_int_compare:w #1 > \c_zero_int
      \exp_after:wN \use_i:nn
    \else:
      \exp_after:wN \use_ii:nn
    \fi:
    { \__kernel_intarray_item:Nn #2 {#1} }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_maplike_break:}
%   Analogous to \cs{tl_map_break:}, this correctly exits
%   \cs{tl_map_inline:nn} and similar constructions and jumps to the
%   matching \cs{prg_break_point:Nn} \cs{@@_maplike_break:} |{| |}|.
%    \begin{macrocode}
\cs_new:Npn \@@_maplike_break:
  { \prg_map_break:Nn \@@_maplike_break: { } }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Constants and variables}
%
% \begin{macro}{\@@_tmp:w}
%   Temporary function used for various short-term purposes.
%    \begin{macrocode}
\cs_new:Npn \@@_tmp:w { }
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}
%   {
%     \l_@@_internal_a_tl,  \l_@@_internal_b_tl,
%     \l_@@_internal_a_int, \l_@@_internal_b_int,
%     \l_@@_internal_c_int, \l_@@_internal_bool,
%     \l_@@_internal_seq,   \g_@@_internal_tl,
%   }
%   Temporary variables used for various purposes.
%    \begin{macrocode}
\tl_new:N   \l_@@_internal_a_tl
\tl_new:N   \l_@@_internal_b_tl
\int_new:N  \l_@@_internal_a_int
\int_new:N  \l_@@_internal_b_int
\int_new:N  \l_@@_internal_c_int
\bool_new:N \l_@@_internal_bool
\seq_new:N  \l_@@_internal_seq
\tl_new:N   \g_@@_internal_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_build_tl}
%   This temporary variable is specifically for use with the |tl_build|
%   machinery.
%    \begin{macrocode}
\tl_new:N \l_@@_build_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_no_match_regex}
%   This regular expression matches nothing, but is still a valid
%   regular expression. We could use a failing assertion, but I went for
%   an empty class. It is used as the initial value for regular
%   expressions declared using \cs{regex_new:N}.
%    \begin{macrocode}
\tl_const:Nn \c_@@_no_match_regex
  {
    \@@_branch:n
      { \@@_class:NnnnN \c_true_bool { } { 1 } { 0 } \c_true_bool }
  }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_balance_int}
%   During this phase, \cs{l_@@_balance_int} counts the balance of
%   begin-group and end-group character tokens which appear before a
%   given point in the token list. This variable is also used to keep
%   track of the balance in the replacement text.
%    \begin{macrocode}
\int_new:N \l_@@_balance_int
%    \end{macrocode}
% \end{variable}
%
% \subsubsection{Testing characters}
%
% \begin{macro}{\c_@@_ascii_min_int, \c_@@_ascii_max_control_int, \c_@@_ascii_max_int}
%    \begin{macrocode}
\int_const:Nn \c_@@_ascii_min_int { 0 }
\int_const:Nn \c_@@_ascii_max_control_int { 31 }
\int_const:Nn \c_@@_ascii_max_int { 127 }
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\c_@@_ascii_lower_int}
%    \begin{macrocode}
\int_const:Nn \c_@@_ascii_lower_int { `a - `A }
%    \end{macrocode}
% \end{variable}
%
% \subsubsection{Internal auxiliaries}
%
% \begin{variable}{\q_@@_recursion_stop}
%   Internal recursion quarks.
%    \begin{macrocode}
\quark_new:N \q_@@_recursion_stop
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[EXP]{
%     \@@_use_none_delimit_by_q_recursion_stop:w,
%     \@@_use_i_delimit_by_q_recursion_stop:nw
%   }
%   Functions to gobble up to a quark.
%    \begin{macrocode}
\cs_new:Npn \@@_use_none_delimit_by_q_recursion_stop:w
  #1 \q_@@_recursion_stop { }
\cs_new:Npn \@@_use_i_delimit_by_q_recursion_stop:nw
  #1 #2 \q_@@_recursion_stop {#1}
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\q_@@_nil}
%   Internal quarks.
%    \begin{macrocode}
\quark_new:N \q_@@_nil
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[pTF]{\@@_quark_if_nil:n}
%   Branching quark conditional.
%    \begin{macrocode}
\__kernel_quark_new_conditional:Nn \@@_quark_if_nil:N { F }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_break_point:TF}
% \begin{macro}{\@@_break_true:w}
%   When testing whether a character of the query token list matches
%   a given character class in the regular expression, we often
%   have to test it against several ranges of characters, checking
%   if any one of those matches. This is done with a structure like
%   \begin{quote}
%     \meta{test1} \ldots{} \meta{test$\sb{n}$} \\
%     \cs{@@_break_point:TF} \Arg{true code} \Arg{false code}
%   \end{quote}
%   If any of the tests succeeds, it calls \cs{@@_break_true:w},
%   which cleans up and leaves \meta{true code} in the input stream.
%   Otherwise, \cs{@@_break_point:TF} leaves the \meta{false code}
%   in the input stream.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_break_true:w
   #1 \@@_break_point:TF #2 #3 {#2}
\cs_new_protected:Npn \@@_break_point:TF #1 #2 { #2 }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_item_reverse:n}
%   This function makes showing regular expressions easier, and lets us
%   define |\D| in terms of |\d| for instance. There is a subtlety: the
%   end of the query is marked by $-2$, and thus matches |\D| and
%   other negated properties; this case is caught by another part of
%   the code.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_item_reverse:n #1
  {
    #1
    \@@_break_point:TF { } \@@_break_true:w
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {\@@_item_caseful_equal:n, \@@_item_caseful_range:nn}
%   Simple comparisons triggering \cs{@@_break_true:w} when true.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_item_caseful_equal:n #1
  {
    \if_int_compare:w #1 = \l_@@_curr_char_int
      \exp_after:wN \@@_break_true:w
    \fi:
  }
\cs_new_protected:Npn \@@_item_caseful_range:nn #1 #2
  {
    \reverse_if:N \if_int_compare:w #1 > \l_@@_curr_char_int
      \reverse_if:N \if_int_compare:w #2 < \l_@@_curr_char_int
        \exp_after:wN \exp_after:wN \exp_after:wN \@@_break_true:w
      \fi:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {\@@_item_caseless_equal:n, \@@_item_caseless_range:nn}
%   For caseless matching, we perform the test both on the
%   \texttt{curr_char} and on the \texttt{case_changed_char}. Before
%   doing the second set of tests, we make sure that
%   \texttt{case_changed_char} has been computed.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_item_caseless_equal:n #1
  {
    \if_int_compare:w #1 = \l_@@_curr_char_int
      \exp_after:wN \@@_break_true:w
    \fi:
    \@@_maybe_compute_ccc:
    \if_int_compare:w #1 = \l_@@_case_changed_char_int
      \exp_after:wN \@@_break_true:w
    \fi:
  }
\cs_new_protected:Npn \@@_item_caseless_range:nn #1 #2
  {
    \reverse_if:N \if_int_compare:w #1 > \l_@@_curr_char_int
      \reverse_if:N \if_int_compare:w #2 < \l_@@_curr_char_int
        \exp_after:wN \exp_after:wN \exp_after:wN \@@_break_true:w
      \fi:
    \fi:
    \@@_maybe_compute_ccc:
    \reverse_if:N \if_int_compare:w #1 > \l_@@_case_changed_char_int
      \reverse_if:N \if_int_compare:w #2 < \l_@@_case_changed_char_int
        \exp_after:wN \exp_after:wN \exp_after:wN \@@_break_true:w
      \fi:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compute_case_changed_char:}
%   This function is called when \cs{l_@@_case_changed_char_int} has
%   not yet been computed. If the current character code is in the range
%   $[65,90]$ (upper-case), then add $32$, making it lowercase. If it is
%   in the lower-case letter range $[97,122]$, subtract $32$.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compute_case_changed_char:
  {
    \int_set_eq:NN \l_@@_case_changed_char_int \l_@@_curr_char_int
    \if_int_compare:w \l_@@_curr_char_int > `Z \exp_stop_f:
      \if_int_compare:w \l_@@_curr_char_int > `z \exp_stop_f: \else:
        \if_int_compare:w \l_@@_curr_char_int < `a \exp_stop_f: \else:
          \int_sub:Nn \l_@@_case_changed_char_int
            { \c_@@_ascii_lower_int }
        \fi:
      \fi:
    \else:
      \if_int_compare:w \l_@@_curr_char_int < `A \exp_stop_f: \else:
        \int_add:Nn \l_@@_case_changed_char_int
          { \c_@@_ascii_lower_int }
      \fi:
    \fi:
    \cs_set_eq:NN \@@_maybe_compute_ccc: \prg_do_nothing:
  }
\cs_new_eq:NN \@@_maybe_compute_ccc: \@@_compute_case_changed_char:
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_item_equal:n, \@@_item_range:nn}
%   Those must always be defined to expand to a \texttt{caseful}
%   (default) or \texttt{caseless} version, and not be protected: they
%   must expand when compiling, to hard-code which tests are caseless or
%   caseful.
%    \begin{macrocode}
\cs_new_eq:NN \@@_item_equal:n ?
\cs_new_eq:NN \@@_item_range:nn ?
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_item_catcode:nT, \@@_item_catcode_reverse:nT}
% \begin{macro}{\@@_item_catcode:}
%   The argument is a sum of powers of $4$ with exponents given by the
%   allowed category codes (between $0$ and $13$). Dividing by a given
%   power of $4$ gives an odd result if and only if that category code
%   is allowed. If the catcode does not match, then skip the character
%   code tests which follow.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_item_catcode:
  {
    "
    \if_case:w \l_@@_curr_catcode_int
         1       \or: 4       \or: 10      \or: 40
    \or: 100     \or:         \or: 1000    \or: 4000
    \or: 10000   \or:         \or: 100000  \or: 400000
    \or: 1000000 \or: 4000000 \else: 1*0
    \fi:
  }
\cs_new_protected:Npn \@@_item_catcode:nT #1
  {
    \if_int_odd:w \int_eval:n { #1 / \@@_item_catcode: } \exp_stop_f:
      \exp_after:wN \use:n
    \else:
      \exp_after:wN \use_none:n
    \fi:
  }
\cs_new_protected:Npn \@@_item_catcode_reverse:nT #1#2
  { \@@_item_catcode:nT {#1} { \@@_item_reverse:n {#2} } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_item_exact:nn, \@@_item_exact_cs:n}
%   This matches an exact \meta{category}-\meta{character code} pair, or
%   an exact control sequence, more precisely one of several possible
%   control sequences, separated by \cs{scan_stop:}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_item_exact:nn #1#2
  {
    \if_int_compare:w #1 = \l_@@_curr_catcode_int
      \if_int_compare:w #2 = \l_@@_curr_char_int
        \exp_after:wN \exp_after:wN \exp_after:wN \@@_break_true:w
      \fi:
    \fi:
  }
\cs_new_protected:Npn \@@_item_exact_cs:n #1
  {
    \int_compare:nNnTF \l_@@_curr_catcode_int = 0
      {
        \__kernel_tl_set:Nx \l_@@_internal_a_tl
          { \scan_stop: \@@_curr_cs_to_str: \scan_stop: }
        \tl_if_in:noTF { \scan_stop: #1 \scan_stop: }
          \l_@@_internal_a_tl
          { \@@_break_true:w } { }
      }
      { }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_item_cs:n}
%   Match a control sequence (the argument is a compiled regex).
%   First test the catcode of the current token to be zero.
%   Then perform the matching test, and break if the csname
%   indeed matches.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_item_cs:n #1
  {
    \int_compare:nNnT \l_@@_curr_catcode_int = 0
      {
        \group_begin:
          \@@_single_match:
          \@@_disable_submatches:
          \@@_build_for_cs:n {#1}
          \bool_set_eq:NN \l_@@_saved_success_bool
            \g_@@_success_bool
          \exp_args:Nx \@@_match_cs:n { \@@_curr_cs_to_str: }
          \if_meaning:w \c_true_bool \g_@@_success_bool
            \group_insert_after:N \@@_break_true:w
          \fi:
          \bool_gset_eq:NN \g_@@_success_bool
            \l_@@_saved_success_bool
        \group_end:
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Character property tests}
%
% \begin{macro}
%   {
%     \@@_prop_d:, \@@_prop_h:, \@@_prop_s:,
%     \@@_prop_v:, \@@_prop_w:, \@@_prop_N:
%   }
%   Character property tests for |\d|, |\W|, \emph{etc.} These character
%   properties are not affected by the |(?i)| option. The characters
%   recognized by each one are as follows: |\d=[0-9]|,
%   |\w=[0-9A-Z_a-z]|, \verb*+\s=[\ \^^I\^^J\^^L\^^M]+,
%   \verb*+\h=[\ \^^I]+, |\v=[\^^J-\^^M]|, and the upper case
%   counterparts match anything that the lower case does not match.  The
%   order in which the various tests appear is optimized for usual
%   mostly lower case letter text.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_prop_d:
  { \@@_item_caseful_range:nn { `0 } { `9 } }
\cs_new_protected:Npn \@@_prop_h:
  {
    \@@_item_caseful_equal:n { `\ }
    \@@_item_caseful_equal:n { `\^^I }
  }
\cs_new_protected:Npn \@@_prop_s:
  {
    \@@_item_caseful_equal:n { `\ }
    \@@_item_caseful_equal:n { `\^^I }
    \@@_item_caseful_equal:n { `\^^J }
    \@@_item_caseful_equal:n { `\^^L }
    \@@_item_caseful_equal:n { `\^^M }
  }
\cs_new_protected:Npn \@@_prop_v:
  { \@@_item_caseful_range:nn { `\^^J } { `\^^M } } % lf, vtab, ff, cr
\cs_new_protected:Npn \@@_prop_w:
  {
    \@@_item_caseful_range:nn { `a } { `z }
    \@@_item_caseful_range:nn { `A } { `Z }
    \@@_item_caseful_range:nn { `0 } { `9 }
    \@@_item_caseful_equal:n { `_ }
  }
\cs_new_protected:Npn \@@_prop_N:
  {
    \@@_item_reverse:n
      { \@@_item_caseful_equal:n { `\^^J } }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_posix_alnum:, \@@_posix_alpha:, \@@_posix_ascii:,
%     \@@_posix_blank:, \@@_posix_cntrl:, \@@_posix_digit:,
%     \@@_posix_graph:, \@@_posix_lower:, \@@_posix_print:,
%     \@@_posix_punct:, \@@_posix_space:, \@@_posix_upper:,
%     \@@_posix_word: , \@@_posix_xdigit:
%   }
%   \textsc{posix} properties. No surprise.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_posix_alnum:
  { \@@_posix_alpha: \@@_posix_digit: }
\cs_new_protected:Npn \@@_posix_alpha:
  { \@@_posix_lower: \@@_posix_upper: }
\cs_new_protected:Npn \@@_posix_ascii:
  {
    \@@_item_caseful_range:nn
      \c_@@_ascii_min_int
      \c_@@_ascii_max_int
  }
\cs_new_eq:NN \@@_posix_blank: \@@_prop_h:
\cs_new_protected:Npn \@@_posix_cntrl:
  {
    \@@_item_caseful_range:nn
      \c_@@_ascii_min_int
      \c_@@_ascii_max_control_int
    \@@_item_caseful_equal:n \c_@@_ascii_max_int
  }
\cs_new_eq:NN \@@_posix_digit: \@@_prop_d:
\cs_new_protected:Npn \@@_posix_graph:
  { \@@_item_caseful_range:nn { `! } { `\~ } }
\cs_new_protected:Npn \@@_posix_lower:
  { \@@_item_caseful_range:nn { `a } { `z } }
\cs_new_protected:Npn \@@_posix_print:
  { \@@_item_caseful_range:nn { `\  } { `\~ } }
\cs_new_protected:Npn \@@_posix_punct:
  {
    \@@_item_caseful_range:nn { `! } { `/ }
    \@@_item_caseful_range:nn { `: } { `@ }
    \@@_item_caseful_range:nn { `[ } { `` }
    \@@_item_caseful_range:nn { `\{ } { `\~ }
  }
\cs_new_protected:Npn \@@_posix_space:
  {
    \@@_item_caseful_equal:n { `\  }
    \@@_item_caseful_range:nn { `\^^I } { `\^^M }
  }
\cs_new_protected:Npn \@@_posix_upper:
  { \@@_item_caseful_range:nn { `A } { `Z } }
\cs_new_eq:NN \@@_posix_word: \@@_prop_w:
\cs_new_protected:Npn \@@_posix_xdigit:
  {
    \@@_posix_digit:
    \@@_item_caseful_range:nn { `A } { `F }
    \@@_item_caseful_range:nn { `a } { `f }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Simple character escape}
%
% Before actually parsing the regular expression or the replacement
% text, we go through them once, converting |\n| to the character $10$,
% \emph{etc.} In this pass, we also convert any special character
% (\texttt{*}, \texttt{?}, \texttt{\{}, etc.) or escaped alphanumeric
% character into a marker indicating that this was a special sequence,
% and replace escaped special characters and non-escaped alphanumeric
% characters by markers indicating that those were \enquote{raw}
% characters. The rest of the code can then avoid caring about escaping
% issues (those can become quite complex to handle in combination with
% ranges in character classes).
%
% Usage: \cs{@@_escape_use:nnnn} \meta{inline~1} \meta{inline~2}
% \meta{inline~3} \Arg{token list} The \meta{token list} is converted to
% a string, then read from left to right, interpreting backslashes as
% escaping the next character.  Unescaped characters are fed to the
% function \meta{inline~1}, and escaped characters are fed to the function
% \meta{inline~2} within an \texttt{x}-expansion context (typically those
% functions perform some tests on their argument to decide how to output
% them).  The escape sequences |\a|, |\e|, |\f|, |\n|, |\r|, |\t| and
% |\x| are recognized, and those are replaced by the corresponding
% character, then fed to \meta{inline~3}. The result is then left in the
% input stream. Spaces are ignored unless escaped.
%
% The conversion is done within an \texttt{x}-expanding assignment.
%
% \begin{macro}{\@@_escape_use:nnnn}
%   The result is built in \cs{l_@@_internal_a_tl}, which is then left
%   in the input stream.  Tracing code is added as appropriate inside
%   this token list.  Go through |#4| once, applying |#1|,
%   |#2|, or |#3| as relevant to each character (after de-escaping
%   it).
%    \begin{macrocode}
\cs_new_protected:Npn \@@_escape_use:nnnn #1#2#3#4
  {
    \group_begin:
      \tl_clear:N \l_@@_internal_a_tl
      \cs_set:Npn \@@_escape_unescaped:N ##1 { #1 }
      \cs_set:Npn \@@_escape_escaped:N ##1 { #2 }
      \cs_set:Npn \@@_escape_raw:N ##1 { #3 }
      \@@_standard_escapechar:
      \__kernel_tl_gset:Nx \g_@@_internal_tl
        { \__kernel_str_to_other_fast:n {#4} }
      \tl_put_right:Nx \l_@@_internal_a_tl
        {
          \exp_after:wN \@@_escape_loop:N \g_@@_internal_tl
          \scan_stop: \prg_break_point:
        }
      \exp_after:wN
    \group_end:
    \l_@@_internal_a_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_escape_loop:N}
% \begin{macro}+\@@_escape_\:w+
%   \cs{@@_escape_loop:N} reads one character: if it is special
%   (space, backslash, or end-marker), perform the associated action,
%   otherwise it is simply an unescaped character. After a backslash,
%   the same is done, but unknown characters are \enquote{escaped}.
%    \begin{macrocode}
\cs_new:Npn \@@_escape_loop:N #1
  {
    \cs_if_exist_use:cF { @@_escape_\token_to_str:N #1:w }
      { \@@_escape_unescaped:N #1 }
    \@@_escape_loop:N
  }
\cs_new:cpn { @@_escape_ \c_backslash_str :w }
    \@@_escape_loop:N #1
  {
    \cs_if_exist_use:cF { @@_escape_/\token_to_str:N #1:w }
      { \@@_escape_escaped:N #1 }
    \@@_escape_loop:N
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}
%   {\@@_escape_unescaped:N, \@@_escape_escaped:N, \@@_escape_raw:N}
%   Those functions are never called before being given a new meaning,
%   so their definitions here don't matter.
%    \begin{macrocode}
\cs_new_eq:NN \@@_escape_unescaped:N ?
\cs_new_eq:NN \@@_escape_escaped:N   ?
\cs_new_eq:NN \@@_escape_raw:N       ?
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_escape_\scan_stop::w, \@@_escape_/\scan_stop::w,
%     \@@_escape_/a:w, \@@_escape_/e:w, \@@_escape_/f:w,
%     \@@_escape_/n:w, \@@_escape_/r:w, \@@_escape_/t:w
%   }
% \begin{macro}+\@@_escape_ :w+
%   The loop is ended upon seeing the end-marker
%   \enquote{\texttt{break}}, with an error if the string ended in a
%   backslash.  Spaces are ignored, and |\a|, |\e|, |\f|, |\n|, |\r|,
%   |\t| take their meaning here.
%    \begin{macrocode}
\cs_new_eq:cN { @@_escape_ \iow_char:N\\scan_stop: :w } \prg_break:
\cs_new:cpn { @@_escape_/ \iow_char:N\\scan_stop: :w }
  {
    \msg_expandable_error:nn { regex } { trailing-backslash }
    \prg_break:
  }
\cs_new:cpn { @@_escape_~:w } { }
\cs_new:cpx { @@_escape_/a:w }
  { \exp_not:N \@@_escape_raw:N \iow_char:N \^^G }
\cs_new:cpx { @@_escape_/t:w }
  { \exp_not:N \@@_escape_raw:N \iow_char:N \^^I }
\cs_new:cpx { @@_escape_/n:w }
  { \exp_not:N \@@_escape_raw:N \iow_char:N \^^J }
\cs_new:cpx { @@_escape_/f:w }
  { \exp_not:N \@@_escape_raw:N \iow_char:N \^^L }
\cs_new:cpx { @@_escape_/r:w }
  { \exp_not:N \@@_escape_raw:N \iow_char:N \^^M }
\cs_new:cpx { @@_escape_/e:w }
  { \exp_not:N \@@_escape_raw:N \iow_char:N \^^[ }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_escape_/x:w}
% \begin{macro}{\@@_escape_x_end:w, \@@_escape_x_large:n}
%   When |\x| is encountered, \cs{@@_escape_x_test:N} is responsible for
%   grabbing some hexadecimal digits, and feeding the result to
%   \cs{@@_escape_x_end:w}. If the number is too big interrupt the
%   assignment and produce an error, otherwise call \cs{@@_escape_raw:N}
%   on the corresponding character token.
%    \begin{macrocode}
\cs_new:cpn { @@_escape_/x:w } \@@_escape_loop:N
  {
    \exp_after:wN \@@_escape_x_end:w
    \int_value:w "0 \@@_escape_x_test:N
  }
\cs_new:Npn \@@_escape_x_end:w #1 ;
  {
    \int_compare:nNnTF {#1} > \c_max_char_int
      {
        \msg_expandable_error:nnff { regex } { x-overflow }
          {#1} { \int_to_Hex:n {#1} }
      }
      {
        \exp_last_unbraced:Nf \@@_escape_raw:N
          { \char_generate:nn {#1} { 12 } }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_escape_x_test:N, \@@_escape_x_testii:N}
%   Find out whether the first character is a left brace (allowing any
%   number of hexadecimal digits), or not (allowing up to two
%   hexadecimal digits). We need to check for the end-of-string marker.
%   Eventually, call either \cs{@@_escape_x_loop:N} or
%   \cs{@@_escape_x:N}.
%    \begin{macrocode}
\cs_new:Npn \@@_escape_x_test:N #1
  {
    \if_meaning:w \scan_stop: #1
      \exp_after:wN \use_i:nnn \exp_after:wN ;
    \fi:
    \use:n
      {
        \if_charcode:w \c_space_token #1
          \exp_after:wN \@@_escape_x_test:N
        \else:
          \exp_after:wN \@@_escape_x_testii:N
          \exp_after:wN #1
        \fi:
      }
  }
\cs_new:Npn \@@_escape_x_testii:N #1
  {
    \if_charcode:w \c_left_brace_str #1
      \exp_after:wN \@@_escape_x_loop:N
    \else:
      \@@_hexadecimal_use:NTF #1
        { \exp_after:wN \@@_escape_x:N }
        { ; \exp_after:wN \@@_escape_loop:N \exp_after:wN #1 }
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_escape_x:N}
%   This looks for the second digit in the unbraced case.
%    \begin{macrocode}
\cs_new:Npn \@@_escape_x:N #1
  {
    \if_meaning:w \scan_stop: #1
      \exp_after:wN \use_i:nnn \exp_after:wN ;
    \fi:
    \use:n
      {
        \@@_hexadecimal_use:NTF #1
          { ; \@@_escape_loop:N }
          { ; \@@_escape_loop:N #1 }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_escape_x_loop:N, \@@_escape_x_loop_error:}
%   Grab hexadecimal digits, skip spaces, and at the end, check that
%   there is a right brace, otherwise raise an error outside the
%   assignment.
%    \begin{macrocode}
\cs_new:Npn \@@_escape_x_loop:N #1
  {
    \if_meaning:w \scan_stop: #1
      \exp_after:wN \use_ii:nnn
    \fi:
    \use_ii:nn
      { ; \@@_escape_x_loop_error:n { } {#1} }
      {
        \@@_hexadecimal_use:NTF #1
          { \@@_escape_x_loop:N }
          {
            \token_if_eq_charcode:NNTF \c_space_token #1
              { \@@_escape_x_loop:N }
              {
                ;
                \exp_after:wN
                \token_if_eq_charcode:NNTF \c_right_brace_str #1
                  { \@@_escape_loop:N }
                  { \@@_escape_x_loop_error:n {#1} }
              }
          }
      }
  }
\cs_new:Npn \@@_escape_x_loop_error:n #1
  {
    \msg_expandable_error:nnn { regex } { x-missing-rbrace } {#1}
    \@@_escape_loop:N #1
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_hexadecimal_use:NTF}
%   \TeX{} detects uppercase hexadecimal digits for us but not the
%   lowercase letters, which we need to detect and replace by their
%   uppercase counterpart.
%    \begin{macrocode}
\prg_new_conditional:Npnn \@@_hexadecimal_use:N #1 { TF }
  {
    \if_int_compare:w 1 < "1 \token_to_str:N #1 \exp_stop_f:
      #1 \prg_return_true:
    \else:
      \if_case:w
        \int_eval:n { \exp_after:wN ` \token_to_str:N #1 - `a }
           A
      \or: B
      \or: C
      \or: D
      \or: E
      \or: F
      \else:
        \prg_return_false:
        \exp_after:wN \use_none:n
      \fi:
      \prg_return_true:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]
%   {\@@_char_if_alphanumeric:NTF, \@@_char_if_special:NTF}
%   These two tests are used in the first pass when parsing a regular
%   expression. That pass is responsible for finding escaped and
%   non-escaped characters, and recognizing which ones have special
%   meanings and which should be interpreted as \enquote{raw}
%   characters. Namely,
%   \begin{itemize}
%     \item alphanumerics are \enquote{raw} if they are not escaped, and
%       may have a special meaning when escaped;
%     \item non-alphanumeric printable ascii characters are
%       \enquote{raw} if they are escaped, and may have a special
%       meaning when not escaped;
%     \item characters other than printable ascii are always
%       \enquote{raw}.
%   \end{itemize}
%   The code is ugly, and highly based on magic numbers and the ascii
%   codes of characters. This is mostly unavoidable for performance
%   reasons.  Maybe the tests can be optimized a little bit more.
%   Here, \enquote{alphanumeric} means \texttt{0}--\texttt{9},
%   \texttt{A}--\texttt{Z}, \texttt{a}--\texttt{z};
%   \enquote{special} character means non-alphanumeric
%   but printable ascii, from space (hex \texttt{20}) to
%   \texttt{del} (hex \texttt{7E}).
%    \begin{macrocode}
\prg_new_conditional:Npnn \@@_char_if_special:N #1 { TF }
  {
    \if_int_compare:w `#1 > `Z \exp_stop_f:
      \if_int_compare:w `#1 > `z \exp_stop_f:
        \if_int_compare:w `#1 < \c_@@_ascii_max_int
          \prg_return_true: \else: \prg_return_false: \fi:
      \else:
        \if_int_compare:w `#1 < `a \exp_stop_f:
          \prg_return_true: \else: \prg_return_false: \fi:
      \fi:
    \else:
      \if_int_compare:w `#1 > `9 \exp_stop_f:
        \if_int_compare:w `#1 < `A \exp_stop_f:
          \prg_return_true: \else: \prg_return_false: \fi:
      \else:
        \if_int_compare:w `#1 < `0 \exp_stop_f:
          \if_int_compare:w `#1 < `\ \exp_stop_f:
            \prg_return_false: \else: \prg_return_true: \fi:
        \else: \prg_return_false: \fi:
      \fi:
    \fi:
  }
\prg_new_conditional:Npnn \@@_char_if_alphanumeric:N #1 { TF }
  {
    \if_int_compare:w `#1 > `Z \exp_stop_f:
      \if_int_compare:w `#1 > `z \exp_stop_f:
        \prg_return_false:
      \else:
        \if_int_compare:w `#1 < `a \exp_stop_f:
          \prg_return_false: \else: \prg_return_true: \fi:
      \fi:
    \else:
      \if_int_compare:w `#1 > `9 \exp_stop_f:
        \if_int_compare:w `#1 < `A \exp_stop_f:
          \prg_return_false: \else: \prg_return_true: \fi:
      \else:
        \if_int_compare:w `#1 < `0 \exp_stop_f:
          \prg_return_false: \else: \prg_return_true: \fi:
      \fi:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Compiling}
%
% A regular expression starts its life as a string of characters. In
% this section, we convert it to internal instructions, resulting in a
% \enquote{compiled} regular expression. This compiled expression is
% then turned into states of an automaton in the building
% phase. Compiled regular expressions consist of the following:
% \begin{itemize}
%   \item \cs{@@_class:NnnnN} \meta{boolean} \Arg{tests} \Arg{min}
%     \Arg{more} \meta{lazyness}
%   \item \cs{@@_group:nnnN} \Arg{branches} \Arg{min} \Arg{more}
%     \meta{lazyness}, also \cs{@@_group_no_capture:nnnN} and
%     \cs{@@_group_resetting:nnnN} with the same syntax.
%   \item \cs{@@_branch:n} \Arg{contents}
%   \item \cs{@@_command_K:}
%   \item \cs{@@_assertion:Nn} \meta{boolean} \Arg{assertion test},
%     where the \meta{assertion test} is \cs{@@_b_test:} or
%     \cs{@@_Z_test:} or \cs{@@_A_test:} or \cs{@@_G_test:}
% \end{itemize}
% Tests can be the following:
% \begin{itemize}
%   \item \cs{@@_item_caseful_equal:n} \Arg{char code}
%   \item \cs{@@_item_caseless_equal:n} \Arg{char code}
%   \item \cs{@@_item_caseful_range:nn} \Arg{min} \Arg{max}
%   \item \cs{@@_item_caseless_range:nn} \Arg{min} \Arg{max}
%   \item \cs{@@_item_catcode:nT} \Arg{catcode bitmap} \Arg{tests}
%   \item \cs{@@_item_catcode_reverse:nT} \Arg{catcode bitmap} \Arg{tests}
%   \item \cs{@@_item_reverse:n} \Arg{tests}
%   \item \cs{@@_item_exact:nn} \Arg{catcode} \Arg{char code}
%   \item \cs{@@_item_exact_cs:n} \Arg{csnames}, more precisely given as
%     \meta{csname} \cs{scan_stop:} \meta{csname} \cs{scan_stop:}
%     \meta{csname} and so on in a brace group.
%   \item \cs{@@_item_cs:n} \Arg{compiled regex}
% \end{itemize}
%
% \subsubsection{Variables used when compiling}
%
% \begin{variable}{\l_@@_group_level_int}
%   We make sure to open the same number of groups as we close.
%    \begin{macrocode}
\int_new:N \l_@@_group_level_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_mode_int}
% \begin{variable}
%   {
%     \c_@@_cs_in_class_mode_int,
%     \c_@@_cs_mode_int,
%     \c_@@_outer_mode_int,
%     \c_@@_catcode_mode_int,
%     \c_@@_class_mode_int,
%     \c_@@_catcode_in_class_mode_int
%   }
%   While compiling, ten modes are recognized, labelled $-63$, $-23$,
%   $-6$, $-2$, $0$, $2$, $3$, $6$, $23$, $63$. See
%   section~\ref{sec:regex-modes}.  We only define some of these as
%   constants.
%    \begin{macrocode}
\int_new:N \l_@@_mode_int
\int_const:Nn \c_@@_cs_in_class_mode_int { -6 }
\int_const:Nn \c_@@_cs_mode_int { -2 }
\int_const:Nn \c_@@_outer_mode_int { 0 }
\int_const:Nn \c_@@_catcode_mode_int { 2 }
\int_const:Nn \c_@@_class_mode_int { 3 }
\int_const:Nn \c_@@_catcode_in_class_mode_int { 6 }
%    \end{macrocode}
% \end{variable}
% \end{variable}
%
% \begin{variable}{\l_@@_catcodes_int, \l_@@_default_catcodes_int}
% \begin{variable}{\l_@@_catcodes_bool}
%   We wish to allow constructions such as |\c[^BE](..\cL[a-z]..)|,
%   where the outer catcode test applies to the whole group, but is
%   superseded by the inner catcode test. For this to work, we need to
%   keep track of lists of allowed category codes:
%   \cs{l_@@_catcodes_int} and \cs{l_@@_default_catcodes_int} are
%   bitmaps, sums of $4^c$, for all allowed catcodes $c$. The latter is
%   local to each capturing group, and we reset
%   \cs{l_@@_catcodes_int} to that value after each character or
%   class, changing it only when encountering a |\c| escape. The boolean
%   records whether the list of categories of a catcode test has to be
%   inverted: compare |\c[^BE]| and |\c[BE]|.
%    \begin{macrocode}
\int_new:N \l_@@_catcodes_int
\int_new:N \l_@@_default_catcodes_int
\bool_new:N \l_@@_catcodes_bool
%    \end{macrocode}
% \end{variable}
% \end{variable}
%
% \begin{variable}
%   {
%     \c_@@_catcode_C_int, \c_@@_catcode_B_int, \c_@@_catcode_E_int,
%     \c_@@_catcode_M_int, \c_@@_catcode_T_int, \c_@@_catcode_P_int,
%     \c_@@_catcode_U_int, \c_@@_catcode_D_int, \c_@@_catcode_S_int,
%     \c_@@_catcode_L_int, \c_@@_catcode_O_int, \c_@@_catcode_A_int
%   }
% \begin{variable}{\c_@@_all_catcodes_int}
%   Constants: $4^c$ for each category, and the sum of all powers of $4$.
%    \begin{macrocode}
\int_const:Nn \c_@@_catcode_C_int { "1 }
\int_const:Nn \c_@@_catcode_B_int { "4 }
\int_const:Nn \c_@@_catcode_E_int { "10 }
\int_const:Nn \c_@@_catcode_M_int { "40 }
\int_const:Nn \c_@@_catcode_T_int { "100 }
\int_const:Nn \c_@@_catcode_P_int { "1000 }
\int_const:Nn \c_@@_catcode_U_int { "4000 }
\int_const:Nn \c_@@_catcode_D_int { "10000 }
\int_const:Nn \c_@@_catcode_S_int { "100000 }
\int_const:Nn \c_@@_catcode_L_int { "400000 }
\int_const:Nn \c_@@_catcode_O_int { "1000000 }
\int_const:Nn \c_@@_catcode_A_int { "4000000 }
\int_const:Nn \c_@@_all_catcodes_int { "5515155 }
%    \end{macrocode}
% \end{variable}
% \end{variable}
%
% \begin{variable}{\l_@@_internal_regex}
%   The compilation step stores its result in this variable.
%    \begin{macrocode}
\cs_new_eq:NN \l_@@_internal_regex \c_@@_no_match_regex
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_show_prefix_seq}
%   This sequence holds the prefix that makes up the line displayed to
%   the user. The various items must be removed from the right, which is
%   tricky with a token list, hence we use a sequence.
%    \begin{macrocode}
\seq_new:N \l_@@_show_prefix_seq
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_show_lines_int}
%   A hack. To know whether a given class has a single item in it or
%   not, we count the number of lines when showing the class.
%    \begin{macrocode}
\int_new:N \l_@@_show_lines_int
%    \end{macrocode}
% \end{variable}
%
% \subsubsection{Generic helpers used when compiling}
%
% \begin{macro}{\@@_two_if_eq:NNNNTF}
%   Used to compare pairs of things like \cs{@@_compile_special:N} |?|
%   together.  It's often inconvenient to get the catcodes of the
%   character to match so we just compare the character code.
%   Besides, the expanding behaviour of \cs{if:w} is very useful as that
%   means we can use \cs{c_left_brace_str} and the like.
%    \begin{macrocode}
\prg_new_conditional:Npnn \@@_two_if_eq:NNNN #1#2#3#4 { TF }
  {
    \if_meaning:w #1 #3
      \if:w #2 #4
        \prg_return_true:
      \else:
        \prg_return_false:
      \fi:
    \else:
      \prg_return_false:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_get_digits:NTFw}
% \begin{macro}[rEXP]{\@@_get_digits_loop:w}
%   If followed by some raw digits, collect them one by one in the
%   integer variable |#1|, and take the \texttt{true} branch. Otherwise,
%   take the \texttt{false} branch.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_get_digits:NTFw #1#2#3#4#5
  {
    \@@_if_raw_digit:NNTF #4 #5
      { #1 = #5 \@@_get_digits_loop:nw {#2} }
      { #3 #4 #5 }
  }
\cs_new:Npn \@@_get_digits_loop:nw #1#2#3
  {
    \@@_if_raw_digit:NNTF #2 #3
      { #3 \@@_get_digits_loop:nw {#1} }
      { \scan_stop: #1 #2 #3 }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_if_raw_digit:NNTF}
%   Test used when grabbing digits for the |{m,n}| quantifier.
%   It only accepts non-escaped digits.
%    \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_raw_digit:NN #1#2 { TF }
  {
    \if_meaning:w \@@_compile_raw:N #1
      \if_int_compare:w 1 < 1 #2 \exp_stop_f:
        \prg_return_true:
      \else:
        \prg_return_false:
      \fi:
    \else:
      \prg_return_false:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Mode}
% \label{sec:regex-modes}
%
% When compiling the \textsc{nfa} corresponding to a given regex string,
% we can be in ten distinct modes, which we label by some magic numbers:
% \begin{itemize}
%   \item[-6] |[\c{...}]| control sequence in a class,
%   \item[-2] |\c{...}| control sequence,
%   \item[0] |...| outer,
%   \item[2] |\c...| catcode test,
%   \item[6] |[\c...]| catcode test in a class,
%   \item[-63] |[\c{[...]}]| class inside mode $-6$,
%   \item[-23] |\c{[...]}| class inside mode $-2$,
%   \item[3] |[...]| class inside mode $0$,
%   \item[23] |\c[...]| class inside mode $2$,
%   \item[63] |[\c[...]]| class inside mode $6$.
% \end{itemize}
% This list is exhaustive, because |\c| escape sequences cannot be
% nested, and character classes cannot be nested directly. The choice of
% numbers is such as to optimize the most useful tests, and make
% transitions from one mode to another as simple as possible.
% \begin{itemize}
%   \item Even modes mean that we are not directly in a character class.
%     In this case, a left bracket appends $3$ to the mode. In a
%     character class, a right bracket changes the mode as $m\to
%     (m-15)/13$, truncated.
%   \item Grouping, assertion, and anchors are allowed in non-positive
%     even modes ($0$, $-2$, $-6$), and do not change the
%     mode. Otherwise, they trigger an error.
%   \item A left bracket is special in even modes, appending $3$ to the
%     mode; in those modes, quantifiers and the dot are recognized, and
%     the right bracket is normal. In odd modes (within classes), the
%     left bracket is normal, but the right bracket ends the class,
%     changing the mode from $m$ to $(m-15)/13$, truncated; also, ranges
%     are recognized.
%   \item In non-negative modes, left and right braces are normal. In
%     negative modes, however, left braces trigger a warning; right
%     braces end the control sequence, going from $-2$ to $0$ or $-6$ to
%     $3$, with error recovery for odd modes.
%   \item Properties (such as the |\d| character class) can appear in
%     any mode.
% \end{itemize}
%
% \begin{macro}[EXP]{\@@_if_in_class:TF}
%   Test whether we are directly in a character class (at the innermost
%   level of nesting). There, many escape sequences are not recognized,
%   and special characters are normal. Also, for every raw character, we
%   must look ahead for a possible raw dash.
%   \begin{macrocode}
\cs_new:Npn \@@_if_in_class:TF
  {
    \if_int_odd:w \l_@@_mode_int
      \exp_after:wN \use_i:nn
    \else:
      \exp_after:wN \use_ii:nn
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_if_in_cs:TF}
%   Right braces are special only directly inside control sequences (at
%   the inner-most level of nesting, not counting groups).
%    \begin{macrocode}
\cs_new:Npn \@@_if_in_cs:TF
  {
    \if_int_odd:w \l_@@_mode_int
      \exp_after:wN \use_ii:nn
    \else:
      \if_int_compare:w \l_@@_mode_int < \c_@@_outer_mode_int
        \exp_after:wN \exp_after:wN \exp_after:wN \use_i:nn
      \else:
        \exp_after:wN \exp_after:wN \exp_after:wN \use_ii:nn
      \fi:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_if_in_class_or_catcode:TF}
%   Assertions are only allowed in modes $0$, $-2$, and $-6$,
%   \emph{i.e.}, even, non-positive modes.
%    \begin{macrocode}
\cs_new:Npn \@@_if_in_class_or_catcode:TF
  {
    \if_int_odd:w \l_@@_mode_int
      \exp_after:wN \use_i:nn
    \else:
      \if_int_compare:w \l_@@_mode_int > \c_@@_outer_mode_int
        \exp_after:wN \exp_after:wN \exp_after:wN \use_i:nn
      \else:
        \exp_after:wN \exp_after:wN \exp_after:wN \use_ii:nn
      \fi:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_if_within_catcode:TF}
%   This test takes the true branch if we are in a catcode test, either
%   immediately following it (modes $2$ and $6$) or in a class on which
%   it applies (modes $23$ and $63$). This is used to tweak how left
%   brackets behave in modes $2$ and $6$.
%    \begin{macrocode}
\cs_new:Npn \@@_if_within_catcode:TF
  {
    \if_int_compare:w \l_@@_mode_int > \c_@@_outer_mode_int
      \exp_after:wN \use_i:nn
    \else:
      \exp_after:wN \use_ii:nn
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_chk_c_allowed:T}
%   The |\c| escape sequence is only allowed in modes $0$ and $3$,
%   \emph{i.e.}, not within any other |\c| escape sequence.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_chk_c_allowed:T
  {
    \if_int_compare:w \l_@@_mode_int = \c_@@_outer_mode_int
      \exp_after:wN \use:n
    \else:
      \if_int_compare:w \l_@@_mode_int = \c_@@_class_mode_int
        \exp_after:wN \exp_after:wN \exp_after:wN \use:n
      \else:
        \msg_error:nn { regex } { c-bad-mode }
        \exp_after:wN \exp_after:wN \exp_after:wN \use_none:n
      \fi:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_mode_quit_c:}
%   This function changes the mode as it is needed just after a catcode
%   test.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_mode_quit_c:
  {
    \if_int_compare:w \l_@@_mode_int = \c_@@_catcode_mode_int
      \int_set_eq:NN \l_@@_mode_int \c_@@_outer_mode_int
    \else:
      \if_int_compare:w \l_@@_mode_int =
        \c_@@_catcode_in_class_mode_int
        \int_set_eq:NN \l_@@_mode_int \c_@@_class_mode_int
      \fi:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Framework}
%
% \begin{macro}{\@@_compile:w, \@@_compile_end:}
%   Used when compiling a user regex or a regex for the |\c{...}| escape
%   sequence within another regex. Start building a token list within a
%   group (with \texttt{x}-expansion at the outset), and set a few
%   variables (group level, catcodes), then start the first branch. At
%   the end, make sure there are no dangling classes nor groups, close
%   the last branch: we are done building \cs{l_@@_internal_regex}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile:w
  {
    \group_begin:
      \tl_build_begin:N \l_@@_build_tl
      \int_zero:N \l_@@_group_level_int
      \int_set_eq:NN \l_@@_default_catcodes_int
        \c_@@_all_catcodes_int
      \int_set_eq:NN \l_@@_catcodes_int \l_@@_default_catcodes_int
      \cs_set:Npn \@@_item_equal:n  { \@@_item_caseful_equal:n }
      \cs_set:Npn \@@_item_range:nn { \@@_item_caseful_range:nn }
      \tl_build_put_right:Nn \l_@@_build_tl
        { \@@_branch:n { \if_false: } \fi: }
  }
\cs_new_protected:Npn \@@_compile_end:
  {
      \@@_if_in_class:TF
        {
          \msg_error:nn { regex } { missing-rbrack }
          \use:c { @@_compile_]: }
          \prg_do_nothing: \prg_do_nothing:
        }
        { }
      \if_int_compare:w \l_@@_group_level_int > \c_zero_int
        \msg_error:nnx { regex } { missing-rparen }
          { \int_use:N \l_@@_group_level_int }
        \prg_replicate:nn
          { \l_@@_group_level_int }
          {
              \tl_build_put_right:Nn \l_@@_build_tl
                {
                  \if_false: { \fi: }
                  \if_false: { \fi: } { 1 } { 0 } \c_true_bool
                }
              \tl_build_end:N \l_@@_build_tl
              \exp_args:NNNo
            \group_end:
            \tl_build_put_right:Nn \l_@@_build_tl
              { \l_@@_build_tl }
          }
      \fi:
      \tl_build_put_right:Nn \l_@@_build_tl { \if_false: { \fi: } }
      \tl_build_end:N \l_@@_build_tl
      \exp_args:NNNx
    \group_end:
    \tl_set:Nn \l_@@_internal_regex { \l_@@_build_tl }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile:n}
%   The compilation is done between \cs{@@_compile:w} and
%   \cs{@@_compile_end:}, starting in mode~$0$. Then
%   \cs{@@_escape_use:nnnn} distinguishes special characters, escaped
%   alphanumerics, and raw characters, interpreting |\a|, |\x| and other
%   sequences. The $4$ trailing \cs{prg_do_nothing:} are needed because
%   some functions defined later look up to $4$ tokens ahead. Before
%   ending, make sure that any |\c{...}| is properly closed.  No need to
%   check that brackets are closed properly since \cs{@@_compile_end:}
%   does that.  However, catch the case of a trailing |\cL|
%   construction.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile:n #1
  {
    \@@_compile:w
      \@@_standard_escapechar:
      \int_set_eq:NN \l_@@_mode_int \c_@@_outer_mode_int
      \@@_escape_use:nnnn
        {
          \@@_char_if_special:NTF ##1
            \@@_compile_special:N \@@_compile_raw:N ##1
        }
        {
          \@@_char_if_alphanumeric:NTF ##1
            \@@_compile_escaped:N \@@_compile_raw:N ##1
        }
        { \@@_compile_raw:N ##1 }
        { #1 }
      \prg_do_nothing: \prg_do_nothing:
      \prg_do_nothing: \prg_do_nothing:
      \int_compare:nNnT \l_@@_mode_int = \c_@@_catcode_mode_int
        { \msg_error:nn { regex } { c-trailing } }
      \int_compare:nNnT \l_@@_mode_int < \c_@@_outer_mode_int
        {
          \msg_error:nn { regex } { c-missing-rbrace }
          \@@_compile_end_cs:
          \prg_do_nothing: \prg_do_nothing:
          \prg_do_nothing: \prg_do_nothing:
        }
    \@@_compile_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_escaped:N, \@@_compile_special:N}
%   If the special character or escaped alphanumeric has a particular
%   meaning in regexes, the corresponding function is used. Otherwise,
%   it is interpreted as a raw character. We distinguish special
%   characters from escaped alphanumeric characters because they behave
%   differently when appearing as an end-point of a range.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_special:N #1
  {
    \cs_if_exist_use:cF { @@_compile_#1: }
      { \@@_compile_raw:N #1 }
  }
\cs_new_protected:Npn \@@_compile_escaped:N #1
  {
    \cs_if_exist_use:cF { @@_compile_/#1: }
      { \@@_compile_raw:N #1 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_one:n}
%   This is used after finding one \enquote{test}, such as |\d|, or a
%   raw character. If that followed a catcode test (\emph{e.g.}, |\cL|),
%   then restore the mode. If we are not in a class, then the test is
%   \enquote{standalone}, and we need to add \cs{@@_class:NnnnN} and
%   search for quantifiers. In any case, insert the test, possibly
%   together with a catcode test if appropriate.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_one:n #1
  {
    \@@_mode_quit_c:
    \@@_if_in_class:TF { }
      {
        \tl_build_put_right:Nn \l_@@_build_tl
          { \@@_class:NnnnN \c_true_bool { \if_false: } \fi: }
      }
    \tl_build_put_right:Nx \l_@@_build_tl
      {
        \if_int_compare:w \l_@@_catcodes_int <
          \c_@@_all_catcodes_int
          \@@_item_catcode:nT { \int_use:N \l_@@_catcodes_int }
            { \exp_not:N \exp_not:n {#1} }
        \else:
          \exp_not:N \exp_not:n {#1}
        \fi:
      }
    \int_set_eq:NN \l_@@_catcodes_int \l_@@_default_catcodes_int
    \@@_if_in_class:TF { } { \@@_compile_quantifier:w }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {\@@_compile_abort_tokens:n, \@@_compile_abort_tokens:x}
%   This function places the collected tokens back in the input stream,
%   each as a raw character. Spaces are not preserved.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_abort_tokens:n #1
  {
    \use:x
      {
        \exp_args:No \tl_map_function:nN { \tl_to_str:n {#1} }
          \@@_compile_raw:N
      }
  }
\cs_generate_variant:Nn \@@_compile_abort_tokens:n { x }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Quantifiers}
%
% \begin{macro}{\@@_compile_if_quantifier:TFw}
%   This looks ahead and checks whether there are any quantifier
%   (special character equal to either of \texttt{?+*\{}).  This is
%   useful for the |\u| and |\ur| escape sequences.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_if_quantifier:TFw #1#2#3#4
  {
    \token_if_eq_meaning:NNTF #3 \@@_compile_special:N
      { \cs_if_exist:cTF { @@_compile_quantifier_#4:w } }
      { \use_ii:nn }
    {#1} {#2} #3 #4
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_quantifier:w}
%   This looks ahead and finds any quantifier (special character equal
%   to either of \texttt{?+*\{}).
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_quantifier:w #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_compile_special:N
      {
        \cs_if_exist_use:cF { @@_compile_quantifier_#2:w }
          { \@@_compile_quantifier_none: #1 #2 }
      }
      { \@@_compile_quantifier_none: #1 #2 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_quantifier_none:}
% \begin{macro}{\@@_compile_quantifier_abort:xNN}
%   Those functions are called whenever there is no quantifier, or a
%   braced construction is invalid (equivalent to no quantifier, and
%   whatever characters were grabbed are left raw).
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_quantifier_none:
  {
    \tl_build_put_right:Nn \l_@@_build_tl
      { \if_false: { \fi: } { 1 } { 0 } \c_false_bool }
  }
\cs_new_protected:Npn \@@_compile_quantifier_abort:xNN #1#2#3
  {
    \@@_compile_quantifier_none:
    \msg_warning:nnxx { regex } { invalid-quantifier } {#1} {#3}
    \@@_compile_abort_tokens:x {#1}
    #2 #3
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_compile_quantifier_lazyness:nnNN}
%   Once the \enquote{main} quantifier (\texttt{?}, \texttt{*},
%   \texttt{+} or a braced construction) is found, we check whether it
%   is lazy (followed by a question mark). We then add to the compiled
%   regex a closing brace (ending \cs{@@_class:NnnnN} and friends),
%   the start-point of the range, its end-point, and a boolean,
%   \texttt{true} for lazy and \texttt{false} for greedy operators.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_quantifier_lazyness:nnNN #1#2#3#4
  {
    \@@_two_if_eq:NNNNTF #3 #4 \@@_compile_special:N ?
      {
        \tl_build_put_right:Nn \l_@@_build_tl
          { \if_false: { \fi: } { #1 } { #2 } \c_true_bool }
      }
      {
        \tl_build_put_right:Nn \l_@@_build_tl
          { \if_false: { \fi: } { #1 } { #2 } \c_false_bool }
        #3 #4
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_compile_quantifier_?:w,
%     \@@_compile_quantifier_*:w,
%     \@@_compile_quantifier_+:w
%   }
%   For each \enquote{basic} quantifier, |?|, |*|, |+|, feed the correct
%   arguments to \cs{@@_compile_quantifier_lazyness:nnNN}, $-1$ means
%   that there is no upper bound on the number of repetitions.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_quantifier_?:w }
  { \@@_compile_quantifier_lazyness:nnNN { 0 } { 1 } }
\cs_new_protected:cpn { @@_compile_quantifier_*:w }
  { \@@_compile_quantifier_lazyness:nnNN { 0 } { -1 } }
\cs_new_protected:cpn { @@_compile_quantifier_+:w }
  { \@@_compile_quantifier_lazyness:nnNN { 1 } { -1 } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}+\@@_compile_quantifier_{:w+ ^^A}
% \begin{macro}
%   {
%     \@@_compile_quantifier_braced_auxi:w,
%     \@@_compile_quantifier_braced_auxii:w,
%     \@@_compile_quantifier_braced_auxiii:w,
%   }
%   Three possible syntaxes: \texttt{\{\meta{int}\}},
%   \texttt{\{\meta{int},\}}, or \texttt{\{\meta{int},\meta{int}\}}. Any
%   other syntax causes us to abort and put whatever we collected back
%   in the input stream, as \texttt{raw} characters, including the
%   opening brace. Grab a number into \cs{l_@@_internal_a_int}. If
%   the number is followed by a right brace, the range is $[a,a]$. If
%   followed by a comma, grab one more number, and call the \texttt{_ii}
%   or \texttt{_iii} auxiliary. Those auxiliaries check for a closing
%   brace, leading to the range $[a,\infty]$ or $[a,b]$, encoded as
%   $\{a\}\{-1\}$ and $\{a\}\{b-a\}$.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_quantifier_ \c_left_brace_str :w }
  {
    \@@_get_digits:NTFw \l_@@_internal_a_int
      { \@@_compile_quantifier_braced_auxi:w }
      { \@@_compile_quantifier_abort:xNN { \c_left_brace_str } }
  }
\cs_new_protected:Npn \@@_compile_quantifier_braced_auxi:w #1#2
  {
    \str_case_e:nnF { #1 #2 }
      {
        { \@@_compile_special:N \c_right_brace_str }
          {
            \exp_args:No \@@_compile_quantifier_lazyness:nnNN
              { \int_use:N \l_@@_internal_a_int } { 0 }
          }
        { \@@_compile_special:N , }
          {
            \@@_get_digits:NTFw \l_@@_internal_b_int
              { \@@_compile_quantifier_braced_auxiii:w }
              { \@@_compile_quantifier_braced_auxii:w }
          }
      }
      {
        \@@_compile_quantifier_abort:xNN
          { \c_left_brace_str \int_use:N \l_@@_internal_a_int }
        #1 #2
      }
  }
\cs_new_protected:Npn \@@_compile_quantifier_braced_auxii:w #1#2
  {
    \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N \c_right_brace_str
      {
        \exp_args:No \@@_compile_quantifier_lazyness:nnNN
          { \int_use:N \l_@@_internal_a_int } { -1 }
      }
      {
        \@@_compile_quantifier_abort:xNN
          { \c_left_brace_str \int_use:N \l_@@_internal_a_int , }
        #1 #2
      }
  }
\cs_new_protected:Npn \@@_compile_quantifier_braced_auxiii:w #1#2
  {
    \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N \c_right_brace_str
      {
        \if_int_compare:w \l_@@_internal_a_int >
          \l_@@_internal_b_int
          \msg_error:nnxx { regex } { backwards-quantifier }
            { \int_use:N \l_@@_internal_a_int }
            { \int_use:N \l_@@_internal_b_int }
          \int_zero:N \l_@@_internal_b_int
        \else:
          \int_sub:Nn \l_@@_internal_b_int \l_@@_internal_a_int
        \fi:
        \exp_args:Noo \@@_compile_quantifier_lazyness:nnNN
          { \int_use:N \l_@@_internal_a_int }
          { \int_use:N \l_@@_internal_b_int }
      }
      {
        \@@_compile_quantifier_abort:xNN
          {
            \c_left_brace_str
            \int_use:N \l_@@_internal_a_int ,
            \int_use:N \l_@@_internal_b_int
          }
        #1 #2
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Raw characters}
%
% \begin{macro}{\@@_compile_raw_error:N}
%   Within character classes, and following catcode tests, some escaped
%   alphanumeric sequences such as |\b| do not have any meaning. They
%   are replaced by a raw character, after spitting out an error.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_raw_error:N #1
  {
    \msg_error:nnx { regex } { bad-escape } {#1}
    \@@_compile_raw:N #1
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_raw:N}
%   If we are in a character class and the next character is an
%   unescaped dash, this denotes a range. Otherwise, the current
%   character |#1| matches itself.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_raw:N #1#2#3
  {
    \@@_if_in_class:TF
      {
        \@@_two_if_eq:NNNNTF #2 #3 \@@_compile_special:N -
          { \@@_compile_range:Nw #1 }
          {
            \@@_compile_one:n
              { \@@_item_equal:n { \int_value:w `#1 } }
            #2 #3
          }
      }
      {
        \@@_compile_one:n
          { \@@_item_equal:n { \int_value:w `#1 } }
        #2 #3
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_range:Nw, \@@_if_end_range:NNTF}
%   We have just read a raw character followed by a dash; this should be
%   followed by an end-point for the range. Valid end-points are: any
%   raw character; any special character, except a right bracket. In
%   particular, escaped characters are forbidden.
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \@@_if_end_range:NN #1#2 { TF }
  {
    \if_meaning:w \@@_compile_raw:N #1
      \prg_return_true:
    \else:
      \if_meaning:w \@@_compile_special:N #1
        \if_charcode:w ] #2
          \prg_return_false:
        \else:
          \prg_return_true:
        \fi:
      \else:
        \prg_return_false:
      \fi:
    \fi:
  }
\cs_new_protected:Npn \@@_compile_range:Nw #1#2#3
  {
    \@@_if_end_range:NNTF #2 #3
      {
        \if_int_compare:w `#1 > `#3 \exp_stop_f:
          \msg_error:nnxx { regex } { range-backwards } {#1} {#3}
        \else:
          \tl_build_put_right:Nx \l_@@_build_tl
            {
              \if_int_compare:w `#1 = `#3 \exp_stop_f:
                \@@_item_equal:n
              \else:
                \@@_item_range:nn { \int_value:w `#1 }
              \fi:
              { \int_value:w `#3 }
            }
        \fi:
      }
      {
        \msg_warning:nnxx { regex } { range-missing-end }
          {#1} { \c_backslash_str #3 }
        \tl_build_put_right:Nx \l_@@_build_tl
          {
            \@@_item_equal:n { \int_value:w `#1 \exp_stop_f: }
            \@@_item_equal:n { \int_value:w `- \exp_stop_f: }
          }
        #2#3
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Character properties}
%
% \begin{macro}{\@@_compile_.:, \@@_prop_.:}
%   In a class, the dot has no special meaning. Outside, insert
%   \cs{@@_prop_.:}, which matches any character or control
%   sequence, and refuses $-2$ (end-marker).
%    \begin{macrocode}
\cs_new_protected:cpx { @@_compile_.: }
  {
    \exp_not:N \@@_if_in_class:TF
      { \@@_compile_raw:N . }
      { \@@_compile_one:n \exp_not:c { @@_prop_.: } }
  }
\cs_new_protected:cpn { @@_prop_.: }
  {
    \if_int_compare:w \l_@@_curr_char_int > - 2 \exp_stop_f:
      \exp_after:wN \@@_break_true:w
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_compile_/d:, \@@_compile_/D:,
%     \@@_compile_/h:, \@@_compile_/H:,
%     \@@_compile_/s:, \@@_compile_/S:,
%     \@@_compile_/v:, \@@_compile_/V:,
%     \@@_compile_/w:, \@@_compile_/W:,
%     \@@_compile_/N:,
%   }
%   The constants \cs{@@_prop_d:}, \emph{etc.} hold
%   a list of tests which match the corresponding character
%   class, and jump to the \cs{@@_break_point:TF} marker.
%   As for a normal character, we check for quantifiers.
%    \begin{macrocode}
\cs_set_protected:Npn \@@_tmp:w #1#2
  {
    \cs_new_protected:cpx { @@_compile_/#1: }
      { \@@_compile_one:n \exp_not:c { @@_prop_#1: } }
    \cs_new_protected:cpx { @@_compile_/#2: }
      {
        \@@_compile_one:n
          { \@@_item_reverse:n \exp_not:c { @@_prop_#1: } }
      }
  }
\@@_tmp:w d D
\@@_tmp:w h H
\@@_tmp:w s S
\@@_tmp:w v V
\@@_tmp:w w W
\cs_new_protected:cpn { @@_compile_/N: }
  { \@@_compile_one:n \@@_prop_N: }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Anchoring and simple assertions}
%
% \begin{macro}{\@@_compile_anchor_letter:NNN}
% \begin{macro}{\@@_compile_/A:, \@@_compile_/G:, \@@_compile_/Z:, \@@_compile_/z:, \@@_compile_/b:, \@@_compile_/B:}
% \begin{macro}+\@@_compile_^:+
% \begin{macro}+\@@_compile_$:+
%   In modes where assertions are forbidden, anchors such as |\A|
%   produce an error (|\A|~is invalid in classes); otherwise they add an
%   \cs{@@_assertion:Nn} test as appropriate (the only negative
%   assertion is~|\B|).  The test functions are defined later.  The
%   implementation for
%   |$| and |^| is only different from |\A| etc because these are valid
%   in a class.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_anchor_letter:NNN #1#2#3
  {
    \@@_if_in_class_or_catcode:TF { \@@_compile_raw_error:N #1 }
      {
        \tl_build_put_right:Nn \l_@@_build_tl
          { \@@_assertion:Nn #2 {#3} }
      }
  }
\cs_new_protected:cpn { @@_compile_/A: }
  { \@@_compile_anchor_letter:NNN A \c_true_bool \@@_A_test: }
\cs_new_protected:cpn { @@_compile_/G: }
  { \@@_compile_anchor_letter:NNN G \c_true_bool \@@_G_test: }
\cs_new_protected:cpn { @@_compile_/Z: }
  { \@@_compile_anchor_letter:NNN Z \c_true_bool \@@_Z_test: }
\cs_new_protected:cpn { @@_compile_/z: }
  { \@@_compile_anchor_letter:NNN z \c_true_bool \@@_Z_test: }
\cs_new_protected:cpn { @@_compile_/b: }
  { \@@_compile_anchor_letter:NNN b \c_true_bool \@@_b_test: }
\cs_new_protected:cpn { @@_compile_/B: }
  { \@@_compile_anchor_letter:NNN B \c_false_bool \@@_b_test: }
\cs_set_protected:Npn \@@_tmp:w #1#2
  {
    \cs_new_protected:cpn { @@_compile_#1: }
      {
        \@@_if_in_class_or_catcode:TF { \@@_compile_raw:N #1 }
          {
            \tl_build_put_right:Nn \l_@@_build_tl
              { \@@_assertion:Nn \c_true_bool {#2} }
          }
      }
  }
\exp_args:Nx \@@_tmp:w { \iow_char:N \^ } { \@@_A_test: }
\exp_args:Nx \@@_tmp:w { \iow_char:N \$ } { \@@_Z_test: }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Character classes}
%
% \begin{macro}{\@@_compile_]:}
%   Outside a class, right brackets have no meaning. In a class, change
%   the mode ($m\to (m-15)/13$, truncated) to reflect the fact that we
%   are leaving the class. Look for quantifiers, unless we are still in
%   a class after leaving one (the case of |[...\cL[...]...]|).
%   quantifiers.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_]: }
  {
    \@@_if_in_class:TF
      {
        \if_int_compare:w \l_@@_mode_int >
          \c_@@_catcode_in_class_mode_int
          \tl_build_put_right:Nn \l_@@_build_tl { \if_false: { \fi: } }
        \fi:
        \tex_advance:D \l_@@_mode_int - 15 \exp_stop_f:
        \tex_divide:D \l_@@_mode_int 13 \exp_stop_f:
        \if_int_odd:w \l_@@_mode_int \else:
          \exp_after:wN \@@_compile_quantifier:w
        \fi:
      }
      { \@@_compile_raw:N ] }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_[:}
%   In a class, left brackets might introduce a \textsc{posix} character
%   class, or mean nothing. Immediately following |\c|\meta{category},
%   we must insert the appropriate catcode test, then parse the class; we
%   pre-expand the catcode as an optimization. Otherwise (modes $0$,
%   $-2$ and $-6$) just parse the class.  The mode is updated later.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_[: }
  {
    \@@_if_in_class:TF
      { \@@_compile_class_posix_test:w }
      {
        \@@_if_within_catcode:TF
          {
            \exp_after:wN \@@_compile_class_catcode:w
              \int_use:N \l_@@_catcodes_int ;
          }
          { \@@_compile_class_normal:w }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_class_normal:w}
%   In the \enquote{normal} case, we insert \cs{@@_class:NnnnN}
%   \meta{boolean} in the compiled code. The \meta{boolean} is true for
%   positive classes, and false for negative classes, characterized by a
%   leading |^|. The auxiliary \cs{@@_compile_class:TFNN} also
%   checks for a leading |]| which has a special meaning.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_class_normal:w
  {
    \@@_compile_class:TFNN
      { \@@_class:NnnnN \c_true_bool }
      { \@@_class:NnnnN \c_false_bool }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_class_catcode:w}
%   This function is called for a left bracket in modes $2$ or $6$
%   (catcode test, and catcode test within a class). In mode $2$ the
%   whole construction needs to be put in a class (like single
%   character). Then determine if the class is positive or negative,
%   inserting \cs{@@_item_catcode:nT} or the \texttt{reverse} variant
%   as appropriate, each with the current catcodes bitmap |#1| as an
%   argument, and reset the catcodes.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_class_catcode:w #1;
  {
    \if_int_compare:w \l_@@_mode_int = \c_@@_catcode_mode_int
      \tl_build_put_right:Nn \l_@@_build_tl
        { \@@_class:NnnnN \c_true_bool { \if_false: } \fi: }
    \fi:
    \int_set_eq:NN \l_@@_catcodes_int \l_@@_default_catcodes_int
    \@@_compile_class:TFNN
      { \@@_item_catcode:nT {#1} }
      { \@@_item_catcode_reverse:nT {#1} }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {\@@_compile_class:TFNN, \@@_compile_class:NN}
%   If the first character is |^|, then the class is negative (use
%   |#2|), otherwise it is positive (use |#1|). If the next character
%   is a right bracket, then it should be changed to a raw one.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_class:TFNN #1#2#3#4
  {
    \l_@@_mode_int = \int_value:w \l_@@_mode_int 3 \exp_stop_f:
    \@@_two_if_eq:NNNNTF #3 #4 \@@_compile_special:N ^
      {
        \tl_build_put_right:Nn \l_@@_build_tl { #2 { \if_false: } \fi: }
        \@@_compile_class:NN
      }
      {
        \tl_build_put_right:Nn \l_@@_build_tl { #1 { \if_false: } \fi: }
        \@@_compile_class:NN #3 #4
      }
  }
\cs_new_protected:Npn \@@_compile_class:NN #1#2
  {
    \token_if_eq_charcode:NNTF #2 ]
      { \@@_compile_raw:N #2 }
      { #1 #2 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_compile_class_posix_test:w,
%     \@@_compile_class_posix:NNNNw,
%     \@@_compile_class_posix_loop:w,
%     \@@_compile_class_posix_end:w
%   }
%   Here we check for a syntax such as |[:alpha:]|. We also detect |[=|
%   and |[.| which have a meaning in \textsc{posix} regular expressions,
%   but are not implemented in \pkg{l3regex}. In case we see |[:|, grab
%   raw characters until hopefully reaching |:]|. If that's missing, or
%   the \textsc{posix} class is unknown, abort. If all is right, add the
%   test to the current class, with an extra \cs{@@_item_reverse:n}
%   for negative classes.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_class_posix_test:w #1#2
  {
    \token_if_eq_meaning:NNT \@@_compile_special:N #1
      {
        \str_case:nn { #2 }
          {
            : { \@@_compile_class_posix:NNNNw }
            = {
                \msg_warning:nnx { regex }
                  { posix-unsupported } { = }
              }
            . {
                \msg_warning:nnx { regex }
                  { posix-unsupported } { . }
              }
          }
      }
    \@@_compile_raw:N [ #1 #2
  }
\cs_new_protected:Npn \@@_compile_class_posix:NNNNw #1#2#3#4#5#6
  {
    \@@_two_if_eq:NNNNTF #5 #6 \@@_compile_special:N ^
      {
        \bool_set_false:N \l_@@_internal_bool
        \__kernel_tl_set:Nx \l_@@_internal_a_tl { \if_false: } \fi:
          \@@_compile_class_posix_loop:w
      }
      {
        \bool_set_true:N \l_@@_internal_bool
        \__kernel_tl_set:Nx \l_@@_internal_a_tl { \if_false: } \fi:
          \@@_compile_class_posix_loop:w #5 #6
      }
  }
\cs_new:Npn \@@_compile_class_posix_loop:w #1#2
  {
    \token_if_eq_meaning:NNTF \@@_compile_raw:N #1
      { #2 \@@_compile_class_posix_loop:w }
      { \if_false: { \fi: } \@@_compile_class_posix_end:w #1 #2 }
  }
\cs_new_protected:Npn \@@_compile_class_posix_end:w #1#2#3#4
  {
    \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N :
      { \@@_two_if_eq:NNNNTF #3 #4 \@@_compile_special:N ] }
      { \use_ii:nn }
      {
        \cs_if_exist:cTF { @@_posix_ \l_@@_internal_a_tl : }
          {
            \@@_compile_one:n
              {
                \bool_if:NF \l_@@_internal_bool \@@_item_reverse:n
                \exp_not:c { @@_posix_ \l_@@_internal_a_tl : }
              }
          }
          {
            \msg_warning:nnx { regex } { posix-unknown }
              { \l_@@_internal_a_tl }
            \@@_compile_abort_tokens:x
              {
                [: \bool_if:NF \l_@@_internal_bool { ^ }
                \l_@@_internal_a_tl :]
              }
          }
      }
      {
        \msg_error:nnxx { regex } { posix-missing-close }
          { [: \l_@@_internal_a_tl } { #2 #4 }
        \@@_compile_abort_tokens:x { [: \l_@@_internal_a_tl }
        #1 #2 #3 #4
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Groups and alternations}
%
% \begin{macro}{\@@_compile_group_begin:N, \@@_compile_group_end:}
%   The contents of a regex group are turned into compiled code in
%   \cs{l_@@_build_tl}, which ends up with items of the form
%   \cs{@@_branch:n} \Arg{concatenation}. This construction is done
%   using \cs[no-index]{tl_build_\ldots{}} functions within a \TeX{} group, which automatically
%   makes sure that options (case-sensitivity and default catcode) are
%   reset at the end of the group. The argument |#1| is
%   \cs{@@_group:nnnN} or a variant thereof. A small subtlety to
%   support |\cL(abc)| as a shorthand for |(\cLa\cLb\cLc)|: exit any
%   pending catcode test, save the category code at the start of the
%   group as the default catcode for that group, and make sure that the
%   catcode is restored to the default outside the group.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_group_begin:N #1
  {
    \tl_build_put_right:Nn \l_@@_build_tl { #1 { \if_false: } \fi: }
    \@@_mode_quit_c:
    \group_begin:
      \tl_build_begin:N \l_@@_build_tl
      \int_set_eq:NN \l_@@_default_catcodes_int \l_@@_catcodes_int
      \int_incr:N \l_@@_group_level_int
      \tl_build_put_right:Nn \l_@@_build_tl
        { \@@_branch:n { \if_false: } \fi: }
  }
\cs_new_protected:Npn \@@_compile_group_end:
  {
    \if_int_compare:w \l_@@_group_level_int > \c_zero_int
        \tl_build_put_right:Nn \l_@@_build_tl { \if_false: { \fi: } }
        \tl_build_end:N \l_@@_build_tl
        \exp_args:NNNx
      \group_end:
      \tl_build_put_right:Nn \l_@@_build_tl { \l_@@_build_tl }
      \int_set_eq:NN \l_@@_catcodes_int \l_@@_default_catcodes_int
      \exp_after:wN \@@_compile_quantifier:w
    \else:
      \msg_warning:nn { regex } { extra-rparen }
      \exp_after:wN \@@_compile_raw:N \exp_after:wN )
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_(:}
%   In a class, parentheses are not special.  In a catcode test inside a
%   class, a left parenthesis gives an error, to catch |[a\cL(bcd)e]|.
%   Otherwise check for a |?|, denoting special groups, and run the code
%   for the corresponding special group.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_(: }
  {
    \@@_if_in_class:TF { \@@_compile_raw:N ( }
      {
        \if_int_compare:w \l_@@_mode_int =
          \c_@@_catcode_in_class_mode_int
          \msg_error:nn { regex } { c-lparen-in-class }
          \exp_after:wN \@@_compile_raw:N \exp_after:wN (
        \else:
          \exp_after:wN \@@_compile_lparen:w
        \fi:
      }
  }
\cs_new_protected:Npn \@@_compile_lparen:w #1#2#3#4
  {
    \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N ?
      {
        \cs_if_exist_use:cF
          { @@_compile_special_group_\token_to_str:N #4 :w }
          {
            \msg_warning:nnx { regex } { special-group-unknown }
              { (? #4 }
            \@@_compile_group_begin:N \@@_group:nnnN
              \@@_compile_raw:N ? #3 #4
          }
      }
      {
        \@@_compile_group_begin:N \@@_group:nnnN
          #1 #2 #3 #4
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}+\@@_compile_|:+
%   In a class, the pipe is not special. Otherwise, end the current
%   branch and open another one.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_|: }
  {
    \@@_if_in_class:TF { \@@_compile_raw:N | }
      {
        \tl_build_put_right:Nn \l_@@_build_tl
          { \if_false: { \fi: } \@@_branch:n { \if_false: } \fi: }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_):}
%   Within a class, parentheses are not special. Outside, close a group.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_): }
  {
    \@@_if_in_class:TF { \@@_compile_raw:N ) }
      { \@@_compile_group_end: }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_special_group_::w}
% \begin{macro}+\@@_compile_special_group_|:w+
%   Non-capturing, and resetting groups are easy to take care of during
%   compilation; for those groups, the harder parts come when building.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_special_group_::w }
  { \@@_compile_group_begin:N \@@_group_no_capture:nnnN }
\cs_new_protected:cpn { @@_compile_special_group_|:w }
  { \@@_compile_group_begin:N \@@_group_resetting:nnnN }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}
%   {\@@_compile_special_group_i:w, \@@_compile_special_group_-:w}
%   The match can be made case-insensitive by setting the option with
%   \texttt{(?i)}; the original behaviour is restored by \texttt{(?-i)}.
%   This is the only supported option.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_special_group_i:w #1#2
  {
    \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N )
      {
        \cs_set:Npn \@@_item_equal:n
          { \@@_item_caseless_equal:n }
        \cs_set:Npn \@@_item_range:nn
          { \@@_item_caseless_range:nn }
      }
      {
        \msg_warning:nnx { regex } { unknown-option } { (?i #2 }
        \@@_compile_raw:N (
        \@@_compile_raw:N ?
        \@@_compile_raw:N i
        #1 #2
      }
  }
\cs_new_protected:cpn { @@_compile_special_group_-:w } #1#2#3#4
  {
    \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_raw:N i
      { \@@_two_if_eq:NNNNTF #3 #4 \@@_compile_special:N ) }
      { \use_ii:nn }
      {
        \cs_set:Npn \@@_item_equal:n
          { \@@_item_caseful_equal:n }
        \cs_set:Npn \@@_item_range:nn
          { \@@_item_caseful_range:nn }
      }
      {
        \msg_warning:nnx { regex } { unknown-option } { (?-#2#4 }
        \@@_compile_raw:N (
        \@@_compile_raw:N ?
        \@@_compile_raw:N -
        #1 #2 #3 #4
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Catcodes and csnames}
%
% \begin{macro}{\@@_compile_/c:, \@@_compile_c_test:NN}
%   The |\c| escape sequence can be followed by a capital letter
%   representing a character category, by a left bracket which starts a
%   list of categories, or by a brace group holding a regular expression
%   for a control sequence name. Otherwise, raise an error.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_/c: }
  { \@@_chk_c_allowed:T { \@@_compile_c_test:NN } }
\cs_new_protected:Npn \@@_compile_c_test:NN #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_compile_raw:N
      {
        \int_if_exist:cTF { c_@@_catcode_#2_int }
          {
            \int_set_eq:Nc \l_@@_catcodes_int
              { c_@@_catcode_#2_int }
            \l_@@_mode_int
              = \if_case:w \l_@@_mode_int
                  \c_@@_catcode_mode_int
                \else:
                  \c_@@_catcode_in_class_mode_int
                \fi:
            \token_if_eq_charcode:NNT C #2 { \@@_compile_c_C:NN }
          }
      }
      { \cs_if_exist_use:cF { @@_compile_c_#2:w } }
          {
            \msg_error:nnx { regex } { c-missing-category } {#2}
            #1 #2
          }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_c_C:NN}
%   If |\cC| is not followed by |.| or |(...)| then complain because
%   that construction cannot match anything, except in cases like
%   |\cC[\c{...}]|, where it has no effect.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_c_C:NN #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_compile_special:N
      {
        \token_if_eq_charcode:NNTF #2 .
          { \use_none:n }
          { \token_if_eq_charcode:NNF #2 ( } % )
      }
      { \use:n }
    { \msg_error:nnn { regex } { c-C-invalid } {#2} }
    #1 #2
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_compile_c_[:w,
%     \@@_compile_c_lbrack_loop:NN,
%     \@@_compile_c_lbrack_add:N,
%     \@@_compile_c_lbrack_end:,
%   }
%   When encountering |\c[|, the task is to collect uppercase letters
%   representing character categories. First check for |^| which negates
%   the list of category codes.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_c_[:w } #1#2
  {
    \l_@@_mode_int
      = \if_case:w \l_@@_mode_int
          \c_@@_catcode_mode_int
        \else:
          \c_@@_catcode_in_class_mode_int
        \fi:
    \int_zero:N \l_@@_catcodes_int
    \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N ^
      {
        \bool_set_false:N \l_@@_catcodes_bool
        \@@_compile_c_lbrack_loop:NN
      }
      {
        \bool_set_true:N \l_@@_catcodes_bool
        \@@_compile_c_lbrack_loop:NN
        #1 #2
      }
  }
\cs_new_protected:Npn \@@_compile_c_lbrack_loop:NN #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_compile_raw:N
      {
        \int_if_exist:cTF { c_@@_catcode_#2_int }
          {
            \exp_args:Nc \@@_compile_c_lbrack_add:N
              { c_@@_catcode_#2_int }
            \@@_compile_c_lbrack_loop:NN
          }
      }
      {
        \token_if_eq_charcode:NNTF #2 ]
          { \@@_compile_c_lbrack_end: }
      }
          {
            \msg_error:nnx { regex } { c-missing-rbrack } {#2}
            \@@_compile_c_lbrack_end:
            #1 #2
          }
  }
\cs_new_protected:Npn \@@_compile_c_lbrack_add:N #1
  {
    \if_int_odd:w \int_eval:n { \l_@@_catcodes_int / #1 } \exp_stop_f:
    \else:
      \int_add:Nn \l_@@_catcodes_int {#1}
    \fi:
  }
\cs_new_protected:Npn \@@_compile_c_lbrack_end:
  {
    \if_meaning:w \c_false_bool \l_@@_catcodes_bool
      \int_set:Nn \l_@@_catcodes_int
        { \c_@@_all_catcodes_int - \l_@@_catcodes_int }
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}+\@@_compile_c_{:+
%   The case of a left brace is easy, based on what we have done so far:
%   in a group, compile the regular expression, after changing the mode
%   to forbid nesting |\c|. Additionally, disable submatch tracking
%   since groups don't escape the scope of |\c{...}|.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_c_ \c_left_brace_str :w }
  {
    \@@_compile:w
      \@@_disable_submatches:
      \l_@@_mode_int
        = \if_case:w \l_@@_mode_int
            \c_@@_cs_mode_int
          \else:
            \c_@@_cs_in_class_mode_int
          \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}+\@@_compile_{:+
%   We forbid unescaped left braces inside a |\c{...}| escape because
%   they otherwise lead to the confusing question of whether the first
%   right brace in |\c{{}x}| should end |\c| or whether one should
%   match braces.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_ \c_left_brace_str : }
  {
    \@@_if_in_cs:TF
      { \msg_error:nnn { regex } { cu-lbrace } { c } }
      { \exp_after:wN \@@_compile_raw:N \c_left_brace_str }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}+\@@_compile_}:+
% \begin{macro}{\@@_compile_end_cs:}
% \begin{macro}[EXP]{\@@_compile_cs_aux:Nn, \@@_compile_cs_aux:NNnnnN}
%   Non-escaped right braces are only special if they appear when
%   compiling the regular expression for a csname, but not within a
%   class: |\c{[{}]}| matches the control sequences |\{| and |\}|.  So,
%   end compiling the inner regex (this closes any dangling class or
%   group).  Then insert the corresponding test in the outer regex.  As
%   an optimization, if the control sequence test simply consists of
%   several explicit possibilities (branches) then use
%   \cs{@@_item_exact_cs:n} with an argument consisting of all
%   possibilities separated by \cs{scan_stop:}.
%    \begin{macrocode}
\flag_new:n { @@_cs }
\cs_new_protected:cpn { @@_compile_ \c_right_brace_str : }
  {
    \@@_if_in_cs:TF
      { \@@_compile_end_cs: }
      { \exp_after:wN \@@_compile_raw:N \c_right_brace_str }
  }
\cs_new_protected:Npn \@@_compile_end_cs:
  {
    \@@_compile_end:
    \flag_clear:n { @@_cs }
    \__kernel_tl_set:Nx \l_@@_internal_a_tl
      {
        \exp_after:wN \@@_compile_cs_aux:Nn \l_@@_internal_regex
        \q_@@_nil \q_@@_nil \q_@@_recursion_stop
      }
    \exp_args:Nx \@@_compile_one:n
      {
        \flag_if_raised:nTF { @@_cs }
          { \@@_item_cs:n { \exp_not:o \l_@@_internal_regex } }
          {
            \@@_item_exact_cs:n
              { \tl_tail:N \l_@@_internal_a_tl }
          }
      }
  }
\cs_new:Npn \@@_compile_cs_aux:Nn #1#2
  {
    \cs_if_eq:NNTF #1 \@@_branch:n
      {
        \scan_stop:
        \@@_compile_cs_aux:NNnnnN #2
        \q_@@_nil \q_@@_nil \q_@@_nil
        \q_@@_nil \q_@@_nil \q_@@_nil \q_@@_recursion_stop
        \@@_compile_cs_aux:Nn
      }
      {
        \@@_quark_if_nil:NF #1 { \flag_raise_if_clear:n { @@_cs } }
        \@@_use_none_delimit_by_q_recursion_stop:w
      }
  }
\cs_new:Npn \@@_compile_cs_aux:NNnnnN #1#2#3#4#5#6
  {
    \bool_lazy_all:nTF
      {
        { \cs_if_eq_p:NN #1 \@@_class:NnnnN }
        {#2}
        { \tl_if_head_eq_meaning_p:nN {#3} \@@_item_caseful_equal:n }
        { \int_compare_p:nNn { \tl_count:n {#3} } = { 2 } }
        { \int_compare_p:nNn {#5} = { 0 } }
      }
      {
        \prg_replicate:nn {#4}
          { \char_generate:nn { \use_ii:nn #3 } {12} }
        \@@_compile_cs_aux:NNnnnN
      }
      {
        \@@_quark_if_nil:NF #1
          {
            \flag_raise_if_clear:n { @@_cs }
            \@@_use_i_delimit_by_q_recursion_stop:nw
          }
        \@@_use_none_delimit_by_q_recursion_stop:w
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Raw token lists with \cs[no-index]{u}}
%
% \begin{macro}{\@@_compile_/u:}
%   The |\u| escape is invalid in classes and directly following a
%   catcode test. Otherwise test for a following |r| (for |\ur|), and
%   call an auxiliary responsible for finding the variable name.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_/u: } #1#2
  {
    \@@_if_in_class_or_catcode:TF
      { \@@_compile_raw_error:N u #1 #2 }
      {
        \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_raw:N r
          { \@@_compile_u_brace:NNN \@@_compile_ur_end: }
          { \@@_compile_u_brace:NNN \@@_compile_u_end: #1 #2 }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_u_brace:NNN}
%   This enforces the presence of a left brace, then starts a loop to
%   find the variable name.
%    \begin{macrocode}
\cs_new:Npn \@@_compile_u_brace:NNN #1#2#3
  {
    \@@_two_if_eq:NNNNTF #2 #3 \@@_compile_special:N \c_left_brace_str
      {
        \tl_set:Nn \l_@@_internal_b_tl {#1}
        \__kernel_tl_set:Nx \l_@@_internal_a_tl { \if_false: } \fi:
        \@@_compile_u_loop:NN
      }
      {
        \msg_error:nn { regex } { u-missing-lbrace }
        \token_if_eq_meaning:NNTF #1 \@@_compile_ur_end:
          { \@@_compile_raw:N u \@@_compile_raw:N r }
          { \@@_compile_raw:N u }
        #2 #3
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_compile_u_loop:NN}
%   We collect the characters for the argument of |\u| within an
%   \texttt{x}-expanding assignment. In principle we could just wait to
%   encounter a right brace, but this is unsafe: if the right brace was
%   missing, then we would reach the end-markers of the regex, and
%   continue, leading to obscure fatal errors. Instead, we only allow
%   raw and special characters, and stop when encountering a special
%   right brace, any escaped character, or the end-marker.
%    \begin{macrocode}
\cs_new:Npn \@@_compile_u_loop:NN #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_compile_raw:N
      { #2 \@@_compile_u_loop:NN }
      {
        \token_if_eq_meaning:NNTF #1 \@@_compile_special:N
          {
            \exp_after:wN \token_if_eq_charcode:NNTF \c_right_brace_str #2
              { \if_false: { \fi: } \l_@@_internal_b_tl }
              {
                \if_charcode:w \c_left_brace_str #2
                  \msg_expandable_error:nnn { regex } { cu-lbrace } { u }
                \else:
                  #2
                \fi:
                \@@_compile_u_loop:NN
              }
          }
          {
            \if_false: { \fi: }
            \msg_error:nnx { regex } { u-missing-rbrace } {#2}
            \l_@@_internal_b_tl
            #1 #2
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_ur_end:, \@@_compile_ur:n}
% \begin{macro}[EXP]{\@@_compile_ur_aux:w}
%   For the |\ur{...}| construction, once we have extracted the
%   variable's name, we replace all groups by non-capturing groups in
%   the compiled regex (passed as the
%   argument of \cs{@@_compile_ur:n}).  If that has a single branch
%   (namely \cs{tl_if_empty:oTF} is false) and there is no quantifier,
%   then simply insert the contents of this branch (obtained by
%   \cs{use_ii:nn}, which is expanded later).  In all other cases,
%   insert a non-capturing group and look for quantifiers to determine
%   the number of repetition etc.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_ur_end:
  {
    \group_begin:
      \cs_set:Npn \@@_group:nnnN { \@@_group_no_capture:nnnN }
      \cs_set:Npn \@@_group_resetting:nnnN { \@@_group_no_capture:nnnN }
      \exp_args:NNx
    \group_end:
    \@@_compile_ur:n { \use:c { \l_@@_internal_a_tl } }
  }
\cs_new_protected:Npn \@@_compile_ur:n #1
  {
    \tl_if_empty:oTF { \@@_compile_ur_aux:w #1 {} ? ? \q_@@_nil }
      { \@@_compile_if_quantifier:TFw }
      { \use_i:nn }
          {
            \tl_build_put_right:Nn \l_@@_build_tl
              { \@@_group_no_capture:nnnN { \if_false: } \fi: #1 }
            \@@_compile_quantifier:w
          }
          { \tl_build_put_right:Nn \l_@@_build_tl { \use_ii:nn #1 } }
  }
\cs_new:Npn \@@_compile_ur_aux:w \@@_branch:n #1#2#3 \q_@@_nil {#2}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_compile_u_end:, \@@_compile_u_payload:}
%   Once we have extracted the variable's name, we check for
%   quantifiers, in which case we set up a non-capturing group with a
%   single branch.  Inside this branch (we omit it and the group if
%   there is no quantifier), \cs{@@_compile_u_payload:} puts
%   the right tests corresponding to the contents of the variable, which
%   we store in \cs{l_@@_internal_a_tl}. The behaviour of |\u|
%   then depends on whether we are within a |\c{...}| escape (in this
%   case, the variable is turned to a string), or not.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_u_end:
  {
    \@@_compile_if_quantifier:TFw
      {
        \tl_build_put_right:Nn \l_@@_build_tl
          {
            \@@_group_no_capture:nnnN { \if_false: } \fi:
            \@@_branch:n { \if_false: } \fi:
          }
        \@@_compile_u_payload:
        \tl_build_put_right:Nn \l_@@_build_tl { \if_false: { \fi: } }
        \@@_compile_quantifier:w
      }
      { \@@_compile_u_payload: }
  }
\cs_new_protected:Npn \@@_compile_u_payload:
  {
    \tl_set:Nv \l_@@_internal_a_tl { \l_@@_internal_a_tl }
    \if_int_compare:w \l_@@_mode_int = \c_@@_outer_mode_int
      \@@_compile_u_not_cs:
    \else:
      \@@_compile_u_in_cs:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_u_in_cs:}
%   When |\u| appears within a control sequence, we convert the variable
%   to a string with escaped spaces. Then for each character insert a
%   class matching exactly that character, once.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_u_in_cs:
  {
    \__kernel_tl_gset:Nx \g_@@_internal_tl
      {
        \exp_args:No \__kernel_str_to_other_fast:n
          { \l_@@_internal_a_tl }
      }
    \tl_build_put_right:Nx \l_@@_build_tl
      {
        \tl_map_function:NN \g_@@_internal_tl
          \@@_compile_u_in_cs_aux:n
      }
  }
\cs_new:Npn \@@_compile_u_in_cs_aux:n #1
  {
    \@@_class:NnnnN \c_true_bool
      { \@@_item_caseful_equal:n { \int_value:w `#1 } }
      { 1 } { 0 } \c_false_bool
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_compile_u_not_cs:}
%   In mode $0$, the |\u| escape adds one state to the NFA for each
%   token in \cs{l_@@_internal_a_tl}. If a given \meta{token} is a
%   control sequence, then insert a string comparison test, otherwise,
%   \cs{@@_item_exact:nn} which compares catcode and character code.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_compile_u_not_cs:
  {
    \tl_analysis_map_inline:Nn \l_@@_internal_a_tl
      {
        \tl_build_put_right:Nx \l_@@_build_tl
          {
            \@@_class:NnnnN \c_true_bool
              {
                \if_int_compare:w "##3 = \c_zero_int
                  \@@_item_exact_cs:n
                    { \exp_after:wN \cs_to_str:N ##1 }
                \else:
                  \@@_item_exact:nn { \int_value:w "##3 } { ##2 }
                \fi:
              }
              { 1 } { 0 } \c_false_bool
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Other}
%
% \begin{macro}{\@@_compile_/K:}
%   The |\K| control sequence is currently the only \enquote{command},
%   which performs some action, rather than matching something. It is
%   allowed in the same contexts as |\b|. At the compilation stage, we
%   leave it as a single control sequence, defined later.
%    \begin{macrocode}
\cs_new_protected:cpn { @@_compile_/K: }
  {
    \int_compare:nNnTF \l_@@_mode_int = \c_@@_outer_mode_int
      { \tl_build_put_right:Nn \l_@@_build_tl { \@@_command_K: } }
      { \@@_compile_raw_error:N K }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Showing regexes}
%
% \begin{macro}[rEXP]
%   {
%     \@@_clean_bool:n, \@@_clean_int:n, \@@_clean_int_aux:N,
%     \@@_clean_regex:n, \@@_clean_regex_loop:w, \@@_clean_branch:n,
%     \@@_clean_branch_loop:n, \@@_clean_assertion:Nn,
%     \@@_clean_class:NnnnN, \@@_clean_group:nnnN, \@@_clean_class:n,
%     \@@_clean_class_loop:nnn, \@@_clean_exact_cs:n,
%     \@@_clean_exact_cs:w
%   }
%   Before showing a regex we check that it is \enquote{clean} in the
%   sense that it has the correct internal structure.  We do this (in
%   the implementation of \cs{regex_show:N} and \cs{regex_log:N}) by
%   comparing it with a cleaned-up version of the same regex.  Along the
%   way we also need similar functions for other types: all
%   \cs[no-index]{@@_clean_\meta{type}:n} functions produce valid
%   \meta{type} tokens (bool, explicit integer, etc.\@) from arbitrary
%   input, and the output coincides with the input if that was valid.
%    \begin{macrocode}
\cs_new:Npn \@@_clean_bool:n #1
  {
    \tl_if_single:nTF {#1}
      { \bool_if:NTF #1 \c_true_bool \c_false_bool }
      { \c_true_bool }
  }
\cs_new:Npn \@@_clean_int:n #1
  {
    \tl_if_head_eq_meaning:nNTF {#1} -
      { - \exp_args:No \@@_clean_int:n { \use_none:n #1 } }
      { \int_eval:n { 0 \str_map_function:nN {#1} \@@_clean_int_aux:N } }
  }
\cs_new:Npn \@@_clean_int_aux:N #1
  {
    \if_int_compare:w 1 < 1 #1 ~
      #1
    \else:
      \exp_after:wN \str_map_break:
    \fi:
  }
\cs_new:Npn \@@_clean_regex:n #1
  {
    \@@_clean_regex_loop:w #1
    \@@_branch:n { \q_recursion_tail } \q_recursion_stop
  }
\cs_new:Npn \@@_clean_regex_loop:w #1 \@@_branch:n #2
  {
    \quark_if_recursion_tail_stop:n {#2}
    \@@_branch:n { \@@_clean_branch:n {#2} }
    \@@_clean_regex_loop:w
  }
\cs_new:Npn \@@_clean_branch:n #1
  {
    \@@_clean_branch_loop:n #1
    ? ? ? ? ? ? \prg_break_point:
  }
\cs_new:Npn \@@_clean_branch_loop:n #1
  {
    \tl_if_single:nF {#1} { \prg_break: }
    \token_case_meaning:NnF #1
      {
        \@@_command_K: { #1 \@@_clean_branch_loop:n }
        \@@_assertion:Nn { #1 \@@_clean_assertion:Nn }
        \@@_class:NnnnN { #1 \@@_clean_class:NnnnN }
        \@@_group:nnnN { #1 \@@_clean_group:nnnN }
        \@@_group_no_capture:nnnN { #1 \@@_clean_group:nnnN }
        \@@_group_resetting:nnnN { #1 \@@_clean_group:nnnN }
      }
      { \prg_break: }
  }
\cs_new:Npn \@@_clean_assertion:Nn #1#2
  {
    \@@_clean_bool:n {#1}
    \tl_if_single:nF {#2} { { \@@_A_test: } \prg_break: }
    \token_case_meaning:NnTF #2
      {
        \@@_A_test: { }
        \@@_G_test: { }
        \@@_Z_test: { }
        \@@_b_test: { }
      }
      { {#2} }
      { { \@@_A_test: } \prg_break: }
    \@@_clean_branch_loop:n
  }
\cs_new:Npn \@@_clean_class:NnnnN #1#2#3#4#5
  {
    \@@_clean_bool:n {#1}
    { \@@_clean_class:n {#2} }
    { \int_max:nn { 0 } { \@@_clean_int:n {#3} } }
    { \int_max:nn { -1 } { \@@_clean_int:n {#4} } }
    \@@_clean_bool:n {#5}
    \@@_clean_branch_loop:n
  }
\cs_new:Npn \@@_clean_group:nnnN #1#2#3#4
  {
    { \@@_clean_regex:n {#1} }
    { \int_max:nn { 0 } { \@@_clean_int:n {#2} } }
    { \int_max:nn { -1 } { \@@_clean_int:n {#3} } }
    \@@_clean_bool:n {#4}
    \@@_clean_branch_loop:n
  }
\cs_new:Npn \@@_clean_class:n #1
  { \@@_clean_class_loop:nnn #1 ????? \prg_break_point: }
\cs_new:Npn \@@_clean_class_loop:nnn #1#2#3
  {
    \tl_if_single:nF {#1} { \prg_break: }
    \token_case_meaning:NnTF #1
      {
        \@@_item_cs:n { #1 { \@@_clean_regex:n {#2} } }
        \@@_item_exact_cs:n { #1 { \@@_clean_exact_cs:n {#2} } }
        \@@_item_caseful_equal:n { #1 { \@@_clean_int:n {#2} } }
        \@@_item_caseless_equal:n { #1 { \@@_clean_int:n {#2} } }
        \@@_item_reverse:n { #1 { \@@_clean_class:n {#2} } }
      }
      { \@@_clean_class_loop:nnn {#3} }
      {
        \token_case_meaning:NnTF #1
          {
            \@@_item_caseful_range:nn { }
            \@@_item_caseless_range:nn { }
            \@@_item_exact:nn { }
          }
          { #1 { \@@_clean_int:n {#2} } { \@@_clean_int:n {#3} } }
          {
            \token_case_meaning:NnTF #1
              {
                \@@_item_catcode:nT { }
                \@@_item_catcode_reverse:nT { }
              }
              {
                #1 { \@@_clean_int:n {#2} } { \@@_clean_class:n {#3} }
                \@@_clean_class_loop:nnn
              }
              { \prg_break: }
          }
      }
  }
\cs_new:Npn \@@_clean_exact_cs:n #1
  {
    \exp_last_unbraced:Nf \use_none:n
      {
        \@@_clean_exact_cs:w #1
        \scan_stop: \q_recursion_tail \scan_stop:
        \q_recursion_stop
      }
  }
\cs_new:Npn \@@_clean_exact_cs:w #1 \scan_stop:
  {
    \quark_if_recursion_tail_stop:n {#1}
    \scan_stop: \tl_to_str:n {#1}
    \@@_clean_exact_cs:w
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_show:N}
%   Within a group and within \cs{tl_build_begin:N} \ldots{} \cs{tl_build_end:N} we
%   redefine all the function that can appear in a compiled regex, then
%   run the regex. The result stored in \cs{l_@@_internal_a_tl} is then
%   meant to be shown.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_show:N #1
  {
    \group_begin:
      \tl_build_begin:N \l_@@_build_tl
      \cs_set_protected:Npn \@@_branch:n
        {
          \seq_pop_right:NN \l_@@_show_prefix_seq
            \l_@@_internal_a_tl
          \@@_show_one:n { +-branch }
          \seq_put_right:No \l_@@_show_prefix_seq
            \l_@@_internal_a_tl
          \use:n
        }
      \cs_set_protected:Npn \@@_group:nnnN
        { \@@_show_group_aux:nnnnN { } }
      \cs_set_protected:Npn \@@_group_no_capture:nnnN
        { \@@_show_group_aux:nnnnN { ~(no~capture) } }
      \cs_set_protected:Npn \@@_group_resetting:nnnN
        { \@@_show_group_aux:nnnnN { ~(resetting) } }
      \cs_set_eq:NN \@@_class:NnnnN \@@_show_class:NnnnN
      \cs_set_protected:Npn \@@_command_K:
        { \@@_show_one:n { reset~match~start~(\iow_char:N\\K) } }
      \cs_set_protected:Npn \@@_assertion:Nn ##1##2
        {
          \@@_show_one:n
            { \bool_if:NF ##1 { negative~ } assertion:~##2 }
        }
      \cs_set:Npn \@@_b_test: { word~boundary }
      \cs_set:Npn \@@_Z_test: { anchor~at~end~(\iow_char:N\\Z) }
      \cs_set:Npn \@@_A_test: { anchor~at~start~(\iow_char:N\\A) }
      \cs_set:Npn \@@_G_test: { anchor~at~start~of~match~(\iow_char:N\\G) }
      \cs_set_protected:Npn \@@_item_caseful_equal:n ##1
        { \@@_show_one:n { char~code~\@@_show_char:n{##1} } }
      \cs_set_protected:Npn \@@_item_caseful_range:nn ##1##2
        {
          \@@_show_one:n
            { range~[\@@_show_char:n{##1}, \@@_show_char:n{##2}] }
        }
      \cs_set_protected:Npn \@@_item_caseless_equal:n ##1
        { \@@_show_one:n { char~code~\@@_show_char:n{##1}~(caseless) } }
      \cs_set_protected:Npn \@@_item_caseless_range:nn ##1##2
        {
          \@@_show_one:n
            { Range~[\@@_show_char:n{##1}, \@@_show_char:n{##2}]~(caseless) }
        }
      \cs_set_protected:Npn \@@_item_catcode:nT
        { \@@_show_item_catcode:NnT \c_true_bool }
      \cs_set_protected:Npn \@@_item_catcode_reverse:nT
        { \@@_show_item_catcode:NnT \c_false_bool }
      \cs_set_protected:Npn \@@_item_reverse:n
        { \@@_show_scope:nn { Reversed~match } }
      \cs_set_protected:Npn \@@_item_exact:nn ##1##2
        { \@@_show_one:n { char~\@@_show_char:n{##2},~catcode~##1 } }
      \cs_set_eq:NN \@@_item_exact_cs:n \@@_show_item_exact_cs:n
      \cs_set_protected:Npn \@@_item_cs:n
        { \@@_show_scope:nn { control~sequence } }
      \cs_set:cpn { @@_prop_.: } { \@@_show_one:n { any~token } }
      \seq_clear:N \l_@@_show_prefix_seq
      \@@_show_push:n { ~ }
      \cs_if_exist_use:N #1
      \tl_build_end:N \l_@@_build_tl
      \exp_args:NNNo
    \group_end:
    \tl_set:Nn \l_@@_internal_a_tl { \l_@@_build_tl }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_show_char:n}
%   Show a single character, together with its ascii representation if available.
%   This could be extended to beyond ascii.  It is not ideal for parentheses themselves.
%    \begin{macrocode}
\cs_new:Npn \@@_show_char:n #1
  {
    \int_eval:n {#1}
    \int_compare:nT { 32 <= #1 <= 126 }
      { ~ ( \char_generate:nn {#1} {12} ) }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_show_one:n}
%   Every part of the final message go through this function, which adds
%   one line to the output, with the appropriate prefix.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_show_one:n #1
  {
    \int_incr:N \l_@@_show_lines_int
    \tl_build_put_right:Nx \l_@@_build_tl
      {
        \exp_not:N \iow_newline:
        \seq_map_function:NN \l_@@_show_prefix_seq \use:n
        #1
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {\@@_show_push:n, \@@_show_pop:, \@@_show_scope:nn}
%   Enter and exit levels of nesting. The \texttt{scope} function prints
%   its first argument as an \enquote{introduction}, then performs its
%   second argument in a deeper level of nesting.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_show_push:n #1
  { \seq_put_right:Nx \l_@@_show_prefix_seq { #1 ~ } }
\cs_new_protected:Npn \@@_show_pop:
  { \seq_pop_right:NN \l_@@_show_prefix_seq \l_@@_internal_a_tl }
\cs_new_protected:Npn \@@_show_scope:nn #1#2
  {
    \@@_show_one:n {#1}
    \@@_show_push:n { ~ }
    #2
    \@@_show_pop:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_show_group_aux:nnnnN}
%   We display all groups in the same way, simply adding a message,
%   \texttt{(no capture)} or \texttt{(resetting)}, to special groups.
%   The odd \cs{use_ii:nn} avoids printing a spurious \texttt{+-branch}
%   for the first branch.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_show_group_aux:nnnnN #1#2#3#4#5
  {
    \@@_show_one:n { ,-group~begin #1 }
    \@@_show_push:n { | }
    \use_ii:nn #2
    \@@_show_pop:
    \@@_show_one:n
      { `-group~end \@@_msg_repeated:nnN {#3} {#4} #5 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_show_class:NnnnN}
%   I'm entirely unhappy about this function: I couldn't find a way to
%   test if a class is a single test. Instead, collect the
%   representation of the tests in the class. If that had more than one
%   line, write \texttt{Match} or \texttt{Don't match} on its own line,
%   with the repeating information if any. Then the various tests on
%   lines of their own, and finally a line. Otherwise, we need to
%   evaluate the representation of the tests again (since the prefix is
%   incorrect). That's clunky, but not too expensive, since it's only
%   one test.
%    \begin{macrocode}
\cs_set:Npn \@@_show_class:NnnnN #1#2#3#4#5
  {
    \group_begin:
      \tl_build_begin:N \l_@@_build_tl
      \int_zero:N \l_@@_show_lines_int
      \@@_show_push:n {~}
      #2
    \int_compare:nTF { \l_@@_show_lines_int = 0 }
      {
        \group_end:
        \@@_show_one:n { \bool_if:NTF #1 { Fail } { Pass } }
      }
      {
        \bool_if:nTF
          { #1 && \int_compare_p:n { \l_@@_show_lines_int = 1 } }
          {
            \group_end:
            #2
            \tl_build_put_right:Nn \l_@@_build_tl
              { \@@_msg_repeated:nnN {#3} {#4} #5 }
          }
          {
              \tl_build_end:N \l_@@_build_tl
              \exp_args:NNNo
            \group_end:
            \tl_set:Nn \l_@@_internal_a_tl \l_@@_build_tl
            \@@_show_one:n
              {
                \bool_if:NTF #1 { Match } { Don't~match }
                \@@_msg_repeated:nnN {#3} {#4} #5
              }
            \tl_build_put_right:Nx \l_@@_build_tl
              { \exp_not:o \l_@@_internal_a_tl }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_show_item_catcode:NnT}
%   Produce a sequence of categories which the catcode bitmap |#2|
%   contains, and show it, indenting the tests on which this catcode
%   constraint applies.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_show_item_catcode:NnT #1#2
  {
    \seq_set_split:Nnn \l_@@_internal_seq { } { CBEMTPUDSLOA }
    \seq_set_filter:NNn \l_@@_internal_seq \l_@@_internal_seq
      { \int_if_odd_p:n { #2 / \int_use:c { c_@@_catcode_##1_int } } }
    \@@_show_scope:nn
      {
        categories~
        \seq_map_function:NN \l_@@_internal_seq \use:n
        , ~
        \bool_if:NF #1 { negative~ } class
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_show_item_exact_cs:n}
%    \begin{macrocode}
\cs_new_protected:Npn \@@_show_item_exact_cs:n #1
  {
    \seq_set_split:Nnn \l_@@_internal_seq { \scan_stop: } {#1}
    \seq_set_map_x:NNn \l_@@_internal_seq
      \l_@@_internal_seq { \iow_char:N\\##1 }
    \@@_show_one:n
      { control~sequence~ \seq_use:Nn \l_@@_internal_seq { ~or~ } }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Building}
%
% \subsubsection{Variables used while building}
%
% \begin{variable}{\l_@@_min_state_int, \l_@@_max_state_int}
%   The last state that was allocated is
%   $\cs{l_@@_max_state_int}-1$, so that \cs{l_@@_max_state_int} always
%   points to a free state.  The \texttt{min_state} variable is
%   $1$ to begin with, but gets shifted in nested calls to the matching
%   code, namely in |\c{...}| constructions.
%    \begin{macrocode}
\int_new:N  \l_@@_min_state_int
\int_set:Nn \l_@@_min_state_int { 1 }
\int_new:N  \l_@@_max_state_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_left_state_int, \l_@@_right_state_int}
% \begin{variable}{\l_@@_left_state_seq, \l_@@_right_state_seq}
%   Alternatives are implemented by branching from a \texttt{left} state
%   into the various choices, then merging those into a \texttt{right}
%   state. We store information about those states in two sequences.
%   Those states are also used to implement group quantifiers. Most
%   often, the left and right pointers only differ by~$1$.
%    \begin{macrocode}
\int_new:N  \l_@@_left_state_int
\int_new:N  \l_@@_right_state_int
\seq_new:N  \l_@@_left_state_seq
\seq_new:N  \l_@@_right_state_seq
%    \end{macrocode}
% \end{variable}
% \end{variable}
%
% \begin{variable}{\l_@@_capturing_group_int}
%   \cs{l_@@_capturing_group_int} is the next \textsc{id} number to
%   be assigned to a capturing group. This starts
%   at $0$ for the group enclosing the full regular expression, and
%   groups are counted in the order of their left parenthesis, except
%   when encountering \texttt{resetting} groups.
%    \begin{macrocode}
\int_new:N  \l_@@_capturing_group_int
%    \end{macrocode}
% \end{variable}
%
% \subsubsection{Framework}
%
% This phase is about going from a compiled regex to an \textsc{nfa}.
% Each state of the \textsc{nfa} is stored in a \tn{toks}. The
% operations which can appear in the \tn{toks} are
% \begin{itemize}
%   \item \cs{@@_action_start_wildcard:N} \meta{boolean} inserted at the
%     start of the regular expression, where a \texttt{true}
%     \meta{boolean} makes it unanchored.
%   \item \cs{@@_action_success:} marks the exit state of the
%     \textsc{nfa}.
%   \item \cs{@@_action_cost:n} \Arg{shift} is a transition from the
%     current \meta{state} to $\meta{state}+\meta{shift}$, which
%     consumes the current character: the target state is saved and will
%     be considered again when matching at the next position.
%   \item \cs{@@_action_free:n} \Arg{shift}, and
%     \cs{@@_action_free_group:n} \Arg{shift} are free transitions,
%     which immediately perform the actions for the state
%     $\meta{state}+\meta{shift}$ of the \textsc{nfa}. They differ in
%     how they detect and avoid infinite loops. For now, we just need to
%     know that the \texttt{group} variant must be used for transitions
%     back to the start of a group.
%   \item \cs{@@_action_submatch:nN} \Arg{group} \meta{key} where the
%     \meta{key} is |<| or |>| for the beginning or end of group
%     numbered \meta{group}.  This causes the current position in the
%     query to be stored as the \meta{key} submatch boundary.
%   \item One of these actions, within a conditional.
% \end{itemize}
%
% We strive to preserve the following properties while building.
% \begin{itemize}
%   \item The current capturing group is
%     $\text{\texttt{capturing_group}}-1$, and if a group opened now
%     it would be labelled \texttt{capturing_group}.
%   \item The last allocated state is $\text{\texttt{max_state}}-1$, so
%     \texttt{max_state} is a free state.
%   \item The \texttt{left_state} points to a state to the left of the
%     current group or of the last class.
%   \item The \texttt{right_state} points to a newly created,
%     empty state, with some transitions leading to it.
%   \item The \texttt{left/right} sequences hold a list of the
%     corresponding end-points of nested groups.
% \end{itemize}
%
% \begin{macro}{\@@_build:n, \@@_build_aux:Nn, \@@_build:N, \@@_build_aux:NN}
%   The \texttt{n}-type function first compiles its argument. Reset some
%   variables. Allocate two states, and put a wildcard in state $0$
%   (transitions to state $1$ and $0$ state). Then build the regex
%   within a (capturing) group numbered $0$ (current
%   value of \texttt{capturing_group}). Finally, if the match reaches the
%   last state, it is successful.  A \texttt{false} boolean for argument
%   |#1| for the auxiliaries will suppress the wildcard and make the
%   match anchored: used for \cs{peek_regex:nTF} and similar.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_build:n
  { \@@_build_aux:Nn \c_true_bool }
\cs_new_protected:Npn \@@_build:N
  { \@@_build_aux:NN \c_true_bool }
\cs_new_protected:Npn \@@_build_aux:Nn #1#2
  {
    \@@_compile:n {#2}
    \@@_build_aux:NN #1 \l_@@_internal_regex
  }
\cs_new_protected:Npn \@@_build_aux:NN #1#2
  {
    \@@_standard_escapechar:
    \int_zero:N \l_@@_capturing_group_int
    \int_set_eq:NN \l_@@_max_state_int \l_@@_min_state_int
    \@@_build_new_state:
    \@@_build_new_state:
    \@@_toks_put_right:Nn \l_@@_left_state_int
      { \@@_action_start_wildcard:N #1 }
    \@@_group:nnnN {#2} { 1 } { 0 } \c_false_bool
    \@@_toks_put_right:Nn \l_@@_right_state_int
      { \@@_action_success: }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_build_for_cs:n}
%   The matching code relies on some global intarray variables, but only
%   uses a range of their entries.  Specifically,
%   \begin{itemize}
%   \item \cs{g_@@_state_active_intarray} from \cs{l_@@_min_state_int}
%     to $\cs{l_@@_max_state_int}-1$;
%   \end{itemize}
%   Here, in this nested call to the
%   matching code, we need the new versions of this range to involve
%   completely new entries of the intarray variables, so we begin by
%   setting (the new) \cs{l_@@_min_state_int} to (the old)
%   \cs{l_@@_max_state_int} to use higher entries.
%
%   When using a regex to match a cs, we don't insert a wildcard, we
%   anchor at the end, and since we ignore submatches, there is no need
%   to surround the expression with a group. However, for branches to
%   work properly at the outer level, we need to put the appropriate
%   \texttt{left} and \texttt{right} states in their sequence.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_build_for_cs:n #1
  {
    \int_set_eq:NN \l_@@_min_state_int \l_@@_max_state_int
    \@@_build_new_state:
    \@@_build_new_state:
    \@@_push_lr_states:
    #1
    \@@_pop_lr_states:
    \@@_toks_put_right:Nn \l_@@_right_state_int
      {
        \if_int_compare:w -2 = \l_@@_curr_char_int
          \exp_after:wN \@@_action_success:
        \fi:
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Helpers for building an \textsc{nfa}}
%
% \begin{macro}{\@@_push_lr_states:, \@@_pop_lr_states:}
%   When building the regular expression, we keep track of pointers to
%   the left-end and right-end of each group without help from \TeX{}'s
%   grouping.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_push_lr_states:
  {
    \seq_push:No \l_@@_left_state_seq
      { \int_use:N \l_@@_left_state_int }
    \seq_push:No \l_@@_right_state_seq
      { \int_use:N \l_@@_right_state_int }
  }
\cs_new_protected:Npn \@@_pop_lr_states:
  {
    \seq_pop:NN \l_@@_left_state_seq  \l_@@_internal_a_tl
    \int_set:Nn \l_@@_left_state_int  \l_@@_internal_a_tl
    \seq_pop:NN \l_@@_right_state_seq \l_@@_internal_a_tl
    \int_set:Nn \l_@@_right_state_int \l_@@_internal_a_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_build_transition_left:NNN,
%     \@@_build_transition_right:nNn
%   }
%   Add a transition from |#2| to |#3| using the function |#1|. The
%   \texttt{left} function is used for higher priority transitions, and
%   the \texttt{right} function for lower priority transitions (which
%   should be performed later). The signatures differ to reflect the
%   differing usage later on. Both functions could be optimized.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_build_transition_left:NNN #1#2#3
  { \@@_toks_put_left:Nx  #2 { #1 { \int_eval:n { #3 - #2 } } } }
\cs_new_protected:Npn \@@_build_transition_right:nNn #1#2#3
  { \@@_toks_put_right:Nx #2 { #1 { \int_eval:n { #3 - #2 } } } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_build_new_state:}
%   Add a new empty state to the \textsc{nfa}. Then update the
%   \texttt{left}, \texttt{right}, and \texttt{max} states, so that the
%   \texttt{right} state is the new empty state, and the \texttt{left}
%   state points to the previously \enquote{current} state.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_build_new_state:
  {
    \@@_toks_clear:N \l_@@_max_state_int
    \int_set_eq:NN \l_@@_left_state_int \l_@@_right_state_int
    \int_set_eq:NN \l_@@_right_state_int \l_@@_max_state_int
    \int_incr:N \l_@@_max_state_int
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_build_transitions_lazyness:NNNNN}
%   This function creates a new state, and puts two transitions starting
%   from the old current state. The order of the transitions is
%   controlled by |#1|, true for lazy quantifiers, and false for greedy
%   quantifiers.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_build_transitions_lazyness:NNNNN #1#2#3#4#5
  {
    \@@_build_new_state:
    \@@_toks_put_right:Nx \l_@@_left_state_int
      {
        \if_meaning:w \c_true_bool #1
          #2 { \int_eval:n { #3 - \l_@@_left_state_int } }
          #4 { \int_eval:n { #5 - \l_@@_left_state_int } }
        \else:
          #4 { \int_eval:n { #5 - \l_@@_left_state_int } }
          #2 { \int_eval:n { #3 - \l_@@_left_state_int } }
        \fi:
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Building classes}
%
% \begin{macro}{\@@_class:NnnnN}
% \begin{macro}[rEXP]{\@@_tests_action_cost:n}
%   The arguments are: \meta{boolean} \Arg{tests} \Arg{min} \Arg{more}
%   \meta{lazyness}. First store the tests with a trailing
%   \cs{@@_action_cost:n}, in the true branch of
%   \cs{@@_break_point:TF} for positive classes, or the false branch
%   for negative classes. The integer \meta{more} is $0$ for fixed
%   repetitions, $-1$ for unbounded repetitions, and
%   $\meta{max}-\meta{min}$ for a range of repetitions.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_class:NnnnN #1#2#3#4#5
  {
    \cs_set:Npx \@@_tests_action_cost:n ##1
      {
        \exp_not:n { \exp_not:n {#2} }
        \bool_if:NTF #1
          { \@@_break_point:TF { \@@_action_cost:n {##1} } { } }
          { \@@_break_point:TF { } { \@@_action_cost:n {##1} } }
      }
    \if_case:w - #4 \exp_stop_f:
           \@@_class_repeat:n   {#3}
    \or:   \@@_class_repeat:nN  {#3}      #5
    \else: \@@_class_repeat:nnN {#3} {#4} #5
    \fi:
  }
\cs_new:Npn \@@_tests_action_cost:n { \@@_action_cost:n }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_class_repeat:n}
%   This is used for a fixed number of repetitions. Build one state for
%   each repetition, with a transition controlled by the tests that we
%   have collected. That works just fine for |#1|${}=0$ repetitions:
%   nothing is built.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_class_repeat:n #1
  {
    \prg_replicate:nn {#1}
      {
        \@@_build_new_state:
        \@@_build_transition_right:nNn \@@_tests_action_cost:n
          \l_@@_left_state_int \l_@@_right_state_int
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_class_repeat:nN}
%   This implements unbounded repetitions of a single class (\emph{e.g.}
%   the |*| and |+| quantifiers). If the minimum number |#1| of
%   repetitions is $0$, then build a transition from the current state
%   to itself governed by the tests, and a free transition to a new
%   state (hence skipping the tests). Otherwise, call
%   \cs{@@_class_repeat:n} for the code to match |#1| repetitions,
%   and add free transitions from the last state to the previous one,
%   and to a new one. In both cases, the order of transitions is
%   controlled by the lazyness boolean |#2|.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_class_repeat:nN #1#2
  {
    \if_int_compare:w #1 = \c_zero_int
      \@@_build_transitions_lazyness:NNNNN #2
        \@@_action_free:n       \l_@@_right_state_int
        \@@_tests_action_cost:n \l_@@_left_state_int
    \else:
      \@@_class_repeat:n {#1}
      \int_set_eq:NN \l_@@_internal_a_int \l_@@_left_state_int
      \@@_build_transitions_lazyness:NNNNN #2
        \@@_action_free:n \l_@@_right_state_int
        \@@_action_free:n \l_@@_internal_a_int
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_class_repeat:nnN}
%   We want to build the code to match from |#1| to $|#1|+|#2|$
%   repetitions. Match |#1| repetitions (can be $0$). Compute the final
%   state of the next construction as \texttt{a}. Build $|#2|>0$ states,
%   each with a transition to the next state governed by the tests, and
%   a transition to the final state \texttt{a}. The computation of
%   \texttt{a} is safe because states are allocated in order, starting
%   from \texttt{max_state}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_class_repeat:nnN #1#2#3
  {
    \@@_class_repeat:n {#1}
    \int_set:Nn \l_@@_internal_a_int
      { \l_@@_max_state_int + #2 - 1 }
    \prg_replicate:nn { #2 }
      {
        \@@_build_transitions_lazyness:NNNNN #3
          \@@_action_free:n       \l_@@_internal_a_int
          \@@_tests_action_cost:n \l_@@_right_state_int
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Building groups}
%
% \begin{macro}{\@@_group_aux:nnnnN}
%   Arguments: \Arg{label} \Arg{contents} \Arg{min} \Arg{more}
%   \meta{lazyness}. If \meta{min} is $0$, we need to add a state before
%   building the group, so that the thread which skips the group does
%   not also set the start-point of the submatch. After adding one more
%   state, the \texttt{left_state} is the left end of the group, from
%   which all branches stem, and the \texttt{right_state} is the
%   right end of the group, and all branches end their course in that
%   state. We store those two integers to be queried for each branch, we
%   build the \textsc{nfa} states for the contents |#2| of the group,
%   and we forget about the two integers. Once this is done, perform the
%   repetition: either exactly |#3| times, or |#3| or more times, or
%   between |#3| and $|#3|+|#4|$ times, with lazyness |#5|. The
%   \meta{label} |#1| is used for submatch tracking. Each of the three
%   auxiliaries expects \texttt{left_state} and \texttt{right_state} to
%   be set properly.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_aux:nnnnN #1#2#3#4#5
  {
      \if_int_compare:w #3 = \c_zero_int
        \@@_build_new_state:
        \@@_build_transition_right:nNn \@@_action_free_group:n
          \l_@@_left_state_int \l_@@_right_state_int
      \fi:
      \@@_build_new_state:
      \@@_push_lr_states:
      #2
      \@@_pop_lr_states:
      \if_case:w - #4 \exp_stop_f:
             \@@_group_repeat:nn   {#1} {#3}
      \or:   \@@_group_repeat:nnN  {#1} {#3}      #5
      \else: \@@_group_repeat:nnnN {#1} {#3} {#4} #5
      \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_group:nnnN, \@@_group_no_capture:nnnN}
%   Hand to \cs{@@_group_aux:nnnnnN} the label of that group
%   (expanded), and the group itself, with some extra commands to
%   perform.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group:nnnN #1
  {
    \exp_args:No \@@_group_aux:nnnnN
      { \int_use:N \l_@@_capturing_group_int }
      {
        \int_incr:N \l_@@_capturing_group_int
        #1
      }
  }
\cs_new_protected:Npn \@@_group_no_capture:nnnN
  { \@@_group_aux:nnnnN { -1 } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_group_resetting:nnnN}
% \begin{macro}{\@@_group_resetting_loop:nnNn}
%   Again, hand the label $-1$ to \cs{@@_group_aux:nnnnN}, but this
%   time we work a little bit harder to keep track of the maximum group
%   label at the end of any branch, and to reset the group number at
%   each branch. This relies on the fact that a compiled regex always is
%   a sequence of items of the form \cs{@@_branch:n} \Arg{branch}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_resetting:nnnN #1
  {
    \@@_group_aux:nnnnN { -1 }
      {
        \exp_args:Noo \@@_group_resetting_loop:nnNn
          { \int_use:N \l_@@_capturing_group_int }
          { \int_use:N \l_@@_capturing_group_int }
          #1
          { ?? \prg_break:n } { }
        \prg_break_point:
      }
  }
\cs_new_protected:Npn \@@_group_resetting_loop:nnNn #1#2#3#4
  {
    \use_none:nn #3 { \int_set:Nn \l_@@_capturing_group_int {#1} }
    \int_set:Nn \l_@@_capturing_group_int {#2}
    #3 {#4}
    \exp_args:Nf \@@_group_resetting_loop:nnNn
      { \int_max:nn {#1} { \l_@@_capturing_group_int } }
      {#2}
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_branch:n}
%   Add a free transition from the left state of the current group to a
%   brand new state, starting point of this branch. Once the branch is
%   built, add a transition from its last state to the right state of
%   the group. The left and right states of the group are extracted from
%   the relevant sequences.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_branch:n #1
  {
    \@@_build_new_state:
    \seq_get:NN \l_@@_left_state_seq \l_@@_internal_a_tl
    \int_set:Nn \l_@@_left_state_int \l_@@_internal_a_tl
    \@@_build_transition_right:nNn \@@_action_free:n
      \l_@@_left_state_int \l_@@_right_state_int
    #1
    \seq_get:NN \l_@@_right_state_seq \l_@@_internal_a_tl
    \@@_build_transition_right:nNn \@@_action_free:n
      \l_@@_right_state_int \l_@@_internal_a_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_group_repeat:nn}
%   This function is called to repeat a group a fixed number of times
%   |#2|; if this is $0$ we remove the group altogether (but don't reset
%   the \texttt{capturing_group} label). Otherwise, the auxiliary
%   \cs{@@_group_repeat_aux:n} copies |#2| times the \tn{toks} for
%   the group, and leaves \texttt{internal_a} pointing to the left end
%   of the last repetition. We only record the submatch information at
%   the last repetition. Finally, add a state at the end (the transition
%   to it has been taken care of by the replicating auxiliary.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_repeat:nn #1#2
  {
    \if_int_compare:w #2 = \c_zero_int
      \int_set:Nn \l_@@_max_state_int
        { \l_@@_left_state_int - 1 }
      \@@_build_new_state:
    \else:
      \@@_group_repeat_aux:n {#2}
      \@@_group_submatches:nNN {#1}
        \l_@@_internal_a_int \l_@@_right_state_int
      \@@_build_new_state:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_group_submatches:nNN}
%   This inserts in states |#2| and |#3| the code for tracking
%   submatches of the group |#1|, unless inhibited by a label of $-1$.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_submatches:nNN #1#2#3
  {
    \if_int_compare:w #1 > - \c_one_int
      \@@_toks_put_left:Nx #2 { \@@_action_submatch:nN {#1} < }
      \@@_toks_put_left:Nx #3 { \@@_action_submatch:nN {#1} > }
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_group_repeat_aux:n}
%   Here we repeat \tn{toks} ranging from \texttt{left_state} to
%   \texttt{max_state}, $|#1|>0$ times. First add a transition so that
%   the copies \enquote{chain} properly. Compute the shift
%   \texttt{c} between the original copy and the last copy we
%   want. Shift the \texttt{right_state} and \texttt{max_state} to their
%   final values. We then want to perform \texttt{c} copy operations. At
%   the end, \texttt{b} is equal to the \texttt{max_state}, and
%   \texttt{a} points to the left of the last copy of the group.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_repeat_aux:n #1
  {
    \@@_build_transition_right:nNn \@@_action_free:n
      \l_@@_right_state_int \l_@@_max_state_int
    \int_set_eq:NN \l_@@_internal_a_int \l_@@_left_state_int
    \int_set_eq:NN \l_@@_internal_b_int \l_@@_max_state_int
    \if_int_compare:w \int_eval:n {#1} > \c_one_int
      \int_set:Nn \l_@@_internal_c_int
        {
          ( #1 - 1 )
          * ( \l_@@_internal_b_int - \l_@@_internal_a_int )
        }
      \int_add:Nn \l_@@_right_state_int { \l_@@_internal_c_int }
      \int_add:Nn \l_@@_max_state_int   { \l_@@_internal_c_int }
      \@@_toks_memcpy:NNn
        \l_@@_internal_b_int
        \l_@@_internal_a_int
        \l_@@_internal_c_int
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_group_repeat:nnN}
%   This function is called to repeat a group at least $n$ times; the
%   case $n=0$ is very different from $n>0$. Assume first that $n=0$.
%   Insert submatch tracking information at the start and end of the
%   group, add a free transition from the right end to the
%   \enquote{true} left state \texttt{a} (remember: in this case we had
%   added an extra state before the left state). This forms the loop,
%   which we break away from by adding a free transition from \texttt{a}
%   to a new state.
%
%   Now consider the case $n>0$. Repeat the group $n$ times, chaining
%   various copies with a free transition. Add submatch tracking only to
%   the last copy, then add a free transition from the right end back to
%   the left end of the last copy, either before or after the transition
%   to move on towards the rest of the \textsc{nfa}. This transition can
%   end up before submatch tracking, but that is irrelevant since it
%   only does so when going again through the group, recording new
%   matches. Finally, add a state; we already have a transition pointing
%   to it from \cs{@@_group_repeat_aux:n}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_repeat:nnN #1#2#3
  {
    \if_int_compare:w #2 = \c_zero_int
      \@@_group_submatches:nNN {#1}
        \l_@@_left_state_int \l_@@_right_state_int
      \int_set:Nn \l_@@_internal_a_int
        { \l_@@_left_state_int - 1 }
      \@@_build_transition_right:nNn \@@_action_free:n
        \l_@@_right_state_int \l_@@_internal_a_int
      \@@_build_new_state:
      \if_meaning:w \c_true_bool #3
        \@@_build_transition_left:NNN \@@_action_free:n
          \l_@@_internal_a_int \l_@@_right_state_int
      \else:
        \@@_build_transition_right:nNn \@@_action_free:n
          \l_@@_internal_a_int \l_@@_right_state_int
      \fi:
    \else:
      \@@_group_repeat_aux:n {#2}
      \@@_group_submatches:nNN {#1}
        \l_@@_internal_a_int \l_@@_right_state_int
      \if_meaning:w \c_true_bool #3
        \@@_build_transition_right:nNn \@@_action_free_group:n
          \l_@@_right_state_int \l_@@_internal_a_int
      \else:
        \@@_build_transition_left:NNN \@@_action_free_group:n
          \l_@@_right_state_int \l_@@_internal_a_int
      \fi:
      \@@_build_new_state:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_group_repeat:nnnN}
%   We wish to repeat the group between |#2| and $|#2|+|#3|$ times, with
%   a lazyness controlled by |#4|. We insert submatch tracking up front:
%   in principle, we could avoid recording submatches for the first |#2|
%   copies of the group, but that forces us to treat specially the case
%   $|#2|=0$. Repeat that group with submatch tracking $|#2|+|#3|$ times
%   (the maximum number of repetitions). Then our goal is to add |#3|
%   transitions from the end of the |#2|-th group, and each subsequent
%   groups, to the end. For a lazy quantifier, we add those transitions
%   to the left states, before submatch tracking. For the greedy case,
%   we add the transitions to the right states, after submatch tracking
%   and the transitions which go on with more repetitions. In the greedy
%   case with $|#2|=0$, the transition which skips over all copies of
%   the group must be added separately, because its starting state does
%   not follow the normal pattern: we had to add it \enquote{by hand}
%   earlier.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_repeat:nnnN #1#2#3#4
  {
    \@@_group_submatches:nNN {#1}
      \l_@@_left_state_int \l_@@_right_state_int
    \@@_group_repeat_aux:n { #2 + #3 }
    \if_meaning:w \c_true_bool #4
      \int_set_eq:NN \l_@@_left_state_int \l_@@_max_state_int
      \prg_replicate:nn { #3 }
        {
          \int_sub:Nn \l_@@_left_state_int
            { \l_@@_internal_b_int - \l_@@_internal_a_int }
          \@@_build_transition_left:NNN \@@_action_free:n
            \l_@@_left_state_int \l_@@_max_state_int
        }
    \else:
      \prg_replicate:nn { #3 - 1 }
        {
          \int_sub:Nn \l_@@_right_state_int
            { \l_@@_internal_b_int - \l_@@_internal_a_int }
          \@@_build_transition_right:nNn \@@_action_free:n
            \l_@@_right_state_int \l_@@_max_state_int
        }
      \if_int_compare:w #2 = \c_zero_int
        \int_set:Nn \l_@@_right_state_int
          { \l_@@_left_state_int - 1 }
      \else:
        \int_sub:Nn \l_@@_right_state_int
          { \l_@@_internal_b_int - \l_@@_internal_a_int }
      \fi:
      \@@_build_transition_right:nNn \@@_action_free:n
        \l_@@_right_state_int \l_@@_max_state_int
    \fi:
    \@@_build_new_state:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Others}
%
% \begin{macro}{\@@_assertion:Nn, \@@_b_test:, \@@_A_test:, \@@_G_test:, \@@_Z_test:}
%   Usage: \cs{@@_assertion:Nn} \meta{boolean} \Arg{test}, where the
%   \meta{test} is either of the two other functions. Add a free
%   transition to a new state, conditionally to the assertion test. The
%   \cs{@@_b_test:} test is used by the |\b| and |\B| escape: check
%   if the last character was a word character or not, and do the same
%   to the current character. The boundary-markers of the string are
%   non-word characters for this purpose.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_assertion:Nn #1#2
  {
    \@@_build_new_state:
    \@@_toks_put_right:Nx \l_@@_left_state_int
      {
        \exp_not:n {#2}
        \@@_break_point:TF
          \bool_if:NF #1 { { } }
          {
            \@@_action_free:n
              {
                \int_eval:n
                  { \l_@@_right_state_int - \l_@@_left_state_int }
              }
          }
          \bool_if:NT #1 { { } }
      }
  }
\cs_new_protected:Npn \@@_b_test:
  {
    \group_begin:
      \int_set_eq:NN \l_@@_curr_char_int \l_@@_last_char_int
      \@@_prop_w:
      \@@_break_point:TF
        { \group_end: \@@_item_reverse:n \@@_prop_w: }
        { \group_end: \@@_prop_w: }
  }
\cs_new_protected:Npn \@@_Z_test:
  {
    \if_int_compare:w -2 = \l_@@_curr_char_int
      \exp_after:wN \@@_break_true:w
    \fi:
  }
\cs_new_protected:Npn \@@_A_test:
  {
    \if_int_compare:w -2 = \l_@@_last_char_int
      \exp_after:wN \@@_break_true:w
    \fi:
  }
\cs_new_protected:Npn \@@_G_test:
  {
    \if_int_compare:w \l_@@_curr_pos_int = \l_@@_start_pos_int
      \exp_after:wN \@@_break_true:w
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_command_K:}
%   Change the starting point of the $0$-th submatch (full match), and
%   transition to a new state, pretending that this is a fresh thread.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_command_K:
  {
    \@@_build_new_state:
    \@@_toks_put_right:Nx \l_@@_left_state_int
      {
        \@@_action_submatch:nN { 0 } <
        \bool_set_true:N \l_@@_fresh_thread_bool
        \@@_action_free:n
          {
            \int_eval:n
              { \l_@@_right_state_int - \l_@@_left_state_int }
          }
        \bool_set_false:N \l_@@_fresh_thread_bool
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Matching}
%
% We search for matches by running all the execution threads through the
% \textsc{nfa} in parallel, reading one token of the query at each step.
% The \textsc{nfa} contains \enquote{free} transitions to other states,
% and transitions which \enquote{consume} the current token.  For free
% transitions, the instruction at the new state of the \textsc{nfa} is
% performed immediately.  When a transition consumes a character, the
% new state is appended to a list of \enquote{active states}, stored in
% \cs{g_@@_thread_info_intarray} (together with submatch information):
% this thread is made active again when the next
% token is read from the query.  At every step (for each token in the
% query), we unpack that list of active states and the corresponding
% submatch props, and empty those.
%
% If two paths through the \textsc{nfa} \enquote{collide} in the sense
% that they reach the same state after reading a given token, then they
% only differ in how they previously matched, and any future execution
% would be identical for both. (Note that this would be wrong in the
% presence of back-references.) Hence, we only need to keep one of the
% two threads: the thread with the highest priority. Our \textsc{nfa} is
% built in such a way that higher priority actions always come before
% lower priority actions, which makes things work.
%
% The explanation in the previous paragraph may make us think that we
% simply need to keep track of which states were visited at a given
% step: after all, the loop generated when matching |(a?)*| against |a|
% is broken, isn't it? No. The group first matches |a|, as it should,
% then repeats; it attempts to match |a| again but fails; it skips |a|,
% and finds out that this state has already been seen at this position
% in the query: the match stops. The capturing group is (wrongly) |a|.
% What went wrong is that a thread collided with itself, and the later
% version, which has gone through the group one more times with an empty
% match, should have a higher priority than not going through the group.
%
% We solve this by distinguishing \enquote{normal} free transitions
% \cs{@@_action_free:n} from transitions
% \cs{@@_action_free_group:n} which go back to the start of the
% group. The former keeps threads unless they have been visited by a
% \enquote{completed} thread, while the latter kind of transition also
% prevents going back to a state visited by the current thread.
%
% \subsubsection{Variables used when matching}
%
% \begin{variable}
%   {
%     \l_@@_min_pos_int,
%     \l_@@_max_pos_int,
%     \l_@@_curr_pos_int,
%     \l_@@_start_pos_int,
%     \l_@@_success_pos_int,
%   }
%   The tokens in the query are indexed from \texttt{min_pos} for the
%   first to $\texttt{max_pos}-1$ for the last, and their information is
%   stored in several arrays and \tn{toks} registers with those numbers.
%   We match
%   without backtracking, keeping all threads in lockstep at the
%   \texttt{curr_pos} in the query. The starting point of the current
%   match attempt is \texttt{start_pos}, and \texttt{success_pos},
%   updated whenever a thread succeeds, is used as the next starting
%   position.
%    \begin{macrocode}
\int_new:N \l_@@_min_pos_int
\int_new:N \l_@@_max_pos_int
\int_new:N \l_@@_curr_pos_int
\int_new:N \l_@@_start_pos_int
\int_new:N \l_@@_success_pos_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {
%     \l_@@_curr_char_int,
%     \l_@@_curr_catcode_int,
%     \l_@@_curr_token_tl,
%     \l_@@_last_char_int,
%     \l_@@_last_char_success_int,
%     \l_@@_case_changed_char_int
%   }
%   The character and category codes of the token at the current
%   position and a token list expanding to that token; the character
%   code of the token at the previous position;
%   the character code of the token just before a successful match;
%   and the character code of the result of changing the case of the
%   current token (|A-Z|$\leftrightarrow$|a-z|). This last integer is
%   only computed when necessary, and is otherwise \cs{c_max_int}.  The
%   \texttt{curr_char} variable is also used in various other phases
%   to hold a character code.
%    \begin{macrocode}
\int_new:N \l_@@_curr_char_int
\int_new:N \l_@@_curr_catcode_int
\tl_new:N \l_@@_curr_token_tl
\int_new:N \l_@@_last_char_int
\int_new:N \l_@@_last_char_success_int
\int_new:N \l_@@_case_changed_char_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_curr_state_int}
%   For every character in the token list, each of the active states is
%   considered in turn.  The variable \cs{l_@@_curr_state_int}
%   holds the state of the \textsc{nfa} which is currently considered:
%   transitions are then given as shifts relative to the current state.
%    \begin{macrocode}
\int_new:N \l_@@_curr_state_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {\l_@@_curr_submatches_tl, \l_@@_success_submatches_tl}
%   The submatches for the thread which is currently active are stored
%   in the \texttt{curr_submatches} list, which is almost a comma list,
%   but ends with a comma. This list is stored by \cs{@@_store_state:n}
%   into an intarray variable, to be retrieved when matching at the next
%   position. When a thread succeeds, this list is copied to
%   \cs{l_@@_success_submatches_tl}: only the last successful thread
%   remains there.
%    \begin{macrocode}
\tl_new:N \l_@@_curr_submatches_tl
\tl_new:N \l_@@_success_submatches_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_step_int}
%   This integer, always even, is increased every time a character in
%   the query is read, and not reset when doing multiple matches.  We
%   store in \cs{g_@@_state_active_intarray} the last step in which each
%   \meta{state} in the \textsc{nfa} was encountered. This lets us break
%   infinite loops by not visiting the same state twice in the same
%   step. In fact, the step we store is equal to \texttt{step} when we
%   have started performing the operations of \tn{toks}\meta{state}, but
%   not finished yet. However, once we finish, we store
%   $\text{\texttt{step}}+1$ in \cs{g_@@_state_active_intarray}.  This is
%   needed to track submatches
%   properly (see building phase). The \texttt{step} is also used to
%   attach each set of submatch information to a given iteration (and
%   automatically discard it when it corresponds to a past step).
%    \begin{macrocode}
\int_new:N \l_@@_step_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_min_thread_int, \l_@@_max_thread_int}
%   All the currently active threads are kept in order of precedence in
%   \cs{g_@@_thread_info_intarray} together with the corresponding
%   submatch information.  Data in this intarray is organized as blocks
%   from \texttt{min_thread} (included) to \texttt{max_thread}
%   (excluded).  At the start of every step, the whole array is
%   unpacked, so that the space can immediately be reused, and
%   \texttt{max_thread} is reset to \texttt{min_thread}, effectively
%   clearing the array.
%    \begin{macrocode}
\int_new:N \l_@@_min_thread_int
\int_new:N \l_@@_max_thread_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_state_active_intarray, \g_@@_thread_info_intarray}
%   \cs{g_@@_state_active_intarray} stores the last \meta{step} in which
%   each \meta{state} was active.  \cs{g_@@_thread_info_intarray} stores
%   threads to be considered in the next step, more precisely the
%   states in which these threads are.
%    \begin{macrocode}
\intarray_new:Nn \g_@@_state_active_intarray { 65536 }
\intarray_new:Nn \g_@@_thread_info_intarray { 65536 }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_matched_analysis_tl, \l_@@_curr_analysis_tl}
%   The list \cs{l_@@_curr_analysis_tl} consists of a brace group
%   containing three brace groups corresponding to the current token,
%   with the same syntax as \cs{tl_analysis_map_inline:nn}.  The list
%   \cs{l_@@_matched_analysis_tl} (constructed under the
%   \texttt{tl\_build} machinery) has one item for each token that has
%   already been treated so far in a given match attempt: each item
%   consists of three brace groups with the same syntax as
%   \cs{tl_analysis_map_inline:nn}.
%    \begin{macrocode}
\tl_new:N \l_@@_matched_analysis_tl
\tl_new:N \l_@@_curr_analysis_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_every_match_tl}
%   Every time a match is found, this token list is used.  For single
%   matching, the token list is empty. For multiple matching, the token
%   list is set to repeat the matching, after performing some operation
%   which depends on the user function. See \cs{@@_single_match:} and
%   \cs{@@_multi_match:n}.
%    \begin{macrocode}
\tl_new:N \l_@@_every_match_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_fresh_thread_bool, \l_@@_empty_success_bool}
% \begin{macro}{\@@_if_two_empty_matches:F}
%   When doing multiple matches, we need to avoid infinite loops where
%   each iteration matches the same empty token list. When an empty
%   token list is matched, the next successful match of the same empty
%   token list is suppressed. We detect empty matches by setting
%   \cs{l_@@_fresh_thread_bool} to \texttt{true} for threads which
%   directly come from the start of the regex or from the |\K| command,
%   and testing that boolean whenever a thread succeeds. The function
%   \cs{@@_if_two_empty_matches:F} is redefined at every match
%   attempt, depending on whether the previous match was empty or not:
%   if it was, then the function must cancel a purported success if it
%   is empty and at the same spot as the previous match; otherwise, we
%   definitely don't have two identical empty matches, so the function
%   is \cs{use:n}.
%    \begin{macrocode}
\bool_new:N \l_@@_fresh_thread_bool
\bool_new:N \l_@@_empty_success_bool
\cs_new_eq:NN \@@_if_two_empty_matches:F \use:n
%    \end{macrocode}
% \end{macro}
% \end{variable}
%
% \begin{variable}
%   {
%     \g_@@_success_bool,
%     \l_@@_saved_success_bool,
%     \l_@@_match_success_bool
%   }
%   The boolean \cs{l_@@_match_success_bool} is true if the current
%   match attempt was successful, and \cs{g_@@_success_bool} is true
%   if there was at least one successful match. This is the only global
%   variable in this whole module, but we would need it to be local when
%   matching a control sequence with |\c{...}|. This is done by saving
%   the global variable into \cs{l_@@_saved_success_bool}, which is
%   local, hence not affected by the changes due to inner regex
%   functions.
%    \begin{macrocode}
\bool_new:N \g_@@_success_bool
\bool_new:N \l_@@_saved_success_bool
\bool_new:N \l_@@_match_success_bool
%    \end{macrocode}
% \end{variable}
%
% \subsubsection{Matching: framework}
%
% \begin{macro}{\@@_match:n, \@@_match_cs:n}
% \begin{macro}{\@@_match_init:}
%   Initialize the variables that should
%   be set once for each user function (even for multiple
%   matches). Namely, the overall matching is not yet successful; none of
%   the states should be marked as visited (\cs{g_@@_state_active_intarray}), and
%   we start at step $0$; we pretend that there was a previous match
%   ending at the start of the query, which was not empty (to avoid
%   smothering an empty match at the start). Once all this is set up, we
%   are ready for the ride. Find the first match.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_match:n #1
  {
    \@@_match_init:
    \@@_match_once_init:
    \tl_analysis_map_inline:nn {#1}
      { \@@_match_one_token:nnN {##1} {##2} ##3 }
    \@@_match_one_token:nnN { } { -2 } F
    \prg_break_point:Nn \@@_maplike_break: { }
  }
\cs_new_protected:Npn \@@_match_cs:n #1
  {
    \int_set_eq:NN \l_@@_min_thread_int \l_@@_max_thread_int
    \@@_match_init:
    \@@_match_once_init:
    \str_map_inline:nn {#1}
      {
        \tl_if_blank:nTF {##1}
          { \@@_match_one_token:nnN {##1} {`##1} A }
          { \@@_match_one_token:nnN {##1} {`##1} C }
      }
    \@@_match_one_token:nnN { } { -2 } F
    \prg_break_point:Nn \@@_maplike_break: { }
  }
\cs_new_protected:Npn \@@_match_init:
  {
    \bool_gset_false:N \g_@@_success_bool
    \int_step_inline:nnn
      \l_@@_min_state_int { \l_@@_max_state_int - 1 }
      {
        \__kernel_intarray_gset:Nnn
          \g_@@_state_active_intarray {##1} { 1 }
      }
    \int_zero:N \l_@@_step_int
    \int_set:Nn \l_@@_min_pos_int { 2 }
    \int_set_eq:NN \l_@@_success_pos_int \l_@@_min_pos_int
    \int_set:Nn \l_@@_last_char_success_int { -2 }
    \tl_build_begin:N \l_@@_matched_analysis_tl
    \tl_clear:N \l_@@_curr_analysis_tl
    \int_set:Nn \l_@@_min_submatch_int { 1 }
    \int_set_eq:NN \l_@@_submatch_int \l_@@_min_submatch_int
    \bool_set_false:N \l_@@_empty_success_bool
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_match_once_init:}
%   This function resets various variables used when finding one match.
%   It is called before the loop through characters, and every time we
%   find a match, before searching for another match (this is controlled
%   by the \texttt{every_match} token list).
%
%   First initialize some variables: set the
%   conditional which detects identical empty matches; this match
%   attempt starts at the previous \texttt{success_pos}, is not yet
%   successful, and has no submatches yet; clear the array of active
%   threads, and put the starting state $0$ in it. We are then almost
%   ready to read our first token in the query, but we actually start
%   one position earlier than the start because
%   \cs{@@_match_one_token:nnN} increments \cs{l_@@_curr_pos_int} and
%   saves \cs{l_@@_curr_char_int} as the \texttt{last_char} so that word
%   boundaries can be correctly identified.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_match_once_init:
  {
    \if_meaning:w \c_true_bool \l_@@_empty_success_bool
      \cs_set:Npn \@@_if_two_empty_matches:F
        {
          \int_compare:nNnF
            \l_@@_start_pos_int = \l_@@_curr_pos_int
        }
    \else:
      \cs_set_eq:NN \@@_if_two_empty_matches:F \use:n
    \fi:
    \int_set_eq:NN \l_@@_start_pos_int \l_@@_success_pos_int
    \bool_set_false:N \l_@@_match_success_bool
    \tl_set:Nx \l_@@_curr_submatches_tl
      { \prg_replicate:nn { 2 * \l_@@_capturing_group_int } { 0 , } }
    \int_set_eq:NN \l_@@_max_thread_int \l_@@_min_thread_int
    \@@_store_state:n { \l_@@_min_state_int }
    \int_set:Nn \l_@@_curr_pos_int
      { \l_@@_start_pos_int - 1 }
    \int_set_eq:NN \l_@@_curr_char_int \l_@@_last_char_success_int
    \tl_build_get:NN \l_@@_matched_analysis_tl \l_@@_internal_a_tl
    \exp_args:NNf \@@_match_once_init_aux:
    \tl_map_inline:nn
      { \exp_after:wN \l_@@_internal_a_tl \l_@@_curr_analysis_tl }
      { \@@_match_one_token:nnN ##1 }
    \prg_break_point:Nn \@@_maplike_break: { }
  }
\cs_new_protected:Npn \@@_match_once_init_aux:
  {
    \tl_build_clear:N \l_@@_matched_analysis_tl
    \tl_clear:N \l_@@_curr_analysis_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_single_match:, \@@_multi_match:n}
%   For a single match, the overall success is determined by whether the
%   only match attempt is a success. When doing multiple matches, the
%   overall matching is successful as soon as any match
%   succeeds. Perform the action |#1|, then find the next match.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_single_match:
  {
    \tl_set:Nn \l_@@_every_match_tl
      {
        \bool_gset_eq:NN
          \g_@@_success_bool
          \l_@@_match_success_bool
        \@@_maplike_break:
      }
  }
\cs_new_protected:Npn \@@_multi_match:n #1
  {
    \tl_set:Nn \l_@@_every_match_tl
      {
        \if_meaning:w \c_false_bool \l_@@_match_success_bool
          \exp_after:wN \@@_maplike_break:
        \fi:
        \bool_gset_true:N \g_@@_success_bool
        #1
        \@@_match_once_init:
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_match_one_token:nnN}
% \begin{macro}[rEXP]{\@@_match_one_active:n}
%   At each new position, set some variables and get the new character
%   and category from the query. Then unpack the array of active
%   threads, and clear it by resetting its length
%   (\texttt{max_thread}). This results in a sequence of
%   \cs{@@_use_state_and_submatches:w} \meta{state}|,|\meta{submatch-clist}|;| and
%   we consider those states one by one in order. As soon as a thread
%   succeeds, exit the step, and, if there are threads to consider at the
%   next position, and we have not reached the end of the string,
%   repeat the loop. Otherwise, the last thread that succeeded is the
%   match.  We explain the \texttt{fresh_thread} business when
%   describing \cs{@@_action_wildcard:}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_match_one_token:nnN #1#2#3
  {
    \int_add:Nn \l_@@_step_int { 2 }
    \int_incr:N \l_@@_curr_pos_int
    \int_set_eq:NN \l_@@_last_char_int \l_@@_curr_char_int
    \cs_set_eq:NN \@@_maybe_compute_ccc: \@@_compute_case_changed_char:
    \tl_set:Nn \l_@@_curr_token_tl {#1}
    \int_set:Nn \l_@@_curr_char_int {#2}
    \int_set:Nn \l_@@_curr_catcode_int { "#3 }
    \tl_build_put_right:Nx \l_@@_matched_analysis_tl
      { \exp_not:o \l_@@_curr_analysis_tl }
    \tl_set:Nn \l_@@_curr_analysis_tl { { {#1} {#2} #3 } }
    \use:x
      {
        \int_set_eq:NN \l_@@_max_thread_int \l_@@_min_thread_int
        \int_step_function:nnN
          { \l_@@_min_thread_int }
          { \l_@@_max_thread_int - 1 }
          \@@_match_one_active:n
      }
    \prg_break_point:
    \bool_set_false:N \l_@@_fresh_thread_bool
    \if_int_compare:w \l_@@_max_thread_int > \l_@@_min_thread_int
      \if_int_compare:w -2 < \l_@@_curr_char_int
        \exp_after:wN \exp_after:wN \exp_after:wN \use_none:n
      \fi:
    \fi:
    \l_@@_every_match_tl
  }
\cs_new:Npn \@@_match_one_active:n #1
  {
    \@@_use_state_and_submatches:w
    \__kernel_intarray_range_to_clist:Nnn
      \g_@@_thread_info_intarray
      { 1 + #1 * (\l_@@_capturing_group_int * 2 + 1) }
      { (1 + #1) * (\l_@@_capturing_group_int * 2 + 1) }
    ;
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Using states of the \textsc{nfa}}
%
% \begin{macro}{\@@_use_state:}
%   Use the current \textsc{nfa} instruction. The state is initially
%   marked as belonging to the current \texttt{step}: this allows normal
%   free transition to repeat, but group-repeating transitions
%   won't. Once we are done exploring all the branches it spawned, the
%   state is marked as $\texttt{step}+1$: any thread hitting it at that
%   point will be terminated.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_use_state:
  {
    \__kernel_intarray_gset:Nnn \g_@@_state_active_intarray
      { \l_@@_curr_state_int } { \l_@@_step_int }
    \@@_toks_use:w \l_@@_curr_state_int
    \__kernel_intarray_gset:Nnn \g_@@_state_active_intarray
      { \l_@@_curr_state_int }
      { \int_eval:n { \l_@@_step_int + 1 } }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_use_state_and_submatches:w}
%   This function is called as one item in the array of active threads
%   after that array has been unpacked for a new step. Update the
%   \texttt{curr_state} and \texttt{curr_submatches} and use the
%   state if it has not yet been encountered at this step.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_use_state_and_submatches:w #1 , #2 ;
  {
    \int_set:Nn \l_@@_curr_state_int {#1}
    \if_int_compare:w
        \__kernel_intarray_item:Nn \g_@@_state_active_intarray
          { \l_@@_curr_state_int }
                      < \l_@@_step_int
      \tl_set:Nn \l_@@_curr_submatches_tl { #2 , }
      \exp_after:wN \@@_use_state:
    \fi:
    \scan_stop:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Actions when matching}
%
% \begin{macro}{\@@_action_start_wildcard:N}
%   For an unanchored match, state $0$ has a free transition to the next
%   and a costly one to itself, to repeat at the next position. To catch
%   repeated identical empty matches, we need to know if a successful
%   thread corresponds to an empty match. The instruction resetting
%   \cs{l_@@_fresh_thread_bool} may be skipped by a successful
%   thread, hence we had to add it to \cs{@@_match_one_token:nnN} too.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_action_start_wildcard:N #1
  {
    \bool_set_true:N \l_@@_fresh_thread_bool
    \@@_action_free:n {1}
    \bool_set_false:N \l_@@_fresh_thread_bool
    \bool_if:NT #1 { \@@_action_cost:n {0} }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_action_free:n, \@@_action_free_group:n}
% \begin{macro}{\@@_action_free_aux:nn}
%   These functions copy a thread after checking that the \textsc{nfa}
%   state has not already been used at this position. If not, store
%   submatches in the new state, and insert the instructions for that
%   state in the input stream.  Then restore the old value of
%   \cs{l_@@_curr_state_int} and of the current submatches.  The
%   two types of free transitions differ by how they test that the state
%   has not been encountered yet: the \texttt{group} version is
%   stricter, and will not use a state if it was used earlier in the
%   current thread, hence forcefully breaking the loop, while the
%   \enquote{normal} version will revisit a state even within the thread
%   itself.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_action_free:n
  { \@@_action_free_aux:nn { > \l_@@_step_int \else: } }
\cs_new_protected:Npn \@@_action_free_group:n
  { \@@_action_free_aux:nn { < \l_@@_step_int } }
\cs_new_protected:Npn \@@_action_free_aux:nn #1#2
  {
    \use:x
      {
        \int_add:Nn \l_@@_curr_state_int {#2}
        \exp_not:n
          {
            \if_int_compare:w
                \__kernel_intarray_item:Nn \g_@@_state_active_intarray
                  { \l_@@_curr_state_int }
                #1
              \exp_after:wN \@@_use_state:
            \fi:
          }
        \int_set:Nn \l_@@_curr_state_int
          { \int_use:N \l_@@_curr_state_int }
        \tl_set:Nn \exp_not:N \l_@@_curr_submatches_tl
          { \exp_not:o \l_@@_curr_submatches_tl }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_action_cost:n}
%   A transition which consumes the current character and shifts the
%   state by |#1|.  The resulting state is stored in the appropriate array
%   for use at the next position, and we also store the current
%   submatches.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_action_cost:n #1
  {
    \exp_args:Nx \@@_store_state:n
      { \int_eval:n { \l_@@_curr_state_int + #1 } }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_store_state:n}
% \begin{macro}{\@@_store_submatches:}
%   Put the given state and current submatch information in
%   \cs{g_@@_thread_info_intarray}, and increment the length of the
%   array.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_store_state:n #1
  {
    \exp_args:No \@@_store_submatches:nn
      \l_@@_curr_submatches_tl {#1}
    \int_incr:N \l_@@_max_thread_int
  }
\cs_new_protected:Npn \@@_store_submatches:nn #1#2
  {
    \__kernel_intarray_gset_range_from_clist:Nnn
      \g_@@_thread_info_intarray
      {
        \@@_int_eval:w
        1 + \l_@@_max_thread_int *
        (\l_@@_capturing_group_int * 2 + 1)
      }
      { #2 , #1 }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_disable_submatches:}
%   Some user functions don't require tracking submatches.
%   We get a performance improvement by simply defining the
%   relevant functions to remove their argument and do nothing
%   with it.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_disable_submatches:
  {
    \cs_set_protected:Npn \@@_store_submatches:n ##1 { }
    \cs_set_protected:Npn \@@_action_submatch:nN ##1##2 { }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_action_submatch:nN, \@@_action_submatch_aux:w, \@@_action_submatch_auxii:w, \@@_action_submatch_auxiii:w, \@@_action_submatch_auxiv:w}
%   Update the current submatches with the information from the current
%   position. Maybe a bottleneck.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_action_submatch:nN #1#2
  {
    \exp_after:wN \@@_action_submatch_aux:w
    \l_@@_curr_submatches_tl ; {#1} #2
  }
\cs_new_protected:Npn \@@_action_submatch_aux:w #1 ; #2#3
  {
    \tl_set:Nx \l_@@_curr_submatches_tl
      {
        \prg_replicate:nn
          { #2 \if_meaning:w > #3 + \l_@@_capturing_group_int \fi: }
          { \@@_action_submatch_auxii:w }
        \@@_action_submatch_auxiii:w
        #1
      }
  }
\cs_new:Npn \@@_action_submatch_auxii:w
    #1 \@@_action_submatch_auxiii:w #2 ,
  { #2 , #1 \@@_action_submatch_auxiii:w }
\cs_new:Npn \@@_action_submatch_auxiii:w #1 ,
  { \int_use:N \l_@@_curr_pos_int , }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_action_success:}
%   There is a successful match when an execution path reaches the last
%   state in the \textsc{nfa}, unless this marks a second identical
%   empty match. Then mark that there was a successful match; it is
%   empty if it is \enquote{fresh}; and we store the current position
%   and submatches. The current step is then interrupted with
%   \cs{prg_break:}, and only paths with higher precedence are
%   pursued further. The values stored here may be overwritten by a
%   later success of a path with higher precedence.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_action_success:
  {
    \@@_if_two_empty_matches:F
      {
        \bool_set_true:N \l_@@_match_success_bool
        \bool_set_eq:NN \l_@@_empty_success_bool
          \l_@@_fresh_thread_bool
        \int_set_eq:NN \l_@@_success_pos_int \l_@@_curr_pos_int
        \int_set_eq:NN \l_@@_last_char_success_int \l_@@_last_char_int
        \tl_build_clear:N \l_@@_matched_analysis_tl
        \tl_set_eq:NN \l_@@_success_submatches_tl
          \l_@@_curr_submatches_tl
        \prg_break:
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Replacement}
%
% \subsubsection{Variables and helpers used in replacement}
%
% \begin{variable}{\l_@@_replacement_csnames_int}
%   The behaviour of closing braces inside a replacement text depends on
%   whether a sequences |\c{| or |\u{| has been encountered. The number
%       of \enquote{open} such sequences that should be closed by |}| is
%     stored in \cs{l_@@_replacement_csnames_int}, and decreased by
%     $1$ by each |}|.
%    \begin{macrocode}
\int_new:N \l_@@_replacement_csnames_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_replacement_category_tl, \l_@@_replacement_category_seq}
%   This sequence of letters is used to correctly restore categories in
%   nested constructions such as |\cL(abc\cD(_)d)|.
%    \begin{macrocode}
\tl_new:N \l_@@_replacement_category_tl
\seq_new:N \l_@@_replacement_category_seq
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_balance_tl}
%   This token list holds the replacement text for
%   \cs{@@_replacement_balance_one_match:n} while it is being built
%   incrementally.
%    \begin{macrocode}
\tl_new:N \l_@@_balance_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[rEXP]{\@@_replacement_balance_one_match:n}
%   This expects as an argument the first index of a set of entries in
%   \cs{g_@@_submatch_begin_intarray} (and related arrays) which hold the
%   submatch information for a given match. It
%   can be used within an integer expression to obtain the brace balance
%   incurred by performing the replacement on that match. This combines
%   the braces lost by removing the match, braces added by all the
%   submatches appearing in the replacement, and braces appearing
%   explicitly in the replacement. Even though it is always redefined
%   before use, we initialize it as for an empty replacement. An
%   important property is that concatenating several calls to that
%   function must result in a valid integer expression (hence a leading
%   |+| in the actual definition).
%    \begin{macrocode}
\cs_new:Npn \@@_replacement_balance_one_match:n #1
  { - \@@_submatch_balance:n {#1} }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_replacement_do_one_match:n}
%   The input is the same as \cs{@@_replacement_balance_one_match:n}.
%   This function is redefined to expand to the part of the token list
%   from the end of the previous match to a given match, followed by the
%   replacement text. Hence concatenating the result of this function
%   with all possible arguments (one call for each match), as well as
%   the range from the end of the last match to the end of the string,
%   produces the fully replaced token list. The initialization does
%   not matter, but (as an example) we set it as for an empty replacement.
%    \begin{macrocode}
\cs_new:Npn \@@_replacement_do_one_match:n #1
  {
    \@@_query_range:nn
      { \__kernel_intarray_item:Nn \g_@@_submatch_prev_intarray {#1} }
      { \__kernel_intarray_item:Nn \g_@@_submatch_begin_intarray {#1} }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_exp_not:N}
%   This function lets us navigate around the fact that the primitive
%   \cs{exp_not:n} requires a braced argument. As far as I can tell, it
%   is only needed if the user tries to include in the replacement text
%   a control sequence set equal to a macro parameter character, such as
%   \cs{c_parameter_token}. Indeed, within an \texttt{x}-expanding
%   assignment, \cs{exp_not:N}~|#| behaves as a single |#|, whereas
%   \cs{exp_not:n}~|{#}| behaves as a doubled |##|.
%    \begin{macrocode}
\cs_new:Npn \@@_replacement_exp_not:N #1 { \exp_not:n {#1} }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_exp_not:V}
%   This is used for the implementation of~|\u|, and it gets redefined
%   for \cs{peek_regex_replace_once:nnTF}.
%    \begin{macrocode}
\cs_new_eq:NN \@@_replacement_exp_not:V \exp_not:V
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Query and brace balance}
%
% \begin{macro}[rEXP]{\@@_query_range:nn}
% \begin{macro}[rEXP]{\@@_query_range_loop:ww}
%   When it is time to extract submatches from the token list, the
%   various tokens are stored in \tn{toks} registers numbered from
%   \cs{l_@@_min_pos_int} inclusive to \cs{l_@@_max_pos_int}
%   exclusive. The function \cs{@@_query_range:nn} \Arg{min}
%   \Arg{max} unpacks registers from the position \meta{min} to the
%   position $\meta{max}-1$ included. Once this is expanded, a second
%   \texttt{x}-expansion results in the actual tokens from the
%   query. That second expansion is only done by user functions at the
%   very end of their operation, after checking (and correcting) the
%   brace balance first.
%    \begin{macrocode}
\cs_new:Npn \@@_query_range:nn #1#2
  {
    \exp_after:wN \@@_query_range_loop:ww
    \int_value:w \@@_int_eval:w #1 \exp_after:wN ;
    \int_value:w \@@_int_eval:w #2 ;
    \prg_break_point:
  }
\cs_new:Npn \@@_query_range_loop:ww #1 ; #2 ;
  {
    \if_int_compare:w #1 < #2 \exp_stop_f:
    \else:
      \exp_after:wN \prg_break:
    \fi:
    \@@_toks_use:w #1 \exp_stop_f:
    \exp_after:wN \@@_query_range_loop:ww
      \int_value:w \@@_int_eval:w #1 + 1 ; #2 ;
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_query_submatch:n}
%   Find the start and end positions for a given submatch (of a given match).
%    \begin{macrocode}
\cs_new:Npn \@@_query_submatch:n #1
  {
    \@@_query_range:nn
      { \__kernel_intarray_item:Nn \g_@@_submatch_begin_intarray {#1} }
      { \__kernel_intarray_item:Nn \g_@@_submatch_end_intarray {#1} }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_submatch_balance:n}
%   Every user function must result in a balanced token list (unbalanced
%   token lists cannot be stored by TeX). When we unpacked the query, we
%   kept track of the brace balance, hence the contribution from a given
%   range is the difference between the brace balances at the
%   \meta{max~pos} and \meta{min~pos}.  These two positions are found in
%   the corresponding \enquote{submatch} arrays.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_submatch_balance:n #1
  {
    \int_eval:n
      {
        \@@_intarray_item:NnF \g_@@_balance_intarray
          {
            \__kernel_intarray_item:Nn
              \g_@@_submatch_end_intarray {#1}
          }
          { 0 }
        -
        \@@_intarray_item:NnF \g_@@_balance_intarray
          {
            \__kernel_intarray_item:Nn
              \g_@@_submatch_begin_intarray {#1}
          }
          { 0 }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Framework}
%
% \begin{macro}{\@@_replacement:n}
% \begin{macro}{\@@_replacement_aux:n}
%   The replacement text is built incrementally. We keep track in
%   \cs{l_@@_balance_int} of the balance of explicit begin- and
%   end-group tokens and we store in \cs{l_@@_balance_tl} some
%   code to compute the brace balance from submatches (see its
%   description). Detect unescaped right braces, and escaped characters,
%   with trailing \cs{prg_do_nothing:} because some of the later
%   function look-ahead. Once the whole replacement text has been
%   parsed, make sure that there is no open csname. Finally, define the
%   \texttt{balance_one_match} and \texttt{do_one_match} functions.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement:n #1
  {
    \group_begin:
      \tl_build_begin:N \l_@@_build_tl
      \int_zero:N \l_@@_balance_int
      \tl_clear:N \l_@@_balance_tl
      \@@_escape_use:nnnn
        {
          \if_charcode:w \c_right_brace_str ##1
            \@@_replacement_rbrace:N
          \else:
            \if_charcode:w \c_left_brace_str ##1
              \@@_replacement_lbrace:N
            \else:
              \@@_replacement_normal:n
            \fi:
          \fi:
          ##1
        }
        { \@@_replacement_escaped:N ##1 }
        { \@@_replacement_normal:n ##1 }
        {#1}
      \prg_do_nothing: \prg_do_nothing:
      \if_int_compare:w \l_@@_replacement_csnames_int > \c_zero_int
        \msg_error:nnx { regex } { replacement-missing-rbrace }
          { \int_use:N \l_@@_replacement_csnames_int }
        \tl_build_put_right:Nx \l_@@_build_tl
          { \prg_replicate:nn \l_@@_replacement_csnames_int \cs_end: }
      \fi:
      \seq_if_empty:NF \l_@@_replacement_category_seq
        {
          \msg_error:nnx { regex } { replacement-missing-rparen }
            { \seq_count:N \l_@@_replacement_category_seq }
          \seq_clear:N \l_@@_replacement_category_seq
        }
      \cs_gset:Npx \@@_replacement_balance_one_match:n ##1
        {
          + \int_use:N \l_@@_balance_int
          \l_@@_balance_tl
          - \@@_submatch_balance:n {##1}
        }
      \tl_build_end:N \l_@@_build_tl
      \exp_args:NNo
    \group_end:
    \@@_replacement_aux:n \l_@@_build_tl
  }
\cs_new_protected:Npn \@@_replacement_aux:n #1
  {
    \cs_set:Npn \@@_replacement_do_one_match:n ##1
      {
        \@@_query_range:nn
          {
            \__kernel_intarray_item:Nn
              \g_@@_submatch_prev_intarray {##1}
          }
          {
            \__kernel_intarray_item:Nn
              \g_@@_submatch_begin_intarray {##1}
          }
        #1
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_replacement_put:n}
%   This gets redefined for \cs{peek_regex_replace_once:nnTF}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_put:n
  { \tl_build_put_right:Nn \l_@@_build_tl }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_normal:n, \@@_replacement_normal_aux:N}
%   Most characters are simply sent to the output by
%   \cs{tl_build_put_right:Nn}, unless a particular category code has been
%   requested: then \cs{@@_replacement_c_A:w} or a similar auxiliary is
%   called.  One exception is right parentheses, which restore the
%   category code in place before the group started.  Note that the
%   sequence is non-empty there: it contains an empty entry
%   corresponding to the initial value of
%   \cs{l_@@_replacement_category_tl}.
%   The argument |#1| is a single character (including the case of a catcode-other space).
%   In case no specific catcode is requested, we taked into account the
%   current catcode regime (at the time the replacement is performed)
%   as much as reasonable, with all impossible catcodes (escape,
%   newline, etc.) being mapped to \enquote{other}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_normal:n #1
  {
    \int_compare:nNnTF { \l_@@_replacement_csnames_int } > 0
      { \exp_args:No \@@_replacement_put:n { \token_to_str:N #1 } }
      {
        \tl_if_empty:NTF \l_@@_replacement_category_tl
          { \@@_replacement_normal_aux:N #1 }
          { % (
            \token_if_eq_charcode:NNTF #1 )
              {
                \seq_pop:NN \l_@@_replacement_category_seq
                  \l_@@_replacement_category_tl
              }
              {
                \use:c { @@_replacement_c_ \l_@@_replacement_category_tl :w }
                ? #1
              }
          }
      }
  }
\cs_new_protected:Npn \@@_replacement_normal_aux:N #1
  {
    \token_if_eq_charcode:NNTF #1 \c_space_token
      { \@@_replacement_c_S:w }
      {
        \exp_after:wN \exp_after:wN
        \if_case:w \tex_catcode:D `#1 \exp_stop_f:
             \@@_replacement_c_O:w
        \or: \@@_replacement_c_B:w
        \or: \@@_replacement_c_E:w
        \or: \@@_replacement_c_M:w
        \or: \@@_replacement_c_T:w
        \or: \@@_replacement_c_O:w
        \or: \@@_replacement_c_P:w
        \or: \@@_replacement_c_U:w
        \or: \@@_replacement_c_D:w
        \or: \@@_replacement_c_O:w
        \or: \@@_replacement_c_S:w
        \or: \@@_replacement_c_L:w
        \or: \@@_replacement_c_O:w
        \or: \@@_replacement_c_A:w
        \else: \@@_replacement_c_O:w
        \fi:
      }
    ? #1
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_escaped:N}
%   As in parsing a regular expression, we use an auxiliary built from
%   |#1| if defined. Otherwise, check for escaped digits (standing from
%   submatches from $0$ to $9$): anything else is a raw character.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_escaped:N #1
  {
    \cs_if_exist_use:cF { @@_replacement_#1:w }
      {
        \if_int_compare:w 1 < 1#1 \exp_stop_f:
          \@@_replacement_put_submatch:n {#1}
        \else:
          \@@_replacement_normal:n {#1}
        \fi:
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Submatches}
%
% \begin{macro}{\@@_replacement_put_submatch:n, \@@_replacement_put_submatch_aux:n}
%   Insert a submatch in the replacement text. This is dropped if the
%   submatch number is larger than the number of capturing groups.
%   Unless the submatch appears inside a |\c{...}| or |\u{...}|
%   construction, it must be taken into account in the brace balance.
%   Later on, |##1| will be replaced by a pointer to the $0$-th submatch for a
%   given match.  There is an \cs{exp_not:N} here as at the point-of-use
%   of \cs{l_@@_balance_tl} there is an \texttt{x}-type expansion which is needed
%   to get |##1| in correctly.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_put_submatch:n #1
  {
    \if_int_compare:w #1 < \l_@@_capturing_group_int
      \@@_replacement_put_submatch_aux:n {#1}
    \else:
      \msg_expandable_error:nnff { regex } { submatch-too-big }
        {#1} { \int_eval:n { \l_@@_capturing_group_int - 1 } }
    \fi:
  }
\cs_new_protected:Npn \@@_replacement_put_submatch_aux:n #1
  {
    \tl_build_put_right:Nn \l_@@_build_tl
      { \@@_query_submatch:n { \int_eval:n { #1 + ##1 } } }
    \if_int_compare:w \l_@@_replacement_csnames_int = \c_zero_int
      \tl_put_right:Nn \l_@@_balance_tl
        {
          + \@@_submatch_balance:n
            { \exp_not:N \int_eval:n { #1 + ##1 } }
        }
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_g:w}
% \begin{macro}[rEXP]{\@@_replacement_g_digits:NN}
%   Grab digits for the |\g| escape sequence in a primitive assignment
%   to the integer \cs{l_@@_internal_a_int}.  At the end of the run of
%   digits, check that it ends with a right brace.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_g:w #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_replacement_lbrace:N
      { \l_@@_internal_a_int = \@@_replacement_g_digits:NN }
      { \@@_replacement_error:NNN g #1 #2 }
  }
\cs_new:Npn \@@_replacement_g_digits:NN #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_replacement_normal:n
      {
        \if_int_compare:w 1 < 1#2 \exp_stop_f:
          #2
          \exp_after:wN \use_i:nnn
          \exp_after:wN \@@_replacement_g_digits:NN
        \else:
          \exp_stop_f:
          \exp_after:wN \@@_replacement_error:NNN
          \exp_after:wN g
        \fi:
      }
      {
        \exp_stop_f:
        \if_meaning:w \@@_replacement_rbrace:N #1
          \exp_args:No \@@_replacement_put_submatch:n
            { \int_use:N \l_@@_internal_a_int }
          \exp_after:wN \use_none:nn
        \else:
          \exp_after:wN \@@_replacement_error:NNN
          \exp_after:wN g
        \fi:
      }
    #1 #2
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Csnames in replacement}
%
% \begin{macro}{\@@_replacement_c:w}
%   |\c| may only be followed by an unescaped character.  If followed by
%   a left brace, start a control sequence by calling an auxiliary
%   common with |\u|.  Otherwise test whether the category is known; if
%   it is not, complain.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_c:w #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_replacement_normal:n
      {
        \cs_if_exist:cTF { @@_replacement_c_#2:w }
          { \@@_replacement_cat:NNN #2 }
          { \@@_replacement_error:NNN c #1#2 }
      }
      {
        \token_if_eq_meaning:NNTF #1 \@@_replacement_lbrace:N
          { \@@_replacement_cu_aux:Nw \@@_replacement_exp_not:N }
          { \@@_replacement_error:NNN c #1#2 }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_cu_aux:Nw}
%   Start a control sequence with \cs{cs:w}, protected
%   from expansion by |#1| (either \cs{@@_replacement_exp_not:N} or
%   \cs{exp_not:V}), or turned to a string by \cs{tl_to_str:V} if inside
%   another csname construction |\c| or |\u|.  We use \cs{tl_to_str:V}
%   rather than \cs{tl_to_str:N} to deal with integers and other
%   registers.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_cu_aux:Nw #1
  {
    \if_case:w \l_@@_replacement_csnames_int
      \tl_build_put_right:Nn \l_@@_build_tl
        { \exp_not:n { \exp_after:wN #1 \cs:w } }
    \else:
      \tl_build_put_right:Nn \l_@@_build_tl
        { \exp_not:n { \exp_after:wN \tl_to_str:V \cs:w } }
    \fi:
    \int_incr:N \l_@@_replacement_csnames_int
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_u:w}
%   Check that |\u| is followed by a left brace. If so, start a control
%   sequence with \cs{cs:w}, which is then unpacked either with
%   \cs{exp_not:V} or \cs{tl_to_str:V} depending on the current context.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_u:w #1#2
  {
    \token_if_eq_meaning:NNTF #1 \@@_replacement_lbrace:N
      { \@@_replacement_cu_aux:Nw \@@_replacement_exp_not:V }
      { \@@_replacement_error:NNN u #1#2 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_rbrace:N}
%   Within a |\c{...}| or |\u{...}| construction, end the control
%   sequence, and decrease the brace count. Otherwise, this is a raw
%   right brace.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_rbrace:N #1
  {
    \if_int_compare:w \l_@@_replacement_csnames_int > \c_zero_int
      \tl_build_put_right:Nn \l_@@_build_tl { \cs_end: }
      \int_decr:N \l_@@_replacement_csnames_int
    \else:
      \@@_replacement_normal:n {#1}
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_lbrace:N}
%   Within a |\c{...}| or |\u{...}| construction, this is
%   forbidden. Otherwise, this is a raw left brace.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_lbrace:N #1
  {
    \if_int_compare:w \l_@@_replacement_csnames_int > \c_zero_int
      \msg_error:nnn { regex } { cu-lbrace } { u }
    \else:
      \@@_replacement_normal:n {#1}
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Characters in replacement}
%
% \begin{macro}{\@@_replacement_cat:NNN}
%   Here, |#1| is a letter among |BEMTPUDSLOA| and |#2#3| denote the
%   next character.  Complain if we reach the end of the replacement or
%   if the construction appears inside |\c{|\ldots{}|}| or
%   |\u{|\ldots{}|}|, and detect the case of a parenthesis.  In that
%   case, store the current category in a sequence and switch to a new
%   one.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_cat:NNN #1#2#3
  {
    \token_if_eq_meaning:NNTF \prg_do_nothing: #3
      { \msg_error:nn { regex } { replacement-catcode-end } }
      {
        \int_compare:nNnTF { \l_@@_replacement_csnames_int } > 0
          {
            \msg_error:nnnn
              { regex } { replacement-catcode-in-cs } {#1} {#3}
            #2 #3
          }
          {
            \@@_two_if_eq:NNNNTF #2 #3 \@@_replacement_normal:n (
              {
                \seq_push:NV \l_@@_replacement_category_seq
                  \l_@@_replacement_category_tl
                \tl_set:Nn \l_@@_replacement_category_tl {#1}
              }
              {
                \token_if_eq_meaning:NNT #2 \@@_replacement_escaped:N
                  {
                    \@@_char_if_alphanumeric:NTF #3
                      {
                        \msg_error:nnnn
                          { regex } { replacement-catcode-escaped }
                          {#1} {#3}
                      }
                      { }
                  }
                \use:c { @@_replacement_c_#1:w } #2 #3
              }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% We now need to change the category code of the null character many
% times, hence work in a group. The catcode-specific macros below are
% defined in alphabetical order; if you are trying to understand the
% code, start from the end of the alphabet as those categories are
% simpler than active or begin-group.
%    \begin{macrocode}
\group_begin:
%    \end{macrocode}
%
% \begin{macro}{\@@_replacement_char:nNN}
%   The only way to produce an arbitrary character--catcode pair is to
%   use the \tn{lowercase} or \tn{uppercase} primitives. This is a
%   wrapper for our purposes. The first argument is the null character
%   with various catcodes. The second and third arguments are grabbed
%   from the input stream: |#3| is the character whose character code to
%   reproduce.  We could use \cs{char_generate:nn} but only for some
%   catcodes (active characters and spaces are not supported).
%    \begin{macrocode}
  \cs_new_protected:Npn \@@_replacement_char:nNN #1#2#3
    {
      \tex_lccode:D 0 = `#3 \scan_stop:
      \tex_lowercase:D { \@@_replacement_put:n {#1} }
    }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_A:w}
%   For an active character, expansion must be avoided, twice because we
%   later do two \texttt{x}-expansions, to unpack \tn{toks} for the
%   query, and to expand their contents to tokens of the query.
%    \begin{macrocode}
  \char_set_catcode_active:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_A:w
    { \@@_replacement_char:nNN { \exp_not:n { \exp_not:N ^^@ } } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_B:w}
%   An explicit begin-group token increases the balance, unless within a
%   |\c{...}| or |\u{...}| construction. Add the desired begin-group
%   character, using the standard \cs{if_false:} trick. We eventually
%   \texttt{x}-expand twice. The first time must yield a balanced token
%   list, and the second one gives the bare begin-group token. The
%   \cs{exp_after:wN} is not strictly needed, but is more consistent
%   with \pkg{l3tl-analysis}.
%    \begin{macrocode}
  \char_set_catcode_group_begin:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_B:w
    {
      \if_int_compare:w \l_@@_replacement_csnames_int = \c_zero_int
        \int_incr:N \l_@@_balance_int
      \fi:
      \@@_replacement_char:nNN
        { \exp_not:n { \exp_after:wN ^^@ \if_false: } \fi: } }
    }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_C:w}
%   This is not quite catcode-related: when the user requests a
%   character with category \enquote{control sequence}, the
%   one-character control symbol is returned. As for the active
%   character, we prepare for two \texttt{x}-expansions.
%    \begin{macrocode}
  \cs_new_protected:Npn \@@_replacement_c_C:w #1#2
    {
      \tl_build_put_right:Nn \l_@@_build_tl
        { \exp_not:N \@@_replacement_exp_not:N \exp_not:c {#2} }
    }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_D:w}
%   Subscripts fit the mould: \tn{lowercase} the null byte with the
%   correct category.
%    \begin{macrocode}
  \char_set_catcode_math_subscript:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_D:w
    { \@@_replacement_char:nNN { ^^@ } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_E:w}
%   Similar to the begin-group case, the second \texttt{x}-expansion
%   produces the bare end-group token.
%    \begin{macrocode}
  \char_set_catcode_group_end:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_E:w
    {
      \if_int_compare:w \l_@@_replacement_csnames_int = \c_zero_int
        \int_decr:N \l_@@_balance_int
      \fi:
      \@@_replacement_char:nNN
        { \exp_not:n { \if_false: { \fi:  ^^@ } }
    }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_L:w}
%   Simply \tn{lowercase} a letter null byte to produce an arbitrary letter.
%    \begin{macrocode}
  \char_set_catcode_letter:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_L:w
    { \@@_replacement_char:nNN { ^^@ } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_M:w}
%   No surprise here, we lowercase the null math toggle.
%    \begin{macrocode}
  \char_set_catcode_math_toggle:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_M:w
    { \@@_replacement_char:nNN { ^^@ } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_O:w}
%   Lowercase an other null byte.
%    \begin{macrocode}
  \char_set_catcode_other:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_O:w
    { \@@_replacement_char:nNN { ^^@ } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_P:w}
%   For macro parameters, expansion is a tricky issue. We need to
%   prepare for two \texttt{x}-expansions and passing through various
%   macro definitions. Note that we cannot replace one \cs{exp_not:n} by
%   doubling the macro parameter characters because this would misbehave
%   if a mischievous user asks for |\c{\cP\#}|, since that macro
%   parameter character would be doubled.
%    \begin{macrocode}
  \char_set_catcode_parameter:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_P:w
    {
      \@@_replacement_char:nNN
        { \exp_not:n { \exp_not:n { ^^@^^@^^@^^@ } } }
    }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_S:w}
%   Spaces are normalized on input by \TeX{} to have character code
%   $32$. It is in fact impossible to get a token with character code
%   $0$ and category code $10$. Hence we use $32$ instead of $0$ as our
%   base character.
%    \begin{macrocode}
  \cs_new_protected:Npn \@@_replacement_c_S:w #1#2
    {
      \if_int_compare:w `#2 = \c_zero_int
        \msg_error:nn { regex } { replacement-null-space }
      \fi:
      \tex_lccode:D `\ = `#2 \scan_stop:
      \tex_lowercase:D { \@@_replacement_put:n {~} }
    }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_T:w}
%   No surprise for alignment tabs here. Those are surrounded by the
%   appropriate braces whenever necessary, hence they don't cause
%   trouble in alignment settings.
%    \begin{macrocode}
  \char_set_catcode_alignment:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_T:w
    { \@@_replacement_char:nNN { ^^@ } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replacement_c_U:w}
%   Simple call to \cs{@@_replacement_char:nNN} which lowercases the
%   math superscript |^^@|.
%    \begin{macrocode}
  \char_set_catcode_math_superscript:N \^^@
  \cs_new_protected:Npn \@@_replacement_c_U:w
    { \@@_replacement_char:nNN { ^^@ } }
%    \end{macrocode}
% \end{macro}
%
% Restore the catcode of the null byte.
%    \begin{macrocode}
\group_end:
%    \end{macrocode}
%
% \subsubsection{An error}
%
% \begin{macro}{\@@_replacement_error:NNN}
%   Simple error reporting by calling one of the messages
%   \texttt{replacement-c}, \texttt{replacement-g}, or
%   \texttt{replacement-u}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replacement_error:NNN #1#2#3
  {
    \msg_error:nnx { regex } { replacement-#1 } {#3}
    #2 #3
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{User functions}
%
% \begin{macro}{\regex_new:N}
%   Before being assigned a sensible value, a regex variable matches
%   nothing.
%    \begin{macrocode}
\cs_new_protected:Npn \regex_new:N #1
  { \cs_new_eq:NN #1 \c_@@_no_match_regex }
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\l_tmpa_regex, \l_tmpb_regex, \g_tmpa_regex, \g_tmpb_regex}
%   The usual scratch space.
%    \begin{macrocode}
\regex_new:N \l_tmpa_regex
\regex_new:N \l_tmpb_regex
\regex_new:N \g_tmpa_regex
\regex_new:N \g_tmpb_regex
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\regex_set:Nn, \regex_gset:Nn, \regex_const:Nn}
%   Compile, then store the result in the user variable with the
%   appropriate assignment function.
%    \begin{macrocode}
\cs_new_protected:Npn \regex_set:Nn #1#2
  {
    \@@_compile:n {#2}
    \tl_set_eq:NN #1 \l_@@_internal_regex
  }
\cs_new_protected:Npn \regex_gset:Nn #1#2
  {
    \@@_compile:n {#2}
    \tl_gset_eq:NN #1 \l_@@_internal_regex
  }
\cs_new_protected:Npn \regex_const:Nn #1#2
  {
    \@@_compile:n {#2}
    \tl_const:Nx #1 { \exp_not:o \l_@@_internal_regex }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \regex_show:n, \regex_log:n, \@@_show:Nn,
%     \regex_show:N, \regex_log:N, \@@_show:NN
%   }
%   User functions: the \texttt{n} variant requires compilation first.
%   Then show the variable with some appropriate text. The auxiliary
%   \cs{@@_show:N} is defined in a different section.
%    \begin{macrocode}
\cs_new_protected:Npn \regex_show:n { \@@_show:Nn \msg_show:nnxxxx }
\cs_new_protected:Npn \regex_log:n { \@@_show:Nn \msg_log:nnxxxx }
\cs_new_protected:Npn \@@_show:Nn #1#2
  {
    \@@_compile:n {#2}
    \@@_show:N \l_@@_internal_regex
    #1 { regex } { show }
      { \tl_to_str:n {#2} } { }
      { \l_@@_internal_a_tl } { }
  }
\cs_new_protected:Npn \regex_show:N { \@@_show:NN \msg_show:nnxxxx }
\cs_new_protected:Npn \regex_log:N { \@@_show:NN \msg_log:nnxxxx }
\cs_new_protected:Npn \@@_show:NN #1#2
  {
    \__kernel_chk_tl_type:NnnT #2 { regex }
      { \exp_args:No \@@_clean_regex:n {#2} }
      {
        \@@_show:N #2
        #1 { regex } { show }
          { } { \token_to_str:N #2 }
          { \l_@@_internal_a_tl } { }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[TF]{\regex_match:nn, \regex_match:Nn}
%   Those conditionals are based on a common auxiliary defined
%   later. Its first argument builds the \textsc{nfa} corresponding to
%   the regex, and the second argument is the query token list. Once we
%   have performed the match, convert the resulting boolean to
%   \cs{prg_return_true:} or \texttt{false}.
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \regex_match:nn #1#2 { T , F , TF }
  {
    \@@_if_match:nn { \@@_build:n {#1} } {#2}
    \@@_return:
  }
\prg_new_protected_conditional:Npnn \regex_match:Nn #1#2 { T , F , TF }
  {
    \@@_if_match:nn { \@@_build:N #1 } {#2}
    \@@_return:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\regex_count:nnN, \regex_count:NnN}
%   Again, use an auxiliary whose first argument builds the \textsc{nfa}.
%    \begin{macrocode}
\cs_new_protected:Npn \regex_count:nnN #1
  { \@@_count:nnN { \@@_build:n {#1} } }
\cs_new_protected:Npn \regex_count:NnN #1
  { \@@_count:nnN { \@@_build:N #1 } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[noTF]
%   {
%     \regex_extract_once:nnN, \regex_extract_once:NnN,
%     \regex_extract_all:nnN,  \regex_extract_all:NnN,
%     \regex_replace_once:nnN, \regex_replace_once:NnN,
%     \regex_replace_all:nnN,  \regex_replace_all:NnN,
%     \regex_split:nnN,        \regex_split:NnN
%   }
%   We define here $40$ user functions, following a common pattern in
%   terms of \texttt{:nnN} auxiliaries, defined in the coming
%   subsections.  The auxiliary is handed \cs{@@_build:n} or
%   \cs{@@_build:N} with the appropriate regex argument, then all
%   other necessary arguments (replacement text, token list, \emph{etc.}
%   The conditionals call \cs{@@_return:} to return either
%   \texttt{true} or \texttt{false} once matching has been performed.
%    \begin{macrocode}
\cs_set_protected:Npn \@@_tmp:w #1#2#3
  {
    \cs_new_protected:Npn #2 ##1 { #1 { \@@_build:n {##1} } }
    \cs_new_protected:Npn #3 ##1 { #1 { \@@_build:N  ##1  } }
    \prg_new_protected_conditional:Npnn #2 ##1##2##3 { T , F , TF }
      { #1 { \@@_build:n {##1} } {##2} ##3 \@@_return: }
    \prg_new_protected_conditional:Npnn #3 ##1##2##3 { T , F , TF }
      { #1 { \@@_build:N  ##1  } {##2} ##3 \@@_return: }
  }
\@@_tmp:w \@@_extract_once:nnN
  \regex_extract_once:nnN \regex_extract_once:NnN
\@@_tmp:w \@@_extract_all:nnN
  \regex_extract_all:nnN \regex_extract_all:NnN
\@@_tmp:w \@@_replace_once:nnN
  \regex_replace_once:nnN \regex_replace_once:NnN
\@@_tmp:w \@@_replace_all:nnN
  \regex_replace_all:nnN \regex_replace_all:NnN
\@@_tmp:w \@@_split:nnN \regex_split:nnN \regex_split:NnN
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Variables and helpers for user functions}
%
% \begin{variable}{\l_@@_match_count_int}
%   The number of matches found so far is stored
%   in \cs{l_@@_match_count_int}. This is only used
%   in the \cs{regex_count:nnN} functions.
%    \begin{macrocode}
\int_new:N \l_@@_match_count_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{@@_begin, @@_end}
%   Those flags are raised to indicate begin-group or end-group tokens
%   that had to be added when extracting submatches.
%    \begin{macrocode}
\flag_new:n { @@_begin }
\flag_new:n { @@_end }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_min_submatch_int, \l_@@_submatch_int, \l_@@_zeroth_submatch_int}
%   The end-points of each submatch are stored in two arrays whose index \meta{submatch} ranges
%   from \cs{l_@@_min_submatch_int} (inclusive) to
%   \cs{l_@@_submatch_int} (exclusive). Each successful match comes
%   with a $0$-th submatch (the full match), and one match for each
%   capturing group: submatches corresponding to the last successful
%   match are labelled starting at \texttt{zeroth_submatch}. The entry
%   \cs{l_@@_zeroth_submatch_int} in \cs{g_@@_submatch_prev_intarray} holds
%   the position at which that match attempt started: this is used for
%   splitting and replacements.
%    \begin{macrocode}
\int_new:N \l_@@_min_submatch_int
\int_new:N \l_@@_submatch_int
\int_new:N \l_@@_zeroth_submatch_int
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_submatch_prev_intarray, \g_@@_submatch_begin_intarray, \g_@@_submatch_end_intarray}
%   Hold the place where the match attempt begun and the end-points of each submatch.
%    \begin{macrocode}
\intarray_new:Nn \g_@@_submatch_prev_intarray { 65536 }
\intarray_new:Nn \g_@@_submatch_begin_intarray { 65536 }
\intarray_new:Nn \g_@@_submatch_end_intarray { 65536 }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_balance_intarray}
%   The first thing we do when matching is to store the balance of
%   begin-group/end-group characters into \cs{g_@@_balance_intarray}.
%    \begin{macrocode}
\intarray_new:Nn \g_@@_balance_intarray { 65536 }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_added_begin_int, \l_@@_added_end_int}
%   Keep track of the number of left/right braces to add when performing
%   a regex operation such as a replacement.
%    \begin{macrocode}
\int_new:N \l_@@_added_begin_int
\int_new:N \l_@@_added_end_int
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_return:}
%   This function triggers either \cs{prg_return_false:} or
%   \cs{prg_return_true:} as appropriate to whether a match was found or
%   not. It is used by all user conditionals.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_return:
  {
    \if_meaning:w \c_true_bool \g_@@_success_bool
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_query_set:n, \@@_query_set_aux:nN}
%   To easily extract subsets of the input once we found the positions
%   at which to cut, store the input tokens one by one into successive
%   \tn{toks} registers.  Also store the brace balance (used to check
%   for overall brace balance) in an array.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_query_set:n #1
  {
    \int_zero:N \l_@@_balance_int
    \int_zero:N \l_@@_curr_pos_int
    \@@_query_set_aux:nN { } F
    \tl_analysis_map_inline:nn {#1}
      { \@@_query_set_aux:nN {##1} ##3 }
    \@@_query_set_aux:nN { } F
    \int_set_eq:NN \l_@@_max_pos_int \l_@@_curr_pos_int
  }
\cs_new_protected:Npn \@@_query_set_aux:nN #1#2
  {
    \int_incr:N \l_@@_curr_pos_int
    \@@_toks_set:Nn \l_@@_curr_pos_int {#1}
    \__kernel_intarray_gset:Nnn \g_@@_balance_intarray
      { \l_@@_curr_pos_int } { \l_@@_balance_int }
    \if_case:w "#2 \exp_stop_f:
    \or: \int_incr:N \l_@@_balance_int
    \or: \int_decr:N \l_@@_balance_int
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Matching}
%
% \begin{macro}{\@@_if_match:nn}
%   We don't track submatches, and stop after a single match. Build the
%   \textsc{nfa} with |#1|, and perform the match on the query |#2|.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_if_match:nn #1#2
  {
    \group_begin:
      \@@_disable_submatches:
      \@@_single_match:
      #1
      \@@_match:n {#2}
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_count:nnN}
%   Again, we don't care about submatches. Instead of aborting after the
%   first \enquote{longest match} is found, we search for multiple
%   matches, incrementing \cs{l_@@_match_count_int} every time to
%   record the number of matches. Build the \textsc{nfa} and match. At
%   the end, store the result in the user's variable.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_count:nnN #1#2#3
  {
    \group_begin:
      \@@_disable_submatches:
      \int_zero:N \l_@@_match_count_int
      \@@_multi_match:n { \int_incr:N \l_@@_match_count_int }
      #1
      \@@_match:n {#2}
      \exp_args:NNNo
    \group_end:
    \int_set:Nn #3 { \int_use:N \l_@@_match_count_int }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Extracting submatches}
%
% \begin{macro}{\@@_extract_once:nnN, \@@_extract_all:nnN}
%   Match once or multiple times. After each match (or after the only
%   match), extract the submatches using \cs{@@_extract:}. At the
%   end, store the sequence containing all the submatches into the user
%   variable |#3| after closing the group.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_extract_once:nnN #1#2#3
  {
    \group_begin:
      \@@_single_match:
      #1
      \@@_match:n {#2}
      \@@_extract:
      \@@_query_set:n {#2}
    \@@_group_end_extract_seq:N #3
  }
\cs_new_protected:Npn \@@_extract_all:nnN #1#2#3
  {
    \group_begin:
      \@@_multi_match:n { \@@_extract: }
      #1
      \@@_match:n {#2}
      \@@_query_set:n {#2}
    \@@_group_end_extract_seq:N #3
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_split:nnN}
%   Splitting at submatches is a bit more tricky. For each match,
%   extract all submatches, and replace the zeroth submatch by the part
%   of the query between the start of the match attempt and the start of
%   the zeroth submatch. This is inhibited if the delimiter matched an
%   empty token list at the start of this match attempt.  After the last
%   match, store the last part of the token list, which ranges from the
%   start of the match attempt to the end of the query. This step is
%   inhibited if the last match was empty and at the very end: decrement
%   \cs{l_@@_submatch_int}, which controls which matches will be used.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_split:nnN #1#2#3
  {
    \group_begin:
      \@@_multi_match:n
        {
          \if_int_compare:w
            \l_@@_start_pos_int < \l_@@_success_pos_int
            \@@_extract:
            \__kernel_intarray_gset:Nnn \g_@@_submatch_prev_intarray
              { \l_@@_zeroth_submatch_int } { 0 }
            \__kernel_intarray_gset:Nnn \g_@@_submatch_end_intarray
              { \l_@@_zeroth_submatch_int }
              {
                \__kernel_intarray_item:Nn \g_@@_submatch_begin_intarray
                  { \l_@@_zeroth_submatch_int }
              }
            \__kernel_intarray_gset:Nnn \g_@@_submatch_begin_intarray
              { \l_@@_zeroth_submatch_int }
              { \l_@@_start_pos_int }
          \fi:
        }
      #1
      \@@_match:n {#2}
      \@@_query_set:n {#2}
      \__kernel_intarray_gset:Nnn \g_@@_submatch_prev_intarray
        { \l_@@_submatch_int } { 0 }
      \__kernel_intarray_gset:Nnn \g_@@_submatch_end_intarray
        { \l_@@_submatch_int }
        { \l_@@_max_pos_int }
      \__kernel_intarray_gset:Nnn \g_@@_submatch_begin_intarray
        { \l_@@_submatch_int }
        { \l_@@_start_pos_int }
      \int_incr:N \l_@@_submatch_int
      \if_meaning:w \c_true_bool \l_@@_empty_success_bool
        \if_int_compare:w \l_@@_start_pos_int = \l_@@_max_pos_int
          \int_decr:N \l_@@_submatch_int
        \fi:
      \fi:
    \@@_group_end_extract_seq:N #3
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_group_end_extract_seq:N}
%   The end-points of submatches are stored as entries of two arrays
%   from \cs{l_@@_min_submatch_int} to \cs{l_@@_submatch_int}
%   (exclusive). Extract the relevant ranges into \cs{g_@@_internal_tl},
%   separated by \cs{@@_tmp:w} |{}|. We keep track in the two flags
%   \texttt{__regex_begin} and \texttt{__regex_end} of the number of
%   begin-group or end-group tokens added to make each of these items
%   overall balanced.  At this step, |}{| is counted as being balanced
%   (same number of begin-group and end-group tokens).  This problem is
%   caught by \cs{@@_extract_check:w}, explained later.  After
%   complaining about any begin-group or end-group tokens we had to add,
%   we are ready to construct the user's sequence outside the group.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_end_extract_seq:N #1
  {
      \flag_clear:n { @@_begin }
      \flag_clear:n { @@_end }
      \cs_set_eq:NN \@@_tmp:w \scan_stop:
      \__kernel_tl_gset:Nx \g_@@_internal_tl
        {
          \int_step_function:nnN { \l_@@_min_submatch_int }
            { \l_@@_submatch_int - 1 } \@@_extract_seq_aux:n
          \@@_tmp:w
        }
      \int_set:Nn \l_@@_added_begin_int
        { \flag_height:n { @@_begin } }
      \int_set:Nn \l_@@_added_end_int
        { \flag_height:n { @@_end } }
      \tex_afterassignment:D \@@_extract_check:w
      \__kernel_tl_gset:Nx \g_@@_internal_tl
        { \g_@@_internal_tl \if_false: { \fi: } }
      \int_compare:nNnT
        { \l_@@_added_begin_int + \l_@@_added_end_int } > 0
        {
          \msg_error:nnxxx { regex } { result-unbalanced }
            { splitting~or~extracting~submatches }
            { \int_use:N \l_@@_added_begin_int }
            { \int_use:N \l_@@_added_end_int }
        }
    \group_end:
    \cs_set_eq:NN \@@_tmp:w \@@_extract_map_loop:w
    \seq_set_from_function:NnN #1
      { \@@_extract_map:N } \exp_not:n
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_extract_seq_aux:n, \@@_extract_seq_aux:ww}
%   The \texttt{:n} auxiliary builds one item of the sequence of
%   submatches. First compute the brace balance of the submatch, then
%   extract the submatch from the query, adding the appropriate braces
%   and raising a flag if the submatch is not balanced.
%    \begin{macrocode}
\cs_new:Npn \@@_extract_seq_aux:n #1
  {
    \@@_tmp:w { }
    \exp_after:wN \@@_extract_seq_aux:ww
    \int_value:w \@@_submatch_balance:n {#1} ; #1;
  }
\cs_new:Npn \@@_extract_seq_aux:ww #1; #2;
  {
    \if_int_compare:w #1 < \c_zero_int
      \prg_replicate:nn {-#1}
        {
          \flag_raise:n { @@_begin }
          \exp_not:n { { \if_false: } \fi: }
        }
    \fi:
    \@@_query_submatch:n {#2}
    \if_int_compare:w #1 > \c_zero_int
      \prg_replicate:nn {#1}
        {
          \flag_raise:n { @@_end }
          \exp_not:n { \if_false: { \fi: } }
        }
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_extract_check:w, \@@_extract_check:n,
%     \@@_extract_check_loop:w, \@@_extract_check_end:w
%   }
%   In \cs{@@_group_end_extract_seq:N} we had to expand
%   \cs{g_@@_internal_tl} to turn \cs{if_false:} constructions into
%   actual begin-group and end-group tokens.  This is done with a
%   \cs{__kernel_tl_gset:Nx} assignment, and \cs{@@_extract_check:w} is
%   run immediately after this assignment ends, thanks to the
%   \tn{afterassignment} primitive.  If all of the items were properly
%   balanced (enough begin-group tokens before end-group tokens, so |}{|
%   is not) then \cs{@@_extract_check:w} is called just before the
%   closing brace of the \cs{__kernel_tl_gset:Nx} (thanks to our sneaky
%   \cs{if_false:} |{| \cs{fi:} |}| construction), and finds that there
%   is nothing left to expand.  If any of the items is unbalanced, the
%   assignment gets ended early by an extra end-group token, and our
%   check finds more tokens needing to be expanded in a new
%   \cs{__kernel_tl_gset:Nx} assignment.  We need to add a begin-group
%   and an end-group tokens to the unbalanced item, namely to the last
%   item found so far, which we reach through a loop.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_extract_check:w
  {
    \exp_after:wN \@@_extract_check:n
    \exp_after:wN { \if_false: } \fi:
  }
\cs_new_protected:Npn \@@_extract_check:n #1
  {
    \tl_if_empty:nF {#1}
      {
        \int_incr:N \l_@@_added_begin_int
        \int_incr:N \l_@@_added_end_int
        \tex_afterassignment:D \@@_extract_check:w
        \__kernel_tl_gset:Nx \g_@@_internal_tl
          {
            \exp_after:wN \@@_extract_check_loop:w
            \g_@@_internal_tl
            \@@_tmp:w \@@_extract_check_end:w
            #1
          }
      }
  }
\cs_new:Npn \@@_extract_check_loop:w #1 \@@_tmp:w #2
  {
    #2
    \exp_not:o {#1}
    \@@_tmp:w { }
    \@@_extract_check_loop:w \prg_do_nothing:
  }
%    \end{macrocode}
%   Arguments of \cs{@@_extract_check_end:w} are: |#1| is the part of
%   the item before the extra end-group token; |#2| is junk; |#3| is
%   \cs{prg_do_nothing:} followed by the not-yet-expanded part of the
%   item after the extra end-group token.  In the replacement text, the
%   first brace and the \cs{if_false:} |{| \cs{fi:} |}| construction are
%   the added begin-group and end-group tokens (the latter being not-yet
%   expanded, just like~|#3|), while the closing brace after
%   \cs{exp_not:o} |{#1}| replaces the extra end-group token that had
%   ended the assignment early.  In particular this means that the
%   character code of that end-group token is lost.
%    \begin{macrocode}
\cs_new:Npn \@@_extract_check_end:w
    \exp_not:o #1#2 \@@_extract_check_loop:w #3 \@@_tmp:w
  {
    { \exp_not:o {#1} }
    #3
    \if_false: { \fi: }
    \@@_tmp:w
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]
%   {
%     \@@_extract_map:N,
%     \@@_extract_map_aux:NNn,
%     \@@_extract_map_loop:w
%   }
%   This receives a |seq| internal function and maps it over all items
%   in \cs{g_@@_internal_tl}.  This token list takes the form
%   \cs{@@_tmp:w} |{}| \meta{item_1} \cs{@@_tmp:w} |{}| \meta{item_2}
%   \ldots{} \cs{@@_tmp:w}, and the calling code has set \cs{@@_tmp:w}
%   equal to \cs{@@_extract_map_loop:w}.  The loop is otherwise pretty
%   standard, with \cs{prg_do_nothing:} to avoid losing braces.
%    \begin{macrocode}
\cs_new:Npn \@@_extract_map:N #1
  {
    \exp_after:wN \@@_extract_map_aux:NNn
    \exp_after:wN #1
    \g_@@_internal_tl \use_none:nnn
  }
\cs_new:Npn \@@_extract_map_aux:NNn #1#2#3
  { #3 #2 #1 \prg_do_nothing: }
\cs_new:Npn \@@_extract_map_loop:w #1#2 \@@_tmp:w #3
  {
    \exp_after:wN #1 \exp_after:wN {#2}
    #3 \@@_extract_map_loop:w #1 \prg_do_nothing:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_extract:, \@@_extract_aux:w}
%   Our task here is to store the list of end-points of submatches, and
%   store them in appropriate array entries, from
%   \cs{l_@@_zeroth_submatch_int} upwards.  First, we store in
%   \cs{g_@@_submatch_prev_intarray} the position at which the match
%   attempt started.  We extract the rest from the comma list
%   \cs{l_@@_success_submatches_tl}, which starts with entries to be
%   stored in \cs{g_@@_submatch_begin_intarray} and continues with
%   entries for \cs{g_@@_submatch_end_intarray}.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_extract:
  {
    \if_meaning:w \c_true_bool \g_@@_success_bool
      \int_set_eq:NN \l_@@_zeroth_submatch_int \l_@@_submatch_int
      \prg_replicate:nn \l_@@_capturing_group_int
        {
          \__kernel_intarray_gset:Nnn \g_@@_submatch_prev_intarray
            { \l_@@_submatch_int } { 0 }
          \int_incr:N \l_@@_submatch_int
        }
      \__kernel_intarray_gset:Nnn \g_@@_submatch_prev_intarray
        { \l_@@_zeroth_submatch_int } { \l_@@_start_pos_int }
      \int_zero:N \l_@@_internal_a_int
      \exp_after:wN \@@_extract_aux:w \l_@@_success_submatches_tl
        \prg_break_point: \@@_use_none_delimit_by_q_recursion_stop:w ,
        \q_@@_recursion_stop
    \fi:
  }
\cs_new_protected:Npn \@@_extract_aux:w #1 ,
  {
    \prg_break: #1 \prg_break_point:
    \if_int_compare:w \l_@@_internal_a_int < \l_@@_capturing_group_int
      \__kernel_intarray_gset:Nnn \g_@@_submatch_begin_intarray
        { \@@_int_eval:w \l_@@_zeroth_submatch_int + \l_@@_internal_a_int } {#1}
    \else:
      \__kernel_intarray_gset:Nnn \g_@@_submatch_end_intarray
        { \@@_int_eval:w \l_@@_zeroth_submatch_int + \l_@@_internal_a_int - \l_@@_capturing_group_int } {#1}
    \fi:
    \int_incr:N \l_@@_internal_a_int
    \@@_extract_aux:w
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Replacement}
%
% \begin{macro}{\@@_replace_once:nnN}
%   Build the \textsc{nfa} and the replacement functions, then find a
%   single match.  If the match failed, simply exit the
%   group. Otherwise, we do the replacement. Extract submatches. Compute
%   the brace balance corresponding to replacing this match by the
%   replacement (this depends on submatches). Prepare the replaced token
%   list: the replacement function produces the tokens from the start of
%   the query to the start of the match and the replacement text for
%   this match; we need to add the tokens from the end of the match to
%   the end of the query. Finally, store the result in the user's
%   variable after closing the group: this step involves an additional
%   \texttt{x}-expansion, and checks that braces are balanced in the
%   final result.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replace_once:nnN #1#2#3
  {
    \group_begin:
      \@@_single_match:
      #1
      \exp_args:No \@@_match:n {#3}
      \if_meaning:w \c_false_bool \g_@@_success_bool
        \group_end:
      \else:
        \@@_extract:
        \exp_args:No \@@_query_set:n {#3}
        \@@_replacement:n {#2}
        \int_set:Nn \l_@@_balance_int
          {
            \@@_replacement_balance_one_match:n
              { \l_@@_zeroth_submatch_int }
          }
        \__kernel_tl_set:Nx \l_@@_internal_a_tl
          {
            \@@_replacement_do_one_match:n
              { \l_@@_zeroth_submatch_int }
            \@@_query_range:nn
              {
                \__kernel_intarray_item:Nn \g_@@_submatch_end_intarray
                  { \l_@@_zeroth_submatch_int }
              }
              { \l_@@_max_pos_int }
          }
        \@@_group_end_replace:N #3
      \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_replace_all:nnN}
%   Match multiple times, and for every match, extract submatches and
%   additionally store the position at which the match attempt started.
%   The entries from \cs{l_@@_min_submatch_int} to
%   \cs{l_@@_submatch_int} hold information about submatches of every
%   match in order; each match corresponds to
%   \cs{l_@@_capturing_group_int} consecutive entries.
%   Compute the brace balance corresponding to doing all the
%   replacements: this is the sum of brace balances for replacing each
%   match. Join together the replacement texts for each match (including
%   the part of the query before the match), and the end of the query.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_replace_all:nnN #1#2#3
  {
    \group_begin:
      \@@_multi_match:n { \@@_extract: }
      #1
      \exp_args:No \@@_match:n {#3}
      \exp_args:No \@@_query_set:n {#3}
      \@@_replacement:n {#2}
      \int_set:Nn \l_@@_balance_int
        {
          0
          \int_step_function:nnnN
            { \l_@@_min_submatch_int }
            \l_@@_capturing_group_int
            { \l_@@_submatch_int - 1 }
            \@@_replacement_balance_one_match:n
        }
      \__kernel_tl_set:Nx \l_@@_internal_a_tl
        {
          \int_step_function:nnnN
            { \l_@@_min_submatch_int }
            \l_@@_capturing_group_int
            { \l_@@_submatch_int - 1 }
            \@@_replacement_do_one_match:n
          \@@_query_range:nn
            \l_@@_start_pos_int \l_@@_max_pos_int
        }
    \@@_group_end_replace:N #3
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_group_end_replace:N, \@@_group_end_replace_try:,
%     \@@_group_end_replace_check:w, \@@_group_end_replace_check:n
%   }
%   At this stage \cs{l_@@_internal_a_tl} (|x|-expands to the desired
%   result).  Guess from \cs{l_@@_balance_int} the number of braces to
%   add before or after the result then try expanding.  The simplest
%   case is when \cs{l_@@_internal_a_tl} together with the braces we
%   insert via \cs{prg_replicate:nn} give a balanced result, and the
%   assignment ends at the \cs{if_false:} |{| \cs{fi:} |}| construction:
%   then \cs{@@_group_end_replace_check:w} sees that there is no
%   material left and we successfully found the result.  The harder case
%   is that expanding \cs{l_@@_internal_a_tl} may produce extra closing
%   braces and end the assignment early.  Then we grab the remaining code
%   using; importantly, what follows has not yet been expanded so that
%   \cs{@@_group_end_replace_check:n} grabs everything until the last
%   brace in \cs{@@_group_end_replace_try:}, letting us try again with
%   an extra surrounding pair of braces.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_group_end_replace:N #1
  {
    \int_set:Nn \l_@@_added_begin_int
      { \int_max:nn { - \l_@@_balance_int } { 0 } }
    \int_set:Nn \l_@@_added_end_int
      { \int_max:nn { \l_@@_balance_int } { 0 } }
    \@@_group_end_replace_try:
    \int_compare:nNnT { \l_@@_added_begin_int + \l_@@_added_end_int } > 0
      {
        \msg_error:nnxxx { regex } { result-unbalanced }
          { replacing } { \int_use:N \l_@@_added_begin_int }
          { \int_use:N \l_@@_added_end_int }
      }
    \group_end:
    \tl_set_eq:NN #1 \g_@@_internal_tl
  }
\cs_new_protected:Npn \@@_group_end_replace_try:
  {
    \tex_afterassignment:D \@@_group_end_replace_check:w
    \__kernel_tl_gset:Nx \g_@@_internal_tl
      {
        \prg_replicate:nn { \l_@@_added_begin_int } { { \if_false: } \fi: }
        \l_@@_internal_a_tl
        \prg_replicate:nn { \l_@@_added_end_int } { \if_false: { \fi: } }
        \if_false: { \fi: }
      }
  }
\cs_new_protected:Npn \@@_group_end_replace_check:w
  {
    \exp_after:wN \@@_group_end_replace_check:n
    \exp_after:wN { \if_false: } \fi:
  }
\cs_new_protected:Npn \@@_group_end_replace_check:n #1
  {
    \tl_if_empty:nF {#1}
      {
        \int_incr:N \l_@@_added_begin_int
        \int_incr:N \l_@@_added_end_int
        \@@_group_end_replace_try:
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Peeking ahead}
%
% \begin{variable}{\l_@@_peek_true_tl, \l_@@_peek_false_tl}
%   True/false code arguments of \cs{peek_regex:nTF} or similar.
%    \begin{macrocode}
\tl_new:N \l_@@_peek_true_tl
\tl_new:N \l_@@_peek_false_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_replacement_tl}
%   When peeking in \cs{peek_regex_replace_once:nnTF} we need to store
%   the replacement text.
%    \begin{macrocode}
\tl_new:N \l_@@_replacement_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_input_tl}
% \begin{macro}{\@@_input_item:n}
%   Stores each token found as \cs{@@_input_item:n} \Arg{tokens}, where
%   the \meta{tokens} \texttt{o}-expand to the token found, as for
%   \cs{tl_analysis_map_inline:nn}.
%    \begin{macrocode}
\tl_new:N \l_@@_input_tl
\cs_new_eq:NN \@@_input_item:n ?
%    \end{macrocode}
% \end{macro}
% \end{variable}
%
% \begin{macro}[TF]
%   {\peek_regex:n, \peek_regex:N, \peek_regex_remove_once:n, \peek_regex_remove_once:N}
%   The |T| and |F| functions just call the corresponding |TF| function.
%   The four |TF| functions differ along two axes: whether to remove the
%   token or not, distinguished by using \cs{@@_peek_end:} or
%   \cs{@@_peek_remove_end:n} (the latter case needs an argument, as we
%   will see), and whether the regex has to be compiled or is already in
%   an |N|-type variable, distinguished by calling \cs{@@_build_aux:Nn}
%   or \cs{@@_build_aux:NN}.  The first argument of these functions is
%   \cs{c_false_bool} to indicate that there should be no implicit
%   insertion of a wildcard at the start of the pattern: otherwise the
%   code would keep looking further into the input stream until matching
%   the regex.
%    \begin{macrocode}
\cs_new_protected:Npn \peek_regex:nTF #1
  {
    \@@_peek:nnTF
      { \@@_build_aux:Nn \c_false_bool {#1} }
      { \@@_peek_end: }
  }
\cs_new_protected:Npn \peek_regex:nT #1#2
  { \peek_regex:nTF {#1} {#2} { } }
\cs_new_protected:Npn \peek_regex:nF #1 { \peek_regex:nTF {#1} { } }
\cs_new_protected:Npn \peek_regex:NTF #1
  {
    \@@_peek:nnTF
      { \@@_build_aux:NN \c_false_bool #1 }
      { \@@_peek_end: }
  }
\cs_new_protected:Npn \peek_regex:NT #1#2
  { \peek_regex:NTF #1 {#2} { } }
\cs_new_protected:Npn \peek_regex:NF #1 { \peek_regex:NTF {#1} { } }
\cs_new_protected:Npn \peek_regex_remove_once:nTF #1
  {
    \@@_peek:nnTF
      { \@@_build_aux:Nn \c_false_bool {#1} }
      { \@@_peek_remove_end:n {##1} }
  }
\cs_new_protected:Npn \peek_regex_remove_once:nT #1#2
  { \peek_regex_remove_once:nTF {#1} {#2} { } }
\cs_new_protected:Npn \peek_regex_remove_once:nF #1
  { \peek_regex_remove_once:nTF {#1} { } }
\cs_new_protected:Npn \peek_regex_remove_once:NTF #1
  {
    \@@_peek:nnTF
      { \@@_build_aux:NN \c_false_bool #1 }
      { \@@_peek_remove_end:n {##1} }
  }
\cs_new_protected:Npn \peek_regex_remove_once:NT #1#2
  { \peek_regex_remove_once:NTF #1 {#2} { } }
\cs_new_protected:Npn \peek_regex_remove_once:NF #1
  { \peek_regex_remove_once:NTF #1 { } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek:nnTF, \@@_peek_aux:nnTF}
%   Store the user's true/false codes (plus \cs{group_end:}) into two
%   token lists.  Then build the automaton with |#1|, without submatch
%   tracking, and aiming for a single match.  Then start matching by
%   setting up a few variables like for any regex matching like
%   \cs{regex_match:nnTF}, with the addition of \cs{l_@@_input_tl}
%   that keeps track of the tokens seen, to reinsert them at the
%   end.  Instead of \cs{tl_analysis_map_inline:nn} on the input, we
%   call \cs{peek_analysis_map_inline:n} to go through tokens in the
%   input stream.  Since \cs{@@_match_one_token:nnN} calls
%   \cs{@@_maplike_break:} we need to catch that and break the
%   \cs{peek_analysis_map_inline:n} loop instead.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek:nnTF #1
  {
    \@@_peek_aux:nnTF
      {
        \@@_disable_submatches:
        #1
      }
  }
\cs_new_protected:Npn \@@_peek_aux:nnTF #1#2#3#4
  {
    \group_begin:
      \tl_set:Nn \l_@@_peek_true_tl { \group_end: #3 }
      \tl_set:Nn \l_@@_peek_false_tl { \group_end: #4 }
      \@@_single_match:
      #1
      \@@_match_init:
      \tl_build_clear:N \l_@@_input_tl
      \@@_match_once_init:
      \peek_analysis_map_inline:n
        {
          \tl_build_put_right:Nn \l_@@_input_tl
            { \@@_input_item:n {##1} }
          \@@_match_one_token:nnN {##1} {##2} ##3
          \use_none:nnn
          \prg_break_point:Nn \@@_maplike_break:
            { \peek_analysis_map_break:n {#2} }
        }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek_end:, \@@_peek_remove_end:n}
%   Once the regex matches (or permanently fails to match) we call
%   \cs{@@_peek_end:}, or \cs{@@_peek_remove_end:n} with argument the
%   last token seen.  For \cs{peek_regex:nTF} we reinsert tokens seen by
%   calling \cs{@@_peek_reinsert:N} regardless of the result of the
%   match.  For \cs{peek_regex_remove_once:nTF} we reinsert the tokens
%   seen only if the match failed; otherwise we just reinsert the
%   tokens~|#1|, with one expansion.  To be more precise, |#1| consists
%   of tokens that \texttt{o}-expand and \texttt{x}-expand to the last
%   token seen, for example it is \cs{exp_not:N} \meta{cs} for a control
%   sequence.  This means that just doing \cs{exp_after:wN}
%   \cs{l_@@_peek_true_tl} |#1| would be unsafe because the expansion of
%   \meta{cs} would be suppressed.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek_end:
  {
    \bool_if:NTF \g_@@_success_bool
      { \@@_peek_reinsert:N \l_@@_peek_true_tl }
      { \@@_peek_reinsert:N \l_@@_peek_false_tl }
  }
\cs_new_protected:Npn \@@_peek_remove_end:n #1
  {
    \bool_if:NTF \g_@@_success_bool
      { \exp_args:NNo \use:nn \l_@@_peek_true_tl {#1} }
      { \@@_peek_reinsert:N \l_@@_peek_false_tl }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek_reinsert:N, \@@_reinsert_item:n}
%   Insert the true/false code |#1|, followed by the tokens found, which
%   were stored in \cs{l_@@_input_tl}.  For this, loop through that
%   token list using \cs{@@_reinsert_item:n}, which expands |#1| once to
%   get a single token, and jumps over it to expand what follows, with
%   suitable \cs{exp:w} and \cs{exp_end:}.  We cannot just use
%   \cs{use:e} on the whole token list because the result may be
%   unbalanced, which would stop the primitive prematurely, or let it
%   continue beyond where we would like.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek_reinsert:N #1
  {
    \tl_build_end:N \l_@@_input_tl
    \cs_set_eq:NN \@@_input_item:n \@@_reinsert_item:n
    \exp_after:wN #1 \exp:w \l_@@_input_tl \exp_end:
  }
\cs_new_protected:Npn \@@_reinsert_item:n #1
  {
    \exp_after:wN \exp_after:wN
    \exp_after:wN \exp_end:
    \exp_after:wN \exp_after:wN
    #1
    \exp:w
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[noTF]
%   {\peek_regex_replace_once:nn, \peek_regex_replace_once:Nn}
%   Similar to \cs{peek_regex:nTF} above.
%    \begin{macrocode}
\cs_new_protected:Npn \peek_regex_replace_once:nnTF #1
  { \@@_peek_replace:nnTF { \@@_build_aux:Nn \c_false_bool {#1} } }
\cs_new_protected:Npn \peek_regex_replace_once:nnT #1#2#3
  { \peek_regex_replace_once:nnTF {#1} {#2} {#3} { } }
\cs_new_protected:Npn \peek_regex_replace_once:nnF #1#2
  { \peek_regex_replace_once:nnTF {#1} {#2} { } }
\cs_new_protected:Npn \peek_regex_replace_once:nn #1#2
  { \peek_regex_replace_once:nnTF {#1} {#2} { } { } }
\cs_new_protected:Npn \peek_regex_replace_once:NnTF #1
  { \@@_peek_replace:nnTF { \@@_build_aux:NN \c_false_bool #1 } }
\cs_new_protected:Npn \peek_regex_replace_once:NnT #1#2#3
  { \peek_regex_replace_once:NnTF #1 {#2} {#3} { } }
\cs_new_protected:Npn \peek_regex_replace_once:NnF #1#2
  { \peek_regex_replace_once:NnTF #1 {#2} { } }
\cs_new_protected:Npn \peek_regex_replace_once:Nn #1#2
  { \peek_regex_replace_once:NnTF #1 {#2} { } { } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek_replace:nnTF}
%   Same as \cs{@@_peek:nnTF} (used for \cs{peek_regex:nTF} above), but
%   without disabling submatches, and with a different end.  The
%   replacement text |#2| is stored, to be analyzed later.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek_replace:nnTF #1#2
  {
    \tl_set:Nn \l_@@_replacement_tl {#2}
    \@@_peek_aux:nnTF {#1} { \@@_peek_replace_end: }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek_replace_end:}
%   If the match failed \cs{@@_peek_reinsert:N} reinserts the tokens
%   found.  Otherwise, finish storing the submatch information using
%   \cs{@@_extract:}, and store the input into \tn{toks}.  Redefine a
%   few auxiliaries to change slightly their expansion behaviour as
%   explained below.  Analyse the replacement text with
%   \cs{@@_replacement:n}, which as usual defines
%   \cs{@@_replacement_do_one_match:n} to insert the tokens from the
%   start of the match attempt to the beginning of the match, followed
%   by the replacement text.  The \cs{use:x} expands for instance the
%   trailing \cs{@@_query_range:nn} down to a sequence of
%   \cs{@@_reinsert_item:n} \Arg{tokens} where \meta{tokens}
%   \texttt{o}-expand to a single token that we want to insert.  After
%   \texttt{x}-expansion, \cs{use:x} does \cs{use:n}, so we have
%   \cs{exp_after:wN} \cs{l_@@_peek_true_tl} \cs{exp:w} \ldots{}
%   \cs{exp_end:}.  This is set up such as to obtain
%   \cs{l_@@_peek_true_tl} followed by the replaced tokens (possibly
%   unbalanced) in the input stream.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek_replace_end:
  {
    \bool_if:NTF \g_@@_success_bool
      {
        \@@_extract:
        \@@_query_set_from_input_tl:
        \cs_set_eq:NN \@@_replacement_put:n \@@_peek_replacement_put:n
        \cs_set_eq:NN \@@_replacement_put_submatch_aux:n
          \@@_peek_replacement_put_submatch_aux:n
        \cs_set_eq:NN \@@_input_item:n \@@_reinsert_item:n
        \cs_set_eq:NN \@@_replacement_exp_not:N \@@_peek_replacement_token:n
        \cs_set_eq:NN \@@_replacement_exp_not:V \@@_peek_replacement_var:N
        \exp_args:No \@@_replacement:n { \l_@@_replacement_tl }
        \use:x
          {
            \exp_not:n { \exp_after:wN \l_@@_peek_true_tl \exp:w }
            \@@_replacement_do_one_match:n
              { \l_@@_zeroth_submatch_int }
            \@@_query_range:nn
              {
                \__kernel_intarray_item:Nn \g_@@_submatch_end_intarray
                  { \l_@@_zeroth_submatch_int }
              }
              { \l_@@_max_pos_int }
            \exp_end:
          }
      }
      { \@@_peek_reinsert:N \l_@@_peek_false_tl }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_query_set_from_input_tl:, \@@_query_set_item:n}
%   The input was stored into \cs{l_@@_input_tl} as successive items
%   \cs{@@_input_item:n} \Arg{tokens}.  Store that in successive
%   \tn{toks}.  It's not clear whether the empty entries before and
%   after are both useful.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_query_set_from_input_tl:
  {
    \tl_build_end:N \l_@@_input_tl
    \int_zero:N \l_@@_curr_pos_int
    \cs_set_eq:NN \@@_input_item:n \@@_query_set_item:n
    \@@_query_set_item:n { }
    \l_@@_input_tl
    \@@_query_set_item:n { }
    \int_set_eq:NN \l_@@_max_pos_int \l_@@_curr_pos_int
  }
\cs_new_protected:Npn \@@_query_set_item:n #1
  {
    \int_incr:N \l_@@_curr_pos_int
    \@@_toks_set:Nn \l_@@_curr_pos_int { \@@_input_item:n {#1} }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek_replacement_put:n}
%   While building the replacement function
%   \cs{@@_replacement_do_one_match:n}, we often want to put simple
%   material, given as |#1|, whose \texttt{x}-expansion
%   \texttt{o}-expands to a single token.  Normally we can just add the
%   token to \cs{l_@@_build_tl}, but for
%   \cs{peek_regex_replace_once:nnTF} we eventually want to do some
%   strange expansion that is basically using \cs{exp_after:wN} to jump
%   through numerous tokens (we cannot use \texttt{x}-expansion like for
%   \cs{regex_replace_once:nnNTF} because it is ok for the result to be
%   unbalanced since we insert it in the input stream rather than
%   storing it.  When within a csname we don't do any such shenanigan
%   because \cs{cs:w} \ldots{} \cs{cs_end:} does all the expansion we
%   need.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek_replacement_put:n #1
  {
    \if_case:w \l_@@_replacement_csnames_int
      \tl_build_put_right:Nn \l_@@_build_tl
        { \exp_not:N \@@_reinsert_item:n {#1} }
    \else:
      \tl_build_put_right:Nn \l_@@_build_tl {#1}
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek_replacement_token:n}
%   When hit with \cs{exp:w}, \cs{@@_peek_replacement_token:n}
%   \Arg{token} stops \cs{exp_end:} and does \cs{exp_after:wN}
%   \meta{token} \cs{exp:w} to continue expansion after it.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek_replacement_token:n #1
  { \exp_after:wN \exp_end: \exp_after:wN #1 \exp:w }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek_replacement_put_submatch_aux:n}
%   While analyzing the replacement we also have to insert submatches
%   found in the query.  Since query items \cs{@@_input_item:n}
%   \Arg{tokens} expand correctly only when surrounded by \cs{exp:w}
%   \ldots{} \cs{exp_end:}, and since these expansion controls are not
%   there within csnames (because \cs{cs:w} \ldots{} \cs{cs_end:} make
%   them unnecessary in most cases), we have to put \cs{exp:w} and
%   \cs{exp_end:} by hand here.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek_replacement_put_submatch_aux:n #1
  {
    \if_case:w \l_@@_replacement_csnames_int
      \tl_build_put_right:Nn \l_@@_build_tl
        { \@@_query_submatch:n { \int_eval:n { #1 + ##1 } } }
    \else:
      \tl_build_put_right:Nn \l_@@_build_tl
        { \exp:w \@@_query_submatch:n { \int_eval:n { #1 + ##1 } } \exp_end: }
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_peek_replacement_var:N}
%   This is used for |\u| outside csnames.  It makes sure to continue
%   expansion with \cs{exp:w} before expanding the variable~|#1| and
%   stopping the \cs{exp:w} that precedes.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_peek_replacement_var:N #1
  {
    \exp_after:wN \exp_last_unbraced:NV
    \exp_after:wN \exp_end:
    \exp_after:wN #1
    \exp:w
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Messages}
%
% Messages for the preparsing phase.
%    \begin{macrocode}
\use:x
  {
    \msg_new:nnn { regex } { trailing-backslash }
      { Trailing~'\iow_char:N\\'~in~regex~or~replacement. }
    \msg_new:nnn { regex } { x-missing-rbrace }
      {
        Missing~brace~'\iow_char:N\}'~in~regex~
        '...\iow_char:N\\x\iow_char:N\{...##1'.
      }
    \msg_new:nnn { regex } { x-overflow }
      {
        Character~code~##1~too~large~in~
        \iow_char:N\\x\iow_char:N\{##2\iow_char:N\}~regex.
      }
  }
%    \end{macrocode}
%
% Invalid quantifier.
%    \begin{macrocode}
\msg_new:nnnn { regex } { invalid-quantifier }
  { Braced~quantifier~'#1'~may~not~be~followed~by~'#2'. }
  {
    The~character~'#2'~is~invalid~in~the~braced~quantifier~'#1'.~
    The~only~valid~quantifiers~are~'*',~'?',~'+',~'{<int>}',~
    '{<min>,}'~and~'{<min>,<max>}',~optionally~followed~by~'?'.
  }
%    \end{macrocode}
%
% Messages for missing or extra closing brackets and parentheses, with
% some fancy singular/plural handling for the case of parentheses.
%    \begin{macrocode}
\msg_new:nnnn { regex } { missing-rbrack }
  { Missing~right~bracket~inserted~in~regular~expression. }
  {
    LaTeX~was~given~a~regular~expression~where~a~character~class~
    was~started~with~'[',~but~the~matching~']'~is~missing.
  }
\msg_new:nnnn { regex } { missing-rparen }
  {
    Missing~right~
    \int_compare:nTF { #1 = 1 } { parenthesis } { parentheses } ~
    inserted~in~regular~expression.
  }
  {
    LaTeX~was~given~a~regular~expression~with~\int_eval:n {#1} ~
    more~left~parentheses~than~right~parentheses.
  }
\msg_new:nnnn { regex } { extra-rparen }
  { Extra~right~parenthesis~ignored~in~regular~expression. }
  {
    LaTeX~came~across~a~closing~parenthesis~when~no~submatch~group~
    was~open.~The~parenthesis~will~be~ignored.
  }
%    \end{macrocode}
%
% Some escaped alphanumerics are not allowed everywhere.
%    \begin{macrocode}
\msg_new:nnnn { regex } { bad-escape }
  {
    Invalid~escape~'\iow_char:N\\#1'~
    \@@_if_in_cs:TF { within~a~control~sequence. }
      {
        \@@_if_in_class:TF
          { in~a~character~class. }
          { following~a~category~test. }
      }
  }
  {
    The~escape~sequence~'\iow_char:N\\#1'~may~not~appear~
    \@@_if_in_cs:TF
      {
        within~a~control~sequence~test~introduced~by~
        '\iow_char:N\\c\iow_char:N\{'.
      }
      {
        \@@_if_in_class:TF
          { within~a~character~class~ }
          { following~a~category~test~such~as~'\iow_char:N\\cL'~ }
        because~it~does~not~match~exactly~one~character.
      }
  }
%    \end{macrocode}
%
% Range errors.
%    \begin{macrocode}
\msg_new:nnnn { regex } { range-missing-end }
  { Invalid~end-point~for~range~'#1-#2'~in~character~class. }
  {
    The~end-point~'#2'~of~the~range~'#1-#2'~may~not~serve~as~an~
    end-point~for~a~range:~alphanumeric~characters~should~not~be~
    escaped,~and~non-alphanumeric~characters~should~be~escaped.
  }
\msg_new:nnnn { regex } { range-backwards }
  { Range~'[#1-#2]'~out~of~order~in~character~class. }
  {
    In~ranges~of~characters~'[x-y]'~appearing~in~character~classes,~
    the~first~character~code~must~not~be~larger~than~the~second.~
    Here,~'#1'~has~character~code~\int_eval:n {`#1},~while~
    '#2'~has~character~code~\int_eval:n {`#2}.
  }
%    \end{macrocode}
%
% Errors related to |\c| and |\u|.
%    \begin{macrocode}
\msg_new:nnnn { regex } { c-bad-mode }
  { Invalid~nested~'\iow_char:N\\c'~escape~in~regular~expression. }
  {
    The~'\iow_char:N\\c'~escape~cannot~be~used~within~
    a~control~sequence~test~'\iow_char:N\\c{...}'~
    nor~another~category~test.~
    To~combine~several~category~tests,~use~'\iow_char:N\\c[...]'.
  }
\msg_new:nnnn { regex } { c-C-invalid }
  { '\iow_char:N\\cC'~should~be~followed~by~'.'~or~'(',~not~'#1'. }
  {
    The~'\iow_char:N\\cC'~construction~restricts~the~next~item~to~be~a~
    control~sequence~or~the~next~group~to~be~made~of~control~sequences.~
    It~only~makes~sense~to~follow~it~by~'.'~or~by~a~group.
  }
\msg_new:nnnn { regex } { cu-lbrace }
  { Left~braces~must~be~escaped~in~'\iow_char:N\\#1{...}'. }
  {
    Constructions~such~as~'\iow_char:N\\#1{...\iow_char:N\{...}'~are~
    not~allowed~and~should~be~replaced~by~
    '\iow_char:N\\#1{...\token_to_str:N\{...}'.
  }
\msg_new:nnnn { regex } { c-lparen-in-class }
  { Catcode~test~cannot~apply~to~group~in~character~class }
  {
    Construction~such~as~'\iow_char:N\\cL(abc)'~are~not~allowed~inside~a~
    class~'[...]'~because~classes~do~not~match~multiple~characters~at~once.
  }
\msg_new:nnnn { regex } { c-missing-rbrace }
  { Missing~right~brace~inserted~for~'\iow_char:N\\c'~escape. }
  {
    LaTeX~was~given~a~regular~expression~where~a~
    '\iow_char:N\\c\iow_char:N\{...'~construction~was~not~ended~
    with~a~closing~brace~'\iow_char:N\}'.
  }
\msg_new:nnnn { regex } { c-missing-rbrack }
  { Missing~right~bracket~inserted~for~'\iow_char:N\\c'~escape. }
  {
    A~construction~'\iow_char:N\\c[...'~appears~in~a~
    regular~expression,~but~the~closing~']'~is~not~present.
  }
\msg_new:nnnn { regex } { c-missing-category }
  { Invalid~character~'#1'~following~'\iow_char:N\\c'~escape. }
  {
    In~regular~expressions,~the~'\iow_char:N\\c'~escape~sequence~
    may~only~be~followed~by~a~left~brace,~a~left~bracket,~or~a~
    capital~letter~representing~a~character~category,~namely~
    one~of~'ABCDELMOPSTU'.
  }
\msg_new:nnnn { regex } { c-trailing }
  { Trailing~category~code~escape~'\iow_char:N\\c'... }
  {
    A~regular~expression~ends~with~'\iow_char:N\\c'~followed~
    by~a~letter.~It~will~be~ignored.
  }
\msg_new:nnnn { regex } { u-missing-lbrace }
  { Missing~left~brace~following~'\iow_char:N\\u'~escape. }
  {
    The~'\iow_char:N\\u'~escape~sequence~must~be~followed~by~
    a~brace~group~with~the~name~of~the~variable~to~use.
  }
\msg_new:nnnn { regex } { u-missing-rbrace }
  { Missing~right~brace~inserted~for~'\iow_char:N\\u'~escape. }
  {
    LaTeX~
    \str_if_eq:eeTF { } {#2}
      { reached~the~end~of~the~string~ }
      { encountered~an~escaped~alphanumeric~character '\iow_char:N\\#2'~ }
    when~parsing~the~argument~of~an~
    '\iow_char:N\\u\iow_char:N\{...\}'~escape.
  }
%    \end{macrocode}
%
% Errors when encountering the \textsc{posix} syntax |[:...:]|.
%    \begin{macrocode}
\msg_new:nnnn { regex } { posix-unsupported }
  { POSIX~collating~element~'[#1 ~ #1]'~not~supported. }
  {
    The~'[.foo.]'~and~'[=bar=]'~syntaxes~have~a~special~meaning~
    in~POSIX~regular~expressions.~This~is~not~supported~by~LaTeX.~
    Maybe~you~forgot~to~escape~a~left~bracket~in~a~character~class?
  }
\msg_new:nnnn { regex } { posix-unknown }
  { POSIX~class~'[:#1:]'~unknown. }
  {
    '[:#1:]'~is~not~among~the~known~POSIX~classes~
    '[:alnum:]',~'[:alpha:]',~'[:ascii:]',~'[:blank:]',~
    '[:cntrl:]',~'[:digit:]',~'[:graph:]',~'[:lower:]',~
    '[:print:]',~'[:punct:]',~'[:space:]',~'[:upper:]',~
    '[:word:]',~and~'[:xdigit:]'.
  }
\msg_new:nnnn { regex } { posix-missing-close }
  { Missing~closing~':]'~for~POSIX~class. }
  { The~POSIX~syntax~'#1'~must~be~followed~by~':]',~not~'#2'. }
%    \end{macrocode}
%
% In various cases, the result of a \pkg{l3regex} operation can leave us
% with an unbalanced token list, which we must re-balance by adding
% begin-group or end-group character tokens.
%    \begin{macrocode}
\msg_new:nnnn { regex } { result-unbalanced }
  { Missing~brace~inserted~when~#1. }
  {
    LaTeX~was~asked~to~do~some~regular~expression~operation,~
    and~the~resulting~token~list~would~not~have~the~same~number~
    of~begin-group~and~end-group~tokens.~Braces~were~inserted:~
    #2~left,~#3~right.
  }
%    \end{macrocode}
%
% Error message for unknown options.
%    \begin{macrocode}
\msg_new:nnnn { regex } { unknown-option }
  { Unknown~option~'#1'~for~regular~expressions. }
  {
    The~only~available~option~is~'case-insensitive',~toggled~by~
    '(?i)'~and~'(?-i)'.
  }
\msg_new:nnnn { regex } { special-group-unknown }
  { Unknown~special~group~'#1~...'~in~a~regular~expression. }
  {
    The~only~valid~constructions~starting~with~'(?'~are~
    '(?:~...~)',~'(?|~...~)',~'(?i)',~and~'(?-i)'.
  }
%    \end{macrocode}
%
% Errors in the replacement text.
%    \begin{macrocode}
\msg_new:nnnn { regex } { replacement-c }
  { Misused~'\iow_char:N\\c'~command~in~a~replacement~text. }
  {
    In~a~replacement~text,~the~'\iow_char:N\\c'~escape~sequence~
    can~be~followed~by~one~of~the~letters~'ABCDELMOPSTU'~
    or~a~brace~group,~not~by~'#1'.
  }
\msg_new:nnnn { regex } { replacement-u }
  { Misused~'\iow_char:N\\u'~command~in~a~replacement~text. }
  {
    In~a~replacement~text,~the~'\iow_char:N\\u'~escape~sequence~
    must~be~~followed~by~a~brace~group~holding~the~name~of~the~
    variable~to~use.
  }
\msg_new:nnnn { regex } { replacement-g }
  {
    Missing~brace~for~the~'\iow_char:N\\g'~construction~
    in~a~replacement~text.
  }
  {
    In~the~replacement~text~for~a~regular~expression~search,~
    submatches~are~represented~either~as~'\iow_char:N \\g{dd..d}',~
    or~'\\d',~where~'d'~are~single~digits.~Here,~a~brace~is~missing.
  }
\msg_new:nnnn { regex } { replacement-catcode-end }
  {
    Missing~character~for~the~'\iow_char:N\\c<category><character>'~
    construction~in~a~replacement~text.
  }
  {
    In~a~replacement~text,~the~'\iow_char:N\\c'~escape~sequence~
    can~be~followed~by~one~of~the~letters~'ABCDELMOPSTU'~representing~
    the~character~category.~Then,~a~character~must~follow.~LaTeX~
    reached~the~end~of~the~replacement~when~looking~for~that.
  }
\msg_new:nnnn { regex } { replacement-catcode-escaped }
  {
    Escaped~letter~or~digit~after~category~code~in~replacement~text.
  }
  {
    In~a~replacement~text,~the~'\iow_char:N\\c'~escape~sequence~
    can~be~followed~by~one~of~the~letters~'ABCDELMOPSTU'~representing~
    the~character~category.~Then,~a~character~must~follow,~not~
    '\iow_char:N\\#2'.
  }
\msg_new:nnnn { regex } { replacement-catcode-in-cs }
  {
    Category~code~'\iow_char:N\\c#1#3'~ignored~inside~
    '\iow_char:N\\c\{...\}'~in~a~replacement~text.
  }
  {
    In~a~replacement~text,~the~category~codes~of~the~argument~of~
    '\iow_char:N\\c\{...\}'~are~ignored~when~building~the~control~
    sequence~name.
  }
\msg_new:nnnn { regex } { replacement-null-space }
  { TeX~cannot~build~a~space~token~with~character~code~0. }
  {
    You~asked~for~a~character~token~with~category~space,~
    and~character~code~0,~for~instance~through~
    '\iow_char:N\\cS\iow_char:N\\x00'.~
    This~specific~case~is~impossible~and~will~be~replaced~
    by~a~normal~space.
  }
\msg_new:nnnn { regex } { replacement-missing-rbrace }
  { Missing~right~brace~inserted~in~replacement~text. }
  {
    There~ \int_compare:nTF { #1 = 1 } { was } { were } ~ #1~
    missing~right~\int_compare:nTF { #1 = 1 } { brace } { braces } .
  }
\msg_new:nnnn { regex } { replacement-missing-rparen }
  { Missing~right~parenthesis~inserted~in~replacement~text. }
  {
    There~ \int_compare:nTF { #1 = 1 } { was } { were } ~ #1~
    missing~right~
    \int_compare:nTF { #1 = 1 } { parenthesis } { parentheses } .
  }
\msg_new:nnn { regex } { submatch-too-big }
  { Submatch~#1~used~but~regex~only~has~#2~group(s) }
%    \end{macrocode}
%
% Some escaped alphanumerics are not allowed everywhere.
%    \begin{macrocode}
\msg_new:nnnn { regex } { backwards-quantifier }
  { Quantifer~"{#1,#2}"~is~backwards. }
  { The~values~given~in~a~quantifier~must~be~in~order. }
%    \end{macrocode}
%
% Used when showing a regex.
%    \begin{macrocode}
\msg_new:nnn { regex } { show }
  {
    >~Compiled~regex~
    \tl_if_empty:nTF {#1} { variable~ #2 } { {#1} } :
    #3
  }
\prop_gput:Nnn \g_msg_module_name_prop { regex } { LaTeX3 }
\prop_gput:Nnn \g_msg_module_type_prop { regex } { }
%    \end{macrocode}
%
% \begin{macro}{\@@_msg_repeated:nnN}
%   This is not technically a message, but seems related enough to go
%   there. The arguments are: |#1| is the minimum number of repetitions;
%   |#2| is the number of allowed extra repetitions ($-1$ for infinite
%   number), and |#3| tells us about lazyness.
%    \begin{macrocode}
\cs_new:Npn \@@_msg_repeated:nnN #1#2#3
  {
    \str_if_eq:eeF { #1 #2 } { 1 0 }
      {
        , ~ repeated ~
        \int_case:nnF {#2}
          {
            { -1 } { #1~or~more~times,~\bool_if:NTF #3 { lazy } { greedy } }
            {  0 } { #1~times }
          }
          {
            between~#1~and~\int_eval:n {#1+#2}~times,~
            \bool_if:NTF #3 { lazy } { greedy }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Code for tracing}
%
% There is a more extensive implementation of tracing in the l3trial
% package \pkg{l3trace}.  Function names are a bit different but could
% be merged.
%
% \begin{macro}
%   {\@@_trace_push:nnN, \@@_trace_pop:nnN, \@@_trace:nnx}
%   Here |#1| is the module name (\texttt{regex}) and |#2| is
%   typically~1.  If the module's current tracing level is less than
%   |#2| show nothing, otherwise write |#3| to the terminal.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_trace_push:nnN #1#2#3
  { \@@_trace:nnx {#1} {#2} { entering~ \token_to_str:N #3 } }
\cs_new_protected:Npn \@@_trace_pop:nnN #1#2#3
   { \@@_trace:nnx {#1} {#2} { leaving~ \token_to_str:N #3 } }
\cs_new_protected:Npn \@@_trace:nnx #1#2#3
  {
    \int_compare:nNnF
      { \int_use:c { g_@@_trace_#1_int } } < {#2}
      { \iow_term:x { Trace:~#3 } }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\g_@@_trace_regex_int}
%   No tracing when that is zero.
%    \begin{macrocode}
\int_new:N \g_@@_trace_regex_int
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_trace_states:n}
%   This function lists the contents of all states of the \textsc{nfa},
%   stored in \tn{toks} from $0$ to \cs{l_@@_max_state_int}
%   (excluded).
%    \begin{macrocode}
\cs_new_protected:Npn \@@_trace_states:n #1
  {
    \int_step_inline:nnn
      \l_@@_min_state_int
      { \l_@@_max_state_int - 1 }
      {
        \@@_trace:nnx { regex } {#1}
          { \iow_char:N \\toks ##1 = { \@@_toks_use:w ##1 } }
      }
  }
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex
% \endinput
%^^A NOT IMPLEMENTED
%^^A    \p{xx}     a character with the xx property
%^^A    \P{xx}     a character without the xx property
%^^A    (?=...)    positive look ahead
%^^A    (?!...)    negative look ahead
%^^A    (?<=...)   positive look behind
%^^A    (?<!...)   negative look behind
%^^A    (?<name>...) or (?'name'...) or (?P<name>...)
%^^A               named capturing group
%^^A    \R         a newline sequence
%^^A    \X         an extended Unicode sequence
%^^A    (?C) or (?Cn)   callout with data n
%^^A    (?R)            recurse whole pattern
%^^A    (?[+-]n) or \g<[+-]n> or (?&name) or (?P>name) or \g<name>
%^^A                    call subpattern
%^^A    (?([+-]n)... or (?(<name>)...
%^^A                    reference condition
%^^A    (?(R)... or (?(Rn)... or (?(R&name)...
%^^A                    recursion condition
%^^A    (?(DEFINE)...   define subpattern for reference
%^^A    (?(assert)...   assertion condition
%^^A    (?(?=..)..|..)  positive/negative look ahead/behind condition
%^^A    (*ACCEPT)       force successful match
%^^A    (*FAIL)         force backtrack; synonym (*F)
%^^A    (*COMMIT)       overall failure, no advance of starting point
%^^A    (*PRUNE)        advance to next starting character
%^^A    (*SKIP)         advance start to current matching position
%^^A    (*THEN)         local failure, backtrack to next alternation
%^^A    (*CR) or (*LF) or (*CRLF) or (*ANYCRLF) or (*ANY)
%^^A                    newline convention
%^^A    (*BSR_ANYCRLF) or (*BSR_UNICODE)
%^^A                    change what \R matches.
%^^A
%^^A    \cx             "control-x", where x is any ASCII character
%^^A    \C              one byte, even in UTF-8 mode (best avoided)
%^^A    +               possessive quantifiers
%^^A    (?>...)         atomic, non-capturing group
%^^A    (?#....)        comment (not nestable)
%^^A    (?Jms-UxX)      options (duplicate names; multiline; single line;
%^^A                      unset what follows; ungreedy; extended;
%^^A                      error on bad escapes)
%^^A    (?i:...|...)    convenient shorthand for (?:(?i)...|...)
%^^A    (*NO_START_OPT) no start-match optimization (PCRE_NO_START_OPTIMIZE)
%^^A    (*UTF8)         set UTF-8 mode (PCRE_UTF8)
%^^A    (*UCP)          set PCRE_UCP (use Unicode properties for \d etc)
%^^A    \n or \gn or \g{[-]n} or \g{name} or (?P=name)
%^^A    or \k<name> or \k'name' or \k{name}
%^^A                    back-references