Print

Print


On 12/02/2009, at 5:00 PM, Will Robertson wrote:

> Having some more troubles, this time with \token_if_expandable.  
> Unfortunately, the only token I can find that trips this "true" is  
> \relax or \scan_stop:, which isn't exactly what I thought the  
> intended behaviour was supposed to be.

After searching through c.t.t. for a while, I found an insane thread  
talking about this sort of thing:
   <http://groups.google.com/group/comp.text.tex/msg/2b571c53da89f86f>

Dan Luecking's comment is that
   \expandafter\ifx\noexpand#1#1
"seems a feasible test for expandability of a token".

I'm in a rush, and might be barking up the wrong tree here, but here's  
a re-implementation of \token_if_expandable that *seems* to behave in  
a more coherent manner.

The big kicker is that it only returns true for tokens that will  
expand into something else -- regardless of whether they are able to  
be fully expanded or not. And really, who could expect anything more?

Will


\def:Npn \token_if_expandable_p:N #1{
   \cs_if_exist:NTF #1 {
     \exp_after:NN \if_token_eq:NN \exp_not:N #1 #1
       \c_false \else: \c_true \fi:
   } {\c_false}
}
\def_test_function:npn {token_if_expandable:N} #1{
\if:w\token_if_expandable_p:N#1}
\token_if_expandable:NTF \FOOOO {T}{F}
\token_if_expandable:NTF a {T}{F}
\token_if_expandable:NTF \foo {T}{F}
\token_if_expandable:NTF \the {T}{F}
\par
\def\foo{bar}
\expandafter\ifx\noexpand\foo\relax T\else F\fi
\expandafter\ifx\noexpand\the\relax T\else F\fi
\expandafter\ifx\noexpand\FOOO\relax T\else F\fi
\catcode`\z=13 \def z{\write18{ls}}
\expandafter\ifx\noexpand z\relax T\else F\fi
\par
\expandafter\ifx\noexpand aa T\else F\fi
\expandafter\ifx\noexpand\message\message T\else F\fi
\expandafter\ifx\noexpand\write\write T\else F\fi