Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3quark.dtx
4 %
5 % Copyright (C) 1990-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{l3quark} module\\ Quarks and scan marks^^A
35 % }\ifdefined\thechapter\label{sec:l3quarks}\fi
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 % Two special types of constants in \LaTeX3 are \enquote{quarks} and
53 % \enquote{scan marks}. By convention all constants of type quark
54 % start out with |\q_|, and scan marks start with |\s_|.
55 %
56 % \section{Quarks}
57 %
58 % Quarks are control sequences (and in fact, token lists) that expand
59 % to themselves and should
60 % therefore \emph{never} be executed directly in the code.
61 % This would result in an endless loop!
62 %
63 % They are meant to be used as delimiter in weird functions, the most
64 % common use case being the `stop token' (i.e., \cs{q_stop}).
65 % For example, when writing a macro to parse a user-defined date
66 % \begin{verbatim}
67 % \date_parse:n {19/June/1981}
68 % \end{verbatim}
69 % one might write a command such as
70 % \begin{verbatim}
71 % \cs_new:Npn \date_parse:n #1 { \date_parse_aux:w #1 \q_stop }
72 % \cs_new:Npn \date_parse_aux:w #1 / #2 / #3 \q_stop
73 % { <do something with the date> }
74 % \end{verbatim}
75 %
76 % Quarks are sometimes also used as error return values for functions
77 % that receive erroneous input.
78 % For example, in the function \cs{prop_get:NnN} to retrieve a value stored
79 % in some key of a property list, if the key does not exist then the return value
80 % is the quark \cs{q_no_value}.
81 % As mentioned above, such quarks are extremely fragile and it is imperative
82 % when using such functions that code is carefully written to check for
83 % pathological cases to avoid leakage of a quark into an uncontrolled
84 % environment.
85 %
86 % Quarks also permit the
87 % following ingenious trick when parsing tokens:
88 % when you pick up a token in a temporary variable
89 % and you want to know whether you have picked up a particular quark,
90 % all you have to do is compare the temporary variable to the quark using
91 % \cs{tl_if_eq:NNTF}. A set of special quark testing functions is set up
92 % below. All the quark testing functions are expandable although the
93 % ones testing only single tokens are much faster.
94 %
95 % \section{Defining quarks}
96 %
97 % \begin{function}{\quark_new:N}
98 % \begin{syntax}
99 % \cs{quark_new:N} \meta{quark}
100 % \end{syntax}
101 % Creates a new \meta{quark} which expands only to \meta{quark}.
102 % The \meta{quark} is defined globally, and an error message
103 % is raised if the name was already taken.
104 % \end{function}
105 %
106 % \begin{variable}{\q_stop}
107 % Used as a marker for delimited arguments, such as
108 % \begin{verbatim}
109 % \cs_set:Npn \tmp:w #1#2 \q_stop {#1}
110 % \end{verbatim}
111 % \end{variable}
112 %
113 % \begin{variable}{\q_mark}
114 % Used as a marker for delimited arguments when \cs{q_stop} is
115 % already in use.
116 % \end{variable}
117 %
118 % \begin{variable}{\q_nil}
119 % Quark to mark a null value in structured variables or functions. Used
120 % as an end delimiter when this may itself need to be tested
121 % (in contrast to \cs{q_stop}, which is only ever used as a delimiter).
122 % \end{variable}
123 %
124 % \begin{variable}{\q_no_value}
125 % A canonical value for a missing value, when one is requested from
126 % a data structure. This is therefore used as a \enquote{return} value
127 % by functions such as \cs{prop_get:NnN} if there is no data to
128 % return.
129 % \end{variable}
130 %
131 % \section{Quark tests}
132 %
133 % The method used to define quarks means that the single token (\texttt{N})
134 % tests are faster than the multi-token (\texttt{n}) tests. The latter
135 % should therefore only be used when the argument can definitely take
136 % more than a single token.
137 %
138 % \begin{function}[EXP,pTF]{\quark_if_nil:N}
139 % \begin{syntax}
140 % \cs{quark_if_nil_p:N} \meta{token}
141 % \cs{quark_if_nil:NTF} \meta{token} \Arg{true code} \Arg{false code}
142 % \end{syntax}
143 % Tests if the \meta{token} is equal to \cs{q_nil}.
144 % \end{function}
145 %
146 % \begin{function}[EXP,pTF]{\quark_if_nil:n, \quark_if_nil:o, \quark_if_nil:V}
147 % \begin{syntax}
148 % \cs{quark_if_nil_p:n} \Arg{token list} \\
149 % \cs{quark_if_nil:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
150 % \end{syntax}
151 % Tests if the \meta{token list} contains only \cs{q_nil} (distinct
152 % from \meta{token list} being empty or containing \cs{q_nil} plus one
153 % or more other tokens).
154 % \end{function}
155 %
156 % \begin{function}[EXP,pTF]{\quark_if_no_value:N, \quark_if_no_value:c}
157 % \begin{syntax}
158 % \cs{quark_if_no_value_p:N} \meta{token} \\
159 % \cs{quark_if_no_value:NTF} \meta{token} \Arg{true code} \Arg{false code}
160 % \end{syntax}
161 % Tests if the \meta{token} is equal to \cs{q_no_value}.
162 % \end{function}
163 %
164 % \begin{function}[EXP,pTF]{\quark_if_no_value:n}
165 % \begin{syntax}
166 % \cs{quark_if_no_value_p:n} \Arg{token list} \\
167 % \cs{quark_if_no_value:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
168 % \end{syntax}
169 % Tests if the \meta{token list} contains only \cs{q_no_value}
170 % (distinct from \meta{token list} being empty or containing
171 % \cs{q_no_value} plus one or more other tokens).
172 % \end{function}
173 %
174 % \section{Recursion}
175 %
176 % This module provides a uniform interface to intercepting and
177 % terminating loops as when one is doing tail recursion. The building
178 % blocks follow below and an example is shown in
179 % Section~\ref{sec:l3quark:quark-example}.
180 %
181 % \begin{variable}{\q_recursion_tail}
182 % This quark is appended to the data structure in question and
183 % appears as a real element there. This means it gets any list
184 % separators around it.
185 % \end{variable}
186 %
187 % \begin{variable}{\q_recursion_stop}
188 % This quark is added \emph{after} the data structure. Its purpose
189 % is to make it possible to terminate the recursion at any point
190 % easily.
191 % \end{variable}
192 %
193 % \begin{function}[EXP]{\quark_if_recursion_tail_stop:N}
194 % \begin{syntax}
195 % \cs{quark_if_recursion_tail_stop:N} \meta{token}
196 % \end{syntax}
197 % Tests if \meta{token} contains only the marker
198 % \cs{q_recursion_tail}, and if so uses
199 % \cs{use_none_delimit_by_q_recursion_stop:w} to terminate the
200 % recursion that this belongs to. The
201 % recursion input must include the marker tokens \cs{q_recursion_tail}
202 % and \cs{q_recursion_stop} as the last two items.
203 % \end{function}
204 %
205 % \begin{function}[EXP,updated = 2011-09-06]
206 % {\quark_if_recursion_tail_stop:n, \quark_if_recursion_tail_stop:o}
207 % \begin{syntax}
208 % \cs{quark_if_recursion_tail_stop:n} \Arg{token list}
209 % \end{syntax}
210 % Tests if the \meta{token list} contains only
211 % \cs{q_recursion_tail}, and if so uses
212 % \cs{use_none_delimit_by_q_recursion_stop:w} to terminate the
213 % recursion that this belongs to. The
214 % recursion input must include the marker tokens \cs{q_recursion_tail}
215 % and \cs{q_recursion_stop} as the last two items.
216 % \end{function}
217 %
218 % \begin{function}[EXP]{\quark_if_recursion_tail_stop_do:Nn}
219 % \begin{syntax}
220 % \cs{quark_if_recursion_tail_stop_do:Nn} \meta{token} \Arg{insertion}
221 % \end{syntax}
222 % Tests if \meta{token} contains only the marker
223 % \cs{q_recursion_tail}, and if so uses
224 % \cs{use_i_delimit_by_q_recursion_stop:w} to terminate the
225 % recursion that this belongs to. The
226 % recursion input must include the marker tokens \cs{q_recursion_tail}
227 % and \cs{q_recursion_stop} as the last two items. The \meta{insertion}
228 % code is then added to the input stream after the recursion has
229 % ended.
230 % \end{function}
231 %
232 % \begin{function}[EXP,updated = 2011-09-06]
233 % {\quark_if_recursion_tail_stop_do:nn, \quark_if_recursion_tail_stop_do:on}
234 % \begin{syntax}
235 % \cs{quark_if_recursion_tail_stop_do:nn} \Arg{token list} \Arg{insertion}
236 % \end{syntax}
237 % Tests if the \meta{token list} contains only
238 % \cs{q_recursion_tail}, and if so uses
239 % \cs{use_i_delimit_by_q_recursion_stop:w} to terminate the
240 % recursion that this belongs to. The
241 % recursion input must include the marker tokens \cs{q_recursion_tail}
242 % and \cs{q_recursion_stop} as the last two items. The \meta{insertion}
243 % code is then added to the input stream after the recursion has
244 % ended.
245 % \end{function}
246 %
247 %
248 % \begin{function}[EXP,added = 2018-04-10]
249 % {
250 % \quark_if_recursion_tail_break:NN,
251 % \quark_if_recursion_tail_break:nN
252 % }
253 % \begin{syntax}
254 % \cs{quark_if_recursion_tail_break:nN} \Arg{token list} \cs[no-index]{\meta{type}_map_break:}
255 % \end{syntax}
256 % Tests if \meta{token list} contains only \cs{q_recursion_tail}, and
257 % if so terminates the recursion using \cs[no-index]{\meta{type}_map_break:}.
258 % The recursion end should be marked by \cs{prg_break_point:Nn}
259 % \cs[no-index]{\meta{type}_map_break:}.
260 % \end{function}
261 %
262 % \subsection{An example of recursion with quarks}
263 % \label{sec:l3quark:quark-example}
264 %
265 % Quarks are mainly used internally in the \pkg{expl3} code to define
266 % recursion functions such as \cs{tl_map_inline:nn} and so on.
267 % Here is a small example to demonstrate how to use quarks in this fashion.
268 % We shall define a command called |\my_map_dbl:nn| which takes a token list
269 % and applies an operation to every \emph{pair} of tokens.
270 % For example, |\my_map_dbl:nn {abcd} {[--#1--#2--]~}| would produce
271 % \enquote{\ttfamily [--a--b--]~[--c--d--]~}.
272 % Using quarks to define such functions simplifies their logic and ensures
273 % robustness in many cases.
274 %
275 %
276 % Here's the definition of |\my_map_dbl:nn|.
277 % First of all, define the function that does the processing based on the
278 % inline function argument |#2|.
279 % Then initiate the recursion using an internal function.
280 % The token list |#1| is terminated using \cs{q_recursion_tail}, with
281 % delimiters according to the type of recursion (here a pair of
282 % \cs{q_recursion_tail}), concluding with \cs{q_recursion_stop}.
283 % These quarks are used to mark the end of the token list being operated upon.
284 %\begin{verbatim}
285 % \cs_new:Npn \my_map_dbl:nn #1#2
286 % {
287 % \cs_set:Npn \__my_map_dbl_fn:nn ##1 ##2 {#2}
288 % \__my_map_dbl:nn #1 \q_recursion_tail \q_recursion_tail
289 % \q_recursion_stop
290 % }
291 %\end{verbatim}
292 %
293 % The definition of the internal recursion function follows.
294 % First check if either of the input tokens are the termination quarks.
295 % Then, if not, apply the inline function to the two arguments.
296 %\begin{verbatim}
297 % \cs_new:Nn \__my_map_dbl:nn
298 % {
299 % \quark_if_recursion_tail_stop:n {#1}
300 % \quark_if_recursion_tail_stop:n {#2}
301 % \__my_map_dbl_fn:nn {#1} {#2}
302 %\end{verbatim}
303 % Finally, recurse:
304 %\begin{verbatim}
305 % \__my_map_dbl:nn
306 % }
307 %\end{verbatim}
308 % Note that contrarily to \LaTeX3 built-in mapping functions, this
309 % mapping function cannot be nested, since the second map would overwrite
310 % the definition of |\__my_map_dbl_fn:nn|.
311 %
312 % \section{Scan marks}
313 %
314 % Scan marks are control sequences set equal to \cs{scan_stop:},
315 % hence never expand in an expansion context and are (largely)
316 % invisible if they are encountered in a typesetting context.
317 %
318 % Like quarks, they can be used as delimiters in weird functions
319 % and are often safer to use for this purpose.
320 % Since they are harmless when executed by \TeX{} in non-expandable
321 % contexts, they can be used to mark the end of a set of instructions.
322 % This allows to skip to that point if the end of the instructions
323 % should not be performed (see \pkg{l3regex}).
324 %
325 % \begin{function}[added = 2018-04-01]{\scan_new:N}
326 % \begin{syntax}
327 % \cs{scan_new:N} \meta{scan mark}
328 % \end{syntax}
329 % Creates a new \meta{scan mark} which is set equal to \cs{scan_stop:}.
330 % The \meta{scan mark} is defined globally, and an error message
331 % is raised if the name was already taken by another scan mark.
332 % \end{function}
333 %
334 % \begin{variable}[added = 2018-04-01]{\s_stop}
335 % Used at the end of a set of instructions, as a marker
336 % that can be jumped to using \cs{use_none_delimit_by_s_stop:w}.
337 % \end{variable}
338 %
339 % \begin{function}[EXP,added = 2018-04-01]{\use_none_delimit_by_s_stop:w}
340 % \begin{syntax}
341 % \cs{use_none_delimit_by_s_stop:w} \meta{tokens} \cs{s_stop}
342 % \end{syntax}
343 % Removes the \meta{tokens} and \cs{s_stop} from the input stream.
344 % This leads to a low-level \TeX{} error if \cs{s_stop} is absent.
345 % \end{function}
346 %
347 % \end{documentation}
348 %
349 % \begin{implementation}
350 %
351 % \section{\pkg{l3quark} implementation}
352 %
353 % \TestFiles{m3quark001.lvt}
354 %
355 % \begin{macrocode}
356 %<*code>
357 % \end{macrocode}
358 %
359 % \subsection{Quarks}
360 %
361 % \begin{macrocode}
362 %<@@=quark>
363 % \end{macrocode}
364 %
365 % \begin{macro}{\quark_new:N}
366 % \UnitTested
367 % Allocate a new quark.
368 % \begin{macrocode}
369 \cs_new_protected:Npn \quark_new:N #1
370 {
371 \__kernel_chk_if_free_cs:N #1
372 \cs_gset_nopar:Npn #1 {#1}
373 }
374 % \end{macrocode}
375 % \end{macro}
376 %
377 % \begin{variable}{\q_nil, \q_mark, \q_no_value, \q_stop}
378 % Some \enquote{public} quarks. \cs{q_stop} is an \enquote{end of
379 % argument} marker, \cs{q_nil} is a empty value and \cs{q_no_value}
380 % marks an empty argument.
381 % \begin{macrocode}
382 \quark_new:N \q_nil
383 \quark_new:N \q_mark
384 \quark_new:N \q_no_value
385 \quark_new:N \q_stop
386 % \end{macrocode}
387 % \end{variable}
388 %
389 % \begin{variable}{\q_recursion_tail, \q_recursion_stop}
390 % Quarks for ending recursions. Only ever used there!
391 % \cs{q_recursion_tail} is appended to whatever list structure we are
392 % doing recursion on, meaning it is added as a proper list item with
393 % whatever list separator is in use. \cs{q_recursion_stop} is placed
394 % directly after the list.
395 % \begin{macrocode}
396 \quark_new:N \q_recursion_tail
397 \quark_new:N \q_recursion_stop
398 % \end{macrocode}
399 % \end{variable}
400 %
401 % \begin{variable}{\s_@@}
402 % Private scan mark used in \pkg{l3quark}. We don't have \pkg{l3scan}
403 % yet, so we declare the scan mark here and add it to the scan mark pool
404 % later.
405 % \begin{macrocode}
406 \cs_new_eq:NN \s_@@ \scan_stop:
407 % \end{macrocode}
408 % \end{variable}
409 %
410 % \begin{variable}{\q_@@_nil}
411 % Private quark use for some tests.
412 % \begin{macrocode}
413 \quark_new:N \q_@@_nil
414 % \end{macrocode}
415 % \end{variable}
416 %
417 % \begin{macro}[EXP]{\quark_if_recursion_tail_stop:N}
418 % \UnitTested
419 % \begin{macro}[EXP]{\quark_if_recursion_tail_stop_do:Nn}
420 % \UnitTested
421 % When doing recursions, it is easy to spend a lot of time testing if the
422 % end marker has been found. To avoid this, a dedicated end marker is used
423 % each time a recursion is set up. Thus if the marker is found everything
424 % can be wrapper up and finished off. The simple case is when the test
425 % can guarantee that only a single token is being tested. In this case,
426 % there is just a dedicated copy of the standard quark test. Both a gobbling
427 % version and one inserting end code are provided.
428 % \begin{macrocode}
429 \cs_new:Npn \quark_if_recursion_tail_stop:N #1
430 {
431 \if_meaning:w \q_recursion_tail #1
432 \exp_after:wN \use_none_delimit_by_q_recursion_stop:w
433 \fi:
434 }
435 \cs_new:Npn \quark_if_recursion_tail_stop_do:Nn #1
436 {
437 \if_meaning:w \q_recursion_tail #1
438 \exp_after:wN \use_i_delimit_by_q_recursion_stop:nw
439 \else:
440 \exp_after:wN \use_none:n
441 \fi:
442 }
443 % \end{macrocode}
444 % \end{macro}
445 % \end{macro}
446 %
447 % \begin{macro}[EXP]
448 % {\quark_if_recursion_tail_stop:n, \quark_if_recursion_tail_stop:o}
449 % \UnitTested
450 % \begin{macro}[EXP]
451 % {\quark_if_recursion_tail_stop_do:nn, \quark_if_recursion_tail_stop_do:on}
452 % \UnitTested
453 % \begin{macro}[EXP]{\@@_if_recursion_tail:w}
454 % See \cs{quark_if_nil:nTF} for the details. Expanding
455 % \cs{@@_if_recursion_tail:w} once in front of the tokens chosen here
456 % gives an empty result if and only if |#1|~is exactly
457 % \cs{q_recursion_tail}.
458 % \begin{macrocode}
459 \cs_new:Npn \quark_if_recursion_tail_stop:n #1
460 {
461 \tl_if_empty:oTF
462 { \@@_if_recursion_tail:w {} #1 {} ?! \q_recursion_tail ??! }
463 { \use_none_delimit_by_q_recursion_stop:w }
464 { }
465 }
466 \cs_new:Npn \quark_if_recursion_tail_stop_do:nn #1
467 {
468 \tl_if_empty:oTF
469 { \@@_if_recursion_tail:w {} #1 {} ?! \q_recursion_tail ??! }
470 { \use_i_delimit_by_q_recursion_stop:nw }
471 { \use_none:n }
472 }
473 \cs_new:Npn \@@_if_recursion_tail:w
474 #1 \q_recursion_tail #2 ? #3 ?! { #1 #2 }
475 \cs_generate_variant:Nn \quark_if_recursion_tail_stop:n { o }
476 \cs_generate_variant:Nn \quark_if_recursion_tail_stop_do:nn { o }
477 % \end{macrocode}
478 % \end{macro}
479 % \end{macro}
480 % \end{macro}
481 %
482 % \begin{macro}[EXP]{\quark_if_recursion_tail_break:NN}
483 % \begin{macro}[EXP]{\quark_if_recursion_tail_break:nN}
484 % Analogues of the \cs[index=quark_if_recursion_tail_stop:n]
485 % {quark_if_recursion_tail_stop\ldots{}} functions.
486 % Break the mapping using |#2|.
487 % \begin{macrocode}
488 \cs_new:Npn \quark_if_recursion_tail_break:NN #1#2
489 {
490 \if_meaning:w \q_recursion_tail #1
491 \exp_after:wN #2
492 \fi:
493 }
494 \cs_new:Npn \quark_if_recursion_tail_break:nN #1#2
495 {
496 \tl_if_empty:oT
497 { \@@_if_recursion_tail:w {} #1 {} ?! \q_recursion_tail ??! }
498 {#2}
499 }
500 % \end{macrocode}
501 % \end{macro}
502 % \end{macro}
503 %
504 % \begin{macro}[pTF]{\quark_if_nil:N}
505 % \UnitTested
506 % \begin{macro}[pTF]{\quark_if_no_value:N, \quark_if_no_value:c}
507 % \UnitTested
508 % Here we test if we found a special quark as the first argument.
509 % We better start with \cs{q_no_value} as the first argument since
510 % the whole thing may otherwise loop if |#1| is wrongly given
511 % a string like |aabc| instead of a single token.\footnote{It may
512 % still loop in special circumstances however!}
513 % \begin{macrocode}
514 \prg_new_conditional:Npnn \quark_if_nil:N #1 { p, T , F , TF }
515 {
516 \if_meaning:w \q_nil #1
517 \prg_return_true:
518 \else:
519 \prg_return_false:
520 \fi:
521 }
522 \prg_new_conditional:Npnn \quark_if_no_value:N #1 { p, T , F , TF }
523 {
524 \if_meaning:w \q_no_value #1
525 \prg_return_true:
526 \else:
527 \prg_return_false:
528 \fi:
529 }
530 \prg_generate_conditional_variant:Nnn \quark_if_no_value:N
531 { c } { p , T , F , TF }
532 % \end{macrocode}
533 % \end{macro}
534 % \end{macro}
535 %
536 % \begin{macro}[pTF]{\quark_if_nil:n, \quark_if_nil:V, \quark_if_nil:o}
537 % \UnitTested
538 % \begin{macro}[pTF]{\quark_if_no_value:n}
539 % \UnitTested
540 % \begin{macro}{\@@_if_nil:w, \@@_if_no_value:w}
541 % \begin{macro}[EXP]{\@@_if_empty_if:o}
542 % Let us explain \cs{quark_if_nil:nTF}. Expanding \cs{@@_if_nil:w}
543 % once is safe thanks to the trailing \cs{q_nil} |??!|. The result of
544 % expanding once is empty if and only if both delimited arguments |#1|
545 % and~|#2| are empty and |#3|~is delimited by the last tokens~|?!|.
546 % Thanks to the leading~|{}|, the argument~|#1| is empty if and only
547 % if the argument of \cs{quark_if_nil:n} starts with \cs{q_nil}. The
548 % argument~|#2| is empty if and only if this \cs{q_nil} is followed
549 % immediately by~|?| or by~|{}?|, coming either from the trailing
550 % tokens in the definition of \cs{quark_if_nil:n}, or from its
551 % argument. In the first case, \cs{@@_if_nil:w} is followed by
552 % |{}\q_nil| |{}?| |!\q_nil|~|??!|, hence |#3|~is delimited by the
553 % final~|?!|, and the test returns \texttt{true} as wanted. In the
554 % second case, the result is not empty since the first~|?!| in the
555 % definition of \cs{quark_if_nil:n} stop~|#3|. The auxiliary here
556 % is the same as \cs{__tl_if_empty_if:o}, with the same comments
557 % applying.
558 % \begin{macrocode}
559 \prg_new_conditional:Npnn \quark_if_nil:n #1 { p, T , F , TF }
560 {
561 \@@_if_empty_if:o
562 { \@@_if_nil:w {} #1 {} ? ! \q_nil ? ? ! }
563 \prg_return_true:
564 \else:
565 \prg_return_false:
566 \fi:
567 }
568 \cs_new:Npn \@@_if_nil:w #1 \q_nil #2 ? #3 ? ! { #1 #2 }
569 \prg_new_conditional:Npnn \quark_if_no_value:n #1 { p, T , F , TF }
570 {
571 \@@_if_empty_if:o
572 { \@@_if_no_value:w {} #1 {} ? ! \q_no_value ? ? ! }
573 \prg_return_true:
574 \else:
575 \prg_return_false:
576 \fi:
577 }
578 \cs_new:Npn \@@_if_no_value:w #1 \q_no_value #2 ? #3 ? ! { #1 #2 }
579 \prg_generate_conditional_variant:Nnn \quark_if_nil:n
580 { V , o } { p , TF , T , F }
581 \cs_new:Npn \@@_if_empty_if:o #1
582 {
583 \exp_after:wN \if_meaning:w \exp_after:wN \q_nil
584 \__kernel_tl_to_str:w \exp_after:wN {#1} \q_nil
585 }
586 % \end{macrocode}
587 % \end{macro}
588 % \end{macro}
589 % \end{macro}
590 % \end{macro}
591 %
592 % \begin{macro}{\__kernel_quark_new_test:N}
593 % The function \cs{__kernel_quark_new_test:N} defines |#1| in a
594 % similar way as \cs[no-index]{quark_if_recursion_tail_...} functions
595 % (as described below), using
596 % \cs[no-index]{q__\meta{namespace}_recursion_tail} as the test quark
597 % and \cs[no-index]{q__\meta{namespace}_recursion_stop} as the
598 % delimiter quark, where the \meta{namespace} is determined as the
599 % first |_|-delimited part in~|#1|.
600 %
601 % There are six possible function types which this function can define,
602 % and which is defined depends on the signature of the function being
603 % defined:
604 % \begin{description}
605 % \def\makelabel#1{\texttt{:#1}~}
606 % \item[n] gives an analogue of \cs{quark_if_recursion_tail_stop:n}
607 % \item[nn] gives an analogue of \cs{quark_if_recursion_tail_stop_do:nn}
608 % \item[nN] gives an analogue of \cs{quark_if_recursion_tail_break:nN}
609 % \item[N] gives an analogue of \cs{quark_if_recursion_tail_stop:N}
610 % \item[Nn] gives an analogue of \cs{quark_if_recursion_tail_stop_do:Nn}
611 % \item[NN] gives an analogue of \cs{quark_if_recursion_tail_break:NN}
612 % \end{description}
613 % Any other signature causes an error, as does a function without signature.
614 %
615 % \begin{macro}{\__kernel_quark_new_conditional:Nn}
616 % Similar to \cs{__kernel_quark_new_test:N}, but defines quark
617 % branching conditionals like \cs{quark_if_nil:nTF} that test for the
618 % quark \cs[no-index]{q__\meta{namespace}_\meta{name}}.
619 % The \meta{namespace} and \meta{name} are determined from the
620 % conditional~|#1|, which must take the rather rigid form
621 % \cs[no-index]{__\meta{namespace}_quark_if_\meta{name}:\meta{arg spec}}.
622 % There are only two cases for the \meta{arg spec} here:
623 % \begin{description}
624 % \def\makelabel#1{\texttt{:#1}~}
625 % \item[n] gives an analogue of \cs{quark_if_nil:nTF}
626 % \item[N] gives an analogue of \cs{quark_if_nil:NTF}
627 % \end{description}
628 % Any other signature causes an error, as does a function without signature.
629 % We use low-level emptiness tests as \pkg{l3tl} is not available yet when these
630 % functions are used; thankfully we only care about whether strings
631 % are empty so a simple \cs{if_meaning:w} \cs{q_nil} \meta{string}
632 % \cs{q_nil} suffices.
633 %
634 % \begin{macro}{\@@_new_test:NNNn, \@@_new_test:Nccn,
635 % \@@_new_test_aux:nnNNnnnn}
636 % \begin{macro}{\@@_new_conditional:Nnnn, \@@_new_conditional:Neen}
637 % \begin{macrocode}
638 \cs_new_protected:Npn \__kernel_quark_new_test:N #1
639 { \@@_new_test_aux:Ne #1 { \@@_module_name:N #1 } }
640 \cs_new_protected:Npn \@@_new_test_aux:Nn #1 #2
641 {
642 \if_meaning:w \q_nil #2 \q_nil
643 \msg_error:nne { quark } { invalid-function }
644 { \token_to_str:N #1 }
645 \else:
646 \@@_new_test:Nccn #1
647 { q__#2_recursion_tail } { q__#2_recursion_stop } { __#2 }
648 \fi:
649 }
650 \cs_generate_variant:Nn \@@_new_test_aux:Nn { Ne }
651 \cs_new_protected:Npn \@@_new_test:NNNn #1
652 {
653 \exp_last_unbraced:Nf \@@_new_test_aux:nnNNnnnn
654 { \cs_split_function:N #1 }
655 #1 { test }
656 }
657 \cs_generate_variant:Nn \@@_new_test:NNNn { Ncc }
658 \cs_new_protected:Npn \__kernel_quark_new_conditional:Nn #1
659 {
660 \@@_new_conditional:Neen #1
661 { \@@_quark_conditional_name:N #1 }
662 { \@@_module_name:N #1 }
663 }
664 \cs_new_protected:Npn \@@_new_conditional:Nnnn #1#2#3#4
665 {
666 \if_meaning:w \q_nil #2 \q_nil
667 \msg_error:nne { quark } { invalid-function }
668 { \token_to_str:N #1 }
669 \else:
670 \if_meaning:w \q_nil #3 \q_nil
671 \msg_error:nne { quark } { invalid-function }
672 { \token_to_str:N #1 }
673 \else:
674 \exp_last_unbraced:Nf \@@_new_test_aux:nnNNnnnn
675 { \cs_split_function:N #1 }
676 #1 { conditional }
677 {#2} {#3} {#4}
678 \fi:
679 \fi:
680 }
681 \cs_generate_variant:Nn \@@_new_conditional:Nnnn { Nee }
682 \cs_new_protected:Npn \@@_new_test_aux:nnNNnnnn #1 #2 #3 #4 #5
683 {
684 \cs_if_exist_use:cTF { @@_new_#5_#2:Nnnn } { #4 }
685 {
686 \msg_error:nnee { quark } { invalid-function }
687 { \token_to_str:N #4 } {#2}
688 \use_none:nnn
689 }
690 }
691 % \end{macrocode}
692 % \end{macro}
693 % \end{macro}
694 % \end{macro}
695 % \end{macro}
696 %
697 % \begin{macro}{
698 % \@@_new_test_n:Nnnn, \@@_new_test_nn:Nnnn,
699 % \@@_new_test_N:Nnnn, \@@_new_test_Nn:Nnnn,
700 % \@@_new_test_NN:Nnnn, \@@_new_test_NN:Nnnn,
701 % }
702 % These macros implement the six possibilities mentioned above, passing
703 % the right arguments to \cs{@@_new_test_aux_do:nNNnnnnNNn},
704 % which defines some auxiliaries, and then to
705 % \cs{@@_new_test_define_tl:nNnNNn} (|:n(n)| variants) or to
706 % \cs{@@_new_test_define_ifx:nNnNNn} (|:N(n)|) which define the
707 % main conditionals.
708 % \begin{macrocode}
709 \cs_new_protected:Npn \@@_new_test_n:Nnnn #1 #2 #3 #4
710 {
711 \@@_new_test_aux_do:nNNnnnnNNn {#4} #2 #3 { none } { } { } { }
712 \@@_new_test_define_tl:nNnNNn #1 { }
713 }
714 \cs_new_protected:Npn \@@_new_test_nn:Nnnn #1 #2 #3 #4
715 {
716 \@@_new_test_aux_do:nNNnnnnNNn {#4} #2 #3 { i } { n } {##1} {##2}
717 \@@_new_test_define_tl:nNnNNn #1 { \use_none:n }
718 }
719 \cs_new_protected:Npn \@@_new_test_nN:Nnnn #1 #2 #3 #4
720 {
721 \@@_new_test_aux_do:nNNnnnnNNn {#4} #2 #3 { i } { n } {##1} {##2}
722 \@@_new_test_define_break_tl:nNNNNn #1 { }
723 }
724 \cs_new_protected:Npn \@@_new_test_N:Nnnn #1 #2 #3 #4
725 {
726 \@@_new_test_aux_do:nNNnnnnNNn {#4} #2 #3 { none } { } { } { }
727 \@@_new_test_define_ifx:nNnNNn #1 { }
728 }
729 \cs_new_protected:Npn \@@_new_test_Nn:Nnnn #1 #2 #3 #4
730 {
731 \@@_new_test_aux_do:nNNnnnnNNn {#4} #2 #3 { i } { n } {##1} {##2}
732 \@@_new_test_define_ifx:nNnNNn #1
733 { \else: \exp_after:wN \use_none:n }
734 }
735 \cs_new_protected:Npn \@@_new_test_NN:Nnnn #1 #2 #3 #4
736 {
737 \@@_new_test_aux_do:nNNnnnnNNn {#4} #2 #3 { i } { n } {##1} {##2}
738 \@@_new_test_define_break_ifx:nNNNNn #1 { }
739 }
740 % \end{macrocode}
741 % \end{macro}
742 %
743 % \begin{macro}{
744 % \@@_new_test_aux_do:nNNnnnnNNn,
745 % \@@_test_define_aux:NNNNnnNNn
746 % }
747 % \cs{@@_new_test_aux_do:nNNnnnnNNn} makes the control sequence names
748 % which will be used by \cs{@@_test_define_aux:NNNNnnNNn}, and then later
749 % by \cs{@@_new_test_define_tl:nNnNNn} or
750 % \cs{@@_new_test_define_ifx:nNnNNn}. The control sequences defined
751 % here are analogous to \cs{@@_if_recursion_tail:w} and to
752 % \cs[no-index]{use_(none|i)_delimit_by_q_recursion_stop:(|n)w}.
753 %
754 % The name is composed by the name-space and the name of the quarks.
755 % Suppose \cs{__kernel_quark_new_test:N} was used with:
756 % \begin{verbatim}
757 % \__kernel_quark_new_test:N \__test_quark_tail:n
758 % \end{verbatim}
759 % then the first auxiliary will be \cs[no-index]{__test_quark_recursion_tail:w},
760 % and the second one will be
761 % \cs[no-index]{__test_use_none_delimit_by_q_recursion_stop:w}.
762 %
763 % Note that the actual quarks are \emph{not} defined here. They should
764 % be defined separately using \cs{quark_new:N}.
765 % \begin{macrocode}
766 \cs_new_protected:Npn \@@_new_test_aux_do:nNNnnnnNNn #1 #2 #3 #4 #5
767 {
768 \exp_args:Ncc \@@_test_define_aux:NNNNnnNNn
769 { #1 _quark_recursion_tail:w }
770 { #1 _use_ #4 _delimit_by_q_recursion_stop: #5 w }
771 #2 #3
772 }
773 \cs_new_protected:Npn \@@_test_define_aux:NNNNnnNNn #1 #2 #3 #4 #5 #6 #7
774 {
775 \cs_gset:Npn #1 ##1 #3 ##2 ? ##3 ?! { ##1 ##2 }
776 \cs_gset:Npn #2 ##1 #6 #4 {#5}
777 #7 {##1} #1 #2 #3
778 }
779 % \end{macrocode}
780 % \end{macro}
781 %
782 % \begin{macro}{
783 % \@@_new_test_define_tl:nNnNNn,
784 % \@@_new_test_define_ifx:nNnNNn
785 % }
786 % \begin{macro}{
787 % \@@_new_test_define_break_tl:nNNNNn,
788 % \@@_new_test_define_break_ifx:nNNNNn
789 % }
790 % Finally, these two macros define the main conditional function using
791 % what's been set up before.
792 % \begin{macrocode}
793 \cs_new_protected:Npn \@@_new_test_define_tl:nNnNNn #1 #2 #3 #4 #5 #6
794 {
795 \cs_new:Npn #5 #1
796 {
797 \tl_if_empty:oTF
798 { #2 {} ##1 {} ?! #4 ??! }
799 {#3} {#6}
800 }
801 }
802 \cs_new_protected:Npn \@@_new_test_define_ifx:nNnNNn #1 #2 #3 #4 #5 #6
803 {
804 \cs_new:Npn #5 #1
805 {
806 \if_meaning:w #4 ##1
807 \exp_after:wN #3
808 #6
809 \fi:
810 }
811 }
812 \cs_new_protected:Npn \@@_new_test_define_break_tl:nNNNNn #1 #2 #3
813 { \@@_new_test_define_tl:nNnNNn {##1##2} #2 {##2} }
814 \cs_new_protected:Npn \@@_new_test_define_break_ifx:nNNNNn #1 #2 #3
815 { \@@_new_test_define_ifx:nNnNNn {##1##2} #2 {##2} }
816 % \end{macrocode}
817 % \end{macro}
818 % \end{macro}
819 %
820 % \begin{macro}{
821 % \@@_new_conditional_n:Nnnn,
822 % \@@_new_conditional_N:Nnnn,
823 % \@@_new_conditional_n_aux:NNNn,
824 % \@@_new_conditional_N_aux:NNNn
825 % }
826 % These macros implement the two possibilities for branching quark
827 % conditionals. To avoid constructing without defining the
828 % \cs[no-index]{__\meta{type}_if_quark_\meta{name}:w} helper,
829 % \texttt{N}-type function accepts a \cs{prg_do_nothing:} as
830 % a placeholder.
831 % \begin{macrocode}
832 \cs_new_protected:Npn \@@_new_conditional_n:Nnnn #1 #2 #3
833 {
834 \exp_args:Ncc \@@_new_conditional_n_aux:NNNn
835 { __ #3 _if_quark_ #2 :w } { q__ #3 _ #2 } #1
836 }
837 \cs_new_protected:Npn \@@_new_conditional_N:Nnnn #1 #2 #3
838 {
839 \exp_args:NNc \@@_new_conditional_N_aux:NNNn
840 \prg_do_nothing: { q__ #3 _ #2 } #1
841 }
842 \cs_new_protected:Npn \@@_new_conditional_n_aux:NNNn #1 #2 #3 #4
843 {
844 \cs_gset:Npn #1 ##1 #2 ##2 ? ##3 ?! { ##1##2 }
845 \prg_new_conditional:Npnn #3 ##1 {#4}
846 {
847 \@@_if_empty_if:o { #1 {} ##1 {} ?! #2 ??! }
848 \prg_return_true:
849 \else:
850 \prg_return_false:
851 \fi:
852 }
853 }
854 \cs_new_protected:Npn \@@_new_conditional_N_aux:NNNn #1 #2 #3 #4
855 {
856 \prg_new_conditional:Npnn #3 ##1 {#4}
857 {
858 \if_meaning:w #2 ##1
859 \prg_return_true:
860 \else:
861 \prg_return_false:
862 \fi:
863 }
864 }
865 % \end{macrocode}
866 % \end{macro}
867 %
868 % \begin{macro}[EXP]{\@@_module_name:N}
869 % \begin{macro}[EXP]{
870 % \@@_module_name:w,
871 % \@@_module_name_loop:w,
872 % \@@_module_name_end:w
873 % }
874 % \cs{@@_module_name:N} takes a control sequence and returns its
875 % \meta{module} name, determined as the first non-empty
876 % non-single-character word, separated by |_| or~|:|. These rules
877 % give the correct result for public functions
878 % \cs[no-index]{\meta{module}_\ldots{}}, private functions
879 % \cs[no-index]{__\meta{module}_\ldots{}}, and variables such as
880 % \cs[no-index]{l_\meta{module}_\ldots{}}. If no valid module is
881 % found the result is an empty string. The approach is to first cut
882 % off everything after the (first) |:| if any is present, then
883 % repeatedly grab |_|-delimited words until finding one of length at
884 % least~$2$ (we use low-level tests as \pkg{l3tl} is not fully
885 % available when \cs{__kernel_quark_new_test:N} is first used.
886 % If no \meta{module} is found (such as in \cs{::n}) we
887 % get the trailing marker \cs{use_none:n} |{}|, which expands to
888 % nothing.
889 % \begin{macrocode}
890 \cs_set:Npn \@@_tmp:w #1#2
891 {
892 \cs_new:Npn \@@_module_name:N ##1
893 {
894 \exp_last_unbraced:Nf \@@_module_name:w
895 { \cs_to_str:N ##1 } #1 \s_@@
896 }
897 \cs_new:Npn \@@_module_name:w ##1 #1 ##2 \s_@@
898 { \@@_module_name_loop:w ##1 #2 \use_none:n { } #2 \s_@@ }
899 \cs_new:Npn \@@_module_name_loop:w ##1 #2
900 {
901 \use_i_ii:nnn \if_meaning:w \prg_do_nothing:
902 ##1 \prg_do_nothing: \prg_do_nothing:
903 \exp_after:wN \@@_module_name_loop:w
904 \else:
905 \@@_module_name_end:w ##1
906 \fi:
907 }
908 \cs_new:Npn \@@_module_name_end:w
909 ##1 \fi: ##2 \s_@@ { \fi: ##1 }
910 }
911 \exp_after:wN \@@_tmp:w \tl_to_str:n { : _ }
912 % \end{macrocode}
913 % \end{macro}
914 % \end{macro}
915 %
916 % \begin{macro}[EXP]{\@@_quark_conditional_name:N, \@@_quark_conditional_name:w}
917 % \cs{@@_quark_conditional_name:N} determines the quark name that the quark
918 % conditional function~|##1| queries, as the part of the function name
919 % between |_quark_if_| and the trailing~|:|. Again we define it
920 % through \cs{@@_tmp:w}, which receives |:| as |#1| and |_quark_if_|
921 % as |#2|. The auxiliary \cs{@@_quark_conditional_name:w} returns the part
922 % between the first |_quark_if_| and the next~|:|, and we apply this
923 % auxiliary to the function name followed by |:| (in case the function
924 % name is lacking a signature), and |_quark_if_:| so that
925 % \cs{@@_quark_conditional_name:N} returns an empty string if |_quark_if_| is
926 % not present.
927 % \begin{macrocode}
928 \cs_set:Npn \@@_tmp:w #1 #2 \s_@@
929 {
930 \cs_new:Npn \@@_quark_conditional_name:N ##1
931 {
932 \exp_last_unbraced:Nf \@@_quark_conditional_name:w
933 { \cs_to_str:N ##1 } #1 #2 #1 \s_@@
934 }
935 \cs_new:Npn \@@_quark_conditional_name:w
936 ##1 #2 ##2 #1 ##3 \s_@@ {##2}
937 }
938 \exp_after:wN \@@_tmp:w \tl_to_str:n { : _quark_if_ } \s_@@
939 % \end{macrocode}
940 % \end{macro}
941 %
942 % \subsection{Scan marks}
943 %
944 % \begin{macrocode}
945 %<@@=scan>
946 % \end{macrocode}
947 %
948 % \begin{macro}{\scan_new:N}
949 % \UnitTested
950 % Check whether the variable is already a scan mark,
951 % then declare it to be equal to \cs{scan_stop:} globally.
952 % \begin{macrocode}
953 \cs_new_protected:Npn \scan_new:N #1
954 {
955 \tl_if_in:NnTF \g_@@_marks_tl { #1 }
956 {
957 \msg_error:nne { scanmark } { already-defined }
958 { \token_to_str:N #1 }
959 }
960 {
961 \tl_gput_right:Nn \g_@@_marks_tl {#1}
962 \cs_new_eq:NN #1 \scan_stop:
963 }
964 }
965 % \end{macrocode}
966 % \end{macro}
967 %
968 % \begin{variable}{\s_stop}
969 % \begin{variable}{\g_@@_marks_tl}
970 % \UnitTested
971 % We only declare one scan mark here, more can be defined
972 % by specific modules.
973 % Can't use \cs{scan_new:N} yet because \pkg{l3tl} isn't loaded,
974 % so define \cs{s_stop} by hand and add it to \cs{g_@@_marks_tl}.
975 % We also add the scan marks declared earlier to the pool here.
976 % Since they lives in a different namespace, a little \pkg{DocStrip}
977 % cheating is necessary.
978 % \begin{macrocode}
979 \cs_new_eq:NN \s_stop \scan_stop:
980 \cs_gset_nopar:Npn \g_@@_marks_tl
981 {
982 \s_stop
983 %<@@=quark>
984 \s_@@
985 %<@@=cs>
986 \s_@@_mark
987 \s_@@_stop
988 %<@@=scan>
989 }
990 % \end{macrocode}
991 % \end{variable}
992 % \end{variable}
993 %
994 % \begin{macro}[EXP]{\use_none_delimit_by_s_stop:w}
995 % \UnitTested
996 % Similar to \cs{use_none_delimit_by_q_stop:w}.
997 % \begin{macrocode}
998 \cs_new:Npn \use_none_delimit_by_s_stop:w #1 \s_stop { }
999 % \end{macrocode}
1000 % \end{macro}
1001 %
1002 % \begin{macrocode}
1003 %</code>
1004 % \end{macrocode}
1005 %
1006 % \end{implementation}
1007 %
1008 % \PrintIndex