| 1 |
% !TeX encoding = UTF-8 |
| 2 |
% Ce fichier contient le code de l'extension "tokstools" |
| 3 |
|
| 4 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5 |
% % |
| 6 |
\def\tktlname {tokstools} % |
| 7 |
\def\tktlver {0.2} % |
| 8 |
% % |
| 9 |
\def\tktldate {2026/04/17} % |
| 10 |
% % |
| 11 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12 |
% % |
| 13 |
% Author : Christian Tellechea % |
| 14 |
% Status : Maintained % |
| 15 |
% Email : [email protected] % |
| 16 |
% Package URL: https://www.ctan.org/pkg/tokstools % |
| 17 |
% Copyright : Christian Tellechea 2026 % |
| 18 |
% Licence : Released under the LaTeX Project Public License v1.3c % |
| 19 |
% or later, see http://www.latex-project.org/lppl.txt % |
| 20 |
% Files : 1) tokstools.tex (this file) % |
| 21 |
% 2) tokstools.sty (sty file for latex use) % |
| 22 |
% 3) tokstools-fr.tex (source of manual in french) % |
| 23 |
% 4) tokstools-fr.pdf (pdf of manual in french) % |
| 24 |
% 5) tokstools-en.tex (source of manual in english) % |
| 25 |
% 6) tokstools-en.pdf (pdf of manual in english) % |
| 26 |
% 7) README.md % |
| 27 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 28 |
|
| 29 |
%--------------------------------------------------------------------- |
| 30 |
%---------------- Annonce package et autres pré-requis --------------- |
| 31 |
%--------------------------------------------------------------------- |
| 32 |
\csname tktl_load_once\endcsname |
| 33 |
\expandafter\let\csname tktl_load_once\endcsname\endinput |
| 34 |
|
| 35 |
\ifdefined\tktlstyfile |
| 36 |
\else |
| 37 |
\immediate\write-1 {Package: \tktlname\space\tktldate\space\space v\tktlver\space\space PEG tools for tokens (CT)}% |
| 38 |
\fi |
| 39 |
|
| 40 |
\expandafter\edef\csname tktl_restore_catcode\endcsname{\catcode\number`\_=\number\catcode`\_\relax} |
| 41 |
\catcode`\_11 |
| 42 |
\def\tktl_star{*} |
| 43 |
\def\tktl_superscript{^} |
| 44 |
% Macros de manipulation d'arguments |
| 45 |
\long\def\tktl_exec_first#1#2{#1} |
| 46 |
\long\def\tktl_exec_second#1#2{#2} |
| 47 |
\long\def\tktl_test_passed\fi\tktl_exec_second#1#2{\fi#1} |
| 48 |
\long\def\tktl_ok_this_test#1#2\tktl_else_all_tests#3{\fi#1} |
| 49 |
\long\def\tktl_else_all_tests#1{#1} |
| 50 |
\long\def\tktl_gob_arg#1{} |
| 51 |
\long\def\tktl_id#1{#1} |
| 52 |
\long\def\tktl_firsttonil#1#2\_nil{#1} |
| 53 |
\def\tktl_def_tok#1#2{\let#1= #2\relax} |
| 54 |
\tktl_def_tok\tktl_sp_token{ } |
| 55 |
\def\tktl_csdef#1{\expandafter\def\csname#1\endcsname} |
| 56 |
\def\tktl_unexpand_earg#1#{% |
| 57 |
\tktl_unexpand_earg_a{#1}% |
| 58 |
} |
| 59 |
\def\tktl_unexpand_earg_a#1#2{% #1=instructions #2=arg -> #1{*#2} |
| 60 |
\unexpanded{#1}{\unexpanded\expandafter{#2}}% |
| 61 |
} |
| 62 |
\def\tktl_expand_n_times#1#2#3{% développe #1 fois l'argument #3 de la _macro_ #2 : #2{*^n#3} |
| 63 |
\tktl_ifnum{#1>0 } |
| 64 |
{\expandafter\tktl_expand_n_times\expandafter{\the\numexpr#1-1\expandafter}\expandafter#2\expandafter{#3}} |
| 65 |
{#2{#3}}% |
| 66 |
} |
| 67 |
\long\def\tktl_antefi#1#2\fi{#2\fi#1} |
| 68 |
\def\tktl_ifnxttok#1#2#3{% |
| 69 |
\tktl_def_tok\tktl_toksmatch{#1}% |
| 70 |
\def\tktl__truecode{#2}% |
| 71 |
\def\tktl__falsecode{#3}% |
| 72 |
\tktl_ifnxttok_a |
| 73 |
} |
| 74 |
\def\tktl_ifnxttok_a{\futurelet\tktl__futurtok\tktl_ifnxttok_b} |
| 75 |
\def\tktl_ifnxttok_b{% |
| 76 |
\ifx\tktl__futurtok\tktl_sp_token |
| 77 |
\afterassignment\tktl_ifnxttok_a |
| 78 |
\tktl_antefi{\let\tktl__futurtok= } |
| 79 |
\else |
| 80 |
\tktl_antefi{% |
| 81 |
\ifx\tktl__futurtok\tktl_toksmatch |
| 82 |
\tktl_antefi\tktl__truecode |
| 83 |
\else |
| 84 |
\tktl_antefi\tktl__falsecode |
| 85 |
\fi |
| 86 |
}% |
| 87 |
\fi |
| 88 |
} |
| 89 |
\def\tktl_futurelet_nospace#1#2{% |
| 90 |
\def\tktl_futurelet_nospace_a{\futurelet#1\tktl_futurelet_nospace_b}% |
| 91 |
\def\tktl_futurelet_nospace_b{% |
| 92 |
\ifx\tktl__futurtok\tktl_sp_token |
| 93 |
\afterassignment\tktl_futurelet_nospace_a |
| 94 |
\tktl_antefi{\let#1= }% |
| 95 |
\else |
| 96 |
\tktl_antefi{#2}% |
| 97 |
\fi |
| 98 |
}% |
| 99 |
\tktl_futurelet_nospace_a |
| 100 |
} |
| 101 |
% Quarks |
| 102 |
\def\active_quark{\active_quark} |
| 103 |
\def\end_parse{\end_parse} |
| 104 |
|
| 105 |
% Retrait d'espaces |
| 106 |
\def\tktl_temp#1{% |
| 107 |
\long\def\tktl_stripsp##1{\expanded{\tktl_stripsp_i\_marksp##1\__nil\_marksp#1\_marksp\_nil}}% |
| 108 |
\long\def\tktl_stripsp_i##1\_marksp#1##2\_marksp##3\_nil{\tktl_stripsp_ii##3##1##2\__nil#1\__nil\_nil}% |
| 109 |
\long\def\tktl_stripsp_ii##1#1\__nil##2\_nil{\tktl_stripsp_iii##1##2\_nil}% |
| 110 |
\long\def\tktl_stripsp_iii##1##2\__nil##3\_nil{\unexpanded{##2}}% |
| 111 |
}\tktl_temp{ } |
| 112 |
\long\def\tktl_addtomacro#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}} |
| 113 |
\long\def\tktl_eaddtomacro#1#2{\edef#1{\unexpanded\expandafter{#1}\unexpanded\expandafter{#2}}} |
| 114 |
\def\tktl_addtotoks#1#2{#1\expandafter{\the#1#2}} |
| 115 |
\def\tktl_eaddtotoks#1#2{#1\expandafter{\expanded{\the#1\unexpanded\expandafter{#2}}}} |
| 116 |
\def\tktl_xaddtotoks#1#2{#1\expandafter{\expanded{\the#1#2}}} |
| 117 |
% Macros de test |
| 118 |
\long\def\tktl_ifempty_or_space#1{\tktl_ifempty_or_space_i#1\_nil\_nil\tktl_exec_second\tktl_exec_first\__nil}% |
| 119 |
\long\def\tktl_ifempty_or_space_i#1#2\_nil#3#4#5\__nil{#4} |
| 120 |
\def\tktl_ifx#1{\ifx#1\tktl_test_passed\fi\tktl_exec_second} |
| 121 |
\def\tktl_ifnum#1{\ifnum#1\tktl_test_passed\fi\tktl_exec_second} |
| 122 |
\long\def\tktl_ifinstr#1#2{% la chaine #1 est-elle dans #2 ? |
| 123 |
\long\def\tktl_ifinstr_i##1#1##2\tktl_ifinstr_i{\ifcat\relax\detokenize{##2}\relax\expandafter\tktl_exec_second\else\expandafter\tktl_exec_first\fi}% |
| 124 |
\tktl_ifinstr_i#2#1\tktl_ifinstr_i |
| 125 |
} |
| 126 |
% Macros auxiliaires |
| 127 |
\def\tktl_addto_special_list#1{% |
| 128 |
\def\tktl__tmp{\def\tktl_list_specials}% |
| 129 |
\expandafter\tktl__tmp\expandafter{\tktl_list_specials#1,}% |
| 130 |
} |
| 131 |
\let\tktl_bg_token{ |
| 132 |
\let\tktl_eg_token} |
| 133 |
|
| 134 |
%--------------------------------------------------------------------- |
| 135 |
%---------------- Macro \tktl_encode_tokens_char_range --------------- |
| 136 |
%--------------------------------------------------------------------- |
| 137 |
% \tktl_encode_tokens_char_range{<char range>}<macroA><macroB> |
| 138 |
% |
| 139 |
% analyse le <char range> et renvoie dans les 2 macros les charcodes |
| 140 |
% min et max correspondant à <char range>, sachant que <char range> est de la forme : |
| 141 |
% . <a>-<b> où <a> et <b> sont 2 caractères entourés d'éventuels espaces |
| 142 |
% . <a> un caractère unique entouré d'éventuels d'espaces |
| 143 |
% . "-" compris comme le caractère unique "-" |
| 144 |
% . " " compris comme le caractère unique <espace> de charcode 32 |
| 145 |
% . <espace>-<b> ou <a>-<espace> ou <espace>-<espace> : les <espaces> donnent 32 |
| 146 |
% |
| 147 |
% Retour : |
| 148 |
%* *<macroA> et <macroB> qui seront égales pour un caractère unique |
| 149 |
% <a> ou pour <a>-<a> |
| 150 |
% * un \errmessage est renvoyé si |
| 151 |
% . <char range> est vide |
| 152 |
% . <a> ou <b> est, après retrait d'espces avant/après, constitué de plusieurs caractères |
| 153 |
% . si le charcode de <a> est supérieur à celui de <b> |
| 154 |
%--------------------------------------------------------------------- |
| 155 |
\def\tktl_char_range_sep{-} |
| 156 |
\def\tktl_encode_tokens_char_range#1#2#3{% #1=intervalle de la forme '<a>-<b>' ou '<a>' #2=charcode min #3=charcodemax |
| 157 |
\def\tktl_current_char_range{#1}% |
| 158 |
\ifx\tktl_current_char_range\empty |
| 159 |
\errmessage{Empty char range, '0' inserted}% |
| 160 |
\edef#2{\number`\0}% |
| 161 |
\let#3#2% |
| 162 |
\else |
| 163 |
\ifx\tktl_current_char_range\space |
| 164 |
\def#2{32}% |
| 165 |
\let#3#2% |
| 166 |
\else |
| 167 |
\tktl_ifinstr-{#1} |
| 168 |
{% |
| 169 |
\ifx\tktl_current_char_range\tktl_char_range_sep% si "-" seul, le prendre comme char |
| 170 |
\tktl_encode_tokens_char_range_b{-}#2% |
| 171 |
\let#3#2% |
| 172 |
\else |
| 173 |
\tktl_encode_tokens_char_range_a#1\_nil#2#3% |
| 174 |
\fi |
| 175 |
} |
| 176 |
{% |
| 177 |
\tktl_encode_tokens_char_range_b{#1}#2% |
| 178 |
\let#3#2% |
| 179 |
}% |
| 180 |
\fi |
| 181 |
\fi |
| 182 |
} |
| 183 |
\def\tktl_encode_tokens_char_range_a#1-#2\_nil#3#4{% |
| 184 |
\def\tktl_current_lo_range{#1}% |
| 185 |
\def\tktl_current_hi_range{#2}% |
| 186 |
\ifnum0\ifx\tktl_current_lo_range\empty1\fi\ifx\tktl_current_hi_range\empty1\fi>0 |
| 187 |
\errmessage{Missing bound in char range \detokenize{#1-#2}, "0-0" inserted}% |
| 188 |
\def\tktl_current_lo_range{0}% |
| 189 |
\def\tktl_current_hi_range{0}% |
| 190 |
\fi |
| 191 |
\ifx\tktl_current_lo_range\space |
| 192 |
\def#3{32}% |
| 193 |
\ifx\tktl_current_hi_range\space |
| 194 |
\def#4{32}% |
| 195 |
\else |
| 196 |
\tktl_encode_tokens_char_range_b{#2}#4% |
| 197 |
\fi |
| 198 |
\else |
| 199 |
\tktl_encode_tokens_char_range_b{#1}#3% |
| 200 |
\ifx\tktl_current_hi_range\space |
| 201 |
\def#4{32}% |
| 202 |
\else |
| 203 |
\tktl_encode_tokens_char_range_b{#2}#4% |
| 204 |
\fi |
| 205 |
\fi |
| 206 |
\ifnum#4<#3 |
| 207 |
\errmessage{Unsorted interval '\detokenize{#1-#2}', '\detokenize{#2-#1}' inserted}% |
| 208 |
\let\tktl__tmp#4% |
| 209 |
\let#4#3% |
| 210 |
\let#3\tktl__tmp |
| 211 |
|
| 212 |
\fi |
| 213 |
} |
| 214 |
\def\tktl_encode_tokens_char_range_b#1#2{% #1=char non espace #2=macro recevant le charcode |
| 215 |
\expandafter\tktl_ifempty_or_space\expandafter{\tktl_gob_arg#1} |
| 216 |
{% |
| 217 |
\edef#2{\number\expandafter`\csname#1\endcsname}% |
| 218 |
} |
| 219 |
{% |
| 220 |
\errmessage{Multiple token '\detokenize{#1}', '0' inserted}% |
| 221 |
\edef#2{\number`\0}% |
| 222 |
}% |
| 223 |
} |
| 224 |
\let\charrange\tktl_encode_tokens_char_range |
| 225 |
|
| 226 |
%--------------------------------------------------------------------- |
| 227 |
%----------------- Macro \tktl_encode_tokensinterval ----------------- |
| 228 |
%--------------------------------------------------------------------- |
| 229 |
% \tktl_encode_tokensinterval admet un argument <intervalle> et |
| 230 |
% retourne dans 2 macros (arg #2 et #3) le minimum et maximum |
| 231 |
% correspondant à l'intervalle |
| 232 |
% <intervalle> = "n" ou "n-m" avec n et m entiers positifs |
| 233 |
% éventuellement vides |
| 234 |
% Utilisation : |
| 235 |
% \tktl_encode_tokensinterval{#1}\macroA\macroB |
| 236 |
% |
| 237 |
% Retour : |
| 238 |
% macros \macroA (borne inf) et \macroB (borne sup) |
| 239 |
%--------------------------------------------------------------------- |
| 240 |
\newcount\tktl_test_cnt |
| 241 |
\def\test_iftexnumber#1{\test_iftexnumber_i#1`\_nil} |
| 242 |
\def\test_iftexnumber_i#1`#2\_nil{\tktl_ifempty_or_space{#1}}% |
| 243 |
\def\tktl_ifinteger#1{% |
| 244 |
\tktl_ifempty_or_space{#1} |
| 245 |
{% |
| 246 |
\tktl_exec_second |
| 247 |
} |
| 248 |
{% |
| 249 |
\afterassignment\tktl_ifinteger_i |
| 250 |
\tktl_test_cnt\numexpr\test_iftexnumber{#1}{}0#1\relax\relax\_nil |
| 251 |
}% |
| 252 |
} |
| 253 |
\def\tktl_ifinteger_i#1\relax\_nil{\tktl_ifempty_or_space{#1}} |
| 254 |
\def\tktl_maxint_repeat{1114109} |
| 255 |
\def\tktl_encode_tokensinterval#1{% #1 est de la forme "n" ou "min-max" #2=macro recevant min #3=macro recevant max |
| 256 |
\tktl_encode_tokensinterval_a#1-#1-\_nil |
| 257 |
} |
| 258 |
\def\tktl_encode_tokensinterval_a#1-#2-#3\_nil#4#5{% |
| 259 |
\edef#4{\the\numexpr#1}% |
| 260 |
\edef#5{\the\numexpr#2}% |
| 261 |
} |
| 262 |
|
| 263 |
%--------------------------------------------------------------------- |
| 264 |
%--------------------- Macro \tktl_test_interval --------------------- |
| 265 |
%--------------------------------------------------------------------- |
| 266 |
% Utilisation : |
| 267 |
% \tktl_test_interval{#1}{#2}{true}{false} |
| 268 |
% - #1 _doit_ être un entier |
| 269 |
% - #2 est une liste i1,i2,...in où les éléments sont des intervalles |
| 270 |
% NE COMPORTANT PAS D'ESPACE de la forme |
| 271 |
% . * (wildcard) qui est toute valeur entière, renvoie true et bypasse les autres intervalles |
| 272 |
% . intervalle au sens de \tktl_encode_tokensinterval (forme "x" ou "x-y") |
| 273 |
% chaque intervalle est 1-développé avant d'etre lu |
| 274 |
% Retour : |
| 275 |
% true si #1 appartient à l'un des intervalles, false sinon |
| 276 |
%--------------------------------------------------------------------- |
| 277 |
\def\tktl_test_interval#1#2{% teste si l'entier #1 est dans la liste d'intervalles #2 de type "i..j" ou entier seul "i" |
| 278 |
\tktl_ifinstr{,*,}{,#2,} |
| 279 |
{% |
| 280 |
\tktl_exec_first |
| 281 |
} |
| 282 |
{% |
| 283 |
\edef\tktl_int{\the\numexpr#1\relax}% |
| 284 |
\tktl_test_interval_i#2,\end_parse,% |
| 285 |
}% |
| 286 |
} |
| 287 |
\def\tktl_test_interval_i#1,{% |
| 288 |
\ifx\end_parse#1% |
| 289 |
\let\tktl_do_next\tktl_exec_second |
| 290 |
\else |
| 291 |
\tktl_encode_tokensinterval{#1}\tktl_tmp_min\tktl_temp_max |
| 292 |
\ifnum\numexpr(\tktl_int-\tktl_tmp_min)*(\tktl_int-\tktl_temp_max)>0 % si n'appartient pas à [min ; max] |
| 293 |
\let\tktl_do_next\tktl_test_interval_i |
| 294 |
\else |
| 295 |
\let\tktl_do_next\tktl_test_interval_gobtoend |
| 296 |
\fi |
| 297 |
\fi |
| 298 |
\tktl_do_next |
| 299 |
} |
| 300 |
\def\tktl_test_interval_gobtoend#1\end_parse,{\tktl_exec_first} |
| 301 |
|
| 302 |
%--------------------------------------------------------------------- |
| 303 |
%--------------------- Macro \tktl_encode_tokens --------------------- |
| 304 |
%--------------------------------------------------------------------- |
| 305 |
% Utilisation : |
| 306 |
% \tktl_encode_tokens{#1} où #1 est un texte équilibré en tokens de catcodes 1 et 2 |
| 307 |
% |
| 308 |
% Retours : |
| 309 |
% 1) le registre de tokens \tktl_enctoks_toks contient la liste des tokens |
| 310 |
% constituant #1 sous la forme |
| 311 |
% (<x>:<y>) où: |
| 312 |
% - x est le charcode du token ou le token lui-même si séquence de contrôle |
| 313 |
% - y est le catcode du token ou 16 si séquence de contrôle |
| 314 |
% 2) le compteur \tktl_enctoks_len_cnt contient le nombre de tokens |
| 315 |
%--------------------------------------------------------------------- |
| 316 |
\newtoks\tktl_enctoks_toks |
| 317 |
\newcount\tktl_enctoks_len_cnt |
| 318 |
\long\def\tktl_encode_tokens#1{% |
| 319 |
\tktl_enctoks_len_cnt 0 |
| 320 |
\let\tktl_list_specials\empty |
| 321 |
\let\tktl_do_with_specials\tktl_scan_special |
| 322 |
\let\tktl_do_with_normals\tktl_gob_tok_and_loop |
| 323 |
% Première passe |
| 324 |
% tous charcodes des tokens spéciaux (catcode 1, 2, 6 et 10) sont mis dans \tktl_list_specials. |
| 325 |
% Les autres tokens (les "normaux") sont ignorés |
| 326 |
\tktl_encode_tokens_encode#1\end_parse |
| 327 |
% Avant la 2e passe : |
| 328 |
% On cherche à écarter les caractères actifs qui ont été lus comme tokens |
| 329 |
% spéciaux (car ils sont \let-égaux à un token spécial) |
| 330 |
% Pour ce faire, on modifie via \uccode et \uppercase tous les tokens contenus dans \tktl_list_specials. |
| 331 |
% Seuls les caractères actifs seront ainsi modifiés, ils sont neutralisés et rendus égaux à |
| 332 |
% \active_quark, ce qui leur enlèvera leur statut de spécial |
| 333 |
\tktl_active_specials |
| 334 |
% Deuxième passe |
| 335 |
% les caractères actifs ayant été reprogrammés, seuls les charcodes des _vrais_ |
| 336 |
% tokens spéciaux seront mis dans \tktl_list_specials |
| 337 |
\let\tktl_list_specials\empty |
| 338 |
\tktl_encode_tokens_encode#1\end_parse |
| 339 |
% Troisième passe |
| 340 |
\tktl_enctoks_toks{}% |
| 341 |
\let\tktl_do_with_specials\tktl_encode_specials |
| 342 |
\let\tktl_do_with_normals\tktl_encode_normal |
| 343 |
\tktl_encode_tokens_encode#1\end_parse |
| 344 |
} |
| 345 |
\long\def\tktl_encode_tokens_encode{% |
| 346 |
\futurelet\tktl_nxt_tok\tktl_encode_tokens_encode_i |
| 347 |
} |
| 348 |
\def\tktl_encode_tokens_encode_i{% |
| 349 |
\ifx\tktl_nxt_tok\end_parse |
| 350 |
\tktl_ok_this_test\tktl_encode_gob_to_end |
| 351 |
\fi% fin de parsing ? |
| 352 |
% les tokens "#", " ", "{" et "}" sont considérés spéciaux |
| 353 |
\ifcat##\noexpand\tktl_nxt_tok |
| 354 |
\tktl_ok_this_test{% |
| 355 |
\def\tok_catcode{6}% |
| 356 |
\tktl_do_with_specials |
| 357 |
}% |
| 358 |
\fi |
| 359 |
\ifcat\tktl_sp_token\noexpand\tktl_nxt_tok |
| 360 |
\tktl_ok_this_test{% |
| 361 |
\def\tok_catcode{10}% |
| 362 |
\tktl_do_with_specials |
| 363 |
}% |
| 364 |
\fi |
| 365 |
\ifcat\tktl_bg_token\noexpand\tktl_nxt_tok |
| 366 |
\tktl_ok_this_test{% |
| 367 |
\def\tok_catcode{1}% |
| 368 |
\tktl_do_with_specials |
| 369 |
}% |
| 370 |
\fi |
| 371 |
\ifcat\tktl_eg_token\noexpand\tktl_nxt_tok |
| 372 |
\tktl_ok_this_test{% |
| 373 |
\def\tok_catcode{2}% |
| 374 |
\tktl_do_with_specials |
| 375 |
} |
| 376 |
\fi |
| 377 |
\tktl_else_all_tests\tktl_do_with_normals |
| 378 |
} |
| 379 |
\def\tktl_encode_gob_to_end\end_parse{}% plus sûr que "\let\tktl_encode_gob_to_end\tktl_gob_arg" |
| 380 |
\def\tktl_scan_special{% |
| 381 |
\begingroup |
| 382 |
\escapechar\if\tktl_nxt_tok\string @`A\else`@\fi\relax% choisir le bon caractère d'échappement |
| 383 |
\expandafter\tktl_scan_special_i\string% pour le token qui suit. Si c'est une macro, seuls les espaces dans son nom seront comptabilisés en spéciaux |
| 384 |
} |
| 385 |
\def\tktl_scan_special_i{% |
| 386 |
\futurelet\tktl_nxt_tok\tktl_scan_special_ii |
| 387 |
} |
| 388 |
\def\tktl_scan_special_ii{% |
| 389 |
\ifcat\tktl_nxt_tok\tktl_sp_token |
| 390 |
\endgroup |
| 391 |
\tktl_addto_special_list{32}% tous les catcode 10 ont un charcode de 32 |
| 392 |
\tktl_antefi\tktl_gob_tok_and_loop |
| 393 |
\else |
| 394 |
\tktl_antefi\tktl_scan_special_iii |
| 395 |
\fi |
| 396 |
} |
| 397 |
\long\def\tktl_scan_special_iii#1{% traite les autres cas spéciaux |
| 398 |
\def\tktl__tmp{% |
| 399 |
\endgroup |
| 400 |
\tktl_addto_special_list}% |
| 401 |
\expandafter\tktl__tmp\expandafter{\number\tktl_ifnum{\escapechar=`#1 }{-1}{`#1} }% si macro : -1 (et eventuellement des 32 si son nom contient des espaces) |
| 402 |
\tktl_encode_tokens_encode |
| 403 |
} |
| 404 |
\def\tktl_active_specials{% |
| 405 |
\expandafter\tktl_active_specials_i\tktl_list_specials\active_quark,% |
| 406 |
} |
| 407 |
\begingroup |
| 408 |
\catcode0 13 |
| 409 |
\gdef\tktl_active_specials_i#1,{% |
| 410 |
\ifx\active_quark#1\else |
| 411 |
\unless\ifnum#1<0 |
| 412 |
\uccode0 #1 |
| 413 |
\uppercase{\let^^00\active_quark}% |
| 414 |
\fi |
| 415 |
\tktl_antefi\tktl_active_specials_i |
| 416 |
\fi |
| 417 |
} |
| 418 |
\endgroup |
| 419 |
\def\tktl_gob_tok_and_loop{% |
| 420 |
\afterassignment\tktl_encode_tokens_encode |
| 421 |
\let\tktl_nxt_tok= |
| 422 |
} |
| 423 |
\def\tktl_read_special_list{% |
| 424 |
\expandafter\tktl_read_special_list_i\tktl_list_specials\tktl_read_special_list |
| 425 |
} |
| 426 |
\def\tktl_read_special_list_i#1,#2\tktl_read_special_list{% |
| 427 |
\def\tktl_current_charcode{#1}% |
| 428 |
\def\tktl_list_specials{#2}% |
| 429 |
} |
| 430 |
\def\tktl_encode_specials{% |
| 431 |
\tktl_read_special_list |
| 432 |
\ifnum\tktl_current_charcode=-1 |
| 433 |
\tktl_antefi\tktl_encode_macro |
| 434 |
\else |
| 435 |
\expandafter\tktl_add_token\expanded{{\unexpanded\expandafter{\tktl_current_charcode}}{\tok_catcode}}% |
| 436 |
\tktl_antefi\tktl_gob_tok_and_loop |
| 437 |
\fi |
| 438 |
} |
| 439 |
\long\def\tktl_encode_macro#1{% |
| 440 |
\tktl_clean_special_list#1% enlève les "32" de la liste qui correspondent aux espaces dans les noms des macros |
| 441 |
\tktl_add_token{#1}{16}% |
| 442 |
\tktl_encode_tokens_encode |
| 443 |
} |
| 444 |
\def\tktl_clean_special_list#1{% |
| 445 |
\expandafter\tktl_clean_special_list_i\string#1 \end_parse |
| 446 |
} |
| 447 |
\def\tktl_clean_special_list_i#1 {% |
| 448 |
\futurelet\tktl_nxt_tok\tktl_clean_special_list_ii |
| 449 |
} |
| 450 |
\def\tktl_clean_special_list_ii{% |
| 451 |
\ifx\tktl_nxt_tok\end_parse |
| 452 |
\tktl_antefi\tktl_encode_gob_to_end |
| 453 |
\else |
| 454 |
\tktl_read_special_list |
| 455 |
\tktl_antefi\tktl_clean_special_list_i |
| 456 |
\fi |
| 457 |
} |
| 458 |
\long\def\tktl_encode_normal#1{% |
| 459 |
\ifcat\relax\noexpand#1% |
| 460 |
\tktl_add_token{#1}{16}% |
| 461 |
\else |
| 462 |
\expandafter\tktl_add_token\expanded{{\number`#1}{\number\tktl_thecatcode#1 }}% |
| 463 |
\fi |
| 464 |
\tktl_encode_tokens_encode |
| 465 |
} |
| 466 |
\long\def\tktl_add_token#1#2{% |
| 467 |
\advance\tktl_enctoks_len_cnt1 |
| 468 |
\tktl_add_to_parsetoks{(#1:#2)}% |
| 469 |
} |
| 470 |
% La macro \tktl_thecatcode{<token non spécial>} est purement développable |
| 471 |
% et renvoit le catcode de <token> |
| 472 |
\begingroup |
| 473 |
\edef\tktl_dotcatcode{\catcode\number`\.=} |
| 474 |
\tktl_dotcatcode3 \gdef\tktl_cat_C{.} |
| 475 |
\tktl_dotcatcode4 \gdef\tktl_cat_D{.} |
| 476 |
\tktl_dotcatcode7 \gdef\tktl_cat_G{.} |
| 477 |
\tktl_dotcatcode8 \gdef\tktl_cat_H{.} |
| 478 |
\tktl_dotcatcode11 \gdef\tktl_cat_K{.} |
| 479 |
\tktl_dotcatcode12 \gdef\tktl_cat_L{.} |
| 480 |
\tktl_dotcatcode13 \gdef\tktl_cat_M{.} |
| 481 |
\endgroup |
| 482 |
\def\tktl_thecatcode#1{% n'envisager les tokens non spéciaux (qui eux, ont un catcode de 1, 2, 6 ou 10) |
| 483 |
\ifcat\tktl_cat_K\noexpand#1\tktl_ok_this_test{11}\fi |
| 484 |
\ifcat\tktl_cat_L\noexpand#1\tktl_ok_this_test{12}\fi |
| 485 |
\ifcat\tktl_cat_C\noexpand#1\tktl_ok_this_test3\fi |
| 486 |
\ifcat\tktl_cat_D\noexpand#1\tktl_ok_this_test4\fi |
| 487 |
\ifcat\tktl_cat_G\noexpand#1\tktl_ok_this_test7\fi |
| 488 |
\ifcat\tktl_cat_H\noexpand#1\tktl_ok_this_test8\fi |
| 489 |
\ifcat\expandafter\noexpand\tktl_cat_M\noexpand#1\tktl_ok_this_test{13}\fi |
| 490 |
\tktl_else_all_tests{% |
| 491 |
\errmessage{This error in \string\tktl_thecatcode\space should not occur with token "\detokenize{#1}"}% |
| 492 |
}% |
| 493 |
} |
| 494 |
|
| 495 |
%--------------------------------------------------------------------- |
| 496 |
%--------------------- Macro \tktl_decode_enctoks -------------------- |
| 497 |
%--------------------------------------------------------------------- |
| 498 |
% Utilisation : |
| 499 |
% \tktl_decode_enctoks{#1}{#2} où #1 est une liste de tokens codés sous la forme et #2 une consigne d'assignation : #2{<résultat>} |
| 500 |
% (<x>:<y>) où: |
| 501 |
% - x est le charcode du token ou le token lui-même si séquence de contrôle |
| 502 |
% - y est le catcode du token ou 0 si séquence de contrôle |
| 503 |
% Retour : |
| 504 |
% L'assignation demandée via #2 contient les tokens |
| 505 |
% Si les tokens de catcode 1 et 2 ne sont pas équilibrés dans #1 -> erreur |
| 506 |
%--------------------------------------------------------------------- |
| 507 |
\newcount\tktl_openbrace_cnt |
| 508 |
\long\def\tktl_decode_enctoks#1#2{% #1=(a:b)(c:d)...(x:y) #2=assignation du résultat |
| 509 |
\begingroup |
| 510 |
\global\tktl_openbrace_cnt0 |
| 511 |
\tktl_enctoks_toks{}% |
| 512 |
\tktl_decode_enctoks_a#1(-1:-1)% |
| 513 |
\def\tktl__tmp{% |
| 514 |
\endgroup |
| 515 |
#2}% |
| 516 |
\expandafter\tktl__tmp\expandafter{\unexpanded\expandafter{\the\tktl_enctoks_toks}}% |
| 517 |
} |
| 518 |
\long\def\tktl_decode_enctoks_a(#1:#2){% #1=charcode ou macro si #2=0 ; #2=catcode ; #3=reliquat |
| 519 |
\ifnum#2=-1 |
| 520 |
\tktl_ok_this_test{% |
| 521 |
\tktl_ifnum{\tktl_openbrace_cnt>0 } |
| 522 |
{% trop d'accolades ouvrantes |
| 523 |
\errmessage{Unbalanced open-group token, \the\tktl_openbrace_cnt\space close-group token added}% |
| 524 |
\expandafter\tktl_decode_enctoks_b |
| 525 |
} |
| 526 |
{}% |
| 527 |
\tktl_gob_arg |
| 528 |
}% |
| 529 |
\fi |
| 530 |
\ifnum#2=16 |
| 531 |
\tktl_ok_this_test{% |
| 532 |
\tktl_add_to_parsetoks{#1}% |
| 533 |
}% |
| 534 |
\fi |
| 535 |
\ifnum#2=1 |
| 536 |
\tktl_ok_this_test{% |
| 537 |
\global\advance\tktl_openbrace_cnt1 |
| 538 |
\begingroup |
| 539 |
\tktl_enctoks_toks{}% |
| 540 |
\lccode`\{ #1 |
| 541 |
}% |
| 542 |
\fi |
| 543 |
\ifnum#2=2 |
| 544 |
\tktl_ok_this_test{% |
| 545 |
\tktl_ifnum{\tktl_openbrace_cnt<1 } |
| 546 |
{% |
| 547 |
\errmessage{Unbalanced close-group token ignored}% |
| 548 |
} |
| 549 |
{% |
| 550 |
\global\advance\tktl_openbrace_cnt-1 |
| 551 |
\lccode`\} #1 |
| 552 |
\lowercase{\toks0{\expandafter{\the\tktl_enctoks_toks}}}% |
| 553 |
\tktl_enctoks_toks\expandafter\expandafter\expandafter{\the\toks0}% |
| 554 |
\expandafter |
| 555 |
\endgroup |
| 556 |
\expandafter\tktl_add_to_parsetoks\expandafter{\the\tktl_enctoks_toks}% |
| 557 |
}% |
| 558 |
}% |
| 559 |
\fi |
| 560 |
\ifnum#2=6 |
| 561 |
\tktl_ok_this_test{% |
| 562 |
\begingroup |
| 563 |
\lccode`\# #1 |
| 564 |
\lowercase{% |
| 565 |
\endgroup |
| 566 |
\tktl_add_to_parsetoks{##}}% |
| 567 |
}% |
| 568 |
\fi |
| 569 |
\tktl_else_all_tests{% |
| 570 |
\begingroup |
| 571 |
\endlinechar-1 |
| 572 |
\everyeof{\noexpand}% |
| 573 |
\scantokens{% |
| 574 |
\catcode`\* #2 |
| 575 |
\lccode `\* #1 |
| 576 |
\catcode`\_ 11 |
| 577 |
\lowercase{% |
| 578 |
\endgroup |
| 579 |
\tktl_add_to_parsetoks{*}% |
| 580 |
}%* |
| 581 |
}% |
| 582 |
}% |
| 583 |
\tktl_decode_enctoks_a |
| 584 |
} |
| 585 |
\def\tktl_decode_enctoks_b{% ajoute accolade(s) fermante(s) manquantes à la fin du décodage si besoin |
| 586 |
\global\advance\tktl_openbrace_cnt-1 |
| 587 |
\lowercase\expandafter{\expandafter |
| 588 |
\endgroup |
| 589 |
\expandafter\tktl_add_to_parsetoks\expandafter{\expandafter{\the\tktl_enctoks_toks}}}% |
| 590 |
\ifnum\tktl_openbrace_cnt>0 |
| 591 |
\expandafter\tktl_decode_enctoks_b |
| 592 |
\fi |
| 593 |
} |
| 594 |
\long\def\tktl_add_to_parsetoks#1{\tktl_enctoks_toks\expandafter{\the\tktl_enctoks_toks#1}} |
| 595 |
|
| 596 |
%--------------------------------------------------------------------- |
| 597 |
%------------------ Capture d'un <motif élémentaire> ----------------- |
| 598 |
%--------------------------------------------------------------------- |
| 599 |
% Entrée : le motif est composé de : |
| 600 |
% - <capture> optionnelle indiquée par \c |
| 601 |
% - <prédicat> optionnel : "!" ou "&" |
| 602 |
% - macro ayant 1 caractère qui spécifie le type de motif : |
| 603 |
% \S{<chaine>} : le prochain token matche avec un des tokens de la <chaine> |
| 604 |
% \s{<chaine>} : les prochains tokens sont les mêmes que ceux de la <chaine> |
| 605 |
% \R{csv de charcodes range "<a>-<b>" ou "<c>" ou "*":csv de catcodes "<n>-<m>" ou "<x>" ou "*"} et si csv de catcodes absent: "*". Macros interdites dans les ranges. |
| 606 |
% \r{csv de chars range "<a>-<b>" ou "<c>":csv de catcodes "<n>-<m>" ou "<n>" ou "*"} et si csv de catcodes absent: "*". Macros interdites dans les ranges. |
| 607 |
% \. : correspond avec tout token (équiv à \R{*:*}) |
| 608 |
% {<motifs>} groupe de motifs (correspond à la macro interne \tktl_patterngroup) |
| 609 |
% - nombre de correspondance requis (optionnel): |
| 610 |
% + : 1 ou + , équivalant à ^{1-} |
| 611 |
% * : 0 ou + , équivalant à ^{0-} |
| 612 |
% ? : 0 ou 1 , équivalant à ^{0-1} |
| 613 |
% ^{n} ou ^{n-m} : de n (0 si absent) à m (max si absent) inlcus |
| 614 |
% si omis : ^{1} |
| 615 |
% - <capture> optionnelle : \c qui capturera seulement la position |
| 616 |
% |
| 617 |
% Renvoie : |
| 618 |
% - \tktl_current_precapture_directive est la capture demandée 0(aucune), 1(\c présent) |
| 619 |
% - \tktl_current_predicate est le prédicat capturé : 0 (aucun) 1 (&) ou -1 (!) |
| 620 |
% - \tktl_current_macro est la macro spécifiant le type de motif (\s, \S, \r, \R ou \.) |
| 621 |
% - \tktl_current_macro_arg est l'argument de la macro (forcément vide pour \.) |
| 622 |
% - \tktl_min_current_repeat est le nombre min de répétitions |
| 623 |
% - \tktl_max_current_repeat est le nombre min de répétitions |
| 624 |
% - \tktl_current_postcapture_directive est la capture demandée 0 (aucune) ou 1 (\c présent) |
| 625 |
%--------------------------------------------------------------------- |
| 626 |
\newcount\tktl_capture_pos_cnt% position d'un token (si on capture la position) |
| 627 |
\newcount\tktl_position_saved_cnt% position sauvegardée d'un token (si on capture la position) |
| 628 |
\newcount\tktl_capture_pos_index_cnt% numéro de la demande de capture de position |
| 629 |
\def\tktl_macro_dot{\.}% |
| 630 |
\def\tktl_macro_s{\s}% |
| 631 |
\def\tktl_macro_S{\S}% |
| 632 |
\def\tktl_macro_r{\r}% |
| 633 |
\def\tktl_macro_R{\R}% |
| 634 |
\def\tktl_macro_group{\tktl_patterngroup} |
| 635 |
\def\tktl_macro_pattern{\tktl_patternname} |
| 636 |
\newif\iftktl_allow_captures_% tenir compte des captures par \c ou pas ? |
| 637 |
|
| 638 |
\long\def\tktl_grab_pattern#1{% #1= pattern unique ou {<groupe de motifs>} |
| 639 |
\let\tktl_current_predicate\empty |
| 640 |
\let\tktl_current_macro\empty |
| 641 |
\let\tktl_current_macro_arg\empty |
| 642 |
\let\tktl_current_repeat\empty |
| 643 |
\def\tktl_current_precapture_directive{0}% |
| 644 |
\tktl_grab_pattern_search_precapture#1\_nil |
| 645 |
} |
| 646 |
\def\tktl_grab_pattern_search_precapture{% |
| 647 |
\tktl_futurelet_nospace\tktl__futurtok\tktl_grab_pattern_search_precapture_a |
| 648 |
} |
| 649 |
\def\tktl_grab_pattern_search_precapture_a{% |
| 650 |
\ifx\tktl__futurtok\c |
| 651 |
\iftktl_allow_captures_ |
| 652 |
\def\tktl_current_precapture_directive{1}% |
| 653 |
\fi |
| 654 |
\tktl_antefi\tktl_grab_pattern_grab_precapture |
| 655 |
\else |
| 656 |
\tktl_antefi\tktl_grab_pattern_search_predicate |
| 657 |
\fi |
| 658 |
} |
| 659 |
\def\tktl_grab_pattern_grab_precapture#1{% #1=\c |
| 660 |
\tktl_futurelet_nospace\tktl__futurtok\tktl_grab_pattern_search_predicate |
| 661 |
} |
| 662 |
\def\tktl_grab_pattern_search_predicate{% |
| 663 |
\ifx!\tktl__futurtok |
| 664 |
\tktl_ok_this_test{% |
| 665 |
\def\tktl_current_predicate{-1}% |
| 666 |
\tktl_grab_predicate |
| 667 |
}% |
| 668 |
\fi |
| 669 |
\ifx&\tktl__futurtok |
| 670 |
\tktl_ok_this_test{% |
| 671 |
\def\tktl_current_predicate{1}% |
| 672 |
\tktl_grab_predicate |
| 673 |
}% |
| 674 |
\fi |
| 675 |
\tktl_else_all_tests{% |
| 676 |
\def\tktl_current_predicate{0}% |
| 677 |
\tktl_test_post_predicate |
| 678 |
}% |
| 679 |
} |
| 680 |
\def\tktl_grab_predicate#1{% #1= ! ou & |
| 681 |
\tktl_futurelet_nospace\tktl__futurtok\tktl_test_post_predicate |
| 682 |
} |
| 683 |
\def\tktl_test_post_predicate{% |
| 684 |
\ifx\bgroup\tktl__futurtok% si après le prédicat il y a "{" |
| 685 |
\tktl_antefi\tktl_grab_group_of_patterns% prendre le groupe de patterns |
| 686 |
\else |
| 687 |
\tktl_antefi\tktl_grab_pattern_grab_macro% sinon, prendre la macro |
| 688 |
\fi |
| 689 |
} |
| 690 |
\long\def\tktl_grab_group_of_patterns#1{% #1= ensemble de motifs entre accolades |
| 691 |
\def\tktl_current_macro{\tktl_patterngroup}% |
| 692 |
\def\tktl_current_macro_arg{#1}% |
| 693 |
\tktl_grab_pattern_grab_repeat_char |
| 694 |
} |
| 695 |
\def\tktl_grab_pattern_grab_macro#1{% #1 = macro |
| 696 |
\tktl_ifinstr{,#1,}{,\s,\S,\r,\R,\.,} |
| 697 |
{% |
| 698 |
\def\tktl_current_macro{#1}% |
| 699 |
\tktl_ifx{\.#1} |
| 700 |
{% |
| 701 |
\tktl_grab_pattern_grab_repeat_char |
| 702 |
} |
| 703 |
{% aller capturer l'argument de la macro |
| 704 |
\tktl_futurelet_nospace\tktl__futurtok\tktl_grab_pattern_grab_macro_arg |
| 705 |
}% |
| 706 |
} |
| 707 |
{% |
| 708 |
\tktl_ifmacro{#1} |
| 709 |
{% aller voir si c'est une macro-pattern |
| 710 |
\tktl_grab_pattern_grab_macro_pattern#1% |
| 711 |
} |
| 712 |
{% |
| 713 |
\errmessage{Found "\detokenize{#1}" when expecting \string\r, \string\R, \string\s, \string\S\space or \string\.}% |
| 714 |
} |
| 715 |
}% |
| 716 |
} |
| 717 |
\def\tktl_grab_pattern_grab_macro_pattern#1{% #1=macro donnée par l'utilisateur |
| 718 |
\def\tktl_current_macro{\tktl_patternname}% |
| 719 |
\def\tktl_current_macro_arg{#1}% |
| 720 |
\tktl_grab_pattern_grab_repeat_char |
| 721 |
} |
| 722 |
\def\tktl_grab_pattern_grab_macro_arg{% |
| 723 |
\ifx\bgroup\tktl__futurtok |
| 724 |
\tktl_antefi\tktl_grab_pattern_grab_macro_arg_a |
| 725 |
\else |
| 726 |
\errmessage{No open brace found after "\detokenize\expandafter{\tktl_current_macro}", aborting pattern}% |
| 727 |
\let\tktl_current_predicate\empty |
| 728 |
\let\tktl_current_macro\empty |
| 729 |
\let\tktl_current_macro_arg\empty |
| 730 |
\let\tktl_current_repeat\empty |
| 731 |
\tktl_antefi\tktl_gob_enctoks_to_end_parse |
| 732 |
\fi |
| 733 |
} |
| 734 |
\def\tktl_gob_enctoks_to_end_parse#1\end_parse{} |
| 735 |
\long\def\tktl_grab_pattern_grab_macro_arg_a#1{% argument de la macro |
| 736 |
\def\tktl_current_macro_arg{#1}% |
| 737 |
\tktl_grab_pattern_grab_repeat_char |
| 738 |
} |
| 739 |
\def\tktl_grab_pattern_grab_repeat_char{% |
| 740 |
\tktl_futurelet_nospace\tktl__futurtok\tktl_grab_pattern_grab_repeat_char_a |
| 741 |
} |
| 742 |
\def\tktl_grab_pattern_grab_repeat_char_a{% |
| 743 |
\def\tktl_min_current_repeat{1}% |
| 744 |
\def\tktl_max_current_repeat{1}% 1 répétition a priori |
| 745 |
\ifx*\tktl__futurtok |
| 746 |
\tktl_ok_this_test{% |
| 747 |
\def\tktl_min_current_repeat{0}% |
| 748 |
\let\tktl_max_current_repeat\tktl_maxint_repeat |
| 749 |
\tktl_grab_pattern_gobble_char |
| 750 |
}% |
| 751 |
\fi |
| 752 |
\ifx+\tktl__futurtok |
| 753 |
\tktl_ok_this_test{% |
| 754 |
\def\tktl_min_current_repeat{1}% |
| 755 |
\let\tktl_max_current_repeat\tktl_maxint_repeat |
| 756 |
\tktl_grab_pattern_gobble_char |
| 757 |
}% |
| 758 |
\fi |
| 759 |
\ifx?\tktl__futurtok |
| 760 |
\tktl_ok_this_test{% |
| 761 |
\def\tktl_min_current_repeat{0}% |
| 762 |
\def\tktl_max_current_repeat{1}% |
| 763 |
\tktl_grab_pattern_gobble_char |
| 764 |
}% |
| 765 |
\fi |
| 766 |
\ifx^\tktl__futurtok |
| 767 |
\tktl_ok_this_test\tktl_grab_pattern_grab_repeat_arg |
| 768 |
\fi |
| 769 |
\tktl_else_all_tests\tktl_grab_pattern_search_postcapture |
| 770 |
} |
| 771 |
\def\tktl_grab_pattern_gobble_char#1{% #1 = + * ? ou ^ |
| 772 |
\tktl_grab_pattern_search_postcapture |
| 773 |
} |
| 774 |
\def\tktl_grab_pattern_grab_repeat_arg^#1{% #1=argument spécifiant le nombre de répétitions |
| 775 |
\tktl_encode_tokensinterval{#1}\tktl_min_current_repeat\tktl_max_current_repeat |
| 776 |
\tktl_grab_pattern_search_postcapture |
| 777 |
} |
| 778 |
\def\tktl_grab_pattern_search_postcapture{% |
| 779 |
\tktl_futurelet_nospace\tktl__futurtok\tktl_grab_pattern_search_postcapture_a |
| 780 |
} |
| 781 |
\def\tktl_grab_pattern_search_postcapture_a{% |
| 782 |
\ifx\c\tktl__futurtok |
| 783 |
\iftktl_allow_captures_ |
| 784 |
\iftktl_allow_post_pattern_catpure_ |
| 785 |
\def\tktl_current_postcapture_directive{1}% |
| 786 |
\let\tktl_do_next\tktl_grab_pattern_grab_postcapture |
| 787 |
\else |
| 788 |
\errmessage{Capture after a pattern not allowed, \string\c\space ignored}% |
| 789 |
\def\tktl_current_postcapture_directive{0}% |
| 790 |
\let\tktl_do_next\tktl_grab_pattern_grab_postcapture |
| 791 |
\fi |
| 792 |
\else |
| 793 |
\def\tktl_current_postcapture_directive{0}% |
| 794 |
\let\tktl_do_next\tktl_grab_pattern_grab_postcapture |
| 795 |
\fi |
| 796 |
\else |
| 797 |
\def\tktl_current_postcapture_directive{0}% |
| 798 |
\let\tktl_do_next\tktl_grab_pattern_remain |
| 799 |
\fi |
| 800 |
\tktl_do_next |
| 801 |
} |
| 802 |
\def\tktl_grab_pattern_grab_postcapture\c{\tktl_grab_pattern_remain} |
| 803 |
\def\tktl_grab_pattern_remain#1\_nil{% |
| 804 |
\tktl_ifempty_or_space{#1} |
| 805 |
{} |
| 806 |
{% |
| 807 |
\errmessage{Unexpected "\detokenize{#1}" found, I ignore it}% |
| 808 |
}% |
| 809 |
} |
| 810 |
\def\tktl_collect_pattern_remain#1\_nil{% |
| 811 |
\def\tktl_after_pattern{#1}% |
| 812 |
} |
| 813 |
\newif\iftktl_if_single_pattern_% vrai si pattern unique (faux pour un groupe ou plusieurs patterns) |
| 814 |
\def\tktl_grab_pattern_remain_test_single#1\_nil{% |
| 815 |
\ifx\tktl_current_macro\tktl_macro_group |
| 816 |
\tktl_if_single_pattern_false |
| 817 |
\else |
| 818 |
\tktl_ifempty_or_space{#1} |
| 819 |
\tktl_if_single_pattern_true |
| 820 |
\tktl_if_single_pattern_false |
| 821 |
\fi |
| 822 |
} |
| 823 |
|
| 824 |
%--------------------------------------------------------------------- |
| 825 |
%-------------- changement de catcode dans des <tokens> -------------- |
| 826 |
%--------------------------------------------------------------------- |
| 827 |
% La macro \tktl_catcode_string{<chaine>} agit sur <chaine> lorsqu'elle |
| 828 |
% contient : |
| 829 |
% - \c{<nombre>}{<tokens>} ou \c{<nombre>}<token> |
| 830 |
% - \c<chiffre>{<tokens>} ou \c<chiffre><token> |
| 831 |
% et modifie le catcode des tokens concernés. |
| 832 |
% Limitations : |
| 833 |
% 1) la macro \c ne peut pas se trouver dans son 2e argument (imbrication impossible) |
| 834 |
% 2) le premier argument doit être un catcode valide (1,2,3,4,6,7,8,10,11,12 ou 13) |
| 835 |
% 3) si une \macro se trouve dans la <chaine>, le seul catcode qu'elle peut revêtir est 12 |
| 836 |
% et dans ce cas, les tokens provenant de \string\macro sont créés. |
| 837 |
% Si le catcode demandé est différent de 12, la macro reste inchangée avec son catcode de 16 |
| 838 |
% |
| 839 |
% Entrée : la <chaine> qui contient 0, 1 ou plusieurs occurences de \c avec ses 2 arguments |
| 840 |
% Sortie : |
| 841 |
% - le registre de tokens \tktl_string_toks qui contient les tokens composants la <chaine> |
| 842 |
% encodés sous la forme (<charcode>:<catcode>) |
| 843 |
% - le compteur \tktl_string_len_cnt contient le nombre de tokens de la <chaine> |
| 844 |
%--------------------------------------------------------------------- |
| 845 |
\newtoks\tktl_string_toks |
| 846 |
\newcount\tktl_string_len_cnt |
| 847 |
\long\def\tktl_catcode_string#1{% #1=tokens contenant ou pas \c |
| 848 |
\tktl_encode_tokens{#1}% |
| 849 |
\tktl_string_len_cnt=\tktl_enctoks_len_cnt |
| 850 |
\expanded{\noexpand\tktl_ifinstr{(\noexpand\c:16)}{\the\tktl_enctoks_toks}}% |
| 851 |
{% si la chaine de tokens contient "\c" |
| 852 |
\tktl_string_toks{}% |
| 853 |
\expandafter\tktl_catcode_string_a\the\tktl_enctoks_toks(0:-1)% |
| 854 |
} |
| 855 |
{% |
| 856 |
\tktl_string_toks=\tktl_enctoks_toks |
| 857 |
}% |
| 858 |
} |
| 859 |
\def\tktl_catcode_string_a#1(\c:16)(#2:#3){% |
| 860 |
\advance\tktl_string_len_cnt -1 % retirer le token "\c" |
| 861 |
\tktl_addtotoks\tktl_string_toks{#1}% |
| 862 |
\ifnum#3=1 % \c est suivi de "{" |
| 863 |
\advance\tktl_string_len_cnt -1 % retirer le token "{" |
| 864 |
\let\tktl_catcode_directive\empty |
| 865 |
\tktl_antefi\tktl_catcode_string_b |
| 866 |
\else%\c n'est pas suivi de "{" -> le chiffre #2 est le catcode demandé |
| 867 |
% TODO : vérifier que #3=12 ??? |
| 868 |
\edef\tktl_catcode_directive{\tktl_charcode_to_digit{#2}}% |
| 869 |
\tktl_antefi\tktl_catcode_string_c |
| 870 |
\fi |
| 871 |
} |
| 872 |
\def\tktl_catcode_string_b(#1:#2){% \c est suivi de "{" qu'on a déjà lu |
| 873 |
\advance\tktl_string_len_cnt -1 % retirer le token correspondant au chiffre du catcode ou à "}" |
| 874 |
\ifnum#2=2 % si #1="}" -> fini de lire le catcode demandé |
| 875 |
\expandafter\tktl_ifempty_or_space\expandafter{\tktl_catcode_directive} |
| 876 |
{% vérifier que ce catcode n'est pas vide |
| 877 |
\errmessage{Empty argument found after\string\c}% |
| 878 |
\def\tktl_catcode_directive{11}% |
| 879 |
} |
| 880 |
{% et qu'il n'est pas illégal |
| 881 |
\expandafter\tktl_ifinstr\expandafter{\expandafter,\tktl_catcode_directive,}{,1,2,3,4,6,7,8,10,11,12,13,} |
| 882 |
{} |
| 883 |
{% |
| 884 |
\errmessage{Illegal catcode of \tktl_catcode_directive\space in argument of\string\c}% |
| 885 |
\def\tktl_catcode_directive{11}% |
| 886 |
}% |
| 887 |
}% |
| 888 |
\tktl_antefi\tktl_catcode_string_c |
| 889 |
\else% pas d'accolade ouvrante -> on ajoute le chiffre et on recommence |
| 890 |
\edef\tktl_catcode_directive{\tktl_catcode_directive\tktl_charcode_to_digit{#1}}% |
| 891 |
\tktl_antefi\tktl_catcode_string_b |
| 892 |
\fi |
| 893 |
} |
| 894 |
\newtoks\tktl_string_collect_toks |
| 895 |
\def\tktl_catcode_string_c(#1:#2){% \c<catcode> ou \c{<catcode>} a été lu et le <catocde> est dans \tktl_catcode_directive |
| 896 |
\ifnum#2=1% si c'est suivi d'une accolade ouvrante |
| 897 |
\def\tktl_bg_number{0}% nombre d'accolades ouvrante rencontrées |
| 898 |
\tktl_string_collect_toks{}% |
| 899 |
\advance\tktl_string_len_cnt -1 % retirer le token "{" |
| 900 |
\expandafter\tktl_catcode_string_e |
| 901 |
\else% n'est pas suivi d'une "{" |
| 902 |
\ifnum#2=16 % si #1 est une macro |
| 903 |
\ifnum\tktl_catcode_directive=12 % à détokéniser ? |
| 904 |
\begingroup |
| 905 |
\expandafter\tktl_encode_tokens\expandafter{\string#1}% |
| 906 |
\expanded{% |
| 907 |
\endgroup |
| 908 |
\noexpand\tktl_addtotoks\noexpand\tktl_string_toks{\the\tktl_enctoks_toks}% |
| 909 |
\advance\noexpand\tktl_string_len_cnt\the\tktl_enctoks_len_cnt\relax% ajoute les tokens provenant de \string#1 |
| 910 |
}% |
| 911 |
\else |
| 912 |
\tktl_addtotoks\tktl_string_toks{(#1:16)}% |
| 913 |
\fi |
| 914 |
\else |
| 915 |
\tktl_addtotoks\tktl_string_toks{(#1:}% |
| 916 |
\tktl_eaddtotoks\tktl_string_toks{\tktl_catcode_directive)}% |
| 917 |
\fi |
| 918 |
\tktl_antefi\tktl_catcode_string_d |
| 919 |
\fi |
| 920 |
} |
| 921 |
\def\tktl_catcode_string_d#1(0:-1){% |
| 922 |
\tktl_ifinstr{(\c:16)}{#1} |
| 923 |
{% |
| 924 |
\tktl_catcode_string_a#1(0:-1)% |
| 925 |
} |
| 926 |
{% |
| 927 |
\tktl_addtotoks\tktl_string_toks{#1}% |
| 928 |
}% |
| 929 |
} |
| 930 |
\def\tktl_catcode_string_e(#1:#2){% \c\<catcode> était suivi d'une accolade |
| 931 |
\let\tktl_do_next\tktl_catcode_string_e% on reboucle à priori |
| 932 |
\ifnum#2=2 % si accolade fermante |
| 933 |
\ifnum\tktl_bg_number=0 % et équilibrage en accolades -> fin de l'argument |
| 934 |
\advance\tktl_string_len_cnt -1 % retirer le token "}" |
| 935 |
\tktl_eaddtotoks\tktl_string_toks{\the\tktl_string_collect_toks}% |
| 936 |
\let\tktl_do_next\tktl_catcode_string_d |
| 937 |
\else% sinon, ajouter l'accolade fermante avec le catcode demandé |
| 938 |
\tktl_addtotoks\tktl_string_collect_toks{(#1:}% |
| 939 |
\tktl_eaddtotoks\tktl_string_collect_toks{\tktl_catcode_directive)}% |
| 940 |
\edef\tktl_bg_number{\the\numexpr\tktl_bg_number-1}% et décrémenter |
| 941 |
\fi |
| 942 |
\else |
| 943 |
\ifnum#2=1 % si accolade ouvrante |
| 944 |
\edef\tktl_bg_number{\the\numexpr\tktl_bg_number-1}% incrémenter |
| 945 |
\tktl_addtotoks\tktl_string_collect_toks{(#1:}% |
| 946 |
\tktl_eaddtotoks\tktl_string_collect_toks{\tktl_catcode_directive)}% |
| 947 |
\else |
| 948 |
\ifnum#2=16 % si macro |
| 949 |
\ifnum\tktl_catcode_directive=12 % à détokéniser ? |
| 950 |
\begingroup |
| 951 |
\expandafter\tktl_encode_tokens\expandafter{\string#1}% |
| 952 |
\advance\tktl_enctoks_len_cnt -1 % pour compenser le token que représente la macro #1 elle-même |
| 953 |
\expanded{% |
| 954 |
\endgroup |
| 955 |
\noexpand\tktl_addtotoks\noexpand\tktl_string_collect_toks{\the\tktl_enctoks_toks}% |
| 956 |
\advance\noexpand\tktl_string_len_cnt\the\tktl_enctoks_len_cnt\relax% ajoute les tokens provenant de \string#1 |
| 957 |
}% |
| 958 |
\else |
| 959 |
\tktl_addtotoks\tktl_string_collect_toks{(#1:}% |
| 960 |
\tktl_addtotoks\tktl_string_collect_toks{16)}% |
| 961 |
\fi |
| 962 |
\else |
| 963 |
\tktl_addtotoks\tktl_string_collect_toks{(#1:}% |
| 964 |
\tktl_eaddtotoks\tktl_string_collect_toks{\tktl_catcode_directive)}% |
| 965 |
\fi |
| 966 |
\fi |
| 967 |
\fi |
| 968 |
\tktl_do_next |
| 969 |
} |
| 970 |
\def\tktl_charcode_to_digit#1{% |
| 971 |
\ifcase\numexpr#1-`\0\relax |
| 972 |
0\or1\or2\or3\or4\or5\or6\or7\or8\or9% |
| 973 |
\else |
| 974 |
\errmessage{Catcode directive not a number}% |
| 975 |
\fi |
| 976 |
} |
| 977 |
|
| 978 |
%--------------------------------------------------------------------- |
| 979 |
%--- Test si un token matche les intervalles de charcode et catcode -- |
| 980 |
%--------------------------------------------------------------------- |
| 981 |
% teste si les entiers #1 et #2 sont dans les intervalles #3 et #4. |
| 982 |
\long\def\tktl_iftokmatch(#1:#2)#3#4{% #1=tok_charcode #2=tok_catcode #3=interval_charcode #4=interval_catcode |
| 983 |
\tktl_ifnum{#2=16 }% si le token est une macro |
| 984 |
{% |
| 985 |
\tktl_test_interval{#2}{#4}% |
| 986 |
{% 16 est dans l'intervalle des catcodes |
| 987 |
\def\tktl_temp_macro_charcode_interval{#3}% |
| 988 |
\tktl_ifx{\tktl_temp_macro_charcode_interval\tktl_star} |
| 989 |
{% si interval charcode = "*" -> renvoyer vrai |
| 990 |
\tktl_exec_first |
| 991 |
} |
| 992 |
{% |
| 993 |
\tktl_ifinstr{#1}{#3}% la macro #1 est-elle dans la liste de macros #3 ? |
| 994 |
}% |
| 995 |
} |
| 996 |
{% |
| 997 |
\tktl_exec_second |
| 998 |
}% |
| 999 |
} |
| 1000 |
{% |
| 1001 |
\tktl_test_interval{#1}{#3} |
| 1002 |
{% charcode correspond |
| 1003 |
\tktl_ifempty_or_space{#4} |
| 1004 |
{% si catcode non spécifié -> wildcard |
| 1005 |
\tktl_exec_first |
| 1006 |
} |
| 1007 |
{% si catcode spécifié |
| 1008 |
\tktl_test_interval{#2}{#4}% |
| 1009 |
}% |
| 1010 |
} |
| 1011 |
{% mauvais charcode -> renvoyer faux |
| 1012 |
\tktl_exec_second |
| 1013 |
}% |
| 1014 |
}% |
| 1015 |
} |
| 1016 |
%--------------------------------------------------------------------- |
| 1017 |
%----------------------- Compilation des motifs ---------------------- |
| 1018 |
%--------------------------------------------------------------------- |
| 1019 |
\newtoks\tktl_pattern_compile_collect |
| 1020 |
\def\tktl_compile_patterns{% compile les patterns avec les opérateurs "|" et ":" |
| 1021 |
\begingroup |
| 1022 |
\tktl_pattern_compile_collect{}% |
| 1023 |
\let\tktl_grab_pattern_remain\tktl_collect_pattern_remain |
| 1024 |
\tktl_compile_patterns_a |
| 1025 |
} |
| 1026 |
\def\tktl_compile_patterns_a#1{% |
| 1027 |
\tktl_ifempty_or_space{#1} |
| 1028 |
{% |
| 1029 |
\expanded{% |
| 1030 |
\endgroup |
| 1031 |
\noexpand\tktl_pattern_compile_collect{\the\tktl_pattern_compile_collect}}% |
| 1032 |
} |
| 1033 |
{% |
| 1034 |
\tktl_compile_single_pattern{#1}% |
| 1035 |
\expandafter\tktl_ifempty_or_space\expandafter{\tktl_after_pattern}% |
| 1036 |
{% |
| 1037 |
\expandafter\tktl_compile_patterns_a\expandafter{\tktl_after_pattern}% |
| 1038 |
} |
| 1039 |
{% |
| 1040 |
\expandafter\tktl_futurelet_nospace\expandafter\tktl__futurtok\expandafter\tktl_compile_patterns_b\tktl_after_pattern\_nil |
| 1041 |
}% |
| 1042 |
}% |
| 1043 |
} |
| 1044 |
\def\tktl_compile_patterns_b{% |
| 1045 |
\ifx|\tktl__futurtok |
| 1046 |
\tktl_ok_this_test\tktl_compile_patterns_c |
| 1047 |
\fi |
| 1048 |
\ifx:\tktl__futurtok |
| 1049 |
\tktl_ok_this_test\tktl_compile_patterns_c |
| 1050 |
\fi |
| 1051 |
\tktl_else_all_tests\tktl_compile_patterns_d% erreur |
| 1052 |
} |
| 1053 |
\def\tktl_compile_patterns_c#1#2\_nil{% #1= | ou : #2=reste |
| 1054 |
\tktl_addtotoks\tktl_pattern_compile_collect{#1}% |
| 1055 |
\tktl_compile_patterns_a{#2}% |
| 1056 |
} |
| 1057 |
\def\tktl_compile_patterns_d#1\_nil{% |
| 1058 |
\errmessage{Something unexpected in pattern here "\detokenize{#1}"}% |
| 1059 |
} |
| 1060 |
\def\tktl_compile_single_pattern#1{% #1=<1-pattern> |
| 1061 |
\tktl_grab_pattern{#1}% |
| 1062 |
\edef\tktl_five_args{{\tktl_current_precapture_directive}{\tktl_current_predicate}{\tktl_min_current_repeat}{\tktl_max_current_repeat}{\tktl_current_postcapture_directive}}% |
| 1063 |
\csname tktl_compile_pattern_\expandafter\string\tktl_current_macro\endcsname |
| 1064 |
} |
| 1065 |
\def\tktl_define_Rr_test#1#2{% |
| 1066 |
\long\def\tktl_token_test_R_or_S(##1:##2){\tktl_iftokmatch(##1:##2){#1}{#2}}% |
| 1067 |
} |
| 1068 |
\def\tktl_define_S_test#1{% |
| 1069 |
\long\def\tktl_token_test_R_or_S(##1:##2){\tktl_ifinstr{(##1:##2)}{#1}}% |
| 1070 |
} |
| 1071 |
\tktl_csdef{tktl_compile_pattern_\expandafter\string\tktl_macro_R}{% |
| 1072 |
\expandafter\tktl_analyse_R_pattern\expandafter{\tktl_current_macro_arg}% séparer csv charcode et csv catcode |
| 1073 |
\tktl_xaddtotoks\tktl_pattern_compile_collect{% |
| 1074 |
\tktl_five_args |
| 1075 |
\noexpand\tktl_define_Rr_test{\tktl_charcode_interval}{\tktl_catcode_interval}% |
| 1076 |
\noexpand\tktl_test_pattern_R_r_S% |
| 1077 |
}% |
| 1078 |
} |
| 1079 |
\tktl_csdef{tktl_compile_pattern_\expandafter\string\tktl_macro_r}{% |
| 1080 |
\expandafter\tktl_analyse_R_pattern\expandafter{\tktl_current_macro_arg}% séparer csv charcode et csv catcode |
| 1081 |
\expandafter\tktl_convert_charcsv_to_charcodecsv\expandafter{\tktl_charcode_interval}% |
| 1082 |
\tktl_xaddtotoks\tktl_pattern_compile_collect{% |
| 1083 |
\tktl_five_args |
| 1084 |
\noexpand\tktl_define_Rr_test{\tktl_charcode_interval}{\tktl_catcode_interval}% |
| 1085 |
\noexpand\tktl_test_pattern_R_r_S% |
| 1086 |
}% |
| 1087 |
} |
| 1088 |
\tktl_csdef{tktl_compile_pattern_\expandafter\string\tktl_macro_S}{% |
| 1089 |
\expandafter\tktl_catcode_string\expandafter{\tktl_current_macro_arg}% prendre en compte les \c{<catcode>}{texte} dans l'argument de \S |
| 1090 |
\tktl_xaddtotoks\tktl_pattern_compile_collect{% |
| 1091 |
\tktl_five_args |
| 1092 |
\noexpand\tktl_define_S_test{\the\tktl_string_toks}% |
| 1093 |
\noexpand\tktl_test_pattern_R_r_S% |
| 1094 |
}% |
| 1095 |
} |
| 1096 |
\def\tktl_test_pattern_R_r_S{% |
| 1097 |
\expandafter\tktl_test_match_R_or_S\tktl_postmatch_enctokens(-1:-1)% |
| 1098 |
\tktl_test_match_pattern_a |
| 1099 |
} |
| 1100 |
\tktl_csdef{tktl_compile_pattern_\expandafter\string\tktl_macro_s}{% |
| 1101 |
\expandafter\tktl_catcode_string\expandafter{\tktl_current_macro_arg}% prendre en compte les \c{<catcode>}{texte} dans l'argument de \s |
| 1102 |
\tktl_xaddtotoks\tktl_pattern_compile_collect{% |
| 1103 |
\tktl_five_args |
| 1104 |
\noexpand\tktl_test_pattern_s{\the\tktl_string_toks}% |
| 1105 |
}% |
| 1106 |
} |
| 1107 |
\def\tktl_test_pattern_s#1{% |
| 1108 |
\def\tktl__tmp{\tktl_test_match_s{#1}}\expandafter\tktl__tmp\expandafter{\tktl_postmatch_enctokens}% |
| 1109 |
\tktl_test_match_pattern_a |
| 1110 |
} |
| 1111 |
\tktl_csdef{tktl_compile_pattern_\expandafter\string\tktl_macro_dot}{% |
| 1112 |
\tktl_eaddtotoks\tktl_pattern_compile_collect{% |
| 1113 |
\tktl_five_args |
| 1114 |
\tktl_test_pattern_dot |
| 1115 |
}% |
| 1116 |
} |
| 1117 |
\def\tktl_test_pattern_dot{% |
| 1118 |
\expandafter\tktl_test_match_dot\tktl_postmatch_enctokens(-1:-1)% |
| 1119 |
\tktl_test_match_pattern_a |
| 1120 |
} |
| 1121 |
\tktl_csdef{tktl_compile_pattern_\expandafter\string\tktl_macro_group}{% |
| 1122 |
\begingroup |
| 1123 |
\expandafter\tktl_compile_patterns\expandafter{\tktl_current_macro_arg}% |
| 1124 |
\expanded{% |
| 1125 |
\endgroup |
| 1126 |
\noexpand\tktl_addtotoks\noexpand\tktl_pattern_compile_collect{% |
| 1127 |
\tktl_five_args |
| 1128 |
\noexpand\tktl_test_pattern_group{\the\tktl_pattern_compile_collect}% |
| 1129 |
}% |
| 1130 |
}% |
| 1131 |
} |
| 1132 |
\def\tktl_test_pattern_group#1{% |
| 1133 |
\begingroup |
| 1134 |
\tktl_repeat_match_cnt 0 |
| 1135 |
\iftktl_in_capture_ |
| 1136 |
\tktl_in_patterngroup_true |
| 1137 |
\else% si pas de capture en cours -> captures permises à l'intérieur du groupe |
| 1138 |
\tktl_in_patterngroup_false |
| 1139 |
\fi |
| 1140 |
\let\tktl_match_tokens_patterngroup\empty |
| 1141 |
\def\tktl_current_macro_arg{#1}% |
| 1142 |
\tktl_test_match_patterngroup% |
| 1143 |
\expanded{% |
| 1144 |
\endgroup |
| 1145 |
\iftktl_match_success_ |
| 1146 |
\tktl_unexpand_earg\def\tktl_postmatch_enctokens{\tktl_postmatch_enctokens}% |
| 1147 |
\tktl_unexpand_earg\def\tktl_match_tokens{\tktl_match_tokens_patterngroup}% |
| 1148 |
\tktl_unexpand_earg\def\tktl_capture_pos_list{\tktl_capture_pos_list}% |
| 1149 |
\tktl_unexpand_earg\def\tktl_capture_tok_list{\tktl_capture_tok_list}% |
| 1150 |
\noexpand\tktl_capture_pos_cnt=\the\tktl_capture_pos_cnt\relax |
| 1151 |
\noexpand\tktl_capture_pos_index_cnt=\the\tktl_capture_pos_index_cnt\relax |
| 1152 |
\noexpand\tktl_capture_tok_index_cnt=\the\tktl_capture_tok_index_cnt\relax |
| 1153 |
\noexpand\tktl_match_success_true |
| 1154 |
\else |
| 1155 |
\noexpand\tktl_match_success_false |
| 1156 |
\fi |
| 1157 |
}% |
| 1158 |
\tktl_test_match_pattern_a |
| 1159 |
} |
| 1160 |
\def\tktl_test_macropattern_group#1{% |
| 1161 |
\def\tktl__tmp{\tktl_ifinstr{#1}}\expandafter\tktl__tmp\expandafter{\tktl_list_of_patterns}% |
| 1162 |
{% |
| 1163 |
\expandafter\expandafter\expandafter\tktl_test_pattern_group\expandafter\expandafter\expandafter{\csname tktl_pattern_\string#1\endcsname}% |
| 1164 |
} |
| 1165 |
{% |
| 1166 |
\errmessage{Marker "\string#1" is not a defined pattern, using "\string\." instead}% |
| 1167 |
\tktl_test_pattern_dot |
| 1168 |
}% |
| 1169 |
} |
| 1170 |
\long\def\tktl_read_compiled_pattern#1#2#3#4#5{% |
| 1171 |
\iftktl_allow_captures_ |
| 1172 |
\def\tktl_current_precapture_directive{#1}% |
| 1173 |
\iftktl_allow_post_pattern_catpure_ |
| 1174 |
\def\tktl_current_postcapture_directive{#5}% |
| 1175 |
\else |
| 1176 |
\def\tktl_current_postcapture_directive{0}% |
| 1177 |
\fi |
| 1178 |
\else |
| 1179 |
\def\tktl_current_precapture_directive{0}% |
| 1180 |
\def\tktl_current_postcapture_directive{0}% |
| 1181 |
\fi |
| 1182 |
\def\tktl_current_predicate{#2}% |
| 1183 |
\def\tktl_min_current_repeat{#3}% |
| 1184 |
\def\tktl_max_current_repeat{#4}% |
| 1185 |
\ifnum#1=1 % si capture de tokens demandée |
| 1186 |
\iftktl_in_capture_ % si déjà en train de capturer |
| 1187 |
\errmessage{Ignoring nested capture of tokens}% |
| 1188 |
\else |
| 1189 |
\advance\tktl_capture_tok_index_cnt 1 |
| 1190 |
\tktl_in_capture_true |
| 1191 |
\fi |
| 1192 |
\fi |
| 1193 |
\tktl_repeat_match_cnt=0 |
| 1194 |
% --> il reste à lire <instruction> qui est après \tktl_five_args |
| 1195 |
} |
| 1196 |
|
| 1197 |
%--------------------------------------------------------------------- |
| 1198 |
%----- Teste si un motif matche avec le début des tokens encodés ----- |
| 1199 |
%--------------------------------------------------------------------- |
| 1200 |
% La macro principale du package : |
| 1201 |
% \tktl_test_match_pattern{<pattern>}{<tokens>} |
| 1202 |
% Entrée : |
| 1203 |
% - le pattern est de la forme |
| 1204 |
% <prédicat><macro de pattern>{<tokens>}<occurences> |
| 1205 |
% - les <tokens> sont encodés de la forme (charcode:catcode) |
| 1206 |
% Sortie : |
| 1207 |
% 1) \iftktl_match_success_ booléen s'il y a correspondance |
| 1208 |
% 2) les tokens encodés \tktl_match_tokens contiennent la partie du |
| 1209 |
% début des <tokens> qui a matché |
| 1210 |
% 3) \tktl_postmatch_enctokens contient les tokens encodés qui |
| 1211 |
% restent après la partie qui a matché |
| 1212 |
%--------------------------------------------------------------------- |
| 1213 |
\newif\iftktl_match_success_ |
| 1214 |
\newif\iftktl_in_capture_ % vrai quand une capture de tokens est en cours |
| 1215 |
\newif\iftktl_in_patterngroup_ |
| 1216 |
\newcount\tktl_capture_tok_index_cnt % numérote les capture de tokens (non imbriquées) |
| 1217 |
\newcount\tktl_repeat_match_cnt |
| 1218 |
\long\def\tktl_test_match_pattern#1#2{% #1=pattern compilé "#1#2#3#4#5<instruction>" #2=tokens encodés (charcode:catcode) |
| 1219 |
\def\tktl_postmatch_enctokens{#2}% |
| 1220 |
\let\tktl_match_tokens\empty |
| 1221 |
\let\tktl_postmatch_enctokens_saved\tktl_postmatch_enctokens% sauvegarde en cas de prédicat ou de non match |
| 1222 |
\tktl_position_saved_cnt=\tktl_capture_pos_cnt |
| 1223 |
\let\tktl_capture_pos_list_saved\tktl_capture_pos_list |
| 1224 |
\let\tktl_capture_tok_list_saved\tktl_capture_tok_list |
| 1225 |
\tktl_read_compiled_pattern#1% #1=pattern compilé |
| 1226 |
} |
| 1227 |
\tktl_csdef{tktl_compile_pattern_\expandafter\string\tktl_macro_pattern}{% |
| 1228 |
\ifcsname tktl_pattern_\expandafter\string\tktl_current_macro_arg\endcsname |
| 1229 |
\tktl_xaddtotoks\tktl_pattern_compile_collect{% |
| 1230 |
\tktl_five_args |
| 1231 |
\noexpand\tktl_test_pattern_group{\unexpanded\expandafter\expandafter\expandafter{\csname tktl_pattern_\expandafter\string\tktl_current_macro_arg\endcsname}}% |
| 1232 |
}% |
| 1233 |
\else |
| 1234 |
\tktl_xaddtotoks\tktl_pattern_compile_collect{% |
| 1235 |
\tktl_five_args\noexpand\tktl_test_macropattern_group\expandafter\noexpand\tktl_current_macro_arg |
| 1236 |
}% |
| 1237 |
\fi |
| 1238 |
} |
| 1239 |
\def\tktl_test_match_pattern_a{% |
| 1240 |
\unless\ifnum\tktl_current_predicate=0 % si le motif est un prédicat |
| 1241 |
\tktl_capture_pos_cnt=\tktl_position_saved_cnt |
| 1242 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_saved% ne rien consommer |
| 1243 |
\let\tktl_match_tokens\empty% aucune correspondance |
| 1244 |
\ifnum\tktl_current_predicate=-1 % si prédicat "!" |
| 1245 |
\iftktl_match_success_% inverser le booléen |
| 1246 |
\tktl_match_success_false |
| 1247 |
\else |
| 1248 |
\tktl_match_success_true |
| 1249 |
\fi |
| 1250 |
\fi |
| 1251 |
\fi |
| 1252 |
\ifnum\tktl_current_precapture_directive=1 |
| 1253 |
\advance\tktl_capture_pos_index_cnt 1 |
| 1254 |
\edef\tktl_capture_pos_list{\tktl_capture_pos_list<\the\tktl_capture_pos_index_cnt=\the\tktl_position_saved_cnt>}% |
| 1255 |
\fi |
| 1256 |
\ifnum\tktl_current_postcapture_directive=1 |
| 1257 |
\advance\tktl_capture_pos_index_cnt 1 |
| 1258 |
\edef\tktl_capture_pos_list{\tktl_capture_pos_list<\the\tktl_capture_pos_index_cnt=\the\tktl_capture_pos_cnt>}% |
| 1259 |
\fi |
| 1260 |
\iftktl_in_capture_\unless\iftktl_in_patterngroup_ % si en cours de capture et hors d'un groupe |
| 1261 |
\tktl_in_capture_false % fin de la capture de tokens |
| 1262 |
\tktl_eaddtomacro\tktl_capture_tok_list{\expanded{<\the\tktl_capture_tok_index_cnt=\unexpanded\expandafter{\tktl_match_tokens}}>}% |
| 1263 |
\fi\fi |
| 1264 |
} |
| 1265 |
\long\def\tktl_gob_enctoks_to_end#1(-1:-1){} |
| 1266 |
|
| 1267 |
\def\tktl_test_match_patterngroup{% |
| 1268 |
\ifx\tktl_postmatch_enctokens\empty% fin atteinte ? |
| 1269 |
\let\tktl_do_next\empty% on sort de toutes façons |
| 1270 |
\expandafter\tktl_test_match_full_patterns\expanded{{\unexpanded\expandafter{\tktl_current_macro_arg}}{\unexpanded\expandafter{\tktl_postmatch_enctokens}}}% |
| 1271 |
\iftktl_match_success_% si correspondance -> motif epsilon a matché |
| 1272 |
\advance\tktl_repeat_match_cnt 1 |
| 1273 |
\fi |
| 1274 |
\ifnum\tktl_repeat_match_cnt<\tktl_min_current_repeat\relax% si nb répétition mini non atteint |
| 1275 |
\tktl_match_success_false |
| 1276 |
\tktl_capture_pos_cnt=\tktl_position_saved_cnt |
| 1277 |
\let\tktl_capture_pos_list\tktl_capture_pos_list_saved |
| 1278 |
\let\tktl_capture_tok_list\tktl_capture_tok_list_saved |
| 1279 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_saved% ne rien consommer |
| 1280 |
\else |
| 1281 |
\tktl_match_success_true |
| 1282 |
\let\tktl_postmatch_enctokens\empty |
| 1283 |
\fi |
| 1284 |
\else |
| 1285 |
\expandafter\tktl_test_match_full_patterns\expanded{{\unexpanded\expandafter{\tktl_current_macro_arg}}{\unexpanded\expandafter{\tktl_postmatch_enctokens}}}% |
| 1286 |
\iftktl_match_success_% si correspondance |
| 1287 |
\advance\tktl_repeat_match_cnt 1 |
| 1288 |
\tktl_eaddtomacro\tktl_match_tokens_patterngroup\tktl_match_full_patterns |
| 1289 |
\ifnum\tktl_repeat_match_cnt<\tktl_min_current_repeat\relax% si nb répétition mini non atteint |
| 1290 |
\let\tktl_do_next\tktl_test_match_patterngroup |
| 1291 |
\else |
| 1292 |
\ifnum\tktl_repeat_match_cnt=\tktl_max_current_repeat\relax% max atteint |
| 1293 |
\tktl_match_success_true |
| 1294 |
\let\tktl_do_next\empty% on sort |
| 1295 |
\else |
| 1296 |
\let\tktl_do_next\tktl_test_match_patterngroup |
| 1297 |
\fi |
| 1298 |
\fi |
| 1299 |
\else% si pas correspondance |
| 1300 |
\let\tktl_do_next\empty% on sort |
| 1301 |
\ifnum\numexpr(\tktl_repeat_match_cnt-\tktl_min_current_repeat)*(\tktl_repeat_match_cnt-\tktl_max_current_repeat)>0 % si le nombre de répétition n'est pas atteint |
| 1302 |
\tktl_match_success_false |
| 1303 |
\let\tktl_match_tokens_patterngroup\empty |
| 1304 |
\tktl_capture_pos_cnt=\tktl_position_saved_cnt |
| 1305 |
\let\tktl_capture_pos_list\tktl_capture_pos_list_saved |
| 1306 |
\let\tktl_capture_tok_list\tktl_capture_tok_list_saved |
| 1307 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_saved% ne rien consommer |
| 1308 |
\else% nombre de répétition atteint |
| 1309 |
\tktl_match_success_true |
| 1310 |
\fi |
| 1311 |
\fi |
| 1312 |
\fi |
| 1313 |
\tktl_do_next |
| 1314 |
} |
| 1315 |
|
| 1316 |
\long\def\tktl_test_match_dot(#1:#2){% #1=token courant forme (charcode,catcode) |
| 1317 |
\ifnum#2=-1 % fin atteinte? |
| 1318 |
\let\tktl_do_next\empty |
| 1319 |
\ifnum\tktl_repeat_match_cnt<\tktl_min_current_repeat\relax% si nb répétition mini non atteint |
| 1320 |
\tktl_match_success_false |
| 1321 |
\tktl_capture_pos_cnt=\tktl_position_saved_cnt |
| 1322 |
\let\tktl_capture_pos_list\tktl_capture_pos_list_saved |
| 1323 |
\let\tktl_capture_tok_list\tktl_capture_tok_list_saved |
| 1324 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_saved% ne rien consommer |
| 1325 |
\else |
| 1326 |
\tktl_match_success_true |
| 1327 |
\let\tktl_postmatch_enctokens\empty |
| 1328 |
\fi |
| 1329 |
\else |
| 1330 |
\advance\tktl_repeat_match_cnt 1 |
| 1331 |
\advance\tktl_capture_pos_cnt1 |
| 1332 |
\tktl_addtomacro\tktl_match_tokens{(#1:#2)}% |
| 1333 |
\ifnum\tktl_repeat_match_cnt<\tktl_min_current_repeat\relax% si nb répétition mini non atteint |
| 1334 |
\let\tktl_do_next\tktl_test_match_dot |
| 1335 |
\else |
| 1336 |
\ifnum\tktl_repeat_match_cnt=\tktl_max_current_repeat\relax% max atteint |
| 1337 |
\tktl_match_success_true |
| 1338 |
\def\tktl_do_next{\tktl_test_match_assign_remaining_tokens{\def\tktl_postmatch_enctokens}}% |
| 1339 |
\else |
| 1340 |
\let\tktl_do_next\tktl_test_match_dot |
| 1341 |
\fi |
| 1342 |
\fi |
| 1343 |
\fi |
| 1344 |
\tktl_do_next |
| 1345 |
} |
| 1346 |
\long\def\tktl_test_match_assign_remaining_tokens#1#2(-1:-1){% #1=consigne de stockage #2=tokens restants |
| 1347 |
#1{#2}% |
| 1348 |
} |
| 1349 |
|
| 1350 |
\long\def\tktl_test_match_s#1#2{% #1=motif forme (charcode,catcode) #2=texte forme (charcode,catcode) |
| 1351 |
\def\tktl_test_match_s_a##1#1##2\_nil{% |
| 1352 |
\tktl_ifempty_or_space{##1} |
| 1353 |
{% si le texte commence par le motif |
| 1354 |
\advance\tktl_capture_pos_cnt\tktl_string_len_cnt |
| 1355 |
\tktl_addtomacro\tktl_match_tokens{#1}% |
| 1356 |
\advance\tktl_repeat_match_cnt 1 |
| 1357 |
\edef\tktl_postmatch_enctokens{\tktl_test_match_s_b##2\_nil}% prendre ce qui est après le motif |
| 1358 |
\ifnum\tktl_repeat_match_cnt<\tktl_min_current_repeat\relax% si nb répétition mini non atteint |
| 1359 |
\def\tktl_do_next{\expandafter\tktl_test_match_s_a\tktl_postmatch_enctokens#1\_nil}% recommencer |
| 1360 |
\else% mini de répétitions atteint |
| 1361 |
\ifnum\tktl_repeat_match_cnt=\tktl_max_current_repeat\relax% max atteint |
| 1362 |
\tktl_match_success_true |
| 1363 |
\let\tktl_do_next\empty |
| 1364 |
\else% max non atteint -> comportement gourmand |
| 1365 |
\def\tktl_do_next{\expandafter\tktl_test_match_s_a\tktl_postmatch_enctokens#1\_nil}% recommencer |
| 1366 |
\fi |
| 1367 |
\fi |
| 1368 |
\tktl_do_next |
| 1369 |
} |
| 1370 |
{% si le texte ne commence pas par le motif |
| 1371 |
\ifnum\tktl_repeat_match_cnt<\tktl_min_current_repeat\relax% si nb répétition mini non atteint |
| 1372 |
\tktl_match_success_false |
| 1373 |
\tktl_capture_pos_cnt=\tktl_position_saved_cnt |
| 1374 |
\let\tktl_capture_pos_list\tktl_capture_pos_list_saved |
| 1375 |
\let\tktl_capture_tok_list\tktl_capture_tok_list_saved |
| 1376 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_saved% ne rien consommer |
| 1377 |
\else |
| 1378 |
\tktl_match_success_true |
| 1379 |
\fi |
| 1380 |
}% |
| 1381 |
}% |
| 1382 |
\def\tktl_test_match_s_b##1#1\_nil{% |
| 1383 |
\unexpanded{##1}% garder ce qui est après le motif et avant le motif mis en dernier |
| 1384 |
}% |
| 1385 |
\tktl_ifempty_or_space{#1}% si motif ε -> renvoyer vrai |
| 1386 |
{% |
| 1387 |
\tktl_match_success_true |
| 1388 |
} |
| 1389 |
{% |
| 1390 |
\tktl_ifempty_or_space{#2} |
| 1391 |
{% |
| 1392 |
\tktl_match_success_false |
| 1393 |
} |
| 1394 |
{% |
| 1395 |
\tktl_test_match_s_a#2#1\_nil |
| 1396 |
}% |
| 1397 |
}% |
| 1398 |
} |
| 1399 |
|
| 1400 |
\def\tktl_analyse_R_pattern#1{% #1 est de la forme "interval charcode" ou "interval charcode:interval catcode" |
| 1401 |
\tktl_analyse_R_pattern_a#1:*:\_nil |
| 1402 |
} |
| 1403 |
\def\tktl_analyse_R_pattern_a#1:#2:#3\_nil{% |
| 1404 |
\def\tktl_charcode_interval{#1}% |
| 1405 |
\def\tktl_catcode_interval{#2}% |
| 1406 |
} |
| 1407 |
\def\tktl_convert_charcsv_to_charcodecsv#1{% |
| 1408 |
\let\tktl_charcode_interval\empty |
| 1409 |
\tktl_convert_charcsv_to_charcodecsv_a#1,\tktl_convert_charcsv_to_charcodecsv_a,% |
| 1410 |
\ifx\tktl_charcode_interval\empty\else% retirer la dernière virgule |
| 1411 |
\expandafter\tktl_convert_charcsv_to_charcodecsv_b\tktl_charcode_interval\_nil\tktl_charcode_interval |
| 1412 |
\fi |
| 1413 |
} |
| 1414 |
\def\tktl_convert_charcsv_to_charcodecsv_a#1,{% |
| 1415 |
\ifx\tktl_convert_charcsv_to_charcodecsv_a#1% |
| 1416 |
\else% TODO : tester si #1 est vide ? |
| 1417 |
\tktl_encode_tokens_char_range{#1}\tktl_charcode_min\tktl_charcodemax |
| 1418 |
\edef\tktl_charcode_interval{\tktl_charcode_interval\tktl_charcode_min-\tktl_charcodemax,}% |
| 1419 |
\expandafter\tktl_convert_charcsv_to_charcodecsv_a |
| 1420 |
\fi |
| 1421 |
} |
| 1422 |
\def\tktl_convert_charcsv_to_charcodecsv_b#1,\_nil#2{% |
| 1423 |
\def#2{#1}% |
| 1424 |
} |
| 1425 |
\long\def\tktl_test_match_R_or_S(#1:#2){% |
| 1426 |
\ifnum#2=-1 % fin atteinte? |
| 1427 |
\let\tktl_do_next\empty |
| 1428 |
\ifnum\tktl_repeat_match_cnt<\tktl_min_current_repeat\relax% si nb répétition mini non atteint |
| 1429 |
\tktl_match_success_false |
| 1430 |
\tktl_capture_pos_cnt=\tktl_position_saved_cnt |
| 1431 |
\let\tktl_capture_pos_list\tktl_capture_pos_list_saved |
| 1432 |
\let\tktl_capture_tok_list\tktl_capture_tok_list_saved |
| 1433 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_saved% ne rien consommer |
| 1434 |
\else |
| 1435 |
\tktl_match_success_true |
| 1436 |
\let\tktl_postmatch_enctokens\empty |
| 1437 |
\fi |
| 1438 |
\else |
| 1439 |
\tktl_token_test_R_or_S(#1:#2) |
| 1440 |
{% si le token courant correspond aux intervalles |
| 1441 |
\advance\tktl_capture_pos_cnt1 |
| 1442 |
\advance\tktl_repeat_match_cnt 1 |
| 1443 |
\tktl_addtomacro\tktl_match_tokens{(#1:#2)}% |
| 1444 |
\ifnum\tktl_repeat_match_cnt<\tktl_min_current_repeat\relax% si nb répétition mini non atteint |
| 1445 |
\let\tktl_do_next\tktl_test_match_R_or_S |
| 1446 |
\else |
| 1447 |
\ifnum\tktl_repeat_match_cnt=\tktl_max_current_repeat\relax% max atteint |
| 1448 |
\tktl_match_success_true |
| 1449 |
\def\tktl_do_next{\tktl_test_match_assign_remaining_tokens{\def\tktl_postmatch_enctokens}}% tout manger et assigner |
| 1450 |
\else |
| 1451 |
\let\tktl_do_next\tktl_test_match_R_or_S |
| 1452 |
\fi |
| 1453 |
\fi |
| 1454 |
} |
| 1455 |
{%le token courant ne correspond pas |
| 1456 |
\ifnum\numexpr(\tktl_repeat_match_cnt-\tktl_min_current_repeat)*(\tktl_repeat_match_cnt-\tktl_max_current_repeat)>0 % si le nombre de répétition n'est aps atteint |
| 1457 |
\tktl_match_success_false |
| 1458 |
\let\tktl_match_tokens\empty |
| 1459 |
\tktl_capture_pos_cnt=\tktl_position_saved_cnt |
| 1460 |
\let\tktl_capture_pos_list\tktl_capture_pos_list_saved |
| 1461 |
\let\tktl_capture_tok_list\tktl_capture_tok_list_saved |
| 1462 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_saved% ne rien consommer |
| 1463 |
\let\tktl_do_next\tktl_gob_enctoks_to_end% tout manger |
| 1464 |
\else% nombre de répétition atteint |
| 1465 |
\tktl_match_success_true |
| 1466 |
\def\tktl_do_next{\tktl_test_match_assign_remaining_tokens{\def\tktl_postmatch_enctokens}(#1:#2)}% remettre le token actuel |
| 1467 |
\fi |
| 1468 |
}% |
| 1469 |
\fi |
| 1470 |
\tktl_do_next |
| 1471 |
} |
| 1472 |
%--------------------------------------------------------------------- |
| 1473 |
%------------ Test si des motifs qui se suivent matchent ------------- |
| 1474 |
%------------ avec le début des tokens encodés ------------- |
| 1475 |
%--------------------------------------------------------------------- |
| 1476 |
% Les motifs sont séparés par des ":" qui est le connecteur de concaténation |
| 1477 |
% Entrée : |
| 1478 |
% #1 = motifs séparés par des ":" |
| 1479 |
% #2 = tokens encodés sous la forme (charcode:catcode) |
| 1480 |
% Revoie : |
| 1481 |
% \iftktl_match_success_ : le booléen selon qu'il y a correspondance ou pas |
| 1482 |
% \tktl_postmatch_enctokens : tokens encodés restant (#1 si booléen faux) |
| 1483 |
% \tktl_match_and_patterns : vide si booléen faux et tokens encodés qui ont matché |
| 1484 |
%--------------------------------------------------------------------- |
| 1485 |
\long\def\tktl_test_match_and_patterns#1#2{% #1= and_patterns séparés par ":" #2=tok_encodés (charcode:catcode) |
| 1486 |
\begingroup |
| 1487 |
\def\tktl_postmatch_enctokens{#2}% |
| 1488 |
\let\tktl_match_and_patterns\empty |
| 1489 |
\let\tktl_postmatch_enctokens_init\tktl_postmatch_enctokens |
| 1490 |
\tktl_test_match_and_patterns_a#1:\relax:% |
| 1491 |
\expanded{% |
| 1492 |
\endgroup |
| 1493 |
\iftktl_match_success_ |
| 1494 |
\tktl_unexpand_earg\def\tktl_match_and_patterns{\tktl_match_and_patterns}% |
| 1495 |
\tktl_unexpand_earg\def\tktl_postmatch_enctokens{\tktl_postmatch_enctokens}% |
| 1496 |
\tktl_unexpand_earg\def\tktl_capture_pos_list{\tktl_capture_pos_list}% |
| 1497 |
\tktl_unexpand_earg\def\tktl_capture_tok_list{\tktl_capture_tok_list}% |
| 1498 |
\noexpand\tktl_capture_pos_cnt=\the\tktl_capture_pos_cnt\relax |
| 1499 |
\noexpand\tktl_capture_pos_index_cnt=\the\tktl_capture_pos_index_cnt\relax |
| 1500 |
\noexpand\tktl_capture_tok_index_cnt=\the\tktl_capture_tok_index_cnt\relax |
| 1501 |
\noexpand\tktl_match_success_true |
| 1502 |
\else |
| 1503 |
\noexpand\tktl_match_success_false |
| 1504 |
\fi |
| 1505 |
}% |
| 1506 |
} |
| 1507 |
\long\def\tktl_test_match_and_patterns_a#1:{% #1= and_pattern courant |
| 1508 |
\def\tktl_current_pattern{#1}% |
| 1509 |
\ifx\tktl_current_pattern\tktl_end_of_patterns |
| 1510 |
\let\tktl_do_next\empty |
| 1511 |
\else |
| 1512 |
\let\tktl_match_tokens\empty |
| 1513 |
\def\tktl__tmp{\tktl_test_match_pattern{#1}}\expandafter\tktl__tmp\expandafter{\tktl_postmatch_enctokens}% |
| 1514 |
\iftktl_match_success_ |
| 1515 |
\tktl_eaddtomacro\tktl_match_and_patterns\tktl_match_tokens |
| 1516 |
\let\tktl_do_next\tktl_test_match_and_patterns_a |
| 1517 |
\else% ce match échoue ? |
| 1518 |
\let\tktl_match_and_patterns\empty% vider les tokens ayant matché |
| 1519 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_init% ne rien consommer |
| 1520 |
\let\tktl_do_next\tktl_gob_all_and_patterns% manger tous les pattern qui restent |
| 1521 |
\fi |
| 1522 |
\fi |
| 1523 |
\tktl_do_next |
| 1524 |
} |
| 1525 |
\def\tktl_gob_all_and_patterns#1\relax:{} |
| 1526 |
\def\tktl_end_of_patterns{\relax} |
| 1527 |
|
| 1528 |
%--------------------------------------------------------------------- |
| 1529 |
%------------- Teste si des motifs quelconques matchent -------------- |
| 1530 |
%------------- avec le début des tokens encodés -------------- |
| 1531 |
%--------------------------------------------------------------------- |
| 1532 |
% Entrée : |
| 1533 |
% #1 = motifs concaténés séparés par des "|" |
| 1534 |
% #2 = tokens encodés sous la forme (charcode:catcode) |
| 1535 |
% Revoie : |
| 1536 |
% \iftktl_match_success_ : le booléen selon qu'il y a correspondance ou pas |
| 1537 |
% \tktl_postmatch_enctokens : tokens encodés restant (=#1 si booléen faux) |
| 1538 |
% \tktl_match_full_patterns : tokens encodés qui ont matché (vide si booléen faux) |
| 1539 |
% \tktl_prematch_pos position avant match |
| 1540 |
% \tktl_postmatch_pos position après match (égale à \tktl_prematch_pos si pas de match) |
| 1541 |
%--------------------------------------------------------------------- |
| 1542 |
\long\def\tktl_test_match_full_patterns#1#2{% #1=patterns séparés par "|" #2=tok_encodés |
| 1543 |
\def\tktl_postmatch_enctokens{#2}% |
| 1544 |
\let\tktl_match_full_patterns\empty |
| 1545 |
\let\tktl_postmatch_enctokens_init\tktl_postmatch_enctokens |
| 1546 |
\edef\tktl_prematch_pos{\the\tktl_capture_pos_cnt}% |
| 1547 |
\tktl_test_match_full_patterns_a#1|\relax|% |
| 1548 |
\edef\tktl_postmatch_pos{\the\tktl_capture_pos_cnt}% |
| 1549 |
} |
| 1550 |
\long\def\tktl_test_match_full_patterns_a#1|{% |
| 1551 |
\def\tktl_current_pattern{#1}% |
| 1552 |
\ifx\tktl_current_pattern\tktl_end_of_patterns% tous les patterns épuisés : |
| 1553 |
\tktl_match_success_false% pas de match |
| 1554 |
\let\tktl_match_full_patterns\empty% vider les tokens ayant matché |
| 1555 |
\let\tktl_postmatch_enctokens\tktl_postmatch_enctokens_init% ne rien consommer |
| 1556 |
\let\tktl_do_next\empty |
| 1557 |
\else |
| 1558 |
\def\tktl__tmp{\tktl_test_match_and_patterns{#1}}\expandafter\tktl__tmp\expandafter{\tktl_postmatch_enctokens}% |
| 1559 |
\iftktl_match_success_ |
| 1560 |
\tktl_eaddtomacro\tktl_match_full_patterns\tktl_match_and_patterns |
| 1561 |
\let\tktl_do_next\tktl_gob_all_full_patterns |
| 1562 |
\else |
| 1563 |
\let\tktl_do_next\tktl_test_match_full_patterns_a |
| 1564 |
\fi |
| 1565 |
\fi |
| 1566 |
\tktl_do_next |
| 1567 |
} |
| 1568 |
\long\def\tktl_gob_all_full_patterns#1\relax|{} |
| 1569 |
|
| 1570 |
%--------------------------------------------------------------------- |
| 1571 |
%------------- Gestion des macros contenant des patterns ------------- |
| 1572 |
%--------------------------------------------------------------------- |
| 1573 |
\let\tktl_list_of_patterns\empty |
| 1574 |
\let\tktl_list_of_single_patterns\empty |
| 1575 |
\def\tktl_ifmacro#1{% teste si #1 est une macro |
| 1576 |
\begingroup |
| 1577 |
\escapechar=92 % "\" |
| 1578 |
\ifnum 0% |
| 1579 |
\tktl_ifempty_or_space{#1} |
| 1580 |
{} |
| 1581 |
{% |
| 1582 |
\expandafter\tktl_ifempty_or_space\expandafter{\tktl_gob_arg#1} |
| 1583 |
{% |
| 1584 |
\ifnum\expandafter\expandafter\expandafter`\expandafter\tktl_firsttonil\string#1.\_nil=92 |
| 1585 |
1% |
| 1586 |
\fi |
| 1587 |
}% |
| 1588 |
{}% |
| 1589 |
}% |
| 1590 |
=1 |
| 1591 |
\endgroup |
| 1592 |
\expandafter\tktl_exec_first |
| 1593 |
\else |
| 1594 |
\endgroup |
| 1595 |
\expandafter\tktl_exec_second |
| 1596 |
\fi |
| 1597 |
} |
| 1598 |
\def\tktl_remove_pattern_in_list#1#2{% #1=macro pattern utilisateur #2=macro contenant une liste |
| 1599 |
\tktl_ifmacro{#1} |
| 1600 |
{% |
| 1601 |
\def\tktl__tmp{\tktl_ifinstr{#1}}\expandafter\tktl__tmp\expandafter{#2}% |
| 1602 |
{% |
| 1603 |
\tktl_remove_pattern_in_list_a#1#2% |
| 1604 |
} |
| 1605 |
{}% |
| 1606 |
} |
| 1607 |
{% |
| 1608 |
\errmessage{Internal error in \string\tktl_remove_pattern_in_list : \detokenize{#1} is not a macro}% |
| 1609 |
}% |
| 1610 |
} |
| 1611 |
\def\tktl_remove_pattern_in_list_a#1#2{% #1=macro pattern interne #2=macro contenant une liste |
| 1612 |
\def\tktl_remove_pattern_in_list_b##1#1##2\_nil{\def#2{##1##2}}% |
| 1613 |
\expandafter\tktl_remove_pattern_in_list_b#2\_nil |
| 1614 |
} |
| 1615 |
|
| 1616 |
\def\tktl_add_pattern_in_list#1#2{% #1=macro pattern utilisateur #2=macro contenant une liste |
| 1617 |
\tktl_ifmacro{#1} |
| 1618 |
{% |
| 1619 |
\def\tktl__tmp{\tktl_ifinstr{#1}}\expandafter\tktl__tmp\expandafter{#2}% |
| 1620 |
{} |
| 1621 |
{% |
| 1622 |
\tktl_addtomacro#2{#1}% |
| 1623 |
}% |
| 1624 |
} |
| 1625 |
{% |
| 1626 |
\errmessage{Internal error in \string\tktl_add_pattern_in_list : \detokenize{#1} is not a macro}% |
| 1627 |
}% |
| 1628 |
} |
| 1629 |
\long\def\defpattern#1#2{% #1 macro représentant le pattern #2=code du pattern |
| 1630 |
\tktl_ifmacro{#1} |
| 1631 |
{% |
| 1632 |
\tktl_ifempty_or_space{#2} |
| 1633 |
{% |
| 1634 |
\errmessage{Pattern code is empty, definition ignored}% |
| 1635 |
} |
| 1636 |
{% |
| 1637 |
\tktl_ifinstr{#1}{\s\S\r\R\.\relax} |
| 1638 |
{% |
| 1639 |
\errmessage{Macro \string#1\space is reserved, a pattern cannot be named with it}% |
| 1640 |
} |
| 1641 |
{% |
| 1642 |
\let\tktl_grab_pattern_remain\tktl_grab_pattern_remain_test_single |
| 1643 |
\tktl_allow_post_pattern_catpure_true |
| 1644 |
\tktl_allow_captures_true% permet les captures à priori |
| 1645 |
\tktl_compile_patterns{#2}% |
| 1646 |
\expandafter\edef\csname tktl_pattern_\string#1\endcsname{\the\tktl_pattern_compile_collect}% |
| 1647 |
\tktl_addtomacro\tktl_list_of_patterns#1% |
| 1648 |
}% |
| 1649 |
}% |
| 1650 |
} |
| 1651 |
{% |
| 1652 |
\errmessage{Found "\detokenize{#1}" after \string\defpattern, definition aborted}% |
| 1653 |
}% |
| 1654 |
} |
| 1655 |
|
| 1656 |
%--------------------------------------------------------------------- |
| 1657 |
%------------------ Gestion des captures effectuées ------------------ |
| 1658 |
%--------------------------------------------------------------------- |
| 1659 |
\def\tktl_ifcontains_ddots#1{% la chaine #1 contient-elle ":" ? |
| 1660 |
\tktl_ifcontains_ddots_a#1:\_nil |
| 1661 |
} |
| 1662 |
\def\tktl_ifcontains_ddots_a#1:#2\_nil{% |
| 1663 |
\ifcat\relax\detokenize{#2}\relax\expandafter\tktl_exec_second\else\expandafter\tktl_exec_first\fi |
| 1664 |
} |
| 1665 |
\def\tktl_process_pos_capture#1#2#3{% #1=liste des captures <index=position> #2=nom des captures #3=maxindex |
| 1666 |
\def\tktl_temp_capture_name{#2}% |
| 1667 |
\tktl_csdef{tktl_capture_position_maxindex_#2}{#3}% |
| 1668 |
\let\tktl_capture_pos_list\empty |
| 1669 |
\tktl_process_pos_capture_a#1<-1=-1>% |
| 1670 |
\expandafter\let\csname tktl_pos_capture_\tktl_temp_capture_name0\endcsname\tktl_capture_pos_list |
| 1671 |
} |
| 1672 |
\def\tktl_process_pos_capture_a<#1=#2>{% |
| 1673 |
\ifnum#1>0 |
| 1674 |
\tktl_addtomacro\tktl_capture_pos_list{,#2}% |
| 1675 |
\tktl_csdef{tktl_pos_capture_\tktl_temp_capture_name#1}{#2}% |
| 1676 |
\expandafter\tktl_process_pos_capture_a |
| 1677 |
\else |
| 1678 |
\ifx\tktl_capture_pos_list\empty\else% retirer la première virgule |
| 1679 |
\expanded{\def\noexpand\tktl_capture_pos_list{\unexpanded\expandafter\expandafter\expandafter{\expandafter\tktl_gob_arg\tktl_capture_pos_list}}}% |
| 1680 |
\fi |
| 1681 |
\fi |
| 1682 |
} |
| 1683 |
\long\def\tktl_process_tok_capture#1#2#3{% #1=liste des captures <index={tokens}> #2=nom des captures #3=maxindex |
| 1684 |
\def\tktl_temp_capture_name{#2}% |
| 1685 |
\tktl_csdef{tktl_capture_tokens_maxindex_#2}{#3}% |
| 1686 |
\let\tktl_capture_tok_list\empty |
| 1687 |
\tktl_process_tok_capture_a#1<-1=-1>% |
| 1688 |
\expandafter\let\csname tktl_tok_capture_\tktl_temp_capture_name0\endcsname\tktl_capture_tok_list |
| 1689 |
} |
| 1690 |
\newtoks\tktl_decode_capture_toks |
| 1691 |
\long\def\tktl_process_tok_capture_a<#1=#2>{% |
| 1692 |
\ifnum#1>0 |
| 1693 |
\tktl_decode_enctoks{#2}\tktl_decode_capture_toks |
| 1694 |
\expandafter\edef\csname tktl_tok_capture_\tktl_temp_capture_name#1\endcsname{\unexpanded\expandafter{\the\tktl_decode_capture_toks}}% |
| 1695 |
\tktl_eaddtomacro\tktl_capture_tok_list{\unexpanded\expandafter{\expandafter,\expandafter{\the\tktl_decode_capture_toks}}}% |
| 1696 |
\expandafter\tktl_process_tok_capture_a |
| 1697 |
\else |
| 1698 |
\ifx\tktl_capture_tok_list\empty\else% retirer la première virgule |
| 1699 |
\expanded{\def\noexpand\tktl_capture_tok_list{\unexpanded\expandafter\expandafter\expandafter{\expandafter\tktl_gob_arg\tktl_capture_tok_list}}}% |
| 1700 |
\fi |
| 1701 |
\fi |
| 1702 |
} |
| 1703 |
\def\poscapture#1{% |
| 1704 |
\expanded{% |
| 1705 |
\tktl_ifcontains_ddots{#1} |
| 1706 |
{\tktl_poscapture_a#1\_nil} |
| 1707 |
{\expanded{\noexpand\tktl_poscapture_b{}{\unexpanded\expandafter\expandafter\expandafter{\tktl_stripsp{#1}}}}}% |
| 1708 |
}% |
| 1709 |
} |
| 1710 |
\def\tktl_poscapture_a#1:#2\_nil{% |
| 1711 |
\expanded{\noexpand\tktl_poscapture_b{\unexpanded\expandafter\expandafter\expandafter{\tktl_stripsp{#1}}}{\unexpanded\expandafter\expandafter\expandafter{\tktl_stripsp{#2}}}}% |
| 1712 |
} |
| 1713 |
\def\tktl_poscapture_b#1#2{% |
| 1714 |
\ifcsname tktl_pos_capture_\detokenize{#1}#2\endcsname |
| 1715 |
\ifnum\numexpr#2*(#2-\csname tktl_capture_position_maxindex_\detokenize{#1}\endcsname)>0 % si #2 n'appartient pas à [0 ; max] |
| 1716 |
\tktl_capture_undefined{position}{#1}{#2}% |
| 1717 |
\else |
| 1718 |
\unexpanded\expandafter\expandafter\expandafter{\csname tktl_pos_capture_\detokenize{#1}#2\endcsname}% |
| 1719 |
\fi |
| 1720 |
\else |
| 1721 |
\tktl_capture_undefined{position}{#1}{#2}% |
| 1722 |
\fi |
| 1723 |
} |
| 1724 |
\def\tktl_capture_undefined#1#2#3{% |
| 1725 |
\errmessage{Undefined #1 capture \ifcat\relax\detokenize{#2}\relax \else named "\detokenize{#2}"\fi at index "\detokenize{#3}"}% |
| 1726 |
} |
| 1727 |
|
| 1728 |
\def\tokscapture#1{% #1= <nom>:<index> ou <index> |
| 1729 |
\expanded{% |
| 1730 |
\tktl_ifcontains_ddots{#1} |
| 1731 |
{\tktl_tokcapture_a#1\_nil} |
| 1732 |
{\expanded{\noexpand\tktl_tokcapture_b{}{\unexpanded\expandafter\expandafter\expandafter{\tktl_stripsp{#1}}}}}% |
| 1733 |
}% |
| 1734 |
} |
| 1735 |
\def\tktl_tokcapture_a#1:#2\_nil{% |
| 1736 |
\expanded{\noexpand\tktl_tokcapture_b{\unexpanded\expandafter\expandafter\expandafter{\tktl_stripsp{#1}}}{\unexpanded\expandafter\expandafter\expandafter{\tktl_stripsp{#2}}}}% |
| 1737 |
} |
| 1738 |
\def\tktl_tokcapture_b#1#2{% |
| 1739 |
\ifcsname tktl_tok_capture_\detokenize{#1}#2\endcsname |
| 1740 |
\ifnum\numexpr#2*(#2-\csname tktl_capture_tokens_maxindex_\detokenize{#1}\endcsname)>0 % si #2 n'appartient pas à [0 ; max] |
| 1741 |
\tktl_capture_undefined{token}{#1}{#2}% |
| 1742 |
\else |
| 1743 |
\unexpanded\expandafter\expandafter\expandafter{\csname tktl_tok_capture_\detokenize{#1}#2\endcsname}% |
| 1744 |
\fi |
| 1745 |
\else |
| 1746 |
\tktl_capture_undefined{token}{#1}{#2}% |
| 1747 |
\fi |
| 1748 |
} |
| 1749 |
%--------------------------------------------------------------------- |
| 1750 |
%--------------------- Macro publique \ifpegmatch -------------------- |
| 1751 |
%--------------------------------------------------------------------- |
| 1752 |
% Utilisation : |
| 1753 |
% \ifpegmatch[<clés/valeurs>]{<patterns>}{<tokens>}{T}{F} |
| 1754 |
% teste si les <tokens> matchent avec les <patterns>, |
| 1755 |
% et exécute T ou F selon l'issue du test |
| 1756 |
% |
| 1757 |
% Sortie : |
| 1758 |
% - les <tokens> qui ont matché sont stockés par défaut dans la macro \matchtoks |
| 1759 |
% - les <tokens> qui restent sont stockés par défaut dans la macro \remaintoks |
| 1760 |
% - la macro \poscapture{<nom>:<index>} ou \poscapture{<index>} renvoie en 2 développements |
| 1761 |
% la position dans les <tokens> capturés par \c de la capture numéro <index>. |
| 1762 |
% Si <index> vaut 0, la liste des positions est renvoyée sous la forme csv |
| 1763 |
% "<pos1>,<pos2>,...,<posn>" |
| 1764 |
% - la macro \matchposition contient la position du match dans les <tokens>. |
| 1765 |
% Elle vaut 0 si pas de match. |
| 1766 |
% - la macro \tokscapture{<nom>:<index>} ou \tokscapture{<index>} renvoie en 2 développements |
| 1767 |
% les <tokens> capturés par \c de la capture numéro <index>. |
| 1768 |
% Si <index> vaut 0, la liste des <tokens> capturés est renvoyée sous la forme csv |
| 1769 |
% "{<tokens1>},{<tokens2>},...,{<tokensn>}" |
| 1770 |
%--------------------------------------------------------------------- |
| 1771 |
\def\tktl_test_mode#1[#2;#3]#4#5{% teste si #1 est compris entre #2 et #3 et assigne le résultat à #5 ou #4 à #5 si échec |
| 1772 |
\tktl_ifinteger{#1} |
| 1773 |
{% |
| 1774 |
\ifnum\numexpr(#1-#2)*(#1-#3)>0 |
| 1775 |
\errmessage{Illegal mode "\detokenize{#1}", "mode=#4" inserted}% |
| 1776 |
\def#5{#4}% |
| 1777 |
\else |
| 1778 |
\def#5{#1}% |
| 1779 |
\fi |
| 1780 |
} |
| 1781 |
{% |
| 1782 |
\errmessage{Illegal mode "\detokenize{#1}", "mode=#4" inserted}% |
| 1783 |
\def#5{#4}% |
| 1784 |
}% |
| 1785 |
} |
| 1786 |
\defKV[pegmatch]{ |
| 1787 |
mode = \tktl_test_mode{#1}[0;2]1\tktl_match_mode, |
| 1788 |
assign match=\def\tktl_assign_match{#1}, |
| 1789 |
assign postmatch=\def\tktl_assign_remain{#1}, |
| 1790 |
assign prematch=\def\tktl_assign_prematch{#1}, |
| 1791 |
capture name=\edef\tktl_capture_name{\detokenize{#1}}, |
| 1792 |
expand arg= \tktl_ifinteger{#1} |
| 1793 |
{\def\tktl_expand_arg_value{#1}} |
| 1794 |
{\errmessage{Key "expand arg" require an integer}\def\tktl_expand_arg_value{0}}, |
| 1795 |
} |
| 1796 |
\setKVdefault[pegmatch]{ |
| 1797 |
mode=1,% 0=match la totalité 1=match début des <tokens> 2=match à n'importe quelle position |
| 1798 |
assign match=\def\matchtoks,% comment assigner les tokens qui ont matché |
| 1799 |
assign postmatch=\def\remaintoks,% comment assigner les tokens qui restent |
| 1800 |
assign prematch=\def\prematchtoks, |
| 1801 |
capture name={},% pour les noms des captures |
| 1802 |
expand arg=0,% développer le 2e argument obligatoire de \ifpegmatch ? |
| 1803 |
} |
| 1804 |
\newcount\tktl_match_position_cnt |
| 1805 |
\def\ifpegmatch{% |
| 1806 |
\tktl_ifnxttok[% |
| 1807 |
{\tktl_ifpegmatch_a} |
| 1808 |
{\tktl_ifpegmatch_a[]}% |
| 1809 |
} |
| 1810 |
\long\def\tktl_ifpegmatch_a[#1]#2#3{% |
| 1811 |
\begingroup |
| 1812 |
\tktl_allow_post_pattern_catpure_true |
| 1813 |
\tktl_allow_captures_true% permet les captures |
| 1814 |
\setKV[pegmatch]{#1}% |
| 1815 |
\ifnum\tktl_expand_arg_value>0 |
| 1816 |
\tktl_antefi{\expandafter\tktl_expand_n_times\expandafter{\tktl_expand_arg_value}}% |
| 1817 |
\fi |
| 1818 |
\tktl_encode_tokens{#3}% |
| 1819 |
\tktl_init_match |
| 1820 |
\tktl_match_position_cnt=1 |
| 1821 |
\let\tktl_prematch_enctokens\empty |
| 1822 |
\tktl_compile_patterns{#2}% |
| 1823 |
\expandafter\tktl_test_match_full_patterns\expanded{{\the\tktl_pattern_compile_collect}{\the\tktl_enctoks_toks}}% |
| 1824 |
\iftktl_match_success_ |
| 1825 |
\ifnum\tktl_match_mode=0 \unless\ifx\tktl_postmatch_enctokens\empty |
| 1826 |
\let\tktl_match_full_patterns\empty% si mode 0 et pas de match au début |
| 1827 |
\let\tktl_prematch_enctokens\empty |
| 1828 |
\edef\tktl_postmatch_enctokens{\the\tktl_enctoks_toks}% |
| 1829 |
\tktl_match_success_false |
| 1830 |
\fi\fi |
| 1831 |
\else |
| 1832 |
\ifnum\tktl_match_mode=2 % mode 2 et pas de match au début |
| 1833 |
\tktl_capture_pos_cnt=1 |
| 1834 |
\expandafter\tktl_ifpegmatch_b\expanded{{\unexpanded\expandafter{\tktl_postmatch_enctokens}}{\the\tktl_pattern_compile_collect}}% |
| 1835 |
\fi |
| 1836 |
\fi |
| 1837 |
\expanded{% sortir du groupe les tokens avant le match, ceux du match, ceux qui restent et les listes de captures |
| 1838 |
\endgroup |
| 1839 |
\edef\noexpand\matchposition{\iftktl_match_success_\the\tktl_match_position_cnt\else0\fi}% |
| 1840 |
\tktl_unexpand_earg\tktl_decode_enctoks{\tktl_match_full_patterns}{\unexpanded\expandafter{\tktl_assign_match}}% |
| 1841 |
\tktl_unexpand_earg\tktl_decode_enctoks{\tktl_prematch_enctokens}{\unexpanded\expandafter{\tktl_assign_prematch}}% |
| 1842 |
\tktl_unexpand_earg\tktl_decode_enctoks{\tktl_postmatch_enctokens}{\unexpanded\expandafter{\tktl_assign_remain}}% |
| 1843 |
\tktl_unexpand_earg\tktl_process_pos_capture{\tktl_capture_pos_list}{\tktl_capture_name}{\the\tktl_capture_pos_index_cnt}% |
| 1844 |
\tktl_unexpand_earg\tktl_process_tok_capture{\tktl_capture_tok_list}{\tktl_capture_name}{\the\tktl_capture_tok_index_cnt}% |
| 1845 |
\expandafter}% on décide avant de sortir du groupe si on renvoie T ou F |
| 1846 |
\csname |
| 1847 |
tktl_exec_% |
| 1848 |
\iftktl_match_success_ first\else second\fi |
| 1849 |
\endcsname |
| 1850 |
} |
| 1851 |
\long\def\tktl_ifpegmatch_b#1#2{% racourcir les enctokens #1 jusqu'à vide ou match succes #2=pattern |
| 1852 |
\tktl_ifempty_or_space{#1} |
| 1853 |
{} |
| 1854 |
{\tktl_ifpegmatch_c#1\_nil{#2}}% |
| 1855 |
} |
| 1856 |
\long\def\tktl_ifpegmatch_c(#1)#2\_nil#3{% #2=enctokens restant sans le 1er #3=pattern |
| 1857 |
\advance\tktl_capture_pos_cnt 1 |
| 1858 |
\advance\tktl_match_position_cnt 1 |
| 1859 |
\tktl_addtomacro\tktl_prematch_enctokens{(#1)}% |
| 1860 |
\tktl_capture_pos_index_cnt=0 |
| 1861 |
\tktl_capture_tok_index_cnt=0 |
| 1862 |
\let\tktl_capture_pos_list\empty |
| 1863 |
\let\tktl_capture_tok_list\empty |
| 1864 |
\tktl_in_capture_false |
| 1865 |
\tktl_in_patterngroup_false |
| 1866 |
\tktl_test_match_full_patterns{#3}{#2}% |
| 1867 |
\unless\iftktl_match_success_ |
| 1868 |
\tktl_antefi{\tktl_ifpegmatch_b{#2}{#3}}% |
| 1869 |
\fi |
| 1870 |
} |
| 1871 |
|
| 1872 |
%--------------------------------------------------------------------- |
| 1873 |
%--------------------- Macro publique \printtoks --------------------- |
| 1874 |
%--------------------------------------------------------------------- |
| 1875 |
% \printtoks{#1} affiche les tokens composant #1 |
| 1876 |
% avec leur charcode et leur catcode si besoin |
| 1877 |
%--------------------------------------------------------------------- |
| 1878 |
\defKV[printtoks]{ |
| 1879 |
baselinecoeff=\edef\tktl_baselineskip_coeff{\tktl_ifempty_or_space{#1}{1}{#1}}, |
| 1880 |
expand arg= \tktl_ifinteger{#1} |
| 1881 |
{\def\tktl_expand_arg_value{#1}} |
| 1882 |
{\errmessage{Key "expand arg" require an integer}\def\tktl_expand_arg_value{0}}, |
| 1883 |
mode= \tktl_test_mode{#1}[0;2]0\tktl_log_mode, |
| 1884 |
} |
| 1885 |
\setKVdefault[printtoks]{ |
| 1886 |
expand arg=0, |
| 1887 |
intertoks = 0.33em, |
| 1888 |
printcharcode = true, |
| 1889 |
printcatcode = true, |
| 1890 |
hexcharcode = false, |
| 1891 |
baselinecoeff = 0.8, |
| 1892 |
vlines=true, |
| 1893 |
boxed = true,% le tout dans une \hbox ? |
| 1894 |
code={}, |
| 1895 |
mode=0,% 0=pas d'envoi sur le log 1=envoi sur le log (et affichage) 2=envoi sur le log seulement |
| 1896 |
} |
| 1897 |
\def\printtoks{% |
| 1898 |
\tktl_ifnxttok[% |
| 1899 |
{\tktl_printtoks_a} |
| 1900 |
{\tktl_printtoks_a[]}% |
| 1901 |
} |
| 1902 |
\long\def\tktl_printtoks_a[#1]#2{% |
| 1903 |
\begingroup |
| 1904 |
\setKV[printtoks]{#1}% |
| 1905 |
\leavevmode |
| 1906 |
\tktl_ifnum{\tktl_expand_arg_value>0 } |
| 1907 |
{\expandafter\tktl_expand_n_times\expandafter{\tktl_expand_arg_value}} |
| 1908 |
{}% |
| 1909 |
\tktl_encode_tokens{#2}% |
| 1910 |
\unless\ifnum\tktl_log_mode=2 |
| 1911 |
\ifboolKV[printtoks]{boxed} |
| 1912 |
{\hbox\bgroup} |
| 1913 |
{}% |
| 1914 |
\ifboolKV[printtoks]{vlines} |
| 1915 |
{% |
| 1916 |
\vrule |
| 1917 |
\hskip.5\dimexpr\useKV[printtoks]{intertoks}\relax |
| 1918 |
} |
| 1919 |
{% |
| 1920 |
\hskip0pt |
| 1921 |
}% |
| 1922 |
\expandafter\tktl_printtoks_b\the\tktl_enctoks_toks(-1:-1)% |
| 1923 |
\ifboolKV[printtoks]{boxed} |
| 1924 |
{\egroup} |
| 1925 |
{}% |
| 1926 |
\fi |
| 1927 |
\ifnum\tktl_log_mode>0 |
| 1928 |
\immediate\write-1{^^J------ Begin token list ------}% |
| 1929 |
\expandafter\tktl_printtoks_c\the\tktl_enctoks_toks(-1:-1)% |
| 1930 |
\immediate\write-1{------- End token list -------^^J}% |
| 1931 |
\fi |
| 1932 |
\endgroup |
| 1933 |
} |
| 1934 |
\long\def\tktl_printtoks_b(#1:#2){% |
| 1935 |
\tktl_ifnum{#2=-1 } |
| 1936 |
{\unskip} |
| 1937 |
{% |
| 1938 |
\vtop{% |
| 1939 |
\baselineskip=\tktl_baselineskip_coeff\baselineskip |
| 1940 |
\halign{% |
| 1941 |
\hss##\hss\cr |
| 1942 |
\useKV[printtoks]{code}\tktl_ifnum{#2=16 }\string\char#1\relax\cr |
| 1943 |
\ifboolKV[printtoks]{printcharcode} |
| 1944 |
{% |
| 1945 |
\unless\ifnum#2=16 |
| 1946 |
$\scriptscriptstyle |
| 1947 |
\ifboolKV[printtoks]{hexcharcode} |
| 1948 |
{\tktl_dectohex{#1}\tktl_id} |
| 1949 |
{#1}% |
| 1950 |
$% |
| 1951 |
\fi |
| 1952 |
\cr |
| 1953 |
} |
| 1954 |
{}% |
| 1955 |
\ifboolKV[printtoks]{printcatcode} |
| 1956 |
{$\scriptscriptstyle#2$\cr} |
| 1957 |
{}% |
| 1958 |
}% |
| 1959 |
}% |
| 1960 |
\hskip.5\dimexpr\useKV[printtoks]{intertoks}\relax |
| 1961 |
\vrule |
| 1962 |
\hskip.5\dimexpr\useKV[printtoks]{intertoks}\relax |
| 1963 |
\tktl_printtoks_b |
| 1964 |
}% |
| 1965 |
} |
| 1966 |
\def\tktl_log_output_catcode#1{% |
| 1967 |
\ifcase#1\relax |
| 1968 |
\or begin-group character% 1 |
| 1969 |
\or end-group character% 2 |
| 1970 |
\or math shift character% 3 |
| 1971 |
\or alignment tab character% 4 |
| 1972 |
\or |
| 1973 |
\or macro parameter charcater% 6 |
| 1974 |
\or superscript character% 7 |
| 1975 |
\or subscript character% 8 |
| 1976 |
\or |
| 1977 |
\or blank space% 10 |
| 1978 |
\or letter% 11 |
| 1979 |
\or character% 12 |
| 1980 |
\or active character%13 |
| 1981 |
\fi |
| 1982 |
} |
| 1983 |
\long\def\tktl_printtoks_c(#1:#2){% |
| 1984 |
\unless\ifnum#2=-1 |
| 1985 |
\ifnum#2=16 % si séquence de contrôle |
| 1986 |
\tktl_if_primitive{#1} |
| 1987 |
{% |
| 1988 |
\immediate\write-1{\space\space\string#1\space catcode 16 (primitive)}% |
| 1989 |
} |
| 1990 |
{% |
| 1991 |
\immediate\write-1{\space\space\string#1\space catcode 16 (\meaning#1)}% |
| 1992 |
}% |
| 1993 |
\else |
| 1994 |
\tktl_hex_to_char{#1}\tktl_current_tok |
| 1995 |
\immediate\write-1{% |
| 1996 |
\space\space\tktl_current_tok\space catcode #2\ifnum#2<10 \space\fi\space(\tktl_log_output_catcode{#2})% |
| 1997 |
}% |
| 1998 |
\fi |
| 1999 |
\expandafter\tktl_printtoks_c |
| 2000 |
\fi |
| 2001 |
} |
| 2002 |
\long\def\tktl_if_primitive#1{% Vérifie que #1 est une primitive |
| 2003 |
\begingroup |
| 2004 |
\edef\__tempa{\meaning#1}% |
| 2005 |
\edef\__tempb{\string#1}% |
| 2006 |
\expandafter |
| 2007 |
\endgroup |
| 2008 |
\csname tktl_exec_\ifx\__tempa\__tempb first\else second\fi\endcsname |
| 2009 |
} |
| 2010 |
\def\tktl_dectohex#1#2{% #1=nombre base 10 #2=consigne d'assignation |
| 2011 |
\def\tktl_hexbase_result{}% |
| 2012 |
\tktl_dectohex_a{#1}% |
| 2013 |
\def\tktl__tmp{#2}\expandafter\tktl__tmp\expandafter{\tktl_hexbase_result}% |
| 2014 |
} |
| 2015 |
\def\tktl_dectohex_a#1{% |
| 2016 |
\tktl_test_cnt#1\relax |
| 2017 |
\divide\tktl_test_cnt16 |
| 2018 |
\edef\tktl_hexbase_result{% |
| 2019 |
\ifcase\numexpr#1-16*\tktl_test_cnt\relax |
| 2020 |
0\or1\or2\or3\or4\or5\or6\or7\or8\or9\or A\or B\or C\or D\or E\or F% |
| 2021 |
\fi |
| 2022 |
\tktl_hexbase_result |
| 2023 |
}% |
| 2024 |
\unless\ifnum\tktl_test_cnt=0 |
| 2025 |
\tktl_antefi{\expandafter\tktl_dectohex_a\expandafter{\the\tktl_test_cnt}}% |
| 2026 |
\fi |
| 2027 |
} |
| 2028 |
\def\tktl_superscript_two{\tktl_superscript\tktl_superscript} |
| 2029 |
\def\tktl_superscript_six{\tktl_superscript\tktl_superscript\tktl_superscript\tktl_superscript\tktl_superscript\tktl_superscript} |
| 2030 |
\def\tktl_hex_to_char#1{% #1=code dec #2=macro qui reçoit \string<token> où <token> a pour charcode #1 |
| 2031 |
\begingroup |
| 2032 |
\tktl_dectohex{#1}{\def\tktl_hex_charcode}% |
| 2033 |
\ifnum#1<"10 |
| 2034 |
\tktl_ok_this_test{\let\tktlsuperscriptrepeat\tktl_superscript_two\edef\tktl_hex_charcode{0\tktl_hex_charcode}}% |
| 2035 |
\fi |
| 2036 |
\ifnum#1<"100 |
| 2037 |
\tktl_ok_this_test{\let\tktlsuperscriptrepeat\tktl_superscript_two}% |
| 2038 |
\fi |
| 2039 |
\ifnum#1<"1000 % si #1>0xFF, mettre 6 chiffres hexadécimaux |
| 2040 |
\tktl_ok_this_test{\let\tktlsuperscriptrepeat\tktl_superscript_six\edef\tktl_hex_charcode{000\tktl_hex_charcode}}% |
| 2041 |
\fi |
| 2042 |
\ifnum#1<"10000 |
| 2043 |
\tktl_ok_this_test{\let\tktlsuperscriptrepeat\tktl_superscript_six\edef\tktl_hex_charcode{00\tktl_hex_charcode}}% |
| 2044 |
\fi |
| 2045 |
\ifnum#1<"100000 |
| 2046 |
\tktl_ok_this_test{\let\tktlsuperscriptrepeat\tktl_superscript_six\edef\tktl_hex_charcode{0\tktl_hex_charcode}}% |
| 2047 |
\fi |
| 2048 |
\tktl_else_all_tests{% |
| 2049 |
\ifnum#1>"FFFFFF \errmessage{Charcode too large, this error should not happen}\fi |
| 2050 |
}% |
| 2051 |
\lowercase\expandafter{\expandafter\tktl_hex_to_char_a\expandafter{\tktl_hex_charcode}}{#1}% |
| 2052 |
} |
| 2053 |
\def\tktl_hex_to_char_a#1#2#3{% #1=code hex minuscule #2=code dec #3=macro qui reçoit \string<token> où <token> a pour charcode #1 |
| 2054 |
\everyeof{\_nil}% |
| 2055 |
\endlinechar=-1 |
| 2056 |
\catcode#2=12 |
| 2057 |
\expandafter\tktl_store_char\expandafter#3\scantokens\expandafter{\expanded{\tktlsuperscriptrepeat#1}}% |
| 2058 |
} |
| 2059 |
\def\tktl_store_char#1#2#3\_nil{% TODO : tester si #3 est non vide -> erreur ? |
| 2060 |
\endgroup |
| 2061 |
\def#1{#2}% |
| 2062 |
} |
| 2063 |
%--------------------------------------------------------------------- |
| 2064 |
%------------------ Macro \tktl_analyse_replace_patterns ------------------ |
| 2065 |
%--------------------------------------------------------------------- |
| 2066 |
% #1 est une liste csv d'élements "motif -> code" |
| 2067 |
% |
| 2068 |
% Stocke les motifs et les codes dans des macros indexées par la macro |
| 2069 |
% \tktl_replace_patterns_index : |
| 2070 |
% paterns dans \replace_patterns__\romannumeral\tktl_replace_patterns_index |
| 2071 |
% codes dans \replace_code__\romannumeral\tktl_replace_patterns_index |
| 2072 |
%--------------------------------------------------------------------- |
| 2073 |
\def\tktl_analyse_replace_patterns#1{% #1 est une list csv de "motifs -> code" |
| 2074 |
\tktl_ifempty_or_space{#1} |
| 2075 |
{% |
| 2076 |
\errmessage{Empty pattern not accepted}% |
| 2077 |
} |
| 2078 |
{% |
| 2079 |
\def\tktl_replace_patterns_index{0}% |
| 2080 |
\tktl_analyse_replace_patterns_a#1,\relax,% |
| 2081 |
}% |
| 2082 |
} |
| 2083 |
\def\tktl_analyse_replace_patterns_a#1,{% #1 = "motifs -> code" en cours |
| 2084 |
\tktl_ifempty_or_space{#1} |
| 2085 |
{% ignorer si élément vide |
| 2086 |
\tktl_analyse_replace_patterns_a |
| 2087 |
} |
| 2088 |
{% |
| 2089 |
\def\tktl_current_pattern{#1}% |
| 2090 |
\unless\ifx\tktl_end_of_patterns\tktl_current_pattern |
| 2091 |
\tktl_antefi{\tktl_analyse_replace_patterns_b#1,}% |
| 2092 |
\fi |
| 2093 |
}% |
| 2094 |
} |
| 2095 |
\def\tktl_analyse_replace_patterns_b#1->{% #1 = motif en cours |
| 2096 |
\tktl_compile_patterns{#1}% |
| 2097 |
\edef\tktl_replace_patterns_index{\the\numexpr\tktl_replace_patterns_index+1}% |
| 2098 |
\expandafter\edef\csname replace_patterns__\romannumeral\tktl_replace_patterns_index\endcsname{\the\tktl_pattern_compile_collect}% |
| 2099 |
\tktl_analyse_replace_patterns_c\relax |
| 2100 |
} |
| 2101 |
\def\tktl_analyse_replace_patterns_c#1,{% #1 = code en cours précédé de \relax |
| 2102 |
\def\tktl__tmp{\expandafter\expandafter\expandafter\tktl_analyse_replace_patterns_d\tktl_stripsp}% |
| 2103 |
\expandafter\tktl__tmp\expandafter{\tktl_gob_arg#1}\tktl_analyse_replace_patterns_c |
| 2104 |
} |
| 2105 |
\def\tktl_analyse_replace_patterns_d#1\tktl_analyse_replace_patterns_c{% #1 = code en cours final (sans espace avant/après et sans accolades éventuelles) |
| 2106 |
\tktl_encode_tokens{#1}% |
| 2107 |
\expandafter\edef\csname replace_code__\romannumeral\tktl_replace_patterns_index\endcsname{\the\tktl_enctoks_toks}% |
| 2108 |
\tktl_analyse_replace_patterns_a |
| 2109 |
} |
| 2110 |
|
| 2111 |
%--------------------------------------------------------------------- |
| 2112 |
%--------------------- Macro publique \pegreplace -------------------- |
| 2113 |
%--------------------------------------------------------------------- |
| 2114 |
% \pegreplace[clés=val]{<csv pattern>}{<texte>} |
| 2115 |
% où : |
| 2116 |
% - <csv pattern> est une liste csv de "motifs -> code de remplacement", sachant |
| 2117 |
% que chaque motif, groupe de motifs ou macro-motif déclarée par \defpattern |
| 2118 |
% peut être précédé de "\c" pour en faire une capture. Il peut y avoir autant |
| 2119 |
% de captures que l'on souhaite |
| 2120 |
% Les captures \c après pattern sont interdites -> \errmessage |
| 2121 |
% - <texte> est un ensemble de tokens équilibrés en accolades dans lequel le <pattern> |
| 2122 |
% est cherché |
| 2123 |
% - le "code de remplacement" est un code tex dans lequel \0 signifie la capture entière |
| 2124 |
% correspondant au <pattern>, \1 est la première capture faite par un \c, \2 la deuxième, |
| 2125 |
% etc jusqu'à \9. |
| 2126 |
% |
| 2127 |
% Pour chaque correspondance au <pattern> trouvée dans le <texte>, Chaque occurence |
| 2128 |
% de \0...\9 dans le <remplacement> est remplacée par la capture concernée. |
| 2129 |
% |
| 2130 |
% Si la clé "assign" est vide, les tokens obtenus après les remplacements sont laissés |
| 2131 |
% dans le flux de lecture de TeX. Sinon, une assignation à une macro ou à un registre de |
| 2132 |
% tokens (obligatoire si le remplacmeent contient '#') est faite, selon la valeur dubooléen |
| 2133 |
% 'assign to toks' |
| 2134 |
%--------------------------------------------------------------------- |
| 2135 |
\newif\iftktl_allow_post_pattern_catpure_ |
| 2136 |
\defKV[pegreplace]{ |
| 2137 |
assign=\def\tktl_assign_result{#1}, |
| 2138 |
expand arg= \tktl_ifinteger{#1} |
| 2139 |
{\def\tktl_expand_arg_value{#1}} |
| 2140 |
{\errmessage{Key "expand arg" require an integer}\def\tktl_expand_arg_value{0}}, |
| 2141 |
mode=\tktl_test_mode{#1}[0;2]2\tktl_pegreplace_mode,% |
| 2142 |
} |
| 2143 |
\setKVdefault[pegreplace]{ |
| 2144 |
assign= ,% comment assigner les tokens qui ont matché (si vide -> les afficher) |
| 2145 |
expand arg=0,% développer le 2e argument obligatoire de \pegreplace ? |
| 2146 |
mode=2,% si 0=remplacer la 1re occurence d'un des motifs et fin |
| 2147 |
% 1=remplacer la 1re occurrence de chaque motif si possible |
| 2148 |
% 2=remplacer toutes les occurrences de tous les motifs |
| 2149 |
} |
| 2150 |
\def\pegreplace{% |
| 2151 |
\tktl_ifnxttok[%] |
| 2152 |
{\tktl_pegreplace_a} |
| 2153 |
{\tktl_pegreplace_a[]}% |
| 2154 |
} |
| 2155 |
\long\def\tktl_pegreplace_a[#1]#2#3{% #1=clé=val #2=csv "pattern->code" #3=texte |
| 2156 |
\begingroup |
| 2157 |
\tktl_allow_post_pattern_catpure_false |
| 2158 |
\tktl_allow_captures_true% permet les captures |
| 2159 |
\setKV[pegreplace]{#1}% |
| 2160 |
\ifnum\tktl_expand_arg_value>0 |
| 2161 |
\tktl_antefi{\tktl_expand_n_times\tktl_expand_arg_value}% |
| 2162 |
\fi |
| 2163 |
\tktl_encode_tokens{#3}% |
| 2164 |
\edef\tktl_replace_text{\the\tktl_enctoks_toks}% |
| 2165 |
\let\tktl_prematch_enctokens\empty |
| 2166 |
\tktl_analyse_replace_patterns{#2}% parcourir tous les "motifs -> code" et les stocker |
| 2167 |
\let\tktl_replace_patterns_max_index\tktl_replace_patterns_index |
| 2168 |
\tktl_capture_pos_cnt=1 |
| 2169 |
\expandafter\tktl_pegreplace_b\expandafter{\tktl_replace_text}% |
| 2170 |
\ifnum\tktl_pegreplace_mode=0 % si on n'a pas testé jusqu'à la fin de #3 |
| 2171 |
\tktl_eaddtomacro\tktl_prematch_enctokens\tktl_postmatch_enctokens |
| 2172 |
\fi |
| 2173 |
\expanded{% |
| 2174 |
\endgroup |
| 2175 |
\noexpand\tktl_decode_enctoks{\unexpanded\expandafter{\tktl_prematch_enctokens}}{% |
| 2176 |
\ifx\empty\tktl_assign_result |
| 2177 |
\noexpand\tktl_id |
| 2178 |
\else |
| 2179 |
\unexpanded\expandafter{\tktl_assign_result}% |
| 2180 |
\fi |
| 2181 |
}% |
| 2182 |
}% |
| 2183 |
} |
| 2184 |
\def\tktl_init_match{% |
| 2185 |
\tktl_capture_pos_cnt=1 |
| 2186 |
\tktl_capture_pos_index_cnt=0 |
| 2187 |
\tktl_capture_tok_index_cnt=0 |
| 2188 |
\let\tktl_capture_pos_list\empty |
| 2189 |
\let\tktl_capture_tok_list\empty |
| 2190 |
\tktl_in_capture_false |
| 2191 |
\tktl_in_patterngroup_false |
| 2192 |
\def\c{tktl_capture}% |
| 2193 |
} |
| 2194 |
\def\tktl_pegreplace_b#1{% #1=encotkens du texte |
| 2195 |
\tktl_ifempty_or_space{#1}% si plus de tokens -> fin |
| 2196 |
{} |
| 2197 |
{% |
| 2198 |
\def\tktl_replace_patterns_index{0}% initialise : numéro du motif en train d'être testé |
| 2199 |
\tktl_pegreplace_c#1\_nil |
| 2200 |
}% |
| 2201 |
} |
| 2202 |
\def\tktl_pegreplace_c(#1)#2\_nil{% #1=premier enctoken #2=enctokens restant |
| 2203 |
\tktl_init_match |
| 2204 |
\edef\tktl_replace_patterns_index{\the\numexpr\tktl_replace_patterns_index+1}% pattern suivant |
| 2205 |
\expandafter\def\expandafter\tktl_pegreplace_currentpattern\expandafter{\csname replace_patterns__\romannumeral\tktl_replace_patterns_index\endcsname}% |
| 2206 |
\def\tktl_do_next{\expandafter\expandafter\expandafter\tktl_test_match_full_patterns\expandafter\expandafter\expandafter{\tktl_pegreplace_currentpattern}{(#1)#2}}% tester si match |
| 2207 |
\ifnum\tktl_pegreplace_mode=1 |
| 2208 |
\expandafter\ifx\tktl_pegreplace_currentpattern\relax% si motif déjà rencontré |
| 2209 |
\tktl_match_success_false% sauf si mode=1 et motif ayant déjà matché |
| 2210 |
\let\tktl_do_next\empty |
| 2211 |
\fi |
| 2212 |
\fi |
| 2213 |
\tktl_do_next |
| 2214 |
\iftktl_match_success_% si (#1)#2 matche avec le motif n°\tktl_replace_patterns_index |
| 2215 |
\let\tktl_do_next\tktl_pegreplace_d% faire les remplacements |
| 2216 |
\ifnum\tktl_pegreplace_mode=1 % annuler le motif actuel |
| 2217 |
\expandafter\let\csname replace_patterns__\romannumeral\tktl_replace_patterns_index\endcsname\relax |
| 2218 |
\fi |
| 2219 |
\else% si pas de match |
| 2220 |
\ifnum\tktl_replace_patterns_index=\tktl_replace_patterns_max_index\relax% dernier pattern atteint ? |
| 2221 |
\tktl_addtomacro\tktl_prematch_enctokens{(#1)}% échec, ajouter (#1) aux tokens précédent le match |
| 2222 |
\def\tktl_do_next{\tktl_pegreplace_b{#2}}% recommencer avec les entokens restants |
| 2223 |
\else% s'il reste des patterns, retenter avec le pattern suivant |
| 2224 |
\def\tktl_do_next{\tktl_pegreplace_c(#1)#2\_nil}% |
| 2225 |
\fi |
| 2226 |
\fi |
| 2227 |
\tktl_do_next |
| 2228 |
} |
| 2229 |
\def\tktl_pegreplace_d{% |
| 2230 |
\edef\tktl_capture_tok_list{<0=\unexpanded\expandafter{\tktl_match_full_patterns}>\unexpanded\expandafter{\tktl_capture_tok_list}}% |
| 2231 |
\unless\ifx\empty\tktl_capture_tok_list% on procède au remplacement dans \tktl_capture_tok_list |
| 2232 |
\expandafter\expandafter\expandafter\tktl_process_replace\csname replace_code__\romannumeral\tktl_replace_patterns_index\endcsname\_nil\00\11\22\33\44\55\66\77\88\99\relax\relax |
| 2233 |
\fi |
| 2234 |
\tktl_eaddtomacro\tktl_prematch_enctokens\tktl_match_full_patterns% ajout les enctokens ayant matché et fin |
| 2235 |
\ifnum\tktl_pegreplace_mode>0 % si on doit continuer avec les enctokens restant |
| 2236 |
\tktl_antefi{\expandafter\tktl_pegreplace_b\expandafter{\tktl_postmatch_enctokens}}% |
| 2237 |
\fi |
| 2238 |
} |
| 2239 |
\def\tktl_pegreplace_all_matches#1#2{% #1=encotkens du texte #2=pattern |
| 2240 |
\tktl_pegreplace_b{#1}{#2}% |
| 2241 |
\unless\ifx\tktl_postmatch_enctokens\empty |
| 2242 |
\tktl_antefi{\expandafter\tktl_pegreplace_all_matches\expandafter{\tktl_postmatch_enctokens}{#2}}% |
| 2243 |
\fi |
| 2244 |
} |
| 2245 |
\long\def\tktl_process_replace#1\_nil#2#3{% #1=liste des remplacements <\d=code> #2=\<d> #3=<d> |
| 2246 |
\ifx\relax#2% |
| 2247 |
\def\tktl_match_full_patterns{#1}% |
| 2248 |
\else% définition de la macro de remplacement |
| 2249 |
\long\def\tktl_process_replace_b##1<#3=##2>##3\_nil##4(#2:16)##5\_nil{% |
| 2250 |
\tktl_process_replace_a{##4##2##5}{#2}{#3}% reboucle pour faire tous les remplacements de \<d> s'il y en a plusieurs |
| 2251 |
}% |
| 2252 |
\tktl_antefi{\tktl_process_replace_a{#1}{#2}{#3}}% |
| 2253 |
\fi |
| 2254 |
} |
| 2255 |
\long\def\tktl_process_replace_a#1#2#3{% |
| 2256 |
\tktl_ifinstr{(#2:16)}{#1} |
| 2257 |
{% |
| 2258 |
\expandafter\tktl_process_replace_b\tktl_capture_tok_list\_nil#1\_nil |
| 2259 |
} |
| 2260 |
{% |
| 2261 |
\tktl_process_replace#1\_nil |
| 2262 |
}% |
| 2263 |
} |
| 2264 |
|
| 2265 |
%--------------------------------------------------------------------- |
| 2266 |
%--------------------- Macro publique \pegcount ---------------------- |
| 2267 |
%--------------------------------------------------------------------- |
| 2268 |
% \pegcount[clés=valeur]{<pattern>}{<tokens>} |
| 2269 |
% compte combien de fois le <pattern> correspond dans les <tokens> |
| 2270 |
% |
| 2271 |
% Les captures \c, où qu'elles soient, sont ignorées |
| 2272 |
% |
| 2273 |
% Renvoie : |
| 2274 |
% - le nombre de correspondances (dans le flux ou dans une macro, selon la clé "assign") |
| 2275 |
% - la liste des positions de correspondances (dans la macro spécifiée par la clé "assign positions") |
| 2276 |
% - les correspondances trouvées consultables par \tokscapture{<index>}. |
| 2277 |
% Si <index>=0, livre la liste des correspondances dans une csv : {capt1},{capt2},...,{captn} |
| 2278 |
%--------------------------------------------------------------------- |
| 2279 |
\defKV[pegcount]{ |
| 2280 |
assign=\def\tktl_assign_result{#1}, |
| 2281 |
expand arg= \tktl_ifinteger{#1} |
| 2282 |
{\def\tktl_expand_arg_value{#1}} |
| 2283 |
{% |
| 2284 |
\errmessage{Key "expand arg" requires an integer}% |
| 2285 |
\def\tktl_expand_arg_value{0}% |
| 2286 |
}, |
| 2287 |
assign positions=\def\tktl_pegcount_assign_list{#1}, |
| 2288 |
name=\def\tktl_pegcount_capturename{#1}, |
| 2289 |
} |
| 2290 |
\setKVdefault[pegcount]{ |
| 2291 |
assign={},% comment assigner les tokens qui ont matché (si vide -> les afficher) |
| 2292 |
expand arg=0,% développer le 2e argument obligatoire de \pegcount ? |
| 2293 |
assign positions=\def\matchposlist, |
| 2294 |
name={},% nom des captures des correspondances |
| 2295 |
} |
| 2296 |
\newcount\tktl_pegcount_cnt% compteur des occurrences trouvées |
| 2297 |
\newcount\tktl_pegcount_pos_cnt% compteur de la position des occurrences trouvées |
| 2298 |
\def\pegcount{% |
| 2299 |
\tktl_ifnxttok[% |
| 2300 |
{\tktl_pegcount_a} |
| 2301 |
{\tktl_pegcount_a[]}% |
| 2302 |
} |
| 2303 |
\long\def\tktl_pegcount_a[#1]#2#3{% #1=clé=val #2=pattern #3=texte |
| 2304 |
\begingroup |
| 2305 |
\tktl_allow_post_pattern_catpure_false |
| 2306 |
\tktl_allow_captures_false% ignore les captures |
| 2307 |
\setKV[pegcount]{#1}% |
| 2308 |
\ifnum\tktl_expand_arg_value>0 |
| 2309 |
\tktl_antefi{\tktl_expand_n_times\tktl_expand_arg_value}% |
| 2310 |
\fi |
| 2311 |
\tktl_encode_tokens{#3}% |
| 2312 |
\tktl_pegcount_cnt=0 |
| 2313 |
\tktl_pegcount_pos_cnt=1 |
| 2314 |
\let\tktl_pegcount_pos_list\empty |
| 2315 |
\let\tktl_pegcount_match_list\empty |
| 2316 |
\tktl_compile_patterns{#2}% |
| 2317 |
\expandafter\tktl_pegcount_all_matches\expanded{{\the\tktl_enctoks_toks}{\the\tktl_pattern_compile_collect}}% |
| 2318 |
\expanded{% sortir du groupe les tokens avant le match, ceux du match, ceux qui restent et les listes de captures |
| 2319 |
\endgroup |
| 2320 |
\tktl_unexpand_earg\tktl_process_tok_capture{\tktl_pegcount_match_list}{\tktl_pegcount_capturename}{\the\tktl_pegcount_cnt}% |
| 2321 |
\unexpanded\expandafter{\tktl_pegcount_assign_list}{% |
| 2322 |
\unless\ifx\tktl_pegcount_pos_list\empty |
| 2323 |
\expandafter\tktl_gob_arg\tktl_pegcount_pos_list |
| 2324 |
\fi |
| 2325 |
}% |
| 2326 |
\ifx\tktl_assign_result\empty |
| 2327 |
\noexpand\tktl_id |
| 2328 |
\else |
| 2329 |
\unexpanded\expandafter{\tktl_assign_result}% |
| 2330 |
\fi{\the\tktl_pegcount_cnt}% |
| 2331 |
}% |
| 2332 |
} |
| 2333 |
\long\def\tktl_pegcount_to_first_match#1#2{% #1=encotkens du texte #2=pattern |
| 2334 |
\tktl_ifempty_or_space{#1}% si plus de tokens -> fin |
| 2335 |
{} |
| 2336 |
{% |
| 2337 |
\tktl_init_match |
| 2338 |
\tktl_test_match_full_patterns{#2}{#1}% match ? |
| 2339 |
\iftktl_match_success_% si match |
| 2340 |
\advance\tktl_pegcount_cnt 1 |
| 2341 |
\tktl_eaddtomacro\tktl_pegcount_match_list{\expanded{<\the\tktl_pegcount_cnt={\unexpanded\expandafter{\tktl_match_full_patterns}}>}}% |
| 2342 |
\tktl_eaddtomacro\tktl_pegcount_pos_list{\expandafter,\the\tktl_pegcount_pos_cnt}% |
| 2343 |
\advance\tktl_pegcount_pos_cnt\numexpr\tktl_postmatch_pos-1\relax |
| 2344 |
\else% si pas de match -> appel de la macro récursive qui mange un à un les enctokens |
| 2345 |
\tktl_antefi{\expandafter\tktl_pegcount_to_first_match_a\tktl_postmatch_enctokens\_nil{#2}}% |
| 2346 |
\fi |
| 2347 |
}% |
| 2348 |
} |
| 2349 |
\long\def\tktl_pegcount_to_first_match_a(#1)#2\_nil#3{% |
| 2350 |
\def\tktl_postmatch_enctokens{#2}% |
| 2351 |
\advance\tktl_pegcount_pos_cnt 1 |
| 2352 |
\tktl_pegcount_to_first_match{#2}{#3}% |
| 2353 |
} |
| 2354 |
|
| 2355 |
\long\def\tktl_pegcount_all_matches#1#2{% #1=encotkens du texte #2=pattern |
| 2356 |
\tktl_pegcount_to_first_match{#1}{#2}% |
| 2357 |
\unless\ifx\tktl_postmatch_enctokens\empty |
| 2358 |
\tktl_antefi{\expandafter\tktl_pegcount_all_matches\expandafter{\tktl_postmatch_enctokens}{#2}}% |
| 2359 |
\fi |
| 2360 |
} |
| 2361 |
|
| 2362 |
%--------------------------------------------------------------------- |
| 2363 |
%-------- Capture d'un motif de correspondance pour *1* token -------- |
| 2364 |
%--------------------------------------------------------------------- |
| 2365 |
%%%%%%%%%%%% capture d'un motif de correspondance pour *1* token |
| 2366 |
% Entrée : le motif est composé de : |
| 2367 |
% - macro ayant 1 caractère qui spécifie le type de motif : |
| 2368 |
% \S{<chaine>} : le token matche avec un des tokens de la <chaine> |
| 2369 |
% \R{csv de charcodes range "<a>-<b>" ou "<c>" ou "*":csv de catcodes "<n>-<m>" ou "<x>" ou "*"} et si csv de catcodes absent: "*". Macros interdites dans les ranges. |
| 2370 |
% \r{csv de chars range "<a>-<b>" ou "<c>":csv de catcodes "<n>-<m>" ou "<n>" ou "*"} et si csv de catcodes absent: "*". Macros interdites dans les ranges. |
| 2371 |
% |
| 2372 |
% Renvoie : |
| 2373 |
% - \tktl_current_macro est la macro spécifiant le type de motif (\S, \r, ou \R) |
| 2374 |
% - \tktl_current_macro_arg est l'argument de la macro |
| 2375 |
%--------------------------------------------------------------------- |
| 2376 |
\long\def\tktl_grab_tokpattern#1{% #1= pattern unique pour 1 token |
| 2377 |
\let\tktl_current_macro\empty |
| 2378 |
\let\tktl_current_macro_arg\empty |
| 2379 |
\tktl_grab_tokpattern_search_precapture#1\_nil |
| 2380 |
} |
| 2381 |
\def\tktl_grab_tokpattern_search_precapture{% |
| 2382 |
\tktl_futurelet_nospace\tktl__futurtok\tktl_grab_tokpattern_grab_macro |
| 2383 |
} |
| 2384 |
\def\tktl_grab_tokpattern_grab_macro#1{% #1 = macro |
| 2385 |
\tktl_ifinstr{,#1,}{,\S,\r,\R,} |
| 2386 |
{% |
| 2387 |
\def\tktl_current_macro{#1}% |
| 2388 |
\tktl_futurelet_nospace\tktl__futurtok\tktl_grab_tokpattern_grab_macro_arg |
| 2389 |
} |
| 2390 |
{% |
| 2391 |
\errmessage{Found "\detokenize{#1}" when expecting \string\r, \string\R, or \string\S}% |
| 2392 |
}% |
| 2393 |
} |
| 2394 |
\def\tktl_grab_tokpattern_grab_macro_arg{% |
| 2395 |
\ifx\bgroup\tktl__futurtok |
| 2396 |
\expandafter\tktl_grab_tokpattern_grab_macro_arg_a |
| 2397 |
\else |
| 2398 |
\errmessage{No open brace found after "\detokenize\expandafter{\tktl_current_macro}", aborting pattern}% |
| 2399 |
\let\tktl_current_macro\empty |
| 2400 |
\let\tktl_current_macro_arg\empty |
| 2401 |
\expandafter\tktl_gob_enctoks_to_end_parse |
| 2402 |
\fi |
| 2403 |
} |
| 2404 |
\long\def\tktl_grab_tokpattern_grab_macro_arg_a#1{% argument de la macro |
| 2405 |
\def\tktl_current_macro_arg{#1}% |
| 2406 |
\tktl_grab_pattern_remain |
| 2407 |
} |
| 2408 |
\long\def\tktl_compile_tokpatterns#1#2{% #1=numéro de groupe de pattern (commencer à 0) #2=groupe de pattern |
| 2409 |
\edef\tked_compile_tokpattern_n{\the\numexpr1000*#1}% compte les macros de compilation |
| 2410 |
\tktl_compile_tokpatterns_a#2|\relax|% |
| 2411 |
} |
| 2412 |
\long\def\tktl_compile_tokpatterns_a#1|{% compiler les n patterns dans n+1 macros |
| 2413 |
\edef\tked_compile_tokpattern_n{\the\numexpr\tked_compile_tokpattern_n+1}% |
| 2414 |
\ifx\relax#1% macro n+1 -> échec, aucun match |
| 2415 |
\expandafter\long\expandafter\def\csname tktl_test_match_tokpattern_\romannumeral\tked_compile_tokpattern_n\endcsname(##1:##2){\tktl_match_success_false}% |
| 2416 |
\else |
| 2417 |
\tktl_grab_tokpattern{#1}% |
| 2418 |
\ifx\tktl_current_macro\tktl_macro_S |
| 2419 |
\expandafter\tktl_catcode_string\expandafter{\tktl_current_macro_arg}% prendre en compte les \c{<catcode>}{texte} dans l'argument de \s |
| 2420 |
\expandafter\long\expandafter\edef\csname tktl_test_match_tokpattern_\romannumeral\tked_compile_tokpattern_n\endcsname(##1:##2){% |
| 2421 |
\noexpand\tktl_ifinstr{(##1:##2)}{\the\tktl_string_toks}% |
| 2422 |
{\noexpand\tktl_match_success_true} |
| 2423 |
{\expandafter\noexpand\csname tktl_test_match_tokpattern_\romannumeral\the\numexpr\tked_compile_tokpattern_n+1\relax\endcsname(##1:##2)}% |
| 2424 |
}% |
| 2425 |
\else% si \R ou \r |
| 2426 |
\ifnum0\ifx\tktl_current_macro\tktl_macro_r1\else\ifx\tktl_current_macro\tktl_macro_R1\fi\fi=1 |
| 2427 |
\expandafter\tktl_analyse_R_pattern\expandafter{\tktl_current_macro_arg}% |
| 2428 |
\ifx\tktl_current_macro\tktl_macro_r |
| 2429 |
\expandafter\tktl_convert_charcsv_to_charcodecsv\expandafter{\tktl_charcode_interval}% |
| 2430 |
\fi |
| 2431 |
\expandafter\long\expandafter\edef\csname tktl_test_match_tokpattern_\romannumeral\tked_compile_tokpattern_n\endcsname(##1:##2){% |
| 2432 |
\noexpand\tktl_iftokmatch(##1:##2){\unexpanded\expandafter{\tktl_charcode_interval}}{\unexpanded\expandafter{\tktl_catcode_interval}} |
| 2433 |
{\noexpand\tktl_match_success_true} |
| 2434 |
{\expandafter\noexpand\csname tktl_test_match_tokpattern_\romannumeral\the\numexpr\tked_compile_tokpattern_n+1\relax\endcsname(##1:##2)}% |
| 2435 |
}% |
| 2436 |
\fi\fi |
| 2437 |
\expandafter\tktl_compile_tokpatterns_a |
| 2438 |
\fi |
| 2439 |
} |
| 2440 |
|
| 2441 |
%--------------------------------------------------------------------- |
| 2442 |
%------------------- Macro utilisateur \tokscount ------------------- |
| 2443 |
%--------------------------------------------------------------------- |
| 2444 |
% \tokscount[clés=valeurs]{<p1> | <p2> | ... }{texte} |
| 2445 |
% |
| 2446 |
% où <pi> est un pattern pour 1 token, c'est-à-dire \r{<consigne>}, \R{<consigne>}, \S{<tokens>} |
| 2447 |
% |
| 2448 |
% Compte combien de tokens dans le texte correspondent aux <pi>. |
| 2449 |
% |
| 2450 |
% Renvoie : |
| 2451 |
% - si aucun <pi> -> compte le nombre de tokens |
| 2452 |
% - sinon : nombre de correspondances (dans le flux ou dans une macro selon la clé "assign") |
| 2453 |
% - les tokens ayant matché dans la consigne "assign match". Seuls les tokens de catcode |
| 2454 |
% 3,4,7,8,10,11,12,13,16 sont capturés, les autres sont ignorés. |
| 2455 |
% Si la valeur de "assign match" est vide, ne renvoie aucun token |
| 2456 |
%--------------------------------------------------------------------- |
| 2457 |
\defKV[tokscount]{ |
| 2458 |
assign=\def\tktl_assign_result{#1}, |
| 2459 |
expand arg= \tktl_ifinteger{#1} |
| 2460 |
{\def\tktl_expand_arg_value{#1}} |
| 2461 |
{% |
| 2462 |
\errmessage{Key "expand arg" requires an integer}% |
| 2463 |
\def\tktl_expand_arg_value{0}% |
| 2464 |
}, |
| 2465 |
assign match= \def\tktl_assign_tokmatch{#1}, |
| 2466 |
} |
| 2467 |
\setKVdefault[tokscount]{ |
| 2468 |
assign={},% comment assigner les tokens qui ont matché (si vide -> les afficher) |
| 2469 |
expand arg=0,% développer le 2e argument obligatoire de \tokscount ? |
| 2470 |
assign match={},% pas d'export de collecte par défaut |
| 2471 |
} |
| 2472 |
|
| 2473 |
\def\tokscount{% |
| 2474 |
\tktl_ifnxttok[%] |
| 2475 |
{\tktl_tokscount_a} |
| 2476 |
{\tktl_tokscount_a[]}% |
| 2477 |
} |
| 2478 |
\long\def\tktl_tokscount_a[#1]#2#3{% #1=clés/valeurs #2=patterns #3=texte |
| 2479 |
\begingroup |
| 2480 |
\let\tktl_tokscount_collect\empty |
| 2481 |
\tktl_allow_post_pattern_catpure_false |
| 2482 |
\tktl_allow_captures_false% ignore les captures |
| 2483 |
\setKV[tokscount]{#1}% |
| 2484 |
\ifnum\tktl_expand_arg_value>0 |
| 2485 |
\tktl_antefi{\tktl_expand_n_times\tktl_expand_arg_value}% |
| 2486 |
\fi |
| 2487 |
\tktl_encode_tokens{#3}% |
| 2488 |
\tktl_ifempty_or_space{#2} |
| 2489 |
{% |
| 2490 |
\tktl_pegcount_cnt=\tktl_enctoks_len_cnt |
| 2491 |
\let\tktl_tokscount_collect\empty% aucune collecte de tokens si pas de pattern |
| 2492 |
} |
| 2493 |
{% |
| 2494 |
\edef\tktl_tokscount_enctokens{\the\tktl_enctoks_toks}% |
| 2495 |
\tktl_compile_tokpatterns0{#2}% compiler les patterns |
| 2496 |
\tktl_pegcount_cnt=0 |
| 2497 |
\expandafter\tktl_tokscount_b\tktl_tokscount_enctokens(-1:-1)% |
| 2498 |
}% |
| 2499 |
\expanded{% |
| 2500 |
\endgroup |
| 2501 |
\unless\ifx\tktl_assign_tokmatch\empty |
| 2502 |
\tktl_unexpand_earg\tktl_decode_enctoks{\tktl_tokscount_collect}{\unexpanded\expandafter{\tktl_assign_tokmatch}}% |
| 2503 |
\fi |
| 2504 |
\tktl_ifx{\tktl_assign_result\empty} |
| 2505 |
{\noexpand\tktl_id} |
| 2506 |
{\unexpanded\expandafter{\tktl_assign_result}}% |
| 2507 |
{\the\tktl_pegcount_cnt}% |
| 2508 |
}% |
| 2509 |
} |
| 2510 |
\long\def\tktl_tokscount_b(#1:#2){% tester tous les enctokens |
| 2511 |
\ifnum#2>0 |
| 2512 |
\tktl_test_match_tokpattern_i(#1:#2)% le token matche-t-il ? |
| 2513 |
\iftktl_match_success_ |
| 2514 |
\tktl_ifinstr{,#2,}{,3,4,7,8,10,11,12,13,16,}% si catcode pas embêtant (différent de 1, 2 ou 6) |
| 2515 |
{\tktl_addtomacro\tktl_tokscount_collect{(#1:#2)}}% ajouter au collecteur de tokens ayant matché |
| 2516 |
{}% |
| 2517 |
\advance\tktl_pegcount_cnt 1 |
| 2518 |
\fi |
| 2519 |
\expandafter\tktl_tokscount_b |
| 2520 |
\fi |
| 2521 |
} |
| 2522 |
|
| 2523 |
%--------------------------------------------------------------------- |
| 2524 |
%--------------------- Macro utilisateur \toksdo -------------------- |
| 2525 |
%--------------------------------------------------------------------- |
| 2526 |
\newif\iftktl_toksdo_collect_ |
| 2527 |
\newif\iftktl_is_one_token_ |
| 2528 |
\newtoks\tktl_result_toks |
| 2529 |
\defKV[toksdo]{% |
| 2530 |
assign=\def\tktl_assign_result{#1},% fix v0.2 |
| 2531 |
expand arg= \tktl_ifinteger{#1} |
| 2532 |
{\def\tktl_expand_arg_value{#1}} |
| 2533 |
{% |
| 2534 |
\errmessage{Key "expand arg" requires an integer}% |
| 2535 |
\def\tktl_expand_arg_value{0}% |
| 2536 |
}, |
| 2537 |
collect = \testboolKV{#1}\tktl_toksdo_collect_true\tktl_toksdo_collect_false, |
| 2538 |
} |
| 2539 |
\setKVdefault[toksdo]{ |
| 2540 |
assign={},% comment assigner les tokens qui ont matché (si vide -> les afficher) |
| 2541 |
expand arg=0,% développer le 2e argument obligatoire de \toksdo ? |
| 2542 |
collect = true,% collecter les tokens obtenus ? |
| 2543 |
} |
| 2544 |
\long\def\tktl_add_token_toksdo#1#2{% |
| 2545 |
\advance\tktl_enctoks_len_cnt1 |
| 2546 |
\expandafter\tktl_add_to_parsetoks\expandafter{\expandafter(\expandafter[\the\tktl_enctoks_len_cnt]#1:#2)}% |
| 2547 |
}% |
| 2548 |
\def\toksdo{% |
| 2549 |
\tktl_ifnxttok[%] |
| 2550 |
{\tktl_toksdo_a} |
| 2551 |
{\tktl_toksdo_a[]}% |
| 2552 |
} |
| 2553 |
\long\def\tktl_toksdo_a[#1]#2#3{% #1=clés/valeurs #2=patterns #3=texte |
| 2554 |
\let\setcharcode_saved\setcharcode \let\setcharcode\tktl_setcharcode |
| 2555 |
\let\setcatcode_saved\setcatcode \let\setcatcode\tktl_setcatcode |
| 2556 |
\let\deltok_saved\deltok \let\deltok\tktl_deltok |
| 2557 |
\let\addtok_saved\addtok \let\addtok\tktl_addtokt |
| 2558 |
\let\selfcharcode_saved\selfcharcode |
| 2559 |
\let\selfcatcode_saved\selfcatcode |
| 2560 |
\let\selfindex_saved\selfindex |
| 2561 |
\let\tktl_toksdo_collect\empty |
| 2562 |
\tktl_allow_post_pattern_catpure_false |
| 2563 |
\tktl_allow_captures_false% ignore les captures |
| 2564 |
\restoreKV[toksdo]% |
| 2565 |
\setKV[toksdo]{#1}% |
| 2566 |
\let\tktl_add_token_saved\tktl_add_token |
| 2567 |
\let\tktl_add_token\tktl_add_token_toksdo |
| 2568 |
\ifnum\tktl_expand_arg_value>0 |
| 2569 |
\tktl_antefi{\tktl_expand_n_times\tktl_expand_arg_value}% |
| 2570 |
\fi |
| 2571 |
\tktl_encode_tokens{#3}% |
| 2572 |
\let\tktl_add_token\tktl_add_token_saved |
| 2573 |
\edef\tokslen{\the\tktl_enctoks_len_cnt}% nombre de tokens |
| 2574 |
\edef\tktl_toksdo_enctokens{\the\tktl_enctoks_toks}% |
| 2575 |
\tktl_ifempty_or_space{#2} |
| 2576 |
{% |
| 2577 |
\errmessage{No pattern and action found, using \string\R\detokenize{{*:*}->{}}}% |
| 2578 |
\tktl_compile_toksdo_patterns{\R{*:*}->{}}% |
| 2579 |
} |
| 2580 |
{% |
| 2581 |
\tktl_compile_toksdo_patterns{#2}% |
| 2582 |
}% |
| 2583 |
\let\tktl_toksdo_pattern_maxindex\tktl_toksdo_pattern_index |
| 2584 |
\let\tktl_toksdo_enctok_collect\empty |
| 2585 |
\expandafter\tktl_toksdo_b\tktl_toksdo_enctokens([-1]-1:-1)% |
| 2586 |
\let\setcharcode\setcharcode_saved |
| 2587 |
\let\setcatcode\setcatcode_saved |
| 2588 |
\let\deltok\deltok_saved |
| 2589 |
\let\addtok\addtok_saved |
| 2590 |
\ifx\tktl_assign_result\empty |
| 2591 |
\expandafter\tktl_decode_enctoks\expandafter{\tktl_toksdo_enctok_collect}{\tktl_result_toks}% |
| 2592 |
\tktl_antefi{\the\tktl_result_toks}% |
| 2593 |
\else |
| 2594 |
\tktl_antefi{\expandafter\tktl_decode_enctoks\expanded{{\unexpanded\expandafter{\tktl_toksdo_enctok_collect}}{\unexpanded\expandafter{\tktl_assign_result}}}}% |
| 2595 |
\fi |
| 2596 |
} |
| 2597 |
\long\def\tktl_toksdo_b([#1]#2:#3){% tester chaque enctokens |
| 2598 |
\ifnum#3>0 |
| 2599 |
\def\selfindex{#1}% |
| 2600 |
\def\selfcharcode{#2}% |
| 2601 |
\def\selfcatcode{#3}% |
| 2602 |
\def\tktl_toksdo_pattern_index{-1}% |
| 2603 |
\let\tktl_addtok_code\tktl_addtok_code_default% ajouter le token seul par défaut |
| 2604 |
\def\tktl_current_token{(#2:#3)}% |
| 2605 |
\tktl_is_one_token_true |
| 2606 |
\tktl_toksdo_c(#2:#3)% |
| 2607 |
\unless\ifx\tktl_addtok_code\empty \iftktl_toksdo_collect_ % si ajout de token et collecte des tokens |
| 2608 |
\ifx\tktl_addtok_code\tktl_addtok_code_default% si pas de redéfinition par \addtok, ne pas perdre de temps et ajouter le token |
| 2609 |
\tktl_eaddtomacro\tktl_toksdo_enctok_collect\tktl_current_token |
| 2610 |
\else |
| 2611 |
\begingroup |
| 2612 |
\expandafter\tktl_encode_tokens\expandafter{\tktl_addtok_code}% |
| 2613 |
\expanded{% |
| 2614 |
\endgroup |
| 2615 |
\noexpand\tktl_subst_all% remplacer |
| 2616 |
{(\noexpand\self:16)}% tous les \self par \tktl_current_token qui est (charcode:catcode) lorsqu'unique token |
| 2617 |
{\unexpanded\expandafter{\tktl_current_token}} |
| 2618 |
{\the\tktl_enctoks_toks}% et ajouter le résultat à \tktl_toksdo_enctok_collect |
| 2619 |
{\noexpand\tktl_eaddtomacro\noexpand\tktl_toksdo_enctok_collect}% |
| 2620 |
}% |
| 2621 |
\fi |
| 2622 |
\fi\fi |
| 2623 |
\expandafter\tktl_toksdo_b |
| 2624 |
\fi |
| 2625 |
} |
| 2626 |
\long\def\tktl_toksdo_c(#1:#2){% tester le token pour tous les patterns/actions spécifiés par l'utilisateur |
| 2627 |
\edef\tktl_toksdo_pattern_index{\the\numexpr\tktl_toksdo_pattern_index+1}% |
| 2628 |
\csname tktl_test_match_tokpattern_\romannumeral\numexpr1000*\tktl_toksdo_pattern_index+1\endcsname(#1:#2)% le token matche-t-il ? |
| 2629 |
\iftktl_match_success_% si oui, effectuer l'action prévue et fin |
| 2630 |
\expandafter\let\expandafter\tktl_do_next\csname tktl_toksdo_action_\romannumeral\tktl_toksdo_pattern_index\endcsname |
| 2631 |
\else |
| 2632 |
\ifnum\tktl_toksdo_pattern_index=\tktl_toksdo_pattern_maxindex\relax% dernier pattern atteint |
| 2633 |
\let\tktl_do_next\empty |
| 2634 |
\else |
| 2635 |
\def\tktl_do_next{\tktl_toksdo_c(#1:#2)}% |
| 2636 |
\fi |
| 2637 |
\fi |
| 2638 |
\tktl_do_next |
| 2639 |
} |
| 2640 |
\long\def\tktl_compile_toksdo_patterns#1{% #1 contient <pattern1> -> <action1>, <pattern2> -> <action2>, etc |
| 2641 |
\def\tktl_toksdo_pattern_index{-1}% |
| 2642 |
\tktl_compile_toksdo_patterns_a#1,\relax,% |
| 2643 |
} |
| 2644 |
\long\def\tktl_compile_toksdo_patterns_a#1,{% |
| 2645 |
\tktl_ifempty_or_space{#1} |
| 2646 |
{% ignorer si vide |
| 2647 |
\tktl_compile_toksdo_patterns_a |
| 2648 |
} |
| 2649 |
{% |
| 2650 |
\def\tktl_current_pattern{#1}% |
| 2651 |
\unless\ifx\tktl_end_of_patterns\tktl_current_pattern |
| 2652 |
\tktl_antefi{\tktl_compile_toksdo_patterns_b#1,}% |
| 2653 |
\fi |
| 2654 |
}% |
| 2655 |
} |
| 2656 |
\long\def\tktl_compile_toksdo_patterns_b#1->#2,{% |
| 2657 |
\edef\tktl_toksdo_pattern_index{\the\numexpr\tktl_toksdo_pattern_index+1}% |
| 2658 |
\expandafter\tktl_compile_tokpatterns\expandafter{\tktl_toksdo_pattern_index}{#1}% compiler les patterns |
| 2659 |
\expandafter\def |
| 2660 |
\csname tktl_toksdo_action_\romannumeral\tktl_toksdo_pattern_index\expandafter\expandafter\expandafter\endcsname |
| 2661 |
\expandafter\expandafter\expandafter{\tktl_stripsp{#2}}% sauvegarder l'action à faire |
| 2662 |
\tktl_compile_toksdo_patterns_a |
| 2663 |
} |
| 2664 |
\def\tktl_addtokt#{% comment ajouter le token en cours. Dans #1, la macro \self serprésente le token modifié (ou pas) |
| 2665 |
\def\tktl_addtok_code |
| 2666 |
} |
| 2667 |
\def\tktl_addtok_code_default{% |
| 2668 |
\self |
| 2669 |
} |
| 2670 |
\long\def\tktl_subst_all#1#2#3#4{% remplace toutes les occurrences de #1 par #2 dans #3, #4=consigne d'assignation |
| 2671 |
\long\def\tktl_subst_all_a##1#1##2\_nil{% |
| 2672 |
\tktl_ifinstr{#1}{##1#2##2} |
| 2673 |
{\tktl_subst_all_a##1#2##2\_nil} |
| 2674 |
{#4{##1#2##2}}% |
| 2675 |
}% |
| 2676 |
\tktl_ifinstr{#1}{#3} |
| 2677 |
{\tktl_subst_all_a#3\_nil} |
| 2678 |
{#4{#3}}% |
| 2679 |
} |
| 2680 |
\def\tktl_deltok{% équivalent à \addotk{} |
| 2681 |
\let\tktl_addtok_code\empty |
| 2682 |
} |
| 2683 |
\def\tktl_setcharcode#1{% |
| 2684 |
\iftktl_is_one_token_% que si c'est 1 seul token |
| 2685 |
\unless\ifnum\selfcatcode=16 % ne rien toucher si une macro |
| 2686 |
\ifnum\numexpr#1<0 |
| 2687 |
\errmessage{Negative charcode in \string\setcharcode\detokenize{{#1}}, directive ignored}% |
| 2688 |
\else |
| 2689 |
\edef\selfcharcode{\the\numexpr#1\relax}% |
| 2690 |
\def\tktl__tmp{\expandafter\tktl_setcharcode_a\tktl_current_token}\expandafter\tktl__tmp\expandafter{\selfcharcode}% |
| 2691 |
\fi |
| 2692 |
\fi |
| 2693 |
\fi |
| 2694 |
} |
| 2695 |
\def\tktl_setcharcode_a(#1:#2)#3{% |
| 2696 |
\def\tktl_current_token{(#3:#2)}% |
| 2697 |
} |
| 2698 |
\def\tktl_setcatcode#1{% |
| 2699 |
\iftktl_is_one_token_% que si c'est 1 seul token |
| 2700 |
\edef\tktl_tmp{\the\numexpr#1\relax}% |
| 2701 |
\ifnum\selfcatcode=16 % si token courant est une macro |
| 2702 |
\ifnum\tktl_tmp=12 % et si catcode demandé=12 |
| 2703 |
\begingroup |
| 2704 |
\expandafter\expandafter\expandafter\tktl_encode_tokens\expandafter\expandafter\expandafter{\expandafter\string\selfcharcode}% |
| 2705 |
\expandafter |
| 2706 |
\endgroup\expandafter |
| 2707 |
\def\expandafter\tktl_current_token\expandafter{\the\tktl_enctoks_toks}% |
| 2708 |
\tktl_is_one_token_false |
| 2709 |
\fi |
| 2710 |
\else % si si token courant n'est pas une macro |
| 2711 |
\expandafter\tktl_ifinstr\expandafter{\expandafter,\tktl_tmp,}{,1,2,3,4,6,7,8,10,11,12,13,} |
| 2712 |
{% |
| 2713 |
\let\selfcatcode\tktl_tmp |
| 2714 |
\def\tktl__tmp{\expandafter\tktl_setcatcode_a\tktl_current_token}\expandafter\tktl__tmp\expandafter{\selfcatcode}% |
| 2715 |
} |
| 2716 |
{% |
| 2717 |
\errmessage{Illegal catcode in \string\setcatcode\detokenize{{#1}}, directive ignored}% |
| 2718 |
}% |
| 2719 |
\fi |
| 2720 |
\fi |
| 2721 |
} |
| 2722 |
\def\tktl_setcatcode_a(#1:#2)#3{% |
| 2723 |
\def\tktl_current_token{(#1:#3)}% |
| 2724 |
} |
| 2725 |
\tktl_restore_catcode |
| 2726 |
\endinput |
| 2727 |
|
| 2728 |
% Changelogs |
| 2729 |
0.2 17/04/2026 |
| 2730 |
- changement de syntaxe et de fonctionnalité pour \pegreplace |
| 2731 |
- ajout d'une clé "mode" pour la macro \printtoks afin de |
| 2732 |
pouvoir aussi diriger vers le fichier log les informations |
| 2733 |
sur les tokens |
| 2734 |
- mise en cohérence des consignes d'assignation. Plus aucune |
| 2735 |
vérification n'est faite |
| 2736 |
- améliorations du code - notamment compilation des motifs-, |
| 2737 |
nettoyage |
| 2738 |
|
| 2739 |
0.1 29/03/2026 |
| 2740 |
- version initiale |