source: EcnlProtoTool/trunk/tools/share/bison/m4sugar/foreach.m4@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

File size: 17.1 KB
Line 
1# -*- Autoconf -*-
2# This file is part of Autoconf.
3# foreach-based replacements for recursive functions.
4# Speeds up GNU M4 1.4.x by avoiding quadratic $@ recursion, but penalizes
5# GNU M4 1.6 by requiring more memory and macro expansions.
6#
7# Copyright (C) 2008 Free Software Foundation, Inc.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22# As a special exception, the Free Software Foundation gives unlimited
23# permission to copy, distribute and modify the configure scripts that
24# are the output of Autoconf. You need not follow the terms of the GNU
25# General Public License when using or distributing such scripts, even
26# though portions of the text of Autoconf appear in them. The GNU
27# General Public License (GPL) does govern all other use of the material
28# that constitutes the Autoconf program.
29#
30# Certain portions of the Autoconf source text are designed to be copied
31# (in certain cases, depending on the input) into the output of
32# Autoconf. We call these the "data" portions. The rest of the Autoconf
33# source text consists of comments plus executable code that decides which
34# of the data portions to output in any given case. We call these
35# comments and executable code the "non-data" portions. Autoconf never
36# copies any of the non-data portions into its output.
37#
38# This special exception to the GPL applies to versions of Autoconf
39# released by the Free Software Foundation. When you make and
40# distribute a modified version of Autoconf, you may extend this special
41# exception to the GPL to apply to your modified version as well, *unless*
42# your modified version has the potential to copy into its output some
43# of the text that was the non-data portion of the version that you started
44# with. (In other words, unless your change moves or copies text from
45# the non-data portions to the data portions.) If your modification has
46# such potential, you must delete any notice of this special exception
47# to the GPL from your modified version.
48#
49# Written by Eric Blake.
50#
51
52# In M4 1.4.x, every byte of $@ is rescanned. This means that an
53# algorithm on n arguments that recurses with one less argument each
54# iteration will scan n * (n + 1) / 2 arguments, for O(n^2) time. In
55# M4 1.6, this was fixed so that $@ is only scanned once, then
56# back-references are made to information stored about the scan.
57# Thus, n iterations need only scan n arguments, for O(n) time.
58# Additionally, in M4 1.4.x, recursive algorithms did not clean up
59# memory very well, requiring O(n^2) memory rather than O(n) for n
60# iterations.
61#
62# This file is designed to overcome the quadratic nature of $@
63# recursion by writing a variant of m4_foreach that uses m4_for rather
64# than $@ recursion to operate on the list. This involves more macro
65# expansions, but avoids the need to rescan a quadratic number of
66# arguments, making these replacements very attractive for M4 1.4.x.
67# On the other hand, in any version of M4, expanding additional macros
68# costs additional time; therefore, in M4 1.6, where $@ recursion uses
69# fewer macros, these replacements actually pessimize performance.
70# Additionally, the use of $10 to mean the tenth argument violates
71# POSIX; although all versions of m4 1.4.x support this meaning, a
72# future m4 version may switch to take it as the first argument
73# concatenated with a literal 0, so the implementations in this file
74# are not future-proof. Thus, this file is conditionally included as
75# part of m4_init(), only when it is detected that M4 probably has
76# quadratic behavior (ie. it lacks the macro __m4_version__).
77#
78# Please keep this file in sync with m4sugar.m4.
79
80# m4_foreach(VARIABLE, LIST, EXPRESSION)
81# --------------------------------------
82# Expand EXPRESSION assigning each value of the LIST to VARIABLE.
83# LIST should have the form `item_1, item_2, ..., item_n', i.e. the
84# whole list must *quoted*. Quote members too if you don't want them
85# to be expanded.
86#
87# This version minimizes the number of times that $@ is evaluated by
88# using m4_for to generate a boilerplate into VARIABLE then passing $@
89# to that temporary macro. Thus, the recursion is done in m4_for
90# without reparsing any user input, and is not quadratic. For an idea
91# of how this works, note that m4_foreach(i,[1,2],[i]) defines i to be
92# m4_define([$1],[$3])$2[]m4_define([$1],[$4])$2[]m4_popdef([i])
93# then calls i([i],[i],[1],[2]).
94m4_define([m4_foreach],
95[m4_if([$2], [], [], [_$0([$1], [$3], $2)])])
96
97m4_define([_m4_foreach],
98[m4_define([$1], m4_pushdef([$1])_m4_for([$1], [3], [$#], [1],
99 [$0_([1], [2], _m4_defn([$1]))])[m4_popdef([$1])])m4_indir([$1], $@)])
100
101m4_define([_m4_foreach_],
102[[m4_define([$$1], [$$3])$$2[]]])
103
104# m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
105# -----------------------------------------------------------
106# Find the first VAL that SWITCH matches, and expand the corresponding
107# IF-VAL. If there are no matches, expand DEFAULT.
108#
109# Use m4_for to create a temporary macro in terms of a boilerplate
110# m4_if with final cleanup. If $# is even, we have DEFAULT; if it is
111# odd, then rounding the last $# up in the temporary macro is
112# harmless. For example, both m4_case(1,2,3,4,5) and
113# m4_case(1,2,3,4,5,6) result in the intermediate _m4_case being
114# m4_if([$1],[$2],[$3],[$1],[$4],[$5],_m4_popdef([_m4_case])[$6])
115m4_define([m4_case],
116[m4_if(m4_eval([$# <= 2]), [1], [$2],
117[m4_pushdef([_$0], [m4_if(]m4_for([_m4_count], [2], m4_decr([$#]), [2],
118 [_$0_([1], _m4_count, m4_incr(_m4_count))])[_m4_popdef(
119 [_$0])]m4_dquote($m4_eval([($# + 1) & ~1]))[)])_$0($@)])])
120
121m4_define([_m4_case_],
122[[[$$1],[$$2],[$$3],]])
123
124# m4_bmatch(SWITCH, RE1, VAL1, RE2, VAL2, ..., DEFAULT)
125# -----------------------------------------------------
126# m4 equivalent of
127#
128# if (SWITCH =~ RE1)
129# VAL1;
130# elif (SWITCH =~ RE2)
131# VAL2;
132# elif ...
133# ...
134# else
135# DEFAULT
136#
137# We build the temporary macro _m4_b:
138# m4_define([_m4_b], _m4_defn([_m4_bmatch]))_m4_b([$1], [$2], [$3])...
139# _m4_b([$1], [$m-1], [$m])_m4_b([], [], [$m+1]_m4_popdef([_m4_b]))
140# then invoke m4_unquote(_m4_b($@)), for concatenation with later text.
141m4_define([m4_bmatch],
142[m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])],
143 [$#], 1, [m4_fatal([$0: too few arguments: $#: $1])],
144 [$#], 2, [$2],
145 [m4_define([_m4_b], m4_pushdef([_m4_b])[m4_define([_m4_b],
146 _m4_defn([_$0]))]_m4_for([_m4_b], [3], m4_eval([($# + 1) / 2 * 2 - 1]),
147 [2], [_$0_([1], m4_decr(_m4_b), _m4_b)])[_m4_b([], [],]m4_dquote(
148 [$]m4_incr(_m4_b))[_m4_popdef([_m4_b]))])m4_unquote(_m4_b($@))])])
149
150m4_define([_m4_bmatch],
151[m4_if(m4_bregexp([$1], [$2]), [-1], [], [[$3]m4_define([$0])])])
152
153m4_define([_m4_bmatch_],
154[[_m4_b([$$1], [$$2], [$$3])]])
155
156
157# m4_cond(TEST1, VAL1, IF-VAL1, TEST2, VAL2, IF-VAL2, ..., [DEFAULT])
158# -------------------------------------------------------------------
159# Similar to m4_if, except that each TEST is expanded when encountered.
160# If the expansion of TESTn matches the string VALn, the result is IF-VALn.
161# The result is DEFAULT if no tests passed. This macro allows
162# short-circuiting of expensive tests, where it pays to arrange quick
163# filter tests to run first.
164#
165# m4_cond already guarantees either 3*n or 3*n + 1 arguments, 1 <= n.
166# We only have to speed up _m4_cond, by building the temporary _m4_c:
167# m4_define([_m4_c], _m4_defn([m4_unquote]))_m4_c([m4_if(($1), [($2)],
168# [[$3]m4_define([_m4_c])])])_m4_c([m4_if(($4), [($5)],
169# [[$6]m4_define([_m4_c])])])..._m4_c([m4_if(($m-2), [($m-1)],
170# [[$m]m4_define([_m4_c])])])_m4_c([[$m+1]]_m4_popdef([_m4_c]))
171# We invoke m4_unquote(_m4_c($@)), for concatenation with later text.
172m4_define([_m4_cond],
173[m4_define([_m4_c], m4_pushdef([_m4_c])[m4_define([_m4_c],
174 _m4_defn([m4_unquote]))]_m4_for([_m4_c], [2], m4_eval([$# / 3 * 3 - 1]), [3],
175 [$0_(m4_decr(_m4_c), _m4_c, m4_incr(_m4_c))])[_m4_c(]m4_dquote(m4_dquote(
176 [$]m4_eval([$# / 3 * 3 + 1])))[_m4_popdef([_m4_c]))])m4_unquote(_m4_c($@))])
177
178m4_define([_m4_cond_],
179[[_m4_c([m4_if(($$1), [($$2)], [[$$3]m4_define([_m4_c])])])]])
180
181# m4_bpatsubsts(STRING, RE1, SUBST1, RE2, SUBST2, ...)
182# ----------------------------------------------------
183# m4 equivalent of
184#
185# $_ = STRING;
186# s/RE1/SUBST1/g;
187# s/RE2/SUBST2/g;
188# ...
189#
190# m4_bpatsubsts already validated an odd number of arguments; we only
191# need to speed up _m4_bpatsubsts. To avoid nesting, we build the
192# temporary _m4_p:
193# m4_define([_m4_p], [$1])m4_define([_m4_p],
194# m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$2], [$3]))m4_define([_m4_p],
195# m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$4], [$5]))m4_define([_m4_p],...
196# m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$m-1], [$m]))m4_unquote(
197# _m4_defn([_m4_p])_m4_popdef([_m4_p]))
198m4_define([_m4_bpatsubsts],
199[m4_define([_m4_p], m4_pushdef([_m4_p])[m4_define([_m4_p],
200 ]m4_dquote([$]1)[)]_m4_for([_m4_p], [3], [$#], [2], [$0_(m4_decr(_m4_p),
201 _m4_p)])[m4_unquote(_m4_defn([_m4_p])_m4_popdef([_m4_p]))])_m4_p($@)])
202
203m4_define([_m4_bpatsubsts_],
204[[m4_define([_m4_p],
205m4_bpatsubst(m4_dquote(_m4_defn([_m4_p])), [$$1], [$$2]))]])
206
207# m4_shiftn(N, ...)
208# -----------------
209# Returns ... shifted N times. Useful for recursive "varargs" constructs.
210#
211# m4_shiftn already validated arguments; we only need to speed up
212# _m4_shiftn. If N is 3, then we build the temporary _m4_s, defined as
213# ,[$5],[$6],...,[$m]_m4_popdef([_m4_s])
214# before calling m4_shift(_m4_s($@)).
215m4_define([_m4_shiftn],
216[m4_if(m4_incr([$1]), [$#], [], [m4_define([_m4_s],
217 m4_pushdef([_m4_s])_m4_for([_m4_s], m4_eval([$1 + 2]), [$#], [1],
218 [[,]m4_dquote([$]_m4_s)])[_m4_popdef([_m4_s])])m4_shift(_m4_s($@))])])
219
220# m4_do(STRING, ...)
221# ------------------
222# This macro invokes all its arguments (in sequence, of course). It is
223# useful for making your macros more structured and readable by dropping
224# unnecessary dnl's and have the macros indented properly.
225#
226# Here, we use the temporary macro _m4_do, defined as
227# $1[]$2[]...[]$n[]_m4_popdef([_m4_do])
228m4_define([m4_do],
229[m4_if([$#], [0], [],
230 [m4_define([_$0], m4_pushdef([_$0])_m4_for([_$0], [1], [$#], [1],
231 [$_$0[[]]])[_m4_popdef([_$0])])_$0($@)])])
232
233# m4_dquote_elt(ARGS)
234# -------------------
235# Return ARGS as an unquoted list of double-quoted arguments.
236#
237# m4_foreach to the rescue. It's easier to shift off the leading comma.
238m4_define([m4_dquote_elt],
239[m4_shift(m4_foreach([_m4_elt], [$@], [,m4_dquote(_m4_defn([_m4_elt]))]))])
240
241# m4_reverse(ARGS)
242# ----------------
243# Output ARGS in reverse order.
244#
245# Invoke _m4_r($@) with the temporary _m4_r built as
246# [$m], [$m-1], ..., [$2], [$1]_m4_popdef([_m4_r])
247m4_define([m4_reverse],
248[m4_if([$#], [0], [], [$#], [1], [[$1]],
249[m4_define([_m4_r], m4_dquote([$$#])m4_pushdef([_m4_r])_m4_for([_m4_r],
250 m4_decr([$#]), [1], [-1],
251 [[, ]m4_dquote([$]_m4_r)])[_m4_popdef([_m4_r])])_m4_r($@)])])
252
253
254# m4_map(MACRO, LIST)
255# -------------------
256# Invoke MACRO($1), MACRO($2) etc. where $1, $2... are the elements
257# of LIST. $1, $2... must in turn be lists, appropriate for m4_apply.
258#
259# m4_map/m4_map_sep only execute once; the speedup comes in fixing
260# _m4_map. The mismatch in () is intentional, since $1 supplies the
261# opening `(' (but it sure looks odd!). Build the temporary _m4_m:
262# $1, [$3])$1, [$4])...$1, [$m])_m4_popdef([_m4_m])
263m4_define([_m4_map],
264[m4_if([$#], [2], [],
265 [m4_define([_m4_m], m4_pushdef([_m4_m])_m4_for([_m4_m], [3], [$#], [1],
266 [$0_([1], _m4_m)])[_m4_popdef([_m4_m])])_m4_m($@)])])
267
268m4_define([_m4_map_],
269[[$$1, [$$2])]])
270
271# m4_transform(EXPRESSION, ARG...)
272# --------------------------------
273# Expand EXPRESSION([ARG]) for each argument. More efficient than
274# m4_foreach([var], [ARG...], [EXPRESSION(m4_defn([var]))])
275#
276# Invoke the temporary macro _m4_transform, defined as:
277# $1([$2])[]$1([$3])[]...$1([$m])[]_m4_popdef([_m4_transform])
278m4_define([m4_transform],
279[m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
280 [$#], [1], [],
281 [m4_define([_$0], m4_pushdef([_$0])_m4_for([_$0], [2], [$#], [1],
282 [_$0_([1], _$0)])[_m4_popdef([_$0])])_$0($@)])])
283
284m4_define([_m4_transform_],
285[[$$1([$$2])[]]])
286
287# m4_transform_pair(EXPRESSION, [END-EXPR = EXPRESSION], ARG...)
288# --------------------------------------------------------------
289# Perform a pairwise grouping of consecutive ARGs, by expanding
290# EXPRESSION([ARG1], [ARG2]). If there are an odd number of ARGs, the
291# final argument is expanded with END-EXPR([ARGn]).
292#
293# Build the temporary macro _m4_transform_pair, with the $2([$m+1])
294# only output if $# is odd:
295# $1([$3], [$4])[]$1([$5], [$6])[]...$1([$m-1],
296# [$m])[]m4_default([$2], [$1])([$m+1])[]_m4_popdef([_m4_transform_pair])
297m4_define([m4_transform_pair],
298[m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
299 [$#], [1], [m4_fatal([$0: too few arguments: $#: $1])],
300 [$#], [2], [],
301 [$#], [3], [m4_default([$2], [$1])([$3])[]],
302 [m4_define([_$0], m4_pushdef([_$0])_m4_for([_$0], [3],
303 m4_eval([$# / 2 * 2 - 1]), [2], [_$0_([1], _$0, m4_incr(_$0))])_$0_end(
304 [1], [2], [$#])[_m4_popdef([_$0])])_$0($@)])])
305
306m4_define([_m4_transform_pair_],
307[[$$1([$$2], [$$3])[]]])
308
309m4_define([_m4_transform_pair_end],
310[m4_if(m4_eval([$3 & 1]), [1], [[m4_default([$$2], [$$1])([$$3])[]]])])
311
312# m4_join(SEP, ARG1, ARG2...)
313# ---------------------------
314# Produce ARG1SEPARG2...SEPARGn. Avoid back-to-back SEP when a given ARG
315# is the empty string. No expansion is performed on SEP or ARGs.
316#
317# Use a self-modifying separator, since we don't know how many
318# arguments might be skipped before a separator is first printed, but
319# be careful if the separator contains $. m4_foreach to the rescue.
320m4_define([m4_join],
321[m4_pushdef([_m4_sep], [m4_define([_m4_sep], _m4_defn([m4_echo]))])]dnl
322[m4_foreach([_m4_arg], [m4_shift($@)],
323 [m4_ifset([_m4_arg], [_m4_sep([$1])_m4_defn([_m4_arg])])])]dnl
324[_m4_popdef([_m4_sep])])
325
326# m4_joinall(SEP, ARG1, ARG2...)
327# ------------------------------
328# Produce ARG1SEPARG2...SEPARGn. An empty ARG results in back-to-back SEP.
329# No expansion is performed on SEP or ARGs.
330#
331# A bit easier than m4_join. m4_foreach to the rescue.
332m4_define([m4_joinall],
333[[$2]m4_if(m4_eval([$# <= 2]), [1], [],
334 [m4_foreach([_m4_arg], [m4_shift2($@)],
335 [[$1]_m4_defn([_m4_arg])])])])
336
337# m4_list_cmp(A, B)
338# -----------------
339# Compare the two lists of integer expressions A and B.
340#
341# m4_list_cmp takes care of any side effects; we only override
342# _m4_list_cmp_raw, where we can safely expand lists multiple times.
343# First, insert padding so that both lists are the same length; the
344# trailing +0 is necessary to handle a missing list. Next, create a
345# temporary macro to perform pairwise comparisons until an inequality
346# is found. For example, m4_list_cmp([1], [1,2]) creates _m4_cmp as
347# m4_if(m4_eval([($1) != ($3)]), [1], [m4_cmp([$1], [$3])],
348# m4_eval([($2) != ($4)]), [1], [m4_cmp([$2], [$4])],
349# [0]_m4_popdef([_m4_cmp], [_m4_size]))
350# then calls _m4_cmp([1+0], [0], [1], [2+0])
351m4_define([_m4_list_cmp_raw],
352[m4_if([$1], [$2], 0, [m4_pushdef(
353 [_m4_size])_m4_list_cmp($1+0_m4_list_pad(m4_count($1), m4_count($2)),
354 $2+0_m4_list_pad(m4_count($2), m4_count($1)))])])
355
356m4_define([_m4_list_pad],
357[m4_if(m4_eval($1 < $2), [1],
358 [_m4_for([_m4_size], m4_incr([$1]), [$2], [1], [,0])])])
359
360m4_define([_m4_list_cmp],
361[m4_define([_m4_size], m4_eval([$# >> 1]))]dnl
362[m4_define([_m4_cmp], m4_pushdef([_m4_cmp])[m4_if(]_m4_for([_m4_cmp],
363 [1], _m4_size, [1], [$0_(_m4_cmp, m4_eval(_m4_cmp + _m4_size))])[
364 [0]_m4_popdef([_m4_cmp], [_m4_size]))])_m4_cmp($@)])
365
366m4_define([_m4_list_cmp_],
367[[m4_eval([($$1) != ($$2)]), [1], [m4_cmp([$$1], [$$2])],
368]])
369
370# m4_max(EXPR, ...)
371# m4_min(EXPR, ...)
372# -----------------
373# Return the decimal value of the maximum (or minimum) in a series of
374# integer expressions.
375#
376# m4_foreach to the rescue; we only need to replace _m4_minmax. Here,
377# we need a temporary macro to track the best answer so far, so that
378# the foreach expression is tractable.
379m4_define([_m4_minmax],
380[m4_pushdef([_m4_best], m4_eval([$2]))m4_foreach([_m4_arg], [m4_shift2($@)],
381 [m4_define([_m4_best], $1(_m4_best, _m4_defn([_m4_arg])))])]dnl
382[_m4_best[]_m4_popdef([_m4_best])])
383
384# m4_set_add_all(SET, VALUE...)
385# -----------------------------
386# Add each VALUE into SET. This is O(n) in the number of VALUEs, and
387# can be faster than calling m4_set_add for each VALUE.
388#
389# m4_foreach to the rescue. If no deletions have occurred, then avoid
390# the speed penalty of m4_set_add.
391m4_define([m4_set_add_all],
392[m4_if([$#], [0], [], [$#], [1], [],
393 [m4_define([_m4_set_size($1)], m4_eval(m4_set_size([$1])
394 + m4_len(m4_foreach([_m4_arg], [m4_shift($@)],
395 m4_ifdef([_m4_set_cleanup($1)],
396 [[m4_set_add([$1], _m4_defn([_m4_arg]))]],
397 [[m4_ifdef([_m4_set([$1],]_m4_defn([_m4_arg])[)], [],
398 [m4_define([_m4_set([$1],]_m4_defn([_m4_arg])[)],
399 [1])m4_pushdef([_m4_set([$1])],
400 _m4_defn([_m4_arg]))-])]])))))])])
Note: See TracBrowser for help on using the repository browser.