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