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