3 # Language-independent M4 Macros for Bison.
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
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.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ## ---------------- ##
23 ## ---------------- ##
25 # b4_copyright(TITLE, [YEARS])
26 # ----------------------------
27 # If YEARS are not defined, use b4_copyright_years.
28 m4_define([b4_copyright],
29 [b4_comment([A Bison parser, made by GNU Bison b4_version.])
33 ]m4_dquote(m4_text_wrap([Copyright (C)
34 ]m4_ifval([$2], [[$2]], [m4_defn([b4_copyright_years])])[
35 Free Software Foundation, Inc.]))[
37 This program is free software: you can redistribute it and/or modify
38 it under the terms of the GNU General Public License as published by
39 the Free Software Foundation, either version 3 of the License, or
40 (at your option) any later version.
42 This program is distributed in the hope that it will be useful,
43 but WITHOUT ANY WARRANTY; without even the implied warranty of
44 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 GNU General Public License for more details.
47 You should have received a copy of the GNU General Public License
48 along with this program. If not, see <http://www.gnu.org/licenses/>.])
50 b4_comment([As a special exception, you may create a larger work that contains
51 part or all of the Bison parser skeleton and distribute that work
52 under terms of your choice, so long as that work isn't itself a
53 parser generator using the skeleton or a modified version thereof
54 as a parser skeleton. Alternatively, if you modify or redistribute
55 the parser skeleton itself, you may (at your option) remove this
56 special exception, which will cause the skeleton and the resulting
57 Bison output files to be licensed under the GNU General Public
58 License without this special exception.
60 This special exception was added by the Free Software Foundation in
61 version 2.2 of Bison.])])
64 ## ---------------- ##
66 ## ---------------- ##
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.
74 # Write TEXT to stdout. Precede the final newline with an @ so that it's
75 # escaped. For example:
77 # b4_cat([[@complain(invalid input@)]])
79 [m4_syscmd([cat <<'_m4eof'
80 ]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@
83 m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])])
85 # b4_error(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
86 # -------------------------------------------------------
87 # Write @KIND(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
91 # b4_error([[complain]], [[input.y:2.3]], [[input.y:5.4]],
92 # [[invalid %s]], [[foo]])
94 [b4_cat([[@complain][(]$1[@,]$2[@,]$3[@,]$4[]]dnl
97 m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))),
98 [[@,]b4_arg])])[@)]])])
100 # b4_warn(FORMAT, [ARG1], [ARG2], ...)
101 # ------------------------------------
102 # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
106 # b4_warn([[invalid value for '%s': %s]], [[foo]], [[3]])
108 # As a simple test suite, this:
111 # m4_define([asdf], [ASDF])
112 # m4_define([fsa], [FSA])
113 # m4_define([fdsa], [FDSA])
114 # b4_warn_at([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
115 # b4_warn_at([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
120 # Should produce this without newlines:
122 # @warn_at([asdf), asdf]@,@,@,[fsa), fsa]@,[fdsa), fdsa]@)
123 # @warn(asdf), asdf@,@,@,fsa), fsa@,fdsa), fdsa@)
128 [b4_error([[warn]], [], [], $@)])
130 # b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
131 # ---------------------------------------------------
132 # Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
136 # b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
137 m4_define([b4_warn_at],
138 [b4_error([[warn]], $@)])
140 # b4_complain(FORMAT, [ARG1], [ARG2], ...)
141 # ----------------------------------------
142 # Bounce to b4_complain_at.
144 # See b4_warn example.
145 m4_define([b4_complain],
146 [b4_error([[complain]], [], [], $@)])
148 # b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
149 # -------------------------------------------------------
150 # Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
152 # See b4_warn_at example.
153 m4_define([b4_complain_at],
154 [b4_error([[complain]], $@)])
156 # b4_fatal(FORMAT, [ARG1], [ARG2], ...)
157 # -------------------------------------
158 # Bounce to b4_fatal_at.
160 # See b4_warn example.
161 m4_define([b4_fatal],
162 [b4_error([[fatal]], [], [], $@)dnl
165 # b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
166 # ----------------------------------------------------
167 # Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
169 # See b4_warn_at example.
170 m4_define([b4_fatal_at],
171 [b4_error([[fatal]], $@)dnl
179 # b4_ints_in(INT1, INT2, LOW, HIGH)
180 # ---------------------------------
181 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
182 m4_define([b4_ints_in],
183 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
186 # b4_subtract(LHS, RHS)
187 # ---------------------
188 # Evaluate LHS - RHS if they are integer literals, otherwise expand
190 m4_define([b4_subtract],
191 [m4_bmatch([$1$2], [^[0123456789]*$],
192 [m4_eval([$1 - $2])],
196 # _b4_join(ARG1, ...)
197 # -------------------
198 # Join with comma, skipping empty arguments.
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
206 [$1[]_$0(m4_shift($@))],
207 [$0(m4_shift($@))])])])
209 # _b4_join(ARGS1, ...)
210 # --------------------
211 m4_define([_b4_join],
214 [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])])
219 # b4_integral_parser_tables_map(MACRO)
220 # -------------------------------------
221 # Map MACRO on all the integral tables. MACRO is expected to have
222 # the signature MACRO(TABLE-NAME, CONTENT, COMMENT).
223 m4_define([b4_integral_parser_tables_map],
224 [$1([pact], [b4_pact],
225 [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
228 $1([defact], [b4_defact],
229 [[YYDEFACT[S] -- default reduction number in state S. Performed when
230 YYTABLE does not specify something else to do. Zero means the default
233 $1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]])
235 $1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]])
237 $1([table], [b4_table],
238 [[YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
239 positive, shift that token. If negative, reduce the rule which
240 number is the opposite. If YYTABLE_NINF, syntax error.]])
242 $1([check], [b4_check])
244 $1([stos], [b4_stos],
245 [[STOS_[STATE-NUM] -- The (internal number of the) accessing
246 symbol of state STATE-NUM.]])
249 [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]])
252 [[YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.]])
256 # b4_parser_tables_declare
257 # b4_parser_tables_define
258 # ------------------------
259 # Define/declare the (deterministic) parser tables.
260 m4_define([b4_parser_tables_declare],
261 [b4_integral_parser_tables_map([b4_integral_parser_table_declare])])
263 m4_define([b4_parser_tables_define],
264 [b4_integral_parser_tables_map([b4_integral_parser_table_define])])
268 ## ------------------ ##
269 ## Decoding options. ##
270 ## ------------------ ##
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.
275 m4_define([b4_flag_if],
279 [m4_fatal([invalid $1 value: ]$1)])])
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.
286 m4_define([b4_define_flag_if],
287 [_b4_define_flag_if($[1], $[2], [$1])])
289 # _b4_define_flag_if($1, $2, FLAG)
290 # --------------------------------
291 # Work around the impossibility to define macros inside macros,
292 # because issuing `[$1]' is not possible in M4. GNU M4 should provide
294 m4_define([_b4_define_flag_if],
295 [m4_if([$1$2], $[1]$[2], [],
296 [m4_fatal([$0: Invalid arguments: $@])])dnl
297 m4_define([b4_$3_if],
298 [b4_flag_if([$3], [$1], [$2])])])
301 # b4_FLAG_if(IF-TRUE, IF-FALSE)
302 # -----------------------------
303 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
304 b4_define_flag_if([defines]) # Whether headers are requested.
305 b4_define_flag_if([glr]) # Whether a GLR parser is requested.
306 b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
307 b4_define_flag_if([token_table]) # Whether yytoken_table is demanded.
308 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
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().
320 # The following macros provide access to these values.
322 # b4_symbol_(NUM, FIELD)
323 # ----------------------
324 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
326 m4_define([b4_symbol_],
327 [m4_indir([b4_symbol($1, $2)])])
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.
334 m4_define([b4_symbol],
336 [id], [m4_do([b4_percent_define_get([api.token.prefix])],
337 [b4_symbol_([$1], [id])])],
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.
345 m4_define([b4_symbol_if],
346 [m4_case(b4_symbol([$1], [$2]),
349 [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])])
352 # b4_symbol_action_location(SYMBOL-NUM, KIND)
353 # -------------------------------------------
354 # Report the location of the KIND action as FILE:LINE.
355 m4_define([b4_symbol_action_location],
356 [b4_symbol([$1], [$2_file]):b4_syncline([b4_symbol([$1], [$2_line])])])
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.
363 m4_define([b4_symbol_action],
364 [b4_symbol_if([$1], [has_$2],
365 [b4_dollar_pushdef([(*yyvaluep)],
366 b4_symbol_if([$1], [has_type],
367 [m4_dquote(b4_symbol([$1], [type]))]),
369 b4_symbol_case_([$1])[]dnl
370 b4_syncline([b4_symbol([$1], [$2_line])], ["b4_symbol([$1], [$2_file])"])
371 b4_symbol([$1], [$2])
372 b4_syncline([@oline@], [@ofile@])
375 b4_dollar_popdef[]dnl
379 # b4_symbol_destructor(SYMBOL-NUM)
380 # b4_symbol_printer(SYMBOL-NUM)
381 # --------------------------------
382 m4_define([b4_symbol_destructor], [b4_symbol_action([$1], [destructor])])
383 m4_define([b4_symbol_printer], [b4_symbol_action([$1], [printer])])
386 # b4_symbol_case_(SYMBOL-NUM)
387 # ---------------------------
388 # Issue a "case NUM" for SYMBOL-NUM.
389 m4_define([b4_symbol_case_],
390 [ case b4_symbol([$1], [number]): // b4_symbol([$1], [tag])
394 # b4_symbol_foreach(MACRO)
395 # ------------------------
396 # Invoke MACRO(SYMBOL-NUM) for each SYMBOL-NUM.
397 m4_define([b4_symbol_foreach],
398 [m4_map([$1], m4_defn([b4_symbol_numbers]))])
400 # b4_symbol_map(MACRO)
401 # --------------------
402 # Return a list (possibly empty elements) of MACRO invoked for each
404 m4_define([b4_symbol_map],
405 [m4_map_args_sep([$1(], [)], [,], b4_symbol_numbers)])
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).
412 m4_define([b4_token_visible_if],
413 [b4_symbol_if([$1], [is_token],
414 [b4_symbol_if([$1], [has_id], [$2], [$3])],
417 # b4_token_has_definition(NUM)
418 # ----------------------------
419 # 1 if NUM is visible, nothing otherwise.
420 m4_define([b4_token_has_definition],
421 [b4_token_visible_if([$1], [1])])
423 # b4_any_token_visible_if([IF-TRUE], [IF-FALSE])
424 # ----------------------------------------------
425 # Whether there is a token that needs to be defined.
426 m4_define([b4_any_token_visible_if],
427 [m4_ifval(b4_symbol_foreach([b4_token_has_definition]),
431 # b4_token_format(FORMAT, NUM)
432 # ----------------------------
433 m4_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])]))])])
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.
448 m4_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])]);
458 # b4_type_foreach(MACRO)
459 # ----------------------
460 # Invoke MACRO(SYMBOL-NUMS) for each set of SYMBOL-NUMS for each type set.
461 m4_define([b4_type_foreach],
462 [m4_map([$1], m4_defn([b4_type_names]))])
472 # Similar to POSIX basename; the differences don't matter here.
473 # Beware that NAME is not evaluated.
474 m4_define([b4_basename],
475 [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
478 # b4_syncline(LINE, FILE)
479 # -----------------------
480 m4_define([b4_syncline],
481 [b4_flag_if([synclines],
482 [b4_sync_end([__line__], [b4_basename(m4_quote(__file__))])
483 b4_sync_start([$1], [$2])])])
485 m4_define([b4_sync_end], [b4_comment([Line $1 of $2])])
486 m4_define([b4_sync_start], [b4_comment([Line $1 of $2])])
488 # b4_user_code(USER-CODE)
489 # -----------------------
490 # Emit code from the user, ending it with synclines.
491 m4_define([b4_user_code],
493 b4_syncline([@oline@], [@ofile@])])
496 # b4_define_user_code(MACRO)
497 # --------------------------
498 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
499 m4_define([b4_define_user_code],
500 [m4_define([b4_user_$1],
501 [b4_user_code([b4_$1])])])
505 # b4_user_initial_action
506 # b4_user_post_prologue
507 # b4_user_pre_prologue
509 # ----------------------
510 # Macros that issue user code, ending with synclines.
511 b4_define_user_code([actions])
512 b4_define_user_code([initial_action])
513 b4_define_user_code([post_prologue])
514 b4_define_user_code([pre_prologue])
515 b4_define_user_code([stype])
518 # b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
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).
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
526 # occurrence: name, start boundary, and end boundary. Empty string names are
527 # fine. An empty list is fine.
529 # For example, to define b4_foo_user_names to be used for USER-LIST with three
530 # name occurrences and with correct quoting:
532 # m4_define([b4_foo_user_names],
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]]]]]])
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.
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:
544 # m4_define([b4_foo_bison_names(bar)])
545 # m4_define([b4_foo_bison_names(baz)])
547 # To invoke b4_check_user_names with TYPE foo, with USER-LIST
548 # b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
551 # b4_check_user_names([[foo]], [b4_foo_user_names],
552 # [[b4_foo_bison_names]])
553 m4_define([b4_check_user_names],
554 [m4_foreach([b4_occurrence], $2,
555 [m4_pushdef([b4_occurrence], b4_occurrence)dnl
556 m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
557 m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
558 m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl
559 m4_ifndef($3[(]m4_quote(b4_user_name)[)],
560 [b4_complain_at([b4_start], [b4_end],
561 [[%s '%s' is not used]],
562 [$1], [b4_user_name])])[]dnl
563 m4_popdef([b4_occurrence])dnl
564 m4_popdef([b4_user_name])dnl
565 m4_popdef([b4_start])dnl
566 m4_popdef([b4_end])dnl
571 ## --------------------- ##
572 ## b4_percent_define_*. ##
573 ## --------------------- ##
576 # b4_percent_define_use(VARIABLE)
577 # -------------------------------
578 # Declare that VARIABLE was used.
579 m4_define([b4_percent_define_use],
580 [m4_define([b4_percent_define_bison_variables(]$1[)])dnl
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
588 # Bison's usage of VARIABLE by defining
589 # b4_percent_define_bison_variables(VARIABLE).
593 # b4_percent_define_get([[foo]])
594 m4_define([b4_percent_define_get],
595 [b4_percent_define_use([$1])dnl
596 m4_ifdef([b4_percent_define(]$1[)],
597 [m4_indir([b4_percent_define(]$1[)])],
600 # b4_percent_define_get_loc(VARIABLE)
601 # -----------------------------------
602 # Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly. That is,
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.
611 # b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
612 m4_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
616 m4_popdef([b4_loc])],
617 [b4_fatal([[b4_percent_define_get_loc: undefined %%define variable '%s']], [$1])])])
619 # b4_percent_define_get_syncline(VARIABLE)
620 # ----------------------------------------
621 # Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly.
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.
630 # b4_percent_define_get_syncline([[foo]])
631 m4_define([b4_percent_define_get_syncline],
632 [m4_ifdef([b4_percent_define_syncline(]$1[)],
633 [m4_indir([b4_percent_define_syncline(]$1[)])],
634 [b4_fatal([[b4_percent_define_get_syncline: undefined %%define variable '%s']], [$1])])])
636 # b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
637 # ------------------------------------------------------
638 # Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly. That is,
639 # if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
640 # IF-FALSE. Also, record Bison's usage of VARIABLE by defining
641 # b4_percent_define_bison_variables(VARIABLE).
645 # b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
646 m4_define([b4_percent_define_ifdef],
647 [m4_ifdef([b4_percent_define(]$1[)],
648 [b4_percent_define_use([$1])$2],
657 # b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
658 # --------------------------------------------------------
659 # Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly. That is,
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).
669 # b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
670 m4_define([b4_percent_define_flag_if],
671 [b4_percent_define_ifdef([$1],
672 [m4_case(b4_percent_define_get([$1]),
673 [], [$2], [true], [$2], [false], [$3],
674 [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
675 [[invalid value for %%define Boolean variable '%s']],
677 [[b4_percent_define_flag_if($1)]])])],
678 [b4_fatal([[b4_percent_define_flag_if: undefined %%define variable '%s']], [$1])])])
681 # b4_percent_define_default(VARIABLE, DEFAULT)
682 # --------------------------------------------
683 # Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly. That is,
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.
690 # b4_percent_define_default([[foo]], [[default value]])
691 m4_define([b4_percent_define_default],
692 [m4_ifndef([b4_percent_define(]$1[)],
693 [m4_define([b4_percent_define(]$1[)], [$2])dnl
694 m4_define([b4_percent_define_loc(]$1[)],
695 [[[[<skeleton default value>:-1.-1]],
696 [[<skeleton default value>:-1.-1]]]])dnl
697 m4_define([b4_percent_define_syncline(]$1[)], [[]])])])
700 # b4_percent_define_if_define(VARIABLE)
701 # -------------------------------------
702 # Define b4_VARIABLE_if that executes its $1 or $2 depending whether
703 # VARIABLE was %defined. The characters `.' and `-' in VARIABLE are mapped
705 m4_define([b4_percent_define_if_define_],
706 [m4_define(m4_bpatsubst([b4_$1_if], [[-.]], [_]),
707 [b4_percent_define_flag_if([$1], [$2], [$3])])])
708 m4_define([b4_percent_define_if_define],
709 [b4_percent_define_default([[$1]], [[false]])
710 b4_percent_define_if_define_([$1], $[1], $[2])])
713 # b4_percent_define_check_values(VALUES)
714 # --------------------------------------
715 # Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly
716 # except that the VALUES structure is more appropriate for M4. That is, VALUES
717 # is a list of sublists of strings. For each sublist, the first string is the
718 # name of a %define variable, and all remaining strings in that sublist are the
719 # valid values for that variable. Complain if such a variable is undefined (a
720 # Bison error since the default value should have been set already) or defined
721 # to any other value (possibly a user error). Don't record this as a Bison
722 # usage of the variable as there's no reason to suspect that the value has yet
723 # influenced the output.
727 # b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
728 # [[[[bar]], [[bar-value1]]]])
729 m4_define([b4_percent_define_check_values],
730 [m4_foreach([b4_sublist], m4_quote($@),
731 [_b4_percent_define_check_values(b4_sublist)])])
733 m4_define([_b4_percent_define_check_values],
734 [m4_ifdef([b4_percent_define(]$1[)],
735 [m4_pushdef([b4_good_value], [0])dnl
737 [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
738 [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
739 [m4_define([b4_good_value], [1])])])])dnl
740 m4_if(b4_good_value, [0],
741 [b4_complain_at(b4_percent_define_get_loc([$1]),
742 [[invalid value for %%define variable '%s': '%s']],
744 m4_dquote(m4_indir([b4_percent_define(]$1[)])))
745 m4_foreach([b4_value], m4_dquote(m4_shift($@)),
746 [b4_error([[note]], b4_percent_define_get_loc([$1]), []
747 [[accepted value: '%s']],
748 m4_dquote(b4_value))])])dnl
749 m4_popdef([b4_good_value])],
750 [b4_fatal([[b4_percent_define_check_values: undefined %%define variable '%s']], [$1])])])
752 # b4_percent_code_get([QUALIFIER])
753 # --------------------------------
754 # If any %code blocks for QUALIFIER are defined, emit them beginning with a
755 # comment and ending with synclines and a newline. If QUALIFIER is not
756 # specified or empty, do this for the unqualified %code blocks. Also, record
757 # Bison's usage of QUALIFIER (if specified) by defining
758 # b4_percent_code_bison_qualifiers(QUALIFIER).
760 # For example, to emit any unqualified %code blocks followed by any %code
761 # blocks for the qualifier foo:
763 # b4_percent_code_get
764 # b4_percent_code_get([[foo]])
765 m4_define([b4_percent_code_get],
766 [m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
767 m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
768 m4_ifdef(b4_macro_name,
769 [b4_comment([m4_if([$#], [0], [[Unqualified %code]],
770 [["%code ]$1["]])[ blocks.]])
771 b4_user_code([m4_indir(b4_macro_name)])
773 m4_popdef([b4_macro_name])])
775 # b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
776 # -----------------------------------------------------
777 # If any %code blocks for QUALIFIER (or unqualified %code blocks if
778 # QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
779 # Also, record Bison's usage of QUALIFIER (if specified) by defining
780 # b4_percent_code_bison_qualifiers(QUALIFIER).
781 m4_define([b4_percent_code_ifdef],
782 [m4_ifdef([b4_percent_code(]$1[)],
783 [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
787 ## ------------------ ##
788 ## Common variables. ##
789 ## ------------------ ##
791 # Default values for %define.
792 # ---------------------------
793 # If the api.token.prefix, it is empty.
794 m4_percent_define_default([[api.token.prefix]], [[]])
796 # b4_parse_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
797 # b4_parse_trace_if([IF-DEBUG-TRACES-ARE-ENABLED], [IF-NOT])
798 # b4_lex_symbol_if([IF-YYLEX-RETURNS-A-COMPLETE-SYMBOL], [IF-NOT])
799 # b4_variant_if([IF-VARIANT-ARE-USED], [IF-NOT])
800 # ----------------------------------------------
801 b4_percent_define_if_define([lex_symbol])
802 b4_percent_define_if_define([locations]) # Whether locations are tracked.
803 b4_percent_define_if_define([parse.assert])
804 b4_percent_define_if_define([parse.trace])
805 b4_percent_define_if_define([variant])
808 # b4_error_verbose_if([IF-ERRORS-ARE-VERBOSE], [IF-NOT])
809 # ------------------------------------------------------
810 # Map %define parse.error "(simple|verbose)" to b4_error_verbose_if and
811 # b4_error_verbose_flag.
812 b4_percent_define_default([[parse.error]], [[simple]])
813 b4_percent_define_check_values([[[[parse.error]],
814 [[simple]], [[verbose]]]])
815 m4_define([b4_error_verbose_flag],
816 [m4_case(b4_percent_define_get([[parse.error]]),
819 b4_define_flag_if([error_verbose])
821 # yytoken_table is needed to support verbose errors.
822 b4_error_verbose_if([m4_define([b4_token_table_flag], [1])])
825 ## ----------------------------------------------------------- ##
826 ## After processing the skeletons, check that all the user's ##
827 ## %define variables and %code qualifiers were used by Bison. ##
828 ## ----------------------------------------------------------- ##
830 m4_define([b4_check_user_names_wrap],
831 [m4_ifdef([b4_percent_]$1[_user_]$2[s],
832 [b4_check_user_names([[%]$1 $2],
833 [b4_percent_]$1[_user_]$2[s],
834 [[b4_percent_]$1[_bison_]$2[s]])])])
837 b4_check_user_names_wrap([[define]], [[variable]])
838 b4_check_user_names_wrap([[code]], [[qualifier]])
842 ## ---------------- ##
843 ## Default values. ##
844 ## ---------------- ##
846 # m4_define_default([b4_lex_param], []) dnl breaks other skeletons
847 m4_define_default([b4_pre_prologue], [])
848 m4_define_default([b4_post_prologue], [])
849 m4_define_default([b4_epilogue], [])
850 m4_define_default([b4_parse_param], [])
852 # The initial column and line.
853 m4_define_default([b4_location_initial_column], [1])
854 m4_define_default([b4_location_initial_line], [1])
857 b4_percent_define_ifdef([api.prefix],
858 [m4_ifdef([b4_prefix],
859 [b4_complain_at(b4_percent_define_get_loc([api.prefix]),
860 [['%s' and '%s' cannot be used together]],
862 [%define api.prefix])])])