m4-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Macro + diversion issue


From: Eric Blake
Subject: Re: Macro + diversion issue
Date: Sat, 19 May 2007 21:21:31 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.10) Gecko/20070221 Thunderbird/1.5.0.10 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Daniel Richard G. on 5/19/2007 7:00 PM:
> Hello,
> 
> I've been messing around with diversions inside macros, and have come upon 
> an odd scenario. I'd like to call it a bug, but I get the same behavior 
> from m4 1.4 as from 1.9a.

Not a bug in m4, but a lack of understanding on your part.  You may find
using the debug and tracing abilities of m4 useful here.

> So, what's going on here, and is there any way to make div10() work as 
> expected without needing to quote the argument to foo()?
> 
> define(`div10', `divert(10)$1`'divert(0)')dnl
> define(`foo', `<FOO>$1</FOO>
> ')dnl
> dnl
> div10(`This is text in div 10.
> ')dnl
> foo(`This goes inside FOO tags.div10(`This is some more text in div 10.
> ')')dnl
> foo(This also goes inside FOO tags.div10(`This is even more text in div 10.
> '))dnl

Or more simply:
foo(text1.div10(`text2'))dnl
               ^

When m4 gets to the point marked by ^, it notices a macro, and expands it
to text as it continues to collect the arguments of foo, resulting in:

foo(text1.divert(10)text2`'divert(0))dnl
          ^

but there are still unexpanded macros, so it continues to expand text,
further resulting in:

foo(text1.text2)dnl
               ^

that is, you both swapped to and back from diversion 10, all while
collecting the argument for foo.  Finally, with the argument now in place,
foo is invoked, as though it had been called with `text1.text2' as its
properly quoted argument.

As for how to do this, I recommend that you stick with consistent quoting,
as you already surmised.  The autoconf manual, and my recent revisions to
the m4 manual, are serious when they say that the policy of a consistent
interface of one level of quoting per level of parentheses is the easiest
way to have predictable results.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGT76684KuGfSFAYARAk7/AKDLHB4jSR5v0zr40xYq/X3J07noTQCgkeaD
tabbF9pj0+JHd5mqbsbuyns=
=AgTJ
-----END PGP SIGNATURE-----




reply via email to

[Prev in Thread] Current Thread [Next in Thread]