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.])])
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 ## ---------------- ##
90 ## ---------------- ##
92 # The following error handling macros print error directives that should not
93 # become arguments of other macro invocations since they would likely then be
94 # mangled. Thus, they print to stdout directly.
98 # Write TEXT to stdout. Precede the final newline with an @ so that it's
99 # escaped. For example:
101 # b4_cat([[@complain(invalid input@)]])
103 [m4_syscmd([cat <<'_m4eof'
104 ]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@
107 m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])])
109 # b4_error(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
110 # -------------------------------------------------------
111 # Write @KIND(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
115 # b4_error([[complain]], [[input.y:2.3]], [[input.y:5.4]],
116 # [[invalid %s]], [[foo]])
117 m4_define([b4_error],
118 [b4_cat([[@complain][(]$1[@,]$2[@,]$3[@,]$4[]]dnl
119 [m4_if([$#], [4], [],
120 [m4_foreach([b4_arg],
121 m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))),
122 [[@,]b4_arg])])[@)]])])
124 # b4_warn(FORMAT, [ARG1], [ARG2], ...)
125 # ------------------------------------
126 # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
130 # b4_warn([[invalid value for '%s': %s]], [[foo]], [[3]])
132 # As a simple test suite, this:
135 # m4_define([asdf], [ASDF])
136 # m4_define([fsa], [FSA])
137 # m4_define([fdsa], [FDSA])
138 # b4_warn_at([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
139 # b4_warn_at([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
144 # Should produce this without newlines:
146 # @warn_at([asdf), asdf]@,@,@,[fsa), fsa]@,[fdsa), fdsa]@)
147 # @warn(asdf), asdf@,@,@,fsa), fsa@,fdsa), fdsa@)
152 [b4_error([[warn]], [], [], $@)])
154 # b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
155 # ---------------------------------------------------
156 # Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
160 # b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
161 m4_define([b4_warn_at],
162 [b4_error([[warn]], $@)])
164 # b4_complain(FORMAT, [ARG1], [ARG2], ...)
165 # ----------------------------------------
166 # Bounce to b4_complain_at.
168 # See b4_warn example.
169 m4_define([b4_complain],
170 [b4_error([[complain]], [], [], $@)])
172 # b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
173 # -------------------------------------------------------
174 # Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
176 # See b4_warn_at example.
177 m4_define([b4_complain_at],
178 [b4_error([[complain]], $@)])
180 # b4_fatal(FORMAT, [ARG1], [ARG2], ...)
181 # -------------------------------------
182 # Bounce to b4_fatal_at.
184 # See b4_warn example.
185 m4_define([b4_fatal],
186 [b4_error([[fatal]], [], [], $@)dnl
189 # b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
190 # ----------------------------------------------------
191 # Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
193 # See b4_warn_at example.
194 m4_define([b4_fatal_at],
195 [b4_error([[fatal]], $@)dnl
203 # b4_ints_in(INT1, INT2, LOW, HIGH)
204 # ---------------------------------
205 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
206 m4_define([b4_ints_in],
207 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
210 # b4_subtract(LHS, RHS)
211 # ---------------------
212 # Evaluate LHS - RHS if they are integer literals, otherwise expand
214 m4_define([b4_subtract],
215 [m4_bmatch([$1$2], [^[0123456789]*$],
216 [m4_eval([$1 - $2])],
220 # _b4_join(ARG1, ...)
221 # -------------------
222 # Join with comma, skipping empty arguments.
223 # b4_join calls itself recursively until it sees the first non-empty
224 # argument, then calls _b4_join which prepends each non-empty argument
230 [$1[]_$0(m4_shift($@))],
231 [$0(m4_shift($@))])])])
233 # _b4_join(ARGS1, ...)
234 # --------------------
235 m4_define([_b4_join],
238 [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])])
243 # b4_integral_parser_tables_map(MACRO)
244 # -------------------------------------
245 # Map MACRO on all the integral tables. MACRO is expected to have
246 # the signature MACRO(TABLE-NAME, CONTENT, COMMENT).
247 m4_define([b4_integral_parser_tables_map],
248 [$1([pact], [b4_pact],
249 [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
252 $1([defact], [b4_defact],
253 [[YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
254 Performed when YYTABLE does not specify something else to do. Zero
255 means the default is an error.]])
257 $1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]])
259 $1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]])
261 $1([table], [b4_table],
262 [[YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If
263 positive, shift that token. If negative, reduce the rule whose
264 number is the opposite. If YYTABLE_NINF, syntax error.]])
266 $1([check], [b4_check])
268 $1([stos], [b4_stos],
269 [[YYSTOS[STATE-NUM] -- The (internal number of the) accessing
270 symbol of state STATE-NUM.]])
273 [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]])
276 [[YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.]])
280 # b4_parser_tables_declare
281 # b4_parser_tables_define
282 # ------------------------
283 # Define/declare the (deterministic) parser tables.
284 m4_define([b4_parser_tables_declare],
285 [b4_integral_parser_tables_map([b4_integral_parser_table_declare])])
287 m4_define([b4_parser_tables_define],
288 [b4_integral_parser_tables_map([b4_integral_parser_table_define])])
292 ## ------------------ ##
293 ## Decoding options. ##
294 ## ------------------ ##
296 # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
297 # -----------------------------------
298 # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
299 m4_define([b4_flag_if],
303 [m4_fatal([invalid $1 value: ]b4_$1_flag)])])
306 # b4_define_flag_if(FLAG)
307 # -----------------------
308 # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
309 # value of the Boolean FLAG.
310 m4_define([b4_define_flag_if],
311 [_b4_define_flag_if($[1], $[2], [$1])])
313 # _b4_define_flag_if($1, $2, FLAG)
314 # --------------------------------
315 # Work around the impossibility to define macros inside macros,
316 # because issuing `[$1]' is not possible in M4. GNU M4 should provide
318 m4_define([_b4_define_flag_if],
319 [m4_if([$1$2], $[1]$[2], [],
320 [m4_fatal([$0: Invalid arguments: $@])])dnl
321 m4_define([b4_$3_if],
322 [b4_flag_if([$3], [$1], [$2])])])
325 # b4_FLAG_if(IF-TRUE, IF-FALSE)
326 # -----------------------------
327 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
328 b4_define_flag_if([defines]) # Whether headers are requested.
329 b4_define_flag_if([glr]) # Whether a GLR parser is requested.
330 b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
331 b4_define_flag_if([token_table]) # Whether yytoken_table is demanded.
332 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
339 # In order to unify the handling of the various aspects of symbols
340 # (tag, type_name, whether terminal, etc.), bison.exe defines one
341 # macro per (token, field), where field can has_id, id, etc.: see
342 # src/output.c:prepare_symbols_definitions().
344 # The various FIELDS are:
347 # Whether the symbol has an id.
349 # If has_id, the id. Guaranteed to be usable as a C identifier.
351 # A representat of the symbol. Can be 'foo', 'foo.id', '"foo"' etc.
352 # - user_number: integer
353 # The assigned (external) number as used by yylex.
355 # Whether this is a terminal symbol.
357 # The internalized number (used after yytranslate).
359 # Whether has a semantic value.
361 # If it has a semantic value, its type tag, or, if variant are used,
363 # - has_printer: 0, 1
365 # - printer_file: string
366 # - printer_line: integer
367 # If the symbol has a printer, everything about it.
368 # - has_destructor, destructor, destructor_file, destructor_line
371 # The following macros provide access to these values.
373 # b4_symbol_(NUM, FIELD)
374 # ----------------------
375 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
377 m4_define([b4_symbol_],
378 [m4_indir([b4_symbol($1, $2)])])
381 # b4_symbol(NUM, FIELD)
382 # ---------------------
383 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
384 # undefined. If FIELD = id, prepend the token prefix.
385 m4_define([b4_symbol],
387 [id], [m4_do([b4_percent_define_get([api.token.prefix])],
388 [b4_symbol_([$1], [id])])],
392 # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
393 # -------------------------------------------
394 # If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE.
395 # Otherwise an error.
396 m4_define([b4_symbol_if],
397 [m4_case(b4_symbol([$1], [$2]),
400 [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])])
403 # b4_symbol_tag_comment(SYMBOL-NUM)
404 # ---------------------------------
405 # Issue a comment giving the tag of symbol NUM.
406 m4_define([b4_symbol_tag_comment],
407 [b4_comment([b4_symbol([$1], [tag])])
411 # b4_symbol_action_location(SYMBOL-NUM, KIND)
412 # -------------------------------------------
413 # Report the location of the KIND action as FILE:LINE.
414 m4_define([b4_symbol_action_location],
415 [b4_symbol([$1], [$2_file]):b4_syncline([b4_symbol([$1], [$2_line])])])
418 # b4_symbol_action(SYMBOL-NUM, KIND)
419 # ----------------------------------
420 # Run the action KIND (destructor or printer) for SYMBOL-NUM.
421 # Same as in C, but using references instead of pointers.
422 m4_define([b4_symbol_action],
423 [b4_symbol_if([$1], [has_$2],
424 [b4_dollar_pushdef([(*yyvaluep)],
425 b4_symbol_if([$1], [has_type],
426 [m4_dquote(b4_symbol([$1], [type]))]),
428 b4_symbol_case_([$1])[]dnl
429 b4_syncline([b4_symbol([$1], [$2_line])], ["b4_symbol([$1], [$2_file])"])
430 b4_symbol([$1], [$2])
431 b4_syncline([@oline@], [@ofile@])
434 b4_dollar_popdef[]dnl
438 # b4_symbol_destructor(SYMBOL-NUM)
439 # b4_symbol_printer(SYMBOL-NUM)
440 # --------------------------------
441 m4_define([b4_symbol_destructor], [b4_symbol_action([$1], [destructor])])
442 m4_define([b4_symbol_printer], [b4_symbol_action([$1], [printer])])
445 # b4_symbol_case_(SYMBOL-NUM)
446 # ---------------------------
447 # Issue a "case NUM" for SYMBOL-NUM.
448 m4_define([b4_symbol_case_],
449 [case b4_symbol([$1], [number]): b4_symbol_tag_comment([$1])])
453 # b4_symbol_foreach(MACRO)
454 # ------------------------
455 # Invoke MACRO(SYMBOL-NUM) for each SYMBOL-NUM.
456 m4_define([b4_symbol_foreach],
457 [m4_map([$1], m4_defn([b4_symbol_numbers]))])
459 # b4_symbol_map(MACRO)
460 # --------------------
461 # Return a list (possibly empty elements) of MACRO invoked for each
463 m4_define([b4_symbol_map],
464 [m4_map_args_sep([$1(], [)], [,], b4_symbol_numbers)])
467 # b4_token_visible_if(NUM, IF-TRUE, IF-FALSE)
468 # -------------------------------------------
469 # Whether NUM denotes a token that has an exported definition (i.e.,
470 # shows in enum yytokentype).
471 m4_define([b4_token_visible_if],
472 [b4_symbol_if([$1], [is_token],
473 [b4_symbol_if([$1], [has_id], [$2], [$3])],
476 # b4_token_has_definition(NUM)
477 # ----------------------------
478 # 1 if NUM is visible, nothing otherwise.
479 m4_define([b4_token_has_definition],
480 [b4_token_visible_if([$1], [1])])
482 # b4_any_token_visible_if([IF-TRUE], [IF-FALSE])
483 # ----------------------------------------------
484 # Whether there is a token that needs to be defined.
485 m4_define([b4_any_token_visible_if],
486 [m4_ifval(b4_symbol_foreach([b4_token_has_definition]),
490 # b4_token_format(FORMAT, NUM)
491 # ----------------------------
492 m4_define([b4_token_format],
493 [b4_token_visible_if([$2],
494 [m4_quote(m4_format([$1],
495 [b4_symbol([$2], [id])],
496 [b4_symbol([$2], [user_number])]))])])
503 # b4_type_action_(NUMS)
504 # ---------------------
505 # Run actions for the symbol NUMS that all have the same type-name.
506 # Skip NUMS that have no type-name.
508 # To specify the action to run, define b4_dollar_dollar(NUMBER,
510 m4_define([b4_type_action_],
511 [b4_symbol_if([$1], [has_type],
512 [m4_map([b4_symbol_case_], [$@])[]dnl
513 b4_dollar_dollar([b4_symbol([$1], [number])],
514 [b4_symbol([$1], [tag])],
515 [b4_symbol([$1], [type])]);
520 # b4_type_foreach(MACRO)
521 # ----------------------
522 # Invoke MACRO(SYMBOL-NUMS) for each set of SYMBOL-NUMS for each type set.
523 m4_define([b4_type_foreach],
524 [m4_map([$1], m4_defn([b4_type_names]))])
534 # Similar to POSIX basename; the differences don't matter here.
535 # Beware that NAME is not evaluated.
536 m4_define([b4_basename],
537 [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
540 # b4_syncline(LINE, FILE)
541 # -----------------------
542 m4_define([b4_syncline],
543 [b4_flag_if([synclines],
544 [b4_sync_start([$1], [$2]) b4_sync_end([__line__],
545 [b4_basename(m4_quote(__file__))])[]dnl
548 # b4_sync_start(LINE, FILE)
549 # -----------------------
550 # Syncline for the new place. Typically a directive for the compiler.
551 m4_define([b4_sync_start], [b4_comment([$2:$1])])
553 # b4_sync_end(LINE, FILE)
554 # -----------------------
555 # Syncline for the current place, which ends. Typically a comment
556 # left for the reader.
557 m4_define([b4_sync_end], [b4_comment([$2:$1])])
560 # b4_user_code(USER-CODE)
561 # -----------------------
562 # Emit code from the user, ending it with synclines.
563 m4_define([b4_user_code],
565 b4_syncline([@oline@], [@ofile@])])
568 # b4_define_user_code(MACRO)
569 # --------------------------
570 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
571 m4_define([b4_define_user_code],
572 [m4_define([b4_user_$1],
573 [b4_user_code([b4_$1])])])
577 # b4_user_initial_action
578 # b4_user_post_prologue
579 # b4_user_pre_prologue
581 # ----------------------
582 # Macros that issue user code, ending with synclines.
583 b4_define_user_code([actions])
584 b4_define_user_code([initial_action])
585 b4_define_user_code([post_prologue])
586 b4_define_user_code([pre_prologue])
587 b4_define_user_code([stype])
590 # b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
591 # -----------------------------------------------------
592 # Complain if any name of type WHAT is used by the user (as recorded in
593 # USER-LIST) but is not used by Bison (as recorded by macros in the
594 # namespace BISON-NAMESPACE).
596 # USER-LIST must expand to a list specifying all user occurrences of all names
597 # of type WHAT. Each item in the list must be a triplet specifying one
598 # occurrence: name, start boundary, and end boundary. Empty string names are
599 # fine. An empty list is fine.
601 # For example, to define b4_foo_user_names to be used for USER-LIST with three
602 # name occurrences and with correct quoting:
604 # m4_define([b4_foo_user_names],
605 # [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]],
606 # [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]],
607 # [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]])
609 # The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT
610 # is used by Bison (in the front-end or in the skeleton). Empty string names
611 # are fine, but it would be ugly for Bison to actually use one.
613 # For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that
614 # the names bar and baz are used by Bison:
616 # m4_define([b4_foo_bison_names(bar)])
617 # m4_define([b4_foo_bison_names(baz)])
619 # To invoke b4_check_user_names with TYPE foo, with USER-LIST
620 # b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
623 # b4_check_user_names([[foo]], [b4_foo_user_names],
624 # [[b4_foo_bison_names]])
625 m4_define([b4_check_user_names],
626 [m4_foreach([b4_occurrence], $2,
627 [m4_pushdef([b4_occurrence], b4_occurrence)dnl
628 m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
629 m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
630 m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl
631 m4_ifndef($3[(]m4_quote(b4_user_name)[)],
632 [b4_complain_at([b4_start], [b4_end],
633 [[%s '%s' is not used]],
634 [$1], [b4_user_name])])[]dnl
635 m4_popdef([b4_occurrence])dnl
636 m4_popdef([b4_user_name])dnl
637 m4_popdef([b4_start])dnl
638 m4_popdef([b4_end])dnl
643 ## --------------------- ##
644 ## b4_percent_define_*. ##
645 ## --------------------- ##
648 # b4_percent_define_use(VARIABLE)
649 # -------------------------------
650 # Declare that VARIABLE was used.
651 m4_define([b4_percent_define_use],
652 [m4_define([b4_percent_define_bison_variables(]$1[)])dnl
655 # b4_percent_define_get(VARIABLE, [DEFAULT])
656 # ------------------------------------------
657 # Mimic muscle_percent_define_get in ../src/muscle-tab.h. That is, if
658 # the %define variable VARIABLE is defined, emit its value. Contrary
659 # to its C counterpart, return DEFAULT otherwise. Also, record
660 # Bison's usage of VARIABLE by defining
661 # b4_percent_define_bison_variables(VARIABLE).
665 # b4_percent_define_get([[foo]])
666 m4_define([b4_percent_define_get],
667 [b4_percent_define_use([$1])dnl
668 m4_ifdef([b4_percent_define(]$1[)],
669 [m4_indir([b4_percent_define(]$1[)])],
672 # b4_percent_define_get_loc(VARIABLE)
673 # -----------------------------------
674 # Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly. That is,
675 # if the %define variable VARIABLE is undefined, complain fatally since that's
676 # a Bison or skeleton error. Otherwise, return its definition location in a
677 # form approriate for the first two arguments of b4_warn_at, b4_complain_at, or
678 # b4_fatal_at. Don't record this as a Bison usage of VARIABLE as there's no
679 # reason to suspect that the user-supplied value has yet influenced the output.
683 # b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
684 m4_define([b4_percent_define_get_loc],
685 [m4_ifdef([b4_percent_define_loc(]$1[)],
686 [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl
688 m4_popdef([b4_loc])],
689 [b4_fatal([[b4_percent_define_get_loc: undefined %%define variable '%s']], [$1])])])
691 # b4_percent_define_get_syncline(VARIABLE)
692 # ----------------------------------------
693 # Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly.
694 # That is, if the %define variable VARIABLE is undefined, complain fatally
695 # since that's a Bison or skeleton error. Otherwise, return its definition
696 # location as a b4_syncline invocation. Don't record this as a Bison usage of
697 # VARIABLE as there's no reason to suspect that the user-supplied value has yet
698 # influenced the output.
702 # b4_percent_define_get_syncline([[foo]])
703 m4_define([b4_percent_define_get_syncline],
704 [m4_ifdef([b4_percent_define_syncline(]$1[)],
705 [m4_indir([b4_percent_define_syncline(]$1[)])],
706 [b4_fatal([[b4_percent_define_get_syncline: undefined %%define variable '%s']], [$1])])])
708 # b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
709 # ------------------------------------------------------
710 # Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly. That is,
711 # if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
712 # IF-FALSE. Also, record Bison's usage of VARIABLE by defining
713 # b4_percent_define_bison_variables(VARIABLE).
717 # b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
718 m4_define([b4_percent_define_ifdef],
719 [m4_ifdef([b4_percent_define(]$1[)],
720 [b4_percent_define_use([$1])$2],
729 # b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
730 # --------------------------------------------------------
731 # Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly. That is,
732 # if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE.
733 # If it is defined to "false", expand IF-FALSE. Complain if it is undefined
734 # (a Bison or skeleton error since the default value should have been set
735 # already) or defined to any other value (possibly a user error). Also, record
736 # Bison's usage of VARIABLE by defining
737 # b4_percent_define_bison_variables(VARIABLE).
741 # b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
742 m4_define([b4_percent_define_flag_if],
743 [b4_percent_define_ifdef([$1],
744 [m4_case(b4_percent_define_get([$1]),
745 [], [$2], [true], [$2], [false], [$3],
746 [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
747 [[invalid value for %%define Boolean variable '%s']],
749 [[b4_percent_define_flag_if($1)]])])],
750 [b4_fatal([[b4_percent_define_flag_if: undefined %%define variable '%s']], [$1])])])
753 # b4_percent_define_default(VARIABLE, DEFAULT)
754 # --------------------------------------------
755 # Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly. That is,
756 # if the %define variable VARIABLE is undefined, set its value to DEFAULT.
757 # Don't record this as a Bison usage of VARIABLE as there's no reason to
758 # suspect that the value has yet influenced the output.
762 # b4_percent_define_default([[foo]], [[default value]])
763 m4_define([b4_percent_define_default],
764 [m4_ifndef([b4_percent_define(]$1[)],
765 [m4_define([b4_percent_define(]$1[)], [$2])dnl
766 m4_define([b4_percent_define_loc(]$1[)],
767 [[[[<skeleton default value>:-1.-1]],
768 [[<skeleton default value>:-1.-1]]]])dnl
769 m4_define([b4_percent_define_syncline(]$1[)], [[]])])])
772 # b4_percent_define_if_define(NAME, [VARIABLE = NAME])
773 # ----------------------------------------------------
774 # Define b4_NAME_if that executes its $1 or $2 depending whether
775 # VARIABLE was %defined. The characters `.' and `-' in VARIABLE are mapped
777 m4_define([b4_percent_define_if_define_],
778 [m4_define(m4_bpatsubst([b4_$1_if], [[-.]], [_]),
779 [b4_percent_define_flag_if(m4_default([$2], [$1]),
781 m4_define([b4_percent_define_if_define],
782 [b4_percent_define_default([m4_default([$2], [$1])], [[false]])
783 b4_percent_define_if_define_([$1], [$2], $[1], $[2])])
786 # b4_percent_define_check_values(VALUES)
787 # --------------------------------------
788 # Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly
789 # except that the VALUES structure is more appropriate for M4. That is, VALUES
790 # is a list of sublists of strings. For each sublist, the first string is the
791 # name of a %define variable, and all remaining strings in that sublist are the
792 # valid values for that variable. Complain if such a variable is undefined (a
793 # Bison error since the default value should have been set already) or defined
794 # to any other value (possibly a user error). Don't record this as a Bison
795 # usage of the variable as there's no reason to suspect that the value has yet
796 # influenced the output.
800 # b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
801 # [[[[bar]], [[bar-value1]]]])
802 m4_define([b4_percent_define_check_values],
803 [m4_foreach([b4_sublist], m4_quote($@),
804 [_b4_percent_define_check_values(b4_sublist)])])
806 m4_define([_b4_percent_define_check_values],
807 [m4_ifdef([b4_percent_define(]$1[)],
808 [m4_pushdef([b4_good_value], [0])dnl
810 [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
811 [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
812 [m4_define([b4_good_value], [1])])])])dnl
813 m4_if(b4_good_value, [0],
814 [b4_complain_at(b4_percent_define_get_loc([$1]),
815 [[invalid value for %%define variable '%s': '%s']],
817 m4_dquote(m4_indir([b4_percent_define(]$1[)])))
818 m4_foreach([b4_value], m4_dquote(m4_shift($@)),
819 [b4_error([[note]], b4_percent_define_get_loc([$1]), []
820 [[accepted value: '%s']],
821 m4_dquote(b4_value))])])dnl
822 m4_popdef([b4_good_value])],
823 [b4_fatal([[b4_percent_define_check_values: undefined %%define variable '%s']], [$1])])])
825 # b4_percent_code_get([QUALIFIER])
826 # --------------------------------
827 # If any %code blocks for QUALIFIER are defined, emit them beginning with a
828 # comment and ending with synclines and a newline. If QUALIFIER is not
829 # specified or empty, do this for the unqualified %code blocks. Also, record
830 # Bison's usage of QUALIFIER (if specified) by defining
831 # b4_percent_code_bison_qualifiers(QUALIFIER).
833 # For example, to emit any unqualified %code blocks followed by any %code
834 # blocks for the qualifier foo:
836 # b4_percent_code_get
837 # b4_percent_code_get([[foo]])
838 m4_define([b4_percent_code_get],
839 [m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
840 m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
841 m4_ifdef(b4_macro_name,
842 [b4_comment([m4_if([$#], [0], [[Unqualified %code]],
843 [["%code ]$1["]])[ blocks.]])
844 b4_user_code([m4_indir(b4_macro_name)])
846 m4_popdef([b4_macro_name])])
848 # b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
849 # -----------------------------------------------------
850 # If any %code blocks for QUALIFIER (or unqualified %code blocks if
851 # QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
852 # Also, record Bison's usage of QUALIFIER (if specified) by defining
853 # b4_percent_code_bison_qualifiers(QUALIFIER).
854 m4_define([b4_percent_code_ifdef],
855 [m4_ifdef([b4_percent_code(]$1[)],
856 [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
860 ## ------------------ ##
861 ## Common variables. ##
862 ## ------------------ ##
864 # Default values for %define.
865 # ---------------------------
866 # If the api.token.prefix, it is empty.
867 m4_percent_define_default([[api.token.prefix]], [[]])
869 # b4_parse_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
870 # b4_parse_trace_if([IF-DEBUG-TRACES-ARE-ENABLED], [IF-NOT])
871 # b4_token_ctor_if([IF-YYLEX-RETURNS-A-TOKEN], [IF-NOT])
872 # ----------------------------------------------
873 b4_percent_define_if_define([token_ctor], [api.token.constructor])
874 b4_percent_define_if_define([locations]) # Whether locations are tracked.
875 b4_percent_define_if_define([parse.assert])
876 b4_percent_define_if_define([parse.trace])
879 # b4_bison_locations_if([IF-TRUE])
880 # --------------------------------
881 # Expand IF-TRUE if using locations, and using the default location
883 m4_define([b4_bison_locations_if],
884 [b4_locations_if([b4_percent_define_ifdef([[api.location.type]], [], [$1])])])
887 # b4_error_verbose_if([IF-ERRORS-ARE-VERBOSE], [IF-NOT])
888 # ------------------------------------------------------
889 # Map %define parse.error "(simple|verbose)" to b4_error_verbose_if and
890 # b4_error_verbose_flag.
891 b4_percent_define_default([[parse.error]], [[simple]])
892 b4_percent_define_check_values([[[[parse.error]],
893 [[simple]], [[verbose]]]])
894 m4_define([b4_error_verbose_flag],
895 [m4_case(b4_percent_define_get([[parse.error]]),
898 b4_define_flag_if([error_verbose])
900 # yytoken_table is needed to support verbose errors.
901 b4_error_verbose_if([m4_define([b4_token_table_flag], [1])])
904 # b4_variant_if([IF-VARIANT-ARE-USED], [IF-NOT])
905 # ----------------------------------------------
906 b4_percent_define_if_define([variant])
907 m4_case(b4_percent_define_get([[api.value.type]]),
908 [variant], [m4_define([b4_variant_flag], [[1]])],
909 [m4_define([b4_variant_flag], [[0]])])
910 b4_define_flag_if([variant])
913 ## ----------------------------------------------------------- ##
914 ## After processing the skeletons, check that all the user's ##
915 ## %define variables and %code qualifiers were used by Bison. ##
916 ## ----------------------------------------------------------- ##
918 m4_define([b4_check_user_names_wrap],
919 [m4_ifdef([b4_percent_]$1[_user_]$2[s],
920 [b4_check_user_names([[%]$1 $2],
921 [b4_percent_]$1[_user_]$2[s],
922 [[b4_percent_]$1[_bison_]$2[s]])])])
925 b4_check_user_names_wrap([[define]], [[variable]])
926 b4_check_user_names_wrap([[code]], [[qualifier]])
930 ## ---------------- ##
931 ## Default values. ##
932 ## ---------------- ##
934 # m4_define_default([b4_lex_param], []) dnl breaks other skeletons
935 m4_define_default([b4_pre_prologue], [])
936 m4_define_default([b4_post_prologue], [])
937 m4_define_default([b4_epilogue], [])
938 m4_define_default([b4_parse_param], [])
940 # The initial column and line.
941 m4_define_default([b4_location_initial_column], [1])
942 m4_define_default([b4_location_initial_line], [1])
945 b4_percent_define_ifdef([api.prefix],
946 [m4_ifdef([b4_prefix],
947 [b4_complain_at(b4_percent_define_get_loc([api.prefix]),
948 [['%s' and '%s' cannot be used together]],
950 [%define api.prefix])])])