3 # C M4 Macros for Bison.
5 # Copyright (C) 2002, 2004-2012 Free Software Foundation, Inc.
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.
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.
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/>.
23 # Convert STRING into a valid C macro name.
25 [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
30 # A valid C macro name to use as a CPP header guard for FILE.
31 m4_define([b4_cpp_guard],
32 [b4_tocpp(m4_defn([b4_prefix])/[$1])])
35 # b4_cpp_guard_open(FILE)
36 # b4_cpp_guard_close(FILE)
37 # ------------------------
38 # Open/close CPP inclusion guards for FILE.
39 m4_define([b4_cpp_guard_open],
40 [#ifndef b4_cpp_guard([$1])
41 # define b4_cpp_guard([$1])])
43 m4_define([b4_cpp_guard_close],
44 [#endif b4_comment([!b4_cpp_guard([$1])])])
47 ## ---------------- ##
49 ## ---------------- ##
51 # b4_comment_(TEXT, OPEN, CONTINUE, END)
52 # -------------------------------------
53 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
54 # Avoid adding indentation to the first line, as the indentation comes
55 # from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
57 # Prefix all the output lines with PREFIX.
58 m4_define([b4_comment_], [$2[]m4_bpatsubst([$1], [
63 # b4_c_comment(TEXT, [PREFIX])
64 # ----------------------------
65 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
66 # Avoid adding indentation to the first line, as the indentation comes
67 # from "/*". That's why we don't patsubst([$1], [^\(.\)], [ \1]).
69 # Prefix all the output lines with PREFIX.
70 m4_define([b4_c_comment],
71 [b4_comment_([$1], [$2/* ], [$2 ], [$2 */])])
74 # b4_comment(TEXT, [PREFIX])
75 # --------------------------
76 # By default, C comments.
77 m4_define([b4_comment], [b4_c_comment($@)])
82 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
83 # b4_pull_flag if they use the values of the %define variables api.pure or
85 m4_define([b4_identification],
86 [[/* Identify Bison output. */
90 #define YYBISON_VERSION "]b4_version["
93 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
96 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
99 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
102 #define YYPULL ]b4_pull_flag])[
104 /* Using locations. */
105 #define YYLSP_NEEDED ]b4_locations_if([1], [0])[
109 ## ---------------- ##
110 ## Default values. ##
111 ## ---------------- ##
113 # If the %union is not named, its name is YYSTYPE.
114 m4_define_default([b4_union_name], [YYSTYPE])
116 # If the %name-prefix is not given, it is yy.
117 m4_define_default([b4_prefix], [yy])
119 ## ------------------------ ##
120 ## Pure/impure interfaces. ##
121 ## ------------------------ ##
125 m4_define([b4_user_args],
126 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
131 # If defined, b4_parse_param arrives double quoted, but below we prefer
132 # it to be single quoted.
133 m4_define([b4_parse_param],
137 # b4_parse_param_for(DECL, FORMAL, BODY)
138 # ---------------------------------------
139 # Iterate over the user parameters, binding the declaration to DECL,
140 # the formal name to FORMAL, and evaluating the BODY.
141 m4_define([b4_parse_param_for],
142 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
143 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
144 m4_pushdef([$2], m4_shift($1_$2))dnl
150 # b4_parse_param_use([VAL], [LOC])
151 # --------------------------------
152 # `YYUSE' VAL, LOC if locations are enabled, and all the parse-params.
153 m4_define([b4_parse_param_use],
154 [m4_ifvaln([$1], [ YYUSE([$1]);])dnl
155 b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl
156 b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
165 # b4_int_type(MIN, MAX)
166 # ---------------------
167 # Return the smallest int type able to handle numbers ranging from
168 # MIN to MAX (included).
169 m4_define([b4_int_type],
170 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
171 b4_ints_in($@, [-128], [127]), [1], [signed char],
173 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
174 b4_ints_in($@, [-32768], [32767]), [1], [short int],
176 m4_eval([0 <= $1]), [1], [unsigned int],
181 # b4_int_type_for(NAME)
182 # ---------------------
183 # Return the smallest int type able to handle numbers ranging from
184 # `NAME_min' to `NAME_max' (included).
185 m4_define([b4_int_type_for],
186 [b4_int_type($1_min, $1_max)])
189 # b4_table_value_equals(TABLE, VALUE, LITERAL)
190 # --------------------------------------------
191 # Without inducing a comparison warning from the compiler, check if the
192 # literal value LITERAL equals VALUE from table TABLE, which must have
193 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
194 # function that suppresses warnings about constant conditions.
195 m4_define([b4_table_value_equals],
196 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
197 || m4_indir([b4_]$1[_max]) < $3), [1],
199 [[((]$2[) == (]$3[))]])])
209 # Portability issues: define a YY_NULL appropriate for the current
210 # language (C, C++98, or C++11).
211 m4_define([b4_null_define],
213 # if defined __cplusplus && 201103L <= __cplusplus
214 # define YY_NULL nullptr
224 # Return a null pointer constant.
225 m4_define([b4_null], [YY_NULL])
227 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
228 # -------------------------------------------------------------
229 # Define "yy<TABLE-NAME>" which contents is CONTENT.
230 m4_define([b4_integral_parser_table_define],
231 [m4_ifvaln([$3], [b4_c_comment([$3], [ ])])dnl
232 static const b4_int_type_for([$2]) yy$1[[]] =
239 ## ------------------------- ##
240 ## Assigning token numbers. ##
241 ## ------------------------- ##
243 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
244 # -----------------------------------------
245 # Output the definition of this token as #define.
246 m4_define([b4_token_define],
247 [#define b4_percent_define_get([api.tokens.prefix])$1 $2
251 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
252 # -------------------------------------------------------
253 # Output the definition of the tokens (if there are) as #defines.
254 m4_define([b4_token_defines],
255 [m4_if([$#$1], [1], [],
257 m4_map([b4_token_define], [$@])])
261 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
262 # ---------------------------------------
263 # Output the definition of this token as an enum.
264 m4_define([b4_token_enum],
265 [b4_percent_define_get([api.tokens.prefix])$1 = $2])
268 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
269 # -----------------------------------------------------
270 # Output the definition of the tokens (if there are) as enums.
271 m4_define([b4_token_enums],
272 [m4_if([$#$1], [1], [],
276 /* Put the tokens into the symbol table, so that GDB and other debuggers
279 m4_map_sep([ b4_token_enum], [,
287 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
288 # -------------------------------------------------------------
289 # Output the definition of the tokens (if there are any) as enums and, if POSIX
290 # Yacc is enabled, as #defines.
291 m4_define([b4_token_enums_defines],
292 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
296 ## ----------------- ##
297 ## Semantic Values. ##
298 ## ----------------- ##
301 # b4_symbol_value(VAL, [TYPE])
302 # ----------------------------
303 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
304 # TYPE if TYPE is given, otherwise just return VAL. The result can be
305 # used safetly, it is put in parens to avoid nasty precedence issues.
306 # TYPE is *not* put in braces, provide some if needed.
307 m4_define([b4_symbol_value],
308 [($1[]m4_ifval([$2], [.$2]))])
312 ## --------------------------------------------- ##
313 ## Defining C functions in both K&R and ANSI-C. ##
314 ## --------------------------------------------- ##
319 # A predicate useful in #if to determine whether C is ancient or modern.
321 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
322 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
323 # reasons, but it defines __C99__FUNC__ so check that as well.
324 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
325 # Consider a C++ compiler to be modern if it defines __cplusplus.
327 m4_define([b4_c_modern],
328 [[(defined __STDC__ || defined __C99__FUNC__ \
329 || defined __cplusplus || defined _MSC_VER)]])
331 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
332 # ----------------------------------------------------------
333 # Declare the function NAME.
334 m4_define([b4_c_function_def],
336 b4_c_ansi_function_def($@)
339 $1 (b4_c_knr_formal_names(m4_shift2($@)))
340 b4_c_knr_formal_decls(m4_shift2($@))
345 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
346 # ---------------------------------------------------------------
347 # Declare the function NAME in ANSI.
348 m4_define([b4_c_ansi_function_def],
350 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
354 # b4_c_ansi_formals([DECL1, NAME1], ...)
355 # --------------------------------------
356 # Output the arguments ANSI-C definition.
357 m4_define([b4_c_ansi_formals],
358 [m4_if([$#], [0], [void],
360 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
362 m4_define([b4_c_ansi_formal],
366 # b4_c_knr_formal_names([DECL1, NAME1], ...)
367 # ------------------------------------------
368 # Output the argument names.
369 m4_define([b4_c_knr_formal_names],
370 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
372 m4_define([b4_c_knr_formal_name],
376 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
377 # ------------------------------------------
378 # Output the K&R argument declarations.
379 m4_define([b4_c_knr_formal_decls],
380 [m4_map_sep([b4_c_knr_formal_decl],
385 m4_define([b4_c_knr_formal_decl],
390 ## ------------------------------------------------------------ ##
391 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
392 ## ------------------------------------------------------------ ##
395 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
396 # -----------------------------------------------------------
397 # Declare the function NAME.
398 m4_define([b4_c_function_decl],
400 b4_c_ansi_function_decl($@)
407 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
408 # ----------------------------------------------------------------
409 # Declare the function NAME.
410 m4_define([b4_c_ansi_function_decl],
411 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
417 ## --------------------- ##
418 ## Calling C functions. ##
419 ## --------------------- ##
422 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
423 # -----------------------------------------------------------
424 # Call the function NAME with arguments NAME1, NAME2 etc.
425 m4_define([b4_c_function_call],
426 [$1 (b4_c_args(m4_shift2($@)))[]dnl
430 # b4_c_args([DECL1, NAME1], ...)
431 # ------------------------------
432 # Output the arguments NAME1, NAME2...
433 m4_define([b4_c_args],
434 [m4_map_sep([b4_c_arg], [, ], [$@])])
436 m4_define([b4_c_arg],
444 # b4_sync_start(LINE, FILE)
445 # -----------------------
446 m4_define([b4_sync_start], [[#]line $1 $2])
453 # b4_case(LABEL, STATEMENTS)
454 # --------------------------
458 b4_syncline([@oline@], [@ofile@])
462 # b4_predicate_case(LABEL, CONDITIONS)
463 # ------------------------------------
464 m4_define([b4_predicate_case],
467 b4_syncline([@oline@], [@ofile@])
471 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
472 # -------------------------------------------
473 # Generate the "yydestruct" function, which declaration is issued using
474 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
475 # or "b4_c_function_def" for K&R.
476 m4_define_default([b4_yydestruct_generate],
477 [[/*-----------------------------------------------.
478 | Release the memory associated to this symbol. |
479 `-----------------------------------------------*/
484 [[const char *yymsg], [yymsg]],
485 [[int yytype], [yytype]],
486 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
487 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
488 m4_ifset([b4_parse_param], [, b4_parse_param]))[
490 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
493 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
497 ]b4_symbol_foreach([b4_symbol_destructor])dnl
505 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
506 # ------------------------------------------------
507 # Generate the "yy_symbol_print" function, which declaration is issued using
508 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
509 # or "b4_c_function_def" for K&R.
510 m4_define_default([b4_yy_symbol_print_generate],
512 /*--------------------------------.
513 | Print this symbol on YYOUTPUT. |
514 `--------------------------------*/
517 ]$1([yy_symbol_value_print],
519 [[FILE *yyoutput], [yyoutput]],
520 [[int yytype], [yytype]],
521 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
522 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
523 m4_ifset([b4_parse_param], [, b4_parse_param]))[
525 FILE *yyo = yyoutput;
526 ]b4_parse_param_use([yyo], [yylocationp])dnl
529 dnl glr.c does not feature yytoknum.
530 m4_if(b4_skeleton, ["yacc.c"],
532 if (yytype < YYNTOKENS)
533 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
538 ]b4_symbol_foreach([b4_symbol_printer])dnl
545 /*--------------------------------.
546 | Print this symbol on YYOUTPUT. |
547 `--------------------------------*/
549 ]$1([yy_symbol_print],
551 [[FILE *yyoutput], [yyoutput]],
552 [[int yytype], [yytype]],
553 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
554 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
555 m4_ifset([b4_parse_param], [, b4_parse_param]))[
557 if (yytype < YYNTOKENS)
558 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
560 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
562 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
563 YYFPRINTF (yyoutput, ": ");
565 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
566 b4_locations_if([, yylocationp])[]b4_user_args[);
567 YYFPRINTF (yyoutput, ")");