3 # C M4 Macros for Bison.
4 # Copyright (C) 2002, 2004-2010 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 3 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, see <http://www.gnu.org/licenses/>.
19 ## ---------------- ##
21 ## ---------------- ##
23 # b4_comment_(TEXT, OPEN, CONTINUE, END)
24 # -------------------------------------
25 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
26 # Avoid adding indentation to the first line, as the indentation comes
27 # from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
29 # Prefix all the output lines with PREFIX.
30 m4_define([b4_comment_], [$2[]m4_bpatsubst([$1], [
35 # b4_c_comment(TEXT, [PREFIX])
36 # ----------------------------
37 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
38 # Avoid adding indentation to the first line, as the indentation comes
39 # from "/*". That's why we don't patsubst([$1], [^\(.\)], [ \1]).
41 # Prefix all the output lines with PREFIX.
42 m4_define([b4_c_comment],
43 [b4_comment_([$1], [$2/* ], [$2 ], [$2 */])])
46 # b4_comment(TEXT, [PREFIX])
47 # --------------------------
48 # By default, C comments.
49 m4_define([b4_comment], [b4_c_comment($@)])
54 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
55 # b4_pull_flag if they use the values of the %define variables api.pure or
57 m4_define([b4_identification],
58 [[/* Identify Bison output. */
62 #define YYBISON_VERSION "]b4_version["
65 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
68 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
71 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
74 #define YYPULL ]b4_pull_flag])[
76 /* Using locations. */
77 #define YYLSP_NEEDED ]b4_locations_if([1], [0])[
81 ## ---------------- ##
83 ## ---------------- ##
85 # If the %union is not named, its name is YYSTYPE.
86 m4_define_default([b4_union_name], [YYSTYPE])
88 # If the %name-prefix is not given, it is yy.
89 m4_define_default([b4_prefix], [yy])
91 ## ------------------------ ##
92 ## Pure/impure interfaces. ##
93 ## ------------------------ ##
97 m4_define([b4_user_args],
98 [m4_ifset([b4_parse_param], [, b4_c_args(b4_parse_param)])])
103 # If defined, b4_parse_param arrives double quoted, but below we prefer
104 # it to be single quoted.
105 m4_define([b4_parse_param],
109 # b4_parse_param_for(DECL, FORMAL, BODY)
110 # ---------------------------------------
111 # Iterate over the user parameters, binding the declaration to DECL,
112 # the formal name to FORMAL, and evaluating the BODY.
113 m4_define([b4_parse_param_for],
114 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
115 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
116 m4_pushdef([$2], m4_shift($1_$2))dnl
124 # `YYUSE' all the parse-params.
125 m4_define([b4_parse_param_use],
126 [b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
135 # b4_int_type(MIN, MAX)
136 # ---------------------
137 # Return the smallest int type able to handle numbers ranging from
138 # MIN to MAX (included).
139 m4_define([b4_int_type],
140 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
141 b4_ints_in($@, [-128], [127]), [1], [signed char],
143 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
144 b4_ints_in($@, [-32768], [32767]), [1], [short int],
146 m4_eval([0 <= $1]), [1], [unsigned int],
151 # b4_int_type_for(NAME)
152 # ---------------------
153 # Return the smallest int type able to handle numbers ranging from
154 # `NAME_min' to `NAME_max' (included).
155 m4_define([b4_int_type_for],
156 [b4_int_type($1_min, $1_max)])
159 # b4_table_value_equals(TABLE, VALUE, LITERAL)
160 # --------------------------------------------
161 # Without inducing a comparison warning from the compiler, check if the
162 # literal value LITERAL equals VALUE from table TABLE, which must have
163 # TABLE_min and TABLE_max defined. YYID must be defined as an identity
164 # function that suppresses warnings about constant conditions.
165 m4_define([b4_table_value_equals],
166 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
167 || m4_indir([b4_]$1[_max]) < $3), [1],
169 [[((]$2[) == (]$3[))]])])
177 # Return a null pointer constant. NULL infringes on the user name
178 # space in C, so use 0 rather than NULL.
179 m4_define([b4_null], [0])
182 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
183 # -------------------------------------------------------------
184 # Define "yy<TABLE-NAME>" which contents is CONTENT.
185 m4_define([b4_integral_parser_table_define],
186 [m4_ifval([$3], [b4_c_comment([$3], [ ])
188 static const b4_int_type_for([$2]) yy$1[[]] =
195 ## ------------------------- ##
196 ## Assigning token numbers. ##
197 ## ------------------------- ##
199 # b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
200 # -----------------------------------------
201 # Output the definition of this token as #define.
202 m4_define([b4_token_define],
203 [#define b4_percent_define_get([api.tokens.prefix])$1 $2
207 # b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
208 # -------------------------------------------------------
209 # Output the definition of the tokens (if there are) as #defines.
210 m4_define([b4_token_defines],
211 [m4_if([$#$1], [1], [],
213 m4_map([b4_token_define], [$@])])
217 # b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
218 # ---------------------------------------
219 # Output the definition of this token as an enum.
220 m4_define([b4_token_enum],
221 [b4_percent_define_get([api.tokens.prefix])$1 = $2])
224 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
225 # -----------------------------------------------------
226 # Output the definition of the tokens (if there are) as enums.
227 m4_define([b4_token_enums],
228 [m4_if([$#$1], [1], [],
232 /* Put the tokens into the symbol table, so that GDB and other debuggers
235 m4_map_sep([ b4_token_enum], [,
243 # b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
244 # -------------------------------------------------------------
245 # Output the definition of the tokens (if there are any) as enums and, if POSIX
246 # Yacc is enabled, as #defines.
247 m4_define([b4_token_enums_defines],
248 [b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
252 ## ----------------- ##
253 ## Semantic Values. ##
254 ## ----------------- ##
257 # b4_symbol_value(VAL, [TYPE])
258 # ----------------------------
259 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
260 # TYPE if TYPE is given, otherwise just return VAL. The result can be
261 # used safetly, it is put in parens to avoid nasty precedence issues.
262 # TYPE is *not* put in braces, provide some if needed.
263 m4_define([b4_symbol_value],
264 [($1[]m4_ifval([$2], [.$2]))])
268 ## --------------------------------------------- ##
269 ## Defining C functions in both K&R and ANSI-C. ##
270 ## --------------------------------------------- ##
275 # A predicate useful in #if to determine whether C is ancient or modern.
277 # If __STDC__ is defined, the compiler is modern. IBM xlc 7.0 when run
278 # as 'cc' doesn't define __STDC__ (or __STDC_VERSION__) for pedantic
279 # reasons, but it defines __C99__FUNC__ so check that as well.
280 # Microsoft C normally doesn't define these macros, but it defines _MSC_VER.
281 # Consider a C++ compiler to be modern if it defines __cplusplus.
283 m4_define([b4_c_modern],
284 [[(defined __STDC__ || defined __C99__FUNC__ \
285 || defined __cplusplus || defined _MSC_VER)]])
287 # b4_c_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
288 # ----------------------------------------------------------
289 # Declare the function NAME.
290 m4_define([b4_c_function_def],
292 b4_c_ansi_function_def($@)
295 $1 (b4_c_knr_formal_names(m4_shift2($@)))
296 b4_c_knr_formal_decls(m4_shift2($@))
301 # b4_c_ansi_function_def(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
302 # ---------------------------------------------------------------
303 # Declare the function NAME in ANSI.
304 m4_define([b4_c_ansi_function_def],
306 $1 (b4_c_ansi_formals(m4_shift2($@)))[]dnl
310 # b4_c_ansi_formals([DECL1, NAME1], ...)
311 # --------------------------------------
312 # Output the arguments ANSI-C definition.
313 m4_define([b4_c_ansi_formals],
314 [m4_if([$#], [0], [void],
316 [m4_map_sep([b4_c_ansi_formal], [, ], [$@])])])
318 m4_define([b4_c_ansi_formal],
322 # b4_c_knr_formal_names([DECL1, NAME1], ...)
323 # ------------------------------------------
324 # Output the argument names.
325 m4_define([b4_c_knr_formal_names],
326 [m4_map_sep([b4_c_knr_formal_name], [, ], [$@])])
328 m4_define([b4_c_knr_formal_name],
332 # b4_c_knr_formal_decls([DECL1, NAME1], ...)
333 # ------------------------------------------
334 # Output the K&R argument declarations.
335 m4_define([b4_c_knr_formal_decls],
336 [m4_map_sep([b4_c_knr_formal_decl],
341 m4_define([b4_c_knr_formal_decl],
346 ## ------------------------------------------------------------ ##
347 ## Declaring (prototyping) C functions in both K&R and ANSI-C. ##
348 ## ------------------------------------------------------------ ##
351 # b4_c_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
352 # -----------------------------------------------------------
353 # Declare the function NAME.
354 m4_define([b4_c_function_decl],
356 b4_c_ansi_function_decl($@)
363 # b4_c_ansi_function_decl(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
364 # ----------------------------------------------------------------
365 # Declare the function NAME.
366 m4_define([b4_c_ansi_function_decl],
367 [$2 $1 (b4_c_ansi_formals(m4_shift2($@)));[]dnl
373 ## --------------------- ##
374 ## Calling C functions. ##
375 ## --------------------- ##
378 # b4_c_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
379 # -----------------------------------------------------------
380 # Call the function NAME with arguments NAME1, NAME2 etc.
381 m4_define([b4_c_function_call],
382 [$1 (b4_c_args(m4_shift2($@)))[]dnl
386 # b4_c_args([DECL1, NAME1], ...)
387 # ------------------------------
388 # Output the arguments NAME1, NAME2...
389 m4_define([b4_c_args],
390 [m4_map_sep([b4_c_arg], [, ], [$@])])
392 m4_define([b4_c_arg],
400 # b4_sync_start(LINE, FILE)
401 # -----------------------
402 m4_define([b4_sync_start], [[#]line $1 $2])
409 # b4_case(LABEL, STATEMENTS)
410 # --------------------------
414 b4_syncline([@oline@], [@ofile@])
418 # b4_yydestruct_generate(FUNCTION-DECLARATOR)
419 # -------------------------------------------
420 # Generate the "yydestruct" function, which declaration is issued using
421 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
422 # or "b4_c_function_def" for K&R.
423 m4_define_default([b4_yydestruct_generate],
424 [[/*-----------------------------------------------.
425 | Release the memory associated to this symbol. |
426 `-----------------------------------------------*/
431 [[const char *yymsg], [yymsg]],
432 [[int yytype], [yytype]],
433 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
434 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
435 m4_ifset([b4_parse_param], [, b4_parse_param]))[
438 ]b4_locations_if([ YYUSE (yylocationp);
440 b4_parse_param_use[]dnl
444 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
448 ]b4_symbol_foreach([b4_symbol_destructor])dnl
456 # b4_yy_symbol_print_generate(FUNCTION-DECLARATOR)
457 # ------------------------------------------------
458 # Generate the "yy_symbol_print" function, which declaration is issued using
459 # FUNCTION-DECLARATOR, which may be "b4_c_ansi_function_def" for ISO C
460 # or "b4_c_function_def" for K&R.
461 m4_define_default([b4_yy_symbol_print_generate],
463 /*--------------------------------.
464 | Print this symbol on YYOUTPUT. |
465 `--------------------------------*/
468 ]$1([yy_symbol_value_print],
470 [[FILE *yyoutput], [yyoutput]],
471 [[int yytype], [yytype]],
472 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
473 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
474 m4_ifset([b4_parse_param], [, b4_parse_param]))[
478 ]b4_locations_if([ YYUSE (yylocationp);
480 b4_parse_param_use[]dnl
482 if (yytype < YYNTOKENS)
483 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
489 ]b4_symbol_foreach([b4_symbol_printer])dnl
496 /*--------------------------------.
497 | Print this symbol on YYOUTPUT. |
498 `--------------------------------*/
500 ]$1([yy_symbol_print],
502 [[FILE *yyoutput], [yyoutput]],
503 [[int yytype], [yytype]],
504 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
505 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
506 m4_ifset([b4_parse_param], [, b4_parse_param]))[
508 if (yytype < YYNTOKENS)
509 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
511 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
513 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
514 YYFPRINTF (yyoutput, ": ");
516 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
517 b4_locations_if([, yylocationp])[]b4_user_args[);
518 YYFPRINTF (yyoutput, ")");