Skip to content

Instantly share code, notes, and snippets.

@doraTeX
Created May 31, 2023 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doraTeX/2e4720d745af12b632f5aa43910585b1 to your computer and use it in GitHub Desktop.
Save doraTeX/2e4720d745af12b632f5aa43910585b1 to your computer and use it in GitHub Desktop.
\futurelet  芸人クイズを解いてみる
% オリジナルの quiz.tex が \Parse{#1} から \Parsed を生成するのと同様に,
% \doraParse{#1} から \doraParsed を生成する命令を,\futurelet を使わずに定義する
\catcode`\@=11 %----------------------------------
\newcount\dora@parse@depth
\def\safescantokens#1#2{\scantokens{\def#1{#2}\ignorespaces}}
\catcode`[=1
\catcode`]=2
\catcode`{=12
\catcode`}=12
\def\dora@parse@open[{]
\def\dora@parse@close[}]
\catcode`[=12
\catcode`]=12
\catcode`{=1
\catcode`}=2
\def\doraParse#1{%
\let\doraParsed\empty
\dora@parse@depth=\z@
\catcode` =3
\catcode`[=1
\catcode`]=2
\catcode`{=12
\catcode`}=12
\edef\dora@parse@target{\scantokens{#1\noexpand}}%
\catcode`[=12
\catcode`]=12
\catcode`{=1
\catcode`}=2
\catcode` =10
\expandafter\dora@parse@a\dora@parse@target\relax
\expandafter\safescantokens\expandafter\doraParsed\expandafter{\doraParsed}%
}
\def\dora@parse@a#1{%
\if#1\relax
\let\dora@next\relax
\else
\ifnum\dora@parse@depth=0\relax
\if#1\dora@parse@open
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\doraParsed\expandafter\expandafter\expandafter{\expandafter\doraParsed\expandafter\G\dora@parse@open}%
\advance\dora@parse@depth\@ne
\else
\expandafter\def\expandafter\doraParsed\expandafter{\doraParsed\S{#1}}%
\fi
\else
\if#1\dora@parse@open
\advance\dora@parse@depth\@ne
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\doraParsed\expandafter\expandafter\expandafter{\expandafter\doraParsed\dora@parse@open}%
\else\if#1\dora@parse@close
\advance\dora@parse@depth\m@ne
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\doraParsed\expandafter\expandafter\expandafter{\expandafter\doraParsed\dora@parse@close}%
\else
\expandafter\def\expandafter\doraParsed\expandafter{\doraParsed#1}%
\fi\fi
\fi
\let\dora@next\dora@parse@a
\fi
\dora@next
}
\catcode`\@=12 %----------------------------------
% quiz.tex (original)
% [source]
% * https://gist.github.com/zr-tex8r/05d15eef0c46746a826217bf070e8df5
% * https://twitter.com/zr_tex8r/status/1663710981279879168
\catcode`\@=11 %----------------------------------
\def\Parse#1{\let\Parsed\empty\my@parse@a#1\relax}
\def\my@parse@a{\futurelet\my@tok\my@parse@b}
\def\my@parse@b{\let\my@next\my@parse@c
\if\my@tok\relax \let\my@next\relax
\else\if\my@tok\space \let\my@next\my@parse@d
\else\if\my@tok\bgroup \let\my@next\my@parse@e
\fi\fi\fi \my@next}
\def\my@parse@c{\my@parse@f\S}
\expandafter\def\expandafter\my@parse@d\space{\my@parse@c{ }}
\def\my@parse@e{\my@parse@f\G}
\def\my@parse@f#1#2{%
\expandafter\def\expandafter\Parsed\expandafter{%
\Parsed#1{#2}}%
\my@parse@a}
\catcode`\@=12 %----------------------------------
% オリジナルの quiz.tex の \Parse が生成する \Parsed と,
% quiz-dora.tex の \doraParse が生成する \doraParsed の内容が一致するかどうかをテストする
\input{quiz}
\input{quiz-dora}
\def\test#1{%
\typeout{Test:#1=>}%
\Parse{#1}%
\typeout{\meaning\Parsed}%
\doraParse{#1}%
\typeout{\meaning\doraParsed}%
\ifx\Parsed\doraParsed
\typeout{OK}%
\else
\typeout{NG}%
\fi
}
\test{ 12{34} 5 {{6}7} }
\test{ 1 {2} {34} 5 {{{6}}7} }
\test{ 1 {2}{34}5{{{6}}7} {8}}
\test{1{{2}}{34}5{{{6}}78} {9} {}}
\stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment