3 # C M4 Macros for Bison.
5 # Copyright (C) 2002, 2004-2015, 2018-2021 Free Software Foundation,
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 m4_include(b4_skeletonsdir/[c-like.m4])
25 # Convert STRING into a valid C macro name.
27 [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
32 # A valid C macro name to use as a CPP header guard for FILE.
33 m4_define([b4_cpp_guard],
34 [[YY_]b4_tocpp(m4_defn([b4_prefix])/[$1])[_INCLUDED]])
37 # b4_cpp_guard_open(FILE)
38 # b4_cpp_guard_close(FILE)
39 # ------------------------
40 # If FILE does not expand to nothing, open/close CPP inclusion guards for FILE.
41 m4_define([b4_cpp_guard_open],
42 [m4_ifval(m4_quote($1),
43 [#ifndef b4_cpp_guard([$1])
44 # define b4_cpp_guard([$1])])])
46 m4_define([b4_cpp_guard_close],
47 [m4_ifval(m4_quote($1),
48 [#endif b4_comment([!b4_cpp_guard([$1])])])])
51 ## ---------------- ##
53 ## ---------------- ##
57 # Depends on individual skeletons to define b4_pure_flag, b4_push_flag, or
58 # b4_pull_flag if they use the values of the %define variables api.pure or
60 m4_define([b4_identification],
61 [[/* Identify Bison output, and Bison version. */
62 #define YYBISON ]b4_version[
64 /* Bison version string. */
65 #define YYBISON_VERSION "]b4_version_string["
68 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
71 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
74 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
77 #define YYPULL ]b4_pull_flag])[
81 ## ---------------- ##
83 ## ---------------- ##
85 # b4_api_prefix, b4_api_PREFIX
86 # ----------------------------
87 # Corresponds to %define api.prefix
88 b4_percent_define_default([[api.prefix]], [[yy]])
89 m4_define([b4_api_prefix],
90 [b4_percent_define_get([[api.prefix]])])
91 m4_define([b4_api_PREFIX],
92 [m4_toupper(b4_api_prefix)])
97 # If the %name-prefix is not given, it is api.prefix.
98 m4_define_default([b4_prefix], [b4_api_prefix])
100 # If the %union is not named, its name is YYSTYPE.
101 b4_percent_define_default([[api.value.union.name]],
102 [b4_api_PREFIX[][STYPE]])
104 b4_percent_define_default([[api.symbol.prefix]], [[YYSYMBOL_]])
106 ## ------------------------ ##
107 ## Pure/impure interfaces. ##
108 ## ------------------------ ##
112 # All the yylex formal arguments.
113 # b4_lex_param arrives quoted twice, but we want to keep only one level.
114 m4_define([b4_lex_formals],
115 [b4_pure_if([[[b4_api_PREFIX[STYPE *yylvalp]], [[&yylval]]][]dnl
116 b4_locations_if([, [b4_api_PREFIX[LTYPE *yyllocp], [&yylloc]]])])dnl
117 m4_ifdef([b4_lex_param], [, ]b4_lex_param)])
124 [b4_function_call([yylex], [int], b4_lex_formals)])
129 m4_define([b4_user_args],
130 [m4_ifset([b4_parse_param], [, b4_user_args_no_comma])])
132 # b4_user_args_no_comma
133 # ---------------------
134 m4_define([b4_user_args_no_comma],
135 [m4_ifset([b4_parse_param], [b4_args(b4_parse_param)])])
140 # The possible parse-params formal arguments preceded by a comma.
141 m4_define([b4_user_formals],
142 [m4_ifset([b4_parse_param], [, b4_formals(b4_parse_param)])])
147 # If defined, b4_parse_param arrives double quoted, but below we prefer
148 # it to be single quoted.
149 m4_define([b4_parse_param],
153 # b4_parse_param_for(DECL, FORMAL, BODY)
154 # ---------------------------------------
155 # Iterate over the user parameters, binding the declaration to DECL,
156 # the formal name to FORMAL, and evaluating the BODY.
157 m4_define([b4_parse_param_for],
158 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
159 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
160 m4_pushdef([$2], m4_shift($1_$2))dnl
169 # Pacify the compiler about some maybe unused value.
173 # b4_parse_param_use([VAL], [LOC])
174 # --------------------------------
175 # 'YY_USE' VAL, LOC if locations are enabled, and all the parse-params.
176 m4_define([b4_parse_param_use],
177 [m4_ifvaln([$1], [ b4_use([$1]);])dnl
178 b4_locations_if([m4_ifvaln([$2], [ b4_use([$2]);])])dnl
179 b4_parse_param_for([Decl], [Formal], [ b4_use(Formal);
188 # b4_int_type(MIN, MAX)
189 # ---------------------
190 # Return a narrow int type able to handle integers ranging from MIN
191 # to MAX (included) in portable C code. Assume MIN and MAX fall in
193 m4_define([b4_int_type],
194 [m4_if(b4_ints_in($@, [-127], [127]), [1], [signed char],
195 b4_ints_in($@, [0], [255]), [1], [unsigned char],
197 b4_ints_in($@, [-32767], [32767]), [1], [short],
198 b4_ints_in($@, [0], [65535]), [1], [unsigned short],
202 # b4_c99_int_type(MIN, MAX)
203 # -------------------------
204 # Like b4_int_type, but for C99.
205 # b4_c99_int_type_define replaces b4_int_type with this.
206 m4_define([b4_c99_int_type],
207 [m4_if(b4_ints_in($@, [-127], [127]), [1], [yytype_int8],
208 b4_ints_in($@, [0], [255]), [1], [yytype_uint8],
210 b4_ints_in($@, [-32767], [32767]), [1], [yytype_int16],
211 b4_ints_in($@, [0], [65535]), [1], [yytype_uint16],
215 # b4_c99_int_type_define
216 # ----------------------
217 # Define private types suitable for holding small integers in C99 or later.
218 m4_define([b4_c99_int_type_define],
219 [m4_copy_force([b4_c99_int_type], [b4_int_type])dnl
224 /* On compilers that do not define __PTRDIFF_MAX__ etc., make sure
225 <limits.h> and (if available) <stdint.h> are included
226 so that the code can choose integer types of a good width. */
228 #ifndef __PTRDIFF_MAX__
229 # include <limits.h> /* INFRINGES ON USER NAME SPACE */
230 # if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
231 # include <stdint.h> /* INFRINGES ON USER NAME SPACE */
236 /* Narrow types that promote to a signed type and that can represent a
237 signed or unsigned integer of at least N bits. In tables they can
238 save space and decrease cache pressure. Promoting to a signed type
239 helps avoid bugs in integer arithmetic. */
241 #ifdef __INT_LEAST8_MAX__
242 typedef __INT_LEAST8_TYPE__ yytype_int8;
243 #elif defined YY_STDINT_H
244 typedef int_least8_t yytype_int8;
246 typedef signed char yytype_int8;
249 #ifdef __INT_LEAST16_MAX__
250 typedef __INT_LEAST16_TYPE__ yytype_int16;
251 #elif defined YY_STDINT_H
252 typedef int_least16_t yytype_int16;
254 typedef short yytype_int16;
257 /* Work around bug in HP-UX 11.23, which defines these macros
258 incorrectly for preprocessor constants. This workaround can likely
259 be removed in 2023, as HPE has promised support for HP-UX 11.23
260 (aka HP-UX 11i v2) only through the end of 2022; see Table 2 of
261 <https://h20195.www2.hpe.com/V2/getpdf.aspx/4AA4-7673ENW.pdf>. */
263 # undef UINT_LEAST8_MAX
264 # undef UINT_LEAST16_MAX
265 # define UINT_LEAST8_MAX 255
266 # define UINT_LEAST16_MAX 65535
269 #if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__
270 typedef __UINT_LEAST8_TYPE__ yytype_uint8;
271 #elif (!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H \
272 && UINT_LEAST8_MAX <= INT_MAX)
273 typedef uint_least8_t yytype_uint8;
274 #elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX
275 typedef unsigned char yytype_uint8;
277 typedef short yytype_uint8;
280 #if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__
281 typedef __UINT_LEAST16_TYPE__ yytype_uint16;
282 #elif (!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H \
283 && UINT_LEAST16_MAX <= INT_MAX)
284 typedef uint_least16_t yytype_uint16;
285 #elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX
286 typedef unsigned short yytype_uint16;
288 typedef int yytype_uint16;
292 # b4_sizes_types_define
293 # ---------------------
294 # Define YYPTRDIFF_T/YYPTRDIFF_MAXIMUM, YYSIZE_T/YYSIZE_MAXIMUM,
296 m4_define([b4_sizes_types_define],
297 [[#ifndef YYPTRDIFF_T
298 # if defined __PTRDIFF_TYPE__ && defined __PTRDIFF_MAX__
299 # define YYPTRDIFF_T __PTRDIFF_TYPE__
300 # define YYPTRDIFF_MAXIMUM __PTRDIFF_MAX__
301 # elif defined PTRDIFF_MAX
303 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
305 # define YYPTRDIFF_T ptrdiff_t
306 # define YYPTRDIFF_MAXIMUM PTRDIFF_MAX
308 # define YYPTRDIFF_T long
309 # define YYPTRDIFF_MAXIMUM LONG_MAX
314 # ifdef __SIZE_TYPE__
315 # define YYSIZE_T __SIZE_TYPE__
316 # elif defined size_t
317 # define YYSIZE_T size_t
318 # elif defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
319 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
320 # define YYSIZE_T size_t
322 # define YYSIZE_T unsigned
326 #define YYSIZE_MAXIMUM \
327 YY_CAST (YYPTRDIFF_T, \
328 (YYPTRDIFF_MAXIMUM < YY_CAST (YYSIZE_T, -1) \
329 ? YYPTRDIFF_MAXIMUM \
330 : YY_CAST (YYSIZE_T, -1)))
332 #define YYSIZEOF(X) YY_CAST (YYPTRDIFF_T, sizeof (X))
336 # b4_int_type_for(NAME)
337 # ---------------------
338 # Return a narrow int type able to handle numbers ranging from
339 # 'NAME_min' to 'NAME_max' (included).
340 m4_define([b4_int_type_for],
341 [b4_int_type($1_min, $1_max)])
344 # b4_table_value_equals(TABLE, VALUE, LITERAL, SYMBOL)
345 # ----------------------------------------------------
346 # Without inducing a comparison warning from the compiler, check if the
347 # literal value LITERAL equals VALUE from table TABLE, which must have
348 # TABLE_min and TABLE_max defined. SYMBOL denotes
349 m4_define([b4_table_value_equals],
350 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
351 || m4_indir([b4_]$1[_max]) < $3), [1],
356 ## ----------------- ##
357 ## Compiler issues. ##
358 ## ----------------- ##
360 # b4_attribute_define([noreturn])
361 # -------------------------------
362 # Provide portable compiler "attributes". If "noreturn" is passed, define
364 m4_define([b4_attribute_define],
365 [[#ifndef YY_ATTRIBUTE_PURE
366 # if defined __GNUC__ && 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
367 # define YY_ATTRIBUTE_PURE __attribute__ ((__pure__))
369 # define YY_ATTRIBUTE_PURE
373 #ifndef YY_ATTRIBUTE_UNUSED
374 # if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
375 # define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__))
377 # define YY_ATTRIBUTE_UNUSED
381 ]m4_bmatch([$1], [\bnoreturn\b], [[/* The _Noreturn keyword of C11. */
382 ]dnl This is close to lib/_Noreturn.h, except that we do enable
383 dnl the use of [[noreturn]], because _Noreturn is used in places
384 dnl where [[noreturn]] works in C++. We need this in particular
385 dnl because of glr.cc which compiles code from glr.c in C++.
386 dnl And the C++ compiler chokes on _Noreturn. Also, we do not
387 dnl use C' _Noreturn in C++, to avoid -Wc11-extensions warnings.
389 # if (defined __cplusplus \
390 && ((201103 <= __cplusplus && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) \
391 || (defined _MSC_VER && 1900 <= _MSC_VER)))
392 # define _Noreturn [[noreturn]]
393 # elif ((!defined __cplusplus || defined __clang__) \
394 && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) \
395 || (!defined __STRICT_ANSI__ \
396 && (__4 < __GNUC__ + (7 <= __GNUC_MINOR__) \
397 || (defined __apple_build_version__ \
398 ? 6000000 <= __apple_build_version__ \
399 : 3 < __clang_major__ + (5 <= __clang_minor__))))))
400 /* _Noreturn works as-is. */
401 # elif (2 < __GNUC__ + (8 <= __GNUC_MINOR__) || defined __clang__ \
402 || 0x5110 <= __SUNPRO_C)
403 # define _Noreturn __attribute__ ((__noreturn__))
404 # elif 1200 <= (defined _MSC_VER ? _MSC_VER : 0)
405 # define _Noreturn __declspec (noreturn)
411 ]])[/* Suppress unused-variable warnings by "using" E. */
412 #if ! defined lint || defined __GNUC__
413 # define YY_USE(E) ((void) (E))
415 # define YY_USE(E) /* empty */
418 /* Suppress an incorrect diagnostic about yylval being uninitialized. */
419 #if defined __GNUC__ && ! defined __ICC && 406 <= __GNUC__ * 100 + __GNUC_MINOR__
420 # if __GNUC__ * 100 + __GNUC_MINOR__ < 407
421 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
422 _Pragma ("GCC diagnostic push") \
423 _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")
425 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
426 _Pragma ("GCC diagnostic push") \
427 _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") \
428 _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
430 # define YY_IGNORE_MAYBE_UNINITIALIZED_END \
431 _Pragma ("GCC diagnostic pop")
433 # define YY_INITIAL_VALUE(Value) Value
435 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
436 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
437 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
439 #ifndef YY_INITIAL_VALUE
440 # define YY_INITIAL_VALUE(Value) /* Nothing. */
443 #if defined __cplusplus && defined __GNUC__ && ! defined __ICC && 6 <= __GNUC__
444 # define YY_IGNORE_USELESS_CAST_BEGIN \
445 _Pragma ("GCC diagnostic push") \
446 _Pragma ("GCC diagnostic ignored \"-Wuseless-cast\"")
447 # define YY_IGNORE_USELESS_CAST_END \
448 _Pragma ("GCC diagnostic pop")
450 #ifndef YY_IGNORE_USELESS_CAST_BEGIN
451 # define YY_IGNORE_USELESS_CAST_BEGIN
452 # define YY_IGNORE_USELESS_CAST_END
459 m4_define([b4_cast_define],
462 # define YY_CAST(Type, Val) static_cast<Type> (Val)
463 # define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast<Type> (Val)
465 # define YY_CAST(Type, Val) ((Type) (Val))
466 # define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val))
474 # Portability issues: define a YY_NULLPTR appropriate for the current
475 # language (C, C++98, or C++11).
477 # In C++ pre C++11 it is standard practice to use 0 (not NULL) for the
478 # null pointer. In C, prefer ((void*)0) to avoid having to include stdlib.h.
479 m4_define([b4_null_define],
481 # if defined __cplusplus
482 # if 201103L <= __cplusplus
483 # define YY_NULLPTR nullptr
485 # define YY_NULLPTR 0
488 # define YY_NULLPTR ((void*)0)
496 # Return a null pointer constant.
497 m4_define([b4_null], [YY_NULLPTR])
505 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
506 # -------------------------------------------------------------
507 # Define "yy<TABLE-NAME>" whose contents is CONTENT.
508 m4_define([b4_integral_parser_table_define],
509 [m4_ifvaln([$3], [b4_comment([$3])])dnl
510 static const b4_int_type_for([$2]) yy$1[[]] =
521 # Because C enums are not scoped, because tokens are exposed in the
522 # header, and because these tokens are common to all the parsers, we
523 # need to make sure their names don't collide: use the api.prefix.
524 # YYEOF is special, since the user may give it a different name.
525 m4_define([b4_symbol(-2, id)], [b4_api_PREFIX[][EMPTY]])
526 m4_define([b4_symbol(-2, tag)], [[No symbol.]])
528 m4_if(b4_symbol(eof, id), [YYEOF],
529 [m4_define([b4_symbol(0, id)], [b4_api_PREFIX[][EOF]])])
530 m4_define([b4_symbol(1, id)], [b4_api_PREFIX[][error]])
531 m4_define([b4_symbol(2, id)], [b4_api_PREFIX[][UNDEF]])
534 # b4_token_define(TOKEN-NUM)
535 # --------------------------
536 # Output the definition of this token as #define.
537 m4_define([b4_token_define],
538 [b4_token_format([#define %s %s], [$1])])
542 # Output the definition of the tokens.
543 m4_define([b4_token_defines],
545 #define ]b4_symbol(empty, [id])[ -2
547 ], b4_symbol_map([b4_token_define]))
551 # b4_token_enum(TOKEN-NUM)
552 # ------------------------
553 # Output the definition of this token as an enum.
554 m4_define([b4_token_enum],
555 [b4_token_visible_if([$1],
556 [m4_format([ %-30s %s],
557 m4_format([[%s = %s%s%s]],
558 b4_symbol([$1], [id]),
559 b4_symbol([$1], b4_api_token_raw_if([[number]], [[code]])),
560 m4_if([$1], b4_last_enum_token, [], [[,]])),
561 [b4_symbol_tag_comment([$1])])])])
566 # The definition of the token kinds.
567 m4_define([b4_token_enums],
568 [b4_any_token_visible_if([[/* Token kinds. */
569 #ifndef ]b4_api_PREFIX[TOKENTYPE
570 # define ]b4_api_PREFIX[TOKENTYPE
571 enum ]b4_api_prefix[tokentype
573 ]b4_symbol(empty, [id])[ = -2,
574 ]b4_symbol_foreach([b4_token_enum])dnl
576 typedef enum ]b4_api_prefix[tokentype ]b4_api_prefix[token_kind_t;
581 # b4_token_enums_defines
582 # ----------------------
583 # The definition of the tokens (if there are any) as enums and,
584 # if POSIX Yacc is enabled, as #defines.
585 m4_define([b4_token_enums_defines],
586 [b4_token_enums[]b4_yacc_if([b4_token_defines])])
589 # b4_symbol_translate(STRING)
590 # ---------------------------
591 # Used by "bison" in the array of symbol names to mark those that
592 # require translation.
593 m4_define([b4_symbol_translate],
602 # b4_symbol_enum(SYMBOL-NUM)
603 # --------------------------
604 # Output the definition of this symbol as an enum.
605 m4_define([b4_symbol_enum],
606 [m4_format([ %-40s %s],
607 m4_format([[%s = %s%s%s]],
608 b4_symbol([$1], [kind_base]),
610 m4_if([$1], b4_last_symbol, [], [[,]])),
611 [b4_symbol_tag_comment([$1])])])
614 # b4_declare_symbol_enum
615 # ----------------------
616 # The definition of the symbol internal numbers as an enum.
617 # Defining YYEMPTY here is important: it forces the compiler
618 # to use a signed type, which matters for yytoken.
619 m4_define([b4_declare_symbol_enum],
623 ]b4_symbol(empty, [kind_base])[ = -2,
624 ]b4_symbol_foreach([b4_symbol_enum])dnl
626 typedef enum yysymbol_kind_t yysymbol_kind_t;
630 ## ----------------- ##
631 ## Semantic Values. ##
632 ## ----------------- ##
635 # b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])
636 # ----------------------------------------------
638 m4_define([b4_symbol_value],
642 [b4_symbol_if([$2], [has_type],
643 [($1.b4_symbol([$2], [type]))],
648 ## ---------------------- ##
649 ## Defining C functions. ##
650 ## ---------------------- ##
653 # b4_formals([DECL1, NAME1], ...)
654 # -------------------------------
655 # The formal arguments of a C function definition.
656 m4_define([b4_formals],
657 [m4_if([$#], [0], [void],
659 [m4_map_sep([b4_formal], [, ], [$@])])])
661 m4_define([b4_formal],
665 # b4_function_declare(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
666 # ------------------------------------------------------------
667 # Declare the function NAME.
668 m4_define([b4_function_declare],
669 [$2 $1 (b4_formals(m4_shift2($@)));[]dnl
674 ## --------------------- ##
675 ## Calling C functions. ##
676 ## --------------------- ##
679 # b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
680 # -----------------------------------------------------------
681 # Call the function NAME with arguments NAME1, NAME2 etc.
682 m4_define([b4_function_call],
683 [$1 (b4_args(m4_shift2($@)))[]dnl
687 # b4_args([DECL1, NAME1], ...)
688 # ----------------------------
689 # Output the arguments NAME1, NAME2...
691 [m4_map_sep([b4_arg], [, ], [$@])])
701 # b4_sync_start(LINE, FILE)
702 # -------------------------
703 m4_define([b4_sync_start], [[#]line $1 $2])
710 # b4_case(LABEL, STATEMENTS, [COMMENTS])
711 # --------------------------------------
713 [ case $1:m4_ifval([$3], [ b4_comment([$3])])
715 b4_syncline([@oline@], [@ofile@])dnl
719 # b4_predicate_case(LABEL, CONDITIONS)
720 # ------------------------------------
721 m4_define([b4_predicate_case],
725 b4_syncline([@oline@], [@ofile@])dnl
729 # b4_yydestruct_define
730 # --------------------
731 # Define the "yydestruct" function.
732 m4_define_default([b4_yydestruct_define],
733 [[/*-----------------------------------------------.
734 | Release the memory associated to this symbol. |
735 `-----------------------------------------------*/
738 yydestruct (const char *yymsg,
739 yysymbol_kind_t yykind, YYSTYPE *yyvaluep]b4_locations_if(dnl
740 [[, YYLTYPE *yylocationp]])[]b4_user_formals[)
742 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
745 YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp);
747 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
748 ]b4_symbol_actions([destructor])[
749 YY_IGNORE_MAYBE_UNINITIALIZED_END
754 # b4_yy_symbol_print_define
755 # -------------------------
756 # Define the "yy_symbol_print" function.
757 m4_define_default([b4_yy_symbol_print_define],
759 /*-----------------------------------.
760 | Print this symbol's value on YYO. |
761 `-----------------------------------*/
764 yy_symbol_value_print (FILE *yyo,
765 yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep]b4_locations_if(dnl
766 [[, YYLTYPE const * const yylocationp]])[]b4_user_formals[)
768 FILE *yyoutput = yyo;
769 ]b4_parse_param_use([yyoutput], [yylocationp])dnl
772 b4_percent_code_get([[pre-printer]])dnl
773 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
774 b4_symbol_actions([printer])
775 YY_IGNORE_MAYBE_UNINITIALIZED_END
776 b4_percent_code_get([[post-printer]])dnl
780 /*---------------------------.
781 | Print this symbol on YYO. |
782 `---------------------------*/
785 yy_symbol_print (FILE *yyo,
786 yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep]b4_locations_if(dnl
787 [[, YYLTYPE const * const yylocationp]])[]b4_user_formals[)
789 YYFPRINTF (yyo, "%s %s (",
790 yykind < YYNTOKENS ? "token" : "nterm", yysymbol_name (yykind));
792 ]b4_locations_if([ YYLOCATION_PRINT (yyo, yylocationp);
793 YYFPRINTF (yyo, ": ");
795 [ yy_symbol_value_print (yyo, yykind, yyvaluep]dnl
796 b4_locations_if([, yylocationp])[]b4_user_args[);
797 YYFPRINTF (yyo, ")");
802 ## ---------------- ##
803 ## api.value.type. ##
804 ## ---------------- ##
807 # ---------------------- #
808 # api.value.type=union. #
809 # ---------------------- #
811 # b4_symbol_type_register(SYMBOL-NUM)
812 # -----------------------------------
813 # Symbol SYMBOL-NUM has a type (for variant) instead of a type-tag.
814 # Extend the definition of %union's body (b4_union_members) with a
815 # field of that type, and extend the symbol's "type" field to point to
816 # the field name, instead of the type name.
817 m4_define([b4_symbol_type_register],
818 [m4_define([b4_symbol($1, type_tag)],
819 [b4_symbol_if([$1], [has_id],
820 [b4_symbol([$1], [id])],
821 [yykind_[]b4_symbol([$1], [number])])])dnl
822 m4_append([b4_union_members],
823 m4_expand([m4_format([ %-40s %s],
824 m4_expand([b4_symbol([$1], [type]) b4_symbol([$1], [type_tag]);]),
825 [b4_symbol_tag_comment([$1])])]))
829 # b4_type_define_tag(SYMBOL1-NUM, ...)
830 # ------------------------------------
831 # For the batch of symbols SYMBOL1-NUM... (which all have the same
832 # type), enhance the %union definition for each of them, and set
833 # there "type" field to the field tag name, instead of the type name.
834 m4_define([b4_type_define_tag],
835 [b4_symbol_if([$1], [has_type],
836 [m4_map([b4_symbol_type_register], [$@])])
840 # b4_symbol_value_union(VAL, SYMBOL-NUM, [TYPE])
841 # ----------------------------------------------
842 # Same of b4_symbol_value, but when api.value.type=union.
843 m4_define([b4_symbol_value_union],
847 [b4_symbol_if([$2], [has_type],
848 [($1.b4_symbol([$2], [type_tag]))],
853 # b4_value_type_setup_union
854 # -------------------------
855 # Setup support for api.value.type=union. Symbols are defined with a
856 # type instead of a union member name: build the corresponding union,
857 # and give the symbols their tag.
858 m4_define([b4_value_type_setup_union],
859 [m4_define([b4_union_members])
860 b4_type_foreach([b4_type_define_tag])
861 m4_copy_force([b4_symbol_value_union], [b4_symbol_value])
865 # -------------------------- #
866 # api.value.type = variant. #
867 # -------------------------- #
869 # b4_value_type_setup_variant
870 # ---------------------------
871 # Setup support for api.value.type=variant. By default, fail, specialized
872 # by other skeletons.
873 m4_define([b4_value_type_setup_variant],
874 [b4_complain_at(b4_percent_define_get_loc([[api.value.type]]),
875 [['%s' does not support '%s']],
877 [%define api.value.type variant])])
880 # _b4_value_type_setup_keyword
881 # ----------------------------
882 # api.value.type is defined with a keyword/string syntax. Check if
883 # that is properly defined, and prepare its use.
884 m4_define([_b4_value_type_setup_keyword],
885 [b4_percent_define_check_values([[[[api.value.type]],
891 m4_case(b4_percent_define_get([[api.value.type]]),
892 [union], [b4_value_type_setup_union],
893 [variant], [b4_value_type_setup_variant])])
896 # b4_value_type_setup
897 # -------------------
898 # Check if api.value.type is properly defined, and possibly prepare
900 b4_define_silent([b4_value_type_setup],
901 [# Define default value.
902 b4_percent_define_ifdef([[api.value.type]], [],
903 [# %union => api.value.type=union-directive
904 m4_ifdef([b4_union_members],
905 [m4_define([b4_percent_define_kind(api.value.type)], [keyword])
906 m4_define([b4_percent_define(api.value.type)], [union-directive])],
907 [# no tag seen => api.value.type={int}
908 m4_if(b4_tag_seen_flag, 0,
909 [m4_define([b4_percent_define_kind(api.value.type)], [code])
910 m4_define([b4_percent_define(api.value.type)], [int])],
911 [# otherwise api.value.type=yystype
912 m4_define([b4_percent_define_kind(api.value.type)], [keyword])
913 m4_define([b4_percent_define(api.value.type)], [yystype])])])])
916 m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
917 [keyword\|string], [_b4_value_type_setup_keyword])
926 # b4_value_type_define
927 # --------------------
928 m4_define([b4_value_type_define],
929 [b4_value_type_setup[]dnl
931 m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
933 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
934 typedef ]b4_percent_define_get([[api.value.type]])[ ]b4_api_PREFIX[STYPE;
935 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1
936 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
939 [m4_bmatch(b4_percent_define_get([[api.value.type]]),
940 [union\|union-directive],
941 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
942 ]b4_percent_define_get_syncline([[api.value.union.name]])dnl
943 [union ]b4_percent_define_get([[api.value.union.name]])[
945 ]b4_user_union_members[
947 ]b4_percent_define_get_syncline([[api.value.union.name]])dnl
948 [typedef union ]b4_percent_define_get([[api.value.union.name]])[ ]b4_api_PREFIX[STYPE;
949 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1
950 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
955 # b4_location_type_define
956 # -----------------------
957 m4_define([b4_location_type_define],
958 [[/* Location type. */
959 ]b4_percent_define_ifdef([[api.location.type]],
960 [[typedef ]b4_percent_define_get([[api.location.type]])[ ]b4_api_PREFIX[LTYPE;
962 [[#if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
963 typedef struct ]b4_api_PREFIX[LTYPE ]b4_api_PREFIX[LTYPE;
964 struct ]b4_api_PREFIX[LTYPE
971 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
972 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
977 # b4_declare_yylstype
978 # -------------------
979 # Declarations that might either go into the header (if --header) or
980 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
981 m4_define([b4_declare_yylstype],
982 [b4_value_type_define[]b4_locations_if([
983 b4_location_type_define])
985 b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
986 ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
992 m4_define([b4_YYDEBUG_define],
993 [[/* Debug traces. */
994 ]m4_if(b4_api_prefix, [yy],
996 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
998 [[#ifndef ]b4_api_PREFIX[DEBUG
1001 # define ]b4_api_PREFIX[DEBUG 1
1003 # define ]b4_api_PREFIX[DEBUG 0
1005 # else /* ! defined YYDEBUG */
1006 # define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[
1007 # endif /* ! defined YYDEBUG */
1008 #endif /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl
1011 # b4_declare_yydebug
1012 # ------------------
1013 m4_define([b4_declare_yydebug],
1015 #if ]b4_api_PREFIX[DEBUG
1016 extern int ]b4_prefix[debug;
1020 # b4_yylloc_default_define
1021 # ------------------------
1022 # Define YYLLOC_DEFAULT.
1023 m4_define([b4_yylloc_default_define],
1024 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
1025 If N is 0, then set CURRENT to the empty location which ends
1026 the previous symbol: RHS[0] (always defined). */
1028 #ifndef YYLLOC_DEFAULT
1029 # define YYLLOC_DEFAULT(Current, Rhs, N) \
1033 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
1034 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
1035 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
1036 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
1040 (Current).first_line = (Current).last_line = \
1041 YYRHSLOC (Rhs, 0).last_line; \
1042 (Current).first_column = (Current).last_column = \
1043 YYRHSLOC (Rhs, 0).last_column; \
1049 # b4_yylocation_print_define
1050 # --------------------------
1051 # Define YYLOCATION_PRINT.
1052 m4_define([b4_yylocation_print_define],
1054 /* YYLOCATION_PRINT -- Print the location on the stream.
1055 This macro was not mandated originally: define only if we know
1056 we won't break user code: when these are the locations we know. */
1058 # ifndef YYLOCATION_PRINT
1060 # if defined YY_LOCATION_PRINT
1062 /* Temporary convenience wrapper in case some people defined the
1063 undocumented and private YY_LOCATION_PRINT macros. */
1064 # define YYLOCATION_PRINT(File, Loc) YY_LOCATION_PRINT(File, *(Loc))
1066 # elif defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1068 /* Print *YYLOCP on YYO. Private, do not rely on its existence. */
1072 yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp)
1075 int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
1076 if (0 <= yylocp->first_line)
1078 res += YYFPRINTF (yyo, "%d", yylocp->first_line);
1079 if (0 <= yylocp->first_column)
1080 res += YYFPRINTF (yyo, ".%d", yylocp->first_column);
1082 if (0 <= yylocp->last_line)
1084 if (yylocp->first_line < yylocp->last_line)
1086 res += YYFPRINTF (yyo, "-%d", yylocp->last_line);
1088 res += YYFPRINTF (yyo, ".%d", end_col);
1090 else if (0 <= end_col && yylocp->first_column < end_col)
1091 res += YYFPRINTF (yyo, "-%d", end_col);
1096 # define YYLOCATION_PRINT yy_location_print_
1098 /* Temporary convenience wrapper in case some people defined the
1099 undocumented and private YY_LOCATION_PRINT macros. */
1100 # define YY_LOCATION_PRINT(File, Loc) YYLOCATION_PRINT(File, &(Loc))
1104 # define YYLOCATION_PRINT(File, Loc) ((void) 0)
1105 /* Temporary convenience wrapper in case some people defined the
1106 undocumented and private YY_LOCATION_PRINT macros. */
1107 # define YY_LOCATION_PRINT YYLOCATION_PRINT
1110 # endif /* !defined YYLOCATION_PRINT */]])
1115 # Expand to a possible default value for yylloc.
1116 m4_define([b4_yyloc_default],
1118 # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1120 m4_defn([b4_location_initial_line]),
1121 m4_defn([b4_location_initial_column]),
1122 m4_defn([b4_location_initial_line]),
1123 m4_defn([b4_location_initial_column]))[ }