Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3tl-analysis.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3tl-analysis.dtx
4 %
5 % Copyright (C) 2011-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 %
34 % \title{^^A
35 % The \pkg{l3tl-analysis} module\\ Analyzing token lists^^A
36 % }
37 %
38 % \author{^^A
39 % The \LaTeX{} Project\thanks
40 % {^^A
41 % E-mail:
42 % \href{mailto:[email protected]}
43 % {[email protected]}^^A
44 % }^^A
45 % }
46 %
47 % \date{Released 2026-05-15}
48 %
49 % \maketitle
50 %
51 % \begin{documentation}
52 %
53 % This module provides functions that are particularly useful in the
54 % \pkg{l3regex} module for mapping through a token list one \meta{token}
55 % at a time (including begin-group/end-group tokens). For
56 % \cs{tl_analysis_map_inline:Nn} or \cs{tl_analysis_map_inline:nn}, the
57 % token list is given as an argument; the analogous function
58 % \cs{peek_analysis_map_inline:n} documented in \pkg{l3token} finds
59 % tokens in the input stream instead. In both cases the user provides
60 % \meta{inline code} that receives three arguments for each
61 % \meta{token}:
62 % \begin{itemize}
63 % \item \meta{tokens}, which both \texttt{o}-expand and
64 % \texttt{e}/\texttt{x}-expand to the \meta{token}. The detailed form of
65 % \meta{tokens} may change in later releases.
66 % \item \meta{char code}, a decimal representation of the character
67 % code of the \meta{token}, $-1$ if it is a control sequence.
68 % \item \meta{catcode}, a capital hexadecimal digit which denotes the
69 % category code of the \meta{token} (0:~control sequence,
70 % 1:~begin-group, 2:~end-group, 3:~math shift, 4:~alignment tab,
71 % 6:~parameter, 7:~superscript, 8:~subscript, A:~space, B:~letter,
72 % C:~other, D:~active). This can be converted to an integer by
73 % writing |"|\meta{catcode}.
74 % \end{itemize}
75 % In addition, there is a debugging function \cs{tl_analysis_show:n},
76 % very similar to the \cs[no-index]{ShowTokens} macro from the \pkg{ted} package.
77 %
78 % \begin{function}[added = 2021-05-11]
79 % {
80 % \tl_analysis_show:N, \tl_analysis_show:n,
81 % \tl_analysis_log:N, \tl_analysis_log:n
82 % }
83 % \begin{syntax}
84 % \cs{tl_analysis_show:n} \Arg{token list}
85 % \cs{tl_analysis_log:n} \Arg{token list}
86 % \end{syntax}
87 % Displays to the terminal (or log) the detailed decomposition of the
88 % \meta{token list} into tokens, showing the category code of each
89 % character token, the meaning of control sequences and active
90 % characters, and the value of registers.
91 % \end{function}
92 %
93 % \begin{function}[added = 2018-04-09, updated = 2022-03-26]
94 % {\tl_analysis_map_inline:nn, \tl_analysis_map_inline:Nn}
95 % \begin{syntax}
96 % \cs{tl_analysis_map_inline:nn} \Arg{token list} \Arg{inline function}
97 % \end{syntax}
98 % Applies the \meta{inline function} to each individual \meta{token}
99 % in the \meta{token list}. The \meta{inline function} receives three
100 % arguments as explained above. As all other mappings the mapping is
101 % done at the current group level, i.e., any local assignments
102 % made by the \meta{inline function} remain in effect after the loop.
103 % \end{function}
104 %
105 % \end{documentation}
106 %
107 % \begin{implementation}
108 %
109 % \section{\pkg{l3tl-analysis} implementation}
110 %
111 % \begin{macrocode}
112 %<@@=tl>
113 % \end{macrocode}
114 %
115 % \subsection{Internal functions}
116 %
117 % \begin{variable}{\s_@@}
118 % The format used to store token lists internally uses the scan mark
119 % \cs{s_@@} as a delimiter.
120 % \end{variable}
121 %
122 % \subsection{Internal format}
123 %
124 % The task of the \pkg{l3tl-analysis} module is to convert token lists
125 % to an internal format which allows us to extract all the relevant
126 % information about individual tokens (category code, character code),
127 % as well as reconstruct the token list quickly. This internal format is
128 % used in \pkg{l3regex} where we need to support arbitrary tokens, and
129 % it is used in conversion functions in \pkg{l3str-convert}, where we wish to
130 % support clusters of characters instead of single tokens.
131 %
132 % We thus need a way to encode any \meta{token} (even begin-group and
133 % end-group character tokens) in a way amenable to manipulating tokens
134 % individually. The best we can do is to find \meta{tokens} which both
135 % \texttt{o}-expand and \texttt{e}/\texttt{x}-expand to the given
136 % \meta{token}. Collecting more information about the category code and
137 % character code is also useful for regular expressions, since most
138 % regexes are catcode-agnostic. The internal format thus takes the form
139 % of a succession of items of the form
140 % \begin{quote}
141 % \meta{tokens} \cs{s_@@} \meta{catcode} \meta{char code} \cs{s_@@}
142 % \end{quote}
143 % The \meta{tokens} \texttt{o}- \emph{and} \texttt{e}/\texttt{x}-expand to the
144 % original token in the token list or to the cluster of tokens
145 % corresponding to one Unicode character in the given encoding (for
146 % \pkg{l3str-convert}). The \meta{catcode} is given as a single hexadecimal
147 % digit, $0$ for control sequences. The \meta{char code} is given as a
148 % decimal number, $-1$ for control sequences.
149 %
150 % Using delimited arguments lets us build the \meta{tokens}
151 % progressively when doing an encoding conversion in \pkg{l3str-convert}. On the
152 % other hand, the delimiter \cs{s_@@} may not appear unbraced in
153 % \meta{tokens}. This is not a problem because we are careful to wrap
154 % control sequences in braces (as an argument to \cs{exp_not:n}) when
155 % converting from a general token list to the internal format.
156 %
157 % The current rule for converting a \meta{token} to a balanced set of
158 % \meta{tokens} which both \texttt{o}-expands and \texttt{e}/\texttt{x}-expands to
159 % it is the following.
160 % \begin{itemize}
161 % \item A control sequence |\cs| becomes |\exp_not:n { \cs }|
162 % \cs{s_@@} $0$ $-1$ \cs{s_@@}.
163 % \item A begin-group character |{| becomes \cs{exp_after:wN} |{|
164 % \cs{if_false:} |}| \cs{fi:} \cs{s_@@} $1$ \meta{char code}
165 % \cs{s_@@}.
166 % \item An end-group character |}| becomes \cs{if_false:} |{| \cs{fi:}
167 % |}| \cs{s_@@} $2$ \meta{char code} \cs{s_@@}.
168 % \item A character with any other category code becomes
169 % \cs{exp_not:n} \Arg{character} \cs{s_@@} \meta{hex catcode}
170 % \meta{char code} \cs{s_@@}.
171 % \end{itemize}
172 % In contrast, for \cs{peek_analysis_map_inline:n} we must allow for an
173 % input stream containing \tn{outer} macros, so that wrapping all
174 % control sequences in \cs{exp_not:n} is unsafe. Instead, we write the
175 % more elaborate \cs{__kernel_exp_not:w} \cs{exp_after:wN} |{|
176 % \cs{exp_not:N} |\cs| |}|. (On the other hand we make a better effort
177 % by avoiding \cs{exp_not:n} for characters other than active and macro
178 % parameters.)
179 %
180 % \begin{macrocode}
181 %<*code>
182 % \end{macrocode}
183 %
184 % \subsection{Variables and helper functions}
185 %
186 % \begin{variable}{\s_@@}
187 % The scan mark \cs{s_@@} is used as a delimiter in the internal
188 % format. This is more practical than using a quark, because we would
189 % then need to control expansion much more carefully: compare
190 % \cs{int_value:w} |`#1| \cs{s_@@} with \cs{int_value:w} |`#1|
191 % \cs{exp_stop_f:} \cs{exp_not:N} \cs{q_mark} to extract a character
192 % code followed by the delimiter in an \texttt{e}-expansion.
193 % \begin{macrocode}
194 \scan_new:N \s_@@
195 % \end{macrocode}
196 % \end{variable}
197 %
198 % \begin{variable}
199 % {\l_@@_analysis_token, \l_@@_analysis_char_token}
200 % The tokens in the token list are probed with the \TeX{} primitive
201 % \tn{futurelet}. We use \cs{l_@@_analysis_token} in that
202 % construction. In some cases, we convert the following token to a
203 % string before probing it: then the token variable used is
204 % \cs{l_@@_analysis_char_token}.
205 % \begin{macrocode}
206 \cs_new_eq:NN \l_@@_analysis_token ?
207 \cs_new_eq:NN \l_@@_analysis_char_token ?
208 % \end{macrocode}
209 % \end{variable}
210 %
211 % \begin{variable}{\l_@@_peek_code_tl}
212 % Holds some code to be run once the next token has been fully
213 % analyzed in \cs{peek_analysis_map_inline:n}.
214 % \begin{macrocode}
215 \tl_new:N \l_@@_peek_code_tl
216 % \end{macrocode}
217 % \end{variable}
218 %
219 % \begin{variable}{\c_@@_peek_catcodes_tl}
220 % A token list containing the character number~$32$ (space) with all
221 % possible category codes except $1$ and $2$ (begin-group and
222 % end-group). Why $32$? Because some \LuaTeX{} versions only allow
223 % creation of catcode~$10$ (space) tokens with this character code, so
224 % that we decided to make \cs{char_generate:nn} refuse to create such
225 % weird spaces as well. We do not include the macro parameter case
226 % (catcode~$6$) because it cannot be used as a macro delimiter.
227 % \begin{macrocode}
228 \group_begin:
229 \char_set_active_eq:NN \ \scan_stop:
230 \tl_const:Ne \c_@@_peek_catcodes_tl
231 {
232 \char_generate:nn { 32 } { 3 } 3
233 \char_generate:nn { 32 } { 4 } 4
234 \char_generate:nn { 32 } { 7 } 7
235 \char_generate:nn { 32 } { 8 } 8
236 \c_space_tl \token_to_str:N A
237 \char_generate:nn { 32 } { 11 } \token_to_str:N B
238 \char_generate:nn { 32 } { 12 } \token_to_str:N C
239 \char_generate:nn { 32 } { 13 } \token_to_str:N D
240 }
241 \group_end:
242 % \end{macrocode}
243 % \end{variable}
244 %
245 % \begin{variable}{\l_@@_analysis_normal_int}
246 % The number of normal (\texttt{N}-type argument) tokens since the
247 % last special token.
248 % \begin{macrocode}
249 \int_new:N \l_@@_analysis_normal_int
250 % \end{macrocode}
251 % \end{variable}
252 %
253 % \begin{variable}{\l_@@_analysis_index_int}
254 % During the first pass, this is the index in the array being built.
255 % During the second pass, it is equal to the maximum index in the
256 % array from the first pass.
257 % \begin{macrocode}
258 \int_new:N \l_@@_analysis_index_int
259 % \end{macrocode}
260 % \end{variable}
261 %
262 % \begin{variable}{\l_@@_analysis_nesting_int}
263 % Nesting depth of explicit begin-group and end-group characters
264 % during the first pass. This lets us detect the end of the token list
265 % without a reserved end-marker.
266 % \begin{macrocode}
267 \int_new:N \l_@@_analysis_nesting_int
268 % \end{macrocode}
269 % \end{variable}
270 %
271 % \begin{variable}{\l_@@_analysis_type_int}
272 % When encountering special characters, we record their \enquote{type}
273 % in this integer.
274 % \begin{macrocode}
275 \int_new:N \l_@@_analysis_type_int
276 % \end{macrocode}
277 % \end{variable}
278 %
279 % \begin{variable}{\g_@@_analysis_result_tl}
280 % The result of the conversion is stored in this token list, with a
281 % succession of items of the form
282 % \begin{quote}
283 % \meta{tokens} \cs{s_@@} \meta{catcode} \meta{char code} \cs{s_@@}
284 % \end{quote}
285 % \begin{macrocode}
286 \tl_new:N \g_@@_analysis_result_tl
287 % \end{macrocode}
288 % \end{variable}
289 %
290 % \begin{macro}[EXP]{\@@_analysis_extract_charcode:}
291 % \begin{macro}[EXP]{\@@_analysis_extract_charcode_aux:w}
292 % Extracting the character code from the meaning of
293 % \cs{l_@@_analysis_token}. This has no error checking, and should
294 % only be assumed to work for begin-group and end-group character
295 % tokens. It produces a number in the form |`|\meta{char}. LuaMeta\TeX{}
296 % uses a different format for \tn{meaning}, so there is a little work to
297 % do: we extract the Unicode value this contains.
298 % \begin{macrocode}
299 \cs_new:Npn \@@_analysis_extract_charcode:
300 {
301 \exp_after:wN \@@_analysis_extract_charcode_aux:w
302 \token_to_meaning:N \l_@@_analysis_token
303 }
304 \cs_new:Npn \@@_analysis_extract_charcode_aux:w #1 ~ #2 ~ { ` }
305 \bool_lazy_and:nnT
306 { \cs_if_exist_p:N \tex_luatexversion:D }
307 { \int_compare_p:nNn { \int_div_truncate:nn { \tex_luatexversion:D } { 100 } } > 1 }
308 {
309 \cs_gset:Npn \@@_analysis_extract_charcode_aux:w #1 + #2 ~ ' #3 ' {"#2}
310 }
311 % \end{macrocode}
312 % \end{macro}
313 % \end{macro}
314 %
315 % \begin{macro}[EXP]{\@@_analysis_cs_space_count:NN}
316 % \begin{macro}[EXP]{\@@_analysis_cs_space_count:w}
317 % \begin{macro}[EXP]{\@@_analysis_cs_space_count_end:w}
318 % Counts the number of spaces in the string representation of its
319 % second argument, as well as the number of characters following the
320 % last space in that representation, and feeds the two numbers as
321 % \cs{@@_sep:}-delimited arguments to the first argument. When this
322 % function is used, the escape character is printable and non-space.
323 % \begin{macrocode}
324 \cs_new:Npe \@@_analysis_cs_space_count:NN #1 #2
325 {
326 \exp_not:N \exp_after:wN #1
327 \exp_not:N \int_value:w \exp_not:N \int_eval:w 0
328 \exp_not:N \exp_after:wN \exp_not:N \@@_analysis_cs_space_count:w
329 \exp_not:N \token_to_str:N #2
330 \exp_not:N \fi: \exp_not:N \@@_analysis_cs_space_count_end:w
331 \exp_not:N \@@_sep: \c_space_tl !
332 }
333 \cs_new:Npn \@@_analysis_cs_space_count:w #1 ~
334 {
335 \if_false: #1 #1 \fi:
336 + 1
337 \@@_analysis_cs_space_count:w
338 }
339 \cs_new:Npn \@@_analysis_cs_space_count_end:w \@@_sep: #1 \fi: #2 !
340 {
341 \exp_after:wN \@@_sep: \int_value:w
342 \str_count_ignore_spaces:n {#1} \@@_sep:
343 }
344 % \end{macrocode}
345 % \end{macro}
346 % \end{macro}
347 % \end{macro}
348 %
349 % \subsection{Plan of attack}
350 %
351 % Our goal is to produce a token list of the form roughly
352 % \begin{quote}
353 % \meta{token 1} \cs{s_@@} \meta{catcode 1} \meta{char code 1} \cs{s_@@} \\
354 % \meta{token 2} \cs{s_@@} \meta{catcode 2} \meta{char code 2} \cs{s_@@} \\
355 % \ldots{}
356 % \meta{token N} \cs{s_@@} \meta{catcode N} \meta{char code N} \cs{s_@@}
357 % \end{quote}
358 % Most but not all tokens can be grabbed as an undelimited
359 % (\texttt{N}-type) argument by \TeX{}. The plan is to have a two pass
360 % system. In the first pass, locate special tokens, and store them in
361 % various \tn{toks} registers. In the second pass, which is done within
362 % an \texttt{e}-expanding assignment, normal tokens are taken in as
363 % \texttt{N}-type arguments, and special tokens are retrieved from the
364 % \tn{toks} registers, and removed from the input stream by some means.
365 % The whole process takes linear time, because we avoid building the
366 % result one item at a time.
367 %
368 % We make the escape character printable (backslash, but this later
369 % oscillates between slash and backslash): this allows us to
370 % distinguish characters from control sequences.
371 %
372 % A token has two characteristics: its \tn{meaning}, and what it looks
373 % like for \TeX{} when it is in scanning mode (\emph{e.g.}, when
374 % capturing parameters for a macro). For our purposes, we distinguish
375 % the following meanings:
376 % \begin{itemize}
377 % \item begin-group token (category code $1$), either space (character
378 % code $32$), or non-space;
379 % \item end-group token (category code $2$), either space (character
380 % code $32$), or non-space;
381 % \item space token (category code $10$, character code $32$);
382 % \item anything else (then the token is always an \texttt{N}-type
383 % argument).
384 % \end{itemize}
385 % The token itself can \enquote{look like} one of the following
386 % \begin{itemize}
387 % \item a non-active character, in which case its meaning is
388 % automatically that associated to its character code and category
389 % code, we call it \enquote{true} character;
390 % \item an active character;
391 % \item a control sequence.
392 % \end{itemize}
393 % The only tokens which are not valid \texttt{N}-type arguments are true
394 % begin-group characters, true end-group characters, and true spaces.
395 % We detect those characters by scanning ahead with \tn{futurelet},
396 % then distinguishing true characters from control sequences set equal
397 % to them using the \tn{string} representation.
398 %
399 % The second pass is a simple exercise in expandable loops.
400 %
401 % \begin{macro}{\@@_analysis:n}
402 % Everything is done within a group, and all definitions are
403 % local. We use \cs[no-index]{group_align_safe_begin/end:} to avoid
404 % problems in case \cs{@@_analysis:n} is used within an alignment and
405 % its argument contains alignment tab tokens.
406 % \begin{macrocode}
407 \cs_new_protected:Npn \@@_analysis:n #1
408 {
409 \group_begin:
410 \group_align_safe_begin:
411 \@@_analysis_a:n {#1}
412 \@@_analysis_b:n {#1}
413 \group_align_safe_end:
414 \group_end:
415 }
416 % \end{macrocode}
417 % \end{macro}
418 %
419 % \subsection{Disabling active characters}
420 %
421 % \begin{macro}{\@@_analysis_disable:n}
422 % Active characters can cause problems later on in the processing, so
423 % we provide a way to disable them, by setting them to
424 % \texttt{undefined}. Since Unicode contains too many characters to
425 % loop over all of them, we instead do this whenever we encounter a
426 % character. For \pTeX{} and \upTeX{} we skip characters beyond
427 % $[0,255]$ because \tn{lccode} only allows those values.
428 % \begin{macrocode}
429 \group_begin:
430 \char_set_catcode_active:N \^^@
431 \cs_new_protected:Npn \@@_analysis_disable:n #1
432 {
433 \tex_lccode:D 0 = #1 \exp_stop_f:
434 \tex_lowercase:D { \tex_let:D ^^@ } \tex_undefined:D
435 }
436 \bool_lazy_or:nnT
437 { \sys_if_engine_ptex_p: }
438 { \sys_if_engine_uptex_p: }
439 {
440 \cs_gset_protected:Npn \@@_analysis_disable:n #1
441 {
442 \if_int_compare:w 256 > #1 \exp_stop_f:
443 \tex_lccode:D 0 = #1 \exp_stop_f:
444 \tex_lowercase:D { \tex_let:D ^^@ } \tex_undefined:D
445 \fi:
446 }
447 }
448 \group_end:
449 % \end{macrocode}
450 % \end{macro}
451 %
452 % \begin{macro}{\@@_analysis_disable_char:N}
453 % Similar to \cs{@@_analysis_disable:n}, but it receives a normal
454 % character token, tests if that token is active (by turning it into
455 % a space: the active space has been undefined at this point), and
456 % if so, disables it. Even if the character is active and set equal
457 % to a primitive conditional, nothing blows up.
458 % Again, in \pTeX{} and \upTeX{} we skip characters beyond $[0,255]$,
459 % which cannot be active anyways.
460 % \begin{macrocode}
461 \group_begin:
462 \char_set_catcode_active:N \^^@
463 \cs_new_protected:Npn \@@_analysis_disable_char:N #1
464 {
465 \tex_lccode:D `#1 = 32 \exp_stop_f:
466 \tex_lowercase:D { \if_meaning:w #1 } \tex_undefined:D
467 \tex_let:D #1 \tex_undefined:D
468 \fi:
469 }
470 \bool_lazy_or:nnT
471 { \sys_if_engine_ptex_p: }
472 { \sys_if_engine_uptex_p: }
473 {
474 \cs_gset_protected:Npn \@@_analysis_disable_char:N #1
475 {
476 \if_int_compare:w 256 > `#1 \exp_stop_f:
477 \tex_lccode:D `#1 = 32 \exp_stop_f:
478 \tex_lowercase:D { \if_meaning:w #1 } \tex_undefined:D
479 \tex_let:D #1 \tex_undefined:D
480 \fi:
481 \fi:
482 }
483 }
484 \group_end:
485 % \end{macrocode}
486 % \end{macro}
487 %
488 % \subsection{First pass}
489 %
490 % The goal of this pass is to detect special (non-\texttt{N}-type) tokens,
491 % and count how many \texttt{N}-type tokens lie between special tokens.
492 % Also, we wish to store some representation of each special token
493 % in a \tn{toks} register.
494 %
495 % We have $11$ types of tokens:
496 % \begin{itemize}
497 % \item[1.] a true non-space begin-group character;
498 % \item[2.] a true space begin-group character;
499 % \item[3.] a true non-space end-group character;
500 % \item[4.] a true space end-group character;
501 % \item[5.] a true space blank space character;
502 % \item[6.] an active character;
503 % \item[7.] any other true character;
504 % \item[8.] a control sequence equal to a begin-group token (category code $1$);
505 % \item[9.] a control sequence equal to an end-group token (category code $2$);
506 % \item[10.] a control sequence equal to a space token
507 % (character code $32$, category code $10$);
508 % \item[11.] any other control sequence.
509 % \end{itemize}
510 % Our first tool is \tn{futurelet}. This cannot distinguish
511 % case $8$ from $1$ or $2$, nor case $9$ from $3$ or $4$,
512 % nor case $10$ from case $5$. Those cases are later distinguished
513 % by applying the \tn{string} primitive to the following token,
514 % after possibly changing the escape character to ensure that
515 % a control sequence's string representation cannot be mistaken
516 % for the true character.
517 %
518 % In cases $6$, $7$, and $11$, the following token is a valid
519 % \texttt{N}-type argument, so we grab it and distinguish the case
520 % of a character from a control sequence: in the latter case,
521 % \cs{str_tail:n} \Arg{token} is non-empty, because the
522 % escape character is printable.
523 %
524 % \begin{macro}{\@@_analysis_a:n}
525 % We read tokens one by one using \tn{futurelet}.
526 % While performing the loop, we keep track of the number of
527 % true begin-group characters minus the number of
528 % true end-group characters in \cs{l_@@_analysis_nesting_int}.
529 % This reaches $-1$ when we read the closing brace.
530 % \begin{macrocode}
531 \cs_new_protected:Npn \@@_analysis_a:n #1
532 {
533 \@@_analysis_disable:n { 32 }
534 \int_set:Nn \tex_escapechar:D { 92 }
535 \int_zero:N \l_@@_analysis_normal_int
536 \int_zero:N \l_@@_analysis_index_int
537 \int_zero:N \l_@@_analysis_nesting_int
538 \if_false: { \fi: \@@_analysis_a_loop:w #1 }
539 \int_decr:N \l_@@_analysis_index_int
540 }
541 % \end{macrocode}
542 % \end{macro}
543 %
544 % \begin{macro}{\@@_analysis_a_loop:w}
545 % Read one character and check its type.
546 % \begin{macrocode}
547 \cs_new_protected:Npn \@@_analysis_a_loop:w
548 { \tex_futurelet:D \l_@@_analysis_token \@@_analysis_a_type:w }
549 % \end{macrocode}
550 % \end{macro}
551 %
552 % \begin{macro}{\@@_analysis_a_type:w}
553 % At this point, \cs{l_@@_analysis_token} holds the meaning
554 % of the following token. We store in \cs{l_@@_analysis_type_int}
555 % information about the meaning of the token ahead:
556 % \begin{itemize}
557 % \item 0 space token;
558 % \item 1 begin-group token;
559 % \item -1 end-group token;
560 % \item 2 other.
561 % \end{itemize}
562 % The values $0$, $1$, $-1$ correspond to how much a true such
563 % character changes the nesting level ($2$ is used only here,
564 % and is irrelevant later). Then call the auxiliary for each case.
565 % Note that nesting conditionals here is safe because we only skip
566 % over \cs{l_@@_analysis_token} if it matches with one of the
567 % character tokens (hence is not a primitive conditional).
568 % \begin{macrocode}
569 \cs_new_protected:Npn \@@_analysis_a_type:w
570 {
571 \l_@@_analysis_type_int =
572 \if_meaning:w \l_@@_analysis_token \c_space_token
573 0
574 \else:
575 \if_catcode:w \exp_not:N \l_@@_analysis_token \c_group_begin_token
576 1
577 \else:
578 \if_catcode:w \exp_not:N \l_@@_analysis_token \c_group_end_token
579 - 1
580 \else:
581 2
582 \fi:
583 \fi:
584 \fi:
585 \exp_stop_f:
586 \if_case:w \l_@@_analysis_type_int
587 \exp_after:wN \@@_analysis_a_space:w
588 \or: \exp_after:wN \@@_analysis_a_bgroup:w
589 \or: \exp_after:wN \@@_analysis_a_safe:N
590 \else: \exp_after:wN \@@_analysis_a_egroup:w
591 \fi:
592 }
593 % \end{macrocode}
594 % \end{macro}
595 %
596 % \begin{macro}{\@@_analysis_a_space:w}
597 % \begin{macro}{\@@_analysis_a_space_test:w}
598 % In this branch, the following token's meaning is a blank space.
599 % Apply \tn{string} to that token: a true blank space gives a space, a
600 % control sequence gives a result starting with the escape character,
601 % an active character gives something else than a space since we
602 % disabled the space. We grab as \cs{l_@@_analysis_char_token} the first
603 % character of the string representation then test it in
604 % \cs{@@_analysis_a_space_test:w}.
605 % Also, since \cs{@@_analysis_a_store:} expects the special token to be
606 % stored in the relevant \tn{toks} register, we do that. The extra
607 % \cs{exp_not:n} is unnecessary of course, but it makes the treatment
608 % of all tokens more homogeneous.
609 % If we discover that the next token was actually a control sequence
610 % or an active character
611 % instead of a true space, then we step the counter of normal tokens.
612 % We now have in front of us the whole string representation of
613 % the control sequence, including potential spaces; those will appear
614 % to be true spaces later in this pass. Hence, all other branches of
615 % the code in this first pass need to consider the string representation,
616 % so that the second pass does not need to test the meaning of tokens,
617 % only strings.
618 % \begin{macrocode}
619 \cs_new_protected:Npn \@@_analysis_a_space:w
620 {
621 \tex_afterassignment:D \@@_analysis_a_space_test:w
622 \exp_after:wN \cs_set_eq:NN
623 \exp_after:wN \l_@@_analysis_char_token
624 \token_to_str:N
625 }
626 \cs_new_protected:Npn \@@_analysis_a_space_test:w
627 {
628 \if_meaning:w \l_@@_analysis_char_token \c_space_token
629 \tex_toks:D \l_@@_analysis_index_int { \exp_not:n { ~ } }
630 \@@_analysis_a_store:
631 \else:
632 \int_incr:N \l_@@_analysis_normal_int
633 \fi:
634 \@@_analysis_a_loop:w
635 }
636 % \end{macrocode}
637 % \end{macro}
638 % \end{macro}
639 %
640 % \begin{macro}{\@@_analysis_a_bgroup:w, \@@_analysis_a_egroup:w}
641 % \begin{macro}
642 % {\@@_analysis_a_group:nw, \@@_analysis_a_group_aux:w, \@@_analysis_a_group_auxii:w, \@@_analysis_a_group_test:w}
643 % The token is most likely a true character token with catcode $1$ or
644 % $2$, but it might be a control sequence, or an active character.
645 % Optimizing for the first case, we store in a toks register some code
646 % that expands to that token. Since we will turn what follows into
647 % a string, we make sure the escape character is different from the
648 % current character code (by switching between solidus and backslash).
649 % To detect the special case of an active character let to the catcode
650 % $1$ or~$2$ character with the same character code, we disable the
651 % active character with that character code and re-test: if the
652 % following token has become undefined we can in fact safely grab it.
653 % We are finally ready to turn what follows to a string and test it.
654 % This is one place where we need \cs{l_@@_analysis_char_token} to be a
655 % separate control sequence from \cs{l_@@_analysis_token}, to compare them.
656 % \begin{macrocode}
657 \group_begin:
658 \char_set_catcode_group_begin:N \^^@ % {
659 \cs_new_protected:Npn \@@_analysis_a_bgroup:w
660 { \@@_analysis_a_group:nw { \exp_after:wN ^^@ \if_false: } \fi: } }
661 \char_set_catcode_group_end:N \^^@
662 \cs_new_protected:Npn \@@_analysis_a_egroup:w
663 { \@@_analysis_a_group:nw { \if_false: { \fi: ^^@ } } % }
664 \group_end:
665 \cs_new_protected:Npn \@@_analysis_a_group:nw #1
666 {
667 \tex_lccode:D 0 = \@@_analysis_extract_charcode: \scan_stop:
668 \tex_lowercase:D { \tex_toks:D \l_@@_analysis_index_int {#1} }
669 \if_int_compare:w \tex_lccode:D 0 = \tex_escapechar:D
670 \int_set:Nn \tex_escapechar:D { 139 - \tex_escapechar:D }
671 \fi:
672 \@@_analysis_disable:n { \tex_lccode:D 0 }
673 \tex_futurelet:D \l_@@_analysis_token \@@_analysis_a_group_aux:w
674 }
675 \cs_new_protected:Npn \@@_analysis_a_group_aux:w
676 {
677 \if_meaning:w \l_@@_analysis_token \tex_undefined:D
678 \exp_after:wN \@@_analysis_a_safe:N
679 \else:
680 \exp_after:wN \@@_analysis_a_group_auxii:w
681 \fi:
682 }
683 \cs_new_protected:Npn \@@_analysis_a_group_auxii:w
684 {
685 \tex_afterassignment:D \@@_analysis_a_group_test:w
686 \exp_after:wN \cs_set_eq:NN
687 \exp_after:wN \l_@@_analysis_char_token
688 \token_to_str:N
689 }
690 \cs_new_protected:Npn \@@_analysis_a_group_test:w
691 {
692 \if_charcode:w \l_@@_analysis_token \l_@@_analysis_char_token
693 \@@_analysis_a_store:
694 \else:
695 \int_incr:N \l_@@_analysis_normal_int
696 \fi:
697 \@@_analysis_a_loop:w
698 }
699 % \end{macrocode}
700 % \end{macro}
701 % \end{macro}
702 %
703 % \begin{macro}{\@@_analysis_a_store:}
704 % This function is called each time we meet a special token;
705 % at this point, the \tn{toks} register \cs{l_@@_analysis_index_int}
706 % holds a token list which expands to the given special token.
707 % Also, the value of \cs{l_@@_analysis_type_int} indicates which case
708 % we are in:
709 % \begin{itemize}
710 % \item -1 end-group character;
711 % \item 0 space character;
712 % \item 1 begin-group character.
713 % \end{itemize}
714 % We need to distinguish further the case of a space character
715 % (code $32$) from other character codes, because those
716 % behave differently in the second pass. Namely, after testing
717 % the \tn{lccode} of $0$ (which holds the present character code)
718 % we change the cases above to
719 % \begin{itemize}
720 % \item -2 space end-group character;
721 % \item -1 non-space end-group character;
722 % \item 0 space blank space character;
723 % \item 1 non-space begin-group character;
724 % \item 2 space begin-group character.
725 % \end{itemize}
726 % This has the property that non-space characters correspond to odd
727 % values of \cs{l_@@_analysis_type_int}. The number of normal tokens until
728 % here and the type of special token are packed into a \tn{skip}
729 % register. Finally, we check whether we reached the last closing
730 % brace, in which case we stop by disabling the looping function
731 % (locally).
732 % \begin{macrocode}
733 \cs_new_protected:Npn \@@_analysis_a_store:
734 {
735 \tex_advance:D \l_@@_analysis_nesting_int \l_@@_analysis_type_int
736 \if_int_compare:w \tex_lccode:D 0 = `\ \exp_stop_f:
737 \tex_advance:D \l_@@_analysis_type_int \l_@@_analysis_type_int
738 \fi:
739 \tex_skip:D \l_@@_analysis_index_int
740 = \l_@@_analysis_normal_int sp
741 plus \l_@@_analysis_type_int sp \scan_stop:
742 \int_incr:N \l_@@_analysis_index_int
743 \int_zero:N \l_@@_analysis_normal_int
744 \if_int_compare:w \l_@@_analysis_nesting_int = - \c_one_int
745 \cs_set_eq:NN \@@_analysis_a_loop:w \scan_stop:
746 \fi:
747 }
748 % \end{macrocode}
749 % \end{macro}
750 %
751 % \begin{macro}{\@@_analysis_a_safe:N}
752 % \begin{macro}{\@@_analysis_a_cs:ww}
753 % This should be the simplest case: since the upcoming token is safe,
754 % we can simply grab it in a second pass. If the token is a single
755 % character (including space), the \cs{if_charcode:w} test yields
756 % true; we disable a potentially active character (that could
757 % otherwise masquerade as the true character in the next pass) and we
758 % count one \enquote{normal} token. On the other
759 % hand, if the token is a control sequence, we should replace it by
760 % its string representation for compatibility with other code
761 % branches. Instead of slowly looping through the characters with
762 % the main code, we use the knowledge of how the second pass works:
763 % if the control sequence name contains no space, count that token
764 % as a number of normal tokens equal to its string length. If the
765 % control sequence contains spaces, they should be registered as
766 % special characters by increasing \cs{l_@@_analysis_index_int}
767 % (no need to carefully count character between each space), and
768 % all characters after the last space should be counted in the
769 % following sequence of \enquote{normal} tokens.
770 % \begin{macrocode}
771 \cs_new_protected:Npn \@@_analysis_a_safe:N #1
772 {
773 \if_charcode:w
774 \scan_stop:
775 \exp_after:wN \use_none:n \token_to_str:N #1 \prg_do_nothing:
776 \scan_stop:
777 \exp_after:wN \use_i:nn
778 \else:
779 \exp_after:wN \use_ii:nn
780 \fi:
781 {
782 \@@_analysis_disable_char:N #1
783 \int_incr:N \l_@@_analysis_normal_int
784 }
785 { \@@_analysis_cs_space_count:NN \@@_analysis_a_cs:ww #1 }
786 \@@_analysis_a_loop:w
787 }
788 \cs_new_protected:Npn \@@_analysis_a_cs:ww #1 \@@_sep: #2 \@@_sep:
789 {
790 \if_int_compare:w #1 > \c_zero_int
791 \tex_skip:D \l_@@_analysis_index_int
792 = \int_eval:n { \l_@@_analysis_normal_int + 1 } sp \exp_stop_f:
793 \tex_advance:D \l_@@_analysis_index_int #1 \exp_stop_f:
794 \else:
795 \tex_advance:D
796 \fi:
797 \l_@@_analysis_normal_int #2 \exp_stop_f:
798 }
799 % \end{macrocode}
800 % \end{macro}
801 % \end{macro}
802 %
803 % \subsection{Second pass}
804 %
805 % The second pass is an exercise in expandable loops.
806 % All the necessary information is stored in \tn{skip}
807 % and \tn{toks} registers.
808 %
809 % \begin{macro}{\@@_analysis_b:n}
810 % \begin{macro}[EXP]{\@@_analysis_b_loop:w}
811 % Start the loop with the index $0$. No need for an end-marker:
812 % the loop stops by itself when the last index is read.
813 % We repeatedly oscillate between reading long stretches
814 % of normal tokens, and reading special tokens.
815 % \begin{macrocode}
816 \cs_new_protected:Npn \@@_analysis_b:n #1
817 {
818 \__kernel_tl_gset:Nx \g_@@_analysis_result_tl
819 {
820 \@@_analysis_b_loop:w 0 \@@_sep: #1
821 \prg_break_point:
822 }
823 }
824 \cs_new:Npn \@@_analysis_b_loop:w #1 \@@_sep:
825 {
826 \exp_after:wN \@@_analysis_b_normals:ww
827 \int_value:w \tex_skip:D #1 \@@_sep: #1 \@@_sep:
828 }
829 % \end{macrocode}
830 % \end{macro}
831 % \end{macro}
832 %
833 % \begin{macro}[EXP]{\@@_analysis_b_normals:ww}
834 % \begin{macro}[EXP]{\@@_analysis_b_normal:wwN}
835 % The first argument is the number of normal tokens which remain
836 % to be read, and the second argument is the index in the array
837 % produced in the first step.
838 % A character's string representation is always one character long,
839 % while a control sequence is always longer (we have set the escape
840 % character to a printable value). In both cases, we leave
841 % \cs{exp_not:n} \Arg{token} \cs{s_@@} in the input stream
842 % (after \texttt{e}-expansion). Here, \cs{exp_not:n} is used
843 % rather than \cs{exp_not:N} because |#3| could be
844 % a macro parameter character or could be \cs{s_@@}
845 % (which must be hidden behind braces in the result).
846 % \begin{macrocode}
847 \cs_new:Npn \@@_analysis_b_normals:ww #1 \@@_sep:
848 {
849 \if_int_compare:w #1 = \c_zero_int
850 \@@_analysis_b_special:w
851 \fi:
852 \@@_analysis_b_normal:wwN #1 \@@_sep:
853 }
854 \cs_new:Npn \@@_analysis_b_normal:wwN #1 \@@_sep: #2 \@@_sep: #3
855 {
856 \exp_not:n { \exp_not:n { #3 } } \s_@@
857 \if_charcode:w
858 \scan_stop:
859 \exp_after:wN \use_none:n \token_to_str:N #3 \prg_do_nothing:
860 \scan_stop:
861 \exp_after:wN \@@_analysis_b_char:Nn
862 \exp_after:wN \@@_analysis_b_char_aux:nww
863 \else:
864 \exp_after:wN \@@_analysis_b_cs:Nww
865 \fi:
866 #3 #1 \@@_sep: #2 \@@_sep:
867 }
868 % \end{macrocode}
869 % \end{macro}
870 % \end{macro}
871 %
872 % \begin{macro}[EXP]{\@@_analysis_b_char:Nn, \@@_analysis_b_char_aux:nww}
873 % This function is called here with arguments
874 % \cs{@@_analysis_b_char_aux:nww} and a normal character, while in the
875 % peek analysis code it is called with \cs{use_none:n} and possibly a
876 % space character, which is why the function has signature |Nn|.
877 % If the normal token we grab is a character, leave
878 % \meta{catcode} \meta{charcode} followed by \cs{s_@@}
879 % in the input stream, and call \cs{@@_analysis_b_normals:ww}
880 % with its first argument decremented.
881 % \begin{macrocode}
882 \cs_new:Npe \@@_analysis_b_char:Nn #1#2
883 {
884 \exp_not:N \if_meaning:w #2 \exp_not:N \tex_undefined:D
885 \token_to_str:N D \exp_not:N \else:
886 \exp_not:N \if_catcode:w #2 \c_catcode_other_token
887 \token_to_str:N C \exp_not:N \else:
888 \exp_not:N \if_catcode:w #2 \c_catcode_letter_token
889 \token_to_str:N B \exp_not:N \else:
890 \exp_not:N \if_catcode:w #2 \c_math_toggle_token 3
891 \exp_not:N \else:
892 \exp_not:N \if_catcode:w #2 \c_alignment_token 4
893 \exp_not:N \else:
894 \exp_not:N \if_catcode:w #2 \c_math_superscript_token 7
895 \exp_not:N \else:
896 \exp_not:N \if_catcode:w #2 \c_math_subscript_token 8
897 \exp_not:N \else:
898 \exp_not:N \if_catcode:w #2 \c_space_token
899 \token_to_str:N A \exp_not:N \else:
900 6
901 \exp_not:n { \fi: \fi: \fi: \fi: \fi: \fi: \fi: \fi: }
902 #1 {#2}
903 }
904 \cs_new:Npn \@@_analysis_b_char_aux:nww #1
905 {
906 \int_value:w `#1 \s_@@
907 \exp_after:wN \@@_analysis_b_normals:ww
908 \int_value:w \int_eval:w - 1 +
909 }
910 % \end{macrocode}
911 % \end{macro}
912 %
913 % \begin{macro}[EXP]{\@@_analysis_b_cs:Nww}
914 % \begin{macro}[EXP]{\@@_analysis_b_cs_test:ww}
915 % If the token we grab is a control sequence, leave
916 % |0 -1| (as category code and character code) in the input stream,
917 % followed by \cs{s_@@},
918 % and call \cs{@@_analysis_b_normals:ww} with updated arguments.
919 % \begin{macrocode}
920 \cs_new:Npn \@@_analysis_b_cs:Nww #1
921 {
922 0 -1 \s_@@
923 \@@_analysis_cs_space_count:NN \@@_analysis_b_cs_test:ww #1
924 }
925 \cs_new:Npn \@@_analysis_b_cs_test:ww
926 #1 \@@_sep: #2 \@@_sep: #3 \@@_sep: #4 \@@_sep:
927 {
928 \exp_after:wN \@@_analysis_b_normals:ww
929 \int_value:w \int_eval:w
930 \if_int_compare:w #1 = \c_zero_int
931 #3
932 \else:
933 \tex_skip:D \int_eval:n { #4 + #1 } \exp_stop_f:
934 \fi:
935 - #2
936 \exp_after:wN \@@_sep:
937 \int_value:w \int_eval:n { #4 + #1 } \@@_sep:
938 }
939 % \end{macrocode}
940 % \end{macro}
941 % \end{macro}
942 %
943 % \begin{macro}[EXP]{\@@_analysis_b_special:w}
944 % \begin{macro}[EXP]{\@@_analysis_b_special_char:wN}
945 % \begin{macro}[EXP]{\@@_analysis_b_special_space:w}
946 % Here, |#1| is the current index in the array built in the first pass.
947 % Check now whether we reached the end (we shouldn't keep the trailing
948 % end-group character that marked the end of the token list in the
949 % first pass).
950 % Unpack the \tn{toks} register: when \texttt{e}/\texttt{x}-expanding again,
951 % we will get the special token.
952 % Then leave the category code in the input stream, followed by
953 % the character code, and call \cs{@@_analysis_b_loop:w} with the next index.
954 % \begin{macrocode}
955 \group_begin:
956 \char_set_catcode_other:N A
957 \cs_new:Npn \@@_analysis_b_special:w
958 \fi: \@@_analysis_b_normal:wwN 0 \@@_sep: #1 \@@_sep:
959 {
960 \fi:
961 \if_int_compare:w #1 = \l_@@_analysis_index_int
962 \exp_after:wN \prg_break:
963 \fi:
964 \tex_the:D \tex_toks:D #1 \s_@@
965 \if_case:w \tex_gluestretch:D \tex_skip:D #1 \exp_stop_f:
966 \token_to_str:N A
967 \or: 1
968 \or: 1
969 \else: 2
970 \fi:
971 \if_int_odd:w \tex_gluestretch:D \tex_skip:D #1 \exp_stop_f:
972 \exp_after:wN \@@_analysis_b_special_char:wN \int_value:w
973 \else:
974 \exp_after:wN \@@_analysis_b_special_space:w \int_value:w
975 \fi:
976 \int_eval:n { 1 + #1 } \exp_after:wN \@@_sep:
977 \token_to_str:N
978 }
979 \group_end:
980 \cs_new:Npn \@@_analysis_b_special_char:wN #1 \@@_sep: #2
981 {
982 \int_value:w `#2 \s_@@
983 \@@_analysis_b_loop:w #1 \@@_sep:
984 }
985 \use:e
986 {
987 \cs_new:Npn \exp_not:N \@@_analysis_b_special_space:w
988 #1 \exp_not:N \@@_sep: \c_space_tl
989 }
990 {
991 32 \s_@@
992 \@@_analysis_b_loop:w #1 \@@_sep:
993 }
994 % \end{macrocode}
995 % \end{macro}
996 % \end{macro}
997 % \end{macro}
998 %
999 % \subsection{Mapping through the analysis}
1000 %
1001 % \begin{macro}{\tl_analysis_map_inline:Nn, \tl_analysis_map_inline:nn}
1002 % \begin{macro}{\@@_analysis_map:Nn}
1003 % \begin{macro}{\@@_analysis_map:NwNw}
1004 % First obtain the analysis of the token list into
1005 % \cs{g_@@_analysis_result_tl}. To allow nested mappings, increase the
1006 % nesting depth \cs{g__kernel_prg_map_int} (shared between all
1007 % modules), then define the payload macro, which runs the user code
1008 % and has a name specific to that nesting depth. The looping macro
1009 % grabs the \meta{tokens}, \meta{catcode} and \meta{char code}; it
1010 % checks for the end of the loop with \cs{use_none:n} |##2|, normally
1011 % empty, but which becomes \cs{tl_map_break:} at the end; it then
1012 % calls the payload macro with the arguments in the correct order
1013 % (this is the reason why we cannot directly use the same macro for
1014 % looping and payload), and loops by calling itself. When the loop
1015 % ends, remember to decrease the nesting depth.
1016 % \begin{macrocode}
1017 \cs_new_protected:Npn \tl_analysis_map_inline:Nn #1
1018 { \exp_args:No \tl_analysis_map_inline:nn #1 }
1019 \cs_new_protected:Npn \tl_analysis_map_inline:nn #1
1020 {
1021 \@@_analysis:n {#1}
1022 \int_gincr:N \g__kernel_prg_map_int
1023 \exp_args:Nc \@@_analysis_map:Nn
1024 { @@_analysis_map_inline_ \int_use:N \g__kernel_prg_map_int :wNw }
1025 }
1026 \cs_new_protected:Npn \@@_analysis_map:Nn #1#2
1027 {
1028 \cs_gset_protected:Npn #1 ##1##2##3 {#2}
1029 \exp_after:wN \@@_analysis_map:NwNw \exp_after:wN #1
1030 \g_@@_analysis_result_tl
1031 \s_@@ { ? \tl_map_break: } \s_@@
1032 \prg_break_point:Nn \tl_map_break:
1033 { \int_gdecr:N \g__kernel_prg_map_int }
1034 }
1035 \cs_new_protected:Npn \@@_analysis_map:NwNw #1 #2 \s_@@ #3 #4 \s_@@
1036 {
1037 \use_none:n #3
1038 #1 {#2} {#4} {#3}
1039 \@@_analysis_map:NwNw #1
1040 }
1041 % \end{macrocode}
1042 % \end{macro}
1043 % \end{macro}
1044 % \end{macro}
1045 %
1046 % \subsection{Showing the results}
1047 %
1048 % \begin{macro}{\tl_analysis_show:N, \tl_analysis_log:N, \@@_analysis_show:NNnnN}
1049 % Add to \cs{@@_analysis:n} a third pass to display tokens to the terminal.
1050 % If the token list variable is not defined, throw the same error
1051 % as \cs{tl_show:N} by simply calling that function.
1052 % \begin{macrocode}
1053 \cs_new_protected:Npn \tl_analysis_show:N
1054 { \@@_analysis_show:NNnnN \msg_show:nneeee \tl_show:N {} {} }
1055 \cs_new_protected:Npn \tl_analysis_log:N
1056 {
1057 \@@_analysis_show:NNnnN \msg_log:nneeee \tl_log:N
1058 { \iow_newline: >~ . } { . }
1059 }
1060 \cs_new_protected:Npn \@@_analysis_show:NNnnN #1#2#3#4#5
1061 {
1062 \tl_if_exist:NTF #5
1063 {
1064 \exp_args:No \@@_analysis:n {#5}
1065 #1 { tl } { show-analysis }
1066 { \token_to_str:N #5 } { \@@_analysis_show: } {#3} {#4}
1067 }
1068 { #2 #3 }
1069 }
1070 % \end{macrocode}
1071 % \end{macro}
1072 %
1073 % \begin{macro}{\tl_analysis_show:n, \tl_analysis_log:n, \@@_analysis_show:Nnnn}
1074 % No existence test needed here.
1075 % \begin{macrocode}
1076 \cs_new_protected:Npn \tl_analysis_show:n
1077 { \@@_analysis_show:Nnnn \msg_show:nneeee {} {} }
1078 \cs_new_protected:Npn \tl_analysis_log:n
1079 { \@@_analysis_show:Nnnn \msg_log:nneeee { \iow_newline: >~ . } { . } }
1080 \cs_new_protected:Npn \@@_analysis_show:Nnnn #1#2#3#4
1081 {
1082 \@@_analysis:n {#4}
1083 #1 { tl } { show-analysis } { } { \@@_analysis_show: } {#2} {#3}
1084 }
1085 % \end{macrocode}
1086 % \end{macro}
1087 %
1088 % \begin{macro}[rEXP]{\@@_analysis_show:, \@@_analysis_show_loop:wNw}
1089 % Here, |#1| \texttt{o}- and \texttt{e}/\texttt{x}-expands to the token;
1090 % |#2| is the category code (one uppercase hexadecimal digit),
1091 % $0$ for control sequences;
1092 % |#3| is the character code, which we ignore.
1093 % In the cases of control sequences and active characters,
1094 % the meaning may overflow one line, and we want to truncate
1095 % it. Those cases are thus separated out.
1096 % \begin{macrocode}
1097 \cs_new:Npn \@@_analysis_show:
1098 {
1099 \exp_after:wN \@@_analysis_show_loop:wNw \g_@@_analysis_result_tl
1100 \s_@@ { ? \prg_break: } \s_@@
1101 \prg_break_point:
1102 }
1103 \cs_new:Npn \@@_analysis_show_loop:wNw #1 \s_@@ #2 #3 \s_@@
1104 {
1105 \use_none:n #2
1106 \iow_newline: > \use:nn { ~ } { ~ }
1107 \if_int_compare:w "#2 = \c_zero_int
1108 \exp_after:wN \@@_analysis_show_cs:n
1109 \else:
1110 \if_int_compare:w "#2 = 13 \exp_stop_f:
1111 \exp_after:wN \exp_after:wN
1112 \exp_after:wN \@@_analysis_show_active:n
1113 \else:
1114 \exp_after:wN \exp_after:wN
1115 \exp_after:wN \@@_analysis_show_normal:n
1116 \fi:
1117 \fi:
1118 {#1}
1119 \@@_analysis_show_loop:wNw
1120 }
1121 % \end{macrocode}
1122 % \end{macro}
1123 %
1124 % \begin{macro}[rEXP]{\@@_analysis_show_normal:n}
1125 % Non-active characters are a simple matter of printing
1126 % the character, and its meaning. Our test suite checks that
1127 % begin-group and end-group characters do not mess up
1128 % \TeX{}'s alignment status.
1129 % \begin{macrocode}
1130 \cs_new:Npn \@@_analysis_show_normal:n #1
1131 {
1132 \exp_after:wN \token_to_str:N #1 ~
1133 ( \exp_after:wN \token_to_meaning:N #1 )
1134 }
1135 % \end{macrocode}
1136 % \end{macro}
1137 %
1138 % \begin{macro}[EXP]{\@@_analysis_show_value:N}
1139 % This expands to the value of |#1| if it has any.
1140 % \begin{macrocode}
1141 \cs_new:Npn \@@_analysis_show_value:N #1
1142 {
1143 \token_if_expandable:NF #1
1144 {
1145 \token_if_chardef:NTF #1 \prg_break: { }
1146 \token_if_mathchardef:NTF #1 \prg_break: { }
1147 \token_if_dim_register:NTF #1 \prg_break: { }
1148 \token_if_int_register:NTF #1 \prg_break: { }
1149 \token_if_skip_register:NTF #1 \prg_break: { }
1150 \token_if_toks_register:NTF #1 \prg_break: { }
1151 \use_none:nnn
1152 \prg_break_point:
1153 \use:n { \exp_after:wN = \tex_the:D #1 }
1154 }
1155 }
1156 % \end{macrocode}
1157 % \end{macro}
1158 %
1159 % \begin{macro}[rEXP]{\@@_analysis_show_cs:n}
1160 % \begin{macro}[rEXP]{\@@_analysis_show_active:n}
1161 % \begin{macro}[rEXP]{\@@_analysis_show_long:nn}
1162 % \begin{macro}[rEXP]{\@@_analysis_show_long_aux:nnnn}
1163 % Control sequences and active characters are printed in the same way,
1164 % making sure not to go beyond the \cs{l_iow_line_count_int}. In case
1165 % of an overflow, we replace the last characters by
1166 % \cs{c_@@_analysis_show_etc_str}.
1167 % \begin{macrocode}
1168 \cs_new:Npn \@@_analysis_show_cs:n #1
1169 { \exp_args:No \@@_analysis_show_long:nn {#1} { control~sequence= } }
1170 \cs_new:Npn \@@_analysis_show_active:n #1
1171 { \exp_args:No \@@_analysis_show_long:nn {#1} { active~character= } }
1172 \cs_new:Npn \@@_analysis_show_long:nn #1
1173 {
1174 \@@_analysis_show_long_aux:oofn
1175 { \token_to_str:N #1 }
1176 { \token_to_meaning:N #1 }
1177 { \@@_analysis_show_value:N #1 }
1178 }
1179 \cs_new:Npn \@@_analysis_show_long_aux:nnnn #1#2#3#4
1180 {
1181 \int_compare:nNnTF
1182 { \str_count:n { #1 ~ ( #4 #2 #3 ) } }
1183 > { \l_iow_line_count_int - 3 }
1184 {
1185 \str_range:nnn { #1 ~ ( #4 #2 #3 ) } { 1 }
1186 {
1187 \l_iow_line_count_int - 3
1188 - \str_count:N \c_@@_analysis_show_etc_str
1189 }
1190 \c_@@_analysis_show_etc_str
1191 }
1192 { #1 ~ ( #4 #2 #3 ) }
1193 }
1194 \cs_generate_variant:Nn \@@_analysis_show_long_aux:nnnn { oof }
1195 % \end{macrocode}
1196 % \end{macro}
1197 % \end{macro}
1198 % \end{macro}
1199 % \end{macro}
1200 %
1201 % \subsection{Peeking ahead}
1202 %
1203 % \begin{macro}[EXP]{\peek_analysis_map_break:, \peek_analysis_map_break:n}
1204 % The break statements use the general \cs{prg_map_break:Nn}.
1205 % \begin{macrocode}
1206 \cs_new:Npn \peek_analysis_map_break:
1207 { \prg_map_break:Nn \peek_analysis_map_break: { } }
1208 \cs_new:Npn \peek_analysis_map_break:n
1209 { \prg_map_break:Nn \peek_analysis_map_break: }
1210 % \end{macrocode}
1211 % \end{macro}
1212 %
1213 % \begin{variable}{\l_@@_peek_charcode_int}
1214 % \begin{macrocode}
1215 \int_new:N \l_@@_peek_charcode_int
1216 % \end{macrocode}
1217 % \end{variable}
1218 %
1219 % \begin{macro}{\@@_analysis_char_arg:Nw, \@@_analysis_char_arg_aux:Nw}
1220 % After a call to \tn{futurelet} \cs{l_@@_analysis_token} followed by
1221 % a stringified character token (either explicit space or catcode
1222 % other character), grab the argument and pass it to |#1|. We only
1223 % need to do anything in the case of a space.
1224 % \begin{macrocode}
1225 \cs_new:Npn \@@_analysis_char_arg:Nw
1226 {
1227 \if_meaning:w \l_@@_analysis_token \c_space_token
1228 \exp_after:wN \@@_analysis_char_arg_aux:Nw
1229 \fi:
1230 }
1231 \cs_new:Npn \@@_analysis_char_arg_aux:Nw #1 ~ { #1 { ~ } }
1232 % \end{macrocode}
1233 % \end{macro}
1234 %
1235 % \begin{macro}
1236 % {
1237 % \peek_analysis_map_inline:n,
1238 % \@@_peek_analysis_loop:NNn, \@@_peek_analysis_test:,
1239 % \@@_peek_analysis_exp:N, \@@_peek_analysis_exp_aux:N,
1240 % \@@_peek_analysis_nonexp:N, \@@_peek_analysis_cs:N,
1241 % \@@_peek_analysis_char:N, \@@_peek_analysis_char:w,
1242 % \@@_peek_analysis_special:, \@@_peek_analysis_retest:,
1243 % \@@_peek_analysis_str:,
1244 % \@@_peek_analysis_str:w, \@@_peek_analysis_str:n,
1245 % \@@_peek_analysis_active_str:n, \@@_peek_analysis_explicit:n,
1246 % \@@_peek_analysis_escape:, \@@_peek_analysis_collect:w,
1247 % \@@_peek_analysis_collect:n, \@@_peek_analysis_collect_loop:,
1248 % \@@_peek_analysis_collect_test:, \@@_peek_analysis_collect_end:NNNN
1249 % }
1250 % Save the user's code in a control sequence that is suitable for
1251 % nested maps. We may wish to pass to this function an \tn{outer}
1252 % control sequence or active character; for this we will undefine
1253 % any expandable token (testing if it is \tn{outer} is much slower)
1254 % within a group, closed immediately after the function reads its
1255 % arguments to avoid affecting the user's code or even our peek code
1256 % (there is no risk of undefining \cs{group_end:} itself since that is
1257 % not expandable). This user's code function also
1258 % calls the loop auxiliary, and includes the trailing
1259 % \cs{prg_break_point:Nn} for when the user wants to stop the loop.
1260 % The loop auxiliary must remove that break point because it must look
1261 % at the input stream.
1262 % \begin{macrocode}
1263 \cs_new_protected:Npn \peek_analysis_map_inline:n #1
1264 {
1265 \group_align_safe_begin:
1266 \int_gincr:N \g__kernel_prg_map_int
1267 \cs_set_protected:cpn
1268 { @@_analysis_map_ \int_use:N \g__kernel_prg_map_int :nnN }
1269 ##1##2##3
1270 {
1271 \group_end:
1272 #1
1273 \@@_peek_analysis_loop:NNn
1274 \prg_break_point:Nn \peek_analysis_map_break:
1275 {
1276 \int_gdecr:N \g__kernel_prg_map_int
1277 \group_align_safe_end:
1278 }
1279 }
1280 \@@_peek_analysis_loop:NNn ? ? ?
1281 }
1282 % \end{macrocode}
1283 % The loop starts a group (closed by the user-code function defined
1284 % above) with a normalized escape character, and checks if the next
1285 % token is special or \texttt{N}-type (distinguishing expandable from
1286 % non-expandable tokens). The test for nonexpandable tokens in
1287 % \cs{@@_peek_analysis_test:} must be done after the tests for
1288 % begin-group, end-group, and space tokens, in case \cs{l_peek_token}
1289 % is either \tn{outer} or is a primitive \TeX{} conditional, as such
1290 % tokens cannot be skipped over correctly by conditional code.
1291 % \begin{macrocode}
1292 \cs_new_protected:Npn \@@_peek_analysis_loop:NNn #1#2#3
1293 {
1294 \group_begin:
1295 \tl_set:Ne \l_@@_peek_code_tl
1296 {
1297 \exp_not:c
1298 { @@_analysis_map_ \int_use:N \g__kernel_prg_map_int :nnN }
1299 }
1300 \int_set:Nn \tex_escapechar:D { `\\ }
1301 \peek_after:Nw \@@_peek_analysis_test:
1302 }
1303 \cs_new_protected:Npn \@@_peek_analysis_test:
1304 {
1305 \if_case:w
1306 \if_catcode:w \exp_not:N \l_peek_token { \c_max_int \fi:
1307 \if_catcode:w \exp_not:N \l_peek_token } \c_max_int \fi:
1308 \if_meaning:w \l_peek_token \c_space_token \c_max_int \fi:
1309 \exp_after:wN \if_meaning:w \exp_not:N \l_peek_token \l_peek_token
1310 \c_one_int
1311 \fi:
1312 \c_zero_int
1313 \exp_after:wN \exp_after:wN
1314 \exp_after:wN \@@_peek_analysis_exp:N
1315 \exp_after:wN \exp_not:N
1316 \or:
1317 \exp_after:wN \@@_peek_analysis_nonexp:N
1318 \else:
1319 \exp_after:wN \@@_peek_analysis_special:
1320 \fi:
1321 }
1322 % \end{macrocode}
1323 % Expandable tokens (which are automatically |N|-type) can be
1324 % \tn{outer} macros, hence the need for \cs{exp_after:wN} and
1325 % \cs{exp_not:N} in the code above, which allows the next function to
1326 % safely grab the token as an argument. To allow the
1327 % possibly-\tn{outer} token~|#1| as an argument of the \meta{user's
1328 % function} (which is protected and stored in \cs{l_@@_peek_code_tl}),
1329 % we set it equal to a harmless macro. This must be done at the very
1330 % last minute because |#1| may be some pretty important function such
1331 % as \cs{exp_after:wN}. Using a primitive \cs{cs_set_nopar:Npe}
1332 % expansion (to avoid \tn{outer} problems) we set up to run the code
1333 % \tn{let} |#1| \meta{user's function} \meta{user's function} followed
1334 % by arguments involving~|#1|. Regardless of~|#1| (including the
1335 % user's function itself), the user's function is run. It always
1336 % starts with \cs{group_end:}, which has not been redefined since |#1|
1337 % started out as expandable, and which restores the definition of~|#1|.
1338 %
1339 % Then we put the elaborate first argument
1340 % \cs{__kernel_exp_not:w} \cs{exp_after:wN} |{| \cs{exp_not:N} |#1| |}|:
1341 % indeed we cannot use \cs{exp_not:n} |{#1}| as this breaks for an
1342 % \tn{outer} macro and we cannot use \cs{exp_not:N} |#1|, as
1343 % \texttt{o}-expanding this yields a \enquote{notexpanded} token equal
1344 % to (a weird) \tn{relax}, which would have the wrong value for
1345 % primitive \TeX{} conditionals such as \cs{if_meaning:w}.
1346 %
1347 % Then we must add |{-1}0| if the token is a
1348 % control sequence and \Arg{charcode}|D| otherwise. Distinguishing
1349 % the two cases is easy: since we have made the escape character
1350 % printable, \cs{token_to_str:N} gives at least two characters for a
1351 % control sequence versus a single one for an active character
1352 % (possibly being a space, in which case the trailing brace group is
1353 % taken as the first argument of \cs{@@_peek_analysis_exp_aux:Nw}).
1354 % Importantly, |#1| could be an \tn{outer} token (as it is only set to
1355 % \cs{scan_stop:} at the last minute) but once we apply
1356 % \cs{token_to_str:N} we no longer need to worry about it.
1357 % \begin{macrocode}
1358 \cs_new_protected:Npn \@@_peek_analysis_exp:N #1
1359 {
1360 \cs_set_nopar:Npe \l_@@_peek_code_tl
1361 {
1362 \tex_let:D \exp_not:N #1 \l_@@_peek_code_tl
1363 \l_@@_peek_code_tl
1364 {
1365 \exp_not:n { \__kernel_exp_not:w \exp_after:wN }
1366 { \exp_not:N \exp_not:N \exp_not:N #1 }
1367 }
1368 \exp_after:wN \@@_peek_analysis_exp_aux:Nw
1369 \token_to_str:N #1 { } \s_@@
1370 }
1371 \l_@@_peek_code_tl
1372 }
1373 \cs_new:Npe \@@_peek_analysis_exp_aux:Nw #1#2 \s_@@
1374 {
1375 \exp_not:N \if_meaning:w \scan_stop: #2 \scan_stop:
1376 { \exp_not:N \int_value:w `#1 ~ } \token_to_str:N D
1377 \exp_not:N \else:
1378 { -1 } 0
1379 \exp_not:N \fi:
1380 }
1381 % \end{macrocode}
1382 % For normal non-expandable tokens we must distinguish characters
1383 % (including active ones and macro parameter characters) from control
1384 % sequences (whose string representation is more than one character
1385 % because we made the escape character printable). For a control
1386 % sequence call the user code with suitable arguments, wrapping |#1|
1387 % within \cs{exp_not:n} just in case it happens to be equal to a macro
1388 % parameter character. We do not skip \cs{exp_not:n} when
1389 % unnecessary, because this auxiliary is also called in
1390 % \cs{@@_peek_analysis_retest:} where we have changed some control
1391 % sequences or active characters to \cs{scan_stop:} temporarily.
1392 % \begin{macrocode}
1393 \cs_new_protected:Npn \@@_peek_analysis_nonexp:N #1
1394 {
1395 \if_charcode:w
1396 \scan_stop:
1397 \exp_after:wN \use_none:n \token_to_str:N #1 \prg_do_nothing:
1398 \scan_stop:
1399 \exp_after:wN \@@_peek_analysis_char:N
1400 \else:
1401 \exp_after:wN \@@_peek_analysis_cs:N
1402 \fi:
1403 #1
1404 }
1405 \cs_new_protected:Npn \@@_peek_analysis_cs:N #1
1406 { \l_@@_peek_code_tl { \exp_not:n {#1} } { -1 } 0 }
1407 % \end{macrocode}
1408 % For normal characters we must determine their catcode. The main
1409 % difficulty is that the character may be an active character
1410 % masquerading as (i.e., set equal to) itself with a different
1411 % catcode. Two approaches based on \tn{lowercase} can detect this.
1412 % One could make an active character with the same catcode as~|#1| and
1413 % change its definition before testing the catcode of~|#1|, but in
1414 % some Unicode engine this fills up the hash table uselessly.
1415 % Instead, we lowercase~|#1| itself, changing its character code
1416 % to~$32$, namely space (because \LuaTeX{} cannot turn catcode~$10$
1417 % characters to anything else than character code~$32$), then we apply
1418 % \cs{@@_analysis_b_char:Nn}, which detects active characters by
1419 % comparing them to \cs{tex_undefined:D}, and we must have undefined
1420 % the active space (locally) for this test to work.
1421 % To define \cs{@@_peek_analysis_char:N} itself we use an
1422 % |e|-expanding assignment to get the active space in the right place
1423 % after making it (just for this definition) unexpandable.
1424 % Finally \cs{@@_peek_analysis_char:w} receives the \meta{charcode},
1425 % \meta{user function}, \meta{catcode}, and \meta{token}, and places
1426 % the arguments in the correct order. It keeps \cs{exp_not:n} for
1427 % macro parameter characters and active characters (the latter could
1428 % be macro parameter characters, and it seems more uniform to always
1429 % put \cs{exp_not:n}), and otherwise eliminates it by expanding once
1430 % with \cs{exp_args:NNNo}.
1431 % \begin{macrocode}
1432 \group_begin:
1433 \char_set_active_eq:NN \ \scan_stop:
1434 \cs_new_protected:Npe \@@_peek_analysis_char:N #1
1435 {
1436 \cs_set_eq:NN
1437 \char_generate:nn { 32 } { 13 }
1438 \exp_not:N \tex_undefined:D
1439 \tex_lccode:D `#1 = 32 \exp_stop_f:
1440 \tex_lowercase:D
1441 {
1442 \tl_put_right:Ne \exp_not:N \l_@@_peek_code_tl
1443 { \exp_not:n { \@@_analysis_b_char:Nn \use_none:n } {#1} }
1444 }
1445 \exp_not:n
1446 {
1447 \exp_after:wN \@@_peek_analysis_char:w
1448 \int_value:w
1449 }
1450 `#1
1451 \exp_not:n { \exp_after:wN \s_@@ \l_@@_peek_code_tl }
1452 #1
1453 }
1454 \group_end:
1455 \cs_new_protected:Npn \@@_peek_analysis_char:w #1 \s_@@ #2#3#4
1456 {
1457 \if_charcode:w 6 #3
1458 \else:
1459 \if_charcode:w D #3
1460 \else:
1461 \exp_args:NNNo
1462 \fi:
1463 \fi:
1464 #2 { \exp_not:n {#4} } {#1} #3
1465 }
1466 % \end{macrocode}
1467 % For special characters the idea is to eventually act with
1468 % \cs{token_to_str:N}, then pick up one by one the characters of this
1469 % string representation until hitting the token that follows. First
1470 % determine the character code of (the meaning of) the \meta{token}
1471 % (which we know is a special token), make sure the escape character
1472 % is different from it, normalize the meanings of two active
1473 % characters and the empty control sequence, and filter out these
1474 % cases in \cs{@@_peek_analysis_retest:}.
1475 % \begin{macrocode}
1476 \cs_new_protected:Npn \@@_peek_analysis_special:
1477 {
1478 \tex_let:D \l_@@_analysis_token = ~ \l_peek_token
1479 \int_set:Nn \l_@@_peek_charcode_int
1480 { \@@_analysis_extract_charcode: }
1481 \if_int_compare:w \l_@@_peek_charcode_int = \tex_escapechar:D
1482 \int_set:Nn \tex_escapechar:D { `\/ }
1483 \fi:
1484 \char_set_active_eq:nN { \l_@@_peek_charcode_int } \scan_stop:
1485 \char_set_active_eq:nN { \tex_escapechar:D } \scan_stop:
1486 \cs_set_eq:cN { } \scan_stop:
1487 \tex_futurelet:D \l_@@_analysis_token
1488 \@@_peek_analysis_retest:
1489 }
1490 \cs_new_protected:Npn \@@_peek_analysis_retest:
1491 {
1492 \if_meaning:w \l_@@_analysis_token \scan_stop:
1493 \exp_after:wN \@@_peek_analysis_nonexp:N
1494 \else:
1495 \exp_after:wN \@@_peek_analysis_str:
1496 \fi:
1497 }
1498 % \end{macrocode}
1499 % At this point we know the meaning of the \meta{token} in the input
1500 % stream is \cs{l_peek_token}, either a space (32, 10) or a
1501 % begin-group or end-group token (catcode $1$ or~$2$), and we excluded
1502 % a few cases that would be difficult later (empty control sequence,
1503 % active character with the same character code as its meaning or as
1504 % the escape character). The idea is to apply \cs{token_to_str:N} to
1505 % the \meta{token} then grab characters (of category code~$12$ except
1506 % for spaces that have category code~$10$) to reconstruct it. In
1507 % earlier versions of the code we would peek at the \meta{next token}
1508 % that lies after \meta{token} in the input stream, which would help
1509 % us be more accurate in reconstructing the \meta{token} case in edge
1510 % cases (mentioned below), but this had the side-effect of tokenizing
1511 % the input stream (turning characters into tokens) farther ahead than
1512 % needed.
1513 %
1514 % We hit the \meta{token} with \cs{token_to_str:N} and start grabbing
1515 % characters. More
1516 % precisely, by looking at the first character in the string
1517 % representation of the \meta{token} we distinguish three cases:
1518 % a stringified control sequence starts with the escape character; for
1519 % an explicit character we find that same character; for an active
1520 % character we find anything else (we made sure to exclude the case of
1521 % an active character whose string representation coincides with the
1522 % other two cases).
1523 % \begin{macrocode}
1524 \cs_new_protected:Npn \@@_peek_analysis_str:
1525 {
1526 \exp_after:wN \tex_futurelet:D
1527 \exp_after:wN \l_@@_analysis_token
1528 \exp_after:wN \@@_peek_analysis_str:w
1529 \token_to_str:N
1530 }
1531 \cs_new_protected:Npn \@@_peek_analysis_str:w
1532 { \@@_analysis_char_arg:Nw \@@_peek_analysis_str:n }
1533 \cs_new_protected:Npn \@@_peek_analysis_str:n #1
1534 {
1535 \int_case:nnF { `#1 }
1536 {
1537 { \l_@@_peek_charcode_int }
1538 { \@@_peek_analysis_explicit:n {#1} }
1539 { \tex_escapechar:D } { \@@_peek_analysis_escape: }
1540 }
1541 { \@@_peek_analysis_active_str:n {#1} }
1542 }
1543 % \end{macrocode}
1544 % When |#1| is a stringified active character we pass appropriate
1545 % arguments to the user's code; thankfully \cs{char_generate:nn}
1546 % can make active characters.
1547 % \begin{macrocode}
1548 \cs_new_protected:Npn \@@_peek_analysis_active_str:n #1
1549 {
1550 \tl_put_right:Ne \l_@@_peek_code_tl
1551 {
1552 { \char_generate:nn { `#1 } { 13 } }
1553 { \int_value:w `#1 }
1554 \token_to_str:N D
1555 }
1556 \l_@@_peek_code_tl
1557 }
1558 % \end{macrocode}
1559 % When |#1| matches the character we had extracted from the meaning of
1560 % \cs{l_peek_token}, the token was an explicit character, which can be
1561 % a standard space, or a begin-group or end-group character with some
1562 % character code. In the latter two cases we call
1563 % \cs{char_generate:nn} with suitable arguments and put suitable
1564 % \cs{if_false:} \cs{fi:} constructions to make the result balanced
1565 % and such that \texttt{o}-expanding or \texttt{e}/\texttt{x}-expanding gives
1566 % back a single (unbalanced) begin-group or end-group character.
1567 % \begin{macrocode}
1568 \cs_new_protected:Npn \@@_peek_analysis_explicit:n #1
1569 {
1570 \tl_put_right:Ne \l_@@_peek_code_tl
1571 {
1572 \if_meaning:w \l_peek_token \c_space_token
1573 { ~ } { 32 } \token_to_str:N A
1574 \else:
1575 \if_catcode:w \l_peek_token \c_group_begin_token
1576 {
1577 \exp_not:N \exp_after:wN
1578 \char_generate:nn { `#1 } { 1 }
1579 \exp_not:N \if_false:
1580 \if_false: { \fi: }
1581 \exp_not:N \fi:
1582 }
1583 { \int_value:w `#1 }
1584 1
1585 \else:
1586 {
1587 \exp_not:N \if_false:
1588 { \if_false: } \fi:
1589 \exp_not:N \fi:
1590 \char_generate:nn { `#1 } { 2 }
1591 }
1592 { \int_value:w `#1 }
1593 2
1594 \fi:
1595 \fi:
1596 }
1597 \l_@@_peek_code_tl
1598 }
1599 % \end{macrocode}
1600 % Finally there is the case of a special token whose string
1601 % representation starts with an escape character, namely the token was
1602 % a control sequence. In that case we could have grabbed the token
1603 % directly as an \texttt{N}-type argument, but of course we couldn't
1604 % know that until we had run all the various tests including
1605 % stringifying the token. We are thus left with the hard work of
1606 % picking up one by one the characters in the csname (being careful
1607 % about spaces), until the constructed csname has the expected
1608 % meaning. This fails if someone defines a token like
1609 % \cs[no-index]{bgroup@my} whose string representation starts the same
1610 % as another token with the same meaning being an implicit character
1611 % token of category code $1$, $2$, or $10$.
1612 % \begin{macrocode}
1613 \cs_new_protected:Npn \@@_peek_analysis_escape:
1614 {
1615 \tl_clear:N \l_@@_tmpa_tl
1616 \tex_futurelet:D \l_@@_analysis_token
1617 \@@_peek_analysis_collect:w
1618 }
1619 \cs_new_protected:Npn \@@_peek_analysis_collect:w
1620 { \@@_analysis_char_arg:Nw \@@_peek_analysis_collect:n }
1621 \cs_new_protected:Npn \@@_peek_analysis_collect:n #1
1622 {
1623 \tl_put_right:Nn \l_@@_tmpa_tl {#1}
1624 \@@_peek_analysis_collect_loop:
1625 }
1626 \cs_new_protected:Npn \@@_peek_analysis_collect_loop:
1627 {
1628 \exp_after:wN \if_meaning:w
1629 \cs:w
1630 \if_cs_exist:w \l_@@_tmpa_tl \cs_end:
1631 \l_@@_tmpa_tl
1632 \else:
1633 c_one % anything short
1634 \fi:
1635 \cs_end:
1636 \l_peek_token
1637 \@@_peek_analysis_collect_end:NNNN
1638 \fi:
1639 \tex_futurelet:D \l_@@_analysis_token
1640 \@@_peek_analysis_collect:w
1641 }
1642 % \end{macrocode}
1643 % As in all other cases, end by calling the user code with suitable
1644 % arguments (here |#1| is \cs{fi:}).
1645 % \begin{macrocode}
1646 \cs_new_protected:Npn \@@_peek_analysis_collect_end:NNNN #1#2#3#4
1647 {
1648 #1
1649 \tl_put_right:Ne \l_@@_peek_code_tl
1650 {
1651 { \exp_not:N \exp_not:n { \exp_not:c { \l_@@_tmpa_tl } } }
1652 { -1 }
1653 0
1654 }
1655 \l_@@_peek_code_tl
1656 }
1657 % \end{macrocode}
1658 % \end{macro}
1659 %
1660 % \subsection{Messages}
1661 %
1662 % \begin{variable}{\c_@@_analysis_show_etc_str}
1663 % When a control sequence (or active character)
1664 % and its meaning are too long to fit in one line
1665 % of the terminal, the end is replaced by this token list.
1666 % \begin{macrocode}
1667 \tl_const:Ne \c_@@_analysis_show_etc_str % (
1668 { \token_to_str:N \ETC.) }
1669 % \end{macrocode}
1670 % \end{variable}
1671 %
1672 % \begin{macrocode}
1673 \msg_new:nnn { tl } { show-analysis }
1674 {
1675 The~token~list~ \tl_if_empty:nF {#1} { #1 ~ }
1676 \tl_if_empty:nTF {#2}
1677 { is~empty #3 }
1678 { contains~the~tokens: #2 #4 }
1679 }
1680 % \end{macrocode}
1681 %
1682 % \begin{macrocode}
1683 %</code>
1684 % \end{macrocode}
1685 %
1686 % \end{implementation}
1687 %
1688 % \PrintIndex