This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "GNU M4 source repository".
http://git.sv.gnu.org/gitweb/?p=m4.git;a=commitdiff;h=b5b24ec23ec1f242e8d4f3571ec288d9d03fd58d The branch, master has been updated via b5b24ec23ec1f242e8d4f3571ec288d9d03fd58d (commit) via daed50d635fad7a37f7d050926dfe04d96af5ebb (commit) from 2e81b080376fcc4f3362a0c4810de084371c87d0 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit b5b24ec23ec1f242e8d4f3571ec288d9d03fd58d Author: Eric Blake <[EMAIL PROTECTED]> Date: Tue Feb 19 12:18:05 2008 -0700 Clean up example on filtering defined symbols. * doc/m4.texinfo (Foreach, Improved foreach): Document another shortcoming in foreach.m4, and improve filter example by using foreach2.m4. Signed-off-by: Eric Blake <[EMAIL PROTECTED]> commit daed50d635fad7a37f7d050926dfe04d96af5ebb Author: Eric Blake <[EMAIL PROTECTED]> Date: Tue Feb 19 08:59:45 2008 -0700 * src/main.c (usage): Fix typo. Signed-off-by: Eric Blake <[EMAIL PROTECTED]> ----------------------------------------------------------------------- Summary of changes: ChangeLog | 9 ++++++ doc/m4.texinfo | 81 +++++++++++++++++++++++++++++++++++++------------------ src/main.c | 2 +- 3 files changed, 64 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index ba56df5..1832be1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-02-19 Eric Blake <[EMAIL PROTECTED]> + + Clean up example on filtering defined symbols. + * doc/m4.texinfo (Foreach, Improved foreach): Document another + shortcoming in foreach.m4, and improve filter example by using + foreach2.m4. + + * src/main.c (usage): Fix typo. + 2008-02-18 Eric Blake <[EMAIL PROTECTED]> Avoid some magic numbers. diff --git a/doc/m4.texinfo b/doc/m4.texinfo index 571c0dc..011fbfb 100644 --- a/doc/m4.texinfo +++ b/doc/m4.texinfo @@ -3362,38 +3362,26 @@ has its own severe flaw. Whereas the @code{foreach} implementation was linear, this macro is quadratic in the number of list elements, and is much more likely to trip up the limit set by the command line option @option{--nesting-limit} (or @option{-L}, @pxref{Limits control, , -Invoking m4}). (It is possible to have robust iteration with linear -behavior for either list style. See if you can learn from the best -elements of both of these implementations to create robust macros; or [EMAIL PROTECTED] foreach, , Answers}). - -With a robust @code{foreach} implementation, it is possible to create a -filter on a list of defined symbols. This next example will find all -symbols that contain @samp{if}. Notice the use of @code{dquote} and [EMAIL PROTECTED] to ensure that the list of macro names is properly -quoted; without these, the iteration would be invoking various macros -with catastrophic effects. This example also shows a trick for -generating the correct number of commas in the resulting output. +Invoking m4}). Additionally, this implementation does not expand [EMAIL PROTECTED]([EMAIL PROTECTED]')} very well, when compared with [EMAIL PROTECTED] @comment examples @example $ @kbd{m4 -I examples} -include(`quote.m4')include(`foreachq.m4') [EMAIL PROTECTED] -pushdef(`sep', ``, '') [EMAIL PROTECTED] -pushdef(`cleanup', `popdef(`sep', `cleanup')') [EMAIL PROTECTED] -pushdef(`sep', `define(`cleanup', - `popdef(`cleanup')')popdef(`sep')') [EMAIL PROTECTED] -foreachq(`macro', dquote(dquote_elt(m4symbols)), - `regexp(macro, `.*if.*', `sep`\&'')') [EMAIL PROTECTED], ifelse, shift -cleanup +include(`foreach.m4')include(`foreachq.m4') @result{} +foreach(`name', `(`a', `b')', ` defn(`name')') [EMAIL PROTECTED] a b +foreachq(`name', ``a', `b'', ` defn(`name')') [EMAIL PROTECTED] _arg1(`a', `b') _arg1(shift(`a', `b')) @end example +It is possible to have robust iteration with linear behavior and sane [EMAIL PROTECTED] contents for either list style. See if you can learn +from the best elements of both of these implementations to create robust +macros (or @pxref{Improved foreach, , Answers}). + @node Debugging @chapter How to debug macros and input @@ -8405,7 +8393,9 @@ Note that the fixed version calls unquoted helper macros in in turn must re-supply the layer of quotes lost in the macro invocation. Contrast the use of @[EMAIL PROTECTED], which quotes the first list element, with @[EMAIL PROTECTED] of the earlier implementation that -returned the first list element directly. +returned the first list element directly. Additionally, by calling the +helper method immediately, the @samp{defn([EMAIL PROTECTED]')} no longer +contains unexpanded macros. The astute m4 programmer might notice that the solution above still uses more memory, and thus more time, than strictly necessary. Note that @@ -8501,10 +8491,43 @@ foreach(`x', `(`1', `2', `3', `4')', `x @error{}m4trace: -3- shift(``4'') @end example [EMAIL PROTECTED] filtering defined symbols [EMAIL PROTECTED] subset of defined symbols [EMAIL PROTECTED] defined symbols, filtering +With a robust @code{foreachq} implementation, it is possible to create a +filter on a list of defined symbols. This next example will find all +symbols that contain @samp{if} or @samp{def}, via two different +approaches. In the first approach, @code{dquote_elt} is used to +overquote each list element, then @code{dquote} forms the list; that +way, the iterator @code{macro} can be expanded in place because its +contents are already quoted. This approach also uses a self-modifying +macro @code{sep} to provide the correct number of commas. In the second +approach, the iterator @code{macro} contains live text, so it must be +used with @code{defn} to avoid unintentional expansion. The correct +number of commas is achieved by using @code{shift} to ignore the first +one, although a leading space still remains. + [EMAIL PROTECTED] examples [EMAIL PROTECTED] +$ @kbd{m4 -I examples} +include(`quote.m4')include(`foreachq2.m4') [EMAIL PROTECTED] +pushdef(`sep', `define(`sep', ``, '')') [EMAIL PROTECTED] +foreachq(`macro', dquote(dquote_elt(m4symbols)), + `regexp(macro, `.*if.*', `sep`\&'')') [EMAIL PROTECTED], ifelse, shift +popdef(`sep') [EMAIL PROTECTED] +shift(foreachq(`macro', dquote(m4symbols), + `regexp(defn(`macro'), `def', `,` ''dquote(defn(`macro')))')) [EMAIL PROTECTED] define, defn, dumpdef, ifdef, popdef, pushdef, undefine [EMAIL PROTECTED] example + In summary, recursion over list elements is trickier than it appeared at first glance, but provides a powerful idiom within @code{m4} processing. As a final demonstration, both list styles are now able to handle -several scenarios that would wreak havoc on the original +several scenarios that would wreak havoc on one or both of the original implementations. This points out one other difference between the list styles. @code{foreach} evaluates unquoted list elements only once, in preparation for calling @[EMAIL PROTECTED], similary for @@ -8538,6 +8561,10 @@ foreach(`x', `(`,')', `<x>') / foreachq(`x', ``,'', `<x>') dnl 2-element list of unbalanced parentheses foreach(`x', `(`(', `)')', `<x>') / foreachq(`x', ``(', `)'', `<x>') @result{}<(><)> / <(><)> +define(`ab', `oops')dnl using defn(`iterator') +foreach(`x', `(`a', `b')', `defn(`x')') /dnl + foreachq(`x', ``a', `b'', `defn(`x')') [EMAIL PROTECTED] / ab define(`active', `ACT, IVE') @result{} traceon(`active') diff --git a/src/main.c b/src/main.c index e36c826..a11e821 100644 --- a/src/main.c +++ b/src/main.c @@ -75,7 +75,7 @@ usage (int status) fputs (_("\ Process macros in FILEs.\n\ If no FILE or if FILE is `-', standard input is read. If no FILE, and both\n\ -standard input and standard output are terminals, -i is implied.\n\ +standard input and standard error are terminals, -i is implied.\n\ "), stdout); puts (""); fputs (_("\ hooks/post-receive -- GNU M4 source repository
