Debian Codegraph @

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

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