Testing if a token list can be assigned to a \dimendef'd quantity

Just to give the flavor, here's the test up to the check whether the first token (after f-expansion) is either \dimen or \skip and is followed by an integer less than 32768:

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\tl_new:N \l_ahmed_arg_tl
\tl_new:N \l_ahmed_first_tl
\tl_new:N \l_ahmed_rest_tl
\tl_new:N \l_ahmed_temp_tl
\seq_new:N \l_ahmed_extr_seq
\prg_new_protected_conditional:Npnn \ahmed_if_dimen:n #1 {T,F,TF}
 {
  \tl_set:Nf \l_ahmed_arg_tl { #1 }
  \ahmed_check_primitive:
 }
\cs_generate_variant:Nn \token_if_eq_meaning_p:NN {NV}
\cs_new_protected:Npn \ahmed_check_primitive:
 {
  \tl_set:Nx \l_ahmed_first_tl { \tl_head:V \l_ahmed_arg_tl }
  \tl_set:Nx \l_ahmed_rest_tl { \tl_tail:V \l_ahmed_arg_tl }
  \bool_if:nTF
   {
    \token_if_eq_meaning_p:NV \tex_dimen:D \l_ahmed_first_tl
    ||
    \token_if_eq_meaning_p:NV \tex_skip:D \l_ahmed_first_tl
   }
   { \ahmed_check_integer: }
   { \ahmed_check_def_token: }
 }

\cs_generate_variant:Nn \regex_extract_once:nnN {nV}
\cs_new_protected:Npn \ahmed_check_integer:
 {
  \tl_set:Nf \l_ahmed_temp_tl { \l_ahmed_rest_tl } 
  \regex_extract_once:nVN { \A \d * } \l_ahmed_temp_tl \l_ahmed_extr_seq
  \seq_if_empty:NTF \l_ahmed_extr_seq
    { \prg_return_false: }
    {
     \int_compare:nTF { \seq_item:Nn \l_ahmed_extr_seq {0} < 32768 }
       { \prg_return_true: }
       { \prg_return_false: }
    }
 }

\cs_new:Npn \ahmed_test:n #1
 {
  \ahmed_if_dimen:nTF { #1 }{ \typeout{YES} } { \typeout{NO} }
 }

\ahmed_test:n {\dimen34abc}
\ahmed_test:n {\skip1234567}
\def\xyz{\dimen22}
\ahmed_test:n {\xyz}
\ahmed_test:n {aaa}

The output is

YES
NO
YES
! Undefined control sequence.
<argument> \ahmed_check_def_token: 

l.53 \ahmed_test:n {aaa}

showing that in the fourth case the control is indeed transferred to the next stage. This is actually still incomplete, because the integer following \dimen or \skip might be an "implicit number" (a count register or \chardef token, for instance), so a check for that would be necessary. Or, worse, it might have been embedded in a macro:

\def\fake{2}
\dimen1\fake 1=1pt

would be a legitimate assignment to \dimen121.

If there is more control on the token list, for example we are sure it can be expanded to a list of unexpandable tokens, the check could be easier.

Tags:

Tex Core