]> git.saurik.com Git - bison.git/blame - data/bison.m4
bison.m4: support b4_*_if macros whose name differ from their variable
[bison.git] / data / bison.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
90b9908d
PB
2
3# Language-independent M4 Macros for Bison.
7d424de1 4
34136e65 5# Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
90b9908d 6
f16b0819 7# This program is free software: you can redistribute it and/or modify
90b9908d 8# it under the terms of the GNU General Public License as published by
f16b0819 9# the Free Software Foundation, either version 3 of the License, or
90b9908d 10# (at your option) any later version.
f16b0819 11#
90b9908d
PB
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
f16b0819 16#
90b9908d 17# You should have received a copy of the GNU General Public License
f16b0819 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
90b9908d
PB
19
20
21## ---------------- ##
22## Identification. ##
23## ---------------- ##
24
a9ce3f54
AD
25# b4_copyright(TITLE, [YEARS])
26# ----------------------------
27# If YEARS are not defined, use b4_copyright_years.
90b9908d
PB
28m4_define([b4_copyright],
29[b4_comment([A Bison parser, made by GNU Bison b4_version.])
30
31b4_comment([$1
32
a9ce3f54
AD
33]m4_dquote(m4_text_wrap([Copyright (C)
34]m4_ifval([$2], [[$2]], [m4_defn([b4_copyright_years])])[
35Free Software Foundation, Inc.]))[
90b9908d 36
f16b0819 37This program is free software: you can redistribute it and/or modify
90b9908d 38it under the terms of the GNU General Public License as published by
f16b0819
PE
39the Free Software Foundation, either version 3 of the License, or
40(at your option) any later version.
90b9908d
PB
41
42This program is distributed in the hope that it will be useful,
43but WITHOUT ANY WARRANTY; without even the implied warranty of
44MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45GNU General Public License for more details.
46
47You should have received a copy of the GNU General Public License
f16b0819 48along with this program. If not, see <http://www.gnu.org/licenses/>.])
90b9908d
PB
49
50b4_comment([As a special exception, you may create a larger work that contains
51part or all of the Bison parser skeleton and distribute that work
52under terms of your choice, so long as that work isn't itself a
53parser generator using the skeleton or a modified version thereof
54as a parser skeleton. Alternatively, if you modify or redistribute
55the parser skeleton itself, you may (at your option) remove this
56special exception, which will cause the skeleton and the resulting
57Bison output files to be licensed under the GNU General Public
58License without this special exception.
59
60This special exception was added by the Free Software Foundation in
61version 2.2 of Bison.])])
62
63
08af01c2
JD
64## ---------------- ##
65## Error handling. ##
66## ---------------- ##
67
7dc4a694
JD
68# The following error handling macros print error directives that should not
69# become arguments of other macro invocations since they would likely then be
70# mangled. Thus, they print to stdout directly.
71
72# b4_cat(TEXT)
73# ------------
74# Write TEXT to stdout. Precede the final newline with an @ so that it's
75# escaped. For example:
76#
77# b4_cat([[@complain(invalid input@)]])
78m4_define([b4_cat],
79[m4_syscmd([cat <<'_m4eof'
80]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@
66f0441d
JD
81_m4eof
82])dnl
7dc4a694
JD
83m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])])
84
c4e3a162
AD
85# b4_error(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
86# -------------------------------------------------------
87# Write @KIND(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
7dc4a694
JD
88#
89# For example:
90#
c4e3a162
AD
91# b4_error([[complain]], [[input.y:2.3]], [[input.y:5.4]],
92# [[invalid %s]], [[foo]])
08af01c2 93m4_define([b4_error],
0505df0c 94[b4_cat([[@complain][(]$1[@,]$2[@,]$3[@,]$4[]]dnl
7dc4a694
JD
95[m4_if([$#], [4], [],
96 [m4_foreach([b4_arg],
97 m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))),
98 [[@,]b4_arg])])[@)]])])
3fc65ead 99
08af01c2 100# b4_warn(FORMAT, [ARG1], [ARG2], ...)
7eb8a0bc 101# ------------------------------------
7dc4a694
JD
102# Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
103#
104# For example:
105#
4aa9d1ff 106# b4_warn([[invalid value for '%s': %s]], [[foo]], [[3]])
08af01c2
JD
107#
108# As a simple test suite, this:
109#
7dc4a694 110# m4_divert(-1)
08af01c2
JD
111# m4_define([asdf], [ASDF])
112# m4_define([fsa], [FSA])
113# m4_define([fdsa], [FDSA])
c4e3a162
AD
114# b4_warn_at([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
115# b4_warn_at([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
116# b4_warn_at()
117# b4_warn_at(1)
118# b4_warn_at(1, 2)
08af01c2 119#
7dc4a694 120# Should produce this without newlines:
08af01c2 121#
c4e3a162
AD
122# @warn_at([asdf), asdf]@,@,@,[fsa), fsa]@,[fdsa), fdsa]@)
123# @warn(asdf), asdf@,@,@,fsa), fsa@,fdsa), fdsa@)
08af01c2 124# @warn(@)
08af01c2
JD
125# @warn(1@)
126# @warn(1@,2@)
127m4_define([b4_warn],
c4e3a162 128[b4_error([[warn]], [], [], $@)])
08af01c2 129
3fc65ead 130# b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
7eb8a0bc 131# ---------------------------------------------------
7dc4a694
JD
132# Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
133#
134# For example:
135#
136# b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
3fc65ead 137m4_define([b4_warn_at],
c4e3a162 138[b4_error([[warn]], $@)])
3fc65ead 139
08af01c2 140# b4_complain(FORMAT, [ARG1], [ARG2], ...)
7eb8a0bc 141# ----------------------------------------
c4e3a162 142# Bounce to b4_complain_at.
08af01c2 143#
7dc4a694 144# See b4_warn example.
08af01c2 145m4_define([b4_complain],
c4e3a162 146[b4_error([[complain]], [], [], $@)])
08af01c2 147
3fc65ead 148# b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
7eb8a0bc 149# -------------------------------------------------------
7dc4a694
JD
150# Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
151#
152# See b4_warn_at example.
3fc65ead 153m4_define([b4_complain_at],
c4e3a162 154[b4_error([[complain]], $@)])
3fc65ead 155
08af01c2 156# b4_fatal(FORMAT, [ARG1], [ARG2], ...)
7eb8a0bc 157# -------------------------------------
c4e3a162 158# Bounce to b4_fatal_at.
08af01c2 159#
7dc4a694 160# See b4_warn example.
08af01c2 161m4_define([b4_fatal],
c4e3a162 162[b4_error([[fatal]], [], [], $@)dnl
7dc4a694 163m4_exit(1)])
08af01c2 164
3fc65ead 165# b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
7eb8a0bc 166# ----------------------------------------------------
7dc4a694
JD
167# Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
168#
169# See b4_warn_at example.
3fc65ead 170m4_define([b4_fatal_at],
c4e3a162 171[b4_error([[fatal]], $@)dnl
7dc4a694 172m4_exit(1)])
3fc65ead 173
08af01c2 174
90b9908d
PB
175## ------------ ##
176## Data Types. ##
177## ------------ ##
178
179# b4_ints_in(INT1, INT2, LOW, HIGH)
180# ---------------------------------
181# Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
182m4_define([b4_ints_in],
183[m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
184
185
bd187d7b
AD
186# b4_subtract(LHS, RHS)
187# ---------------------
188# Evaluate LHS - RHS if they are integer literals, otherwise expand
189# to (LHS) - (RHS).
190m4_define([b4_subtract],
191[m4_bmatch([$1$2], [^[0123456789]*$],
192 [m4_eval([$1 - $2])],
193 [($1) - ($2)])])
194
710c4a65
AD
195# b4_join(ARG1, ...)
196# _b4_join(ARG1, ...)
e0c653e7
AD
197# -------------------
198# Join with comma, skipping empty arguments.
710c4a65
AD
199# b4_join calls itself recursively until it sees the first non-empty
200# argument, then calls _b4_join which prepends each non-empty argument
e0c653e7 201# with a comma.
710c4a65 202m4_define([b4_join],
e0c653e7
AD
203[m4_if([$#$1],
204 [1], [],
205 [m4_ifval([$1],
206 [$1[]_$0(m4_shift($@))],
207 [$0(m4_shift($@))])])])
208
710c4a65 209# _b4_join(ARGS1, ...)
e0c653e7 210# --------------------
710c4a65 211m4_define([_b4_join],
e0c653e7
AD
212[m4_if([$#$1],
213 [1], [],
214 [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])])
215
216
217
90b9908d 218
ba206cf4
AD
219# b4_integral_parser_tables_map(MACRO)
220# -------------------------------------
0991e29b
AD
221# Map MACRO on all the integral tables. MACRO is expected to have
222# the signature MACRO(TABLE-NAME, CONTENT, COMMENT).
ba206cf4 223m4_define([b4_integral_parser_tables_map],
0991e29b
AD
224[$1([pact], [b4_pact],
225 [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
226STATE-NUM.]])
227
228$1([defact], [b4_defact],
110ef36a
JD
229 [[YYDEFACT[S] -- default reduction number in state S. Performed when
230YYTABLE does not specify something else to do. Zero means the default
231is an error.]])
0991e29b
AD
232
233$1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]])
234
235$1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]])
236
237$1([table], [b4_table],
238 [[YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
239positive, shift that token. If negative, reduce the rule which
aa0cb40d 240number is the opposite. If YYTABLE_NINF, syntax error.]])
0991e29b
AD
241
242$1([check], [b4_check])
243
244$1([stos], [b4_stos],
245 [[STOS_[STATE-NUM] -- The (internal number of the) accessing
246symbol of state STATE-NUM.]])
247
248$1([r1], [b4_r1],
249 [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]])
250
251$1([r2], [b4_r2],
252 [[YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.]])
253])
254
255
ba206cf4
AD
256# b4_parser_tables_declare
257# b4_parser_tables_define
258# ------------------------
0991e29b 259# Define/declare the (deterministic) parser tables.
ba206cf4
AD
260m4_define([b4_parser_tables_declare],
261[b4_integral_parser_tables_map([b4_integral_parser_table_declare])])
0991e29b 262
ba206cf4
AD
263m4_define([b4_parser_tables_define],
264[b4_integral_parser_tables_map([b4_integral_parser_table_define])])
0991e29b
AD
265
266
267
90b9908d
PB
268## ------------------ ##
269## Decoding options. ##
270## ------------------ ##
271
272# b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
273# -----------------------------------
274# Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
275m4_define([b4_flag_if],
276[m4_case(b4_$1_flag,
277 [0], [$3],
e9690142
JD
278 [1], [$2],
279 [m4_fatal([invalid $1 value: ]$1)])])
90b9908d
PB
280
281
282# b4_define_flag_if(FLAG)
283# -----------------------
284# Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
285# value of the Boolean FLAG.
286m4_define([b4_define_flag_if],
287[_b4_define_flag_if($[1], $[2], [$1])])
288
289# _b4_define_flag_if($1, $2, FLAG)
290# --------------------------------
258cddbc
AD
291# Work around the impossibility to define macros inside macros,
292# because issuing `[$1]' is not possible in M4. GNU M4 should provide
293# $$1 a la M5/TeX.
90b9908d
PB
294m4_define([_b4_define_flag_if],
295[m4_if([$1$2], $[1]$[2], [],
296 [m4_fatal([$0: Invalid arguments: $@])])dnl
297m4_define([b4_$3_if],
298 [b4_flag_if([$3], [$1], [$2])])])
299
300
301# b4_FLAG_if(IF-TRUE, IF-FALSE)
302# -----------------------------
303# Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
e9690142
JD
304b4_define_flag_if([defines]) # Whether headers are requested.
305b4_define_flag_if([glr]) # Whether a GLR parser is requested.
306b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
258cddbc 307b4_define_flag_if([token_table]) # Whether yytoken_table is demanded.
e9690142 308b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
90b9908d
PB
309
310
cf6fb222
AD
311## --------- ##
312## Symbols. ##
313## --------- ##
314
e3990e3c
AD
315# In order to unify the handling of the various aspects of symbols
316# (tag, type_name, whether terminal, etc.), bison.exe defines one
317# macro per (token, field), where field can has_id, id, etc.: see
318# src/output.c:prepare_symbols_definitions().
319#
320# The following macros provide access to these values.
321
cf6fb222
AD
322# b4_symbol_(NUM, FIELD)
323# ----------------------
324# Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
325# undefined.
326m4_define([b4_symbol_],
327[m4_indir([b4_symbol($1, $2)])])
328
329
330# b4_symbol(NUM, FIELD)
331# ---------------------
332# Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
333# undefined. If FIELD = id, prepend the prefix.
334m4_define([b4_symbol],
335[m4_case([$2],
2a6b66c5 336 [id], [m4_do([b4_percent_define_get([api.token.prefix])],
cf6fb222
AD
337 [b4_symbol_([$1], [id])])],
338 [b4_symbol_($@)])])
339
340
341# b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
342# -------------------------------------------
343# If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE.
344# Otherwise an error.
345m4_define([b4_symbol_if],
346[m4_case(b4_symbol([$1], [$2]),
347 [1], [$3],
348 [0], [$4],
349 [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])])
350
351
3bb21113
AD
352# b4_symbol_action_location(SYMBOL-NUM, KIND)
353# -------------------------------------------
354# Report the location of the KIND action as FILE:LINE.
355m4_define([b4_symbol_action_location],
356[b4_symbol([$1], [$2_file]):b4_syncline([b4_symbol([$1], [$2_line])])])
357
358
cf6fb222
AD
359# b4_symbol_action(SYMBOL-NUM, KIND)
360# ----------------------------------
361# Run the action KIND (destructor or printer) for SYMBOL-NUM.
362# Same as in C, but using references instead of pointers.
363m4_define([b4_symbol_action],
364[b4_symbol_if([$1], [has_$2],
4323e0da
AD
365[b4_dollar_pushdef([(*yyvaluep)],
366 b4_symbol_if([$1], [has_type],
367 [m4_dquote(b4_symbol([$1], [type]))]),
368 [(*yylocationp)])dnl
d507251c 369 b4_symbol_case_([$1])[]dnl
cf6fb222
AD
370b4_syncline([b4_symbol([$1], [$2_line])], ["b4_symbol([$1], [$2_file])"])
371 b4_symbol([$1], [$2])
372b4_syncline([@oline@], [@ofile@])
373 break;
374
4323e0da 375b4_dollar_popdef[]dnl
cf6fb222
AD
376])])
377
378
379# b4_symbol_destructor(SYMBOL-NUM)
380# b4_symbol_printer(SYMBOL-NUM)
381# --------------------------------
382m4_define([b4_symbol_destructor], [b4_symbol_action([$1], [destructor])])
383m4_define([b4_symbol_printer], [b4_symbol_action([$1], [printer])])
384
385
386# b4_symbol_case_(SYMBOL-NUM)
387# ---------------------------
388# Issue a "case NUM" for SYMBOL-NUM.
389m4_define([b4_symbol_case_],
390[ case b4_symbol([$1], [number]): // b4_symbol([$1], [tag])
391])
392
393
394# b4_symbol_foreach(MACRO)
395# ------------------------
396# Invoke MACRO(SYMBOL-NUM) for each SYMBOL-NUM.
397m4_define([b4_symbol_foreach],
398 [m4_map([$1], m4_defn([b4_symbol_numbers]))])
e3990e3c
AD
399
400# b4_symbol_map(MACRO)
401# --------------------
402# Return a list (possibly empty elements) of MACRO invoked for each
403# SYMBOL-NUM.
404m4_define([b4_symbol_map],
405[m4_map_args_sep([$1(], [)], [,], b4_symbol_numbers)])
406
407
408# b4_token_visible_if(NUM, IF-TRUE, IF-FALSE)
409# -------------------------------------------
410# Whether NUM denotes a token that has an exported definition (i.e.,
411# shows in enum yytokentype).
412m4_define([b4_token_visible_if],
413[b4_symbol_if([$1], [is_token],
414 [b4_symbol_if([$1], [has_id], [$2], [$3])],
415 [$3])])
416
417# b4_token_has_definition(NUM)
418# ----------------------------
419# 1 if NUM is visible, nothing otherwise.
420m4_define([b4_token_has_definition],
421[b4_token_visible_if([$1], [1])])
422
423# b4_any_token_visible_if([IF-TRUE], [IF-FALSE])
424# ----------------------------------------------
425# Whether there is a token that needs to be defined.
426m4_define([b4_any_token_visible_if],
427[m4_ifval(b4_symbol_foreach([b4_token_has_definition]),
428 [$1], [$2])])
429
430
431# b4_token_format(FORMAT, NUM)
432# ----------------------------
433m4_define([b4_token_format],
434[b4_token_visible_if([$2],
435[m4_quote(m4_format([$1],
436 [b4_symbol([$2], [id])],
437 [b4_symbol([$2], [user_number])]))])])
cf6fb222
AD
438
439
440## ------- ##
441## Types. ##
442## ------- ##
443
444# b4_type_action_(NUMS)
445# ---------------------
446# Run actions for the symbol NUMS that all have the same type-name.
447# Skip NUMS that have no type-name.
448m4_define([b4_type_action_],
449[b4_symbol_if([$1], [has_type],
450[m4_map([b4_symbol_case_], [$@])[]dnl
451 b4_dollar_dollar([b4_symbol([$1], [number])],
452 [b4_symbol([$1], [tag])],
453 [b4_symbol([$1], [type])]);
e9690142 454 break;
cf6fb222
AD
455
456])])
457
458# b4_type_foreach(MACRO)
459# ----------------------
460# Invoke MACRO(SYMBOL-NUMS) for each set of SYMBOL-NUMS for each type set.
461m4_define([b4_type_foreach],
462 [m4_map([$1], m4_defn([b4_type_names]))])
463
464
465
90b9908d
PB
466## ----------- ##
467## Synclines. ##
468## ----------- ##
469
470# b4_basename(NAME)
471# -----------------
472# Similar to POSIX basename; the differences don't matter here.
473# Beware that NAME is not evaluated.
474m4_define([b4_basename],
475[m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
476
477
478# b4_syncline(LINE, FILE)
479# -----------------------
480m4_define([b4_syncline],
fad814bd
AD
481[b4_flag_if([synclines],
482[b4_sync_end([__line__], [b4_basename(m4_quote(__file__))])
90b9908d
PB
483b4_sync_start([$1], [$2])])])
484
485m4_define([b4_sync_end], [b4_comment([Line $1 of $2])])
486m4_define([b4_sync_start], [b4_comment([Line $1 of $2])])
487
488# b4_user_code(USER-CODE)
489# -----------------------
490# Emit code from the user, ending it with synclines.
491m4_define([b4_user_code],
492[$1
493b4_syncline([@oline@], [@ofile@])])
494
495
496# b4_define_user_code(MACRO)
497# --------------------------
498# From b4_MACRO, build b4_user_MACRO that includes the synclines.
499m4_define([b4_define_user_code],
500[m4_define([b4_user_$1],
501[b4_user_code([b4_$1])])])
502
503
504# b4_user_actions
505# b4_user_initial_action
506# b4_user_post_prologue
136a0f76 507# b4_user_pre_prologue
90b9908d
PB
508# b4_user_stype
509# ----------------------
510# Macros that issue user code, ending with synclines.
511b4_define_user_code([actions])
512b4_define_user_code([initial_action])
513b4_define_user_code([post_prologue])
136a0f76 514b4_define_user_code([pre_prologue])
90b9908d
PB
515b4_define_user_code([stype])
516
1c7b7e1d 517
9611cfa2 518# b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
c6abeab1
JD
519# -----------------------------------------------------
520# Complain if any name of type WHAT is used by the user (as recorded in
521# USER-LIST) but is not used by Bison (as recorded by macros in the
522# namespace BISON-NAMESPACE).
7eb8a0bc 523#
f8e7258f
JD
524# USER-LIST must expand to a list specifying all user occurrences of all names
525# of type WHAT. Each item in the list must be a triplet specifying one
6afc30cc
JD
526# occurrence: name, start boundary, and end boundary. Empty string names are
527# fine. An empty list is fine.
7eb8a0bc 528#
a4e25e1d 529# For example, to define b4_foo_user_names to be used for USER-LIST with three
6afc30cc 530# name occurrences and with correct quoting:
8e0a5e9e 531#
a4e25e1d 532# m4_define([b4_foo_user_names],
7eb8a0bc
JD
533# [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]],
534# [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]],
535# [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]])
1c7b7e1d 536#
9611cfa2
JD
537# The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT
538# is used by Bison (in the front-end or in the skeleton). Empty string names
539# are fine, but it would be ugly for Bison to actually use one.
6afc30cc 540#
9611cfa2
JD
541# For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that
542# the names bar and baz are used by Bison:
1c7b7e1d 543#
9611cfa2
JD
544# m4_define([b4_foo_bison_names(bar)])
545# m4_define([b4_foo_bison_names(baz)])
1c7b7e1d 546#
6afc30cc 547# To invoke b4_check_user_names with TYPE foo, with USER-LIST
9611cfa2
JD
548# b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
549# quoting:
1c7b7e1d 550#
a4e25e1d 551# b4_check_user_names([[foo]], [b4_foo_user_names],
9611cfa2 552# [[b4_foo_bison_names]])
6afc30cc
JD
553m4_define([b4_check_user_names],
554[m4_foreach([b4_occurrence], $2,
555[m4_pushdef([b4_occurrence], b4_occurrence)dnl
556m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
557m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
558m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl
a4e25e1d 559m4_ifndef($3[(]m4_quote(b4_user_name)[)],
c6abeab1 560 [b4_complain_at([b4_start], [b4_end],
4aa9d1ff 561 [[%s '%s' is not used]],
c6abeab1 562 [$1], [b4_user_name])])[]dnl
6afc30cc
JD
563m4_popdef([b4_occurrence])dnl
564m4_popdef([b4_user_name])dnl
565m4_popdef([b4_start])dnl
566m4_popdef([b4_end])dnl
567])])
568
67501061
AD
569
570
571## --------------------- ##
572## b4_percent_define_*. ##
573## --------------------- ##
574
575
576# b4_percent_define_use(VARIABLE)
577# -------------------------------
578# Declare that VARIABLE was used.
579m4_define([b4_percent_define_use],
580[m4_define([b4_percent_define_bison_variables(]$1[)])dnl
581])
582
7789b6e3
AD
583# b4_percent_define_get(VARIABLE, [DEFAULT])
584# ------------------------------------------
585# Mimic muscle_percent_define_get in ../src/muscle-tab.h. That is, if
586# the %define variable VARIABLE is defined, emit its value. Contrary
587# to its C counterpart, return DEFAULT otherwise. Also, record
9611cfa2
JD
588# Bison's usage of VARIABLE by defining
589# b4_percent_define_bison_variables(VARIABLE).
a4e25e1d
JD
590#
591# For example:
592#
593# b4_percent_define_get([[foo]])
594m4_define([b4_percent_define_get],
67501061 595[b4_percent_define_use([$1])dnl
7789b6e3
AD
596m4_ifdef([b4_percent_define(]$1[)],
597 [m4_indir([b4_percent_define(]$1[)])],
598 [$2])])
a4e25e1d 599
35b8730d
JD
600# b4_percent_define_get_loc(VARIABLE)
601# -----------------------------------
416a9da6 602# Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly. That is,
35b8730d
JD
603# if the %define variable VARIABLE is undefined, complain fatally since that's
604# a Bison or skeleton error. Otherwise, return its definition location in a
605# form approriate for the first two arguments of b4_warn_at, b4_complain_at, or
606# b4_fatal_at. Don't record this as a Bison usage of VARIABLE as there's no
607# reason to suspect that the user-supplied value has yet influenced the output.
608#
609# For example:
610#
611# b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
612m4_define([b4_percent_define_get_loc],
613[m4_ifdef([b4_percent_define_loc(]$1[)],
614 [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl
615b4_loc[]dnl
616m4_popdef([b4_loc])],
4a9cd8f2 617 [b4_fatal([[b4_percent_define_get_loc: undefined %%define variable '%s']], [$1])])])
35b8730d
JD
618
619# b4_percent_define_get_syncline(VARIABLE)
620# ----------------------------------------
416a9da6 621# Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly.
35b8730d
JD
622# That is, if the %define variable VARIABLE is undefined, complain fatally
623# since that's a Bison or skeleton error. Otherwise, return its definition
624# location as a b4_syncline invocation. Don't record this as a Bison usage of
625# VARIABLE as there's no reason to suspect that the user-supplied value has yet
626# influenced the output.
627#
628# For example:
629#
630# b4_percent_define_get_syncline([[foo]])
631m4_define([b4_percent_define_get_syncline],
632[m4_ifdef([b4_percent_define_syncline(]$1[)],
633 [m4_indir([b4_percent_define_syncline(]$1[)])],
4a9cd8f2 634 [b4_fatal([[b4_percent_define_get_syncline: undefined %%define variable '%s']], [$1])])])
35b8730d 635
592d0b1e
PB
636# b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
637# ------------------------------------------------------
416a9da6 638# Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly. That is,
f124d423 639# if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
9611cfa2
JD
640# IF-FALSE. Also, record Bison's usage of VARIABLE by defining
641# b4_percent_define_bison_variables(VARIABLE).
642#
643# For example:
644#
645# b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
592d0b1e
PB
646m4_define([b4_percent_define_ifdef],
647[m4_ifdef([b4_percent_define(]$1[)],
e9690142
JD
648 [b4_percent_define_use([$1])$2],
649 [$3])])
592d0b1e 650
b47b6ff7 651
b47b6ff7
AD
652## --------- ##
653## Options. ##
654## --------- ##
655
656
592d0b1e
PB
657# b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
658# --------------------------------------------------------
416a9da6 659# Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly. That is,
9611cfa2
JD
660# if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE.
661# If it is defined to "false", expand IF-FALSE. Complain if it is undefined
662# (a Bison or skeleton error since the default value should have been set
663# already) or defined to any other value (possibly a user error). Also, record
664# Bison's usage of VARIABLE by defining
665# b4_percent_define_bison_variables(VARIABLE).
666#
667# For example:
668#
669# b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
592d0b1e
PB
670m4_define([b4_percent_define_flag_if],
671[b4_percent_define_ifdef([$1],
9611cfa2
JD
672 [m4_case(b4_percent_define_get([$1]),
673 [], [$2], [true], [$2], [false], [$3],
35b8730d 674 [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
4aa9d1ff 675 [[invalid value for %%define Boolean variable '%s']],
35b8730d
JD
676 [$1])],
677 [[b4_percent_define_flag_if($1)]])])],
4a9cd8f2 678 [b4_fatal([[b4_percent_define_flag_if: undefined %%define variable '%s']], [$1])])])
592d0b1e 679
b3a2272a 680
a4e25e1d
JD
681# b4_percent_define_default(VARIABLE, DEFAULT)
682# --------------------------------------------
416a9da6 683# Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly. That is,
9611cfa2
JD
684# if the %define variable VARIABLE is undefined, set its value to DEFAULT.
685# Don't record this as a Bison usage of VARIABLE as there's no reason to
686# suspect that the value has yet influenced the output.
a4e25e1d
JD
687#
688# For example:
689#
690# b4_percent_define_default([[foo]], [[default value]])
691m4_define([b4_percent_define_default],
692[m4_ifndef([b4_percent_define(]$1[)],
9611cfa2
JD
693 [m4_define([b4_percent_define(]$1[)], [$2])dnl
694 m4_define([b4_percent_define_loc(]$1[)],
92822aff
JD
695 [[[[<skeleton default value>:-1.-1]],
696 [[<skeleton default value>:-1.-1]]]])dnl
697 m4_define([b4_percent_define_syncline(]$1[)], [[]])])])
6afc30cc 698
b3a2272a 699
62cb570a
AD
700# b4_percent_define_if_define(NAME, [VARIABLE = NAME])
701# ----------------------------------------------------
702# Define b4_NAME_if that executes its $1 or $2 depending whether
4f646c37
AD
703# VARIABLE was %defined. The characters `.' and `-' in VARIABLE are mapped
704# to `_'.
b3a2272a 705m4_define([b4_percent_define_if_define_],
4f646c37 706[m4_define(m4_bpatsubst([b4_$1_if], [[-.]], [_]),
62cb570a
AD
707 [b4_percent_define_flag_if(m4_default([$2], [$1]),
708 [$3], [$4])])])
b3a2272a 709m4_define([b4_percent_define_if_define],
62cb570a
AD
710[b4_percent_define_default([m4_default([$2], [$1])], [[false]])
711b4_percent_define_if_define_([$1], [$2], $[1], $[2])])
b3a2272a
AD
712
713
d782395d
JD
714# b4_percent_define_check_values(VALUES)
715# --------------------------------------
416a9da6 716# Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly
d782395d
JD
717# except that the VALUES structure is more appropriate for M4. That is, VALUES
718# is a list of sublists of strings. For each sublist, the first string is the
719# name of a %define variable, and all remaining strings in that sublist are the
720# valid values for that variable. Complain if such a variable is undefined (a
721# Bison error since the default value should have been set already) or defined
722# to any other value (possibly a user error). Don't record this as a Bison
723# usage of the variable as there's no reason to suspect that the value has yet
724# influenced the output.
725#
726# For example:
727#
728# b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
729# [[[[bar]], [[bar-value1]]]])
730m4_define([b4_percent_define_check_values],
731[m4_foreach([b4_sublist], m4_quote($@),
732 [_b4_percent_define_check_values(b4_sublist)])])
733
734m4_define([_b4_percent_define_check_values],
735[m4_ifdef([b4_percent_define(]$1[)],
736 [m4_pushdef([b4_good_value], [0])dnl
737 m4_if($#, 1, [],
738 [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
739 [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
740 [m4_define([b4_good_value], [1])])])])dnl
741 m4_if(b4_good_value, [0],
35b8730d 742 [b4_complain_at(b4_percent_define_get_loc([$1]),
4aa9d1ff 743 [[invalid value for %%define variable '%s': '%s']],
d782395d 744 [$1],
f4909773
JD
745 m4_dquote(m4_indir([b4_percent_define(]$1[)])))
746 m4_foreach([b4_value], m4_dquote(m4_shift($@)),
c6c8de16 747 [b4_error([[note]], b4_percent_define_get_loc([$1]), []
4aa9d1ff 748 [[accepted value: '%s']],
f4909773 749 m4_dquote(b4_value))])])dnl
d782395d 750 m4_popdef([b4_good_value])],
4a9cd8f2 751 [b4_fatal([[b4_percent_define_check_values: undefined %%define variable '%s']], [$1])])])
d782395d 752
a4e25e1d 753# b4_percent_code_get([QUALIFIER])
6afc30cc
JD
754# --------------------------------
755# If any %code blocks for QUALIFIER are defined, emit them beginning with a
756# comment and ending with synclines and a newline. If QUALIFIER is not
592d0b1e 757# specified or empty, do this for the unqualified %code blocks. Also, record
9611cfa2
JD
758# Bison's usage of QUALIFIER (if specified) by defining
759# b4_percent_code_bison_qualifiers(QUALIFIER).
a4e25e1d
JD
760#
761# For example, to emit any unqualified %code blocks followed by any %code
762# blocks for the qualifier foo:
763#
764# b4_percent_code_get
765# b4_percent_code_get([[foo]])
766m4_define([b4_percent_code_get],
592d0b1e 767[m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
9611cfa2 768m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
6afc30cc
JD
769m4_ifdef(b4_macro_name,
770[b4_comment([m4_if([$#], [0], [[Unqualified %code]],
42ee26bb 771 [["%code ]$1["]])[ blocks.]])
a4e25e1d
JD
772b4_user_code([m4_indir(b4_macro_name)])
773])dnl
6afc30cc
JD
774m4_popdef([b4_macro_name])])
775
592d0b1e
PB
776# b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
777# -----------------------------------------------------
778# If any %code blocks for QUALIFIER (or unqualified %code blocks if
779# QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
9611cfa2
JD
780# Also, record Bison's usage of QUALIFIER (if specified) by defining
781# b4_percent_code_bison_qualifiers(QUALIFIER).
592d0b1e
PB
782m4_define([b4_percent_code_ifdef],
783[m4_ifdef([b4_percent_code(]$1[)],
9611cfa2 784 [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
e9690142 785 [$3])])
592d0b1e 786
6afc30cc 787
ad597a78
AD
788## ------------------ ##
789## Common variables. ##
790## ------------------ ##
791
5679f311
AD
792# Default values for %define.
793# ---------------------------
2a6b66c5
AD
794# If the api.token.prefix, it is empty.
795m4_percent_define_default([[api.token.prefix]], [[]])
5679f311 796
ad597a78
AD
797# b4_parse_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
798# b4_parse_trace_if([IF-DEBUG-TRACES-ARE-ENABLED], [IF-NOT])
799# b4_lex_symbol_if([IF-YYLEX-RETURNS-A-COMPLETE-SYMBOL], [IF-NOT])
800# b4_variant_if([IF-VARIANT-ARE-USED], [IF-NOT])
801# ----------------------------------------------
802b4_percent_define_if_define([lex_symbol])
803b4_percent_define_if_define([locations]) # Whether locations are tracked.
804b4_percent_define_if_define([parse.assert])
805b4_percent_define_if_define([parse.trace])
806b4_percent_define_if_define([variant])
807
808
809# b4_error_verbose_if([IF-ERRORS-ARE-VERBOSE], [IF-NOT])
810# ------------------------------------------------------
811# Map %define parse.error "(simple|verbose)" to b4_error_verbose_if and
812# b4_error_verbose_flag.
813b4_percent_define_default([[parse.error]], [[simple]])
814b4_percent_define_check_values([[[[parse.error]],
815 [[simple]], [[verbose]]]])
816m4_define([b4_error_verbose_flag],
82db7cdb
JD
817 [m4_case(b4_percent_define_get([[parse.error]]),
818 [simple], [[0]],
819 [verbose], [[1]])])
ad597a78 820b4_define_flag_if([error_verbose])
5679f311 821
f1246164
AD
822# yytoken_table is needed to support verbose errors.
823b4_error_verbose_if([m4_define([b4_token_table_flag], [1])])
824
5679f311 825
9611cfa2
JD
826## ----------------------------------------------------------- ##
827## After processing the skeletons, check that all the user's ##
828## %define variables and %code qualifiers were used by Bison. ##
829## ----------------------------------------------------------- ##
6afc30cc 830
a4e25e1d
JD
831m4_define([b4_check_user_names_wrap],
832[m4_ifdef([b4_percent_]$1[_user_]$2[s],
833 [b4_check_user_names([[%]$1 $2],
834 [b4_percent_]$1[_user_]$2[s],
9611cfa2 835 [[b4_percent_]$1[_bison_]$2[s]])])])
6afc30cc 836
882a1fbf 837m4_wrap_lifo([
a4e25e1d
JD
838b4_check_user_names_wrap([[define]], [[variable]])
839b4_check_user_names_wrap([[code]], [[qualifier]])
6afc30cc 840])
2a824fae
AD
841
842
843## ---------------- ##
844## Default values. ##
845## ---------------- ##
846
847# m4_define_default([b4_lex_param], []) dnl breaks other skeletons
848m4_define_default([b4_pre_prologue], [])
849m4_define_default([b4_post_prologue], [])
850m4_define_default([b4_epilogue], [])
851m4_define_default([b4_parse_param], [])
852
853# The initial column and line.
854m4_define_default([b4_location_initial_column], [1])
855m4_define_default([b4_location_initial_line], [1])
32ae07ef
AD
856
857# Sanity checks.
858b4_percent_define_ifdef([api.prefix],
859[m4_ifdef([b4_prefix],
860[b4_complain_at(b4_percent_define_get_loc([api.prefix]),
861 [['%s' and '%s' cannot be used together]],
862 [%name-prefix],
863 [%define api.prefix])])])