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_],
62 [$2[]m4_bpatsubst(m4_expand([$1]), [
67 # b4_comment(TEXT, [PREFIX])
68 # --------------------------
69 # Put TEXT in comment. Prefix all the output lines with PREFIX.
70 m4_define([b4_comment],
71 [b4_comment_([$1], [$2/* ], [$2 ], [ */])])
76 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
77 # b4_pull_flag if they use the values of the %define variables api.pure or
79 m4_define([b4_identification],
80 [[/* Identify Bison output. */
84 #define YYBISON_VERSION "]b4_version["
87 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
90 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
93 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
96 #define YYPULL ]b4_pull_flag])[
100 ## ---------------- ##
101 ## Default values. ##
102 ## ---------------- ##
104 # b4_api_prefix, b4_api_PREFIX
105 # ----------------------------
106 # Corresponds to %define api.prefix
107 b4_percent_define_default([[api.prefix]], [[yy]])
108 m4_define([b4_api_prefix],
109 [b4_percent_define_get([[api.prefix]])])
110 m4_define([b4_api_PREFIX],
111 [m4_toupper(b4_api_prefix)])
116 # If the %name-prefix is not given, it is api.prefix.
117 m4_define_default([b4_prefix], [b4_api_prefix])
119 # If the %union is not named, its name is YYSTYPE.
120 m4_define_default([b4_union_name], [b4_api_PREFIX[]STYPE])
123 ## ------------------------ ##
124 ## Pure/impure interfaces. ##
125 ## ------------------------ ##
129 # All the yylex formal arguments.
130 # b4_lex_param arrives quoted twice, but we want to keep only one level.
131 m4_define([b4_lex_formals],
132 [b4_pure_if([[[[YYSTYPE *yylvalp]], [[&yylval]]][]dnl
133 b4_locations_if([, [[YYLTYPE *yyllocp], [&yylloc]]])])dnl
134 m4_ifdef([b4_lex_param], [, ]b4_lex_param)])
141 [b4_function_call([yylex], [int], b4_lex_formals)])
146 m4_define([b4_user_args],
147 [m4_ifset([b4_parse_param], [, b4_args(b4_parse_param)])])
152 # If defined, b4_parse_param arrives double quoted, but below we prefer
153 # it to be single quoted.
154 m4_define([b4_parse_param],
158 # b4_parse_param_for(DECL, FORMAL, BODY)
159 # ---------------------------------------
160 # Iterate over the user parameters, binding the declaration to DECL,
161 # the formal name to FORMAL, and evaluating the BODY.
162 m4_define([b4_parse_param_for],
163 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
164 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
165 m4_pushdef([$2], m4_shift($1_$2))dnl
171 # b4_parse_param_use([VAL], [LOC])
172 # --------------------------------
173 # `YYUSE' VAL, LOC if locations are enabled, and all the parse-params.
174 m4_define([b4_parse_param_use],
175 [m4_ifvaln([$1], [ YYUSE([$1]);])dnl
176 b4_locations_if([m4_ifvaln([$2], [ YYUSE ([$2]);])])dnl
177 b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal);
186 # b4_int_type(MIN, MAX)
187 # ---------------------
188 # Return the smallest int type able to handle numbers ranging from
189 # MIN to MAX (included).
190 m4_define([b4_int_type],
191 [m4_if(b4_ints_in($@, [0], [255]), [1], [unsigned char],
192 b4_ints_in($@, [-128], [127]), [1], [signed char],
194 b4_ints_in($@, [0], [65535]), [1], [unsigned short int],
195 b4_ints_in($@, [-32768], [32767]), [1], [short int],
197 m4_eval([0 <= $1]), [1], [unsigned int],
202 # b4_int_type_for(NAME)
203 # ---------------------
204 # Return the smallest int type able to handle numbers ranging from
205 # `NAME_min' to `NAME_max' (included).
206 m4_define([b4_int_type_for],
207 [b4_int_type($1_min, $1_max)])
210 # b4_table_value_equals(TABLE, VALUE, LITERAL)
211 # --------------------------------------------
212 # Without inducing a comparison warning from the compiler, check if the
213 # literal value LITERAL equals VALUE from table TABLE, which must have
214 # TABLE_min and TABLE_max defined.
215 m4_define([b4_table_value_equals],
216 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
217 || m4_indir([b4_]$1[_max]) < $3), [1],
219 [(!!(($2) == ($3)))])])
229 # Portability issues: define a YY_NULL appropriate for the current
230 # language (C, C++98, or C++11).
231 m4_define([b4_null_define],
233 # if defined __cplusplus && 201103L <= __cplusplus
234 # define YY_NULL nullptr
244 # Return a null pointer constant.
245 m4_define([b4_null], [YY_NULL])
247 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
248 # -------------------------------------------------------------
249 # Define "yy<TABLE-NAME>" which contents is CONTENT.
250 m4_define([b4_integral_parser_table_define],
251 [m4_ifvaln([$3], [b4_comment([$3], [ ])])dnl
252 static const b4_int_type_for([$2]) yy$1[[]] =
259 ## ------------------------- ##
260 ## Assigning token numbers. ##
261 ## ------------------------- ##
263 # b4_token_define(TOKEN-NUM)
264 # --------------------------
265 # Output the definition of this token as #define.
266 m4_define([b4_token_define],
267 [b4_token_format([#define %s %s], [$1])])
271 # Output the definition of the tokens.
272 m4_define([b4_token_defines],
273 [b4_any_token_visible_if([/* Tokens. */
275 ], b4_symbol_map([b4_token_define]))
279 # b4_token_enum(TOKEN-NUM)
280 # ------------------------
281 # Output the definition of this token as an enum.
282 m4_define([b4_token_enum],
283 [b4_token_format([%s = %s], [$1])])
288 # Output the definition of the tokens (if there are) as enums.
289 m4_define([b4_token_enums],
290 [b4_any_token_visible_if([[/* Token type. */
291 #ifndef ]b4_api_PREFIX[TOKENTYPE
292 # define ]b4_api_PREFIX[TOKENTYPE
293 enum ]b4_api_prefix[tokentype
297 b4_symbol_map([b4_token_enum]))[
303 # b4_token_enums_defines
304 # ----------------------
305 # Output the definition of the tokens (if there are any) as enums and,
306 # if POSIX Yacc is enabled, as #defines.
307 m4_define([b4_token_enums_defines],
308 [b4_token_enums[]b4_yacc_if([b4_token_defines])])
311 ## ----------------- ##
312 ## Semantic Values. ##
313 ## ----------------- ##
316 # b4_symbol_value(VAL, [TYPE])
317 # ----------------------------
318 # Given a semantic value VAL ($$, $1 etc.), extract its value of type
319 # TYPE if TYPE is given, otherwise just return VAL. The result can be
320 # used safetly, it is put in parens to avoid nasty precedence issues.
321 # TYPE is *not* put in braces, provide some if needed.
322 m4_define([b4_symbol_value],
323 [($1[]m4_ifval([$2], [.$2]))])
327 ## ---------------------- ##
328 ## Defining C functions. ##
329 ## ---------------------- ##
332 # b4_function_define(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
333 # -----------------------------------------------------------
334 # Declare the function NAME in C.
335 m4_define([b4_function_define],
337 $1 (b4_formals(m4_shift2($@)))[]dnl
341 # b4_formals([DECL1, NAME1], ...)
342 # -------------------------------
343 # The formal arguments of a C function definition.
344 m4_define([b4_formals],
345 [m4_if([$#], [0], [void],
347 [m4_map_sep([b4_formal], [, ], [$@])])])
349 m4_define([b4_formal],
354 ## ----------------------- ##
355 ## Declaring C functions. ##
356 ## ----------------------- ##
359 # b4_function_declare(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
360 # ------------------------------------------------------------
361 # Declare the function NAME.
362 m4_define([b4_function_declare],
363 [$2 $1 (b4_formals(m4_shift2($@)));[]dnl
369 ## --------------------- ##
370 ## Calling C functions. ##
371 ## --------------------- ##
374 # b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
375 # -----------------------------------------------------------
376 # Call the function NAME with arguments NAME1, NAME2 etc.
377 m4_define([b4_function_call],
378 [$1 (b4_args(m4_shift2($@)))[]dnl
382 # b4_args([DECL1, NAME1], ...)
383 # ----------------------------
384 # Output the arguments NAME1, NAME2...
386 [m4_map_sep([b4_arg], [, ], [$@])])
396 # b4_sync_start(LINE, FILE)
397 # -------------------------
398 m4_define([b4_sync_start], [[#]line $1 $2])
405 # b4_case(LABEL, STATEMENTS)
406 # --------------------------
410 b4_syncline([@oline@], [@ofile@])
414 # b4_predicate_case(LABEL, CONDITIONS)
415 # ------------------------------------
416 m4_define([b4_predicate_case],
419 b4_syncline([@oline@], [@ofile@])
423 # b4_yydestruct_define
424 # --------------------
425 # Define the "yydestruct" function.
426 m4_define_default([b4_yydestruct_define],
427 [[/*-----------------------------------------------.
428 | Release the memory associated to this symbol. |
429 `-----------------------------------------------*/
431 ]b4_function_define([yydestruct],
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]))[
439 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
442 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
446 ]b4_symbol_foreach([b4_symbol_destructor])dnl
454 # b4_yy_symbol_print_define
455 # -------------------------
456 # Define the "yy_symbol_print" function.
457 m4_define_default([b4_yy_symbol_print_define],
459 /*--------------------------------.
460 | Print this symbol on YYOUTPUT. |
461 `--------------------------------*/
463 ]b4_function_define([yy_symbol_value_print],
465 [[FILE *yyoutput], [yyoutput]],
466 [[int yytype], [yytype]],
467 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
468 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
469 m4_ifset([b4_parse_param], [, b4_parse_param]))[
471 FILE *yyo = yyoutput;
472 ]b4_parse_param_use([yyo], [yylocationp])dnl
475 dnl glr.c does not feature yytoknum.
476 m4_if(b4_skeleton, ["yacc.c"],
478 if (yytype < YYNTOKENS)
479 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
484 ]b4_symbol_foreach([b4_symbol_printer])dnl
491 /*--------------------------------.
492 | Print this symbol on YYOUTPUT. |
493 `--------------------------------*/
495 ]b4_function_define([yy_symbol_print],
497 [[FILE *yyoutput], [yyoutput]],
498 [[int yytype], [yytype]],
499 [[YYSTYPE const * const yyvaluep], [yyvaluep]][]dnl
500 b4_locations_if([, [[YYLTYPE const * const yylocationp], [yylocationp]]])[]dnl
501 m4_ifset([b4_parse_param], [, b4_parse_param]))[
503 if (yytype < YYNTOKENS)
504 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
506 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
508 ]b4_locations_if([ YY_LOCATION_PRINT (yyoutput, *yylocationp);
509 YYFPRINTF (yyoutput, ": ");
511 [ yy_symbol_value_print (yyoutput, yytype, yyvaluep]dnl
512 b4_locations_if([, yylocationp])[]b4_user_args[);
513 YYFPRINTF (yyoutput, ")");
521 # b4_value_type_define
522 # --------------------
523 m4_define([b4_value_type_define],
525 #if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
526 ]m4_ifdef([b4_stype],
527 [[typedef union ]b4_union_name[ ]b4_api_PREFIX[STYPE;
528 union ]b4_union_name[
532 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]],
533 [m4_if(b4_tag_seen_flag, 0,
534 [[typedef int ]b4_api_PREFIX[STYPE;
535 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1]])])[
536 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
541 # b4_location_type_define
542 # -----------------------
543 m4_define([b4_location_type_define],
544 [[/* Location type. */
545 #if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
546 typedef struct ]b4_api_PREFIX[LTYPE ]b4_api_PREFIX[LTYPE;
547 struct ]b4_api_PREFIX[LTYPE
554 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
555 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
560 # b4_declare_yylstype
561 # -------------------
562 # Declarations that might either go into the header (if --defines) or
563 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
564 m4_define([b4_declare_yylstype],
565 [b4_value_type_define[]b4_locations_if([
566 b4_location_type_define])
568 b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
569 ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
575 m4_define([b4_YYDEBUG_define],
576 [[/* Debug traces. */
577 ]m4_if(b4_api_prefix, [yy],
579 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
581 [[#ifndef ]b4_api_PREFIX[DEBUG
584 # define ]b4_api_PREFIX[DEBUG 1
586 # define ]b4_api_PREFIX[DEBUG 0
588 # else /* ! defined YYDEBUG */
589 # define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[
590 # endif /* ! defined YYDEBUG */
591 #endif /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl
596 m4_define([b4_declare_yydebug],
598 #if ]b4_api_PREFIX[DEBUG
599 extern int ]b4_prefix[debug;
603 # b4_yylloc_default_define
604 # ------------------------
605 # Define YYLLOC_DEFAULT.
606 m4_define([b4_yylloc_default_define],
607 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
608 If N is 0, then set CURRENT to the empty location which ends
609 the previous symbol: RHS[0] (always defined). */
611 #ifndef YYLLOC_DEFAULT
612 # define YYLLOC_DEFAULT(Current, Rhs, N) \
616 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
617 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
618 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
619 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
623 (Current).first_line = (Current).last_line = \
624 YYRHSLOC (Rhs, 0).last_line; \
625 (Current).first_column = (Current).last_column = \
626 YYRHSLOC (Rhs, 0).last_column; \
632 # b4_yy_location_print_define
633 # ---------------------------
634 # Define YY_LOCATION_PRINT.
635 m4_define([b4_yy_location_print_define],
637 /* YY_LOCATION_PRINT -- Print the location on the stream.
638 This macro was not mandated originally: define only if we know
639 we won't break user code: when these are the locations we know. */
641 #ifndef YY_LOCATION_PRINT
642 # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
644 /* Print *YYLOCP on YYO. Private, do not rely on its existence. */
646 __attribute__((__unused__))
647 ]b4_function_define([yy_location_print_],
649 [[FILE *yyo], [yyo]],
650 [[YYLTYPE const * const yylocp], [yylocp]])[
653 int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
654 if (0 <= yylocp->first_line)
656 res += fprintf (yyo, "%d", yylocp->first_line);
657 if (0 <= yylocp->first_column)
658 res += fprintf (yyo, ".%d", yylocp->first_column);
660 if (0 <= yylocp->last_line)
662 if (yylocp->first_line < yylocp->last_line)
664 res += fprintf (yyo, "-%d", yylocp->last_line);
666 res += fprintf (yyo, ".%d", end_col);
668 else if (0 <= end_col && yylocp->first_column < end_col)
669 res += fprintf (yyo, "-%d", end_col);
674 # define YY_LOCATION_PRINT(File, Loc) \
675 yy_location_print_ (File, &(Loc))
678 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
681 [[/* This macro is provided for backward compatibility. */
682 #ifndef YY_LOCATION_PRINT
683 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
689 # Expand to a possible default value for yylloc.
690 m4_define([b4_yyloc_default],
692 # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
694 m4_defn([b4_location_initial_line]),
695 m4_defn([b4_location_initial_column]),
696 m4_defn([b4_location_initial_line]),
697 m4_defn([b4_location_initial_column]))[ }