1                                                            -*- Autoconf -*-
2
3# C M4 Macros for Bison.
4
5# Copyright (C) 2002, 2004-2015, 2018-2021 Free Software Foundation,
6# Inc.
7
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.
12#
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.
17#
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/>.
20
21m4_include(b4_skeletonsdir/[c-like.m4])
22
23# b4_tocpp(STRING)
24# ----------------
25# Convert STRING into a valid C macro name.
26m4_define([b4_tocpp],
27[m4_toupper(m4_bpatsubst(m4_quote($1), [[^a-zA-Z0-9]+], [_]))])
28
29
30# b4_cpp_guard(FILE)
31# ------------------
32# A valid C macro name to use as a CPP header guard for FILE.
33m4_define([b4_cpp_guard],
34[[YY_]b4_tocpp(m4_defn([b4_prefix])/[$1])[_INCLUDED]])
35
36
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.
41m4_define([b4_cpp_guard_open],
42[m4_ifval(m4_quote($1),
43[#ifndef b4_cpp_guard([$1])
44# define b4_cpp_guard([$1])])])
45
46m4_define([b4_cpp_guard_close],
47[m4_ifval(m4_quote($1),
48[#endif b4_comment([!b4_cpp_guard([$1])])])])
49
50
51## ---------------- ##
52## Identification.  ##
53## ---------------- ##
54
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.
60m4_define([b4_identification],
61[[/* Identify Bison output, and Bison version.  */
62#define YYBISON ]b4_version[
63
64/* Bison version string.  */
65#define YYBISON_VERSION "]b4_version_string["
66
67/* Skeleton name.  */
68#define YYSKELETON_NAME ]b4_skeleton[]m4_ifdef([b4_pure_flag], [[
69
70/* Pure parsers.  */
71#define YYPURE ]b4_pure_flag])[]m4_ifdef([b4_push_flag], [[
72
73/* Push parsers.  */
74#define YYPUSH ]b4_push_flag])[]m4_ifdef([b4_pull_flag], [[
75
76/* Pull parsers.  */
77#define YYPULL ]b4_pull_flag])[
78]])
79
80
81## ---------------- ##
82## Default values.  ##
83## ---------------- ##
84
85# b4_api_prefix, b4_api_PREFIX
86# ----------------------------
87# Corresponds to %define api.prefix
88b4_percent_define_default([[api.prefix]], [[yy]])
89m4_define([b4_api_prefix],
90[b4_percent_define_get([[api.prefix]])])
91m4_define([b4_api_PREFIX],
92[m4_toupper(b4_api_prefix)])
93
94
95# b4_prefix
96# ---------
97# If the %name-prefix is not given, it is api.prefix.
98m4_define_default([b4_prefix], [b4_api_prefix])
99
100# If the %union is not named, its name is YYSTYPE.
101b4_percent_define_default([[api.value.union.name]],
102                          [b4_api_PREFIX[][STYPE]])
103
104b4_percent_define_default([[api.symbol.prefix]], [[YYSYMBOL_]])
105
106## ------------------------ ##
107## Pure/impure interfaces.  ##
108## ------------------------ ##
109
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.
114m4_define([b4_lex_formals],
115[b4_pure_if([[[[YYSTYPE *yylvalp]], [[&yylval]]][]dnl
116b4_locations_if([, [[YYLTYPE *yyllocp], [&yylloc]]])])dnl
117m4_ifdef([b4_lex_param], [, ]b4_lex_param)])
118
119
120# b4_lex
121# ------
122# Call yylex.
123m4_define([b4_lex],
124[b4_function_call([yylex], [int], b4_lex_formals)])
125
126
127# b4_user_args
128# ------------
129m4_define([b4_user_args],
130[m4_ifset([b4_parse_param], [, b4_args(b4_parse_param)])])
131
132
133# b4_user_formals
134# ---------------
135# The possible parse-params formal arguments preceded by a comma.
136m4_define([b4_user_formals],
137[m4_ifset([b4_parse_param], [, b4_formals(b4_parse_param)])])
138
139
140# b4_parse_param
141# --------------
142# If defined, b4_parse_param arrives double quoted, but below we prefer
143# it to be single quoted.
144m4_define([b4_parse_param],
145b4_parse_param)
146
147
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.
152m4_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
155m4_pushdef([$2], m4_shift($1_$2))dnl
156$3[]dnl
157m4_popdef([$2])dnl
158m4_popdef([$1])dnl
159])])
160
161# b4_parse_param_use([VAL], [LOC])
162# --------------------------------
163# 'YY_USE' VAL, LOC if locations are enabled, and all the parse-params.
164m4_define([b4_parse_param_use],
165[m4_ifvaln([$1], [  YY_USE ([$1]);])dnl
166b4_locations_if([m4_ifvaln([$2], [  YY_USE ([$2]);])])dnl
167b4_parse_param_for([Decl], [Formal], [  YY_USE (Formal);
168])dnl
169])
170
171
172## ------------ ##
173## Data Types.  ##
174## ------------ ##
175
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
180# 'int' range.
181m4_define([b4_int_type],
182[m4_if(b4_ints_in($@,   [-127],   [127]), [1], [signed char],
183       b4_ints_in($@,      [0],   [255]), [1], [unsigned char],
184
185       b4_ints_in($@, [-32767], [32767]), [1], [short],
186       b4_ints_in($@,      [0], [65535]), [1], [unsigned short],
187
188                                               [int])])
189
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.
194m4_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],
197
198       b4_ints_in($@, [-32767], [32767]), [1], [yytype_int16],
199       b4_ints_in($@,      [0], [65535]), [1], [yytype_uint16],
200
201                                               [int])])
202
203# b4_c99_int_type_define
204# ----------------------
205# Define private types suitable for holding small integers in C99 or later.
206m4_define([b4_c99_int_type_define],
207[m4_copy_force([b4_c99_int_type], [b4_int_type])dnl
208[#ifdef short
209# undef short
210#endif
211
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.  */
215
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 */
220#  define YY_STDINT_H
221# endif
222#endif
223
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.  */
228
229#ifdef __INT_LEAST8_MAX__
230typedef __INT_LEAST8_TYPE__ yytype_int8;
231#elif defined YY_STDINT_H
232typedef int_least8_t yytype_int8;
233#else
234typedef signed char yytype_int8;
235#endif
236
237#ifdef __INT_LEAST16_MAX__
238typedef __INT_LEAST16_TYPE__ yytype_int16;
239#elif defined YY_STDINT_H
240typedef int_least16_t yytype_int16;
241#else
242typedef short yytype_int16;
243#endif
244
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>.  */
250#ifdef __hpux
251# undef UINT_LEAST8_MAX
252# undef UINT_LEAST16_MAX
253# define UINT_LEAST8_MAX 255
254# define UINT_LEAST16_MAX 65535
255#endif
256
257#if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__
258typedef __UINT_LEAST8_TYPE__ yytype_uint8;
259#elif (!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H \
260       && UINT_LEAST8_MAX <= INT_MAX)
261typedef uint_least8_t yytype_uint8;
262#elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX
263typedef unsigned char yytype_uint8;
264#else
265typedef short yytype_uint8;
266#endif
267
268#if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__
269typedef __UINT_LEAST16_TYPE__ yytype_uint16;
270#elif (!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H \
271       && UINT_LEAST16_MAX <= INT_MAX)
272typedef uint_least16_t yytype_uint16;
273#elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX
274typedef unsigned short yytype_uint16;
275#else
276typedef int yytype_uint16;
277#endif]])
278
279
280# b4_sizes_types_define
281# ---------------------
282# Define YYPTRDIFF_T/YYPTRDIFF_MAXIMUM, YYSIZE_T/YYSIZE_MAXIMUM,
283# and YYSIZEOF.
284m4_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
290#  ifndef ptrdiff_t
291#   include <stddef.h> /* INFRINGES ON USER NAME SPACE */
292#  endif
293#  define YYPTRDIFF_T ptrdiff_t
294#  define YYPTRDIFF_MAXIMUM PTRDIFF_MAX
295# else
296#  define YYPTRDIFF_T long
297#  define YYPTRDIFF_MAXIMUM LONG_MAX
298# endif
299#endif
300
301#ifndef YYSIZE_T
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
309# else
310#  define YYSIZE_T unsigned
311# endif
312#endif
313
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)))
319
320#define YYSIZEOF(X) YY_CAST (YYPTRDIFF_T, sizeof (X))
321]])
322
323
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).
328m4_define([b4_int_type_for],
329[b4_int_type($1_min, $1_max)])
330
331
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
337m4_define([b4_table_value_equals],
338[m4_if(m4_eval($3 < m4_indir([b4_]$1[_min])
339               || m4_indir([b4_]$1[_max]) < $3), [1],
340       [[0]],
341       [(($2) == $4)])])
342
343
344## ----------------- ##
345## Compiler issues.  ##
346## ----------------- ##
347
348# b4_attribute_define([noreturn])
349# -------------------------------
350# Provide portable compiler "attributes".  If "noreturn" is passed, define
351# _Noreturn.
352m4_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__))
356# else
357#  define YY_ATTRIBUTE_PURE
358# endif
359#endif
360
361#ifndef YY_ATTRIBUTE_UNUSED
362# if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
363#  define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__))
364# else
365#  define YY_ATTRIBUTE_UNUSED
366# endif
367#endif
368
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
371dnl the use of [[noreturn]], because _Noreturn is used in places
372dnl where [[noreturn]] works in C++.  We need this in particular
373dnl because of glr.cc which compiles code from glr.c in C++.
374dnl And the C++ compiler chokes on _Noreturn.  Also, we do not
375dnl use C' _Noreturn in C++, to avoid -Wc11-extensions warnings.
376[#ifndef _Noreturn
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)
394# else
395#  define _Noreturn
396# endif
397#endif
398
399]])[/* Suppress unused-variable warnings by "using" E.  */
400#if ! defined lint || defined __GNUC__
401# define YY_USE(E) ((void) (E))
402#else
403# define YY_USE(E) /* empty */
404#endif
405
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")
414#else
415# define YY_INITIAL_VALUE(Value) Value
416#endif
417#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
418# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
419# define YY_IGNORE_MAYBE_UNINITIALIZED_END
420#endif
421#ifndef YY_INITIAL_VALUE
422# define YY_INITIAL_VALUE(Value) /* Nothing. */
423#endif
424
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")
431#endif
432#ifndef YY_IGNORE_USELESS_CAST_BEGIN
433# define YY_IGNORE_USELESS_CAST_BEGIN
434# define YY_IGNORE_USELESS_CAST_END
435#endif
436]])
437
438
439# b4_cast_define
440# --------------
441m4_define([b4_cast_define],
442[# ifndef YY_CAST
443#  ifdef __cplusplus
444#   define YY_CAST(Type, Val) static_cast<Type> (Val)
445#   define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast<Type> (Val)
446#  else
447#   define YY_CAST(Type, Val) ((Type) (Val))
448#   define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val))
449#  endif
450# endif[]dnl
451])
452
453
454# b4_null_define
455# --------------
456# Portability issues: define a YY_NULLPTR appropriate for the current
457# language (C, C++98, or C++11).
458#
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.
461m4_define([b4_null_define],
462[# ifndef YY_NULLPTR
463#  if defined __cplusplus
464#   if 201103L <= __cplusplus
465#    define YY_NULLPTR nullptr
466#   else
467#    define YY_NULLPTR 0
468#   endif
469#  else
470#   define YY_NULLPTR ((void*)0)
471#  endif
472# endif[]dnl
473])
474
475
476# b4_null
477# -------
478# Return a null pointer constant.
479m4_define([b4_null], [YY_NULLPTR])
480
481
482
483## ---------##
484## Values.  ##
485## ---------##
486
487# b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
488# -------------------------------------------------------------
489# Define "yy<TABLE-NAME>" whose contents is CONTENT.
490m4_define([b4_integral_parser_table_define],
491[m4_ifvaln([$3], [b4_comment([$3], [  ])])dnl
492static const b4_int_type_for([$2]) yy$1[[]] =
493{
494  $2
495};dnl
496])
497
498
499## ------------- ##
500## Token kinds.  ##
501## ------------- ##
502
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.
507m4_define([b4_symbol(-2, id)],  [b4_api_PREFIX[][EMPTY]])
508m4_define([b4_symbol(-2, tag)], [[No symbol.]])
509
510m4_if(b4_symbol(0, id), [YYEOF],
511     [m4_define([b4_symbol(0, id)],  [b4_api_PREFIX[][EOF]])])
512m4_define([b4_symbol(1, id)],  [b4_api_PREFIX[][error]])
513m4_define([b4_symbol(2, id)],  [b4_api_PREFIX[][UNDEF]])
514
515
516# b4_token_define(TOKEN-NUM)
517# --------------------------
518# Output the definition of this token as #define.
519m4_define([b4_token_define],
520[b4_token_format([#define %s %s], [$1])])
521
522# b4_token_defines
523# ----------------
524# Output the definition of the tokens.
525m4_define([b4_token_defines],
526[[/* Token kinds.  */
527#define ]b4_symbol([-2], [id])[ -2
528]m4_join([
529], b4_symbol_map([b4_token_define]))
530])
531
532
533# b4_token_enum(TOKEN-NUM)
534# ------------------------
535# Output the definition of this token as an enum.
536m4_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])])])])
544
545
546# b4_token_enums
547# --------------
548# The definition of the token kinds.
549m4_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
554  {
555    ]b4_symbol([-2], [id])[ = -2,
556]b4_symbol_foreach([b4_token_enum])dnl
557[  };
558  typedef enum ]b4_api_prefix[tokentype ]b4_api_prefix[token_kind_t;
559#endif
560]])])
561
562
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.
567m4_define([b4_token_enums_defines],
568[b4_token_enums[]b4_yacc_if([b4_token_defines])])
569
570
571# b4_symbol_translate(STRING)
572# ---------------------------
573# Used by "bison" in the array of symbol names to mark those that
574# require translation.
575m4_define([b4_symbol_translate],
576[[N_($1)]])
577
578
579
580## -------------- ##
581## Symbol kinds.  ##
582## -------------- ##
583
584# b4_symbol_enum(SYMBOL-NUM)
585# --------------------------
586# Output the definition of this symbol as an enum.
587m4_define([b4_symbol_enum],
588[m4_format([  %-40s %s],
589           m4_format([[%s = %s%s%s]],
590                     b4_symbol([$1], [kind_base]),
591                     [$1],
592                     m4_if([$1], b4_last_symbol, [], [[,]])),
593           [b4_symbol_tag_comment([$1])])])
594
595
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.
601m4_define([b4_declare_symbol_enum],
602[[/* Symbol kind.  */
603enum yysymbol_kind_t
604{
605  ]b4_symbol([-2], kind_base)[ = -2,
606]b4_symbol_foreach([b4_symbol_enum])dnl
607[};
608typedef enum yysymbol_kind_t yysymbol_kind_t;
609]])])
610
611
612## ----------------- ##
613## Semantic Values.  ##
614## ----------------- ##
615
616
617# b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])
618# ----------------------------------------------
619# See README.
620m4_define([b4_symbol_value],
621[m4_ifval([$3],
622          [($1.$3)],
623          [m4_ifval([$2],
624                    [b4_symbol_if([$2], [has_type],
625                                  [($1.b4_symbol([$2], [type]))],
626                                  [$1])],
627                    [$1])])])
628
629
630## ---------------------- ##
631## Defining C functions.  ##
632## ---------------------- ##
633
634
635# b4_formals([DECL1, NAME1], ...)
636# -------------------------------
637# The formal arguments of a C function definition.
638m4_define([b4_formals],
639[m4_if([$#], [0], [void],
640       [$#$1], [1], [void],
641               [m4_map_sep([b4_formal], [, ], [$@])])])
642
643m4_define([b4_formal],
644[$1])
645
646
647
648## --------------------- ##
649## Calling C functions.  ##
650## --------------------- ##
651
652
653# b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
654# -----------------------------------------------------------
655# Call the function NAME with arguments NAME1, NAME2 etc.
656m4_define([b4_function_call],
657[$1 (b4_args(m4_shift2($@)))[]dnl
658])
659
660
661# b4_args([DECL1, NAME1], ...)
662# ----------------------------
663# Output the arguments NAME1, NAME2...
664m4_define([b4_args],
665[m4_map_sep([b4_arg], [, ], [$@])])
666
667m4_define([b4_arg],
668[$2])
669
670
671## ----------- ##
672## Synclines.  ##
673## ----------- ##
674
675# b4_sync_start(LINE, FILE)
676# -------------------------
677m4_define([b4_sync_start], [[#]line $1 $2])
678
679
680## -------------- ##
681## User actions.  ##
682## -------------- ##
683
684# b4_case(LABEL, STATEMENTS, [COMMENTS])
685# --------------------------------------
686m4_define([b4_case],
687[  case $1:m4_ifval([$3], [ b4_comment([$3])])
688$2
689b4_syncline([@oline@], [@ofile@])dnl
690    break;])
691
692
693# b4_predicate_case(LABEL, CONDITIONS)
694# ------------------------------------
695m4_define([b4_predicate_case],
696[  case $1:
697    if (! (
698$2)) YYERROR;
699b4_syncline([@oline@], [@ofile@])dnl
700    break;])
701
702
703# b4_yydestruct_define
704# --------------------
705# Define the "yydestruct" function.
706m4_define_default([b4_yydestruct_define],
707[[/*-----------------------------------------------.
708| Release the memory associated to this symbol.  |
709`-----------------------------------------------*/
710
711static void
712yydestruct (const char *yymsg,
713            yysymbol_kind_t yykind, YYSTYPE *yyvaluep]b4_locations_if(dnl
714[[, YYLTYPE *yylocationp]])[]b4_user_formals[)
715{
716]b4_parse_param_use([yyvaluep], [yylocationp])dnl
717[  if (!yymsg)
718    yymsg = "Deleting";
719  YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp);
720
721  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
722  ]b4_symbol_actions([destructor])[
723  YY_IGNORE_MAYBE_UNINITIALIZED_END
724}]dnl
725])
726
727
728# b4_yy_symbol_print_define
729# -------------------------
730# Define the "yy_symbol_print" function.
731m4_define_default([b4_yy_symbol_print_define],
732[[
733/*-----------------------------------.
734| Print this symbol's value on YYO.  |
735`-----------------------------------*/
736
737static void
738yy_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[)
741{
742  FILE *yyoutput = yyo;
743]b4_parse_param_use([yyoutput], [yylocationp])dnl
744[  if (!yyvaluep)
745    return;]
746dnl glr.c does not feature yytoknum.
747m4_if(b4_skeleton, ["yacc.c"],
748[[# ifdef YYPRINT
749  if (yykind < YYNTOKENS)
750    YYPRINT (yyo, yytoknum[yykind], *yyvaluep);
751# endif
752]])dnl
753b4_percent_code_get([[pre-printer]])dnl
754  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
755  b4_symbol_actions([printer])
756  YY_IGNORE_MAYBE_UNINITIALIZED_END
757b4_percent_code_get([[post-printer]])dnl
758[}
759
760
761/*---------------------------.
762| Print this symbol on YYO.  |
763`---------------------------*/
764
765static void
766yy_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[)
769{
770  YYFPRINTF (yyo, "%s %s (",
771             yykind < YYNTOKENS ? "token" : "nterm", yysymbol_name (yykind));
772
773]b4_locations_if([  YY_LOCATION_PRINT (yyo, *yylocationp);
774  YYFPRINTF (yyo, ": ");
775])dnl
776[  yy_symbol_value_print (yyo, yykind, yyvaluep]dnl
777b4_locations_if([, yylocationp])[]b4_user_args[);
778  YYFPRINTF (yyo, ")");
779}]dnl
780])
781
782
783## ---------------- ##
784## api.value.type.  ##
785## ---------------- ##
786
787
788# ---------------------- #
789# api.value.type=union.  #
790# ---------------------- #
791
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.
798m4_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
803m4_append([b4_union_members],
804m4_expand([m4_format([  %-40s %s],
805                     m4_expand([b4_symbol([$1], [type]) b4_symbol([$1], [type_tag]);]),
806                     [b4_symbol_tag_comment([$1])])]))
807])
808
809
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.
815m4_define([b4_type_define_tag],
816[b4_symbol_if([$1], [has_type],
817              [m4_map([b4_symbol_type_register], [$@])])
818])
819
820
821# b4_symbol_value_union(VAL, SYMBOL-NUM, [TYPE])
822# ----------------------------------------------
823# Same of b4_symbol_value, but when api.value.type=union.
824m4_define([b4_symbol_value_union],
825[m4_ifval([$3],
826          [(*($3*)(&$1))],
827          [m4_ifval([$2],
828                    [b4_symbol_if([$2], [has_type],
829                                  [($1.b4_symbol([$2], [type_tag]))],
830                                  [$1])],
831                    [$1])])])
832
833
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.
839m4_define([b4_value_type_setup_union],
840[m4_define([b4_union_members])
841b4_type_foreach([b4_type_define_tag])
842m4_copy_force([b4_symbol_value_union], [b4_symbol_value])
843])
844
845
846# -------------------------- #
847# api.value.type = variant.  #
848# -------------------------- #
849
850# b4_value_type_setup_variant
851# ---------------------------
852# Setup support for api.value.type=variant.  By default, fail, specialized
853# by other skeletons.
854m4_define([b4_value_type_setup_variant],
855[b4_complain_at(b4_percent_define_get_loc([[api.value.type]]),
856                [['%s' does not support '%s']],
857                [b4_skeleton],
858                [%define api.value.type variant])])
859
860
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.
865m4_define([_b4_value_type_setup_keyword],
866[b4_percent_define_check_values([[[[api.value.type]],
867                                  [[none]],
868                                  [[union]],
869                                  [[union-directive]],
870                                  [[variant]],
871                                  [[yystype]]]])dnl
872m4_case(b4_percent_define_get([[api.value.type]]),
873        [union],   [b4_value_type_setup_union],
874        [variant], [b4_value_type_setup_variant])])
875
876
877# b4_value_type_setup
878# -------------------
879# Check if api.value.type is properly defined, and possibly prepare
880# its use.
881b4_define_silent([b4_value_type_setup],
882[# Define default value.
883b4_percent_define_ifdef([[api.value.type]], [],
884[# %union => api.value.type=union-directive
885m4_ifdef([b4_union_members],
886[m4_define([b4_percent_define_kind(api.value.type)], [keyword])
887m4_define([b4_percent_define(api.value.type)], [union-directive])],
888[# no tag seen => api.value.type={int}
889m4_if(b4_tag_seen_flag, 0,
890[m4_define([b4_percent_define_kind(api.value.type)], [code])
891m4_define([b4_percent_define(api.value.type)], [int])],
892[# otherwise api.value.type=yystype
893m4_define([b4_percent_define_kind(api.value.type)], [keyword])
894m4_define([b4_percent_define(api.value.type)], [yystype])])])])
895
896# Set up.
897m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
898   [keyword\|string], [_b4_value_type_setup_keyword])
899])
900
901
902## -------------- ##
903## Declarations.  ##
904## -------------- ##
905
906
907# b4_value_type_define
908# --------------------
909m4_define([b4_value_type_define],
910[b4_value_type_setup[]dnl
911/* Value type.  */
912m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
913[code],
914[[#if ! defined ]b4_api_PREFIX[STYPE && ! defined ]b4_api_PREFIX[STYPE_IS_DECLARED
915typedef ]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
918#endif
919]],
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]])[
925{
926]b4_user_union_members[
927};
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
932#endif
933]])])])
934
935
936# b4_location_type_define
937# -----------------------
938m4_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;
942]],
943[[#if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
944typedef struct ]b4_api_PREFIX[LTYPE ]b4_api_PREFIX[LTYPE;
945struct ]b4_api_PREFIX[LTYPE
946{
947  int first_line;
948  int first_column;
949  int last_line;
950  int last_column;
951};
952# define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
953# define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
954#endif
955]])])
956
957
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.
962m4_define([b4_declare_yylstype],
963[b4_value_type_define[]b4_locations_if([
964b4_location_type_define])
965
966b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
967]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
968])
969
970
971# b4_YYDEBUG_define
972# -----------------
973m4_define([b4_YYDEBUG_define],
974[[/* Debug traces.  */
975]m4_if(b4_api_prefix, [yy],
976[[#ifndef YYDEBUG
977# define YYDEBUG ]b4_parse_trace_if([1], [0])[
978#endif]],
979[[#ifndef ]b4_api_PREFIX[DEBUG
980# if defined YYDEBUG
981#if YYDEBUG
982#   define ]b4_api_PREFIX[DEBUG 1
983#  else
984#   define ]b4_api_PREFIX[DEBUG 0
985#  endif
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
990])
991
992# b4_declare_yydebug
993# ------------------
994m4_define([b4_declare_yydebug],
995[b4_YYDEBUG_define[
996#if ]b4_api_PREFIX[DEBUG
997extern int ]b4_prefix[debug;
998#endif][]dnl
999])
1000
1001# b4_yylloc_default_define
1002# ------------------------
1003# Define YYLLOC_DEFAULT.
1004m4_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).  */
1008
1009#ifndef YYLLOC_DEFAULT
1010# define YYLLOC_DEFAULT(Current, Rhs, N)                                \
1011    do                                                                  \
1012      if (N)                                                            \
1013        {                                                               \
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;       \
1018        }                                                               \
1019      else                                                              \
1020        {                                                               \
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;                              \
1025        }                                                               \
1026    while (0)
1027#endif
1028]])
1029
1030# b4_yy_location_print_define
1031# ---------------------------
1032# Define YY_LOCATION_PRINT.
1033m4_define([b4_yy_location_print_define],
1034[b4_locations_if([[
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.  */
1038
1039# ifndef YY_LOCATION_PRINT
1040#  if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1041
1042/* Print *YYLOCP on YYO.  Private, do not rely on its existence. */
1043
1044YY_ATTRIBUTE_UNUSED
1045static int
1046yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp)
1047{
1048  int res = 0;
1049  int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
1050  if (0 <= yylocp->first_line)
1051    {
1052      res += YYFPRINTF (yyo, "%d", yylocp->first_line);
1053      if (0 <= yylocp->first_column)
1054        res += YYFPRINTF (yyo, ".%d", yylocp->first_column);
1055    }
1056  if (0 <= yylocp->last_line)
1057    {
1058      if (yylocp->first_line < yylocp->last_line)
1059        {
1060          res += YYFPRINTF (yyo, "-%d", yylocp->last_line);
1061          if (0 <= end_col)
1062            res += YYFPRINTF (yyo, ".%d", end_col);
1063        }
1064      else if (0 <= end_col && yylocp->first_column < end_col)
1065        res += YYFPRINTF (yyo, "-%d", end_col);
1066    }
1067  return res;
1068 }
1069
1070#   define YY_LOCATION_PRINT(File, Loc)          \
1071  yy_location_print_ (File, &(Loc))
1072
1073#  else
1074#   define YY_LOCATION_PRINT(File, Loc) ((void) 0)
1075#  endif
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)
1080# endif]])
1081])
1082
1083# b4_yyloc_default
1084# ----------------
1085# Expand to a possible default value for yylloc.
1086m4_define([b4_yyloc_default],
1087[[
1088# if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1089  = { ]m4_join([, ],
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]))[ }
1094# endif
1095]])
1096