在纯 e-TeX 中实现 \luaescapestring

在纯 e-TeX 中实现 \luaescapestring

我正在尝试\luaescapestring用纯 e-TeX 实现。我的解决方案需要完全可扩展,这样才能\immediate\write\luascript{ local file = "\luaescapestring{\something}" }工作。以下代码几乎完成了,但它会占用空格字符:

\def\luaescapestring#1{%
  \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter
  \expandafter\next\expandafter\expandafter\expandafter\gobbleone
  \expandafter\detokenize\expandafter{\csname#1\endcsname}\relax}%
\def\gobbleone#1{}%
\begingroup
  \catcode`\"=12%
  \catcode`\|=0%
  \catcode`\\=12%
  |gdef|next#1{%
    |if#1|relax|else
      |if#1"%
        \"|expandafter|expandafter|expandafter
          |next
      |else
        |if#1\%
          \\|expandafter|expandafter|expandafter|expandafter
            |expandafter|expandafter|expandafter|next
        |else
          #1|expandafter|expandafter|expandafter|expandafter
            |expandafter|expandafter|expandafter|next
        |fi
      |fi
    |fi}%
|endgroup

与包中使用的代码类似的代码letterspacing.tex考虑到了空间,但\futurelet破坏了派对。

答案1

我用了OPmac 技巧 0153,所以我只需要定义\eapply宏:

\def\tmp#1{}\edef\nb{\expandafter\tmp\string\\}  % \nb is normal backslash

\def\luaescapestring#1{\etoksA #1{\end}}
\def\etoksA#1#{\etokssB#1 {\end} }
\def\etokssB#1 #2 {\etoksC#1\end 
    \ifx\end#2\empty\expandafter\etoksD\else\space\fihere{\etokssB#2 }\fi}
\def\etoksC#1{\ifx\end#1\else\eapply{#1}\expandafter\etoksC\fi}
\def\etoksD#1{\ifx\end#1\empty\else\fihere{{\luaescapestring{#1}}\etoksA}\fi}
\def\fihere#1\fi{\fi#1}

\def\eapply#1{\ifx"#1\nb"\else\bbstring#1\fi}
\def\bbstring#1{\expandafter\bbstringA\string#1}
\def\bbstringA#1{\expandafter\ifx\nb#1\nb\fi#1}

%test:
\message{...\luaescapestring{"a"\ hah ah \a}}

\end

\message命令扩展其参数并...\"a\"\\ hah ah \\a在我们的示例中打印。

请注意,您不需要任何特殊的 eTeX 原语。一切都由经典 TeX 完成。

相关内容