3 # Language-independent M4 Macros for Bison.
4 # Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
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, FORMAT, [ARG1], [ARG2], ...)
86 # -------------------------------------------
87 # Write @KIND(FORMAT@,ARG1@,ARG2@,...@) to stdout.
91 # b4_error([[warn]], [[invalid value for `%s': %s]], [[foo]], [[3]])
93 [b4_cat([[@]$1[(]$2[]]dnl
96 m4_dquote(m4_shift(m4_shift($@))),
97 [[@,]b4_arg])])[@)]])])
99 # b4_error_at(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
100 # ----------------------------------------------------------
101 # Write @KIND_at(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
105 # b4_error_at([[complain]], [[input.y:2.3]], [[input.y:5.4]],
106 # [[invalid %s]], [[foo]])
107 m4_define([b4_error_at],
108 [b4_cat([[@]$1[_at(]$2[@,]$3[@,]$4[]]dnl
109 [m4_if([$#], [4], [],
110 [m4_foreach([b4_arg],
111 m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))),
112 [[@,]b4_arg])])[@)]])])
114 # b4_warn(FORMAT, [ARG1], [ARG2], ...)
115 # ------------------------------------
116 # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
120 # b4_warn([[invalid value for `%s': %s]], [[foo]], [[3]])
122 # As a simple test suite, this:
125 # m4_define([asdf], [ASDF])
126 # m4_define([fsa], [FSA])
127 # m4_define([fdsa], [FDSA])
128 # b4_warn([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
129 # b4_warn([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
134 # Should produce this without newlines:
136 # @warn([asdf), asdf]@,[fsa), fsa]@,[fdsa), fdsa]@)
137 # @warn(asdf), asdf@,fsa), fsa@,fdsa), fdsa@)
142 [b4_error([[warn]], $@)])
144 # b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
145 # ---------------------------------------------------
146 # Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
150 # b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
151 m4_define([b4_warn_at],
152 [b4_error_at([[warn]], $@)])
154 # b4_complain(FORMAT, [ARG1], [ARG2], ...)
155 # ----------------------------------------
156 # Write @complain(FORMAT@,ARG1@,ARG2@,...@) to stdout.
158 # See b4_warn example.
159 m4_define([b4_complain],
160 [b4_error([[complain]], $@)])
162 # b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
163 # -------------------------------------------------------
164 # Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
166 # See b4_warn_at example.
167 m4_define([b4_complain_at],
168 [b4_error_at([[complain]], $@)])
170 # b4_fatal(FORMAT, [ARG1], [ARG2], ...)
171 # -------------------------------------
172 # Write @fatal(FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
174 # See b4_warn example.
175 m4_define([b4_fatal],
176 [b4_error([[fatal]], $@)dnl
179 # b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
180 # ----------------------------------------------------
181 # Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
183 # See b4_warn_at example.
184 m4_define([b4_fatal_at],
185 [b4_error_at([[fatal]], $@)dnl
189 ## ---------------- ##
190 ## Default values. ##
191 ## ---------------- ##
193 # m4_define_default([b4_lex_param], []) dnl breaks other skeletons
194 m4_define_default([b4_pre_prologue], [])
195 m4_define_default([b4_post_prologue], [])
196 m4_define_default([b4_epilogue], [])
197 m4_define_default([b4_parse_param], [])
199 # The initial column and line.
200 m4_define_default([b4_location_initial_column], [1])
201 m4_define_default([b4_location_initial_line], [1])
208 # b4_ints_in(INT1, INT2, LOW, HIGH)
209 # ---------------------------------
210 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
211 m4_define([b4_ints_in],
212 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
215 # b4_subtract(LHS, RHS)
216 # ---------------------
217 # Evaluate LHS - RHS if they are integer literals, otherwise expand
219 m4_define([b4_subtract],
220 [m4_bmatch([$1$2], [^[0123456789]*$],
221 [m4_eval([$1 - $2])],
225 # _b4_args(ARG1, ...)
226 # -------------------
227 # Join with comma, skipping empty arguments.
228 # b4_args calls itself recursively until it sees the first non-empty
229 # argument, then calls _b4_args which prepends each non-empty argument
235 [$1[]_$0(m4_shift($@))],
236 [$0(m4_shift($@))])])])
238 # _b4_args(ARGS1, ...)
239 # --------------------
240 m4_define([_b4_args],
243 [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])])
248 ## ------------------ ##
249 ## Decoding options. ##
250 ## ------------------ ##
252 # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
253 # -----------------------------------
254 # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
255 m4_define([b4_flag_if],
259 [m4_fatal([invalid $1 value: ]$1)])])
262 # b4_define_flag_if(FLAG)
263 # -----------------------
264 # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
265 # value of the Boolean FLAG.
266 m4_define([b4_define_flag_if],
267 [_b4_define_flag_if($[1], $[2], [$1])])
269 # _b4_define_flag_if($1, $2, FLAG)
270 # --------------------------------
271 # This macro works around the impossibility to define macros
272 # inside macros, because issuing `[$1]' is not possible in M4 :(.
273 # This sucks hard, GNU M4 should really provide M5 like $$1.
274 m4_define([_b4_define_flag_if],
275 [m4_if([$1$2], $[1]$[2], [],
276 [m4_fatal([$0: Invalid arguments: $@])])dnl
277 m4_define([b4_$3_if],
278 [b4_flag_if([$3], [$1], [$2])])])
281 # b4_FLAG_if(IF-TRUE, IF-FALSE)
282 # -----------------------------
283 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
284 b4_define_flag_if([defines]) # Whether headers are requested.
285 b4_define_flag_if([error_verbose]) # Whether error are verbose.
286 b4_define_flag_if([glr]) # Whether a GLR parser is requested.
287 b4_define_flag_if([locations]) # Whether locations are tracked.
288 b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
289 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
298 # Similar to POSIX basename; the differences don't matter here.
299 # Beware that NAME is not evaluated.
300 m4_define([b4_basename],
301 [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
304 # b4_syncline(LINE, FILE)
305 # -----------------------
306 m4_define([b4_syncline],
307 [b4_flag_if([synclines], [
308 b4_sync_end([__line__], [b4_basename(m4_quote(__file__))])
309 b4_sync_start([$1], [$2])])])
311 m4_define([b4_sync_end], [b4_comment([Line $1 of $2])])
312 m4_define([b4_sync_start], [b4_comment([Line $1 of $2])])
314 # b4_user_code(USER-CODE)
315 # -----------------------
316 # Emit code from the user, ending it with synclines.
317 m4_define([b4_user_code],
319 b4_syncline([@oline@], [@ofile@])])
322 # b4_define_user_code(MACRO)
323 # --------------------------
324 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
325 m4_define([b4_define_user_code],
326 [m4_define([b4_user_$1],
327 [b4_user_code([b4_$1])])])
331 # b4_user_initial_action
332 # b4_user_post_prologue
333 # b4_user_pre_prologue
335 # ----------------------
336 # Macros that issue user code, ending with synclines.
337 b4_define_user_code([actions])
338 b4_define_user_code([initial_action])
339 b4_define_user_code([post_prologue])
340 b4_define_user_code([pre_prologue])
341 b4_define_user_code([stype])
344 # b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
345 # --------------------------------------------------------
346 # Warn if any name of type WHAT is used by the user (as recorded in USER-LIST)
347 # but is not used by Bison (as recorded by macros in the namespace
350 # USER-LIST must expand to a list specifying all grammar occurrences of all
351 # names of type WHAT. Each item in the list must be a triplet specifying one
352 # occurrence: name, start boundary, and end boundary. Empty string names are
353 # fine. An empty list is fine.
355 # For example, to define b4_foo_user_names to be used for USER-LIST with three
356 # name occurrences and with correct quoting:
358 # m4_define([b4_foo_user_names],
359 # [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]],
360 # [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]],
361 # [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]])
363 # The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT
364 # is used by Bison (in the front-end or in the skeleton). Empty string names
365 # are fine, but it would be ugly for Bison to actually use one.
367 # For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that
368 # the names bar and baz are used by Bison:
370 # m4_define([b4_foo_bison_names(bar)])
371 # m4_define([b4_foo_bison_names(baz)])
373 # To invoke b4_check_user_names with TYPE foo, with USER-LIST
374 # b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
377 # b4_check_user_names([[foo]], [b4_foo_user_names],
378 # [[b4_foo_bison_names]])
379 m4_define([b4_check_user_names],
380 [m4_foreach([b4_occurrence], $2,
381 [m4_pushdef([b4_occurrence], b4_occurrence)dnl
382 m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
383 m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
384 m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl
385 m4_ifndef($3[(]m4_quote(b4_user_name)[)],
386 [b4_warn_at([b4_start], [b4_end],
387 [[%s `%s' is not used]],
388 [$1], [b4_user_name])])[]dnl
389 m4_popdef([b4_occurrence])dnl
390 m4_popdef([b4_user_name])dnl
391 m4_popdef([b4_start])dnl
392 m4_popdef([b4_end])dnl
395 # b4_percent_define_get(VARIABLE)
396 # -------------------------------
397 # Mimic muscle_percent_define_get in ../src/muscle_tab.h exactly. That is, if
398 # the %define variable VARIABLE is defined, emit its value. Also, record
399 # Bison's usage of VARIABLE by defining
400 # b4_percent_define_bison_variables(VARIABLE).
404 # b4_percent_define_get([[foo]])
405 m4_define([b4_percent_define_get],
406 [m4_define([b4_percent_define_bison_variables(]$1[)])dnl
407 m4_ifdef([b4_percent_define(]$1[)], [m4_indir([b4_percent_define(]$1[)])])])
409 # b4_percent_define_get_loc(VARIABLE)
410 # -----------------------------------
411 # Mimic muscle_percent_define_get_loc in ../src/muscle_tab.h exactly. That is,
412 # if the %define variable VARIABLE is undefined, complain fatally since that's
413 # a Bison or skeleton error. Otherwise, return its definition location in a
414 # form approriate for the first two arguments of b4_warn_at, b4_complain_at, or
415 # b4_fatal_at. Don't record this as a Bison usage of VARIABLE as there's no
416 # reason to suspect that the user-supplied value has yet influenced the output.
420 # b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
421 m4_define([b4_percent_define_get_loc],
422 [m4_ifdef([b4_percent_define_loc(]$1[)],
423 [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl
425 m4_popdef([b4_loc])],
426 [b4_fatal([[undefined %%define variable `%s' passed to b4_percent_define_get_loc]], [$1])])])
428 # b4_percent_define_get_syncline(VARIABLE)
429 # ----------------------------------------
430 # Mimic muscle_percent_define_get_syncline in ../src/muscle_tab.h exactly.
431 # That is, if the %define variable VARIABLE is undefined, complain fatally
432 # since that's a Bison or skeleton error. Otherwise, return its definition
433 # location as a b4_syncline invocation. Don't record this as a Bison usage of
434 # VARIABLE as there's no reason to suspect that the user-supplied value has yet
435 # influenced the output.
439 # b4_percent_define_get_syncline([[foo]])
440 m4_define([b4_percent_define_get_syncline],
441 [m4_ifdef([b4_percent_define_syncline(]$1[)],
442 [m4_indir([b4_percent_define_syncline(]$1[)])],
443 [b4_fatal([[undefined %%define variable `%s' passed to b4_percent_define_get_syncline]], [$1])])])
445 # b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
446 # ------------------------------------------------------
447 # Mimic muscle_percent_define_ifdef in ../src/muscle_tab.h exactly. That is,
448 # if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
449 # IF-FALSE. Also, record Bison's usage of VARIABLE by defining
450 # b4_percent_define_bison_variables(VARIABLE).
454 # b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
455 m4_define([b4_percent_define_ifdef],
456 [m4_ifdef([b4_percent_define(]$1[)],
457 [m4_define([b4_percent_define_bison_variables(]$1[)])$2],
460 # b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
461 # --------------------------------------------------------
462 # Mimic muscle_percent_define_flag_if in ../src/muscle_tab.h exactly. That is,
463 # if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE.
464 # If it is defined to "false", expand IF-FALSE. Complain if it is undefined
465 # (a Bison or skeleton error since the default value should have been set
466 # already) or defined to any other value (possibly a user error). Also, record
467 # Bison's usage of VARIABLE by defining
468 # b4_percent_define_bison_variables(VARIABLE).
472 # b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
473 m4_define([b4_percent_define_flag_if],
474 [b4_percent_define_ifdef([$1],
475 [m4_case(b4_percent_define_get([$1]),
476 [], [$2], [true], [$2], [false], [$3],
477 [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
478 [[invalid value for %%define Boolean variable `%s']],
480 [[b4_percent_define_flag_if($1)]])])],
481 [b4_fatal([[undefined %%define variable `%s' passed to b4_percent_define_flag_if]], [$1])])])
483 # b4_percent_define_default(VARIABLE, DEFAULT)
484 # --------------------------------------------
485 # Mimic muscle_percent_define_default in ../src/muscle_tab.h exactly. That is,
486 # if the %define variable VARIABLE is undefined, set its value to DEFAULT.
487 # Don't record this as a Bison usage of VARIABLE as there's no reason to
488 # suspect that the value has yet influenced the output.
492 # b4_percent_define_default([[foo]], [[default value]])
493 m4_define([b4_percent_define_default],
494 [m4_ifndef([b4_percent_define(]$1[)],
495 [m4_define([b4_percent_define(]$1[)], [$2])dnl
496 m4_define([b4_percent_define_loc(]$1[)],
497 [[[[[Bison:b4_percent_define_default]:1.0]], [[[Bison:b4_percent_define_default]:1.0]]]])dnl
498 m4_define([b4_percent_define_syncline(]$1[)],
499 [[]b4_syncline(1, [["[Bison:b4_percent_define_default]"]])[
502 # b4_percent_define_check_values(VALUES)
503 # --------------------------------------
504 # Mimic muscle_percent_define_check_values in ../src/muscle_tab.h exactly
505 # except that the VALUES structure is more appropriate for M4. That is, VALUES
506 # is a list of sublists of strings. For each sublist, the first string is the
507 # name of a %define variable, and all remaining strings in that sublist are the
508 # valid values for that variable. Complain if such a variable is undefined (a
509 # Bison error since the default value should have been set already) or defined
510 # to any other value (possibly a user error). Don't record this as a Bison
511 # usage of the variable as there's no reason to suspect that the value has yet
512 # influenced the output.
516 # b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
517 # [[[[bar]], [[bar-value1]]]])
518 m4_define([b4_percent_define_check_values],
519 [m4_foreach([b4_sublist], m4_quote($@),
520 [_b4_percent_define_check_values(b4_sublist)])])
522 m4_define([_b4_percent_define_check_values],
523 [m4_ifdef([b4_percent_define(]$1[)],
524 [m4_pushdef([b4_good_value], [0])dnl
526 [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
527 [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
528 [m4_define([b4_good_value], [1])])])])dnl
529 m4_if(b4_good_value, [0],
530 [b4_complain_at(b4_percent_define_get_loc([$1]),
531 [[invalid value for %%define variable `%s': `%s']],
533 m4_dquote(m4_indir([b4_percent_define(]$1[)])))])dnl
534 m4_popdef([b4_good_value])],
535 [b4_fatal([[undefined %%define variable `%s' passed to b4_percent_define_check_values]], [$1])])])
537 # b4_percent_code_get([QUALIFIER])
538 # --------------------------------
539 # If any %code blocks for QUALIFIER are defined, emit them beginning with a
540 # comment and ending with synclines and a newline. If QUALIFIER is not
541 # specified or empty, do this for the unqualified %code blocks. Also, record
542 # Bison's usage of QUALIFIER (if specified) by defining
543 # b4_percent_code_bison_qualifiers(QUALIFIER).
545 # For example, to emit any unqualified %code blocks followed by any %code
546 # blocks for the qualifier foo:
548 # b4_percent_code_get
549 # b4_percent_code_get([[foo]])
550 m4_define([b4_percent_code_get],
551 [m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
552 m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
553 m4_ifdef(b4_macro_name,
554 [b4_comment([m4_if([$#], [0], [[Unqualified %code]],
555 [["%code ]$1["]])[ blocks.]])
556 b4_user_code([m4_indir(b4_macro_name)])
558 m4_popdef([b4_macro_name])])
560 # b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
561 # -----------------------------------------------------
562 # If any %code blocks for QUALIFIER (or unqualified %code blocks if
563 # QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
564 # Also, record Bison's usage of QUALIFIER (if specified) by defining
565 # b4_percent_code_bison_qualifiers(QUALIFIER).
566 m4_define([b4_percent_code_ifdef],
567 [m4_ifdef([b4_percent_code(]$1[)],
568 [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
572 # Default values for %define.
573 # ---------------------------
574 # If the %token.prefix, it is empty.
575 m4_percent_define_default([[token.prefix]], [[]])
579 ## ----------------------------------------------------------- ##
580 ## After processing the skeletons, check that all the user's ##
581 ## %define variables and %code qualifiers were used by Bison. ##
582 ## ----------------------------------------------------------- ##
584 m4_define([b4_check_user_names_wrap],
585 [m4_ifdef([b4_percent_]$1[_user_]$2[s],
586 [b4_check_user_names([[%]$1 $2],
587 [b4_percent_]$1[_user_]$2[s],
588 [[b4_percent_]$1[_bison_]$2[s]])])])
591 b4_check_user_names_wrap([[define]], [[variable]])
592 b4_check_user_names_wrap([[code]], [[qualifier]])