Re: Better way to interpret $N as dollars, not macro argument?

Daniel Goldman <[email protected]> Sat, 16 Aug 2014 11:10:09 -0700
Newsgroups gmane.comp.gnu.m4.general
Message-ID <[email protected]>
On 8/15/2014 12:07 PM, Eric Blake wrote:
>
> Side note: it's a bad idea to use m4_define without quoting the first
> argument.  If the define gets called more than once, then you end up
> defining a completely different macro name (namely, whatever the
> expansion of the original macro name was).  You should ALWAYS quote the
> first argument to define.
>

I'm well aware of that strong recommendation. I appreciate your bringing 
it up. To not get off-topic on this post, and to give me time to think 
about this more before going out on a limb (which might break), it's 
best for me to hold off responding to the side note.

>
> Alas, such is life.  Another approach that I have personally used is to
> delegate to helper functions that concatenate the results into the
> desired output.  Instead of:
>
> m4_define([[[foo]]], [[[[[[$]]]1 is $1]]])
>
> I do:
>
> m4_define([[[cat]]], [[[$1$2]]])
> m4_define([[[foo]]], [[[cat($, [[[1 is $1]]])]]])
>
> which still requires special casing $, but a little less thought on how
> to add creative use of quoting.
>

Thanks for the suggestion. I do something similar, but not for the 
dollar sign. Here is an example.

m4_define(M4_JAM, $1$2)
m4_define(CHT_Def_Val_001, [[[ M4_JAM(CHT_Def_Val_, RUN_ID_001) ]]])

Maybe I should quote $1$2 like you do, it wouldn't hurt. It works fine 
unquoted, because it always "jams" together parts of C names, so at 
least never any commas to protect. But if I used [[[$1$2]]] it would be 
more general purpose.

>> Anyway, my question: Is there a better way to tell m4 not to interpret
>> $N as "dollar amount"? Based on the manual, the answer seems "no". But I
>> thought I would ask, hoping maybe some other way...
>
> Sadly, this is one area where M4 is locked into historical warts.  At
> one point, there was an M5 language written, where you could use $$
> inside any macro definition to have the expansion of that macro use one
> less $ instead of treating $ as a possible parameter lead-in (thus,
> m4_define([[[foo]]], [[[$1 $$1]]]) and called as foo(a) would expand to
> 'a $1').  It might be interesting if someone were to write a patch to
> add an opt-in setting to m4 to copy from M5's dollar quoting mechanism
> (of course, it must remain off by default for back-compat).  But I don't
> know of anyone currently trying to write such a patch.
>
> http://www.worldcat.org/title/users-guide-to-the-m5-macro-language/oclc/25491290
>
> (Hmm, I know at one point I was able to read a pdf about M5 at no
> charge, but can't seem to find a URL for it via a quick search today)
>

I saw something on m5, when I was researching and learning about m4. The 
m5 stuff seemed old and dead, similar to what you found dated from 1991. :(

Personally, instead of trying to implement $$, I think a better priority 
might be to get named macros working. [[[$1 $$1]]] IS a lot easier to 
read than [[[$1 [[[$]]]1]]] (IMO), but maybe the extra effort to add $$ 
is not worth the modest readability benefit, especially as `$1 $$1' and 
`$1 `$'1' seem closer in readability (IMO).

Thanks,
Daniel