yacc: comply with recent POSIX updates: declare yyerror and yylex
[bison.git] / data / skeletons / c.m4
blob2a992f515dda4e7bf548b0c2e9a42da3dc327570
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([[[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)])
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])
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...
690 m4_define([b4_args],
691 [m4_map_sep([b4_arg], [, ], [$@])])
693 m4_define([b4_arg],
694 [$2])
697 ## ----------- ##
698 ## Synclines.  ##
699 ## ----------- ##
701 # b4_sync_start(LINE, FILE)
702 # -------------------------
703 m4_define([b4_sync_start], [[#]line $1 $2])
706 ## -------------- ##
707 ## User actions.  ##
708 ## -------------- ##
710 # b4_case(LABEL, STATEMENTS, [COMMENTS])
711 # --------------------------------------
712 m4_define([b4_case],
713 [  case $1:m4_ifval([$3], [ b4_comment([$3])])
715 b4_syncline([@oline@], [@ofile@])dnl
716     break;])
719 # b4_predicate_case(LABEL, CONDITIONS)
720 # ------------------------------------
721 m4_define([b4_predicate_case],
722 [  case $1:
723     if (! (
724 $2)) YYERROR;
725 b4_syncline([@oline@], [@ofile@])dnl
726     break;])
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 `-----------------------------------------------*/
737 static void
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
743 [  if (!yymsg)
744     yymsg = "Deleting";
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
750 }]dnl
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 `-----------------------------------*/
763 static void
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
770 [  if (!yyvaluep)
771     return;]
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 `---------------------------*/
784 static void
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, ": ");
794 ])dnl
795 [  yy_symbol_value_print (yyo, yykind, yyvaluep]dnl
796 b4_locations_if([, yylocationp])[]b4_user_args[);
797   YYFPRINTF (yyo, ")");
798 }]dnl
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],
844 [m4_ifval([$3],
845           [(*($3*)(&$1))],
846           [m4_ifval([$2],
847                     [b4_symbol_if([$2], [has_type],
848                                   [($1.b4_symbol([$2], [type_tag]))],
849                                   [$1])],
850                     [$1])])])
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']],
876                 [b4_skeleton],
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]],
886                                   [[none]],
887                                   [[union]],
888                                   [[union-directive]],
889                                   [[variant]],
890                                   [[yystype]]]])dnl
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
899 # its use.
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])])])])
915 # Set up.
916 m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
917    [keyword\|string], [_b4_value_type_setup_keyword])
921 ## -------------- ##
922 ## Declarations.  ##
923 ## -------------- ##
926 # b4_value_type_define
927 # --------------------
928 m4_define([b4_value_type_define],
929 [b4_value_type_setup[]dnl
930 /* Value type.  */
931 m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
932 [code],
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
937 #endif
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
951 #endif
952 ]])])])
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
966   int first_line;
967   int first_column;
968   int last_line;
969   int last_column;
971 # define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
972 # define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
973 #endif
974 ]])])
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
990 # b4_YYDEBUG_define
991 # -----------------
992 m4_define([b4_YYDEBUG_define],
993 [[/* Debug traces.  */
994 ]m4_if(b4_api_prefix, [yy],
995 [[#ifndef YYDEBUG
996 # define YYDEBUG ]b4_parse_trace_if([1], [0])[
997 #endif]],
998 [[#ifndef ]b4_api_PREFIX[DEBUG
999 # if defined YYDEBUG
1000 #if YYDEBUG
1001 #   define ]b4_api_PREFIX[DEBUG 1
1002 #  else
1003 #   define ]b4_api_PREFIX[DEBUG 0
1004 #  endif
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],
1014 [b4_YYDEBUG_define[
1015 #if ]b4_api_PREFIX[DEBUG
1016 extern int ]b4_prefix[debug;
1017 #endif][]dnl
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)                                \
1030     do                                                                  \
1031       if (N)                                                            \
1032         {                                                               \
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;       \
1037         }                                                               \
1038       else                                                              \
1039         {                                                               \
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;                              \
1044         }                                                               \
1045     while (0)
1046 #endif
1049 # b4_yylocation_print_define
1050 # --------------------------
1051 # Define YYLOCATION_PRINT.
1052 m4_define([b4_yylocation_print_define],
1053 [b4_locations_if([[
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. */
1070 YY_ATTRIBUTE_UNUSED
1071 static int
1072 yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp)
1074   int res = 0;
1075   int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
1076   if (0 <= yylocp->first_line)
1077     {
1078       res += YYFPRINTF (yyo, "%d", yylocp->first_line);
1079       if (0 <= yylocp->first_column)
1080         res += YYFPRINTF (yyo, ".%d", yylocp->first_column);
1081     }
1082   if (0 <= yylocp->last_line)
1083     {
1084       if (yylocp->first_line < yylocp->last_line)
1085         {
1086           res += YYFPRINTF (yyo, "-%d", yylocp->last_line);
1087           if (0 <= end_col)
1088             res += YYFPRINTF (yyo, ".%d", end_col);
1089         }
1090       else if (0 <= end_col && yylocp->first_column < end_col)
1091         res += YYFPRINTF (yyo, "-%d", end_col);
1092     }
1093   return res;
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))
1102 #  else
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
1109 #  endif
1110 # endif /* !defined YYLOCATION_PRINT */]])
1113 # b4_yyloc_default
1114 # ----------------
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
1119   = { ]m4_join([, ],
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]))[ }
1124 # endif