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/>.
20 m4_include(b4_pkgdatadir/[c-like.m4])
24 # Convert STRING into a valid C macro name.
26 [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
31 # A valid C macro name to use as a CPP header guard for FILE.
32 m4_define([b4_cpp_guard],
33 [[YY_]b4_tocpp(m4_defn([b4_prefix])/[$1])[_INCLUDED]])
36 # b4_cpp_guard_open(FILE)
37 # b4_cpp_guard_close(FILE)
38 # ------------------------
39 # If FILE does not expand to nothing, open/close CPP inclusion guards for FILE.
40 m4_define([b4_cpp_guard_open],
41 [m4_ifval(m4_quote($1),
42 [#ifndef b4_cpp_guard([$1])
43 # define b4_cpp_guard([$1])])])
45 m4_define([b4_cpp_guard_close],
46 [m4_ifval(m4_quote($1),
47 [#endif b4_comment([!b4_cpp_guard([$1])])])])
50 ## ---------------- ##
52 ## ---------------- ##
54 # b4_comment_(TEXT, OPEN, CONTINUE, END)
55 # --------------------------------------
56 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
57 # Avoid adding indentation to the first line, as the indentation comes
58 # from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
60 # Prefix all the output lines with PREFIX.
61 m4_define([b4_comment_], [$2[]m4_bpatsubst([$1], [
66 # b4_comment(TEXT, [PREFIX])
67 # --------------------------
68 # Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
69 # Avoid adding indentation to the first line, as the indentation comes
70 # from "/*". That's why we don't patsubst([$1], [^\(.\)], [ \1]).
72 # Prefix all the output lines with PREFIX.
73 m4_define([b4_comment],
74 [b4_comment_([$1], [$2/* ], [$2 ], [$2 */])])
79 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
80 # b4_pull_flag if they use the values of the %define variables api.pure or
82 m4_define([b4_identification],
83 [[/* Identify Bison output. */
87 #define YYBISON_VERSION "]b4_version["
90 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
93 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
96 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
99 #define YYPULL ]b4_pull_flag])[
103 ## ---------------- ##
104 ## Default values. ##
105 ## ---------------- ##
107 # b4_api_prefix, b4_api_PREFIX
108 # ----------------------------
109 # Corresponds to %define api.prefix
110 b4_percent_define_default([[api.prefix]], [[yy]])
111 m4_define([b4_api_prefix],
112 [b4_percent_define_get([[api.prefix]])])
113 m4_define([b4_api_PREFIX],
114 [m4_toupper(b4_api_prefix)])
119 # If the %name-prefix is not given, it is api.prefix.
120 m4_define_default([b4_prefix], [b4_api_prefix])
122 # If the %union is not named, its name is YYSTYPE.
123 m4_define_default([b4_union_name], [b4_api_PREFIX[]STYPE])
126 ## ------------------------ ##
127 ## Pure/impure interfaces. ##
128 ## ------------------------ ##
132 m4_define([b4_user_args],
133 [m4_ifset([b4_parse_param], [, b4_args(b4_parse_param)])])
138 # If defined, b4_parse_param arrives double quoted, but below we prefer
139 # it to be single quoted.
140 m4_define([b4_parse_param],
144 # b4_parse_param_for(DECL, FORMAL, BODY)
145 # ---------------------------------------
146 # Iterate over the user parameters, binding the declaration to DECL,
147 # the formal name to FORMAL, and evaluating the BODY.
148 m4_define([b4_parse_param_for],
149 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
150 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
151 m4_pushdef([$2], m4_shift($1_$2))dnl
157 # b4_parse_param_use([VAL], [LOC])
158 # --------------------------------
159 # `YYUSE' VAL, LOC if locations are enabled, and all the parse-params.
160 m4_define([b4_parse_param_use],
161 [m4_ifvaln([$1], [ YYUSE([$1]);])dnl
162 b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl
163 b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
172 # b4_int_type(MIN, MAX)
173 # ---------------------
174 # Return the smallest int type able to handle numbers ranging from
175 # MIN to MAX (included).
176 m4_define([b4_int_type],
177 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
178 b4_ints_in($@, [-128], [127]), [1], [signed char],
180 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
181 b4_ints_in($@, [-32768], [32767]), [1], [short int],
183 m4_eval([0 <= $1]), [1], [unsigned int],
188 # b4_int_type_for(NAME)
189 # ---------------------
190 # Return the smallest int type able to handle numbers ranging from
191 # `NAME_min' to `NAME_max' (included).
192 m4_define([b4_int_type_for],
193 [b4_int_type($1_min, $1_max)])
196 # b4_table_value_equals(TABLE, VALUE, LITERAL)
197 # --------------------------------------------
198 # Without inducing a comparison warning from the compiler, check if the
199 # literal value LITERAL equals VALUE from table TABLE, which must have
200 # TABLE_min and TABLE_max defined.
201 m4_define([b4_table_value_equals],
202 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
203 || m4_indir([b4_]$1[_max]) < $3), [1],
205 [(!!(($2) == ($3)))])])
215 # Portability issues: define a YY_NULL appropriate for the current
216 # language (C, C++98, or C++11).
217 m4_define([b4_null_define],
219 # if defined __cplusplus && 201103L <= __cplusplus
220 # define YY_NULL nullptr
230 # Return a null pointer constant.
231 m4_define([b4_null], [YY_NULL])
233 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
234 # -------------------------------------------------------------
235 # Define "yy<TABLE-NAME>" which contents is CONTENT.
236 m4_define([b4_integral_parser_table_define],
237 [m4_ifvaln([$3], [b4_comment([$3], [ ])])dnl
238 static const b4_int_type_for([$2]) yy$1[[]] =
245 ## ------------------------- ##
246 ## Assigning token numbers. ##
247 ## ------------------------- ##
249 # b4_token_define(TOKEN-NUM)
250 # --------------------------
251 # Output the definition of this token as #define.
252 m4_define([b4_token_define],
253 [b4_token_format([#define %s %s], [$1])])
257 # Output the definition of the tokens.
258 m4_define([b4_token_defines],
259 [b4_any_token_visible_if([/* Tokens. */
261 ], b4_symbol_map([b4_token_define]))
265 # b4_token_enum(TOKEN-NUM)
266 # ------------------------
267 # Output the definition of this token as an enum.
268 m4_define([b4_token_enum],
269 [b4_token_format([%s = %s], [$1])])
274 # Output the definition of the tokens (if there are) as enums.
275 m4_define([b4_token_enums],
276 [b4_any_token_visible_if([[/* Tokens. */
277 #ifndef ]b4_api_PREFIX[TOKENTYPE
278 # define ]b4_api_PREFIX[TOKENTYPE
279 /* Put the tokens into the symbol table, so that GDB and other debuggers
281 enum ]b4_api_prefix[tokentype
285 b4_symbol_map([b4_token_enum]))[
291 # b4_token_enums_defines
292 # ----------------------
293 # Output the definition of the tokens (if there are any) as enums and,
294 # if POSIX Yacc is enabled, as #defines.
295 m4_define([b4_token_enums_defines],
296 [b4_token_enums[]b4_yacc_if([b4_token_defines])])
299 ## ----------------- ##
300 ## Semantic Values. ##
301 ## ----------------- ##
304 # b4_symbol_value(VAL, [TYPE])
305 # ----------------------------
306 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
307 # TYPE if TYPE is given, otherwise just return VAL. The result can be
308 # used safetly, it is put in parens to avoid nasty precedence issues.
309 # TYPE is *not* put in braces, provide some if needed.
310 m4_define([b4_symbol_value],
311 [($1[]m4_ifval([$2], [.$2]))])
315 ## ---------------------- ##
316 ## Defining C functions. ##
317 ## ---------------------- ##
320 # b4_function_define(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
321 # -----------------------------------------------------------
322 # Declare the function NAME in C.
323 m4_define([b4_function_define],
325 $1 (b4_formals(m4_shift2($@)))[]dnl
329 # b4_formals([DECL1, NAME1], ...)
330 # -------------------------------
331 # The formal arguments of a C function definition.
332 m4_define([b4_formals],
333 [m4_if([$#], [0], [void],
335 [m4_map_sep([b4_formal], [, ], [$@])])])
337 m4_define([b4_formal],
342 ## ----------------------- ##
343 ## Declaring C functions. ##
344 ## ----------------------- ##
347 # b4_function_declare(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
348 # ------------------------------------------------------------
349 # Declare the function NAME.
350 m4_define([b4_function_declare],
351 [$2 $1 (b4_formals(m4_shift2($@)));[]dnl
357 ## --------------------- ##
358 ## Calling C functions. ##
359 ## --------------------- ##
362 # b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
363 # -----------------------------------------------------------
364 # Call the function NAME with arguments NAME1, NAME2 etc.
365 m4_define([b4_function_call],
366 [$1 (b4_args(m4_shift2($@)))[]dnl
370 # b4_args([DECL1, NAME1], ...)
371 # ----------------------------
372 # Output the arguments NAME1, NAME2...
374 [m4_map_sep([b4_arg], [, ], [$@])])
384 # b4_sync_start(LINE, FILE)
385 # -------------------------
386 m4_define([b4_sync_start], [[#]line $1 $2])
393 # b4_case(LABEL, STATEMENTS)
394 # --------------------------
398 b4_syncline([@oline@], [@ofile@])
402 # b4_predicate_case(LABEL, CONDITIONS)
403 # ------------------------------------
404 m4_define([b4_predicate_case],
407 b4_syncline([@oline@], [@ofile@])
411 # b4_yydestruct_define
412 # --------------------
413 # Define the "yydestruct" function.
414 m4_define_default([b4_yydestruct_define],
415 [[/*-----------------------------------------------.
416 | Release the memory associated to this symbol. |
417 `-----------------------------------------------*/
419 ]b4_function_define([yydestruct],
421 [[const char *yymsg], [yymsg]],
422 [[int yytype], [yytype]],
423 [[YYSTYPE *yyvaluep], [yyvaluep]][]dnl
424 b4_locations_if( [, [[YYLTYPE *yylocationp], [yylocationp]]])[]dnl
425 m4_ifset([b4_parse_param], [, b4_parse_param]))[
427 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
430 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
434 ]b4_symbol_foreach([b4_symbol_destructor])dnl
442 # b4_yy_symbol_print_define
443 # -------------------------
444 # Define the "yy_symbol_print" function.
445 m4_define_default([b4_yy_symbol_print_define],
447 /*--------------------------------.
448 | Print this symbol on YYOUTPUT. |
449 `--------------------------------*/
451 ]b4_function_define([yy_symbol_value_print],
453 [[FILE *yyoutput], [yyoutput]],
454 [[int yytype], [yytype]],
455 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
456 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
457 m4_ifset([b4_parse_param], [, b4_parse_param]))[
459 FILE *yyo = yyoutput;
460 ]b4_parse_param_use([yyo], [yylocationp])dnl
463 dnl glr.c does not feature yytoknum.
464 m4_if(b4_skeleton, ["yacc.c"],
466 if (yytype < YYNTOKENS)
467 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
472 ]b4_symbol_foreach([b4_symbol_printer])dnl
479 /*--------------------------------.
480 | Print this symbol on YYOUTPUT. |
481 `--------------------------------*/
483 ]b4_function_define([yy_symbol_print],
485 [[FILE *yyoutput], [yyoutput]],
486 [[int yytype], [yytype]],
487 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
488 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
489 m4_ifset([b4_parse_param], [, b4_parse_param]))[
491 if (yytype < YYNTOKENS)
492 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
494 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
496 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
497 YYFPRINTF (yyoutput, ": ");
499 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
500 b4_locations_if([, yylocationp])[]b4_user_args[);
501 YYFPRINTF (yyoutput, ")");
509 # b4_declare_yylstype
510 # -------------------
511 # Declarations that might either go into the header (if --defines) or
512 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
513 m4_define([b4_declare_yylstype],
514 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
515 ]m4_ifdef([b4_stype],
516 [[typedef union ]b4_union_name[
519 } ]b4_api_PREFIX[STYPE;
520 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]],
521 [m4_if(b4_tag_seen_flag, 0,
522 [[typedef int ]b4_api_PREFIX[STYPE;
523 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]])])[
524 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
525 #endif]b4_locations_if([[
527 #if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
528 typedef struct ]b4_api_PREFIX[LTYPE
534 } ]b4_api_PREFIX[LTYPE;
535 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
536 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
539 b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
540 ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
545 m4_define([b4_YYDEBUG_define],
546 [[/* Enabling traces. */
547 ]m4_if(b4_api_prefix, [yy],
549 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
551 [[#ifndef ]b4_api_PREFIX[DEBUG
554 # define ]b4_api_PREFIX[DEBUG 1
556 # define ]b4_api_PREFIX[DEBUG 0
558 # else /* ! defined YYDEBUG */
559 # define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[
560 # endif /* ! defined YYDEBUG */
561 #endif /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl
566 m4_define([b4_declare_yydebug],
568 #if ]b4_api_PREFIX[DEBUG
569 extern int ]b4_prefix[debug;
573 # b4_yylloc_default_define
574 # ------------------------
575 # Define YYLLOC_DEFAULT.
576 m4_define([b4_yylloc_default_define],
577 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
578 If N is 0, then set CURRENT to the empty location which ends
579 the previous symbol: RHS[0] (always defined). */
581 #ifndef YYLLOC_DEFAULT
582 # define YYLLOC_DEFAULT(Current, Rhs, N) \
586 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
587 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
588 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
589 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
593 (Current).first_line = (Current).last_line = \
594 YYRHSLOC (Rhs, 0).last_line; \
595 (Current).first_column = (Current).last_column = \
596 YYRHSLOC (Rhs, 0).last_column; \