Re: problem embedding argument inside nested macro def's

[email protected] (Eric Blake)
Newsgroups gmane.comp.gnu.m4.general
Message-ID <062820080638.18942.4865DC7A000E8439000049FE22069997350A050E040D0C079D0A@comcast.net>
From: "Piotr Wysocki":

Hello Piotr,

[you caught me on vacation, but I can answer this anyway]

> Hi,
> 
> I have a problem. This is my code:
> 
> What I meant is to define ___DEF macro, whose argument will
> be used in creating another macro (__T in the above example),
> which will be a macro creating a simple macro (without args).
> I would like the last expression to result in "aabc".
> The problem is that the last `$1' is expanded into `T'. I would
> rather like to expand it later.

This has come up before; in fact, the git repository now has an
example in the manual that does the very task of defining a
macro with deferred $1 expansion:
http://git.savannah.gnu.org/gitweb/?p=m4.git;a=commitdiff;h=bc9b4d7

But that example was not added until after the 1.4.11 release,
so hopefully reading a texinfo diff is not too confusing, until
such time as a new release is made and the online manual
updated to contain the same example rendered in html.

> What is the correct syntax to achieve my goal?

You need to use a helper macro, and supply any instance of
$ followed by a special character that must appear literally
in the nested define as independent literally-quoted strings
in the forwarding macro.  Something like this (but untested,
since I'm not at my regular computer):

divert(-1)

define(`___DEF', `_$0(`$1', `$'`1')')
define(`____DEF', `define(`__$1', `define(`_a$2', `a$2')')')
dumpdef(`___DEF')
___DEF(`T')
dumpdef(`__T')
__T(`abc')
dumpdef(`_aT')

divert(0)dnl

Note how ___DEF now calls ____DEF with two arguments,
the `$1' argument handed to ___DEF, and the literal argument
consisting of `$' concatenated with `1'.  That way, in ____DEF,
use of $1 refers to the argument of ___DEF, and use of $2 refers
to the deferred string $1 for use in the second round of defines.

-- 
Eric Blake
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.