Last active
December 30, 2015 20:29
-
-
Save k16shikano/7880997 to your computer and use it in GitHub Desktop.
replace tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%% replace all tokens; | |
%%% space -> +, leftbrace -> (, rightbrace -> ), others -> *, | |
%%% provided that the argument doesn' have ! and \end. | |
\def\length#1{\spaces{#1!}\notspaces{#1!}} | |
\def\notspaces#1{\beforegroup#1\iffalse{}\fi} | |
\def\beforegroup#1#{\recur#1!\futurelength} | |
\def\recur#1{% | |
\ifx!#1\else*\expandafter\recur\fi} | |
\def\futurelength#1#2!{% | |
\ifx!#1\notspaces{#1!}\else(\length{#1})\notspaces{#2!}\fi} | |
\def\spaces#1{% | |
\ifx!#1\ifEnd% | |
\else% | |
\expandafter\expandafter\expandafter\crash% | |
\expandafter\ifx\space#1% | |
\end+\expandafter\restSpace{#1}% | |
\else% | |
\end\expandafter\rest\noexpand#1% | |
\fi% | |
\fi} | |
\def\rest#1#2\fi{\fi\expandafter\spaces\expandafter{\noexpand#2}} | |
\def\restSpace#1{\expandafter\expandafter\expandafter\spaces% | |
\expandafter\expandafter\expandafter{\expandafter\chopSpace#1}} | |
\def\ifEnd#1\fi\fi{\fi} | |
\def\crash#1\end{} | |
\def\chop#1{} | |
\def\chopSpace#1 {} | |
%\edef\result{\length{a bcd }} | |
%\edef\result{\length{ \a {b\cd ef{g} }\h}} | |
%\edef\result{\length{{ }{ {}} {}}} | |
%\edef\result{\length{ \a {b\cd ef{g} }\h}} | |
%\edef\result{\length{{ a} {bc ef{g} }h {} }} | |
%\edef\result{\length{{>>+<+<\-}}} | |
%\edef\result{\length{{s}}} | |
%\edef\result{\length{\expandafter\expandafter\expandafter}} | |
%\edef\result{\expandafter\length\expandafter{\csname ?TeX\endcsname}} | |
\edef\next{\noexpand\length{a\space\space\space b}}\edef\result{\next} | |
\show\result | |
\end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment