Question
Is there a way to check whether a macro is fully expandable (or rather "safe in an expansion-only context" [1])?
Consider this code:
\def\a{Just a string}
\def\b{\a}
\def\c{\def\unsafe}
\def\d{\c}
How could I check which of the macros (a-d) are safe in an expansion-only context? By looking at them I know that a and b are whereas c and d are not but if I wanted to know the same for a macro I haven't written myself this could get quite useful.
Background
I am working on a way to detect whether some input is a valid number in PGF. For this I developed this approach which makes use of passing the input into \pgfmathfloatparsenumber
.
The problem I have run into is that said macro appears to somehow manages to expand the input until there is an error (if the input is in fact not safe in an expansion-only-context). I tried using protected
, noexpand
and similar but somehow PGF manages to circumvent those.
So the idea is to check whether the input is safe before actually passing it to PGF. The problem is: I don't know how I'd go about that...
\unsafe
first then the edef would give something bad but would probably not give an error during the actual edef, such cases you can probably detect. similarly if you have\edef\foo{ {\mbox} }
you are going to get a low level parse error if you expand \mbox and it hits the}
it woul dbe verh hard to avoid such errors if you allow bad input\romannumeral-`0
triggered expansion, then examine first token if a digit ok remove and repeat and do repetitively until either nothing is left or you hit some unexpandable token which is not a digit. You have to detect case of braces etc... The idea here is that\edef
can cause errors if your material is not expandable, but "full-first" expansion will not.