| 1 |
% seplist.tex |
| 2 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 |
% Petr Olsak 2014 |
| 4 |
|
| 5 |
% This macro enables to declare more than one separator for parameter |
| 6 |
% scanning of your macro. Usage: |
| 7 |
|
| 8 |
% \long\def\yourmacro#1{usage of #1parameter} |
| 9 |
% \seplist{list of separators}\yourmacro parameter-text separator |
| 10 |
% example: |
| 11 |
% \seplist{{sepA}{SepB}{SEPC}}\yourmacro text separated by sepA |
| 12 |
% \seplist{{sepA}{SepB}{SEPC}}\yourmacro text separated by SepB |
| 13 |
% \seplist{{sepA}{SepB}{SEPC}}\yourmacro text separated by SEPC |
| 14 |
% All lines of this example are expanded to "usage of text separated by parameter". |
| 15 |
|
| 16 |
% The actually used separator is stored globally to the \sepused macro. |
| 17 |
% The macro programmer can use this. |
| 18 |
|
| 19 |
% The input stream is read to the first instance of any of the listed |
| 20 |
% separator, no more. The separator list includes separators in braces. |
| 21 |
% If there are only one-token separators, braces can be omited. Example |
| 22 |
|
| 23 |
% \seplist{0123456789}\mymacro text to the first decimal digit 7 |
| 24 |
|
| 25 |
% The parameter have to be balanced. Thus, the separator hidden in braces |
| 26 |
% is ignored. This behavior is similar like in normal separated parameters. |
| 27 |
|
| 28 |
% The \par or spaces or arbitrary control sequences can be a part of |
| 29 |
% separators. The # (of catcode 6) cannot be a part of the separator but |
| 30 |
% it can be included in the parameter text. |
| 31 |
|
| 32 |
% See implementation details at the end of this file |
| 33 |
|
| 34 |
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}} |
| 35 |
\newtoks\seplistT |
| 36 |
|
| 37 |
\long\def\seplistD#1{% |
| 38 |
\seplistS##2\seplistE{\def\tmpa{##1}\def\tmpb{##2}\seplistE}% |
| 39 |
\def\tmpb{\tmpa #1}\expandafter\tmpb \tmp\seplistD\seplistE |
| 40 |
} |
| 41 |
\long\def\seplistE#1{% |
| 42 |
\ifx\tmpa\empty |
| 43 |
\seplistS\seplistD{\def\tmpb{##1}}\expandafter\tmpa\tmpb |
| 44 |
\ifx\tmpb\empty \seplistQ{#1}% |
| 45 |
\else \expandafter\addto\expandafter\seplistLx |
| 46 |
\expandafter {\expandafter\seplistD\expandafter{\tmpb}{#1}}% |
| 47 |
\fi\fi |
| 48 |
} |
| 49 |
\def\seplistS{\long\expandafter\def\expandafter\tmpa\expandafter##\expandafter1\tmp} |
| 50 |
\long\def\seplistQ#1#2\seplistA{\fi\fi\gdef\sepused{#1}\seplistZ} |
| 51 |
|
| 52 |
\long\def\seplist#1#2{\begingroup |
| 53 |
\toks0={#2}\let\bgroup=\relax \let\egroup=\relax |
| 54 |
\def\seplistL{}\def\seplistLx{}\seplistI#1{}\gdef\sepused{}% |
| 55 |
\ifx\seplistL\empty \expandafter\endgroup \the\toks0\else |
| 56 |
\seplistT={}\expandafter\seplistA\fi |
| 57 |
} |
| 58 |
\def\seplistA{\futurelet\tmp\seplistB} |
| 59 |
\def\seplistB{\let\next=\seplistP |
| 60 |
\expandafter\ifx\space\tmp \let\next=\seplistC \let\nexxt=\seplistM \fi |
| 61 |
\ifx##\tmp \let\next=\seplistC \let\nexxt=\seplistH \fi |
| 62 |
\ifx{\tmp \let\next=\seplistG \fi |
| 63 |
\ifx}\tmp \let\next=\seplistC \let\nexxt=\seplistF \fi |
| 64 |
\next |
| 65 |
} |
| 66 |
\def\seplistC{\afterassignment\nexxt \let\next= } |
| 67 |
\long\def\seplistP#1{\seplistX#1\def\tmp{#1}\seplistN} |
| 68 |
\def\seplistM{\seplistX{ }\def\tmp{ }\seplistN} |
| 69 |
\def\seplistH{\seplistX{##}\def\seplistLx{}\seplistA} |
| 70 |
\def\seplistN{\edef\seplistLx{\expandafter}\seplistLx \seplistL \seplistA} |
| 71 |
\long\def\seplistG#1{\def\seplistLx{}\seplistX{{#1}}\seplistA} |
| 72 |
\def\seplistF{\seplistT\expandafter{\expandafter{\the\seplistT}}\seplistZ} |
| 73 |
\long\def\seplistX#1{\seplistT\expandafter{\the\seplistT#1}} |
| 74 |
\def\seplistZ{\let\tmp=\sepused |
| 75 |
\expandafter\seplistS\expandafter{\the\toks0{##1}}% |
| 76 |
\expandafter\expandafter\expandafter\endgroup\expandafter\tmpa\the\seplistT |
| 77 |
} |
| 78 |
\long\def\seplistI#1{\ifx\seplistI#1\seplistI\else |
| 79 |
\addto\seplistL{\seplistD{#1}{#1}}\expandafter\seplistI \fi |
| 80 |
} |
| 81 |
|
| 82 |
\endinput |
| 83 |
|
| 84 |
\def\m#1{\toks0={#1}\message{param: "\the\toks0", separator: "\sepused"}} |
| 85 |
\seplist{ab{c a}{cb}0123456789}\m uv#wcd{6cb7}uffc a |
| 86 |
|
| 87 |
% You can define \sepdef \macro #1[{sepA}{sepB}{sepC}]{usage of the parameter #1...} |
| 88 |
|
| 89 |
\def\sepdef #1#2[#3]{\def#1{\seplist{#3}{\csname:\string#1\endcsname}}% |
| 90 |
\long\expandafter\def\csname:\string#1\endcsname ##1} |
| 91 |
|
| 92 |
\sepdef\test #1[uvw]#2{\message{1="#1", 2="#2"}} |
| 93 |
|
| 94 |
\test ahawx \test bhavy |
| 95 |
|
| 96 |
\end |
| 97 |
|
| 98 |
Comments to the implementation. |
| 99 |
|
| 100 |
We read the parameter token-per-token similarly as in openbrace.tex or |
| 101 |
eparam.tex and store these tokens in \seplistT token list. The internal |
| 102 |
macro \seplistL includes the list of separators in the form: |
| 103 |
|
| 104 |
\seplistD{sepA}{sepA}\seplistD{sepB}{sepB}... |
| 105 |
|
| 106 |
We store the already read token to \tmp and run \seplistL. More exactly: at |
| 107 |
the start, the temporary \seplistLx is emty. For each read token, we expand |
| 108 |
\seplistLx and \seplistL to the input stream and before executing it we |
| 109 |
reset \def\seplistLx{}. Now, the input stream is executed, i.e. the |
| 110 |
\seplistD macro is processed for each separator. The task of |
| 111 |
\seplistD{sepA}{sepA} is the following: to test if the \tmp is equal to the |
| 112 |
first token of its first parameter ("s" in this example). If it is true, |
| 113 |
then \seplistD (using \seplistE) adds the text \seplistD{epA}{sepA} (the |
| 114 |
first token from the first parameter is removed) to the temporary list |
| 115 |
\seplistLx which will be executed for the next token. If \tmp isn't equal to |
| 116 |
the first token of the first parameter then \seplistD does nothing. |
| 117 |
|
| 118 |
For example, the next read token \tmp is "e". Then \seplistD{epA}{sepA} |
| 119 |
saves the \seplistD{pA}{sepA} to \seplistLx, because the first letter |
| 120 |
\tmp="e". If the next token \tmp is "p", then \seplistD{A}{sepA} is stored |
| 121 |
to the \seplistLx. And finally, if the next \tmp is "A", then |
| 122 |
\seplistD{A}{sepA} does not store \seplistD{}{sepA}, but it decides that |
| 123 |
separator is found because the first parameter is empty. It defines \sepused |
| 124 |
to its second parameter "sepA" and it does the end of this game by \seplistQ |
| 125 |
plus \seplistZ. If the last token \tmp isn't "A" then the \seplistD{A}{sepA} |
| 126 |
does nothing and the chain is broken because the \seplistLx is set to empty |
| 127 |
in each step. The new chain can be built because \seplistD{sepA}{sepA} is |
| 128 |
still included in \seplistL which isn't changed during calculation. |
| 129 |
|
| 130 |
Macro-programing in TeX is beautiful but it is different than the classical |
| 131 |
technique used by "normal" programming. |