• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1                                                            -*- Autoconf -*-
2
3# Language-independent M4 Macros for Bison.
4
5# Copyright (C) 2002, 2004-2015, 2018-2019 Free Software Foundation,
6# Inc.
7
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21
22## ---------------- ##
23## Identification.  ##
24## ---------------- ##
25
26# b4_generated_by
27# ---------------
28m4_define([b4_generated_by],
29[b4_comment([A Bison parser, made by GNU Bison b4_version.])
30])
31
32# b4_copyright(TITLE, [YEARS])
33# ----------------------------
34# If YEARS are not defined, use b4_copyright_years.
35m4_define([b4_copyright],
36[b4_generated_by
37b4_comment([$1
38
39]m4_dquote(m4_text_wrap([Copyright (C)
40]m4_ifval([$2], [[$2]], [m4_defn([b4_copyright_years])])[
41Free Software Foundation, Inc.]))[
42
43This program is free software: you can redistribute it and/or modify
44it under the terms of the GNU General Public License as published by
45the Free Software Foundation, either version 3 of the License, or
46(at your option) any later version.
47
48This program is distributed in the hope that it will be useful,
49but WITHOUT ANY WARRANTY; without even the implied warranty of
50MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51GNU General Public License for more details.
52
53You should have received a copy of the GNU General Public License
54along with this program.  If not, see <http://www.gnu.org/licenses/>.])
55
56b4_comment([As a special exception, you may create a larger work that contains
57part or all of the Bison parser skeleton and distribute that work
58under terms of your choice, so long as that work isn't itself a
59parser generator using the skeleton or a modified version thereof
60as a parser skeleton.  Alternatively, if you modify or redistribute
61the parser skeleton itself, you may (at your option) remove this
62special exception, which will cause the skeleton and the resulting
63Bison output files to be licensed under the GNU General Public
64License without this special exception.
65
66This special exception was added by the Free Software Foundation in
67version 2.2 of Bison.])
68])
69
70
71# b4_disclaimer
72# -------------
73# Issue a warning about private implementation details.
74m4_define([b4_disclaimer],
75[b4_comment([Undocumented macros, especially those whose name start with YY_,
76are private implementation details.  Do not rely on them.])
77])
78
79
80
81# b4_required_version_if(VERSION, IF_NEWER, IF_OLDER)
82# ---------------------------------------------------
83# If the version %require'd by the user is VERSION (or newer) expand
84# IF_NEWER, otherwise IF_OLDER.  VERSION should be an integer, e.g.,
85# 302 for 3.2.
86m4_define([b4_required_version_if],
87[m4_if(m4_eval($1 <= b4_required_version),
88              [1], [$2], [$3])])
89
90
91## -------- ##
92## Output.  ##
93## -------- ##
94
95# b4_output_begin(FILE1, FILE2)
96# -----------------------------
97# Enable output, i.e., send to diversion 0, expand after "#", and
98# generate the tag to output into FILE.  Must be followed by EOL.
99# FILE is FILE1 concatenated to FILE2.  FILE2 can be empty, or be
100# absolute: do the right thing.
101m4_define([b4_output_begin],
102[m4_changecom()
103m4_divert_push(0)dnl
104@output(m4_unquote([$1])@,m4_unquote([$2])@)@dnl
105])
106
107
108# b4_output_end
109# -------------
110# Output nothing, restore # as comment character (no expansions after #).
111m4_define([b4_output_end],
112[m4_divert_pop(0)
113m4_changecom([#])
114])
115
116
117# b4_divert_kill(CODE)
118# --------------------
119# Expand CODE for its side effects, discard its output.
120m4_define([b4_divert_kill],
121[m4_divert_text([KILL], [$1])])
122
123
124# b4_define_silent(MACRO, CODE)
125# -----------------------------
126# Same as m4_define, but throw away the expansion of CODE.
127m4_define([b4_define_silent],
128[m4_define([$1], [b4_divert_kill([$2])])])
129
130
131## ---------------- ##
132## Error handling.  ##
133## ---------------- ##
134
135# The following error handling macros print error directives that should not
136# become arguments of other macro invocations since they would likely then be
137# mangled.  Thus, they print to stdout directly.
138
139# b4_cat(TEXT)
140# ------------
141# Write TEXT to stdout.  Precede the final newline with an @ so that it's
142# escaped.  For example:
143#
144#   b4_cat([[@complain(invalid input@)]])
145m4_define([b4_cat],
146[m4_syscmd([cat <<'_m4eof'
147]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@
148_m4eof
149])dnl
150m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])])
151
152# b4_error(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
153# -------------------------------------------------------
154# Write @KIND(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
155#
156# For example:
157#
158#   b4_error([[complain]], [[input.y:2.3]], [[input.y:5.4]],
159#            [[invalid %s]], [[foo]])
160m4_define([b4_error],
161[b4_cat([[@complain][(]$1[@,]$2[@,]$3[@,]$4[]]dnl
162[m4_if([$#], [4], [],
163       [m4_foreach([b4_arg],
164                   m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))),
165                   [[@,]b4_arg])])[@)]])])
166
167# b4_warn(FORMAT, [ARG1], [ARG2], ...)
168# ------------------------------------
169# Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
170#
171# For example:
172#
173#   b4_warn([[invalid value for '%s': %s]], [[foo]], [[3]])
174#
175# As a simple test suite, this:
176#
177#   m4_divert(-1)
178#   m4_define([asdf], [ASDF])
179#   m4_define([fsa], [FSA])
180#   m4_define([fdsa], [FDSA])
181#   b4_warn_at([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
182#   b4_warn_at([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
183#   b4_warn_at()
184#   b4_warn_at(1)
185#   b4_warn_at(1, 2)
186#
187# Should produce this without newlines:
188#
189#   @warn_at([asdf), asdf]@,@,@,[fsa), fsa]@,[fdsa), fdsa]@)
190#   @warn(asdf), asdf@,@,@,fsa), fsa@,fdsa), fdsa@)
191#   @warn(@)
192#   @warn(1@)
193#   @warn(1@,2@)
194m4_define([b4_warn],
195[b4_error([[warn]], [], [], $@)])
196
197# b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
198# ---------------------------------------------------
199# Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
200#
201# For example:
202#
203#   b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
204m4_define([b4_warn_at],
205[b4_error([[warn]], $@)])
206
207# b4_complain(FORMAT, [ARG1], [ARG2], ...)
208# ----------------------------------------
209# Bounce to b4_complain_at.
210#
211# See b4_warn example.
212m4_define([b4_complain],
213[b4_error([[complain]], [], [], $@)])
214
215# b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
216# -------------------------------------------------------
217# Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
218#
219# See b4_warn_at example.
220m4_define([b4_complain_at],
221[b4_error([[complain]], $@)])
222
223# b4_fatal(FORMAT, [ARG1], [ARG2], ...)
224# -------------------------------------
225# Bounce to b4_fatal_at.
226#
227# See b4_warn example.
228m4_define([b4_fatal],
229[b4_error([[fatal]], [], [], $@)dnl
230m4_exit(1)])
231
232# b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
233# ----------------------------------------------------
234# Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
235#
236# See b4_warn_at example.
237m4_define([b4_fatal_at],
238[b4_error([[fatal]], $@)dnl
239m4_exit(1)])
240
241
242## ------------ ##
243## Data Types.  ##
244## ------------ ##
245
246# b4_ints_in(INT1, INT2, LOW, HIGH)
247# ---------------------------------
248# Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
249m4_define([b4_ints_in],
250[m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
251
252
253# b4_subtract(LHS, RHS)
254# ---------------------
255# Evaluate LHS - RHS if they are integer literals, otherwise expand
256# to (LHS) - (RHS).
257m4_define([b4_subtract],
258[m4_bmatch([$1$2], [^[0123456789]*$],
259           [m4_eval([$1 - $2])],
260           [($1) - ($2)])])
261
262# b4_join(ARG1, ...)
263# _b4_join(ARG1, ...)
264# -------------------
265# Join with comma, skipping empty arguments.
266# b4_join calls itself recursively until it sees the first non-empty
267# argument, then calls _b4_join (i.e., `_$0`) which prepends each
268# non-empty argument with a comma.
269m4_define([b4_join],
270[m4_if([$#$1],
271       [1], [],
272       [m4_ifval([$1],
273                 [$1[]_$0(m4_shift($@))],
274                 [$0(m4_shift($@))])])])
275
276# _b4_join(ARGS1, ...)
277# --------------------
278m4_define([_b4_join],
279[m4_if([$#$1],
280       [1], [],
281       [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])])
282
283
284
285
286# b4_integral_parser_tables_map(MACRO)
287# -------------------------------------
288# Map MACRO on all the integral tables.  MACRO is expected to have
289# the signature MACRO(TABLE-NAME, CONTENT, COMMENT).
290m4_define([b4_integral_parser_tables_map],
291[$1([pact], [b4_pact],
292    [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
293STATE-NUM.]])
294
295$1([defact], [b4_defact],
296   [[YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
297Performed when YYTABLE does not specify something else to do.  Zero
298means the default is an error.]])
299
300$1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]])
301
302$1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]])
303
304$1([table], [b4_table],
305   [[YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM.  If
306positive, shift that token.  If negative, reduce the rule whose
307number is the opposite.  If YYTABLE_NINF, syntax error.]])
308
309$1([check], [b4_check])
310
311$1([stos], [b4_stos],
312   [[YYSTOS[STATE-NUM] -- The (internal number of the) accessing
313symbol of state STATE-NUM.]])
314
315$1([r1], [b4_r1],
316   [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]])
317
318$1([r2], [b4_r2],
319   [[YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.]])
320])
321
322
323# b4_parser_tables_declare
324# b4_parser_tables_define
325# ------------------------
326# Define/declare the (deterministic) parser tables.
327m4_define([b4_parser_tables_declare],
328[b4_integral_parser_tables_map([b4_integral_parser_table_declare])])
329
330m4_define([b4_parser_tables_define],
331[b4_integral_parser_tables_map([b4_integral_parser_table_define])])
332
333
334
335## ------------------ ##
336## Decoding options.  ##
337## ------------------ ##
338
339# b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
340# -----------------------------------
341# Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
342m4_define([b4_flag_if],
343[m4_case(b4_$1_flag,
344         [0], [$3],
345         [1], [$2],
346         [m4_fatal([invalid $1 value: ]b4_$1_flag)])])
347
348
349# b4_define_flag_if(FLAG)
350# -----------------------
351# Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
352# value of the Boolean FLAG.
353m4_define([b4_define_flag_if],
354[_b4_define_flag_if($[1], $[2], [$1])])
355
356# _b4_define_flag_if($1, $2, FLAG)
357# --------------------------------
358# Work around the impossibility to define macros inside macros,
359# because issuing '[$1]' is not possible in M4.  GNU M4 should provide
360# $$1 a la M5/TeX.
361m4_define([_b4_define_flag_if],
362[m4_if([$1$2], $[1]$[2], [],
363       [m4_fatal([$0: Invalid arguments: $@])])dnl
364m4_define([b4_$3_if],
365          [b4_flag_if([$3], [$1], [$2])])])
366
367
368# b4_FLAG_if(IF-TRUE, IF-FALSE)
369# -----------------------------
370# Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
371b4_define_flag_if([defines])            # Whether headers are requested.
372b4_define_flag_if([glr])                # Whether a GLR parser is requested.
373b4_define_flag_if([nondeterministic])   # Whether conflicts should be handled.
374b4_define_flag_if([token_table])        # Whether yytoken_table is demanded.
375b4_define_flag_if([yacc])               # Whether POSIX Yacc is emulated.
376
377
378# b4_glr_cc_if([IF-TRUE], [IF-FALSE])
379# -----------------------------------
380m4_define([b4_glr_cc_if],
381          [m4_if(b4_skeleton, ["glr.cc"], $@)])
382
383
384## --------- ##
385## Symbols.  ##
386## --------- ##
387
388# For a description of the Symbol handling, see README.
389#
390# The following macros provide access to symbol related values.
391
392# __b4_symbol(NUM, FIELD)
393# -----------------------
394# Recover a FIELD about symbol #NUM.  Thanks to m4_indir, fails if
395# undefined.
396m4_define([__b4_symbol],
397[m4_indir([b4_symbol($1, $2)])])
398
399
400# _b4_symbol(NUM, FIELD)
401# ----------------------
402# Recover a FIELD about symbol #NUM (or "orig NUM").  Fails if
403# undefined.
404m4_define([_b4_symbol],
405[m4_ifdef([b4_symbol($1, number)],
406          [__b4_symbol(m4_indir([b4_symbol($1, number)]), $2)],
407          [__b4_symbol([$1], [$2])])])
408
409
410
411# b4_symbol(NUM, FIELD)
412# ---------------------
413# Recover a FIELD about symbol #NUM (or "orig NUM").  Fails if
414# undefined.  If FIELD = id, prepend the token prefix.
415m4_define([b4_symbol],
416[m4_case([$2],
417         [id],    [m4_do([b4_percent_define_get([api.token.prefix])],
418                         [_b4_symbol([$1], [id])])],
419         [_b4_symbol($@)])])
420
421
422# b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
423# -------------------------------------------
424# If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE.
425# Otherwise an error.
426m4_define([b4_symbol_if],
427[m4_case(b4_symbol([$1], [$2]),
428         [1], [$3],
429         [0], [$4],
430         [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])])
431
432
433# b4_symbol_tag_comment(SYMBOL-NUM)
434# ---------------------------------
435# Issue a comment giving the tag of symbol NUM.
436m4_define([b4_symbol_tag_comment],
437[b4_comment([b4_symbol([$1], [tag])])
438])
439
440
441# b4_symbol_action_location(SYMBOL-NUM, KIND)
442# -------------------------------------------
443# Report the location of the KIND action as FILE:LINE.
444m4_define([b4_symbol_action_location],
445[b4_symbol([$1], [$2_file]):b4_syncline([b4_symbol([$1], [$2_line])])])
446
447
448# b4_symbol_action(SYMBOL-NUM, KIND)
449# ----------------------------------
450# Run the action KIND (destructor or printer) for SYMBOL-NUM.
451m4_define([b4_symbol_action],
452[b4_symbol_if([$1], [has_$2],
453[b4_dollar_pushdef([(*yyvaluep)],
454                   [$1],
455                   [],
456                   [(*yylocationp)])dnl
457    _b4_symbol_case([$1])[]dnl
458b4_syncline([b4_symbol([$1], [$2_line])], [b4_symbol([$1], [$2_file])])
459      b4_symbol([$1], [$2])
460b4_syncline([@oline@], [@ofile@])
461        break;
462
463b4_dollar_popdef[]dnl
464])])
465
466
467# b4_symbol_destructor(SYMBOL-NUM)
468# b4_symbol_printer(SYMBOL-NUM)
469# --------------------------------
470m4_define([b4_symbol_destructor], [b4_symbol_action([$1], [destructor])])
471m4_define([b4_symbol_printer],    [b4_symbol_action([$1], [printer])])
472
473
474# b4_symbol_actions(KIND, [TYPE = yytype])
475# ----------------------------------------
476# Emit the symbol actions for KIND ("printer" or "destructor").
477# Dispatch on TYPE.
478m4_define([b4_symbol_actions],
479[m4_pushdef([b4_actions_], m4_expand([b4_symbol_foreach([b4_symbol_$1])]))dnl
480m4_ifval(m4_defn([b4_actions_]),
481[switch (m4_default([$2], [yytype]))
482    {
483m4_defn([b4_actions_])[]dnl
484      default:
485        break;
486    }dnl
487],
488[YYUSE (m4_default([$2], [yytype]));])dnl
489m4_popdef([b4_actions_])dnl
490])
491
492# _b4_symbol_case(SYMBOL-NUM)
493# ---------------------------
494# Issue a "case NUM" for SYMBOL-NUM.  Ends with its EOL to make it
495# easier to use with m4_map, but then, use []dnl to suppress the last
496# one.
497m4_define([_b4_symbol_case],
498[case b4_symbol([$1], [number]): b4_symbol_tag_comment([$1])])
499])
500
501
502# b4_symbol_foreach(MACRO)
503# ------------------------
504# Invoke MACRO(SYMBOL-NUM) for each SYMBOL-NUM.
505m4_define([b4_symbol_foreach],
506          [m4_map([$1], m4_defn([b4_symbol_numbers]))])
507
508# b4_symbol_map(MACRO)
509# --------------------
510# Return a list (possibly empty elements) of MACRO invoked for each
511# SYMBOL-NUM.
512m4_define([b4_symbol_map],
513[m4_map_args_sep([$1(], [)], [,], b4_symbol_numbers)])
514
515
516# b4_token_visible_if(NUM, IF-TRUE, IF-FALSE)
517# -------------------------------------------
518# Whether NUM denotes a token that has an exported definition (i.e.,
519# shows in enum yytokentype).
520m4_define([b4_token_visible_if],
521[b4_symbol_if([$1], [is_token],
522              [b4_symbol_if([$1], [has_id], [$2], [$3])],
523              [$3])])
524
525# b4_token_has_definition(NUM)
526# ----------------------------
527# 1 if NUM is visible, nothing otherwise.
528m4_define([b4_token_has_definition],
529[b4_token_visible_if([$1], [1])])
530
531# b4_any_token_visible_if([IF-TRUE], [IF-FALSE])
532# ----------------------------------------------
533# Whether there is a token that needs to be defined.
534m4_define([b4_any_token_visible_if],
535[m4_ifval(b4_symbol_foreach([b4_token_has_definition]),
536          [$1], [$2])])
537
538
539# b4_token_format(FORMAT, NUM)
540# ----------------------------
541m4_define([b4_token_format],
542[b4_token_visible_if([$2],
543[m4_quote(m4_format([$1],
544                     [b4_symbol([$2], [id])],
545                     [b4_symbol([$2], [user_number])]))])])
546
547
548## ------- ##
549## Types.  ##
550## ------- ##
551
552# _b4_type_action(NUMS)
553# ---------------------
554# Run actions for the symbol NUMS that all have the same type-name.
555# Skip NUMS that have no type-name.
556#
557# To specify the action to run, define b4_dollar_dollar(SYMBOL-NUM,
558# TAG, TYPE).
559m4_define([_b4_type_action],
560[b4_symbol_if([$1], [has_type],
561[m4_map([      _b4_symbol_case], [$@])[]dnl
562        b4_dollar_dollar([b4_symbol([$1], [number])],
563                         [b4_symbol([$1], [tag])],
564                         [b4_symbol([$1], [type])]);
565        break;
566
567])])
568
569# b4_type_foreach(MACRO)
570# ----------------------
571# Invoke MACRO(SYMBOL-NUMS) for each set of SYMBOL-NUMS for each type set.
572m4_define([b4_type_foreach],
573          [m4_map([$1], m4_defn([b4_type_names]))])
574
575
576
577## ----------- ##
578## Synclines.  ##
579## ----------- ##
580
581# b4_basename(NAME)
582# -----------------
583# Similar to POSIX basename; the differences don't matter here.
584# Beware that NAME is not evaluated.
585m4_define([b4_basename],
586[m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
587
588
589# b4_syncline(LINE, FILE)
590# -----------------------
591m4_define([b4_syncline],
592[b4_flag_if([synclines],
593[b4_sync_start([$1], [$2]) b4_sync_end([__line__],
594                                       [b4_basename(m4_quote(__file__))])[]dnl
595])])
596
597# b4_sync_start(LINE, FILE)
598# -----------------------
599# Syncline for the new place.  Typically a directive for the compiler.
600m4_define([b4_sync_start], [b4_comment([$2:$1])])
601
602# b4_sync_end(LINE, FILE)
603# -----------------------
604# Syncline for the current place, which ends.  Typically a comment
605# left for the reader.
606m4_define([b4_sync_end],   [b4_comment([$2:$1])])
607
608
609# b4_user_code(USER-CODE)
610# -----------------------
611# Emit code from the user, ending it with synclines.
612m4_define([b4_user_code],
613[$1
614b4_syncline([@oline@], [@ofile@])])
615
616
617# b4_define_user_code(MACRO, COMMENT)
618# -----------------------------------
619# From b4_MACRO, if defined, build b4_user_MACRO that includes the synclines.
620m4_define([b4_define_user_code],
621[m4_define([b4_user_$1],
622           [m4_ifdef([b4_$1],
623                     [m4_ifval([$2],
624                               [b4_comment([$2])
625])b4_user_code([b4_$1])])])])
626
627# b4_user_actions
628# b4_user_initial_action
629# b4_user_post_prologue
630# b4_user_pre_prologue
631# b4_user_union_members
632# ----------------------
633# Macros that issue user code, ending with synclines.
634b4_define_user_code([actions])
635b4_define_user_code([initial_action], [User initialization code.])
636b4_define_user_code([post_prologue], [Second part of user prologue.])
637b4_define_user_code([pre_prologue], [First part of user prologue.])
638b4_define_user_code([union_members])
639
640
641# b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
642# -----------------------------------------------------
643# Complain if any name of type WHAT is used by the user (as recorded in
644# USER-LIST) but is not used by Bison (as recorded by macros in the
645# namespace BISON-NAMESPACE).
646#
647# USER-LIST must expand to a list specifying all user occurrences of all names
648# of type WHAT.   Each item in the list must be a triplet specifying one
649# occurrence: name, start boundary, and end boundary.  Empty string names are
650# fine.  An empty list is fine.
651#
652# For example, to define b4_foo_user_names to be used for USER-LIST with three
653# name occurrences and with correct quoting:
654#
655#   m4_define([b4_foo_user_names],
656#             [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]],
657#               [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]],
658#               [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]])
659#
660# The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT
661# is used by Bison (in the front-end or in the skeleton).  Empty string names
662# are fine, but it would be ugly for Bison to actually use one.
663#
664# For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that
665# the names bar and baz are used by Bison:
666#
667#   m4_define([b4_foo_bison_names(bar)])
668#   m4_define([b4_foo_bison_names(baz)])
669#
670# To invoke b4_check_user_names with TYPE foo, with USER-LIST
671# b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
672# quoting:
673#
674#   b4_check_user_names([[foo]], [b4_foo_user_names],
675#                       [[b4_foo_bison_names]])
676m4_define([b4_check_user_names],
677[m4_foreach([b4_occurrence], $2,
678[m4_pushdef([b4_occurrence], b4_occurrence)dnl
679m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
680m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
681m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl
682m4_ifndef($3[(]m4_quote(b4_user_name)[)],
683          [b4_complain_at([b4_start], [b4_end],
684                          [[%s '%s' is not used]],
685                          [$1], [b4_user_name])])[]dnl
686m4_popdef([b4_occurrence])dnl
687m4_popdef([b4_user_name])dnl
688m4_popdef([b4_start])dnl
689m4_popdef([b4_end])dnl
690])])
691
692
693
694## --------------------- ##
695## b4_percent_define_*.  ##
696## --------------------- ##
697
698
699# b4_percent_define_use(VARIABLE)
700# -------------------------------
701# Declare that VARIABLE was used.
702m4_define([b4_percent_define_use],
703[m4_define([b4_percent_define_bison_variables(]$1[)])dnl
704])
705
706# b4_percent_define_get(VARIABLE, [DEFAULT])
707# ------------------------------------------
708# Mimic muscle_percent_define_get in ../src/muscle-tab.h.  That is, if
709# the %define variable VARIABLE is defined, emit its value.  Contrary
710# to its C counterpart, return DEFAULT otherwise.  Also, record
711# Bison's usage of VARIABLE by defining
712# b4_percent_define_bison_variables(VARIABLE).
713#
714# For example:
715#
716#   b4_percent_define_get([[foo]])
717m4_define([b4_percent_define_get],
718[b4_percent_define_use([$1])dnl
719_b4_percent_define_ifdef([$1],
720                         [m4_indir([b4_percent_define(]$1[)])],
721                         [$2])])
722
723# b4_percent_define_get_loc(VARIABLE)
724# -----------------------------------
725# Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly.  That is,
726# if the %define variable VARIABLE is undefined, complain fatally since that's
727# a Bison or skeleton error.  Otherwise, return its definition location in a
728# form appropriate for the first two arguments of b4_warn_at, b4_complain_at, or
729# b4_fatal_at.  Don't record this as a Bison usage of VARIABLE as there's no
730# reason to suspect that the user-supplied value has yet influenced the output.
731#
732# For example:
733#
734#   b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
735m4_define([b4_percent_define_get_loc],
736[m4_ifdef([b4_percent_define_loc(]$1[)],
737          [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl
738b4_loc[]dnl
739m4_popdef([b4_loc])],
740          [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
741
742# b4_percent_define_get_kind(VARIABLE)
743# ------------------------------------
744# Get the kind (code, keyword, string) of VARIABLE, i.e., how its
745# value was defined (braces, not delimiters, quotes).
746#
747# If the %define variable VARIABLE is undefined, complain fatally
748# since that's a Bison or skeleton error.  Don't record this as a
749# Bison usage of VARIABLE as there's no reason to suspect that the
750# user-supplied value has yet influenced the output.
751m4_define([b4_percent_define_get_kind],
752[m4_ifdef([b4_percent_define_kind(]$1[)],
753          [m4_indir([b4_percent_define_kind(]$1[)])],
754          [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
755
756# b4_percent_define_get_syncline(VARIABLE)
757# ----------------------------------------
758# Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly.
759# That is, if the %define variable VARIABLE is undefined, complain fatally
760# since that's a Bison or skeleton error.  Otherwise, return its definition
761# location as a b4_syncline invocation.  Don't record this as a Bison usage of
762# VARIABLE as there's no reason to suspect that the user-supplied value has yet
763# influenced the output.
764#
765# For example:
766#
767#   b4_percent_define_get_syncline([[foo]])
768m4_define([b4_percent_define_get_syncline],
769[m4_ifdef([b4_percent_define_syncline(]$1[)],
770          [m4_indir([b4_percent_define_syncline(]$1[)])],
771          [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
772
773# _b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
774# ------------------------------------------------------
775# If the %define variable VARIABLE is defined, expand IF-TRUE, else expand
776# IF-FALSE.  Don't record usage of VARIABLE.
777#
778# For example:
779#
780#   _b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
781m4_define([_b4_percent_define_ifdef],
782[m4_ifdef([b4_percent_define(]$1[)],
783          [$2],
784          [$3])])
785
786# b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
787# ------------------------------------------------------
788# Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly.  That is,
789# if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
790# IF-FALSE.  Also, record Bison's usage of VARIABLE by defining
791# b4_percent_define_bison_variables(VARIABLE).
792#
793# For example:
794#
795#   b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
796m4_define([b4_percent_define_ifdef],
797[_b4_percent_define_ifdef([$1],
798                         [b4_percent_define_use([$1])$2],
799                         [$3])])
800
801
802# b4_percent_define_check_file_complain(VARIABLE)
803# -----------------------------------------------
804# Warn about %define variable VARIABLE having an incorrect
805# value.
806m4_define([b4_percent_define_check_file_complain],
807[b4_complain_at(b4_percent_define_get_loc([$1]),
808                [[%%define variable '%s' requires 'none' or '"..."' values]],
809                [$1])])
810
811
812# b4_percent_define_check_file(MACRO, VARIABLE, DEFAULT)
813# ------------------------------------------------------
814# If the %define variable VARIABLE:
815# - is undefined, then if DEFAULT is non-empty, define MACRO to DEFAULT
816# - is a string, define MACRO to its value
817# - is the keyword 'none', do nothing
818# - otherwise, warn about the incorrect value.
819m4_define([b4_percent_define_check_file],
820[b4_percent_define_ifdef([$2],
821  [m4_case(b4_percent_define_get_kind([$2]),
822    [string],
823         [m4_define([$1], b4_percent_define_get([$2]))],
824    [keyword],
825         [m4_if(b4_percent_define_get([$2]), [none], [],
826                [b4_percent_define_check_file_complain([$2])])],
827    [b4_percent_define_check_file_complain([$2])])
828   ],
829   [m4_ifval([$3],
830             [m4_define([$1], [$3])])])
831])
832
833
834
835## --------- ##
836## Options.  ##
837## --------- ##
838
839
840# b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
841# --------------------------------------------------------
842# Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly.  That is,
843# if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE.
844# If it is defined to "false", expand IF-FALSE.  Complain if it is undefined
845# (a Bison or skeleton error since the default value should have been set
846# already) or defined to any other value (possibly a user error).  Also, record
847# Bison's usage of VARIABLE by defining
848# b4_percent_define_bison_variables(VARIABLE).
849#
850# For example:
851#
852#   b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
853m4_define([b4_percent_define_flag_if],
854[b4_percent_define_ifdef([$1],
855  [m4_case(b4_percent_define_get([$1]),
856           [], [$2], [true], [$2], [false], [$3],
857           [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
858                                           [[invalid value for %%define Boolean variable '%s']],
859                                           [$1])],
860                           [[b4_percent_define_flag_if($1)]])])],
861  [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
862
863
864# b4_percent_define_default(VARIABLE, DEFAULT, [KIND = keyword])
865# --------------------------------------------------------------
866# Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly.  That is,
867# if the %define variable VARIABLE is undefined, set its value to DEFAULT.
868# Don't record this as a Bison usage of VARIABLE as there's no reason to
869# suspect that the value has yet influenced the output.
870#
871# For example:
872#
873#   b4_percent_define_default([[foo]], [[default value]])
874m4_define([b4_percent_define_default],
875[_b4_percent_define_ifdef([$1], [],
876           [m4_define([b4_percent_define(]$1[)], [$2])dnl
877            m4_define([b4_percent_define_kind(]$1[)],
878                      [m4_default([$3], [keyword])])dnl
879            m4_define([b4_percent_define_loc(]$1[)],
880                      [[[[<skeleton default value>:-1.-1]],
881                        [[<skeleton default value>:-1.-1]]]])dnl
882            m4_define([b4_percent_define_syncline(]$1[)], [[]])])])
883
884
885# b4_percent_define_if_define(NAME, [VARIABLE = NAME])
886# ----------------------------------------------------
887# Define b4_NAME_if that executes its $1 or $2 depending whether
888# VARIABLE was %defined.  The characters '.' and `-' in VARIABLE are mapped
889# to '_'.
890m4_define([_b4_percent_define_if_define],
891[m4_define(m4_bpatsubst([b4_$1_if], [[-.]], [_]),
892           [b4_percent_define_flag_if(m4_default([$2], [$1]),
893                                      [$3], [$4])])])
894m4_define([b4_percent_define_if_define],
895[b4_percent_define_default([m4_default([$2], [$1])], [[false]])
896_b4_percent_define_if_define([$1], [$2], $[1], $[2])])
897
898
899# b4_percent_define_check_kind(VARIABLE, KIND, [DIAGNOSTIC = complain])
900# ---------------------------------------------------------------------
901m4_define([b4_percent_define_check_kind],
902[_b4_percent_define_ifdef([$1],
903  [m4_if(b4_percent_define_get_kind([$1]), [$2], [],
904    [b4_error([m4_default([$3], [complain])],
905              b4_percent_define_get_loc([$1]),
906              [m4_case([$2],
907                 [code],    [[%%define variable '%s' requires '{...}' values]],
908                 [keyword], [[%%define variable '%s' requires keyword values]],
909                 [string],  [[%%define variable '%s' requires '"..."' values]])],
910              [$1])])])dnl
911])
912
913
914# b4_percent_define_check_values(VALUES)
915# --------------------------------------
916# Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly
917# except that the VALUES structure is more appropriate for M4.  That is, VALUES
918# is a list of sublists of strings.  For each sublist, the first string is the
919# name of a %define variable, and all remaining strings in that sublist are the
920# valid values for that variable.  Complain if such a variable is undefined (a
921# Bison error since the default value should have been set already) or defined
922# to any other value (possibly a user error).  Don't record this as a Bison
923# usage of the variable as there's no reason to suspect that the value has yet
924# influenced the output.
925#
926# For example:
927#
928#   b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
929#                                  [[[[bar]], [[bar-value1]]]])
930m4_define([b4_percent_define_check_values],
931[m4_foreach([b4_sublist], m4_quote($@),
932            [_b4_percent_define_check_values(b4_sublist)])])
933
934m4_define([_b4_percent_define_check_values],
935[_b4_percent_define_ifdef([$1],
936  [b4_percent_define_check_kind(]$1[, [keyword], [deprecated])dnl
937   m4_pushdef([b4_good_value], [0])dnl
938   m4_if($#, 1, [],
939         [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
940                     [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
941                            [m4_define([b4_good_value], [1])])])])dnl
942   m4_if(b4_good_value, [0],
943         [b4_complain_at(b4_percent_define_get_loc([$1]),
944                         [[invalid value for %%define variable '%s': '%s']],
945                         [$1],
946                         m4_dquote(m4_indir([b4_percent_define(]$1[)])))
947          m4_foreach([b4_value], m4_dquote(m4_shift($@)),
948                     [b4_error([[note]], b4_percent_define_get_loc([$1]), []
949                                     [[accepted value: '%s']],
950                                     m4_dquote(b4_value))])])dnl
951   m4_popdef([b4_good_value])],
952  [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
953
954# b4_percent_code_get([QUALIFIER])
955# --------------------------------
956# If any %code blocks for QUALIFIER are defined, emit them beginning with a
957# comment and ending with synclines and a newline.  If QUALIFIER is not
958# specified or empty, do this for the unqualified %code blocks.  Also, record
959# Bison's usage of QUALIFIER (if specified) by defining
960# b4_percent_code_bison_qualifiers(QUALIFIER).
961#
962# For example, to emit any unqualified %code blocks followed by any %code
963# blocks for the qualifier foo:
964#
965#   b4_percent_code_get
966#   b4_percent_code_get([[foo]])
967m4_define([b4_percent_code_get],
968[m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
969m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
970m4_ifdef(b4_macro_name,
971[b4_comment([m4_if([$#], [0], [[Unqualified %code]],
972                   [["%code ]$1["]])[ blocks.]])
973b4_user_code([m4_indir(b4_macro_name)])
974])dnl
975m4_popdef([b4_macro_name])])
976
977# b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
978# -----------------------------------------------------
979# If any %code blocks for QUALIFIER (or unqualified %code blocks if
980# QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
981# Also, record Bison's usage of QUALIFIER (if specified) by defining
982# b4_percent_code_bison_qualifiers(QUALIFIER).
983m4_define([b4_percent_code_ifdef],
984[m4_ifdef([b4_percent_code(]$1[)],
985          [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
986          [$3])])
987
988
989## ------------------ ##
990## Common variables.  ##
991## ------------------ ##
992
993
994# b4_parse_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
995# b4_parse_trace_if([IF-DEBUG-TRACES-ARE-ENABLED], [IF-NOT])
996# b4_token_ctor_if([IF-YYLEX-RETURNS-A-TOKEN], [IF-NOT])
997# ----------------------------------------------------------
998b4_percent_define_if_define([token_ctor], [api.token.constructor])
999b4_percent_define_if_define([locations])     # Whether locations are tracked.
1000b4_percent_define_if_define([parse.assert])
1001b4_percent_define_if_define([parse.trace])
1002
1003
1004# b4_bison_locations_if([IF-TRUE])
1005# --------------------------------
1006# Expand IF-TRUE if using locations, and using the default location
1007# type.
1008m4_define([b4_bison_locations_if],
1009[b4_locations_if([b4_percent_define_ifdef([[api.location.type]], [], [$1])])])
1010
1011
1012# b4_error_verbose_if([IF-ERRORS-ARE-VERBOSE], [IF-NOT])
1013# ------------------------------------------------------
1014# Map %define parse.error "(simple|verbose)" to b4_error_verbose_if and
1015# b4_error_verbose_flag.
1016b4_percent_define_default([[parse.error]], [[simple]])
1017b4_percent_define_check_values([[[[parse.error]],
1018                                 [[simple]], [[verbose]]]])
1019m4_define([b4_error_verbose_flag],
1020          [m4_case(b4_percent_define_get([[parse.error]]),
1021                   [simple],  [[0]],
1022                   [verbose], [[1]])])
1023b4_define_flag_if([error_verbose])
1024
1025# yytoken_table is needed to support verbose errors.
1026b4_error_verbose_if([m4_define([b4_token_table_flag], [1])])
1027
1028
1029# b4_variant_if([IF-VARIANT-ARE-USED], [IF-NOT])
1030# ----------------------------------------------
1031b4_percent_define_if_define([variant])
1032m4_define([b4_variant_flag], [[0]])
1033b4_percent_define_ifdef([[api.value.type]],
1034   [m4_case(b4_percent_define_get_kind([[api.value.type]]), [keyword],
1035            [m4_case(b4_percent_define_get([[api.value.type]]), [variant],
1036                    [m4_define([b4_variant_flag], [[1]])])])])
1037b4_define_flag_if([variant])
1038
1039
1040## ----------------------------------------------------------- ##
1041## After processing the skeletons, check that all the user's   ##
1042## %define variables and %code qualifiers were used by Bison.  ##
1043## ----------------------------------------------------------- ##
1044
1045m4_define([b4_check_user_names_wrap],
1046[m4_ifdef([b4_percent_]$1[_user_]$2[s],
1047          [b4_check_user_names([[%]$1 $2],
1048                               [b4_percent_]$1[_user_]$2[s],
1049                               [[b4_percent_]$1[_bison_]$2[s]])])])
1050
1051m4_wrap_lifo([
1052b4_check_user_names_wrap([[define]], [[variable]])
1053b4_check_user_names_wrap([[code]], [[qualifier]])
1054])
1055
1056
1057## ---------------- ##
1058## Default values.  ##
1059## ---------------- ##
1060
1061# m4_define_default([b4_lex_param], [])   dnl breaks other skeletons
1062m4_define_default([b4_epilogue], [])
1063m4_define_default([b4_parse_param], [])
1064
1065# The initial column and line.
1066m4_define_default([b4_location_initial_column], [1])
1067m4_define_default([b4_location_initial_line],   [1])
1068
1069
1070## --------------- ##
1071## Sanity checks.  ##
1072## --------------- ##
1073
1074# api.location.prefix={...} (Java and C++).
1075b4_percent_define_check_kind([api.location.type], [code], [deprecated])
1076
1077# api.position.prefix={...} (Java).
1078b4_percent_define_check_kind([api.position.type], [code], [deprecated])
1079
1080# api.prefix >< %name-prefix.
1081b4_percent_define_check_kind([api.prefix], [code], [deprecated])
1082b4_percent_define_ifdef([api.prefix],
1083[m4_ifdef([b4_prefix],
1084[b4_complain_at(b4_percent_define_get_loc([api.prefix]),
1085                [['%s' and '%s' cannot be used together]],
1086                [%name-prefix],
1087                [%define api.prefix])])])
1088
1089# api.token.prefix={...}
1090# Make it a warning for those who used betas of Bison 3.0.
1091b4_percent_define_check_kind([api.token.prefix], [code], [deprecated])
1092
1093# api.value.type >< %union.
1094b4_percent_define_ifdef([api.value.type],
1095[m4_ifdef([b4_union_members],
1096[b4_complain_at(b4_percent_define_get_loc([api.value.type]),
1097                [['%s' and '%s' cannot be used together]],
1098                [%union],
1099                [%define api.value.type])])])
1100
1101# api.value.type=union >< %yacc.
1102b4_percent_define_ifdef([api.value.type],
1103[m4_if(b4_percent_define_get([api.value.type]), [union],
1104[b4_yacc_if(dnl
1105[b4_complain_at(b4_percent_define_get_loc([api.value.type]),
1106                [['%s' and '%s' cannot be used together]],
1107                [%yacc],
1108                [%define api.value.type "union"])])])])
1109
1110# api.value.union.name.
1111b4_percent_define_check_kind([api.value.union.name], [keyword])
1112