]> git.saurik.com Git - bison.git/blob - data/bison.m4
bison.m4: support b4_*_if macros whose name differ from their variable
[bison.git] / data / bison.m4
1 -*- Autoconf -*-
2
3 # Language-independent M4 Macros for Bison.
4
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
6
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.
11 #
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.
16 #
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/>.
19
20
21 ## ---------------- ##
22 ## Identification. ##
23 ## ---------------- ##
24
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.])
30
31 b4_comment([$1
32
33 ]m4_dquote(m4_text_wrap([Copyright (C)
34 ]m4_ifval([$2], [[$2]], [m4_defn([b4_copyright_years])])[
35 Free Software Foundation, Inc.]))[
36
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.
41
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.
46
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/>.])
49
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.
59
60 This special exception was added by the Free Software Foundation in
61 version 2.2 of Bison.])])
62
63
64 ## ---------------- ##
65 ## Error handling. ##
66 ## ---------------- ##
67
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.
71
72 # b4_cat(TEXT)
73 # ------------
74 # Write TEXT to stdout. Precede the final newline with an @ so that it's
75 # escaped. For example:
76 #
77 # b4_cat([[@complain(invalid input@)]])
78 m4_define([b4_cat],
79 [m4_syscmd([cat <<'_m4eof'
80 ]m4_bpatsubst(m4_dquote($1), [_m4eof], [_m4@`eof])[@
81 _m4eof
82 ])dnl
83 m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write to stdout])])])
84
85 # b4_error(KIND, START, END, FORMAT, [ARG1], [ARG2], ...)
86 # -------------------------------------------------------
87 # Write @KIND(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
88 #
89 # For example:
90 #
91 # b4_error([[complain]], [[input.y:2.3]], [[input.y:5.4]],
92 # [[invalid %s]], [[foo]])
93 m4_define([b4_error],
94 [b4_cat([[@complain][(]$1[@,]$2[@,]$3[@,]$4[]]dnl
95 [m4_if([$#], [4], [],
96 [m4_foreach([b4_arg],
97 m4_dquote(m4_shift(m4_shift(m4_shift(m4_shift($@))))),
98 [[@,]b4_arg])])[@)]])])
99
100 # b4_warn(FORMAT, [ARG1], [ARG2], ...)
101 # ------------------------------------
102 # Write @warn(FORMAT@,ARG1@,ARG2@,...@) to stdout.
103 #
104 # For example:
105 #
106 # b4_warn([[invalid value for '%s': %s]], [[foo]], [[3]])
107 #
108 # As a simple test suite, this:
109 #
110 # m4_divert(-1)
111 # m4_define([asdf], [ASDF])
112 # m4_define([fsa], [FSA])
113 # m4_define([fdsa], [FDSA])
114 # b4_warn_at([[[asdf), asdf]]], [[[fsa), fsa]]], [[[fdsa), fdsa]]])
115 # b4_warn_at([[asdf), asdf]], [[fsa), fsa]], [[fdsa), fdsa]])
116 # b4_warn_at()
117 # b4_warn_at(1)
118 # b4_warn_at(1, 2)
119 #
120 # Should produce this without newlines:
121 #
122 # @warn_at([asdf), asdf]@,@,@,[fsa), fsa]@,[fdsa), fdsa]@)
123 # @warn(asdf), asdf@,@,@,fsa), fsa@,fdsa), fdsa@)
124 # @warn(@)
125 # @warn(1@)
126 # @warn(1@,2@)
127 m4_define([b4_warn],
128 [b4_error([[warn]], [], [], $@)])
129
130 # b4_warn_at(START, END, FORMAT, [ARG1], [ARG2], ...)
131 # ---------------------------------------------------
132 # Write @warn(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
133 #
134 # For example:
135 #
136 # b4_warn_at([[input.y:2.3]], [[input.y:5.4]], [[invalid %s]], [[foo]])
137 m4_define([b4_warn_at],
138 [b4_error([[warn]], $@)])
139
140 # b4_complain(FORMAT, [ARG1], [ARG2], ...)
141 # ----------------------------------------
142 # Bounce to b4_complain_at.
143 #
144 # See b4_warn example.
145 m4_define([b4_complain],
146 [b4_error([[complain]], [], [], $@)])
147
148 # b4_complain_at(START, END, FORMAT, [ARG1], [ARG2], ...)
149 # -------------------------------------------------------
150 # Write @complain(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout.
151 #
152 # See b4_warn_at example.
153 m4_define([b4_complain_at],
154 [b4_error([[complain]], $@)])
155
156 # b4_fatal(FORMAT, [ARG1], [ARG2], ...)
157 # -------------------------------------
158 # Bounce to b4_fatal_at.
159 #
160 # See b4_warn example.
161 m4_define([b4_fatal],
162 [b4_error([[fatal]], [], [], $@)dnl
163 m4_exit(1)])
164
165 # b4_fatal_at(START, END, FORMAT, [ARG1], [ARG2], ...)
166 # ----------------------------------------------------
167 # Write @fatal(START@,END@,FORMAT@,ARG1@,ARG2@,...@) to stdout and exit.
168 #
169 # See b4_warn_at example.
170 m4_define([b4_fatal_at],
171 [b4_error([[fatal]], $@)dnl
172 m4_exit(1)])
173
174
175 ## ------------ ##
176 ## Data Types. ##
177 ## ------------ ##
178
179 # b4_ints_in(INT1, INT2, LOW, HIGH)
180 # ---------------------------------
181 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
182 m4_define([b4_ints_in],
183 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
184
185
186 # b4_subtract(LHS, RHS)
187 # ---------------------
188 # Evaluate LHS - RHS if they are integer literals, otherwise expand
189 # to (LHS) - (RHS).
190 m4_define([b4_subtract],
191 [m4_bmatch([$1$2], [^[0123456789]*$],
192 [m4_eval([$1 - $2])],
193 [($1) - ($2)])])
194
195 # b4_join(ARG1, ...)
196 # _b4_join(ARG1, ...)
197 # -------------------
198 # Join with comma, skipping empty arguments.
199 # b4_join calls itself recursively until it sees the first non-empty
200 # argument, then calls _b4_join which prepends each non-empty argument
201 # with a comma.
202 m4_define([b4_join],
203 [m4_if([$#$1],
204 [1], [],
205 [m4_ifval([$1],
206 [$1[]_$0(m4_shift($@))],
207 [$0(m4_shift($@))])])])
208
209 # _b4_join(ARGS1, ...)
210 # --------------------
211 m4_define([_b4_join],
212 [m4_if([$#$1],
213 [1], [],
214 [m4_ifval([$1], [, $1])[]$0(m4_shift($@))])])
215
216
217
218
219 # b4_integral_parser_tables_map(MACRO)
220 # -------------------------------------
221 # Map MACRO on all the integral tables. MACRO is expected to have
222 # the signature MACRO(TABLE-NAME, CONTENT, COMMENT).
223 m4_define([b4_integral_parser_tables_map],
224 [$1([pact], [b4_pact],
225 [[YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
226 STATE-NUM.]])
227
228 $1([defact], [b4_defact],
229 [[YYDEFACT[S] -- default reduction number in state S. Performed when
230 YYTABLE does not specify something else to do. Zero means the default
231 is an error.]])
232
233 $1([pgoto], [b4_pgoto], [[YYPGOTO[NTERM-NUM].]])
234
235 $1([defgoto], [b4_defgoto], [[YYDEFGOTO[NTERM-NUM].]])
236
237 $1([table], [b4_table],
238 [[YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
239 positive, shift that token. If negative, reduce the rule which
240 number is the opposite. If YYTABLE_NINF, syntax error.]])
241
242 $1([check], [b4_check])
243
244 $1([stos], [b4_stos],
245 [[STOS_[STATE-NUM] -- The (internal number of the) accessing
246 symbol of state STATE-NUM.]])
247
248 $1([r1], [b4_r1],
249 [[YYR1[YYN] -- Symbol number of symbol that rule YYN derives.]])
250
251 $1([r2], [b4_r2],
252 [[YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.]])
253 ])
254
255
256 # b4_parser_tables_declare
257 # b4_parser_tables_define
258 # ------------------------
259 # Define/declare the (deterministic) parser tables.
260 m4_define([b4_parser_tables_declare],
261 [b4_integral_parser_tables_map([b4_integral_parser_table_declare])])
262
263 m4_define([b4_parser_tables_define],
264 [b4_integral_parser_tables_map([b4_integral_parser_table_define])])
265
266
267
268 ## ------------------ ##
269 ## Decoding options. ##
270 ## ------------------ ##
271
272 # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
273 # -----------------------------------
274 # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
275 m4_define([b4_flag_if],
276 [m4_case(b4_$1_flag,
277 [0], [$3],
278 [1], [$2],
279 [m4_fatal([invalid $1 value: ]$1)])])
280
281
282 # b4_define_flag_if(FLAG)
283 # -----------------------
284 # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
285 # value of the Boolean FLAG.
286 m4_define([b4_define_flag_if],
287 [_b4_define_flag_if($[1], $[2], [$1])])
288
289 # _b4_define_flag_if($1, $2, FLAG)
290 # --------------------------------
291 # Work around the impossibility to define macros inside macros,
292 # because issuing `[$1]' is not possible in M4. GNU M4 should provide
293 # $$1 a la M5/TeX.
294 m4_define([_b4_define_flag_if],
295 [m4_if([$1$2], $[1]$[2], [],
296 [m4_fatal([$0: Invalid arguments: $@])])dnl
297 m4_define([b4_$3_if],
298 [b4_flag_if([$3], [$1], [$2])])])
299
300
301 # b4_FLAG_if(IF-TRUE, IF-FALSE)
302 # -----------------------------
303 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
304 b4_define_flag_if([defines]) # Whether headers are requested.
305 b4_define_flag_if([glr]) # Whether a GLR parser is requested.
306 b4_define_flag_if([nondeterministic]) # Whether conflicts should be handled.
307 b4_define_flag_if([token_table]) # Whether yytoken_table is demanded.
308 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
309
310
311 ## --------- ##
312 ## Symbols. ##
313 ## --------- ##
314
315 # In order to unify the handling of the various aspects of symbols
316 # (tag, type_name, whether terminal, etc.), bison.exe defines one
317 # macro per (token, field), where field can has_id, id, etc.: see
318 # src/output.c:prepare_symbols_definitions().
319 #
320 # The following macros provide access to these values.
321
322 # b4_symbol_(NUM, FIELD)
323 # ----------------------
324 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
325 # undefined.
326 m4_define([b4_symbol_],
327 [m4_indir([b4_symbol($1, $2)])])
328
329
330 # b4_symbol(NUM, FIELD)
331 # ---------------------
332 # Recover a FIELD about symbol #NUM. Thanks to m4_indir, fails if
333 # undefined. If FIELD = id, prepend the prefix.
334 m4_define([b4_symbol],
335 [m4_case([$2],
336 [id], [m4_do([b4_percent_define_get([api.token.prefix])],
337 [b4_symbol_([$1], [id])])],
338 [b4_symbol_($@)])])
339
340
341 # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
342 # -------------------------------------------
343 # If FIELD about symbol #NUM is 1 expand IF-TRUE, if is 0, expand IF-FALSE.
344 # Otherwise an error.
345 m4_define([b4_symbol_if],
346 [m4_case(b4_symbol([$1], [$2]),
347 [1], [$3],
348 [0], [$4],
349 [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])])
350
351
352 # b4_symbol_action_location(SYMBOL-NUM, KIND)
353 # -------------------------------------------
354 # Report the location of the KIND action as FILE:LINE.
355 m4_define([b4_symbol_action_location],
356 [b4_symbol([$1], [$2_file]):b4_syncline([b4_symbol([$1], [$2_line])])])
357
358
359 # b4_symbol_action(SYMBOL-NUM, KIND)
360 # ----------------------------------
361 # Run the action KIND (destructor or printer) for SYMBOL-NUM.
362 # Same as in C, but using references instead of pointers.
363 m4_define([b4_symbol_action],
364 [b4_symbol_if([$1], [has_$2],
365 [b4_dollar_pushdef([(*yyvaluep)],
366 b4_symbol_if([$1], [has_type],
367 [m4_dquote(b4_symbol([$1], [type]))]),
368 [(*yylocationp)])dnl
369 b4_symbol_case_([$1])[]dnl
370 b4_syncline([b4_symbol([$1], [$2_line])], ["b4_symbol([$1], [$2_file])"])
371 b4_symbol([$1], [$2])
372 b4_syncline([@oline@], [@ofile@])
373 break;
374
375 b4_dollar_popdef[]dnl
376 ])])
377
378
379 # b4_symbol_destructor(SYMBOL-NUM)
380 # b4_symbol_printer(SYMBOL-NUM)
381 # --------------------------------
382 m4_define([b4_symbol_destructor], [b4_symbol_action([$1], [destructor])])
383 m4_define([b4_symbol_printer], [b4_symbol_action([$1], [printer])])
384
385
386 # b4_symbol_case_(SYMBOL-NUM)
387 # ---------------------------
388 # Issue a "case NUM" for SYMBOL-NUM.
389 m4_define([b4_symbol_case_],
390 [ case b4_symbol([$1], [number]): // b4_symbol([$1], [tag])
391 ])
392
393
394 # b4_symbol_foreach(MACRO)
395 # ------------------------
396 # Invoke MACRO(SYMBOL-NUM) for each SYMBOL-NUM.
397 m4_define([b4_symbol_foreach],
398 [m4_map([$1], m4_defn([b4_symbol_numbers]))])
399
400 # b4_symbol_map(MACRO)
401 # --------------------
402 # Return a list (possibly empty elements) of MACRO invoked for each
403 # SYMBOL-NUM.
404 m4_define([b4_symbol_map],
405 [m4_map_args_sep([$1(], [)], [,], b4_symbol_numbers)])
406
407
408 # b4_token_visible_if(NUM, IF-TRUE, IF-FALSE)
409 # -------------------------------------------
410 # Whether NUM denotes a token that has an exported definition (i.e.,
411 # shows in enum yytokentype).
412 m4_define([b4_token_visible_if],
413 [b4_symbol_if([$1], [is_token],
414 [b4_symbol_if([$1], [has_id], [$2], [$3])],
415 [$3])])
416
417 # b4_token_has_definition(NUM)
418 # ----------------------------
419 # 1 if NUM is visible, nothing otherwise.
420 m4_define([b4_token_has_definition],
421 [b4_token_visible_if([$1], [1])])
422
423 # b4_any_token_visible_if([IF-TRUE], [IF-FALSE])
424 # ----------------------------------------------
425 # Whether there is a token that needs to be defined.
426 m4_define([b4_any_token_visible_if],
427 [m4_ifval(b4_symbol_foreach([b4_token_has_definition]),
428 [$1], [$2])])
429
430
431 # b4_token_format(FORMAT, NUM)
432 # ----------------------------
433 m4_define([b4_token_format],
434 [b4_token_visible_if([$2],
435 [m4_quote(m4_format([$1],
436 [b4_symbol([$2], [id])],
437 [b4_symbol([$2], [user_number])]))])])
438
439
440 ## ------- ##
441 ## Types. ##
442 ## ------- ##
443
444 # b4_type_action_(NUMS)
445 # ---------------------
446 # Run actions for the symbol NUMS that all have the same type-name.
447 # Skip NUMS that have no type-name.
448 m4_define([b4_type_action_],
449 [b4_symbol_if([$1], [has_type],
450 [m4_map([b4_symbol_case_], [$@])[]dnl
451 b4_dollar_dollar([b4_symbol([$1], [number])],
452 [b4_symbol([$1], [tag])],
453 [b4_symbol([$1], [type])]);
454 break;
455
456 ])])
457
458 # b4_type_foreach(MACRO)
459 # ----------------------
460 # Invoke MACRO(SYMBOL-NUMS) for each set of SYMBOL-NUMS for each type set.
461 m4_define([b4_type_foreach],
462 [m4_map([$1], m4_defn([b4_type_names]))])
463
464
465
466 ## ----------- ##
467 ## Synclines. ##
468 ## ----------- ##
469
470 # b4_basename(NAME)
471 # -----------------
472 # Similar to POSIX basename; the differences don't matter here.
473 # Beware that NAME is not evaluated.
474 m4_define([b4_basename],
475 [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
476
477
478 # b4_syncline(LINE, FILE)
479 # -----------------------
480 m4_define([b4_syncline],
481 [b4_flag_if([synclines],
482 [b4_sync_end([__line__], [b4_basename(m4_quote(__file__))])
483 b4_sync_start([$1], [$2])])])
484
485 m4_define([b4_sync_end], [b4_comment([Line $1 of $2])])
486 m4_define([b4_sync_start], [b4_comment([Line $1 of $2])])
487
488 # b4_user_code(USER-CODE)
489 # -----------------------
490 # Emit code from the user, ending it with synclines.
491 m4_define([b4_user_code],
492 [$1
493 b4_syncline([@oline@], [@ofile@])])
494
495
496 # b4_define_user_code(MACRO)
497 # --------------------------
498 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
499 m4_define([b4_define_user_code],
500 [m4_define([b4_user_$1],
501 [b4_user_code([b4_$1])])])
502
503
504 # b4_user_actions
505 # b4_user_initial_action
506 # b4_user_post_prologue
507 # b4_user_pre_prologue
508 # b4_user_stype
509 # ----------------------
510 # Macros that issue user code, ending with synclines.
511 b4_define_user_code([actions])
512 b4_define_user_code([initial_action])
513 b4_define_user_code([post_prologue])
514 b4_define_user_code([pre_prologue])
515 b4_define_user_code([stype])
516
517
518 # b4_check_user_names(WHAT, USER-LIST, BISON-NAMESPACE)
519 # -----------------------------------------------------
520 # Complain if any name of type WHAT is used by the user (as recorded in
521 # USER-LIST) but is not used by Bison (as recorded by macros in the
522 # namespace BISON-NAMESPACE).
523 #
524 # USER-LIST must expand to a list specifying all user occurrences of all names
525 # of type WHAT. Each item in the list must be a triplet specifying one
526 # occurrence: name, start boundary, and end boundary. Empty string names are
527 # fine. An empty list is fine.
528 #
529 # For example, to define b4_foo_user_names to be used for USER-LIST with three
530 # name occurrences and with correct quoting:
531 #
532 # m4_define([b4_foo_user_names],
533 # [[[[[[bar]], [[parser.y:1.7]], [[parser.y:1.16]]]],
534 # [[[[bar]], [[parser.y:5.7]], [[parser.y:5.16]]]],
535 # [[[[baz]], [[parser.y:8.7]], [[parser.y:8.16]]]]]])
536 #
537 # The macro BISON-NAMESPACE(bar) must be defined iff the name bar of type WHAT
538 # is used by Bison (in the front-end or in the skeleton). Empty string names
539 # are fine, but it would be ugly for Bison to actually use one.
540 #
541 # For example, to use b4_foo_bison_names for BISON-NAMESPACE and define that
542 # the names bar and baz are used by Bison:
543 #
544 # m4_define([b4_foo_bison_names(bar)])
545 # m4_define([b4_foo_bison_names(baz)])
546 #
547 # To invoke b4_check_user_names with TYPE foo, with USER-LIST
548 # b4_foo_user_names, with BISON-NAMESPACE b4_foo_bison_names, and with correct
549 # quoting:
550 #
551 # b4_check_user_names([[foo]], [b4_foo_user_names],
552 # [[b4_foo_bison_names]])
553 m4_define([b4_check_user_names],
554 [m4_foreach([b4_occurrence], $2,
555 [m4_pushdef([b4_occurrence], b4_occurrence)dnl
556 m4_pushdef([b4_user_name], m4_car(b4_occurrence))dnl
557 m4_pushdef([b4_start], m4_car(m4_shift(b4_occurrence)))dnl
558 m4_pushdef([b4_end], m4_shift(m4_shift(b4_occurrence)))dnl
559 m4_ifndef($3[(]m4_quote(b4_user_name)[)],
560 [b4_complain_at([b4_start], [b4_end],
561 [[%s '%s' is not used]],
562 [$1], [b4_user_name])])[]dnl
563 m4_popdef([b4_occurrence])dnl
564 m4_popdef([b4_user_name])dnl
565 m4_popdef([b4_start])dnl
566 m4_popdef([b4_end])dnl
567 ])])
568
569
570
571 ## --------------------- ##
572 ## b4_percent_define_*. ##
573 ## --------------------- ##
574
575
576 # b4_percent_define_use(VARIABLE)
577 # -------------------------------
578 # Declare that VARIABLE was used.
579 m4_define([b4_percent_define_use],
580 [m4_define([b4_percent_define_bison_variables(]$1[)])dnl
581 ])
582
583 # b4_percent_define_get(VARIABLE, [DEFAULT])
584 # ------------------------------------------
585 # Mimic muscle_percent_define_get in ../src/muscle-tab.h. That is, if
586 # the %define variable VARIABLE is defined, emit its value. Contrary
587 # to its C counterpart, return DEFAULT otherwise. Also, record
588 # Bison's usage of VARIABLE by defining
589 # b4_percent_define_bison_variables(VARIABLE).
590 #
591 # For example:
592 #
593 # b4_percent_define_get([[foo]])
594 m4_define([b4_percent_define_get],
595 [b4_percent_define_use([$1])dnl
596 m4_ifdef([b4_percent_define(]$1[)],
597 [m4_indir([b4_percent_define(]$1[)])],
598 [$2])])
599
600 # b4_percent_define_get_loc(VARIABLE)
601 # -----------------------------------
602 # Mimic muscle_percent_define_get_loc in ../src/muscle-tab.h exactly. That is,
603 # if the %define variable VARIABLE is undefined, complain fatally since that's
604 # a Bison or skeleton error. Otherwise, return its definition location in a
605 # form approriate for the first two arguments of b4_warn_at, b4_complain_at, or
606 # b4_fatal_at. Don't record this as a Bison usage of VARIABLE as there's no
607 # reason to suspect that the user-supplied value has yet influenced the output.
608 #
609 # For example:
610 #
611 # b4_complain_at(b4_percent_define_get_loc([[foo]]), [[invalid foo]])
612 m4_define([b4_percent_define_get_loc],
613 [m4_ifdef([b4_percent_define_loc(]$1[)],
614 [m4_pushdef([b4_loc], m4_indir([b4_percent_define_loc(]$1[)]))dnl
615 b4_loc[]dnl
616 m4_popdef([b4_loc])],
617 [b4_fatal([[b4_percent_define_get_loc: undefined %%define variable '%s']], [$1])])])
618
619 # b4_percent_define_get_syncline(VARIABLE)
620 # ----------------------------------------
621 # Mimic muscle_percent_define_get_syncline in ../src/muscle-tab.h exactly.
622 # That is, if the %define variable VARIABLE is undefined, complain fatally
623 # since that's a Bison or skeleton error. Otherwise, return its definition
624 # location as a b4_syncline invocation. Don't record this as a Bison usage of
625 # VARIABLE as there's no reason to suspect that the user-supplied value has yet
626 # influenced the output.
627 #
628 # For example:
629 #
630 # b4_percent_define_get_syncline([[foo]])
631 m4_define([b4_percent_define_get_syncline],
632 [m4_ifdef([b4_percent_define_syncline(]$1[)],
633 [m4_indir([b4_percent_define_syncline(]$1[)])],
634 [b4_fatal([[b4_percent_define_get_syncline: undefined %%define variable '%s']], [$1])])])
635
636 # b4_percent_define_ifdef(VARIABLE, IF-TRUE, [IF-FALSE])
637 # ------------------------------------------------------
638 # Mimic muscle_percent_define_ifdef in ../src/muscle-tab.h exactly. That is,
639 # if the %define variable VARIABLE is defined, expand IF-TRUE, else expand
640 # IF-FALSE. Also, record Bison's usage of VARIABLE by defining
641 # b4_percent_define_bison_variables(VARIABLE).
642 #
643 # For example:
644 #
645 # b4_percent_define_ifdef([[foo]], [[it's defined]], [[it's undefined]])
646 m4_define([b4_percent_define_ifdef],
647 [m4_ifdef([b4_percent_define(]$1[)],
648 [b4_percent_define_use([$1])$2],
649 [$3])])
650
651
652 ## --------- ##
653 ## Options. ##
654 ## --------- ##
655
656
657 # b4_percent_define_flag_if(VARIABLE, IF-TRUE, [IF-FALSE])
658 # --------------------------------------------------------
659 # Mimic muscle_percent_define_flag_if in ../src/muscle-tab.h exactly. That is,
660 # if the %define variable VARIABLE is defined to "" or "true", expand IF-TRUE.
661 # If it is defined to "false", expand IF-FALSE. Complain if it is undefined
662 # (a Bison or skeleton error since the default value should have been set
663 # already) or defined to any other value (possibly a user error). Also, record
664 # Bison's usage of VARIABLE by defining
665 # b4_percent_define_bison_variables(VARIABLE).
666 #
667 # For example:
668 #
669 # b4_percent_define_flag_if([[foo]], [[it's true]], [[it's false]])
670 m4_define([b4_percent_define_flag_if],
671 [b4_percent_define_ifdef([$1],
672 [m4_case(b4_percent_define_get([$1]),
673 [], [$2], [true], [$2], [false], [$3],
674 [m4_expand_once([b4_complain_at(b4_percent_define_get_loc([$1]),
675 [[invalid value for %%define Boolean variable '%s']],
676 [$1])],
677 [[b4_percent_define_flag_if($1)]])])],
678 [b4_fatal([[b4_percent_define_flag_if: undefined %%define variable '%s']], [$1])])])
679
680
681 # b4_percent_define_default(VARIABLE, DEFAULT)
682 # --------------------------------------------
683 # Mimic muscle_percent_define_default in ../src/muscle-tab.h exactly. That is,
684 # if the %define variable VARIABLE is undefined, set its value to DEFAULT.
685 # Don't record this as a Bison usage of VARIABLE as there's no reason to
686 # suspect that the value has yet influenced the output.
687 #
688 # For example:
689 #
690 # b4_percent_define_default([[foo]], [[default value]])
691 m4_define([b4_percent_define_default],
692 [m4_ifndef([b4_percent_define(]$1[)],
693 [m4_define([b4_percent_define(]$1[)], [$2])dnl
694 m4_define([b4_percent_define_loc(]$1[)],
695 [[[[<skeleton default value>:-1.-1]],
696 [[<skeleton default value>:-1.-1]]]])dnl
697 m4_define([b4_percent_define_syncline(]$1[)], [[]])])])
698
699
700 # b4_percent_define_if_define(NAME, [VARIABLE = NAME])
701 # ----------------------------------------------------
702 # Define b4_NAME_if that executes its $1 or $2 depending whether
703 # VARIABLE was %defined. The characters `.' and `-' in VARIABLE are mapped
704 # to `_'.
705 m4_define([b4_percent_define_if_define_],
706 [m4_define(m4_bpatsubst([b4_$1_if], [[-.]], [_]),
707 [b4_percent_define_flag_if(m4_default([$2], [$1]),
708 [$3], [$4])])])
709 m4_define([b4_percent_define_if_define],
710 [b4_percent_define_default([m4_default([$2], [$1])], [[false]])
711 b4_percent_define_if_define_([$1], [$2], $[1], $[2])])
712
713
714 # b4_percent_define_check_values(VALUES)
715 # --------------------------------------
716 # Mimic muscle_percent_define_check_values in ../src/muscle-tab.h exactly
717 # except that the VALUES structure is more appropriate for M4. That is, VALUES
718 # is a list of sublists of strings. For each sublist, the first string is the
719 # name of a %define variable, and all remaining strings in that sublist are the
720 # valid values for that variable. Complain if such a variable is undefined (a
721 # Bison error since the default value should have been set already) or defined
722 # to any other value (possibly a user error). Don't record this as a Bison
723 # usage of the variable as there's no reason to suspect that the value has yet
724 # influenced the output.
725 #
726 # For example:
727 #
728 # b4_percent_define_check_values([[[[foo]], [[foo-value1]], [[foo-value2]]]],
729 # [[[[bar]], [[bar-value1]]]])
730 m4_define([b4_percent_define_check_values],
731 [m4_foreach([b4_sublist], m4_quote($@),
732 [_b4_percent_define_check_values(b4_sublist)])])
733
734 m4_define([_b4_percent_define_check_values],
735 [m4_ifdef([b4_percent_define(]$1[)],
736 [m4_pushdef([b4_good_value], [0])dnl
737 m4_if($#, 1, [],
738 [m4_foreach([b4_value], m4_dquote(m4_shift($@)),
739 [m4_if(m4_indir([b4_percent_define(]$1[)]), b4_value,
740 [m4_define([b4_good_value], [1])])])])dnl
741 m4_if(b4_good_value, [0],
742 [b4_complain_at(b4_percent_define_get_loc([$1]),
743 [[invalid value for %%define variable '%s': '%s']],
744 [$1],
745 m4_dquote(m4_indir([b4_percent_define(]$1[)])))
746 m4_foreach([b4_value], m4_dquote(m4_shift($@)),
747 [b4_error([[note]], b4_percent_define_get_loc([$1]), []
748 [[accepted value: '%s']],
749 m4_dquote(b4_value))])])dnl
750 m4_popdef([b4_good_value])],
751 [b4_fatal([[b4_percent_define_check_values: undefined %%define variable '%s']], [$1])])])
752
753 # b4_percent_code_get([QUALIFIER])
754 # --------------------------------
755 # If any %code blocks for QUALIFIER are defined, emit them beginning with a
756 # comment and ending with synclines and a newline. If QUALIFIER is not
757 # specified or empty, do this for the unqualified %code blocks. Also, record
758 # Bison's usage of QUALIFIER (if specified) by defining
759 # b4_percent_code_bison_qualifiers(QUALIFIER).
760 #
761 # For example, to emit any unqualified %code blocks followed by any %code
762 # blocks for the qualifier foo:
763 #
764 # b4_percent_code_get
765 # b4_percent_code_get([[foo]])
766 m4_define([b4_percent_code_get],
767 [m4_pushdef([b4_macro_name], [[b4_percent_code(]$1[)]])dnl
768 m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])dnl
769 m4_ifdef(b4_macro_name,
770 [b4_comment([m4_if([$#], [0], [[Unqualified %code]],
771 [["%code ]$1["]])[ blocks.]])
772 b4_user_code([m4_indir(b4_macro_name)])
773 ])dnl
774 m4_popdef([b4_macro_name])])
775
776 # b4_percent_code_ifdef(QUALIFIER, IF-TRUE, [IF-FALSE])
777 # -----------------------------------------------------
778 # If any %code blocks for QUALIFIER (or unqualified %code blocks if
779 # QUALIFIER is empty) are defined, expand IF-TRUE, else expand IF-FALSE.
780 # Also, record Bison's usage of QUALIFIER (if specified) by defining
781 # b4_percent_code_bison_qualifiers(QUALIFIER).
782 m4_define([b4_percent_code_ifdef],
783 [m4_ifdef([b4_percent_code(]$1[)],
784 [m4_ifval([$1], [m4_define([b4_percent_code_bison_qualifiers(]$1[)])])$2],
785 [$3])])
786
787
788 ## ------------------ ##
789 ## Common variables. ##
790 ## ------------------ ##
791
792 # Default values for %define.
793 # ---------------------------
794 # If the api.token.prefix, it is empty.
795 m4_percent_define_default([[api.token.prefix]], [[]])
796
797 # b4_parse_assert_if([IF-ASSERTIONS-ARE-USED], [IF-NOT])
798 # b4_parse_trace_if([IF-DEBUG-TRACES-ARE-ENABLED], [IF-NOT])
799 # b4_lex_symbol_if([IF-YYLEX-RETURNS-A-COMPLETE-SYMBOL], [IF-NOT])
800 # b4_variant_if([IF-VARIANT-ARE-USED], [IF-NOT])
801 # ----------------------------------------------
802 b4_percent_define_if_define([lex_symbol])
803 b4_percent_define_if_define([locations]) # Whether locations are tracked.
804 b4_percent_define_if_define([parse.assert])
805 b4_percent_define_if_define([parse.trace])
806 b4_percent_define_if_define([variant])
807
808
809 # b4_error_verbose_if([IF-ERRORS-ARE-VERBOSE], [IF-NOT])
810 # ------------------------------------------------------
811 # Map %define parse.error "(simple|verbose)" to b4_error_verbose_if and
812 # b4_error_verbose_flag.
813 b4_percent_define_default([[parse.error]], [[simple]])
814 b4_percent_define_check_values([[[[parse.error]],
815 [[simple]], [[verbose]]]])
816 m4_define([b4_error_verbose_flag],
817 [m4_case(b4_percent_define_get([[parse.error]]),
818 [simple], [[0]],
819 [verbose], [[1]])])
820 b4_define_flag_if([error_verbose])
821
822 # yytoken_table is needed to support verbose errors.
823 b4_error_verbose_if([m4_define([b4_token_table_flag], [1])])
824
825
826 ## ----------------------------------------------------------- ##
827 ## After processing the skeletons, check that all the user's ##
828 ## %define variables and %code qualifiers were used by Bison. ##
829 ## ----------------------------------------------------------- ##
830
831 m4_define([b4_check_user_names_wrap],
832 [m4_ifdef([b4_percent_]$1[_user_]$2[s],
833 [b4_check_user_names([[%]$1 $2],
834 [b4_percent_]$1[_user_]$2[s],
835 [[b4_percent_]$1[_bison_]$2[s]])])])
836
837 m4_wrap_lifo([
838 b4_check_user_names_wrap([[define]], [[variable]])
839 b4_check_user_names_wrap([[code]], [[qualifier]])
840 ])
841
842
843 ## ---------------- ##
844 ## Default values. ##
845 ## ---------------- ##
846
847 # m4_define_default([b4_lex_param], []) dnl breaks other skeletons
848 m4_define_default([b4_pre_prologue], [])
849 m4_define_default([b4_post_prologue], [])
850 m4_define_default([b4_epilogue], [])
851 m4_define_default([b4_parse_param], [])
852
853 # The initial column and line.
854 m4_define_default([b4_location_initial_column], [1])
855 m4_define_default([b4_location_initial_line], [1])
856
857 # Sanity checks.
858 b4_percent_define_ifdef([api.prefix],
859 [m4_ifdef([b4_prefix],
860 [b4_complain_at(b4_percent_define_get_loc([api.prefix]),
861 [['%s' and '%s' cannot be used together]],
862 [%name-prefix],
863 [%define api.prefix])])])