\def\tcname {tokcycle} \def\tcver {1.42} % \def\tcdate {2021/08/25} % % Author : Steven B Segletes, Christian Tellechea (contributor) % Maintainer : Steven B Segletes % License : Released under the LaTeX Project Public License v1.3c % or later, see http://www.latex-project.org/lppl.txt % Files : 1) tokcycle.tex % 2) tokcycle.sty % 3) tokcycle-doc.tex % 4) tokcycle-doc.pdf % 5) tokcycle-examples.tex % 6) tokcycle-examples.pdf % 7) README %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MACRO FORM \long\def\tokcycle#1#2#3#4#5{\tokcycraw{#1}{#2}{#3}{#4}#5\endtokcycraw} % \expanded-ARGUMENT MACRO FORM \long\def\expandedtokcycle#1#2#3#4#5{\cytoks{\tokcycraw{#1}{#2}{#3}{#4}}% \expandafter\the\expandafter\cytoks\expanded{#5}\endtokcycraw} % ENVIRONMENT FORM \long\def\tokencycle#1#2#3#4{\begingroup\let\endtokencycle\endtokcycraw \aftertokcycle{\the\cytoks\tcendgroup}\tokcycraw{#1}{#2}{#3}{#4}} % XPRESS-INTERFACE MACRO FORM \long\def\tokcyclexpress#1{\tokcycrawxpress#1\endtokcycraw} % XPRESS-INTERFACE \expanded-ARGUMENT MACRO FORM \long\def\expandedtokcyclexpress#1{% \expandafter\tokcycrawxpress\expanded{#1}\endtokcycraw} % XPRESS-INTERFACE ENVIRONMENT FORM \def\tokencyclexpress{\begingroup\let\endtokencyclexpress\endtokcycraw \aftertokcycle{\the\cytoks\tcendgroup}\tokcycrawxpress} % INITIALIZATION & INTERNAL TOOLS \def\tcendgroup{\expandafter\endgroup\expandafter\tcenvscope\expandafter{\the\cytoks}} \def\tcenvscope{\cytoks}% CAN SET TO \global\cytoks TO OVERCOME SCOPE LIMITS \edef\restorecatcode{\catcode\number`\@=\number\catcode`\@\relax} \catcode`\@11 \newif\iftc@implicitgrp \newif\if@argnext \newtoks\tc@tok \newcount\tcdepth \def\tc@gobble#1{} \def\tc@deftok#1#2{\let#1= #2\empty} \tc@deftok\tc@sptoken{ } \expandafter\def\expandafter\tc@absorbSpace\space{} \def\tc@ifempty#1{\tc@testxifx{\expandafter\relax\detokenize{#1}\relax}} \def\tc@defx#1{\tc@earg{\def\expandafter#1}} \long\def\tc@earg#1{\expandafter#1\expandafter} \long\def\tc@xarg#1#2{\tc@earg#1{\expanded{#2}}} \long\def\tc@exfirst#1#2{#1} \long\def\tc@exsecond#1#2{#2} \long\def\tc@testxifx{\tc@earg\tctestifx} \long\def\test@ifmacro#1{\tctestifcatnx#1\relax} \def\tc@addtoks#1#2{\toks#1\expandafter{\the\toks#1 #2}} \def\add@tcdepth{\advance\tcdepth 1\relax} \def\sub@tcdepth{\advance\tcdepth-1\relax} \def\tc@resetifs{\activetokfalse\implicittokfalse\tc@implicitgrpfalse\catSIXfalse \activecharfalse\activetokunexpandablefalse\def\theactivespace{}} \long\def\count@stringtoks#1{\tc@earg\count@toks{\string#1}} \long\def\count@toks#1{\the\numexpr-1\count@@toks#1.\tc@endcnt} \long\def\count@@toks#1#2\tc@endcnt{+1\tc@ifempty{#2}{\relax}{\count@@toks#2\tc@endcnt}} \def\sv@hash{##} \def\def@popname{\expandafter\def\tc@popname} % EXTERNAL TOOLS \let\tcsptoken= \tc@sptoken \long\def\tctestifcon#1{#1\expandafter\tc@exfirst\else\expandafter\tc@exsecond\fi} \long\def\tctestifcatnx#1#2{\tctestifcon{\ifcat\noexpand#1\noexpand#2}} \long\def\tctestifx#1{\tctestifcon{\ifx#1}} \long\def\tctestifnum#1{\tctestifcon{\ifnum#1\relax}} \newif\ifstripgrouping \def\stripimplicitgroupingcase#1{\edef\@implicitgroupingcase{\the\numexpr1-#1}} \newif\ifcatSIX \newif\ifimplicittok \newif\ifactivetok \newif\ifactivechar \newif\ifactivetokunexpandable \newif\ifspacepopped \newtoks\cytoks \long\def\tokcycleenvironment#1#2#3#4#5{\expandafter\def\expandafter#1% \expandafter{\expandafter\let\csname end\expandafter\tc@gobble \string#1\endcsname\endtokcycraw\tokencycle{#2}{#3}{#4}{#5}}} \long\def\xtokcycleenvironment#1#2#3#4#5#6#7{\expandafter\def\expandafter #1\expandafter{\expandafter\let\csname end\expandafter\tc@gobble \string#1\endcsname\endtokcycraw\begingroup \long\def\tcafterenv####1{\tc@defx\tcendgroup{\tcendgroup####1}}% \aftertokcycle{#7\the\cytoks\tcendgroup}#6\tokcycraw{#2}{#3}{#4}{#5}}} \long\def\processtoks#1{\@tokcycle#1\endtokcycraw} \def\whennotprocessingparameter#1#2{\tctestifcon\if@argnext{\@argnextfalse\cytoks \expandafter{\the\cytoks###1}}{\tctestifcon\ifcatSIX{\@argnexttrue}{#2}}} \long\def\truncategroup#1{\tctestifx{\endtokcycraw#1}{#1}{\truncategroup}} \long\def\truncatecycle#1{\tctestifx{\endtokcycraw#1}{\tctestifnum{\tcdepth=0} {#1}{\truncatecycle}}{\tctestifx{\sub@tcdepth#1}{#1} {\tctestifx{\exit@grouped#1}{#1}{}}\truncatecycle}} \long\def\truncategroupiftokis#1#2{\tctestifx{\tc@next#1}{\truncategroup}{#2}} \long\def\truncatecycleiftokis#1#2{\tctestifx{\tc@next#1}{\truncatecycle}{#2}} % ESSENTIAL METHOD: STREAMING MACRO WITH TERMINATOR: % \tokcycraw{}{}{}{}\endtokcycraw \long\def\tokcycraw#1#2#3#4{\def\@chrT##1{#1}\long\def\@grpT##1{#2}% \long\def\@macT##1{#3}\def\@spcT##1{#4}\tokcycrawxpress} % ENTRY POINT FOR XPRESS METHOD WITHOUT EXPLICIT ARGUMENTS \def\tokcycrawxpress{\cytoks{}\tcdepth=0\relax\@tokcycle} % CODE TO EXECUTE AT COMPLETION \long\def\aftertokcycle#1{\def\@aftertokcycle{#1}} \def\endtokcycraw{\tctestifnum{\tcdepth=0}{\@aftertokcycle}{}} % LOOP ENTRY POINT \def\@tokcycle{\tc@resetifs\futurelet\tc@next\detect@CANTabsorb} \def\detect@CANTabsorb{\tctestifx{\tc@next\tc@sptoken}{\stringify\@@@@@@spcT}% {\tctestifx{\tc@next\bgroup}{\stringify\@@@@grpT}{\can@absorb}}} % NON cat1,10 TOKENS \long\def\can@absorb#1{\tc@tok{#1}\trapcatSIX{#1}\expandafter\can@absorb@ \the\tc@tok} \long\def\can@absorb@#1{\tctestifnum{\count@stringtoks{#1}>1}% {\tctestifx{\endtokcycraw#1}{#1}{\backslashcmds#1\@tokcycle}}% {\trapactives#1\tc@trapescape#1{\tc@escapecytoks\empty}{\can@absorb@@#1}}} \long\def\can@absorb@@#1{\let\@tmp=#1\test@ifmacro\@tmp{\implicittokfalse \@macT#1}{\trapimplicitegrp#1\implicitgrpfork#1}\@tokcycle} %CONVERT NEXT (SPACE OR BEGIN-GROUP) TOKEN TO STRING \def\stringify#1{\expandafter#1\string}% #1 WILL BE \@@@@@@spcT or \@@@@grpT %SPACE DECODE \def\@@@@@@spcT{\futurelet\tc@str\@@@@@spcT} \def\@@@@@spcT{% \tctestifx{\tc@str\tc@sptoken}% {\def\@tmp{\@@spcT{ }}\expandafter\@tmp\tc@absorbSpace}% EXPLICIT SPACE {\implicittoktrue\tctestifcon{\if\expandafter\@firstoftwo\string\\\tc@str}% {\expandafter\@@@spcT\tc@gobble}% IMPLICIT MACRO SPACE {\activetoktrue\@@@@spcT}}}% IMPLICIT ACTIVE SPACE \def\@@@@spcT#1{\def\theactivespace{#1}\trapactivechar{#1}\ifactivechar\edef\tc@spc {\scantokens{#1\noexpand}}\else\def\tc@spc{\tcsptoken}\fi\expandafter\@@spcT \expandafter{\tc@spc}} \def\@@@spcT{\csmk{\expandafter\@@spcT\thecs}} \def\@@spcT#1{\@spcT{#1}\@tokcycle} % GROUP DECODE \def\@@@@grpT{\futurelet\tc@str\@@@grpT} \def\@@@grpT#1{\tctestifnum{\number\catcode`#1=1}% {\expandafter\@@grpT\expandafter{\iffalse}\fi}% { {\implicittoktrue\tc@implicitgrptrue% \tctestifnum{`#1=92}% WORKS EVEN IF CAT-0 HAS CHANGED {\csmk{\expandafter\backslashcmds\thecs\@tokcycle}}% \bgroup {\begingroup\catcode`#1=\active \xdef\@tmp{\scantokens{#1\noexpand}}\endgroup \expandafter\implicitgrpfork\@tmp\@tokcycle}% ACTIVE CHAR \bgroup }} \long\def\@@grpT#1{\add@tcdepth\tctestifcon{\ifstripgrouping}{% \@grpT{#1}}{\groupedcytoks{\@grpT{#1}}}\sub@tcdepth\@tokcycle} % \ COMMANDS (MACROS AND IMPLICITS) \long\def\backslashcmds#1{% \test@ifmacro#1{\tctestifcon\ifcatSIX{\implicittoktrue\@chrT#1}{\@macT#1}}% {\implicittoktrue\trapimplicitegrp#1\implicitgrpfork#1}} % FORK BASED ON IMPLICIT GROUP TREATMENT \def\implicitgrpfork#1{\tctestifcon{\iftc@implicitgrp}{\ifcase \@implicitgroupingcase\or\addcytoks{#1}\or\@chrT{#1}\fi}{\@chrT#1}} % SET UP ESCAPE MECHANISM \def\settcEscapechar#1{\let\@tcEscapeptr#1% \def\tc@escapecytoks##1#1{\addcytoks[1]{##1}\@tokcycle}} \def\tc@trapescape#1{\tctestifx{\@tcEscapeptr#1}} % TRAP CAT-6 \long\def\trapcatSIX#1{\tctestifcatnx#1\relax{}{\trapcatSIXb#1}} \def\trapcatSIXb#1{\expandafter\tctestifcatnx\sv@hash#1{\catSIXtrue\trapcatSIXc#1}{}} \def\trapcatSIXc#1{\tctestifnum{\count@stringtoks{#1}>1}{\tc@defx\six@str{\string#1}% \global\let\implicitsixtok\six@str\tc@tok{\implicitsixtok}}% {\tc@tok\expandafter{\string#1}\tctestifnum{\number\catcode`#1=6}% {}{\activetoktrue\implicittoktrue}}} % DIRECTIVES FOR HANDLING GROUPED OUTPUT; DEFINE tokcycle GROUPING CHARS \long\def\groupedcytoks#1{\begingroup\cytoks{}#1\exit@grouped} \def\defineexit@grouped#1{\def\exit@grouped{\expandafter\endgroup\expandafter \addcytoks\expandafter{\expandafter#1}}} \def\settcGrouping#1{\def\@tmp##1{#1}\tc@defx\@@tmp{\@tmp{\the\cytoks}}% \tc@earg\defineexit@grouped{\@@tmp}} % FAUX TOKENIZATION OF COMMAND NAME (WHEN \ AND COMMAND-NAME TOKS ARE NOW cat12) \def\csmk#1{\def\csaftermk{#1}\toks0{}\@csmkA} \def\@csmkA{\futurelet\@tmp\@csmkB} \def\@csmkB{\tctestifx{\@tmp\tc@sptoken}% {\toks0{ }\expandafter\@csmkF\tc@absorbSpace}{\@csmkCA}} \def\@csmkCA#1{\tc@addtoks0{#1}\tctestifnum{\number\catcode`#1=11}% {\futurelet\@tmp\@csmkD}{\@csmkF}} \def\@csmkC#1{\tctestifnum{\number\catcode`#1=11} {\tc@addtoks0{#1}\futurelet\@tmp\@csmkD}{\@csmkE#1}} \def\@csmkD{\tctestifcatnx 0\@tmp\@csmkC\@csmkE} \def\@csmkE{\tctestifx{\@tmp\tc@sptoken}% {\expandafter\@csmkF\tc@absorbSpace}{\@csmkF}} \def\@csmkF{\tc@defx\thecs{\csname\the\toks0\endcsname}\csaftermk} % TRAP IMPLICIT END GROUP TOK (e.g., \egroup); SET \iftc@implicitgrp \def\trapimplicitegrp#1{\tctestifx{#1\egroup}{% \implicittoktrue\tc@implicitgrptrue}{}} % TRAP ACTIVE TOK \def\trapactives#1{\trapactivechar{#1}\trapactivetok{#1}} \def\trapactivechar#1{\tctestifnum{\number\catcode`#1=13}{\activechartrue}{}} \def\trapactivetok#1{\tctestifcatnx~#1{\activetoktrue}{\trapactivetokunexpandable#1}} %% WILL ALSO TRAP ACTIVE \let TO PRIMITIVES AS IMPLICIT; UNDO LATER IN \can@absorb@@ \def\trapactivetokunexpandable#1{\tctestifcon{\expandafter\if \detokenize{#1}#1}{}{\activetoktrue\activetokunexpandabletrue\implicittoktrue}} % FEATURES TO LOOK-AHEAD INTO THE INPUT STREAM (INTRODUCED v1.4) \long\def\tcpeek#1#2\@tokcycle{\def\tc@tmp{\ifx#1\endtokcycraw \let#1=\empty\fi#2\@tokcycle}\futurelet#1\tc@tmp}%___________________________PEEK_ \def\tcpopliteral#1{\tcpopwhitespace#1\tcpeek\@tmp\ifx\@tmp\bgroup \tcpop\@tmp\def\tc@tmp{\tcappto#1from}\expandafter\tc@tmp\expandafter {\expandafter{\@tmp}}\else\tcpopappto#1\fi} \def\tcpop{\long\def\tc@@@pop##1{\tctestifx{\endtokcycraw##1}{\def@popname{}% \tc@tmp\endtokcycraw}{\def@popname{##1}\tc@tmp}}\tc@pop} \long\def\tcpopuntil#1{\long\def\tc@@@pop##1#1{\def@popname{##1#1}\tc@tmp}\tc@pop} \long\def\tc@pop#1#2\@tokcycle{\def\tc@popname{#1}\def\tc@tmp {#2\@tokcycle}\futurelet\tc@futuretok\tc@@pop} \def\tc@@pop{\tc@trapescape\tc@futuretok{\def@popname{}\tc@tmp}{\tctestifx{% \endtokcycraw\tc@futuretok}{\def@popname{}\tc@tmp}{\tctestifx{\tc@sptoken% \tc@futuretok}{\spacepoppedtrue\tc@@@pop}{\spacepoppedfalse\tc@@@pop}}}}%_____POP_ \def\tcappto#1from#2{% \expandafter\tc@defx\expandafter#1\expandafter{\expandafter#1#2}} \def\tcpopliteralappto#1{\tcpopliteral\@@tmp\tcappto#1from\@@tmp} \def\tcpopappto#1{\tcpop\@tmp\tcappto#1from\@tmp}%___________________________APPEND_ \long\def\tcpopwhitespace#1#2\@tokcycle{\def\tc@popname{#1}\def@popname{}% \def\tc@tmp{#2\@tokcycle}\futurelet\tc@futuretok\tc@popspc} \def\tc@popspc{\tctestifx{\tc@sptoken\tc@futuretok}{\discern@space}{\tc@tmp}} \def\discern@space{\begingroup\def\@@spcT##1{\tctestifcon\ifimplicittok{\gdef \tc@nxt{\tc@tmp##1}}{\gdef\tc@nxt{\def@popname{##1}\tc@tmp}}\endgroup \tc@nxt}\stringify\@@@@@@spcT}%_____________________________________________SPACE_ \long\def\@tcpush#1#2\@tokcycle{\def\tc@tmp{#2\@tokcycle}\expandafter\tc@tmp#1} \long\def\@tcpushgroup#1#2\@tokcycle{\def\tc@tmp{#2\@tokcycle}\expandafter\tc@tmp \expandafter{#1}} % ...BORROW \addcytoks OPTIONAL ARGUMENT EXPANSION FEATURE FOR \tcpush[group] \def\tcpush{\bgroup\long\def\tc@addtotoks##1{\egroup \@tcpush{##1}}\futurelet\nxttok\addcytoks@A} \def\tcpushgroup{\bgroup\long\def\tc@addtotoks##1{\egroup \@tcpushgroup{##1}}\futurelet\nxttok\addcytoks@A}%___________________________PUSH_ % EXPRESS-INTERFACE - ALLOWS TO EXTERNALLY DEFINE DIRECTIVES \def\Characterdirective{\def\@chrT##1} \def\Groupdirective{\long\def\@grpT##1} \def\Macrodirective{\long\def\@macT##1} \def\Spacedirective{\def\@spcT##1} % EXPRESS-INTERFACE - DEFAULT DIRECTIVES \def\resetCharacterdirective{\Characterdirective{\addcytoks{##1}}} \def\resetGroupdirective{\Groupdirective{\processtoks{##1}}} \def\resetMacrodirective{\Macrodirective{\addcytoks{##1}}} \def\resetSpacedirective{\Spacedirective{\addcytoks{##1}}} \def\resettokcycle{\resetCharacterdirective\resetGroupdirective \resetMacrodirective\resetSpacedirective\aftertokcycle{}% \stripgroupingfalse\stripimplicitgroupingcase{0}} % SUPPORT MACROS FOR TOKENIZED OUTPUT: \addcytoks[]{} % (CONTRIBUTED BY CHRISTIAN TELLECHEA) \def\addcytoks{\futurelet\nxttok\addcytoks@A} \long\def\tc@addtotoks#1{\cytoks\expandafter{\the\cytoks#1}} \def\addcytoks@A{\tctestifx{[\nxttok}\addcytoks@B\tc@addtotoks} \long\def\addcytoks@B[#1]#2{\tc@ifempty{#1}\tc@addtotoks {\tctestifx{x#1}{\tc@xarg\tc@addtotoks}{\addcytoks@C{#1}}}{#2}} \def\addcytoks@C#1{\tctestifnum{#1>0}{\tc@earg\addcytoks@C {\the\numexpr#1-1\expandafter}\expandafter}\tc@addtotoks} % SET INITIAL PARAMETERS \settcGrouping{{#1}}% E.G. <<#1>> IF cat-1,2 SET TO < AND > \settcEscapechar{|}% BYPASS TOKCYCLE PROCESSING BETWEEN |...| \resettokcycle% WHICH ALSO CONTAINS THE FOLLOWING 3 RESETS: % \stripimplicitgroupingcase{0}% DEFAULT RETAIN UNALTERED \b/e-groups % \stripgroupingfalse% DEFAULT RETAIN UNALTERED {} GROUPING % \aftertokcycle{}% NO DEFAULT CODE EXECUTED AFTER EACH TOKCYCLE INVOCATION \restorecatcode \endinput EDIT HISTORY v1.0 2019/08/21 - Initial release v1.1 2019/09/27 - Introduced \ifactivechar, \ifactivetokunexpandable - Tightened up consistent definition of implicit (to exclude primitives) - Rewrote active token trapping logic, to differentiate between active token vs. active character code, in the event that an earlier tokenized token no longer shares the current characteristics of the character code - Added ability to handle active-implicit grouping tokens - Added ability to handle active-implicit cat-6 tokens v1.11 2020/02/04 - Fixed bug in \can@absorb@@ macro, which prevented the proper absorption/ handling of the = token. v1.12 2020/02/11 - Documentation correction: \tokcycleenvironment, not \tokencycleenvironment - Documentation correction: misspelling in tokcycle-examples.tex - Redefined \tc@defx and \tc@earg to omit #2 as part of definition - Corrected \trapcatSIXb definition to account for revised \tc@earg definition. v1.2 2020/10/01 - Added/fixed capability to handle active-implicit spaces. While the #1 passed to the \Spacedirective, in such a case, is an implicit space \tc@sptoken, the name of the active character from whence it originated in the input stream is stored as an explicit cat-12 token in the definition \theactivespace. The flags \implicittok and \activetok are both set true, and the \activechar flag is checked, as well. v1.3 2021/03/10 - Introduced \xtokcycleenvironment, similar to \tokcycleenvironment, but allows two additional arguments, defining the setup and trailing code that will be run prior to the invocation and following conclusion of the token cycle. - Introduced \truncategroup, a directive to discard remaining tokens in the current token-cycle group and close the group. - Introduced \truncatecycle, a directive to discard remaining tokens in the token-cycle input stream, but closing any open groups. - Introduced \truncategroupiftokis{}{} to conditionally issue a \truncategroup if the current token under consideration matches the 1st argument. - Introduced \truncatecycleiftokis{}{} to conditionally issue a \truncatecycle if the current token under consideration matches the 1st argument. - Introduced \tcendgroup as a form of \endgroup that saves contents of \cytoks upon group exit. Gives clarity to definitions of \tokencycle and \tokencyclexpress environments. Available for general used. - Fixed bug. Made \@@grpT \long, in the event that \par occurs inside a group. - Fixed bug. Added \relax to end of \add@tcdepth definition, the absence of which had prevented timely update of the \tcdepth count. - Documentation correction: improved explanation of implicit-cat-6 token limitations. Likewise, added warning regarding active-implicit spaces. - Documentation prepared with lmodern font, rather than default cm, for reasons of better PDF hinting. - Introduced \exit@grouped to the \groupedcytoks definition, for clarity and ease of \expandafter. - Reworked logic so that \tcdepth is associated with nested calls to the Group directive, rather than with each invocation of \processtoks. This primarily required redefinition of \@@grpT and its components. - Changed name of \tc@depth to \tcdepth, as it may be a useful parameter for users to check the token-cycle nesting depth. Therefore, also renamed \subtc@depth to \sub@tcdepth, \addtc@depth to \add@tcdepth. - Excised \sub@tcdepth from \endtokcycraw as part of new logic making \tcdepth associated with the Group directive (also, outer token cycle operates with depth = 0 rather than 1). - Renamed \@defgroupedcytoks to \defineexit@grouped, to better match function. v1.4 2021/05/26 - Concerning the \@@@@spcT macro: previously, active-implicit spaces were passed to the Spacedirective as \tc@sptoken, with the \string of the active char passed in \theactivespace. Now, the active-implicit space token itself is passed to the Spacedirective instead of \tc@sptoken, but only *IF* the charcode of that character is currently active; otherwise, a generic implicit space, \tcsptoken is passed. - Introduced a set of "look ahead" macros: \tcpeek, \tcpop, \tcpopliteral, \tcpopappto, \tcpopliteralappto, \tcappto#1from#2, \tcpopuntil, \tcpopwhitespace, \tcpush, and \tcpushgroup. - Bug fixed in \tc@addtoks definition, in the event that #2 was a number. v1.41 2021/06/25 - Bug fix in \tcpopliteralappto (\@tmp interfered with itself). Altered, substituting \@@tmp. v1.42 2021/08/25 - Bug fix in \tc@escapecytoks (if escaped text was a single brace group, the braces were lost)