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])[
106 ## ---------------- ##
107 ## Default values. ##
108 ## ---------------- ##
110 # b4_api_prefix, b4_api_PREFIX
111 # ----------------------------
112 # Corresponds to %define api.prefix
113 b4_percent_define_default([[api.prefix]], [[yy]])
114 m4_define([b4_api_prefix],
115 [b4_percent_define_get([[api.prefix]])])
116 m4_define([b4_api_PREFIX],
117 [m4_toupper(b4_api_prefix)])
122 # If the %name-prefix is not given, it is api.prefix.
123 m4_define_default([b4_prefix], [b4_api_prefix])
125 # If the %union is not named, its name is YYSTYPE.
126 m4_define_default([b4_union_name], [b4_api_PREFIX[]STYPE])
129 ## ------------------------ ##
130 ## Pure/impure interfaces. ##
131 ## ------------------------ ##
135 m4_define([b4_user_args],
136 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
141 # If defined, b4_parse_param arrives double quoted, but below we prefer
142 # it to be single quoted.
143 m4_define([b4_parse_param],
147 # b4_parse_param_for(DECL, FORMAL, BODY)
148 # ---------------------------------------
149 # Iterate over the user parameters, binding the declaration to DECL,
150 # the formal name to FORMAL, and evaluating the BODY.
151 m4_define([b4_parse_param_for],
152 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
153 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
154 m4_pushdef([$2], m4_shift($1_$2))dnl
160 # b4_parse_param_use([VAL], [LOC])
161 # --------------------------------
162 # `YYUSE' VAL, LOC if locations are enabled, and all the parse-params.
163 m4_define([b4_parse_param_use],
164 [m4_ifvaln([$1], [ YYUSE([$1]);])dnl
165 b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl
166 b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
175 # b4_int_type(MIN, MAX)
176 # ---------------------
177 # Return the smallest int type able to handle numbers ranging from
178 # MIN to MAX (included).
179 m4_define([b4_int_type],
180 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
181 b4_ints_in($@, [-128], [127]), [1], [signed char],
183 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
184 b4_ints_in($@, [-32768], [32767]), [1], [short int],
186 m4_eval([0 <= $1]), [1], [unsigned int],
191 # b4_int_type_for(NAME)
192 # ---------------------
193 # Return the smallest int type able to handle numbers ranging from
194 # `NAME_min' to `NAME_max' (included).
195 m4_define([b4_int_type_for],
196 [b4_int_type($1_min, $1_max)])
199 # b4_table_value_equals(TABLE, VALUE, LITERAL)
200 # --------------------------------------------
201 # Without inducing a comparison warning from the compiler, check if the
202 # literal value LITERAL equals VALUE from table TABLE, which must have
203 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
204 # function that suppresses warnings about constant conditions.
205 m4_define([b4_table_value_equals],
206 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
207 || m4_indir([b4_]$1[_max]) < $3), [1],
209 [[((]$2[) == (]$3[))]])])
219 # Portability issues: define a YY_NULL appropriate for the current
220 # language (C, C++98, or C++11).
221 m4_define([b4_null_define],
223 # if defined __cplusplus && 201103L <= __cplusplus
224 # define YY_NULL nullptr
234 # Return a null pointer constant.
235 m4_define([b4_null], [YY_NULL])
237 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
238 # -------------------------------------------------------------
239 # Define "yy<TABLE-NAME>" which contents is CONTENT.
240 m4_define([b4_integral_parser_table_define],
241 [m4_ifvaln([$3], [b4_c_comment([$3], [ ])])dnl
242 static const b4_int_type_for([$2]) yy$1[[]] =
249 ## ------------------------- ##
250 ## Assigning token numbers. ##
251 ## ------------------------- ##
253 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
254 # -----------------------------------------
255 # Output the definition of this token as #define.
256 m4_define([b4_token_define],
257 [#define b4_percent_define_get([api.tokens.prefix])$1 $2
261 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
262 # -------------------------------------------------------
263 # Output the definition of the tokens (if there are) as #defines.
264 m4_define([b4_token_defines],
265 [m4_if([$#$1], [1], [],
267 m4_map([b4_token_define], [$@])])
271 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
272 # ---------------------------------------
273 # Output the definition of this token as an enum.
274 m4_define([b4_token_enum],
275 [b4_percent_define_get([api.tokens.prefix])$1 = $2])
278 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
279 # -----------------------------------------------------
280 # Output the definition of the tokens (if there are) as enums.
281 m4_define([b4_token_enums],
282 [m4_if([$#$1], [1], [],
284 #ifndef ]b4_api_PREFIX[TOKENTYPE
285 # define ]b4_api_PREFIX[TOKENTYPE
286 /* Put the tokens into the symbol table, so that GDB and other debuggers
288 enum ]b4_api_prefix[tokentype {
289 ]m4_map_sep([ b4_token_enum], [,
297 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
298 # -------------------------------------------------------------
299 # Output the definition of the tokens (if there are any) as enums and, if POSIX
300 # Yacc is enabled, as #defines.
301 m4_define([b4_token_enums_defines],
302 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
306 ## ----------------- ##
307 ## Semantic Values. ##
308 ## ----------------- ##
311 # b4_symbol_value(VAL, [TYPE])
312 # ----------------------------
313 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
314 # TYPE if TYPE is given, otherwise just return VAL. The result can be
315 # used safetly, it is put in parens to avoid nasty precedence issues.
316 # TYPE is *not* put in braces, provide some if needed.
317 m4_define([b4_symbol_value],
318 [($1[]m4_ifval([$2], [.$2]))])
322 ## --------------------------------------------- ##
323 ## Defining C functions in both K&R and ANSI-C. ##
324 ## --------------------------------------------- ##
329 # A predicate useful in #if to determine whether C is ancient or modern.
331 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
332 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
333 # reasons, but it defines __C99__FUNC__ so check that as well.
334 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
335 # Consider a C++ compiler to be modern if it defines __cplusplus.
337 m4_define([b4_c_modern],
338 [[(defined __STDC__ || defined __C99__FUNC__ \
339 || defined __cplusplus || defined _MSC_VER)]])
341 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
342 # ----------------------------------------------------------
343 # Declare the function NAME.
344 m4_define([b4_c_function_def],
346 b4_c_ansi_function_def($@)
349 $1 (b4_c_knr_formal_names(m4_shift2($@)))
350 b4_c_knr_formal_decls(m4_shift2($@))
355 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
356 # ---------------------------------------------------------------
357 # Declare the function NAME in ANSI.
358 m4_define([b4_c_ansi_function_def],
360 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
364 # b4_c_ansi_formals([DECL1, NAME1], ...)
365 # --------------------------------------
366 # Output the arguments ANSI-C definition.
367 m4_define([b4_c_ansi_formals],
368 [m4_if([$#], [0], [void],
370 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
372 m4_define([b4_c_ansi_formal],
376 # b4_c_knr_formal_names([DECL1, NAME1], ...)
377 # ------------------------------------------
378 # Output the argument names.
379 m4_define([b4_c_knr_formal_names],
380 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
382 m4_define([b4_c_knr_formal_name],
386 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
387 # ------------------------------------------
388 # Output the K&R argument declarations.
389 m4_define([b4_c_knr_formal_decls],
390 [m4_map_sep([b4_c_knr_formal_decl],
395 m4_define([b4_c_knr_formal_decl],
400 ## ------------------------------------------------------------ ##
401 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
402 ## ------------------------------------------------------------ ##
405 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
406 # -----------------------------------------------------------
407 # Declare the function NAME.
408 m4_define([b4_c_function_decl],
410 b4_c_ansi_function_decl($@)
417 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
418 # ----------------------------------------------------------------
419 # Declare the function NAME.
420 m4_define([b4_c_ansi_function_decl],
421 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
427 ## --------------------- ##
428 ## Calling C functions. ##
429 ## --------------------- ##
432 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
433 # -----------------------------------------------------------
434 # Call the function NAME with arguments NAME1, NAME2 etc.
435 m4_define([b4_c_function_call],
436 [$1 (b4_c_args(m4_shift2($@)))[]dnl
440 # b4_c_args([DECL1, NAME1], ...)
441 # ------------------------------
442 # Output the arguments NAME1, NAME2...
443 m4_define([b4_c_args],
444 [m4_map_sep([b4_c_arg], [, ], [$@])])
446 m4_define([b4_c_arg],
454 # b4_sync_start(LINE, FILE)
455 # -----------------------
456 m4_define([b4_sync_start], [[#]line $1 $2])
463 # b4_case(LABEL, STATEMENTS)
464 # --------------------------
468 b4_syncline([@oline@], [@ofile@])
472 # b4_predicate_case(LABEL, CONDITIONS)
473 # ------------------------------------
474 m4_define([b4_predicate_case],
477 b4_syncline([@oline@], [@ofile@])
481 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
482 # -------------------------------------------
483 # Generate the "yydestruct" function, which declaration is issued using
484 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
485 # or "b4_c_function_def" for K&R.
486 m4_define_default([b4_yydestruct_generate],
487 [[/*-----------------------------------------------.
488 | Release the memory associated to this symbol. |
489 `-----------------------------------------------*/
494 [[const char *yymsg], [yymsg]],
495 [[int yytype], [yytype]],
496 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
497 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
498 m4_ifset([b4_parse_param], [, b4_parse_param]))[
500 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
503 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
507 ]b4_symbol_foreach([b4_symbol_destructor])dnl
515 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
516 # ------------------------------------------------
517 # Generate the "yy_symbol_print" function, which declaration is issued using
518 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
519 # or "b4_c_function_def" for K&R.
520 m4_define_default([b4_yy_symbol_print_generate],
522 /*--------------------------------.
523 | Print this symbol on YYOUTPUT. |
524 `--------------------------------*/
527 ]$1([yy_symbol_value_print],
529 [[FILE *yyoutput], [yyoutput]],
530 [[int yytype], [yytype]],
531 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
532 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
533 m4_ifset([b4_parse_param], [, b4_parse_param]))[
535 FILE *yyo = yyoutput;
536 ]b4_parse_param_use([yyo], [yylocationp])dnl
539 dnl glr.c does not feature yytoknum.
540 m4_if(b4_skeleton, ["yacc.c"],
542 if (yytype < YYNTOKENS)
543 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
548 ]b4_symbol_foreach([b4_symbol_printer])dnl
555 /*--------------------------------.
556 | Print this symbol on YYOUTPUT. |
557 `--------------------------------*/
559 ]$1([yy_symbol_print],
561 [[FILE *yyoutput], [yyoutput]],
562 [[int yytype], [yytype]],
563 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
564 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
565 m4_ifset([b4_parse_param], [, b4_parse_param]))[
567 if (yytype < YYNTOKENS)
568 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
570 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
572 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
573 YYFPRINTF (yyoutput, ": ");
575 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
576 b4_locations_if([, yylocationp])[]b4_user_args[);
577 YYFPRINTF (yyoutput, ")");
585 # b4_declare_yylstype
586 # -------------------
587 # Declarations that might either go into the header (if --defines) or
588 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
589 m4_define([b4_declare_yylstype],
590 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
591 ]m4_ifdef([b4_stype],
592 [[typedef union ]b4_union_name[
595 } ]b4_api_PREFIX[STYPE;
596 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]],
597 [m4_if(b4_tag_seen_flag, 0,
598 [[typedef int ]b4_api_PREFIX[STYPE;
599 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]])])[
600 # define ]b4_api_prefix[stype ]b4_api_PREFIX[STYPE /* obsolescent; will be withdrawn */
601 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
602 #endif]b4_locations_if([[
604 #if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
605 typedef struct ]b4_api_PREFIX[LTYPE
611 } ]b4_api_PREFIX[LTYPE;
612 # define ]b4_api_prefix[ltype ]b4_api_PREFIX[LTYPE /* obsolescent; will be withdrawn */
613 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
614 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
617 b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
618 ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
623 m4_define([b4_declare_yydebug],
624 [[/* Enabling traces. */
626 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
629 extern int ]b4_prefix[debug;