summaryrefslogtreecommitdiff
path: root/macros/generic/gtl/gtl.dtx
blob: 2f42e6b535176831907b6033a9ebfd2e4782da3f (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
% \iffalse
%% File: gtl.dtx Copyright (C) 2013,2015,2017,2018 Bruno Le Floch
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%%
%% This work has the LPPL maintenance status 'maintained'
%% and the current maintainer is Bruno Le Floch.
%%
%% This work consists of the files gtl.dtx and gtl.ins and
%% derived file gtl.sty.
%% -----------------------------------------------------------------------
%
%<*driver>
%\fi
%\iffalse
\documentclass[full]{l3doc}
\usepackage{gtl}
\begin{document}
  \DocInput{gtl.dtx}
\end{document}
%</driver>
% \fi
%
% \title{The \textsf{gtl} package: \\
%   manipulate unbalanced lists of tokens\thanks{This
%     file has version number 0.5, last revised 2018/12/28.}}
% \author{Bruno Le Floch}
% \date{2018/12/28}
%
% \maketitle
% \tableofcontents
%
% \begin{documentation}
%
% \section{\pkg{gtl} documentation}
%
% The \texttt{expl3} programming language provides various tools to
% manipulate lists of tokens (package \pkg{l3tl}).  However, those token
% lists must have balanced braces, or more precisely balanced
% begin-group and end-group characters.  The \pkg{gtl} package
% manipulates instead lists of tokens which may be unbalanced, with more
% begin-group or more end-group characters.
%
% A technical comment: currently, all begin-group characters are
% assumed to have the character code of ``|{|'' and all end-group
% characters that of~``|}|''.
%
% Please report bugs (or suggestions) on the issue tracker
% (\url{https://github.com/blefloch/latex-gtl/issues}).
%
% ^^A The following code checks the test coverage
% \begingroup\ExplSyntaxOn
% \msg_new:nnn { gtldoc } { discarded } { Discarded~some~material~after~closing~brace~of~{function}~or~TEST~or~TESTEXP~argument:~``#1'' }
% \msg_new:nnn { gtldoc } { bad-test } { TEST~or~TESTEXP~followed~by~unexpected~character~``#1'' }
% \msg_new:nnn { gtldoc } { multi-function } { Some~functions~are~defined~twice }
% \msg_new:nnn { gtldoc } { doc-no-test } { ``#1''~is~documented~but~has~no~test }
% \msg_new:nnn { gtldoc } { test-no-doc } { ``#1''~is~tested~but~not~documented }
% \msg_new:nnn { gtldoc } { junk } { There~is~some~unexpected~junk.~Giving~up~on~this~line. }
% \tl_new:N \l__gtldoc_arg_tl
% \seq_new:N \l__gtldoc_internal_seq
% \cs_generate_variant:Nn \use:nn { no }
% \cs_generate_variant:Nn \tl_if_in:nnTF { no }
% \cs_generate_variant:Nn \tl_if_in:nnT { no }
% \cs_generate_variant:Nn \tl_remove_all:Nn { Nx }
% \cs_generate_variant:Nn \seq_set_filter:NNn { NNx }
% \group_begin:
%   \seq_new:N \l__gtldoc_fn_seq
%   \iow_new:N \g__gtldoc_ior
%   \tl_new:N \l__gtldoc_internal_tl
%   \str_new:N \l__gtldoc_TF_str
%   \seq_new:N \l__gtldoc_arg_seq
%   \seq_new:N \l__gtldoc_arg_TF_seq
%   \cs_new:Npn \__gtldoc_remove_backslash:N #1
%     { \exp_after:wN \token_if_eq_charcode:NNF \c_backslash_str #1 {#1} }
%   \cs_new_protected:Npn \gtldoc_get_fn:n #1
%     {
%       \seq_clear:N \l__gtldoc_fn_seq
%       \clist_map_inline:nn {#1}
%         {
%           \__gtldoc_get_fn:nn { function } {#1} ^^A todo: this is wasteful, going through the file twice!
%           \__gtldoc_get_fn:nn { variable } {#1}
%         }
%     }
%   \cs_new_protected:Npn \__gtldoc_get_fn:nn #1
%     {
%       \exp_args:Noo \__gtldoc_get_fn_aux:nnn
%         { \tl_to_str:n { begin } } { \tl_to_str:n { {#1} } }
%     }
%   \cs_new_eq:NN \__gtldoc_test:w ?
%   \cs_new_eq:NN \__gtldoc_skip:w ?
%   \cs_new_protected:Npn \__gtldoc_get_fn_aux:nnn #1#2#3
%     {
%       \cs_gset_protected:Npn \__gtldoc_test:w ##1 #1 ##2 #2 { }
%       \cs_gset_protected:Npn \__gtldoc_skip:w ##1 #1 ##2 #2
%         {
%           \tl_clear:N \l__gtldoc_arg_tl
%           \str_clear:N \l__gtldoc_TF_str
%           \tl_if_blank:nF {##2}
%             {
%               \msg_error:nn { gtldoc } { junk }
%               \use_none_delimit_by_q_recursion_stop:w
%             }
%           \__gtldoc_aux:N
%         }
%       \ior_open:Nn \g__gtldoc_ior {#3}
%       \ior_str_map_inline:Nn \g__gtldoc_ior
%         {
%           \tl_if_in:noT {##1} { \tl_to_str:n { end { documentation } } } ^^A careful if you remove spaces!
%             { \ior_map_break: }
%           \tl_if_empty:oF { \__gtldoc_test:w ##1 #1 #2 }
%             { \__gtldoc_skip:w ##1 \q_recursion_tail \q_recursion_stop }
%         }
%       \ior_close:N \g__gtldoc_ior
%     }
%   \cs_new_protected:Npn \__gtldoc_aux:N #1
%     {
%       \token_if_eq_meaning:NNT #1 \q_recursion_tail
%         { \__gtldoc_more:Nw \__gtldoc_aux:N }
%       \str_case_x:nnF {#1}
%         {
%           \c_percent_str { \__gtldoc_aux:N } ^^A ignore percent
%           ^ { \__gtldoc_hat:N } ^^A check for "^^A"
%           [ { \__gtldoc_opt:w }
%           \c_left_brace_str { \__gtldoc_arg:w }
%         }
%         { \__gtldoc_varg:N #1 }
%     }
%   \cs_new_protected:Npn \__gtldoc_more:Nw #1 #2 \q_recursion_stop
%     { ^^A grab one more line and restart the loop
%       \ior_str_get:NN \g__gtldoc_ior \l__gtldoc_internal_tl
%       \exp_after:wN #1 \l__gtldoc_internal_tl
%         \q_recursion_tail \q_recursion_stop
%     }
%   \cs_new_protected:Npn \__gtldoc_hat:N #1
%     {
%       \token_if_eq_charcode:NNTF #1 ^
%         { \__gtldoc_hat_hat:N }
%         { \__gtldoc_varg:N ^ #1 }
%     }
%   \cs_new_protected:Npn \__gtldoc_hat_hat:N #1 ^^A in principle we should worry about crazy people doing ^^01 or ^^5b etc
%     {
%       \token_if_eq_charcode:NNTF #1 A
%         { \__gtldoc_more:Nw \__gtldoc_aux:N } ^^A we found a comment
%         { \__gtldoc_varg:N ^ ^ #1 }
%     }
%   \cs_new_protected:Npn \__gtldoc_opt:w #1 \q_recursion_tail
%     {
%       \tl_if_in:nnTF {#1} { ] }
%         { \__gtldoc_opt_aux:w #1 \q_recursion_tail }
%         { \__gtldoc_more:Nw \__gtldoc_opt:w }
%     }
%   \cs_new_protected:Npn \__gtldoc_opt_aux:w #1 ]
%     {
%       \tl_if_in:noTF {#1} { \tl_to_str:n { pTF } }
%         { \str_set:Nn \l__gtldoc_TF_str { pTF } }
%         {
%           \tl_if_in:noT {#1} { \tl_to_str:n { TF } }
%             { \str_set:Nn \l__gtldoc_TF_str { TF } }
%         }
%       \__gtldoc_aux:N
%     }
%   \cs_new_protected:Npn \__gtldoc_arg:w
%     { \exp_after:wN \__gtldoc_varg:N \c_right_brace_str }
%   \cs_new_protected:Npn \__gtldoc_varg:N #1
%     { \exp_args:No \__gtldoc_varg_aux:n { \token_to_str:N #1 } }
%   \cs_new_protected:Npn \__gtldoc_varg_aux:n #1
%     {
%       \cs_gset_protected:Npn \__gtldoc_varg_aux:w ##1 #1 { {##1} }
%       \cs_gset_protected:Npn \__gtldoc_varg_loop:w ##1 \q_recursion_tail
%         {
%           \tl_if_in:noTF {##1} {#1}
%             {
%               \exp_after:wN \__gtldoc_varg_last:nw
%               \__gtldoc_varg_aux:w ##1 \q_recursion_tail
%             }
%             {
%               \tl_put_right:Nx \l__gtldoc_arg_tl { ##1 \iow_char:N \^^M }
%               \__gtldoc_more:Nw \__gtldoc_varg_loop:w
%             }
%         }
%       \__gtldoc_varg_loop:w
%     }
%   \cs_new_eq:NN \__gtldoc_varg_aux:w ?
%   \cs_new_eq:NN \__gtldoc_varg_loop:w ?
%   \cs_new_protected:Npn \__gtldoc_varg_last:nw
%       #1#2 \q_recursion_tail \q_recursion_stop
%     {
%       \tl_if_blank:nF {#2}
%         { \msg_warning:nnn { gtldoc } { discarded } {#2} }
%       \tl_put_right:Nn \l__gtldoc_arg_tl {#1}
%       \tl_remove_all:Nx \l__gtldoc_arg_tl { \iow_char:N \^^M \iow_char:N \% } ^^A from l3doc
%       \tl_remove_all:Nx \l__gtldoc_arg_tl { \tl_to_str:n { ^ ^ A } } ^^A from l3doc
%       \tl_remove_all:Nx \l__gtldoc_arg_tl { \iow_char:N \^^I } ^^A from l3doc
%       \tl_remove_all:Nx \l__gtldoc_arg_tl { \iow_char:N \^^M } ^^A from l3doc
%       ^^A \@@_replace_at_at:N \l_@@_tmpa_tl ^^A from l3doc but not done here
%       \exp_args:NNx \seq_set_from_clist:Nn \l__gtldoc_arg_seq
%         { \tl_to_str:N \l__gtldoc_arg_tl }
%       \str_if_empty:NF \l__gtldoc_TF_str
%         {
%           \seq_clear:N \l__gtldoc_arg_TF_seq
%           \seq_map_inline:Nn \l__gtldoc_arg_seq
%             {
%               \seq_put_right:Nx \l__gtldoc_arg_TF_seq { ##1 TF }
%               \seq_put_right:Nx \l__gtldoc_arg_TF_seq { ##1 T }
%               \seq_put_right:Nx \l__gtldoc_arg_TF_seq { ##1 F }
%               \str_if_eq_x:nnT { \l__gtldoc_TF_str } { pTF }
%                 { \seq_put_right:Nx \l__gtldoc_arg_TF_seq { \__gtldoc_pred:w ##1 } }
%             }
%           \seq_set_map:NNn \l__gtldoc_arg_seq
%             \l__gtldoc_arg_TF_seq { \tl_to_str:n {##1} }
%         }
%       \seq_concat:NNN \l__gtldoc_fn_seq \l__gtldoc_fn_seq \l__gtldoc_arg_seq
%     }
%   \use:no { \cs_new:Npn \__gtldoc_pred:w #1 } { \token_to_str:N : } { #1 _p: }
% \group_end:
% \group_begin:
%   \seq_new:N \l__gtldoc_tested_fn_seq
%   \cs_new_protected:Npn \gtldoc_get_tests:n #1
%     {
%       \seq_clear:N \l__gtldoc_tested_fn_seq
%       \clist_map_function:nN {#1} \__gtldoc_get_test:n
%     }
%   \cs_new_protected:Npn \__gtldoc_get_test:n #1
%     {
%       \ior_open:Nn \g__gtldoc_ior {#1}
%       \ior_str_map_inline:Nn \g__gtldoc_ior
%         { \__gtldoc_test_aux:n {##1} }
%       \ior_close:N \g__gtldoc_ior
%     }
%   \cs_gset_protected:Npn \__gtldoc_tmp:nn #1#2
%     {
%       \cs_new_protected:Npn \__gtldoc_test_skipi:w ##1 #1 { }
%       \cs_new_protected:Npn \__gtldoc_test_skipii:w ##1 #2 { }
%       \cs_new_protected:Npn \__gtldoc_test_aux:n ##1
%         {
%           \tl_if_empty:oF { \__gtldoc_test_skipi:w ##1 #1 }
%             {
%               \tl_if_empty:oTF { \__gtldoc_test_skipii:w ##1 #2 }
%                 { \__gtldoc_test_auxi:w } { \__gtldoc_test_auxii:w }
%                 ##1 \q_recursion_tail \q_recursion_stop 
%             }
%         }
%       \cs_new_protected:Npn \__gtldoc_test_auxi:w ##1 #1
%         { \__gtldoc_test_auxiii:N }
%       \cs_new_protected:Npn \__gtldoc_test_auxii:w ##1 #2
%         { \__gtldoc_test_auxiii:N }
%     }
%   \exp_args:Noo \__gtldoc_tmp:nn
%     { \token_to_str:N \TEST }
%     { \token_to_str:N \TESTEXP }
%   \cs_new_protected:Npn \__gtldoc_test_auxiii:N #1
%     {
%       \token_if_eq_meaning:NNT \q_recursion_tail #1
%         { \__gtldoc_more:Nw \__gtldoc_test_auxiii:N }
%       \exp_after:wN \token_if_eq_charcode:NNTF \c_left_brace_str #1
%         { \__gtldoc_test_arg:w }
%         {
%           \msg_error:nnn { gtldoc } { bad-test } {#1}
%           \use_none_delimit_by_q_recursion_stop:w
%         }
%     }
%   \cs_new_protected:Npn \__gtldoc_test_arg:w
%     {
%       \tl_clear:N \l__gtldoc_arg_tl
%       \exp_args:No \__gtldoc_test_arg_aux:N \c_right_brace_str
%     }
%   \cs_new_protected:Npn \__gtldoc_test_arg_aux:N #1
%     {
%       \cs_gset_protected:Npn \__gtldoc_test_arg_aux:w ##1 #1 { {##1} }
%       \cs_gset_protected:Npn \__gtldoc_test_arg_loop:w ##1 \q_recursion_tail
%         {
%           \tl_if_in:noTF {##1} {#1}
%             {
%               \exp_after:wN \__gtldoc_test_arg_last:nw
%               \__gtldoc_test_arg_aux:w ##1 \q_recursion_tail
%             }
%             {
%               \tl_put_right:Nn \l__gtldoc_arg_tl {##1}
%               \__gtldoc_more:Nw \__gtldoc_test_arg_loop:w
%             }
%         }
%       \__gtldoc_test_arg_loop:w
%     }
%   \cs_new_eq:NN \__gtldoc_test_arg_aux:w ?
%   \cs_new_eq:NN \__gtldoc_test_arg_loop:w ?
%   \cs_new_protected:Npn \__gtldoc_test_arg_last:nw
%       #1#2 \q_recursion_tail \q_recursion_stop
%     {
%       \tl_if_blank:nF {#2}
%         { \msg_warning:nnn { gtldoc } { discarded } {#2} }
%       \tl_put_right:Nn \l__gtldoc_arg_tl {#1}
%       \exp_args:NNx \seq_set_from_clist:Nn \l__gtldoc_arg_seq
%         { \tl_to_str:N \l__gtldoc_arg_tl }
%       \seq_set_filter:NNx \l__gtldoc_arg_seq \l__gtldoc_arg_seq
%         { \exp_not:N \tl_if_head_eq_charcode_p:nN {####1} \c_backslash_str }
%       \seq_concat:NNN \l__gtldoc_tested_fn_seq \l__gtldoc_tested_fn_seq \l__gtldoc_arg_seq
%     }
% \group_end:
% \gtldoc_get_fn:n { gtl.dtx }
% \gtldoc_get_tests:n
%   {
%     ../../testfiles/gtl000.lvt ,
%     ../../testfiles/gtl001.lvt ,
%     ../../testfiles/gtl002.lvt ,
%     ../../testfiles/gtl003.lvt ,
%     ../../testfiles/gtl004.lvt ,
%     ../../testfiles/gtl005.lvt ,
%     ../../testfiles/gtl006.lvt ,
%     ../../testfiles-plain/gtl007.lvt
%   }
% \group_begin:
%   \seq_set_eq:NN \l__gtldoc_internal_seq \l__gtldoc_fn_seq
%   \seq_remove_duplicates:N \l__gtldoc_internal_seq
%   \int_compare:nF
%     { \seq_count:N \l__gtldoc_internal_seq = \seq_count:N \l__gtldoc_fn_seq }
%     {
%       \msg_error:nn { gtldoc } { multi-function }
%       \seq_set_eq:NN \l__gtldoc_fn_seq \l__gtldoc_internal_seq
%     }
%   \seq_remove_duplicates:N \l__gtldoc_tested_fn_seq
%   \seq_map_inline:Nn \l__gtldoc_fn_seq
%     {
%       \seq_if_in:NnTF \l__gtldoc_tested_fn_seq {#1}
%         { \seq_remove_all:Nn \l__gtldoc_tested_fn_seq {#1} }
%         { \msg_error:nnn { gtldoc } { doc-no-test } {#1} }
%     }
%   \seq_map_inline:Nn \l__gtldoc_tested_fn_seq
%     { \msg_error:nnn { gtldoc } { test-no-doc } {#1} }
% \group_end:
% \endgroup
%
% \subsection{Creating and initialising extended token lists}
%
% \begin{function}{\gtl_new:N}
%   \begin{syntax}
%     \cs{gtl_new:N} \meta{gtl~var}
%   \end{syntax}
%   Creates a new \meta{gtl~var} or raises an error if the name is
%   already taken.  The declaration is global.  The \meta{gtl~var} will
%   initially be empty.
% \end{function}
%
% \begin{function}{\gtl_const:Nn, \gtl_const:Nx}
%   \begin{syntax}
%     \cs{gtl_const:Nn} \meta{gtl~var} \Arg{token list}
%   \end{syntax}
%   Creates a new constant \meta{gtl~var} or raises an error if the name
%   is already taken.  The value of the \meta{gtl~var} will be set
%   globally to the balanced \meta{token list}.
% \end{function}
%
% \begin{function}{\gtl_clear:N, \gtl_gclear:N}
%   \begin{syntax}
%     \cs{gtl_clear:N} \meta{gtl~var}
%   \end{syntax}
%   Empties the \meta{gtl~var}, locally or globally.
% \end{function}
%
% \begin{function}{\gtl_clear_new:N, \gtl_gclear_new:N}
%   \begin{syntax}
%     \cs{gtl_clear_new:N} \meta{gtl~var}
%   \end{syntax}
%   Ensures that the \meta{gtl~var} exists globally by applying
%   \cs{gtl_new:N} if necessary, then applies \cs{gtl_(g)clear:N} to
%   leave the \meta{gtl~var} empty.
% \end{function}
%
% \begin{function}{\gtl_set_eq:NN, \gtl_gset_eq:NN}
%   \begin{syntax}
%     \cs{gtl_set_eq:NN} \meta{gtl~var_1} \meta{gtl~var_2}
%   \end{syntax}
%   Sets the content of \meta{gtl~var_1} equal to that of
%   \meta{gtl~var_2}.
% \end{function}
%
% \begin{function}{\gtl_concat:NNN, \gtl_gconcat:NNN}
%   \begin{syntax}
%     \cs{gtl_concat:NNN} \meta{gtl~var_1} \meta{gtl~var_2} \meta{gtl~var_3}
%   \end{syntax}
%   Concatenates the content of \meta{gtl~var_2} and \meta{gtl~var_3}
%   together and saves the result in \meta{gtl~var_1}. The
%   \meta{gtl~var_2} will be placed at the left side of the new extended
%   token list.
% \end{function}
%
% \begin{function}[EXP, pTF]{\gtl_if_exist:N}
%   \begin{syntax}
%     \cs{gtl_if_exist_p:N} \meta{gtl~var}
%     \cs{gtl_if_exist:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether the \meta{gtl~var} is currently defined.  This does
%   not check that the \meta{gtl~var} really is an extended token list
%   variable.
% \end{function}
%
% \subsection{Adding data to token list variables}
%
% \begin{function}{\gtl_set:Nn, \gtl_set:Nx, \gtl_gset:Nn, \gtl_gset:Nx}
%   \begin{syntax}
%     \cs{gtl_set:Nn} \meta{gtl~var} \Arg{token list}
%   \end{syntax}
%   Sets \meta{gtl~var} to contain the balanced \meta{token list},
%   removing any previous content from the variable.
% \end{function}
%
% \begin{function}{\gtl_put_left:Nn, \gtl_gput_left:Nn}
%   \begin{syntax}
%     \cs{gtl_put_left:Nn} \meta{gtl~var} \Arg{token list}
%   \end{syntax}
%   Appends the balanced \meta{token list} to the left side of the
%   current content of \meta{gtl~var}.
% \end{function}
%
% \begin{function}{\gtl_put_right:Nn, \gtl_gput_right:Nn}
%   \begin{syntax}
%     \cs{gtl_put_right:Nn} \meta{gtl~var} \Arg{token list}
%   \end{syntax}
%   Appends the balanced \meta{token list} to the right side of the
%   current content of \meta{gtl~var}.
% \end{function}
%
% \subsection{Extended token list conditionals}
%
% \begin{function}[EXP, pTF]{\gtl_if_blank:N}
%   \begin{syntax}
%     \cs{gtl_if_blank_p:N} \Arg{gtl~var}
%     \cs{gtl_if_blank:NTF} \Arg{gtl~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests if the \meta{gtl~var} consists only of blank spaces.  The test
%   is \texttt{true} if \meta{gtl~var} consists of zero or more explicit
%   space characters (explicit tokens with character code~$32$ and
%   category code~$10$), and is \texttt{false} otherwise.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_empty:N}
%   \begin{syntax}
%     \cs{gtl_if_empty_p:N} \meta{gtl~var}
%     \cs{gtl_if_empty:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests if the \meta{gtl~var} is entirely empty (\emph{i.e.}~contains
%   no tokens at all).
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_eq:NN}
%   \begin{syntax}
%     \cs{gtl_if_eq_p:NN} \Arg{gtl~var_1} \Arg{gtl~var_2}
%     \cs{gtl_if_eq:NNTF} \Arg{gtl~var_1} \Arg{gtl~var_2} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests if \meta{gtl~var_1} and \meta{gtl~var_2} have the same
%   content.  The test is \texttt{true} if the two contain the same list
%   of tokens (identical in both character code and category code).
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_single_token:N}
%   \begin{syntax}
%     \cs{gtl_if_single_token_p:N} \meta{gtl~var}
%     \cs{gtl_if_single_token:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests if the content of the \meta{gtl~var} consists of a single
%   token.  Such a token list has token count $1$ according to
%   \cs{gtl_count_tokens:N}.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_tl:N}
%   \begin{syntax}
%     \cs{gtl_if_tl_p:N} \meta{gtl~var}
%     \cs{gtl_if_tl:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests if the \meta{gtl~var} is balanced.
% \end{function}
%
% \subsection{The first token from an extended token list}
%
% \begin{function}[EXP]{\gtl_head:N}
%   \begin{syntax}
%     \cs{gtl_head:N} \meta{gtl~var}
%   \end{syntax}
%   Leaves in the input stream the first token in the \meta{gtl~var}.
%   If the \meta{gtl~var} is empty, nothing is left in the input stream.
% \end{function}
%
% \begin{function}[EXP]{\gtl_head_do:NN}
%   \begin{syntax}
%     \cs{gtl_head_do:NN} \meta{gtl~var} \meta{cs}
%   \end{syntax}
%   Leaves in the input stream the \meta{control sequence} followed by
%   the first token in \meta{gtl~var}.  If the \meta{gtl~var} is empty,
%   the \meta{cs} is followed by \cs{q_no_value}.
% \end{function}
%
% \begin{function}[EXP, TF]{\gtl_head_do:NN}
%   \begin{syntax}
%     \cs{gtl_head_do:NNTF} \meta{gtl~var} \meta{cs} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   If the \meta{gtl~var} is empty, leaves the \meta{false code} in the
%   input stream.  Otherwise leaves the \meta{control sequence}
%   followed by the first token in \meta{gtl~var} and the \meta{true
%   code}.
% \end{function}
%
% \begin{function}{\gtl_get_left:NN}
%   \begin{syntax}
%     \cs{gtl_get_left:NN} \meta{gtl~var_1} \meta{gtl~var_2}
%   \end{syntax}
%   Stores the first token from \meta{gtl~var_1} in \meta{gtl~var_2} as
%   an single-token extended token list, without removing it from
%   \meta{gtl~var_1}.
% \end{function}
%
% \begin{function}{\gtl_pop_left:N, \gtl_gpop_left:N}
%   \begin{syntax}
%     \cs{gtl_pop_left:N} \meta{gtl~var}
%   \end{syntax}
%   Remove the first token from \meta{gtl~var_1}.
%   If the \meta{gtl~var_1} is empty nothing happens.
% \end{function}
%
% \begin{function}{\gtl_pop_left:NN, \gtl_gpop_left:NN}
%   \begin{syntax}
%     \cs{gtl_pop_left:NN} \meta{gtl~var_1} \meta{gtl~var_2}
%   \end{syntax}
%   Stores the first token from \meta{gtl~var_1} in \meta{gtl~var_2} as
%   an single-token extended token list, and remove it from
%   \meta{gtl~var_1}.  If the \meta{gtl~var_1} is empty it remains so,
%   and \meta{gtl~var_2} is set to contain \cs{q_no_value}.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_head_eq_catcode:NN}
%   \begin{syntax}
%     \cs{gtl_if_head_eq_catcode_p:NN} \Arg{gtl~var} \meta{test token}
%     \cs{gtl_if_head_eq_catcode:NNTF} \Arg{gtl~var} \meta{test token}
%     ~~\Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests if the first token in \meta{gtl~var} has the same category
%   code as the \meta{test token}.  In the case where \meta{gtl~var} is
%   empty, the test will always be \texttt{false}.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_head_eq_charcode:NN}
%   \begin{syntax}
%     \cs{gtl_if_head_eq_charcode_p:NN} \Arg{gtl~var} \meta{test token}
%     \cs{gtl_if_head_eq_charcode:NNTF} \Arg{gtl~var} \meta{test token}
%     ~~\Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests if the first token in \meta{gtl~var} has the same character
%   code as the \meta{test token}.  In the case where \meta{gtl~var} is
%   empty, the test will always be \texttt{false}.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_head_eq_meaning:NN}
%   \begin{syntax}
%     \cs{gtl_if_head_eq_meaning_p:NN} \Arg{gtl~var} \meta{test token}
%     \cs{gtl_if_head_eq_meaning:NNTF} \Arg{gtl~var} \meta{test token}
%     ~~\Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests if the first token in \meta{gtl~var} has the same meaning as
%   the \meta{test token}.  In the case where \meta{gtl~var} is empty,
%   the test will always be \texttt{false}.
% \end{function}
%
% \begin{function}[EXP,pTF]
%   {
%     \gtl_if_head_is_group_begin:N,
%     \gtl_if_head_is_group_end:N,
%     \gtl_if_head_is_N_type:N,
%     \gtl_if_head_is_space:N,
%   }
%   \begin{syntax}
%     \cs{gtl_if_head_is_group_begin_p:N} \Arg{gtl~var}
%     \cs{gtl_if_head_is_group_begin:NTF} \Arg{gtl~var}
%     ~~\Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether the first token in \meta{gtl~var} is an explicit
%   begin-group character, an explicit end-group character, an |N|-type
%   token, or a space.  In the case where \meta{gtl~var} is empty, the
%   test will always be \texttt{false}.
% \end{function}
%
% \subsection{The first few tokens from an extended token list}
%
% \begin{function}[EXP]{\gtl_left_tl:N}
%   \begin{syntax}
%     \cs{gtl_left_tl:N} \meta{gtl~var}
%   \end{syntax}
%   Leaves in the input stream all tokens in \meta{gtl~var} until the
%   first extra begin-group or extra end-group character, within
%   \cs{exp_not:n}.  This is the longest balanced token list starting
%   from the left of \meta{gtl~var}.
% \end{function}
%
% \begin{function}{\gtl_pop_left_tl:N, \gtl_gpop_left_tl:N}
%   \begin{syntax}
%     \cs{gtl_pop_left_tl:N} \meta{gtl~var}
%   \end{syntax}
%   Remove from the \meta{gtl~var} all tokens before the first extra
%   begin-group or extra end-group character.  The tokens that are
%   removed form the longest balanced token list starting from the left
%   of \meta{gtl~var}.
% \end{function}
%
% \begin{function}[EXP]{\gtl_left_item:NF}
%   \begin{syntax}
%     \cs{gtl_left_item:NF} \meta{gtl~var} \Arg{false code}
%   \end{syntax}
%   Leaves in the input stream the first \meta{item} of the
%   \meta{gtl~var}: this is identical to \cs{tl_head:n} applied to the
%   result of \cs{gtl_left_tl:N}.  If there is no such item, the
%   \meta{false code} is left in the input stream.
% \end{function}
%
% \begin{function}[TF]{\gtl_pop_left_item:NN, \gtl_gpop_left_item:NN}
%   \begin{syntax}
%     \cs{gtl_pop_left_item:NNTF} \meta{gtl~var} \meta{tl~var}
%       \Arg{true~code} \Arg{false~code}
%   \end{syntax}
%   Stores the first item of \meta{gtl~var} in \meta{tl~var}, locally,
%   and removes it from \meta{gtl~var}, together with any space before
%   it.  If there is no such item, the \meta{gtl~var} is not affected,
%   and the meta{tl~var} may or may not be affected.
% \end{function}
%
% \begin{function}[EXP]{\gtl_left_text:NF}
%   \begin{syntax}
%     \cs{gtl_left_text:NF} \meta{gtl~var} \Arg{false code}
%   \end{syntax}
%   Starting from the first token in \meta{gtl~var}, this function finds
%   a pattern of the form \meta{tokens_1} \Arg{tokens_2}, where the
%   \meta{tokens_1} contain no begin-group nor end-group characters,
%   then leaves \meta{tokens_1} \Arg{tokens_2} in the input stream,
%   within \cs{exp_not:n}.  If no such pattern exists (this happens if
%   the result of \cs{gtl_left_tl:N} contains no brace group), the
%   \meta{false code} is run instead.
% \end{function}
%
% \begin{function}{\gtl_pop_left_text:N, \gtl_gpop_left_text:N}
%   \begin{syntax}
%     \cs{gtl_pop_left_text:N} \meta{gtl~var}
%   \end{syntax}
%   Starting from the first token in \meta{gtl~var}, this function finds
%   a pattern of the form \meta{tokens_1} \Arg{tokens_2}, where the
%   \meta{tokens_1} contain no begin-group nor end-group characters,
%   then removes \meta{tokens_1} \Arg{tokens_2} from \meta{gtl~var}.  If
%   no such pattern exists (this happens if the result of
%   \cs{gtl_left_tl:N} contains no brace group), the \meta{gtl~var} is
%   not modified instead.
% \end{function}
%
% \subsection{Working with the contents of extended token lists}
%
% \begin{function}[EXP]{\gtl_count_tokens:N}
%   \begin{syntax}
%     \cs{gtl_count_tokens:N} \meta{gtl~var}
%   \end{syntax}
%   Counts the number of tokens in the \meta{gtl~var} and leaves this
%   information in the input stream.
% \end{function}
%
% \begin{function}[EXP]{\gtl_extra_begin:N, \gtl_extra_end:N}
%   \begin{syntax}
%     \cs{gtl_extra_begin:N} \meta{gtl~var}
%   \end{syntax}
%   Counts the number of explicit extra begin-group (or end-group)
%   characters in the \meta{gtl~var} and leaves this information in the
%   input stream.
% \end{function}
%
% \begin{function}{\gtl_show:N, \gtl_log:N}
%   \begin{syntax}
%     \cs{gtl_show:N} \meta{gtl~var}
%   \end{syntax}
%   Displays the content of the \meta{gtl~var} on the terminal or in
%   the log file.
% \end{function}
%
% \begin{function}[EXP, added = 2018-04-04]{\gtl_to_str:N, \gtl_to_str:n}
%   \begin{syntax}
%     \cs{gtl_to_str:N} \meta{gtl~var}
%   \end{syntax}
%   Converts the content of the \meta{gtl~var} into a series of
%   characters with category code $12$ (other) with the exception of
%   spaces, which retain category code $10$ (space).  This string is
%   then left in the input stream.
% \end{function}
%
% \subsection{Constant extended token lists}
%
% \begin{variable}{\c_empty_gtl}
%   Constant that is always empty.
% \end{variable}
%
% \begin{variable}{\c_group_begin_gtl}
%   An explicit begin-group character contained in an extended token
%   list.
% \end{variable}
%
% \begin{variable}{\c_group_end_gtl}
%   An explicit end-group character contained in an extended token list.
% \end{variable}
%
% \subsection{Future perhaps}
%
% \begin{itemize}
% \item Test if a token appears in an extended token list.
% \item Test if an extended token list appears in another.
% \item Remove an extended token list from another, once or every time
%   it appears.
% \item Replace an extended token list by another in a third: once, or
%   every time it appears.
% \item Case statement.
% \item Mapping?
% \item Inserting an extended token list into the input stream, with all
%   its glorious unbalanced braces.
% \item Convert in various ways to a token list.
% \item Reverse the order of tokens.
% \item Extract a token given its position.
% \item Extract a range of tokens given their position.
% \item Trim spaces.
% \item Crazy idea below.
% \end{itemize}
%
% We could add (with lots of work) the expandable function
% \begin{quote}\ttfamily\obeylines\obeyspaces
% \cs{gtl_concat:nF}
%   \{
%     \Arg{tl_1} \Arg{start_1} \Arg{stop_1}
%     \Arg{tl_2} \Arg{start_2} \Arg{stop_2}
%     \ldots{}
%     \Arg{tl_n} \Arg{start_n} \Arg{stop_n}
%   \}
%   \Arg{false code}
% \end{quote}
% For each triplet, this function builds the sub-token list of \meta{tl_i}
% corresponding to the tokens ranging from position \meta{start_i} to
% position \meta{stop_i} of \meta{tl_i}.  The results obtained for each triplet
% are then concatenated.  If nothing bad happens (see below), the
% concatenation is left in the input stream, and the \meta{false code} is
% removed.  Two cases can lead to running the \meta{false code} (and dropping
% the first argument altogether).  The first case is when the number of
% brace groups in \cs{gtl_concat:nF} is not a multiple of 3.  The second
% case is when the concatenation gives rise to an unbalanced token list:
% then the result is not a valid token list.  Note that each part is
% allowed to be unbalanced: only the full result must be balanced.
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{gtl} implementation}
%
% Some support packages are loaded first, then we declare the package's
% name, date, version, and purpose.
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=gtl>
%    \end{macrocode}
%
% Load \pkg{expl3}, either through \cs{RequirePackage} or through
% inputting the generic loader, depending on the format in use.
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
  \input expl3-generic.tex
\else
  \RequirePackage{expl3}[2017/11/14]
\fi
\ExplSyntaxOn
\cs_if_exist:NTF \ProvidesExplPackage
  {
    \cs_new_eq:NN \@@_end_package_hook: \prg_do_nothing:
    \ExplSyntaxOff
    \ProvidesExplPackage
  }
  {
    \cs_new_eq:NN \@@_end_package_hook: \ExplSyntaxOff
    \group_begin:
    \ExplSyntaxOff
    \cs_set_protected:Npn \@@_tmp:w #1#2#3#4
      {
        \group_end:
        \tl_gset:cx { ver @ #1 . sty } { #2 ~ v#3 ~ #4 }
        \cs_if_exist_use:NF \wlog { \iow_log:x }
          { Package: ~ #1 ~ #2 ~ v#3 ~ #4 }
      }
    \@@_tmp:w
  }
      {gtl} {2018/12/28} {0.5} {Manipulate unbalanced lists of tokens}
%    \end{macrocode}
%
% \subsection{Helpers}
%
%    \begin{macrocode}
\cs_generate_variant:Nn \use:nn { no }
%    \end{macrocode}
%
% \begin{macro}[EXP]{\@@_exp_not_n:N}
%   Used in one case where we need to prevent expansion of a token
%   within an |x|-expanding definition.  Using \cs{exp_not:N} there
%   would fail when the argument is a macro parameter character.
%    \begin{macrocode}
\cs_new:Npn \@@_exp_not_n:N #1 { \exp_not:n {#1} }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_brace:nn, \@@_brace_swap:nn}
%   Those functions are used to add some tokens, |#1|, to an item |#2|
%   in an extended token list: \cs{@@_brace:nn} adds tokens on the left,
%   while \cs{@@_brace_swap:nn} adds them on the right.
%    \begin{macrocode}
\cs_new:Npn \@@_brace:nn #1#2 { { #1 #2 } }
\cs_new:Npn \@@_brace_swap:nn #1#2 { { #2 #1 } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_strip_nil_mark:w,}
% \begin{macro}[EXP]{\@@_strip_nil_mark_aux:w}
%   Removes the following \cs{q_nil} \cs{q_mark} without losing any
%   braces, and places the result into \cs{exp_not:n}.
%    \begin{macrocode}
\cs_new:Npn \@@_strip_nil_mark:w
  { \@@_strip_nil_mark_aux:w \prg_do_nothing: }
\cs_new:Npn \@@_strip_nil_mark_aux:w #1 \q_nil \q_mark
  { \exp_not:o {#1} }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Structure of extended token lists}
%
% Token lists must have balanced braces (or rather, begin-group and
% end-group characters).  Extended token lists lift this requirement,
% and can represent arbitrary lists of tokens.  A list of tokens can
% fail to be balanced in two ways: one may encounter too many end-group
% characters near the beginning of the list, or too many begin-group
% characters near the end of the list.  In fact, a list of tokens always
% has the form
% \begin{quote}\ttfamily
%   \meta{b_1} \} \ldots{} \meta{b_n} \}
%   \meta{c}
%   \{ \meta{e_1} \ldots{} \{ \meta{e_p}
% \end{quote}
% where the \meta{b_i}, \meta{c}, and \meta{e_i} are all balanced token
% lists.  This can be seen by listing the tokens, and keeping track of a
% counter, which starts at~$0$, and is incremented at each begin-group
% character, and decremented at each end-group character: then the
% \meta{b_i} are delimited by positions where the counter reaches a new
% minimum, whereas the \meta{e_i} are delimited by positions where the
% counter last takes a given negative value.  Such a token list is
% stored as
% \begin{quote}\ttfamily
%   \cs{s_@@}
%   \{ \Arg{b_1} \ldots{} \Arg{b_n} \}
%   \Arg{c}
%   \{ \Arg{e_p} \ldots{} \Arg{e_1} \}
% \end{quote}
% Note that the \meta{e_i} are in a reversed order, as this makes the
% ends of extended token lists more accessible.  Balanced token lists
% have $n = p = 0$: the first and third parts are empty, while the
% second contains the tokens.
%
% In the following code comments, the balanced token lists \meta{b_i}
% are called ``leading chunks'', \meta{c} is called ``middle chunk'',
% and \meta{e_i} are called ``trailing chunks''.  It is important to
% note that a balanced sub-list of a gtl must be entirely contained in
% one of the chunk.
%
% \begin{variable}{\s_@@}
%   This marker appears at the start of extended token lists.
%    \begin{macrocode}
\cs_new_eq:NN \s_@@ \scan_stop:
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}
%   {
%     \gtl_set:Nn, \gtl_gset:Nn, \gtl_const:Nn,
%     \gtl_set:Nx, \gtl_gset:Nx, \gtl_const:Nx
%   }
%   Storing a balanced token list into an extended token list variable
%   simply means adding \cs{s_@@} and two empty brace
%   groups: there are no leading nor trailing chunks.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_set:Nn   { \@@_set:NNn \tl_set:Nn   }
\cs_new_protected:Npn \gtl_gset:Nn  { \@@_set:NNn \tl_gset:Nn  }
\cs_new_protected:Npn \gtl_const:Nn { \@@_set:NNn \tl_const:Nn }
\cs_new_protected:Npn \gtl_set:Nx   { \@@_set:NNn \tl_set:Nx   }
\cs_new_protected:Npn \gtl_gset:Nx  { \@@_set:NNn \tl_gset:Nx  }
\cs_new_protected:Npn \gtl_const:Nx { \@@_set:NNn \tl_const:Nx }
\cs_new_protected:Npn \@@_set:NNn   #1#2#3
  { #1 #2 { \s_@@ { } {#3} { } } }
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\c_empty_gtl}
%   An empty extended token list, obtained thanks to the
%   \cs{gtl_const:Nn} function just defined.
%    \begin{macrocode}
\gtl_const:Nn \c_empty_gtl { }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_group_begin_gtl, \c_group_end_gtl}
%   Extended token lists with exactly one begin-group/end-group
%   character are built by including a single (empty) leading or
%   trailing chunk.
%    \begin{macrocode}
\tl_const:Nn \c_group_end_gtl   { \s_@@ { { } } { } {     } }
\tl_const:Nn \c_group_begin_gtl { \s_@@ {     } { } { { } } }
%    \end{macrocode}
% \end{variable}
%
% \subsection{Creating extended token list variables}
%
% \begin{macro}{\gtl_new:N}
%   A new extended token list is created empty.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_new:N #1
  { \cs_new_eq:NN #1 \c_empty_gtl }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gtl_set_eq:NN, \gtl_gset_eq:NN}
%   All the data about an extended token list is stored as a single
%   token list, so copying is easy.
%    \begin{macrocode}
\cs_new_eq:NN \gtl_set_eq:NN  \tl_set_eq:NN
\cs_new_eq:NN \gtl_gset_eq:NN \tl_gset_eq:NN
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gtl_clear:N, \gtl_gclear:N}
%   Clearing an extended token list by setting it to the empty one.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_clear:N  #1
  { \gtl_set_eq:NN  #1 \c_empty_gtl }
\cs_new_protected:Npn \gtl_gclear:N #1
  { \gtl_gset_eq:NN #1 \c_empty_gtl }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gtl_clear_new:N, \gtl_gclear_new:N}
%   If the variable exists, clear it.  Otherwise declare it.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_clear_new:N #1
  { \gtl_if_exist:NTF #1 { \gtl_clear:N #1 } { \gtl_new:N #1 } }
\cs_new_protected:Npn \gtl_gclear_new:N #1
  { \gtl_if_exist:NTF #1 { \gtl_gclear:N #1 } { \gtl_new:N #1 } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP, pTF]{\gtl_if_exist:N}
%   Again a copy of token list functions.
%    \begin{macrocode}
\prg_new_eq_conditional:NNn \gtl_if_exist:N \tl_if_exist:N
  { p , T , F , TF }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Adding data to extended token list variables}
%
% \begin{macro}{\gtl_put_left:Nn, \gtl_gput_left:Nn}
% \begin{macro}[rEXP]{\@@_put_left:wn}
%   If there is no leading chunk in the gtl variable, then add the new
%   material to the middle chunk.  Otherwise add it to the first
%   leading chunk, namely the first brace group in the first argument
%   of \cs{@@_put_left:wn}.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_put_left:Nn #1#2
  { \tl_set:Nx #1 { \exp_after:wN \@@_put_left:wn #1 {#2} } }
\cs_new_protected:Npn \gtl_gput_left:Nn #1#2
  { \tl_gset:Nx #1 { \exp_after:wN \@@_put_left:wn #1 {#2} } }
\cs_new:Npn \@@_put_left:wn \s_@@ #1#2#3 #4
  {
    \tl_if_empty:nTF {#1}
      { \exp_not:n { \s_@@ { } { #4 #2 } {#3} } }
      {
        \s_@@
        { \exp_not:o { \@@_brace:nn {#4} #1 } }
        { \exp_not:n {#2} }
        { \exp_not:n {#3} }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\gtl_put_right:Nn, \gtl_gput_right:Nn}
% \begin{macro}[rEXP]{\@@_put_right:wn}
%   Symmetric of \cs{gtl_put_left:Nn}.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_put_right:Nn #1#2
  { \tl_set:Nx #1 { \exp_after:wN \@@_put_right:wn #1 {#2} } }
\cs_new_protected:Npn \gtl_gput_right:Nn #1#2
  { \tl_gset:Nx #1 { \exp_after:wN \@@_put_right:wn #1 {#2} } }
\cs_new:Npn \@@_put_right:wn \s_@@ #1#2#3 #4
  {
    \tl_if_empty:nTF {#3}
      { \exp_not:n { \s_@@ {#1} { #2 #4 } { } } }
      {
        \s_@@
        { \exp_not:n {#1} }
        { \exp_not:n {#2} }
        { \exp_not:o { \@@_brace_swap:nn {#4} #3 } }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\gtl_concat:NNN, \gtl_gconcat:NNN}
% \begin{macro}[rEXP]
%   {
%     \@@_concat:ww,
%     \@@_concat_aux:nnnnnn,
%     \@@_concat_auxi:nnnnnn,
%     \@@_concat_auxii:nnnnnn,
%     \@@_concat_auxiii:w,
%     \@@_concat_auxiv:nnnn,
%     \@@_concat_auxv:wnwnn,
%     \@@_concat_auxvi:nnwnwnn
%   }
%   Concatenating two lists of tokens of the form
%   \begin{quote}\ttfamily
%     \cs{s_@@}
%     \{ \Arg{b_1} \ldots{} \Arg{b_n} \}
%     \Arg{c}
%     \{ \Arg{e_p} \ldots{} \Arg{e_1} \}
%   \end{quote}
%   is not an easy task.  The \meta{e} parts of the first join with the
%   \meta{b} parts of the second to make balanced pairs, and the
%   follow-up depends on whether there were more \meta{e} parts or more
%   \meta{b} parts.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_concat:NNN #1#2#3
  { \tl_set:Nx  #1 { \exp_last_two_unbraced:Noo \@@_concat:ww #2 #3 } }
\cs_new_protected:Npn \gtl_gconcat:NNN #1#2#3
  { \tl_gset:Nx #1 { \exp_last_two_unbraced:Noo \@@_concat:ww #2 #3 } }
\cs_new:Npn \@@_concat:ww \s_@@ #1#2#3 \s_@@ #4#5#6
  {
    \tl_if_blank:nTF {#3}
      {
        \tl_if_blank:nTF {#4}
          { \@@_concat_aux:nnnnnn }
          { \@@_concat_auxi:nnnnnn }
      }
      {
        \tl_if_blank:nTF {#4}
          { \@@_concat_auxii:nnnnnn }
          { \@@_concat_auxiv:nnnn }
      }
      {#1} {#2} {#3} {#4} {#5} {#6}
  }
\cs_new:Npn \@@_concat_aux:nnnnnn #1#2#3#4#5#6
  { \exp_not:n { \s_@@ {#1} { #2 #5 } {#6} } }
\cs_new:Npn \@@_concat_auxi:nnnnnn #1#2#3#4#5#6
  {
    \s_@@
    {
      \exp_not:n {#1}
      \exp_not:f
        { \@@_concat_auxiii:w \@@_brace:nn {#2} #4 ~ \q_stop }
    }
    { \exp_not:n {#5} }
    { \exp_not:n {#6} }
  }
\cs_new:Npn \@@_concat_auxii:nnnnnn #1#2#3#4#5#6
  {
    \s_@@
    { \exp_not:n {#1} }
    { \exp_not:n {#2} }
    {
      \exp_not:n {#6}
      \exp_not:f
        { \@@_concat_auxiii:w \@@_brace_swap:nn {#5} #3 ~ \q_stop }
    }
  }
\cs_new:Npn \@@_concat_auxiii:w #1 ~ #2 \q_stop {#1}
\cs_new:Npn \@@_concat_auxiv:nnnn #1#2#3#4
  {
    \tl_if_single:nTF {#3}
      { \@@_concat_auxv:wnwnn }
      { \@@_concat_auxvi:nnwnwnn }
    #3 ~ \q_mark #4 ~ \q_mark {#1} {#2}
  }
\cs_new:Npn \@@_concat_auxv:wnwnn
    #1#2 \q_mark #3#4 \q_mark #5#6
  {
    \@@_concat:ww
      \s_@@ {#5} { #6 { #1 #3 } } { }
      \s_@@ {#4}
  }
\cs_new:Npn \@@_concat_auxvi:nnwnwnn
    #1#2#3 \q_mark #4#5 \q_mark #6#7
  {
    \@@_concat:ww
      \s_@@ {#6} {#7} { { #2 { #1 #4 } } #3 }
      \s_@@ {#5}
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Showing extended token lists}
%
% \begin{macro}[EXP]{\gtl_to_str:N, \gtl_to_str:n}
% \begin{macro}[EXP]
%   {
%     \@@_to_str:w,
%     \@@_to_str_loopi:nnw,
%     \@@_to_str_testi:nnw,
%     \@@_to_str_endi:nnn,
%     \@@_to_str_loopii:nnw,
%     \@@_to_str_endii:nnw
%   }
%    \begin{macrocode}
\cs_new:Npn \gtl_to_str:N #1 { \exp_after:wN \@@_to_str:w #1 }
\cs_new:Npn \gtl_to_str:n #1 { \@@_to_str:w #1 }
\cs_new:Npn \@@_to_str:w \s_@@ #1#2#3
  { \@@_to_str_loopi:nnw { } #1 \q_nil \q_mark {#2} {#3} }
\cs_new:Npx \@@_to_str_loopi:nnw #1#2
  {
    \exp_not:N \quark_if_nil:nTF {#2}
      { \exp_not:N \@@_to_str_testi:nnw {#1} {#2} }
      { \exp_not:N \@@_to_str_loopi:nnw { #1 #2 \iow_char:N \} } }
  }
\cs_new:Npx \@@_to_str_testi:nnw #1#2#3 \q_mark
  {
    \exp_not:N \tl_if_empty:nTF {#3}
      { \exp_not:N \@@_to_str_endi:nnn {#1} }
      {
        \exp_not:N \@@_to_str_loopi:nnw
          { #1 #2 \iow_char:N \} } #3 \exp_not:N \q_mark
      }
  }
\cs_new:Npn \@@_to_str_endi:nnn #1#2#3
  { \@@_to_str_loopii:nnw #3 { #1 #2 } \q_nil \q_stop }
\cs_new:Npx \@@_to_str_loopii:nnw #1#2
  {
    \exp_not:N \quark_if_nil:nTF {#2}
      { \exp_not:N \@@_to_str_testii:nnw {#1} {#2} }
      { \exp_not:N \@@_to_str_loopii:nnw { #2 \iow_char:N \{ #1 } }
  }
\cs_new:Npx \@@_to_str_testii:nnw #1#2#3 \q_stop
  {
    \exp_not:N \tl_if_empty:nTF {#3}
      { \exp_not:N \tl_to_str:n {#1} }
      {
        \exp_not:N \@@_to_str_loopii:nnw
          { #2 \iow_char:N \{ #1 } #3 \exp_not:N \q_stop
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\gtl_show:N, \gtl_log:N}
% \begin{macro}{\@@_show:NNN}
%   Display the variable name, then its string representation.  Before
%   that, test that the variable indeed exists, and if appropriate
%   throw an error message by sending the undefined variable to
%   \cs{tl_show:N} or \cs{tl_log:N}.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_show:N
  { \@@_show:NNN \tl_show:n \tl_show:N }
\cs_new_protected:Npn \gtl_log:N
  { \@@_show:NNN \tl_log:n \tl_log:N }
\cs_new_protected:Npn \@@_show:NNN #1#2#3
  {
    \gtl_if_exist:NTF #3
      { \exp_args:Nx #1 { \token_to_str:N #3 = \gtl_to_str:N #3 } }
      { #2 #3 }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Extended token list conditionals}
%
% \begin{macro}[EXP, pTF]{\gtl_if_eq:NN}
%   Two extended token lists are equal if the underlying token lists
%   are the same.
%    \begin{macrocode}
\prg_new_eq_conditional:NNn \gtl_if_eq:NN \tl_if_eq:NN
  { p , T , F , TF }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP, pTF]{\gtl_if_empty:N}
%   An extended token list is empty if it is equal to the empty gtl.
%    \begin{macrocode}
\prg_new_conditional:Npnn \gtl_if_empty:N #1 { p , T , F , TF }
  {
    \tl_if_eq:NNTF #1 \c_empty_gtl
      { \prg_return_true: } { \prg_return_false: }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP, pTF]{\gtl_if_tl:N}
% \begin{macro}[EXP]{\@@_if_tl_return:w}
%   A gtl is balanced if it has neither leading nor trailing chunk.
%    \begin{macrocode}
\prg_new_conditional:Npnn \gtl_if_tl:N #1 { p , T , F , TF }
  { \exp_after:wN \@@_if_tl_return:w #1 }
\cs_new:Npn \@@_if_tl_return:w \s_@@ #1#2#3
  {
    \tl_if_empty:nTF { #1 #3 }
      { \prg_return_true: } { \prg_return_false: }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP, pTF]{\gtl_if_single_token:N}
% \begin{macro}[EXP]{\@@_if_single_token_return:w}
%   If there are neither leading nor trailing chunks then the gtl is a
%   single token if and only if the middle chunk is a single token.
%   Otherwise the gtl is a single token only if it is exactly a
%   begin-group or an end-group token.
%    \begin{macrocode}
\prg_new_conditional:Npnn \gtl_if_single_token:N #1 { p , T , F , TF }
  { \exp_after:wN \@@_if_single_token_return:w #1 #1 }
\cs_new:Npn \@@_if_single_token_return:w \s_@@ #1#2#3 #4
  {
    \tl_if_empty:nTF { #1 #3 }
      {
        \tl_if_single_token:nTF {#2}
          { \prg_return_true: }
          { \prg_return_false: }
      }
      {
        \gtl_if_eq:NNTF #4 \c_group_begin_gtl
          { \prg_return_true: }
          {
            \gtl_if_eq:NNTF #4 \c_group_end_gtl
              { \prg_return_true: }
              { \prg_return_false: }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP,pTF]{\gtl_if_blank:N}
% \begin{macro}[EXP]{\@@_if_blank_return:w}
%   A gtl is blank if its middle chunk is blank and it has no leading
%   nor trailing chunk (those would lead to |#1| or |#3| containing
%   brace groups).
%    \begin{macrocode}
\prg_new_conditional:Npnn \gtl_if_blank:N #1 { p , T , F , TF }
  { \exp_after:wN \@@_if_blank_return:w #1 }
\cs_new:Npn \@@_if_blank_return:w \s_@@ #1#2#3
  {
    \tl_if_blank:nTF { #1 #2 #3 }
      { \prg_return_true: }
      { \prg_return_false: }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP,pTF]
%   {
%     \gtl_if_head_is_group_begin:N,
%     \gtl_if_head_is_group_end:N,
%     \gtl_if_head_is_space:N,
%     \gtl_if_head_is_N_type:N,
%   }
%   Based on a five-way test \cs{@@_head:wnnnnn} defined later.  The
%   five cases are: the gtl is empty, it starts with a begin-group,
%   with an end-group, with a space, or with an N-type token.  In the
%   last case, the token is left in the input stream after the brace
%   group, hence the need for \cs{use_none:n} here.
%    \begin{macrocode}
\prg_new_conditional:Npnn \gtl_if_head_is_group_begin:N #1
  { p , T , F , TF }
  {
    \exp_after:wN \@@_head:wnnnnn #1
      { \prg_return_false: }
      { \prg_return_true: }
      { \prg_return_false: }
      { \prg_return_false: }
      { \prg_return_false: \use_none:n }
  }
\prg_new_conditional:Npnn \gtl_if_head_is_group_end:N #1
  { p , T , F , TF }
  {
    \exp_after:wN \@@_head:wnnnnn #1
      { \prg_return_false: }
      { \prg_return_false: }
      { \prg_return_true: }
      { \prg_return_false: }
      { \prg_return_false: \use_none:n }
  }
\prg_new_conditional:Npnn \gtl_if_head_is_space:N #1
  { p , T , F , TF }
  {
    \exp_after:wN \@@_head:wnnnnn #1
      { \prg_return_false: }
      { \prg_return_false: }
      { \prg_return_false: }
      { \prg_return_true: }
      { \prg_return_false: \use_none:n }
  }
\prg_new_conditional:Npnn \gtl_if_head_is_N_type:N #1
  { p , T , F , TF }
  {
    \exp_after:wN \@@_head:wnnnnn #1
      { \prg_return_false: }
      { \prg_return_false: }
      { \prg_return_false: }
      { \prg_return_false: }
      { \prg_return_true: \use_none:n }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP, pTF]{\gtl_if_head_eq_catcode:NN}
% \begin{macro}[EXP, pTF]{\gtl_if_head_eq_charcode:NN}
% \begin{macro}[EXP]{\@@_if_head_eq_code_return:NNN}
%   In the empty case, |?| can match with |#2|, but then
%   \cs{use_none:nn} gets rid of \cs{prg_return_true:} and \cs{else:},
%   to correctly leave \cs{prg_return_false:}.  We could not simplify
%   this by placing the \cs{exp_not:N} |#2| after the construction
%   involving~|#1|, because |#2|~must be taken into the \TeX{} primitive
%   test, in case |#2|~itself is a primitive \TeX{} conditional, which
%   would mess up conditional nesting.
%    \begin{macrocode}
\prg_new_conditional:Npnn \gtl_if_head_eq_catcode:NN #1#2
  { p , T , F , TF }
  { \@@_if_head_eq_code_return:NNN \if_catcode:w #1#2 }
\prg_new_conditional:Npnn \gtl_if_head_eq_charcode:NN #1#2
  { p , T , F , TF }
  { \@@_if_head_eq_code_return:NNN \if_charcode:w #1#2 }
\cs_new:Npn \@@_if_head_eq_code_return:NNN #1#2#3
  {
    #1
        \exp_not:N #3
        \exp_after:wN \@@_head:wnnnnn #2
          { ? \use_none:nn }
          { \c_group_begin_token }
          { \c_group_end_token }
          { \c_space_token }
          { \exp_not:N }
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP, pTF]{\gtl_if_head_eq_meaning:NN}
% \begin{macro}[EXP]{\@@_if_head_eq_meaning_return:NN}
%    \begin{macrocode}
\prg_new_conditional:Npnn \gtl_if_head_eq_meaning:NN #1#2
  { p , T , F , TF }
  { \@@_if_head_eq_meaning_return:NN #1#2 }
\cs_new:Npn \@@_if_head_eq_meaning_return:NN #1#2
  {
    \exp_after:wN \@@_head:wnnnnn #1
      { \if_false: }
      { \if_meaning:w #2 \c_group_begin_token }
      { \if_meaning:w #2 \c_group_end_token }
      { \if_meaning:w #2 \c_space_token }
      { \if_meaning:w #2 }
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{First token of an extended token list}
%
% \begin{macro}[EXP]{\@@_head:wnnnnn}
% \begin{macro}[EXP]{\@@_head_aux:nwnnnn}
% \begin{macro}[EXP]{\@@_head_auxii:N, \@@_head_auxiii:Nnn}
%   This function performs |#4|~if the \texttt{gtl} is empty, |#5|~if it
%   starts with a begin-group character, |#6|~if it starts with an
%   end-group character, |#7|~if it starts with a space, and in other
%   cases (when the first token is |N|-type), it performs |#8|~followed
%   by the first token.
%    \begin{macrocode}
\cs_new:Npn \@@_head:wnnnnn \s_@@ #1#2#3 #4#5#6#7#8
  {
    \tl_if_empty:nTF {#1}
      {
        \tl_if_empty:nTF {#2}
          { \tl_if_empty:nTF {#3} {#4} {#5} }
          { \@@_head_aux:nwnnnn {#2} \q_stop {#5} {#6} {#7} {#8} }
      }
      { \@@_head_aux:nwnnnn #1 \q_stop {#5} {#6} {#7} {#8} }
  }
\cs_new:Npn \@@_head_aux:nwnnnn #1#2 \q_stop #3#4#5#6
  {
    \tl_if_head_is_group:nTF {#1} {#3}
      {
        \tl_if_empty:nTF {#1} {#4}
          {
            \tl_if_head_is_space:nTF {#1} {#5}
              { \if_false: { \fi: \@@_head_auxii:N #1 } {#6} }
          }
      }
  }
\cs_new:Npn \@@_head_auxii:N #1
  {
    \exp_after:wN \@@_head_auxiii:Nnn
    \exp_after:wN #1
    \exp_after:wN { \if_false: } \fi:
  }
\cs_new:Npn \@@_head_auxiii:Nnn #1#2#3 { #3 #1 }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\gtl_head:N}
%   If |#1| is empty, do nothing.  If it starts with a begin-group
%   character or an end-group character leave the appropriate brace
%   (thanks to \cs{if_false:} tricks).  If it starts with a space, leave
%   that, and finally if it starts with a normal token, leave it, within
%   \cs{exp_not:n}.
%    \begin{macrocode}
\cs_new:Npn \gtl_head:N #1
  {
    \exp_after:wN \@@_head:wnnnnn #1
      { }
      { \exp_after:wN { \if_false: } \fi: }
      { \if_false: { \fi: } }
      { ~ }
      { \@@_exp_not_n:N }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\gtl_head_do:NN}
%   Similar to \cs{gtl_head:N}, but inserting |#2| before the resulting token.
%    \begin{macrocode}
\cs_new:Npn \gtl_head_do:NN #1#2
  {
    \exp_after:wN \@@_head:wnnnnn #1
      { #2 \q_no_value }
      { \exp_after:wN #2 \exp_after:wN { \if_false: } \fi: }
      { \if_false: { \fi: #2 } }
      { #2 ~ }
      { #2 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP, TF]{\gtl_head_do:NN}
%   Test for emptyness then use \cs{gtl_head_do:NN}, placing the
%   \meta{true code} or \meta{false code} as appropriate.
%    \begin{macrocode}
\cs_new:Npn \gtl_head_do:NNT #1#2#3
  {
    \gtl_if_empty:NTF #1
      { }
      { \gtl_head_do:NN #1 #2 #3 }
  }
\cs_new:Npn \gtl_head_do:NNF #1#2#3
  {
    \gtl_if_empty:NTF #1
      {#3}
      { \gtl_head_do:NN #1 #2 }
  }
\cs_new:Npn \gtl_head_do:NNTF #1#2#3#4
  {
    \gtl_if_empty:NTF #1
      {#4}
      { \gtl_head_do:NN #1 #2 #3 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gtl_get_left:NN}
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_get_left:NN #1#2
  {
    \exp_after:wN \@@_head:wnnnnn #1
      { \gtl_set:Nn #2 { \q_no_value } }
      { \gtl_set_eq:NN #2 \c_group_begin_gtl }
      { \gtl_set_eq:NN #2 \c_group_end_gtl }
      { \gtl_set:Nn #2 { ~ } }
      { \gtl_set:Nn #2 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gtl_pop_left:N, \gtl_gpop_left:N}
% \begin{macro}[rEXP]
%   {
%     \@@_pop_left:w,
%     \@@_pop_left_auxi:n,
%     \@@_pop_left_auxii:nnnw,
%     \@@_pop_left_auxiii:nnnw,
%     \@@_pop_left_auxiv:nn,
%     \@@_pop_left_auxv:nnn,
%     \@@_pop_left_auxvi:n
%   }
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_pop_left:N #1
  {
    \gtl_if_empty:NF #1
      { \tl_set:Nx #1 { \exp_after:wN \@@_pop_left:w #1 } }
  }
\cs_new_protected:Npn \gtl_gpop_left:N #1
  {
    \gtl_if_empty:NF #1
      { \tl_gset:Nx #1 { \exp_after:wN \@@_pop_left:w #1 } }
  }
\cs_new:Npn \@@_pop_left:w \s_@@ #1#2#3
  {
    \tl_if_empty:nTF {#1}
      {
        \tl_if_empty:nTF {#2}
          { \@@_pop_left_auxi:n {#3} }
          { \@@_pop_left_auxiv:nn {#2} {#3} }
      }
      { \@@_pop_left_auxv:nnn {#1} {#2} {#3} }
  }
\cs_new:Npn \@@_pop_left_auxi:n #1
  {
    \s_@@
    { }
    \@@_pop_left_auxii:nnnw { } { } #1 \q_nil \q_stop
  }
\cs_new:Npn \@@_pop_left_auxii:nnnw #1#2#3
  {
    \quark_if_nil:nTF {#3}
      { \@@_pop_left_auxiii:nnnw {#1} {#2} {#3} }
      { \@@_pop_left_auxii:nnnw { #1 #2 } { {#3} } }
  }
\cs_new:Npn \@@_pop_left_auxiii:nnnw #1#2#3#4 \q_stop
  {
    \tl_if_empty:nTF {#4}
      { \exp_not:n { #2 {#1} } }
      { \@@_pop_left_auxii:nnnw { #1 #2 } { {#3} } }
  }
\cs_new:Npn \@@_pop_left_auxiv:nn #1#2
  {
    \s_@@
    { \tl_if_head_is_group:nT {#1} { { \tl_head:n {#1} } } }
    { \tl_if_head_is_space:nTF {#1} { \exp_not:f } { \tl_tail:n } {#1} }
    { \exp_not:n {#2} }
  }
\cs_new:Npn \@@_pop_left_auxv:nnn #1#2#3
  {
    \s_@@
    { \if_false: { \fi: \@@_pop_left_auxvi:n #1 } }
    { \exp_not:n {#2} }
    { \exp_not:n {#3} }
  }
\cs_new:Npn \@@_pop_left_auxvi:n #1
  {
    \tl_if_empty:nF {#1}
      {
        \tl_if_head_is_group:nT {#1} { { \tl_head:n {#1} } }
        {
          \tl_if_head_is_space:nTF {#1}
            { \exp_not:f } { \tl_tail:n } {#1}
        }
      }
    \exp_after:wN \exp_not:n \exp_after:wN { \if_false: } \fi:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\gtl_pop_left:NN, \gtl_gpop_left:NN}
%   Getting the first token and removing it from the extended token list
%   is done in two steps.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_pop_left:NN #1#2
  {
    \gtl_get_left:NN #1 #2
    \gtl_pop_left:N #1
  }
\cs_new_protected:Npn \gtl_gpop_left:NN #1#2
  {
    \gtl_get_left:NN #1 #2
    \gtl_gpop_left:N #1
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Longest token list starting an extended token list}
%
% \begin{macro}[EXP]{\gtl_left_tl:N}
% \begin{macro}[EXP]{\@@_left_tl:w}
%   If there is no leading chunk, return the middle chunk, otherwise
%   the first leading chunk.
%    \begin{macrocode}
\cs_new:Npn \gtl_left_tl:N #1
  { \exp_after:wN \@@_left_tl:w #1 }
\cs_new:Npn \@@_left_tl:w \s_@@ #1#2#3
  { \tl_if_empty:nTF {#1} { \exp_not:n {#2} } { \tl_head:n {#1} } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\gtl_pop_left_tl:N, \gtl_gpop_left_tl:N}
%   If there is no left chunk, remove the middle chunk, hence the
%   resulting gtl will start with two empty brace groups (one for the
%   absence of leading chunk, and one for the emptyness of the middle
%   chunk).  If there are left chunks replace the first one by an empty
%   chunk.
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_pop_left_tl:N #1
  { \tl_set:Nx #1 { \exp_after:wN \@@_pop_left_tl:w #1 } }
\cs_new_protected:Npn \gtl_gpop_left_tl:N #1
  { \tl_gset:Nx #1 { \exp_after:wN \@@_pop_left_tl:w #1 } }
\cs_new:Npn \@@_pop_left_tl:w \s_@@ #1#2#3
  {
    \s_@@
    \tl_if_empty:nTF {#1}
      { { } { } }
      {
        { { } \tl_tail:n {#1} }
        { \exp_not:n {#2} }
      }
    { \exp_not:n {#3} }
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{First item of an extended token list}
%
% \begin{macro}[EXP]{\gtl_left_item:NF}
% \begin{macro}[EXP]{\@@_left_item:wF, \@@_left_item_auxi:nwF}
%   The left-most item of an extended token list is the head of its left
%   token list.  The code thus starts like \cs{gtl_left_tl:N}.  It ends
%   with a check to test if we should use the head, or issue the false
%   code.
%    \begin{macrocode}
\cs_new:Npn \gtl_left_item:NF #1
  { \exp_after:wN \@@_left_item:wF #1 }
\cs_new:Npn \@@_left_item:wF \s_@@ #1#2#3
  { \@@_left_item_auxi:nwF #1 {#2} \q_stop }
\cs_new:Npn \@@_left_item_auxi:nwF #1#2 \q_stop #3
  { \tl_if_blank:nTF {#1} {#3} { \tl_head:n {#1} } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[TF]{\gtl_pop_left_item:NN, \gtl_gpop_left_item:NN}
% \begin{macro}
%   {\@@_pop_left_item:wNNN, \@@_pop_left_item_aux:nwnnNNN}
%   If there is no extra end-group characters, and if the balanced part
%   is blank, we cannot extract an item: return \texttt{false}.  If the
%   balanced part is not blank, store its first item into |#4|, and
%   store the altered generalized token list into~|#6|, locally or
%   globally.  Otherwise, pick out the part before the first extra
%   end-group character as |#1| of the second auxiliary, and do
%   essentially the same: if it is blank, there is no item, and if it is
%   not blank, pop its first item.
%    \begin{macrocode}
\prg_new_protected_conditional:Npnn \gtl_pop_left_item:NN #1#2 { TF , T , F }
  { \exp_after:wN \@@_pop_left_item:wNNN #1#2 \tl_set:Nx #1 }
\prg_new_protected_conditional:Npnn \gtl_gpop_left_item:NN #1#2 { TF , T , F }
  { \exp_after:wN \@@_pop_left_item:wNNN #1#2 \tl_gset:Nx #1 }
\cs_new_protected:Npn \@@_pop_left_item:wNNN
    \s_@@ #1#2#3 #4#5#6
  {
    \tl_if_empty:nTF {#1}
      {
        \tl_if_blank:nTF {#2} { \prg_return_false: }
          {
            \tl_set:Nx #4 { \tl_head:n {#2} }
            #5 #6
              {
                \s_@@ { } { \tl_tail:n {#2} }
                { \exp_not:n {#3} }
              }
            \prg_return_true:
          }
      }
      {
        \@@_pop_left_item_aux:nwnnNNN #1 \q_nil \q_stop
          {#2} {#3} #4 #5 #6
      }
  }
\cs_new_protected:Npn \@@_pop_left_item_aux:nwnnNNN
    #1#2 \q_stop #3#4#5#6#7
  {
    \tl_if_blank:nTF {#1} { \prg_return_false: }
      {
        \tl_set:Nx #5 { \tl_head:n {#1} }
        #6 #7
          {
            \s_@@
            { { \tl_tail:n {#1} } \@@_strip_nil_mark:w #2 \q_mark }
            { \exp_not:n {#3} }
            { \exp_not:n {#4} }
          }
        \prg_return_true:
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{First group in an extended token list}
%
% The functions of this section extract from an extended token list the
% tokens that would be absorbed after |\def\foo|, namely tokens with no
% begin-group nor end-group characters, followed by one group.  Those
% tokens are either left in the input stream or stored in a token list
% variable, and the |pop| functions also remove those tokens from the
% extended token list variable.
%
% \begin{macro}[EXP]{\gtl_left_text:NF}
% \begin{macro}[EXP]
%   {
%     \@@_left_text:wF,
%     \@@_left_text_auxi:nwF,
%     \@@_left_text_auxii:wnwF,
%     \@@_left_text_auxiii:nnwF,
%   }
%    \begin{macrocode}
\cs_new:Npn \gtl_left_text:NF #1
  { \exp_after:wN \@@_left_text:wF #1 }
\cs_new:Npn \@@_left_text:wF \s_@@ #1#2#3
  {
    \tl_if_empty:nTF {#1}
      { \@@_left_text_auxi:nwF {#2} \q_stop }
      { \@@_left_text_auxi:nwF #1 \q_stop }
  }
\cs_new:Npn \@@_left_text_auxi:nwF #1#2 \q_stop
  { \@@_left_text_auxii:wnwF #1 \q_mark { } \q_mark \q_stop }
\cs_new:Npn \@@_left_text_auxii:wnwF #1 #
  { \@@_left_text_auxiii:nnwF {#1} }
\cs_new:Npn \@@_left_text_auxiii:nnwF #1#2 #3 \q_mark #4 \q_stop #5
  { \tl_if_empty:nTF {#4} {#5} { \exp_not:n { #1 {#2} } } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\gtl_pop_left_text:N, \gtl_gpop_left_text:N}
% \begin{macro}[rEXP]
%   {
%     \@@_pop_left_text:w,
%     \@@_pop_left_text_auxi:n,
%     \@@_pop_left_text_auxii:wnw,
%     \@@_pop_left_text_auxiii:nnw,
%     \@@_pop_left_text_auxiv:nw,
%   }
%    \begin{macrocode}
\cs_new_protected:Npn \gtl_pop_left_text:N #1
  { \tl_set:Nx #1 { \exp_after:wN \@@_pop_left_text:w #1 } }
\cs_new_protected:Npn \gtl_gpop_left_text:N #1
  { \tl_gset:Nx #1 { \exp_after:wN \@@_pop_left_text:w #1 } }
\cs_new:Npn \@@_pop_left_text:w \s_@@ #1#2#3
  {
    \s_@@
    \tl_if_empty:nTF {#1}
      {
        { }
        { \@@_pop_left_text_auxi:n {#2} }
      }
      {
        { \@@_pop_left_text_auxiv:nw #1 \q_nil \q_mark }
        { \exp_not:n {#2} }
      }
    { \exp_not:n {#3} }
  }
\cs_new:Npn \@@_pop_left_text_auxi:n #1
  {
    \@@_pop_left_text_auxii:wnw #1
      \q_nil \q_mark { } \q_mark \q_stop
  }
\cs_new:Npn \@@_pop_left_text_auxii:wnw #1 #
  { \@@_pop_left_text_auxiii:nnw {#1} }
\cs_new:Npn \@@_pop_left_text_auxiii:nnw #1#2#3 \q_mark #4 \q_stop
  {
    \tl_if_empty:nTF {#4}
      { \@@_strip_nil_mark:w #1 }
      { \@@_strip_nil_mark:w #3 \q_mark }
  }
\cs_new:Npn \@@_pop_left_text_auxiv:nw #1
  {
    { \@@_pop_left_text_auxi:n {#1} }
    \@@_strip_nil_mark:w
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Counting tokens}
%
% \begin{macro}[EXP]{\@@_tl_count:n}
% \begin{macro}[EXP]{\@@_tl_count_loop:n, \@@_tl_count_test:w}
%   A more robust version of \cs{tl_count:n}, which will however break
%   if the token list contains \cs{q_stop} at the outer brace level.
%   This cannot happen when \cs{@@_tl_count:n} is called with lists of
%   braced items.  The technique is to loop, and when seeing
%   \cs{q_mark}, make sure that this is really the end of the list.
%    \begin{macrocode}
\cs_new:Npn \@@_tl_count:n #1
  { \int_eval:n { 0 \@@_tl_count_loop:n #1 \q_nil \q_stop } }
\cs_new:Npn \@@_tl_count_loop:n #1
  {
    \quark_if_nil:nTF {#1}
      { \@@_tl_count_test:w }
      { + 1 \@@_tl_count_loop:n }
  }
\cs_new:Npn \@@_tl_count_test:w #1 \q_stop
  { \tl_if_empty:nF {#1} { + 1 \@@_tl_count_loop:n #1 \q_stop } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\gtl_extra_begin:N, \gtl_extra_end:N}
% \begin{macro}[EXP]{\@@_extra_begin:w, \@@_extra_end:w}
%   Count the number of extra end-group or of extra begin-group
%   characters in an extended token list.  This is the number of items
%   in the first or third brace groups.  We cannot use \cs{tl_count:n},
%   as \pkg{gtl} is meant to be robust against inclusion of quarks.
%    \begin{macrocode}
\cs_new:Npn \gtl_extra_end:N #1
  { \exp_after:wN \@@_extra_end:w #1 }
\cs_new:Npn \@@_extra_end:w \s_@@ #1#2#3
  { \@@_tl_count:n {#1} }
\cs_new:Npn \gtl_extra_begin:N #1
  { \exp_after:wN \@@_extra_begin:w #1 }
\cs_new:Npn \@@_extra_begin:w \s_@@ #1#2#3
  { \@@_tl_count:n {#3} }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\gtl_count_tokens:N}
% \begin{macro}[rEXP]
%   {
%     \@@_count_tokens:w,
%     \@@_count_auxi:nw,
%     \@@_count_auxii:w,
%     \@@_count_auxiii:n
%   }
%    \begin{macrocode}
\cs_new:Npn \gtl_count_tokens:N #1
  { \exp_after:wN \@@_count_tokens:w #1 }
\cs_new:Npn \@@_count_tokens:w \s_@@ #1#2#3
  {
    \int_eval:n
      { -1 \@@_count_auxi:nw #1 {#2} #3 \q_nil \q_stop }
  }
\cs_new:Npn \@@_count_auxi:nw #1
  {
    \quark_if_nil:nTF {#1}
      { \@@_count_auxii:w }
      {
        + 1
        \@@_count_auxiii:n {#1}
        \@@_count_auxi:nw
      }
  }
\cs_new:Npn \@@_count_auxii:w #1 \q_stop
  {
    \tl_if_empty:nF {#1}
      {
        + 2
        \@@_count_auxi:nw #1 \q_stop
      }
  }
\cs_new:Npn \@@_count_auxiii:n #1
  {
    \tl_if_empty:nF {#1}
      {
        \tl_if_head_is_group:nTF {#1}
          {
            + 2
            \exp_args:No \@@_count_auxiii:n { \use:n #1 }
          }
          {
            + 1
            \tl_if_head_is_N_type:nTF {#1}
              { \exp_args:No \@@_count_auxiii:n { \use_none:n #1 } }
              { \exp_args:Nf \@@_count_auxiii:n {#1} }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
\@@_end_package_hook:
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \end{implementation}
%
% \endinput