autoconf-patches
[Top][All Lists]
Advanced

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

Re: Handling of single-quoted comma [was: m4_defn overhead]


From: Eric Blake
Subject: Re: Handling of single-quoted comma [was: m4_defn overhead]
Date: Wed, 24 Oct 2007 03:18:47 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Eric Blake <ebb9 <at> byu.net> writes:

> 
>       Fix 2007-10-03 regression with AT_SETUP([a, b]).
>       * lib/m4sugar/m4sugar.m4 (m4_expand): New macro.

My first implementation uses recursion (which, until m4 is fixed, scales 
quadratically [1]), collapses whitespace after unquoted commas in the 
expansion, and leaves garbage around overquoted commas in the original (since 
in general, m4_split can't distinquish over-quoted separators from regular 
separators):

m4_define([active], [ACT, IVE])dnl
m4_expand([active, [a, a], [[a, a]]])
=> ACT,IVE, a, a, [a], [ a]

So I fixed all of these - the new version needs no recursion (so it scales 
linearly even for the current versions of m4 [1]), handles unquoted commas in 
expansion, and doesn't try to split whitespace after quoted commas.  Oh, and it 
uses fewer regular expressions.  Hands down a slicker implementation!  (OK, so 
maybe it abuses changequote in the process - but hey, I'm also the m4 
maintainer, so I think I can get away with it :)

m4_define([active], [ACT, IVE])dnl
m4_expand([active, [a, a], [[a, a]]])
=> ACT, IVE, a, a, [a, a]

[1] yes, I'm working on fixing m4 to avoid quadratic scaling on recursive 
algorithms, but that's much tougher

From: Eric Blake <address@hidden>
Date: Tue, 23 Oct 2007 20:47:08 -0600
Subject: [PATCH] Improve corner case of m4_expand.

* lib/m4sugar/m4sugar.m4 (m4_expand, _m4_expand): Rewrite more
efficiently.
* tests/m4sh.at (AS@&address@hidden): Test overquoted comma.
* doc/autoconf.texi (Evaluation Macros) <m4_expand>: Update
documentation.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog              |    9 +++++++++
 doc/autoconf.texi      |    6 +++---
 lib/m4sugar/m4sugar.m4 |   29 ++++++++++-------------------
 tests/m4sh.at          |    4 +++-
 4 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3c4b592..b0411e1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2007-10-23  Eric Blake  <address@hidden>
+
+       Improve corner case of m4_expand.
+       * lib/m4sugar/m4sugar.m4 (m4_expand, _m4_expand): Rewrite more
+       efficiently.
+       * tests/m4sh.at (AS@&address@hidden): Test overquoted comma.
+       * doc/autoconf.texi (Evaluation Macros) <m4_expand>: Update
+       documentation.
+
 2007-10-23  Paul Eggert  <address@hidden>
 
        * doc/make-stds.texi: Update from gnulib.
diff --git a/doc/autoconf.texi b/doc/autoconf.texi
index e23f709..f05b2ec 100644
--- a/doc/autoconf.texi
+++ b/doc/autoconf.texi
@@ -10771,8 +10771,8 @@ on quoted text.  The distinction is in the treatment of 
whitespace
 following a comma in the original @var{arg}.  Any time multiple
 arguments are collected into one with @code{m4_quote}, the M4 argument
 collection rules discard the whitespace.  However, with @code{m4_expand},
-whitespace is discarded only if it results from unquoted commas in the
-expansion of macros contained in @var{arg}.
+whitespace is preserved, even after the expansion of macros contained in
address@hidden
 
 Note that @code{m4_expand} cannot parse everything.  The expansion of
 @var{arg} must not contain unbalanced quotes (although quadrigraphs can
@@ -10786,7 +10786,7 @@ m4_define([active2], [[ACT, IVE]])dnl
 m4_quote(active, active)
 @result{}ACT,IVE,ACT,IVE
 m4_expand([active, active])
address@hidden,IVE, ACT,IVE
address@hidden, IVE, ACT, IVE
 m4_quote(active2, active2)
 @result{}ACT, IVE,ACT, IVE
 m4_expand([active2, active2])
diff --git a/lib/m4sugar/m4sugar.m4 b/lib/m4sugar/m4sugar.m4
index 7ac53d5..e4bf9b2 100644
--- a/lib/m4sugar/m4sugar.m4
+++ b/lib/m4sugar/m4sugar.m4
@@ -665,34 +665,25 @@ m4_define([m4_echo], address@hidden)
 # --------------
 # Return the expansion of ARG as a single string.  Unlike m4_quote($1), this
 # correctly preserves whitespace following single-quoted commas that appeared
-# within ARG (however, it does not preserve whitespace after any unquoted
-# commas encountered in the expansion).
+# within ARG.
 #
 #   m4_define([active], [ACT, IVE])
 #   m4_define([active2], [[ACT, IVE]])
 #   m4_quote(active, active2)
 #   => ACT,IVE,ACT, IVE
 #   m4_expand([active, active2])
-#   => ACT,IVE, ACT, IVE
+#   => ACT, IVE, ACT, IVE
 #
 # Unfortunately, due to limitations in m4, ARG must contain balanced quotes
 # (use quadrigraphs) and balanced parentheses (use creative shell comments
 # when writing shell case statements).
 #
-# Splitting a quoted ARG on `,' preserves space, but produces a quoted list.
-# Unquote the list, then expand each argument while preserving the leading
-# spaces; finally, collect each argument back into the final string.
-m4_define([m4_expand],
-[m4_quote(_$0(m4_unquote(m4_split([$1], [,]))))])
-
-# _m4_expand(ARGS)
-# ----------------
-# Return the expansion of each ARG, separated by `,'.  Less efficient than
-# m4_unquote, but preserves quoted leading space in each ARG.
+# Exploit that extra () will group unquoted commas and the following
+# whitespace, then convert () to [].  m4_bpatsubst can't handle newlines
+# inside $1, and m4_substr strips quoting.  So we (ab)use m4_changequote.
+m4_define([m4_expand], [_$0(($1))])
 m4_define([_m4_expand],
-[m4_if([$#], [0], [],
-       [$#], [1], [$1],
-       [$1,$0(m4_shift($@))])])
+[m4_changequote([(], [)])$1m4_changequote`'m4_changequote(`[', `]')])
 
 
 # m4_ignore(ARGS)
diff --git a/tests/m4sh.at b/tests/m4sh.at
index 9c6624c..966ca9a 100644
--- a/tests/m4sh.at
+++ b/tests/m4sh.at
@@ -491,7 +491,7 @@ AT_CLEANUP
 ## -------------- ##
 
 AT_SETUP([AS@&address@hidden)
-AT_KEYWORDS([m4@&address@hidden)
+AT_KEYWORDS([m4@&address@hidden m4@&address@hidden)
 
 AT_DATA_M4SH([script.as],
 [[AS_INIT
@@ -547,6 +547,7 @@ m4_define([mac], [MACRO])dnl
 echo "AS_HELP_STRING([--mac], [mac])"
 echo "AS_HELP_STRING([--o1, --o2], [two
 options,       one  description])"
+echo "AS_HELP_STRING([[[--o3, --o4]]], [comma inside literal quoting])"
 echo "AS_HELP_STRING([--tune1], [check out the tuned formatting],
 [            ])"
 echo "AS_HELP_STRING([--tune2], [check out the tuned formatting],
@@ -612,6 +613,7 @@ AT_CHECK([./script], [0],
                           80 characters.
   --MACRO                 mac
   --o1, --o2              two options, one description
+  [--o3, --o4]            comma inside literal quoting
   --tune1   check out the tuned formatting
   --tune2   check out the tuned formatting
   --tune3                 check out the
-- 
1.5.3.2








reply via email to

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