diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2018-10-17 07:39:21 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2018-10-17 07:49:49 +0300 |
commit | 10a9035e6764945748555dde6af255f55d8f5ad2 (patch) | |
tree | b298f76313f19dd2a3380f8400c828e6a21f47c9 | |
parent | a1e13cc50fca680b52355e7ac261dd29088f04cd (diff) | |
download | m4kwargs-10a9035e6764945748555dde6af255f55d8f5ad2.tar.gz m4kwargs-10a9035e6764945748555dde6af255f55d8f5ad2.tar.bz2 |
Fix empty argument handling.
* m4/Makefile (install): Add dnl after changequote
* m4/kwargs.m4: Divert everything to -1.
(KWARG_SET): Tolerate empty argument.
(m4_kwargs_parse): Don't stop on empty arguments.
(KWARGS_DUMP): Change arguments.
(KWARGS_LIST): New macro.
* t/define.t: Reflect changes to KWARGS_DUMP.
* t/dump.t: Likewise.
* t/null.t: Likewise.
-rw-r--r-- | README | 13 | ||||
-rw-r--r-- | m4/Makefile | 2 | ||||
-rw-r--r-- | m4/kwargs.m4 | 40 | ||||
-rw-r--r-- | t/define.t | 10 | ||||
-rw-r--r-- | t/dump.t | 7 | ||||
-rw-r--r-- | t/null.t | 2 |
6 files changed, 50 insertions, 24 deletions
@@ -109,9 +109,12 @@ by category. Debugging --------- -KWARGS_DUMP([PREFIX]) - Dump all keyword arguments to stdout, each on a separate line, prefixed - with PREFIX. +KWARGS_DUMP([DELIM],[EXP]) + For each defined keyword argument, produce the following expansion: + + EXP(`KW=VAL')DELIM + + DELIM defaults to newline, EXP - to m4_quote, Keyword arguments access macros ------------------------------- @@ -140,6 +143,10 @@ KWARGS_FOREACH(ITERATOR, TEXT) TEXT is appended to the expansion of KWARGS_FOREACH. TEXT may refer to ITERATOR. Any definition of ITERATOR prior to this invocation is restored. +KWARGS_LIST() + Expand to a comma-delimited list of quoted keyword arguments, suitable for + use as argument list to a macro. + Expansion in keyword argument context ------------------------------------- diff --git a/m4/Makefile b/m4/Makefile index 0db042f..4333402 100644 --- a/m4/Makefile +++ b/m4/Makefile @@ -14,7 +14,7 @@ install: if test "$(BRACKETQUOTES)" -eq 1; then \ for file in $(SOURCES); do \ cat $$file | tr \`\' '[]' | \ - sed 's/^dnl bracketquotes/changequote([,])/' \ + sed 's/^dnl bracketquotes/changequote([,])dnl/' \ > $(DESTDIR)$(M4DIR)/$$file;\ done;\ else \ diff --git a/m4/kwargs.m4 b/m4/kwargs.m4 index 982fc44..a0d6f1a 100644 --- a/m4/kwargs.m4 +++ b/m4/kwargs.m4 @@ -13,11 +13,11 @@ dnl GNU General Public License for more details. dnl dnl You should have received a copy of the GNU General Public License dnl along with M4KWARGS. If not, see <http://www.gnu.org/licenses/>. -pushdef(`__diversion__',divnum)divert(-1) - +dnl dnl The 'bracketquotes' comment line will be replaced by make install dnl with the appropriate changequote statement, if required. dnl bracketquotes +pushdef(`__diversion__',divnum)divert(-1) dnl Determine whether the patsubst builtin is available and whether it dnl takes an extended regular expression as its second argument. @@ -94,12 +94,13 @@ m4_define(`_m4_kwargs_list') # ------------------------- # Sets argument NAME to VALUE m4_define(`KWARG_SET', +`m4_ifelse(`$1',`',, `m4_pushdef(`__opt_name__',m4_dquote(m4_kwargs_mangle_name(`$1')))m4_dnl m4_kwargs_list_if_member(`_m4_kwargs_list',`$1',`m4_popdef(__opt_name__)', `m4_define(`_m4_kwargs_list',m4_ifempty(m4_quote(_m4_kwargs_list), m4_dquote(m4_dquote(`$1')),`m4_quote(m4_dquote(_m4_kwargs_list), m4_dquote(`$1'))'))')m4_dnl m4_pushdef(__opt_name__,`$2')m4_dnl -m4_popdef(`__opt_name__')') +m4_popdef(`__opt_name__')')') # KWARG_CLR(NAME) # --------------- @@ -197,17 +198,28 @@ m4_popdef(`_m4_kwargs_list')') m4_define(`KWARGS_FOREACH', `m4_foreach(`$1',m4_quote_names(_m4_kwargs_list),`$2')') -# KWARGS_DUMP([PREFIX]) +# KWARGS_DUMP([DELIM],[EXP]) # --------------------- -# Dump all keyword arguments to stdout, each on a separate line, prefixed -# with PREFIX. -# +# For each defined keyword argument, expand to +# `KW=VAL'DELIM +# If EXP is supplied, expand to +# EXP(`KW=VAL')DELIM +# instead. +# Default DELIM is newline. m4_define(`KWARGS_DUMP', -`m4_pushdef(`__dump_newline__')m4_dnl -KWARGS_FOREACH(`_GR_Arg',`m4_ifelse(__dump_newline__,,,` -')m4_define(`__dump_newline__',1)m4_dnl -`$1'_GR_Arg=KWARG_VALUE(_GR_Arg)')m4_dnl -m4_popdef(`__dump_newline__')') + `_$0(`m4_ifelse(`$1',,` +',`$1')',`m4_ifelse(`$2',`',`m4_quote',`$2')',_m4_kwargs_list)') + +m4_define(`_KWARGS_DUMP', + `m4_ifelse(`$3',,, + `m4_quote($2(`$3'=KWARG_VALUE(`$3')))_$0(`$1',`$2',m4_shift(m4_shift(m4_shift($@))))')') + +# _KWARGS_DUMP(DELIM,EXP,ARGS...) +m4_define(`__KWARGS_DUMP', + `m4_ifelse(`$3',,, + `$1_KWARGS_DUMP($@)')') + +m4_define(`KWARGS_LIST',`KWARGS_DUMP(`,')') m4_define(`KW_ARG_1',`$1') m4_define(`KW_ARG_2',`$2') @@ -246,8 +258,8 @@ m4_define(`KWARGS_WITH', `m4_pushdef(`__call__',`$1')KWARGS_APPLY(`__call__',m4_shift($@))m4_popdef(`__call__')') m4_define(`m4_kwargs_parse', -`m4_ifelse(`$1',`',`', - `m4_ifelse(m4_index(`$1',`='),-1,`,``$1''`'m4_kwargs_parse(m4_shift($@))',`KWARGS_ASGN($@)')')') +`m4_ifelse(`$#',`0',`',m4_index(`$1',`='),-1,`,``$1''`'_m4_kwargs_parse($@)',`KWARGS_ASGN($@)')') +m4_define(`_m4_kwargs_parse',`m4_ifelse(`$#',1,`',`m4_kwargs_parse(m4_shift($@))')') # KWARGS_DEFINE(NAME,TEXT) # ------------------------ @@ -21,7 +21,8 @@ argv[1]=KW_ARGV(1) argc=KW_ARGC arg@=KW_ARGQ y=KWARG_VALUE(y) -KWARGS_DUMP(: )')m4_dnl +dump: +KWARGS_DUMP()')m4_dnl # EXPAND FOO(a,`b,x',c,x=4,y=`5,67',a=5) # END @@ -33,8 +34,9 @@ argv[1]=a argc=3 arg@=`a',`b,x',`c' y=5,67 -: x=4 -: y=5,67 -: a=5 +dump: +x=4 +y=5,67 +a=5 # END !END @@ -19,12 +19,17 @@ m4_define(`_m4_kwargs_list',``foo',`bar',`baz'')m4_dnl m4_pushdef(`--foo',`1') m4_pushdef(`--bar',`Bar') m4_pushdef(`--baz',`Baz qux') +m4_define(`printarg',`// `$1'') m4_divert(0)m4_dnl // BEGIN -KWARGS_DUMP(`// ') +KWARGS_DUMP() +KWARGS_DUMP(,`printarg') // END !OUT // BEGIN +foo=1 +bar=Bar +baz=Baz qux // foo=1 // bar=Bar // baz=Baz qux @@ -15,7 +15,7 @@ !C along with M4KWARGS. If not, see <http://www.gnu.org/licenses/>. !IN # BEGIN -KWARGS_DUMP(`# ') +KWARGS_DUMP() # END !OUT # BEGIN |