yacc: remove support for YYPRINT
[bison.git] / data / skeletons / c.m4
bloba8e1a9d4a2d7f480f3be143b0a3e9eaca136f577
1                                                             -*- Autoconf -*-
3 # C M4 Macros for Bison.
5 # Copyright (C) 2002, 2004-2015, 2018-2021 Free Software Foundation,
6 # Inc.
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])
23 # b4_tocpp(STRING)
24 # ----------------
25 # Convert STRING into a valid C macro name.
26 m4_define([b4_tocpp],
27 [m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
30 # b4_cpp_guard(FILE)
31 # ------------------
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 ## ---------------- ##
52 ## Identification.  ##
53 ## ---------------- ##
55 # b4_identification
56 # -----------------
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
59 # api.push-pull.
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["
67 /* Skeleton name.  */
68 #define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
70 /* Pure parsers.  */
71 #define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
73 /* Push parsers.  */
74 #define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
76 /* Pull parsers.  */
77 #define YYPULL ]b4_pull_flag])[
78 ]])
81 ## ---------------- ##
82 ## Default values.  ##
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)])
95 # b4_prefix
96 # ---------
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 ## ------------------------ ##
110 # b4_lex_formals
111 # --------------
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)])
120 # b4_lex
121 # ------
122 # Call yylex.
123 m4_define([b4_lex],
124 [b4_function_call([yylex], [int], b4_lex_formals)])
127 # b4_user_args
128 # ------------
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)])])
138 # b4_user_formals
139 # ---------------
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)])])
145 # b4_parse_param
146 # --------------
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],
150 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
161 $3[]dnl
162 m4_popdef([$2])dnl
163 m4_popdef([$1])dnl
164 ])])
167 # b4_use(EXPR)
168 # ------------
169 # Pacify the compiler about some maybe unused value.
170 m4_define([b4_use],
171 [YY_USE ($1)])
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);
180 ])dnl
184 ## ------------ ##
185 ## Data Types.  ##
186 ## ------------ ##
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
192 # 'int' range.
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],
200                                                [int])])
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],
213                                                [int])])
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
220 [#ifdef short
221 # undef short
222 #endif
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 */
232 #  define YY_STDINT_H
233 # endif
234 #endif
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;
245 #else
246 typedef signed char yytype_int8;
247 #endif
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;
253 #else
254 typedef short yytype_int16;
255 #endif
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>.  */
262 #ifdef __hpux
263 # undef UINT_LEAST8_MAX
264 # undef UINT_LEAST16_MAX
265 # define UINT_LEAST8_MAX 255
266 # define UINT_LEAST16_MAX 65535
267 #endif
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;
276 #else
277 typedef short yytype_uint8;
278 #endif
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;
287 #else
288 typedef int yytype_uint16;
289 #endif]])
292 # b4_sizes_types_define
293 # ---------------------
294 # Define YYPTRDIFF_T/YYPTRDIFF_MAXIMUM, YYSIZE_T/YYSIZE_MAXIMUM,
295 # and YYSIZEOF.
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
302 #  ifndef ptrdiff_t
303 #   include <stddef.h> /* INFRINGES ON USER NAME SPACE */
304 #  endif
305 #  define YYPTRDIFF_T ptrdiff_t
306 #  define YYPTRDIFF_MAXIMUM PTRDIFF_MAX
307 # else
308 #  define YYPTRDIFF_T long
309 #  define YYPTRDIFF_MAXIMUM LONG_MAX
310 # endif
311 #endif
313 #ifndef YYSIZE_T
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
321 # else
322 #  define YYSIZE_T unsigned
323 # endif
324 #endif
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],
352        [[0]],
353        [(($2) == $4)])])
356 ## ----------------- ##
357 ## Compiler issues.  ##
358 ## ----------------- ##
360 # b4_attribute_define([noreturn])
361 # -------------------------------
362 # Provide portable compiler "attributes".  If "noreturn" is passed, define
363 # _Noreturn.
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__))
368 # else
369 #  define YY_ATTRIBUTE_PURE
370 # endif
371 #endif
373 #ifndef YY_ATTRIBUTE_UNUSED
374 # if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
375 #  define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__))
376 # else
377 #  define YY_ATTRIBUTE_UNUSED
378 # endif
379 #endif
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.
388 [#ifndef _Noreturn
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)
406 # else
407 #  define _Noreturn
408 # endif
409 #endif
411 ]])[/* Suppress unused-variable warnings by "using" E.  */
412 #if ! defined lint || defined __GNUC__
413 # define YY_USE(E) ((void) (E))
414 #else
415 # define YY_USE(E) /* empty */
416 #endif
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\"")
424 # else
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\"")
429 # endif
430 # define YY_IGNORE_MAYBE_UNINITIALIZED_END      \
431     _Pragma ("GCC diagnostic pop")
432 #else
433 # define YY_INITIAL_VALUE(Value) Value
434 #endif
435 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
436 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
437 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
438 #endif
439 #ifndef YY_INITIAL_VALUE
440 # define YY_INITIAL_VALUE(Value) /* Nothing. */
441 #endif
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")
449 #endif
450 #ifndef YY_IGNORE_USELESS_CAST_BEGIN
451 # define YY_IGNORE_USELESS_CAST_BEGIN
452 # define YY_IGNORE_USELESS_CAST_END
453 #endif
457 # b4_cast_define
458 # --------------
459 m4_define([b4_cast_define],
460 [# ifndef YY_CAST
461 #  ifdef __cplusplus
462 #   define YY_CAST(Type, Val) static_cast<Type> (Val)
463 #   define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast<Type> (Val)
464 #  else
465 #   define YY_CAST(Type, Val) ((Type) (Val))
466 #   define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val))
467 #  endif
468 # endif[]dnl
472 # b4_null_define
473 # --------------
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],
480 [# ifndef YY_NULLPTR
481 #  if defined __cplusplus
482 #   if 201103L <= __cplusplus
483 #    define YY_NULLPTR nullptr
484 #   else
485 #    define YY_NULLPTR 0
486 #   endif
487 #  else
488 #   define YY_NULLPTR ((void*)0)
489 #  endif
490 # endif[]dnl
494 # b4_null
495 # -------
496 # Return a null pointer constant.
497 m4_define([b4_null], [YY_NULLPTR])
501 ## ---------##
502 ## Values.  ##
503 ## ---------##
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[[]] =
512   $2
513 };dnl
517 ## ------------- ##
518 ## Token kinds.  ##
519 ## ------------- ##
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])])
540 # b4_token_defines
541 # ----------------
542 # Output the definition of the tokens.
543 m4_define([b4_token_defines],
544 [[/* Token kinds.  */
545 #define ]b4_symbol(empty, [id])[ -2
546 ]m4_join([
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])])])])
564 # b4_token_enums
565 # --------------
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
572   {
573     ]b4_symbol(empty, [id])[ = -2,
574 ]b4_symbol_foreach([b4_token_enum])dnl
575 [  };
576   typedef enum ]b4_api_prefix[tokentype ]b4_api_prefix[token_kind_t;
577 #endif
578 ]])])
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],
594 [[N_($1)]])
598 ## -------------- ##
599 ## Symbol kinds.  ##
600 ## -------------- ##
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]),
609                      [$1],
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],
620 [[/* Symbol kind.  */
621 enum yysymbol_kind_t
623   ]b4_symbol(empty, [kind_base])[ = -2,
624 ]b4_symbol_foreach([b4_symbol_enum])dnl
626 typedef enum yysymbol_kind_t yysymbol_kind_t;
627 ]])])
630 ## ----------------- ##
631 ## Semantic Values.  ##
632 ## ----------------- ##
635 # b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])
636 # ----------------------------------------------
637 # See README.
638 m4_define([b4_symbol_value],
639 [m4_ifval([$3],
640           [($1.$3)],
641           [m4_ifval([$2],
642                     [b4_symbol_if([$2], [has_type],
643                                   [($1.b4_symbol([$2], [type]))],
644                                   [$1])],
645                     [$1])])])
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],
658        [$#$1], [1], [void],
659                [m4_map_sep([b4_formal], [, ], [$@])])])
661 m4_define([b4_formal],
662 [$1])
666 ## --------------------- ##
667 ## Calling C functions.  ##
668 ## --------------------- ##
671 # b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
672 # -----------------------------------------------------------
673 # Call the function NAME with arguments NAME1, NAME2 etc.
674 m4_define([b4_function_call],
675 [$1 (b4_args(m4_shift2($@)))[]dnl
679 # b4_args([DECL1, NAME1], ...)
680 # ----------------------------
681 # Output the arguments NAME1, NAME2...
682 m4_define([b4_args],
683 [m4_map_sep([b4_arg], [, ], [$@])])
685 m4_define([b4_arg],
686 [$2])
689 ## ----------- ##
690 ## Synclines.  ##
691 ## ----------- ##
693 # b4_sync_start(LINE, FILE)
694 # -------------------------
695 m4_define([b4_sync_start], [[#]line $1 $2])
698 ## -------------- ##
699 ## User actions.  ##
700 ## -------------- ##
702 # b4_case(LABEL, STATEMENTS, [COMMENTS])
703 # --------------------------------------
704 m4_define([b4_case],
705 [  case $1:m4_ifval([$3], [ b4_comment([$3])])
707 b4_syncline([@oline@], [@ofile@])dnl
708     break;])
711 # b4_predicate_case(LABEL, CONDITIONS)
712 # ------------------------------------
713 m4_define([b4_predicate_case],
714 [  case $1:
715     if (! (
716 $2)) YYERROR;
717 b4_syncline([@oline@], [@ofile@])dnl
718     break;])
721 # b4_yydestruct_define
722 # --------------------
723 # Define the "yydestruct" function.
724 m4_define_default([b4_yydestruct_define],
725 [[/*-----------------------------------------------.
726 | Release the memory associated to this symbol.  |
727 `-----------------------------------------------*/
729 static void
730 yydestruct (const char *yymsg,
731             yysymbol_kind_t yykind, YYSTYPE *yyvaluep]b4_locations_if(dnl
732 [[, YYLTYPE *yylocationp]])[]b4_user_formals[)
734 ]b4_parse_param_use([yyvaluep], [yylocationp])dnl
735 [  if (!yymsg)
736     yymsg = "Deleting";
737   YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp);
739   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
740   ]b4_symbol_actions([destructor])[
741   YY_IGNORE_MAYBE_UNINITIALIZED_END
742 }]dnl
746 # b4_yy_symbol_print_define
747 # -------------------------
748 # Define the "yy_symbol_print" function.
749 m4_define_default([b4_yy_symbol_print_define],
751 /*-----------------------------------.
752 | Print this symbol's value on YYO.  |
753 `-----------------------------------*/
755 static void
756 yy_symbol_value_print (FILE *yyo,
757                        yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep]b4_locations_if(dnl
758 [[, YYLTYPE const * const yylocationp]])[]b4_user_formals[)
760   FILE *yyoutput = yyo;
761 ]b4_parse_param_use([yyoutput], [yylocationp])dnl
762 [  if (!yyvaluep)
763     return;]
764 b4_percent_code_get([[pre-printer]])dnl
765   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
766   b4_symbol_actions([printer])
767   YY_IGNORE_MAYBE_UNINITIALIZED_END
768 b4_percent_code_get([[post-printer]])dnl
772 /*---------------------------.
773 | Print this symbol on YYO.  |
774 `---------------------------*/
776 static void
777 yy_symbol_print (FILE *yyo,
778                  yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep]b4_locations_if(dnl
779 [[, YYLTYPE const * const yylocationp]])[]b4_user_formals[)
781   YYFPRINTF (yyo, "%s %s (",
782              yykind < YYNTOKENS ? "token" : "nterm", yysymbol_name (yykind));
784 ]b4_locations_if([  YY_LOCATION_PRINT (yyo, *yylocationp);
785   YYFPRINTF (yyo, ": ");
786 ])dnl
787 [  yy_symbol_value_print (yyo, yykind, yyvaluep]dnl
788 b4_locations_if([, yylocationp])[]b4_user_args[);
789   YYFPRINTF (yyo, ")");
790 }]dnl
794 ## ---------------- ##
795 ## api.value.type.  ##
796 ## ---------------- ##
799 # ---------------------- #
800 # api.value.type=union.  #
801 # ---------------------- #
803 # b4_symbol_type_register(SYMBOL-NUM)
804 # -----------------------------------
805 # Symbol SYMBOL-NUM has a type (for variant) instead of a type-tag.
806 # Extend the definition of %union's body (b4_union_members) with a
807 # field of that type, and extend the symbol's "type" field to point to
808 # the field name, instead of the type name.
809 m4_define([b4_symbol_type_register],
810 [m4_define([b4_symbol($1, type_tag)],
811            [b4_symbol_if([$1], [has_id],
812                          [b4_symbol([$1], [id])],
813                          [yykind_[]b4_symbol([$1], [number])])])dnl
814 m4_append([b4_union_members],
815 m4_expand([m4_format([  %-40s %s],
816                      m4_expand([b4_symbol([$1], [type]) b4_symbol([$1], [type_tag]);]),
817                      [b4_symbol_tag_comment([$1])])]))
821 # b4_type_define_tag(SYMBOL1-NUM, ...)
822 # ------------------------------------
823 # For the batch of symbols SYMBOL1-NUM... (which all have the same
824 # type), enhance the %union definition for each of them, and set
825 # there "type" field to the field tag name, instead of the type name.
826 m4_define([b4_type_define_tag],
827 [b4_symbol_if([$1], [has_type],
828               [m4_map([b4_symbol_type_register], [$@])])
832 # b4_symbol_value_union(VAL, SYMBOL-NUM, [TYPE])
833 # ----------------------------------------------
834 # Same of b4_symbol_value, but when api.value.type=union.
835 m4_define([b4_symbol_value_union],
836 [m4_ifval([$3],
837           [(*($3*)(&$1))],
838           [m4_ifval([$2],
839                     [b4_symbol_if([$2], [has_type],
840                                   [($1.b4_symbol([$2], [type_tag]))],
841                                   [$1])],
842                     [$1])])])
845 # b4_value_type_setup_union
846 # -------------------------
847 # Setup support for api.value.type=union.  Symbols are defined with a
848 # type instead of a union member name: build the corresponding union,
849 # and give the symbols their tag.
850 m4_define([b4_value_type_setup_union],
851 [m4_define([b4_union_members])
852 b4_type_foreach([b4_type_define_tag])
853 m4_copy_force([b4_symbol_value_union], [b4_symbol_value])
857 # -------------------------- #
858 # api.value.type = variant.  #
859 # -------------------------- #
861 # b4_value_type_setup_variant
862 # ---------------------------
863 # Setup support for api.value.type=variant.  By default, fail, specialized
864 # by other skeletons.
865 m4_define([b4_value_type_setup_variant],
866 [b4_complain_at(b4_percent_define_get_loc([[api.value.type]]),
867                 [['%s' does not support '%s']],
868                 [b4_skeleton],
869                 [%define api.value.type variant])])
872 # _b4_value_type_setup_keyword
873 # ----------------------------
874 # api.value.type is defined with a keyword/string syntax.  Check if
875 # that is properly defined, and prepare its use.
876 m4_define([_b4_value_type_setup_keyword],
877 [b4_percent_define_check_values([[[[api.value.type]],
878                                   [[none]],
879                                   [[union]],
880                                   [[union-directive]],
881                                   [[variant]],
882                                   [[yystype]]]])dnl
883 m4_case(b4_percent_define_get([[api.value.type]]),
884         [union],   [b4_value_type_setup_union],
885         [variant], [b4_value_type_setup_variant])])
888 # b4_value_type_setup
889 # -------------------
890 # Check if api.value.type is properly defined, and possibly prepare
891 # its use.
892 b4_define_silent([b4_value_type_setup],
893 [# Define default value.
894 b4_percent_define_ifdef([[api.value.type]], [],
895 [# %union => api.value.type=union-directive
896 m4_ifdef([b4_union_members],
897 [m4_define([b4_percent_define_kind(api.value.type)], [keyword])
898 m4_define([b4_percent_define(api.value.type)], [union-directive])],
899 [# no tag seen => api.value.type={int}
900 m4_if(b4_tag_seen_flag, 0,
901 [m4_define([b4_percent_define_kind(api.value.type)], [code])
902 m4_define([b4_percent_define(api.value.type)], [int])],
903 [# otherwise api.value.type=yystype
904 m4_define([b4_percent_define_kind(api.value.type)], [keyword])
905 m4_define([b4_percent_define(api.value.type)], [yystype])])])])
907 # Set up.
908 m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
909    [keyword\|string], [_b4_value_type_setup_keyword])
913 ## -------------- ##
914 ## Declarations.  ##
915 ## -------------- ##
918 # b4_value_type_define
919 # --------------------
920 m4_define([b4_value_type_define],
921 [b4_value_type_setup[]dnl
922 /* Value type.  */
923 m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
924 [code],
925 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
926 typedef ]b4_percent_define_get([[api.value.type]])[ ]b4_api_PREFIX[STYPE;
927 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1
928 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
929 #endif
931 [m4_bmatch(b4_percent_define_get([[api.value.type]]),
932 [union\|union-directive],
933 [[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
934 ]b4_percent_define_get_syncline([[api.value.union.name]])dnl
935 [union ]b4_percent_define_get([[api.value.union.name]])[
937 ]b4_user_union_members[
939 ]b4_percent_define_get_syncline([[api.value.union.name]])dnl
940 [typedef union ]b4_percent_define_get([[api.value.union.name]])[ ]b4_api_PREFIX[STYPE;
941 # define ]b4_api_PREFIX[STYPE_IS_TRIVIAL 1
942 # define ]b4_api_PREFIX[STYPE_IS_DECLARED 1
943 #endif
944 ]])])])
947 # b4_location_type_define
948 # -----------------------
949 m4_define([b4_location_type_define],
950 [[/* Location type.  */
951 ]b4_percent_define_ifdef([[api.location.type]],
952 [[typedef ]b4_percent_define_get([[api.location.type]])[ ]b4_api_PREFIX[LTYPE;
954 [[#if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
955 typedef struct ]b4_api_PREFIX[LTYPE ]b4_api_PREFIX[LTYPE;
956 struct ]b4_api_PREFIX[LTYPE
958   int first_line;
959   int first_column;
960   int last_line;
961   int last_column;
963 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
964 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
965 #endif
966 ]])])
969 # b4_declare_yylstype
970 # -------------------
971 # Declarations that might either go into the header (if --header) or
972 # in the parser body.  Declare YYSTYPE/YYLTYPE, and yylval/yylloc.
973 m4_define([b4_declare_yylstype],
974 [b4_value_type_define[]b4_locations_if([
975 b4_location_type_define])
977 b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
978 ]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
982 # b4_YYDEBUG_define
983 # -----------------
984 m4_define([b4_YYDEBUG_define],
985 [[/* Debug traces.  */
986 ]m4_if(b4_api_prefix, [yy],
987 [[#ifndef YYDEBUG
988 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
989 #endif]],
990 [[#ifndef ]b4_api_PREFIX[DEBUG
991 # if defined YYDEBUG
992 #if YYDEBUG
993 #   define ]b4_api_PREFIX[DEBUG 1
994 #  else
995 #   define ]b4_api_PREFIX[DEBUG 0
996 #  endif
997 # else /* ! defined YYDEBUG */
998 #  define ]b4_api_PREFIX[DEBUG ]b4_parse_trace_if([1], [0])[
999 # endif /* ! defined YYDEBUG */
1000 #endif  /* ! defined ]b4_api_PREFIX[DEBUG */]])[]dnl
1003 # b4_declare_yydebug
1004 # ------------------
1005 m4_define([b4_declare_yydebug],
1006 [b4_YYDEBUG_define[
1007 #if ]b4_api_PREFIX[DEBUG
1008 extern int ]b4_prefix[debug;
1009 #endif][]dnl
1012 # b4_yylloc_default_define
1013 # ------------------------
1014 # Define YYLLOC_DEFAULT.
1015 m4_define([b4_yylloc_default_define],
1016 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
1017    If N is 0, then set CURRENT to the empty location which ends
1018    the previous symbol: RHS[0] (always defined).  */
1020 #ifndef YYLLOC_DEFAULT
1021 # define YYLLOC_DEFAULT(Current, Rhs, N)                                \
1022     do                                                                  \
1023       if (N)                                                            \
1024         {                                                               \
1025           (Current).first_line   = YYRHSLOC (Rhs, 1).first_line;        \
1026           (Current).first_column = YYRHSLOC (Rhs, 1).first_column;      \
1027           (Current).last_line    = YYRHSLOC (Rhs, N).last_line;         \
1028           (Current).last_column  = YYRHSLOC (Rhs, N).last_column;       \
1029         }                                                               \
1030       else                                                              \
1031         {                                                               \
1032           (Current).first_line   = (Current).last_line   =              \
1033             YYRHSLOC (Rhs, 0).last_line;                                \
1034           (Current).first_column = (Current).last_column =              \
1035             YYRHSLOC (Rhs, 0).last_column;                              \
1036         }                                                               \
1037     while (0)
1038 #endif
1041 # b4_yy_location_print_define
1042 # ---------------------------
1043 # Define YY_LOCATION_PRINT.
1044 m4_define([b4_yy_location_print_define],
1045 [b4_locations_if([[
1046 /* YY_LOCATION_PRINT -- Print the location on the stream.
1047    This macro was not mandated originally: define only if we know
1048    we won't break user code: when these are the locations we know.  */
1050 # ifndef YY_LOCATION_PRINT
1051 #  if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1053 /* Print *YYLOCP on YYO.  Private, do not rely on its existence. */
1055 YY_ATTRIBUTE_UNUSED
1056 static int
1057 yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp)
1059   int res = 0;
1060   int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
1061   if (0 <= yylocp->first_line)
1062     {
1063       res += YYFPRINTF (yyo, "%d", yylocp->first_line);
1064       if (0 <= yylocp->first_column)
1065         res += YYFPRINTF (yyo, ".%d", yylocp->first_column);
1066     }
1067   if (0 <= yylocp->last_line)
1068     {
1069       if (yylocp->first_line < yylocp->last_line)
1070         {
1071           res += YYFPRINTF (yyo, "-%d", yylocp->last_line);
1072           if (0 <= end_col)
1073             res += YYFPRINTF (yyo, ".%d", end_col);
1074         }
1075       else if (0 <= end_col && yylocp->first_column < end_col)
1076         res += YYFPRINTF (yyo, "-%d", end_col);
1077     }
1078   return res;
1081 #   define YY_LOCATION_PRINT(File, Loc)          \
1082   yy_location_print_ (File, &(Loc))
1084 #  else
1085 #   define YY_LOCATION_PRINT(File, Loc) ((void) 0)
1086 #  endif
1087 # endif /* !defined YY_LOCATION_PRINT */]],
1088 [[/* This macro is provided for backward compatibility. */
1089 # ifndef YY_LOCATION_PRINT
1090 #  define YY_LOCATION_PRINT(File, Loc) ((void) 0)
1091 # endif]])
1094 # b4_yyloc_default
1095 # ----------------
1096 # Expand to a possible default value for yylloc.
1097 m4_define([b4_yyloc_default],
1099 # if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1100   = { ]m4_join([, ],
1101                m4_defn([b4_location_initial_line]),
1102                m4_defn([b4_location_initial_column]),
1103                m4_defn([b4_location_initial_line]),
1104                m4_defn([b4_location_initial_column]))[ }
1105 # endif