ensure we don't break m4_expand

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.m4.patches
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The recent autoconf addition of m4_expand exploits some pretty low-level
M4 magic, so I thought I'd better add a test to make sure we don't break
it.  As Paolo Bonzini so aptly stated [1], this (ab)use of changequote is
"Mindblowing."

[1] http://lists.gnu.org/archive/html/autoconf-patches/2007-10/msg00214.html

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

Eric Blake             [email protected]
-----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

iD8DBQFHIy4B84KuGfSFAYARAuquAJ49MEeqYZgfTvI+e/virXclvWYYngCfY48d
q8lZS/GZdn2fWk1wLT1cVmw=
=FOpB
-----END PGP SIGNATURE-----

_______________________________________________
M4-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/m4-patches
m4.patch288 (text/plain, 2.6 KB)
From 85920dd1b26a268744d8da4d4e6d52843b77a36a Mon Sep 17 00:00:00 2001
From: Eric Blake <[email protected]>
Date: Sat, 27 Oct 2007 06:15:51 -0600
Subject: [PATCH] Document one use of changequote(`(',`)').

* doc/m4.texinfo (Changequote): Add new test, based on recent
autoconf addition of m4_expand.

Signed-off-by: Eric Blake <[email protected]>
(cherry picked from commit 37e491acd84f447920057b1619ebab148adea8b1)
---
 ChangeLog      |    6 ++++++
 doc/m4.texinfo |   28 ++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4e9dcbc..6f56dee 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-10-27  Eric Blake  <[email protected]>
+
+	Document one use of changequote(`(',`)').
+	* doc/m4.texinfo (Changequote): Add new test, based on recent
+	autoconf addition of m4_expand.
+
 2007-10-24  Ralf Wildenhues  <[email protected]>
 
 	* Makefile.am (dist_pkgdata_DATA): Add examples/capitalize2.m4.
diff --git a/doc/m4.texinfo b/doc/m4.texinfo
index 5cedc18..f84f4d3 100644
--- a/doc/m4.texinfo
+++ b/doc/m4.texinfo
@@ -4074,6 +4074,34 @@ echo(hi,hi)bye)
 @result{}1:HIhibye:
 @end example
 
+However, if you are not worried about portability, using @samp{(} and
+@samp{)} as quoting characters has an interesting property---you can use
+it to compute a quoted string containing the expansion of any quoted
+text, as long as the expansion results in both balanced quotes and
+balanced parentheses.  The trick is realizing @code{expand} uses
+@samp{$1} unquoted, to trigger its expansion using the normal quoting
+characters, but uses extra parentheses to group unquoted commas that
+occur in the expansion without consuming whitespace following those
+commas.  Then @code{_expand} uses @code{changequote} to convert the
+extra parentheses back into quoting characters.  Note that it takes two
+more @code{changequote} invocations to restore the original quotes.
+Contrast the behavior on whitespace when using @samp{$*}, via
+@code{quote}, to attempt the same task.
+
+@example
+changequote(`[', `]')dnl
+define([a], [1, (b)])dnl
+define([b], [2])dnl
+define([quote], [[$*]])dnl
+define([expand], [_$0(($1))])dnl
+define([_expand],
+  [changequote([(], [)])$1changequote`'changequote(`[', `]')])dnl
+expand([a, a, [a, a], [[a, a]]])
+@result{}1, (2), 1, (2), a, a, [a, a]
+quote(a, a, [a, a], [[a, a]])
+@result{}1,(2),1,(2),a, a,[a, a]
+@end example
+
 If @var{end} is a prefix of @var{start}, the end-quote will be
 recognized in preference to a nested begin-quote.  In particular,
 changing the quotes to have the same string for @var{start} and
-- 
1.5.3.2
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.