3 # C M4 Macros for Bison.
5 # Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
6 # Software Foundation, Inc.
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ## ---------------- ##
23 ## ---------------- ##
25 # b4_comment_(TEXT, OPEN, CONTINUE, END)
26 # -------------------------------------
27 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
28 # Avoid adding indentation to the first line, as the indentation comes
29 # from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
31 # Prefix all the output lines with PREFIX.
32 m4_define([b4_comment_], [$2[]m4_bpatsubst([$1], [
37 # b4_c_comment(TEXT, [PREFIX])
38 # ----------------------------
39 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
40 # Avoid adding indentation to the first line, as the indentation comes
41 # from "/*". That's why we don't patsubst([$1], [^\(.\)], [ \1]).
43 # Prefix all the output lines with PREFIX.
44 m4_define([b4_c_comment],
45 [b4_comment_([$1], [$2/* ], [$2 ], [$2 */])])
48 # b4_comment(TEXT, [PREFIX])
49 # --------------------------
50 # By default, C comments.
51 m4_define([b4_comment], [b4_c_comment($@)])
56 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
57 # b4_pull_flag if they use the values of the %define variables api.pure or
59 m4_define([b4_identification],
60 [[/* Identify Bison output. */
64 #define YYBISON_VERSION "]b4_version["
67 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
70 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
73 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
76 #define YYPULL ]b4_pull_flag])[
78 /* Using locations. */
79 #define YYLSP_NEEDED ]b4_locations_if([1], [0])[
83 ## ---------------- ##
85 ## ---------------- ##
87 # If the %union is not named, its name is YYSTYPE.
88 m4_define_default([b4_union_name], [YYSTYPE])
90 # If the %name-prefix is not given, it is yy.
91 m4_define_default([b4_prefix], [yy])
93 ## ------------------------ ##
94 ## Pure/impure interfaces. ##
95 ## ------------------------ ##
99 m4_define([b4_user_args],
100 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
105 # If defined, b4_parse_param arrives double quoted, but below we prefer
106 # it to be single quoted.
107 m4_define([b4_parse_param],
111 # b4_parse_param_for(DECL, FORMAL, BODY)
112 # ---------------------------------------
113 # Iterate over the user parameters, binding the declaration to DECL,
114 # the formal name to FORMAL, and evaluating the BODY.
115 m4_define([b4_parse_param_for],
116 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
117 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
118 m4_pushdef([$2], m4_shift($1_$2))dnl
126 # `YYUSE' all the parse-params.
127 m4_define([b4_parse_param_use],
128 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
137 # b4_int_type(MIN, MAX)
138 # ---------------------
139 # Return the smallest int type able to handle numbers ranging from
140 # MIN to MAX (included).
141 m4_define([b4_int_type],
142 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
143 b4_ints_in($@, [-128], [127]), [1], [signed char],
145 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
146 b4_ints_in($@, [-32768], [32767]), [1], [short int],
148 m4_eval([0 <= $1]), [1], [unsigned int],
153 # b4_int_type_for(NAME)
154 # ---------------------
155 # Return the smallest int type able to handle numbers ranging from
156 # `NAME_min' to `NAME_max' (included).
157 m4_define([b4_int_type_for],
158 [b4_int_type($1_min, $1_max)])
161 # b4_table_value_equals(TABLE, VALUE, LITERAL)
162 # --------------------------------------------
163 # Without inducing a comparison warning from the compiler, check if the
164 # literal value LITERAL equals VALUE from table TABLE, which must have
165 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
166 # function that suppresses warnings about constant conditions.
167 m4_define([b4_table_value_equals],
168 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
169 || m4_indir([b4_]$1[_max]) < $3), [1],
171 [[((]$2[) == (]$3[))]])])
179 # Return a null pointer constant. NULL infringes on the user name
180 # space in C, so use 0 rather than NULL.
181 m4_define([b4_null], [0])
184 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
185 # -------------------------------------------------------------
186 # Define "yy<TABLE-NAME>" which contents is CONTENT.
187 m4_define([b4_integral_parser_table_define],
188 [m4_ifval([$3], [b4_c_comment([$3], [ ])
190 static const b4_int_type_for([$2]) yy$1[[]] =
197 ## ------------------------- ##
198 ## Assigning token numbers. ##
199 ## ------------------------- ##
201 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
202 # -----------------------------------------
203 # Output the definition of this token as #define.
204 m4_define([b4_token_define],
205 [#define b4_percent_define_get([api.tokens.prefix])$1 $2
209 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
210 # -------------------------------------------------------
211 # Output the definition of the tokens (if there are) as #defines.
212 m4_define([b4_token_defines],
213 [m4_if([$#$1], [1], [],
215 m4_map([b4_token_define], [$@])])
219 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
220 # ---------------------------------------
221 # Output the definition of this token as an enum.
222 m4_define([b4_token_enum],
223 [b4_percent_define_get([api.tokens.prefix])$1 = $2])
226 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
227 # -----------------------------------------------------
228 # Output the definition of the tokens (if there are) as enums.
229 m4_define([b4_token_enums],
230 [m4_if([$#$1], [1], [],
234 /* Put the tokens into the symbol table, so that GDB and other debuggers
237 m4_map_sep([ b4_token_enum], [,
245 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
246 # -------------------------------------------------------------
247 # Output the definition of the tokens (if there are any) as enums and, if POSIX
248 # Yacc is enabled, as #defines.
249 m4_define([b4_token_enums_defines],
250 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
254 ## ----------------- ##
255 ## Semantic Values. ##
256 ## ----------------- ##
259 # b4_symbol_value(VAL, [TYPE])
260 # ----------------------------
261 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
262 # TYPE if TYPE is given, otherwise just return VAL. The result can be
263 # used safetly, it is put in parens to avoid nasty precedence issues.
264 # TYPE is *not* put in braces, provide some if needed.
265 m4_define([b4_symbol_value],
266 [($1[]m4_ifval([$2], [.$2]))])
270 ## --------------------------------------------- ##
271 ## Defining C functions in both K&R and ANSI-C. ##
272 ## --------------------------------------------- ##
277 # A predicate useful in #if to determine whether C is ancient or modern.
279 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
280 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
281 # reasons, but it defines __C99__FUNC__ so check that as well.
282 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
283 # Consider a C++ compiler to be modern if it defines __cplusplus.
285 m4_define([b4_c_modern],
286 [[(defined __STDC__ || defined __C99__FUNC__ \
287 || defined __cplusplus || defined _MSC_VER)]])
289 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
290 # ----------------------------------------------------------
291 # Declare the function NAME.
292 m4_define([b4_c_function_def],
294 b4_c_ansi_function_def($@)
297 $1 (b4_c_knr_formal_names(m4_shift2($@)))
298 b4_c_knr_formal_decls(m4_shift2($@))
303 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
304 # ---------------------------------------------------------------
305 # Declare the function NAME in ANSI.
306 m4_define([b4_c_ansi_function_def],
308 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
312 # b4_c_ansi_formals([DECL1, NAME1], ...)
313 # --------------------------------------
314 # Output the arguments ANSI-C definition.
315 m4_define([b4_c_ansi_formals],
316 [m4_if([$#], [0], [void],
318 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
320 m4_define([b4_c_ansi_formal],
324 # b4_c_knr_formal_names([DECL1, NAME1], ...)
325 # ------------------------------------------
326 # Output the argument names.
327 m4_define([b4_c_knr_formal_names],
328 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
330 m4_define([b4_c_knr_formal_name],
334 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
335 # ------------------------------------------
336 # Output the K&R argument declarations.
337 m4_define([b4_c_knr_formal_decls],
338 [m4_map_sep([b4_c_knr_formal_decl],
343 m4_define([b4_c_knr_formal_decl],
348 ## ------------------------------------------------------------ ##
349 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
350 ## ------------------------------------------------------------ ##
353 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
354 # -----------------------------------------------------------
355 # Declare the function NAME.
356 m4_define([b4_c_function_decl],
358 b4_c_ansi_function_decl($@)
365 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
366 # ----------------------------------------------------------------
367 # Declare the function NAME.
368 m4_define([b4_c_ansi_function_decl],
369 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
375 ## --------------------- ##
376 ## Calling C functions. ##
377 ## --------------------- ##
380 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
381 # -----------------------------------------------------------
382 # Call the function NAME with arguments NAME1, NAME2 etc.
383 m4_define([b4_c_function_call],
384 [$1 (b4_c_args(m4_shift2($@)))[]dnl
388 # b4_c_args([DECL1, NAME1], ...)
389 # ------------------------------
390 # Output the arguments NAME1, NAME2...
391 m4_define([b4_c_args],
392 [m4_map_sep([b4_c_arg], [, ], [$@])])
394 m4_define([b4_c_arg],
402 # b4_sync_start(LINE, FILE)
403 # -----------------------
404 m4_define([b4_sync_start], [[#]line $1 $2])
411 # b4_case(LABEL, STATEMENTS)
412 # --------------------------
416 b4_syncline([@oline@], [@ofile@])
420 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
421 # -------------------------------------------
422 # Generate the "yydestruct" function, which declaration is issued using
423 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
424 # or "b4_c_function_def" for K&R.
425 m4_define_default([b4_yydestruct_generate],
426 [[/*-----------------------------------------------.
427 | Release the memory associated to this symbol. |
428 `-----------------------------------------------*/
433 [[const char *yymsg], [yymsg]],
434 [[int yytype], [yytype]],
435 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
436 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
437 m4_ifset([b4_parse_param], [, b4_parse_param]))[
440 ]b4_locations_if([ YYUSE (yylocationp);
442 b4_parse_param_use[]dnl
446 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
450 ]b4_symbol_foreach([b4_symbol_destructor])dnl
458 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
459 # ------------------------------------------------
460 # Generate the "yy_symbol_print" function, which declaration is issued using
461 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
462 # or "b4_c_function_def" for K&R.
463 m4_define_default([b4_yy_symbol_print_generate],
465 /*--------------------------------.
466 | Print this symbol on YYOUTPUT. |
467 `--------------------------------*/
470 ]$1([yy_symbol_value_print],
472 [[FILE *yyoutput], [yyoutput]],
473 [[int yytype], [yytype]],
474 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
475 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
476 m4_ifset([b4_parse_param], [, b4_parse_param]))[
480 ]b4_locations_if([ YYUSE (yylocationp);
482 b4_parse_param_use[]dnl
484 if (yytype < YYNTOKENS)
485 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
491 ]b4_symbol_foreach([b4_symbol_printer])dnl
498 /*--------------------------------.
499 | Print this symbol on YYOUTPUT. |
500 `--------------------------------*/
502 ]$1([yy_symbol_print],
504 [[FILE *yyoutput], [yyoutput]],
505 [[int yytype], [yytype]],
506 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
507 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
508 m4_ifset([b4_parse_param], [, b4_parse_param]))[
510 if (yytype < YYNTOKENS)
511 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
513 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
515 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
516 YYFPRINTF (yyoutput, ": ");
518 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
519 b4_locations_if([, yylocationp])[]b4_user_args[);
520 YYFPRINTF (yyoutput, ")");