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 m4_define([b4_copyright],
28 [b4_comment([A Bison parser, made by GNU Bison b4_version.])
32 m4_text_wrap([Copyright (C) $2 Free Software Foundation, Inc.], [ ])
34 This program is free software: you can redistribute it and/or modify
35 it under the terms of the GNU General Public License as published by
36 the Free Software Foundation, either version 3 of the License, or
37 (at your option) any later version.
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU General Public License for more details.
44 You should have received a copy of the GNU General Public License
45 along with this program. If not, see <http://www.gnu.org/licenses/>.])
47 b4_comment([As a special exception, you may create a larger work that contains
48 part or all of the Bison parser skeleton and distribute that work
49 under terms of your choice, so long as that work isn't itself a
50 parser generator using the skeleton or a modified version thereof
51 as a parser skeleton. Alternatively, if you modify or redistribute
52 the parser skeleton itself, you may (at your option) remove this
53 special exception, which will cause the skeleton and the resulting
54 Bison output files to be licensed under the GNU General Public
55 License without this special exception.
57 This special exception was added by the Free Software Foundation in
58 version 2.2 of Bison.])])
65 # b4_output_begin(FILE)
66 # ---------------------
67 # Enable output, i.e., send to diversion 0, expand after "#", and
68 # generate the tag to output into FILE. Must be followed by EOL.
69 m4_define([b4_output_begin],
72 @output(m4_unquote([$1])@)@dnl
78 # Output nothing, restore # as comment character (no expansions after #).
79 m4_define([b4_output_end],
85 ## ---------------- ##
87 ## ---------------- ##
89 # The following error handling macros print error directives that should not
90 # become arguments of other macro invocations since they would likely then be
91 # mangled. Thus, they print to stdout directly.
95 # Write TEXT to stdout. Precede the final newline with an @ so that it's
96 # escaped. For example:
98 # b4_cat([[@complain(invalid input@)]])
100 [m4_syscmd([cat <<'_m4eof'
101 ]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@
104 m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])])
106 # b4_error(KIND, FORMAT, [ARG1], [ARG2], ...)
107 # -------------------------------------------
108 # Write @KIND(FORMAT@,ARG1@,ARG2@,...@) to stdout.
112 # b4_error([[warn]], [[invalid value for '%s': %s]], [[foo]], [[3]])
113 m4_define([b4_error],
114 [b4_cat([[@]$1[(]$2[]]dnl
115 [m4_if([$#], [2], [],
116 [m4_foreach([b4_arg],
117 m4_dquote(m4_shift(m4_shift($@))),
118 [[@,]b4_arg])])[@)]])])
120 # b4_error_at(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
121 # ----------------------------------------------------------
122 # Write @KIND_at(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
126 # b4_error_at([[complain]], [[input.y:2.3]], [[input.y:5.4]],
127 # [[invalid %s]], [[foo]])
128 m4_define([b4_error_at],
129 [b4_cat([[@]$1[_at(]$2[@,]$3[@,]$4[]]dnl
130 [m4_if([$#], [4], [],
131 [m4_foreach([b4_arg],
132 m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))),
133 [[@,]b4_arg])])[@)]])])
135 # b4_warn(FORMAT, [ARG1], [ARG2], ...)
136 # ------------------------------------
137 # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
141 # b4_warn([[invalid value for '%s': %s]], [[foo]], [[3]])
143 # As a simple test suite, this:
146 # m4_define([asdf], [ASDF])
147 # m4_define([fsa], [FSA])
148 # m4_define([fdsa], [FDSA])
149 # b4_warn([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
150 # b4_warn([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
155 # Should produce this without newlines:
157 # @warn([asdf), asdf]@,[fsa), fsa]@,[fdsa), fdsa]@)
158 # @warn(asdf), asdf@,fsa), fsa@,fdsa), fdsa@)
163 [b4_error([[warn]], $@)])
165 # b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
166 # ---------------------------------------------------
167 # Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
171 # b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
172 m4_define([b4_warn_at],
173 [b4_error_at([[warn]], $@)])
175 # b4_complain(FORMAT, [ARG1], [ARG2], ...)
176 # ----------------------------------------
177 # Write @complain(FORMAT@,ARG1@,ARG2@,...@) to stdout.
179 # See b4_warn example.
180 m4_define([b4_complain],
181 [b4_error([[complain]], $@)])
183 # b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
184 # -------------------------------------------------------
185 # Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
187 # See b4_warn_at example.
188 m4_define([b4_complain_at],
189 [b4_error_at([[complain]], $@)])
191 # b4_fatal(FORMAT, [ARG1], [ARG2], ...)
192 # -------------------------------------
193 # Write @fatal(FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
195 # See b4_warn example.
196 m4_define([b4_fatal],
197 [b4_error([[fatal]], $@)dnl
200 # b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
201 # ----------------------------------------------------
202 # Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
204 # See b4_warn_at example.
205 m4_define([b4_fatal_at],
206 [b4_error_at([[fatal]], $@)dnl
214 # b4_ints_in(INT1, INT2, LOW, HIGH)
215 # ---------------------------------
216 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
217 m4_define([b4_ints_in],
218 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
222 ## ------------------ ##
223 ## Decoding options. ##
224 ## ------------------ ##
226 # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
227 # -----------------------------------
228 # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
229 m4_define([b4_flag_if],
233 [m4_fatal([invalid $1 value: ]$1)])])
236 # b4_define_flag_if(FLAG)
237 # -----------------------
238 # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
239 # value of the Boolean FLAG.
240 m4_define([b4_define_flag_if],
241 [_b4_define_flag_if($[1], $[2], [$1])])
243 # _b4_define_flag_if($1, $2, FLAG)
244 # --------------------------------
245 # Work around the impossibility to define macros inside macros,
246 # because issuing `[$1]' is not possible in M4. GNU M4 should provide
248 m4_define([_b4_define_flag_if],
249 [m4_if([$1$2], $[1]$[2], [],
250 [m4_fatal([$0: Invalid arguments: $@])])dnl
251 m4_define([b4_$3_if],
252 [b4_flag_if([$3], [$1], [$2])])])
255 # b4_FLAG_if(IF-TRUE, IF-FALSE)
256 # -----------------------------
257 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
258 b4_define_flag_if([defines]) # Whether headers are requested.
259 b4_define_flag_if([error_verbose]) # Whether error are verbose.
260 b4_define_flag_if([glr]) # Whether a GLR parser is requested.
261 b4_define_flag_if([locations]) # Whether locations are tracked.
262 b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
263 b4_define_flag_if([token_table]) # Whether yytoken_table is demanded.
264 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
266 # yytoken_table is needed to support verbose errors.
267 b4_error_verbose_if([m4_define([b4_token_table_flag], [1])])
277 # Similar to POSIX basename; the differences don't matter here.
278 # Beware that NAME is not evaluated.
279 m4_define([b4_basename],
280 [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
283 # b4_syncline(LINE, FILE)
284 # -----------------------
285 m4_define([b4_syncline],
286 [b4_flag_if([synclines],
287 [b4_sync_end([__line__], [b4_basename(m4_quote(__file__))])
288 b4_sync_start([$1], [$2])])])
290 m4_define([b4_sync_end], [b4_comment([Line $1 of $2])])
291 m4_define([b4_sync_start], [b4_comment([Line $1 of $2])])
293 # b4_user_code(USER-CODE)
294 # -----------------------
295 # Emit code from the user, ending it with synclines.
296 m4_define([b4_user_code],
298 b4_syncline([@oline@], [@ofile@])])
301 # b4_define_user_code(MACRO)
302 # --------------------------
303 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
304 m4_define([b4_define_user_code],
305 [m4_define([b4_user_$1],
306 [b4_user_code([b4_$1])])])
310 # b4_user_initial_action
311 # b4_user_post_prologue
312 # b4_user_pre_prologue
314 # ----------------------
315 # Macros that issue user code, ending with synclines.
316 b4_define_user_code([actions])
317 b4_define_user_code([initial_action])
318 b4_define_user_code([post_prologue])
319 b4_define_user_code([pre_prologue])
320 b4_define_user_code([stype])
323 # b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
324 # -----------------------------------------------------
325 # Complain if any name of type WHAT is used by the user (as recorded in
326 # USER-LIST) but is not used by Bison (as recorded by macros in the
327 # namespace BISON-NAMESPACE).
329 # USER-LIST must expand to a list specifying all user occurrences of all names
330 # of type WHAT. Each item in the list must be a triplet specifying one
331 # occurrence: name, start boundary, and end boundary. Empty string names are
332 # fine. An empty list is fine.
334 # For example, to define b4_foo_user_names to be used for USER-LIST with three
335 # name occurrences and with correct quoting:
337 # m4_define([b4_foo_user_names],
338 # [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]],
339 # [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]],
340 # [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]])
342 # The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT
343 # is used by Bison (in the front-end or in the skeleton). Empty string names
344 # are fine, but it would be ugly for Bison to actually use one.
346 # For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that
347 # the names bar and baz are used by Bison:
349 # m4_define([b4_foo_bison_names(bar)])
350 # m4_define([b4_foo_bison_names(baz)])
352 # To invoke b4_check_user_names with TYPE foo, with USER-LIST
353 # b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
356 # b4_check_user_names([[foo]], [b4_foo_user_names],
357 # [[b4_foo_bison_names]])
358 m4_define([b4_check_user_names],
359 [m4_foreach([b4_occurrence], $2,
360 [m4_pushdef([b4_occurrence], b4_occurrence)dnl
361 m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
362 m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
363 m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl
364 m4_ifndef($3[(]m4_quote(b4_user_name)[)],
365 [b4_complain_at([b4_start], [b4_end],
366 [[%s '%s' is not used]],
367 [$1], [b4_user_name])])[]dnl
368 m4_popdef([b4_occurrence])dnl
369 m4_popdef([b4_user_name])dnl
370 m4_popdef([b4_start])dnl
371 m4_popdef([b4_end])dnl
377 ## --------------------- ##
378 ## b4_percent_define_*. ##
379 ## --------------------- ##
382 # b4_percent_define_use(VARIABLE)
383 # -------------------------------
384 # Declare that VARIABLE was used.
385 m4_define([b4_percent_define_use],
386 [m4_define([b4_percent_define_bison_variables(]$1[)])dnl
389 # b4_percent_define_get(VARIABLE, [DEFAULT])
390 # ------------------------------------------
391 # Mimic muscle_percent_define_get in ../src/muscle-tab.h. That is, if
392 # the %define variable VARIABLE is defined, emit its value. Contrary
393 # to its C counterpart, return DEFAULT otherwise. Also, record
394 # Bison's usage of VARIABLE by defining
395 # b4_percent_define_bison_variables(VARIABLE).
399 # b4_percent_define_get([[foo]])
400 m4_define([b4_percent_define_get],
401 [b4_percent_define_use([$1])dnl
402 m4_ifdef([b4_percent_define(]$1[)],
403 [m4_indir([b4_percent_define(]$1[)])],
407 # b4_percent_define_get_loc(VARIABLE)
408 # -----------------------------------
409 # Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly. That is,
410 # if the %define variable VARIABLE is undefined, complain fatally since that's
411 # a Bison or skeleton error. Otherwise, return its definition location in a
412 # form approriate for the first two arguments of b4_warn_at, b4_complain_at, or
413 # b4_fatal_at. Don't record this as a Bison usage of VARIABLE as there's no
414 # reason to suspect that the user-supplied value has yet influenced the output.
418 # b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
419 m4_define([b4_percent_define_get_loc],
420 [m4_ifdef([b4_percent_define_loc(]$1[)],
421 [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl
423 m4_popdef([b4_loc])],
424 [b4_fatal([[b4_percent_define_get_loc: undefined %%define variable '%s']], [$1])])])
426 # b4_percent_define_get_syncline(VARIABLE)
427 # ----------------------------------------
428 # Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly.
429 # That is, if the %define variable VARIABLE is undefined, complain fatally
430 # since that's a Bison or skeleton error. Otherwise, return its definition
431 # location as a b4_syncline invocation. Don't record this as a Bison usage of
432 # VARIABLE as there's no reason to suspect that the user-supplied value has yet
433 # influenced the output.
437 # b4_percent_define_get_syncline([[foo]])
438 m4_define([b4_percent_define_get_syncline],
439 [m4_ifdef([b4_percent_define_syncline(]$1[)],
440 [m4_indir([b4_percent_define_syncline(]$1[)])],
441 [b4_fatal([[b4_percent_define_get_syncline: undefined %%define variable '%s']], [$1])])])
443 # b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
444 # ------------------------------------------------------
445 # Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly. That is,
446 # if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
447 # IF-FALSE. Also, record Bison's usage of VARIABLE by defining
448 # b4_percent_define_bison_variables(VARIABLE).
452 # b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
453 m4_define([b4_percent_define_ifdef],
454 [m4_ifdef([b4_percent_define(]$1[)],
455 [m4_define([b4_percent_define_bison_variables(]$1[)])$2],
458 # b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
459 # --------------------------------------------------------
460 # Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly. That is,
461 # if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE.
462 # If it is defined to "false", expand IF-FALSE. Complain if it is undefined
463 # (a Bison or skeleton error since the default value should have been set
464 # already) or defined to any other value (possibly a user error). Also, record
465 # Bison's usage of VARIABLE by defining
466 # b4_percent_define_bison_variables(VARIABLE).
470 # b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
471 m4_define([b4_percent_define_flag_if],
472 [b4_percent_define_ifdef([$1],
473 [m4_case(b4_percent_define_get([$1]),
474 [], [$2], [true], [$2], [false], [$3],
475 [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
476 [[invalid value for %%define Boolean variable '%s']],
478 [[b4_percent_define_flag_if($1)]])])],
479 [b4_fatal([[b4_percent_define_flag_if: undefined %%define variable '%s']], [$1])])])
481 # b4_percent_define_default(VARIABLE, DEFAULT)
482 # --------------------------------------------
483 # Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly. That is,
484 # if the %define variable VARIABLE is undefined, set its value to DEFAULT.
485 # Don't record this as a Bison usage of VARIABLE as there's no reason to
486 # suspect that the value has yet influenced the output.
490 # b4_percent_define_default([[foo]], [[default value]])
491 m4_define([b4_percent_define_default],
492 [m4_ifndef([b4_percent_define(]$1[)],
493 [m4_define([b4_percent_define(]$1[)], [$2])dnl
494 m4_define([b4_percent_define_loc(]$1[)],
495 [[[[<skeleton default value>:-1.-1]],
496 [[<skeleton default value>:-1.-1]]]])dnl
497 m4_define([b4_percent_define_syncline(]$1[)], [[]])])])
499 # b4_percent_define_check_values(VALUES)
500 # --------------------------------------
501 # Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly
502 # except that the VALUES structure is more appropriate for M4. That is, VALUES
503 # is a list of sublists of strings. For each sublist, the first string is the
504 # name of a %define variable, and all remaining strings in that sublist are the
505 # valid values for that variable. Complain if such a variable is undefined (a
506 # Bison error since the default value should have been set already) or defined
507 # to any other value (possibly a user error). Don't record this as a Bison
508 # usage of the variable as there's no reason to suspect that the value has yet
509 # influenced the output.
513 # b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
514 # [[[[bar]], [[bar-value1]]]])
515 m4_define([b4_percent_define_check_values],
516 [m4_foreach([b4_sublist], m4_quote($@),
517 [_b4_percent_define_check_values(b4_sublist)])])
519 m4_define([_b4_percent_define_check_values],
520 [m4_ifdef([b4_percent_define(]$1[)],
521 [m4_pushdef([b4_good_value], [0])dnl
523 [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
524 [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
525 [m4_define([b4_good_value], [1])])])])dnl
526 m4_if(b4_good_value, [0],
527 [b4_complain_at(b4_percent_define_get_loc([$1]),
528 [[invalid value for %%define variable '%s': '%s']],
530 m4_dquote(m4_indir([b4_percent_define(]$1[)])))
531 m4_foreach([b4_value], m4_dquote(m4_shift($@)),
532 [b4_complain_at(b4_percent_define_get_loc([$1]),
533 [[accepted value: '%s']],
534 m4_dquote(b4_value))])])dnl
535 m4_popdef([b4_good_value])],
536 [b4_fatal([[b4_percent_define_check_values: undefined %%define variable '%s']], [$1])])])
538 # b4_percent_code_get([QUALIFIER])
539 # --------------------------------
540 # If any %code blocks for QUALIFIER are defined, emit them beginning with a
541 # comment and ending with synclines and a newline. If QUALIFIER is not
542 # specified or empty, do this for the unqualified %code blocks. Also, record
543 # Bison's usage of QUALIFIER (if specified) by defining
544 # b4_percent_code_bison_qualifiers(QUALIFIER).
546 # For example, to emit any unqualified %code blocks followed by any %code
547 # blocks for the qualifier foo:
549 # b4_percent_code_get
550 # b4_percent_code_get([[foo]])
551 m4_define([b4_percent_code_get],
552 [m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
553 m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
554 m4_ifdef(b4_macro_name,
555 [b4_comment([m4_if([$#], [0], [[Unqualified %code]],
556 [["%code ]$1["]])[ blocks.]])
557 b4_user_code([m4_indir(b4_macro_name)])
559 m4_popdef([b4_macro_name])])
561 # b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
562 # -----------------------------------------------------
563 # If any %code blocks for QUALIFIER (or unqualified %code blocks if
564 # QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
565 # Also, record Bison's usage of QUALIFIER (if specified) by defining
566 # b4_percent_code_bison_qualifiers(QUALIFIER).
567 m4_define([b4_percent_code_ifdef],
568 [m4_ifdef([b4_percent_code(]$1[)],
569 [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
573 ## ----------------------------------------------------------- ##
574 ## After processing the skeletons, check that all the user's ##
575 ## %define variables and %code qualifiers were used by Bison. ##
576 ## ----------------------------------------------------------- ##
578 m4_define([b4_check_user_names_wrap],
579 [m4_ifdef([b4_percent_]$1[_user_]$2[s],
580 [b4_check_user_names([[%]$1 $2],
581 [b4_percent_]$1[_user_]$2[s],
582 [[b4_percent_]$1[_bison_]$2[s]])])])
585 b4_check_user_names_wrap([[define]], [[variable]])
586 b4_check_user_names_wrap([[code]], [[qualifier]])
590 ## ---------------- ##
591 ## Default values. ##
592 ## ---------------- ##
594 # m4_define_default([b4_lex_param], []) dnl breaks other skeletons
595 m4_define_default([b4_pre_prologue], [])
596 m4_define_default([b4_post_prologue], [])
597 m4_define_default([b4_epilogue], [])
598 m4_define_default([b4_parse_param], [])
600 # The initial column and line.
601 m4_define_default([b4_location_initial_column], [1])
602 m4_define_default([b4_location_initial_line], [1])
605 b4_percent_define_ifdef([api.prefix],
606 [m4_ifdef([b4_prefix],
607 [b4_complain_at(b4_percent_define_get_loc([api.prefix]),
608 [['%s' and '%s' cannot be used together]],
610 [%define api.prefix])])])