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