Debian Codegraph @

texmf-dist/source/latex-dev/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-26}
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 (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 (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: { p, 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 (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 \bool_lazy_or:nnTF
2803 { \@@_if_in_class_p: }
2804 { \int_compare_p:nNn \l_@@_mode_int = \c_@@_catcode_in_class_mode_int }
2805 {
2806 \@@_two_if_eq:NNNNTF #2 #3 \@@_compile_special:N -
2807 { \@@_compile_range:Nw #1 }
2808 {
2809 \@@_compile_one:n
2810 { \@@_item_equal:n { \int_value:w `#1 } }
2811 #2 #3
2812 }
2813 }
2814 {
2815 \@@_compile_one:n
2816 { \@@_item_equal:n { \int_value:w `#1 } }
2817 #2 #3
2818 }
2819 }
2820 % \end{macrocode}
2821 % \end{macro}
2822 %
2823 % \begin{macro}{\@@_compile_range:Nw, \@@_if_end_range:NNTF}
2824 % We have just read a raw character followed by a dash; this should be
2825 % followed by an end-point for the range. Valid end-points are: any
2826 % raw character; any special character, except a right bracket. In
2827 % particular, escaped characters are forbidden.
2828 % \begin{macrocode}
2829 \cs_new_protected:Npn \@@_if_end_range:NNTF #1#2
2830 {
2831 \if_meaning:w \@@_compile_raw:N #1
2832 \else:
2833 \if_meaning:w \@@_compile_special:N #1
2834 \if_charcode:w ] #2
2835 \use_i:nn
2836 \fi:
2837 \else:
2838 \exp_after:wN \exp_after:wN \exp_after:wN \use_iii:nnn
2839 \fi:
2840 \fi:
2841 \use_i:nn
2842 }
2843 \cs_new_protected:Npn \@@_compile_range:Nw #1#2#3
2844 {
2845 \@@_if_end_range:NNTF #2 #3
2846 {
2847 \if_int_compare:w `#1 > `#3 \exp_stop_f:
2848 \msg_error:nnee { regex } { range-backwards } {#1} {#3}
2849 \else:
2850 \@@_compile_one:n
2851 {
2852 \if_int_compare:w `#1 = `#3 \exp_stop_f:
2853 \@@_item_equal:n
2854 \else:
2855 \@@_item_range:nn { \int_value:w `#1 }
2856 \fi:
2857 { \int_value:w `#3 }
2858 }
2859 \fi:
2860 }
2861 {
2862 \msg_warning:nnee { regex } { range-missing-end }
2863 {#1} { \c_backslash_str #3 }
2864 \@@_compile_one:n
2865 {
2866 \@@_item_equal:n { \int_value:w `#1 \exp_stop_f: }
2867 \@@_item_equal:n { \int_value:w `- \exp_stop_f: }
2868 }
2869 #2#3
2870 }
2871 }
2872 % \end{macrocode}
2873 % \end{macro}
2874 %
2875 % \subsubsection{Character properties}
2876 %
2877 % \begin{macro}{\@@_compile_.:, \@@_prop_.:}
2878 % In a class, the dot has no special meaning. Outside, insert
2879 % \cs{@@_prop_.:}, which matches any character or control
2880 % sequence, and refuses $-2$ (end-marker).
2881 % \begin{macrocode}
2882 \cs_new_protected:cpe { @@_compile_.: }
2883 {
2884 \exp_not:N \@@_if_in_class:TF
2885 { \@@_compile_raw:N . }
2886 { \@@_compile_one:n \exp_not:c { @@_prop_.: } }
2887 }
2888 \cs_new_protected:cpn { @@_prop_.: }
2889 {
2890 \if_int_compare:w \l_@@_curr_char_int > - 2 \exp_stop_f:
2891 \exp_after:wN \@@_break_true:w
2892 \fi:
2893 }
2894 % \end{macrocode}
2895 % \end{macro}
2896 %
2897 % \begin{macro}
2898 % {
2899 % \@@_compile_/d:, \@@_compile_/D:,
2900 % \@@_compile_/h:, \@@_compile_/H:,
2901 % \@@_compile_/s:, \@@_compile_/S:,
2902 % \@@_compile_/v:, \@@_compile_/V:,
2903 % \@@_compile_/w:, \@@_compile_/W:,
2904 % \@@_compile_/N:,
2905 % }
2906 % The constants \cs{@@_prop_d:}, etc. hold
2907 % a list of tests which match the corresponding character
2908 % class, and jump to the \cs{@@_break_point:TF} marker.
2909 % As for a normal character, we check for quantifiers.
2910 % \begin{macrocode}
2911 \cs_set_protected:Npn \@@_tmp:w #1#2
2912 {
2913 \cs_new_protected:cpe { @@_compile_/#1: }
2914 { \@@_compile_one:n \exp_not:c { @@_prop_#1: } }
2915 \cs_new_protected:cpe { @@_compile_/#2: }
2916 {
2917 \@@_compile_one:n
2918 { \@@_item_reverse:n { \exp_not:c { @@_prop_#1: } } }
2919 }
2920 }
2921 \@@_tmp:w d D
2922 \@@_tmp:w h H
2923 \@@_tmp:w s S
2924 \@@_tmp:w v V
2925 \@@_tmp:w w W
2926 \cs_new_protected:cpn { @@_compile_/N: }
2927 { \@@_compile_one:n \@@_prop_N: }
2928 % \end{macrocode}
2929 % \end{macro}
2930 %
2931 % \subsubsection{Anchoring and simple assertions}
2932 %
2933 % \begin{macro}{\@@_compile_anchor_letter:NNN}
2934 % \begin{macro}{\@@_compile_/A:, \@@_compile_/G:, \@@_compile_/Z:, \@@_compile_/z:, \@@_compile_/b:, \@@_compile_/B:}
2935 % \begin{macro}+\@@_compile_^:+
2936 % \begin{macro}+\@@_compile_$:+
2937 % In modes where assertions are forbidden, anchors such as |\A|
2938 % produce an error (|\A|~is invalid in classes); otherwise they add an
2939 % \cs{@@_assertion:Nn} test as appropriate (the only negative
2940 % assertion is~|\B|). The test functions are defined later. The
2941 % implementation for
2942 % |$| and |^| is only different from |\A| etc because these are valid
2943 % in a class.
2944 % \begin{macrocode}
2945 \cs_new_protected:Npn \@@_compile_anchor_letter:NNN #1#2#3
2946 {
2947 \@@_if_in_class_or_catcode:TF { \@@_compile_raw_error:N #1 }
2948 {
2949 \tl_build_put_right:Nn \l_@@_build_tl
2950 { \@@_assertion:Nn #2 {#3} }
2951 }
2952 }
2953 \cs_new_protected:cpn { @@_compile_/A: }
2954 { \@@_compile_anchor_letter:NNN A \c_true_bool \@@_A_test: }
2955 \cs_new_protected:cpn { @@_compile_/G: }
2956 { \@@_compile_anchor_letter:NNN G \c_true_bool \@@_G_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_/z: }
2960 { \@@_compile_anchor_letter:NNN z \c_true_bool \@@_Z_test: }
2961 \cs_new_protected:cpn { @@_compile_/b: }
2962 { \@@_compile_anchor_letter:NNN b \c_true_bool \@@_b_test: }
2963 \cs_new_protected:cpn { @@_compile_/B: }
2964 { \@@_compile_anchor_letter:NNN B \c_false_bool \@@_b_test: }
2965 \cs_set_protected:Npn \@@_tmp:w #1#2
2966 {
2967 \cs_new_protected:cpn { @@_compile_#1: }
2968 {
2969 \@@_if_in_class_or_catcode:TF { \@@_compile_raw:N #1 }
2970 {
2971 \tl_build_put_right:Nn \l_@@_build_tl
2972 { \@@_assertion:Nn \c_true_bool {#2} }
2973 }
2974 }
2975 }
2976 \exp_args:Ne \@@_tmp:w { \iow_char:N \^ } { \@@_A_test: }
2977 \exp_args:Ne \@@_tmp:w { \iow_char:N \$ } { \@@_Z_test: }
2978 % \end{macrocode}
2979 % \end{macro}
2980 % \end{macro}
2981 % \end{macro}
2982 % \end{macro}
2983 %
2984 % \subsubsection{Character classes}
2985 %
2986 % \begin{macro}{\@@_compile_]:}
2987 % Outside a class, right brackets have no meaning. In a class, change
2988 % the mode ($m\to (m-15)/13$, truncated) to reflect the fact that we
2989 % are leaving the class. Look for quantifiers, unless we are still in
2990 % a class after leaving one (the case of |[...\cL[...]...]|).
2991 % quantifiers.
2992 % \begin{macrocode}
2993 \cs_new_protected:cpn { @@_compile_]: }
2994 {
2995 \@@_if_in_class:TF
2996 {
2997 \if_int_compare:w \l_@@_mode_int >
2998 \c_@@_catcode_in_class_mode_int
2999 \tl_build_put_right:Nn \l_@@_build_tl { \if_false: { \fi: } }
3000 \fi:
3001 \tex_advance:D \l_@@_mode_int - 15 \exp_stop_f:
3002 \tex_divide:D \l_@@_mode_int 13 \exp_stop_f:
3003 \if_int_odd:w \l_@@_mode_int \else:
3004 \exp_after:wN \@@_compile_quantifier:w
3005 \fi:
3006 }
3007 { \@@_compile_raw:N ] }
3008 }
3009 % \end{macrocode}
3010 % \end{macro}
3011 %
3012 % \begin{macro}{\@@_compile_[:}
3013 % In a class, left brackets might introduce a \textsc{posix} character
3014 % class, or mean nothing. Immediately following |\c|\meta{category},
3015 % we must insert the appropriate catcode test, then parse the class; we
3016 % pre-expand the catcode as an optimization. Otherwise (modes $0$,
3017 % $-2$ and $-6$) just parse the class. The mode is updated later.
3018 % \begin{macrocode}
3019 \cs_new_protected:cpn { @@_compile_[: }
3020 {
3021 \@@_if_in_class:TF
3022 { \@@_compile_class_posix_test:w }
3023 {
3024 \@@_if_within_catcode:TF
3025 {
3026 \exp_after:wN \@@_compile_class_catcode:w
3027 \int_use:N \l_@@_catcodes_int \@@_sep:
3028 }
3029 { \@@_compile_class_normal:w }
3030 }
3031 }
3032 % \end{macrocode}
3033 % \end{macro}
3034 %
3035 % \begin{macro}{\@@_compile_class_normal:w}
3036 % In the \enquote{normal} case, we insert \cs{@@_class:NnnnN}
3037 % \meta{boolean} in the compiled code. The \meta{boolean} is true for
3038 % positive classes, and false for negative classes, characterized by a
3039 % leading |^|. The auxiliary \cs{@@_compile_class:TFNN} also
3040 % checks for a leading |]| which has a special meaning.
3041 % \begin{macrocode}
3042 \cs_new_protected:Npn \@@_compile_class_normal:w
3043 {
3044 \@@_compile_class:TFNN
3045 { \@@_class:NnnnN \c_true_bool }
3046 { \@@_class:NnnnN \c_false_bool }
3047 }
3048 % \end{macrocode}
3049 % \end{macro}
3050 %
3051 % \begin{macro}{\@@_compile_class_catcode:w}
3052 % This function is called for a left bracket in modes $2$ or $6$
3053 % (catcode test, and catcode test within a class). In mode $2$ the
3054 % whole construction needs to be put in a class (like single
3055 % character). Then determine if the class is positive or negative,
3056 % inserting \cs{@@_item_catcode:nT} or the \texttt{reverse} variant
3057 % as appropriate, each with the current catcodes bitmap |#1| as an
3058 % argument, and reset the catcodes.
3059 % \begin{macrocode}
3060 \cs_new_protected:Npn \@@_compile_class_catcode:w #1 \@@_sep:
3061 {
3062 \if_int_compare:w \l_@@_mode_int = \c_@@_catcode_mode_int
3063 \tl_build_put_right:Nn \l_@@_build_tl
3064 { \@@_class:NnnnN \c_true_bool { \if_false: } \fi: }
3065 \fi:
3066 \int_set_eq:NN \l_@@_catcodes_int \l_@@_default_catcodes_int
3067 \@@_compile_class:TFNN
3068 { \@@_item_catcode:nT {#1} }
3069 { \@@_item_catcode_reverse:nT {#1} }
3070 }
3071 % \end{macrocode}
3072 % \end{macro}
3073 %
3074 % \begin{macro}
3075 % {\@@_compile_class:TFNN, \@@_compile_class:NN}
3076 % If the first character is |^|, then the class is negative (use
3077 % |#2|), otherwise it is positive (use |#1|). If the next character
3078 % is a right bracket, then it should be changed to a raw one.
3079 % \begin{macrocode}
3080 \cs_new_protected:Npn \@@_compile_class:TFNN #1#2#3#4
3081 {
3082 \l_@@_mode_int = \int_value:w \l_@@_mode_int 3 \exp_stop_f:
3083 \@@_two_if_eq:NNNNTF #3 #4 \@@_compile_special:N ^
3084 {
3085 \tl_build_put_right:Nn \l_@@_build_tl { #2 { \if_false: } \fi: }
3086 \@@_compile_class:NN
3087 }
3088 {
3089 \tl_build_put_right:Nn \l_@@_build_tl { #1 { \if_false: } \fi: }
3090 \@@_compile_class:NN #3 #4
3091 }
3092 }
3093 \cs_new_protected:Npn \@@_compile_class:NN #1#2
3094 {
3095 \token_if_eq_charcode:NNTF #2 ]
3096 { \@@_compile_raw:N #2 }
3097 { #1 #2 }
3098 }
3099 % \end{macrocode}
3100 % \end{macro}
3101 %
3102 % \begin{macro}
3103 % {
3104 % \@@_compile_class_posix_test:w,
3105 % \@@_compile_class_posix:NNNNw,
3106 % \@@_compile_class_posix_loop:w,
3107 % \@@_compile_class_posix_end:w
3108 % }
3109 % Here we check for a syntax such as |[:alpha:]|. We also detect |[=|
3110 % and |[.| which have a meaning in \textsc{posix} regular expressions,
3111 % but are not implemented in \pkg{l3regex}. In case we see |[:|, grab
3112 % raw characters until hopefully reaching |:]|. If that's missing, or
3113 % the \textsc{posix} class is unknown, abort. If all is right, add the
3114 % test to the current class, with an extra \cs{@@_item_reverse:n}
3115 % for negative classes (we make sure to wrap its argument in braces
3116 % otherwise \cs{regex_show:N} would not recognize the regex as valid).
3117 % \begin{macrocode}
3118 \cs_new_protected:Npn \@@_compile_class_posix_test:w #1#2
3119 {
3120 \token_if_eq_meaning:NNT \@@_compile_special:N #1
3121 {
3122 \str_case:nn { #2 }
3123 {
3124 : { \@@_compile_class_posix:NNNNw }
3125 = {
3126 \msg_warning:nne { regex }
3127 { posix-unsupported } { = }
3128 }
3129 . {
3130 \msg_warning:nne { regex }
3131 { posix-unsupported } { . }
3132 }
3133 }
3134 }
3135 \@@_compile_raw:N [ #1 #2
3136 }
3137 \cs_new_protected:Npn \@@_compile_class_posix:NNNNw #1#2#3#4#5#6
3138 {
3139 \@@_two_if_eq:NNNNTF #5 #6 \@@_compile_special:N ^
3140 {
3141 \bool_set_false:N \l_@@_tmp_bool
3142 \__kernel_tl_set:Nx \l_@@_tmpa_tl { \if_false: } \fi:
3143 \@@_compile_class_posix_loop:w
3144 }
3145 {
3146 \bool_set_true:N \l_@@_tmp_bool
3147 \__kernel_tl_set:Nx \l_@@_tmpa_tl { \if_false: } \fi:
3148 \@@_compile_class_posix_loop:w #5 #6
3149 }
3150 }
3151 \cs_new:Npn \@@_compile_class_posix_loop:w #1#2
3152 {
3153 \token_if_eq_meaning:NNTF \@@_compile_raw:N #1
3154 { #2 \@@_compile_class_posix_loop:w }
3155 { \if_false: { \fi: } \@@_compile_class_posix_end:w #1 #2 }
3156 }
3157 \cs_new_protected:Npn \@@_compile_class_posix_end:w #1#2#3#4
3158 {
3159 \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N :
3160 { \@@_two_if_eq:NNNNTF #3 #4 \@@_compile_special:N ] }
3161 { \use_ii:nn }
3162 {
3163 \cs_if_exist:cTF { @@_posix_ \l_@@_tmpa_tl : }
3164 {
3165 \@@_compile_one:n
3166 {
3167 \bool_if:NTF \l_@@_tmp_bool \use:n \@@_item_reverse:n
3168 { \exp_not:c { @@_posix_ \l_@@_tmpa_tl : } }
3169 }
3170 }
3171 {
3172 \msg_warning:nne { regex } { posix-unknown }
3173 { \l_@@_tmpa_tl }
3174 \@@_compile_abort_tokens:e
3175 {
3176 [: \bool_if:NF \l_@@_tmp_bool { ^ }
3177 \l_@@_tmpa_tl :]
3178 }
3179 }
3180 }
3181 {
3182 \msg_error:nnee { regex } { posix-missing-close }
3183 { [: \l_@@_tmpa_tl } { #2 #4 }
3184 \@@_compile_abort_tokens:e { [: \l_@@_tmpa_tl }
3185 #1 #2 #3 #4
3186 }
3187 }
3188 % \end{macrocode}
3189 % \end{macro}
3190 %
3191 % \subsubsection{Groups and alternations}
3192 %
3193 % \begin{macro}{\@@_compile_group_begin:N, \@@_compile_group_end:}
3194 % The contents of a regex group are turned into compiled code in
3195 % \cs{l_@@_build_tl}, which ends up with items of the form
3196 % \cs{@@_branch:n} \Arg{concatenation}. This construction is done
3197 % using \cs[no-index]{tl_build_\ldots{}} functions within a \TeX{} group, which automatically
3198 % makes sure that options (case-sensitivity and default catcode) are
3199 % reset at the end of the group. The argument |#1| is
3200 % \cs{@@_group:nnnN} or a variant thereof. A small subtlety to
3201 % support |\cL(abc)| as a shorthand for |(\cLa\cLb\cLc)|: exit any
3202 % pending catcode test, save the category code at the start of the
3203 % group as the default catcode for that group, and make sure that the
3204 % catcode is restored to the default outside the group.
3205 % \begin{macrocode}
3206 \cs_new_protected:Npn \@@_compile_group_begin:N #1
3207 {
3208 \tl_build_put_right:Nn \l_@@_build_tl { #1 { \if_false: } \fi: }
3209 \@@_mode_quit_c:
3210 \group_begin:
3211 \tl_build_begin:N \l_@@_build_tl
3212 \int_set_eq:NN \l_@@_default_catcodes_int \l_@@_catcodes_int
3213 \int_incr:N \l_@@_group_level_int
3214 \tl_build_put_right:Nn \l_@@_build_tl
3215 { \@@_branch:n { \if_false: } \fi: }
3216 }
3217 \cs_new_protected:Npn \@@_compile_group_end:
3218 {
3219 \if_int_compare:w \l_@@_group_level_int > \c_zero_int
3220 \tl_build_put_right:Nn \l_@@_build_tl { \if_false: { \fi: } }
3221 \tl_build_end:N \l_@@_build_tl
3222 \exp_args:NNNe
3223 \group_end:
3224 \tl_build_put_right:Nn \l_@@_build_tl { \l_@@_build_tl }
3225 \int_set_eq:NN \l_@@_catcodes_int \l_@@_default_catcodes_int
3226 \exp_after:wN \@@_compile_quantifier:w
3227 \else:
3228 \msg_warning:nn { regex } { extra-rparen }
3229 \exp_after:wN \@@_compile_raw:N \exp_after:wN )
3230 \fi:
3231 }
3232 % \end{macrocode}
3233 % \end{macro}
3234 %
3235 % \begin{macro}{\@@_compile_(:}
3236 % In a class, parentheses are not special. In a catcode test inside a
3237 % class, a left parenthesis gives an error, to catch |[a\cL(bcd)e]|.
3238 % Otherwise check for a |?|, denoting special groups, and run the code
3239 % for the corresponding special group.
3240 % \begin{macrocode}
3241 \cs_new_protected:cpn { @@_compile_(: }
3242 {
3243 \@@_if_in_class:TF { \@@_compile_raw:N ( }
3244 {
3245 \if_int_compare:w \l_@@_mode_int =
3246 \c_@@_catcode_in_class_mode_int
3247 \msg_error:nn { regex } { c-lparen-in-class }
3248 \exp_after:wN \@@_compile_raw:N \exp_after:wN (
3249 \else:
3250 \exp_after:wN \@@_compile_lparen:w
3251 \fi:
3252 }
3253 }
3254 \cs_new_protected:Npn \@@_compile_lparen:w #1#2#3#4
3255 {
3256 \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N ?
3257 {
3258 \cs_if_exist_use:cF
3259 { @@_compile_special_group_\token_to_str:N #4 :w }
3260 {
3261 \msg_warning:nne { regex } { special-group-unknown }
3262 { (? #4 }
3263 \@@_compile_group_begin:N \@@_group:nnnN
3264 \@@_compile_raw:N ? #3 #4
3265 }
3266 }
3267 {
3268 \@@_compile_group_begin:N \@@_group:nnnN
3269 #1 #2 #3 #4
3270 }
3271 }
3272 % \end{macrocode}
3273 % \end{macro}
3274 %
3275 % \begin{macro}+\@@_compile_|:+
3276 % In a class, the pipe is not special. Otherwise, end the current
3277 % branch and open another one.
3278 % \begin{macrocode}
3279 \cs_new_protected:cpn { @@_compile_|: }
3280 {
3281 \@@_if_in_class:TF { \@@_compile_raw:N | }
3282 {
3283 \tl_build_put_right:Nn \l_@@_build_tl
3284 { \if_false: { \fi: } \@@_branch:n { \if_false: } \fi: }
3285 }
3286 }
3287 % \end{macrocode}
3288 % \end{macro}
3289 %
3290 % \begin{macro}{\@@_compile_):}
3291 % Within a class, parentheses are not special. Outside, close a group.
3292 % \begin{macrocode}
3293 \cs_new_protected:cpn { @@_compile_): }
3294 {
3295 \@@_if_in_class:TF { \@@_compile_raw:N ) }
3296 { \@@_compile_group_end: }
3297 }
3298 % \end{macrocode}
3299 % \end{macro}
3300 %
3301 % \begin{macro}{\@@_compile_special_group_::w}
3302 % \begin{macro}+\@@_compile_special_group_|:w+
3303 % Non-capturing, and resetting groups are easy to take care of during
3304 % compilation; for those groups, the harder parts come when building.
3305 % \begin{macrocode}
3306 \cs_new_protected:cpn { @@_compile_special_group_::w }
3307 { \@@_compile_group_begin:N \@@_group_no_capture:nnnN }
3308 \cs_new_protected:cpn { @@_compile_special_group_|:w }
3309 { \@@_compile_group_begin:N \@@_group_resetting:nnnN }
3310 % \end{macrocode}
3311 % \end{macro}
3312 % \end{macro}
3313 %
3314 % \begin{macro}
3315 % {\@@_compile_special_group_i:w, \@@_compile_special_group_-:w}
3316 % The match can be made case-insensitive by setting the option with
3317 % \texttt{(?i)}; the original behavior is restored by \texttt{(?-i)}.
3318 % This is the only supported option.
3319 % \begin{macrocode}
3320 \cs_new_protected:Npn \@@_compile_special_group_i:w #1#2
3321 {
3322 \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N )
3323 {
3324 \cs_set:Npn \@@_item_equal:n
3325 { \@@_item_caseless_equal:n }
3326 \cs_set:Npn \@@_item_range:nn
3327 { \@@_item_caseless_range:nn }
3328 }
3329 {
3330 \msg_warning:nne { regex } { unknown-option } { (?i #2 }
3331 \@@_compile_raw:N (
3332 \@@_compile_raw:N ?
3333 \@@_compile_raw:N i
3334 #1 #2
3335 }
3336 }
3337 \cs_new_protected:cpn { @@_compile_special_group_-:w } #1#2#3#4
3338 {
3339 \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_raw:N i
3340 { \@@_two_if_eq:NNNNTF #3 #4 \@@_compile_special:N ) }
3341 { \use_ii:nn }
3342 {
3343 \cs_set:Npn \@@_item_equal:n
3344 { \@@_item_caseful_equal:n }
3345 \cs_set:Npn \@@_item_range:nn
3346 { \@@_item_caseful_range:nn }
3347 }
3348 {
3349 \msg_warning:nne { regex } { unknown-option } { (?-#2#4 }
3350 \@@_compile_raw:N (
3351 \@@_compile_raw:N ?
3352 \@@_compile_raw:N -
3353 #1 #2 #3 #4
3354 }
3355 }
3356 % \end{macrocode}
3357 % \end{macro}
3358 %
3359 % \subsubsection{Catcodes and csnames}
3360 %
3361 % \begin{macro}{\@@_compile_/c:, \@@_compile_c_test:NN}
3362 % The |\c| escape sequence can be followed by a capital letter
3363 % representing a character category, by a left bracket which starts a
3364 % list of categories, or by a brace group holding a regular expression
3365 % for a control sequence name. Otherwise, raise an error.
3366 % \begin{macrocode}
3367 \cs_new_protected:cpn { @@_compile_/c: }
3368 { \@@_chk_c_allowed:T { \@@_compile_c_test:NN } }
3369 \cs_new_protected:Npn \@@_compile_c_test:NN #1#2
3370 {
3371 \token_if_eq_meaning:NNTF #1 \@@_compile_raw:N
3372 {
3373 \int_if_exist:cTF { c_@@_catcode_#2_int }
3374 {
3375 \int_set_eq:Nc \l_@@_catcodes_int
3376 { c_@@_catcode_#2_int }
3377 \l_@@_mode_int
3378 = \if_case:w \l_@@_mode_int
3379 \c_@@_catcode_mode_int
3380 \else:
3381 \c_@@_catcode_in_class_mode_int
3382 \fi:
3383 \token_if_eq_charcode:NNT C #2 { \@@_compile_c_C:NN }
3384 }
3385 }
3386 { \cs_if_exist_use:cF { @@_compile_c_#2:w } }
3387 {
3388 \msg_error:nne { regex } { c-missing-category } {#2}
3389 #1 #2
3390 }
3391 }
3392 % \end{macrocode}
3393 % \end{macro}
3394 %
3395 % \begin{macro}{\@@_compile_c_C:NN}
3396 % If |\cC| is not followed by |.| or |(...)| then complain because
3397 % that construction cannot match anything, except in cases like
3398 % |\cC[\c{...}]|, where it has no effect.
3399 % \begin{macrocode}
3400 \cs_new_protected:Npn \@@_compile_c_C:NN #1#2
3401 {
3402 \token_if_eq_meaning:NNTF #1 \@@_compile_special:N
3403 {
3404 \token_if_eq_charcode:NNTF #2 .
3405 { \use_none:n }
3406 { \token_if_eq_charcode:NNF #2 ( } % )
3407 }
3408 { \use:n }
3409 { \msg_error:nnn { regex } { c-C-invalid } {#2} }
3410 #1 #2
3411 }
3412 % \end{macrocode}
3413 % \end{macro}
3414 %
3415 % \begin{macro}
3416 % {
3417 % \@@_compile_c_[:w,
3418 % \@@_compile_c_lbrack_loop:NN,
3419 % \@@_compile_c_lbrack_add:N,
3420 % \@@_compile_c_lbrack_end:,
3421 % }
3422 % When encountering |\c[|, the task is to collect uppercase letters
3423 % representing character categories. First check for |^| which negates
3424 % the list of category codes.
3425 % \begin{macrocode}
3426 \cs_new_protected:cpn { @@_compile_c_[:w } #1#2
3427 {
3428 \l_@@_mode_int
3429 = \if_case:w \l_@@_mode_int
3430 \c_@@_catcode_mode_int
3431 \else:
3432 \c_@@_catcode_in_class_mode_int
3433 \fi:
3434 \int_zero:N \l_@@_catcodes_int
3435 \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_special:N ^
3436 {
3437 \bool_set_false:N \l_@@_catcodes_bool
3438 \@@_compile_c_lbrack_loop:NN
3439 }
3440 {
3441 \bool_set_true:N \l_@@_catcodes_bool
3442 \@@_compile_c_lbrack_loop:NN
3443 #1 #2
3444 }
3445 }
3446 \cs_new_protected:Npn \@@_compile_c_lbrack_loop:NN #1#2
3447 {
3448 \token_if_eq_meaning:NNTF #1 \@@_compile_raw:N
3449 {
3450 \int_if_exist:cTF { c_@@_catcode_#2_int }
3451 {
3452 \exp_args:Nc \@@_compile_c_lbrack_add:N
3453 { c_@@_catcode_#2_int }
3454 \@@_compile_c_lbrack_loop:NN
3455 }
3456 }
3457 {
3458 \token_if_eq_charcode:NNTF #2 ]
3459 { \@@_compile_c_lbrack_end: }
3460 }
3461 {
3462 \msg_error:nne { regex } { c-missing-rbrack } {#2}
3463 \@@_compile_c_lbrack_end:
3464 #1 #2
3465 }
3466 }
3467 \cs_new_protected:Npn \@@_compile_c_lbrack_add:N #1
3468 {
3469 \if_int_odd:w \@@_int_eval:w \l_@@_catcodes_int / #1 \scan_stop:
3470 \else:
3471 \int_add:Nn \l_@@_catcodes_int {#1}
3472 \fi:
3473 }
3474 \cs_new_protected:Npn \@@_compile_c_lbrack_end:
3475 {
3476 \if_meaning:w \c_false_bool \l_@@_catcodes_bool
3477 \int_set:Nn \l_@@_catcodes_int
3478 { \c_@@_all_catcodes_int - \l_@@_catcodes_int }
3479 \fi:
3480 }
3481 % \end{macrocode}
3482 % \end{macro}
3483 %
3484 % \begin{macro}+\@@_compile_c_{:+
3485 % The case of a left brace is easy, based on what we have done so far:
3486 % in a group, compile the regular expression, after changing the mode
3487 % to forbid nesting |\c|. Additionally, disable submatch tracking
3488 % since groups don't escape the scope of |\c{...}|.
3489 % \begin{macrocode}
3490 \cs_new_protected:cpn { @@_compile_c_ \c_left_brace_str :w }
3491 {
3492 \@@_compile:w
3493 \@@_disable_submatches:
3494 \l_@@_mode_int
3495 = \if_case:w \l_@@_mode_int
3496 \c_@@_cs_mode_int
3497 \else:
3498 \c_@@_cs_in_class_mode_int
3499 \fi:
3500 }
3501 % \end{macrocode}
3502 % \end{macro}
3503 %
3504 % \begin{macro}+\@@_compile_{:+
3505 % We forbid unescaped left braces inside a |\c{...}| escape because
3506 % they otherwise lead to the confusing question of whether the first
3507 % right brace in |\c{{}x}| should end |\c| or whether one should
3508 % match braces.
3509 % \begin{macrocode}
3510 \cs_new_protected:cpn { @@_compile_ \c_left_brace_str : }
3511 {
3512 \@@_if_in_cs:TF
3513 { \msg_error:nnn { regex } { cu-lbrace } { c } }
3514 { \exp_after:wN \@@_compile_raw:N \c_left_brace_str }
3515 }
3516 % \end{macrocode}
3517 % \end{macro}
3518 %
3519 % \begin{variable}{\l_@@_cs_flag}
3520 % \begin{macro}+\@@_compile_}:+
3521 % \begin{macro}{\@@_compile_end_cs:}
3522 % \begin{macro}[EXP]{\@@_compile_cs_aux:Nn, \@@_compile_cs_aux:NNnnnN}
3523 % Non-escaped right braces are only special if they appear when
3524 % compiling the regular expression for a csname, but not within a
3525 % class: |\c{[{}]}| matches the control sequences |\{| and |\}|. So,
3526 % end compiling the inner regex (this closes any dangling class or
3527 % group). Then insert the corresponding test in the outer regex. As
3528 % an optimization, if the control sequence test simply consists of
3529 % several explicit possibilities (branches) then use
3530 % \cs{@@_item_exact_cs:n} with an argument consisting of all
3531 % possibilities separated by \cs{scan_stop:}.
3532 % \begin{macrocode}
3533 \flag_new:N \l_@@_cs_flag
3534 \cs_new_protected:cpn { @@_compile_ \c_right_brace_str : }
3535 {
3536 \@@_if_in_cs:TF
3537 { \@@_compile_end_cs: }
3538 { \exp_after:wN \@@_compile_raw:N \c_right_brace_str }
3539 }
3540 \cs_new_protected:Npn \@@_compile_end_cs:
3541 {
3542 \@@_compile_end:
3543 \flag_clear:N \l_@@_cs_flag
3544 \__kernel_tl_set:Nx \l_@@_tmpa_tl
3545 {
3546 \exp_after:wN \@@_compile_cs_aux:Nn \l_@@_tmp_regex
3547 \q_@@_nil \q_@@_nil \q_@@_recursion_stop
3548 }
3549 \exp_args:Ne \@@_compile_one:n
3550 {
3551 \flag_if_raised:NTF \l_@@_cs_flag
3552 { \@@_item_cs:n { \exp_not:o \l_@@_tmp_regex } }
3553 {
3554 \@@_item_exact_cs:n
3555 { \tl_tail:N \l_@@_tmpa_tl }
3556 }
3557 }
3558 }
3559 \cs_new:Npn \@@_compile_cs_aux:Nn #1#2
3560 {
3561 \cs_if_eq:NNTF #1 \@@_branch:n
3562 {
3563 \scan_stop:
3564 \@@_compile_cs_aux:NNnnnN #2
3565 \q_@@_nil \q_@@_nil \q_@@_nil
3566 \q_@@_nil \q_@@_nil \q_@@_nil \q_@@_recursion_stop
3567 \@@_compile_cs_aux:Nn
3568 }
3569 {
3570 \@@_quark_if_nil:NF #1 { \flag_ensure_raised:N \l_@@_cs_flag }
3571 \@@_use_none_delimit_by_q_recursion_stop:w
3572 }
3573 }
3574 \cs_new:Npn \@@_compile_cs_aux:NNnnnN #1#2#3#4#5#6
3575 {
3576 \bool_lazy_all:nTF
3577 {
3578 { \cs_if_eq_p:NN #1 \@@_class:NnnnN }
3579 {#2}
3580 { \tl_if_head_eq_meaning_p:nN {#3} \@@_item_caseful_equal:n }
3581 { \int_compare_p:nNn { \tl_count:n {#3} } = { 2 } }
3582 { \int_compare_p:nNn {#5} = \c_zero_int }
3583 }
3584 {
3585 \prg_replicate:nn {#4}
3586 { \char_generate:nn { \use_ii:nn #3 } {12} }
3587 \@@_compile_cs_aux:NNnnnN
3588 }
3589 {
3590 \@@_quark_if_nil:NF #1
3591 {
3592 \flag_ensure_raised:N \l_@@_cs_flag
3593 \@@_use_i_delimit_by_q_recursion_stop:nw
3594 }
3595 \@@_use_none_delimit_by_q_recursion_stop:w
3596 }
3597 }
3598 % \end{macrocode}
3599 % \end{macro}
3600 % \end{macro}
3601 % \end{macro}
3602 % \end{variable}
3603 %
3604 % \subsubsection{Raw token lists with \cs[no-index]{u}}
3605 %
3606 % \begin{macro}{\@@_compile_/u:}
3607 % The |\u| escape is invalid in classes and directly following a
3608 % catcode test. Otherwise test for a following |r| (for |\ur|), and
3609 % call an auxiliary responsible for finding the variable name.
3610 % \begin{macrocode}
3611 \cs_new_protected:cpn { @@_compile_/u: } #1#2
3612 {
3613 \@@_if_in_class_or_catcode:TF
3614 { \@@_compile_raw_error:N u #1 #2 }
3615 {
3616 \@@_two_if_eq:NNNNTF #1 #2 \@@_compile_raw:N r
3617 { \@@_compile_u_brace:NNN \@@_compile_ur_end: }
3618 { \@@_compile_u_brace:NNN \@@_compile_u_end: #1 #2 }
3619 }
3620 }
3621 % \end{macrocode}
3622 % \end{macro}
3623 %
3624 % \begin{macro}{\@@_compile_u_brace:NNN}
3625 % This enforces the presence of a left brace, then starts a loop to
3626 % find the variable name.
3627 % \begin{macrocode}
3628 \cs_new:Npn \@@_compile_u_brace:NNN #1#2#3
3629 {
3630 \@@_two_if_eq:NNNNTF #2 #3 \@@_compile_special:N \c_left_brace_str
3631 {
3632 \tl_set:Nn \l_@@_tmpb_tl {#1}
3633 \__kernel_tl_set:Nx \l_@@_tmpa_tl { \if_false: } \fi:
3634 \@@_compile_u_loop:NN
3635 }
3636 {
3637 \msg_error:nn { regex } { u-missing-lbrace }
3638 \token_if_eq_meaning:NNTF #1 \@@_compile_ur_end:
3639 { \@@_compile_raw:N u \@@_compile_raw:N r }
3640 { \@@_compile_raw:N u }
3641 #2 #3
3642 }
3643 }
3644 % \end{macrocode}
3645 % \end{macro}
3646 %
3647 % \begin{macro}[EXP]{\@@_compile_u_loop:NN}
3648 % We collect the characters for the argument of |\u| within an
3649 % \texttt{e}-expanding assignment. In principle we could just wait to
3650 % encounter a right brace, but this is unsafe: if the right brace was
3651 % missing, then we would reach the end-markers of the regex, and
3652 % continue, leading to obscure fatal errors. Instead, we only allow
3653 % raw and special characters, and stop when encountering a special
3654 % right brace, any escaped character, or the end-marker.
3655 % \begin{macrocode}
3656 \cs_new:Npn \@@_compile_u_loop:NN #1#2
3657 {
3658 \token_if_eq_meaning:NNTF #1 \@@_compile_raw:N
3659 { #2 \@@_compile_u_loop:NN }
3660 {
3661 \token_if_eq_meaning:NNTF #1 \@@_compile_special:N
3662 {
3663 \exp_after:wN \token_if_eq_charcode:NNTF \c_right_brace_str #2
3664 { \if_false: { \fi: } \l_@@_tmpb_tl }
3665 {
3666 \if_charcode:w \c_left_brace_str #2
3667 \msg_expandable_error:nnn { regex } { cu-lbrace } { u }
3668 \else:
3669 #2
3670 \fi:
3671 \@@_compile_u_loop:NN
3672 }
3673 }
3674 {
3675 \if_false: { \fi: }
3676 \msg_error:nne { regex } { u-missing-rbrace } {#2}
3677 \l_@@_tmpb_tl
3678 #1 #2
3679 }
3680 }
3681 }
3682 % \end{macrocode}
3683 % \end{macro}
3684 %
3685 % \begin{macro}{\@@_compile_ur_end:, \@@_compile_ur:n}
3686 % \begin{macro}[EXP]{\@@_compile_ur_aux:w}
3687 % For the |\ur{...}| construction, once we have extracted the
3688 % variable's name, we replace all groups by non-capturing groups in
3689 % the compiled regex (passed as the
3690 % argument of \cs{@@_compile_ur:n}). If that has a single branch
3691 % (namely \cs{tl_if_empty:oTF} is false) and there is no quantifier,
3692 % then simply insert the contents of this branch (obtained by
3693 % \cs{use_ii:nn}, which is expanded later). In all other cases,
3694 % insert a non-capturing group and look for quantifiers to determine
3695 % the number of repetition etc.
3696 % \begin{macrocode}
3697 \cs_new_protected:Npn \@@_compile_ur_end:
3698 {
3699 \group_begin:
3700 \cs_set:Npn \@@_group:nnnN { \@@_group_no_capture:nnnN }
3701 \cs_set:Npn \@@_group_resetting:nnnN { \@@_group_no_capture:nnnN }
3702 \exp_args:NNe
3703 \group_end:
3704 \@@_compile_ur:n { \use:c { \l_@@_tmpa_tl } }
3705 }
3706 \cs_new_protected:Npn \@@_compile_ur:n #1
3707 {
3708 \tl_if_empty:oTF { \@@_compile_ur_aux:w #1 {} ? ? \q_@@_nil }
3709 { \@@_compile_if_quantifier:TFw }
3710 { \use_i:nn }
3711 {
3712 \tl_build_put_right:Nn \l_@@_build_tl
3713 { \@@_group_no_capture:nnnN { \if_false: } \fi: #1 }
3714 \@@_compile_quantifier:w
3715 }
3716 { \tl_build_put_right:Nn \l_@@_build_tl { \use_ii:nn #1 } }
3717 }
3718 \cs_new:Npn \@@_compile_ur_aux:w \@@_branch:n #1#2#3 \q_@@_nil {#2}
3719 % \end{macrocode}
3720 % \end{macro}
3721 % \end{macro}
3722 %
3723 % \begin{macro}{\@@_compile_u_end:, \@@_compile_u_payload:}
3724 % Once we have extracted the variable's name, we check for
3725 % quantifiers, in which case we set up a non-capturing group with a
3726 % single branch. Inside this branch (we omit it and the group if
3727 % there is no quantifier), \cs{@@_compile_u_payload:} puts
3728 % the right tests corresponding to the contents of the variable, which
3729 % we store in \cs{l_@@_tmpa_tl}. The behavior of |\u|
3730 % then depends on whether we are within a |\c{...}| escape (in this
3731 % case, the variable is turned to a string), or not.
3732 % \begin{macrocode}
3733 \cs_new_protected:Npn \@@_compile_u_end:
3734 {
3735 \@@_compile_if_quantifier:TFw
3736 {
3737 \tl_build_put_right:Nn \l_@@_build_tl
3738 {
3739 \@@_group_no_capture:nnnN { \if_false: } \fi:
3740 \@@_branch:n { \if_false: } \fi:
3741 }
3742 \@@_compile_u_payload:
3743 \tl_build_put_right:Nn \l_@@_build_tl { \if_false: { \fi: } }
3744 \@@_compile_quantifier:w
3745 }
3746 { \@@_compile_u_payload: }
3747 }
3748 \cs_new_protected:Npn \@@_compile_u_payload:
3749 {
3750 \tl_set:Nv \l_@@_tmpa_tl { \l_@@_tmpa_tl }
3751 \if_int_compare:w \l_@@_mode_int = \c_@@_outer_mode_int
3752 \@@_compile_u_not_cs:
3753 \else:
3754 \@@_compile_u_in_cs:
3755 \fi:
3756 }
3757 % \end{macrocode}
3758 % \end{macro}
3759 %
3760 % \begin{macro}{\@@_compile_u_in_cs:}
3761 % When |\u| appears within a control sequence, we convert the variable
3762 % to a string with escaped spaces. Then for each character insert a
3763 % class matching exactly that character, once.
3764 % \begin{macrocode}
3765 \cs_new_protected:Npn \@@_compile_u_in_cs:
3766 {
3767 \__kernel_tl_gset:Nx \g_@@_tmp_tl
3768 {
3769 \exp_args:No \__kernel_str_to_other_fast:n
3770 { \l_@@_tmpa_tl }
3771 }
3772 \tl_build_put_right:Ne \l_@@_build_tl
3773 {
3774 \tl_map_function:NN \g_@@_tmp_tl
3775 \@@_compile_u_in_cs_aux:n
3776 }
3777 }
3778 \cs_new:Npn \@@_compile_u_in_cs_aux:n #1
3779 {
3780 \@@_class:NnnnN \c_true_bool
3781 { \@@_item_caseful_equal:n { \int_value:w `#1 } }
3782 { 1 } { 0 } \c_false_bool
3783 }
3784 % \end{macrocode}
3785 % \end{macro}
3786 %
3787 % \begin{macro}{\@@_compile_u_not_cs:}
3788 % In mode $0$, the |\u| escape adds one state to the NFA for each
3789 % token in \cs{l_@@_tmpa_tl}. If a given \meta{token} is a
3790 % control sequence, then insert a string comparison test, otherwise,
3791 % \cs{@@_item_exact:nn} which compares catcode and character code.
3792 % \begin{macrocode}
3793 \cs_new_protected:Npn \@@_compile_u_not_cs:
3794 {
3795 \tl_analysis_map_inline:Nn \l_@@_tmpa_tl
3796 {
3797 \tl_build_put_right:Ne \l_@@_build_tl
3798 {
3799 \@@_class:NnnnN \c_true_bool
3800 {
3801 \if_int_compare:w "##3 = \c_zero_int
3802 \@@_item_exact_cs:n
3803 { \exp_after:wN \cs_to_str:N ##1 }
3804 \else:
3805 \@@_item_exact:nn { \int_value:w "##3 } { ##2 }
3806 \fi:
3807 }
3808 { 1 } { 0 } \c_false_bool
3809 }
3810 }
3811 }
3812 % \end{macrocode}
3813 % \end{macro}
3814 %
3815 % \subsubsection{Other}
3816 %
3817 % \begin{macro}{\@@_compile_/K:}
3818 % The |\K| control sequence is currently the only \enquote{command},
3819 % which performs some action, rather than matching something. It is
3820 % allowed in the same contexts as |\b|. At the compilation stage, we
3821 % leave it as a single control sequence, defined later.
3822 % \begin{macrocode}
3823 \cs_new_protected:cpn { @@_compile_/K: }
3824 {
3825 \int_compare:nNnTF \l_@@_mode_int = \c_@@_outer_mode_int
3826 { \tl_build_put_right:Nn \l_@@_build_tl { \@@_command_K: } }
3827 { \@@_compile_raw_error:N K }
3828 }
3829 % \end{macrocode}
3830 % \end{macro}
3831 %
3832 % \subsubsection{Showing regexes}
3833 %
3834 % \begin{macro}[rEXP]
3835 % {
3836 % \@@_clean_bool:n, \@@_clean_int:n, \@@_clean_int_aux:N,
3837 % \@@_clean_regex:n, \@@_clean_regex_loop:w, \@@_clean_branch:n,
3838 % \@@_clean_branch_loop:n, \@@_clean_assertion:Nn,
3839 % \@@_clean_class:NnnnN, \@@_clean_group:nnnN, \@@_clean_class:n,
3840 % \@@_clean_class_loop:nnn, \@@_clean_exact_cs:n,
3841 % \@@_clean_exact_cs:w
3842 % }
3843 % Before showing a regex we check that it is \enquote{clean} in the
3844 % sense that it has the correct internal structure. We do this (in
3845 % the implementation of \cs{regex_show:N} and \cs{regex_log:N}) by
3846 % comparing it with a cleaned-up version of the same regex. Along the
3847 % way we also need similar functions for other types: all
3848 % \cs[no-index]{@@_clean_\meta{type}:n} functions produce valid
3849 % \meta{type} tokens (bool, explicit integer, etc.\@) from arbitrary
3850 % input, and the output coincides with the input if that was valid.
3851 % \begin{macrocode}
3852 \cs_new:Npn \@@_clean_bool:n #1
3853 {
3854 \tl_if_single:nTF {#1}
3855 { \bool_if:NTF #1 \c_true_bool \c_false_bool }
3856 { \c_true_bool }
3857 }
3858 \cs_new:Npn \@@_clean_int:n #1
3859 {
3860 \tl_if_head_eq_meaning:nNTF {#1} -
3861 { - \exp_args:No \@@_clean_int:n { \use_none:n #1 } }
3862 { \int_eval:n { 0 \str_map_function:nN {#1} \@@_clean_int_aux:N } }
3863 }
3864 \cs_new:Npn \@@_clean_int_aux:N #1
3865 {
3866 \if_int_compare:w \c_one_int < 1 #1 ~
3867 #1
3868 \else:
3869 \str_map_break:n
3870 \fi:
3871 }
3872 \cs_new:Npn \@@_clean_regex:n #1
3873 {
3874 \@@_clean_regex_loop:w #1
3875 \@@_branch:n { \q_recursion_tail } \q_recursion_stop
3876 }
3877 \cs_new:Npn \@@_clean_regex_loop:w #1 \@@_branch:n #2
3878 {
3879 \quark_if_recursion_tail_stop:n {#2}
3880 \@@_branch:n { \@@_clean_branch:n {#2} }
3881 \@@_clean_regex_loop:w
3882 }
3883 \cs_new:Npn \@@_clean_branch:n #1
3884 {
3885 \@@_clean_branch_loop:n #1
3886 ? ? ? ? ? ? \prg_break_point:
3887 }
3888 \cs_new:Npn \@@_clean_branch_loop:n #1
3889 {
3890 \tl_if_single:nF {#1} \prg_break:
3891 \token_case_meaning:NnF #1
3892 {
3893 \@@_command_K: { #1 \@@_clean_branch_loop:n }
3894 \@@_assertion:Nn { #1 \@@_clean_assertion:Nn }
3895 \@@_class:NnnnN { #1 \@@_clean_class:NnnnN }
3896 \@@_group:nnnN { #1 \@@_clean_group:nnnN }
3897 \@@_group_no_capture:nnnN { #1 \@@_clean_group:nnnN }
3898 \@@_group_resetting:nnnN { #1 \@@_clean_group:nnnN }
3899 }
3900 \prg_break:
3901 }
3902 \cs_new:Npn \@@_clean_assertion:Nn #1#2
3903 {
3904 \@@_clean_bool:n {#1}
3905 \tl_if_single:nF {#2} { { \@@_A_test: } \prg_break: }
3906 \token_case_meaning:NnTF #2
3907 {
3908 \@@_A_test: { }
3909 \@@_G_test: { }
3910 \@@_Z_test: { }
3911 \@@_b_test: { }
3912 }
3913 { {#2} }
3914 { { \@@_A_test: } \prg_break: }
3915 \@@_clean_branch_loop:n
3916 }
3917 \cs_new:Npn \@@_clean_class:NnnnN #1#2#3#4#5
3918 {
3919 \@@_clean_bool:n {#1}
3920 { \@@_clean_class:n {#2} }
3921 { \int_max:nn \c_zero_int { \@@_clean_int:n {#3} } }
3922 { \int_max:nn { -\c_one_int } { \@@_clean_int:n {#4} } }
3923 \@@_clean_bool:n {#5}
3924 \@@_clean_branch_loop:n
3925 }
3926 \cs_new:Npn \@@_clean_group:nnnN #1#2#3#4
3927 {
3928 { \@@_clean_regex:n {#1} }
3929 { \int_max:nn \c_zero_int { \@@_clean_int:n {#2} } }
3930 { \int_max:nn { -\c_one_int } { \@@_clean_int:n {#3} } }
3931 \@@_clean_bool:n {#4}
3932 \@@_clean_branch_loop:n
3933 }
3934 \cs_new:Npn \@@_clean_class:n #1
3935 { \@@_clean_class_loop:nnn #1 ????? \prg_break_point: }
3936 % \end{macrocode}
3937 % When cleaning a class there are many cases, including a dozen or so
3938 % like \cs{@@_prop_d:} or \cs{@@_posix_alpha:}. To avoid listing all of
3939 % them we allow any command that starts with the
3940 % $13$ characters |__regex_prop_| or |__regex_posix| (handily these have
3941 % the same length, except for the trailing underscore).
3942 % \begin{macrocode}
3943 \cs_new:Npn \@@_clean_class_loop:nnn #1#2#3
3944 {
3945 \tl_if_single:nF {#1} \prg_break:
3946 \token_case_meaning:NnTF #1
3947 {
3948 \@@_item_cs:n { #1 { \@@_clean_regex:n {#2} } }
3949 \@@_item_exact_cs:n { #1 { \@@_clean_exact_cs:n {#2} } }
3950 \@@_item_caseful_equal:n { #1 { \@@_clean_int:n {#2} } }
3951 \@@_item_caseless_equal:n { #1 { \@@_clean_int:n {#2} } }
3952 \@@_item_reverse:n { #1 { \@@_clean_class:n {#2} } }
3953 }
3954 { \@@_clean_class_loop:nnn {#3} }
3955 {
3956 \token_case_meaning:NnTF #1
3957 {
3958 \@@_item_caseful_range:nn { }
3959 \@@_item_caseless_range:nn { }
3960 \@@_item_exact:nn { }
3961 }
3962 {
3963 #1 { \@@_clean_int:n {#2} } { \@@_clean_int:n {#3} }
3964 \@@_clean_class_loop:nnn
3965 }
3966 {
3967 \token_case_meaning:NnTF #1
3968 {
3969 \@@_item_catcode:nT { }
3970 \@@_item_catcode_reverse:nT { }
3971 }
3972 {
3973 #1 { \@@_clean_int:n {#2} } { \@@_clean_class:n {#3} }
3974 \@@_clean_class_loop:nnn
3975 }
3976 {
3977 \exp_args:Ne \str_case:nnTF
3978 {
3979 \exp_args:Ne \str_range:nnn
3980 { \cs_to_str:N #1 } \c_one_int { 13 }
3981 }
3982 {
3983 { @@_prop_ } { }
3984 { @@_posix } { }
3985 }
3986 {
3987 #1
3988 \@@_clean_class_loop:nnn {#2} {#3}
3989 }
3990 \prg_break:
3991 }
3992 }
3993 }
3994 }
3995 \cs_new:Npn \@@_clean_exact_cs:n #1
3996 {
3997 \exp_last_unbraced:Nf \use_none:n
3998 {
3999 \@@_clean_exact_cs:w #1
4000 \scan_stop: \q_recursion_tail \scan_stop:
4001 \q_recursion_stop
4002 }
4003 }
4004 \cs_new:Npn \@@_clean_exact_cs:w #1 \scan_stop:
4005 {
4006 \quark_if_recursion_tail_stop:n {#1}
4007 \scan_stop: \tl_to_str:n {#1}
4008 \@@_clean_exact_cs:w
4009 }
4010 % \end{macrocode}
4011 % \end{macro}
4012 %
4013 % \begin{macro}{\@@_show:N}
4014 % Within a group and within \cs{tl_build_begin:N} \ldots{} \cs{tl_build_end:N} we
4015 % redefine all the function that can appear in a compiled regex, then
4016 % run the regex. The result stored in \cs{l_@@_tmpa_tl} is then
4017 % meant to be shown.
4018 % \begin{macrocode}
4019 \cs_new_protected:Npn \@@_show:N #1
4020 {
4021 \group_begin:
4022 \tl_build_begin:N \l_@@_build_tl
4023 \cs_set_protected:Npn \@@_branch:n
4024 {
4025 \seq_pop_right:NN \l_@@_show_prefix_seq
4026 \l_@@_tmpa_tl
4027 \@@_show_one:n { +-branch }
4028 \seq_put_right:No \l_@@_show_prefix_seq
4029 \l_@@_tmpa_tl
4030 \use:n
4031 }
4032 \cs_set_protected:Npn \@@_group:nnnN
4033 { \@@_show_group_aux:nnnnN { } }
4034 \cs_set_protected:Npn \@@_group_no_capture:nnnN
4035 { \@@_show_group_aux:nnnnN { ~(no~capture) } }
4036 \cs_set_protected:Npn \@@_group_resetting:nnnN
4037 { \@@_show_group_aux:nnnnN { ~(resetting) } }
4038 \cs_set_eq:NN \@@_class:NnnnN \@@_show_class:NnnnN
4039 \cs_set_protected:Npn \@@_command_K:
4040 { \@@_show_one:n { reset~match~start~(\iow_char:N\\K) } }
4041 \cs_set_protected:Npn \@@_assertion:Nn ##1##2
4042 {
4043 \@@_show_one:n
4044 { \bool_if:NF ##1 { negative~ } assertion:~##2 }
4045 }
4046 \cs_set:Npn \@@_b_test: { word~boundary }
4047 \cs_set:Npn \@@_Z_test: { anchor~at~end~(\iow_char:N\\Z) }
4048 \cs_set:Npn \@@_A_test: { anchor~at~start~(\iow_char:N\\A) }
4049 \cs_set:Npn \@@_G_test: { anchor~at~start~of~match~(\iow_char:N\\G) }
4050 \cs_set_protected:Npn \@@_item_caseful_equal:n ##1
4051 { \@@_show_one:n { char~code~\@@_show_char:n{##1} } }
4052 \cs_set_protected:Npn \@@_item_caseful_range:nn ##1##2
4053 {
4054 \@@_show_one:n
4055 { range~[\@@_show_char:n{##1}, \@@_show_char:n{##2}] }
4056 }
4057 \cs_set_protected:Npn \@@_item_caseless_equal:n ##1
4058 { \@@_show_one:n { char~code~\@@_show_char:n{##1}~(caseless) } }
4059 \cs_set_protected:Npn \@@_item_caseless_range:nn ##1##2
4060 {
4061 \@@_show_one:n
4062 { Range~[\@@_show_char:n{##1}, \@@_show_char:n{##2}]~(caseless) }
4063 }
4064 \cs_set_protected:Npn \@@_item_catcode:nT
4065 { \@@_show_item_catcode:NnT \c_true_bool }
4066 \cs_set_protected:Npn \@@_item_catcode_reverse:nT
4067 { \@@_show_item_catcode:NnT \c_false_bool }
4068 \cs_set_protected:Npn \@@_item_reverse:n
4069 { \@@_show_scope:nn { Reversed~match } }
4070 \cs_set_protected:Npn \@@_item_exact:nn ##1##2
4071 { \@@_show_one:n { char~\@@_show_char:n{##2},~catcode~##1 } }
4072 \cs_set_eq:NN \@@_item_exact_cs:n \@@_show_item_exact_cs:n
4073 \cs_set_protected:Npn \@@_item_cs:n
4074 { \@@_show_scope:nn { control~sequence } }
4075 \cs_set:cpn { @@_prop_.: } { \@@_show_one:n { any~token } }
4076 \seq_clear:N \l_@@_show_prefix_seq
4077 \@@_show_push:n { ~ }
4078 \cs_if_exist_use:N #1
4079 \tl_build_end:N \l_@@_build_tl
4080 \exp_args:NNNo
4081 \group_end:
4082 \tl_set:Nn \l_@@_tmpa_tl { \l_@@_build_tl }
4083 }
4084 % \end{macrocode}
4085 % \end{macro}
4086 %
4087 % \begin{macro}[EXP]{\@@_show_char:n}
4088 % Show a single character, together with its ascii representation if available.
4089 % This could be extended to beyond ascii. It is not ideal for parentheses themselves.
4090 % \begin{macrocode}
4091 \cs_new:Npn \@@_show_char:n #1
4092 {
4093 \int_eval:n {#1}
4094 \int_compare:nT { 32 <= #1 <= 126 }
4095 { ~ ( \char_generate:nn {#1} {12} ) }
4096 }
4097 % \end{macrocode}
4098 % \end{macro}
4099 %
4100 % \begin{macro}{\@@_show_one:n}
4101 % Every part of the final message go through this function, which adds
4102 % one line to the output, with the appropriate prefix.
4103 % \begin{macrocode}
4104 \cs_new_protected:Npn \@@_show_one:n #1
4105 {
4106 \int_incr:N \l_@@_show_lines_int
4107 \tl_build_put_right:Ne \l_@@_build_tl
4108 {
4109 \exp_not:N \iow_newline:
4110 \seq_map_function:NN \l_@@_show_prefix_seq \use:n
4111 #1
4112 }
4113 }
4114 % \end{macrocode}
4115 % \end{macro}
4116 %
4117 % \begin{macro}
4118 % {\@@_show_push:n, \@@_show_pop:, \@@_show_scope:nn}
4119 % Enter and exit levels of nesting. The \texttt{scope} function prints
4120 % its first argument as an \enquote{introduction}, then performs its
4121 % second argument in a deeper level of nesting.
4122 % \begin{macrocode}
4123 \cs_new_protected:Npn \@@_show_push:n #1
4124 { \seq_put_right:Ne \l_@@_show_prefix_seq { #1 ~ } }
4125 \cs_new_protected:Npn \@@_show_pop:
4126 { \seq_pop_right:NN \l_@@_show_prefix_seq \l_@@_tmpa_tl }
4127 \cs_new_protected:Npn \@@_show_scope:nn #1#2
4128 {
4129 \@@_show_one:n {#1}
4130 \@@_show_push:n { ~ }
4131 #2
4132 \@@_show_pop:
4133 }
4134 % \end{macrocode}
4135 % \end{macro}
4136 %
4137 % \begin{macro}{\@@_show_group_aux:nnnnN}
4138 % We display all groups in the same way, simply adding a message,
4139 % \texttt{(no capture)} or \texttt{(resetting)}, to special groups.
4140 % The odd \cs{use_ii:nn} avoids printing a spurious \texttt{+-branch}
4141 % for the first branch.
4142 % \begin{macrocode}
4143 \cs_new_protected:Npn \@@_show_group_aux:nnnnN #1#2#3#4#5
4144 {
4145 \@@_show_one:n { ,-group~begin #1 }
4146 \@@_show_push:n { | }
4147 \use_ii:nn #2
4148 \@@_show_pop:
4149 \@@_show_one:n
4150 { `-group~end \@@_msg_repeated:nnN {#3} {#4} #5 }
4151 }
4152 % \end{macrocode}
4153 % \end{macro}
4154 %
4155 % \begin{macro}{\@@_show_class:NnnnN}
4156 % I'm entirely unhappy about this function: I couldn't find a way to
4157 % test if a class is a single test. Instead, collect the
4158 % representation of the tests in the class. If that had more than one
4159 % line, write \texttt{Match} or \texttt{Don't match} on its own line,
4160 % with the repeating information if any. Then the various tests on
4161 % lines of their own, and finally a line. Otherwise, we need to
4162 % evaluate the representation of the tests again (since the prefix is
4163 % incorrect). That's clunky, but not too expensive, since it's only
4164 % one test.
4165 % \begin{macrocode}
4166 \cs_new:Npn \@@_show_class:NnnnN #1#2#3#4#5
4167 {
4168 \group_begin:
4169 \tl_build_begin:N \l_@@_build_tl
4170 \int_zero:N \l_@@_show_lines_int
4171 \@@_show_push:n {~}
4172 #2
4173 \int_compare:nTF { \l_@@_show_lines_int = \c_zero_int }
4174 {
4175 \group_end:
4176 \@@_show_one:n { \bool_if:NTF #1 { Fail } { Pass } }
4177 }
4178 {
4179 \bool_if:nTF
4180 { #1 && \int_compare_p:n { \l_@@_show_lines_int = \c_one_int } }
4181 {
4182 \group_end:
4183 #2
4184 \tl_build_put_right:Nn \l_@@_build_tl
4185 { \@@_msg_repeated:nnN {#3} {#4} #5 }
4186 }
4187 {
4188 \tl_build_end:N \l_@@_build_tl
4189 \exp_args:NNNo
4190 \group_end:
4191 \tl_set:Nn \l_@@_tmpa_tl \l_@@_build_tl
4192 \@@_show_one:n
4193 {
4194 \bool_if:NTF #1 { Match } { Don't~match }
4195 \@@_msg_repeated:nnN {#3} {#4} #5
4196 }
4197 \tl_build_put_right:Ne \l_@@_build_tl
4198 { \exp_not:o \l_@@_tmpa_tl }
4199 }
4200 }
4201 }
4202 % \end{macrocode}
4203 % \end{macro}
4204 %
4205 % \begin{macro}{\@@_show_item_catcode:NnT}
4206 % Produce a sequence of categories which the catcode bitmap |#2|
4207 % contains, and show it, indenting the tests on which this catcode
4208 % constraint applies.
4209 % \begin{macrocode}
4210 \cs_new_protected:Npn \@@_show_item_catcode:NnT #1#2
4211 {
4212 \seq_set_split:Nnn \l_@@_tmp_seq { } { CBEMTPUDSLOA }
4213 \seq_set_filter:NNn \l_@@_tmp_seq \l_@@_tmp_seq
4214 { \int_if_odd_p:n { #2 / \int_use:c { c_@@_catcode_##1_int } } }
4215 \@@_show_scope:nn
4216 {
4217 categories~
4218 \seq_map_function:NN \l_@@_tmp_seq \use:n
4219 , ~
4220 \bool_if:NF #1 { negative~ } class
4221 }
4222 }
4223 % \end{macrocode}
4224 % \end{macro}
4225 %
4226 % \begin{macro}{\@@_show_item_exact_cs:n}
4227 % \begin{macrocode}
4228 \cs_new_protected:Npn \@@_show_item_exact_cs:n #1
4229 {
4230 \seq_set_split:Nnn \l_@@_tmp_seq { \scan_stop: } {#1}
4231 \seq_set_map_e:NNn \l_@@_tmp_seq
4232 \l_@@_tmp_seq { \iow_char:N\\##1 }
4233 \@@_show_one:n
4234 { control~sequence~ \seq_use:Nn \l_@@_tmp_seq { ~or~ } }
4235 }
4236 % \end{macrocode}
4237 % \end{macro}
4238 %
4239 % \subsection{Building}
4240 %
4241 % \subsubsection{Variables used while building}
4242 %
4243 % \begin{variable}{\l_@@_min_state_int, \l_@@_max_state_int}
4244 % The last state that was allocated is
4245 % $\cs{l_@@_max_state_int}-1$, so that \cs{l_@@_max_state_int} always
4246 % points to a free state. The \texttt{min_state} variable is
4247 % $1$ to begin with, but gets shifted in nested calls to the matching
4248 % code, namely in |\c{...}| constructions.
4249 % \begin{macrocode}
4250 \int_new:N \l_@@_min_state_int
4251 \int_set:Nn \l_@@_min_state_int { 1 }
4252 \int_new:N \l_@@_max_state_int
4253 % \end{macrocode}
4254 % \end{variable}
4255 %
4256 % \begin{variable}{\l_@@_left_state_int, \l_@@_right_state_int}
4257 % \begin{variable}{\l_@@_left_state_seq, \l_@@_right_state_seq}
4258 % Alternatives are implemented by branching from a \texttt{left} state
4259 % into the various choices, then merging those into a \texttt{right}
4260 % state. We store information about those states in two sequences.
4261 % Those states are also used to implement group quantifiers. Most
4262 % often, the left and right pointers only differ by~$1$.
4263 % \begin{macrocode}
4264 \int_new:N \l_@@_left_state_int
4265 \int_new:N \l_@@_right_state_int
4266 \seq_new:N \l_@@_left_state_seq
4267 \seq_new:N \l_@@_right_state_seq
4268 % \end{macrocode}
4269 % \end{variable}
4270 % \end{variable}
4271 %
4272 % \begin{variable}{\l_@@_capturing_group_int}
4273 % \cs{l_@@_capturing_group_int} is the next \textsc{id} number to
4274 % be assigned to a capturing group. This starts
4275 % at $0$ for the group enclosing the full regular expression, and
4276 % groups are counted in the order of their left parenthesis, except
4277 % when encountering \texttt{resetting} groups.
4278 % \begin{macrocode}
4279 \int_new:N \l_@@_capturing_group_int
4280 % \end{macrocode}
4281 % \end{variable}
4282 %
4283 % \subsubsection{Framework}
4284 %
4285 % This phase is about going from a compiled regex to an \textsc{nfa}.
4286 % Each state of the \textsc{nfa} is stored in a \tn{toks}. The
4287 % operations which can appear in the \tn{toks} are
4288 % \begin{itemize}
4289 % \item \cs{@@_action_start_wildcard:N} \meta{boolean} inserted at the
4290 % start of the regular expression, where a \texttt{true}
4291 % \meta{boolean} makes it unanchored.
4292 % \item \cs{@@_action_success:} marks the exit state of the
4293 % \textsc{nfa}.
4294 % \item \cs{@@_action_cost:n} \Arg{shift} is a transition from the
4295 % current \meta{state} to $\meta{state}+\meta{shift}$, which
4296 % consumes the current character: the target state is saved and will
4297 % be considered again when matching at the next position.
4298 % \item \cs{@@_action_free:n} \Arg{shift}, and
4299 % \cs{@@_action_free_group:n} \Arg{shift} are free transitions,
4300 % which immediately perform the actions for the state
4301 % $\meta{state}+\meta{shift}$ of the \textsc{nfa}. They differ in
4302 % how they detect and avoid infinite loops. For now, we just need to
4303 % know that the \texttt{group} variant must be used for transitions
4304 % back to the start of a group.
4305 % \item \cs{@@_action_submatch:nN} \Arg{group} \meta{key} where the
4306 % \meta{key} is |<| or |>| for the beginning or end of group
4307 % numbered \meta{group}. This causes the current position in the
4308 % query to be stored as the \meta{key} submatch boundary.
4309 % \item One of these actions, within a conditional.
4310 % \end{itemize}
4311 %
4312 % We strive to preserve the following properties while building.
4313 % \begin{itemize}
4314 % \item The current capturing group is
4315 % $\text{\texttt{capturing_group}}-1$, and if a group opened now
4316 % it would be labeled \texttt{capturing_group}.
4317 % \item The last allocated state is $\text{\texttt{max_state}}-1$, so
4318 % \texttt{max_state} is a free state.
4319 % \item The \texttt{left_state} points to a state to the left of the
4320 % current group or of the last class.
4321 % \item The \texttt{right_state} points to a newly created,
4322 % empty state, with some transitions leading to it.
4323 % \item The \texttt{left/right} sequences hold a list of the
4324 % corresponding end-points of nested groups.
4325 % \end{itemize}
4326 %
4327 % \begin{macro}{\@@_build:n, \@@_build_aux:Nn, \@@_build:N, \@@_build_aux:NN}
4328 % The \texttt{n}-type function first compiles its argument. Reset some
4329 % variables. Allocate two states, and put a wildcard in state $0$
4330 % (transitions to state $1$ and $0$ state). Then build the regex
4331 % within a (capturing) group numbered $0$ (current
4332 % value of \texttt{capturing_group}). Finally, if the match reaches the
4333 % last state, it is successful. A \texttt{false} boolean for argument
4334 % |#1| for the auxiliaries will suppress the wildcard and make the
4335 % match anchored: used for \cs{peek_regex:nTF} and similar.
4336 % \begin{macrocode}
4337 \cs_new_protected:Npn \@@_build:n
4338 { \@@_build_aux:Nn \c_true_bool }
4339 \cs_new_protected:Npn \@@_build:N
4340 { \@@_build_aux:NN \c_true_bool }
4341 \cs_new_protected:Npn \@@_build_aux:Nn #1#2
4342 {
4343 \@@_compile:n {#2}
4344 \@@_build_aux:NN #1 \l_@@_tmp_regex
4345 }
4346 \cs_new_protected:Npn \@@_build_aux:NN #1#2
4347 {
4348 \@@_standard_escapechar:
4349 \int_zero:N \l_@@_capturing_group_int
4350 \int_set_eq:NN \l_@@_max_state_int \l_@@_min_state_int
4351 \@@_build_new_state:
4352 \@@_build_new_state:
4353 \@@_toks_put_right:Nn \l_@@_left_state_int
4354 { \@@_action_start_wildcard:N #1 }
4355 \@@_group:nnnN {#2} { 1 } { 0 } \c_false_bool
4356 \@@_toks_put_right:Nn \l_@@_right_state_int
4357 { \@@_action_success: }
4358 }
4359 % \end{macrocode}
4360 % \end{macro}
4361 %
4362 % \begin{variable}{\g_@@_case_int}
4363 % Case number that was successfully matched in
4364 % \cs{regex_match_case:nn} and related functions.
4365 % \begin{macrocode}
4366 \int_new:N \g_@@_case_int
4367 % \end{macrocode}
4368 % \end{variable}
4369 %
4370 % \begin{variable}{\l_@@_case_max_group_int}
4371 % The largest group number appearing in any of the \meta{regex} in the
4372 % argument of \cs{regex_match_case:nn} and related functions.
4373 % \begin{macrocode}
4374 \int_new:N \l_@@_case_max_group_int
4375 % \end{macrocode}
4376 % \end{variable}
4377 %
4378 % \begin{macro}{\@@_case_build:n, \@@_case_build:e, \@@_case_build_aux:Nn, \@@_case_build_loop:n}
4379 % See \cs{@@_build:n}, but with a loop.
4380 % \begin{macrocode}
4381 \cs_new_protected:Npn \@@_case_build:n #1
4382 {
4383 \@@_case_build_aux:Nn \c_true_bool {#1}
4384 \int_gzero:N \g_@@_case_int
4385 }
4386 \cs_generate_variant:Nn \@@_case_build:n { e }
4387 \cs_new_protected:Npn \@@_case_build_aux:Nn #1#2
4388 {
4389 \@@_standard_escapechar:
4390 \int_set_eq:NN \l_@@_max_state_int \l_@@_min_state_int
4391 \@@_build_new_state:
4392 \@@_build_new_state:
4393 \@@_toks_put_right:Nn \l_@@_left_state_int
4394 { \@@_action_start_wildcard:N #1 }
4395 %
4396 \@@_build_new_state:
4397 \@@_toks_put_left:Ne \l_@@_left_state_int
4398 { \@@_action_submatch:nN \c_zero_int < }
4399 \@@_push_lr_states:
4400 \int_zero:N \l_@@_case_max_group_int
4401 \int_gzero:N \g_@@_case_int
4402 \tl_map_inline:nn {#2}
4403 {
4404 \int_gincr:N \g_@@_case_int
4405 \@@_case_build_loop:n {##1}
4406 }
4407 \int_set_eq:NN \l_@@_capturing_group_int \l_@@_case_max_group_int
4408 \@@_pop_lr_states:
4409 }
4410 \cs_new_protected:Npn \@@_case_build_loop:n #1
4411 {
4412 \int_set_eq:NN \l_@@_capturing_group_int \c_one_int
4413 \@@_compile_use:n {#1}
4414 \int_set:Nn \l_@@_case_max_group_int
4415 { \int_max:nn \l_@@_case_max_group_int \l_@@_capturing_group_int }
4416 \seq_pop:NN \l_@@_right_state_seq \l_@@_tmpa_tl
4417 \int_set:Nn \l_@@_right_state_int \l_@@_tmpa_tl
4418 \@@_toks_put_left:Ne \l_@@_right_state_int
4419 {
4420 \@@_action_submatch:nN \c_zero_int >
4421 \int_gset:Nn \g_@@_case_int
4422 { \int_use:N \g_@@_case_int }
4423 \@@_action_success:
4424 }
4425 \@@_toks_clear:N \l_@@_max_state_int
4426 \seq_push:No \l_@@_right_state_seq
4427 { \int_use:N \l_@@_max_state_int }
4428 \int_incr:N \l_@@_max_state_int
4429 }
4430 % \end{macrocode}
4431 % \end{macro}
4432 %
4433 % \begin{macro}{\@@_build_for_cs:n}
4434 % The matching code relies on some global intarray variables, but only
4435 % uses a range of their entries. Specifically,
4436 % \begin{itemize}
4437 % \item \cs{g_@@_state_active_intarray} from \cs{l_@@_min_state_int}
4438 % to $\cs{l_@@_max_state_int}-1$;
4439 % \end{itemize}
4440 % Here, in this nested call to the
4441 % matching code, we need the new versions of this range to involve
4442 % completely new entries of the intarray variables, so we begin by
4443 % setting (the new) \cs{l_@@_min_state_int} to (the old)
4444 % \cs{l_@@_max_state_int} to use higher entries.
4445 %
4446 % When using a regex to match a cs, we don't insert a wildcard, we
4447 % anchor at the end, and since we ignore submatches, there is no need
4448 % to surround the expression with a group. However, for branches to
4449 % work properly at the outer level, we need to put the appropriate
4450 % \texttt{left} and \texttt{right} states in their sequence.
4451 % \begin{macrocode}
4452 \cs_new_protected:Npn \@@_build_for_cs:n #1
4453 {
4454 \int_set_eq:NN \l_@@_min_state_int \l_@@_max_state_int
4455 \@@_build_new_state:
4456 \@@_build_new_state:
4457 \@@_push_lr_states:
4458 #1
4459 \@@_pop_lr_states:
4460 \@@_toks_put_right:Nn \l_@@_right_state_int
4461 {
4462 \if_int_compare:w -2 = \l_@@_curr_char_int
4463 \exp_after:wN \@@_action_success:
4464 \fi:
4465 }
4466 }
4467 % \end{macrocode}
4468 % \end{macro}
4469 %
4470 % \subsubsection{Helpers for building an \textsc{nfa}}
4471 %
4472 % \begin{macro}{\@@_push_lr_states:, \@@_pop_lr_states:}
4473 % When building the regular expression, we keep track of pointers to
4474 % the left-end and right-end of each group without help from \TeX{}'s
4475 % grouping.
4476 % \begin{macrocode}
4477 \cs_new_protected:Npn \@@_push_lr_states:
4478 {
4479 \seq_push:No \l_@@_left_state_seq
4480 { \int_use:N \l_@@_left_state_int }
4481 \seq_push:No \l_@@_right_state_seq
4482 { \int_use:N \l_@@_right_state_int }
4483 }
4484 \cs_new_protected:Npn \@@_pop_lr_states:
4485 {
4486 \seq_pop:NN \l_@@_left_state_seq \l_@@_tmpa_tl
4487 \int_set:Nn \l_@@_left_state_int \l_@@_tmpa_tl
4488 \seq_pop:NN \l_@@_right_state_seq \l_@@_tmpa_tl
4489 \int_set:Nn \l_@@_right_state_int \l_@@_tmpa_tl
4490 }
4491 % \end{macrocode}
4492 % \end{macro}
4493 %
4494 % \begin{macro}
4495 % {
4496 % \@@_build_transition_left:NNN,
4497 % \@@_build_transition_right:nNn
4498 % }
4499 % Add a transition from |#2| to |#3| using the function |#1|. The
4500 % \texttt{left} function is used for higher priority transitions, and
4501 % the \texttt{right} function for lower priority transitions (which
4502 % should be performed later). The signatures differ to reflect the
4503 % differing usage later on. Both functions could be optimized.
4504 % \begin{macrocode}
4505 \cs_new_protected:Npn \@@_build_transition_left:NNN #1#2#3
4506 { \@@_toks_put_left:Ne #2 { #1 { \tex_the:D \@@_int_eval:w #3 - #2 } } }
4507 \cs_new_protected:Npn \@@_build_transition_right:nNn #1#2#3
4508 { \@@_toks_put_right:Ne #2 { #1 { \tex_the:D \@@_int_eval:w #3 - #2 } } }
4509 % \end{macrocode}
4510 % \end{macro}
4511 %
4512 % \begin{macro}{\@@_build_new_state:}
4513 % Add a new empty state to the \textsc{nfa}. Then update the
4514 % \texttt{left}, \texttt{right}, and \texttt{max} states, so that the
4515 % \texttt{right} state is the new empty state, and the \texttt{left}
4516 % state points to the previously \enquote{current} state.
4517 % \begin{macrocode}
4518 \cs_new_protected:Npn \@@_build_new_state:
4519 {
4520 \@@_toks_clear:N \l_@@_max_state_int
4521 \int_set_eq:NN \l_@@_left_state_int \l_@@_right_state_int
4522 \int_set_eq:NN \l_@@_right_state_int \l_@@_max_state_int
4523 \int_incr:N \l_@@_max_state_int
4524 }
4525 % \end{macrocode}
4526 % \end{macro}
4527 %
4528 % \begin{macro}{\@@_build_transitions_laziness:NNNNN}
4529 % This function creates a new state, and puts two transitions starting
4530 % from the old current state. The order of the transitions is
4531 % controlled by |#1|, true for lazy quantifiers, and false for greedy
4532 % quantifiers.
4533 % \begin{macrocode}
4534 \cs_new_protected:Npn \@@_build_transitions_laziness:NNNNN #1#2#3#4#5
4535 {
4536 \@@_build_new_state:
4537 \@@_toks_put_right:Ne \l_@@_left_state_int
4538 {
4539 \if_meaning:w \c_true_bool #1
4540 #2 { \tex_the:D \@@_int_eval:w #3 - \l_@@_left_state_int }
4541 #4 { \tex_the:D \@@_int_eval:w #5 - \l_@@_left_state_int }
4542 \else:
4543 #4 { \tex_the:D \@@_int_eval:w #5 - \l_@@_left_state_int }
4544 #2 { \tex_the:D \@@_int_eval:w #3 - \l_@@_left_state_int }
4545 \fi:
4546 }
4547 }
4548 % \end{macrocode}
4549 % \end{macro}
4550 %
4551 % \subsubsection{Building classes}
4552 %
4553 % \begin{macro}{\@@_class:NnnnN}
4554 % \begin{macro}[rEXP]{\@@_tests_action_cost:n}
4555 % The arguments are: \meta{boolean} \Arg{tests} \Arg{min} \Arg{more}
4556 % \meta{laziness}. First store the tests with a trailing
4557 % \cs{@@_action_cost:n}, in the true branch of
4558 % \cs{@@_break_point:TF} for positive classes, or the false branch
4559 % for negative classes. The integer \meta{more} is $0$ for fixed
4560 % repetitions, $-1$ for unbounded repetitions, and
4561 % $\meta{max}-\meta{min}$ for a range of repetitions.
4562 % \begin{macrocode}
4563 \cs_new_protected:Npn \@@_class:NnnnN #1#2#3#4#5
4564 {
4565 \cs_set:Npe \@@_tests_action_cost:n ##1
4566 {
4567 \exp_not:n { \exp_not:n {#2} }
4568 \bool_if:NTF #1
4569 { \@@_break_point:TF { \@@_action_cost:n {##1} } { } }
4570 { \@@_break_point:TF { } { \@@_action_cost:n {##1} } }
4571 }
4572 \if_case:w - #4 \exp_stop_f:
4573 \@@_class_repeat:n {#3}
4574 \or: \@@_class_repeat:nN {#3} #5
4575 \else: \@@_class_repeat:nnN {#3} {#4} #5
4576 \fi:
4577 }
4578 \cs_new:Npn \@@_tests_action_cost:n { \@@_action_cost:n }
4579 % \end{macrocode}
4580 % \end{macro}
4581 % \end{macro}
4582 %
4583 % \begin{macro}{\@@_class_repeat:n}
4584 % This is used for a fixed number of repetitions. Build one state for
4585 % each repetition, with a transition controlled by the tests that we
4586 % have collected. That works just fine for |#1|${}=0$ repetitions:
4587 % nothing is built.
4588 % \begin{macrocode}
4589 \cs_new_protected:Npn \@@_class_repeat:n #1
4590 {
4591 \prg_replicate:nn {#1}
4592 {
4593 \@@_build_new_state:
4594 \@@_build_transition_right:nNn \@@_tests_action_cost:n
4595 \l_@@_left_state_int \l_@@_right_state_int
4596 }
4597 }
4598 % \end{macrocode}
4599 % \end{macro}
4600 %
4601 % \begin{macro}{\@@_class_repeat:nN}
4602 % This implements unbounded repetitions of a single class (e.g.,
4603 % the |*| and |+| quantifiers). If the minimum number |#1| of
4604 % repetitions is $0$, then build a transition from the current state
4605 % to itself governed by the tests, and a free transition to a new
4606 % state (hence skipping the tests). Otherwise, call
4607 % \cs{@@_class_repeat:n} for the code to match |#1| repetitions,
4608 % and add free transitions from the last state to the previous one,
4609 % and to a new one. In both cases, the order of transitions is
4610 % controlled by the laziness boolean |#2|.
4611 % \begin{macrocode}
4612 \cs_new_protected:Npn \@@_class_repeat:nN #1#2
4613 {
4614 \if_int_compare:w #1 = \c_zero_int
4615 \@@_build_transitions_laziness:NNNNN #2
4616 \@@_action_free:n \l_@@_right_state_int
4617 \@@_tests_action_cost:n \l_@@_left_state_int
4618 \else:
4619 \@@_class_repeat:n {#1}
4620 \int_set_eq:NN \l_@@_tmpa_int \l_@@_left_state_int
4621 \@@_build_transitions_laziness:NNNNN #2
4622 \@@_action_free:n \l_@@_right_state_int
4623 \@@_action_free:n \l_@@_tmpa_int
4624 \fi:
4625 }
4626 % \end{macrocode}
4627 % \end{macro}
4628 %
4629 % \begin{macro}{\@@_class_repeat:nnN}
4630 % We want to build the code to match from |#1| to $|#1|+|#2|$
4631 % repetitions. Match |#1| repetitions (can be $0$). Compute the final
4632 % state of the next construction as \texttt{a}. Build $|#2|>0$ states,
4633 % each with a transition to the next state governed by the tests, and
4634 % a transition to the final state \texttt{a}. The computation of
4635 % \texttt{a} is safe because states are allocated in order, starting
4636 % from \texttt{max_state}.
4637 % \begin{macrocode}
4638 \cs_new_protected:Npn \@@_class_repeat:nnN #1#2#3
4639 {
4640 \@@_class_repeat:n {#1}
4641 \int_set:Nn \l_@@_tmpa_int
4642 { \l_@@_max_state_int + #2 - \c_one_int }
4643 \prg_replicate:nn { #2 }
4644 {
4645 \@@_build_transitions_laziness:NNNNN #3
4646 \@@_action_free:n \l_@@_tmpa_int
4647 \@@_tests_action_cost:n \l_@@_right_state_int
4648 }
4649 }
4650 % \end{macrocode}
4651 % \end{macro}
4652 %
4653 % \subsubsection{Building groups}
4654 %
4655 % \begin{macro}{\@@_group_aux:nnnnN}
4656 % Arguments: \Arg{label} \Arg{contents} \Arg{min} \Arg{more}
4657 % \meta{laziness}. If \meta{min} is $0$, we need to add a state before
4658 % building the group, so that the thread which skips the group does
4659 % not also set the start-point of the submatch. After adding one more
4660 % state, the \texttt{left_state} is the left end of the group, from
4661 % which all branches stem, and the \texttt{right_state} is the
4662 % right end of the group, and all branches end their course in that
4663 % state. We store those two integers to be queried for each branch, we
4664 % build the \textsc{nfa} states for the contents |#2| of the group,
4665 % and we forget about the two integers. Once this is done, perform the
4666 % repetition: either exactly |#3| times, or |#3| or more times, or
4667 % between |#3| and $|#3|+|#4|$ times, with laziness |#5|. The
4668 % \meta{label} |#1| is used for submatch tracking. Each of the three
4669 % auxiliaries expects \texttt{left_state} and \texttt{right_state} to
4670 % be set properly.
4671 % \begin{macrocode}
4672 \cs_new_protected:Npn \@@_group_aux:nnnnN #1#2#3#4#5
4673 {
4674 \if_int_compare:w #3 = \c_zero_int
4675 \@@_build_new_state:
4676 \@@_build_transition_right:nNn \@@_action_free_group:n
4677 \l_@@_left_state_int \l_@@_right_state_int
4678 \fi:
4679 \@@_build_new_state:
4680 \@@_push_lr_states:
4681 #2
4682 \@@_pop_lr_states:
4683 \if_case:w - #4 \exp_stop_f:
4684 \@@_group_repeat:nn {#1} {#3}
4685 \or: \@@_group_repeat:nnN {#1} {#3} #5
4686 \else: \@@_group_repeat:nnnN {#1} {#3} {#4} #5
4687 \fi:
4688 }
4689 % \end{macrocode}
4690 % \end{macro}
4691 %
4692 % \begin{macro}{\@@_group:nnnN, \@@_group_no_capture:nnnN}
4693 % Hand to \cs{@@_group_aux:nnnnnN} the label of that group
4694 % (expanded), and the group itself, with some extra commands to
4695 % perform.
4696 % \begin{macrocode}
4697 \cs_new_protected:Npn \@@_group:nnnN #1
4698 {
4699 \exp_args:No \@@_group_aux:nnnnN
4700 { \int_use:N \l_@@_capturing_group_int }
4701 {
4702 \int_incr:N \l_@@_capturing_group_int
4703 #1
4704 }
4705 }
4706 \cs_new_protected:Npn \@@_group_no_capture:nnnN
4707 { \@@_group_aux:nnnnN { -1 } }
4708 % \end{macrocode}
4709 % \end{macro}
4710 %
4711 % \begin{macro}{\@@_group_resetting:nnnN}
4712 % \begin{macro}{\@@_group_resetting_loop:nnNn}
4713 % Again, hand the label $-1$ to \cs{@@_group_aux:nnnnN}, but this
4714 % time we work a little bit harder to keep track of the maximum group
4715 % label at the end of any branch, and to reset the group number at
4716 % each branch. This relies on the fact that a compiled regex always is
4717 % a sequence of items of the form \cs{@@_branch:n} \Arg{branch}.
4718 % \begin{macrocode}
4719 \cs_new_protected:Npn \@@_group_resetting:nnnN #1
4720 {
4721 \@@_group_aux:nnnnN { -1 }
4722 {
4723 \exp_args:Noo \@@_group_resetting_loop:nnNn
4724 { \int_use:N \l_@@_capturing_group_int }
4725 { \int_use:N \l_@@_capturing_group_int }
4726 #1
4727 { ?? \prg_break:n } { }
4728 \prg_break_point:
4729 }
4730 }
4731 \cs_new_protected:Npn \@@_group_resetting_loop:nnNn #1#2#3#4
4732 {
4733 \use_none:nn #3 { \int_set:Nn \l_@@_capturing_group_int {#1} }
4734 \int_set:Nn \l_@@_capturing_group_int {#2}
4735 #3 {#4}
4736 \exp_args:Ne \@@_group_resetting_loop:nnNn
4737 { \int_max:nn {#1} \l_@@_capturing_group_int }
4738 {#2}
4739 }
4740 % \end{macrocode}
4741 % \end{macro}
4742 % \end{macro}
4743 %
4744 % \begin{macro}{\@@_branch:n}
4745 % Add a free transition from the left state of the current group to a
4746 % brand new state, starting point of this branch. Once the branch is
4747 % built, add a transition from its last state to the right state of
4748 % the group. The left and right states of the group are extracted from
4749 % the relevant sequences.
4750 % \begin{macrocode}
4751 \cs_new_protected:Npn \@@_branch:n #1
4752 {
4753 \@@_build_new_state:
4754 \seq_get:NN \l_@@_left_state_seq \l_@@_tmpa_tl
4755 \int_set:Nn \l_@@_left_state_int \l_@@_tmpa_tl
4756 \@@_build_transition_right:nNn \@@_action_free:n
4757 \l_@@_left_state_int \l_@@_right_state_int
4758 #1
4759 \seq_get:NN \l_@@_right_state_seq \l_@@_tmpa_tl
4760 \@@_build_transition_right:nNn \@@_action_free:n
4761 \l_@@_right_state_int \l_@@_tmpa_tl
4762 }
4763 % \end{macrocode}
4764 % \end{macro}
4765 %
4766 % \begin{macro}{\@@_group_repeat:nn}
4767 % This function is called to repeat a group a fixed number of times
4768 % |#2|; if this is $0$ we remove the group altogether (but don't reset
4769 % the \texttt{capturing_group} label). Otherwise, the auxiliary
4770 % \cs{@@_group_repeat_aux:n} copies |#2| times the \tn{toks} for
4771 % the group, and leaves \texttt{internal_a} pointing to the left end
4772 % of the last repetition. We only record the submatch information at
4773 % the last repetition. Finally, add a state at the end (the transition
4774 % to it has been taken care of by the replicating auxiliary).
4775 % \begin{macrocode}
4776 \cs_new_protected:Npn \@@_group_repeat:nn #1#2
4777 {
4778 \if_int_compare:w #2 = \c_zero_int
4779 \int_set:Nn \l_@@_max_state_int
4780 { \l_@@_left_state_int - \c_one_int }
4781 \@@_build_new_state:
4782 \else:
4783 \@@_group_repeat_aux:n {#2}
4784 \@@_group_submatches:nNN {#1}
4785 \l_@@_tmpa_int \l_@@_right_state_int
4786 \@@_build_new_state:
4787 \fi:
4788 }
4789 % \end{macrocode}
4790 % \end{macro}
4791 %
4792 % \begin{macro}{\@@_group_submatches:nNN}
4793 % This inserts in states |#2| and |#3| the code for tracking
4794 % submatches of the group |#1|, unless inhibited by a label of $-1$.
4795 % \begin{macrocode}
4796 \cs_new_protected:Npn \@@_group_submatches:nNN #1#2#3
4797 {
4798 \if_int_compare:w #1 > - \c_one_int
4799 \@@_toks_put_left:Ne #2 { \@@_action_submatch:nN {#1} < }
4800 \@@_toks_put_left:Ne #3 { \@@_action_submatch:nN {#1} > }
4801 \fi:
4802 }
4803 % \end{macrocode}
4804 % \end{macro}
4805 %
4806 % \begin{macro}{\@@_group_repeat_aux:n}
4807 % Here we repeat \tn{toks} ranging from \texttt{left_state} to
4808 % \texttt{max_state}, $|#1|>0$ times. First add a transition so that
4809 % the copies \enquote{chain} properly. Compute the shift
4810 % \texttt{c} between the original copy and the last copy we
4811 % want. Shift the \texttt{right_state} and \texttt{max_state} to their
4812 % final values. We then want to perform \texttt{c} copy operations. At
4813 % the end, \texttt{b} is equal to the \texttt{max_state}, and
4814 % \texttt{a} points to the left of the last copy of the group.
4815 % \begin{macrocode}
4816 \cs_new_protected:Npn \@@_group_repeat_aux:n #1
4817 {
4818 \@@_build_transition_right:nNn \@@_action_free:n
4819 \l_@@_right_state_int \l_@@_max_state_int
4820 \int_set_eq:NN \l_@@_tmpa_int \l_@@_left_state_int
4821 \int_set_eq:NN \l_@@_tmpb_int \l_@@_max_state_int
4822 \if_int_compare:w \@@_int_eval:w #1 > \c_one_int
4823 \int_set:Nn \l_@@_tmpc_int
4824 {
4825 ( #1 - \c_one_int )
4826 * ( \l_@@_tmpb_int - \l_@@_tmpa_int )
4827 }
4828 \int_add:Nn \l_@@_right_state_int \l_@@_tmpc_int
4829 \int_add:Nn \l_@@_max_state_int \l_@@_tmpc_int
4830 \@@_toks_memcpy:NNn
4831 \l_@@_tmpb_int
4832 \l_@@_tmpa_int
4833 \l_@@_tmpc_int
4834 \fi:
4835 }
4836 % \end{macrocode}
4837 % \end{macro}
4838 %
4839 % \begin{macro}{\@@_group_repeat:nnN}
4840 % This function is called to repeat a group at least $n$ times; the
4841 % case $n=0$ is very different from $n>0$. Assume first that $n=0$.
4842 % Insert submatch tracking information at the start and end of the
4843 % group, add a free transition from the right end to the
4844 % \enquote{true} left state \texttt{a} (remember: in this case we had
4845 % added an extra state before the left state). This forms the loop,
4846 % which we break away from by adding a free transition from \texttt{a}
4847 % to a new state.
4848 %
4849 % Now consider the case $n>0$. Repeat the group $n$ times, chaining
4850 % various copies with a free transition. Add submatch tracking only to
4851 % the last copy, then add a free transition from the right end back to
4852 % the left end of the last copy, either before or after the transition
4853 % to move on towards the rest of the \textsc{nfa}. This transition can
4854 % end up before submatch tracking, but that is irrelevant since it
4855 % only does so when going again through the group, recording new
4856 % matches. Finally, add a state; we already have a transition pointing
4857 % to it from \cs{@@_group_repeat_aux:n}.
4858 % \begin{macrocode}
4859 \cs_new_protected:Npn \@@_group_repeat:nnN #1#2#3
4860 {
4861 \if_int_compare:w #2 = \c_zero_int
4862 \@@_group_submatches:nNN {#1}
4863 \l_@@_left_state_int \l_@@_right_state_int
4864 \int_set:Nn \l_@@_tmpa_int
4865 { \l_@@_left_state_int - \c_one_int }
4866 \@@_build_transition_right:nNn \@@_action_free:n
4867 \l_@@_right_state_int \l_@@_tmpa_int
4868 \@@_build_new_state:
4869 \if_meaning:w \c_true_bool #3
4870 \@@_build_transition_left:NNN \@@_action_free:n
4871 \l_@@_tmpa_int \l_@@_right_state_int
4872 \else:
4873 \@@_build_transition_right:nNn \@@_action_free:n
4874 \l_@@_tmpa_int \l_@@_right_state_int
4875 \fi:
4876 \else:
4877 \@@_group_repeat_aux:n {#2}
4878 \@@_group_submatches:nNN {#1}
4879 \l_@@_tmpa_int \l_@@_right_state_int
4880 \if_meaning:w \c_true_bool #3
4881 \@@_build_transition_right:nNn \@@_action_free_group:n
4882 \l_@@_right_state_int \l_@@_tmpa_int
4883 \else:
4884 \@@_build_transition_left:NNN \@@_action_free_group:n
4885 \l_@@_right_state_int \l_@@_tmpa_int
4886 \fi:
4887 \@@_build_new_state:
4888 \fi:
4889 }
4890 % \end{macrocode}
4891 % \end{macro}
4892 %
4893 % \begin{macro}{\@@_group_repeat:nnnN}
4894 % We wish to repeat the group between |#2| and $|#2|+|#3|$ times, with
4895 % a laziness controlled by |#4|. We insert submatch tracking up front:
4896 % in principle, we could avoid recording submatches for the first |#2|
4897 % copies of the group, but that forces us to treat specially the case
4898 % $|#2|=0$. Repeat that group with submatch tracking $|#2|+|#3|$ times
4899 % (the maximum number of repetitions). Then our goal is to add |#3|
4900 % transitions from the end of the |#2|-th group, and each subsequent
4901 % groups, to the end. For a lazy quantifier, we add those transitions
4902 % to the left states, before submatch tracking. For the greedy case,
4903 % we add the transitions to the right states, after submatch tracking
4904 % and the transitions which go on with more repetitions. In the greedy
4905 % case with $|#2|=0$, the transition which skips over all copies of
4906 % the group must be added separately, because its starting state does
4907 % not follow the normal pattern: we had to add it \enquote{by hand}
4908 % earlier.
4909 % \begin{macrocode}
4910 \cs_new_protected:Npn \@@_group_repeat:nnnN #1#2#3#4
4911 {
4912 \@@_group_submatches:nNN {#1}
4913 \l_@@_left_state_int \l_@@_right_state_int
4914 \@@_group_repeat_aux:n { #2 + #3 }
4915 \if_meaning:w \c_true_bool #4
4916 \int_set_eq:NN \l_@@_left_state_int \l_@@_max_state_int
4917 \prg_replicate:nn { #3 }
4918 {
4919 \int_sub:Nn \l_@@_left_state_int
4920 { \l_@@_tmpb_int - \l_@@_tmpa_int }
4921 \@@_build_transition_left:NNN \@@_action_free:n
4922 \l_@@_left_state_int \l_@@_max_state_int
4923 }
4924 \else:
4925 \prg_replicate:nn { #3 - \c_one_int }
4926 {
4927 \int_sub:Nn \l_@@_right_state_int
4928 { \l_@@_tmpb_int - \l_@@_tmpa_int }
4929 \@@_build_transition_right:nNn \@@_action_free:n
4930 \l_@@_right_state_int \l_@@_max_state_int
4931 }
4932 \if_int_compare:w #2 = \c_zero_int
4933 \int_set:Nn \l_@@_right_state_int
4934 { \l_@@_left_state_int - \c_one_int }
4935 \else:
4936 \int_sub:Nn \l_@@_right_state_int
4937 { \l_@@_tmpb_int - \l_@@_tmpa_int }
4938 \fi:
4939 \@@_build_transition_right:nNn \@@_action_free:n
4940 \l_@@_right_state_int \l_@@_max_state_int
4941 \fi:
4942 \@@_build_new_state:
4943 }
4944 % \end{macrocode}
4945 % \end{macro}
4946 %
4947 % \subsubsection{Others}
4948 %
4949 % \begin{macro}{\@@_assertion:Nn, \@@_b_test:, \@@_A_test:, \@@_G_test:, \@@_Z_test:}
4950 % Usage: \cs{@@_assertion:Nn} \meta{boolean} \Arg{test}, where the
4951 % \meta{test} is either of the two other functions. Add a free
4952 % transition to a new state, conditionally to the assertion test. The
4953 % \cs{@@_b_test:} test is used by the |\b| and |\B| escape: check
4954 % if the last character was a word character or not, and do the same
4955 % to the current character. The boundary-markers of the string are
4956 % non-word characters for this purpose.
4957 % \begin{macrocode}
4958 \cs_new_protected:Npn \@@_assertion:Nn #1#2
4959 {
4960 \@@_build_new_state:
4961 \@@_toks_put_right:Ne \l_@@_left_state_int
4962 {
4963 \exp_not:n {#2}
4964 \@@_break_point:TF
4965 \bool_if:NF #1 { { } }
4966 {
4967 \@@_action_free:n
4968 {
4969 \tex_the:D \@@_int_eval:w
4970 \l_@@_right_state_int - \l_@@_left_state_int
4971 }
4972 }
4973 \bool_if:NT #1 { { } }
4974 }
4975 }
4976 \cs_new_protected:Npn \@@_b_test:
4977 {
4978 \group_begin:
4979 \int_set_eq:NN \l_@@_curr_char_int \l_@@_last_char_int
4980 \@@_prop_w:
4981 \@@_break_point:TF
4982 { \group_end: \@@_item_reverse:n { \@@_prop_w: } }
4983 { \group_end: \@@_prop_w: }
4984 }
4985 \cs_new_protected:Npn \@@_Z_test:
4986 {
4987 \if_int_compare:w -2 = \l_@@_curr_char_int
4988 \exp_after:wN \@@_break_true:w
4989 \fi:
4990 }
4991 \cs_new_protected:Npn \@@_A_test:
4992 {
4993 \if_int_compare:w -2 = \l_@@_last_char_int
4994 \exp_after:wN \@@_break_true:w
4995 \fi:
4996 }
4997 \cs_new_protected:Npn \@@_G_test:
4998 {
4999 \if_int_compare:w \l_@@_curr_pos_int = \l_@@_start_pos_int
5000 \exp_after:wN \@@_break_true:w
5001 \fi:
5002 }
5003 % \end{macrocode}
5004 % \end{macro}
5005 %
5006 % \begin{macro}{\@@_command_K:}
5007 % Change the starting point of the $0$-th submatch (full match), and
5008 % transition to a new state, pretending that this is a fresh thread.
5009 % \begin{macrocode}
5010 \cs_new_protected:Npn \@@_command_K:
5011 {
5012 \@@_build_new_state:
5013 \@@_toks_put_right:Ne \l_@@_left_state_int
5014 {
5015 \@@_action_submatch:nN \c_zero_int <
5016 \bool_set_true:N \l_@@_fresh_thread_bool
5017 \@@_action_free:n
5018 {
5019 \tex_the:D \@@_int_eval:w
5020 \l_@@_right_state_int - \l_@@_left_state_int
5021 }
5022 \bool_set_false:N \l_@@_fresh_thread_bool
5023 }
5024 }
5025 % \end{macrocode}
5026 % \end{macro}
5027 %
5028 % \subsection{Matching}
5029 %
5030 % We search for matches by running all the execution threads through the
5031 % \textsc{nfa} in parallel, reading one token of the query at each step.
5032 % The \textsc{nfa} contains \enquote{free} transitions to other states,
5033 % and transitions which \enquote{consume} the current token. For free
5034 % transitions, the instruction at the new state of the \textsc{nfa} is
5035 % performed immediately. When a transition consumes a character, the
5036 % new state is appended to a list of \enquote{active states}, stored in
5037 % \cs{g_@@_thread_info_intarray} (together with submatch information):
5038 % this thread is made active again when the next
5039 % token is read from the query. At every step (for each token in the
5040 % query), we unpack that list of active states and the corresponding
5041 % submatch props, and empty those.
5042 %
5043 % If two paths through the \textsc{nfa} \enquote{collide} in the sense
5044 % that they reach the same state after reading a given token, then they
5045 % only differ in how they previously matched, and any future execution
5046 % would be identical for both. (Note that this would be wrong in the
5047 % presence of back-references.) Hence, we only need to keep one of the
5048 % two threads: the thread with the highest priority. Our \textsc{nfa} is
5049 % built in such a way that higher priority actions always come before
5050 % lower priority actions, which makes things work.
5051 %
5052 % The explanation in the previous paragraph may make us think that we
5053 % simply need to keep track of which states were visited at a given
5054 % step: after all, the loop generated when matching |(a?)*| against |a|
5055 % is broken, isn't it? No. The group first matches |a|, as it should,
5056 % then repeats; it attempts to match |a| again but fails; it skips |a|,
5057 % and finds out that this state has already been seen at this position
5058 % in the query: the match stops. The capturing group is (wrongly) |a|.
5059 % What went wrong is that a thread collided with itself, and the later
5060 % version, which has gone through the group one more times with an empty
5061 % match, should have a higher priority than not going through the group.
5062 %
5063 % We solve this by distinguishing \enquote{normal} free transitions
5064 % \cs{@@_action_free:n} from transitions
5065 % \cs{@@_action_free_group:n} which go back to the start of the
5066 % group. The former keeps threads unless they have been visited by a
5067 % \enquote{completed} thread, while the latter kind of transition also
5068 % prevents going back to a state visited by the current thread.
5069 %
5070 % \subsubsection{Variables used when matching}
5071 %
5072 % \begin{variable}
5073 % {
5074 % \l_@@_min_pos_int,
5075 % \l_@@_max_pos_int,
5076 % \l_@@_curr_pos_int,
5077 % \l_@@_start_pos_int,
5078 % \l_@@_success_pos_int,
5079 % }
5080 % The tokens in the query are indexed from \texttt{min_pos} for the
5081 % first to $\texttt{max_pos}-1$ for the last, and their information is
5082 % stored in several arrays and \tn{toks} registers with those numbers.
5083 % We match
5084 % without backtracking, keeping all threads in lockstep at the
5085 % \texttt{curr_pos} in the query. The starting point of the current
5086 % match attempt is \texttt{start_pos}, and \texttt{success_pos},
5087 % updated whenever a thread succeeds, is used as the next starting
5088 % position.
5089 % \begin{macrocode}
5090 \int_new:N \l_@@_min_pos_int
5091 \int_new:N \l_@@_max_pos_int
5092 \int_new:N \l_@@_curr_pos_int
5093 \int_new:N \l_@@_start_pos_int
5094 \int_new:N \l_@@_success_pos_int
5095 % \end{macrocode}
5096 % \end{variable}
5097 %
5098 % \begin{variable}
5099 % {
5100 % \l_@@_curr_char_int,
5101 % \l_@@_curr_catcode_int,
5102 % \l_@@_curr_token_tl,
5103 % \l_@@_last_char_int,
5104 % \l_@@_last_char_success_int,
5105 % \l_@@_case_changed_char_int
5106 % }
5107 % The character and category codes of the token at the current
5108 % position and a token list expanding to that token; the character
5109 % code of the token at the previous position;
5110 % the character code of the token just before a successful match;
5111 % and the character code of the result of changing the case of the
5112 % current token (|A-Z|$\leftrightarrow$|a-z|). This last integer is
5113 % only computed when necessary, and is otherwise \cs{c_max_int}. The
5114 % \texttt{curr_char} variable is also used in various other phases
5115 % to hold a character code.
5116 % \begin{macrocode}
5117 \int_new:N \l_@@_curr_char_int
5118 \int_new:N \l_@@_curr_catcode_int
5119 \tl_new:N \l_@@_curr_token_tl
5120 \int_new:N \l_@@_last_char_int
5121 \int_new:N \l_@@_last_char_success_int
5122 \int_new:N \l_@@_case_changed_char_int
5123 % \end{macrocode}
5124 % \end{variable}
5125 %
5126 % \begin{variable}{\l_@@_curr_state_int}
5127 % For every character in the token list, each of the active states is
5128 % considered in turn. The variable \cs{l_@@_curr_state_int}
5129 % holds the state of the \textsc{nfa} which is currently considered:
5130 % transitions are then given as shifts relative to the current state.
5131 % \begin{macrocode}
5132 \int_new:N \l_@@_curr_state_int
5133 % \end{macrocode}
5134 % \end{variable}
5135 %
5136 % \begin{variable}
5137 % {\l_@@_curr_submatches_tl, \l_@@_success_submatches_tl}
5138 % The submatches for the thread which is currently active are stored
5139 % in the \texttt{curr_submatches} list, which is almost a comma list,
5140 % but ends with a comma. This list is stored by \cs{@@_store_state:n}
5141 % into an intarray variable, to be retrieved when matching at the next
5142 % position. When a thread succeeds, this list is copied to
5143 % \cs{l_@@_success_submatches_tl}: only the last successful thread
5144 % remains there.
5145 % \begin{macrocode}
5146 \tl_new:N \l_@@_curr_submatches_tl
5147 \tl_new:N \l_@@_success_submatches_tl
5148 % \end{macrocode}
5149 % \end{variable}
5150 %
5151 % \begin{variable}{\l_@@_step_int}
5152 % This integer, always even, is increased every time a character in
5153 % the query is read, and not reset when doing multiple matches. We
5154 % store in \cs{g_@@_state_active_intarray} the last step in which each
5155 % \meta{state} in the \textsc{nfa} was encountered. This lets us break
5156 % infinite loops by not visiting the same state twice in the same
5157 % step. In fact, the step we store is equal to \texttt{step} when we
5158 % have started performing the operations of \tn{toks}\meta{state}, but
5159 % not finished yet. However, once we finish, we store
5160 % $\text{\texttt{step}}+1$ in \cs{g_@@_state_active_intarray}. This is
5161 % needed to track submatches
5162 % properly (see building phase). The \texttt{step} is also used to
5163 % attach each set of submatch information to a given iteration (and
5164 % automatically discard it when it corresponds to a past step).
5165 % \begin{macrocode}
5166 \int_new:N \l_@@_step_int
5167 % \end{macrocode}
5168 % \end{variable}
5169 %
5170 % \begin{variable}{\l_@@_min_thread_int, \l_@@_max_thread_int}
5171 % All the currently active threads are kept in order of precedence in
5172 % \cs{g_@@_thread_info_intarray} together with the corresponding
5173 % submatch information. Data in this intarray is organized as blocks
5174 % from \texttt{min_thread} (included) to \texttt{max_thread}
5175 % (excluded). At the start of every step, the whole array is
5176 % unpacked, so that the space can immediately be reused, and
5177 % \texttt{max_thread} is reset to \texttt{min_thread}, effectively
5178 % clearing the array.
5179 % \begin{macrocode}
5180 \int_new:N \l_@@_min_thread_int
5181 \int_new:N \l_@@_max_thread_int
5182 % \end{macrocode}
5183 % \end{variable}
5184 %
5185 % \begin{variable}{\g_@@_state_active_intarray, \g_@@_thread_info_intarray}
5186 % \cs{g_@@_state_active_intarray} stores the last \meta{step} in which
5187 % each \meta{state} was active. \cs{g_@@_thread_info_intarray} stores
5188 % threads to be considered in the next step, more precisely the
5189 % states in which these threads are.
5190 % \begin{macrocode}
5191 \intarray_new:Nn \g_@@_state_active_intarray { 65536 }
5192 \intarray_new:Nn \g_@@_thread_info_intarray { 65536 }
5193 % \end{macrocode}
5194 % \end{variable}
5195 %
5196 % \begin{variable}{\l_@@_matched_analysis_tl, \l_@@_curr_analysis_tl}
5197 % The list \cs{l_@@_curr_analysis_tl} consists of a brace group
5198 % containing three brace groups corresponding to the current token,
5199 % with the same syntax as \cs{tl_analysis_map_inline:nn}. The list
5200 % \cs{l_@@_matched_analysis_tl} (constructed under the
5201 % \texttt{tl\_build} machinery) has one item for each token that has
5202 % already been treated so far in a given match attempt: each item
5203 % consists of three brace groups with the same syntax as
5204 % \cs{tl_analysis_map_inline:nn}.
5205 % \begin{macrocode}
5206 \tl_new:N \l_@@_matched_analysis_tl
5207 \tl_new:N \l_@@_curr_analysis_tl
5208 % \end{macrocode}
5209 % \end{variable}
5210 %
5211 % \begin{variable}{\l_@@_every_match_tl}
5212 % Every time a match is found, this token list is used. For single
5213 % matching, the token list is empty. For multiple matching, the token
5214 % list is set to repeat the matching, after performing some operation
5215 % which depends on the user function. See \cs{@@_single_match:} and
5216 % \cs{@@_multi_match:n}.
5217 % \begin{macrocode}
5218 \tl_new:N \l_@@_every_match_tl
5219 % \end{macrocode}
5220 % \end{variable}
5221 %
5222 % \begin{variable}{\l_@@_fresh_thread_bool, \l_@@_empty_success_bool}
5223 % \begin{macro}{\@@_if_two_empty_matches:F}
5224 % When doing multiple matches, we need to avoid infinite loops where
5225 % each iteration matches the same empty token list. When an empty
5226 % token list is matched, the next successful match of the same empty
5227 % token list is suppressed. We detect empty matches by setting
5228 % \cs{l_@@_fresh_thread_bool} to \texttt{true} for threads which
5229 % directly come from the start of the regex or from the |\K| command,
5230 % and testing that boolean whenever a thread succeeds. The function
5231 % \cs{@@_if_two_empty_matches:F} is redefined at every match
5232 % attempt, depending on whether the previous match was empty or not:
5233 % if it was, then the function must cancel a purported success if it
5234 % is empty and at the same spot as the previous match; otherwise, we
5235 % definitely don't have two identical empty matches, so the function
5236 % is \cs{use:n}.
5237 % \begin{macrocode}
5238 \bool_new:N \l_@@_fresh_thread_bool
5239 \bool_new:N \l_@@_empty_success_bool
5240 \cs_new_eq:NN \@@_if_two_empty_matches:F \use:n
5241 % \end{macrocode}
5242 % \end{macro}
5243 % \end{variable}
5244 %
5245 % \begin{variable}
5246 % {
5247 % \g_@@_success_bool,
5248 % \l_@@_saved_success_bool,
5249 % \l_@@_match_success_bool
5250 % }
5251 % The boolean \cs{l_@@_match_success_bool} is true if the current
5252 % match attempt was successful, and \cs{g_@@_success_bool} is true
5253 % if there was at least one successful match. This is the only global
5254 % variable in this whole module, but we would need it to be local when
5255 % matching a control sequence with |\c{...}|. This is done by saving
5256 % the global variable into \cs{l_@@_saved_success_bool}, which is
5257 % local, hence not affected by the changes due to inner regex
5258 % functions.
5259 % \begin{macrocode}
5260 \bool_new:N \g_@@_success_bool
5261 \bool_new:N \l_@@_saved_success_bool
5262 \bool_new:N \l_@@_match_success_bool
5263 % \end{macrocode}
5264 % \end{variable}
5265 %
5266 % \subsubsection{Matching: framework}
5267 %
5268 % \begin{macro}{\@@_match:n, \@@_match_cs:n}
5269 % \begin{macro}{\@@_match_init:}
5270 % Initialize the variables that should
5271 % be set once for each user function (even for multiple
5272 % matches). Namely, the overall matching is not yet successful; none of
5273 % the states should be marked as visited (\cs{g_@@_state_active_intarray}), and
5274 % we start at step $0$; we pretend that there was a previous match
5275 % ending at the start of the query, which was not empty (to avoid
5276 % smothering an empty match at the start). Once all this is set up, we
5277 % are ready for the ride. Find the first match.
5278 % \begin{macrocode}
5279 \cs_new_protected:Npn \@@_match:n #1
5280 {
5281 \@@_match_init:
5282 \@@_match_once_init:
5283 \tl_analysis_map_inline:nn {#1}
5284 { \@@_match_one_token:nnN {##1} {##2} ##3 }
5285 \@@_match_one_token:nnN { } { -2 } F
5286 \prg_break_point:Nn \@@_maplike_break: { }
5287 }
5288 \cs_new_protected:Npn \@@_match_cs:n #1
5289 {
5290 \int_set_eq:NN \l_@@_min_thread_int \l_@@_max_thread_int
5291 \@@_match_init:
5292 \@@_match_once_init:
5293 \str_map_inline:nn {#1}
5294 {
5295 \tl_if_blank:nTF {##1}
5296 { \@@_match_one_token:nnN {##1} {`##1} A }
5297 { \@@_match_one_token:nnN {##1} {`##1} C }
5298 }
5299 \@@_match_one_token:nnN { } { -2 } F
5300 \prg_break_point:Nn \@@_maplike_break: { }
5301 }
5302 \cs_new_protected:Npn \@@_match_init:
5303 {
5304 \bool_gset_false:N \g_@@_success_bool
5305 \int_step_inline:nnn
5306 \l_@@_min_state_int { \l_@@_max_state_int - \c_one_int }
5307 {
5308 \__kernel_intarray_gset:Nnn
5309 \g_@@_state_active_intarray {##1} \c_one_int
5310 }
5311 \int_zero:N \l_@@_step_int
5312 \int_set:Nn \l_@@_min_pos_int { 2 }
5313 \int_set_eq:NN \l_@@_success_pos_int \l_@@_min_pos_int
5314 \int_set:Nn \l_@@_last_char_success_int { -2 }
5315 \tl_build_begin:N \l_@@_matched_analysis_tl
5316 \tl_clear:N \l_@@_curr_analysis_tl
5317 \int_set_eq:NN \l_@@_min_submatch_int \c_one_int
5318 \int_set_eq:NN \l_@@_submatch_int \l_@@_min_submatch_int
5319 \bool_set_false:N \l_@@_empty_success_bool
5320 }
5321 % \end{macrocode}
5322 % \end{macro}
5323 % \end{macro}
5324 %
5325 % \begin{macro}{\@@_match_once_init:}
5326 % This function resets various variables used when finding one match.
5327 % It is called before the loop through characters, and every time we
5328 % find a match, before searching for another match (this is controlled
5329 % by the \texttt{every_match} token list).
5330 %
5331 % First initialize some variables: set the
5332 % conditional which detects identical empty matches; this match
5333 % attempt starts at the previous \texttt{success_pos}, is not yet
5334 % successful, and has no submatches yet; clear the array of active
5335 % threads, and put the starting state $0$ in it. We are then almost
5336 % ready to read our first token in the query, but we actually start
5337 % one position earlier than the start because
5338 % \cs{@@_match_one_token:nnN} increments \cs{l_@@_curr_pos_int} and
5339 % saves \cs{l_@@_curr_char_int} as the \texttt{last_char} so that word
5340 % boundaries can be correctly identified.
5341 % \begin{macrocode}
5342 \cs_new_protected:Npn \@@_match_once_init:
5343 {
5344 \if_meaning:w \c_true_bool \l_@@_empty_success_bool
5345 \cs_set:Npn \@@_if_two_empty_matches:F
5346 {
5347 \int_compare:nNnF
5348 \l_@@_start_pos_int = \l_@@_curr_pos_int
5349 }
5350 \else:
5351 \cs_set_eq:NN \@@_if_two_empty_matches:F \use:n
5352 \fi:
5353 \int_set_eq:NN \l_@@_start_pos_int \l_@@_success_pos_int
5354 \bool_set_false:N \l_@@_match_success_bool
5355 \tl_set:Ne \l_@@_curr_submatches_tl
5356 { \prg_replicate:nn { 2 * \l_@@_capturing_group_int } { 0 , } }
5357 \int_set_eq:NN \l_@@_max_thread_int \l_@@_min_thread_int
5358 \@@_store_state:n { \l_@@_min_state_int }
5359 \int_set:Nn \l_@@_curr_pos_int { \l_@@_start_pos_int - \c_one_int }
5360 \int_set_eq:NN \l_@@_curr_char_int \l_@@_last_char_success_int
5361 \tl_build_get_intermediate:NN \l_@@_matched_analysis_tl \l_@@_tmpa_tl
5362 \exp_args:NNf \@@_match_once_init_aux:
5363 \tl_map_inline:nn
5364 { \exp_after:wN \l_@@_tmpa_tl \l_@@_curr_analysis_tl }
5365 { \@@_match_one_token:nnN ##1 }
5366 \prg_break_point:Nn \@@_maplike_break: { }
5367 }
5368 \cs_new_protected:Npn \@@_match_once_init_aux:
5369 {
5370 \tl_build_begin:N \l_@@_matched_analysis_tl
5371 \tl_clear:N \l_@@_curr_analysis_tl
5372 }
5373 % \end{macrocode}
5374 % \end{macro}
5375 %
5376 % \begin{macro}{\@@_single_match:, \@@_multi_match:n}
5377 % For a single match, the overall success is determined by whether the
5378 % only match attempt is a success. When doing multiple matches, the
5379 % overall matching is successful as soon as any match
5380 % succeeds. Perform the action |#1|, then find the next match.
5381 % \begin{macrocode}
5382 \cs_new_protected:Npn \@@_single_match:
5383 {
5384 \tl_set:Nn \l_@@_every_match_tl
5385 {
5386 \bool_gset_eq:NN
5387 \g_@@_success_bool
5388 \l_@@_match_success_bool
5389 \@@_maplike_break:
5390 }
5391 }
5392 \cs_new_protected:Npn \@@_multi_match:n #1
5393 {
5394 \tl_set:Nn \l_@@_every_match_tl
5395 {
5396 \if_meaning:w \c_false_bool \l_@@_match_success_bool
5397 \exp_after:wN \@@_maplike_break:
5398 \fi:
5399 \bool_gset_true:N \g_@@_success_bool
5400 #1
5401 \@@_match_once_init:
5402 }
5403 }
5404 % \end{macrocode}
5405 % \end{macro}
5406 %
5407 % \begin{macro}{\@@_match_one_token:nnN}
5408 % \begin{macro}[rEXP]{\@@_match_one_active:n}
5409 % At each new position, set some variables and get the new character
5410 % and category from the query. Then unpack the array of active
5411 % threads, and clear it by resetting its length
5412 % (\texttt{max_thread}). This results in a sequence of
5413 % \cs{@@_use_state_and_submatches:w} \meta{state}|,|\meta{submatch-clist}\cs{@@_sep:} and
5414 % we consider those states one by one in order. As soon as a thread
5415 % succeeds, exit the step, and, if there are threads to consider at the
5416 % next position, and we have not reached the end of the string,
5417 % repeat the loop. Otherwise, the last thread that succeeded is the
5418 % match. We explain the \texttt{fresh_thread} business when
5419 % describing \cs{@@_action_wildcard:}.
5420 % \begin{macrocode}
5421 \cs_new_protected:Npn \@@_match_one_token:nnN #1#2#3
5422 {
5423 \int_add:Nn \l_@@_step_int { 2 }
5424 \int_incr:N \l_@@_curr_pos_int
5425 \int_set_eq:NN \l_@@_last_char_int \l_@@_curr_char_int
5426 \cs_set_eq:NN \@@_maybe_compute_ccc: \@@_compute_case_changed_char:
5427 \tl_set:Nn \l_@@_curr_token_tl {#1}
5428 \int_set:Nn \l_@@_curr_char_int {#2}
5429 \int_set:Nn \l_@@_curr_catcode_int { "#3 }
5430 \tl_build_put_right:Ne \l_@@_matched_analysis_tl
5431 { \exp_not:o \l_@@_curr_analysis_tl }
5432 \tl_set:Nn \l_@@_curr_analysis_tl { { {#1} {#2} #3 } }
5433 \use:e
5434 {
5435 \int_set_eq:NN \l_@@_max_thread_int \l_@@_min_thread_int
5436 \int_step_function:nnN
5437 \l_@@_min_thread_int
5438 { \l_@@_max_thread_int - \c_one_int }
5439 \@@_match_one_active:n
5440 }
5441 \prg_break_point:
5442 \bool_set_false:N \l_@@_fresh_thread_bool
5443 \if_int_compare:w \l_@@_max_thread_int > \l_@@_min_thread_int
5444 \if_int_compare:w -2 < \l_@@_curr_char_int
5445 \exp_after:wN \use_i:nn
5446 \fi:
5447 \fi:
5448 \l_@@_every_match_tl
5449 }
5450 \cs_new:Npn \@@_match_one_active:n #1
5451 {
5452 \@@_use_state_and_submatches:w
5453 \__kernel_intarray_range_to_clist:Nnn
5454 \g_@@_thread_info_intarray
5455 { \c_one_int + #1 * (\l_@@_capturing_group_int * 2 + \c_one_int) }
5456 { (\c_one_int + #1) * (\l_@@_capturing_group_int * 2 + \c_one_int) }
5457 \@@_sep:
5458 }
5459 % \end{macrocode}
5460 % \end{macro}
5461 % \end{macro}
5462 %
5463 % \subsubsection{Using states of the \textsc{nfa}}
5464 %
5465 % \begin{macro}{\@@_use_state:}
5466 % Use the current \textsc{nfa} instruction. The state is initially
5467 % marked as belonging to the current \texttt{step}: this allows normal
5468 % free transition to repeat, but group-repeating transitions
5469 % won't. Once we are done exploring all the branches it spawned, the
5470 % state is marked as $\texttt{step}+1$: any thread hitting it at that
5471 % point will be terminated.
5472 % \begin{macrocode}
5473 \cs_new_protected:Npn \@@_use_state:
5474 {
5475 \__kernel_intarray_gset:Nnn \g_@@_state_active_intarray
5476 \l_@@_curr_state_int \l_@@_step_int
5477 \@@_toks_use:w \l_@@_curr_state_int
5478 \__kernel_intarray_gset:Nnn \g_@@_state_active_intarray
5479 \l_@@_curr_state_int
5480 { \@@_int_eval:w \l_@@_step_int + \c_one_int \scan_stop: }
5481 }
5482 % \end{macrocode}
5483 % \end{macro}
5484 %
5485 % \begin{macro}{\@@_use_state_and_submatches:w}
5486 % This function is called as one item in the array of active threads
5487 % after that array has been unpacked for a new step. Update the
5488 % \texttt{curr_state} and \texttt{curr_submatches} and use the
5489 % state if it has not yet been encountered at this step.
5490 % \begin{macrocode}
5491 \cs_new_protected:Npn \@@_use_state_and_submatches:w #1 , #2 \@@_sep:
5492 {
5493 \int_set:Nn \l_@@_curr_state_int {#1}
5494 \if_int_compare:w
5495 \__kernel_intarray_item:Nn \g_@@_state_active_intarray
5496 \l_@@_curr_state_int
5497 < \l_@@_step_int
5498 \tl_set:Nn \l_@@_curr_submatches_tl { #2 , }
5499 \exp_after:wN \@@_use_state:
5500 \fi:
5501 \scan_stop:
5502 }
5503 % \end{macrocode}
5504 % \end{macro}
5505 %
5506 % \subsubsection{Actions when matching}
5507 %
5508 % \begin{macro}{\@@_action_start_wildcard:N}
5509 % For an unanchored match, state $0$ has a free transition to the next
5510 % and a costly one to itself, to repeat at the next position. To catch
5511 % repeated identical empty matches, we need to know if a successful
5512 % thread corresponds to an empty match. The instruction resetting
5513 % \cs{l_@@_fresh_thread_bool} may be skipped by a successful
5514 % thread, hence we had to add it to \cs{@@_match_one_token:nnN} too.
5515 % \begin{macrocode}
5516 \cs_new_protected:Npn \@@_action_start_wildcard:N #1
5517 {
5518 \bool_set_true:N \l_@@_fresh_thread_bool
5519 \@@_action_free:n {1}
5520 \bool_set_false:N \l_@@_fresh_thread_bool
5521 \bool_if:NT #1 { \@@_action_cost:n {0} }
5522 }
5523 % \end{macrocode}
5524 % \end{macro}
5525 %
5526 % \begin{macro}{\@@_action_free:n, \@@_action_free_group:n}
5527 % \begin{macro}{\@@_action_free_aux:nn}
5528 % These functions copy a thread after checking that the \textsc{nfa}
5529 % state has not already been used at this position. If not, store
5530 % submatches in the new state, and insert the instructions for that
5531 % state in the input stream. Then restore the old value of
5532 % \cs{l_@@_curr_state_int} and of the current submatches. The
5533 % two types of free transitions differ by how they test that the state
5534 % has not been encountered yet: the \texttt{group} version is
5535 % stricter, and will not use a state if it was used earlier in the
5536 % current thread, hence forcefully breaking the loop, while the
5537 % \enquote{normal} version will revisit a state even within the thread
5538 % itself.
5539 % \begin{macrocode}
5540 \cs_new_protected:Npn \@@_action_free:n
5541 { \@@_action_free_aux:nn { > \l_@@_step_int \else: } }
5542 \cs_new_protected:Npn \@@_action_free_group:n
5543 { \@@_action_free_aux:nn { < \l_@@_step_int } }
5544 \cs_new_protected:Npn \@@_action_free_aux:nn #1#2
5545 {
5546 \use:e
5547 {
5548 \int_add:Nn \l_@@_curr_state_int {#2}
5549 \exp_not:n
5550 {
5551 \if_int_compare:w
5552 \__kernel_intarray_item:Nn \g_@@_state_active_intarray
5553 \l_@@_curr_state_int
5554 #1
5555 \exp_after:wN \@@_use_state:
5556 \fi:
5557 }
5558 \int_set:Nn \l_@@_curr_state_int
5559 { \int_use:N \l_@@_curr_state_int }
5560 \tl_set:Nn \exp_not:N \l_@@_curr_submatches_tl
5561 { \exp_not:o \l_@@_curr_submatches_tl }
5562 }
5563 }
5564 % \end{macrocode}
5565 % \end{macro}
5566 % \end{macro}
5567 %
5568 % \begin{macro}{\@@_action_cost:n}
5569 % A transition which consumes the current character and shifts the
5570 % state by |#1|. The resulting state is stored in the appropriate array
5571 % for use at the next position, and we also store the current
5572 % submatches.
5573 % \begin{macrocode}
5574 \cs_new_protected:Npn \@@_action_cost:n #1
5575 {
5576 \exp_args:No \@@_store_state:n
5577 { \tex_the:D \@@_int_eval:w \l_@@_curr_state_int + #1 }
5578 }
5579 % \end{macrocode}
5580 % \end{macro}
5581 %
5582 % \begin{macro}{\@@_store_state:n}
5583 % \begin{macro}{\@@_store_submatches:}
5584 % Put the given state and current submatch information in
5585 % \cs{g_@@_thread_info_intarray}, and increment the length of the
5586 % array.
5587 % \begin{macrocode}
5588 \cs_new_protected:Npn \@@_store_state:n #1
5589 {
5590 \exp_args:No \@@_store_submatches:nn
5591 \l_@@_curr_submatches_tl {#1}
5592 \int_incr:N \l_@@_max_thread_int
5593 }
5594 \cs_new_protected:Npn \@@_store_submatches:nn #1#2
5595 {
5596 \__kernel_intarray_gset_range_from_clist:Nnn
5597 \g_@@_thread_info_intarray
5598 {
5599 \@@_int_eval:w
5600 \c_one_int + \l_@@_max_thread_int *
5601 (\l_@@_capturing_group_int * 2 + \c_one_int)
5602 }
5603 { #2 , #1 }
5604 }
5605 % \end{macrocode}
5606 % \end{macro}
5607 % \end{macro}
5608 %
5609 % \begin{macro}{\@@_disable_submatches:}
5610 % Some user functions don't require tracking submatches.
5611 % We get a performance improvement by simply defining the
5612 % relevant functions to remove their argument and do nothing
5613 % with it.
5614 % \begin{macrocode}
5615 \cs_new_protected:Npn \@@_disable_submatches:
5616 {
5617 \cs_set_protected:Npn \@@_store_submatches:n ##1 { }
5618 \cs_set_protected:Npn \@@_action_submatch:nN ##1##2 { }
5619 }
5620 % \end{macrocode}
5621 % \end{macro}
5622 %
5623 % \begin{macro}{\@@_action_submatch:nN, \@@_action_submatch_aux:w, \@@_action_submatch_auxii:w, \@@_action_submatch_auxiii:w, \@@_action_submatch_auxiv:w}
5624 % Update the current submatches with the information from the current
5625 % position. Maybe a bottleneck.
5626 % \begin{macrocode}
5627 \cs_new_protected:Npn \@@_action_submatch:nN #1#2
5628 {
5629 \exp_after:wN \@@_action_submatch_aux:w
5630 \l_@@_curr_submatches_tl \@@_sep: {#1} #2
5631 }
5632 \cs_new_protected:Npn \@@_action_submatch_aux:w #1 \@@_sep: #2#3
5633 {
5634 \tl_set:Ne \l_@@_curr_submatches_tl
5635 {
5636 \prg_replicate:nn
5637 { #2 \if_meaning:w > #3 + \l_@@_capturing_group_int \fi: }
5638 { \@@_action_submatch_auxii:w }
5639 \@@_action_submatch_auxiii:w
5640 #1
5641 }
5642 }
5643 \cs_new:Npn \@@_action_submatch_auxii:w
5644 #1 \@@_action_submatch_auxiii:w #2 ,
5645 { #2 , #1 \@@_action_submatch_auxiii:w }
5646 \cs_new:Npn \@@_action_submatch_auxiii:w #1 ,
5647 { \int_use:N \l_@@_curr_pos_int , }
5648 % \end{macrocode}
5649 % \end{macro}
5650 %
5651 % \begin{macro}{\@@_action_success:}
5652 % There is a successful match when an execution path reaches the last
5653 % state in the \textsc{nfa}, unless this marks a second identical
5654 % empty match. Then mark that there was a successful match; it is
5655 % empty if it is \enquote{fresh}; and we store the current position
5656 % and submatches. The current step is then interrupted with
5657 % \cs{prg_break:}, and only paths with higher precedence are
5658 % pursued further. The values stored here may be overwritten by a
5659 % later success of a path with higher precedence.
5660 % \begin{macrocode}
5661 \cs_new_protected:Npn \@@_action_success:
5662 {
5663 \@@_if_two_empty_matches:F
5664 {
5665 \bool_set_true:N \l_@@_match_success_bool
5666 \bool_set_eq:NN \l_@@_empty_success_bool
5667 \l_@@_fresh_thread_bool
5668 \int_set_eq:NN \l_@@_success_pos_int \l_@@_curr_pos_int
5669 \int_set_eq:NN \l_@@_last_char_success_int \l_@@_last_char_int
5670 \tl_build_begin:N \l_@@_matched_analysis_tl
5671 \tl_set_eq:NN \l_@@_success_submatches_tl
5672 \l_@@_curr_submatches_tl
5673 \prg_break:
5674 }
5675 }
5676 % \end{macrocode}
5677 % \end{macro}
5678 %
5679 % \subsection{Replacement}
5680 %
5681 % \subsubsection{Variables and helpers used in replacement}
5682 %
5683 % \begin{variable}{\l_@@_replacement_csnames_int}
5684 % The behavior of closing braces inside a replacement text depends on
5685 % whether a sequences |\c{| or |\u{| has been encountered. The number
5686 % of \enquote{open} such sequences that should be closed by |}| is
5687 % stored in \cs{l_@@_replacement_csnames_int}, and decreased by
5688 % $1$ by each |}|.
5689 % \begin{macrocode}
5690 \int_new:N \l_@@_replacement_csnames_int
5691 % \end{macrocode}
5692 % \end{variable}
5693 %
5694 % \begin{variable}{\l_@@_replacement_category_tl, \l_@@_replacement_category_seq}
5695 % This sequence of letters is used to correctly restore categories in
5696 % nested constructions such as |\cL(abc\cD(_)d)|.
5697 % \begin{macrocode}
5698 \tl_new:N \l_@@_replacement_category_tl
5699 \seq_new:N \l_@@_replacement_category_seq
5700 % \end{macrocode}
5701 % \end{variable}
5702 %
5703 % \begin{variable}{\g_@@_balance_tl}
5704 % This token list holds the replacement text for
5705 % \cs{@@_replacement_balance_one_match:n} while it is being built
5706 % incrementally.
5707 % \begin{macrocode}
5708 \tl_new:N \g_@@_balance_tl
5709 % \end{macrocode}
5710 % \end{variable}
5711 %
5712 % \begin{macro}[rEXP]{\@@_replacement_balance_one_match:n}
5713 % This expects as an argument the first index of a set of entries in
5714 % \cs{g_@@_submatch_begin_intarray} (and related arrays) which hold the
5715 % submatch information for a given match. It
5716 % can be used within an integer expression to obtain the brace balance
5717 % incurred by performing the replacement on that match. This combines
5718 % the braces lost by removing the match, braces added by all the
5719 % submatches appearing in the replacement, and braces appearing
5720 % explicitly in the replacement. Even though it is always redefined
5721 % before use, we initialize it as for an empty replacement. An
5722 % important property is that concatenating several calls to that
5723 % function must result in a valid integer expression (hence a leading
5724 % |+| in the actual definition).
5725 % \begin{macrocode}
5726 \cs_new:Npn \@@_replacement_balance_one_match:n #1
5727 { - \@@_submatch_balance:n {#1} }
5728 % \end{macrocode}
5729 % \end{macro}
5730 %
5731 % \begin{macro}[rEXP]{\@@_replacement_do_one_match:n}
5732 % The input is the same as \cs{@@_replacement_balance_one_match:n}.
5733 % This function is redefined to expand to the part of the token list
5734 % from the end of the previous match to a given match, followed by the
5735 % replacement text. Hence concatenating the result of this function
5736 % with all possible arguments (one call for each match), as well as
5737 % the range from the end of the last match to the end of the string,
5738 % produces the fully replaced token list. The initialization does
5739 % not matter, but (as an example) we set it as for an empty replacement.
5740 % \begin{macrocode}
5741 \cs_new:Npn \@@_replacement_do_one_match:n #1
5742 {
5743 \@@_query_range:nn
5744 { \__kernel_intarray_item:Nn \g_@@_submatch_prev_intarray {#1} }
5745 { \__kernel_intarray_item:Nn \g_@@_submatch_begin_intarray {#1} }
5746 }
5747 % \end{macrocode}
5748 % \end{macro}
5749 %
5750 % \begin{macro}{\@@_replacement_exp_not:N}
5751 % This function lets us navigate around the fact that the primitive
5752 % \cs{exp_not:n} requires a braced argument. As far as I can tell, it
5753 % is only needed if the user tries to include in the replacement text
5754 % a control sequence set equal to a macro parameter character, such as
5755 % \cs{c_parameter_token}. Indeed, within an \texttt{e}/\texttt{x}-expanding
5756 % assignment, \cs{exp_not:N}~|#| behaves as a single |#|, whereas
5757 % \cs{exp_not:n}~|{#}| behaves as a doubled |##|.
5758 % \begin{macrocode}
5759 \cs_new:Npn \@@_replacement_exp_not:N #1 { \exp_not:n {#1} }
5760 % \end{macrocode}
5761 % \end{macro}
5762 %
5763 % \begin{macro}{\@@_replacement_exp_not:V}
5764 % This is used for the implementation of~|\u|, and it gets redefined
5765 % for \cs{peek_regex_replace_once:nnTF}.
5766 % \begin{macrocode}
5767 \cs_new_eq:NN \@@_replacement_exp_not:V \exp_not:V
5768 % \end{macrocode}
5769 % \end{macro}
5770 %
5771 % \subsubsection{Query and brace balance}
5772 %
5773 % \begin{macro}[rEXP]{\@@_query_range:nn}
5774 % \begin{macro}[rEXP]{\@@_query_range_loop:ww}
5775 % When it is time to extract submatches from the token list, the
5776 % various tokens are stored in \tn{toks} registers numbered from
5777 % \cs{l_@@_min_pos_int} inclusive to \cs{l_@@_max_pos_int}
5778 % exclusive. The function \cs{@@_query_range:nn} \Arg{min}
5779 % \Arg{max} unpacks registers from the position \meta{min} to the
5780 % position $\meta{max}-1$ included. Once this is expanded, a second
5781 % \texttt{e}-expansion results in the actual tokens from the
5782 % query. That second expansion is only done by user functions at the
5783 % very end of their operation, after checking (and correcting) the
5784 % brace balance first.
5785 % \begin{macrocode}
5786 \cs_new:Npn \@@_query_range:nn #1#2
5787 {
5788 \exp_after:wN \@@_query_range_loop:ww
5789 \int_value:w \@@_int_eval:w #1 \exp_after:wN \@@_sep:
5790 \int_value:w \@@_int_eval:w #2 \@@_sep:
5791 \prg_break_point:
5792 }
5793 \cs_new:Npn \@@_query_range_loop:ww #1 \@@_sep: #2 \@@_sep:
5794 {
5795 \if_int_compare:w #1 < #2 \exp_stop_f:
5796 \else:
5797 \prg_break:n
5798 \fi:
5799 \@@_toks_use:w #1 \exp_stop_f:
5800 \exp_after:wN \@@_query_range_loop:ww
5801 \int_value:w \@@_int_eval:w #1 + \c_one_int \@@_sep: #2 \@@_sep:
5802 }
5803 % \end{macrocode}
5804 % \end{macro}
5805 % \end{macro}
5806 %
5807 % \begin{macro}{\@@_query_submatch:n}
5808 % Find the start and end positions for a given submatch (of a given match).
5809 % \begin{macrocode}
5810 \cs_new:Npn \@@_query_submatch:n #1
5811 {
5812 \@@_query_range:nn
5813 { \__kernel_intarray_item:Nn \g_@@_submatch_begin_intarray {#1} }
5814 { \__kernel_intarray_item:Nn \g_@@_submatch_end_intarray {#1} }
5815 }
5816 % \end{macrocode}
5817 % \end{macro}
5818 %
5819 % \begin{macro}{\@@_submatch_balance:n}
5820 % Every user function must result in a balanced token list (unbalanced
5821 % token lists cannot be stored by TeX). When we unpacked the query, we
5822 % kept track of the brace balance, hence the contribution from a given
5823 % range is the difference between the brace balances at the
5824 % \meta{max~pos} and \meta{min~pos}. These two positions are found in
5825 % the corresponding \enquote{submatch} arrays.
5826 % \begin{macrocode}
5827 \cs_new_protected:Npn \@@_submatch_balance:n #1
5828 {
5829 \tex_the:D \@@_int_eval:w
5830 \@@_intarray_item:NnF \g_@@_balance_intarray
5831 {
5832 \__kernel_intarray_item:Nn
5833 \g_@@_submatch_end_intarray {#1}
5834 }
5835 \c_zero_int
5836 -
5837 \@@_intarray_item:NnF \g_@@_balance_intarray
5838 {
5839 \__kernel_intarray_item:Nn
5840 \g_@@_submatch_begin_intarray {#1}
5841 }
5842 \c_zero_int
5843 \scan_stop:
5844 }
5845 % \end{macrocode}
5846 % \end{macro}
5847 %
5848 % \subsubsection{Framework}
5849 %
5850 % \begin{macro}{\@@_replacement:n, \@@_replacement:e}
5851 % \begin{macro}{\@@_replacement_apply:Nn, \@@_replacement_set:n}
5852 % The replacement text is built incrementally. We keep track in
5853 % \cs{l_@@_balance_int} of the balance of explicit begin- and
5854 % end-group tokens and we store in \cs{g_@@_balance_tl} some
5855 % code to compute the brace balance from submatches (see its
5856 % description). Detect unescaped right braces, and escaped characters,
5857 % with trailing \cs{prg_do_nothing:} because some of the later
5858 % function look-ahead. Once the whole replacement text has been
5859 % parsed, make sure that there is no open csname. Finally, define the
5860 % \texttt{balance_one_match} and \texttt{do_one_match} functions.
5861 % \begin{macrocode}
5862 \cs_new_protected:Npn \@@_replacement:n
5863 { \@@_replacement_apply:Nn \@@_replacement_set:n }
5864 \cs_new_protected:Npn \@@_replacement_apply:Nn #1#2
5865 {
5866 \group_begin:
5867 \tl_build_begin:N \l_@@_build_tl
5868 \int_zero:N \l_@@_balance_int
5869 \tl_gclear:N \g_@@_balance_tl
5870 \@@_escape_use:nnnn
5871 {
5872 \if_charcode:w \c_right_brace_str ##1
5873 \@@_replacement_rbrace:N
5874 \else:
5875 \if_charcode:w \c_left_brace_str ##1
5876 \@@_replacement_lbrace:N
5877 \else:
5878 \@@_replacement_normal:n
5879 \fi:
5880 \fi:
5881 ##1
5882 }
5883 { \@@_replacement_escaped:N ##1 }
5884 { \@@_replacement_normal:n ##1 }
5885 {#2}
5886 \prg_do_nothing: \prg_do_nothing:
5887 \if_int_compare:w \l_@@_replacement_csnames_int > \c_zero_int
5888 \msg_error:nne { regex } { replacement-missing-rbrace }
5889 { \int_use:N \l_@@_replacement_csnames_int }
5890 \tl_build_put_right:Ne \l_@@_build_tl
5891 { \prg_replicate:nn \l_@@_replacement_csnames_int \cs_end: }
5892 \fi:
5893 \seq_if_empty:NF \l_@@_replacement_category_seq
5894 {
5895 \msg_error:nne { regex } { replacement-missing-rparen }
5896 { \seq_count:N \l_@@_replacement_category_seq }
5897 \seq_clear:N \l_@@_replacement_category_seq
5898 }
5899 \tl_gput_right:Ne \g_@@_balance_tl
5900 { + \int_use:N \l_@@_balance_int }
5901 \tl_build_end:N \l_@@_build_tl
5902 \exp_args:NNo
5903 \group_end:
5904 #1 \l_@@_build_tl
5905 }
5906 \cs_generate_variant:Nn \@@_replacement:n { e }
5907 \cs_new_protected:Npn \@@_replacement_set:n #1
5908 {
5909 \cs_set:Npn \@@_replacement_do_one_match:n ##1
5910 {
5911 \@@_query_range:nn
5912 {
5913 \__kernel_intarray_item:Nn
5914 \g_@@_submatch_prev_intarray {##1}
5915 }
5916 {
5917 \__kernel_intarray_item:Nn
5918 \g_@@_submatch_begin_intarray {##1}
5919 }
5920 #1
5921 }
5922 \exp_args:Nno \use:n
5923 { \cs_gset:Npn \@@_replacement_balance_one_match:n ##1 }
5924 {
5925 \g_@@_balance_tl
5926 - \@@_submatch_balance:n {##1}
5927 }
5928 }
5929 % \end{macrocode}
5930 % \end{macro}
5931 % \end{macro}
5932 %
5933 % \begin{macro}{\@@_case_replacement:n, \@@_case_replacement:e}
5934 % \begin{macrocode}
5935 \tl_new:N \g_@@_case_replacement_tl
5936 \tl_new:N \g_@@_case_balance_tl
5937 \cs_new_protected:Npn \@@_case_replacement:n #1
5938 {
5939 \tl_gset:Nn \g_@@_case_balance_tl
5940 {
5941 \if_case:w
5942 \__kernel_intarray_item:Nn
5943 \g_@@_submatch_case_intarray {##1}
5944 }
5945 \tl_gset_eq:NN \g_@@_case_replacement_tl \g_@@_case_balance_tl
5946 \tl_map_tokens:nn {#1}
5947 { \@@_replacement_apply:Nn \@@_case_replacement_aux:n }
5948 \tl_gset:No \g_@@_balance_tl
5949 { \g_@@_case_balance_tl \fi: }
5950 \exp_args:No \@@_replacement_set:n
5951 { \g_@@_case_replacement_tl \fi: }
5952 }
5953 \cs_generate_variant:Nn \@@_case_replacement:n { e }
5954 \cs_new_protected:Npn \@@_case_replacement_aux:n #1
5955 {
5956 \tl_gput_right:Nn \g_@@_case_replacement_tl { \or: #1 }
5957 \tl_gput_right:No \g_@@_case_balance_tl
5958 { \exp_after:wN \or: \g_@@_balance_tl }
5959 }
5960 % \end{macrocode}
5961 % \end{macro}
5962 %
5963 % \begin{macro}{\@@_replacement_put:n}
5964 % This gets redefined for \cs{peek_regex_replace_once:nnTF}.
5965 % \begin{macrocode}
5966 \cs_new_protected:Npn \@@_replacement_put:n
5967 { \tl_build_put_right:Nn \l_@@_build_tl }
5968 % \end{macrocode}
5969 % \end{macro}
5970 %
5971 % \begin{macro}{\@@_replacement_normal:n, \@@_replacement_normal_aux:N}
5972 % Most characters are simply sent to the output by
5973 % \cs{tl_build_put_right:Nn}, unless a particular category code has been
5974 % requested: then \cs{@@_replacement_c_A:w} or a similar auxiliary is
5975 % called. One exception is right parentheses, which restore the
5976 % category code in place before the group started. Note that the
5977 % sequence is non-empty there: it contains an empty entry
5978 % corresponding to the initial value of
5979 % \cs{l_@@_replacement_category_tl}.
5980 % The argument |#1| is a single character (including the case of a catcode-other space).
5981 % In case no specific catcode is requested, we take into account the
5982 % current catcode régime (at the time the replacement is performed)
5983 % as much as reasonable, with all impossible catcodes (escape,
5984 % newline, etc.) being mapped to \enquote{other}.
5985 % \begin{macrocode}
5986 \cs_new_protected:Npn \@@_replacement_normal:n #1
5987 {
5988 \int_compare:nNnTF \l_@@_replacement_csnames_int > \c_zero_int
5989 { \exp_args:No \@@_replacement_put:n { \token_to_str:N #1 } }
5990 {
5991 \tl_if_empty:NTF \l_@@_replacement_category_tl
5992 { \@@_replacement_normal_aux:N #1 }
5993 { % (
5994 \token_if_eq_charcode:NNTF #1 )
5995 {
5996 \seq_pop:NN \l_@@_replacement_category_seq
5997 \l_@@_replacement_category_tl
5998 }
5999 {
6000 \use:c { @@_replacement_c_ \l_@@_replacement_category_tl :w }
6001 ? #1
6002 }
6003 }
6004 }
6005 }
6006 \cs_new_protected:Npn \@@_replacement_normal_aux:N #1
6007 {
6008 \token_if_eq_charcode:NNTF #1 \c_space_token
6009 { \@@_replacement_c_S:w }
6010 {
6011 \exp_after:wN \exp_after:wN
6012 \if_case:w \tex_catcode:D `#1 \exp_stop_f:
6013 \@@_replacement_c_O:w
6014 \or: \@@_replacement_c_B:w
6015 \or: \@@_replacement_c_E:w
6016 \or: \@@_replacement_c_M:w
6017 \or: \@@_replacement_c_T:w
6018 \or: \@@_replacement_c_O:w
6019 \or: \@@_replacement_c_P:w
6020 \or: \@@_replacement_c_U:w
6021 \or: \@@_replacement_c_D:w
6022 \or: \@@_replacement_c_O:w
6023 \or: \@@_replacement_c_S:w
6024 \or: \@@_replacement_c_L:w
6025 \or: \@@_replacement_c_O:w
6026 \or: \@@_replacement_c_A:w
6027 \else: \@@_replacement_c_O:w
6028 \fi:
6029 }
6030 ? #1
6031 }
6032 % \end{macrocode}
6033 % \end{macro}
6034 %
6035 % \begin{macro}{\@@_replacement_escaped:N}
6036 % As in parsing a regular expression, we use an auxiliary built from
6037 % |#1| if defined. Otherwise, check for escaped digits (standing from
6038 % submatches from $0$ to $9$): anything else is a raw character.
6039 % \begin{macrocode}
6040 \cs_new_protected:Npn \@@_replacement_escaped:N #1
6041 {
6042 \cs_if_exist_use:cF { @@_replacement_#1:w }
6043 {
6044 \if_int_compare:w \c_one_int < 1#1 \exp_stop_f:
6045 \@@_replacement_put_submatch:n {#1}
6046 \else:
6047 \@@_replacement_normal:n {#1}
6048 \fi:
6049 }
6050 }
6051 % \end{macrocode}
6052 % \end{macro}
6053 %
6054 % \subsubsection{Submatches}
6055 %
6056 % \begin{macro}{\@@_replacement_put_submatch:n, \@@_replacement_put_submatch_aux:n}
6057 % Insert a submatch in the replacement text. This is dropped if the
6058 % submatch number is larger than the number of capturing groups.
6059 % Unless the submatch appears inside a |\c{...}| or |\u{...}|
6060 % construction, it must be taken into account in the brace balance.
6061 % Later on, |##1| will be replaced by a pointer to the $0$-th submatch for a
6062 % given match.
6063 % \begin{macrocode}
6064 \cs_new_protected:Npn \@@_replacement_put_submatch:n #1
6065 {
6066 \if_int_compare:w #1 < \l_@@_capturing_group_int
6067 \@@_replacement_put_submatch_aux:n {#1}
6068 \else:
6069 \msg_expandable_error:nnff { regex } { submatch-too-big }
6070 {#1} { \int_eval:n { \l_@@_capturing_group_int - \c_one_int } }
6071 \fi:
6072 }
6073 \cs_new_protected:Npn \@@_replacement_put_submatch_aux:n #1
6074 {
6075 \tl_build_put_right:Nn \l_@@_build_tl
6076 { \@@_query_submatch:n { \@@_int_eval:w #1 + ##1 \scan_stop: } }
6077 \if_int_compare:w \l_@@_replacement_csnames_int = \c_zero_int
6078 \tl_gput_right:Nn \g_@@_balance_tl
6079 { + \@@_submatch_balance:n { \@@_int_eval:w #1 + ##1 \scan_stop: } }
6080 \fi:
6081 }
6082 % \end{macrocode}
6083 % \end{macro}
6084 %
6085 % \begin{macro}{\@@_replacement_g:w}
6086 % \begin{macro}[rEXP]{\@@_replacement_g_digits:NN}
6087 % Grab digits for the |\g| escape sequence in a primitive assignment
6088 % to the integer \cs{l_@@_tmpa_int}. At the end of the run of
6089 % digits, check that it ends with a right brace.
6090 % \begin{macrocode}
6091 \cs_new_protected:Npn \@@_replacement_g:w #1#2
6092 {
6093 \token_if_eq_meaning:NNTF #1 \@@_replacement_lbrace:N
6094 { \l_@@_tmpa_int = \@@_replacement_g_digits:NN }
6095 { \@@_replacement_error:NNN g #1 #2 }
6096 }
6097 \cs_new:Npn \@@_replacement_g_digits:NN #1#2
6098 {
6099 \token_if_eq_meaning:NNTF #1 \@@_replacement_normal:n
6100 {
6101 \if_int_compare:w \c_one_int < 1#2 \exp_stop_f:
6102 #2
6103 \exp_after:wN \use_i:nnn
6104 \exp_after:wN \@@_replacement_g_digits:NN
6105 \else:
6106 \exp_stop_f:
6107 \exp_after:wN \@@_replacement_error:NNN
6108 \exp_after:wN g
6109 \fi:
6110 }
6111 {
6112 \exp_stop_f:
6113 \if_meaning:w \@@_replacement_rbrace:N #1
6114 \exp_args:No \@@_replacement_put_submatch:n
6115 { \int_use:N \l_@@_tmpa_int }
6116 \exp_after:wN \use_none:nn
6117 \else:
6118 \exp_after:wN \@@_replacement_error:NNN
6119 \exp_after:wN g
6120 \fi:
6121 }
6122 #1 #2
6123 }
6124 % \end{macrocode}
6125 % \end{macro}
6126 % \end{macro}
6127 %
6128 % \subsubsection{Csnames in replacement}
6129 %
6130 % \begin{macro}{\@@_replacement_c:w}
6131 % |\c| may only be followed by an unescaped character. If followed by
6132 % a left brace, start a control sequence by calling an auxiliary
6133 % common with |\u|. Otherwise test whether the category is known; if
6134 % it is not, complain.
6135 % \begin{macrocode}
6136 \cs_new_protected:Npn \@@_replacement_c:w #1#2
6137 {
6138 \token_if_eq_meaning:NNTF #1 \@@_replacement_normal:n
6139 {
6140 \cs_if_exist:cTF { @@_replacement_c_#2:w }
6141 { \@@_replacement_cat:NNN #2 }
6142 { \@@_replacement_error:NNN c #1#2 }
6143 }
6144 {
6145 \token_if_eq_meaning:NNTF #1 \@@_replacement_lbrace:N
6146 { \@@_replacement_cu_aux:Nw \@@_replacement_exp_not:N }
6147 { \@@_replacement_error:NNN c #1#2 }
6148 }
6149 }
6150 % \end{macrocode}
6151 % \end{macro}
6152 %
6153 % \begin{macro}{\@@_replacement_cu_aux:Nw}
6154 % Start a control sequence with \cs{cs:w}, protected
6155 % from expansion by |#1| (either \cs{@@_replacement_exp_not:N} or
6156 % \cs{exp_not:V}), or turned to a string by \cs{tl_to_str:V} if inside
6157 % another csname construction |\c| or |\u|. We use \cs{tl_to_str:V}
6158 % rather than \cs{tl_to_str:N} to deal with integers and other
6159 % registers.
6160 % \begin{macrocode}
6161 \cs_new_protected:Npn \@@_replacement_cu_aux:Nw #1
6162 {
6163 \if_case:w \l_@@_replacement_csnames_int
6164 \tl_build_put_right:Nn \l_@@_build_tl
6165 { \exp_not:n { \exp_after:wN #1 \cs:w } }
6166 \else:
6167 \tl_build_put_right:Nn \l_@@_build_tl
6168 { \exp_not:n { \exp_after:wN \tl_to_str:V \cs:w } }
6169 \fi:
6170 \int_incr:N \l_@@_replacement_csnames_int
6171 }
6172 % \end{macrocode}
6173 % \end{macro}
6174 %
6175 % \begin{macro}{\@@_replacement_u:w}
6176 % Check that |\u| is followed by a left brace. If so, start a control
6177 % sequence with \cs{cs:w}, which is then unpacked either with
6178 % \cs{exp_not:V} or \cs{tl_to_str:V} depending on the current context.
6179 % \begin{macrocode}
6180 \cs_new_protected:Npn \@@_replacement_u:w #1#2
6181 {
6182 \token_if_eq_meaning:NNTF #1 \@@_replacement_lbrace:N
6183 { \@@_replacement_cu_aux:Nw \@@_replacement_exp_not:V }
6184 { \@@_replacement_error:NNN u #1#2 }
6185 }
6186 % \end{macrocode}
6187 % \end{macro}
6188 %
6189 % \begin{macro}{\@@_replacement_rbrace:N}
6190 % Within a |\c{...}| or |\u{...}| construction, end the control
6191 % sequence, and decrease the brace count. Otherwise, this is a raw
6192 % right brace.
6193 % \begin{macrocode}
6194 \cs_new_protected:Npn \@@_replacement_rbrace:N #1
6195 {
6196 \if_int_compare:w \l_@@_replacement_csnames_int > \c_zero_int
6197 \tl_build_put_right:Nn \l_@@_build_tl { \cs_end: }
6198 \int_decr:N \l_@@_replacement_csnames_int
6199 \else:
6200 \@@_replacement_normal:n {#1}
6201 \fi:
6202 }
6203 % \end{macrocode}
6204 % \end{macro}
6205 %
6206 % \begin{macro}{\@@_replacement_lbrace:N}
6207 % Within a |\c{...}| or |\u{...}| construction, this is
6208 % forbidden. Otherwise, this is a raw left brace.
6209 % \begin{macrocode}
6210 \cs_new_protected:Npn \@@_replacement_lbrace:N #1
6211 {
6212 \if_int_compare:w \l_@@_replacement_csnames_int > \c_zero_int
6213 \msg_error:nnn { regex } { cu-lbrace } { u }
6214 \else:
6215 \@@_replacement_normal:n {#1}
6216 \fi:
6217 }
6218 % \end{macrocode}
6219 % \end{macro}
6220 %
6221 % \subsubsection{Characters in replacement}
6222 %
6223 % \begin{macro}{\@@_replacement_cat:NNN}
6224 % Here, |#1| is a letter among |BEMTPUDSLOA| and |#2#3| denote the
6225 % next character. Complain if we reach the end of the replacement or
6226 % if the construction appears inside |\c{|\ldots{}|}| or
6227 % |\u{|\ldots{}|}|, and detect the case of a parenthesis. In that
6228 % case, store the current category in a sequence and switch to a new
6229 % one.
6230 % \begin{macrocode}
6231 \cs_new_protected:Npn \@@_replacement_cat:NNN #1#2#3
6232 {
6233 \token_if_eq_meaning:NNTF \prg_do_nothing: #3
6234 { \msg_error:nn { regex } { replacement-catcode-end } }
6235 {
6236 \int_compare:nNnTF \l_@@_replacement_csnames_int > \c_zero_int
6237 {
6238 \msg_error:nnnn
6239 { regex } { replacement-catcode-in-cs } {#1} {#3}
6240 #2 #3
6241 }
6242 {
6243 \@@_two_if_eq:NNNNTF #2 #3 \@@_replacement_normal:n (
6244 {
6245 \seq_push:NV \l_@@_replacement_category_seq
6246 \l_@@_replacement_category_tl
6247 \tl_set:Nn \l_@@_replacement_category_tl {#1}
6248 }
6249 {
6250 \token_if_eq_meaning:NNT #2 \@@_replacement_escaped:N
6251 {
6252 \@@_char_if_alphanumeric:NTF #3
6253 {
6254 \msg_error:nnnn
6255 { regex } { replacement-catcode-escaped }
6256 {#1} {#3}
6257 }
6258 { }
6259 }
6260 \use:c { @@_replacement_c_#1:w } #2 #3
6261 }
6262 }
6263 }
6264 }
6265 % \end{macrocode}
6266 % \end{macro}
6267 %
6268 % We now need to change the category code of the null character many
6269 % times, hence work in a group. The catcode-specific macros below are
6270 % defined in alphabetical order; if you are trying to understand the
6271 % code, start from the end of the alphabet as those categories are
6272 % simpler than active or begin-group.
6273 % \begin{macrocode}
6274 \group_begin:
6275 % \end{macrocode}
6276 %
6277 % \begin{macro}{\@@_replacement_char:nNN}
6278 % The only way to produce an arbitrary character--catcode pair is to
6279 % use the \tn{lowercase} or \tn{uppercase} primitives. This is a
6280 % wrapper for our purposes. The first argument is the null character
6281 % with various catcodes. The second and third arguments are grabbed
6282 % from the input stream: |#3| is the character whose character code to
6283 % reproduce. We could use \cs{char_generate:nn} but only for some
6284 % catcodes (active characters and spaces are not supported).
6285 % \begin{macrocode}
6286 \cs_new_protected:Npn \@@_replacement_char:nNN #1#2#3
6287 {
6288 \tex_lccode:D \c_zero_int = `#3 \scan_stop:
6289 \tex_lowercase:D { \@@_replacement_put:n {#1} }
6290 }
6291 % \end{macrocode}
6292 % \end{macro}
6293 %
6294 % \begin{macro}{\@@_replacement_c_A:w}
6295 % For an active character, expansion must be avoided, twice because we
6296 % later do two \texttt{e}-expansions, to unpack \tn{toks} for the
6297 % query, and to expand their contents to tokens of the query.
6298 % \begin{macrocode}
6299 \char_set_catcode_active:N \^^@
6300 \cs_new_protected:Npn \@@_replacement_c_A:w
6301 { \@@_replacement_char:nNN { \exp_not:n { \exp_not:N ^^@ } } }
6302 % \end{macrocode}
6303 % \end{macro}
6304 %
6305 % \begin{macro}{\@@_replacement_c_B:w}
6306 % An explicit begin-group token increases the balance, unless within a
6307 % |\c{...}| or |\u{...}| construction. Add the desired begin-group
6308 % character, using the standard \cs{if_false:} trick. We eventually
6309 % \texttt{e}-expand twice. The first time must yield a balanced token
6310 % list, and the second one gives the bare begin-group token. The
6311 % \cs{exp_after:wN} is not strictly needed, but is more consistent
6312 % with \pkg{l3tl-analysis}.
6313 % \begin{macrocode}
6314 \char_set_catcode_group_begin:N \^^@
6315 \cs_new_protected:Npn \@@_replacement_c_B:w
6316 {
6317 \if_int_compare:w \l_@@_replacement_csnames_int = \c_zero_int
6318 \int_incr:N \l_@@_balance_int
6319 \fi:
6320 \@@_replacement_char:nNN
6321 { \exp_not:n { \exp_after:wN ^^@ \if_false: } \fi: } }
6322 }
6323 % \end{macrocode}
6324 % \end{macro}
6325 %
6326 % \begin{macro}{\@@_replacement_c_C:w}
6327 % This is not quite catcode-related: when the user requests a
6328 % character with category \enquote{control sequence}, the
6329 % one-character control symbol is returned. As for the active
6330 % character, we prepare for two \texttt{e}-expansions.
6331 % \begin{macrocode}
6332 \cs_new_protected:Npn \@@_replacement_c_C:w #1#2
6333 {
6334 \tl_build_put_right:Nn \l_@@_build_tl
6335 { \exp_not:N \@@_replacement_exp_not:N \exp_not:c {#2} }
6336 }
6337 % \end{macrocode}
6338 % \end{macro}
6339 %
6340 % \begin{macro}{\@@_replacement_c_D:w}
6341 % Subscripts fit the mold: \tn{lowercase} the null byte with the
6342 % correct category.
6343 % \begin{macrocode}
6344 \char_set_catcode_math_subscript:N \^^@
6345 \cs_new_protected:Npn \@@_replacement_c_D:w
6346 { \@@_replacement_char:nNN { ^^@ } }
6347 % \end{macrocode}
6348 % \end{macro}
6349 %
6350 % \begin{macro}{\@@_replacement_c_E:w}
6351 % Similar to the begin-group case, the second \texttt{e}-expansion
6352 % produces the bare end-group token.
6353 % \begin{macrocode}
6354 \char_set_catcode_group_end:N \^^@
6355 \cs_new_protected:Npn \@@_replacement_c_E:w
6356 {
6357 \if_int_compare:w \l_@@_replacement_csnames_int = \c_zero_int
6358 \int_decr:N \l_@@_balance_int
6359 \fi:
6360 \@@_replacement_char:nNN
6361 { \exp_not:n { \if_false: { \fi: ^^@ } }
6362 }
6363 % \end{macrocode}
6364 % \end{macro}
6365 %
6366 % \begin{macro}{\@@_replacement_c_L:w}
6367 % Simply \tn{lowercase} a letter null byte to produce an arbitrary letter.
6368 % \begin{macrocode}
6369 \char_set_catcode_letter:N \^^@
6370 \cs_new_protected:Npn \@@_replacement_c_L:w
6371 { \@@_replacement_char:nNN { ^^@ } }
6372 % \end{macrocode}
6373 % \end{macro}
6374 %
6375 % \begin{macro}{\@@_replacement_c_M:w}
6376 % No surprise here, we lowercase the null math toggle.
6377 % \begin{macrocode}
6378 \char_set_catcode_math_toggle:N \^^@
6379 \cs_new_protected:Npn \@@_replacement_c_M:w
6380 { \@@_replacement_char:nNN { ^^@ } }
6381 % \end{macrocode}
6382 % \end{macro}
6383 %
6384 % \begin{macro}{\@@_replacement_c_O:w}
6385 % Lowercase an other null byte.
6386 % \begin{macrocode}
6387 \char_set_catcode_other:N \^^@
6388 \cs_new_protected:Npn \@@_replacement_c_O:w
6389 { \@@_replacement_char:nNN { ^^@ } }
6390 % \end{macrocode}
6391 % \end{macro}
6392 %
6393 % \begin{macro}{\@@_replacement_c_P:w}
6394 % For macro parameters, expansion is a tricky issue. We need to
6395 % prepare for two \texttt{e}-expansions and passing through various
6396 % macro definitions. Note that we cannot replace one \cs{exp_not:n} by
6397 % doubling the macro parameter characters because this would misbehave
6398 % if a mischievous user asks for |\c{\cP\#}|, since that macro
6399 % parameter character would be doubled.
6400 % \begin{macrocode}
6401 \char_set_catcode_parameter:N \^^@
6402 \cs_new_protected:Npn \@@_replacement_c_P:w
6403 {
6404 \@@_replacement_char:nNN
6405 { \exp_not:n { \exp_not:n { ^^@^^@^^@^^@ } } }
6406 }
6407 % \end{macrocode}
6408 % \end{macro}
6409 %
6410 % \begin{macro}{\@@_replacement_c_S:w}
6411 % Spaces are normalized on input by \TeX{} to have character code
6412 % $32$. It is in fact impossible to get a token with character code
6413 % $0$ and category code $10$. Hence we use $32$ instead of $0$ as our
6414 % base character.
6415 % \begin{macrocode}
6416 \cs_new_protected:Npn \@@_replacement_c_S:w #1#2
6417 {
6418 \if_int_compare:w `#2 = \c_zero_int
6419 \msg_error:nn { regex } { replacement-null-space }
6420 \fi:
6421 \tex_lccode:D `\ = `#2 \scan_stop:
6422 \tex_lowercase:D { \@@_replacement_put:n {~} }
6423 }
6424 % \end{macrocode}
6425 % \end{macro}
6426 %
6427 % \begin{macro}{\@@_replacement_c_T:w}
6428 % No surprise for alignment tabs here. Those are surrounded by the
6429 % appropriate braces whenever necessary, hence they don't cause
6430 % trouble in alignment settings.
6431 % \begin{macrocode}
6432 \char_set_catcode_alignment:N \^^@
6433 \cs_new_protected:Npn \@@_replacement_c_T:w
6434 { \@@_replacement_char:nNN { ^^@ } }
6435 % \end{macrocode}
6436 % \end{macro}
6437 %
6438 % \begin{macro}{\@@_replacement_c_U:w}
6439 % Simple call to \cs{@@_replacement_char:nNN} which lowercases the
6440 % math superscript |^^@|.
6441 % \begin{macrocode}
6442 \char_set_catcode_math_superscript:N \^^@
6443 \cs_new_protected:Npn \@@_replacement_c_U:w
6444 { \@@_replacement_char:nNN { ^^@ } }
6445 % \end{macrocode}
6446 % \end{macro}
6447 %
6448 % Restore the catcode of the null byte.
6449 % \begin{macrocode}
6450 \group_end:
6451 % \end{macrocode}
6452 %
6453 % \subsubsection{An error}
6454 %
6455 % \begin{macro}{\@@_replacement_error:NNN}
6456 % Simple error reporting by calling one of the messages
6457 % \texttt{replacement-c}, \texttt{replacement-g}, or
6458 % \texttt{replacement-u}.
6459 % \begin{macrocode}
6460 \cs_new_protected:Npn \@@_replacement_error:NNN #1#2#3
6461 {
6462 \msg_error:nne { regex } { replacement-#1 } {#3}
6463 #2 #3
6464 }
6465 % \end{macrocode}
6466 % \end{macro}
6467 %
6468 % \subsection{User functions}
6469 %
6470 % \begin{macro}{\regex_new:N}
6471 % Before being assigned a sensible value, a regex variable matches
6472 % nothing.
6473 % \begin{macrocode}
6474 \cs_new_protected:Npn \regex_new:N #1
6475 { \cs_new_eq:NN #1 \c_@@_no_match_regex }
6476 % \end{macrocode}
6477 % \end{macro}
6478 %
6479 % \begin{variable}{\l_tmpa_regex, \l_tmpb_regex, \g_tmpa_regex, \g_tmpb_regex}
6480 % The usual scratch space.
6481 % \begin{macrocode}
6482 \regex_new:N \l_tmpa_regex
6483 \regex_new:N \l_tmpb_regex
6484 \regex_new:N \g_tmpa_regex
6485 \regex_new:N \g_tmpb_regex
6486 % \end{macrocode}
6487 % \end{variable}
6488 %
6489 % \begin{macro}{\regex_set:Nn, \regex_gset:Nn, \regex_const:Nn}
6490 % Compile, then store the result in the user variable with the
6491 % appropriate assignment function.
6492 % \begin{macrocode}
6493 \cs_new_protected:Npn \regex_set:Nn #1#2
6494 {
6495 \@@_compile:n {#2}
6496 \tl_set_eq:NN #1 \l_@@_tmp_regex
6497 }
6498 \cs_new_protected:Npn \regex_gset:Nn #1#2
6499 {
6500 \@@_compile:n {#2}
6501 \tl_gset_eq:NN #1 \l_@@_tmp_regex
6502 }
6503 \cs_new_protected:Npn \regex_const:Nn #1#2
6504 {
6505 \@@_compile:n {#2}
6506 \tl_const:Ne #1 { \exp_not:o \l_@@_tmp_regex }
6507 }
6508 % \end{macrocode}
6509 % \end{macro}
6510 %
6511 % \begin{macro}
6512 % {
6513 % \regex_show:n, \regex_log:n, \@@_show:Nn,
6514 % \regex_show:N, \regex_log:N, \@@_show:NN
6515 % }
6516 % User functions: the \texttt{n} variant requires compilation first.
6517 % Then show the variable with some appropriate text. The auxiliary
6518 % \cs{@@_show:N} is defined in a different section.
6519 % \begin{macrocode}
6520 \cs_new_protected:Npn \regex_show:n { \@@_show:Nn \msg_show:nneeee }
6521 \cs_new_protected:Npn \regex_log:n { \@@_show:Nn \msg_log:nneeee }
6522 \cs_new_protected:Npn \@@_show:Nn #1#2
6523 {
6524 \@@_compile:n {#2}
6525 \@@_show:N \l_@@_tmp_regex
6526 #1 { regex } { show }
6527 { \tl_to_str:n {#2} } { }
6528 { \l_@@_tmpa_tl } { }
6529 }
6530 \cs_new_protected:Npn \regex_show:N { \@@_show:NN \msg_show:nneeee }
6531 \cs_new_protected:Npn \regex_log:N { \@@_show:NN \msg_log:nneeee }
6532 \cs_new_protected:Npn \@@_show:NN #1#2
6533 {
6534 \__kernel_chk_tl_type:NnnT #2 { regex }
6535 { \exp_args:No \@@_clean_regex:n {#2} }
6536 {
6537 \@@_show:N #2
6538 #1 { regex } { show }
6539 { } { \token_to_str:N #2 }
6540 { \l_@@_tmpa_tl } { }
6541 }
6542 }
6543 % \end{macrocode}
6544 % \end{macro}
6545 %
6546 % \begin{macro}[TF]
6547 % {
6548 % \regex_if_match:nn, \regex_if_match:nV,
6549 % \regex_if_match:Nn, \regex_if_match:NV
6550 % }
6551 % Those conditionals are based on a common auxiliary defined
6552 % later. Its first argument builds the \textsc{nfa} corresponding to
6553 % the regex, and the second argument is the query token list. Once we
6554 % have performed the match, convert the resulting boolean to
6555 % \cs{prg_return_true:} or \texttt{false}.
6556 % \begin{macrocode}
6557 \prg_new_protected_conditional:Npnn \regex_if_match:nn #1#2 { T , F , TF }
6558 {
6559 \@@_if_match:nn { \@@_build:n {#1} } {#2}
6560 \@@_return:
6561 }
6562 \prg_generate_conditional_variant:Nnn \regex_if_match:nn { nV } { T , F , TF }
6563 \prg_new_protected_conditional:Npnn \regex_if_match:Nn #1#2 { T , F , TF }
6564 {
6565 \@@_if_match:nn { \@@_build:N #1 } {#2}
6566 \@@_return:
6567 }
6568 \prg_generate_conditional_variant:Nnn \regex_if_match:Nn { NV } { T , F , TF }
6569 % \end{macrocode}
6570 % \end{macro}
6571 %
6572 % \begin{macro}
6573 % {\regex_count:nnN, \regex_count:nVN, \regex_count:NnN, \regex_count:NVN}
6574 % Again, use an auxiliary whose first argument builds the \textsc{nfa}.
6575 % \begin{macrocode}
6576 \cs_new_protected:Npn \regex_count:nnN #1
6577 { \@@_count:nnN { \@@_build:n {#1} } }
6578 \cs_new_protected:Npn \regex_count:NnN #1
6579 { \@@_count:nnN { \@@_build:N #1 } }
6580 \cs_generate_variant:Nn \regex_count:nnN { nV }
6581 \cs_generate_variant:Nn \regex_count:NnN { NV }
6582 % \end{macrocode}
6583 % \end{macro}
6584 %
6585 % \begin{macro}[noTF]{\regex_match_case:nn}
6586 % The auxiliary errors if |#1| has an odd number of items, and
6587 % otherwise it sets \cs{g_@@_case_int} according to which case was
6588 % found (zero if not found). The \texttt{true} branch leaves the
6589 % corresponding code in the input stream.
6590 % \begin{macrocode}
6591 \cs_new_protected:Npn \regex_match_case:nnTF #1#2#3
6592 {
6593 \@@_match_case:nnTF {#1} {#2}
6594 {
6595 \tl_item:nn {#1} { 2 * \g_@@_case_int }
6596 #3
6597 }
6598 }
6599 \cs_new_protected:Npn \regex_match_case:nn #1#2
6600 { \regex_match_case:nnTF {#1} {#2} { } { } }
6601 \cs_new_protected:Npn \regex_match_case:nnT #1#2#3
6602 { \regex_match_case:nnTF {#1} {#2} {#3} { } }
6603 \cs_new_protected:Npn \regex_match_case:nnF #1#2
6604 { \regex_match_case:nnTF {#1} {#2} { } }
6605 % \end{macrocode}
6606 % \end{macro}
6607 %
6608 % \begin{macro}[noTF]
6609 % {
6610 % \regex_extract_once:nnN, \regex_extract_once:nVN,
6611 % \regex_extract_once:NnN, \regex_extract_once:NVN,
6612 % \regex_extract_all:nnN, \regex_extract_all:nVN,
6613 % \regex_extract_all:NnN, \regex_extract_all:NVN,
6614 % \regex_replace_once:nnN, \regex_replace_once:nVN,
6615 % \regex_replace_once:NnN, \regex_replace_once:NVN,
6616 % \regex_replace_all:nnN, \regex_replace_all:nVN,
6617 % \regex_replace_all:NnN, \regex_replace_all:NVN,
6618 % \regex_split:NnN, \regex_split:NVN,
6619 % \regex_split:nnN, \regex_split:nVN
6620 % }
6621 % We define here $40$ user functions, following a common pattern in
6622 % terms of \texttt{:nnN} auxiliaries, defined in the coming
6623 % subsections. The auxiliary is handed \cs{@@_build:n} or
6624 % \cs{@@_build:N} with the appropriate regex argument, then all
6625 % other necessary arguments (replacement text, token list, etc.
6626 % The conditionals call \cs{@@_return:} to return either
6627 % \texttt{true} or \texttt{false} once matching has been performed.
6628 % \begin{macrocode}
6629 \cs_set_protected:Npn \@@_tmp:w #1#2#3
6630 {
6631 \cs_new_protected:Npn #2 ##1 { #1 { \@@_build:n {##1} } }
6632 \cs_new_protected:Npn #3 ##1 { #1 { \@@_build:N ##1 } }
6633 \prg_new_protected_conditional:Npnn #2 ##1##2##3 { T , F , TF }
6634 { #1 { \@@_build:n {##1} } {##2} ##3 \@@_return: }
6635 \prg_new_protected_conditional:Npnn #3 ##1##2##3 { T , F , TF }
6636 { #1 { \@@_build:N ##1 } {##2} ##3 \@@_return: }
6637 \cs_generate_variant:Nn #2 { nV }
6638 \prg_generate_conditional_variant:Nnn #2 { nV } { T , F , TF }
6639 \cs_generate_variant:Nn #3 { NV }
6640 \prg_generate_conditional_variant:Nnn #3 { NV } { T , F , TF }
6641 }
6642 \@@_tmp:w \@@_extract_once:nnN
6643 \regex_extract_once:nnN \regex_extract_once:NnN
6644 \@@_tmp:w \@@_extract_all:nnN
6645 \regex_extract_all:nnN \regex_extract_all:NnN
6646 \@@_tmp:w \@@_replace_once:nnN
6647 \regex_replace_once:nnN \regex_replace_once:NnN
6648 \@@_tmp:w \@@_replace_all:nnN
6649 \regex_replace_all:nnN \regex_replace_all:NnN
6650 \@@_tmp:w \@@_split:nnN \regex_split:nnN \regex_split:NnN
6651 % \end{macrocode}
6652 % \end{macro}
6653 %
6654 % \begin{macro}[noTF]{\regex_replace_case_once:nN}
6655 % If the input is bad (odd number of items) then take the false
6656 % branch. Otherwise, use the same auxiliary as
6657 % \cs{regex_replace_once:nnN}, but with more complicated code to build
6658 % the automaton, and to find what replacement text to use. The
6659 % \cs{tl_item:nn} is only expanded once we know the value of
6660 % \cs{g_@@_case_int}, namely which case matched.
6661 % \begin{macrocode}
6662 \cs_new_protected:Npn \regex_replace_case_once:nNTF #1#2
6663 {
6664 \int_if_odd:nTF { \tl_count:n {#1} }
6665 {
6666 \msg_error:nneeee { regex } { case-odd }
6667 { \token_to_str:N \regex_replace_case_once:nN(TF) } { code }
6668 { \tl_count:n {#1} } { \tl_to_str:n {#1} }
6669 \use_ii:nn
6670 }
6671 {
6672 \@@_replace_once_aux:nnN
6673 { \@@_case_build:e { \@@_tl_odd_items:n {#1} } }
6674 { \@@_replacement:e { \tl_item:nn {#1} { 2 * \g_@@_case_int } } }
6675 #2
6676 \bool_if:NTF \g_@@_success_bool
6677 }
6678 }
6679 \cs_new_protected:Npn \regex_replace_case_once:nN #1#2
6680 { \regex_replace_case_once:nNTF {#1} {#2} { } { } }
6681 \cs_new_protected:Npn \regex_replace_case_once:nNT #1#2#3
6682 { \regex_replace_case_once:nNTF {#1} {#2} {#3} { } }
6683 \cs_new_protected:Npn \regex_replace_case_once:nNF #1#2
6684 { \regex_replace_case_once:nNTF {#1} {#2} { } }
6685 % \end{macrocode}
6686 % \end{macro}
6687 %
6688 % \begin{macro}[noTF]{\regex_replace_case_all:nN}
6689 % If the input is bad (odd number of items) then take the false
6690 % branch. Otherwise, use the same auxiliary as
6691 % \cs{regex_replace_all:nnN}, but with more complicated code to build
6692 % the automaton, and to find what replacement text to use.
6693 % \begin{macrocode}
6694 \cs_new_protected:Npn \regex_replace_case_all:nNTF #1#2
6695 {
6696 \int_if_odd:nTF { \tl_count:n {#1} }
6697 {
6698 \msg_error:nneeee { regex } { case-odd }
6699 { \token_to_str:N \regex_replace_case_all:nN(TF) } { code }
6700 { \tl_count:n {#1} } { \tl_to_str:n {#1} }
6701 \use_ii:nn
6702 }
6703 {
6704 \@@_replace_all_aux:nnN
6705 { \@@_case_build:e { \@@_tl_odd_items:n {#1} } }
6706 { \@@_case_replacement:e { \@@_tl_even_items:n {#1} } }
6707 #2
6708 \bool_if:NTF \g_@@_success_bool
6709 }
6710 }
6711 \cs_new_protected:Npn \regex_replace_case_all:nN #1#2
6712 { \regex_replace_case_all:nNTF {#1} {#2} { } { } }
6713 \cs_new_protected:Npn \regex_replace_case_all:nNT #1#2#3
6714 { \regex_replace_case_all:nNTF {#1} {#2} {#3} { } }
6715 \cs_new_protected:Npn \regex_replace_case_all:nNF #1#2
6716 { \regex_replace_case_all:nNTF {#1} {#2} { } }
6717 % \end{macrocode}
6718 % \end{macro}
6719 %
6720 % \subsubsection{Variables and helpers for user functions}
6721 %
6722 % \begin{variable}{\l_@@_match_count_int}
6723 % The number of matches found so far is stored
6724 % in \cs{l_@@_match_count_int}. This is only used
6725 % in the \cs{regex_count:nnN} functions.
6726 % \begin{macrocode}
6727 \int_new:N \l_@@_match_count_int
6728 % \end{macrocode}
6729 % \end{variable}
6730 %
6731 % \begin{variable}{\l_@@_begin_flag, \l_@@_end_flag}
6732 % Those flags are raised to indicate begin-group or end-group tokens
6733 % that had to be added when extracting submatches.
6734 % \begin{macrocode}
6735 \flag_new:N \l_@@_begin_flag
6736 \flag_new:N \l_@@_end_flag
6737 % \end{macrocode}
6738 % \end{variable}
6739 %
6740 % \begin{variable}{\l_@@_min_submatch_int, \l_@@_submatch_int, \l_@@_zeroth_submatch_int}
6741 % The end-points of each submatch are stored in two arrays whose index \meta{submatch} ranges
6742 % from \cs{l_@@_min_submatch_int} (inclusive) to
6743 % \cs{l_@@_submatch_int} (exclusive). Each successful match comes
6744 % with a $0$-th submatch (the full match), and one match for each
6745 % capturing group: submatches corresponding to the last successful
6746 % match are labeled starting at \texttt{zeroth_submatch}. The entry
6747 % \cs{l_@@_zeroth_submatch_int} in \cs{g_@@_submatch_prev_intarray} holds
6748 % the position at which that match attempt started: this is used for
6749 % splitting and replacements.
6750 % \begin{macrocode}
6751 \int_new:N \l_@@_min_submatch_int
6752 \int_new:N \l_@@_submatch_int
6753 \int_new:N \l_@@_zeroth_submatch_int
6754 % \end{macrocode}
6755 % \end{variable}
6756 %
6757 % \begin{variable}{\g_@@_submatch_prev_intarray, \g_@@_submatch_begin_intarray, \g_@@_submatch_end_intarray, \g_@@_submatch_case_intarray}
6758 % Hold the place where the match attempt begun, the end-points of each
6759 % submatch, and which regex case the match corresponds to, respectively.
6760 % \begin{macrocode}
6761 \intarray_new:Nn \g_@@_submatch_prev_intarray { 65536 }
6762 \intarray_new:Nn \g_@@_submatch_begin_intarray { 65536 }
6763 \intarray_new:Nn \g_@@_submatch_end_intarray { 65536 }
6764 \intarray_new:Nn \g_@@_submatch_case_intarray { 65536 }
6765 % \end{macrocode}
6766 % \end{variable}
6767 %
6768 % \begin{variable}{\g_@@_balance_intarray}
6769 % The first thing we do when matching is to store the balance of
6770 % begin-group/end-group characters into \cs{g_@@_balance_intarray}.
6771 % \begin{macrocode}
6772 \intarray_new:Nn \g_@@_balance_intarray { 65536 }
6773 % \end{macrocode}
6774 % \end{variable}
6775 %
6776 % \begin{variable}{\l_@@_added_begin_int, \l_@@_added_end_int}
6777 % Keep track of the number of left/right braces to add when performing
6778 % a regex operation such as a replacement.
6779 % \begin{macrocode}
6780 \int_new:N \l_@@_added_begin_int
6781 \int_new:N \l_@@_added_end_int
6782 % \end{macrocode}
6783 % \end{variable}
6784 %
6785 % \begin{macro}{\@@_return:}
6786 % This function triggers either \cs{prg_return_false:} or
6787 % \cs{prg_return_true:} as appropriate to whether a match was found or
6788 % not. It is used by all user conditionals.
6789 % \begin{macrocode}
6790 \cs_new_protected:Npn \@@_return:
6791 {
6792 \if_meaning:w \c_true_bool \g_@@_success_bool
6793 \prg_return_true:
6794 \else:
6795 \prg_return_false:
6796 \fi:
6797 }
6798 % \end{macrocode}
6799 % \end{macro}
6800 %
6801 % \begin{macro}{\@@_query_set:n, \@@_query_set_aux:nN}
6802 % To easily extract subsets of the input once we found the positions
6803 % at which to cut, store the input tokens one by one into successive
6804 % \tn{toks} registers. Also store the brace balance (used to check
6805 % for overall brace balance) in an array.
6806 % \begin{macrocode}
6807 \cs_new_protected:Npn \@@_query_set:n #1
6808 {
6809 \int_zero:N \l_@@_balance_int
6810 \int_zero:N \l_@@_curr_pos_int
6811 \@@_query_set_aux:nN { } F
6812 \tl_analysis_map_inline:nn {#1}
6813 { \@@_query_set_aux:nN {##1} ##3 }
6814 \@@_query_set_aux:nN { } F
6815 \int_set_eq:NN \l_@@_max_pos_int \l_@@_curr_pos_int
6816 }
6817 \cs_new_protected:Npn \@@_query_set_aux:nN #1#2
6818 {
6819 \int_incr:N \l_@@_curr_pos_int
6820 \@@_toks_set:Nn \l_@@_curr_pos_int {#1}
6821 \__kernel_intarray_gset:Nnn \g_@@_balance_intarray
6822 \l_@@_curr_pos_int \l_@@_balance_int
6823 \if_case:w "#2 \exp_stop_f:
6824 \or: \int_incr:N \l_@@_balance_int
6825 \or: \int_decr:N \l_@@_balance_int
6826 \fi:
6827 }
6828 % \end{macrocode}
6829 % \end{macro}
6830 %
6831 % \subsubsection{Matching}
6832 %
6833 % \begin{macro}{\@@_if_match:nn}
6834 % We don't track submatches, and stop after a single match. Build the
6835 % \textsc{nfa} with |#1|, and perform the match on the query |#2|.
6836 % \begin{macrocode}
6837 \cs_new_protected:Npn \@@_if_match:nn #1#2
6838 {
6839 \group_begin:
6840 \@@_disable_submatches:
6841 \@@_single_match:
6842 #1
6843 \@@_match:n {#2}
6844 \group_end:
6845 }
6846 % \end{macrocode}
6847 % \end{macro}
6848 %
6849 % \begin{macro}{\@@_match_case:nnTF}
6850 % \begin{macro}[EXP]{\@@_match_case_aux:nn}
6851 % The code would get badly messed up if the number of items in |#1|
6852 % were not even, so we catch this case, then follow the same code as
6853 % \cs{regex_if_match:nnTF} but using \cs{@@_case_build:n} and without
6854 % returning a result.
6855 % \begin{macrocode}
6856 \cs_new_protected:Npn \@@_match_case:nnTF #1#2
6857 {
6858 \int_if_odd:nTF { \tl_count:n {#1} }
6859 {
6860 \msg_error:nneeee { regex } { case-odd }
6861 { \token_to_str:N \regex_match_case:nn(TF) } { code }
6862 { \tl_count:n {#1} } { \tl_to_str:n {#1} }
6863 \use_ii:nn
6864 }
6865 {
6866 \@@_if_match:nn
6867 { \@@_case_build:e { \@@_tl_odd_items:n {#1} } }
6868 {#2}
6869 \bool_if:NTF \g_@@_success_bool
6870 }
6871 }
6872 \cs_new:Npn \@@_match_case_aux:nn #1#2 { \exp_not:n { {#1} } }
6873 % \end{macrocode}
6874 % \end{macro}
6875 % \end{macro}
6876 %
6877 % \begin{macro}{\@@_count:nnN}
6878 % Again, we don't care about submatches. Instead of aborting after the
6879 % first \enquote{longest match} is found, we search for multiple
6880 % matches, incrementing \cs{l_@@_match_count_int} every time to
6881 % record the number of matches. Build the \textsc{nfa} and match. At
6882 % the end, store the result in the user's variable.
6883 % \begin{macrocode}
6884 \cs_new_protected:Npn \@@_count:nnN #1#2#3
6885 {
6886 \group_begin:
6887 \@@_disable_submatches:
6888 \int_zero:N \l_@@_match_count_int
6889 \@@_multi_match:n { \int_incr:N \l_@@_match_count_int }
6890 #1
6891 \@@_match:n {#2}
6892 \exp_args:NNNo
6893 \group_end:
6894 \int_set:Nn #3 { \int_use:N \l_@@_match_count_int }
6895 }
6896 % \end{macrocode}
6897 % \end{macro}
6898 %
6899 % \subsubsection{Extracting submatches}
6900 %
6901 % \begin{macro}{\@@_extract_once:nnN, \@@_extract_all:nnN}
6902 % Match once or multiple times. After each match (or after the only
6903 % match), extract the submatches using \cs{@@_extract:}. At the
6904 % end, store the sequence containing all the submatches into the user
6905 % variable |#3| after closing the group.
6906 % \begin{macrocode}
6907 \cs_new_protected:Npn \@@_extract_once:nnN #1#2#3
6908 {
6909 \group_begin:
6910 \@@_single_match:
6911 #1
6912 \@@_match:n {#2}
6913 \@@_extract:
6914 \@@_query_set:n {#2}
6915 \@@_group_end_extract_seq:N #3
6916 }
6917 \cs_new_protected:Npn \@@_extract_all:nnN #1#2#3
6918 {
6919 \group_begin:
6920 \@@_multi_match:n { \@@_extract: }
6921 #1
6922 \@@_match:n {#2}
6923 \@@_query_set:n {#2}
6924 \@@_group_end_extract_seq:N #3
6925 }
6926 % \end{macrocode}
6927 % \end{macro}
6928 %
6929 % \begin{macro}{\@@_split:nnN}
6930 % Splitting at submatches is a bit more tricky. For each match,
6931 % extract all submatches, and replace the zeroth submatch by the part
6932 % of the query between the start of the match attempt and the start of
6933 % the zeroth submatch. This is inhibited if the delimiter matched an
6934 % empty token list at the start of this match attempt. After the last
6935 % match, store the last part of the token list, which ranges from the
6936 % start of the match attempt to the end of the query. This step is
6937 % inhibited if the last match was empty and at the very end: decrement
6938 % \cs{l_@@_submatch_int}, which controls which matches will be used.
6939 % \begin{macrocode}
6940 \cs_new_protected:Npn \@@_split:nnN #1#2#3
6941 {
6942 \group_begin:
6943 \@@_multi_match:n
6944 {
6945 \if_int_compare:w
6946 \l_@@_start_pos_int < \l_@@_success_pos_int
6947 \@@_extract:
6948 \__kernel_intarray_gset:Nnn \g_@@_submatch_prev_intarray
6949 \l_@@_zeroth_submatch_int \c_zero_int
6950 \__kernel_intarray_gset:Nnn \g_@@_submatch_end_intarray
6951 \l_@@_zeroth_submatch_int
6952 {
6953 \__kernel_intarray_item:Nn \g_@@_submatch_begin_intarray
6954 \l_@@_zeroth_submatch_int
6955 }
6956 \__kernel_intarray_gset:Nnn \g_@@_submatch_begin_intarray
6957 \l_@@_zeroth_submatch_int
6958 \l_@@_start_pos_int
6959 \fi:
6960 }
6961 #1
6962 \@@_match:n {#2}
6963 \@@_query_set:n {#2}
6964 \__kernel_intarray_gset:Nnn \g_@@_submatch_prev_intarray
6965 \l_@@_submatch_int \c_zero_int
6966 \__kernel_intarray_gset:Nnn \g_@@_submatch_end_intarray
6967 \l_@@_submatch_int
6968 \l_@@_max_pos_int
6969 \__kernel_intarray_gset:Nnn \g_@@_submatch_begin_intarray
6970 \l_@@_submatch_int
6971 \l_@@_start_pos_int
6972 \int_incr:N \l_@@_submatch_int
6973 \if_meaning:w \c_true_bool \l_@@_empty_success_bool
6974 \if_int_compare:w \l_@@_start_pos_int = \l_@@_max_pos_int
6975 \int_decr:N \l_@@_submatch_int
6976 \fi:
6977 \fi:
6978 \@@_group_end_extract_seq:N #3
6979 }
6980 % \end{macrocode}
6981 % \end{macro}
6982 %
6983 % \begin{macro}{\@@_group_end_extract_seq:N}
6984 % \begin{macro}{\@@_extract_seq:N}
6985 % \begin{macro}{\@@_extract_seq:NNn}
6986 % \begin{macro}{\@@_extract_seq_loop:Nw}
6987 % The end-points of submatches are stored as entries of two arrays
6988 % from \cs{l_@@_min_submatch_int} to \cs{l_@@_submatch_int}
6989 % (exclusive). Extract the relevant ranges into \cs{g_@@_tmp_tl},
6990 % separated by \cs{@@_tmp:w} |{}|. We keep track in the two flags
6991 % \texttt{__regex_begin} and \texttt{__regex_end} of the number of
6992 % begin-group or end-group tokens added to make each of these items
6993 % overall balanced. At this step, |}{| is counted as being balanced
6994 % (same number of begin-group and end-group tokens). This problem is
6995 % caught by \cs{@@_extract_check:w}, explained later. After
6996 % complaining about any begin-group or end-group tokens we had to add,
6997 % we are ready to construct the user's sequence outside the group.
6998 % \begin{macrocode}
6999 \cs_new_protected:Npn \@@_group_end_extract_seq:N #1
7000 {
7001 \flag_clear:N \l_@@_begin_flag
7002 \flag_clear:N \l_@@_end_flag
7003 \cs_set_eq:NN \@@_tmp:w \scan_stop:
7004 \__kernel_tl_gset:Nx \g_@@_tmp_tl
7005 {
7006 \int_step_function:nnN \l_@@_min_submatch_int
7007 { \l_@@_submatch_int - \c_one_int } \@@_extract_seq_aux:n
7008 \@@_tmp:w
7009 }
7010 \int_set:Nn \l_@@_added_begin_int
7011 { \flag_height:N \l_@@_begin_flag }
7012 \int_set:Nn \l_@@_added_end_int
7013 { \flag_height:N \l_@@_end_flag }
7014 \tex_afterassignment:D \@@_extract_check:w
7015 \__kernel_tl_gset:Nx \g_@@_tmp_tl
7016 { \g_@@_tmp_tl \if_false: { \fi: } }
7017 \int_compare:nNnT
7018 { \l_@@_added_begin_int + \l_@@_added_end_int } > \c_zero_int
7019 {
7020 \msg_error:nneee { regex } { result-unbalanced }
7021 { splitting~or~extracting~submatches }
7022 { \int_use:N \l_@@_added_begin_int }
7023 { \int_use:N \l_@@_added_end_int }
7024 }
7025 \group_end:
7026 \@@_extract_seq:N #1
7027 }
7028 \cs_gset_protected:Npn \@@_extract_seq:N #1
7029 {
7030 \seq_clear:N #1
7031 \cs_set_eq:NN \@@_tmp:w \@@_extract_seq_loop:Nw
7032 \exp_after:wN \@@_extract_seq:NNn
7033 \exp_after:wN #1
7034 \g_@@_tmp_tl \use_none:nnn
7035 }
7036 \cs_new_protected:Npn \@@_extract_seq:NNn #1#2#3
7037 { #3 #2 #1 \prg_do_nothing: }
7038 \cs_new_protected:Npn \@@_extract_seq_loop:Nw #1#2 \@@_tmp:w #3
7039 {
7040 \seq_put_right:No #1 {#2}
7041 #3 \@@_extract_seq_loop:Nw #1 \prg_do_nothing:
7042 }
7043 % \end{macrocode}
7044 % \end{macro}
7045 % \end{macro}
7046 % \end{macro}
7047 % \end{macro}
7048 %
7049 % \begin{macro}[EXP]{\@@_extract_seq_aux:n, \@@_extract_seq_aux:ww}
7050 % The \texttt{:n} auxiliary builds one item of the sequence of
7051 % submatches. First compute the brace balance of the submatch, then
7052 % extract the submatch from the query, adding the appropriate braces
7053 % and raising a flag if the submatch is not balanced.
7054 % \begin{macrocode}
7055 \cs_new:Npn \@@_extract_seq_aux:n #1
7056 {
7057 \@@_tmp:w { }
7058 \exp_after:wN \@@_extract_seq_aux:ww
7059 \int_value:w \@@_submatch_balance:n {#1} \@@_sep: #1 \@@_sep:
7060 }
7061 \cs_new:Npn \@@_extract_seq_aux:ww #1 \@@_sep: #2 \@@_sep:
7062 {
7063 \if_int_compare:w #1 < \c_zero_int
7064 \prg_replicate:nn {-#1}
7065 {
7066 \flag_raise:N \l_@@_begin_flag
7067 \exp_not:n { { \if_false: } \fi: }
7068 }
7069 \fi:
7070 \@@_query_submatch:n {#2}
7071 \if_int_compare:w #1 > \c_zero_int
7072 \prg_replicate:nn {#1}
7073 {
7074 \flag_raise:N \l_@@_end_flag
7075 \exp_not:n { \if_false: { \fi: } }
7076 }
7077 \fi:
7078 }
7079 % \end{macrocode}
7080 % \end{macro}
7081 %
7082 % \begin{macro}
7083 % {
7084 % \@@_extract_check:w, \@@_extract_check:n,
7085 % \@@_extract_check_loop:w, \@@_extract_check_end:w
7086 % }
7087 % In \cs{@@_group_end_extract_seq:N} we had to expand
7088 % \cs{g_@@_tmp_tl} to turn \cs{if_false:} constructions into
7089 % actual begin-group and end-group tokens. This is done with a
7090 % \cs{__kernel_tl_gset:Nx} assignment, and \cs{@@_extract_check:w} is
7091 % run immediately after this assignment ends, thanks to the
7092 % \tn{afterassignment} primitive. If all of the items were properly
7093 % balanced (enough begin-group tokens before end-group tokens, so |}{|
7094 % is not) then \cs{@@_extract_check:w} is called just before the
7095 % closing brace of the \cs{__kernel_tl_gset:Nx} (thanks to our sneaky
7096 % \cs{if_false:} |{| \cs{fi:} |}| construction), and finds that there
7097 % is nothing left to expand. If any of the items is unbalanced, the
7098 % assignment gets ended early by an extra end-group token, and our
7099 % check finds more tokens needing to be expanded in a new
7100 % \cs{__kernel_tl_gset:Nx} assignment. We need to add a begin-group
7101 % and an end-group tokens to the unbalanced item, namely to the last
7102 % item found so far, which we reach through a loop.
7103 % \begin{macrocode}
7104 \cs_new_protected:Npn \@@_extract_check:w
7105 {
7106 \exp_after:wN \@@_extract_check:n
7107 \exp_after:wN { \if_false: } \fi:
7108 }
7109 \cs_new_protected:Npn \@@_extract_check:n #1
7110 {
7111 \tl_if_empty:nF {#1}
7112 {
7113 \int_incr:N \l_@@_added_begin_int
7114 \int_incr:N \l_@@_added_end_int
7115 \tex_afterassignment:D \@@_extract_check:w
7116 \__kernel_tl_gset:Nx \g_@@_tmp_tl
7117 {
7118 \exp_after:wN \@@_extract_check_loop:w
7119 \g_@@_tmp_tl
7120 \@@_tmp:w \@@_extract_check_end:w
7121 #1
7122 }
7123 }
7124 }
7125 \cs_new:Npn \@@_extract_check_loop:w #1 \@@_tmp:w #2
7126 {
7127 #2
7128 \exp_not:o {#1}
7129 \@@_tmp:w { }
7130 \@@_extract_check_loop:w \prg_do_nothing:
7131 }
7132 % \end{macrocode}
7133 % Arguments of \cs{@@_extract_check_end:w} are: |#1| is the part of
7134 % the item before the extra end-group token; |#2| is junk; |#3| is
7135 % \cs{prg_do_nothing:} followed by the not-yet-expanded part of the
7136 % item after the extra end-group token. In the replacement text, the
7137 % first brace and the \cs{if_false:} |{| \cs{fi:} |}| construction are
7138 % the added begin-group and end-group tokens (the latter being not-yet
7139 % expanded, just like~|#3|), while the closing brace after
7140 % \cs{exp_not:o} |{#1}| replaces the extra end-group token that had
7141 % ended the assignment early. In particular this means that the
7142 % character code of that end-group token is lost.
7143 % \begin{macrocode}
7144 \cs_new:Npn \@@_extract_check_end:w
7145 \exp_not:o #1#2 \@@_extract_check_loop:w #3 \@@_tmp:w
7146 {
7147 { \exp_not:o {#1} }
7148 #3
7149 \if_false: { \fi: }
7150 \@@_tmp:w
7151 }
7152 % \end{macrocode}
7153 % \end{macro}
7154 %
7155 % \begin{macro}{\@@_extract:, \@@_extract_aux:w}
7156 % Our task here is to store the list of end-points of submatches, and
7157 % store them in appropriate array entries, from
7158 % \cs{l_@@_zeroth_submatch_int} upwards. First, we store in
7159 % \cs{g_@@_submatch_prev_intarray} the position at which the match
7160 % attempt started. We extract the rest from the comma list
7161 % \cs{l_@@_success_submatches_tl}, which starts with entries to be
7162 % stored in \cs{g_@@_submatch_begin_intarray} and continues with
7163 % entries for \cs{g_@@_submatch_end_intarray}.
7164 % \begin{macrocode}
7165 \cs_new_protected:Npn \@@_extract:
7166 {
7167 \if_meaning:w \c_true_bool \g_@@_success_bool
7168 \int_set_eq:NN \l_@@_zeroth_submatch_int \l_@@_submatch_int
7169 \prg_replicate:nn \l_@@_capturing_group_int
7170 {
7171 \__kernel_intarray_gset:Nnn \g_@@_submatch_prev_intarray
7172 \l_@@_submatch_int \c_zero_int
7173 \__kernel_intarray_gset:Nnn \g_@@_submatch_case_intarray
7174 \l_@@_submatch_int \c_zero_int
7175 \int_incr:N \l_@@_submatch_int
7176 }
7177 \__kernel_intarray_gset:Nnn \g_@@_submatch_prev_intarray
7178 \l_@@_zeroth_submatch_int \l_@@_start_pos_int
7179 \__kernel_intarray_gset:Nnn \g_@@_submatch_case_intarray
7180 \l_@@_zeroth_submatch_int \g_@@_case_int
7181 \int_zero:N \l_@@_tmpa_int
7182 \exp_after:wN \@@_extract_aux:w \l_@@_success_submatches_tl
7183 \prg_break_point: \@@_use_none_delimit_by_q_recursion_stop:w ,
7184 \q_@@_recursion_stop
7185 \fi:
7186 }
7187 \cs_new_protected:Npn \@@_extract_aux:w #1 ,
7188 {
7189 \prg_break: #1 \prg_break_point:
7190 \if_int_compare:w \l_@@_tmpa_int < \l_@@_capturing_group_int
7191 \__kernel_intarray_gset:Nnn \g_@@_submatch_begin_intarray
7192 { \@@_int_eval:w \l_@@_zeroth_submatch_int + \l_@@_tmpa_int } {#1}
7193 \else:
7194 \__kernel_intarray_gset:Nnn \g_@@_submatch_end_intarray
7195 {
7196 \@@_int_eval:w
7197 \l_@@_zeroth_submatch_int + \l_@@_tmpa_int
7198 - \l_@@_capturing_group_int
7199 }
7200 {#1}
7201 \fi:
7202 \int_incr:N \l_@@_tmpa_int
7203 \@@_extract_aux:w
7204 }
7205 % \end{macrocode}
7206 % \end{macro}
7207 %
7208 % \subsubsection{Replacement}
7209 %
7210 % \begin{macro}{\@@_replace_once:nnN, \@@_replace_once_aux:nnN}
7211 % Build the \textsc{nfa} and the replacement functions, then find a
7212 % single match. If the match failed, simply exit the
7213 % group. Otherwise, we do the replacement. Extract submatches. Compute
7214 % the brace balance corresponding to replacing this match by the
7215 % replacement (this depends on submatches). Prepare the replaced token
7216 % list: the replacement function produces the tokens from the start of
7217 % the query to the start of the match and the replacement text for
7218 % this match; we need to add the tokens from the end of the match to
7219 % the end of the query. Finally, store the result in the user's
7220 % variable after closing the group: this step involves an additional
7221 % \texttt{e}-expansion, and checks that braces are balanced in the
7222 % final result.
7223 % \begin{macrocode}
7224 \cs_new_protected:Npn \@@_replace_once:nnN #1#2
7225 { \@@_replace_once_aux:nnN {#1} { \@@_replacement:n {#2} } }
7226 \cs_new_protected:Npn \@@_replace_once_aux:nnN #1#2#3
7227 {
7228 \group_begin:
7229 \@@_single_match:
7230 #1
7231 \exp_args:No \@@_match:n {#3}
7232 \bool_if:NTF \g_@@_success_bool
7233 {
7234 \@@_extract:
7235 \exp_args:No \@@_query_set:n {#3}
7236 #2
7237 \int_set:Nn \l_@@_balance_int
7238 { \@@_replacement_balance_one_match:n \l_@@_zeroth_submatch_int }
7239 \__kernel_tl_set:Nx \l_@@_tmpa_tl
7240 {
7241 \@@_replacement_do_one_match:n \l_@@_zeroth_submatch_int
7242 \@@_query_range:nn
7243 {
7244 \__kernel_intarray_item:Nn \g_@@_submatch_end_intarray
7245 \l_@@_zeroth_submatch_int
7246 }
7247 \l_@@_max_pos_int
7248 }
7249 \@@_group_end_replace:N #3
7250 }
7251 { \group_end: }
7252 }
7253 % \end{macrocode}
7254 % \end{macro}
7255 %
7256 % \begin{macro}{\@@_replace_all:nnN}
7257 % Match multiple times, and for every match, extract submatches and
7258 % additionally store the position at which the match attempt started.
7259 % The entries from \cs{l_@@_min_submatch_int} to
7260 % \cs{l_@@_submatch_int} hold information about submatches of every
7261 % match in order; each match corresponds to
7262 % \cs{l_@@_capturing_group_int} consecutive entries.
7263 % Compute the brace balance corresponding to doing all the
7264 % replacements: this is the sum of brace balances for replacing each
7265 % match. Join together the replacement texts for each match (including
7266 % the part of the query before the match), and the end of the query.
7267 % \begin{macrocode}
7268 \cs_new_protected:Npn \@@_replace_all:nnN #1#2
7269 { \@@_replace_all_aux:nnN {#1} { \@@_replacement:n {#2} } }
7270 \cs_new_protected:Npn \@@_replace_all_aux:nnN #1#2#3
7271 {
7272 \group_begin:
7273 \@@_multi_match:n { \@@_extract: }
7274 #1
7275 \exp_args:No \@@_match:n {#3}
7276 \exp_args:No \@@_query_set:n {#3}
7277 #2
7278 \int_set:Nn \l_@@_balance_int
7279 {
7280 \c_zero_int
7281 \int_step_function:nnnN
7282 \l_@@_min_submatch_int
7283 \l_@@_capturing_group_int
7284 { \l_@@_submatch_int - \c_one_int }
7285 \@@_replacement_balance_one_match:n
7286 }
7287 \__kernel_tl_set:Nx \l_@@_tmpa_tl
7288 {
7289 \int_step_function:nnnN
7290 \l_@@_min_submatch_int
7291 \l_@@_capturing_group_int
7292 { \l_@@_submatch_int - \c_one_int }
7293 \@@_replacement_do_one_match:n
7294 \@@_query_range:nn
7295 \l_@@_start_pos_int \l_@@_max_pos_int
7296 }
7297 \@@_group_end_replace:N #3
7298 }
7299 % \end{macrocode}
7300 % \end{macro}
7301 %
7302 % \begin{macro}
7303 % {
7304 % \@@_group_end_replace:N, \@@_group_end_replace_try:,
7305 % \@@_group_end_replace_check:w, \@@_group_end_replace_check:n
7306 % }
7307 % At this stage \cs{l_@@_tmpa_tl} (|e|-expands to the desired
7308 % result). Guess from \cs{l_@@_balance_int} the number of braces to
7309 % add before or after the result then try expanding. The simplest
7310 % case is when \cs{l_@@_tmpa_tl} together with the braces we
7311 % insert via \cs{prg_replicate:nn} give a balanced result, and the
7312 % assignment ends at the \cs{if_false:} |{| \cs{fi:} |}| construction:
7313 % then \cs{@@_group_end_replace_check:w} sees that there is no
7314 % material left and we successfully found the result. The harder case
7315 % is that expanding \cs{l_@@_tmpa_tl} may produce extra closing
7316 % braces and end the assignment early. Then we grab the remaining code
7317 % using; importantly, what follows has not yet been expanded so that
7318 % \cs{@@_group_end_replace_check:n} grabs everything until the last
7319 % brace in \cs{@@_group_end_replace_try:}, letting us try again with
7320 % an extra surrounding pair of braces.
7321 % \begin{macrocode}
7322 \cs_new_protected:Npn \@@_group_end_replace:N #1
7323 {
7324 \int_set:Nn \l_@@_added_begin_int
7325 { \int_max:nn { - \l_@@_balance_int } \c_zero_int }
7326 \int_set:Nn \l_@@_added_end_int
7327 { \int_max:nn \l_@@_balance_int \c_zero_int }
7328 \@@_group_end_replace_try:
7329 \int_compare:nNnT { \l_@@_added_begin_int + \l_@@_added_end_int }
7330 > \c_zero_int
7331 {
7332 \msg_error:nneee { regex } { result-unbalanced }
7333 { replacing } { \int_use:N \l_@@_added_begin_int }
7334 { \int_use:N \l_@@_added_end_int }
7335 }
7336 \group_end:
7337 \tl_set_eq:NN #1 \g_@@_tmp_tl
7338 }
7339 \cs_new_protected:Npn \@@_group_end_replace_try:
7340 {
7341 \tex_afterassignment:D \@@_group_end_replace_check:w
7342 \__kernel_tl_gset:Nx \g_@@_tmp_tl
7343 {
7344 \prg_replicate:nn \l_@@_added_begin_int { { \if_false: } \fi: }
7345 \l_@@_tmpa_tl
7346 \prg_replicate:nn \l_@@_added_end_int { \if_false: { \fi: } }
7347 \if_false: { \fi: }
7348 }
7349 }
7350 \cs_new_protected:Npn \@@_group_end_replace_check:w
7351 {
7352 \exp_after:wN \@@_group_end_replace_check:n
7353 \exp_after:wN { \if_false: } \fi:
7354 }
7355 \cs_new_protected:Npn \@@_group_end_replace_check:n #1
7356 {
7357 \tl_if_empty:nF {#1}
7358 {
7359 \int_incr:N \l_@@_added_begin_int
7360 \int_incr:N \l_@@_added_end_int
7361 \@@_group_end_replace_try:
7362 }
7363 }
7364 % \end{macrocode}
7365 % \end{macro}
7366 %
7367 % \subsubsection{Peeking ahead}
7368 %
7369 % \begin{variable}{\l_@@_peek_true_tl, \l_@@_peek_false_tl}
7370 % True/false code arguments of \cs{peek_regex:nTF} or similar.
7371 % \begin{macrocode}
7372 \tl_new:N \l_@@_peek_true_tl
7373 \tl_new:N \l_@@_peek_false_tl
7374 % \end{macrocode}
7375 % \end{variable}
7376 %
7377 % \begin{variable}{\l_@@_replacement_tl}
7378 % When peeking in \cs{peek_regex_replace_once:nnTF} we need to store
7379 % the replacement text.
7380 % \begin{macrocode}
7381 \tl_new:N \l_@@_replacement_tl
7382 % \end{macrocode}
7383 % \end{variable}
7384 %
7385 % \begin{variable}{\l_@@_input_tl}
7386 % \begin{macro}{\@@_input_item:n}
7387 % Stores each token found as \cs{@@_input_item:n} \Arg{tokens}, where
7388 % the \meta{tokens} \texttt{o}-expand to the token found, as for
7389 % \cs{tl_analysis_map_inline:nn}.
7390 % \begin{macrocode}
7391 \tl_new:N \l_@@_input_tl
7392 \cs_new_eq:NN \@@_input_item:n ?
7393 % \end{macrocode}
7394 % \end{macro}
7395 % \end{variable}
7396 %
7397 % \begin{macro}[TF]
7398 % {\peek_regex:n, \peek_regex:N, \peek_regex_remove_once:n, \peek_regex_remove_once:N}
7399 % The |T| and |F| functions just call the corresponding |TF| function.
7400 % The four |TF| functions differ along two axes: whether to remove the
7401 % token or not, distinguished by using \cs{@@_peek_end:} or
7402 % \cs{@@_peek_remove_end:n} (the latter case needs an argument, as we
7403 % will see), and whether the regex has to be compiled or is already in
7404 % an |N|-type variable, distinguished by calling \cs{@@_build_aux:Nn}
7405 % or \cs{@@_build_aux:NN}. The first argument of these functions is
7406 % \cs{c_false_bool} to indicate that there should be no implicit
7407 % insertion of a wildcard at the start of the pattern: otherwise the
7408 % code would keep looking further into the input stream until matching
7409 % the regex.
7410 % \begin{macrocode}
7411 \cs_new_protected:Npn \peek_regex:nTF #1
7412 {
7413 \@@_peek:nnTF
7414 { \@@_build_aux:Nn \c_false_bool {#1} }
7415 { \@@_peek_end: }
7416 }
7417 \cs_new_protected:Npn \peek_regex:nT #1#2
7418 { \peek_regex:nTF {#1} {#2} { } }
7419 \cs_new_protected:Npn \peek_regex:nF #1 { \peek_regex:nTF {#1} { } }
7420 \cs_new_protected:Npn \peek_regex:NTF #1
7421 {
7422 \@@_peek:nnTF
7423 { \@@_build_aux:NN \c_false_bool #1 }
7424 { \@@_peek_end: }
7425 }
7426 \cs_new_protected:Npn \peek_regex:NT #1#2
7427 { \peek_regex:NTF #1 {#2} { } }
7428 \cs_new_protected:Npn \peek_regex:NF #1 { \peek_regex:NTF {#1} { } }
7429 \cs_new_protected:Npn \peek_regex_remove_once:nTF #1
7430 {
7431 \@@_peek:nnTF
7432 { \@@_build_aux:Nn \c_false_bool {#1} }
7433 { \@@_peek_remove_end:n {##1} }
7434 }
7435 \cs_new_protected:Npn \peek_regex_remove_once:nT #1#2
7436 { \peek_regex_remove_once:nTF {#1} {#2} { } }
7437 \cs_new_protected:Npn \peek_regex_remove_once:nF #1
7438 { \peek_regex_remove_once:nTF {#1} { } }
7439 \cs_new_protected:Npn \peek_regex_remove_once:NTF #1
7440 {
7441 \@@_peek:nnTF
7442 { \@@_build_aux:NN \c_false_bool #1 }
7443 { \@@_peek_remove_end:n {##1} }
7444 }
7445 \cs_new_protected:Npn \peek_regex_remove_once:NT #1#2
7446 { \peek_regex_remove_once:NTF #1 {#2} { } }
7447 \cs_new_protected:Npn \peek_regex_remove_once:NF #1
7448 { \peek_regex_remove_once:NTF #1 { } }
7449 % \end{macrocode}
7450 % \end{macro}
7451 %
7452 % \begin{macro}{\@@_peek:nnTF, \@@_peek_aux:nnTF}
7453 % Store the user's true/false codes (plus \cs{group_end:}) into two
7454 % token lists. Then build the automaton with |#1|, without submatch
7455 % tracking, and aiming for a single match. Then start matching by
7456 % setting up a few variables like for any regex matching like
7457 % \cs{regex_if_match:nnTF}, with the addition of \cs{l_@@_input_tl}
7458 % that keeps track of the tokens seen, to reinsert them at the
7459 % end. Instead of \cs{tl_analysis_map_inline:nn} on the input, we
7460 % call \cs{peek_analysis_map_inline:n} to go through tokens in the
7461 % input stream. Since \cs{@@_match_one_token:nnN} calls
7462 % \cs{@@_maplike_break:} we need to catch that and break the
7463 % \cs{peek_analysis_map_inline:n} loop instead.
7464 % \begin{macrocode}
7465 \cs_new_protected:Npn \@@_peek:nnTF #1
7466 {
7467 \@@_peek_aux:nnTF
7468 {
7469 \@@_disable_submatches:
7470 #1
7471 }
7472 }
7473 \cs_new_protected:Npn \@@_peek_aux:nnTF #1#2#3#4
7474 {
7475 \group_begin:
7476 \tl_set:Nn \l_@@_peek_true_tl { \group_end: #3 }
7477 \tl_set:Nn \l_@@_peek_false_tl { \group_end: #4 }
7478 \@@_single_match:
7479 #1
7480 \@@_match_init:
7481 \tl_build_begin:N \l_@@_input_tl
7482 \@@_match_once_init:
7483 \peek_analysis_map_inline:n
7484 {
7485 \tl_build_put_right:Nn \l_@@_input_tl
7486 { \@@_input_item:n {##1} }
7487 \@@_match_one_token:nnN {##1} {##2} ##3
7488 \use_none:nnn
7489 \prg_break_point:Nn \@@_maplike_break:
7490 { \peek_analysis_map_break:n {#2} }
7491 }
7492 }
7493 % \end{macrocode}
7494 % \end{macro}
7495 %
7496 % \begin{macro}{\@@_peek_end:, \@@_peek_remove_end:n}
7497 % Once the regex matches (or permanently fails to match) we call
7498 % \cs{@@_peek_end:}, or \cs{@@_peek_remove_end:n} with argument the
7499 % last token seen. For \cs{peek_regex:nTF} we reinsert tokens seen by
7500 % calling \cs{@@_peek_reinsert:N} regardless of the result of the
7501 % match. For \cs{peek_regex_remove_once:nTF} we reinsert the tokens
7502 % seen only if the match failed; otherwise we just reinsert the
7503 % tokens~|#1|, with one expansion. To be more precise, |#1| consists
7504 % of tokens that \texttt{o}-expand and \texttt{e}-expand to the last
7505 % token seen, for example it is \cs{exp_not:n} |\{| \meta{cs} |\}| for a
7506 % non-expandable primitive. Thanks to \cs{exp_not:n} (rather than
7507 % \cs{exp_not:N}) we don't need to worry about the suppressed further
7508 % expansion of \meta{cs}.
7509 % \begin{macrocode}
7510 \cs_new_protected:Npn \@@_peek_end:
7511 {
7512 \bool_if:NTF \g_@@_success_bool
7513 { \@@_peek_reinsert:N \l_@@_peek_true_tl }
7514 { \@@_peek_reinsert:N \l_@@_peek_false_tl }
7515 }
7516 \cs_new_protected:Npn \@@_peek_remove_end:n #1
7517 {
7518 \bool_if:NTF \g_@@_success_bool
7519 { \exp_after:wN \l_@@_peek_true_tl #1 }
7520 { \@@_peek_reinsert:N \l_@@_peek_false_tl }
7521 }
7522 % \end{macrocode}
7523 % \end{macro}
7524 %
7525 % \begin{macro}{\@@_peek_reinsert:N, \@@_reinsert_item:n}
7526 % Insert the true/false code |#1|, followed by the tokens found, which
7527 % were stored in \cs{l_@@_input_tl}. For this, loop through that
7528 % token list using \cs{@@_reinsert_item:n}, which expands |#1| once to
7529 % get a single token, and jumps over it to expand what follows, with
7530 % suitable \cs{exp:w} and \cs{exp_end:}. We cannot just use
7531 % \cs{use:e} on the whole token list because the result may be
7532 % unbalanced, which would stop the primitive prematurely, or let it
7533 % continue beyond where we would like.
7534 % \begin{macrocode}
7535 \cs_new_protected:Npn \@@_peek_reinsert:N #1
7536 {
7537 \tl_build_end:N \l_@@_input_tl
7538 \cs_set_eq:NN \@@_input_item:n \@@_reinsert_item:n
7539 \exp_after:wN #1 \exp:w \l_@@_input_tl \exp_end:
7540 }
7541 \cs_new_protected:Npn \@@_reinsert_item:n #1
7542 {
7543 \exp_after:wN \exp_after:wN
7544 \exp_after:wN \exp_end:
7545 \exp_after:wN \exp_after:wN
7546 #1
7547 \exp:w
7548 }
7549 % \end{macrocode}
7550 % \end{macro}
7551 %
7552 % \begin{macro}[noTF]
7553 % {\peek_regex_replace_once:nn, \peek_regex_replace_once:Nn}
7554 % Similar to \cs{peek_regex:nTF} above.
7555 % \begin{macrocode}
7556 \cs_new_protected:Npn \peek_regex_replace_once:nnTF #1
7557 { \@@_peek_replace:nnTF { \@@_build_aux:Nn \c_false_bool {#1} } }
7558 \cs_new_protected:Npn \peek_regex_replace_once:nnT #1#2#3
7559 { \peek_regex_replace_once:nnTF {#1} {#2} {#3} { } }
7560 \cs_new_protected:Npn \peek_regex_replace_once:nnF #1#2
7561 { \peek_regex_replace_once:nnTF {#1} {#2} { } }
7562 \cs_new_protected:Npn \peek_regex_replace_once:nn #1#2
7563 { \peek_regex_replace_once:nnTF {#1} {#2} { } { } }
7564 \cs_new_protected:Npn \peek_regex_replace_once:NnTF #1
7565 { \@@_peek_replace:nnTF { \@@_build_aux:NN \c_false_bool #1 } }
7566 \cs_new_protected:Npn \peek_regex_replace_once:NnT #1#2#3
7567 { \peek_regex_replace_once:NnTF #1 {#2} {#3} { } }
7568 \cs_new_protected:Npn \peek_regex_replace_once:NnF #1#2
7569 { \peek_regex_replace_once:NnTF #1 {#2} { } }
7570 \cs_new_protected:Npn \peek_regex_replace_once:Nn #1#2
7571 { \peek_regex_replace_once:NnTF #1 {#2} { } { } }
7572 % \end{macrocode}
7573 % \end{macro}
7574 %
7575 % \begin{macro}{\@@_peek_replace:nnTF}
7576 % Same as \cs{@@_peek:nnTF} (used for \cs{peek_regex:nTF} above), but
7577 % without disabling submatches, and with a different end. The
7578 % replacement text |#2| is stored, to be analyzed later.
7579 % \begin{macrocode}
7580 \cs_new_protected:Npn \@@_peek_replace:nnTF #1#2
7581 {
7582 \tl_set:Nn \l_@@_replacement_tl {#2}
7583 \@@_peek_aux:nnTF {#1} { \@@_peek_replace_end: }
7584 }
7585 % \end{macrocode}
7586 % \end{macro}
7587 %
7588 % \begin{macro}{\@@_peek_replace_end:}
7589 % If the match failed \cs{@@_peek_reinsert:N} reinserts the tokens
7590 % found. Otherwise, finish storing the submatch information using
7591 % \cs{@@_extract:}, and store the input into \tn{toks}. Redefine a
7592 % few auxiliaries to change slightly their expansion behavior as
7593 % explained below. Analyze the replacement text with
7594 % \cs{@@_replacement:n}, which as usual defines
7595 % \cs{@@_replacement_do_one_match:n} to insert the tokens from the
7596 % start of the match attempt to the beginning of the match, followed
7597 % by the replacement text. The \cs{use:e} expands for instance the
7598 % trailing \cs{@@_query_range:nn} down to a sequence of
7599 % \cs{@@_reinsert_item:n} \Arg{tokens} where \meta{tokens}
7600 % \texttt{o}-expand to a single token that we want to insert. After
7601 % \texttt{e}-expansion, \cs{use:e} does \cs{use:n}, so we have
7602 % \cs{exp_after:wN} \cs{l_@@_peek_true_tl} \cs{exp:w} \ldots{}
7603 % \cs{exp_end:}. This is set up such as to obtain
7604 % \cs{l_@@_peek_true_tl} followed by the replaced tokens (possibly
7605 % unbalanced) in the input stream.
7606 % \begin{macrocode}
7607 \cs_new_protected:Npn \@@_peek_replace_end:
7608 {
7609 \bool_if:NTF \g_@@_success_bool
7610 {
7611 \@@_extract:
7612 \@@_query_set_from_input_tl:
7613 \cs_set_eq:NN \@@_replacement_put:n \@@_peek_replacement_put:n
7614 \cs_set_eq:NN \@@_replacement_put_submatch_aux:n
7615 \@@_peek_replacement_put_submatch_aux:n
7616 \cs_set_eq:NN \@@_input_item:n \@@_reinsert_item:n
7617 \cs_set_eq:NN \@@_replacement_exp_not:N \@@_peek_replacement_token:n
7618 \cs_set_eq:NN \@@_replacement_exp_not:V \@@_peek_replacement_var:N
7619 \exp_args:No \@@_replacement:n { \l_@@_replacement_tl }
7620 \use:e
7621 {
7622 \exp_not:n { \exp_after:wN \l_@@_peek_true_tl \exp:w }
7623 \@@_replacement_do_one_match:n \l_@@_zeroth_submatch_int
7624 \@@_query_range:nn
7625 {
7626 \__kernel_intarray_item:Nn \g_@@_submatch_end_intarray
7627 \l_@@_zeroth_submatch_int
7628 }
7629 \l_@@_max_pos_int
7630 \exp_end:
7631 }
7632 }
7633 { \@@_peek_reinsert:N \l_@@_peek_false_tl }
7634 }
7635 % \end{macrocode}
7636 % \end{macro}
7637 %
7638 % \begin{macro}{\@@_query_set_from_input_tl:, \@@_query_set_item:n}
7639 % The input was stored into \cs{l_@@_input_tl} as successive items
7640 % \cs{@@_input_item:n} \Arg{tokens}. Store that in successive
7641 % \tn{toks}. It's not clear whether the empty entries before and
7642 % after are both useful.
7643 % \begin{macrocode}
7644 \cs_new_protected:Npn \@@_query_set_from_input_tl:
7645 {
7646 \tl_build_end:N \l_@@_input_tl
7647 \int_zero:N \l_@@_curr_pos_int
7648 \cs_set_eq:NN \@@_input_item:n \@@_query_set_item:n
7649 \@@_query_set_item:n { }
7650 \l_@@_input_tl
7651 \@@_query_set_item:n { }
7652 \int_set_eq:NN \l_@@_max_pos_int \l_@@_curr_pos_int
7653 }
7654 \cs_new_protected:Npn \@@_query_set_item:n #1
7655 {
7656 \int_incr:N \l_@@_curr_pos_int
7657 \@@_toks_set:Nn \l_@@_curr_pos_int { \@@_input_item:n {#1} }
7658 }
7659 % \end{macrocode}
7660 % \end{macro}
7661 %
7662 % \begin{macro}{\@@_peek_replacement_put:n}
7663 % While building the replacement function
7664 % \cs{@@_replacement_do_one_match:n}, we often want to put simple
7665 % material, given as |#1|, whose \texttt{e}-expansion
7666 % \texttt{o}-expands to a single token. Normally we can just add the
7667 % token to \cs{l_@@_build_tl}, but for
7668 % \cs{peek_regex_replace_once:nnTF} we eventually want to do some
7669 % strange expansion that is basically using \cs{exp_after:wN} to jump
7670 % through numerous tokens (we cannot use \texttt{e}-expansion like for
7671 % \cs{regex_replace_once:nnNTF} because it is ok for the result to be
7672 % unbalanced since we insert it in the input stream rather than
7673 % storing it. When within a csname we don't do any such shenanigan
7674 % because \cs{cs:w} \ldots{} \cs{cs_end:} does all the expansion we
7675 % need.
7676 % \begin{macrocode}
7677 \cs_new_protected:Npn \@@_peek_replacement_put:n #1
7678 {
7679 \if_case:w \l_@@_replacement_csnames_int
7680 \tl_build_put_right:Nn \l_@@_build_tl
7681 { \exp_not:N \@@_reinsert_item:n {#1} }
7682 \else:
7683 \tl_build_put_right:Nn \l_@@_build_tl {#1}
7684 \fi:
7685 }
7686 % \end{macrocode}
7687 % \end{macro}
7688 %
7689 % \begin{macro}{\@@_peek_replacement_token:n}
7690 % When hit with \cs{exp:w}, \cs{@@_peek_replacement_token:n}
7691 % \Arg{token} stops \cs{exp_end:} and does \cs{exp_after:wN}
7692 % \meta{token} \cs{exp:w} to continue expansion after it.
7693 % \begin{macrocode}
7694 \cs_new_protected:Npn \@@_peek_replacement_token:n #1
7695 { \exp_after:wN \exp_end: \exp_after:wN #1 \exp:w }
7696 % \end{macrocode}
7697 % \end{macro}
7698 %
7699 % \begin{macro}{\@@_peek_replacement_put_submatch_aux:n}
7700 % While analyzing the replacement we also have to insert submatches
7701 % found in the query. Since query items \cs{@@_input_item:n}
7702 % \Arg{tokens} expand correctly only when surrounded by \cs{exp:w}
7703 % \ldots{} \cs{exp_end:}, and since these expansion controls are not
7704 % there within csnames (because \cs{cs:w} \ldots{} \cs{cs_end:} make
7705 % them unnecessary in most cases), we have to put \cs{exp:w} and
7706 % \cs{exp_end:} by hand here.
7707 % \begin{macrocode}
7708 \cs_new_protected:Npn \@@_peek_replacement_put_submatch_aux:n #1
7709 {
7710 \if_case:w \l_@@_replacement_csnames_int
7711 \tl_build_put_right:Nn \l_@@_build_tl
7712 { \@@_query_submatch:n { \@@_int_eval:w #1 + ##1 \scan_stop: } }
7713 \else:
7714 \tl_build_put_right:Nn \l_@@_build_tl
7715 {
7716 \exp:w
7717 \@@_query_submatch:n { \@@_int_eval:w #1 + ##1 \scan_stop: }
7718 \exp_end:
7719 }
7720 \fi:
7721 }
7722 % \end{macrocode}
7723 % \end{macro}
7724 %
7725 % \begin{macro}{\@@_peek_replacement_var:N}
7726 % This is used for |\u| outside csnames. It makes sure to continue
7727 % expansion with \cs{exp:w} before expanding the variable~|#1| and
7728 % stopping the \cs{exp:w} that precedes.
7729 % \begin{macrocode}
7730 \cs_new_protected:Npn \@@_peek_replacement_var:N #1
7731 {
7732 \exp_after:wN \exp_last_unbraced:NV
7733 \exp_after:wN \exp_end:
7734 \exp_after:wN #1
7735 \exp:w
7736 }
7737 % \end{macrocode}
7738 % \end{macro}
7739 %
7740 % \subsection{Messages}
7741 %
7742 % Messages for the preparsing phase.
7743 % \begin{macrocode}
7744 \use:e
7745 {
7746 \msg_new:nnn { regex } { trailing-backslash }
7747 { Trailing~'\iow_char:N\\'~in~regex~or~replacement. }
7748 \msg_new:nnn { regex } { x-missing-rbrace }
7749 {
7750 Missing~brace~'\iow_char:N\}'~in~regex~
7751 '...\iow_char:N\\x\iow_char:N\{...##1'.
7752 }
7753 \msg_new:nnn { regex } { x-overflow }
7754 {
7755 Character~code~##1~too~large~in~
7756 \iow_char:N\\x\iow_char:N\{##2\iow_char:N\}~regex.
7757 }
7758 }
7759 % \end{macrocode}
7760 %
7761 % Invalid quantifier.
7762 % \begin{macrocode}
7763 \msg_new:nnnn { regex } { invalid-quantifier }
7764 { Braced~quantifier~'#1'~may~not~be~followed~by~'#2'. }
7765 {
7766 The~character~'#2'~is~invalid~in~the~braced~quantifier~'#1'.~
7767 The~only~valid~quantifiers~are~'*',~'?',~'+',~'{<int>}',~
7768 '{<min>,}'~and~'{<min>,<max>}',~optionally~followed~by~'?'.
7769 }
7770 % \end{macrocode}
7771 %
7772 % Messages for missing or extra closing brackets and parentheses, with
7773 % some fancy singular/plural handling for the case of parentheses.
7774 % \begin{macrocode}
7775 \msg_new:nnnn { regex } { missing-rbrack }
7776 { Missing~right~bracket~inserted~in~regular~expression. }
7777 {
7778 LaTeX~was~given~a~regular~expression~where~a~character~class~
7779 was~started~with~'[',~but~the~matching~']'~is~missing.
7780 }
7781 \msg_new:nnnn { regex } { missing-rparen }
7782 {
7783 Missing~right~
7784 \int_compare:nTF { #1 = 1 } { parenthesis } { parentheses } ~
7785 inserted~in~regular~expression.
7786 }
7787 {
7788 LaTeX~was~given~a~regular~expression~with~\int_eval:n {#1} ~
7789 more~left~parentheses~than~right~parentheses.
7790 }
7791 \msg_new:nnnn { regex } { extra-rparen }
7792 { Extra~right~parenthesis~ignored~in~regular~expression. }
7793 {
7794 LaTeX~came~across~a~closing~parenthesis~when~no~submatch~group~
7795 was~open.~The~parenthesis~will~be~ignored.
7796 }
7797 % \end{macrocode}
7798 %
7799 % Some escaped alphanumerics are not allowed everywhere.
7800 % \begin{macrocode}
7801 \msg_new:nnnn { regex } { bad-escape }
7802 {
7803 Invalid~escape~'\iow_char:N\\#1'~
7804 \@@_if_in_cs:TF { within~a~control~sequence. }
7805 {
7806 \@@_if_in_class:TF
7807 { in~a~character~class. }
7808 { following~a~category~test. }
7809 }
7810 }
7811 {
7812 The~escape~sequence~'\iow_char:N\\#1'~may~not~appear~
7813 \@@_if_in_cs:TF
7814 {
7815 within~a~control~sequence~test~introduced~by~
7816 '\iow_char:N\\c\iow_char:N\{'.
7817 }
7818 {
7819 \@@_if_in_class:TF
7820 { within~a~character~class~ }
7821 { following~a~category~test~such~as~'\iow_char:N\\cL'~ }
7822 because~it~does~not~match~exactly~one~character.
7823 }
7824 }
7825 % \end{macrocode}
7826 %
7827 % Range errors.
7828 % \begin{macrocode}
7829 \msg_new:nnnn { regex } { range-missing-end }
7830 { Invalid~end-point~for~range~'#1-#2'~in~character~class. }
7831 {
7832 The~end-point~'#2'~of~the~range~'#1-#2'~may~not~serve~as~an~
7833 end-point~for~a~range:~alphanumeric~characters~should~not~be~
7834 escaped,~and~non-alphanumeric~characters~should~be~escaped.
7835 }
7836 \msg_new:nnnn { regex } { range-backwards }
7837 { Range~'[#1-#2]'~out~of~order~in~character~class. }
7838 {
7839 In~ranges~of~characters~'[x-y]'~appearing~in~character~classes,~
7840 the~first~character~code~must~not~be~larger~than~the~second.~
7841 Here,~'#1'~has~character~code~\int_eval:n {`#1},~while~
7842 '#2'~has~character~code~\int_eval:n {`#2}.
7843 }
7844 % \end{macrocode}
7845 %
7846 % Errors related to |\c| and |\u|.
7847 % \begin{macrocode}
7848 \msg_new:nnnn { regex } { c-bad-mode }
7849 { Invalid~nested~'\iow_char:N\\c'~escape~in~regular~expression. }
7850 {
7851 The~'\iow_char:N\\c'~escape~cannot~be~used~within~
7852 a~control~sequence~test~'\iow_char:N\\c{...}'~
7853 nor~another~category~test.~
7854 To~combine~several~category~tests,~use~'\iow_char:N\\c[...]'.
7855 }
7856 \msg_new:nnnn { regex } { c-C-invalid }
7857 { '\iow_char:N\\cC'~should~be~followed~by~'.'~or~'(',~not~'#1'. }
7858 {
7859 The~'\iow_char:N\\cC'~construction~restricts~the~next~item~to~be~a~
7860 control~sequence~or~the~next~group~to~be~made~of~control~sequences.~
7861 It~only~makes~sense~to~follow~it~by~'.'~or~by~a~group.
7862 }
7863 \msg_new:nnnn { regex } { cu-lbrace }
7864 { Left~braces~must~be~escaped~in~'\iow_char:N\\#1{...}'. }
7865 {
7866 Constructions~such~as~'\iow_char:N\\#1{...\iow_char:N\{...}'~are~
7867 not~allowed~and~should~be~replaced~by~
7868 '\iow_char:N\\#1{...\token_to_str:N\{...}'.
7869 }
7870 \msg_new:nnnn { regex } { c-lparen-in-class }
7871 { Catcode~test~cannot~apply~to~group~in~character~class }
7872 {
7873 Construction~such~as~'\iow_char:N\\cL(abc)'~are~not~allowed~inside~a~
7874 class~'[...]'~because~classes~do~not~match~multiple~characters~at~once.
7875 }
7876 \msg_new:nnnn { regex } { c-missing-rbrace }
7877 { Missing~right~brace~inserted~for~'\iow_char:N\\c'~escape. }
7878 {
7879 LaTeX~was~given~a~regular~expression~where~a~
7880 '\iow_char:N\\c\iow_char:N\{...'~construction~was~not~ended~
7881 with~a~closing~brace~'\iow_char:N\}'.
7882 }
7883 \msg_new:nnnn { regex } { c-missing-rbrack }
7884 { Missing~right~bracket~inserted~for~'\iow_char:N\\c'~escape. }
7885 {
7886 A~construction~'\iow_char:N\\c[...'~appears~in~a~
7887 regular~expression,~but~the~closing~']'~is~not~present.
7888 }
7889 \msg_new:nnnn { regex } { c-missing-category }
7890 { Invalid~character~'#1'~following~'\iow_char:N\\c'~escape. }
7891 {
7892 In~regular~expressions,~the~'\iow_char:N\\c'~escape~sequence~
7893 may~only~be~followed~by~a~left~brace,~a~left~bracket,~or~a~
7894 capital~letter~representing~a~character~category,~namely~
7895 one~of~'ABCDELMOPSTU'.
7896 }
7897 \msg_new:nnnn { regex } { c-trailing }
7898 { Trailing~category~code~escape~'\iow_char:N\\c'... }
7899 {
7900 A~regular~expression~ends~with~'\iow_char:N\\c'~followed~
7901 by~a~letter.~It~will~be~ignored.
7902 }
7903 \msg_new:nnnn { regex } { u-missing-lbrace }
7904 { Missing~left~brace~following~'\iow_char:N\\u'~escape. }
7905 {
7906 The~'\iow_char:N\\u'~escape~sequence~must~be~followed~by~
7907 a~brace~group~with~the~name~of~the~variable~to~use.
7908 }
7909 \msg_new:nnnn { regex } { u-missing-rbrace }
7910 { Missing~right~brace~inserted~for~'\iow_char:N\\u'~escape. }
7911 {
7912 LaTeX~
7913 \str_if_eq:eeTF { } {#2}
7914 { reached~the~end~of~the~string~ }
7915 { encountered~an~escaped~alphanumeric~character '\iow_char:N\\#2'~ }
7916 when~parsing~the~argument~of~an~
7917 '\iow_char:N\\u\iow_char:N\{...\}'~escape.
7918 }
7919 % \end{macrocode}
7920 %
7921 % Errors when encountering the \textsc{posix} syntax |[:...:]|.
7922 % \begin{macrocode}
7923 \msg_new:nnnn { regex } { posix-unsupported }
7924 { POSIX~collating~element~'[#1 ~ #1]'~not~supported. }
7925 {
7926 The~'[.foo.]'~and~'[=bar=]'~syntaxes~have~a~special~meaning~
7927 in~POSIX~regular~expressions.~This~is~not~supported~by~LaTeX.~
7928 Maybe~you~forgot~to~escape~a~left~bracket~in~a~character~class?
7929 }
7930 \msg_new:nnnn { regex } { posix-unknown }
7931 { POSIX~class~'[:#1:]'~unknown. }
7932 {
7933 '[:#1:]'~is~not~among~the~known~POSIX~classes~
7934 '[:alnum:]',~'[:alpha:]',~'[:ascii:]',~'[:blank:]',~
7935 '[:cntrl:]',~'[:digit:]',~'[:graph:]',~'[:lower:]',~
7936 '[:print:]',~'[:punct:]',~'[:space:]',~'[:upper:]',~
7937 '[:word:]',~and~'[:xdigit:]'.
7938 }
7939 \msg_new:nnnn { regex } { posix-missing-close }
7940 { Missing~closing~':]'~for~POSIX~class. }
7941 { The~POSIX~syntax~'#1'~must~be~followed~by~':]',~not~'#2'. }
7942 % \end{macrocode}
7943 %
7944 % In various cases, the result of a \pkg{l3regex} operation can leave us
7945 % with an unbalanced token list, which we must re-balance by adding
7946 % begin-group or end-group character tokens.
7947 % \begin{macrocode}
7948 \msg_new:nnnn { regex } { result-unbalanced }
7949 { Missing~brace~inserted~when~#1. }
7950 {
7951 LaTeX~was~asked~to~do~some~regular~expression~operation,~
7952 and~the~resulting~token~list~would~not~have~the~same~number~
7953 of~begin-group~and~end-group~tokens.~Braces~were~inserted:~
7954 #2~left,~#3~right.
7955 }
7956 % \end{macrocode}
7957 %
7958 % Error message for unknown options.
7959 % \begin{macrocode}
7960 \msg_new:nnnn { regex } { unknown-option }
7961 { Unknown~option~'#1'~for~regular~expressions. }
7962 {
7963 The~only~available~option~is~'case-insensitive',~toggled~by~
7964 '(?i)'~and~'(?-i)'.
7965 }
7966 \msg_new:nnnn { regex } { special-group-unknown }
7967 { Unknown~special~group~'#1~...'~in~a~regular~expression. }
7968 {
7969 The~only~valid~constructions~starting~with~'(?'~are~
7970 '(?:~...~)',~'(?|~...~)',~'(?i)',~and~'(?-i)'.
7971 }
7972 % \end{macrocode}
7973 %
7974 % Errors in the replacement text.
7975 % \begin{macrocode}
7976 \msg_new:nnnn { regex } { replacement-c }
7977 { Misused~'\iow_char:N\\c'~command~in~a~replacement~text. }
7978 {
7979 In~a~replacement~text,~the~'\iow_char:N\\c'~escape~sequence~
7980 can~be~followed~by~one~of~the~letters~'ABCDELMOPSTU'~
7981 or~a~brace~group,~not~by~'#1'.
7982 }
7983 \msg_new:nnnn { regex } { replacement-u }
7984 { Misused~'\iow_char:N\\u'~command~in~a~replacement~text. }
7985 {
7986 In~a~replacement~text,~the~'\iow_char:N\\u'~escape~sequence~
7987 must~be~~followed~by~a~brace~group~holding~the~name~of~the~
7988 variable~to~use.
7989 }
7990 \msg_new:nnnn { regex } { replacement-g }
7991 {
7992 Missing~brace~for~the~'\iow_char:N\\g'~construction~
7993 in~a~replacement~text.
7994 }
7995 {
7996 In~the~replacement~text~for~a~regular~expression~search,~
7997 submatches~are~represented~either~as~'\iow_char:N \\g{dd..d}',~
7998 or~'\\d',~where~'d'~are~single~digits.~Here,~a~brace~is~missing.
7999 }
8000 \msg_new:nnnn { regex } { replacement-catcode-end }
8001 {
8002 Missing~character~for~the~'\iow_char:N\\c<category><character>'~
8003 construction~in~a~replacement~text.
8004 }
8005 {
8006 In~a~replacement~text,~the~'\iow_char:N\\c'~escape~sequence~
8007 can~be~followed~by~one~of~the~letters~'ABCDELMOPSTU'~representing~
8008 the~character~category.~Then,~a~character~must~follow.~LaTeX~
8009 reached~the~end~of~the~replacement~when~looking~for~that.
8010 }
8011 \msg_new:nnnn { regex } { replacement-catcode-escaped }
8012 {
8013 Escaped~letter~or~digit~after~category~code~in~replacement~text.
8014 }
8015 {
8016 In~a~replacement~text,~the~'\iow_char:N\\c'~escape~sequence~
8017 can~be~followed~by~one~of~the~letters~'ABCDELMOPSTU'~representing~
8018 the~character~category.~Then,~a~character~must~follow,~not~
8019 '\iow_char:N\\#2'.
8020 }
8021 \msg_new:nnnn { regex } { replacement-catcode-in-cs }
8022 {
8023 Category~code~'\iow_char:N\\c#1#3'~ignored~inside~
8024 '\iow_char:N\\c\{...\}'~in~a~replacement~text.
8025 }
8026 {
8027 In~a~replacement~text,~the~category~codes~of~the~argument~of~
8028 '\iow_char:N\\c\{...\}'~are~ignored~when~building~the~control~
8029 sequence~name.
8030 }
8031 \msg_new:nnnn { regex } { replacement-null-space }
8032 { TeX~cannot~build~a~space~token~with~character~code~0. }
8033 {
8034 You~asked~for~a~character~token~with~category~space,~
8035 and~character~code~0,~for~instance~through~
8036 '\iow_char:N\\cS\iow_char:N\\x00'.~
8037 This~specific~case~is~impossible~and~will~be~replaced~
8038 by~a~normal~space.
8039 }
8040 \msg_new:nnnn { regex } { replacement-missing-rbrace }
8041 { Missing~right~brace~inserted~in~replacement~text. }
8042 {
8043 There~ \int_compare:nTF { #1 = 1 } { was } { were } ~ #1~
8044 missing~right~\int_compare:nTF { #1 = 1 } { brace } { braces } .
8045 }
8046 \msg_new:nnnn { regex } { replacement-missing-rparen }
8047 { Missing~right~parenthesis~inserted~in~replacement~text. }
8048 {
8049 There~ \int_compare:nTF { #1 = 1 } { was } { were } ~ #1~
8050 missing~right~
8051 \int_compare:nTF { #1 = 1 } { parenthesis } { parentheses } .
8052 }
8053 \msg_new:nnn { regex } { submatch-too-big }
8054 { Submatch~#1~used~but~regex~only~has~#2~group(s) }
8055 % \end{macrocode}
8056 %
8057 % Some escaped alphanumerics are not allowed everywhere.
8058 % \begin{macrocode}
8059 \msg_new:nnnn { regex } { backwards-quantifier }
8060 { Quantifer~"{#1,#2}"~is~backwards. }
8061 { The~values~given~in~a~quantifier~must~be~in~order. }
8062 % \end{macrocode}
8063 %
8064 % Used in user commands, and when showing a regex.
8065 % \begin{macrocode}
8066 \msg_new:nnnn { regex } { case-odd }
8067 { #1~with~odd~number~of~items }
8068 {
8069 There~must~be~a~#2~part~for~each~regex:~
8070 found~odd~number~of~items~(#3)~in\\
8071 \iow_indent:n {#4}
8072 }
8073 \msg_new:nnn { regex } { show }
8074 {
8075 >~Compiled~regex~
8076 \tl_if_empty:nTF {#1} { variable~ #2 } { {#1} } :
8077 #3
8078 }
8079 \prop_gput:Nnn \g_msg_module_name_prop { regex } { LaTeX }
8080 \prop_gput:Nnn \g_msg_module_type_prop { regex } { }
8081 % \end{macrocode}
8082 %
8083 % \begin{macro}{\@@_msg_repeated:nnN}
8084 % This is not technically a message, but seems related enough to go
8085 % there. The arguments are: |#1| is the minimum number of repetitions;
8086 % |#2| is the number of allowed extra repetitions ($-1$ for infinite
8087 % number), and |#3| tells us about laziness.
8088 % \begin{macrocode}
8089 \cs_new:Npn \@@_msg_repeated:nnN #1#2#3
8090 {
8091 \str_if_eq:eeF { #1 #2 } { 1 0 }
8092 {
8093 , ~ repeated ~
8094 \int_case:nnF {#2}
8095 {
8096 { -1 } { #1~or~more~times,~\bool_if:NTF #3 { lazy } { greedy } }
8097 { 0 } { #1~times }
8098 }
8099 {
8100 between~#1~and~\int_eval:n {#1+#2}~times,~
8101 \bool_if:NTF #3 { lazy } { greedy }
8102 }
8103 }
8104 }
8105 % \end{macrocode}
8106 % \end{macro}
8107 %
8108 % \subsection{Code for tracing}
8109 %
8110 % There is a more extensive implementation of tracing in the l3trial
8111 % package \pkg{l3trace}. Function names are a bit different but could
8112 % be merged.
8113 %
8114 % \begin{macro}
8115 % {\@@_trace_push:nnN, \@@_trace_pop:nnN, \@@_trace:nne}
8116 % Here |#1| is the module name (\texttt{regex}) and |#2| is
8117 % typically~1. If the module's current tracing level is less than
8118 % |#2| show nothing, otherwise write |#3| to the terminal.
8119 % \begin{macrocode}
8120 \cs_new_protected:Npn \@@_trace_push:nnN #1#2#3
8121 { \@@_trace:nne {#1} {#2} { entering~ \token_to_str:N #3 } }
8122 \cs_new_protected:Npn \@@_trace_pop:nnN #1#2#3
8123 { \@@_trace:nne {#1} {#2} { leaving~ \token_to_str:N #3 } }
8124 \cs_new_protected:Npn \@@_trace:nne #1#2#3
8125 {
8126 \int_compare:nNnF
8127 { \int_use:c { g_@@_trace_#1_int } } < {#2}
8128 { \iow_term:e { Trace:~#3 } }
8129 }
8130 % \end{macrocode}
8131 % \end{macro}
8132 %
8133 % \begin{variable}{\g_@@_trace_regex_int}
8134 % No tracing when that is zero.
8135 % \begin{macrocode}
8136 \int_new:N \g_@@_trace_regex_int
8137 % \end{macrocode}
8138 % \end{variable}
8139 %
8140 % \begin{macro}{\@@_trace_states:n}
8141 % This function lists the contents of all states of the \textsc{nfa},
8142 % stored in \tn{toks} from $0$ to \cs{l_@@_max_state_int}
8143 % (excluded).
8144 % \begin{macrocode}
8145 \cs_new_protected:Npn \@@_trace_states:n #1
8146 {
8147 \int_step_inline:nnn
8148 \l_@@_min_state_int
8149 { \l_@@_max_state_int - \c_one_int }
8150 {
8151 \@@_trace:nne { regex } {#1}
8152 { \iow_char:N \\toks ##1 = { \@@_toks_use:w ##1 } }
8153 }
8154 }
8155 % \end{macrocode}
8156 % \end{macro}
8157 %
8158 % \begin{macrocode}
8159 %</code>
8160 % \end{macrocode}
8161 %
8162 % \end{implementation}
8163 %
8164 % \PrintIndex
8165 % \endinput
8166 %^^A NOT IMPLEMENTED
8167 %^^A \p{xx} a character with the xx property
8168 %^^A \P{xx} a character without the xx property
8169 %^^A (?=...) positive look ahead
8170 %^^A (?!...) negative look ahead
8171 %^^A (?<=...) positive look behind
8172 %^^A (?<!...) negative look behind
8173 %^^A (?<name>...) or (?'name'...) or (?P<name>...)
8174 %^^A named capturing group
8175 %^^A \R a newline sequence
8176 %^^A \X an extended Unicode sequence
8177 %^^A (?C) or (?Cn) callout with data n
8178 %^^A (?R) recurse whole pattern
8179 %^^A (?[+-]n) or \g<[+-]n> or (?&name) or (?P>name) or \g<name>
8180 %^^A call subpattern
8181 %^^A (?([+-]n)... or (?(<name>)...
8182 %^^A reference condition
8183 %^^A (?(R)... or (?(Rn)... or (?(R&name)...
8184 %^^A recursion condition
8185 %^^A (?(DEFINE)... define subpattern for reference
8186 %^^A (?(assert)... assertion condition
8187 %^^A (?(?=..)..|..) positive/negative look ahead/behind condition
8188 %^^A (*ACCEPT) force successful match
8189 %^^A (*FAIL) force backtrack; synonym (*F)
8190 %^^A (*COMMIT) overall failure, no advance of starting point
8191 %^^A (*PRUNE) advance to next starting character
8192 %^^A (*SKIP) advance start to current matching position
8193 %^^A (*THEN) local failure, backtrack to next alternation
8194 %^^A (*CR) or (*LF) or (*CRLF) or (*ANYCRLF) or (*ANY)
8195 %^^A newline convention
8196 %^^A (*BSR_ANYCRLF) or (*BSR_UNICODE)
8197 %^^A change what \R matches.
8198 %^^A
8199 %^^A \cx "control-x", where x is any ASCII character
8200 %^^A \C one byte, even in UTF-8 mode (best avoided)
8201 %^^A + possessive quantifiers
8202 %^^A (?>...) atomic, non-capturing group
8203 %^^A (?#....) comment (not nestable)
8204 %^^A (?Jms-UxX) options (duplicate names; multiline; single line;
8205 %^^A unset what follows; ungreedy; extended;
8206 %^^A error on bad escapes)
8207 %^^A (?i:...|...) convenient shorthand for (?:(?i)...|...)
8208 %^^A (*NO_START_OPT) no start-match optimization (PCRE_NO_START_OPTIMIZE)
8209 %^^A (*UTF8) set UTF-8 mode (PCRE_UTF8)
8210 %^^A (*UCP) set PCRE_UCP (use Unicode properties for \d etc)
8211 %^^A \n or \gn or \g{[-]n} or \g{name} or (?P=name)
8212 %^^A or \k<name> or \k'name' or \k{name}
8213 %^^A back-references