From 0a757bb3dc2b684ba1863646f7090e39627f93a4 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 8 Oct 2001 08:22:49 +0000 Subject: [PATCH] * lib/m4sugar/m4sugar.m4 (m4_case, m4_bmatch, m4_normalize) (m4_list_cmp): Use $0 to reinvoke yourself. (m4_patsubsts): New. (m4_strip, m4_version_unletter): Use it. * tests/atspecific.m4 (AT_DATA_M4SUGAR, AT_DATA_M4SH): Likewise. --- ChangeLog | 9 ++++++ lib/m4sugar/m4sugar.m4 | 67 ++++++++++++++++++++++++++++-------------- tests/atspecific.m4 | 12 ++++---- tests/tools.at | 2 +- 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index aaa7680d..b03dd733 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2001-10-08 Akim Demaille + + * lib/m4sugar/m4sugar.m4 (m4_case, m4_bmatch, m4_normalize) + (m4_list_cmp): Use $0 to reinvoke yourself. + (m4_patsubsts): New. + (m4_strip, m4_version_unletter): Use it. + * tests/atspecific.m4 (AT_DATA_M4SUGAR, AT_DATA_M4SH): Likewise. + + 2001-10-08 Akim Demaille * lib/autoconf/autoconf.m4, lib/autoconf/general.m4, diff --git a/lib/m4sugar/m4sugar.m4 b/lib/m4sugar/m4sugar.m4 index 8e459d4c..6733134d 100644 --- a/lib/m4sugar/m4sugar.m4 +++ b/lib/m4sugar/m4sugar.m4 @@ -400,7 +400,7 @@ m4_define([m4_case], [$#], 1, [], [$#], 2, [$2], [$1], [$2], [$3], - [m4_case([$1], m4_shiftn(3, $@))])]) + [$0([$1], m4_shiftn(3, $@))])]) # m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT) @@ -422,7 +422,7 @@ m4_define([m4_bmatch], [m4_if([$#], 0, [], [$#], 1, [], [$#], 2, [$2], - m4_bregexp([$1], [$2]), -1, [m4_bmatch([$1], m4_shiftn(3, $@))], + m4_bregexp([$1], [$2]), -1, [$0([$1], m4_shiftn(3, $@))], [$3])]) @@ -431,6 +431,30 @@ m4_define([m4_bmatch], ## 6. Enhanced version of some primitives. ## ## ---------------------------------------- ## +# m4_patsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...) +# --------------------------------------------------- +# m4 equivalent of +# +# $_ = STRING; +# s/RE1/SUBST1/g; +# s/RE2/SUBST2/g; +# ... +# +# All the values are optional, and the macro is robust to active symbols +# properly quoted. +# +# I would have liked to name this macro `m4_patsubst', unfortunately, +# due to quotation problems, I need to double quote $1 below, therefore +# the anchors are broken :( I can't let users be trapped by that. +m4_define([m4_bpatsubsts], +[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])], + [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])], + [$#], 2, [m4_builtin([patsubst], $@)], + [$0(m4_builtin([patsubst], [[$1]], [$2], [$3]), + m4_shiftn(3, $@))])]) + + + # m4_do(STRING, ...) # ------------------ # This macro invokes all its arguments (in sequence, of course). It is @@ -1442,17 +1466,16 @@ m4_define([m4_flatten], # of brackets around $1 (don't forget that the result must be quoted # too, hence one more quoting than applications). # -# Then notice the patsubst of the middle: it is in charge of removing -# the leading space. Why not just `patsubst(..., [^ ])'? Because this -# macro will receive the output of the preceding patsubst, i.e. more or -# less [[STRING]]. So if there is a leading space in STRING, then it is -# the *third* character, since there are two leading `['; Equally for -# the outer patsubst. +# Then notice the 2 last pattens: they are in charge of removing the +# leading/trailing spaces. Why not just `[^ ]'? Because they are +# applied to doubly quoted strings, i.e. more or less [[STRING]]. So +# if there is a leading space in STRING, then it is the *third* +# character, since there are two leading `['; equally for the last pattern. m4_define([m4_strip], -[m4_bpatsubst(m4_bpatsubst(m4_bpatsubst([[[[$1]]]], - [[ ]+], [ ]), - [^\(..\) ], [\1]), - [ \(.\)$], [\1])]) +[m4_bpatsubsts([[$1]], + [[ ]+], [ ], + [^\(..\) ], [\1], + [ \(..\)$], [\1])]) # m4_normalize(STRING) @@ -1479,7 +1502,7 @@ m4_defun([m4_join], [m4_case([$#], [1], [], [2], [[$2]], - [[$2][$1]m4_join([$1], m4_shift(m4_shift($@)))])]) + [[$2][$1]$0([$1], m4_shiftn(2, $@))])]) @@ -1660,12 +1683,12 @@ m4_define([m4_cmp], # m4_list_cmp((1), (1, 2)) -> -1 m4_define([m4_list_cmp], [m4_if([$1$2], [()()], 0, - [$1], [()], [m4_list_cmp((0), [$2])], - [$2], [()], [m4_list_cmp([$1], (0))], + [$1], [()], [$0((0), [$2])], + [$2], [()], [$0([$1], (0))], [m4_case(m4_cmp(m4_car$1, m4_car$2), -1, -1, 1, 1, - 0, [m4_list_cmp((m4_shift$1), (m4_shift$2))])])]) + 0, [$0((m4_shift$1), (m4_shift$2))])])]) @@ -1684,13 +1707,13 @@ m4_define([m4_list_cmp], # This macro is absolutely not robust to active macro, it expects # reasonable version numbers and is valid up to `z', no double letters. m4_define([m4_version_unletter], -[m4_translit(m4_bpatsubst(m4_bpatsubst(m4_bpatsubst([$1], - [\([0-9]+\)\([abcdefghi]\)], - [m4_eval(\1 + 1).-1.\2]), - [\([0-9]+\)\([jklmnopqrs]\)], - [m4_eval(\1 + 1).-1.1\2]), +[m4_translit(m4_bpatsubsts([$1], + [\([0-9]+\)\([abcdefghi]\)], + [m4_eval(\1 + 1).-1.\2], + [\([0-9]+\)\([jklmnopqrs]\)], + [m4_eval(\1 + 1).-1.1\2], [\([0-9]+\)\([tuvwxyz]\)], - [m4_eval(\1 + 1).-1.2\2]), + [m4_eval(\1 + 1).-1.2\2]), [abcdefghijklmnopqrstuvwxyz], [12345678901234567890123456])]) diff --git a/tests/atspecific.m4 b/tests/atspecific.m4 index 1ae9a3fb..13cab5e1 100644 --- a/tests/atspecific.m4 +++ b/tests/atspecific.m4 @@ -24,12 +24,13 @@ # AT_DATA_M4SUGAR(FILENAME, CONTENTS) -# -------------------------------- +# ----------------------------------- # Escape the invalid tokens with @&t@. m4_define([AT_DATA_M4SUGAR], [AT_DATA([$1], -[m4_bpatsubst(m4_bpatsubst([[$2]], [\(m4\)_], [\1@&t@_]), - [dnl], [d@&t@nl])])]) +[m4_bpatsubsts([$2], + [\(m4\)_], [\1@&t@_], + [dnl], [d@&t@nl])])]) # AT_CHECK_M4SUGAR(FLAGS, [EXIT-STATUS = 0], STDOUT, STDERR) @@ -51,8 +52,9 @@ AT_CHECK([autom4te --language=m4sugar script.4s -o script $1], # Escape the invalid tokens with @&t@. m4_define([AT_DATA_M4SH], [AT_DATA([$1], -[m4_bpatsubst(m4_bpatsubst([[$2]], [\(m4\|AS\)_], [\1@&t@_]), - [dnl], [d@&t@nl])])]) +[m4_bpatsubsts([$2], + [\(m4\|AS\)_], [\1@&t@_], + [dnl], [d@&t@nl])])]) # AT_CHECK_M4SH(FLAGS, [EXIT-STATUS = 0], STDOUT, STDERR) diff --git a/tests/tools.at b/tests/tools.at index ab1eb524..c7b5c432 100644 --- a/tests/tools.at +++ b/tests/tools.at @@ -247,7 +247,7 @@ m4_foo _m4_bar AS_FOO _AS_BAR -[d@&t@nl] +[dnl] ]]) AT_DATA_M4SH([experr],