Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3token.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3token.dtx
4 %
5 % Copyright (C) 2005-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3token} module\\ Token manipulation^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % This module deals with tokens. Now this is perhaps not the most
53 % precise description so let's try with a better description: When
54 % programming in \TeX{}, it is often desirable to know just what a
55 % certain token is: is it a control sequence or something
56 % else. Similarly one often needs to know if a control sequence is
57 % expandable or not, a macro or a primitive, how many arguments it
58 % takes etc. Another thing of great importance (especially when it
59 % comes to document commands) is looking ahead in the token stream to
60 % see if a certain character is present and maybe even remove it or
61 % disregard other tokens while scanning. This module provides
62 % functions for both and as such has two primary function
63 % categories: |\token_| for anything that deals with tokens and
64 % |\peek_| for looking ahead in the token stream.
65 %
66 % Most functions we describe here can be used on control sequences,
67 % as those are tokens as well.
68 %
69 % It is important to distinguish two aspects of a token: its
70 % \enquote{shape} (for lack of a better word), which affects the
71 % matching of delimited arguments and the comparison of token lists
72 % containing this token, and its \enquote{meaning}, which affects
73 % whether the token expands or what operation it performs. One can have
74 % tokens of different shapes with the same meaning, but not the
75 % converse.
76 %
77 % For instance, \cs{if:w}, \cs{if_charcode:w}, and \cs[no-index]{tex_if:D} are
78 % three names for the same internal operation of \TeX{}, namely the
79 % primitive testing the next two characters for equality of their
80 % character code. They have the same meaning hence behave identically
81 % in many situations. However, \TeX{} distinguishes them when searching
82 % for a delimited argument. Namely, the example function
83 % |\show_until_if:w| defined below takes everything until \cs{if:w}
84 % as an argument, despite the presence of other copies of \cs{if:w}
85 % under different names.
86 % \begin{verbatim}
87 % \cs_new:Npn \show_until_if:w #1 \if:w { \tl_show:n {#1} }
88 % \show_until_if:w \tex_if:D \if_charcode:w \if:w
89 % \end{verbatim}
90 % A list of all possible shapes and a list of all possible meanings are
91 % given in section~\ref{sec:l3token:all-tokens}.
92 %
93 % \section{Creating character tokens}
94 %
95 % \begin{function}[updated = 2015-11-12]
96 % {
97 % \char_set_active_eq:NN, \char_set_active_eq:Nc,
98 % \char_gset_active_eq:NN, \char_gset_active_eq:Nc
99 % }
100 % \begin{syntax}
101 % \cs{char_set_active_eq:NN} \meta{char} \meta{function}
102 % \end{syntax}
103 % Sets the behavior of the \meta{char} in situations where it is
104 % active (category code $13$) to be equivalent to that of the
105 % definition of the \meta{function} at the time \cs{char_set_active_eq:NN}
106 % is used. The category code of the \meta{char} is
107 % \emph{unchanged} by this process. The \meta{function} may itself
108 % be an active character.
109 % \end{function}
110 %
111 % \begin{function}[added = 2015-11-12]
112 % {
113 % \char_set_active_eq:nN, \char_set_active_eq:nc,
114 % \char_gset_active_eq:nN, \char_gset_active_eq:nc
115 % }
116 % \begin{syntax}
117 % \cs{char_set_active_eq:nN} \Arg{integer expression} \meta{function}
118 % \end{syntax}
119 % Sets the behavior of the \meta{char} which has character
120 % code as given by the \meta{integer expression} in situations
121 % where it is active (category code $13$) to be equivalent to that of the
122 % \meta{function} at the time \cs{char_set_active_eq:nN}
123 % is used. The category code of the \meta{char} is
124 % \emph{unchanged} by this process. The \meta{function} may itself
125 % be an active character.
126 % \end{function}
127 %
128 % \begin{function}[EXP, added = 2015-09-09, updated = 2019-01-16]
129 % {\char_generate:nn}
130 % \begin{syntax}
131 % \cs{char_generate:nn} \Arg{charcode} \Arg{catcode}
132 % \end{syntax}
133 % Generates a character token of the given \meta{charcode} and \meta{catcode}
134 % (both of which may be integer expressions). The \meta{catcode} may be
135 % one of
136 % \begin{itemize}
137 % \item $1$ (begin group)
138 % \item $2$ (end group)
139 % \item $3$ (math toggle)
140 % \item $4$ (alignment)
141 % \item $6$ (parameter)
142 % \item $7$ (math superscript)
143 % \item $8$ (math subscript)
144 % \item $10$ (space)
145 % \item $11$ (letter)
146 % \item $12$ (other)
147 % \item $13$ (active)
148 % \end{itemize}
149 % and other values raise an error. The \meta{charcode} may be any one valid
150 % for the engine in use, except that for \meta{catcode} $10$, \meta{charcode}
151 % $0$ is not allowed.
152 % Active characters cannot be generated in older versions of \XeTeX{}.
153 % Another way to build token lists with unusual category codes is
154 % \cs{regex_replace_all:nnN} |{.*}| \Arg{replacement} \meta{tl~var}.
155 % \begin{texnote}
156 % Exactly two expansions are needed to produce the character.
157 % \end{texnote}
158 % \end{function}
159 %
160 % \begin{variable}[added = 2017-08-07]{\c_catcode_active_space_tl}
161 % Token list containing one character with category code $13$,
162 % (\enquote{active}), and character code $32$ (space).
163 % \end{variable}
164 %
165 % \begin{variable}[added = 2011-09-05]{\c_catcode_other_space_tl}
166 % Token list containing one character with category code $12$,
167 % (\enquote{other}), and character code $32$ (space).
168 % \end{variable}
169 %
170 % \section{Manipulating and interrogating character tokens}
171 %
172 % \begin{function}[updated = 2015-11-11]
173 % {
174 % \char_set_catcode_escape:N ,
175 % \char_set_catcode_group_begin:N ,
176 % \char_set_catcode_group_end:N ,
177 % \char_set_catcode_math_toggle:N ,
178 % \char_set_catcode_alignment:N ,
179 % \char_set_catcode_end_line:N ,
180 % \char_set_catcode_parameter:N ,
181 % \char_set_catcode_math_superscript:N ,
182 % \char_set_catcode_math_subscript:N ,
183 % \char_set_catcode_ignore:N ,
184 % \char_set_catcode_space:N ,
185 % \char_set_catcode_letter:N ,
186 % \char_set_catcode_other:N ,
187 % \char_set_catcode_active:N ,
188 % \char_set_catcode_comment:N ,
189 % \char_set_catcode_invalid:N
190 % }
191 % \begin{syntax}
192 % \cs{char_set_catcode_letter:N} \meta{character}
193 % \end{syntax}
194 % Sets the category code of the \meta{character} to that indicated in
195 % the function name. Depending on the current category code of the
196 % \meta{token} the escape token may also be needed:
197 % \begin{verbatim}
198 % \char_set_catcode_other:N \%
199 % \end{verbatim}
200 % The assignment is local.
201 % \end{function}
202 %
203 % \begin{function}[updated = 2015-11-11]
204 % {
205 % \char_set_catcode_escape:n ,
206 % \char_set_catcode_group_begin:n ,
207 % \char_set_catcode_group_end:n ,
208 % \char_set_catcode_math_toggle:n ,
209 % \char_set_catcode_alignment:n ,
210 % \char_set_catcode_end_line:n ,
211 % \char_set_catcode_parameter:n ,
212 % \char_set_catcode_math_superscript:n ,
213 % \char_set_catcode_math_subscript:n ,
214 % \char_set_catcode_ignore:n ,
215 % \char_set_catcode_space:n ,
216 % \char_set_catcode_letter:n ,
217 % \char_set_catcode_other:n ,
218 % \char_set_catcode_active:n ,
219 % \char_set_catcode_comment:n ,
220 % \char_set_catcode_invalid:n
221 % }
222 % \begin{syntax}
223 % \cs{char_set_catcode_letter:n} \Arg{integer expression}
224 % \end{syntax}
225 % Sets the category code of the \meta{character} which has character
226 % code as given by the \meta{integer expression}. This version can be
227 % used to set up characters which cannot otherwise be given
228 % (\emph{cf.}~the \texttt{N}-type variants). The assignment is local.
229 % \end{function}
230 %
231 % \begin{function}[updated = 2015-11-11]{\char_set_catcode:nn}
232 % \begin{syntax}
233 % \cs{char_set_catcode:nn} \Arg{int expr_1} \Arg{int expr_2}
234 % \end{syntax}
235 % These functions set the category code of the \meta{character} which
236 % has character code as given by the \meta{integer expression}.
237 % The first \meta{integer expression}
238 % is the character code and the second is the category code to apply.
239 % The setting applies within the current \TeX{} group. In general, the
240 % symbolic functions \cs[no-index]{char_set_catcode_\meta{type}} should be preferred,
241 % but there are cases where these lower-level functions may be useful.
242 % \end{function}
243 %
244 % \begin{function}[EXP]{\char_value_catcode:n}
245 % \begin{syntax}
246 % \cs{char_value_catcode:n} \Arg{integer expression}
247 % \end{syntax}
248 % Expands to the current category code of the \meta{character} with
249 % character code given by the
250 % \meta{integer expression}.
251 % \end{function}
252 %
253 % \begin{function}{\char_show_value_catcode:n}
254 % \begin{syntax}
255 % \cs{char_show_value_catcode:n} \Arg{integer expression}
256 % \end{syntax}
257 % Displays the current category code of the \meta{character} with
258 % character code given by the \meta{integer expression} on the
259 % terminal.
260 % \end{function}
261 %
262 % \begin{function}[updated = 2015-08-06]{\char_set_lccode:nn}
263 % \begin{syntax}
264 % \cs{char_set_lccode:nn} \Arg{int expr_1} \Arg{int expr_2}
265 % \end{syntax}
266 % Sets up the behavior of the \meta{character} when
267 % found inside \cs{text_lowercase:n}, such that \meta{character_1}
268 % will be converted into \meta{character_2}. The two \meta{characters}
269 % may be specified using an \meta{integer expression} for the character code
270 % concerned. This may include the \TeX{} |`|\meta{character}
271 % method for converting a single character into its character
272 % code:
273 % \begin{verbatim}
274 % \char_set_lccode:nn { `\A } { `\a } % Standard behavior
275 % \char_set_lccode:nn { `\A } { `\A + 32 }
276 % \char_set_lccode:nn { 50 } { 60 }
277 % \end{verbatim}
278 % The setting applies within the current \TeX{} group.
279 % \end{function}
280 %
281 % \begin{function}[EXP]{\char_value_lccode:n}
282 % \begin{syntax}
283 % \cs{char_value_lccode:n} \Arg{integer expression}
284 % \end{syntax}
285 % Expands to the current lower case code of the \meta{character} with
286 % character code given by the
287 % \meta{integer expression}.
288 % \end{function}
289 %
290 % \begin{function}{\char_show_value_lccode:n}
291 % \begin{syntax}
292 % \cs{char_show_value_lccode:n} \Arg{integer expression}
293 % \end{syntax}
294 % Displays the current lower case code of the \meta{character} with
295 % character code given by the \meta{integer expression} on the
296 % terminal.
297 % \end{function}
298 %
299 % \begin{function}[updated = 2015-08-06]{\char_set_uccode:nn}
300 % \begin{syntax}
301 % \cs{char_set_uccode:nn} \Arg{int expr_1} \Arg{int expr_2}
302 % \end{syntax}
303 % Sets up the behavior of the \meta{character} when
304 % found inside \cs{text_uppercase:n}, such that \meta{character_1}
305 % will be converted into \meta{character_2}. The two \meta{characters}
306 % may be specified using an \meta{integer expression} for the character code
307 % concerned. This may include the \TeX{} |`|\meta{character}
308 % method for converting a single character into its character
309 % code:
310 % \begin{verbatim}
311 % \char_set_uccode:nn { `\a } { `\A } % Standard behavior
312 % \char_set_uccode:nn { `\A } { `\A - 32 }
313 % \char_set_uccode:nn { 60 } { 50 }
314 % \end{verbatim}
315 % The setting applies within the current \TeX{} group.
316 % \end{function}
317 %
318 % \begin{function}[EXP]{\char_value_uccode:n}
319 % \begin{syntax}
320 % \cs{char_value_uccode:n} \Arg{integer expression}
321 % \end{syntax}
322 % Expands to the current upper case code of the \meta{character} with
323 % character code given by the
324 % \meta{integer expression}.
325 % \end{function}
326 %
327 % \begin{function}{\char_show_value_uccode:n}
328 % \begin{syntax}
329 % \cs{char_show_value_uccode:n} \Arg{integer expression}
330 % \end{syntax}
331 % Displays the current upper case code of the \meta{character} with
332 % character code given by the \meta{integer expression} on the
333 % terminal.
334 % \end{function}
335 %
336 % \begin{function}[updated = 2015-08-06]{\char_set_mathcode:nn}
337 % \begin{syntax}
338 % \cs{char_set_mathcode:nn} \Arg{int expr_1} \Arg{int expr_2}
339 % \end{syntax}
340 % This function sets up the math code of \meta{character}.
341 % The \meta{character} is specified as
342 % an \meta{integer expression} which will be used as the character
343 % code of the relevant character. The setting applies within the
344 % current \TeX{} group.
345 % \end{function}
346 %
347 % \begin{function}[EXP]{\char_value_mathcode:n}
348 % \begin{syntax}
349 % \cs{char_value_mathcode:n} \Arg{integer expression}
350 % \end{syntax}
351 % Expands to the current math code of the \meta{character} with
352 % character code given by the
353 % \meta{integer expression}.
354 % \end{function}
355 %
356 % \begin{function}{\char_show_value_mathcode:n}
357 % \begin{syntax}
358 % \cs{char_show_value_mathcode:n} \Arg{integer expression}
359 % \end{syntax}
360 % Displays the current math code of the \meta{character} with
361 % character code given by the \meta{integer expression} on the
362 % terminal.
363 % \end{function}
364 %
365 % \begin{function}[updated = 2015-08-06]{\char_set_sfcode:nn}
366 % \begin{syntax}
367 % \cs{char_set_sfcode:nn} \Arg{int expr_1} \Arg{int expr_2}
368 % \end{syntax}
369 % This function sets up the space factor for the \meta{character}.
370 % The \meta{character} is specified as
371 % an \meta{integer expression} which will be used as the character
372 % code of the relevant character. The setting applies within the
373 % current \TeX{} group.
374 % \end{function}
375 %
376 % \begin{function}[EXP]{\char_value_sfcode:n}
377 % \begin{syntax}
378 % \cs{char_value_sfcode:n} \Arg{integer expression}
379 % \end{syntax}
380 % Expands to the current space factor for the \meta{character} with
381 % character code given by the
382 % \meta{integer expression}.
383 % \end{function}
384 %
385 % \begin{function}{\char_show_value_sfcode:n}
386 % \begin{syntax}
387 % \cs{char_show_value_sfcode:n} \Arg{integer expression}
388 % \end{syntax}
389 % Displays the current space factor for the \meta{character} with
390 % character code given by the \meta{integer expression} on the
391 % terminal.
392 % \end{function}
393 %
394 % \begin{variable}[added = 2012-01-23, updated = 2015-11-11]{\l_char_active_seq}
395 % Used to track which tokens may require special handling at the document
396 % level as they are (or have been at some point)
397 % of category \meta{active} (catcode~$13$). Each entry in
398 % the sequence consists of a single escaped token, for example |\~|.
399 % Active tokens should be added to the sequence when they are defined for
400 % general document use.
401 % \end{variable}
402 %
403 % \begin{variable}[added = 2012-01-23, updated = 2015-11-11]{\l_char_special_seq}
404 % Used to track which tokens will require special handling when working with
405 % verbatim-like material at the document level as they are not of categories
406 % \meta{letter} (catcode~$11$) or \meta{other} (catcode~$12$). Each entry in
407 % the sequence consists of a single escaped token, for example |\\| for the
408 % backslash or |\{| for an opening brace. ^^A \}
409 % Escaped tokens should be added to the sequence when they are defined for
410 % general document use.
411 % \end{variable}
412 %
413 % \section{Generic tokens}
414 %
415 % \begin{variable}[module = token]
416 % {
417 % \c_group_begin_token,
418 % \c_group_end_token,
419 % \c_math_toggle_token,
420 % \c_alignment_token,
421 % \c_parameter_token,
422 % \c_math_superscript_token,
423 % \c_math_subscript_token,
424 % \c_space_token
425 % }
426 % These are implicit tokens which have the category code described
427 % by their name. They are used internally for test purposes but
428 % are also available to the programmer for other uses.
429 % \begin{texnote}
430 % The tokens \cs{c_group_begin_token}, \cs{c_group_end_token}, and
431 % \cs{c_space_token} are \pkg{expl3} counterparts of \LaTeXe{}'s
432 % \tn{bgroup}, \tn{egroup}, and \cs{@sptoken}.
433 % \end{texnote}
434 % \end{variable}
435 %
436 % \begin{variable}
437 % {
438 % \c_catcode_letter_token,
439 % \c_catcode_other_token
440 % }
441 % These are implicit tokens which have the category code described
442 % by their name. They are used internally for test purposes and should
443 % not be used other than for category code tests.
444 % \end{variable}
445 %
446 % \section{Converting tokens}
447 %
448 % \begin{function}[EXP]{\token_to_meaning:N, \token_to_meaning:c}
449 % \begin{syntax}
450 % \cs{token_to_meaning:N} \meta{token}
451 % \end{syntax}
452 % Inserts the current meaning of the \meta{token} into the input
453 % stream as a series of characters of category code $12$ (other).
454 % This is the primitive \TeX{} description of the \meta{token},
455 % thus for example both functions defined by \cs{cs_set_nopar:Npn}
456 % and token list variables defined using \cs{tl_new:N} are described
457 % as |macro|s.
458 % \begin{texnote}
459 % This is the \TeX{} primitive \tn{meaning}.
460 % The \meta{token} can thus be an explicit space token or an
461 % explicit begin-group or end-group character token (|{|~or~|}| when
462 % normal \TeX{} category codes apply) even though these are not
463 % valid \texttt{N}-type arguments.
464 % \end{texnote}
465 % \end{function}
466 %
467 % \begin{function}[EXP]{\token_to_str:N, \token_to_str:c}
468 % \begin{syntax}
469 % \cs{token_to_str:N} \meta{token}
470 % \end{syntax}
471 % Converts the given \meta{token} into a series of characters with
472 % category code $12$ (other). If the \meta{token} is a control
473 % sequence, this will start with the current escape character with
474 % category code $12$ (the escape character is part of the
475 % \meta{token}). This function requires only a single expansion.
476 % \begin{texnote}
477 % \cs{token_to_str:N} is the \TeX{} primitive \tn{string}.
478 % The \meta{token} can thus be an explicit space tokens or an
479 % explicit begin-group or end-group character token (|{|~or~|}| when
480 % normal \TeX{} category codes apply) even though these are not
481 % valid \texttt{N}-type arguments.
482 % \end{texnote}
483 % \end{function}
484 %
485 % \begin{function}[EXP,added = 2023-10-15]{\token_to_catcode:N}
486 % \begin{syntax}
487 % \cs{token_to_catcode:N} \meta{token}
488 % \end{syntax}
489 % Converts the given \meta{token} into a number describing its category code.
490 % If \meta{token} is a control sequence this expands to $16$. This can't
491 % detect the categories $0$ (escape character), $5$ (end of line), $9$
492 % (ignored character), $14$ (comment character), or $15$ (invalid character).
493 % Control sequences or active characters let to a token of one of the
494 % detectable category codes will yield that category.
495 % \end{function}
496 %
497 % \section{Token conditionals}
498 %
499 % \begin{function}[EXP,pTF]{\token_if_group_begin:N}
500 % \begin{syntax}
501 % \cs{token_if_group_begin_p:N} \meta{token} \\
502 % \cs{token_if_group_begin:NTF} \meta{token} \Arg{true code} \Arg{false code}
503 % \end{syntax}
504 % Tests if \meta{token} has the category code of a begin group token
505 % (|{| when normal \TeX{} category codes are in ^^A }
506 % force).
507 % Note that an explicit begin group token cannot be tested in this way,
508 % as it is not a valid \texttt{N}-type argument.
509 % \end{function}
510 %
511 % \begin{function}[EXP,pTF]{\token_if_group_end:N}
512 % \begin{syntax}
513 % \cs{token_if_group_end_p:N} \meta{token} \\
514 % \cs{token_if_group_end:NTF} \meta{token} \Arg{true code} \Arg{false code}
515 % \end{syntax}
516 % Tests if \meta{token} has the category code of an end group token
517 % (^^A {
518 % |}| when normal \TeX{} category codes are in force).
519 % Note that an explicit end group token cannot be tested in this way,
520 % as it is not a valid \texttt{N}-type argument.
521 % \end{function}
522 %
523 % \begin{function}[EXP,pTF]{\token_if_math_toggle:N}
524 % \begin{syntax}
525 % \cs{token_if_math_toggle_p:N} \meta{token} \\
526 % \cs{token_if_math_toggle:NTF} \meta{token} \Arg{true code} \Arg{false code}
527 % \end{syntax}
528 % Tests if \meta{token} has the category code of a math shift token
529 % (|$| when normal \TeX{} category codes are in force).
530 % \end{function}
531 %
532 % \begin{function}[EXP,pTF]{\token_if_alignment:N}
533 % \begin{syntax}
534 % \cs{token_if_alignment_p:N} \meta{token} \\
535 % \cs{token_if_alignment:NTF} \meta{token} \Arg{true code} \Arg{false code}
536 % \end{syntax}
537 % Tests if \meta{token} has the category code of an alignment token
538 % (|&| when normal \TeX{} category codes are in force).
539 % \end{function}
540 %
541 % \begin{function}[EXP,pTF]{\token_if_parameter:N}
542 % \begin{syntax}
543 % \cs{token_if_parameter_p:N} \meta{token} \\
544 % \cs{token_if_parameter:NTF} \meta{token} \Arg{true code} \Arg{false code}
545 % \end{syntax}
546 % Tests if \meta{token} has the category code of a macro parameter token
547 % (|#| when normal \TeX{} category codes are in force).
548 % \end{function}
549 %
550 % \begin{function}[EXP,pTF]{\token_if_math_superscript:N}
551 % \begin{syntax}
552 % \cs{token_if_math_superscript_p:N} \meta{token} \\
553 % \cs{token_if_math_superscript:NTF} \meta{token} \Arg{true code} \Arg{false code}
554 % \end{syntax}
555 % Tests if \meta{token} has the category code of a superscript token
556 % (|^| when normal \TeX{} category codes are in force).
557 % \end{function}
558 %
559 % \begin{function}[EXP,pTF]{\token_if_math_subscript:N}
560 % \begin{syntax}
561 % \cs{token_if_math_subscript_p:N} \meta{token} \\
562 % \cs{token_if_math_subscript:NTF} \meta{token} \Arg{true code} \Arg{false code}
563 % \end{syntax}
564 % Tests if \meta{token} has the category code of a subscript token
565 % (|_| when normal \TeX{} category codes are in force).
566 % \end{function}
567 %
568 % \begin{function}[EXP,pTF]{\token_if_space:N}
569 % \begin{syntax}
570 % \cs{token_if_space_p:N} \meta{token} \\
571 % \cs{token_if_space:NTF} \meta{token} \Arg{true code} \Arg{false code}
572 % \end{syntax}
573 % Tests if \meta{token} has the category code of a space token.
574 % Note that an explicit space token with character code $32$ cannot
575 % be tested in this way, as it is not a valid \texttt{N}-type argument.
576 % \end{function}
577 %
578 % \begin{function}[EXP,pTF]{\token_if_letter:N}
579 % \begin{syntax}
580 % \cs{token_if_letter_p:N} \meta{token} \\
581 % \cs{token_if_letter:NTF} \meta{token} \Arg{true code} \Arg{false code}
582 % \end{syntax}
583 % Tests if \meta{token} has the category code of a letter token.
584 % \end{function}
585 %
586 % \begin{function}[EXP,pTF]{\token_if_other:N}
587 % \begin{syntax}
588 % \cs{token_if_other_p:N} \meta{token} \\
589 % \cs{token_if_other:NTF} \meta{token} \Arg{true code} \Arg{false code}
590 % \end{syntax}
591 % Tests if \meta{token} has the category code of an \enquote{other}
592 % token.
593 % \end{function}
594 %
595 % \begin{function}[EXP,pTF]{\token_if_active:N}
596 % \begin{syntax}
597 % \cs{token_if_active_p:N} \meta{token} \\
598 % \cs{token_if_active:NTF} \meta{token} \Arg{true code} \Arg{false code}
599 % \end{syntax}
600 % Tests if \meta{token} has the category code of an active character.
601 % \end{function}
602 %
603 % \begin{function}[EXP,pTF]{\token_if_eq_catcode:NN}
604 % \begin{syntax}
605 % \cs{token_if_eq_catcode_p:NN} \meta{token_1} \meta{token_2} \\
606 % \cs{token_if_eq_catcode:NNTF} \meta{token_1} \meta{token_2} \Arg{true code} \Arg{false code}
607 % \end{syntax}
608 % Tests if the two \meta{tokens} have the same category code.
609 % \end{function}
610 %
611 % \begin{function}[EXP,pTF]{\token_if_eq_charcode:NN}
612 % \begin{syntax}
613 % \cs{token_if_eq_charcode_p:NN} \meta{token_1} \meta{token_2} \\
614 % \cs{token_if_eq_charcode:NNTF} \meta{token_1} \meta{token_2} \Arg{true code} \Arg{false code}
615 % \end{syntax}
616 % Tests if the two \meta{tokens} have the same character code.
617 % \end{function}
618 %
619 % \begin{function}[EXP,pTF]{\token_if_eq_meaning:NN}
620 % \begin{syntax}
621 % \cs{token_if_eq_meaning_p:NN} \meta{token_1} \meta{token_2} \\
622 % \cs{token_if_eq_meaning:NNTF} \meta{token_1} \meta{token_2} \Arg{true code} \Arg{false code}
623 % \end{syntax}
624 % Tests if the two \meta{tokens} have the same meaning when expanded.
625 % \end{function}
626 %
627 % \begin{function}[updated = 2011-05-23, EXP,pTF]{\token_if_macro:N}
628 % \begin{syntax}
629 % \cs{token_if_macro_p:N} \meta{token} \\
630 % \cs{token_if_macro:NTF} \meta{token} \Arg{true code} \Arg{false code}
631 % \end{syntax}
632 % Tests if the \meta{token} is a \TeX{} macro.
633 % \end{function}
634 %
635 % \begin{function}[EXP,pTF]{\token_if_cs:N}
636 % \begin{syntax}
637 % \cs{token_if_cs_p:N} \meta{token} \\
638 % \cs{token_if_cs:NTF} \meta{token} \Arg{true code} \Arg{false code}
639 % \end{syntax}
640 % Tests if the \meta{token} is a control sequence.
641 % \end{function}
642 %
643 % \begin{function}[EXP,pTF]{\token_if_expandable:N}
644 % \begin{syntax}
645 % \cs{token_if_expandable_p:N} \meta{token} \\
646 % \cs{token_if_expandable:NTF} \meta{token} \Arg{true code} \Arg{false code}
647 % \end{syntax}
648 % Tests if the \meta{token} is expandable. This test returns \meta{false}
649 % for an undefined token.
650 % \end{function}
651 %
652 % \begin{function}[EXP, pTF, added = 2025-05-12]{\token_if_control_symbol:N}
653 % \begin{syntax}
654 % \cs{token_if_control_symbol_p:N} \meta{token}
655 % \cs{token_if_control_symbol:NTF} \meta{token} \Arg{true code} \Arg{false code}
656 % \end{syntax}
657 % Tests whether the \meta{token} is a control sequence with a name comprised
658 % of exactly one non-letter character (called a \enquote{control symbol}).
659 % Specifically, only the following tokens leave \meta{false code}:
660 % \begin{itemize}
661 % \item explicit characters, such as |a| or |"|
662 % \item the escape character followed by one or more characters of category
663 % code 11 (letter), such as |\foo|
664 % \end{itemize}
665 % Any other token will leave \meta{true code}. The category codes which
666 % apply are those at the point the test is used, not those used when the \meta{token}
667 % is defined.
668 % \end{function}
669 %
670 % \begin{function}[EXP, pTF, added = 2025-05-12]{\token_if_control_word:N}
671 % \begin{syntax}
672 % \cs{token_if_control_word_p:N} \meta{token}
673 % \cs{token_if_control_word:NTF} \meta{token} \Arg{true code} \Arg{false code}
674 % \end{syntax}
675 % Tests whether the \meta{token} is a control sequence with a name comprised
676 % of one or more letters (called a \enquote{control word}).
677 % Specifically, only these tokens leave \meta{false code}:
678 % \begin{itemize}
679 % \item explicit characters, such as |a| or |"|
680 % \item the escape character followed by exactly one character whose
681 % category code is not 11 (letter) when used (not tokenized), such as
682 % |\ |, |\&|
683 % \end{itemize}
684 % Any other token will leave \meta{true code}. The category codes which
685 % apply are those at the point the test is used, not those used when the \meta{token}
686 % is defined.
687 % \end{function}
688 %
689 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_long_macro:N}
690 % \begin{syntax}
691 % \cs{token_if_long_macro_p:N} \meta{token} \\
692 % \cs{token_if_long_macro:NTF} \meta{token} \Arg{true code} \Arg{false code}
693 % \end{syntax}
694 % Tests if the \meta{token} is a long macro with no other prefix; to
695 % test for a macro that is both long and protected, use
696 % \cs[index = token_if_protected_long_macro:NTF]^^A
697 % {token_if_protected_long_macro:N(TF)}.
698 % \end{function}
699 %
700 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_protected_macro:N}
701 % \begin{syntax}
702 % \cs{token_if_protected_macro_p:N} \meta{token} \\
703 % \cs{token_if_protected_macro:NTF} \meta{token} \Arg{true code} \Arg{false code}
704 % \end{syntax}
705 % Tests if the \meta{token} is a protected macro with no other prefix; to
706 % test for a macro that is both protected and long, use
707 % \cs[index = token_if_protected_long_macro:NTF]^^A
708 % {token_if_protected_long_macro:N(TF)}.
709 % \end{function}
710 %
711 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_protected_long_macro:N}
712 % \begin{syntax}
713 % \cs{token_if_protected_long_macro_p:N} \meta{token} \\
714 % \cs{token_if_protected_long_macro:NTF} \meta{token} \Arg{true code} \Arg{false code}
715 % \end{syntax}
716 % Tests if the \meta{token} is a protected long macro.
717 % \end{function}
718 %
719 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_chardef:N}
720 % \begin{syntax}
721 % \cs{token_if_chardef_p:N} \meta{token} \\
722 % \cs{token_if_chardef:NTF} \meta{token} \Arg{true code} \Arg{false code}
723 % \end{syntax}
724 % Tests if the \meta{token} is defined to be a chardef.
725 % \begin{texnote}
726 % Booleans, boxes and small integer constants are implemented as
727 % \tn{chardef}s.
728 % \end{texnote}
729 % \end{function}
730 %
731 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_mathchardef:N}
732 % \begin{syntax}
733 % \cs{token_if_mathchardef_p:N} \meta{token} \\
734 % \cs{token_if_mathchardef:NTF} \meta{token} \Arg{true code} \Arg{false code}
735 % \end{syntax}
736 % Tests if the \meta{token} is defined to be a mathchardef.
737 % \end{function}
738 %
739 % \begin{function}[EXP,pTF, added=2020-10-27]{\token_if_font_selection:N}
740 % \begin{syntax}
741 % \cs{token_if_font_selection_p:N} \meta{token} \\
742 % \cs{token_if_font_selection:NTF} \meta{token} \Arg{true code} \Arg{false code}
743 % \end{syntax}
744 % Tests if the \meta{token} is defined to be a font selection command.
745 % \end{function}
746 %
747 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_dim_register:N}
748 % \begin{syntax}
749 % \cs{token_if_dim_register_p:N} \meta{token} \\
750 % \cs{token_if_dim_register:NTF} \meta{token} \Arg{true code} \Arg{false code}
751 % \end{syntax}
752 % Tests if the \meta{token} is defined to be a dimension register.
753 % \end{function}
754 %
755 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_int_register:N}
756 % \begin{syntax}
757 % \cs{token_if_int_register_p:N} \meta{token} \\
758 % \cs{token_if_int_register:NTF} \meta{token} \Arg{true code} \Arg{false code}
759 % \end{syntax}
760 % Tests if the \meta{token} is defined to be a integer register.
761 % \begin{texnote}
762 % Constant integers may be implemented as integer registers,
763 % \tn{chardef}s, or \tn{mathchardef}s depending on their value.
764 % \end{texnote}
765 % \end{function}
766 %
767 % \begin{function}[EXP,pTF, added=2012-02-15]{\token_if_muskip_register:N}
768 % \begin{syntax}
769 % \cs{token_if_muskip_register_p:N} \meta{token} \\
770 % \cs{token_if_muskip_register:NTF} \meta{token} \Arg{true code} \Arg{false code}
771 % \end{syntax}
772 % Tests if the \meta{token} is defined to be a muskip register.
773 % \end{function}
774 %
775 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_skip_register:N}
776 % \begin{syntax}
777 % \cs{token_if_skip_register_p:N} \meta{token} \\
778 % \cs{token_if_skip_register:NTF} \meta{token} \Arg{true code} \Arg{false code}
779 % \end{syntax}
780 % Tests if the \meta{token} is defined to be a skip register.
781 % \end{function}
782 %
783 % \begin{function}[EXP,pTF, updated=2012-01-20]{\token_if_toks_register:N}
784 % \begin{syntax}
785 % \cs{token_if_toks_register_p:N} \meta{token} \\
786 % \cs{token_if_toks_register:NTF} \meta{token} \Arg{true code} \Arg{false code}
787 % \end{syntax}
788 % Tests if the \meta{token} is defined to be a toks register
789 % (not used by \LaTeX3).
790 % \end{function}
791 %
792 % \begin{function}[updated = 2020-09-11, EXP,pTF]{\token_if_primitive:N}
793 % \begin{syntax}
794 % \cs{token_if_primitive_p:N} \meta{token} \\
795 % \cs{token_if_primitive:NTF} \meta{token} \Arg{true code} \Arg{false code}
796 % \end{syntax}
797 % Tests if the \meta{token} is an engine primitive. In \LuaTeX{}
798 % this includes primitive-like commands defined using |token.set_lua|.
799 % \end{function}
800 %
801 % \begin{function}[added = 2020-12-03, EXP, noTF]
802 % {\token_case_catcode:Nn, \token_case_charcode:Nn, \token_case_meaning:Nn}
803 % \begin{syntax}
804 % \cs{token_case_meaning:NnTF} \meta{test token} \\
805 % ~~"{" \\
806 % ~~~~\meta{token case_1} \Arg{code case_1} \\
807 % ~~~~\meta{token case_2} \Arg{code case_2} \\
808 % ~~~~\ldots \\
809 % ~~~~\meta{token case_n} \Arg{code case_n} \\
810 % ~~"}" \\
811 % ~~\Arg{true code}
812 % ~~\Arg{false code}
813 % \end{syntax}
814 % This function compares the \meta{test token} in turn with each of
815 % the \meta{token case}s. If the two are equal (as described for
816 % \cs{token_if_eq_catcode:NNTF}, \cs{token_if_eq_charcode:NNTF} and
817 % \cs{token_if_eq_meaning:NNTF}, respectively) then the associated
818 % \meta{code} is left in the input stream and other cases are
819 % discarded. If any of the cases are matched, the \meta{true code} is
820 % also inserted into the input stream (after the code for the
821 % appropriate case), while if none match then the \meta{false code} is
822 % inserted. The functions \cs{token_case_catcode:Nn},
823 % \cs{token_case_charcode:Nn}, and \cs{token_case_meaning:Nn}, which
824 % do nothing if there is no match, are also available.
825 % \end{function}
826 %
827 % \section{Peeking ahead at the next token}
828 %
829 % There is often a need to look ahead at the next token in the input
830 % stream while leaving it in place. This is handled using the
831 % \enquote{peek} functions. The generic \cs{peek_after:Nw} is provided
832 % along with a family of predefined tests for common cases. Peeking
833 % ahead does \emph{not} skip spaces: rather, \cs{peek_remove_spaces:n}
834 % should be used. In addition, using \cs{peek_analysis_map_inline:n},
835 % one can map through the following tokens in the input stream and
836 % repeatedly perform some tests.
837 %
838 % \begin{function}{\peek_after:Nw}
839 % \begin{syntax}
840 % \cs{peek_after:Nw} \meta{function} \meta{token}
841 % \end{syntax}
842 % Locally sets the test variable \cs{l_peek_token} equal to \meta{token}
843 % (as an implicit token, \emph{not} as a token list), and then
844 % expands the \meta{function}. The \meta{token} remains in
845 % the input stream as the next item after the \meta{function}.
846 % The \meta{token} here may be \verb*| |, |{| or |}| (assuming
847 % normal \TeX{} category codes), i.e., it is not necessarily the
848 % next argument which would be grabbed by a normal function.
849 % \end{function}
850 %
851 % \begin{function}{\peek_gafter:Nw}
852 % \begin{syntax}
853 % \cs{peek_gafter:Nw} \meta{function} \meta{token}
854 % \end{syntax}
855 % Globally sets the test variable \cs{g_peek_token} equal to \meta{token}
856 % (as an implicit token, \emph{not} as a token list), and then
857 % expands the \meta{function}. The \meta{token} remains in
858 % the input stream as the next item after the \meta{function}.
859 % The \meta{token} here may be \verb*| |, |{| or |}| (assuming
860 % normal \TeX{} category codes), i.e., it is not necessarily the
861 % next argument which would be grabbed by a normal function.
862 % \end{function}
863 %
864 % \begin{variable}{\l_peek_token}
865 % Token set by \cs{peek_after:Nw} and available for testing
866 % as described above.
867 % \end{variable}
868 %
869 % \begin{variable}{\g_peek_token}
870 % Token set by \cs{peek_gafter:Nw} and available for testing
871 % as described above.
872 % \end{variable}
873 %
874 % \begin{function}[updated = 2012-12-20, TF]{\peek_catcode:N}
875 % \begin{syntax}
876 % \cs{peek_catcode:NTF} \meta{test token} \Arg{true code} \Arg{false code}
877 % \end{syntax}
878 % Tests if the next \meta{token} in the input stream has the same
879 % category code as the \meta{test token} (as defined by the test
880 % \cs{token_if_eq_catcode:NNTF}). Spaces are respected by the test
881 % and the \meta{token} is left in the input stream after
882 % the \meta{true code} or \meta{false code} (as appropriate to the
883 % result of the test).
884 % \end{function}
885 %
886 % \begin{function}[updated = 2012-12-20, TF]{\peek_catcode_remove:N}
887 % \begin{syntax}
888 % \cs{peek_catcode_remove:NTF} \meta{test token} \Arg{true code} \Arg{false code}
889 % \end{syntax}
890 % Tests if the next \meta{token} in the input stream has the same
891 % category code as the \meta{test token} (as defined by the test
892 % \cs{token_if_eq_catcode:NNTF}). Spaces are respected by the test
893 % and the \meta{token} is removed from the input stream if the
894 % test is true. The function then places either the
895 % \meta{true code} or \meta{false code} in the input stream (as
896 % appropriate to the result of the test).
897 % \end{function}
898 %
899 % \begin{function}[updated = 2012-12-20, TF]{\peek_charcode:N}
900 % \begin{syntax}
901 % \cs{peek_charcode:NTF} \meta{test token} \Arg{true code} \Arg{false code}
902 % \end{syntax}
903 % Tests if the next \meta{token} in the input stream has the same
904 % character code as the \meta{test token} (as defined by the test
905 % \cs{token_if_eq_charcode:NNTF}). Spaces are respected by the test
906 % and the \meta{token} is left in the input stream after
907 % the \meta{true code} or \meta{false code} (as appropriate to the
908 % result of the test).
909 % \end{function}
910 %
911 % \begin{function}[updated = 2012-12-20, TF]{\peek_charcode_remove:N}
912 % \begin{syntax}
913 % \cs{peek_charcode_remove:NTF} \meta{test token} \Arg{true code} \Arg{false code}
914 % \end{syntax}
915 % Tests if the next \meta{token} in the input stream has the same
916 % character code as the \meta{test token} (as defined by the test
917 % \cs{token_if_eq_charcode:NNTF}). Spaces are respected by the test
918 % and the \meta{token} is removed from the input stream if the
919 % test is true. The function then places either the
920 % \meta{true code} or \meta{false code} in the input stream (as
921 % appropriate to the result of the test).
922 % \end{function}
923 %
924 % \begin{function}[updated = 2011-07-02, TF]{\peek_meaning:N}
925 % \begin{syntax}
926 % \cs{peek_meaning:NTF} \meta{test token} \Arg{true code} \Arg{false code}
927 % \end{syntax}
928 % Tests if the next \meta{token} in the input stream has the same
929 % meaning as the \meta{test token} (as defined by the test
930 % \cs{token_if_eq_meaning:NNTF}). Spaces are respected by the test
931 % and the \meta{token} is left in the input stream after
932 % the \meta{true code} or \meta{false code} (as appropriate to the
933 % result of the test).
934 % \end{function}
935 %
936 % \begin{function}[updated = 2011-07-02, TF]{\peek_meaning_remove:N}
937 % \begin{syntax}
938 % \cs{peek_meaning_remove:NTF} \meta{test token} \Arg{true code} \Arg{false code}
939 % \end{syntax}
940 % Tests if the next \meta{token} in the input stream has the same
941 % meaning as the \meta{test token} (as defined by the test
942 % \cs{token_if_eq_meaning:NNTF}). Spaces are respected by the test
943 % and the \meta{token} is removed from the input stream if the
944 % test is true. The function then places either the
945 % \meta{true code} or \meta{false code} in the input stream (as
946 % appropriate to the result of the test).
947 % \end{function}
948 %
949 % \begin{function}[added = 2018-10-01]{\peek_remove_spaces:n}
950 % \begin{syntax}
951 % \cs{peek_remove_spaces:n} \Arg{code}
952 % \end{syntax}
953 % Peeks ahead and detect if the following token is a space (category code
954 % $10$ and character code $32$). If so, removes the token and checks the
955 % next token. Once a non-space token is found, the \meta{code} will be
956 % inserted into the input stream. Typically this will contain a \texttt{peek}
957 % operation, but this is not required.
958 % \end{function}
959 %
960 % \begin{function}[added = 2022-01-10]{\peek_remove_filler:n}
961 % \begin{syntax}
962 % \cs{peek_remove_filler:n} \Arg{code}
963 % \end{syntax}
964 % Peeks ahead and detect if the following token is a space (category code
965 % $10$) or has meaning equal to \cs{scan_stop:}. If so,
966 % removes the token and checks the next token. If neither of these cases
967 % apply, expands the next token using \texttt{f}-type expansion, then checks
968 % the resulting leading token in the same way. If after expansion the next
969 % token is neither of the two test cases, the \meta{code} will be inserted
970 % into the input stream. Typically this will contain a \texttt{peek}
971 % operation, but this is not required.
972 % \begin{texnote}
973 % This is essentially a macro-based implementation of how \TeX{} handles
974 % the search for a left brace after for example \tn{everypar}, except that
975 % any non-expandable token cleanly ends the \meta{filler} (i.e., it does not
976 % lead to a \TeX{} error).
977 %
978 % In contrast to \TeX{}'s filler removal, a construct
979 % \verb|\exp_not:N \foo| will be treated in the same way as \verb|\foo|.
980 % \end{texnote}
981 % \end{function}
982 %
983 % \begin{function}[TF, updated = 2012-12-20]{\peek_N_type:}
984 % \begin{syntax}
985 % \cs{peek_N_type:TF} \Arg{true code} \Arg{false code}
986 % \end{syntax}
987 % Tests if the next \meta{token} in the input stream can be safely
988 % grabbed as an \texttt{N}-type argument. The test is \meta{false}
989 % if the next \meta{token} is either an explicit or implicit
990 % begin-group or end-group token (with any character code), or
991 % an explicit or implicit space character (with character code $32$
992 % and category code $10$), or an outer token (never used in \LaTeX3)
993 % and \meta{true} in all other cases.
994 % Note that a \meta{true} result ensures that the next \meta{token} is
995 % a valid \texttt{N}-type argument. However, if the next \meta{token}
996 % is for instance \cs{c_space_token}, the test takes the
997 % \meta{false} branch, even though the next \meta{token} is in fact
998 % a valid \texttt{N}-type argument. The \meta{token} is left
999 % in the input stream after the \meta{true code} or \meta{false code}
1000 % (as appropriate to the result of the test).
1001 % \end{function}
1002 %
1003 % \begin{function}[added = 2020-12-03, updated = 2024-02-07]
1004 % {\peek_analysis_map_inline:n}
1005 % \begin{syntax}
1006 % \cs{peek_analysis_map_inline:n} \Arg{inline function}
1007 % \end{syntax}
1008 % Repeatedly removes one \meta{token} from the input stream and
1009 % applies the \meta{inline function} to it, until
1010 % \cs{peek_analysis_map_break:} is called. The \meta{inline function}
1011 % receives three arguments for each \meta{token} in the input stream:
1012 % \begin{itemize}
1013 % \item \meta{tokens}, which both \texttt{o}-expand and
1014 % \texttt{e}/\texttt{x}-expand to the \meta{token}. The detailed form of
1015 % \meta{tokens} may change in later releases.
1016 % \item \meta{char code}, a decimal representation of the character
1017 % code of the \meta{token}, $-1$ if it is a control sequence.
1018 % \item \meta{catcode}, a capital hexadecimal digit which denotes the
1019 % category code of the \meta{token} (0:~control sequence,
1020 % 1:~begin-group, 2:~end-group, 3:~math shift, 4:~alignment tab,
1021 % 6:~parameter, 7:~superscript, 8:~subscript, A:~space, B:~letter,
1022 % C:~other, D:~active). This can be converted to an integer by
1023 % writing |"|\meta{catcode}.
1024 % \end{itemize}
1025 % These arguments are the same as for \cs{tl_analysis_map_inline:nn}
1026 % defined in \pkg{l3tl-analysis}. The \meta{char code} and
1027 % \meta{catcode} do not take the meaning of a control sequence or
1028 % active character into account: for instance, upon encountering the
1029 % token \cs{c_group_begin_token} in the input stream,
1030 % \cs{peek_analysis_map_inline:n} calls the \meta{inline function}
1031 % with |#1| being \cs{exp_not:n} |{| \cs{c_group_begin_token} |}|
1032 % (with the current implementation),
1033 % |#2|~being~$-1$, and
1034 % |#3|~being~$0$, as for any other control sequence. In contrast,
1035 % upon encountering an explicit begin-group token~|{|, % ^^A |}|
1036 % the \meta{inline function} is called with arguments
1037 % \cs{exp_after:wN} |{| \cs{if_false:} |}| \cs{fi:}, $123$ and~$1$.
1038 %
1039 % The mapping is done at the current group level, i.e., any
1040 % local assignments made by the \meta{inline function} remain in
1041 % effect after the loop. Within the code, \cs{l_peek_token} is set
1042 % equal (as a token, not a token list) to the token under
1043 % consideration.
1044 %
1045 % Peek functions cannot be used within this mapping function (nor
1046 % other mapping functions) since the input stream contains trailing
1047 % material necessary for the functioning of the loop.
1048 % \begin{texnote}
1049 % In case the input stream has not yet been tokenized (converted
1050 % from characters to tokens), characters are tokenized one by one as
1051 % needed by \cs{peek_analysis_map_inline:n} using the current
1052 % category code régime.
1053 % \end{texnote}
1054 % \end{function}
1055 %
1056 % \begin{function}[added = 2020-12-03]
1057 % {\peek_analysis_map_break:, \peek_analysis_map_break:n}
1058 % \begin{syntax}
1059 % \cs{peek_analysis_map_inline:n}
1060 % |{| \dots{} \cs{peek_analysis_map_break:n} \Arg{code} |}|
1061 % \end{syntax}
1062 % Stops the \cs{peek_analysis_map_inline:n} loop from seeking more
1063 % tokens, and inserts \meta{code} in the input stream (empty for
1064 % \cs{peek_analysis_map_break:}).
1065 % \end{function}
1066 %
1067 % \begin{function}[added = 2020-12-03, TF]{\peek_regex:n, \peek_regex:N}
1068 % \begin{syntax}
1069 % \cs{peek_regex:nTF} \Arg{regex} \Arg{true code} \Arg{false code}
1070 % \end{syntax}
1071 % Tests if the \meta{tokens} that follow in the input stream match the
1072 % \meta{regular expression}. Any \meta{tokens} that have been read
1073 % are left in the input stream after the \meta{true code} or
1074 % \meta{false code} (as appropriate to the result of the test). See
1075 % \pkg{l3regex} for documentation of the syntax of regular
1076 % expressions. The \meta{regular expression} is implicitly anchored
1077 % at the start, so for instance \cs{peek_regex:nTF}~|{|~|a|~|}| is
1078 % essentially equivalent to \cs{peek_charcode:NTF}~|a|.
1079 % \begin{texnote}
1080 % Implicit character tokens are correctly considered by
1081 % \cs{peek_regex:nTF} as control sequences, while functions that
1082 % inspect individual tokens (for instance \cs{peek_charcode:NTF})
1083 % only take into account their meaning.
1084 %
1085 % The \cs{peek_regex:nTF} function only inspects as few tokens as
1086 % necessary to determine whether the regular expression matches.
1087 % For instance \cs{peek_regex:nTF} \verb"{ abc | [a-z] }" |{ } { }|
1088 % |abc| will only inspect the first token~|a| even though the first
1089 % branch |abc| of the alternative is preferred in functions such as
1090 % \cs{peek_regex_remove_once:nTF}. This may have an effect on
1091 % tokenization if the input stream has not yet been tokenized and
1092 % category codes are changed.
1093 % \end{texnote}
1094 % \end{function}
1095 %
1096 % \begin{function}[added = 2020-12-03, TF]
1097 % {\peek_regex_remove_once:n, \peek_regex_remove_once:N}
1098 % \begin{syntax}
1099 % \cs{peek_regex_remove_once:nTF} \Arg{regex} \Arg{true code} \Arg{false code}
1100 % \end{syntax}
1101 % Tests if the \meta{tokens} that follow in the input stream match the
1102 % \meta{regex}. If the test is true, the \meta{tokens} are removed
1103 % from the input stream and the \meta{true code} is inserted, while if
1104 % the test is false, the \meta{false code} is inserted followed by the
1105 % \meta{tokens} that were originally in the input stream.
1106 % See \pkg{l3regex} for documentation of the syntax of
1107 % regular expressions. The \meta{regular expression} is implicitly
1108 % anchored at the start, so for instance
1109 % \cs{peek_regex_remove_once:nTF}~|{|~|a|~|}| is essentially equivalent to
1110 % \cs{peek_charcode_remove:NTF}~|a|.
1111 % \begin{texnote}
1112 % Implicit character tokens are correctly considered by
1113 % \cs{peek_regex_remove_once:nTF} as control sequences, while functions
1114 % that inspect individual tokens (for instance
1115 % \cs{peek_charcode:NTF}) only take into account their meaning.
1116 % \end{texnote}
1117 % \end{function}
1118 %
1119 % \begin{function}[added = 2020-12-03, noTF]
1120 % {\peek_regex_replace_once:nn, \peek_regex_replace_once:Nn}
1121 % \begin{syntax}
1122 % \cs{peek_regex_replace_once:nnTF} \Arg{regex} \Arg{replacement} \Arg{true code} \Arg{false code}
1123 % \end{syntax}
1124 % If the \meta{tokens} that follow in the input stream match the
1125 % \meta{regex}, replaces them according to the \meta{replacement} as
1126 % for \cs{regex_replace_once:nnN}, and leaves the result in the input
1127 % stream, after the \meta{true code}. Otherwise, leaves \meta{false
1128 % code} followed by the \meta{tokens} that were originally in the
1129 % input stream, with no modifications. See \pkg{l3regex} for
1130 % documentation of the syntax of regular expressions and of the
1131 % \meta{replacement}: for instance |\0| in the \meta{replacement} is
1132 % replaced by the tokens that were matched in the input stream. The
1133 % \meta{regular expression} is implicitly anchored at the start. In
1134 % contrast to \cs{regex_replace_once:nnN}, no error arises if the
1135 % \meta{replacement} leads to an unbalanced token list: the tokens are
1136 % inserted into the input stream without issue.
1137 % \begin{texnote}
1138 % Implicit character tokens are correctly considered by
1139 % \cs{peek_regex_replace_once:nnTF} as control sequences, while
1140 % functions that inspect individual tokens (for instance
1141 % \cs{peek_charcode:NTF}) only take into account their meaning.
1142 % \end{texnote}
1143 % \end{function}
1144 %
1145 % \section{Description of all possible tokens}
1146 % \label{sec:l3token:all-tokens}
1147 %
1148 % Let us end by reviewing every case that a given token can fall into.
1149 % This section is quite technical and some details are only meant for
1150 % completeness. We distinguish the meaning of the token, which controls
1151 % the expansion of the token and its effect on \TeX{}'s state, and its
1152 % shape, which is used when comparing token lists such as for delimited
1153 % arguments. Two tokens of the same shape must have the same meaning,
1154 % but the converse does not hold.
1155 %
1156 % A token has one of the following shapes.
1157 % \begin{itemize}
1158 % \item A control sequence, characterized by the sequence of
1159 % characters that constitute its name: for instance, \cs{use:n} is a
1160 % five-letter control sequence.
1161 % \item An active character token, characterized by its character code
1162 % (between $0$ and $1114111$ for \LuaTeX{} and \XeTeX{} and less for
1163 % other engines) and category code~$13$.
1164 % \item A character token, characterized by its character code and
1165 % category code (one of $1$, $2$, $3$, $4$, $6$, $7$, $8$, $10$,
1166 % $11$ or~$12$ whose meaning is described below).
1167 % \end{itemize}
1168 % There are also a few internal tokens. The following list may be
1169 % incomplete in some engines.
1170 % \begin{itemize}
1171 % \item Expanding \tn{the}\tn{font} results in a token that looks
1172 % identical to the command that was used to select the current font
1173 % (such as \tn{tenrm}) but it differs from it in shape.
1174 % \item A \enquote{frozen} |\relax|, which differs from the primitive in
1175 % shape (but has the same meaning), is inserted when the closing \tn{fi} of a
1176 % conditional is encountered before the conditional is evaluated.
1177 % \item Expanding \tn{noexpand} \meta{token} (when the \meta{token} is
1178 % expandable) results in an internal token, displayed (temporarily)
1179 % as \cs[module = {}]{notexpanded: \meta{token}}, whose shape coincides with the
1180 % \meta{token} and whose meaning differs from \tn{relax}.
1181 % \item An |\outer endtemplate:| can be encountered when peeking ahead
1182 % at the next token; this expands to another internal token,
1183 % |end of alignment template|.
1184 % \item Tricky programming might access a frozen |\endwrite|.
1185 % \item Some frozen tokens can only be accessed in interactive
1186 % sessions: |\cr|, |\right|, |\endgroup|, |\fi|, |\inaccessible|.
1187 % \item In \LuaTeX{}, there is also the strange case of \enquote{bytes}
1188 % |^^^^^^1100|$xy$ where $x,y$ are any two lowercase hexadecimal
1189 % digits, so that the hexadecimal number ranges from
1190 % |"|$11\,0000=1\,114\,112$ to~|"|$110\,0\mathrm{ff}=1\,114\,367$. These are
1191 % used to output individual bytes to files, rather than UTF-8. For
1192 % the purposes of token comparisons they behave like non-expandable
1193 % primitive control sequences (\emph{not characters}) whose
1194 % \tn{meaning} is \verb*|the character | followed by the given byte.
1195 % If this byte is in the range |80|--|ff| this gives an ``invalid
1196 % utf-8 sequence'' error: applying \cs{token_to_str:N} or
1197 % \cs{token_to_meaning:N} to these tokens is unsafe. Unfortunately,
1198 % they don't seem to be detectable safely by any means except perhaps
1199 % Lua code.
1200 % \end{itemize}
1201 %
1202 % The meaning of a (non-active) character token is fixed by its category
1203 % code (and character code) and cannot be changed. We call these
1204 % tokens \emph{explicit} character tokens. Category codes that a
1205 % character token can have are listed below by giving a sample output of
1206 % the \TeX{} primitive \tn{meaning}, together with their \LaTeX3 names
1207 % and most common example:
1208 % \begin{itemize}
1209 % \item[1] begin-group character (|group_begin|, often |{|),
1210 % \item[2] end-group character (|group_end|, often |}|),
1211 % \item[3] math shift character (|math_toggle|, often |$|), %^^A $
1212 % \item[4] alignment tab character (|alignment|, often |&|),
1213 % \item[6] macro parameter character (|parameter|, often |#|),
1214 % \item[7] superscript character (|math_superscript|, often |^|),
1215 % \item[8] subscript character (|math_subscript|, often |_|),
1216 % \item[10] blank space (|space|, often character code~$32$),
1217 % \item[11] the letter (|letter|, such as |A|),
1218 % \item[12] the character (|other|, such as |0|).
1219 % \end{itemize}
1220 % Category code~$13$ (|active|) is discussed below. Input characters
1221 % can also have several other category codes which do not lead to
1222 % character tokens for later processing: $0$~(|escape|),
1223 % $5$~(|end_line|), $9$~(|ignore|), $14$~(|comment|), and
1224 % $15$~(|invalid|).
1225 %
1226 % The meaning of a control sequence or active character can be identical
1227 % to that of any character token listed above (with any character code),
1228 % and we call such tokens \emph{implicit} character tokens. The
1229 % meaning is otherwise in the following list:
1230 % \begin{itemize}
1231 % \item a macro, used in \LaTeX3 for most functions and some variables
1232 % (|tl|, |fp|, |seq|, \ldots{}),
1233 % \item a primitive such as \tn{def} or \tn{topmark}, used in \LaTeX3
1234 % for some functions,
1235 % \item a register such as \tn{count}|123|, used in \LaTeX3{} for the
1236 % implementation of some variables (|int|, |dim|, \ldots{}),
1237 % \item a constant integer such as \tn{char}|"56| or \tn{mathchar}|"121|,
1238 % \item a font selection command,
1239 % \item undefined.
1240 % \end{itemize}
1241 % Macros can be \tn{protected} or not, \tn{long} or not (the opposite of
1242 % what \LaTeX3 calls |nopar|), and \tn{outer} or not (unused in
1243 % \LaTeX3). Their \tn{meaning} takes the form
1244 % \begin{quote}
1245 % \meta{prefix} |macro:|\meta{argument}|->|\meta{replacement}
1246 % \end{quote}
1247 % where \meta{prefix} is among \tn{protected}\tn{long}\tn{outer},
1248 % \meta{argument} describes parameters that the macro expects, such as
1249 % |#1#2#3|, and \meta{replacement} describes how the parameters are
1250 % manipulated, such as~|\int_eval:n{#2+#1*#3}|.
1251 %
1252 % Now is perhaps a good time to mention some subtleties relating to
1253 % tokens with category code $10$ (space). Any input character with this
1254 % category code (normally, space and tab characters) becomes a normal
1255 % space, with character code~$32$ and category code~$10$.
1256 %
1257 % When a macro takes an undelimited argument, explicit space characters
1258 % (with character code $32$ and category code $10$) are ignored. If the
1259 % following token is an explicit character token with category code $1$
1260 % (begin-group) and an arbitrary character code, then \TeX{} scans ahead
1261 % to obtain an equal number of explicit character tokens with category
1262 % code $1$ (begin-group) and $2$ (end-group), and the resulting list of
1263 % tokens (with outer braces removed) becomes the argument. Otherwise, a
1264 % single token is taken as the argument for the macro: we call such
1265 % single tokens \enquote{N-type}, as they are suitable to be used as an
1266 % argument for a function with the signature~\texttt{:N}.
1267 %
1268 % When a macro takes a delimited argument \TeX{} scans ahead until
1269 % finding the delimiter (outside any pairs of begin-group/end-group
1270 % explicit characters), and the resulting list of tokens (with outer
1271 % braces removed) becomes the argument. Note that explicit space
1272 % characters at the start of the argument are \emph{not} ignored in this
1273 % case (and they prevent brace-stripping).
1274 %
1275 % \end{documentation}
1276 %
1277 % \begin{implementation}
1278 %
1279 % \section{\pkg{l3token} implementation}
1280 %
1281 % \begin{macrocode}
1282 %<@@=char>
1283 % \end{macrocode}
1284 % \begin{macrocode}
1285 %<*code>
1286 % \end{macrocode}
1287 %
1288 % \subsection{Internal auxiliaries}
1289 %
1290 % \begin{variable}{\s_@@_stop}
1291 % Internal scan mark.
1292 % \begin{macrocode}
1293 \scan_new:N \s_@@_stop
1294 % \end{macrocode}
1295 % \end{variable}
1296 %
1297 % \begin{variable}{\q_@@_no_value}
1298 % Internal recursion quarks.
1299 % \begin{macrocode}
1300 \quark_new:N \q_@@_no_value
1301 % \end{macrocode}
1302 % \end{variable}
1303 %
1304 % \begin{macro}[pTF]{\@@_quark_if_no_value:N}
1305 % Functions to query recursion quarks.
1306 % \begin{macrocode}
1307 \__kernel_quark_new_conditional:Nn \@@_quark_if_no_value:N { TF }
1308 % \end{macrocode}
1309 % \end{macro}
1310 %
1311 % \subsection{Manipulating and interrogating character tokens}
1312 %
1313 % \begin{macro}{\char_set_catcode:nn}
1314 % \begin{macro}{\char_value_catcode:n}
1315 % \begin{macro}{\char_show_value_catcode:n}
1316 % Simple wrappers around the primitives.
1317 % \begin{macrocode}
1318 \cs_new_protected:Npn \char_set_catcode:nn #1#2
1319 { \tex_catcode:D \int_eval:n {#1} = \int_eval:n {#2} \exp_stop_f: }
1320 \cs_new:Npn \char_value_catcode:n #1
1321 { \tex_the:D \tex_catcode:D \int_eval:n {#1} \exp_stop_f: }
1322 \cs_new_protected:Npn \char_show_value_catcode:n #1
1323 { \exp_args:Nf \tl_show:n { \char_value_catcode:n {#1} } }
1324 % \end{macrocode}
1325 % \end{macro}
1326 % \end{macro}
1327 % \end{macro}
1328 %
1329 % \begin{macro}
1330 % {
1331 % \char_set_catcode_escape:N ,
1332 % \char_set_catcode_group_begin:N ,
1333 % \char_set_catcode_group_end:N ,
1334 % \char_set_catcode_math_toggle:N ,
1335 % \char_set_catcode_alignment:N ,
1336 % \char_set_catcode_end_line:N ,
1337 % \char_set_catcode_parameter:N ,
1338 % \char_set_catcode_math_superscript:N ,
1339 % \char_set_catcode_math_subscript:N ,
1340 % \char_set_catcode_ignore:N ,
1341 % \char_set_catcode_space:N ,
1342 % \char_set_catcode_letter:N ,
1343 % \char_set_catcode_other:N ,
1344 % \char_set_catcode_active:N ,
1345 % \char_set_catcode_comment:N ,
1346 % \char_set_catcode_invalid:N
1347 % }
1348 % \begin{macrocode}
1349 \cs_new_protected:Npn \char_set_catcode_escape:N #1
1350 { \char_set_catcode:nn { `#1 } { 0 } }
1351 \cs_new_protected:Npn \char_set_catcode_group_begin:N #1
1352 { \char_set_catcode:nn { `#1 } { 1 } }
1353 \cs_new_protected:Npn \char_set_catcode_group_end:N #1
1354 { \char_set_catcode:nn { `#1 } { 2 } }
1355 \cs_new_protected:Npn \char_set_catcode_math_toggle:N #1
1356 { \char_set_catcode:nn { `#1 } { 3 } }
1357 \cs_new_protected:Npn \char_set_catcode_alignment:N #1
1358 { \char_set_catcode:nn { `#1 } { 4 } }
1359 \cs_new_protected:Npn \char_set_catcode_end_line:N #1
1360 { \char_set_catcode:nn { `#1 } { 5 } }
1361 \cs_new_protected:Npn \char_set_catcode_parameter:N #1
1362 { \char_set_catcode:nn { `#1 } { 6 } }
1363 \cs_new_protected:Npn \char_set_catcode_math_superscript:N #1
1364 { \char_set_catcode:nn { `#1 } { 7 } }
1365 \cs_new_protected:Npn \char_set_catcode_math_subscript:N #1
1366 { \char_set_catcode:nn { `#1 } { 8 } }
1367 \cs_new_protected:Npn \char_set_catcode_ignore:N #1
1368 { \char_set_catcode:nn { `#1 } { 9 } }
1369 \cs_new_protected:Npn \char_set_catcode_space:N #1
1370 { \char_set_catcode:nn { `#1 } { 10 } }
1371 \cs_new_protected:Npn \char_set_catcode_letter:N #1
1372 { \char_set_catcode:nn { `#1 } { 11 } }
1373 \cs_new_protected:Npn \char_set_catcode_other:N #1
1374 { \char_set_catcode:nn { `#1 } { 12 } }
1375 \cs_new_protected:Npn \char_set_catcode_active:N #1
1376 { \char_set_catcode:nn { `#1 } { 13 } }
1377 \cs_new_protected:Npn \char_set_catcode_comment:N #1
1378 { \char_set_catcode:nn { `#1 } { 14 } }
1379 \cs_new_protected:Npn \char_set_catcode_invalid:N #1
1380 { \char_set_catcode:nn { `#1 } { 15 } }
1381 % \end{macrocode}
1382 % \end{macro}
1383 %
1384 % \begin{macro}
1385 % {
1386 % \char_set_catcode_escape:n ,
1387 % \char_set_catcode_group_begin:n ,
1388 % \char_set_catcode_group_end:n ,
1389 % \char_set_catcode_math_toggle:n ,
1390 % \char_set_catcode_alignment:n ,
1391 % \char_set_catcode_end_line:n ,
1392 % \char_set_catcode_parameter:n ,
1393 % \char_set_catcode_math_superscript:n ,
1394 % \char_set_catcode_math_subscript:n ,
1395 % \char_set_catcode_ignore:n ,
1396 % \char_set_catcode_space:n ,
1397 % \char_set_catcode_letter:n ,
1398 % \char_set_catcode_other:n ,
1399 % \char_set_catcode_active:n ,
1400 % \char_set_catcode_comment:n ,
1401 % \char_set_catcode_invalid:n
1402 % }
1403 % \begin{macrocode}
1404 \cs_new_protected:Npn \char_set_catcode_escape:n #1
1405 { \char_set_catcode:nn {#1} { 0 } }
1406 \cs_new_protected:Npn \char_set_catcode_group_begin:n #1
1407 { \char_set_catcode:nn {#1} { 1 } }
1408 \cs_new_protected:Npn \char_set_catcode_group_end:n #1
1409 { \char_set_catcode:nn {#1} { 2 } }
1410 \cs_new_protected:Npn \char_set_catcode_math_toggle:n #1
1411 { \char_set_catcode:nn {#1} { 3 } }
1412 \cs_new_protected:Npn \char_set_catcode_alignment:n #1
1413 { \char_set_catcode:nn {#1} { 4 } }
1414 \cs_new_protected:Npn \char_set_catcode_end_line:n #1
1415 { \char_set_catcode:nn {#1} { 5 } }
1416 \cs_new_protected:Npn \char_set_catcode_parameter:n #1
1417 { \char_set_catcode:nn {#1} { 6 } }
1418 \cs_new_protected:Npn \char_set_catcode_math_superscript:n #1
1419 { \char_set_catcode:nn {#1} { 7 } }
1420 \cs_new_protected:Npn \char_set_catcode_math_subscript:n #1
1421 { \char_set_catcode:nn {#1} { 8 } }
1422 \cs_new_protected:Npn \char_set_catcode_ignore:n #1
1423 { \char_set_catcode:nn {#1} { 9 } }
1424 \cs_new_protected:Npn \char_set_catcode_space:n #1
1425 { \char_set_catcode:nn {#1} { 10 } }
1426 \cs_new_protected:Npn \char_set_catcode_letter:n #1
1427 { \char_set_catcode:nn {#1} { 11 } }
1428 \cs_new_protected:Npn \char_set_catcode_other:n #1
1429 { \char_set_catcode:nn {#1} { 12 } }
1430 \cs_new_protected:Npn \char_set_catcode_active:n #1
1431 { \char_set_catcode:nn {#1} { 13 } }
1432 \cs_new_protected:Npn \char_set_catcode_comment:n #1
1433 { \char_set_catcode:nn {#1} { 14 } }
1434 \cs_new_protected:Npn \char_set_catcode_invalid:n #1
1435 { \char_set_catcode:nn {#1} { 15 } }
1436 % \end{macrocode}
1437 % \end{macro}
1438 %
1439 % \begin{macro}{\char_set_mathcode:nn}
1440 % \begin{macro}{\char_value_mathcode:n}
1441 % \begin{macro}{\char_show_value_mathcode:n}
1442 % \begin{macro}{\char_set_lccode:nn}
1443 % \begin{macro}{\char_value_lccode:n}
1444 % \begin{macro}{\char_show_value_lccode:n}
1445 % \begin{macro}{\char_set_uccode:nn}
1446 % \begin{macro}{\char_value_uccode:n}
1447 % \begin{macro}{\char_show_value_uccode:n}
1448 % \begin{macro}{\char_set_sfcode:nn}
1449 % \begin{macro}{\char_value_sfcode:n}
1450 % \begin{macro}{\char_show_value_sfcode:n}
1451 % Pretty repetitive, but necessary!
1452 % \begin{macrocode}
1453 \cs_new_protected:Npn \char_set_mathcode:nn #1#2
1454 { \tex_mathcode:D \int_eval:n {#1} = \int_eval:n {#2} \exp_stop_f: }
1455 \cs_new:Npn \char_value_mathcode:n #1
1456 { \tex_the:D \tex_mathcode:D \int_eval:n {#1} \exp_stop_f: }
1457 \cs_new_protected:Npn \char_show_value_mathcode:n #1
1458 { \exp_args:Nf \tl_show:n { \char_value_mathcode:n {#1} } }
1459 \cs_new_protected:Npn \char_set_lccode:nn #1#2
1460 { \tex_lccode:D \int_eval:n {#1} = \int_eval:n {#2} \exp_stop_f: }
1461 \cs_new:Npn \char_value_lccode:n #1
1462 { \tex_the:D \tex_lccode:D \int_eval:n {#1} \exp_stop_f: }
1463 \cs_new_protected:Npn \char_show_value_lccode:n #1
1464 { \exp_args:Nf \tl_show:n { \char_value_lccode:n {#1} } }
1465 \cs_new_protected:Npn \char_set_uccode:nn #1#2
1466 { \tex_uccode:D \int_eval:n {#1} = \int_eval:n {#2} \exp_stop_f: }
1467 \cs_new:Npn \char_value_uccode:n #1
1468 { \tex_the:D \tex_uccode:D \int_eval:n {#1} \exp_stop_f: }
1469 \cs_new_protected:Npn \char_show_value_uccode:n #1
1470 { \exp_args:Nf \tl_show:n { \char_value_uccode:n {#1} } }
1471 \cs_new_protected:Npn \char_set_sfcode:nn #1#2
1472 { \tex_sfcode:D \int_eval:n {#1} = \int_eval:n {#2} \exp_stop_f: }
1473 \cs_new:Npn \char_value_sfcode:n #1
1474 { \tex_the:D \tex_sfcode:D \int_eval:n {#1} \exp_stop_f: }
1475 \cs_new_protected:Npn \char_show_value_sfcode:n #1
1476 { \exp_args:Nf \tl_show:n { \char_value_sfcode:n {#1} } }
1477 % \end{macrocode}
1478 % \end{macro}
1479 % \end{macro}
1480 % \end{macro}
1481 % \end{macro}
1482 % \end{macro}
1483 % \end{macro}
1484 % \end{macro}
1485 % \end{macro}
1486 % \end{macro}
1487 % \end{macro}
1488 % \end{macro}
1489 % \end{macro}
1490 %
1491 % \begin{variable}{\l_char_active_seq, \l_char_special_seq}
1492 % Two sequences for dealing with special characters. The first is characters
1493 % which may be active, the second longer list is for \enquote{special}
1494 % characters more generally. Both lists are escaped so that for example
1495 % bulk code assignments can be carried out. In both cases, the order is
1496 % by \textsc{ascii} character code (as is done in for example
1497 % \cs{ExplSyntaxOn}).
1498 % \begin{macrocode}
1499 \seq_new:N \l_char_special_seq
1500 \seq_set_split:Nnn \l_char_special_seq { }
1501 { \ \" \# \$ \% \& \\ \^ \_ \{ \} \~ }
1502 \seq_new:N \l_char_active_seq
1503 \seq_set_split:Nnn \l_char_active_seq { }
1504 { \" \$ \& \^ \_ \~ }
1505 % \end{macrocode}
1506 % \end{variable}
1507 %
1508 % \subsection{Creating character tokens}
1509 %
1510 % \begin{macro}
1511 % {
1512 % \char_set_active_eq:NN, \char_gset_active_eq:NN,
1513 % \char_set_active_eq:Nc, \char_gset_active_eq:Nc,
1514 % \char_set_active_eq:nN, \char_gset_active_eq:nN,
1515 % \char_set_active_eq:nc, \char_gset_active_eq:nc
1516 % }
1517 % Four simple functions with very similar definitions, so set up using
1518 % an auxiliary.
1519 % These are similar to \LuaTeX{}'s \tn{letcharcode} primitive.
1520 % \begin{macrocode}
1521 \group_begin:
1522 \char_set_catcode_active:N \^^@
1523 \cs_set_protected:Npn \@@_tmp:nN #1#2
1524 {
1525 \cs_new_protected:cpn { #1 :nN } ##1
1526 {
1527 \group_begin:
1528 \char_set_lccode:nn { `\^^@ } { ##1 }
1529 \tex_lowercase:D { \group_end: #2 ^^@ }
1530 }
1531 \cs_new_protected:cpe { #1 :NN } ##1
1532 { \exp_not:c { #1 : nN } { `##1 } }
1533 }
1534 \@@_tmp:nN { char_set_active_eq } \cs_set_eq:NN
1535 \@@_tmp:nN { char_gset_active_eq } \cs_gset_eq:NN
1536 \group_end:
1537 \cs_generate_variant:Nn \char_set_active_eq:NN { Nc }
1538 \cs_generate_variant:Nn \char_gset_active_eq:NN { Nc }
1539 \cs_generate_variant:Nn \char_set_active_eq:nN { nc }
1540 \cs_generate_variant:Nn \char_gset_active_eq:nN { nc }
1541 % \end{macrocode}
1542 % \end{macro}
1543 %
1544 % \begin{macro}{\@@_int_to_roman:w}
1545 % For efficiency in 8-bit engines, we use the faster primitive approach
1546 % to making roman numerals.
1547 % \begin{macrocode}
1548 \cs_new_eq:NN \@@_int_to_roman:w \tex_romannumeral:D
1549 % \end{macrocode}
1550 % \end{macro}
1551 %
1552 % \begin{macro}{\@@_sep:}
1553 % \begin{macrocode}
1554 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
1555 % \end{macrocode}
1556 % \end{macro}
1557 %
1558 % \begin{macro}[EXP]{\char_generate:nn}
1559 % \begin{macro}[EXP]{\@@_generate_aux:nn}
1560 % \begin{macro}[EXP]{\@@_generate_aux:nnw, \@@_generate_auxii:nnw}
1561 % \begin{variable}{\l_@@_tmp_tl}
1562 % \begin{macro}[EXP]{\@@_generate_invalid_catcode:}
1563 % The aim here is to generate characters of (broadly) arbitrary category
1564 % code. Where possible, that is done using engine support (\XeTeX{},
1565 % \LuaTeX{}). There are though various issues which are covered below. At
1566 % the interface layer, turn the two arguments into integers up-front so
1567 % this is only done once.
1568 % \begin{macrocode}
1569 \cs_new:Npn \char_generate:nn #1#2
1570 {
1571 \exp:w \exp_after:wN \@@_generate_aux:w
1572 \int_value:w \int_eval:n {#1} \exp_after:wN \@@_sep:
1573 \int_value:w \int_eval:n {#2} \@@_sep:
1574 }
1575 % \end{macrocode}
1576 % Before doing any actual conversion, first some special case filtering.
1577 % Spaces are out here as \LuaTeX{} emulation only makes normal (charcode
1578 % $32$ spaces). However, |^^@| is filtered out separately as that can't be
1579 % done with macro emulation either, so is treated separately. That
1580 % done, hand off to the engine-dependent part.
1581 % \begin{macrocode}
1582 \cs_new:Npn \@@_generate_aux:w #1 \@@_sep: #2 \@@_sep:
1583 {
1584 \if_int_odd:w 0
1585 \if_int_compare:w #2 < 1 \exp_stop_f: 1 \fi:
1586 \if_int_compare:w #2 = 5 \exp_stop_f: 1 \fi:
1587 \if_int_compare:w #2 = 9 \exp_stop_f: 1 \fi:
1588 \if_int_compare:w #2 > 13 \exp_stop_f: 1 \fi: \exp_stop_f:
1589 \msg_expandable_error:nn { char }
1590 { invalid-catcode }
1591 \else:
1592 \if_int_odd:w 0
1593 \if_int_compare:w #1 < \c_zero_int 1 \fi:
1594 \if_int_compare:w #1 > \c_max_char_int 1 \fi: \exp_stop_f:
1595 \msg_expandable_error:nn { char }
1596 { out-of-range }
1597 \else:
1598 \if_int_compare:w #2#1 = 100 \exp_stop_f:
1599 \msg_expandable_error:nn { char } { null-space }
1600 \else:
1601 \@@_generate_aux:nnw {#1} {#2}
1602 \fi:
1603 \fi:
1604 \fi:
1605 \exp_end:
1606 }
1607 \tl_new:N \l_@@_tmp_tl
1608 % \end{macrocode}
1609 % Engine-dependent definitions are now needed for the implementation. Recent
1610 % (u)p\TeX{} and the Unicode engines \LuaTeX{} and \XeTeX{} have engine-level
1611 % support for expandable character creation. \pdfTeX{} and older (u)p\TeX{}
1612 % releases do not. The branching here if low-level to avoid fixing
1613 % the category code of the null character used in the false branch.
1614 % The final level is the basic definition at the engine level: the arguments
1615 % here are integers so there is no need to worry about them too much.
1616 % Older versions of \XeTeX{} cannot generate active characters so we filter
1617 % that:
1618 % at some future stage that may change: the slightly odd ordering of
1619 % auxiliaries reflects that.
1620 % \begin{macrocode}
1621 \group_begin:
1622 \char_set_catcode_active:N \^^L
1623 \cs_set:Npn ^^L { }
1624 \if_cs_exist:N \tex_Ucharcat:D
1625 \cs_new:Npn \@@_generate_aux:nnw #1#2#3 \exp_end:
1626 {
1627 #3
1628 \exp_after:wN \exp_end:
1629 \tex_Ucharcat:D #1 \exp_stop_f: #2 \exp_stop_f:
1630 }
1631 \else:
1632 % \end{macrocode}
1633 % For engines where \tn{Ucharcat} isn't available or emulated, we have
1634 % to work in macros, and cover only the $8$-bit range. The first stage is
1635 % to build up a |tl| containing |^^@| with each category code that can
1636 % be accessed in this way, with an error set up for the other cases. This
1637 % is all done such that it can be quickly accessed using a |\if_case:w|
1638 % low-level conditional. The list is done in reverse as this puts the case
1639 % of an active token \emph{first}: that's needed to cover the possibility
1640 % that it is \tn{outer}. Getting the braces into the list is done using
1641 % some standard \cs{if_false:} manipulation, while all of the \cs{exp_not:N}
1642 % are required as there is an expansion in the setup.
1643 % \begin{macrocode}
1644 \char_set_catcode_active:n { 0 }
1645 \tl_set:Nn \l_@@_tmp_tl { \exp_not:N ^^@ \exp_not:N \or: }
1646 \char_set_catcode_other:n { 0 }
1647 \tl_put_right:Nn \l_@@_tmp_tl { ^^@ \exp_not:N \or: }
1648 \char_set_catcode_letter:n { 0 }
1649 \tl_put_right:Nn \l_@@_tmp_tl { ^^@ \exp_not:N \or: }
1650 % \end{macrocode}
1651 % For making spaces, there needs to be an |o|-type expansion of a |\use:n|
1652 % (or some other tokenization) to avoid dropping the space.
1653 % \begin{macrocode}
1654 \tl_put_right:Nn \l_@@_tmp_tl { \use:n { ~ } \exp_not:N \or: }
1655 \tl_put_right:Nn \l_@@_tmp_tl { \exp_not:N \or: }
1656 \char_set_catcode_math_subscript:n { 0 }
1657 \tl_put_right:Nn \l_@@_tmp_tl { ^^@ \exp_not:N \or: }
1658 \char_set_catcode_math_superscript:n { 0 }
1659 \tl_put_right:Nn \l_@@_tmp_tl { ^^@ \exp_not:N \or: }
1660 \char_set_catcode_parameter:n { 0 }
1661 \tl_put_right:Nn \l_@@_tmp_tl { ^^@ \exp_not:N \or: }
1662 \tl_put_right:Nn \l_@@_tmp_tl { { \if_false: } \fi: \exp_not:N \or: }
1663 \char_set_catcode_alignment:n { 0 }
1664 \tl_put_right:Nn \l_@@_tmp_tl { ^^@ \exp_not:N \or: }
1665 \char_set_catcode_math_toggle:n { 0 }
1666 \tl_put_right:Nn \l_@@_tmp_tl { ^^@ \exp_not:N \or: }
1667 \char_set_catcode_group_end:n { 0 }
1668 \tl_put_right:Nn \l_@@_tmp_tl { \if_false: { \fi: ^^@ \exp_not:N \or: } % }
1669 \char_set_catcode_group_begin:n { 0 } % {
1670 \tl_put_right:Nn \l_@@_tmp_tl { ^^@ \exp_not:N \or: } }
1671 % \end{macrocode}
1672 % Convert the above temporary list into a series of constant token
1673 % lists, one for each character code, using \cs{tex_lowercase:D} to
1674 % convert |^^@| in each case. The \texttt{e}-type expansion ensures
1675 % that \cs{tex_lowercase:D} receives the contents of the token list.
1676 % \begin{macrocode}
1677 \cs_set_protected:Npn \@@_tmp:n #1
1678 {
1679 \char_set_lccode:nn { 0 } {#1}
1680 \char_set_lccode:nn { 32 } {#1}
1681 \exp_args:Ne \tex_lowercase:D
1682 {
1683 \tl_const:Ne
1684 \exp_not:c { c_@@_ \@@_int_to_roman:w #1 _tl }
1685 { \exp_not:o \l_@@_tmp_tl }
1686 }
1687 }
1688 \int_step_function:nnN { 0 } { 255 } \@@_tmp:n
1689 % \end{macrocode}
1690 % As \TeX{} is very unhappy if it finds an alignment character inside
1691 % a primitive \tn{halign} even when skipping false branches, some
1692 % precautions are required. \TeX{} is happy if the token is hidden
1693 % between braces within \cs{if_false:} \dots{} \cs{fi:}. The
1694 % rather low-level approach here expands in one step to the
1695 % \meta{target token} (\cs{or:} \dots{}), then \cs{exp_after:wN}
1696 % \meta{target token} (\cs{or:} \dots{}) expands in one step to
1697 % \meta{target token}. This means that \cs{exp_not:N} is applied to
1698 % a potentially-problematic active token.
1699 % \begin{macrocode}
1700 \cs_new:Npn \@@_generate_aux:nnw #1#2#3 \exp_end:
1701 {
1702 #3
1703 \if_false: { \fi:
1704 \exp_after:wN \exp_after:wN \exp_after:wN \exp_end:
1705 \exp_after:wN \exp_after:wN
1706 \if_case:w \tex_numexpr:D 13 - #2
1707 \exp_after:wN \exp_after:wN \exp_after:wN \exp_after:wN
1708 \exp_after:wN \exp_after:wN \exp_after:wN \scan_stop:
1709 \exp_after:wN \exp_after:wN \exp_after:wN \exp_not:N
1710 \cs:w c_@@_ \@@_int_to_roman:w #1 _tl \cs_end:
1711 }
1712 \fi:
1713 }
1714 \fi:
1715 \group_end:
1716 % \end{macrocode}
1717 % \end{macro}
1718 % \end{variable}
1719 % \end{macro}
1720 % \end{macro}
1721 % \end{macro}
1722 %
1723 % \begin{variable}{\c_catcode_active_space_tl}
1724 % While \cs{char_generate:nn} can produce active characters in some
1725 % engines it cannot in general. It would be possible to simply change
1726 % the catcode of space but then the code would need to avoid all
1727 % spaces, making it quite unreadable. Instead we use the primitive
1728 % \cs{tex_lowercase:D} trick.
1729 % \begin{macrocode}
1730 \group_begin:
1731 \char_set_catcode_active:N *
1732 \char_set_lccode:nn { `* } { `\ }
1733 \tex_lowercase:D { \tl_const:Nn \c_catcode_active_space_tl { * } }
1734 \group_end:
1735 % \end{macrocode}
1736 % \end{variable}
1737 %
1738 % \begin{macro}{\c_catcode_other_space_tl}
1739 % Create a space with category code $12$: an \enquote{other} space.
1740 % \begin{macrocode}
1741 \tl_const:Ne \c_catcode_other_space_tl { \char_generate:nn { `\ } { 12 } }
1742 % \end{macrocode}
1743 % \end{macro}
1744 %
1745 % \subsection{Generic tokens}
1746 %
1747 % \begin{macrocode}
1748 %<@@=token>
1749 % \end{macrocode}
1750 %
1751 % \begin{variable}{\s_@@_mark, \s_@@_stop}
1752 % Internal scan marks.
1753 % \begin{macrocode}
1754 \scan_new:N \s_@@_mark
1755 \scan_new:N \s_@@_stop
1756 % \end{macrocode}
1757 % \end{variable}
1758 %
1759 % \begin{macro}{\token_to_meaning:N, \token_to_meaning:c}
1760 % \begin{macro}{\token_to_str:N, \token_to_str:c}
1761 % These are all defined in \pkg{l3basics}, as they are needed
1762 % \enquote{early}. This is just a reminder!
1763 % \end{macro}
1764 % \end{macro}
1765 %
1766 % \begin{macro}{\token_to_catcode:N}
1767 % \begin{macro}{\@@_to_catcode:N}
1768 % The macro works by comparing the input token with \cs{if_catcode:w} with all
1769 % valid category codes. Since the most common tokens in an average argument
1770 % list are of category $11$ or $12$ those are tested first. And since a space
1771 % and braces are no ordinary |N|-type arguments, and only control sequences
1772 % let to those categories can match them they are tested last.
1773 % \begin{macrocode}
1774 \cs_new:Npn \token_to_catcode:N
1775 { \int_value:w \group_align_safe_begin: \@@_to_catcode:N }
1776 \cs_new:Npn \@@_to_catcode:N #1
1777 {
1778 \if_catcode:w \exp_not:N #1 \c_catcode_letter_token
1779 11
1780 \else:
1781 \if_catcode:w \exp_not:N #1 \c_catcode_other_token
1782 12
1783 \else:
1784 \if_catcode:w \exp_not:N #1 \c_math_toggle_token
1785 3
1786 \else:
1787 \if_catcode:w \exp_not:N #1 \c_alignment_token
1788 4
1789 \else:
1790 \if_catcode:w \exp_not:N #1 ##
1791 6
1792 \else:
1793 \if_catcode:w \exp_not:N #1 \c_math_superscript_token
1794 7
1795 \else:
1796 \if_catcode:w \exp_not:N #1 \c_math_subscript_token
1797 8
1798 \else:
1799 \if_catcode:w \exp_not:N #1 \c_group_begin_token
1800 1
1801 \else:
1802 \if_catcode:w \exp_not:N #1 \c_group_end_token
1803 2
1804 \else:
1805 \if_catcode:w \exp_not:N #1 \c_space_token
1806 10
1807 \else:
1808 \token_if_cs:NTF #1 { 16 } { 13 }
1809 \fi:
1810 \fi:
1811 \fi:
1812 \fi:
1813 \fi:
1814 \fi:
1815 \fi:
1816 \fi:
1817 \fi:
1818 \fi:
1819 \group_align_safe_end:
1820 \exp_stop_f:
1821 }
1822 % \end{macrocode}
1823 % \end{macro}
1824 % \end{macro}
1825 %
1826 % \begin{macro}
1827 % {
1828 % \c_group_begin_token,
1829 % \c_group_end_token,
1830 % \c_math_toggle_token,
1831 % \c_alignment_token,
1832 % \c_parameter_token,
1833 % \c_math_superscript_token,
1834 % \c_math_subscript_token,
1835 % \c_space_token,
1836 % \c_catcode_letter_token,
1837 % \c_catcode_other_token
1838 % }
1839 % We define these useful tokens. For the brace and space tokens things have
1840 % to be done by hand: the formal argument spec.~for \cs{cs_new_eq:NN} does
1841 % not cover them so we do things by hand. (As currently coded it would
1842 % \emph{work} with \cs{cs_new_eq:NN} but that's not really a great idea to
1843 % show off: we want people to stick to the defined interfaces and that
1844 % includes us.) So that these few odd names go into the log when appropriate
1845 % there is a need to hand-apply the \cs{__kernel_chk_if_free_cs:N} check.
1846 % \begin{macrocode}
1847 \group_begin:
1848 \__kernel_chk_if_free_cs:N \c_group_begin_token
1849 \tex_global:D \tex_let:D \c_group_begin_token {
1850 \__kernel_chk_if_free_cs:N \c_group_end_token
1851 \tex_global:D \tex_let:D \c_group_end_token }
1852 \char_set_catcode_math_toggle:N \*
1853 \cs_new_eq:NN \c_math_toggle_token *
1854 \char_set_catcode_alignment:N \*
1855 \cs_new_eq:NN \c_alignment_token *
1856 \cs_new_eq:NN \c_parameter_token #
1857 \cs_new_eq:NN \c_math_superscript_token ^
1858 \char_set_catcode_math_subscript:N \*
1859 \cs_new_eq:NN \c_math_subscript_token *
1860 \__kernel_chk_if_free_cs:N \c_space_token
1861 \use:n { \tex_global:D \tex_let:D \c_space_token = ~ } ~
1862 \cs_new_eq:NN \c_catcode_letter_token a
1863 \cs_new_eq:NN \c_catcode_other_token 1
1864 \group_end:
1865 % \end{macrocode}
1866 % \end{macro}
1867 %
1868 % \begin{variable}{\c_@@_active_tl}
1869 % Not an implicit token!
1870 % \begin{macrocode}
1871 \group_begin:
1872 \char_set_catcode_active:N \*
1873 \tl_const:Nn \c_@@_active_tl { \exp_not:N * }
1874 \group_end:
1875 % \end{macrocode}
1876 % \end{variable}
1877 %
1878 % \subsection{Token conditionals}
1879 %
1880 % \begin{macro}[pTF]{\token_if_group_begin:N}
1881 % Check if token is a begin group token. We use the constant
1882 % \cs{c_group_begin_token} for this.
1883 % \begin{macrocode}
1884 \prg_new_conditional:Npnn \token_if_group_begin:N #1 { p , T , F , TF }
1885 {
1886 \if_catcode:w \exp_not:N #1 \c_group_begin_token
1887 \prg_return_true: \else: \prg_return_false: \fi:
1888 }
1889 % \end{macrocode}
1890 % \end{macro}
1891 %
1892 % \begin{macro}[pTF]{\token_if_group_end:N}
1893 % Check if token is a end group token. We use the constant
1894 % \cs{c_group_end_token} for this.
1895 % \begin{macrocode}
1896 \prg_new_conditional:Npnn \token_if_group_end:N #1 { p , T , F , TF }
1897 {
1898 \if_catcode:w \exp_not:N #1 \c_group_end_token
1899 \prg_return_true: \else: \prg_return_false: \fi:
1900 }
1901 % \end{macrocode}
1902 % \end{macro}
1903 %
1904 % \begin{macro}[pTF]{\token_if_math_toggle:N}
1905 % Check if token is a math shift token. We use the constant
1906 % \cs{c_math_toggle_token} for this.
1907 % \begin{macrocode}
1908 \prg_new_conditional:Npnn \token_if_math_toggle:N #1 { p , T , F , TF }
1909 {
1910 \if_catcode:w \exp_not:N #1 \c_math_toggle_token
1911 \prg_return_true: \else: \prg_return_false: \fi:
1912 }
1913 % \end{macrocode}
1914 % \end{macro}
1915 %
1916 % \begin{macro}[pTF]{\token_if_alignment:N}
1917 % Check if token is an alignment tab token. We use the constant
1918 % \cs{c_alignment_token} for this.
1919 % \begin{macrocode}
1920 \prg_new_conditional:Npnn \token_if_alignment:N #1 { p , T , F , TF }
1921 {
1922 \if_catcode:w \exp_not:N #1 \c_alignment_token
1923 \prg_return_true: \else: \prg_return_false: \fi:
1924 }
1925 % \end{macrocode}
1926 % \end{macro}
1927 %
1928 % \begin{macro}[pTF]{\token_if_parameter:N}
1929 % Check if token is a parameter token. We use the constant
1930 % \cs{c_parameter_token} for this. We have to trick \TeX{} a bit to
1931 % avoid an error message: within a group we prevent
1932 % \cs{c_parameter_token} from behaving like a macro parameter character.
1933 % The definitions of \cs{prg_new_conditional:Npnn} are global, so they
1934 % remain after the group.
1935 % \begin{macrocode}
1936 \group_begin:
1937 \cs_set_eq:NN \c_parameter_token \scan_stop:
1938 \prg_new_conditional:Npnn \token_if_parameter:N #1 { p , T , F , TF }
1939 {
1940 \if_catcode:w \exp_not:N #1 \c_parameter_token
1941 \prg_return_true: \else: \prg_return_false: \fi:
1942 }
1943 \group_end:
1944 % \end{macrocode}
1945 % \end{macro}
1946 %
1947 % \begin{macro}[pTF]{\token_if_math_superscript:N}
1948 % Check if token is a math superscript token. We use the constant
1949 % \cs{c_math_superscript_token} for this.
1950 % \begin{macrocode}
1951 \prg_new_conditional:Npnn \token_if_math_superscript:N #1
1952 { p , T , F , TF }
1953 {
1954 \if_catcode:w \exp_not:N #1 \c_math_superscript_token
1955 \prg_return_true: \else: \prg_return_false: \fi:
1956 }
1957 % \end{macrocode}
1958 % \end{macro}
1959 %
1960 % \begin{macro}[pTF]{\token_if_math_subscript:N}
1961 % Check if token is a math subscript token. We use the constant
1962 % \cs{c_math_subscript_token} for this.
1963 % \begin{macrocode}
1964 \prg_new_conditional:Npnn \token_if_math_subscript:N #1 { p , T , F , TF }
1965 {
1966 \if_catcode:w \exp_not:N #1 \c_math_subscript_token
1967 \prg_return_true: \else: \prg_return_false: \fi:
1968 }
1969 % \end{macrocode}
1970 % \end{macro}
1971 %
1972 % \begin{macro}[pTF]{\token_if_space:N}
1973 % Check if token is a space token. We use the constant
1974 % \cs{c_space_token} for this.
1975 % \begin{macrocode}
1976 \prg_new_conditional:Npnn \token_if_space:N #1 { p , T , F , TF }
1977 {
1978 \if_catcode:w \exp_not:N #1 \c_space_token
1979 \prg_return_true: \else: \prg_return_false: \fi:
1980 }
1981 % \end{macrocode}
1982 % \end{macro}
1983 %
1984 % \begin{macro}[pTF]{\token_if_letter:N}
1985 % Check if token is a letter token. We use the constant
1986 % \cs{c_catcode_letter_token} for this.
1987 % \begin{macrocode}
1988 \prg_new_conditional:Npnn \token_if_letter:N #1 { p , T , F , TF }
1989 {
1990 \if_catcode:w \exp_not:N #1 \c_catcode_letter_token
1991 \prg_return_true: \else: \prg_return_false: \fi:
1992 }
1993 % \end{macrocode}
1994 % \end{macro}
1995 %
1996 % \begin{macro}[pTF]{\token_if_other:N}
1997 % Check if token is an other char token. We use the constant
1998 % \cs{c_catcode_other_token} for this.
1999 % \begin{macrocode}
2000 \prg_new_conditional:Npnn \token_if_other:N #1 { p , T , F , TF }
2001 {
2002 \if_catcode:w \exp_not:N #1 \c_catcode_other_token
2003 \prg_return_true: \else: \prg_return_false: \fi:
2004 }
2005 % \end{macrocode}
2006 % \end{macro}
2007 %
2008 % \begin{macro}[pTF]{\token_if_active:N}
2009 % Check if token is an active char token. We use the constant
2010 % \cs{c_@@_active_tl} for this. A technical point is that
2011 % \cs{c_@@_active_tl} is in fact a macro expanding to
2012 % |\exp_not:N *|, where |*| is active.
2013 % \begin{macrocode}
2014 \prg_new_conditional:Npnn \token_if_active:N #1 { p , T , F , TF }
2015 {
2016 \if_catcode:w \exp_not:N #1 \c_@@_active_tl
2017 \prg_return_true: \else: \prg_return_false: \fi:
2018 }
2019 % \end{macrocode}
2020 % \end{macro}
2021 %
2022 % \begin{macro}[pTF]{\token_if_eq_meaning:NN}
2023 % Check if the tokens |#1| and |#2| have same meaning.
2024 % \begin{macrocode}
2025 \prg_new_eq_conditional:NNn \token_if_eq_meaning:NN \cs_if_eq:NN
2026 { p , T , F , TF }
2027 % \end{macrocode}
2028 % \end{macro}
2029 %
2030 % \begin{macro}[pTF]{\token_if_eq_catcode:NN}
2031 % Check if the tokens |#1| and |#2| have same category code.
2032 % \begin{macrocode}
2033 \prg_new_conditional:Npnn \token_if_eq_catcode:NN #1#2 { p , T , F , TF }
2034 {
2035 \if_catcode:w \exp_not:N #1 \exp_not:N #2
2036 \prg_return_true: \else: \prg_return_false: \fi:
2037 }
2038 % \end{macrocode}
2039 % \end{macro}
2040 %
2041 % \begin{macro}[pTF]{\token_if_eq_charcode:NN}
2042 % Check if the tokens |#1| and |#2| have same character code.
2043 % \begin{macrocode}
2044 \prg_new_conditional:Npnn \token_if_eq_charcode:NN #1#2 { p , T , F , TF }
2045 {
2046 \if_charcode:w \exp_not:N #1 \exp_not:N #2
2047 \prg_return_true: \else: \prg_return_false: \fi:
2048 }
2049 % \end{macrocode}
2050 % \end{macro}
2051 %
2052 % \begin{macro}[pTF]{\token_if_macro:N}
2053 % \begin{macro}{\@@_if_macro_p:w}
2054 % When a token is a macro, \cs{token_to_meaning:N} always outputs
2055 % something like |\long macro:#1->#1| so we could naively check to
2056 % see if the meaning contains |->|. However, this can fail the five
2057 % \tn[no-index]{...mark} primitives, whose meaning has the form
2058 % |...mark:|\meta{user material}. The problem is that the
2059 % \meta{user material} can contain |->|.
2060 %
2061 % However, only characters, macros, and marks can contain the colon
2062 % character. The idea is thus to grab until the first |:|, and analyze
2063 % what is left. However, macros can have any combination of |\long|,
2064 % |\protected| or |\outer| (not used in \LaTeX3) before the string
2065 % |macro:|. We thus only select the part of the meaning between
2066 % the first |ma| and the first following |:|. If this string is
2067 % |cro|, then we have a macro. If the string is |rk|, then we have
2068 % a mark. The string can also be |cro parameter character | for a
2069 % colon with a weird category code (namely the usual category code
2070 % of |#|). Otherwise, it is empty.
2071 %
2072 % This relies on the fact that |\long|, |\protected|, |\outer|
2073 % cannot contain |ma|, regardless of the escape character, even if
2074 % the escape character is |m|\ldots{}
2075 %
2076 % Both |ma| and |:| must be of category code $12$ (other), so are
2077 % detokenized.
2078 %
2079 % \begin{macrocode}
2080 \use:e
2081 {
2082 \prg_new_conditional:Npnn \exp_not:N \token_if_macro:N #1
2083 { p , T , F , TF }
2084 {
2085 \exp_not:N \exp_after:wN \exp_not:N \@@_if_macro_p:w
2086 \exp_not:N \token_to_meaning:N #1 \tl_to_str:n { ma : }
2087 \s_@@_stop
2088 }
2089 \cs_new:Npn \exp_not:N \@@_if_macro_p:w
2090 #1 \tl_to_str:n { ma } #2 \c_colon_str #3 \s_@@_stop
2091 }
2092 {
2093 \str_if_eq:nnTF { #2 } { cro }
2094 { \prg_return_true: }
2095 { \prg_return_false: }
2096 }
2097 % \end{macrocode}
2098 % \end{macro}
2099 % \end{macro}
2100 %
2101 % \begin{macro}[pTF]{\token_if_cs:N}
2102 % Check if token has same catcode as a control sequence. This
2103 % follows the same pattern as for \cs{token_if_letter:N} etc.
2104 % We use \cs{scan_stop:} for this.
2105 % \begin{macrocode}
2106 \prg_new_conditional:Npnn \token_if_cs:N #1 { p , T , F , TF }
2107 {
2108 \if_catcode:w \exp_not:N #1 \scan_stop:
2109 \prg_return_true: \else: \prg_return_false: \fi:
2110 }
2111 % \end{macrocode}
2112 % \end{macro}
2113 %
2114 % \begin{macro}[pTF]{\token_if_expandable:N}
2115 % Check if token is expandable. We use the fact that \TeX{}
2116 % temporarily converts \cs{exp_not:N} \meta{token} into \cs{scan_stop:}
2117 % if \meta{token} is expandable. An \texttt{undefined} token is not
2118 % considered as expandable. No problem nesting the conditionals,
2119 % since the third |#1| is only skipped if it is non-expandable (hence
2120 % not part of \TeX{}'s conditional apparatus).
2121 % \begin{macrocode}
2122 \prg_new_conditional:Npnn \token_if_expandable:N #1 { p , T , F , TF }
2123 {
2124 \exp_after:wN \if_meaning:w \exp_not:N #1 #1
2125 \prg_return_false:
2126 \else:
2127 \if_cs_exist:N #1
2128 \prg_return_true:
2129 \else:
2130 \prg_return_false:
2131 \fi:
2132 \fi:
2133 }
2134 % \end{macrocode}
2135 % \end{macro}
2136 %
2137 % \begin{macro}
2138 % {
2139 % \@@_delimit_by_char":w,
2140 % \@@_delimit_by_count:w,
2141 % \@@_delimit_by_dimen:w,
2142 % \@@_delimit_by_ font:w,
2143 % \@@_delimit_by_macro:w,
2144 % \@@_delimit_by_muskip:w,
2145 % \@@_delimit_by_skip:w,
2146 % \@@_delimit_by_toks:w,
2147 % }
2148 % These auxiliary functions are used below to define some
2149 % conditionals which detect whether the \tn{meaning} of their
2150 % argument begins with a particular string. Each auxiliary takes an
2151 % argument delimited by a string, a second one delimited by
2152 % \cs{s_@@_stop}, and returns the first one and its delimiter.
2153 % This result is eventually compared to another string.
2154 % Note that the ``font'' auxiliary is delimited by a space followed by
2155 % ``\texttt{font}''. This avoids an unnecessary check for the
2156 % \tn{font} primitive below.
2157 % \begin{macrocode}
2158 \group_begin:
2159 \cs_set_protected:Npn \@@_tmp:w #1
2160 {
2161 \use:e
2162 {
2163 \cs_new:Npn \exp_not:c { @@_delimit_by_ #1 :w }
2164 ##1 \tl_to_str:n {#1} ##2 \s_@@_stop
2165 { ##1 \tl_to_str:n {#1} }
2166 }
2167 }
2168 \@@_tmp:w { char" }
2169 \@@_tmp:w { count }
2170 \@@_tmp:w { dimen }
2171 \@@_tmp:w { ~ font }
2172 \@@_tmp:w { macro }
2173 \@@_tmp:w { muskip }
2174 \@@_tmp:w { skip }
2175 \@@_tmp:w { toks }
2176 \group_end:
2177 % \end{macrocode}
2178 % \end{macro}
2179 %
2180 % \begin{macro}[pTF]
2181 % {
2182 % \token_if_chardef:N, \token_if_mathchardef:N,
2183 % \token_if_long_macro:N,
2184 % \token_if_protected_macro:N, \token_if_protected_long_macro:N,
2185 % \token_if_font_selection:N,
2186 % \token_if_dim_register:N, \token_if_int_register:N,
2187 % \token_if_muskip_register:N,
2188 % \token_if_skip_register:N, \token_if_toks_register:N,
2189 % }
2190 % Each of these conditionals tests whether its argument's
2191 % \tn{meaning} starts with a given string. This is essentially done
2192 % by having an auxiliary grab an argument delimited by the string and
2193 % testing whether the argument was empty. Of course, a copy of this
2194 % string must first be added to the end of the \tn{meaning} to avoid
2195 % a runaway argument in case it does not contain the string. Two
2196 % complications arise. First, the escape character is not fixed, and
2197 % cannot be included in the delimiter of the auxiliary function (this
2198 % function cannot be defined on the fly because tests must remain
2199 % expandable): instead the first argument of the auxiliary (plus the
2200 % delimiter to avoid complications with trailing spaces) is compared
2201 % using \cs{str_if_eq:eeTF} to the result of applying
2202 % \cs{token_to_str:N} to a control sequence. Second, the
2203 % \tn{meaning} of primitives such as \tn{dimen} or \tn{dimendef}
2204 % starts in the same way as registers such as
2205 % \tn{dimen}\texttt{123}, so they must be tested for.
2206 %
2207 % Characters used as delimiters must have catcode~$12$
2208 % and are obtained through \cs{tl_to_str:n}. This requires doing all
2209 % definitions within \texttt{e}-expansion. The temporary function
2210 % \cs{@@_tmp:w} used to define each conditional receives three
2211 % arguments: the name of the conditional, the auxiliary's delimiter
2212 % (also used to name the auxiliary), and the string to which one
2213 % compares the auxiliary's result. Note that the \tn{meaning} of a
2214 % protected long macro starts with |\protected\long macro|, with no
2215 % space after |\protected| but a space after |\long|, hence the
2216 % mixture of \cs{token_to_str:N} and \cs{tl_to_str:n}.
2217 %
2218 % For the first six conditionals, \cs{cs_if_exist:cT} turns out to
2219 % be \texttt{false} (thanks to the leading space for \texttt{font}),
2220 % and the code boils down to a string comparison
2221 % between the result of the auxiliary on the \tn{meaning} of the
2222 % conditional's argument~|####1|, and~|#3|. Both are evaluated at
2223 % run-time, as this is important to get the correct escape character.
2224 %
2225 % The other five conditionals have additional code that compares the
2226 % argument~|####1| to two \TeX{} primitives which would wrongly be
2227 % recognized as registers otherwise. Despite using \TeX{}'s
2228 % primitive conditional construction, this does not break
2229 % when~|####1| is itself a conditional, because branches of the
2230 % conditionals are only skipped if |####1|~is one of the two
2231 % primitives that are tested for (which are not \TeX{} conditionals).
2232 % \begin{macrocode}
2233 \group_begin:
2234 \cs_set_protected:Npn \@@_tmp:w #1#2#3
2235 {
2236 \use:e
2237 {
2238 \prg_new_conditional:Npnn \exp_not:c { token_if_ #1 :N } ##1
2239 { p , T , F , TF }
2240 {
2241 \cs_if_exist:cT { tex_ #2 :D }
2242 {
2243 \exp_not:N \if_meaning:w ##1 \exp_not:c { tex_ #2 :D }
2244 \exp_not:N \prg_return_false:
2245 \exp_not:N \else:
2246 \exp_not:N \if_meaning:w ##1 \exp_not:c { tex_ #2 def:D }
2247 \exp_not:N \prg_return_false:
2248 \exp_not:N \else:
2249 }
2250 \exp_not:N \str_if_eq:eeTF
2251 {
2252 \exp_not:N \exp_after:wN
2253 \exp_not:c { @@_delimit_by_ #2 :w }
2254 \exp_not:N \token_to_meaning:N ##1
2255 ? \tl_to_str:n {#2} \s_@@_stop
2256 }
2257 { \exp_not:n {#3} }
2258 { \exp_not:N \prg_return_true: }
2259 { \exp_not:N \prg_return_false: }
2260 \cs_if_exist:cT { tex_ #2 :D }
2261 {
2262 \exp_not:N \fi:
2263 \exp_not:N \fi:
2264 }
2265 }
2266 }
2267 }
2268 \@@_tmp:w { chardef } { char" } { \token_to_str:N \char" }
2269 \@@_tmp:w { mathchardef } { char" } { \token_to_str:N \mathchar" }
2270 \@@_tmp:w { long_macro } { macro } { \tl_to_str:n { \long } macro }
2271 \@@_tmp:w { protected_macro } { macro }
2272 { \tl_to_str:n { \protected } macro }
2273 \@@_tmp:w { protected_long_macro } { macro }
2274 { \token_to_str:N \protected \tl_to_str:n { \long } macro }
2275 \@@_tmp:w { font_selection } { ~ font } { select ~ font }
2276 \@@_tmp:w { dim_register } { dimen } { \token_to_str:N \dimen }
2277 \@@_tmp:w { int_register } { count } { \token_to_str:N \count }
2278 \@@_tmp:w { muskip_register } { muskip } { \token_to_str:N \muskip }
2279 \@@_tmp:w { skip_register } { skip } { \token_to_str:N \skip }
2280 \@@_tmp:w { toks_register } { toks } { \token_to_str:N \toks }
2281 \group_end:
2282 % \end{macrocode}
2283 % \end{macro}
2284 %
2285 % \begin{macro}[pTF]{\token_if_control_word:N}
2286 % \begin{macro}[EXP]{\@@_if_control_word_aux:w}
2287 % This checks that the token consists of the escape character followed by
2288 % one or more letters. As |#1| could be a control symbol let to
2289 % \cs{else:} or \cs{fi:}, etc., so we use an auxiliary function
2290 % to grab the second \cs{if:w}.
2291 % \begin{macrocode}
2292 \prg_new_conditional:Npnn \token_if_control_word:N #1 { p , T , F , TF }
2293 {
2294 \if:w \exp_not:N #1 \token_to_str:N #1
2295 \@@_if_control_word_false:w
2296 \fi:
2297 \if:w 0 \tex_strcmp:D { \exp_not:N #1 } { \token_to_str:N #1 }
2298 \@@_if_control_word_false:w
2299 \fi:
2300 \if_true:
2301 \prg_return_true:
2302 \else:
2303 \prg_return_false:
2304 \fi:
2305 }
2306 \cs_new:Npn \@@_if_control_word_false:w \fi: #1 \if_true: { \fi: \if_false: }
2307 % \end{macrocode}
2308 % \end{macro}
2309 % \end{macro}
2310 % \begin{macro}[pTF]{\token_if_control_symbol:N}
2311 % \begin{macro}[EXP]{\@@_if_control_symbol_false:w}
2312 % A similar plan but we need to test for the space behavior, and then whether we
2313 % are dealing with a control sequence. In this case there is no worry about
2314 % a dangling token.
2315 % \begin{macrocode}
2316 \prg_new_conditional:Npnn \token_if_control_symbol:N #1 { p , T , F , TF }
2317 {
2318 \if_catcode:w \exp_not:N #1 \scan_stop:
2319 \else:
2320 \@@_if_control_symbol_false:w
2321 \fi:
2322 \if:w 0 \tex_strcmp:D { \exp_not:N #1 } { \token_to_str:N #1 }
2323 \prg_return_true:
2324 \else:
2325 \prg_return_false:
2326 \fi:
2327 }
2328 \cs_new:Npn \@@_if_control_symbol_false:w \fi: \if:w 0 \tex_strcmp:D #1#2
2329 { \fi: \if_false: }
2330 % \end{macrocode}
2331 % \end{macro}
2332 % \end{macro}
2333 %
2334 % \begin{macro}[pTF]{\token_if_primitive:N}
2335 % \begin{macro}{\@@_if_primitive:NNw,
2336 % \@@_if_primitive_space:w,
2337 % \@@_if_primitive_nullfont:N,
2338 % \@@_if_primitive_loop:N,
2339 % \@@_if_primitive:Nw,
2340 % \@@_if_primitive_undefined:N,
2341 % \@@_if_primitive_lua:N}
2342 %^^A See http://groups.google.com/group/comp.text.tex/browse_thread/thread/0a72666873f8753d#
2343 %
2344 % We filter out macros first, because they cause endless trouble later
2345 % otherwise.
2346 %
2347 % Primitives are almost distinguished by the fact that the result
2348 % of \cs{token_to_meaning:N} is formed from letters only. Every other
2349 % token has either a space (e.g., |the letter A|), a digit
2350 % (e.g., |\count123|) or a double quote (e.g., |\char"A|).
2351 %
2352 % Ten exceptions: on the one hand, \cs{tex_undefined:D} is not a
2353 % primitive, but its meaning is |undefined|, only letters;
2354 % on the other hand, \tn{space}, \tn{italiccorr},
2355 % \tn{hyphen}, \tn{firstmark}, \tn{topmark},
2356 % \tn{botmark}, \tn{splitfirstmark}, \tn{splitbotmark},
2357 % and \tn{nullfont} are primitives, but have non-letters
2358 % in their meaning.
2359 %
2360 % We start by removing the two first (non-space) characters from
2361 % the meaning. This removes the escape character (which may be
2362 % nonexistent depending on \tn{endlinechar}), and takes care
2363 % of three of the exceptions: \tn{space}, \tn{italiccorr}
2364 % and \tn{hyphen}, whose meaning is at most two characters.
2365 % This leaves a string terminated by some |:|, and \cs{s_@@_stop}.
2366 %
2367 % The meaning of each one of the five \tn[no-index]{...mark} primitives
2368 % has the form \meta{letters}|:|\meta{user material}. In other words,
2369 % the first non-letter is a colon. We remove everything after the first
2370 % colon.
2371 %
2372 % We are now left with a string, which we must analyze. For primitives,
2373 % it contains only letters. For non-primitives, it contains either
2374 % |"|, or a space, or a digit. Two exceptions remain: \cs{tex_undefined:D},
2375 % which is not a primitive, and \tn{nullfont}, which is a primitive.
2376 %
2377 % Spaces cannot be grabbed in an undelimited way, so we check them
2378 % separately. If there is a space, we test for \tn{nullfont}.
2379 % Otherwise, we go through characters one by one, and stop at the
2380 % first character less than |`A| (this is not quite a test for
2381 % \enquote{only letters}, but is close enough to work in this context).
2382 % If this first character is |:| then we have a primitive, or
2383 % \cs{tex_undefined:D}, and if it is |"| or a digit, then the token
2384 % is not a primitive.
2385 %
2386 % For \LuaTeX{} we use a different implementation which just looks at the
2387 % command code for the token and compares it to a list of non-primitives.
2388 % Again, \tn{nullfont} is a special case because it is the only primitive
2389 % with the normally non-primitive |set_font| command code.
2390 %
2391 % In LuaMeta\TeX{} some of the command names are different, so we check for
2392 % both versions. The first one is always the \LuaTeX{} version.
2393 %
2394 % \begin{macrocode}
2395 \sys_if_engine_luatex:TF
2396 {
2397 %</code>
2398 %<*lua>
2399 do
2400 local get_command = token.get_command
2401 local get_index = token.get_index
2402 local get_mode = token.get_mode or token.get_index
2403 local cmd = command_id
2404 local set_font = cmd'get_font'
2405 local biggest_char = token.biggest_char and token.biggest_char()
2406 or status.getconstants().max_character_code
2407
2408 local mode_below_biggest_char = {}
2409 local index_not_nil = {}
2410 local mode_not_null = {}
2411 local non_primitive = {
2412 [cmd'left_brace'] = true,
2413 [cmd'right_brace'] = true,
2414 [cmd'math_shift'] = true,
2415 [cmd'mac_param' or cmd'parameter'] = mode_below_biggest_char,
2416 [cmd'sup_mark' or cmd'superscript'] = true,
2417 [cmd'sub_mark' or cmd'subscript'] = true,
2418 [cmd'endv' or cmd'ignore'] = true,
2419 [cmd'spacer'] = true,
2420 [cmd'letter'] = true,
2421 [cmd'other_char'] = true,
2422 [cmd'tab_mark' or cmd'alignment_tab'] = mode_below_biggest_char,
2423 [cmd'char_given'] = true,
2424 [cmd'math_given' or 'math_char_given'] = true,
2425 [cmd'xmath_given' or 'math_char_xgiven'] = true,
2426 [cmd'set_font'] = mode_not_null,
2427 [cmd'undefined_cs'] = true,
2428 [cmd'call'] = true,
2429 [cmd'long_call' or cmd'protected_call'] = true,
2430 [cmd'outer_call' or cmd'tolerant_call'] = true,
2431 [cmd'long_outer_call' or cmd'tolerant_protected_call'] = true,
2432 [cmd'assign_glue' or cmd'register_glue'] = index_not_nil,
2433 [cmd'assign_mu_glue' or cmd'register_mu_glue' or cmd'register_muglue'] = index_not_nil,
2434 [cmd'assign_toks' or cmd'register_toks'] = index_not_nil,
2435 [cmd'assign_int' or cmd'register_int' or cmd'register_integer'] = index_not_nil,
2436 [cmd'assign_attr' or cmd'register_attribute'] = true,
2437 [cmd'assign_dimen' or cmd'register_dimen' or cmd'register_dimension'] = index_not_nil,
2438 }
2439
2440 luacmd("@@_if_primitive_lua:N", function()
2441 local tok = get_next()
2442 local is_non_primitive = non_primitive[get_command(tok)]
2443 return put_next(
2444 is_non_primitive == true
2445 and false_tok
2446 or is_non_primitive == nil
2447 and true_tok
2448 or is_non_primitive == mode_not_null
2449 and (get_mode(tok) == 0 and true_tok or false_tok)
2450 or is_non_primitive == index_not_nil
2451 and (get_index(tok) and false_tok or true_tok)
2452 or is_non_primitive == mode_below_biggest_char
2453 and (get_mode(tok) > biggest_char and true_tok or false_tok))
2454 end, "global")
2455 end
2456 %</lua>
2457 %<*code>
2458 \prg_new_conditional:Npnn \token_if_primitive:N #1 { p , T , F , TF }
2459 {
2460 \@@_if_primitive_lua:N #1
2461 }
2462 }
2463 {
2464 \tex_global:D \tex_chardef:D \c_@@_A_int = `A ~ %
2465 \use:e
2466 {
2467 \prg_new_conditional:Npnn \exp_not:N \token_if_primitive:N #1
2468 { p , T , F , TF }
2469 {
2470 \exp_not:N \token_if_macro:NTF #1
2471 \exp_not:N \prg_return_false:
2472 {
2473 \exp_not:N \exp_after:wN \exp_not:N \@@_if_primitive:NNw
2474 \exp_not:N \token_to_meaning:N #1
2475 \tl_to_str:n { : : : } \s_@@_stop #1
2476 }
2477 }
2478 \cs_new:Npn \exp_not:N \@@_if_primitive:NNw
2479 #1#2 #3 \c_colon_str #4 \s_@@_stop
2480 {
2481 \exp_not:N \tl_if_empty:oTF
2482 { \exp_not:N \@@_if_primitive_space:w #3 ~ }
2483 {
2484 \exp_not:N \@@_if_primitive_loop:N #3
2485 \c_colon_str \s_@@_stop
2486 }
2487 { \exp_not:N \@@_if_primitive_nullfont:N }
2488 }
2489 }
2490 \cs_new:Npn \@@_if_primitive_space:w #1 ~ { }
2491 \cs_new:Npn \@@_if_primitive_nullfont:N #1
2492 {
2493 \if_meaning:w \tex_nullfont:D #1
2494 \prg_return_true:
2495 \else:
2496 \prg_return_false:
2497 \fi:
2498 }
2499 \cs_new:Npn \@@_if_primitive_loop:N #1
2500 {
2501 \if_int_compare:w `#1 < \c_@@_A_int %
2502 \exp_after:wN \@@_if_primitive:Nw
2503 \exp_after:wN #1
2504 \else:
2505 \exp_after:wN \@@_if_primitive_loop:N
2506 \fi:
2507 }
2508 \cs_new:Npn \@@_if_primitive:Nw #1 #2 \s_@@_stop
2509 {
2510 \if:w : #1
2511 \exp_after:wN \@@_if_primitive_undefined:N
2512 \else:
2513 \prg_return_false:
2514 \exp_after:wN \use_none:n
2515 \fi:
2516 }
2517 \cs_new:Npn \@@_if_primitive_undefined:N #1
2518 {
2519 \if_cs_exist:N #1
2520 \prg_return_true:
2521 \else:
2522 \prg_return_false:
2523 \fi:
2524 }
2525 }
2526 % \end{macrocode}
2527 % \end{macro}
2528 % \end{macro}
2529 %
2530 % \begin{macro}[EXP, noTF]
2531 % {\token_case_catcode:Nn, \token_case_charcode:Nn, \token_case_meaning:Nn}
2532 % \begin{macro}[EXP]{\@@_case:NNnTF, \@@_case:NNw, \@@_case_end:nw}
2533 % The aim here is to allow the case statement to be evaluated
2534 % using a known number of expansion steps (two), and without
2535 % needing to use an explicit \enquote{end of recursion} marker.
2536 % That is achieved by using the test input as the final case,
2537 % as this is always true. The trick is then to tidy up
2538 % the output such that the appropriate case code plus either
2539 % the \texttt{true} or \texttt{false} branch code is inserted.
2540 % \begin{macrocode}
2541 \cs_new:Npn \token_case_catcode:Nn #1#2
2542 { \exp:w \@@_case:NNnTF \token_if_eq_catcode:NNTF #1 {#2} { } { } }
2543 \cs_new:Npn \token_case_catcode:NnT #1#2#3
2544 { \exp:w \@@_case:NNnTF \token_if_eq_catcode:NNTF #1 {#2} {#3} { } }
2545 \cs_new:Npn \token_case_catcode:NnF #1#2
2546 { \exp:w \@@_case:NNnTF \token_if_eq_catcode:NNTF #1 {#2} { } }
2547 \cs_new:Npn \token_case_catcode:NnTF
2548 { \exp:w \@@_case:NNnTF \token_if_eq_catcode:NNTF }
2549 \cs_new:Npn \token_case_charcode:Nn #1#2
2550 { \exp:w \@@_case:NNnTF \token_if_eq_charcode:NNTF #1 {#2} { } { } }
2551 \cs_new:Npn \token_case_charcode:NnT #1#2#3
2552 { \exp:w \@@_case:NNnTF \token_if_eq_charcode:NNTF #1 {#2} {#3} { } }
2553 \cs_new:Npn \token_case_charcode:NnF #1#2
2554 { \exp:w \@@_case:NNnTF \token_if_eq_charcode:NNTF #1 {#2} { } }
2555 \cs_new:Npn \token_case_charcode:NnTF
2556 { \exp:w \@@_case:NNnTF \token_if_eq_charcode:NNTF }
2557 \cs_new:Npn \token_case_meaning:Nn #1#2
2558 { \exp:w \@@_case:NNnTF \token_if_eq_meaning:NNTF #1 {#2} { } { } }
2559 \cs_new:Npn \token_case_meaning:NnT #1#2#3
2560 { \exp:w \@@_case:NNnTF \token_if_eq_meaning:NNTF #1 {#2} {#3} { } }
2561 \cs_new:Npn \token_case_meaning:NnF #1#2
2562 { \exp:w \@@_case:NNnTF \token_if_eq_meaning:NNTF #1 {#2} { } }
2563 \cs_new:Npn \token_case_meaning:NnTF
2564 { \exp:w \@@_case:NNnTF \token_if_eq_meaning:NNTF }
2565 \cs_new:Npn \@@_case:NNnTF #1#2#3#4#5
2566 {
2567 \@@_case:NNw #1 #2 #3 #2 { }
2568 \s_@@_mark {#4}
2569 \s_@@_mark {#5}
2570 \s_@@_stop
2571 }
2572 \cs_new:Npn \@@_case:NNw #1#2#3#4
2573 {
2574 #1 #2 #3
2575 { \@@_case_end:nw {#4} }
2576 { \@@_case:NNw #1 #2 }
2577 }
2578 % \end{macrocode}
2579 % To tidy up the recursion, there are two outcomes. If there was a hit to
2580 % one of the cases searched for, then |#1| is the code to insert,
2581 % |#2| is the \emph{next} case to check on and |#3| is all of
2582 % the rest of the cases code. That means that |#4| is the \texttt{true}
2583 % branch code, and |#5| tidies up the spare \cs{s_@@_mark} and the
2584 % \texttt{false} branch. On the other hand, if none of the cases matched
2585 % then we arrive here using the \enquote{termination} case of comparing
2586 % the search with itself. That means that |#1| is empty, |#2| is
2587 % the first \cs{s_@@_mark} and so |#4| is the \texttt{false} code (the
2588 % \texttt{true} code is mopped up by |#3|).
2589 % \begin{macrocode}
2590 \cs_new:Npn \@@_case_end:nw #1#2#3 \s_@@_mark #4#5 \s_@@_stop
2591 { \exp_end: #1 #4 }
2592 % \end{macrocode}
2593 % \end{macro}
2594 % \end{macro}
2595 %
2596 % \subsection{Peeking ahead at the next token}
2597 %
2598 % \begin{macrocode}
2599 %<@@=peek>
2600 % \end{macrocode}
2601 %
2602 % Peeking ahead is implemented using a two part mechanism. The
2603 % outer level provides a defined interface to the lower level material.
2604 % This allows a large amount of code to be shared. There are four
2605 % cases:
2606 % \begin{enumerate}
2607 % \item peek at the next token;
2608 % \item peek at the next non-space token;
2609 % \item peek at the next token and remove it;
2610 % \item peek at the next non-space token and remove it.
2611 % \end{enumerate}
2612 %
2613 % \begin{variable}{\l_peek_token}
2614 % \begin{variable}{\g_peek_token}
2615 % Storage tokens which are publicly documented: the token peeked.
2616 % \begin{macrocode}
2617 \cs_new_eq:NN \l_peek_token ?
2618 \cs_new_eq:NN \g_peek_token ?
2619 % \end{macrocode}
2620 % \end{variable}
2621 % \end{variable}
2622 %
2623 % \begin{variable}{\l_@@_search_token}
2624 % The token to search for as an implicit token:
2625 % \emph{cf.}~\cs{l_@@_search_tl}.
2626 % \begin{macrocode}
2627 \cs_new_eq:NN \l_@@_search_token ?
2628 % \end{macrocode}
2629 % \end{variable}
2630 %
2631 % \begin{variable}{\l_@@_search_tl}
2632 % The token to search for as an explicit token:
2633 % \emph{cf.}~\cs{l_@@_search_token}.
2634 % \begin{macrocode}
2635 \tl_new:N \l_@@_search_tl
2636 % \end{macrocode}
2637 % \end{variable}
2638 %
2639 % \begin{macro}
2640 % {\@@_true:w, \@@_true_aux:w, \@@_false:w, \@@_tmp:w}
2641 % Functions used by the branching and space-stripping code.
2642 % \begin{macrocode}
2643 \cs_new:Npn \@@_true:w { }
2644 \cs_new:Npn \@@_true_aux:w { }
2645 \cs_new:Npn \@@_false:w { }
2646 \cs_new:Npn \@@_tmp:w { }
2647 % \end{macrocode}
2648 % \end{macro}
2649 %
2650 % \begin{variable}{\s_@@_mark,\s_@@_stop}
2651 % Internal scan marks.
2652 % \begin{macrocode}
2653 \scan_new:N \s_@@_mark
2654 \scan_new:N \s_@@_stop
2655 % \end{macrocode}
2656 % \end{variable}
2657 %
2658 % \begin{macro}[EXP]{\@@_use_none_delimit_by_s_stop:w}
2659 % Functions to gobble up to a scan mark.
2660 % \begin{macrocode}
2661 \cs_new:Npn \@@_use_none_delimit_by_s_stop:w #1 \s_@@_stop { }
2662 % \end{macrocode}
2663 % \end{macro}
2664 %
2665 % \begin{macro}{\peek_after:Nw}
2666 % \begin{macro}{\peek_gafter:Nw}
2667 % Simple wrappers for \tn{futurelet}: no arguments absorbed
2668 % here.
2669 % \begin{macrocode}
2670 \cs_new_protected:Npn \peek_after:Nw
2671 { \tex_futurelet:D \l_peek_token }
2672 \cs_new_protected:Npn \peek_gafter:Nw
2673 { \tex_global:D \tex_futurelet:D \g_peek_token }
2674 % \end{macrocode}
2675 % \end{macro}
2676 % \end{macro}
2677 %
2678 % \begin{macro}{\@@_true_remove:w}
2679 % A function to remove the next token and then regain control.
2680 % \begin{macrocode}
2681 \cs_new_protected:Npn \@@_true_remove:w
2682 {
2683 \tex_afterassignment:D \@@_true_aux:w
2684 \cs_set_eq:NN \@@_tmp:w
2685 }
2686 % \end{macrocode}
2687 % \end{macro}
2688 %
2689 % \begin{macro}{\peek_remove_spaces:n, \@@_remove_spaces:}
2690 % Repeatedly use \cs{@@_true_remove:w} to remove a space and call
2691 % \cs{@@_true_aux:w}.
2692 % \begin{macrocode}
2693 \cs_new_protected:Npn \peek_remove_spaces:n #1
2694 {
2695 \cs_set:Npe \@@_false:w { \exp_not:n {#1} }
2696 \group_align_safe_begin:
2697 \cs_set:Npn \@@_true_aux:w { \peek_after:Nw \@@_remove_spaces: }
2698 \@@_true_aux:w
2699 }
2700 \cs_new_protected:Npn \@@_remove_spaces:
2701 {
2702 \if_meaning:w \l_peek_token \c_space_token
2703 \exp_after:wN \@@_true_remove:w
2704 \else:
2705 \group_align_safe_end:
2706 \exp_after:wN \@@_false:w
2707 \fi:
2708 }
2709 % \end{macrocode}
2710 % \end{macro}
2711 %
2712 % \begin{macro}{\peek_remove_filler:n}
2713 % \begin{macro}{\@@_remove_filler:w}
2714 % \begin{macro}{\@@_remove_filler:}
2715 % \begin{macro}{\@@_remove_filler_expand:w}
2716 % Here we expand the input, removing spaces and \cs{scan_stop:} tokens until
2717 % we reach a non-expandable token. At that stage we re-insert the payload.
2718 % To deal with the problem of |&| tokens, we have to put the align-safe
2719 % group in the correct place.
2720 % \begin{macrocode}
2721 \cs_new_protected:Npn \peek_remove_filler:n #1
2722 {
2723 \cs_set:Npn \@@_true_aux:w { \@@_remove_filler:w }
2724 \cs_set:Npe \@@_false:w
2725 {
2726 \exp_not:N \group_align_safe_end:
2727 \exp_not:n {#1}
2728 }
2729 \group_align_safe_begin:
2730 \@@_remove_filler:w
2731 }
2732 \cs_new_protected:Npn \@@_remove_filler:w
2733 {
2734 \exp_after:wN \peek_after:Nw \exp_after:wN \@@_remove_filler:
2735 \exp:w \exp_end_continue_f:w
2736 }
2737 % \end{macrocode}
2738 % Here we can nest conditionals as \cs{l_peek_token} is only skipped over in
2739 % the nested one if it's a space: no problems with conditionals or outer
2740 % tokens.
2741 % \begin{macrocode}
2742 \cs_new_protected:Npn \@@_remove_filler:
2743 {
2744 \if_catcode:w \exp_not:N \l_peek_token \c_space_token
2745 \exp_after:wN \@@_true_remove:w
2746 \else:
2747 \if_meaning:w \l_peek_token \scan_stop:
2748 \exp_after:wN \exp_after:wN \exp_after:wN
2749 \@@_true_remove:w
2750 \else:
2751 \exp_after:wN \exp_after:wN \exp_after:wN
2752 \@@_remove_filler_expand:w
2753 \fi:
2754 \fi:
2755 }
2756 % \end{macrocode}
2757 % To deal with undefined control sequences in the same way \TeX{} does,
2758 % we need to check for expansion manually.
2759 % \begin{macrocode}
2760 \cs_new_protected:Npn \@@_remove_filler_expand:w
2761 {
2762 \exp_after:wN \if_meaning:w \exp_not:N \l_peek_token \l_peek_token
2763 \exp_after:wN \@@_false:w
2764 \else:
2765 \exp_after:wN \@@_remove_filler:w
2766 \fi:
2767 }
2768 % \end{macrocode}
2769 % \end{macro}
2770 % \end{macro}
2771 % \end{macro}
2772 % \end{macro}
2773 %
2774 % \begin{macro}{\@@_token_generic_aux:NNNTF}
2775 % The generic functions store the test token in both implicit and
2776 % explicit modes, and the \texttt{true} and \texttt{false} code as
2777 % token lists, more or less. The two branches have to be absorbed here
2778 % as the input stream needs to be cleared for the peek function itself.
2779 % Here, |#1| is \cs{@@_true_remove:w} when removing the token and
2780 % \cs{@@_true_aux:w} otherwise.
2781 % \begin{macrocode}
2782 \cs_new_protected:Npn \@@_token_generic_aux:NNNTF #1#2#3#4#5
2783 {
2784 \group_align_safe_begin:
2785 \cs_set_eq:NN \l_@@_search_token #3
2786 \tl_set:Nn \l_@@_search_tl {#3}
2787 \cs_set:Npe \@@_true_aux:w
2788 {
2789 \exp_not:N \group_align_safe_end:
2790 \exp_not:n {#4}
2791 }
2792 \cs_set_eq:NN \@@_true:w #1
2793 \cs_set:Npe \@@_false:w
2794 {
2795 \exp_not:N \group_align_safe_end:
2796 \exp_not:n {#5}
2797 }
2798 \peek_after:Nw #2
2799 }
2800 % \end{macrocode}
2801 % \end{macro}
2802 %
2803 % \begin{macro}[TF]{\@@_token_generic:NN, \@@_token_remove_generic:NN}
2804 % For token removal there needs to be a call to the auxiliary
2805 % function which does the work.
2806 % \begin{macrocode}
2807 \cs_new_protected:Npn \@@_token_generic:NNTF
2808 { \@@_token_generic_aux:NNNTF \@@_true_aux:w }
2809 \cs_new_protected:Npn \@@_token_generic:NNT #1#2#3
2810 { \@@_token_generic:NNTF #1 #2 {#3} { } }
2811 \cs_new_protected:Npn \@@_token_generic:NNF #1#2#3
2812 { \@@_token_generic:NNTF #1 #2 { } {#3} }
2813 \cs_new_protected:Npn \@@_token_remove_generic:NNTF
2814 { \@@_token_generic_aux:NNNTF \@@_true_remove:w }
2815 \cs_new_protected:Npn \@@_token_remove_generic:NNT #1#2#3
2816 { \@@_token_remove_generic:NNTF #1 #2 {#3} { } }
2817 \cs_new_protected:Npn \@@_token_remove_generic:NNF #1#2#3
2818 { \@@_token_remove_generic:NNTF #1 #2 { } {#3} }
2819 % \end{macrocode}
2820 % \end{macro}
2821 %
2822 % \begin{macro}{\@@_execute_branches_meaning:}
2823 % The meaning test is straight forward.
2824 % \begin{macrocode}
2825 \cs_new:Npn \@@_execute_branches_meaning:
2826 {
2827 \if_meaning:w \l_peek_token \l_@@_search_token
2828 \exp_after:wN \@@_true:w
2829 \else:
2830 \exp_after:wN \@@_false:w
2831 \fi:
2832 }
2833 % \end{macrocode}
2834 % \end{macro}
2835 %
2836 % \begin{macro}{\@@_execute_branches_catcode:, \@@_execute_branches_charcode:}
2837 % \begin{macro}
2838 % {
2839 % \@@_execute_branches_catcode_aux: ,
2840 % \@@_execute_branches_catcode_auxii:N ,
2841 % \@@_execute_branches_catcode_auxiii:
2842 % }
2843 % The catcode and charcode tests are very similar, and in order to use
2844 % the same auxiliaries we do something a little bit odd, firing
2845 % \cs{if_catcode:w} and \cs{if_charcode:w} before finding the operands
2846 % for those tests, which are only given in the |auxii:N| and
2847 % |auxiii:| auxiliaries. For our purposes, three kinds of tokens may
2848 % follow the peeking function:
2849 % \begin{itemize}
2850 % \item control sequences which are not equal to a non-active
2851 % character token (\emph{e.g.}, macro, primitive);
2852 % \item active characters which are not equal to a non-active
2853 % character token (\emph{e.g.}, macro, primitive);
2854 % \item explicit non-active character tokens, or control sequences
2855 % or active characters set equal to a non-active character token.
2856 % \end{itemize}
2857 % The first two cases are not distinguishable simply using \TeX{}'s
2858 % \tn{futurelet}, because we can only access the \tn{meaning} of
2859 % tokens in that way. In those cases, detected thanks to a
2860 % comparison with \cs{scan_stop:}, we grab the following token, and
2861 % compare it explicitly with the explicit search token stored in
2862 % \cs{l_@@_search_tl}. The \cs{exp_not:N} prevents outer macros
2863 % (coming from non-\LaTeX3 code) from blowing up. In the third case,
2864 % \cs{l_peek_token} is good enough for the test, and we compare it
2865 % again with the explicit search token. Just like the peek token, the
2866 % search token may be of any of the three types above, hence the need
2867 % to use the explicit token that was given to the peek function.
2868 % \begin{macrocode}
2869 \cs_new:Npn \@@_execute_branches_catcode:
2870 { \if_catcode:w \@@_execute_branches_catcode_aux: }
2871 \cs_new:Npn \@@_execute_branches_charcode:
2872 { \if_charcode:w \@@_execute_branches_catcode_aux: }
2873 \cs_new:Npn \@@_execute_branches_catcode_aux:
2874 {
2875 \if_catcode:w \exp_not:N \l_peek_token \scan_stop:
2876 \exp_after:wN \exp_after:wN
2877 \exp_after:wN \@@_execute_branches_catcode_auxii:N
2878 \exp_after:wN \exp_not:N
2879 \else:
2880 \exp_after:wN \@@_execute_branches_catcode_auxiii:
2881 \fi:
2882 }
2883 \cs_new:Npn \@@_execute_branches_catcode_auxii:N #1
2884 {
2885 \exp_not:N #1
2886 \exp_after:wN \exp_not:N \l_@@_search_tl
2887 \exp_after:wN \@@_true:w
2888 \else:
2889 \exp_after:wN \@@_false:w
2890 \fi:
2891 #1
2892 }
2893 \cs_new:Npn \@@_execute_branches_catcode_auxiii:
2894 {
2895 \exp_not:N \l_peek_token
2896 \exp_after:wN \exp_not:N \l_@@_search_tl
2897 \exp_after:wN \@@_true:w
2898 \else:
2899 \exp_after:wN \@@_false:w
2900 \fi:
2901 }
2902 % \end{macrocode}
2903 % \end{macro}
2904 % \end{macro}
2905 %
2906 % \begin{macro}[TF]
2907 % {
2908 % \peek_catcode:N,
2909 % \peek_catcode_remove:N,
2910 % \peek_charcode:N,
2911 % \peek_charcode_remove:N,
2912 % \peek_meaning:N,
2913 % \peek_meaning_remove:N,
2914 % }
2915 % The public functions themselves cannot be defined using
2916 % \cs{prg_new_protected_conditional:Npnn}. Instead, the |TF|, |T|, |F| variants
2917 % are defined in terms of corresponding variants of
2918 % \cs{@@_token_generic:NNTF} or \cs{@@_token_remove_generic:NNTF},
2919 % with first argument one of \cs{@@_execute_branches_catcode:},
2920 % \cs{@@_execute_branches_charcode:}, or
2921 % \cs{@@_execute_branches_meaning:}.
2922 % \begin{macrocode}
2923 \tl_map_inline:nn { { catcode } { charcode } { meaning } }
2924 {
2925 \tl_map_inline:nn { { } { _remove } }
2926 {
2927 \tl_map_inline:nn { { TF } { T } { F } }
2928 {
2929 \cs_new_protected:cpe { peek_ #1 ##1 :N ####1 }
2930 {
2931 \exp_not:c { @@_token ##1 _generic:NN ####1 }
2932 \exp_not:c { @@_execute_branches_ #1 : }
2933 }
2934 }
2935 }
2936 }
2937 % \end{macrocode}
2938 % \end{macro}
2939 %
2940 % \begin{macro}[TF]{\peek_N_type:}
2941 % \begin{macro}
2942 % {\@@_execute_branches_N_type:, \@@_N_type:w, \@@_N_type_aux:nnw}
2943 % All tokens are \texttt{N}-type tokens, except in four cases:
2944 % begin-group tokens, end-group tokens, space tokens with character
2945 % code~$32$, and outer tokens. Since \cs{l_peek_token} might be
2946 % outer, we cannot use the convenient \cs{bool_if:nTF} function, and
2947 % must resort to the old trick of using \tn{ifodd} to expand a set of
2948 % tests. The \texttt{false} branch of this test is taken if the token
2949 % is one of the first three kinds of non-\texttt{N}-type tokens
2950 % (explicit or implicit), thus we call \cs{@@_false:w}. In the
2951 % \texttt{true} branch, we must detect outer tokens, without impacting
2952 % performance too much for non-outer tokens. The first filter is to
2953 % search for \texttt{outer} in the \tn{meaning} of \cs{l_peek_token}.
2954 % If that is absent, \cs{@@_use_none_delimit_by_s_stop:w} cleans up, and
2955 % we call \cs{@@_true:w}. Otherwise, the token can be a non-outer
2956 % macro or a primitive mark whose parameter or replacement text
2957 % contains \texttt{outer}, it can be the primitive \tn{outer}, or it
2958 % can be an outer token. Macros and marks would have \texttt{ma} in
2959 % the part before the first occurrence of \texttt{outer}; the meaning
2960 % of \tn{outer} has nothing after \texttt{outer}, contrarily to outer
2961 % macros; and that covers all cases, calling \cs{@@_true:w} or
2962 % \cs{@@_false:w} as appropriate. Here, there is no \meta{search
2963 % token}, so we feed a dummy \cs{scan_stop:} to the
2964 % \cs{@@_token_generic:NNTF} function.
2965 % \begin{macrocode}
2966 \group_begin:
2967 \cs_set_protected:Npn \@@_tmp:w #1 \s_@@_stop
2968 {
2969 \cs_new_protected:Npn \@@_execute_branches_N_type:
2970 {
2971 \if_int_odd:w
2972 \if_catcode:w \exp_not:N \l_peek_token { \c_zero_int \fi:
2973 \if_catcode:w \exp_not:N \l_peek_token } \c_zero_int \fi:
2974 \if_meaning:w \l_peek_token \c_space_token \c_zero_int \fi:
2975 \c_one_int
2976 \exp_after:wN \@@_N_type:w
2977 \token_to_meaning:N \l_peek_token
2978 \s_@@_mark \@@_N_type_aux:nnw
2979 #1 \s_@@_mark \@@_use_none_delimit_by_s_stop:w
2980 \s_@@_stop
2981 \exp_after:wN \@@_true:w
2982 \else:
2983 \exp_after:wN \@@_false:w
2984 \fi:
2985 }
2986 \cs_new_protected:Npn \@@_N_type:w ##1 #1 ##2 \s_@@_mark ##3
2987 { ##3 {##1} {##2} }
2988 }
2989 \exp_after:wN \@@_tmp:w \tl_to_str:n { outer } \s_@@_stop
2990 \group_end:
2991 \cs_new_protected:Npn \@@_N_type_aux:nnw #1 #2 #3 \fi:
2992 {
2993 \fi:
2994 \tl_if_in:noTF {#1} { \tl_to_str:n {ma} }
2995 { \@@_true:w }
2996 { \tl_if_empty:nTF {#2} { \@@_true:w } { \@@_false:w } }
2997 }
2998 \cs_new_protected:Npn \peek_N_type:TF
2999 {
3000 \@@_token_generic:NNTF
3001 \@@_execute_branches_N_type: \scan_stop:
3002 }
3003 \cs_new_protected:Npn \peek_N_type:T
3004 { \@@_token_generic:NNT \@@_execute_branches_N_type: \scan_stop: }
3005 \cs_new_protected:Npn \peek_N_type:F
3006 { \@@_token_generic:NNF \@@_execute_branches_N_type: \scan_stop: }
3007 % \end{macrocode}
3008 % \end{macro}
3009 % \end{macro}
3010 %
3011 % \begin{macrocode}
3012 %</code>
3013 % \end{macrocode}
3014 %
3015 % \end{implementation}
3016 %
3017 % \PrintIndex