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([[[[YYSTYPE *yylvalp]], [[&yylval]]][]dnl
116 b4_locations_if([, [[YYLTYPE *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_args(b4_parse_param)])])
135 # The possible parse-params formal arguments preceded by a comma.
136 m4_define([b4_user_formals],
137 [m4_ifset([b4_parse_param], [, b4_formals(b4_parse_param)])])
142 # If defined, b4_parse_param arrives double quoted, but below we prefer
143 # it to be single quoted.
144 m4_define([b4_parse_param],
148 # b4_parse_param_for(DECL, FORMAL, BODY)
149 # ---------------------------------------
150 # Iterate over the user parameters, binding the declaration to DECL,
151 # the formal name to FORMAL, and evaluating the BODY.
152 m4_define([b4_parse_param_for],
153 [m4_foreach([$1_$2], m4_defn([b4_parse_param]),
154 [m4_pushdef([$1], m4_unquote(m4_car($1_$2)))dnl
155 m4_pushdef([$2], m4_shift($1_$2))dnl
161 # b4_parse_param_use([VAL], [LOC])
162 # --------------------------------
163 # 'YY_USE' VAL, LOC if locations are enabled, and all the parse-params.
164 m4_define([b4_parse_param_use],
165 [m4_ifvaln([$1], [ YY_USE ([$1]);])dnl
166 b4_locations_if([m4_ifvaln([$2], [ YY_USE ([$2]);])])dnl
167 b4_parse_param_for([Decl], [Formal], [ YY_USE (Formal);
176 # b4_int_type(MIN, MAX)
177 # ---------------------
178 # Return a narrow int type able to handle integers ranging from MIN
179 # to MAX (included) in portable C code. Assume MIN and MAX fall in
181 m4_define([b4_int_type],
182 [m4_if(b4_ints_in($@, [-127], [127]), [1], [signed char],
183 b4_ints_in($@, [0], [255]), [1], [unsigned char],
185 b4_ints_in($@, [-32767], [32767]), [1], [short],
186 b4_ints_in($@, [0], [65535]), [1], [unsigned short],
190 # b4_c99_int_type(MIN, MAX)
191 # -------------------------
192 # Like b4_int_type, but for C99.
193 # b4_c99_int_type_define replaces b4_int_type with this.
194 m4_define([b4_c99_int_type],
195 [m4_if(b4_ints_in($@, [-127], [127]), [1], [yytype_int8],
196 b4_ints_in($@, [0], [255]), [1], [yytype_uint8],
198 b4_ints_in($@, [-32767], [32767]), [1], [yytype_int16],
199 b4_ints_in($@, [0], [65535]), [1], [yytype_uint16],
203 # b4_c99_int_type_define
204 # ----------------------
205 # Define private types suitable for holding small integers in C99 or later.
206 m4_define([b4_c99_int_type_define],
207 [m4_copy_force([b4_c99_int_type], [b4_int_type])dnl
212 /* On compilers that do not define __PTRDIFF_MAX__ etc., make sure
213 <limits.h> and (if available) <stdint.h> are included
214 so that the code can choose integer types of a good width. */
216 #ifndef __PTRDIFF_MAX__
217 # include <limits.h> /* INFRINGES ON USER NAME SPACE */
218 # if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
219 # include <stdint.h> /* INFRINGES ON USER NAME SPACE */
224 /* Narrow types that promote to a signed type and that can represent a
225 signed or unsigned integer of at least N bits. In tables they can
226 save space and decrease cache pressure. Promoting to a signed type
227 helps avoid bugs in integer arithmetic. */
229 #ifdef __INT_LEAST8_MAX__
230 typedef __INT_LEAST8_TYPE__ yytype_int8;
231 #elif defined YY_STDINT_H
232 typedef int_least8_t yytype_int8;
234 typedef signed char yytype_int8;
237 #ifdef __INT_LEAST16_MAX__
238 typedef __INT_LEAST16_TYPE__ yytype_int16;
239 #elif defined YY_STDINT_H
240 typedef int_least16_t yytype_int16;
242 typedef short yytype_int16;
245 /* Work around bug in HP-UX 11.23, which defines these macros
246 incorrectly for preprocessor constants. This workaround can likely
247 be removed in 2023, as HPE has promised support for HP-UX 11.23
248 (aka HP-UX 11i v2) only through the end of 2022; see Table 2 of
249 <https://h20195.www2.hpe.com/V2/getpdf.aspx/4AA4-7673ENW.pdf>. */
251 # undef UINT_LEAST8_MAX
252 # undef UINT_LEAST16_MAX
253 # define UINT_LEAST8_MAX 255
254 # define UINT_LEAST16_MAX 65535
257 #if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__
258 typedef __UINT_LEAST8_TYPE__ yytype_uint8;
259 #elif (!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H \
260 && UINT_LEAST8_MAX <= INT_MAX)
261 typedef uint_least8_t yytype_uint8;
262 #elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX
263 typedef unsigned char yytype_uint8;
265 typedef short yytype_uint8;
268 #if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__
269 typedef __UINT_LEAST16_TYPE__ yytype_uint16;
270 #elif (!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H \
271 && UINT_LEAST16_MAX <= INT_MAX)
272 typedef uint_least16_t yytype_uint16;
273 #elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX
274 typedef unsigned short yytype_uint16;
276 typedef int yytype_uint16;
280 # b4_sizes_types_define
281 # ---------------------
282 # Define YYPTRDIFF_T/YYPTRDIFF_MAXIMUM, YYSIZE_T/YYSIZE_MAXIMUM,
284 m4_define([b4_sizes_types_define],
285 [[#ifndef YYPTRDIFF_T
286 # if defined __PTRDIFF_TYPE__ && defined __PTRDIFF_MAX__
287 # define YYPTRDIFF_T __PTRDIFF_TYPE__
288 # define YYPTRDIFF_MAXIMUM __PTRDIFF_MAX__
289 # elif defined PTRDIFF_MAX
291 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
293 # define YYPTRDIFF_T ptrdiff_t
294 # define YYPTRDIFF_MAXIMUM PTRDIFF_MAX
296 # define YYPTRDIFF_T long
297 # define YYPTRDIFF_MAXIMUM LONG_MAX
302 # ifdef __SIZE_TYPE__
303 # define YYSIZE_T __SIZE_TYPE__
304 # elif defined size_t
305 # define YYSIZE_T size_t
306 # elif defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
307 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
308 # define YYSIZE_T size_t
310 # define YYSIZE_T unsigned
314 #define YYSIZE_MAXIMUM \
315 YY_CAST (YYPTRDIFF_T, \
316 (YYPTRDIFF_MAXIMUM < YY_CAST (YYSIZE_T, -1) \
317 ? YYPTRDIFF_MAXIMUM \
318 : YY_CAST (YYSIZE_T, -1)))
320 #define YYSIZEOF(X) YY_CAST (YYPTRDIFF_T, sizeof (X))
324 # b4_int_type_for(NAME)
325 # ---------------------
326 # Return a narrow int type able to handle numbers ranging from
327 # 'NAME_min' to 'NAME_max' (included).
328 m4_define([b4_int_type_for],
329 [b4_int_type($1_min, $1_max)])
332 # b4_table_value_equals(TABLE, VALUE, LITERAL, SYMBOL)
333 # ----------------------------------------------------
334 # Without inducing a comparison warning from the compiler, check if the
335 # literal value LITERAL equals VALUE from table TABLE, which must have
336 # TABLE_min and TABLE_max defined. SYMBOL denotes
337 m4_define([b4_table_value_equals],
338 [m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
339 || m4_indir([b4_]$1[_max]) < $3), [1],
344 ## ----------------- ##
345 ## Compiler issues. ##
346 ## ----------------- ##
348 # b4_attribute_define([noreturn])
349 # -------------------------------
350 # Provide portable compiler "attributes". If "noreturn" is passed, define
352 m4_define([b4_attribute_define],
353 [[#ifndef YY_ATTRIBUTE_PURE
354 # if defined __GNUC__ && 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
355 # define YY_ATTRIBUTE_PURE __attribute__ ((__pure__))
357 # define YY_ATTRIBUTE_PURE
361 #ifndef YY_ATTRIBUTE_UNUSED
362 # if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
363 # define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__))
365 # define YY_ATTRIBUTE_UNUSED
369 ]m4_bmatch([$1], [\bnoreturn\b], [[/* The _Noreturn keyword of C11. */
370 ]dnl This is close to lib/_Noreturn.h, except that we do enable
371 dnl the use of [[noreturn]], because _Noreturn is used in places
372 dnl where [[noreturn]] works in C++. We need this in particular
373 dnl because of glr.cc which compiles code from glr.c in C++.
374 dnl And the C++ compiler chokes on _Noreturn. Also, we do not
375 dnl use C' _Noreturn in C++, to avoid -Wc11-extensions warnings.
377 # if (defined __cplusplus \
378 && ((201103 <= __cplusplus && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) \
379 || (defined _MSC_VER && 1900 <= _MSC_VER)))
380 # define _Noreturn [[noreturn]]
381 # elif ((!defined __cplusplus || defined __clang__) \
382 && (201112 <= (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) \
383 || (!defined __STRICT_ANSI__ \
384 && (__4 < __GNUC__ + (7 <= __GNUC_MINOR__) \
385 || (defined __apple_build_version__ \
386 ? 6000000 <= __apple_build_version__ \
387 : 3 < __clang_major__ + (5 <= __clang_minor__))))))
388 /* _Noreturn works as-is. */
389 # elif (2 < __GNUC__ + (8 <= __GNUC_MINOR__) || defined __clang__ \
390 || 0x5110 <= __SUNPRO_C)
391 # define _Noreturn __attribute__ ((__noreturn__))
392 # elif 1200 <= (defined _MSC_VER ? _MSC_VER : 0)
393 # define _Noreturn __declspec (noreturn)
399 ]])[/* Suppress unused-variable warnings by "using" E. */
400 #if ! defined lint || defined __GNUC__
401 # define YY_USE(E) ((void) (E))
403 # define YY_USE(E) /* empty */
406 #if defined __GNUC__ && ! defined __ICC && 407 <= __GNUC__ * 100 + __GNUC_MINOR__
407 /* Suppress an incorrect diagnostic about yylval being uninitialized. */
408 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
409 _Pragma ("GCC diagnostic push") \
410 _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") \
411 _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
412 # define YY_IGNORE_MAYBE_UNINITIALIZED_END \
413 _Pragma ("GCC diagnostic pop")
415 # define YY_INITIAL_VALUE(Value) Value
417 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
418 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
419 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
421 #ifndef YY_INITIAL_VALUE
422 # define YY_INITIAL_VALUE(Value) /* Nothing. */
425 #if defined __cplusplus && defined __GNUC__ && ! defined __ICC && 6 <= __GNUC__
426 # define YY_IGNORE_USELESS_CAST_BEGIN \
427 _Pragma ("GCC diagnostic push") \
428 _Pragma ("GCC diagnostic ignored \"-Wuseless-cast\"")
429 # define YY_IGNORE_USELESS_CAST_END \
430 _Pragma ("GCC diagnostic pop")
432 #ifndef YY_IGNORE_USELESS_CAST_BEGIN
433 # define YY_IGNORE_USELESS_CAST_BEGIN
434 # define YY_IGNORE_USELESS_CAST_END
441 m4_define([b4_cast_define],
444 # define YY_CAST(Type, Val) static_cast<Type> (Val)
445 # define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast<Type> (Val)
447 # define YY_CAST(Type, Val) ((Type) (Val))
448 # define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val))
456 # Portability issues: define a YY_NULLPTR appropriate for the current
457 # language (C, C++98, or C++11).
459 # In C++ pre C++11 it is standard practice to use 0 (not NULL) for the
460 # null pointer. In C, prefer ((void*)0) to avoid having to include stdlib.h.
461 m4_define([b4_null_define],
463 # if defined __cplusplus
464 # if 201103L <= __cplusplus
465 # define YY_NULLPTR nullptr
467 # define YY_NULLPTR 0
470 # define YY_NULLPTR ((void*)0)
478 # Return a null pointer constant.
479 m4_define([b4_null], [YY_NULLPTR])
487 # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
488 # -------------------------------------------------------------
489 # Define "yy<TABLE-NAME>" whose contents is CONTENT.
490 m4_define([b4_integral_parser_table_define],
491 [m4_ifvaln([$3], [b4_comment([$3], [ ])])dnl
492 static const b4_int_type_for([$2]) yy$1[[]] =
503 # Because C enums are not scoped, because tokens are exposed in the
504 # header, and because these tokens are common to all the parsers, we
505 # need to make sure their names don't collide: use the api.prefix.
506 # YYEOF is special, since the user may give it a different name.
507 m4_define([b4_symbol(-2, id)], [b4_api_PREFIX[][EMPTY]])
508 m4_define([b4_symbol(-2, tag)], [[No symbol.]])
510 m4_if(b4_symbol(0, id), [YYEOF],
511 [m4_define([b4_symbol(0, id)], [b4_api_PREFIX[][EOF]])])
512 m4_define([b4_symbol(1, id)], [b4_api_PREFIX[][error]])
513 m4_define([b4_symbol(2, id)], [b4_api_PREFIX[][UNDEF]])
516 # b4_token_define(TOKEN-NUM)
517 # --------------------------
518 # Output the definition of this token as #define.
519 m4_define([b4_token_define],
520 [b4_token_format([#define %s %s], [$1])])
524 # Output the definition of the tokens.
525 m4_define([b4_token_defines],
527 #define ]b4_symbol([-2], [id])[ -2
529 ], b4_symbol_map([b4_token_define]))
533 # b4_token_enum(TOKEN-NUM)
534 # ------------------------
535 # Output the definition of this token as an enum.
536 m4_define([b4_token_enum],
537 [b4_token_visible_if([$1],
538 [m4_format([ %-30s %s],
539 m4_format([[%s = %s%s%s]],
540 b4_symbol([$1], [id]),
541 b4_symbol([$1], b4_api_token_raw_if([[number]], [[code]])),
542 m4_if([$1], b4_last_enum_token, [], [[,]])),
543 [b4_symbol_tag_comment([$1])])])])
548 # The definition of the token kinds.
549 m4_define([b4_token_enums],
550 [b4_any_token_visible_if([[/* Token kinds. */
551 #ifndef ]b4_api_PREFIX[TOKENTYPE
552 # define ]b4_api_PREFIX[TOKENTYPE
553 enum ]b4_api_prefix[tokentype
555 ]b4_symbol([-2], [id])[ = -2,
556 ]b4_symbol_foreach([b4_token_enum])dnl
558 typedef enum ]b4_api_prefix[tokentype ]b4_api_prefix[token_kind_t;
563 # b4_token_enums_defines
564 # ----------------------
565 # The definition of the tokens (if there are any) as enums and,
566 # if POSIX Yacc is enabled, as #defines.
567 m4_define([b4_token_enums_defines],
568 [b4_token_enums[]b4_yacc_if([b4_token_defines])])
571 # b4_symbol_translate(STRING)
572 # ---------------------------
573 # Used by "bison" in the array of symbol names to mark those that
574 # require translation.
575 m4_define([b4_symbol_translate],
584 # b4_symbol_enum(SYMBOL-NUM)
585 # --------------------------
586 # Output the definition of this symbol as an enum.
587 m4_define([b4_symbol_enum],
588 [m4_format([ %-40s %s],
589 m4_format([[%s = %s%s%s]],
590 b4_symbol([$1], [kind_base]),
592 m4_if([$1], b4_last_symbol, [], [[,]])),
593 [b4_symbol_tag_comment([$1])])])
596 # b4_declare_symbol_enum
597 # ----------------------
598 # The definition of the symbol internal numbers as an enum.
599 # Defining YYEMPTY here is important: it forces the compiler
600 # to use a signed type, which matters for yytoken.
601 m4_define([b4_declare_symbol_enum],
605 ]b4_symbol([-2], kind_base)[ = -2,
606 ]b4_symbol_foreach([b4_symbol_enum])dnl
608 typedef enum yysymbol_kind_t yysymbol_kind_t;
612 ## ----------------- ##
613 ## Semantic Values. ##
614 ## ----------------- ##
617 # b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])
618 # ----------------------------------------------
620 m4_define([b4_symbol_value],
624 [b4_symbol_if([$2], [has_type],
625 [($1.b4_symbol([$2], [type]))],
630 ## ---------------------- ##
631 ## Defining C functions. ##
632 ## ---------------------- ##
635 # b4_formals([DECL1, NAME1], ...)
636 # -------------------------------
637 # The formal arguments of a C function definition.
638 m4_define([b4_formals],
639 [m4_if([$#], [0], [void],
641 [m4_map_sep([b4_formal], [, ], [$@])])])
643 m4_define([b4_formal],
648 ## --------------------- ##
649 ## Calling C functions. ##
650 ## --------------------- ##
653 # b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
654 # -----------------------------------------------------------
655 # Call the function NAME with arguments NAME1, NAME2 etc.
656 m4_define([b4_function_call],
657 [$1 (b4_args(m4_shift2($@)))[]dnl
661 # b4_args([DECL1, NAME1], ...)
662 # ----------------------------
663 # Output the arguments NAME1, NAME2...
665 [m4_map_sep([b4_arg], [, ], [$@])])
675 # b4_sync_start(LINE, FILE)
676 # -------------------------
677 m4_define([b4_sync_start], [[#]line $1 $2])
684 # b4_case(LABEL, STATEMENTS, [COMMENTS])
685 # --------------------------------------
687 [ case $1:m4_ifval([$3], [ b4_comment([$3])])
689 b4_syncline([@oline@], [@ofile@])dnl
693 # b4_predicate_case(LABEL, CONDITIONS)
694 # ------------------------------------
695 m4_define([b4_predicate_case],
699 b4_syncline([@oline@], [@ofile@])dnl
703 # b4_yydestruct_define
704 # --------------------
705 # Define the "yydestruct" function.
706 m4_define_default([b4_yydestruct_define],
707 [[/*-----------------------------------------------.
708 | Release the memory associated to this symbol. |
709 `-----------------------------------------------*/
712 yydestruct (const char *yymsg,
713 yysymbol_kind_t yykind, YYSTYPE *yyvaluep]b4_locations_if(dnl
714 [[, YYLTYPE *yylocationp]])[]b4_user_formals[)
716 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
719 YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp);
721 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
722 ]b4_symbol_actions([destructor])[
723 YY_IGNORE_MAYBE_UNINITIALIZED_END
728 # b4_yy_symbol_print_define
729 # -------------------------
730 # Define the "yy_symbol_print" function.
731 m4_define_default([b4_yy_symbol_print_define],
733 /*-----------------------------------.
734 | Print this symbol's value on YYO. |
735 `-----------------------------------*/
738 yy_symbol_value_print (FILE *yyo,
739 yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep]b4_locations_if(dnl
740 [[, YYLTYPE const * const yylocationp]])[]b4_user_formals[)
742 FILE *yyoutput = yyo;
743 ]b4_parse_param_use([yyoutput], [yylocationp])dnl
746 dnl glr.c does not feature yytoknum.
747 m4_if(b4_skeleton, ["yacc.c"],
749 if (yykind < YYNTOKENS)
750 YYPRINT (yyo, yytoknum[yykind], *yyvaluep);
753 b4_percent_code_get([[pre-printer]])dnl
754 YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
755 b4_symbol_actions([printer])
756 YY_IGNORE_MAYBE_UNINITIALIZED_END
757 b4_percent_code_get([[post-printer]])dnl
761 /*---------------------------.
762 | Print this symbol on YYO. |
763 `---------------------------*/
766 yy_symbol_print (FILE *yyo,
767 yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep]b4_locations_if(dnl
768 [[, YYLTYPE const * const yylocationp]])[]b4_user_formals[)
770 YYFPRINTF (yyo, "%s %s (",
771 yykind < YYNTOKENS ? "token" : "nterm", yysymbol_name (yykind));
773 ]b4_locations_if([ YY_LOCATION_PRINT (yyo, *yylocationp);
774 YYFPRINTF (yyo, ": ");
776 [ yy_symbol_value_print (yyo, yykind, yyvaluep]dnl
777 b4_locations_if([, yylocationp])[]b4_user_args[);
778 YYFPRINTF (yyo, ")");
783 ## ---------------- ##
784 ## api.value.type. ##
785 ## ---------------- ##
788 # ---------------------- #
789 # api.value.type=union. #
790 # ---------------------- #
792 # b4_symbol_type_register(SYMBOL-NUM)
793 # -----------------------------------
794 # Symbol SYMBOL-NUM has a type (for variant) instead of a type-tag.
795 # Extend the definition of %union's body (b4_union_members) with a
796 # field of that type, and extend the symbol's "type" field to point to
797 # the field name, instead of the type name.
798 m4_define([b4_symbol_type_register],
799 [m4_define([b4_symbol($1, type_tag)],
800 [b4_symbol_if([$1], [has_id],
801 [b4_symbol([$1], [id])],
802 [yykind_[]b4_symbol([$1], [number])])])dnl
803 m4_append([b4_union_members],
804 m4_expand([m4_format([ %-40s %s],
805 m4_expand([b4_symbol([$1], [type]) b4_symbol([$1], [type_tag]);]),
806 [b4_symbol_tag_comment([$1])])]))
810 # b4_type_define_tag(SYMBOL1-NUM, ...)
811 # ------------------------------------
812 # For the batch of symbols SYMBOL1-NUM... (which all have the same
813 # type), enhance the %union definition for each of them, and set
814 # there "type" field to the field tag name, instead of the type name.
815 m4_define([b4_type_define_tag],
816 [b4_symbol_if([$1], [has_type],
817 [m4_map([b4_symbol_type_register], [$@])])
821 # b4_symbol_value_union(VAL, SYMBOL-NUM, [TYPE])
822 # ----------------------------------------------
823 # Same of b4_symbol_value, but when api.value.type=union.
824 m4_define([b4_symbol_value_union],
828 [b4_symbol_if([$2], [has_type],
829 [($1.b4_symbol([$2], [type_tag]))],
834 # b4_value_type_setup_union
835 # -------------------------
836 # Setup support for api.value.type=union. Symbols are defined with a
837 # type instead of a union member name: build the corresponding union,
838 # and give the symbols their tag.
839 m4_define([b4_value_type_setup_union],
840 [m4_define([b4_union_members])
841 b4_type_foreach([b4_type_define_tag])
842 m4_copy_force([b4_symbol_value_union], [b4_symbol_value])
846 # -------------------------- #
847 # api.value.type = variant. #
848 # -------------------------- #
850 # b4_value_type_setup_variant
851 # ---------------------------
852 # Setup support for api.value.type=variant. By default, fail, specialized
853 # by other skeletons.
854 m4_define([b4_value_type_setup_variant],
855 [b4_complain_at(b4_percent_define_get_loc([[api.value.type]]),
856 [['%s' does not support '%s']],
858 [%define api.value.type variant])])
861 # _b4_value_type_setup_keyword
862 # ----------------------------
863 # api.value.type is defined with a keyword/string syntax. Check if
864 # that is properly defined, and prepare its use.
865 m4_define([_b4_value_type_setup_keyword],
866 [b4_percent_define_check_values([[[[api.value.type]],
872 m4_case(b4_percent_define_get([[api.value.type]]),
873 [union], [b4_value_type_setup_union],
874 [variant], [b4_value_type_setup_variant])])
877 # b4_value_type_setup
878 # -------------------
879 # Check if api.value.type is properly defined, and possibly prepare
881 b4_define_silent([b4_value_type_setup],
882 [# Define default value.
883 b4_percent_define_ifdef([[api.value.type]], [],
884 [# %union => api.value.type=union-directive
885 m4_ifdef([b4_union_members],
886 [m4_define([b4_percent_define_kind(api.value.type)], [keyword])
887 m4_define([b4_percent_define(api.value.type)], [union-directive])],
888 [# no tag seen => api.value.type={int}
889 m4_if(b4_tag_seen_flag, 0,
890 [m4_define([b4_percent_define_kind(api.value.type)], [code])
891 m4_define([b4_percent_define(api.value.type)], [int])],
892 [# otherwise api.value.type=yystype
893 m4_define([b4_percent_define_kind(api.value.type)], [keyword])
894 m4_define([b4_percent_define(api.value.type)], [yystype])])])])
897 m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
898 [keyword\|string], [_b4_value_type_setup_keyword])
907 # b4_value_type_define
908 # --------------------
909 m4_define([b4_value_type_define],
910 [b4_value_type_setup[]dnl
912 m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
914 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
915 typedef ]b4_percent_define_get([[api.value.type]])[ ]b4_api_PREFIX[STYPE;
916 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1
917 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
920 [m4_bmatch(b4_percent_define_get([[api.value.type]]),
921 [union\|union-directive],
922 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
923 ]b4_percent_define_get_syncline([[api.value.union.name]])dnl
924 [union ]b4_percent_define_get([[api.value.union.name]])[
926 ]b4_user_union_members[
928 ]b4_percent_define_get_syncline([[api.value.union.name]])dnl
929 [typedef union ]b4_percent_define_get([[api.value.union.name]])[ ]b4_api_PREFIX[STYPE;
930 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1
931 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
936 # b4_location_type_define
937 # -----------------------
938 m4_define([b4_location_type_define],
939 [[/* Location type. */
940 ]b4_percent_define_ifdef([[api.location.type]],
941 [[typedef ]b4_percent_define_get([[api.location.type]])[ ]b4_api_PREFIX[LTYPE;
943 [[#if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
944 typedef struct ]b4_api_PREFIX[LTYPE ]b4_api_PREFIX[LTYPE;
945 struct ]b4_api_PREFIX[LTYPE
952 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
953 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
958 # b4_declare_yylstype
959 # -------------------
960 # Declarations that might either go into the header (if --defines) or
961 # in the parser body. Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
962 m4_define([b4_declare_yylstype],
963 [b4_value_type_define[]b4_locations_if([
964 b4_location_type_define])
966 b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
967 ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
973 m4_define([b4_YYDEBUG_define],
974 [[/* Debug traces. */
975 ]m4_if(b4_api_prefix, [yy],
977 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
979 [[#ifndef ]b4_api_PREFIX[DEBUG
982 # define ]b4_api_PREFIX[DEBUG 1
984 # define ]b4_api_PREFIX[DEBUG 0
986 # else /* ! defined YYDEBUG */
987 # define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[
988 # endif /* ! defined YYDEBUG */
989 #endif /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl
994 m4_define([b4_declare_yydebug],
996 #if ]b4_api_PREFIX[DEBUG
997 extern int ]b4_prefix[debug;
1001 # b4_yylloc_default_define
1002 # ------------------------
1003 # Define YYLLOC_DEFAULT.
1004 m4_define([b4_yylloc_default_define],
1005 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
1006 If N is 0, then set CURRENT to the empty location which ends
1007 the previous symbol: RHS[0] (always defined). */
1009 #ifndef YYLLOC_DEFAULT
1010 # define YYLLOC_DEFAULT(Current, Rhs, N) \
1014 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
1015 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
1016 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
1017 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
1021 (Current).first_line = (Current).last_line = \
1022 YYRHSLOC (Rhs, 0).last_line; \
1023 (Current).first_column = (Current).last_column = \
1024 YYRHSLOC (Rhs, 0).last_column; \
1030 # b4_yy_location_print_define
1031 # ---------------------------
1032 # Define YY_LOCATION_PRINT.
1033 m4_define([b4_yy_location_print_define],
1035 /* YY_LOCATION_PRINT -- Print the location on the stream.
1036 This macro was not mandated originally: define only if we know
1037 we won't break user code: when these are the locations we know. */
1039 # ifndef YY_LOCATION_PRINT
1040 # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1042 /* Print *YYLOCP on YYO. Private, do not rely on its existence. */
1046 yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp)
1049 int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
1050 if (0 <= yylocp->first_line)
1052 res += YYFPRINTF (yyo, "%d", yylocp->first_line);
1053 if (0 <= yylocp->first_column)
1054 res += YYFPRINTF (yyo, ".%d", yylocp->first_column);
1056 if (0 <= yylocp->last_line)
1058 if (yylocp->first_line < yylocp->last_line)
1060 res += YYFPRINTF (yyo, "-%d", yylocp->last_line);
1062 res += YYFPRINTF (yyo, ".%d", end_col);
1064 else if (0 <= end_col && yylocp->first_column < end_col)
1065 res += YYFPRINTF (yyo, "-%d", end_col);
1070 # define YY_LOCATION_PRINT(File, Loc) \
1071 yy_location_print_ (File, &(Loc))
1074 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
1076 # endif /* !defined YY_LOCATION_PRINT */]],
1077 [[/* This macro is provided for backward compatibility. */
1078 # ifndef YY_LOCATION_PRINT
1079 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
1085 # Expand to a possible default value for yylloc.
1086 m4_define([b4_yyloc_default],
1088 # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1090 m4_defn([b4_location_initial_line]),
1091 m4_defn([b4_location_initial_column]),
1092 m4_defn([b4_location_initial_line]),
1093 m4_defn([b4_location_initial_column]))[ }