1 m4_divert(-1) -*- Autoconf -*-
3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
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 2 of the License, or
9 # (at your option) any later version.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 ## ---------------- ##
24 ## ---------------- ##
26 # b4_copyright(TITLE, YEARS)
27 # --------------------------
28 m4_define([b4_copyright],
29 [/* A Bison parser, made by GNU Bison b4_version. */
33 m4_text_wrap([Copyright (C) $2 Free Software Foundation, Inc.], [ ])
35 This program is free software; you can redistribute it and/or modify
36 it under the terms of the GNU General Public License as published by
37 the Free Software Foundation; either version 2, or (at your option)
40 This program is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
45 You should have received a copy of the GNU General Public License
46 along with this program; if not, write to the Free Software
47 Foundation, Inc., 51 Franklin Street, Fifth Floor,
48 Boston, MA 02110-1301, USA. */
50 /* As a special exception, you may create a larger work that contains
51 part or all of the Bison parser skeleton and distribute that work
52 under terms of your choice, so long as that work isn't itself a
53 parser generator using the skeleton or a modified version thereof
54 as a parser skeleton. Alternatively, if you modify or redistribute
55 the parser skeleton itself, you may (at your option) remove this
56 special exception, which will cause the skeleton and the resulting
57 Bison output files to be licensed under the GNU General Public
58 License without this special exception.
60 This special exception was added by the Free Software Foundation in
61 version 2.2 of Bison. */])
66 m4_define([b4_identification],
67 [/* Identify Bison output. */
71 [#]define YYBISON_VERSION "b4_version"
74 [#]define YYSKELETON_NAME b4_skeleton
77 [#]define YYPURE b4_pure_flag
79 /* Using locations. */
80 [#]define YYLSP_NEEDED b4_locations_flag
84 ## ---------------- ##
86 ## ---------------- ##
88 m4_define_default([b4_epilogue], [])
90 # If the %union is not named, its name is YYSTYPE.
91 m4_define_default([b4_union_name], [YYSTYPE])
93 # The initial column and line.
94 m4_define_default([b4_location_initial_column], [1])
95 m4_define_default([b4_location_initial_line], [1])
98 ## ------------------------ ##
99 ## Pure/impure interfaces. ##
100 ## ------------------------ ##
104 m4_define([b4_user_args],
105 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
110 # If defined, b4_parse_param arrives double quoted, but below we prefer
111 # it to be single quoted.
112 m4_define_default([b4_parse_param])
113 m4_define([b4_parse_param],
117 # b4_parse_param_for(DECL, FORMAL, BODY)
118 # ---------------------------------------
119 # Iterate over the user parameters, binding the declaration to DECL,
120 # the formal name to FORMAL, and evaluating the BODY.
121 m4_define([b4_parse_param_for],
122 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
123 [m4_pushdef([$1], m4_fst($1_$2))dnl
124 m4_pushdef([$2], m4_shift($1_$2))dnl
132 # `YYUSE' all the parse-params.
133 m4_define([b4_parse_param_use],
134 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
143 # b4_ints_in(INT1, INT2, LOW, HIGH)
144 # ---------------------------------
145 # Return 1 iff both INT1 and INT2 are in [LOW, HIGH], 0 otherwise.
146 m4_define([b4_ints_in],
147 [m4_eval([$3 <= $1 && $1 <= $4 && $3 <= $2 && $2 <= $4])])
150 # b4_int_type(MIN, MAX)
151 # ---------------------
152 # Return the smallest int type able to handle numbers ranging from
153 # MIN to MAX (included).
154 m4_define([b4_int_type],
155 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
156 b4_ints_in($@, [-128], [127]), [1], [signed char],
158 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
159 b4_ints_in($@, [-32768], [32767]), [1], [short int],
161 m4_eval([0 <= $1]), [1], [unsigned int],
166 # b4_int_type_for(NAME)
167 # ---------------------
168 # Return the smallest int type able to handle numbers ranging from
169 # `NAME_min' to `NAME_max' (included).
170 m4_define([b4_int_type_for],
171 [b4_int_type($1_min, $1_max)])
180 # Return a null pointer constant. NULL infringes on the user name
181 # space in C, so use 0 rather than NULL.
182 m4_define([b4_null], [0])
185 ## ------------------ ##
186 ## Decoding options. ##
187 ## ------------------ ##
189 # b4_flag_if(FLAG, IF-TRUE, IF-FALSE)
190 # -----------------------------------
191 # Run IF-TRUE if b4_FLAG_flag is 1, IF-FALSE if FLAG is 0, otherwise fail.
192 m4_define([b4_flag_if],
196 [m4_fatal([invalid $1 value: ]$1)])])
199 # b4_define_flag_if(FLAG)
200 # -----------------------
201 # Define "b4_FLAG_if(IF-TRUE, IF-FALSE)" that depends on the
202 # value of the Boolean FLAG.
203 m4_define([b4_define_flag_if],
204 [_b4_define_flag_if($[1], $[2], [$1])])
206 # _b4_define_flag_if($1, $2, FLAG)
207 # --------------------------------
208 # This macro works around the impossibility to define macros
209 # inside macros, because issuing `[$1]' is not possible in M4 :(.
210 # This sucks hard, GNU M4 should really provide M5 like $$1.
211 m4_define([_b4_define_flag_if],
212 [m4_if([$1$2], $[1]$[2], [],
213 [m4_fatal([$0: Invalid arguments: $@])])dnl
214 m4_define([b4_$3_if],
215 [b4_flag_if([$3], [$1], [$2])])])
218 # b4_FLAG_if(IF-TRUE, IF-FALSE)
219 # -----------------------------
220 # Expand IF-TRUE, if FLAG is true, IF-FALSE otherwise.
221 b4_define_flag_if([defines]) # Whether headers are requested.
222 b4_define_flag_if([error_verbose]) # Whether error are verbose.
223 b4_define_flag_if([locations]) # Whether locations are tracked.
224 b4_define_flag_if([pure]) # Whether the interface is pure.
225 b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
229 ## ------------------------- ##
230 ## Assigning token numbers. ##
231 ## ------------------------- ##
233 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
234 # -----------------------------------------
235 # Output the definition of this token as #define.
236 m4_define([b4_token_define],
241 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
242 # -------------------------------------------------------
243 # Output the definition of the tokens (if there are) as #defines.
244 m4_define([b4_token_defines],
245 [m4_if([$@], [[]], [],
247 m4_map([b4_token_define], [$@])])
251 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
252 # ---------------------------------------
253 # Output the definition of this token as an enum.
254 m4_define([b4_token_enum],
258 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
259 # -----------------------------------------------------
260 # Output the definition of the tokens (if there are) as enums.
261 m4_define([b4_token_enums],
262 [m4_if([$@], [[]], [],
266 /* Put the tokens into the symbol table, so that GDB and other debuggers
269 m4_map_sep([ b4_token_enum], [,
277 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
278 # -------------------------------------------------------------
279 # Output the definition of the tokens (if there are any) as enums and, if POSIX
280 # Yacc is enabled, as #defines.
281 m4_define([b4_token_enums_defines],
282 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
287 ## --------------------------------------------- ##
288 ## Defining C functions in both K&R and ANSI-C. ##
289 ## --------------------------------------------- ##
294 # A predicate useful in #if to determine whether C is ancient or modern.
296 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
297 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
298 # reasons, but it defines __C99__FUNC__ so check that as well.
299 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
300 # Consider a C++ compiler to be modern if it defines __cplusplus.
302 m4_define([b4_c_modern],
303 [[(defined __STDC__ || defined __C99__FUNC__ \
304 || defined __cplusplus || defined _MSC_VER)]])
306 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
307 # ----------------------------------------------------------
308 # Declare the function NAME.
309 m4_define([b4_c_function_def],
311 b4_c_ansi_function_def($@)
314 $1 (b4_c_knr_formal_names(m4_shiftn(2, $@)))
315 b4_c_knr_formal_decls(m4_shiftn(2, $@))
320 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
321 # ---------------------------------------------------------------
322 # Declare the function NAME in ANSI.
323 m4_define([b4_c_ansi_function_def],
325 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)))[]dnl
329 # b4_c_ansi_formals([DECL1, NAME1], ...)
330 # --------------------------------------
331 # Output the arguments ANSI-C definition.
332 m4_define([b4_c_ansi_formals],
336 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
338 m4_define([b4_c_ansi_formal],
342 # b4_c_knr_formal_names([DECL1, NAME1], ...)
343 # ------------------------------------------
344 # Output the argument names.
345 m4_define([b4_c_knr_formal_names],
346 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
348 m4_define([b4_c_knr_formal_name],
352 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
353 # ------------------------------------------
354 # Output the K&R argument declarations.
355 m4_define([b4_c_knr_formal_decls],
356 [m4_map_sep([b4_c_knr_formal_decl],
361 m4_define([b4_c_knr_formal_decl],
366 ## ------------------------------------------------------------ ##
367 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
368 ## ------------------------------------------------------------ ##
371 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
372 # -----------------------------------------------------------
373 # Declare the function NAME.
374 m4_define([b4_c_function_decl],
375 [#if defined __STDC__ || defined __cplusplus
376 b4_c_ansi_function_decl($@)
383 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
384 # ----------------------------------------------------------------
385 # Declare the function NAME.
386 m4_define([b4_c_ansi_function_decl],
387 [$2 $1 (b4_c_ansi_formals(m4_shiftn(2, $@)));[]dnl
393 ## --------------------- ##
394 ## Calling C functions. ##
395 ## --------------------- ##
398 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
399 # -----------------------------------------------------------
400 # Call the function NAME with arguments NAME1, NAME2 etc.
401 m4_define([b4_c_function_call],
402 [$1 (b4_c_args(m4_shiftn(2, $@)))[]dnl
406 # b4_c_args([DECL1, NAME1], ...)
407 # ------------------------------
408 # Output the arguments NAME1, NAME2...
409 m4_define([b4_c_args],
410 [m4_map_sep([b4_c_arg], [, ], [$@])])
412 m4_define([b4_c_arg],
422 # Similar to POSIX basename; the differences don't matter here.
423 # Beware that NAME is not evaluated.
424 m4_define([b4_basename],
425 [m4_bpatsubst([$1], [^.*/\([^/]+\)/*$], [\1])])
428 # b4_syncline(LINE, FILE)
429 # -----------------------
430 m4_define([b4_syncline],
431 [b4_flag_if([synclines],
432 [/* Line __line__ of b4_basename(m4_quote(__file__)). */
436 # b4_user_code(USER-CODE)
437 # -----------------------
438 # Emit code from the user, ending it with synclines.
439 m4_define([b4_user_code],
441 b4_syncline([@oline@], [@ofile@])])
444 # b4_define_user_code(MACRO)
445 # --------------------------
446 # From b4_MACRO, build b4_user_MACRO that includes the synclines.
447 m4_define([b4_define_user_code],
448 [m4_define([b4_user_$1],
449 [b4_user_code([b4_$1])])])
453 # b4_user_initial_action
454 # b4_user_post_prologue
455 # b4_user_start_header
457 # ----------------------
458 # Macros that issue user code, ending with synclines.
459 b4_define_user_code([actions])
460 b4_define_user_code([initial_action])
461 b4_define_user_code([post_prologue])
462 b4_define_user_code([start_header])
463 b4_define_user_code([stype])
470 # b4_case(LABEL, STATEMENTS)
471 # --------------------------
477 # b4_symbol_actions(FILENAME, LINENO,
478 # SYMBOL-TAG, SYMBOL-NUM,
479 # SYMBOL-ACTION, SYMBOL-TYPENAME)
480 # -------------------------------------------------
481 m4_define([b4_symbol_actions],
482 [m4_pushdef([b4_dollar_dollar],
483 [m4_ifval([$6], [(yyvaluep->$6)], [(*yyvaluep)])])dnl
484 m4_pushdef([b4_at_dollar], [(*yylocationp)])dnl
486 b4_syncline([$2], [$1])
488 b4_syncline([@oline@], [@ofile@])
490 m4_popdef([b4_at_dollar])dnl
491 m4_popdef([b4_dollar_dollar])dnl
495 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
496 # -------------------------------------------
497 # Generate the "yydestruct" function, which declaration is issued using
498 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
499 # or "b4_c_function_def" for K&R.
500 m4_define_default([b4_yydestruct_generate],
501 [[/*-----------------------------------------------.
502 | Release the memory associated to this symbol. |
503 `-----------------------------------------------*/
508 [[const char *yymsg], [yymsg]],
509 [[int yytype], [yytype]],
510 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
511 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
512 m4_ifset([b4_parse_param], [, b4_parse_param]))[
515 ]b4_locations_if([ YYUSE (yylocationp);
517 b4_parse_param_use[]dnl
521 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
525 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_destructors]))[
533 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
534 # ------------------------------------------------
535 # Generate the "yy_symbol_print" function, which declaration is issued using
536 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
537 # or "b4_c_function_def" for K&R.
538 m4_define_default([b4_yy_symbol_print_generate],
540 /*--------------------------------.
541 | Print this symbol on YYOUTPUT. |
542 `--------------------------------*/
545 ]$1([yy_symbol_value_print],
547 [[FILE *yyoutput], [yyoutput]],
548 [[int yytype], [yytype]],
549 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
550 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
551 m4_ifset([b4_parse_param], [, b4_parse_param]))[
555 ]b4_locations_if([ YYUSE (yylocationp);
557 b4_parse_param_use[]dnl
559 if (yytype < YYNTOKENS)
560 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
566 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
573 /*--------------------------------.
574 | Print this symbol on YYOUTPUT. |
575 `--------------------------------*/
577 ]$1([yy_symbol_print],
579 [[FILE *yyoutput], [yyoutput]],
580 [[int yytype], [yytype]],
581 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
582 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
583 m4_ifset([b4_parse_param], [, b4_parse_param]))[
585 if (yytype < YYNTOKENS)
586 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
588 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
590 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
591 YYFPRINTF (yyoutput, ": ");
593 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
594 b4_locations_if([, yylocationp])[]b4_user_args[);
595 YYFPRINTF (yyoutput, ")");