• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_yylex_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_yylex_formals],
115[b4_pure_if([[[b4_api_PREFIX[STYPE *yylvalp]], [[&yylval]]][]dnl
116b4_locations_if([, [b4_api_PREFIX[LTYPE *yyllocp], [&yylloc]]])])dnl
117m4_ifdef([b4_lex_param], [, ]b4_lex_param)])
118
119
120# b4_yylex
121# --------
122# Call yylex.
123m4_define([b4_yylex],
124[b4_function_call([yylex], [int], b4_yylex_formals)])
125
126
127# b4_user_args
128# ------------
129m4_define([b4_user_args],
130[m4_ifset([b4_parse_param], [, b4_user_args_no_comma])])
131
132# b4_user_args_no_comma
133# ---------------------
134m4_define([b4_user_args_no_comma],
135[m4_ifset([b4_parse_param], [b4_args(b4_parse_param)])])
136
137
138# b4_user_formals
139# ---------------
140# The possible parse-params formal arguments preceded by a comma.
141m4_define([b4_user_formals],
142[m4_ifset([b4_parse_param], [, b4_formals(b4_parse_param)])])
143
144
145# b4_parse_param
146# --------------
147# If defined, b4_parse_param arrives double quoted, but below we prefer
148# it to be single quoted.
149m4_define([b4_parse_param],
150b4_parse_param)
151
152
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.
157m4_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
160m4_pushdef([$2], m4_shift($1_$2))dnl
161$3[]dnl
162m4_popdef([$2])dnl
163m4_popdef([$1])dnl
164])])
165
166
167# b4_use(EXPR)
168# ------------
169# Pacify the compiler about some maybe unused value.
170m4_define([b4_use],
171[YY_USE ($1)])
172
173# b4_parse_param_use([VAL], [LOC])
174# --------------------------------
175# 'YY_USE' VAL, LOC if locations are enabled, and all the parse-params.
176m4_define([b4_parse_param_use],
177[m4_ifvaln([$1], [  b4_use([$1]);])dnl
178b4_locations_if([m4_ifvaln([$2], [  b4_use([$2]);])])dnl
179b4_parse_param_for([Decl], [Formal], [  b4_use(Formal);
180])dnl
181])
182
183
184## ------------ ##
185## Data Types.  ##
186## ------------ ##
187
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.
193m4_define([b4_int_type],
194[m4_if(b4_ints_in($@,   [-127],   [127]), [1], [signed char],
195       b4_ints_in($@,      [0],   [255]), [1], [unsigned char],
196
197       b4_ints_in($@, [-32767], [32767]), [1], [short],
198       b4_ints_in($@,      [0], [65535]), [1], [unsigned short],
199
200                                               [int])])
201
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.
206m4_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],
209
210       b4_ints_in($@, [-32767], [32767]), [1], [yytype_int16],
211       b4_ints_in($@,      [0], [65535]), [1], [yytype_uint16],
212
213                                               [int])])
214
215# b4_c99_int_type_define
216# ----------------------
217# Define private types suitable for holding small integers in C99 or later.
218m4_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
223
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.  */
227
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
235
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.  */
240
241#ifdef __INT_LEAST8_MAX__
242typedef __INT_LEAST8_TYPE__ yytype_int8;
243#elif defined YY_STDINT_H
244typedef int_least8_t yytype_int8;
245#else
246typedef signed char yytype_int8;
247#endif
248
249#ifdef __INT_LEAST16_MAX__
250typedef __INT_LEAST16_TYPE__ yytype_int16;
251#elif defined YY_STDINT_H
252typedef int_least16_t yytype_int16;
253#else
254typedef short yytype_int16;
255#endif
256
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
268
269#if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__
270typedef __UINT_LEAST8_TYPE__ yytype_uint8;
271#elif (!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H \
272       && UINT_LEAST8_MAX <= INT_MAX)
273typedef uint_least8_t yytype_uint8;
274#elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX
275typedef unsigned char yytype_uint8;
276#else
277typedef short yytype_uint8;
278#endif
279
280#if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__
281typedef __UINT_LEAST16_TYPE__ yytype_uint16;
282#elif (!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H \
283       && UINT_LEAST16_MAX <= INT_MAX)
284typedef uint_least16_t yytype_uint16;
285#elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX
286typedef unsigned short yytype_uint16;
287#else
288typedef int yytype_uint16;
289#endif]])
290
291
292# b4_sizes_types_define
293# ---------------------
294# Define YYPTRDIFF_T/YYPTRDIFF_MAXIMUM, YYSIZE_T/YYSIZE_MAXIMUM,
295# and YYSIZEOF.
296m4_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
312
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
325
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)))
331
332#define YYSIZEOF(X) YY_CAST (YYPTRDIFF_T, sizeof (X))
333]])
334
335
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).
340m4_define([b4_int_type_for],
341[b4_int_type($1_min, $1_max)])
342
343
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
349m4_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)])])
354
355
356## ----------------- ##
357## Compiler issues.  ##
358## ----------------- ##
359
360# b4_attribute_define([noreturn])
361# -------------------------------
362# Provide portable compiler "attributes".  If "noreturn" is passed, define
363# _Noreturn.
364m4_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
372
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
380
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
383dnl the use of [[noreturn]], because _Noreturn is used in places
384dnl where [[noreturn]] works in C++.  We need this in particular
385dnl because of glr.cc which compiles code from glr.c in C++.
386dnl And the C++ compiler chokes on _Noreturn.  Also, we do not
387dnl 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
410
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
417
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
442
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
454]])
455
456
457# b4_cast_define
458# --------------
459m4_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
469])
470
471
472# b4_null_define
473# --------------
474# Portability issues: define a YY_NULLPTR appropriate for the current
475# language (C, C++98, or C++11).
476#
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.
479m4_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
491])
492
493
494# b4_null
495# -------
496# Return a null pointer constant.
497m4_define([b4_null], [YY_NULLPTR])
498
499
500
501## ---------##
502## Values.  ##
503## ---------##
504
505# b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
506# -------------------------------------------------------------
507# Define "yy<TABLE-NAME>" whose contents is CONTENT.
508m4_define([b4_integral_parser_table_define],
509[m4_ifvaln([$3], [b4_comment([$3])])dnl
510static const b4_int_type_for([$2]) yy$1[[]] =
511{
512  $2
513};dnl
514])
515
516
517## ------------- ##
518## Token kinds.  ##
519## ------------- ##
520
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.
525m4_define([b4_symbol(-2, id)],  [b4_api_PREFIX[][EMPTY]])
526m4_define([b4_symbol(-2, tag)], [[No symbol.]])
527
528m4_if(b4_symbol(eof, id), [YYEOF],
529     [m4_define([b4_symbol(0, id)],  [b4_api_PREFIX[][EOF]])])
530m4_define([b4_symbol(1, id)],  [b4_api_PREFIX[][error]])
531m4_define([b4_symbol(2, id)],  [b4_api_PREFIX[][UNDEF]])
532
533
534# b4_token_define(TOKEN-NUM)
535# --------------------------
536# Output the definition of this token as #define.
537m4_define([b4_token_define],
538[b4_token_format([#define %s %s], [$1])])
539
540# b4_token_defines
541# ----------------
542# Output the definition of the tokens.
543m4_define([b4_token_defines],
544[[/* Token kinds.  */
545#define ]b4_symbol(empty, [id])[ -2
546]m4_join([
547], b4_symbol_map([b4_token_define]))
548])
549
550
551# b4_token_enum(TOKEN-NUM)
552# ------------------------
553# Output the definition of this token as an enum.
554m4_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])])])])
562
563
564# b4_token_enums
565# --------------
566# The definition of the token kinds.
567m4_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]])])
579
580
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.
585m4_define([b4_token_enums_defines],
586[b4_token_enums[]b4_yacc_if([b4_token_defines])])
587
588
589# b4_symbol_translate(STRING)
590# ---------------------------
591# Used by "bison" in the array of symbol names to mark those that
592# require translation.
593m4_define([b4_symbol_translate],
594[[N_($1)]])
595
596
597
598## -------------- ##
599## Symbol kinds.  ##
600## -------------- ##
601
602# b4_symbol_enum(SYMBOL-NUM)
603# --------------------------
604# Output the definition of this symbol as an enum.
605m4_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])])])
612
613
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.
619m4_define([b4_declare_symbol_enum],
620[[/* Symbol kind.  */
621enum yysymbol_kind_t
622{
623  ]b4_symbol(empty, [kind_base])[ = -2,
624]b4_symbol_foreach([b4_symbol_enum])dnl
625[};
626typedef enum yysymbol_kind_t yysymbol_kind_t;
627]])])
628
629
630## ----------------- ##
631## Semantic Values.  ##
632## ----------------- ##
633
634
635# b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])
636# ----------------------------------------------
637# See README.
638m4_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])])])
646
647
648## ---------------------- ##
649## Defining C functions.  ##
650## ---------------------- ##
651
652
653# b4_formals([DECL1, NAME1], ...)
654# -------------------------------
655# The formal arguments of a C function definition.
656m4_define([b4_formals],
657[m4_if([$#], [0], [void],
658       [$#$1], [1], [void],
659               [m4_map_sep([b4_formal], [, ], [$@])])])
660
661m4_define([b4_formal],
662[$1])
663
664
665# b4_function_declare(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
666# ------------------------------------------------------------
667# Declare the function NAME.
668m4_define([b4_function_declare],
669[$2 $1 (b4_formals(m4_shift2($@)));[]dnl
670])
671
672
673
674## --------------------- ##
675## Calling C functions.  ##
676## --------------------- ##
677
678
679# b4_function_call(NAME, RETURN-VALUE, [DECL1, NAME1], ...)
680# -----------------------------------------------------------
681# Call the function NAME with arguments NAME1, NAME2 etc.
682m4_define([b4_function_call],
683[$1 (b4_args(m4_shift2($@)))[]dnl
684])
685
686
687# b4_args([DECL1, NAME1], ...)
688# ----------------------------
689# Output the arguments NAME1, NAME2...
690m4_define([b4_args],
691[m4_map_sep([b4_arg], [, ], [$@])])
692
693m4_define([b4_arg],
694[$2])
695
696
697## ----------- ##
698## Synclines.  ##
699## ----------- ##
700
701# b4_sync_start(LINE, FILE)
702# -------------------------
703m4_define([b4_sync_start], [[#]line $1 $2])
704
705
706## -------------- ##
707## User actions.  ##
708## -------------- ##
709
710# b4_case(LABEL, STATEMENTS, [COMMENTS])
711# --------------------------------------
712m4_define([b4_case],
713[  case $1:m4_ifval([$3], [ b4_comment([$3])])
714$2
715b4_syncline([@oline@], [@ofile@])dnl
716    break;])
717
718
719# b4_predicate_case(LABEL, CONDITIONS)
720# ------------------------------------
721m4_define([b4_predicate_case],
722[  case $1:
723    if (! (
724$2)) YYERROR;
725b4_syncline([@oline@], [@ofile@])dnl
726    break;])
727
728
729# b4_yydestruct_define
730# --------------------
731# Define the "yydestruct" function.
732m4_define_default([b4_yydestruct_define],
733[[/*-----------------------------------------------.
734| Release the memory associated to this symbol.  |
735`-----------------------------------------------*/
736
737static void
738yydestruct (const char *yymsg,
739            yysymbol_kind_t yykind, YYSTYPE *yyvaluep]b4_locations_if(dnl
740[[, YYLTYPE *yylocationp]])[]b4_user_formals[)
741{
742]b4_parse_param_use([yyvaluep], [yylocationp])dnl
743[  if (!yymsg)
744    yymsg = "Deleting";
745  YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp);
746
747  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
748  ]b4_symbol_actions([destructor])[
749  YY_IGNORE_MAYBE_UNINITIALIZED_END
750}]dnl
751])
752
753
754# b4_yy_symbol_print_define
755# -------------------------
756# Define the "yy_symbol_print" function.
757m4_define_default([b4_yy_symbol_print_define],
758[[
759/*-----------------------------------.
760| Print this symbol's value on YYO.  |
761`-----------------------------------*/
762
763static void
764yy_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[)
767{
768  FILE *yyoutput = yyo;
769]b4_parse_param_use([yyoutput], [yylocationp])dnl
770[  if (!yyvaluep)
771    return;]
772b4_percent_code_get([[pre-printer]])dnl
773  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
774  b4_symbol_actions([printer])
775  YY_IGNORE_MAYBE_UNINITIALIZED_END
776b4_percent_code_get([[post-printer]])dnl
777[}
778
779
780/*---------------------------.
781| Print this symbol on YYO.  |
782`---------------------------*/
783
784static void
785yy_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[)
788{
789  YYFPRINTF (yyo, "%s %s (",
790             yykind < YYNTOKENS ? "token" : "nterm", yysymbol_name (yykind));
791
792]b4_locations_if([  YYLOCATION_PRINT (yyo, yylocationp);
793  YYFPRINTF (yyo, ": ");
794])dnl
795[  yy_symbol_value_print (yyo, yykind, yyvaluep]dnl
796b4_locations_if([, yylocationp])[]b4_user_args[);
797  YYFPRINTF (yyo, ")");
798}]dnl
799])
800
801
802## ---------------- ##
803## api.value.type.  ##
804## ---------------- ##
805
806
807# ---------------------- #
808# api.value.type=union.  #
809# ---------------------- #
810
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.
817m4_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
822m4_append([b4_union_members],
823m4_expand([m4_format([  %-40s %s],
824                     m4_expand([b4_symbol([$1], [type]) b4_symbol([$1], [type_tag]);]),
825                     [b4_symbol_tag_comment([$1])])]))
826])
827
828
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.
834m4_define([b4_type_define_tag],
835[b4_symbol_if([$1], [has_type],
836              [m4_map([b4_symbol_type_register], [$@])])
837])
838
839
840# b4_symbol_value_union(VAL, SYMBOL-NUM, [TYPE])
841# ----------------------------------------------
842# Same of b4_symbol_value, but when api.value.type=union.
843m4_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])])])
851
852
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.
858m4_define([b4_value_type_setup_union],
859[m4_define([b4_union_members])
860b4_type_foreach([b4_type_define_tag])
861m4_copy_force([b4_symbol_value_union], [b4_symbol_value])
862])
863
864
865# -------------------------- #
866# api.value.type = variant.  #
867# -------------------------- #
868
869# b4_value_type_setup_variant
870# ---------------------------
871# Setup support for api.value.type=variant.  By default, fail, specialized
872# by other skeletons.
873m4_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])])
878
879
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.
884m4_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
891m4_case(b4_percent_define_get([[api.value.type]]),
892        [union],   [b4_value_type_setup_union],
893        [variant], [b4_value_type_setup_variant])])
894
895
896# b4_value_type_setup
897# -------------------
898# Check if api.value.type is properly defined, and possibly prepare
899# its use.
900b4_define_silent([b4_value_type_setup],
901[# Define default value.
902b4_percent_define_ifdef([[api.value.type]], [],
903[# %union => api.value.type=union-directive
904m4_ifdef([b4_union_members],
905[m4_define([b4_percent_define_kind(api.value.type)], [keyword])
906m4_define([b4_percent_define(api.value.type)], [union-directive])],
907[# no tag seen => api.value.type={int}
908m4_if(b4_tag_seen_flag, 0,
909[m4_define([b4_percent_define_kind(api.value.type)], [code])
910m4_define([b4_percent_define(api.value.type)], [int])],
911[# otherwise api.value.type=yystype
912m4_define([b4_percent_define_kind(api.value.type)], [keyword])
913m4_define([b4_percent_define(api.value.type)], [yystype])])])])
914
915# Set up.
916m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
917   [keyword\|string], [_b4_value_type_setup_keyword])
918])
919
920
921## -------------- ##
922## Declarations.  ##
923## -------------- ##
924
925
926# b4_value_type_define
927# --------------------
928m4_define([b4_value_type_define],
929[b4_value_type_setup[]dnl
930/* Value type.  */
931m4_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
934typedef ]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
938]],
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]])[
944{
945]b4_user_union_members[
946};
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]])])])
953
954
955# b4_location_type_define
956# -----------------------
957m4_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;
961]],
962[[#if ! defined ]b4_api_PREFIX[LTYPE && ! defined ]b4_api_PREFIX[LTYPE_IS_DECLARED
963typedef struct ]b4_api_PREFIX[LTYPE ]b4_api_PREFIX[LTYPE;
964struct ]b4_api_PREFIX[LTYPE
965{
966  int first_line;
967  int first_column;
968  int last_line;
969  int last_column;
970};
971# define ]b4_api_PREFIX[LTYPE_IS_DECLARED 1
972# define ]b4_api_PREFIX[LTYPE_IS_TRIVIAL 1
973#endif
974]])])
975
976
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.
981m4_define([b4_declare_yylstype],
982[b4_value_type_define[]b4_locations_if([
983b4_location_type_define])
984
985b4_pure_if([], [[extern ]b4_api_PREFIX[STYPE ]b4_prefix[lval;
986]b4_locations_if([[extern ]b4_api_PREFIX[LTYPE ]b4_prefix[lloc;]])])[]dnl
987])
988
989
990# b4_YYDEBUG_define
991# -----------------
992m4_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
1009])
1010
1011# b4_declare_yydebug
1012# ------------------
1013m4_define([b4_declare_yydebug],
1014[b4_YYDEBUG_define[
1015#if ]b4_api_PREFIX[DEBUG
1016extern int ]b4_prefix[debug;
1017#endif][]dnl
1018])
1019
1020# b4_yylloc_default_define
1021# ------------------------
1022# Define YYLLOC_DEFAULT.
1023m4_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).  */
1027
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
1047]])
1048
1049# b4_yylocation_print_define
1050# --------------------------
1051# Define YYLOCATION_PRINT.
1052m4_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.  */
1057
1058# ifndef YYLOCATION_PRINT
1059
1060#  if defined YY_LOCATION_PRINT
1061
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))
1065
1066#  elif defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
1067
1068/* Print *YYLOCP on YYO.  Private, do not rely on its existence. */
1069
1070YY_ATTRIBUTE_UNUSED
1071static int
1072yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp)
1073{
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;
1094}
1095
1096#   define YYLOCATION_PRINT  yy_location_print_
1097
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))
1101
1102#  else
1103
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
1108
1109#  endif
1110# endif /* !defined YYLOCATION_PRINT */]])
1111])
1112
1113# b4_yyloc_default
1114# ----------------
1115# Expand to a possible default value for yylloc.
1116m4_define([b4_yyloc_default],
1117[[
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
1125]])
1126