3 # Language-independent M4 Macros for Bison.
5 # Copyright (C) 2002, 2004-2013 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.])])
68 # b4_output_begin(FILE)
69 # ---------------------
70 # Enable output, i.e., send to diversion 0, expand after "#", and
71 # generate the tag to output into FILE. Must be followed by EOL.
72 m4_define([b4_output_begin],
75 @output(m4_unquote([$1])@)@dnl
81 # Output nothing, restore # as comment character (no expansions after #).
82 m4_define([b4_output_end],
88 # b4_divert_kill(CODE)
89 # --------------------
90 # Expand CODE for its side effects, discard its output.
91 m4_define([b4_divert_kill],
92 [m4_divert_text([KILL], [$1])])
95 # b4_define_silent(MACRO, CODE)
96 # -----------------------------
97 # Same as m4_define, but throw away the expansion of CODE.
98 m4_define([b4_define_silent],
99 [m4_define([$1], [b4_divert_kill([$2])])])
102 ## ---------------- ##
103 ## Error handling. ##
104 ## ---------------- ##
106 # The following error handling macros print error directives that should not
107 # become arguments of other macro invocations since they would likely then be
108 # mangled. Thus, they print to stdout directly.
112 # Write TEXT to stdout. Precede the final newline with an @ so that it's
113 # escaped. For example:
115 # b4_cat([[@complain(invalid input@)]])
117 [m4_syscmd([cat <<'_m4eof'
118 ]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@
121 m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])])
123 # b4_error(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
124 # -------------------------------------------------------
125 # Write @KIND(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
129 # b4_error([[complain]], [[input.y:2.3]], [[input.y:5.4]],
130 # [[invalid %s]], [[foo]])
131 m4_define([b4_error],
132 [b4_cat([[@complain][(]$1[@,]$2[@,]$3[@,]$4[]]dnl
133 [m4_if([$#], [4], [],
134 [m4_foreach([b4_arg],
135 m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))),
136 [[@,]b4_arg])])[@)]])])
138 # b4_warn(FORMAT, [ARG1], [ARG2], ...)
139 # ------------------------------------
140 # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
144 # b4_warn([[invalid value for '%s': %s]], [[foo]], [[3]])
146 # As a simple test suite, this:
149 # m4_define([asdf], [ASDF])
150 # m4_define([fsa], [FSA])
151 # m4_define([fdsa], [FDSA])
152 # b4_warn_at([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
153 # b4_warn_at([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
158 # Should produce this without newlines:
160 # @warn_at([asdf), asdf]@,@,@,[fsa), fsa]@,[fdsa), fdsa]@)
161 # @warn(asdf), asdf@,@,@,fsa), fsa@,fdsa), fdsa@)
166 [b4_error([[warn]], [], [], $@)])
168 # b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
169 # ---------------------------------------------------
170 # Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
174 # b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
175 m4_define([b4_warn_at],
176 [b4_error([[warn]], $@)])
178 # b4_complain(FORMAT, [ARG1], [ARG2], ...)
179 # ----------------------------------------
180 # Bounce to b4_complain_at.
182 # See b4_warn example.
183 m4_define([b4_complain],
184 [b4_error([[complain]], [], [], $@)])
186 # b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
187 # -------------------------------------------------------
188 # Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
190 # See b4_warn_at example.
191 m4_define([b4_complain_at],
192 [b4_error([[complain]], $@)])
194 # b4_fatal(FORMAT, [ARG1], [ARG2], ...)
195 # -------------------------------------
196 # Bounce to b4_fatal_at.
198 # See b4_warn example.
199 m4_define([b4_fatal],
200 [b4_error([[fatal]], [], [], $@)dnl
203 # b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
204 # ----------------------------------------------------
205 # Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
207 # See b4_warn_at example.
208 m4_define([b4_fatal_at],
209 [b4_error([[fatal]], $@)dnl
217 # b4_ints_in(INT1, INT2, LOW, HIGH)
218 # ---------------------------------
219 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
220 m4_define([b4_ints_in],
221 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
224 # b4_subtract(LHS, RHS)
225 # ---------------------
226 # Evaluate LHS - RHS if they are integer literals, otherwise expand
228 m4_define([b4_subtract],
229 [m4_bmatch([$1$2], [^[0123456789]*$],
230 [m4_eval([$1 - $2])],
234 # _b4_join(ARG1, ...)
235 # -------------------
236 # Join with comma, skipping empty arguments.
237 # b4_join calls itself recursively until it sees the first non-empty
238 # argument, then calls _b4_join which prepends each non-empty argument
244 [$1[]_$0(m4_shift($@))],
245 [$0(m4_shift($@))])])])
247 # _b4_join(ARGS1, ...)
248 # --------------------
249 m4_define([_b4_join],
252 [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])])
257 # b4_integral_parser_tables_map(MACRO)
258 # -------------------------------------
259 # Map MACRO on all the integral tables. MACRO is expected to have
260 # the signature MACRO(TABLE-NAME, CONTENT, COMMENT).
261 m4_define([b4_integral_parser_tables_map],
262 [$1([pact], [b4_pact],
263 [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
266 $1([defact], [b4_defact],
267 [[YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
268 Performed when YYTABLE does not specify something else to do. Zero
269 means the default is an error.]])
271 $1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]])
273 $1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]])
275 $1([table], [b4_table],
276 [[YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If
277 positive, shift that token. If negative, reduce the rule whose
278 number is the opposite. If YYTABLE_NINF, syntax error.]])
280 $1([check], [b4_check])
282 $1([stos], [b4_stos],
283 [[YYSTOS[STATE-NUM] -- The (internal number of the) accessing
284 symbol of state STATE-NUM.]])
287 [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]])
290 [[YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.]])
294 # b4_parser_tables_declare
295 # b4_parser_tables_define
296 # ------------------------
297 # Define/declare the (deterministic) parser tables.
298 m4_define([b4_parser_tables_declare],
299 [b4_integral_parser_tables_map([b4_integral_parser_table_declare])])
301 m4_define([b4_parser_tables_define],
302 [b4_integral_parser_tables_map([b4_integral_parser_table_define])])
306 ## ------------------ ##
307 ## Decoding options. ##
308 ## ------------------ ##
310 # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
311 # -----------------------------------
312 # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
313 m4_define([b4_flag_if],
317 [m4_fatal([invalid $1 value: ]b4_$1_flag)])])
320 # b4_define_flag_if(FLAG)
321 # -----------------------
322 # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
323 # value of the Boolean FLAG.
324 m4_define([b4_define_flag_if],
325 [_b4_define_flag_if($[1], $[2], [$1])])
327 # _b4_define_flag_if($1, $2, FLAG)
328 # --------------------------------
329 # Work around the impossibility to define macros inside macros,
330 # because issuing '[$1]' is not possible in M4. GNU M4 should provide
332 m4_define([_b4_define_flag_if],
333 [m4_if([$1$2], $[1]$[2], [],
334 [m4_fatal([$0: Invalid arguments: $@])])dnl
335 m4_define([b4_$3_if],
336 [b4_flag_if([$3], [$1], [$2])])])
339 # b4_FLAG_if(IF-TRUE, IF-FALSE)
340 # -----------------------------
341 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
342 b4_define_flag_if([defines]) # Whether headers are requested.
343 b4_define_flag_if([glr]) # Whether a GLR parser is requested.
344 b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
345 b4_define_flag_if([token_table]) # Whether yytoken_table is demanded.
346 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
353 # In order to unify the handling of the various aspects of symbols
354 # (tag, type_name, whether terminal, etc.), bison.exe defines one
355 # macro per (token, field), where field can has_id, id, etc.: see
356 # src/output.c:prepare_symbols_definitions().
358 # The various FIELDS are:
361 # Whether the symbol has an id.
363 # If has_id, the id. Guaranteed to be usable as a C identifier.
364 # Prefixed by api.token.prefix if defined.
366 # A representat of the symbol. Can be 'foo', 'foo.id', '"foo"' etc.
367 # - user_number: integer
368 # The assigned (external) number as used by yylex.
370 # Whether this is a terminal symbol.
372 # The internalized number (used after yytranslate).
374 # Whether has a semantic value.
376 # When api.value.type=union, the generated name for the union member.
377 # yytype_INT etc. for symbols that has_id, otherwise yytype_1 etc.
379 # If it has a semantic value, its type tag, or, if variant are used,
381 # In the case of api.value.type=union, type is the real type (e.g. int).
382 # - has_printer: 0, 1
384 # - printer_file: string
385 # - printer_line: integer
386 # If the symbol has a printer, everything about it.
387 # - has_destructor, destructor, destructor_file, destructor_line
390 # The following macros provide access to these values.
392 # b4_symbol_(NUM, FIELD)
393 # ----------------------
394 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
396 m4_define([b4_symbol_],
397 [m4_indir([b4_symbol($1, $2)])])
400 # b4_symbol(NUM, FIELD)
401 # ---------------------
402 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
403 # undefined. If FIELD = id, prepend the token prefix.
404 m4_define([b4_symbol],
406 [id], [m4_do([b4_percent_define_get([api.token.prefix])],
407 [b4_symbol_([$1], [id])])],
411 # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
412 # -------------------------------------------
413 # If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE.
414 # Otherwise an error.
415 m4_define([b4_symbol_if],
416 [m4_case(b4_symbol([$1], [$2]),
419 [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])])
422 # b4_symbol_tag_comment(SYMBOL-NUM)
423 # ---------------------------------
424 # Issue a comment giving the tag of symbol NUM.
425 m4_define([b4_symbol_tag_comment],
426 [b4_comment([b4_symbol([$1], [tag])])
430 # b4_symbol_action_location(SYMBOL-NUM, KIND)
431 # -------------------------------------------
432 # Report the location of the KIND action as FILE:LINE.
433 m4_define([b4_symbol_action_location],
434 [b4_symbol([$1], [$2_file]):b4_syncline([b4_symbol([$1], [$2_line])])])
437 # b4_symbol_action(SYMBOL-NUM, KIND)
438 # ----------------------------------
439 # Run the action KIND (destructor or printer) for SYMBOL-NUM.
440 # Same as in C, but using references instead of pointers.
441 m4_define([b4_symbol_action],
442 [b4_symbol_if([$1], [has_$2],
443 [b4_dollar_pushdef([(*yyvaluep)],
444 b4_symbol_if([$1], [has_type],
445 [m4_dquote(b4_symbol([$1], [type]))]),
447 b4_symbol_case_([$1])[]dnl
448 b4_syncline([b4_symbol([$1], [$2_line])], ["b4_symbol([$1], [$2_file])"])
449 b4_symbol([$1], [$2])
450 b4_syncline([@oline@], [@ofile@])
453 b4_dollar_popdef[]dnl
457 # b4_symbol_destructor(SYMBOL-NUM)
458 # b4_symbol_printer(SYMBOL-NUM)
459 # --------------------------------
460 m4_define([b4_symbol_destructor], [b4_symbol_action([$1], [destructor])])
461 m4_define([b4_symbol_printer], [b4_symbol_action([$1], [printer])])
464 # b4_symbol_actions(KIND, [TYPE = yytype])
465 # ----------------------------------------
466 # Emit the symbol actions for KIND ("printer" or "destructor").
468 m4_define([b4_symbol_actions],
469 [m4_pushdef([b4_actions_], m4_expand([b4_symbol_foreach([b4_symbol_$1])]))dnl
470 m4_ifval(m4_defn([b4_actions_]),
471 [switch (m4_default([$2], [yytype]))
473 m4_defn([b4_actions_])
478 [YYUSE (m4_default([$2], [yytype]));])dnl
479 m4_popdef([b4_actions_])dnl
482 # b4_symbol_case_(SYMBOL-NUM)
483 # ---------------------------
484 # Issue a "case NUM" for SYMBOL-NUM.
485 m4_define([b4_symbol_case_],
486 [case b4_symbol([$1], [number]): b4_symbol_tag_comment([$1])])
490 # b4_symbol_foreach(MACRO)
491 # ------------------------
492 # Invoke MACRO(SYMBOL-NUM) for each SYMBOL-NUM.
493 m4_define([b4_symbol_foreach],
494 [m4_map([$1], m4_defn([b4_symbol_numbers]))])
496 # b4_symbol_map(MACRO)
497 # --------------------
498 # Return a list (possibly empty elements) of MACRO invoked for each
500 m4_define([b4_symbol_map],
501 [m4_map_args_sep([$1(], [)], [,], b4_symbol_numbers)])
504 # b4_token_visible_if(NUM, IF-TRUE, IF-FALSE)
505 # -------------------------------------------
506 # Whether NUM denotes a token that has an exported definition (i.e.,
507 # shows in enum yytokentype).
508 m4_define([b4_token_visible_if],
509 [b4_symbol_if([$1], [is_token],
510 [b4_symbol_if([$1], [has_id], [$2], [$3])],
513 # b4_token_has_definition(NUM)
514 # ----------------------------
515 # 1 if NUM is visible, nothing otherwise.
516 m4_define([b4_token_has_definition],
517 [b4_token_visible_if([$1], [1])])
519 # b4_any_token_visible_if([IF-TRUE], [IF-FALSE])
520 # ----------------------------------------------
521 # Whether there is a token that needs to be defined.
522 m4_define([b4_any_token_visible_if],
523 [m4_ifval(b4_symbol_foreach([b4_token_has_definition]),
527 # b4_token_format(FORMAT, NUM)
528 # ----------------------------
529 m4_define([b4_token_format],
530 [b4_token_visible_if([$2],
531 [m4_quote(m4_format([$1],
532 [b4_symbol([$2], [id])],
533 [b4_symbol([$2], [user_number])]))])])
540 # b4_type_action_(NUMS)
541 # ---------------------
542 # Run actions for the symbol NUMS that all have the same type-name.
543 # Skip NUMS that have no type-name.
545 # To specify the action to run, define b4_dollar_dollar(NUMBER,
547 m4_define([b4_type_action_],
548 [b4_symbol_if([$1], [has_type],
549 [m4_map([ b4_symbol_case_], [$@])[]dnl
550 b4_dollar_dollar([b4_symbol([$1], [number])],
551 [b4_symbol([$1], [tag])],
552 [b4_symbol([$1], [type])]);
557 # b4_type_foreach(MACRO)
558 # ----------------------
559 # Invoke MACRO(SYMBOL-NUMS) for each set of SYMBOL-NUMS for each type set.
560 m4_define([b4_type_foreach],
561 [m4_map([$1], m4_defn([b4_type_names]))])
571 # Similar to POSIX basename; the differences don't matter here.
572 # Beware that NAME is not evaluated.
573 m4_define([b4_basename],
574 [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
577 # b4_syncline(LINE, FILE)
578 # -----------------------
579 m4_define([b4_syncline],
580 [b4_flag_if([synclines],
581 [b4_sync_start([$1], [$2]) b4_sync_end([__line__],
582 [b4_basename(m4_quote(__file__))])[]dnl
585 # b4_sync_start(LINE, FILE)
586 # -----------------------
587 # Syncline for the new place. Typically a directive for the compiler.
588 m4_define([b4_sync_start], [b4_comment([$2:$1])])
590 # b4_sync_end(LINE, FILE)
591 # -----------------------
592 # Syncline for the current place, which ends. Typically a comment
593 # left for the reader.
594 m4_define([b4_sync_end], [b4_comment([$2:$1])])
597 # b4_user_code(USER-CODE)
598 # -----------------------
599 # Emit code from the user, ending it with synclines.
600 m4_define([b4_user_code],
602 b4_syncline([@oline@], [@ofile@])])
605 # b4_define_user_code(MACRO)
606 # --------------------------
607 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
608 m4_define([b4_define_user_code],
609 [m4_define([b4_user_$1],
610 [b4_user_code([b4_$1])])])
614 # b4_user_initial_action
615 # b4_user_post_prologue
616 # b4_user_pre_prologue
617 # b4_user_union_members
618 # ----------------------
619 # Macros that issue user code, ending with synclines.
620 b4_define_user_code([actions])
621 b4_define_user_code([initial_action])
622 b4_define_user_code([post_prologue])
623 b4_define_user_code([pre_prologue])
624 b4_define_user_code([union_members])
627 # b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
628 # -----------------------------------------------------
629 # Complain if any name of type WHAT is used by the user (as recorded in
630 # USER-LIST) but is not used by Bison (as recorded by macros in the
631 # namespace BISON-NAMESPACE).
633 # USER-LIST must expand to a list specifying all user occurrences of all names
634 # of type WHAT. Each item in the list must be a triplet specifying one
635 # occurrence: name, start boundary, and end boundary. Empty string names are
636 # fine. An empty list is fine.
638 # For example, to define b4_foo_user_names to be used for USER-LIST with three
639 # name occurrences and with correct quoting:
641 # m4_define([b4_foo_user_names],
642 # [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]],
643 # [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]],
644 # [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]])
646 # The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT
647 # is used by Bison (in the front-end or in the skeleton). Empty string names
648 # are fine, but it would be ugly for Bison to actually use one.
650 # For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that
651 # the names bar and baz are used by Bison:
653 # m4_define([b4_foo_bison_names(bar)])
654 # m4_define([b4_foo_bison_names(baz)])
656 # To invoke b4_check_user_names with TYPE foo, with USER-LIST
657 # b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
660 # b4_check_user_names([[foo]], [b4_foo_user_names],
661 # [[b4_foo_bison_names]])
662 m4_define([b4_check_user_names],
663 [m4_foreach([b4_occurrence], $2,
664 [m4_pushdef([b4_occurrence], b4_occurrence)dnl
665 m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
666 m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
667 m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl
668 m4_ifndef($3[(]m4_quote(b4_user_name)[)],
669 [b4_complain_at([b4_start], [b4_end],
670 [[%s '%s' is not used]],
671 [$1], [b4_user_name])])[]dnl
672 m4_popdef([b4_occurrence])dnl
673 m4_popdef([b4_user_name])dnl
674 m4_popdef([b4_start])dnl
675 m4_popdef([b4_end])dnl
680 ## --------------------- ##
681 ## b4_percent_define_*. ##
682 ## --------------------- ##
685 # b4_percent_define_use(VARIABLE)
686 # -------------------------------
687 # Declare that VARIABLE was used.
688 m4_define([b4_percent_define_use],
689 [m4_define([b4_percent_define_bison_variables(]$1[)])dnl
692 # b4_percent_define_get(VARIABLE, [DEFAULT])
693 # ------------------------------------------
694 # Mimic muscle_percent_define_get in ../src/muscle-tab.h. That is, if
695 # the %define variable VARIABLE is defined, emit its value. Contrary
696 # to its C counterpart, return DEFAULT otherwise. Also, record
697 # Bison's usage of VARIABLE by defining
698 # b4_percent_define_bison_variables(VARIABLE).
702 # b4_percent_define_get([[foo]])
703 m4_define([b4_percent_define_get],
704 [b4_percent_define_use([$1])dnl
705 b4_percent_define_ifdef_([$1],
706 [m4_indir([b4_percent_define(]$1[)])],
709 # b4_percent_define_get_loc(VARIABLE)
710 # -----------------------------------
711 # Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly. That is,
712 # if the %define variable VARIABLE is undefined, complain fatally since that's
713 # a Bison or skeleton error. Otherwise, return its definition location in a
714 # form approriate for the first two arguments of b4_warn_at, b4_complain_at, or
715 # b4_fatal_at. Don't record this as a Bison usage of VARIABLE as there's no
716 # reason to suspect that the user-supplied value has yet influenced the output.
720 # b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
721 m4_define([b4_percent_define_get_loc],
722 [m4_ifdef([b4_percent_define_loc(]$1[)],
723 [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl
725 m4_popdef([b4_loc])],
726 [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
728 # b4_percent_define_get_kind(VARIABLE)
729 # ------------------------------------
730 # Get the kind (code, keyword, string) of VARIABLE, i.e., how its
731 # value was defined (braces, not delimiters, quotes).
733 # If the %define variable VARIABLE is undefined, complain fatally
734 # since that's a Bison or skeleton error. Don't record this as a
735 # Bison usage of VARIABLE as there's no reason to suspect that the
736 # user-supplied value has yet influenced the output.
737 m4_define([b4_percent_define_get_kind],
738 [m4_ifdef([b4_percent_define_kind(]$1[)],
739 [m4_indir([b4_percent_define_kind(]$1[)])],
740 [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
742 # b4_percent_define_get_syncline(VARIABLE)
743 # ----------------------------------------
744 # Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly.
745 # That is, if the %define variable VARIABLE is undefined, complain fatally
746 # since that's a Bison or skeleton error. Otherwise, return its definition
747 # location as a b4_syncline invocation. Don't record this as a Bison usage of
748 # VARIABLE as there's no reason to suspect that the user-supplied value has yet
749 # influenced the output.
753 # b4_percent_define_get_syncline([[foo]])
754 m4_define([b4_percent_define_get_syncline],
755 [m4_ifdef([b4_percent_define_syncline(]$1[)],
756 [m4_indir([b4_percent_define_syncline(]$1[)])],
757 [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
759 # b4_percent_define_ifdef_(VARIABLE, IF-TRUE, [IF-FALSE])
760 # ------------------------------------------------------
761 # If the %define variable VARIABLE is defined, expand IF-TRUE, else expand
762 # IF-FALSE. Don't record usage of VARIABLE.
766 # b4_percent_define_ifdef_([[foo]], [[it's defined]], [[it's undefined]])
767 m4_define([b4_percent_define_ifdef_],
768 [m4_ifdef([b4_percent_define(]$1[)],
772 # b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
773 # ------------------------------------------------------
774 # Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly. That is,
775 # if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
776 # IF-FALSE. Also, record Bison's usage of VARIABLE by defining
777 # b4_percent_define_bison_variables(VARIABLE).
781 # b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
782 m4_define([b4_percent_define_ifdef],
783 [b4_percent_define_ifdef_([$1],
784 [b4_percent_define_use([$1])$2],
793 # b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
794 # --------------------------------------------------------
795 # Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly. That is,
796 # if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE.
797 # If it is defined to "false", expand IF-FALSE. Complain if it is undefined
798 # (a Bison or skeleton error since the default value should have been set
799 # already) or defined to any other value (possibly a user error). Also, record
800 # Bison's usage of VARIABLE by defining
801 # b4_percent_define_bison_variables(VARIABLE).
805 # b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
806 m4_define([b4_percent_define_flag_if],
807 [b4_percent_define_ifdef([$1],
808 [m4_case(b4_percent_define_get([$1]),
809 [], [$2], [true], [$2], [false], [$3],
810 [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
811 [[invalid value for %%define Boolean variable '%s']],
813 [[b4_percent_define_flag_if($1)]])])],
814 [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
817 # b4_percent_define_default(VARIABLE, DEFAULT, [KIND = keyword])
818 # --------------------------------------------------------------
819 # Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly. That is,
820 # if the %define variable VARIABLE is undefined, set its value to DEFAULT.
821 # Don't record this as a Bison usage of VARIABLE as there's no reason to
822 # suspect that the value has yet influenced the output.
826 # b4_percent_define_default([[foo]], [[default value]])
827 m4_define([b4_percent_define_default],
828 [b4_percent_define_ifdef_([$1], [],
829 [m4_define([b4_percent_define(]$1[)], [$2])dnl
830 m4_define([b4_percent_define_kind(]$1[)],
831 [m4_default([$3], [keyword])])dnl
832 m4_define([b4_percent_define_loc(]$1[)],
833 [[[[<skeleton default value>:-1.-1]],
834 [[<skeleton default value>:-1.-1]]]])dnl
835 m4_define([b4_percent_define_syncline(]$1[)], [[]])])])
838 # b4_percent_define_if_define(NAME, [VARIABLE = NAME])
839 # ----------------------------------------------------
840 # Define b4_NAME_if that executes its $1 or $2 depending whether
841 # VARIABLE was %defined. The characters '.' and `-' in VARIABLE are mapped
843 m4_define([b4_percent_define_if_define_],
844 [m4_define(m4_bpatsubst([b4_$1_if], [[-.]], [_]),
845 [b4_percent_define_flag_if(m4_default([$2], [$1]),
847 m4_define([b4_percent_define_if_define],
848 [b4_percent_define_default([m4_default([$2], [$1])], [[false]])
849 b4_percent_define_if_define_([$1], [$2], $[1], $[2])])
852 # b4_percent_define_check_kind(VARIABLE, KIND, [DIAGNOSTIC = complain])
853 # ---------------------------------------------------------------------
854 m4_define([b4_percent_define_check_kind],
855 [b4_percent_define_ifdef_([$1],
856 [m4_if(b4_percent_define_get_kind([$1]), [$2], [],
857 [b4_error([m4_default([$3], [complain])],
858 b4_percent_define_get_loc([$1]),
860 [code], [[%%define variable '%s' requires '{...}' values]],
861 [keyword], [[%%define variable '%s' requires keyword values]],
862 [string], [[%%define variable '%s' requires '"..."' values]])],
867 # b4_percent_define_check_values(VALUES)
868 # --------------------------------------
869 # Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly
870 # except that the VALUES structure is more appropriate for M4. That is, VALUES
871 # is a list of sublists of strings. For each sublist, the first string is the
872 # name of a %define variable, and all remaining strings in that sublist are the
873 # valid values for that variable. Complain if such a variable is undefined (a
874 # Bison error since the default value should have been set already) or defined
875 # to any other value (possibly a user error). Don't record this as a Bison
876 # usage of the variable as there's no reason to suspect that the value has yet
877 # influenced the output.
881 # b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
882 # [[[[bar]], [[bar-value1]]]])
883 m4_define([b4_percent_define_check_values],
884 [m4_foreach([b4_sublist], m4_quote($@),
885 [_b4_percent_define_check_values(b4_sublist)])])
887 m4_define([_b4_percent_define_check_values],
888 [b4_percent_define_ifdef_([$1],
889 [b4_percent_define_check_kind(]$1[, [keyword], [deprecated])dnl
890 m4_pushdef([b4_good_value], [0])dnl
892 [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
893 [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
894 [m4_define([b4_good_value], [1])])])])dnl
895 m4_if(b4_good_value, [0],
896 [b4_complain_at(b4_percent_define_get_loc([$1]),
897 [[invalid value for %%define variable '%s': '%s']],
899 m4_dquote(m4_indir([b4_percent_define(]$1[)])))
900 m4_foreach([b4_value], m4_dquote(m4_shift($@)),
901 [b4_error([[note]], b4_percent_define_get_loc([$1]), []
902 [[accepted value: '%s']],
903 m4_dquote(b4_value))])])dnl
904 m4_popdef([b4_good_value])],
905 [b4_fatal([[$0: undefined %%define variable '%s']], [$1])])])
907 # b4_percent_code_get([QUALIFIER])
908 # --------------------------------
909 # If any %code blocks for QUALIFIER are defined, emit them beginning with a
910 # comment and ending with synclines and a newline. If QUALIFIER is not
911 # specified or empty, do this for the unqualified %code blocks. Also, record
912 # Bison's usage of QUALIFIER (if specified) by defining
913 # b4_percent_code_bison_qualifiers(QUALIFIER).
915 # For example, to emit any unqualified %code blocks followed by any %code
916 # blocks for the qualifier foo:
918 # b4_percent_code_get
919 # b4_percent_code_get([[foo]])
920 m4_define([b4_percent_code_get],
921 [m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
922 m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
923 m4_ifdef(b4_macro_name,
924 [b4_comment([m4_if([$#], [0], [[Unqualified %code]],
925 [["%code ]$1["]])[ blocks.]])
926 b4_user_code([m4_indir(b4_macro_name)])
928 m4_popdef([b4_macro_name])])
930 # b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
931 # -----------------------------------------------------
932 # If any %code blocks for QUALIFIER (or unqualified %code blocks if
933 # QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
934 # Also, record Bison's usage of QUALIFIER (if specified) by defining
935 # b4_percent_code_bison_qualifiers(QUALIFIER).
936 m4_define([b4_percent_code_ifdef],
937 [m4_ifdef([b4_percent_code(]$1[)],
938 [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
942 ## ------------------ ##
943 ## Common variables. ##
944 ## ------------------ ##
947 # b4_parse_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
948 # b4_parse_trace_if([IF-DEBUG-TRACES-ARE-ENABLED], [IF-NOT])
949 # b4_token_ctor_if([IF-YYLEX-RETURNS-A-TOKEN], [IF-NOT])
950 # ----------------------------------------------
951 b4_percent_define_if_define([token_ctor], [api.token.constructor])
952 b4_percent_define_if_define([locations]) # Whether locations are tracked.
953 b4_percent_define_if_define([parse.assert])
954 b4_percent_define_if_define([parse.trace])
957 # b4_bison_locations_if([IF-TRUE])
958 # --------------------------------
959 # Expand IF-TRUE if using locations, and using the default location
961 m4_define([b4_bison_locations_if],
962 [b4_locations_if([b4_percent_define_ifdef([[api.location.type]], [], [$1])])])
965 # b4_error_verbose_if([IF-ERRORS-ARE-VERBOSE], [IF-NOT])
966 # ------------------------------------------------------
967 # Map %define parse.error "(simple|verbose)" to b4_error_verbose_if and
968 # b4_error_verbose_flag.
969 b4_percent_define_default([[parse.error]], [[simple]])
970 b4_percent_define_check_values([[[[parse.error]],
971 [[simple]], [[verbose]]]])
972 m4_define([b4_error_verbose_flag],
973 [m4_case(b4_percent_define_get([[parse.error]]),
976 b4_define_flag_if([error_verbose])
978 # yytoken_table is needed to support verbose errors.
979 b4_error_verbose_if([m4_define([b4_token_table_flag], [1])])
982 # b4_variant_if([IF-VARIANT-ARE-USED], [IF-NOT])
983 # ----------------------------------------------
984 b4_percent_define_if_define([variant])
985 m4_define([b4_variant_flag], [[0]])
986 b4_percent_define_ifdef([[api.value.type]],
987 [m4_case(b4_percent_define_get_kind([[api.value.type]]), [keyword],
988 [m4_case(b4_percent_define_get([[api.value.type]]), [variant],
989 [m4_define([b4_variant_flag], [[1]])])])])
990 b4_define_flag_if([variant])
993 ## ----------------------------------------------------------- ##
994 ## After processing the skeletons, check that all the user's ##
995 ## %define variables and %code qualifiers were used by Bison. ##
996 ## ----------------------------------------------------------- ##
998 m4_define([b4_check_user_names_wrap],
999 [m4_ifdef([b4_percent_]$1[_user_]$2[s],
1000 [b4_check_user_names([[%]$1 $2],
1001 [b4_percent_]$1[_user_]$2[s],
1002 [[b4_percent_]$1[_bison_]$2[s]])])])
1005 b4_check_user_names_wrap([[define]], [[variable]])
1006 b4_check_user_names_wrap([[code]], [[qualifier]])
1010 ## ---------------- ##
1011 ## Default values. ##
1012 ## ---------------- ##
1014 # m4_define_default([b4_lex_param], []) dnl breaks other skeletons
1015 m4_define_default([b4_pre_prologue], [])
1016 m4_define_default([b4_post_prologue], [])
1017 m4_define_default([b4_epilogue], [])
1018 m4_define_default([b4_parse_param], [])
1020 # The initial column and line.
1021 m4_define_default([b4_location_initial_column], [1])
1022 m4_define_default([b4_location_initial_line], [1])
1025 ## --------------- ##
1026 ## Sanity checks. ##
1027 ## --------------- ##
1029 # api.location.prefix={...} (Java and C++).
1030 b4_percent_define_check_kind([api.location.type], [code], [deprecated])
1032 # api.position.prefix={...} (Java).
1033 b4_percent_define_check_kind([api.position.type], [code], [deprecated])
1035 # api.prefix >< %name-prefix.
1036 b4_percent_define_check_kind([api.prefix], [code], [deprecated])
1037 b4_percent_define_ifdef([api.prefix],
1038 [m4_ifdef([b4_prefix],
1039 [b4_complain_at(b4_percent_define_get_loc([api.prefix]),
1040 [['%s' and '%s' cannot be used together]],
1042 [%define api.prefix])])])
1044 # api.token.prefix={...}
1045 # Make it a warning for those who used betas of Bison 3.0.
1046 b4_percent_define_check_kind([api.token.prefix], [code], [deprecated])
1048 # api.value.type >< %union.
1049 b4_percent_define_ifdef([api.value.type],
1050 [m4_ifdef([b4_union_members],
1051 [b4_complain_at(b4_percent_define_get_loc([api.value.type]),
1052 [['%s' and '%s' cannot be used together]],
1054 [%define api.value.type])])])
1056 # api.value.type=union >< %yacc.
1057 b4_percent_define_ifdef([api.value.type],
1058 [m4_if(b4_percent_define_get([api.value.type]), [union],
1060 [b4_complain_at(b4_percent_define_get_loc([api.value.type]),
1061 [['%s' and '%s' cannot be used together]],
1063 [%define api.value.type "union"])])])])